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