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