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