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" |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 26 | #include "requester.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 | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 236 | requester::RequestState state(m_keyChain, item, RequestType::NEW); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 237 | auto interest = requester::Requester::genNewInterest(state, Name("/ndn/zhiyi"), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 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 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 261 | auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, 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 | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 287 | requester::RequestState 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(); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 289 | auto interest1 = requester::Requester::genNewInterest(state, Name("/ndn/zhiyi"), current_tp, current_tp - time::hours(1)); |
| 290 | auto interest2 = requester::Requester::genNewInterest(state, Name("/ndn/zhiyi"), current_tp, current_tp + time::days(361)); |
| 291 | auto interest3 = requester::Requester::genNewInterest(state, 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 | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 305 | BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix) |
| 306 | { |
| 307 | auto identity = addIdentity(Name("/ndn")); |
| 308 | auto key = identity.getDefaultKey(); |
| 309 | auto cert = key.getDefaultCertificate(); |
| 310 | |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 311 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 312 | 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] | 313 | advanceClocks(time::milliseconds(20), 60); |
| 314 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 315 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 316 | item.caPrefix = Name("/ndn"); |
| 317 | item.cert = std::make_shared<security::Certificate>(cert); |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 318 | requester::RequestState state(m_keyChain, item, RequestType::NEW); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 319 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 320 | auto interest1 = requester::Requester::genNewInterest(state, Name("/ndn/a"), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 321 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 322 | auto interest2 = requester::Requester::genNewInterest(state, Name("/ndn/a/b"), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 323 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 324 | auto interest3 = requester::Requester::genNewInterest(state, Name("/ndn/a/b/c/d"), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 325 | time::system_clock::now() + time::days(1)); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 326 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 327 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 328 | auto contentTlv = response.getContent(); |
| 329 | contentTlv.parse(); |
| 330 | if (interest3->getName().isPrefixOf(response.getName())) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 331 | auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 332 | BOOST_CHECK(errorCode != ErrorCode::NO_ERROR); |
| 333 | } |
| 334 | else { |
| 335 | // should successfully get responses |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 336 | BOOST_CHECK_EXCEPTION(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)), std::runtime_error, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 337 | [](const auto& e) { return true; }); |
| 338 | } |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 339 | }); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 340 | face.receive(*interest1); |
| 341 | face.receive(*interest2); |
| 342 | face.receive(*interest3); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 343 | advanceClocks(time::milliseconds(20), 60); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 344 | } |
| 345 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 346 | BOOST_AUTO_TEST_CASE(HandleNewWithInvalidLength1) |
| 347 | { |
| 348 | auto identity = addIdentity(Name("/ndn")); |
| 349 | auto key = identity.getDefaultKey(); |
| 350 | auto cert = key.getDefaultCertificate(); |
| 351 | |
| 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"); |
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 | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 359 | requester::RequestState state(m_keyChain, item, RequestType::NEW); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 360 | |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 361 | auto current_tp = time::system_clock::now(); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 362 | auto interest1 = requester::Requester::genNewInterest(state, Name("/ndn"), current_tp, current_tp + time::days(1)); |
| 363 | auto interest2 = requester::Requester::genNewInterest(state, Name("/ndn/a/b/c/d"), current_tp, current_tp + time::days(1)); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 364 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 365 | auto contentTlv = response.getContent(); |
| 366 | contentTlv.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 367 | auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 368 | BOOST_CHECK(errorCode != ErrorCode::NO_ERROR); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 369 | }); |
| 370 | face.receive(*interest1); |
| 371 | face.receive(*interest2); |
| 372 | |
| 373 | advanceClocks(time::milliseconds(20), 60); |
| 374 | } |
| 375 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 376 | BOOST_AUTO_TEST_CASE(HandleChallenge) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 377 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 378 | auto identity = addIdentity(Name("/ndn")); |
| 379 | auto key = identity.getDefaultKey(); |
| 380 | auto cert = key.getDefaultCertificate(); |
| 381 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 382 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 383 | 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] | 384 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 385 | |
| 386 | // generate NEW Interest |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 387 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 388 | item.caPrefix = Name("/ndn"); |
| 389 | item.cert = std::make_shared<security::Certificate>(cert); |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 390 | requester::RequestState state(m_keyChain, item, RequestType::NEW); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 391 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 392 | auto newInterest = requester::Requester::genNewInterest(state, Name("/ndn/zhiyi"), time::system_clock::now(), |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 393 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 394 | |
| 395 | // generate CHALLENGE Interest |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 396 | shared_ptr<Interest> challengeInterest = nullptr; |
| 397 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 398 | shared_ptr<Interest> challengeInterest3 = nullptr; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 399 | |
| 400 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 401 | face.onSendData.connect([&](const Data& response) { |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 402 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 403 | auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, response); |
| 404 | auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin"); |
| 405 | challengeInterest = requester::Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 406 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 407 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 408 | count++; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 409 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 410 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 411 | requester::Requester::onChallengeResponse(state, response); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 412 | BOOST_CHECK(state.status == Status::CHALLENGE); |
| 413 | BOOST_CHECK_EQUAL(state.challengeStatus, ChallengePin::NEED_CODE); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 414 | auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin"); |
| 415 | challengeInterest2 = requester::Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 416 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 417 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 418 | count++; |
| 419 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 420 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 421 | requester::Requester::onChallengeResponse(state, response); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 422 | BOOST_CHECK(state.status == Status::CHALLENGE); |
| 423 | BOOST_CHECK_EQUAL(state.challengeStatus, ChallengePin::WRONG_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 424 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 425 | auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 426 | auto request = ca.getCertificateRequest(*challengeInterest2); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 427 | auto secret = request->challengeState->secrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 428 | paramList.begin()->second = secret; |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 429 | challengeInterest3 = requester::Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 430 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 431 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 432 | count++; |
| 433 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 434 | requester::Requester::onChallengeResponse(state, response); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 435 | BOOST_CHECK(state.status == Status::SUCCESS); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 436 | } |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 437 | }); |
| 438 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 439 | face.receive(*newInterest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 440 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 441 | face.receive(*challengeInterest); |
| 442 | advanceClocks(time::milliseconds(20), 60); |
| 443 | face.receive(*challengeInterest2); |
| 444 | advanceClocks(time::milliseconds(20), 60); |
| 445 | face.receive(*challengeInterest3); |
| 446 | advanceClocks(time::milliseconds(20), 60); |
| 447 | BOOST_CHECK_EQUAL(count, 3); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 448 | } |
| 449 | |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 450 | BOOST_AUTO_TEST_CASE(HandleRevoke) |
| 451 | { |
| 452 | auto identity = addIdentity(Name("/ndn")); |
| 453 | auto key = identity.getDefaultKey(); |
| 454 | auto cert = key.getDefaultCertificate(); |
| 455 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 456 | util::DummyClientFace face(io, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 457 | 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] | 458 | advanceClocks(time::milliseconds(20), 60); |
| 459 | |
| 460 | //generate a certificate |
| 461 | auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty"); |
| 462 | auto clientKey = clientIdentity.getDefaultKey(); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 463 | security::Certificate clientCert; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 464 | clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion()); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 465 | clientCert.setContentType(ndn::tlv::ContentType_Key); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 466 | clientCert.setFreshnessPeriod(time::hours(24)); |
| 467 | clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size()); |
| 468 | SignatureInfo signatureInfo; |
| 469 | signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 470 | time::system_clock::now() + time::hours(10))); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 471 | m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo)); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 472 | RequestId requestId = {{101}}; |
| 473 | RequestState certRequest; |
| 474 | certRequest.caPrefix = Name("/ndn"); |
| 475 | certRequest.requestId = requestId; |
| 476 | certRequest.requestType = RequestType::NEW; |
| 477 | certRequest.status = Status::SUCCESS; |
| 478 | certRequest.cert = clientCert; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 479 | auto issuedCert = ca.issueCertificate(certRequest); |
| 480 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 481 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 482 | item.caPrefix = Name("/ndn"); |
| 483 | item.cert = std::make_shared<security::Certificate>(cert); |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 484 | requester::RequestState state(m_keyChain, item, RequestType::REVOKE); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 485 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 486 | auto interest = requester::Requester::genRevokeInterest(state, issuedCert); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 487 | |
| 488 | int count = 0; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 489 | face.onSendData.connect([&](const Data& response) { |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 490 | count++; |
| 491 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 492 | auto contentBlock = response.getContent(); |
| 493 | contentBlock.parse(); |
| 494 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 495 | BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != ""); |
| 496 | BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != ""); |
| 497 | BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != ""); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 498 | |
| 499 | auto challengeBlockCount = 0; |
| 500 | for (auto const& element : contentBlock.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 501 | if (element.type() == tlv::Challenge) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 502 | challengeBlockCount++; |
| 503 | } |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | BOOST_CHECK(challengeBlockCount != 0); |
| 507 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 508 | auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, response); |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 509 | RequestId requestId; |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 510 | 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] | 511 | auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey; |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 512 | BOOST_CHECK_EQUAL_COLLECTIONS(state.aesKey.begin(), state.aesKey.end(), |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 513 | ca_encryption_key.begin(), ca_encryption_key.end()); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 514 | }); |
| 515 | face.receive(*interest); |
| 516 | |
| 517 | advanceClocks(time::milliseconds(20), 60); |
| 518 | BOOST_CHECK_EQUAL(count, 1); |
| 519 | } |
| 520 | |
| 521 | BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert) |
| 522 | { |
| 523 | auto identity = addIdentity(Name("/ndn")); |
| 524 | auto key = identity.getDefaultKey(); |
| 525 | auto cert = key.getDefaultCertificate(); |
| 526 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 527 | util::DummyClientFace face(io, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 528 | 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] | 529 | advanceClocks(time::milliseconds(20), 60); |
| 530 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 531 | // generate a certificate |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 532 | auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty"); |
| 533 | auto clientKey = clientIdentity.getDefaultKey(); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 534 | security::Certificate clientCert; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 535 | clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append(std::to_string(1473283247810732701))); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 536 | clientCert.setContentType(ndn::tlv::ContentType_Key); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 537 | clientCert.setFreshnessPeriod(time::hours(24)); |
| 538 | clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size()); |
| 539 | SignatureInfo signatureInfo; |
| 540 | signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 541 | time::system_clock::now() + time::hours(10))); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 542 | m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo)); |
| 543 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 544 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 545 | item.caPrefix = Name("/ndn"); |
| 546 | item.cert = std::make_shared<security::Certificate>(cert); |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 547 | requester::RequestState state(m_keyChain, item, RequestType::NEW); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 548 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 549 | auto interest = requester::Requester::genRevokeInterest(state, clientCert); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 550 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 551 | bool receiveData = false; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 552 | face.onSendData.connect([&](const Data& response) { |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 553 | receiveData = true; |
| 554 | auto contentTlv = response.getContent(); |
| 555 | contentTlv.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 556 | BOOST_CHECK(static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))) != ErrorCode::NO_ERROR); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 557 | }); |
| 558 | face.receive(*interest); |
| 559 | |
| 560 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 561 | BOOST_CHECK_EQUAL(receiveData, true); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 564 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 565 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 566 | } // namespace tests |
| 567 | } // namespace ndncert |
| 568 | } // namespace ndn |