Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 2 | /* |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [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 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 21 | #include "ca-module.hpp" |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 22 | #include "challenge/challenge-module.hpp" |
| 23 | #include "challenge/challenge-email.hpp" |
| 24 | #include "challenge/challenge-pin.hpp" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 25 | #include "detail/info-encoder.hpp" |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 26 | #include "requester-request.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 27 | #include "test-common.hpp" |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 28 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 29 | namespace ndncert { |
| 30 | namespace tests { |
| 31 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 32 | using namespace ca; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 33 | using ndn::util::DummyClientFace; |
| 34 | using ndn::security::verifySignature; |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 35 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_CASE(Initialization) |
| 39 | { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 40 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 41 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(ca.getCaConf().caProfile.caPrefix, "/ndn"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 43 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 44 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 1); // removed local discovery registration |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 5); // infoMeta, onProbe, onNew, onChallenge, onRevoke |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 49 | BOOST_AUTO_TEST_CASE(HandleProfileFetching) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 50 | { |
| 51 | auto identity = addIdentity(Name("/ndn")); |
| 52 | auto key = identity.getDefaultKey(); |
| 53 | auto cert = key.getDefaultCertificate(); |
| 54 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 55 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 56 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 57 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 58 | auto profileData = ca.getCaProfileData(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 60 | Interest interest = ndn::MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO")); |
| 61 | std::shared_ptr<Interest> infoInterest = nullptr; |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 62 | |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 63 | face.setInterestFilter( |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 64 | ndn::InterestFilter("/ndn/CA/INFO"), |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 65 | [&](const auto&, const Interest& interest) { |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 66 | if (interest.getName() == profileData.getName()) { |
| 67 | face.put(profileData); |
| 68 | } |
| 69 | }, |
| 70 | nullptr, nullptr); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 71 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 72 | |
| 73 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 74 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 75 | if (count == 0) { |
| 76 | count++; |
| 77 | auto block = response.getContent(); |
| 78 | block.parse(); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 79 | infoInterest = std::make_shared<Interest>(Name(block.get(ndn::tlv::Name)).appendSegment(0)); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 80 | } |
| 81 | else { |
| 82 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 83 | BOOST_CHECK(verifySignature(response, cert)); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 84 | auto contentBlock = response.getContent(); |
| 85 | contentBlock.parse(); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 86 | auto caItem = infotlv::decodeDataContent(contentBlock); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(caItem.caPrefix, "/ndn"); |
| 88 | BOOST_CHECK_EQUAL(caItem.probeParameterKeys.size(), 1); |
| 89 | BOOST_CHECK_EQUAL(caItem.probeParameterKeys.front(), "full name"); |
| 90 | BOOST_CHECK_EQUAL(caItem.cert->wireEncode(), cert.wireEncode()); |
| 91 | BOOST_CHECK_EQUAL(caItem.caInfo, "ndn testbed ca"); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 92 | } |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 93 | }); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 94 | face.receive(interest); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 95 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 96 | face.receive(*infoInterest); |
| 97 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 98 | |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(count, 2); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 102 | BOOST_AUTO_TEST_CASE(HandleProbe) |
| 103 | { |
| 104 | auto identity = addIdentity(Name("/ndn")); |
| 105 | auto key = identity.getDefaultKey(); |
| 106 | auto cert = key.getDefaultCertificate(); |
| 107 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 108 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 109 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 110 | advanceClocks(time::milliseconds(20), 60); |
| 111 | |
| 112 | Interest interest("/ndn/CA/PROBE"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 113 | Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters); |
| 114 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name")); |
| 115 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi")); |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 116 | paramTLV.encode(); |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 117 | interest.setApplicationParameters(paramTLV); |
| 118 | |
| 119 | int count = 0; |
| 120 | face.onSendData.connect([&](const Data& response) { |
| 121 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 122 | BOOST_CHECK(verifySignature(response, cert)); |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 123 | Block contentBlock = response.getContent(); |
| 124 | contentBlock.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 125 | Block probeResponse = contentBlock.get(tlv::ProbeResponse); |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 126 | probeResponse.parse(); |
| 127 | Name caName; |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 128 | caName.wireDecode(probeResponse.get(ndn::tlv::Name)); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(caName.size(), 2); |
Zhiyi Zhang | 6f39581 | 2020-09-29 14:42:03 -0700 | [diff] [blame] | 130 | }); |
| 131 | face.receive(interest); |
| 132 | |
| 133 | advanceClocks(time::milliseconds(20), 60); |
| 134 | BOOST_CHECK_EQUAL(count, 1); |
| 135 | } |
| 136 | |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 137 | BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler) |
| 138 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 139 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 140 | auto key = identity.getDefaultKey(); |
| 141 | auto cert = key.getDefaultCertificate(); |
| 142 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 143 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 144 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 145 | advanceClocks(time::milliseconds(20), 60); |
| 146 | |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 147 | Interest interest("/ndn/CA/PROBE"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 148 | Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters); |
| 149 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name")); |
| 150 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi")); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 151 | paramTLV.encode(); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 152 | interest.setApplicationParameters(paramTLV); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 153 | |
| 154 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 155 | face.onSendData.connect([&](const Data& response) { |
| 156 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 157 | BOOST_CHECK(verifySignature(response, cert)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 158 | auto contentBlock = response.getContent(); |
| 159 | contentBlock.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 160 | auto probeResponseBlock = contentBlock.get(tlv::ProbeResponse); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 161 | probeResponseBlock.parse(); |
| 162 | Name caPrefix; |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 163 | caPrefix.wireDecode(probeResponseBlock.get(ndn::tlv::Name)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 164 | BOOST_CHECK(caPrefix != ""); |
| 165 | }); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 166 | face.receive(interest); |
| 167 | |
| 168 | advanceClocks(time::milliseconds(20), 60); |
| 169 | BOOST_CHECK_EQUAL(count, 1); |
| 170 | } |
| 171 | |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 172 | BOOST_AUTO_TEST_CASE(HandleProbeRedirection) |
| 173 | { |
| 174 | auto identity = addIdentity(Name("/ndn")); |
| 175 | auto key = identity.getDefaultKey(); |
| 176 | auto cert = key.getDefaultCertificate(); |
| 177 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 178 | DummyClientFace face(io, m_keyChain, {true, true}); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 179 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory"); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 180 | advanceClocks(time::milliseconds(20), 60); |
| 181 | |
| 182 | Interest interest("/ndn/CA/PROBE"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 183 | Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters); |
| 184 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name")); |
| 185 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi")); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 186 | paramTLV.encode(); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 187 | interest.setApplicationParameters(paramTLV); |
| 188 | |
| 189 | int count = 0; |
| 190 | face.onSendData.connect([&](const Data& response) { |
| 191 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 192 | BOOST_CHECK(verifySignature(response, cert)); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 193 | Block contentBlock = response.getContent(); |
| 194 | contentBlock.parse(); |
| 195 | |
| 196 | // Test CA sent redirections |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 197 | std::vector<Name> redirectionItems; |
| 198 | for (auto item : contentBlock.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 199 | if (item.type() == tlv::ProbeRedirect) { |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 200 | redirectionItems.push_back(Name(item.blockFromValue())); |
| 201 | } |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 202 | } |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(redirectionItems.size(), 2); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 204 | BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirectionItems[0].getPrefix(-1)), "/ndn/edu/ucla"); |
| 205 | BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirectionItems[1].getPrefix(-1)), "/ndn/edu/ucla/cs/irl"); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 206 | }); |
| 207 | face.receive(interest); |
| 208 | advanceClocks(time::milliseconds(20), 60); |
| 209 | BOOST_CHECK_EQUAL(count, 1); |
| 210 | } |
| 211 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 212 | BOOST_AUTO_TEST_CASE(HandleNew) |
| 213 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 214 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 215 | auto key = identity.getDefaultKey(); |
| 216 | auto cert = key.getDefaultCertificate(); |
| 217 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 218 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 219 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 220 | advanceClocks(time::milliseconds(20), 60); |
| 221 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 222 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 223 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 224 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 225 | requester::Request state(m_keyChain, item, RequestType::NEW); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 226 | auto interest = state.genNewInterest(addIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(), |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 227 | time::system_clock::now(), |
| 228 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 229 | |
| 230 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 231 | face.onSendData.connect([&](const Data& response) { |
| 232 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 233 | BOOST_CHECK(verifySignature(response, cert)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 234 | auto contentBlock = response.getContent(); |
| 235 | contentBlock.parse(); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 236 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 237 | BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != ""); |
| 238 | BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != ""); |
| 239 | BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != ""); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 240 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 241 | auto challengeBlockCount = 0; |
| 242 | for (auto const& element : contentBlock.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 243 | if (element.type() == tlv::Challenge) { |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 244 | challengeBlockCount++; |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 245 | } |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 246 | } |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 247 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 248 | BOOST_CHECK(challengeBlockCount != 0); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 249 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 250 | auto challengeList = state.onNewRenewRevokeResponse(response); |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 251 | RequestId requestId; |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 252 | std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size()); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 253 | auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey; |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL_COLLECTIONS(state.m_aesKey.begin(), state.m_aesKey.end(), |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 255 | ca_encryption_key.begin(), ca_encryption_key.end()); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 256 | }); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 257 | face.receive(*interest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 258 | |
| 259 | advanceClocks(time::milliseconds(20), 60); |
| 260 | BOOST_CHECK_EQUAL(count, 1); |
| 261 | } |
| 262 | |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 263 | BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1) |
| 264 | { |
| 265 | auto identity = addIdentity(Name("/ndn")); |
| 266 | auto key = identity.getDefaultKey(); |
| 267 | auto cert = key.getDefaultCertificate(); |
| 268 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 269 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 270 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1"); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 271 | advanceClocks(time::milliseconds(20), 60); |
| 272 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 273 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 274 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 275 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 276 | requester::Request state(m_keyChain, item, RequestType::NEW); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 277 | auto client = addIdentity(Name("/ndn/zhiyi")); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 278 | auto current_tp = time::system_clock::now(); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 279 | auto interest1 = state.genNewInterest(client.getDefaultKey().getName(), current_tp, current_tp - time::hours(1)); |
| 280 | auto interest2 = state.genNewInterest(client.getDefaultKey().getName(), current_tp, current_tp + time::days(361)); |
| 281 | auto interest3 = state.genNewInterest(client.getDefaultKey().getName(), |
| 282 | current_tp - time::hours(1), current_tp + time::hours(2)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 283 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 284 | auto contentTlv = response.getContent(); |
| 285 | contentTlv.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 286 | auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 287 | BOOST_CHECK(errorCode != ErrorCode::NO_ERROR); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 288 | }); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 289 | face.receive(*interest1); |
| 290 | face.receive(*interest2); |
| 291 | face.receive(*interest3); |
| 292 | |
| 293 | advanceClocks(time::milliseconds(20), 60); |
| 294 | } |
| 295 | |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 296 | BOOST_AUTO_TEST_CASE(HandleNewWithServerBadValidity) |
| 297 | { |
| 298 | auto identity = addIdentity(Name("/ndn")); |
| 299 | auto key = identity.getDefaultKey(); |
| 300 | |
| 301 | //build expired cert |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 302 | Certificate cert; |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 303 | cert.setName(Name(key.getName()).append("self-sign").appendVersion()); |
| 304 | cert.setContentType(ndn::tlv::ContentType_Key); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 305 | cert.setContent(key.getPublicKey()); |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 306 | SignatureInfo signatureInfo; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 307 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now() - time::days(1), |
| 308 | time::system_clock::now() - time::seconds(1))); |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 309 | m_keyChain.sign(cert, signingByKey(key.getName()).setSignatureInfo(signatureInfo)); |
| 310 | m_keyChain.setDefaultCertificate(key, cert); |
| 311 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 312 | DummyClientFace face(io, m_keyChain, {true, true}); |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 313 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
| 314 | advanceClocks(time::milliseconds(20), 60); |
| 315 | |
| 316 | CaProfile item; |
| 317 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 318 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 319 | requester::Request state(m_keyChain, item, RequestType::NEW); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 320 | auto interest = state.genNewInterest(addIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(), |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame] | 321 | time::system_clock::now(), |
| 322 | time::system_clock::now() + time::days(1)); |
| 323 | |
| 324 | int count = 0; |
| 325 | face.onSendData.connect([&](const Data& response) { |
| 326 | auto contentTlv = response.getContent(); |
| 327 | contentTlv.parse(); |
| 328 | auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))); |
| 329 | BOOST_CHECK(errorCode != ErrorCode::NO_ERROR); |
| 330 | count ++; |
| 331 | }); |
| 332 | face.receive(*interest); |
| 333 | |
| 334 | advanceClocks(time::milliseconds(20), 60); |
| 335 | BOOST_CHECK_EQUAL(count, 1); |
| 336 | } |
| 337 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 338 | BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix) |
| 339 | { |
| 340 | auto identity = addIdentity(Name("/ndn")); |
| 341 | auto key = identity.getDefaultKey(); |
| 342 | auto cert = key.getDefaultCertificate(); |
| 343 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 344 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 345 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 346 | advanceClocks(time::milliseconds(20), 60); |
| 347 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 348 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 349 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 350 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 351 | requester::Request state(m_keyChain, item, RequestType::NEW); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 352 | |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 353 | auto interest1 = state.genNewInterest(addIdentity(Name("/ndn/a")).getDefaultKey().getName(), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 354 | time::system_clock::now() + time::days(1)); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 355 | auto interest2 = state.genNewInterest(addIdentity(Name("/ndn/a/b")).getDefaultKey().getName(), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 356 | time::system_clock::now() + time::days(1)); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 357 | auto interest3 = state.genNewInterest(addIdentity(Name("/ndn/a/b/c/d")).getDefaultKey().getName(), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 358 | time::system_clock::now() + time::days(1)); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 359 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 360 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 361 | auto contentTlv = response.getContent(); |
| 362 | contentTlv.parse(); |
| 363 | if (interest3->getName().isPrefixOf(response.getName())) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 364 | auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 365 | BOOST_CHECK(errorCode != ErrorCode::NO_ERROR); |
| 366 | } |
| 367 | else { |
| 368 | // should successfully get responses |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 369 | BOOST_CHECK_THROW(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)), std::runtime_error); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 370 | } |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 371 | }); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 372 | face.receive(*interest1); |
| 373 | face.receive(*interest2); |
| 374 | face.receive(*interest3); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 375 | advanceClocks(time::milliseconds(20), 60); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 376 | } |
| 377 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 378 | BOOST_AUTO_TEST_CASE(HandleNewWithInvalidLength1) |
| 379 | { |
| 380 | auto identity = addIdentity(Name("/ndn")); |
| 381 | auto key = identity.getDefaultKey(); |
| 382 | auto cert = key.getDefaultCertificate(); |
| 383 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 384 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 385 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1"); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 386 | advanceClocks(time::milliseconds(20), 60); |
| 387 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 388 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 389 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 390 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 391 | requester::Request state(m_keyChain, item, RequestType::NEW); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 392 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 393 | auto current_tp = time::system_clock::now(); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 394 | auto interest1 = state.genNewInterest(identity.getDefaultKey().getName(), current_tp, current_tp + time::days(1)); |
| 395 | auto interest2 = state.genNewInterest(addIdentity(Name("/ndn/a/b/c/d")).getDefaultKey().getName(), |
| 396 | current_tp, current_tp + time::days(1)); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 397 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 398 | auto contentTlv = response.getContent(); |
| 399 | contentTlv.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 400 | auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 401 | BOOST_CHECK(errorCode != ErrorCode::NO_ERROR); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 402 | }); |
| 403 | face.receive(*interest1); |
| 404 | face.receive(*interest2); |
| 405 | |
| 406 | advanceClocks(time::milliseconds(20), 60); |
| 407 | } |
| 408 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 409 | BOOST_AUTO_TEST_CASE(HandleChallenge) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 410 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 411 | auto identity = addIdentity(Name("/ndn")); |
| 412 | auto key = identity.getDefaultKey(); |
| 413 | auto cert = key.getDefaultCertificate(); |
| 414 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 415 | DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 416 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 417 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 418 | |
| 419 | // generate NEW Interest |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 420 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 421 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 422 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 423 | requester::Request state(m_keyChain, item, RequestType::NEW); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 424 | |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 425 | auto newInterest = state.genNewInterest(addIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 426 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 427 | |
| 428 | // generate CHALLENGE Interest |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 429 | std::shared_ptr<Interest> challengeInterest; |
| 430 | std::shared_ptr<Interest> challengeInterest2; |
| 431 | std::shared_ptr<Interest> challengeInterest3; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 432 | |
| 433 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 434 | face.onSendData.connect([&](const Data& response) { |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 435 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 436 | auto challengeList = state.onNewRenewRevokeResponse(response); |
| 437 | auto paramList = state.selectOrContinueChallenge("pin"); |
| 438 | challengeInterest = state.genChallengeInterest(std::move(paramList)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 439 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 440 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 441 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 442 | BOOST_CHECK(verifySignature(response, cert)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 443 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 444 | state.onChallengeResponse(response); |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 445 | BOOST_CHECK(state.m_status == Status::CHALLENGE); |
| 446 | BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 447 | auto paramList = state.selectOrContinueChallenge("pin"); |
| 448 | challengeInterest2 = state.genChallengeInterest(std::move(paramList)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 449 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 450 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 451 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 452 | BOOST_CHECK(verifySignature(response, cert)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 453 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 454 | state.onChallengeResponse(response); |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 455 | BOOST_CHECK(state.m_status == Status::CHALLENGE); |
| 456 | BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 457 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 458 | auto paramList = state.selectOrContinueChallenge("pin"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 459 | auto request = ca.getCertificateRequest(*challengeInterest2); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 460 | auto secret = request->challengeState->secrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 461 | paramList.begin()->second = secret; |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 462 | challengeInterest3 = state.genChallengeInterest(std::move(paramList)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 463 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 464 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 465 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 466 | BOOST_CHECK(verifySignature(response, cert)); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 467 | state.onChallengeResponse(response); |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 468 | BOOST_CHECK(state.m_status == Status::SUCCESS); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 469 | } |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 470 | }); |
| 471 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 472 | face.receive(*newInterest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 473 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 474 | face.receive(*challengeInterest); |
| 475 | advanceClocks(time::milliseconds(20), 60); |
| 476 | face.receive(*challengeInterest2); |
| 477 | advanceClocks(time::milliseconds(20), 60); |
| 478 | face.receive(*challengeInterest3); |
| 479 | advanceClocks(time::milliseconds(20), 60); |
| 480 | BOOST_CHECK_EQUAL(count, 3); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 481 | } |
| 482 | |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 483 | BOOST_AUTO_TEST_CASE(HandleRevoke) |
| 484 | { |
| 485 | auto identity = addIdentity(Name("/ndn")); |
| 486 | auto key = identity.getDefaultKey(); |
| 487 | auto cert = key.getDefaultCertificate(); |
| 488 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 489 | DummyClientFace face(io, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 490 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 491 | advanceClocks(time::milliseconds(20), 60); |
| 492 | |
| 493 | //generate a certificate |
| 494 | auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty"); |
| 495 | auto clientKey = clientIdentity.getDefaultKey(); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 496 | Certificate clientCert; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 497 | clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion()); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 498 | clientCert.setContentType(ndn::tlv::ContentType_Key); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 499 | clientCert.setFreshnessPeriod(time::hours(24)); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 500 | clientCert.setContent(clientKey.getPublicKey()); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 501 | SignatureInfo signatureInfo; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 502 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(), |
| 503 | time::system_clock::now() + time::hours(10))); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 504 | m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo)); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 505 | RequestId requestId = {{101}}; |
| 506 | RequestState certRequest; |
| 507 | certRequest.caPrefix = Name("/ndn"); |
| 508 | certRequest.requestId = requestId; |
| 509 | certRequest.requestType = RequestType::NEW; |
| 510 | certRequest.status = Status::SUCCESS; |
| 511 | certRequest.cert = clientCert; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 512 | auto issuedCert = ca.issueCertificate(certRequest); |
| 513 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 514 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 515 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 516 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 517 | requester::Request state(m_keyChain, item, RequestType::REVOKE); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 518 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 519 | auto interest = state.genRevokeInterest(issuedCert); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 520 | |
| 521 | int count = 0; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 522 | face.onSendData.connect([&](const Data& response) { |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 523 | count++; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 524 | BOOST_CHECK(verifySignature(response, cert)); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 525 | auto contentBlock = response.getContent(); |
| 526 | contentBlock.parse(); |
| 527 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 528 | BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != ""); |
| 529 | BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != ""); |
| 530 | BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != ""); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 531 | |
| 532 | auto challengeBlockCount = 0; |
| 533 | for (auto const& element : contentBlock.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 534 | if (element.type() == tlv::Challenge) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 535 | challengeBlockCount++; |
| 536 | } |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | BOOST_CHECK(challengeBlockCount != 0); |
| 540 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 541 | auto challengeList = state.onNewRenewRevokeResponse(response); |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 542 | RequestId requestId; |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 543 | std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size()); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 544 | auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey; |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 545 | BOOST_CHECK_EQUAL_COLLECTIONS(state.m_aesKey.begin(), state.m_aesKey.end(), |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 546 | ca_encryption_key.begin(), ca_encryption_key.end()); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 547 | }); |
| 548 | face.receive(*interest); |
| 549 | |
| 550 | advanceClocks(time::milliseconds(20), 60); |
| 551 | BOOST_CHECK_EQUAL(count, 1); |
| 552 | } |
| 553 | |
| 554 | BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert) |
| 555 | { |
| 556 | auto identity = addIdentity(Name("/ndn")); |
| 557 | auto key = identity.getDefaultKey(); |
| 558 | auto cert = key.getDefaultCertificate(); |
| 559 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 560 | DummyClientFace face(io, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 561 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 562 | advanceClocks(time::milliseconds(20), 60); |
| 563 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 564 | // generate a certificate |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 565 | auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty"); |
| 566 | auto clientKey = clientIdentity.getDefaultKey(); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 567 | Certificate clientCert; |
| 568 | clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append("1473283247810732701")); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 569 | clientCert.setContentType(ndn::tlv::ContentType_Key); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 570 | clientCert.setFreshnessPeriod(time::hours(24)); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 571 | clientCert.setContent(clientKey.getPublicKey()); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 572 | SignatureInfo signatureInfo; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 573 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(), |
| 574 | time::system_clock::now() + time::hours(10))); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 575 | m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo)); |
| 576 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 577 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 578 | item.caPrefix = Name("/ndn"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 579 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 580 | requester::Request state(m_keyChain, item, RequestType::NEW); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 581 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 582 | auto interest = state.genRevokeInterest(clientCert); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 583 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 584 | bool receiveData = false; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 585 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 586 | receiveData = true; |
| 587 | auto contentTlv = response.getContent(); |
| 588 | contentTlv.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 589 | BOOST_CHECK(static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))) != ErrorCode::NO_ERROR); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 590 | }); |
| 591 | face.receive(*interest); |
| 592 | |
| 593 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 594 | BOOST_CHECK_EQUAL(receiveData, true); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 597 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 598 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 599 | } // namespace tests |
| 600 | } // namespace ndncert |