blob: ed4f8f34393e8e34b795c587b73b4b1e1c215777 [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/*
swa770de007bc2020-03-24 21:26:21 -07003 * Copyright (c) 2017-2020, 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"
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070026#include "requester.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070027#include "test-common.hpp"
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080028
29namespace ndn {
30namespace ndncert {
31namespace tests {
32
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070033using namespace ca;
34
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070035BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080036
37BOOST_AUTO_TEST_CASE(Initialization)
38{
Zhiyi Zhang22998612020-09-25 14:43:23 -070039 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070040 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080041 BOOST_CHECK_EQUAL(ca.getCaConf().caProfile.caPrefix, "/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080042
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080043 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070044 BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 1); // removed local discovery registration
Zhiyi Zhang696cd042020-10-07 21:27:36 -070045 BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 5); // infoMeta, onProbe, onNew, onChallenge, onRevoke
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080046}
47
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070048BOOST_AUTO_TEST_CASE(HandleProfileFetching)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049{
Zhiyi Zhang615b6b72021-01-11 14:25:32 -080050 name::setConventionEncoding(name::Convention::TYPED);
51
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052 auto identity = addIdentity(Name("/ndn"));
53 auto key = identity.getDefaultKey();
54 auto cert = key.getDefaultCertificate();
55
Zhiyi Zhang22998612020-09-25 14:43:23 -070056 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070057 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070059 auto profileData = ca.getCaProfileData();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070061 Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
62 shared_ptr<Interest> infoInterest = nullptr;
63
Zhiyi Zhang696cd042020-10-07 21:27:36 -070064 face.setInterestFilter(
65 InterestFilter("/ndn/CA/INFO"),
66 [&](const auto&, const Interest& interest) {
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070067 if (interest.getName() == profileData.getName()) {
68 face.put(profileData);
69 }
70 },
71 nullptr, nullptr);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070072 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073
74 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070075 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070076 if (count == 0) {
77 count++;
78 auto block = response.getContent();
79 block.parse();
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070080 infoInterest =std::make_shared<Interest>(Name(block.get(ndn::tlv::Name)).appendSegment(0));
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070081 infoInterest->setCanBePrefix(false);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070082 }
83 else {
84 count++;
85 BOOST_CHECK(security::verifySignature(response, cert));
86 auto contentBlock = response.getContent();
87 contentBlock.parse();
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080088 auto caItem = infotlv::decodeDataContent(contentBlock);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080089 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 Zhangfbcab842020-10-07 15:17:13 -070094 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070095 });
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080096 face.receive(interest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080097 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070098 face.receive(*infoInterest);
99 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700100
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700101 BOOST_CHECK_EQUAL(count, 2);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800102}
103
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700104BOOST_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 Zhang9829da92020-09-30 16:19:34 -0700111 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700112 advanceClocks(time::milliseconds(20), 60);
113
114 Interest interest("/ndn/CA/PROBE");
115 interest.setCanBePrefix(false);
116
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700117 Block paramTLV = makeEmptyBlock(ndn::tlv::ApplicationParameters);
tylerliu50d679e2020-10-14 14:08:39 -0700118 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, "name"));
119 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi"));
Zhiyi Zhang6f395812020-09-29 14:42:03 -0700120 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();
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{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700144 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700145 auto key = identity.getDefaultKey();
146 auto cert = key.getDefaultCertificate();
147
Zhiyi Zhang22998612020-09-25 14:43:23 -0700148 util::DummyClientFace face(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");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700153 interest.setCanBePrefix(false);
Suyong Won7968f7a2020-05-12 01:01:25 -0700154
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700155 Block paramTLV = makeEmptyBlock(ndn::tlv::ApplicationParameters);
tylerliu50d679e2020-10-14 14:08:39 -0700156 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, "name"));
157 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi"));
Suyong Won7968f7a2020-05-12 01:01:25 -0700158 paramTLV.encode();
159
160 interest.setApplicationParameters(paramTLV);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700161
162 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700163 face.onSendData.connect([&](const Data& response) {
164 count++;
165 BOOST_CHECK(security::verifySignature(response, cert));
166 auto contentBlock = response.getContent();
167 contentBlock.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700168 auto probeResponseBlock = contentBlock.get(tlv::ProbeResponse);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700169 probeResponseBlock.parse();
170 Name caPrefix;
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700171 caPrefix.wireDecode(probeResponseBlock.get(ndn::tlv::Name));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700172 BOOST_CHECK(caPrefix != "");
173 });
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700174 face.receive(interest);
175
176 advanceClocks(time::milliseconds(20), 60);
177 BOOST_CHECK_EQUAL(count, 1);
178}
179
Suyong Wone2afeb52020-10-04 03:05:39 +0900180BOOST_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 Wone2afeb52020-10-04 03:05:39 +0900188 advanceClocks(time::milliseconds(20), 60);
189
190 Interest interest("/ndn/CA/PROBE");
191 interest.setCanBePrefix(false);
192
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700193 Block paramTLV = makeEmptyBlock(ndn::tlv::ApplicationParameters);
tylerliu50d679e2020-10-14 14:08:39 -0700194 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, "name"));
195 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi"));
Suyong Wone2afeb52020-10-04 03:05:39 +0900196 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 Zhang34a8d432020-10-03 22:14:25 -0700208 std::vector<Name> redirectionItems;
209 for (auto item : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700210 if (item.type() == tlv::ProbeRedirect) {
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700211 redirectionItems.push_back(Name(item.blockFromValue()));
212 }
Suyong Wone2afeb52020-10-04 03:05:39 +0900213 }
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700214 BOOST_CHECK_EQUAL(redirectionItems.size(), 2);
tylerliua7bea662020-10-08 18:51:02 -0700215 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirectionItems[0].getPrefix(-1)), "/ndn/site1");
216 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirectionItems[1].getPrefix(-1)), "/ndn/site1");
Suyong Wone2afeb52020-10-04 03:05:39 +0900217 });
218 face.receive(interest);
219 advanceClocks(time::milliseconds(20), 60);
220 BOOST_CHECK_EQUAL(count, 1);
221}
222
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800223BOOST_AUTO_TEST_CASE(HandleNew)
224{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700225 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800226 auto key = identity.getDefaultKey();
227 auto cert = key.getDefaultCertificate();
228
Zhiyi Zhang22998612020-09-25 14:43:23 -0700229 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700230 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800231 advanceClocks(time::milliseconds(20), 60);
232
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700233 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800234 item.caPrefix = Name("/ndn");
235 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800236 requester::RequestState state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700237 auto interest = requester::Requester::genNewInterest(state, Name("/ndn/zhiyi"),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700238 time::system_clock::now(),
239 time::system_clock::now() + time::days(1));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800240
241 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700242 face.onSendData.connect([&](const Data& response) {
243 count++;
244 BOOST_CHECK(security::verifySignature(response, cert));
245 auto contentBlock = response.getContent();
246 contentBlock.parse();
Suyong Won7968f7a2020-05-12 01:01:25 -0700247
tylerliu50d679e2020-10-14 14:08:39 -0700248 BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != "");
249 BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != "");
250 BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != "");
Suyong Won7968f7a2020-05-12 01:01:25 -0700251
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700252 auto challengeBlockCount = 0;
253 for (auto const& element : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700254 if (element.type() == tlv::Challenge) {
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700255 challengeBlockCount++;
Suyong Won7968f7a2020-05-12 01:01:25 -0700256 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700257 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700258
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700259 BOOST_CHECK(challengeBlockCount != 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700260
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700261 auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, response);
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700262 RequestId requestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700263 std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size());
tylerliu7b9185c2020-11-24 12:15:18 -0800264 auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey;
tylerliuf2e6bb52020-12-13 13:23:05 -0800265 BOOST_CHECK_EQUAL_COLLECTIONS(state.aesKey.begin(), state.aesKey.end(),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700266 ca_encryption_key.begin(), ca_encryption_key.end());
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700267 });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700268 face.receive(*interest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800269
270 advanceClocks(time::milliseconds(20), 60);
271 BOOST_CHECK_EQUAL(count, 1);
272}
273
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700274BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1)
275{
276 auto identity = addIdentity(Name("/ndn"));
277 auto key = identity.getDefaultKey();
278 auto cert = key.getDefaultCertificate();
279
Zhiyi Zhang22998612020-09-25 14:43:23 -0700280 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700281 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1");
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700282 advanceClocks(time::milliseconds(20), 60);
283
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700284 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800285 item.caPrefix = Name("/ndn");
286 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800287 requester::RequestState state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700288 auto current_tp = time::system_clock::now();
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700289 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 Zhangfc1678a2020-05-12 16:52:14 -0700292 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700293 auto contentTlv = response.getContent();
294 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700295 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700296 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700297 });
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700298 face.receive(*interest1);
299 face.receive(*interest2);
300 face.receive(*interest3);
301
302 advanceClocks(time::milliseconds(20), 60);
303}
304
tylerliu0b6d0db2020-09-28 17:52:02 -0700305BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix)
306{
307 auto identity = addIdentity(Name("/ndn"));
308 auto key = identity.getDefaultKey();
309 auto cert = key.getDefaultCertificate();
310
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700311 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700312 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu0b6d0db2020-09-28 17:52:02 -0700313 advanceClocks(time::milliseconds(20), 60);
314
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700315 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800316 item.caPrefix = Name("/ndn");
317 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800318 requester::RequestState state(m_keyChain, item, RequestType::NEW);
tylerliu0b6d0db2020-09-28 17:52:02 -0700319
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700320 auto interest1 = requester::Requester::genNewInterest(state, Name("/ndn/a"), time::system_clock::now(),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700321 time::system_clock::now() + time::days(1));
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700322 auto interest2 = requester::Requester::genNewInterest(state, Name("/ndn/a/b"), time::system_clock::now(),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700323 time::system_clock::now() + time::days(1));
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700324 auto interest3 = requester::Requester::genNewInterest(state, Name("/ndn/a/b/c/d"), time::system_clock::now(),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700325 time::system_clock::now() + time::days(1));
tylerliu0b6d0db2020-09-28 17:52:02 -0700326
tylerliu0b6d0db2020-09-28 17:52:02 -0700327 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700328 auto contentTlv = response.getContent();
329 contentTlv.parse();
330 if (interest3->getName().isPrefixOf(response.getName())) {
tylerliu50d679e2020-10-14 14:08:39 -0700331 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700332 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
333 }
334 else {
335 // should successfully get responses
tylerliu50d679e2020-10-14 14:08:39 -0700336 BOOST_CHECK_EXCEPTION(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)), std::runtime_error,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700337 [](const auto& e) { return true; });
338 }
tylerliu0b6d0db2020-09-28 17:52:02 -0700339 });
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700340 face.receive(*interest1);
341 face.receive(*interest2);
342 face.receive(*interest3);
tylerliu0b6d0db2020-09-28 17:52:02 -0700343 advanceClocks(time::milliseconds(20), 60);
tylerliu0b6d0db2020-09-28 17:52:02 -0700344}
345
tylerliu0b6d0db2020-09-28 17:52:02 -0700346BOOST_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 Zhang9829da92020-09-30 16:19:34 -0700353 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1");
tylerliu0b6d0db2020-09-28 17:52:02 -0700354 advanceClocks(time::milliseconds(20), 60);
355
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700356 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800357 item.caPrefix = Name("/ndn");
358 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800359 requester::RequestState state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700360
tylerliu0b6d0db2020-09-28 17:52:02 -0700361 auto current_tp = time::system_clock::now();
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700362 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));
tylerliu0b6d0db2020-09-28 17:52:02 -0700364 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700365 auto contentTlv = response.getContent();
366 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700367 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode)));
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700368 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
tylerliu0b6d0db2020-09-28 17:52:02 -0700369 });
370 face.receive(*interest1);
371 face.receive(*interest2);
372
373 advanceClocks(time::milliseconds(20), 60);
374}
375
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700376BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800377{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700378 auto identity = addIdentity(Name("/ndn"));
379 auto key = identity.getDefaultKey();
380 auto cert = key.getDefaultCertificate();
381
Zhiyi Zhang22998612020-09-25 14:43:23 -0700382 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700383 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800384 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700385
386 // generate NEW Interest
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700387 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800388 item.caPrefix = Name("/ndn");
389 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800390 requester::RequestState state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700391
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700392 auto newInterest = requester::Requester::genNewInterest(state, Name("/ndn/zhiyi"), time::system_clock::now(),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700393 time::system_clock::now() + time::days(1));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700394
395 // generate CHALLENGE Interest
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700396 shared_ptr<Interest> challengeInterest = nullptr;
397 shared_ptr<Interest> challengeInterest2 = nullptr;
398 shared_ptr<Interest> challengeInterest3 = nullptr;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800399
400 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700401 face.onSendData.connect([&](const Data& response) {
swa770de007bc2020-03-24 21:26:21 -0700402 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700403 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 Zhangaf7c2902019-03-14 22:13:21 -0700406 }
swa770de007bc2020-03-24 21:26:21 -0700407 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800408 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700409 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800410
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700411 requester::Requester::onChallengeResponse(state, response);
tylerliuf2e6bb52020-12-13 13:23:05 -0800412 BOOST_CHECK(state.status == Status::CHALLENGE);
413 BOOST_CHECK_EQUAL(state.challengeStatus, ChallengePin::NEED_CODE);
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700414 auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin");
415 challengeInterest2 = requester::Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700416 }
swa770de007bc2020-03-24 21:26:21 -0700417 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700418 count++;
419 BOOST_CHECK(security::verifySignature(response, cert));
420
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700421 requester::Requester::onChallengeResponse(state, response);
tylerliuf2e6bb52020-12-13 13:23:05 -0800422 BOOST_CHECK(state.status == Status::CHALLENGE);
423 BOOST_CHECK_EQUAL(state.challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400424
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700425 auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700426 auto request = ca.getCertificateRequest(*challengeInterest2);
tylerliu7b9185c2020-11-24 12:15:18 -0800427 auto secret = request->challengeState->secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
tylerliu40226332020-11-11 15:37:16 -0800428 paramList.begin()->second = secret;
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700429 challengeInterest3 = requester::Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700430 }
swa770de007bc2020-03-24 21:26:21 -0700431 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700432 count++;
433 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700434 requester::Requester::onChallengeResponse(state, response);
tylerliuf2e6bb52020-12-13 13:23:05 -0800435 BOOST_CHECK(state.status == Status::SUCCESS);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700436 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400437 });
438
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700439 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800440 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700441 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 Zhang1c0bd372017-12-18 18:32:55 +0800448}
449
tylerliu182bc532020-09-25 01:54:45 -0700450BOOST_AUTO_TEST_CASE(HandleRevoke)
451{
452 auto identity = addIdentity(Name("/ndn"));
453 auto key = identity.getDefaultKey();
454 auto cert = key.getDefaultCertificate();
455
Zhiyi Zhang46049832020-09-28 17:08:12 -0700456 util::DummyClientFace face(io, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700457 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu182bc532020-09-25 01:54:45 -0700458 advanceClocks(time::milliseconds(20), 60);
459
460 //generate a certificate
461 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
462 auto clientKey = clientIdentity.getDefaultKey();
tylerliua7bea662020-10-08 18:51:02 -0700463 security::Certificate clientCert;
tylerliu182bc532020-09-25 01:54:45 -0700464 clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700465 clientCert.setContentType(ndn::tlv::ContentType_Key);
tylerliu182bc532020-09-25 01:54:45 -0700466 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 Zhang46049832020-09-28 17:08:12 -0700470 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700471 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800472 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;
tylerliu182bc532020-09-25 01:54:45 -0700479 auto issuedCert = ca.issueCertificate(certRequest);
480
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700481 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800482 item.caPrefix = Name("/ndn");
483 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800484 requester::RequestState state(m_keyChain, item, RequestType::REVOKE);
tylerliu182bc532020-09-25 01:54:45 -0700485
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700486 auto interest = requester::Requester::genRevokeInterest(state, issuedCert);
tylerliu182bc532020-09-25 01:54:45 -0700487
488 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700489 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700490 count++;
491 BOOST_CHECK(security::verifySignature(response, cert));
492 auto contentBlock = response.getContent();
493 contentBlock.parse();
494
tylerliu50d679e2020-10-14 14:08:39 -0700495 BOOST_CHECK(readString(contentBlock.get(tlv::EcdhPub)) != "");
496 BOOST_CHECK(readString(contentBlock.get(tlv::Salt)) != "");
497 BOOST_CHECK(readString(contentBlock.get(tlv::RequestId)) != "");
tylerliu182bc532020-09-25 01:54:45 -0700498
499 auto challengeBlockCount = 0;
500 for (auto const& element : contentBlock.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700501 if (element.type() == tlv::Challenge) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700502 challengeBlockCount++;
503 }
tylerliu182bc532020-09-25 01:54:45 -0700504 }
505
506 BOOST_CHECK(challengeBlockCount != 0);
507
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700508 auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, response);
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700509 RequestId requestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700510 std::memcpy(requestId.data(), contentBlock.get(tlv::RequestId).value(), contentBlock.get(tlv::RequestId).value_size());
tylerliu7b9185c2020-11-24 12:15:18 -0800511 auto ca_encryption_key = ca.getCaStorage()->getRequest(requestId).encryptionKey;
tylerliuf2e6bb52020-12-13 13:23:05 -0800512 BOOST_CHECK_EQUAL_COLLECTIONS(state.aesKey.begin(), state.aesKey.end(),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700513 ca_encryption_key.begin(), ca_encryption_key.end());
tylerliu182bc532020-09-25 01:54:45 -0700514 });
515 face.receive(*interest);
516
517 advanceClocks(time::milliseconds(20), 60);
518 BOOST_CHECK_EQUAL(count, 1);
519}
520
521BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert)
522{
523 auto identity = addIdentity(Name("/ndn"));
524 auto key = identity.getDefaultKey();
525 auto cert = key.getDefaultCertificate();
526
Zhiyi Zhang46049832020-09-28 17:08:12 -0700527 util::DummyClientFace face(io, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700528 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
tylerliu182bc532020-09-25 01:54:45 -0700529 advanceClocks(time::milliseconds(20), 60);
530
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700531 // generate a certificate
tylerliu182bc532020-09-25 01:54:45 -0700532 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
533 auto clientKey = clientIdentity.getDefaultKey();
tylerliua7bea662020-10-08 18:51:02 -0700534 security::Certificate clientCert;
tylerliu182bc532020-09-25 01:54:45 -0700535 clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append(std::to_string(1473283247810732701)));
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700536 clientCert.setContentType(ndn::tlv::ContentType_Key);
tylerliu182bc532020-09-25 01:54:45 -0700537 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 Zhang46049832020-09-28 17:08:12 -0700541 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700542 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
543
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700544 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800545 item.caPrefix = Name("/ndn");
546 item.cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800547 requester::RequestState state(m_keyChain, item, RequestType::NEW);
tylerliu182bc532020-09-25 01:54:45 -0700548
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700549 auto interest = requester::Requester::genRevokeInterest(state, clientCert);
tylerliu182bc532020-09-25 01:54:45 -0700550
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700551 bool receiveData = false;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700552 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700553 receiveData = true;
554 auto contentTlv = response.getContent();
555 contentTlv.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700556 BOOST_CHECK(static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv::ErrorCode))) != ErrorCode::NO_ERROR);
tylerliu182bc532020-09-25 01:54:45 -0700557 });
558 face.receive(*interest);
559
560 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700561 BOOST_CHECK_EQUAL(receiveData, true);
tylerliu182bc532020-09-25 01:54:45 -0700562}
563
Zhiyi Zhang46049832020-09-28 17:08:12 -0700564BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800565
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700566} // namespace tests
567} // namespace ndncert
568} // namespace ndn