Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2017-2019, Regents of the University of California. |
| 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 | dbd9d43 | 2020-10-07 15:56:27 -0700 | [diff] [blame^] | 22 | #include "identity-challenge/challenge-pin.hpp" |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 23 | #include "protocol-detail/info.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 | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 35 | auto identity = addIdentity(Name("/ndn")); |
| 36 | auto key = identity.getDefaultKey(); |
| 37 | auto cert = key.getDefaultCertificate(); |
| 38 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 39 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 40 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | f61404d | 2020-10-07 15:41:07 -0700 | [diff] [blame] | 41 | auto metaData = ca.generateCaProfileMetaData(); |
| 42 | auto profileData = ca.generateCaProfileData(); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 43 | advanceClocks(time::milliseconds(20), 60); |
| 44 | |
| 45 | Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO")); |
| 46 | std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl; |
| 47 | shared_ptr<Interest> infoInterest = nullptr; |
| 48 | |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 49 | face.setInterestFilter(InterestFilter("/ndn/CA/INFO"), |
| 50 | [&](const auto&, const Interest& interest) { |
| 51 | if (interest.matchesData(*metaData)) { |
| 52 | face.put(*metaData); |
| 53 | } |
| 54 | else { |
| 55 | BOOST_CHECK(interest.matchesData(*profileData)); |
| 56 | face.put(*profileData); |
| 57 | } |
| 58 | }, nullptr, nullptr); |
| 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++; |
| 65 | std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl; |
| 66 | auto block = response.getContent(); |
| 67 | block.parse(); |
| 68 | Interest interest(Name(block.get(tlv::Name))); |
| 69 | interest.setCanBePrefix(true); |
| 70 | infoInterest = make_shared<Interest>(interest); |
| 71 | std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 72 | } |
| 73 | else { |
| 74 | count++; |
| 75 | std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl; |
| 76 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 77 | auto contentBlock = response.getContent(); |
| 78 | contentBlock.parse(); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 79 | auto caItem = INFO::decodeDataContent(contentBlock); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn"); |
Zhiyi Zhang | b940aa1 | 2020-09-30 16:38:57 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1); |
| 82 | BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.front(), "full name"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(caItem.m_cert->wireEncode(), cert.wireEncode()); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca"); |
| 85 | } |
| 86 | }); |
| 87 | face.receive(interest); |
| 88 | advanceClocks(time::milliseconds(20), 60); |
| 89 | face.receive(*infoInterest); |
| 90 | advanceClocks(time::milliseconds(20), 60); |
| 91 | |
| 92 | BOOST_CHECK_EQUAL(count, 2); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(PacketSize1) |
| 96 | { |
| 97 | auto identity = addIdentity(Name("/ndn")); |
| 98 | auto key = identity.getDefaultKey(); |
| 99 | auto cert = key.getDefaultCertificate(); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 100 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 101 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 102 | 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] | 103 | advanceClocks(time::milliseconds(20), 60); |
| 104 | |
| 105 | // generate NEW Interest |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 106 | CaProfile item; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 107 | item.m_caPrefix = Name("/ndn"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 108 | item.m_cert = std::make_shared<security::v2::Certificate>(cert); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 109 | RequesterState state(m_keyChain, item, RequestType::NEW); |
| 110 | auto newInterest = Requester::genNewInterest(state, Name("/ndn/alice"), |
| 111 | time::system_clock::now(), |
| 112 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 113 | |
| 114 | std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl; |
| 115 | |
| 116 | // generate CHALLENGE Interest |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 117 | shared_ptr<Interest> challengeInterest = nullptr; |
| 118 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 119 | shared_ptr<Interest> challengeInterest3 = nullptr; |
| 120 | |
| 121 | int count = 0; |
| 122 | face.onSendData.connect([&](const Data& response) { |
| 123 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
| 124 | std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 125 | auto challengeList = Requester::onNewRenewRevokeResponse(state, response); |
| 126 | auto paramList = Requester::selectOrContinueChallenge(state, "pin"); |
| 127 | challengeInterest = Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 128 | } |
| 129 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
| 130 | count++; |
| 131 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 132 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 133 | Requester::onChallengeResponse(state, response); |
| 134 | BOOST_CHECK(state.m_status == Status::CHALLENGE); |
| 135 | BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE); |
| 136 | auto paramList = Requester::selectOrContinueChallenge(state, "pin"); |
| 137 | challengeInterest2 = Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 138 | } |
| 139 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
| 140 | count++; |
| 141 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 142 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 143 | Requester::onChallengeResponse(state, response); |
| 144 | BOOST_CHECK(state.m_status == Status::CHALLENGE); |
| 145 | BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 146 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 147 | auto paramList = Requester::selectOrContinueChallenge(state, "pin"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 148 | auto request = ca.getCertificateRequest(*challengeInterest2); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 149 | auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 150 | std::get<1>(paramList[0]) = secret; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 151 | challengeInterest3 = Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 152 | std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl; |
| 153 | } |
| 154 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
| 155 | std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl; |
| 156 | count++; |
| 157 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 158 | Requester::onChallengeResponse(state, response); |
| 159 | BOOST_CHECK(state.m_status == Status::SUCCESS); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 160 | } |
| 161 | }); |
| 162 | |
| 163 | face.receive(*newInterest); |
| 164 | advanceClocks(time::milliseconds(20), 60); |
| 165 | face.receive(*challengeInterest); |
| 166 | advanceClocks(time::milliseconds(20), 60); |
| 167 | face.receive(*challengeInterest2); |
| 168 | advanceClocks(time::milliseconds(20), 60); |
| 169 | face.receive(*challengeInterest3); |
| 170 | advanceClocks(time::milliseconds(20), 60); |
| 171 | BOOST_CHECK_EQUAL(count, 3); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_SUITE_END() // TestCaConfig |
| 175 | |
| 176 | } // namespace tests |
| 177 | } // namespace ndncert |
| 178 | } // namespace ndn |