blob: 00a3f58f782eb3615a4477bb2fe7808f877003ce [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento914d05f2019-07-13 16:20:19 -04002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
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 Zhangf5246c42017-01-26 09:39:20 -080021#include "ca-module.hpp"
Zhiyi Zhang84e11842020-11-19 20:03:23 -080022#include "challenge/challenge-module.hpp"
23#include "challenge/challenge-email.hpp"
24#include "challenge/challenge-pin.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070025#include "detail/info-encoder.hpp"
tylerliu4140fe82021-01-27 15:45:44 -080026#include "requester-request.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040027
Davide Pesavento829aff62022-05-15 20:30:34 -040028#include "tests/boost-test.hpp"
29#include "tests/io-key-chain-fixture.hpp"
30
31#include <ndn-cxx/metadata-object.hpp>
32#include <ndn-cxx/security/verification-helpers.hpp>
33#include <ndn-cxx/util/dummy-client-face.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080034
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040035namespace ndncert::tests {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080036
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070037using namespace ca;
Davide Pesavento0dc02012021-11-23 22:55:03 -050038using ndn::util::DummyClientFace;
39using ndn::security::verifySignature;
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070040
Davide Pesavento829aff62022-05-15 20:30:34 -040041BOOST_FIXTURE_TEST_SUITE(TestCaModule, IoKeyChainFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080042
43BOOST_AUTO_TEST_CASE(Initialization)
44{
Davide Pesavento829aff62022-05-15 20:30:34 -040045 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070046 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080047 BOOST_CHECK_EQUAL(ca.getCaConf().caProfile.caPrefix, "/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080048
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070050 BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 1); // removed local discovery registration
Zhiyi Zhang696cd042020-10-07 21:27:36 -070051 BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 5); // infoMeta, onProbe, onNew, onChallenge, onRevoke
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080052}
53
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070054BOOST_AUTO_TEST_CASE(HandleProfileFetching)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055{
Davide Pesavento829aff62022-05-15 20:30:34 -040056 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070057 auto key = identity.getDefaultKey();
58 auto cert = key.getDefaultCertificate();
59
Davide Pesavento829aff62022-05-15 20:30:34 -040060 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070061 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070063 auto profileData = ca.getCaProfileData();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064
Davide Pesavento0dc02012021-11-23 22:55:03 -050065 Interest interest = ndn::MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
66 std::shared_ptr<Interest> infoInterest = nullptr;
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070067
Zhiyi Zhang696cd042020-10-07 21:27:36 -070068 face.setInterestFilter(
Davide Pesavento0dc02012021-11-23 22:55:03 -050069 ndn::InterestFilter("/ndn/CA/INFO"),
Zhiyi Zhang696cd042020-10-07 21:27:36 -070070 [&](const auto&, const Interest& interest) {
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070071 if (interest.getName() == profileData.getName()) {
72 face.put(profileData);
73 }
74 },
75 nullptr, nullptr);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070076 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077
78 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070079 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070080 if (count == 0) {
81 count++;
82 auto block = response.getContent();
83 block.parse();
Davide Pesavento0dc02012021-11-23 22:55:03 -050084 infoInterest = std::make_shared<Interest>(Name(block.get(ndn::tlv::Name)).appendSegment(0));
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070085 }
86 else {
87 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -050088 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070089 auto contentBlock = response.getContent();
90 contentBlock.parse();
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080091 auto caItem = infotlv::decodeDataContent(contentBlock);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080092 BOOST_CHECK_EQUAL(caItem.caPrefix, "/ndn");
93 BOOST_CHECK_EQUAL(caItem.probeParameterKeys.size(), 1);
94 BOOST_CHECK_EQUAL(caItem.probeParameterKeys.front(), "full name");
95 BOOST_CHECK_EQUAL(caItem.cert->wireEncode(), cert.wireEncode());
96 BOOST_CHECK_EQUAL(caItem.caInfo, "ndn testbed ca");
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070097 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070098 });
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080099 face.receive(interest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800100 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700101 face.receive(*infoInterest);
102 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700103
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700104 BOOST_CHECK_EQUAL(count, 2);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800105}
106
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700107BOOST_AUTO_TEST_CASE(HandleProbe)
108{
Davide Pesavento829aff62022-05-15 20:30:34 -0400109 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700110 auto key = identity.getDefaultKey();
111 auto cert = key.getDefaultCertificate();
112
Davide Pesavento829aff62022-05-15 20:30:34 -0400113 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700114 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700115 advanceClocks(time::milliseconds(20), 60);
116
117 Interest interest("/ndn/CA/PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500118 Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters);
119 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name"));
120 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi"));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700121 paramTLV.encode();
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700122 interest.setApplicationParameters(paramTLV);
123
124 int count = 0;
125 face.onSendData.connect([&](const Data& response) {
126 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500127 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700128 Block contentBlock = response.getContent();
129 contentBlock.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700130 Block probeResponse = contentBlock.get(tlv::ProbeResponse);
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700131 probeResponse.parse();
132 Name caName;
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700133 caName.wireDecode(probeResponse.get(ndn::tlv::Name));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700134 BOOST_CHECK_EQUAL(caName.size(), 2);
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700135 });
136 face.receive(interest);
137
138 advanceClocks(time::milliseconds(20), 60);
139 BOOST_CHECK_EQUAL(count, 1);
140}
141
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700142BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler)
143{
Davide Pesavento829aff62022-05-15 20:30:34 -0400144 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700145 auto key = identity.getDefaultKey();
146 auto cert = key.getDefaultCertificate();
147
Davide Pesavento829aff62022-05-15 20:30:34 -0400148 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700149 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700150 advanceClocks(time::milliseconds(20), 60);
151
swa770de007bc2020-03-24 21:26:21 -0700152 Interest interest("/ndn/CA/PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500153 Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters);
154 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name"));
155 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi"));
Suyong Won7968f7a2020-05-12 01:01:25 -0700156 paramTLV.encode();
Suyong Won7968f7a2020-05-12 01:01:25 -0700157 interest.setApplicationParameters(paramTLV);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700158
159 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700160 face.onSendData.connect([&](const Data& response) {
161 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500162 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700163 auto contentBlock = response.getContent();
164 contentBlock.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700165 auto probeResponseBlock = contentBlock.get(tlv::ProbeResponse);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700166 probeResponseBlock.parse();
167 Name caPrefix;
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700168 caPrefix.wireDecode(probeResponseBlock.get(ndn::tlv::Name));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700169 BOOST_CHECK(caPrefix != "");
170 });
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700171 face.receive(interest);
172
173 advanceClocks(time::milliseconds(20), 60);
174 BOOST_CHECK_EQUAL(count, 1);
175}
176
Suyong Wone2afeb52020-10-04 03:05:39 +0900177BOOST_AUTO_TEST_CASE(HandleProbeRedirection)
178{
Davide Pesavento829aff62022-05-15 20:30:34 -0400179 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Suyong Wone2afeb52020-10-04 03:05:39 +0900180 auto key = identity.getDefaultKey();
181 auto cert = key.getDefaultCertificate();
182
Davide Pesavento829aff62022-05-15 20:30:34 -0400183 DummyClientFace face(m_io, m_keyChain, {true, true});
Suyong Wone2afeb52020-10-04 03:05:39 +0900184 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
Suyong Wone2afeb52020-10-04 03:05:39 +0900185 advanceClocks(time::milliseconds(20), 60);
186
187 Interest interest("/ndn/CA/PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500188 Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters);
189 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name"));
190 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi"));
Suyong Wone2afeb52020-10-04 03:05:39 +0900191 paramTLV.encode();
Suyong Wone2afeb52020-10-04 03:05:39 +0900192 interest.setApplicationParameters(paramTLV);
193
194 int count = 0;
195 face.onSendData.connect([&](const Data& response) {
196 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500197 BOOST_CHECK(verifySignature(response, cert));
Suyong Wone2afeb52020-10-04 03:05:39 +0900198 Block contentBlock = response.getContent();
199 contentBlock.parse();
200
201 // Test CA sent redirections
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700202 std::vector<Name> redirectionItems;
203 for (auto item : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700204 if (item.type() == tlv::ProbeRedirect) {
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700205 redirectionItems.push_back(Name(item.blockFromValue()));
206 }
Suyong Wone2afeb52020-10-04 03:05:39 +0900207 }
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700208 BOOST_CHECK_EQUAL(redirectionItems.size(), 2);
Tianyuan Yu13aac732022-03-03 20:59:54 -0800209 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirectionItems[0].getPrefix(-1)), "/ndn/edu/ucla");
210 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirectionItems[1].getPrefix(-1)), "/ndn/edu/ucla/cs/irl");
Suyong Wone2afeb52020-10-04 03:05:39 +0900211 });
212 face.receive(interest);
213 advanceClocks(time::milliseconds(20), 60);
214 BOOST_CHECK_EQUAL(count, 1);
215}
216
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800217BOOST_AUTO_TEST_CASE(HandleNew)
218{
Davide Pesavento829aff62022-05-15 20:30:34 -0400219 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800220 auto key = identity.getDefaultKey();
221 auto cert = key.getDefaultCertificate();
222
Davide Pesavento829aff62022-05-15 20:30:34 -0400223 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700224 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800225 advanceClocks(time::milliseconds(20), 60);
226
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700227 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800228 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500229 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800230 requester::Request state(m_keyChain, item, RequestType::NEW);
Davide Pesavento829aff62022-05-15 20:30:34 -0400231 auto interest = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(),
tylerliu4140fe82021-01-27 15:45:44 -0800232 time::system_clock::now(),
233 time::system_clock::now() + time::days(1));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800234
235 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700236 face.onSendData.connect([&](const Data& response) {
237 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500238 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700239 auto contentBlock = response.getContent();
240 contentBlock.parse();
Suyong Won7968f7a2020-05-12 01:01:25 -0700241
tylerliu50d679e2020-10-14 14:08:39 -0700242 BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != "");
243 BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != "");
244 BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != "");
Suyong Won7968f7a2020-05-12 01:01:25 -0700245
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700246 auto challengeBlockCount = 0;
247 for (auto const& element : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700248 if (element.type() == tlv::Challenge) {
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700249 challengeBlockCount++;
Suyong Won7968f7a2020-05-12 01:01:25 -0700250 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700251 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700252
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700253 BOOST_CHECK(challengeBlockCount != 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700254
tylerliu4140fe82021-01-27 15:45:44 -0800255 auto challengeList = state.onNewRenewRevokeResponse(response);
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700256 RequestId requestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700257 std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size());
tylerliu7b9185c2020-11-24 12:15:18 -0800258 auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800259 BOOST_CHECK_EQUAL_COLLECTIONS(state.m_aesKey.begin(), state.m_aesKey.end(),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700260 ca_encryption_key.begin(), ca_encryption_key.end());
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700261 });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700262 face.receive(*interest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800263
264 advanceClocks(time::milliseconds(20), 60);
265 BOOST_CHECK_EQUAL(count, 1);
266}
267
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700268BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1)
269{
Davide Pesavento829aff62022-05-15 20:30:34 -0400270 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700271 auto key = identity.getDefaultKey();
272 auto cert = key.getDefaultCertificate();
273
Davide Pesavento829aff62022-05-15 20:30:34 -0400274 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700275 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1");
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700276 advanceClocks(time::milliseconds(20), 60);
277
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700278 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800279 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500280 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800281 requester::Request state(m_keyChain, item, RequestType::NEW);
Davide Pesavento829aff62022-05-15 20:30:34 -0400282 auto client = m_keyChain.createIdentity(Name("/ndn/zhiyi"));
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700283 auto current_tp = time::system_clock::now();
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800284 auto interest1 = state.genNewInterest(client.getDefaultKey().getName(), current_tp, current_tp - time::hours(1));
285 auto interest2 = state.genNewInterest(client.getDefaultKey().getName(), current_tp, current_tp + time::days(361));
286 auto interest3 = state.genNewInterest(client.getDefaultKey().getName(),
287 current_tp - time::hours(1), current_tp + time::hours(2));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700288 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700289 auto contentTlv = response.getContent();
290 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700291 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700292 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700293 });
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700294 face.receive(*interest1);
295 face.receive(*interest2);
296 face.receive(*interest3);
297
298 advanceClocks(time::milliseconds(20), 60);
299}
300
tylerliuf62cfeb2021-01-28 11:01:33 -0800301BOOST_AUTO_TEST_CASE(HandleNewWithServerBadValidity)
302{
Davide Pesavento829aff62022-05-15 20:30:34 -0400303 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliuf62cfeb2021-01-28 11:01:33 -0800304 auto key = identity.getDefaultKey();
305
306 //build expired cert
Davide Pesavento0dc02012021-11-23 22:55:03 -0500307 Certificate cert;
tylerliuf62cfeb2021-01-28 11:01:33 -0800308 cert.setName(Name(key.getName()).append("self-sign").appendVersion());
309 cert.setContentType(ndn::tlv::ContentType_Key);
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400310 cert.setContent(key.getPublicKey());
tylerliuf62cfeb2021-01-28 11:01:33 -0800311 SignatureInfo signatureInfo;
Davide Pesavento829aff62022-05-15 20:30:34 -0400312 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod::makeRelative(-1_days, -1_s));
tylerliuf62cfeb2021-01-28 11:01:33 -0800313 m_keyChain.sign(cert, signingByKey(key.getName()).setSignatureInfo(signatureInfo));
314 m_keyChain.setDefaultCertificate(key, cert);
315
Davide Pesavento829aff62022-05-15 20:30:34 -0400316 DummyClientFace face(m_io, m_keyChain, {true, true});
tylerliuf62cfeb2021-01-28 11:01:33 -0800317 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
318 advanceClocks(time::milliseconds(20), 60);
319
320 CaProfile item;
321 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500322 item.cert = std::make_shared<Certificate>(cert);
tylerliuf62cfeb2021-01-28 11:01:33 -0800323 requester::Request state(m_keyChain, item, RequestType::NEW);
Davide Pesavento829aff62022-05-15 20:30:34 -0400324 auto interest = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(),
tylerliuf62cfeb2021-01-28 11:01:33 -0800325 time::system_clock::now(),
326 time::system_clock::now() + time::days(1));
327
328 int count = 0;
329 face.onSendData.connect([&](const Data& response) {
330 auto contentTlv = response.getContent();
331 contentTlv.parse();
332 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
333 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
334 count ++;
335 });
336 face.receive(*interest);
337
338 advanceClocks(time::milliseconds(20), 60);
339 BOOST_CHECK_EQUAL(count, 1);
340}
341
tylerliu0b6d0db2020-09-28 17:52:02 -0700342BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix)
343{
Davide Pesavento829aff62022-05-15 20:30:34 -0400344 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu0b6d0db2020-09-28 17:52:02 -0700345 auto key = identity.getDefaultKey();
346 auto cert = key.getDefaultCertificate();
347
Davide Pesavento829aff62022-05-15 20:30:34 -0400348 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700349 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu0b6d0db2020-09-28 17:52:02 -0700350 advanceClocks(time::milliseconds(20), 60);
351
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700352 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800353 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500354 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800355 requester::Request state(m_keyChain, item, RequestType::NEW);
tylerliu0b6d0db2020-09-28 17:52:02 -0700356
Davide Pesavento829aff62022-05-15 20:30:34 -0400357 auto interest1 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a")).getDefaultKey().getName(),
358 time::system_clock::now(),
359 time::system_clock::now() + time::days(1));
360 auto interest2 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a/b")).getDefaultKey().getName(),
361 time::system_clock::now(),
362 time::system_clock::now() + time::days(1));
363 auto interest3 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a/b/c/d")).getDefaultKey().getName(),
364 time::system_clock::now(),
365 time::system_clock::now() + time::days(1));
tylerliu0b6d0db2020-09-28 17:52:02 -0700366
tylerliu0b6d0db2020-09-28 17:52:02 -0700367 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700368 auto contentTlv = response.getContent();
369 contentTlv.parse();
370 if (interest3->getName().isPrefixOf(response.getName())) {
tylerliu50d679e2020-10-14 14:08:39 -0700371 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700372 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
373 }
374 else {
375 // should successfully get responses
Davide Pesavento0dc02012021-11-23 22:55:03 -0500376 BOOST_CHECK_THROW(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)), std::runtime_error);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700377 }
tylerliu0b6d0db2020-09-28 17:52:02 -0700378 });
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700379 face.receive(*interest1);
380 face.receive(*interest2);
381 face.receive(*interest3);
tylerliu0b6d0db2020-09-28 17:52:02 -0700382 advanceClocks(time::milliseconds(20), 60);
tylerliu0b6d0db2020-09-28 17:52:02 -0700383}
384
tylerliu0b6d0db2020-09-28 17:52:02 -0700385BOOST_AUTO_TEST_CASE(HandleNewWithInvalidLength1)
386{
Davide Pesavento829aff62022-05-15 20:30:34 -0400387 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu0b6d0db2020-09-28 17:52:02 -0700388 auto key = identity.getDefaultKey();
389 auto cert = key.getDefaultCertificate();
390
Davide Pesavento829aff62022-05-15 20:30:34 -0400391 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700392 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1");
tylerliu0b6d0db2020-09-28 17:52:02 -0700393 advanceClocks(time::milliseconds(20), 60);
394
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700395 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800396 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500397 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800398 requester::Request state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700399
tylerliu0b6d0db2020-09-28 17:52:02 -0700400 auto current_tp = time::system_clock::now();
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800401 auto interest1 = state.genNewInterest(identity.getDefaultKey().getName(), current_tp, current_tp + time::days(1));
Davide Pesavento829aff62022-05-15 20:30:34 -0400402 auto interest2 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a/b/c/d")).getDefaultKey().getName(),
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800403 current_tp, current_tp + time::days(1));
tylerliu0b6d0db2020-09-28 17:52:02 -0700404 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700405 auto contentTlv = response.getContent();
406 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700407 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700408 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
tylerliu0b6d0db2020-09-28 17:52:02 -0700409 });
410 face.receive(*interest1);
411 face.receive(*interest2);
412
413 advanceClocks(time::milliseconds(20), 60);
414}
415
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700416BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800417{
Davide Pesavento829aff62022-05-15 20:30:34 -0400418 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700419 auto key = identity.getDefaultKey();
420 auto cert = key.getDefaultCertificate();
421
Davide Pesavento829aff62022-05-15 20:30:34 -0400422 DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700423 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800424 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700425
426 // generate NEW Interest
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700427 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800428 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500429 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800430 requester::Request state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700431
Davide Pesavento829aff62022-05-15 20:30:34 -0400432 auto newInterest = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(),
433 time::system_clock::now(),
434 time::system_clock::now() + time::days(1));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700435
436 // generate CHALLENGE Interest
Davide Pesavento0dc02012021-11-23 22:55:03 -0500437 std::shared_ptr<Interest> challengeInterest;
438 std::shared_ptr<Interest> challengeInterest2;
439 std::shared_ptr<Interest> challengeInterest3;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800440
441 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700442 face.onSendData.connect([&](const Data& response) {
swa770de007bc2020-03-24 21:26:21 -0700443 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
tylerliu4140fe82021-01-27 15:45:44 -0800444 auto challengeList = state.onNewRenewRevokeResponse(response);
445 auto paramList = state.selectOrContinueChallenge("pin");
446 challengeInterest = state.genChallengeInterest(std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700447 }
swa770de007bc2020-03-24 21:26:21 -0700448 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800449 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500450 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800451
tylerliu4140fe82021-01-27 15:45:44 -0800452 state.onChallengeResponse(response);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800453 BOOST_CHECK(state.m_status == Status::CHALLENGE);
454 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE);
tylerliu4140fe82021-01-27 15:45:44 -0800455 auto paramList = state.selectOrContinueChallenge("pin");
456 challengeInterest2 = state.genChallengeInterest(std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700457 }
swa770de007bc2020-03-24 21:26:21 -0700458 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700459 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500460 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700461
tylerliu4140fe82021-01-27 15:45:44 -0800462 state.onChallengeResponse(response);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800463 BOOST_CHECK(state.m_status == Status::CHALLENGE);
464 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400465
tylerliu4140fe82021-01-27 15:45:44 -0800466 auto paramList = state.selectOrContinueChallenge("pin");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700467 auto request = ca.getCertificateRequest(*challengeInterest2);
tylerliu7b9185c2020-11-24 12:15:18 -0800468 auto secret = request->challengeState->secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
tylerliu40226332020-11-11 15:37:16 -0800469 paramList.begin()->second = secret;
tylerliu4140fe82021-01-27 15:45:44 -0800470 challengeInterest3 = state.genChallengeInterest(std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700471 }
swa770de007bc2020-03-24 21:26:21 -0700472 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700473 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500474 BOOST_CHECK(verifySignature(response, cert));
tylerliu4140fe82021-01-27 15:45:44 -0800475 state.onChallengeResponse(response);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800476 BOOST_CHECK(state.m_status == Status::SUCCESS);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700477 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400478 });
Tianyuan Yue78fd4d2022-05-25 13:18:06 -0700479 ca.setStatusUpdateCallback([](const RequestState& request) {
480 if (request.status == Status::SUCCESS && request.requestType == RequestType::NEW) {
481 BOOST_REQUIRE_NO_THROW(Certificate{request.cert});
482 BOOST_CHECK(Certificate(request.cert).isValid());
483 }
484 });
Davide Pesavento914d05f2019-07-13 16:20:19 -0400485
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700486 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800487 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700488 face.receive(*challengeInterest);
489 advanceClocks(time::milliseconds(20), 60);
490 face.receive(*challengeInterest2);
491 advanceClocks(time::milliseconds(20), 60);
492 face.receive(*challengeInterest3);
493 advanceClocks(time::milliseconds(20), 60);
494 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800495}
496
tylerliu182bc532020-09-25 01:54:45 -0700497BOOST_AUTO_TEST_CASE(HandleRevoke)
498{
Davide Pesavento829aff62022-05-15 20:30:34 -0400499 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu182bc532020-09-25 01:54:45 -0700500 auto key = identity.getDefaultKey();
501 auto cert = key.getDefaultCertificate();
502
Davide Pesavento829aff62022-05-15 20:30:34 -0400503 DummyClientFace face(m_io, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700504 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu182bc532020-09-25 01:54:45 -0700505 advanceClocks(time::milliseconds(20), 60);
506
507 //generate a certificate
508 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
509 auto clientKey = clientIdentity.getDefaultKey();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500510 Certificate clientCert;
tylerliu182bc532020-09-25 01:54:45 -0700511 clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700512 clientCert.setContentType(ndn::tlv::ContentType_Key);
tylerliu182bc532020-09-25 01:54:45 -0700513 clientCert.setFreshnessPeriod(time::hours(24));
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400514 clientCert.setContent(clientKey.getPublicKey());
tylerliu182bc532020-09-25 01:54:45 -0700515 SignatureInfo signatureInfo;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500516 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(),
517 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700518 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800519 RequestId requestId = {{101}};
520 RequestState certRequest;
521 certRequest.caPrefix = Name("/ndn");
522 certRequest.requestId = requestId;
523 certRequest.requestType = RequestType::NEW;
524 certRequest.status = Status::SUCCESS;
525 certRequest.cert = clientCert;
tylerliu182bc532020-09-25 01:54:45 -0700526 auto issuedCert = ca.issueCertificate(certRequest);
527
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700528 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800529 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500530 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800531 requester::Request state(m_keyChain, item, RequestType::REVOKE);
tylerliu182bc532020-09-25 01:54:45 -0700532
tylerliu4140fe82021-01-27 15:45:44 -0800533 auto interest = state.genRevokeInterest(issuedCert);
tylerliu182bc532020-09-25 01:54:45 -0700534
535 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700536 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700537 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500538 BOOST_CHECK(verifySignature(response, cert));
tylerliu182bc532020-09-25 01:54:45 -0700539 auto contentBlock = response.getContent();
540 contentBlock.parse();
541
tylerliu50d679e2020-10-14 14:08:39 -0700542 BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != "");
543 BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != "");
544 BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != "");
tylerliu182bc532020-09-25 01:54:45 -0700545
546 auto challengeBlockCount = 0;
547 for (auto const& element : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700548 if (element.type() == tlv::Challenge) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700549 challengeBlockCount++;
550 }
tylerliu182bc532020-09-25 01:54:45 -0700551 }
552
553 BOOST_CHECK(challengeBlockCount != 0);
554
tylerliu4140fe82021-01-27 15:45:44 -0800555 auto challengeList = state.onNewRenewRevokeResponse(response);
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700556 RequestId requestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700557 std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size());
tylerliu7b9185c2020-11-24 12:15:18 -0800558 auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800559 BOOST_CHECK_EQUAL_COLLECTIONS(state.m_aesKey.begin(), state.m_aesKey.end(),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700560 ca_encryption_key.begin(), ca_encryption_key.end());
tylerliu182bc532020-09-25 01:54:45 -0700561 });
562 face.receive(*interest);
563
564 advanceClocks(time::milliseconds(20), 60);
565 BOOST_CHECK_EQUAL(count, 1);
566}
567
568BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert)
569{
Davide Pesavento829aff62022-05-15 20:30:34 -0400570 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu182bc532020-09-25 01:54:45 -0700571 auto key = identity.getDefaultKey();
572 auto cert = key.getDefaultCertificate();
573
Davide Pesavento829aff62022-05-15 20:30:34 -0400574 DummyClientFace face(m_io, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700575 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu182bc532020-09-25 01:54:45 -0700576 advanceClocks(time::milliseconds(20), 60);
577
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700578 // generate a certificate
tylerliu182bc532020-09-25 01:54:45 -0700579 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
580 auto clientKey = clientIdentity.getDefaultKey();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500581 Certificate clientCert;
582 clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append("1473283247810732701"));
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700583 clientCert.setContentType(ndn::tlv::ContentType_Key);
tylerliu182bc532020-09-25 01:54:45 -0700584 clientCert.setFreshnessPeriod(time::hours(24));
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400585 clientCert.setContent(clientKey.getPublicKey());
tylerliu182bc532020-09-25 01:54:45 -0700586 SignatureInfo signatureInfo;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500587 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(),
588 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700589 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
590
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700591 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800592 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500593 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800594 requester::Request state(m_keyChain, item, RequestType::NEW);
tylerliu182bc532020-09-25 01:54:45 -0700595
tylerliu4140fe82021-01-27 15:45:44 -0800596 auto interest = state.genRevokeInterest(clientCert);
tylerliu182bc532020-09-25 01:54:45 -0700597
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700598 bool receiveData = false;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700599 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700600 receiveData = true;
601 auto contentTlv = response.getContent();
602 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700603 BOOST_CHECK(static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))) != ErrorCode::NO_ERROR);
tylerliu182bc532020-09-25 01:54:45 -0700604 });
605 face.receive(*interest);
606
607 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700608 BOOST_CHECK_EQUAL(receiveData, true);
tylerliu182bc532020-09-25 01:54:45 -0700609}
610
Davide Pesavento0dc02012021-11-23 22:55:03 -0500611BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800612
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400613} // namespace ndncert::tests