Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "ca-module.hpp" |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 22 | #include "challenge/challenge-pin.hpp" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 23 | #include "detail/info-encoder.hpp" |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 24 | #include "requester.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 25 | #include "test-common.hpp" |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace tests { |
| 30 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 31 | BOOST_FIXTURE_TEST_SUITE(TestForBenchmark, IdentityManagementTimeFixture) |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 32 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(PacketSize0) |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 34 | { |
Zhiyi Zhang | 615b6b7 | 2021-01-11 14:25:32 -0800 | [diff] [blame^] | 35 | name::setConventionEncoding(name::Convention::TYPED); |
| 36 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 37 | auto identity = addIdentity(Name("/ndn")); |
| 38 | auto key = identity.getDefaultKey(); |
| 39 | auto cert = key.getDefaultCertificate(); |
| 40 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 41 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 42 | ca::CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 43 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 44 | auto profileData = ca.getCaProfileData(); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 45 | |
| 46 | Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO")); |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 47 | // std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 48 | shared_ptr<Interest> infoInterest = nullptr; |
| 49 | |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 50 | face.setInterestFilter( |
| 51 | InterestFilter("/ndn/CA/INFO"), |
| 52 | [&](const auto&, const Interest& interest) { |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 53 | // std::cout << interest.getName() << std::endl; |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 54 | if (interest.getName() == profileData.getName()) { |
| 55 | face.put(profileData); |
| 56 | } |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 57 | }, |
| 58 | nullptr, nullptr); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 59 | advanceClocks(time::milliseconds(20), 60); |
| 60 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 61 | int count = 0; |
| 62 | face.onSendData.connect([&](const Data& response) { |
| 63 | if (count == 0) { |
| 64 | count++; |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 65 | // std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 66 | auto block = response.getContent(); |
| 67 | block.parse(); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 68 | infoInterest =std::make_shared<Interest>(Name(block.get(ndn::tlv::Name)).appendSegment(0)); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 69 | infoInterest->setCanBePrefix(false); |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 70 | // std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 71 | } |
| 72 | else { |
| 73 | count++; |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 74 | // std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 75 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 76 | auto contentBlock = response.getContent(); |
| 77 | contentBlock.parse(); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 78 | auto caItem = infotlv::decodeDataContent(contentBlock); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(caItem.caPrefix, "/ndn"); |
| 80 | BOOST_CHECK_EQUAL(caItem.probeParameterKeys.size(), 1); |
| 81 | BOOST_CHECK_EQUAL(caItem.probeParameterKeys.front(), "full name"); |
| 82 | BOOST_CHECK_EQUAL(caItem.cert->wireEncode(), cert.wireEncode()); |
| 83 | BOOST_CHECK_EQUAL(caItem.caInfo, "ndn testbed ca"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 84 | } |
| 85 | }); |
| 86 | face.receive(interest); |
| 87 | advanceClocks(time::milliseconds(20), 60); |
| 88 | face.receive(*infoInterest); |
| 89 | advanceClocks(time::milliseconds(20), 60); |
| 90 | |
| 91 | BOOST_CHECK_EQUAL(count, 2); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(PacketSize1) |
| 95 | { |
| 96 | auto identity = addIdentity(Name("/ndn")); |
| 97 | auto key = identity.getDefaultKey(); |
| 98 | auto cert = key.getDefaultCertificate(); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 99 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 100 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 101 | ca::CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 102 | advanceClocks(time::milliseconds(20), 60); |
| 103 | |
| 104 | // generate NEW Interest |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 105 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 106 | item.caPrefix = Name("/ndn"); |
| 107 | item.cert = std::make_shared<security::Certificate>(cert); |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 108 | requester::RequestState state(m_keyChain, item, RequestType::NEW); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 109 | auto newInterest = requester::Requester::genNewInterest(state, Name("/ndn/alice"), |
Zhiyi Zhang | 4f1c010 | 2020-12-21 15:08:09 -0800 | [diff] [blame] | 110 | time::system_clock::now(), |
| 111 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 112 | |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 113 | // std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 114 | |
| 115 | // generate CHALLENGE Interest |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 116 | shared_ptr<Interest> challengeInterest = nullptr; |
| 117 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 118 | shared_ptr<Interest> challengeInterest3 = nullptr; |
| 119 | |
| 120 | int count = 0; |
| 121 | face.onSendData.connect([&](const Data& response) { |
| 122 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 123 | // std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl; |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 124 | auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, response); |
| 125 | auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin"); |
| 126 | challengeInterest = requester::Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 127 | } |
| 128 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
| 129 | count++; |
| 130 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 131 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 132 | requester::Requester::onChallengeResponse(state, response); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 133 | BOOST_CHECK(state.status == Status::CHALLENGE); |
| 134 | BOOST_CHECK_EQUAL(state.challengeStatus, ChallengePin::NEED_CODE); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 135 | auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin"); |
| 136 | challengeInterest2 = requester::Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 137 | } |
| 138 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
| 139 | count++; |
| 140 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 141 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 142 | requester::Requester::onChallengeResponse(state, response); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 143 | BOOST_CHECK(state.status == Status::CHALLENGE); |
| 144 | BOOST_CHECK_EQUAL(state.challengeStatus, ChallengePin::WRONG_CODE); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 145 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 146 | auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 147 | auto request = ca.getCertificateRequest(*challengeInterest2); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 148 | auto secret = request->challengeState->secrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 149 | paramList.begin()->second = secret; |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 150 | challengeInterest3 = requester::Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 151 | // std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 152 | } |
| 153 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
Zhiyi Zhang | aeab497 | 2020-10-22 22:20:40 -0700 | [diff] [blame] | 154 | // std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 155 | count++; |
| 156 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 157 | requester::Requester::onChallengeResponse(state, response); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 158 | BOOST_CHECK(state.status == Status::SUCCESS); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 159 | } |
| 160 | }); |
| 161 | |
| 162 | face.receive(*newInterest); |
| 163 | advanceClocks(time::milliseconds(20), 60); |
| 164 | face.receive(*challengeInterest); |
| 165 | advanceClocks(time::milliseconds(20), 60); |
| 166 | face.receive(*challengeInterest2); |
| 167 | advanceClocks(time::milliseconds(20), 60); |
| 168 | face.receive(*challengeInterest3); |
| 169 | advanceClocks(time::milliseconds(20), 60); |
| 170 | BOOST_CHECK_EQUAL(count, 3); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() // TestCaConfig |
| 174 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 175 | } // namespace tests |
| 176 | } // namespace ndncert |
| 177 | } // namespace ndn |