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