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