blob: 7a7d3dd1cc90cd495cc8e3edf8ace4223072f87b [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 Zhangf5246c42017-01-26 09:39:20 -080022#include "challenge-module.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070023#include "challenge-module/challenge-email.hpp"
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070024#include "challenge-module/challenge-pin.hpp"
25#include "client-module.hpp"
Suyong Won7968f7a2020-05-12 01:01:25 -070026#include "protocol-detail/info.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 Zhangae123bf2017-04-14 12:24:53 -070033BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080034
35BOOST_AUTO_TEST_CASE(Initialization)
36{
Zhiyi Zhang22998612020-09-25 14:43:23 -070037 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070038 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Suyong Won44d0cce2020-05-10 04:07:43 -070039 BOOST_CHECK_EQUAL(ca.getCaConf().m_caPrefix, "/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080040
41 auto identity = addIdentity(Name("/ndn/site2"));
42 auto key = identity.getDefaultKey();
43 auto cert = key.getDefaultCertificate();
44 ca.getCaStorage()->addCertificate("111", cert);
45 BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2"));
46
47 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 2);
Zhiyi Zhang46049832020-09-28 17:08:12 -070049 BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 5); // onInfo, onProbe, onNew, onChallenge, onRevoke
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050}
51
Zhiyi Zhang46049832020-09-28 17:08:12 -070052BOOST_AUTO_TEST_CASE(HandleProbe)
53{
54 auto identity = addIdentity(Name("/ndn"));
55 auto key = identity.getDefaultKey();
56 auto cert = key.getDefaultCertificate();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080057
Zhiyi Zhang46049832020-09-28 17:08:12 -070058 util::DummyClientFace face(io, m_keyChain, {true, true});
59 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
60 ca.setNameAssignmentFunction([&](const std::vector<std::tuple<std::string, std::string>>) -> std::vector<std::string> {
61 std::vector<std::string> result;
62 result.push_back("example");
63 return result;
64 });
65 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080066
Zhiyi Zhang46049832020-09-28 17:08:12 -070067 Interest interest("/ndn/CA/PROBE");
68 interest.setCanBePrefix(false);
Suyong Won44d0cce2020-05-10 04:07:43 -070069
Zhiyi Zhang46049832020-09-28 17:08:12 -070070 Block paramTLV = makeEmptyBlock(tlv::ApplicationParameters);
71 paramTLV.push_back(makeStringBlock(tlv_parameter_key, JSON_CLIENT_PROBE_INFO));
72 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi"));
73 paramTLV.encode();
Suyong Won7968f7a2020-05-12 01:01:25 -070074
Zhiyi Zhang46049832020-09-28 17:08:12 -070075 interest.setApplicationParameters(paramTLV);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080076
Zhiyi Zhang46049832020-09-28 17:08:12 -070077 int count = 0;
78 face.onSendData.connect([&](const Data& response) {
79 count++;
80 BOOST_CHECK(security::verifySignature(response, cert));
81 Block contentBlock = response.getContent();
82 contentBlock.parse();
83 Block probeResponse = contentBlock.get(tlv_probe_response);
84 probeResponse.parse();
85 Name caName;
86 caName.wireDecode(probeResponse.get(tlv::Name));
87 BOOST_CHECK_EQUAL(caName, "/ndn/example");
88 });
89 face.receive(interest);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090
Zhiyi Zhang46049832020-09-28 17:08:12 -070091 advanceClocks(time::milliseconds(20), 60);
92 BOOST_CHECK_EQUAL(count, 1);
93}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070094
swa77020643ac2020-03-26 02:24:45 -070095BOOST_AUTO_TEST_CASE(HandleInfo)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096{
97 auto identity = addIdentity(Name("/ndn"));
98 auto key = identity.getDefaultKey();
99 auto cert = key.getDefaultCertificate();
100
Zhiyi Zhang22998612020-09-25 14:43:23 -0700101 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700102 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103 advanceClocks(time::milliseconds(20), 60);
104
swa77020643ac2020-03-26 02:24:45 -0700105 Interest interest("/ndn/CA/INFO");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700106 interest.setCanBePrefix(false);
107
108 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700109 face.onSendData.connect([&](const Data& response) {
110 count++;
111 BOOST_CHECK(security::verifySignature(response, cert));
112 auto contentBlock = response.getContent();
113 contentBlock.parse();
114 auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
115 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
116 BOOST_CHECK_EQUAL(caItem.m_probe, "");
117 BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
118 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
119 });
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800120 face.receive(interest);
121
122 advanceClocks(time::milliseconds(20), 60);
123 BOOST_CHECK_EQUAL(count, 1);
124}
125
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700126BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler)
127{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700129 auto key = identity.getDefaultKey();
130 auto cert = key.getDefaultCertificate();
131
Zhiyi Zhang22998612020-09-25 14:43:23 -0700132 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700133 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700134 advanceClocks(time::milliseconds(20), 60);
135
swa770de007bc2020-03-24 21:26:21 -0700136 Interest interest("/ndn/CA/PROBE");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700137 interest.setCanBePrefix(false);
Suyong Won7968f7a2020-05-12 01:01:25 -0700138
139 Block paramTLV = makeEmptyBlock(tlv::ApplicationParameters);
140 paramTLV.push_back(makeStringBlock(tlv_parameter_key, JSON_CLIENT_PROBE_INFO));
141 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi"));
142 paramTLV.encode();
143
144 interest.setApplicationParameters(paramTLV);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700145
146 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700147 face.onSendData.connect([&](const Data& response) {
148 count++;
149 BOOST_CHECK(security::verifySignature(response, cert));
150 auto contentBlock = response.getContent();
151 contentBlock.parse();
152 auto probeResponseBlock = contentBlock.get(tlv_probe_response);
153 probeResponseBlock.parse();
154 Name caPrefix;
155 caPrefix.wireDecode(probeResponseBlock.get(tlv::Name));
156 BOOST_CHECK(caPrefix != "");
157 });
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700158 face.receive(interest);
159
160 advanceClocks(time::milliseconds(20), 60);
161 BOOST_CHECK_EQUAL(count, 1);
162}
163
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800164BOOST_AUTO_TEST_CASE(HandleNew)
165{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700166 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800167 auto key = identity.getDefaultKey();
168 auto cert = key.getDefaultCertificate();
169
Zhiyi Zhang22998612020-09-25 14:43:23 -0700170 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700171 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800172 advanceClocks(time::milliseconds(20), 60);
173
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700174 ClientModule client(m_keyChain);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800175 ClientCaItem item;
Suyong Won256c9062020-05-11 02:45:56 -0700176 item.m_caPrefix = Name("/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800177 item.m_anchor = cert;
178 client.getClientConf().m_caItems.push_back(item);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700179
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700180 auto interest = client.generateNewInterest(time::system_clock::now(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700181 time::system_clock::now() + time::days(1),
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700182 Name("/ndn/zhiyi"));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800183
184 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700185 face.onSendData.connect([&](const Data& response) {
186 count++;
187 BOOST_CHECK(security::verifySignature(response, cert));
188 auto contentBlock = response.getContent();
189 contentBlock.parse();
Suyong Won7968f7a2020-05-12 01:01:25 -0700190
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700191 BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != "");
192 BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != "");
193 BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != "");
Suyong Won7968f7a2020-05-12 01:01:25 -0700194
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700195 auto challengeBlockCount = 0;
196 for (auto const& element : contentBlock.elements()) {
197 if (element.type() == tlv_challenge) {
198 challengeBlockCount++;
Suyong Won7968f7a2020-05-12 01:01:25 -0700199 }
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700200 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700201
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700202 BOOST_CHECK(challengeBlockCount != 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700203
tylerliu0e176c32020-09-29 11:39:46 -0700204 client.onNewRenewRevokeResponse(response);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700205 BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey),
206 ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey));
207 });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700208 face.receive(*interest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800209
210 advanceClocks(time::milliseconds(20), 60);
211 BOOST_CHECK_EQUAL(count, 1);
212}
213
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700214BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1)
215{
216 auto identity = addIdentity(Name("/ndn"));
217 auto key = identity.getDefaultKey();
218 auto cert = key.getDefaultCertificate();
219
Zhiyi Zhang22998612020-09-25 14:43:23 -0700220 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700221 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
222 advanceClocks(time::milliseconds(20), 60);
223
224 ClientModule client(m_keyChain);
225 ClientCaItem item;
Suyong Won256c9062020-05-11 02:45:56 -0700226 item.m_caPrefix = Name("/ndn");
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700227 item.m_anchor = cert;
228 client.getClientConf().m_caItems.push_back(item);
229 auto current_tp = time::system_clock::now();
230 auto interest1 = client.generateNewInterest(current_tp, current_tp - time::hours(1),
231 Name("/ndn/zhiyi"));
232 auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(361),
233 Name("/ndn/zhiyi"));
234 auto interest3 = client.generateNewInterest(current_tp - time::hours(1),
235 current_tp + time::hours(2),
236 Name("/ndn/zhiyi"));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700237 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700238 auto contentTlv = response.getContent();
239 contentTlv.parse();
240 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv_error_code)));
241 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700242 });
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700243 face.receive(*interest1);
244 face.receive(*interest2);
245 face.receive(*interest3);
246
247 advanceClocks(time::milliseconds(20), 60);
248}
249
tylerliu0b6d0db2020-09-28 17:52:02 -0700250BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix)
251{
252 auto identity = addIdentity(Name("/ndn"));
253 auto key = identity.getDefaultKey();
254 auto cert = key.getDefaultCertificate();
255
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700256 util::DummyClientFace face(io, m_keyChain, {true, true});
tylerliu0b6d0db2020-09-28 17:52:02 -0700257 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
258 advanceClocks(time::milliseconds(20), 60);
259
260 ClientModule client(m_keyChain);
261 ClientCaItem item;
262 item.m_caPrefix = Name("/ndn");
263 item.m_anchor = cert;
264 client.getClientConf().m_caItems.push_back(item);
265
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700266 auto interest1 = client.generateNewInterest(time::system_clock::now(),
267 time::system_clock::now() + time::days(1),
268 Name("/ndn/a"));
269 auto interest2 = client.generateNewInterest(time::system_clock::now(),
270 time::system_clock::now() + time::days(1),
271 Name("/ndn/a/b"));
272 auto interest3 = client.generateNewInterest(time::system_clock::now(),
273 time::system_clock::now() + time::days(1),
274 Name("/ndn/a/b/c/d"));
tylerliu0b6d0db2020-09-28 17:52:02 -0700275
tylerliu0b6d0db2020-09-28 17:52:02 -0700276 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700277 auto contentTlv = response.getContent();
278 contentTlv.parse();
279 if (interest3->getName().isPrefixOf(response.getName())) {
280 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv_error_code)));
281 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
282 }
283 else {
284 // should successfully get responses
285 BOOST_CHECK_EXCEPTION(readNonNegativeInteger(contentTlv.get(tlv_error_code)), std::runtime_error,
286 [](const auto& e) { return true; });
287 }
tylerliu0b6d0db2020-09-28 17:52:02 -0700288 });
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700289 face.receive(*interest1);
290 face.receive(*interest2);
291 face.receive(*interest3);
tylerliu0b6d0db2020-09-28 17:52:02 -0700292 advanceClocks(time::milliseconds(20), 60);
tylerliu0b6d0db2020-09-28 17:52:02 -0700293}
294
tylerliu0b6d0db2020-09-28 17:52:02 -0700295BOOST_AUTO_TEST_CASE(HandleNewWithInvalidLength1)
296{
297 auto identity = addIdentity(Name("/ndn"));
298 auto key = identity.getDefaultKey();
299 auto cert = key.getDefaultCertificate();
300
301 util::DummyClientFace face(io, m_keyChain, {true, true});
302 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
303 advanceClocks(time::milliseconds(20), 60);
304
305 ClientModule client(m_keyChain);
306 ClientCaItem item;
307 item.m_caPrefix = Name("/ndn");
308 item.m_anchor = cert;
309 client.getClientConf().m_caItems.push_back(item);
310 auto current_tp = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700311 auto interest1 = client.generateNewInterest(current_tp, current_tp + time::days(1), Name("/ndn"));
312 auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(1), Name("/ndn/a/b/c/d"));
tylerliu0b6d0db2020-09-28 17:52:02 -0700313 face.onSendData.connect([&](const Data& response) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700314 auto contentTlv = response.getContent();
315 contentTlv.parse();
316 auto errorCode = static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv_error_code)));
317 BOOST_CHECK(errorCode != ErrorCode::NO_ERROR);
tylerliu0b6d0db2020-09-28 17:52:02 -0700318 });
319 face.receive(*interest1);
320 face.receive(*interest2);
321
322 advanceClocks(time::milliseconds(20), 60);
323}
324
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700325BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800326{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700327 auto identity = addIdentity(Name("/ndn"));
328 auto key = identity.getDefaultKey();
329 auto cert = key.getDefaultCertificate();
330
Zhiyi Zhang22998612020-09-25 14:43:23 -0700331 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700332 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800333 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700334
335 // generate NEW Interest
336 ClientModule client(m_keyChain);
337 ClientCaItem item;
Suyong Won256c9062020-05-11 02:45:56 -0700338 item.m_caPrefix = Name("/ndn");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700339 item.m_anchor = cert;
340 client.getClientConf().m_caItems.push_back(item);
341 auto newInterest = client.generateNewInterest(time::system_clock::now(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700342 time::system_clock::now() + time::days(1), Name("/ndn/zhiyi"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700343
344 // generate CHALLENGE Interest
345 ChallengePin pinChallenge;
346 shared_ptr<Interest> challengeInterest = nullptr;
347 shared_ptr<Interest> challengeInterest2 = nullptr;
348 shared_ptr<Interest> challengeInterest3 = nullptr;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800349
350 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700351 face.onSendData.connect([&](const Data& response) {
swa770de007bc2020-03-24 21:26:21 -0700352 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
tylerliu0e176c32020-09-29 11:39:46 -0700353 client.onNewRenewRevokeResponse(response);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700354 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Suyong Won44d0cce2020-05-10 04:07:43 -0700355 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700356 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700357 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700358 }
swa770de007bc2020-03-24 21:26:21 -0700359 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800360 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700361 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800362
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700363 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700364 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700365 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400366
Zhiyi Zhang46049832020-09-28 17:08:12 -0700367 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Suyong Won44d0cce2020-05-10 04:07:43 -0700368 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700369 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700370 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700371 }
swa770de007bc2020-03-24 21:26:21 -0700372 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700373 count++;
374 BOOST_CHECK(security::verifySignature(response, cert));
375
376 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700377 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700378 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400379
Zhiyi Zhang46049832020-09-28 17:08:12 -0700380 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700381 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700382 auto secret = request.m_challengeSecrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
383 std::get<1>(paramList[0]) = secret;
Suyong Won44d0cce2020-05-10 04:07:43 -0700384 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700385 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700386 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700387 }
swa770de007bc2020-03-24 21:26:21 -0700388 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700389 count++;
390 BOOST_CHECK(security::verifySignature(response, cert));
391
392 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700393 BOOST_CHECK(client.m_status == Status::SUCCESS);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700394 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400395 });
396
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700397 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800398 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700399 face.receive(*challengeInterest);
400 advanceClocks(time::milliseconds(20), 60);
401 face.receive(*challengeInterest2);
402 advanceClocks(time::milliseconds(20), 60);
403 face.receive(*challengeInterest3);
404 advanceClocks(time::milliseconds(20), 60);
405 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800406}
407
tylerliu182bc532020-09-25 01:54:45 -0700408BOOST_AUTO_TEST_CASE(HandleRevoke)
409{
410 auto identity = addIdentity(Name("/ndn"));
411 auto key = identity.getDefaultKey();
412 auto cert = key.getDefaultCertificate();
413
Zhiyi Zhang46049832020-09-28 17:08:12 -0700414 util::DummyClientFace face(io, {true, true});
tylerliu182bc532020-09-25 01:54:45 -0700415 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
416 advanceClocks(time::milliseconds(20), 60);
417
418 //generate a certificate
419 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
420 auto clientKey = clientIdentity.getDefaultKey();
421 security::v2::Certificate clientCert;
422 clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion());
423 clientCert.setContentType(tlv::ContentType_Key);
424 clientCert.setFreshnessPeriod(time::hours(24));
425 clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size());
426 SignatureInfo signatureInfo;
427 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700428 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700429 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700430 CertificateRequest certRequest(Name("/ndn"), "122", RequestType::NEW, Status::SUCCESS, clientCert);
tylerliu182bc532020-09-25 01:54:45 -0700431 auto issuedCert = ca.issueCertificate(certRequest);
432
433 ClientModule client(m_keyChain);
434 ClientCaItem item;
435 item.m_caName = Name("/ndn");
436 item.m_anchor = cert;
437 client.getClientConf().m_caItems.push_back(item);
438
439 auto interest = client.generateRevokeInterest(issuedCert);
440
441 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700442 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700443 count++;
444 BOOST_CHECK(security::verifySignature(response, cert));
445 auto contentBlock = response.getContent();
446 contentBlock.parse();
447
448 BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != "");
449 BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != "");
450 BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != "");
451
452 auto challengeBlockCount = 0;
453 for (auto const& element : contentBlock.elements()) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700454 if (element.type() == tlv_challenge) {
455 challengeBlockCount++;
456 }
tylerliu182bc532020-09-25 01:54:45 -0700457 }
458
459 BOOST_CHECK(challengeBlockCount != 0);
460
tylerliu0e176c32020-09-29 11:39:46 -0700461 client.onNewRenewRevokeResponse(response);
tylerliu182bc532020-09-25 01:54:45 -0700462 BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700463 ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey));
tylerliu182bc532020-09-25 01:54:45 -0700464 });
465 face.receive(*interest);
466
467 advanceClocks(time::milliseconds(20), 60);
468 BOOST_CHECK_EQUAL(count, 1);
469}
470
471BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert)
472{
473 auto identity = addIdentity(Name("/ndn"));
474 auto key = identity.getDefaultKey();
475 auto cert = key.getDefaultCertificate();
476
Zhiyi Zhang46049832020-09-28 17:08:12 -0700477 util::DummyClientFace face(io, {true, true});
tylerliu182bc532020-09-25 01:54:45 -0700478 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
479 advanceClocks(time::milliseconds(20), 60);
480
481 //generate a certificate
482 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
483 auto clientKey = clientIdentity.getDefaultKey();
484 security::v2::Certificate clientCert;
485 clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append(std::to_string(1473283247810732701)));
486 clientCert.setContentType(tlv::ContentType_Key);
487 clientCert.setFreshnessPeriod(time::hours(24));
488 clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size());
489 SignatureInfo signatureInfo;
490 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700491 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700492 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
493
494 ClientModule client(m_keyChain);
495 ClientCaItem item;
496 item.m_caName = Name("/ndn");
497 item.m_anchor = cert;
498 client.getClientConf().m_caItems.push_back(item);
499
500 auto interest = client.generateRevokeInterest(clientCert);
501
502 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700503 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700504 count++;
505 });
506 face.receive(*interest);
507
508 advanceClocks(time::milliseconds(20), 60);
509 BOOST_CHECK_EQUAL(count, 0);
510}
511
Zhiyi Zhang46049832020-09-28 17:08:12 -0700512BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800513
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700514} // namespace tests
515} // namespace ndncert
516} // namespace ndn