blob: 14b72b2eb1de0befa5876e56a127d1437dada710 [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/*
Davide Pesavento76304d82023-08-10 23:38:06 -04003 * Copyright (c) 2017-2023, 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::security::verifySignature;
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070039
Davide Pesavento829aff62022-05-15 20:30:34 -040040BOOST_FIXTURE_TEST_SUITE(TestCaModule, IoKeyChainFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080041
42BOOST_AUTO_TEST_CASE(Initialization)
43{
Davide Pesavento76304d82023-08-10 23:38:06 -040044 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070045 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080046 BOOST_CHECK_EQUAL(ca.getCaConf().caProfile.caPrefix, "/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080048 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070049 BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 1); // removed local discovery registration
Zhiyi Zhang696cd042020-10-07 21:27:36 -070050 BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 5); // infoMeta, onProbe, onNew, onChallenge, onRevoke
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080051}
52
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070053BOOST_AUTO_TEST_CASE(HandleProfileFetching)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054{
Davide Pesavento829aff62022-05-15 20:30:34 -040055 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056 auto key = identity.getDefaultKey();
57 auto cert = key.getDefaultCertificate();
58
Davide Pesavento76304d82023-08-10 23:38:06 -040059 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070060 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070062 auto profileData = ca.getCaProfileData();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063
Davide Pesavento0dc02012021-11-23 22:55:03 -050064 Interest interest = ndn::MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
65 std::shared_ptr<Interest> infoInterest = nullptr;
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070066
Zhiyi Zhang696cd042020-10-07 21:27:36 -070067 face.setInterestFilter(
Davide Pesavento0dc02012021-11-23 22:55:03 -050068 ndn::InterestFilter("/ndn/CA/INFO"),
Zhiyi Zhang696cd042020-10-07 21:27:36 -070069 [&](const auto&, const Interest& interest) {
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070070 if (interest.getName() == profileData.getName()) {
71 face.put(profileData);
72 }
73 },
74 nullptr, nullptr);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070075 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076
77 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070078 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070079 if (count == 0) {
80 count++;
81 auto block = response.getContent();
82 block.parse();
Davide Pesavento0dc02012021-11-23 22:55:03 -050083 infoInterest = std::make_shared<Interest>(Name(block.get(ndn::tlv::Name)).appendSegment(0));
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070084 }
85 else {
86 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -050087 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070088 auto contentBlock = response.getContent();
89 contentBlock.parse();
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080090 auto caItem = infotlv::decodeDataContent(contentBlock);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080091 BOOST_CHECK_EQUAL(caItem.caPrefix, "/ndn");
92 BOOST_CHECK_EQUAL(caItem.probeParameterKeys.size(), 1);
93 BOOST_CHECK_EQUAL(caItem.probeParameterKeys.front(), "full name");
94 BOOST_CHECK_EQUAL(caItem.cert->wireEncode(), cert.wireEncode());
95 BOOST_CHECK_EQUAL(caItem.caInfo, "ndn testbed ca");
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070096 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070097 });
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080098 face.receive(interest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080099 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700100 face.receive(*infoInterest);
101 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700102
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700103 BOOST_CHECK_EQUAL(count, 2);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800104}
105
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700106BOOST_AUTO_TEST_CASE(HandleProbe)
107{
Davide Pesavento829aff62022-05-15 20:30:34 -0400108 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700109 auto key = identity.getDefaultKey();
110 auto cert = key.getDefaultCertificate();
111
Davide Pesavento76304d82023-08-10 23:38:06 -0400112 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700113 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700114 advanceClocks(time::milliseconds(20), 60);
115
116 Interest interest("/ndn/CA/PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500117 Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters);
118 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name"));
119 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi"));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700120 paramTLV.encode();
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700121 interest.setApplicationParameters(paramTLV);
122
123 int count = 0;
124 face.onSendData.connect([&](const Data& response) {
125 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500126 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700127 Block contentBlock = response.getContent();
128 contentBlock.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700129 Block probeResponse = contentBlock.get(tlv::ProbeResponse);
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700130 probeResponse.parse();
131 Name caName;
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700132 caName.wireDecode(probeResponse.get(ndn::tlv::Name));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700133 BOOST_CHECK_EQUAL(caName.size(), 2);
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700134 });
135 face.receive(interest);
136
137 advanceClocks(time::milliseconds(20), 60);
138 BOOST_CHECK_EQUAL(count, 1);
139}
140
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700141BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler)
142{
Davide Pesavento829aff62022-05-15 20:30:34 -0400143 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700144 auto key = identity.getDefaultKey();
145 auto cert = key.getDefaultCertificate();
146
Davide Pesavento76304d82023-08-10 23:38:06 -0400147 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700148 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700149 advanceClocks(time::milliseconds(20), 60);
150
swa770de007bc2020-03-24 21:26:21 -0700151 Interest interest("/ndn/CA/PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500152 Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters);
153 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name"));
154 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi"));
Suyong Won7968f7a2020-05-12 01:01:25 -0700155 paramTLV.encode();
Suyong Won7968f7a2020-05-12 01:01:25 -0700156 interest.setApplicationParameters(paramTLV);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700157
158 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700159 face.onSendData.connect([&](const Data& response) {
160 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500161 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700162 auto contentBlock = response.getContent();
163 contentBlock.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700164 auto probeResponseBlock = contentBlock.get(tlv::ProbeResponse);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700165 probeResponseBlock.parse();
166 Name caPrefix;
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700167 caPrefix.wireDecode(probeResponseBlock.get(ndn::tlv::Name));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700168 BOOST_CHECK(caPrefix != "");
169 });
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700170 face.receive(interest);
171
172 advanceClocks(time::milliseconds(20), 60);
173 BOOST_CHECK_EQUAL(count, 1);
174}
175
Suyong Wone2afeb52020-10-04 03:05:39 +0900176BOOST_AUTO_TEST_CASE(HandleProbeRedirection)
177{
Davide Pesavento829aff62022-05-15 20:30:34 -0400178 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Suyong Wone2afeb52020-10-04 03:05:39 +0900179 auto key = identity.getDefaultKey();
180 auto cert = key.getDefaultCertificate();
181
Davide Pesavento76304d82023-08-10 23:38:06 -0400182 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Suyong Wone2afeb52020-10-04 03:05:39 +0900183 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
Suyong Wone2afeb52020-10-04 03:05:39 +0900184 advanceClocks(time::milliseconds(20), 60);
185
186 Interest interest("/ndn/CA/PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500187 Block paramTLV = ndn::makeEmptyBlock(ndn::tlv::ApplicationParameters);
188 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, "name"));
189 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi"));
Suyong Wone2afeb52020-10-04 03:05:39 +0900190 paramTLV.encode();
Suyong Wone2afeb52020-10-04 03:05:39 +0900191 interest.setApplicationParameters(paramTLV);
192
193 int count = 0;
194 face.onSendData.connect([&](const Data& response) {
195 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500196 BOOST_CHECK(verifySignature(response, cert));
Suyong Wone2afeb52020-10-04 03:05:39 +0900197 Block contentBlock = response.getContent();
198 contentBlock.parse();
199
200 // Test CA sent redirections
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700201 std::vector<Name> redirectionItems;
202 for (auto item : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700203 if (item.type() == tlv::ProbeRedirect) {
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700204 redirectionItems.push_back(Name(item.blockFromValue()));
205 }
Suyong Wone2afeb52020-10-04 03:05:39 +0900206 }
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700207 BOOST_CHECK_EQUAL(redirectionItems.size(), 2);
Tianyuan Yu13aac732022-03-03 20:59:54 -0800208 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirectionItems[0].getPrefix(-1)), "/ndn/edu/ucla");
209 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirectionItems[1].getPrefix(-1)), "/ndn/edu/ucla/cs/irl");
Suyong Wone2afeb52020-10-04 03:05:39 +0900210 });
211 face.receive(interest);
212 advanceClocks(time::milliseconds(20), 60);
213 BOOST_CHECK_EQUAL(count, 1);
214}
215
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800216BOOST_AUTO_TEST_CASE(HandleNew)
217{
Davide Pesavento829aff62022-05-15 20:30:34 -0400218 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800219 auto key = identity.getDefaultKey();
220 auto cert = key.getDefaultCertificate();
221
Davide Pesavento76304d82023-08-10 23:38:06 -0400222 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700223 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800224 advanceClocks(time::milliseconds(20), 60);
225
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700226 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800227 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500228 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800229 requester::Request state(m_keyChain, item, RequestType::NEW);
Davide Pesavento829aff62022-05-15 20:30:34 -0400230 auto interest = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(),
tylerliu4140fe82021-01-27 15:45:44 -0800231 time::system_clock::now(),
232 time::system_clock::now() + time::days(1));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800233
234 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700235 face.onSendData.connect([&](const Data& response) {
236 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500237 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700238 auto contentBlock = response.getContent();
239 contentBlock.parse();
Suyong Won7968f7a2020-05-12 01:01:25 -0700240
tylerliu50d679e2020-10-14 14:08:39 -0700241 BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != "");
242 BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != "");
243 BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != "");
Suyong Won7968f7a2020-05-12 01:01:25 -0700244
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700245 auto challengeBlockCount = 0;
246 for (auto const& element : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700247 if (element.type() == tlv::Challenge) {
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700248 challengeBlockCount++;
Suyong Won7968f7a2020-05-12 01:01:25 -0700249 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700250 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700251
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700252 BOOST_CHECK(challengeBlockCount != 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700253
tylerliu4140fe82021-01-27 15:45:44 -0800254 auto challengeList = state.onNewRenewRevokeResponse(response);
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700255 RequestId requestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700256 std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size());
tylerliu7b9185c2020-11-24 12:15:18 -0800257 auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800258 BOOST_CHECK_EQUAL_COLLECTIONS(state.m_aesKey.begin(), state.m_aesKey.end(),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700259 ca_encryption_key.begin(), ca_encryption_key.end());
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700260 });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700261 face.receive(*interest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800262
263 advanceClocks(time::milliseconds(20), 60);
264 BOOST_CHECK_EQUAL(count, 1);
265}
266
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700267BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1)
268{
Davide Pesavento829aff62022-05-15 20:30:34 -0400269 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700270 auto key = identity.getDefaultKey();
271 auto cert = key.getDefaultCertificate();
272
Davide Pesavento76304d82023-08-10 23:38:06 -0400273 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700274 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1");
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700275 advanceClocks(time::milliseconds(20), 60);
276
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700277 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800278 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500279 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800280 requester::Request state(m_keyChain, item, RequestType::NEW);
Davide Pesavento829aff62022-05-15 20:30:34 -0400281 auto client = m_keyChain.createIdentity(Name("/ndn/zhiyi"));
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700282 auto current_tp = time::system_clock::now();
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800283 auto interest1 = state.genNewInterest(client.getDefaultKey().getName(), current_tp, current_tp - time::hours(1));
284 auto interest2 = state.genNewInterest(client.getDefaultKey().getName(), current_tp, current_tp + time::days(361));
285 auto interest3 = state.genNewInterest(client.getDefaultKey().getName(),
286 current_tp - time::hours(1), current_tp + time::hours(2));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700287 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700288 auto contentTlv = response.getContent();
289 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700290 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700291 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700292 });
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700293 face.receive(*interest1);
294 face.receive(*interest2);
295 face.receive(*interest3);
296
297 advanceClocks(time::milliseconds(20), 60);
298}
299
tylerliuf62cfeb2021-01-28 11:01:33 -0800300BOOST_AUTO_TEST_CASE(HandleNewWithServerBadValidity)
301{
Davide Pesavento829aff62022-05-15 20:30:34 -0400302 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliuf62cfeb2021-01-28 11:01:33 -0800303 auto key = identity.getDefaultKey();
304
305 //build expired cert
Davide Pesavento0dc02012021-11-23 22:55:03 -0500306 Certificate cert;
tylerliuf62cfeb2021-01-28 11:01:33 -0800307 cert.setName(Name(key.getName()).append("self-sign").appendVersion());
308 cert.setContentType(ndn::tlv::ContentType_Key);
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400309 cert.setContent(key.getPublicKey());
tylerliuf62cfeb2021-01-28 11:01:33 -0800310 SignatureInfo signatureInfo;
Davide Pesavento829aff62022-05-15 20:30:34 -0400311 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod::makeRelative(-1_days, -1_s));
tylerliuf62cfeb2021-01-28 11:01:33 -0800312 m_keyChain.sign(cert, signingByKey(key.getName()).setSignatureInfo(signatureInfo));
313 m_keyChain.setDefaultCertificate(key, cert);
314
Davide Pesavento76304d82023-08-10 23:38:06 -0400315 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
tylerliuf62cfeb2021-01-28 11:01:33 -0800316 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
317 advanceClocks(time::milliseconds(20), 60);
318
319 CaProfile item;
320 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500321 item.cert = std::make_shared<Certificate>(cert);
tylerliuf62cfeb2021-01-28 11:01:33 -0800322 requester::Request state(m_keyChain, item, RequestType::NEW);
Davide Pesavento829aff62022-05-15 20:30:34 -0400323 auto interest = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(),
tylerliuf62cfeb2021-01-28 11:01:33 -0800324 time::system_clock::now(),
325 time::system_clock::now() + time::days(1));
326
327 int count = 0;
328 face.onSendData.connect([&](const Data& response) {
329 auto contentTlv = response.getContent();
330 contentTlv.parse();
331 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
332 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
333 count ++;
334 });
335 face.receive(*interest);
336
337 advanceClocks(time::milliseconds(20), 60);
338 BOOST_CHECK_EQUAL(count, 1);
339}
340
tylerliu0b6d0db2020-09-28 17:52:02 -0700341BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix)
342{
Davide Pesavento829aff62022-05-15 20:30:34 -0400343 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu0b6d0db2020-09-28 17:52:02 -0700344 auto key = identity.getDefaultKey();
345 auto cert = key.getDefaultCertificate();
346
Davide Pesavento76304d82023-08-10 23:38:06 -0400347 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700348 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu0b6d0db2020-09-28 17:52:02 -0700349 advanceClocks(time::milliseconds(20), 60);
350
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700351 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800352 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500353 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800354 requester::Request state(m_keyChain, item, RequestType::NEW);
tylerliu0b6d0db2020-09-28 17:52:02 -0700355
Davide Pesavento829aff62022-05-15 20:30:34 -0400356 auto interest1 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a")).getDefaultKey().getName(),
357 time::system_clock::now(),
358 time::system_clock::now() + time::days(1));
359 auto interest2 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a/b")).getDefaultKey().getName(),
360 time::system_clock::now(),
361 time::system_clock::now() + time::days(1));
362 auto interest3 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a/b/c/d")).getDefaultKey().getName(),
363 time::system_clock::now(),
364 time::system_clock::now() + time::days(1));
tylerliu0b6d0db2020-09-28 17:52:02 -0700365
tylerliu0b6d0db2020-09-28 17:52:02 -0700366 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700367 auto contentTlv = response.getContent();
368 contentTlv.parse();
369 if (interest3->getName().isPrefixOf(response.getName())) {
tylerliu50d679e2020-10-14 14:08:39 -0700370 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700371 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
372 }
373 else {
374 // should successfully get responses
Davide Pesavento0dc02012021-11-23 22:55:03 -0500375 BOOST_CHECK_THROW(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)), std::runtime_error);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700376 }
tylerliu0b6d0db2020-09-28 17:52:02 -0700377 });
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700378 face.receive(*interest1);
379 face.receive(*interest2);
380 face.receive(*interest3);
tylerliu0b6d0db2020-09-28 17:52:02 -0700381 advanceClocks(time::milliseconds(20), 60);
tylerliu0b6d0db2020-09-28 17:52:02 -0700382}
383
tylerliu0b6d0db2020-09-28 17:52:02 -0700384BOOST_AUTO_TEST_CASE(HandleNewWithInvalidLength1)
385{
Davide Pesavento829aff62022-05-15 20:30:34 -0400386 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu0b6d0db2020-09-28 17:52:02 -0700387 auto key = identity.getDefaultKey();
388 auto cert = key.getDefaultCertificate();
389
Davide Pesavento76304d82023-08-10 23:38:06 -0400390 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700391 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1");
tylerliu0b6d0db2020-09-28 17:52:02 -0700392 advanceClocks(time::milliseconds(20), 60);
393
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700394 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800395 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500396 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800397 requester::Request state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700398
tylerliu0b6d0db2020-09-28 17:52:02 -0700399 auto current_tp = time::system_clock::now();
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800400 auto interest1 = state.genNewInterest(identity.getDefaultKey().getName(), current_tp, current_tp + time::days(1));
Davide Pesavento829aff62022-05-15 20:30:34 -0400401 auto interest2 = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/a/b/c/d")).getDefaultKey().getName(),
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800402 current_tp, current_tp + time::days(1));
tylerliu0b6d0db2020-09-28 17:52:02 -0700403 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700404 auto contentTlv = response.getContent();
405 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700406 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700407 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
tylerliu0b6d0db2020-09-28 17:52:02 -0700408 });
409 face.receive(*interest1);
410 face.receive(*interest2);
411
412 advanceClocks(time::milliseconds(20), 60);
413}
414
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700415BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800416{
Davide Pesavento829aff62022-05-15 20:30:34 -0400417 auto identity = m_keyChain.createIdentity(Name("/ndn"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700418 auto key = identity.getDefaultKey();
419 auto cert = key.getDefaultCertificate();
420
Davide Pesavento76304d82023-08-10 23:38:06 -0400421 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700422 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800423 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700424
425 // generate NEW Interest
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700426 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800427 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500428 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800429 requester::Request state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700430
Davide Pesavento829aff62022-05-15 20:30:34 -0400431 auto newInterest = state.genNewInterest(m_keyChain.createIdentity(Name("/ndn/zhiyi")).getDefaultKey().getName(),
432 time::system_clock::now(),
433 time::system_clock::now() + time::days(1));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700434
435 // generate CHALLENGE Interest
Davide Pesavento0dc02012021-11-23 22:55:03 -0500436 std::shared_ptr<Interest> challengeInterest;
437 std::shared_ptr<Interest> challengeInterest2;
438 std::shared_ptr<Interest> challengeInterest3;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800439
440 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700441 face.onSendData.connect([&](const Data& response) {
swa770de007bc2020-03-24 21:26:21 -0700442 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
tylerliu4140fe82021-01-27 15:45:44 -0800443 auto challengeList = state.onNewRenewRevokeResponse(response);
444 auto paramList = state.selectOrContinueChallenge("pin");
445 challengeInterest = state.genChallengeInterest(std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700446 }
swa770de007bc2020-03-24 21:26:21 -0700447 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800448 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500449 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800450
tylerliu4140fe82021-01-27 15:45:44 -0800451 state.onChallengeResponse(response);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800452 BOOST_CHECK(state.m_status == Status::CHALLENGE);
453 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE);
tylerliu4140fe82021-01-27 15:45:44 -0800454 auto paramList = state.selectOrContinueChallenge("pin");
455 challengeInterest2 = state.genChallengeInterest(std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700456 }
swa770de007bc2020-03-24 21:26:21 -0700457 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700458 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500459 BOOST_CHECK(verifySignature(response, cert));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700460
tylerliu4140fe82021-01-27 15:45:44 -0800461 state.onChallengeResponse(response);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800462 BOOST_CHECK(state.m_status == Status::CHALLENGE);
463 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400464
tylerliu4140fe82021-01-27 15:45:44 -0800465 auto paramList = state.selectOrContinueChallenge("pin");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700466 auto request = ca.getCertificateRequest(*challengeInterest2);
tylerliu7b9185c2020-11-24 12:15:18 -0800467 auto secret = request->challengeState->secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
tylerliu40226332020-11-11 15:37:16 -0800468 paramList.begin()->second = secret;
tylerliu4140fe82021-01-27 15:45:44 -0800469 challengeInterest3 = state.genChallengeInterest(std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700470 }
swa770de007bc2020-03-24 21:26:21 -0700471 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700472 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500473 BOOST_CHECK(verifySignature(response, cert));
tylerliu4140fe82021-01-27 15:45:44 -0800474 state.onChallengeResponse(response);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800475 BOOST_CHECK(state.m_status == Status::SUCCESS);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700476 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400477 });
Tianyuan Yue78fd4d2022-05-25 13:18:06 -0700478 ca.setStatusUpdateCallback([](const RequestState& request) {
479 if (request.status == Status::SUCCESS && request.requestType == RequestType::NEW) {
480 BOOST_REQUIRE_NO_THROW(Certificate{request.cert});
481 BOOST_CHECK(Certificate(request.cert).isValid());
482 }
483 });
Davide Pesavento914d05f2019-07-13 16:20:19 -0400484
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700485 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800486 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700487 face.receive(*challengeInterest);
488 advanceClocks(time::milliseconds(20), 60);
489 face.receive(*challengeInterest2);
490 advanceClocks(time::milliseconds(20), 60);
491 face.receive(*challengeInterest3);
492 advanceClocks(time::milliseconds(20), 60);
493 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800494}
495
tylerliu182bc532020-09-25 01:54:45 -0700496BOOST_AUTO_TEST_CASE(HandleRevoke)
497{
Davide Pesavento829aff62022-05-15 20:30:34 -0400498 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu182bc532020-09-25 01:54:45 -0700499 auto key = identity.getDefaultKey();
500 auto cert = key.getDefaultCertificate();
501
Davide Pesavento76304d82023-08-10 23:38:06 -0400502 ndn::DummyClientFace face(m_io, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700503 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu182bc532020-09-25 01:54:45 -0700504 advanceClocks(time::milliseconds(20), 60);
505
506 //generate a certificate
507 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
508 auto clientKey = clientIdentity.getDefaultKey();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500509 Certificate clientCert;
tylerliu182bc532020-09-25 01:54:45 -0700510 clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700511 clientCert.setContentType(ndn::tlv::ContentType_Key);
tylerliu182bc532020-09-25 01:54:45 -0700512 clientCert.setFreshnessPeriod(time::hours(24));
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400513 clientCert.setContent(clientKey.getPublicKey());
tylerliu182bc532020-09-25 01:54:45 -0700514 SignatureInfo signatureInfo;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500515 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(),
516 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700517 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800518 RequestId requestId = {{101}};
519 RequestState certRequest;
520 certRequest.caPrefix = Name("/ndn");
521 certRequest.requestId = requestId;
522 certRequest.requestType = RequestType::NEW;
523 certRequest.status = Status::SUCCESS;
524 certRequest.cert = clientCert;
tylerliu182bc532020-09-25 01:54:45 -0700525 auto issuedCert = ca.issueCertificate(certRequest);
526
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700527 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800528 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500529 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800530 requester::Request state(m_keyChain, item, RequestType::REVOKE);
tylerliu182bc532020-09-25 01:54:45 -0700531
tylerliu4140fe82021-01-27 15:45:44 -0800532 auto interest = state.genRevokeInterest(issuedCert);
tylerliu182bc532020-09-25 01:54:45 -0700533
534 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700535 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700536 count++;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500537 BOOST_CHECK(verifySignature(response, cert));
tylerliu182bc532020-09-25 01:54:45 -0700538 auto contentBlock = response.getContent();
539 contentBlock.parse();
540
tylerliu50d679e2020-10-14 14:08:39 -0700541 BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != "");
542 BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != "");
543 BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != "");
tylerliu182bc532020-09-25 01:54:45 -0700544
545 auto challengeBlockCount = 0;
546 for (auto const& element : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700547 if (element.type() == tlv::Challenge) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700548 challengeBlockCount++;
549 }
tylerliu182bc532020-09-25 01:54:45 -0700550 }
551
552 BOOST_CHECK(challengeBlockCount != 0);
553
tylerliu4140fe82021-01-27 15:45:44 -0800554 auto challengeList = state.onNewRenewRevokeResponse(response);
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700555 RequestId requestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700556 std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size());
tylerliu7b9185c2020-11-24 12:15:18 -0800557 auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800558 BOOST_CHECK_EQUAL_COLLECTIONS(state.m_aesKey.begin(), state.m_aesKey.end(),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700559 ca_encryption_key.begin(), ca_encryption_key.end());
tylerliu182bc532020-09-25 01:54:45 -0700560 });
561 face.receive(*interest);
562
563 advanceClocks(time::milliseconds(20), 60);
564 BOOST_CHECK_EQUAL(count, 1);
565}
566
567BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert)
568{
Davide Pesavento829aff62022-05-15 20:30:34 -0400569 auto identity = m_keyChain.createIdentity(Name("/ndn"));
tylerliu182bc532020-09-25 01:54:45 -0700570 auto key = identity.getDefaultKey();
571 auto cert = key.getDefaultCertificate();
572
Davide Pesavento76304d82023-08-10 23:38:06 -0400573 ndn::DummyClientFace face(m_io, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700574 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu182bc532020-09-25 01:54:45 -0700575 advanceClocks(time::milliseconds(20), 60);
576
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700577 // generate a certificate
tylerliu182bc532020-09-25 01:54:45 -0700578 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
579 auto clientKey = clientIdentity.getDefaultKey();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500580 Certificate clientCert;
581 clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append("1473283247810732701"));
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700582 clientCert.setContentType(ndn::tlv::ContentType_Key);
tylerliu182bc532020-09-25 01:54:45 -0700583 clientCert.setFreshnessPeriod(time::hours(24));
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400584 clientCert.setContent(clientKey.getPublicKey());
tylerliu182bc532020-09-25 01:54:45 -0700585 SignatureInfo signatureInfo;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500586 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(),
587 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700588 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
589
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700590 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800591 item.caPrefix = Name("/ndn");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500592 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800593 requester::Request state(m_keyChain, item, RequestType::NEW);
tylerliu182bc532020-09-25 01:54:45 -0700594
tylerliu4140fe82021-01-27 15:45:44 -0800595 auto interest = state.genRevokeInterest(clientCert);
tylerliu182bc532020-09-25 01:54:45 -0700596
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700597 bool receiveData = false;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700598 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700599 receiveData = true;
600 auto contentTlv = response.getContent();
601 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700602 BOOST_CHECK(static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))) != ErrorCode::NO_ERROR);
tylerliu182bc532020-09-25 01:54:45 -0700603 });
604 face.receive(*interest);
605
606 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700607 BOOST_CHECK_EQUAL(receiveData, true);
tylerliu182bc532020-09-25 01:54:45 -0700608}
609
Davide Pesavento0dc02012021-11-23 22:55:03 -0500610BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800611
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400612} // namespace ndncert::tests