blob: f3fde9df7eb2aaebb972754426d9bf217d059a36 [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) {
238 BOOST_CHECK(false);
239 });
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700240 face.receive(*interest1);
241 face.receive(*interest2);
242 face.receive(*interest3);
243
244 advanceClocks(time::milliseconds(20), 60);
245}
246
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700247BOOST_AUTO_TEST_CASE(HandleNewWithProbeToken)
248{
249 auto identity = addIdentity(Name("/ndn"));
250 auto key = identity.getDefaultKey();
251 auto cert = key.getDefaultCertificate();
252
Zhiyi Zhang22998612020-09-25 14:43:23 -0700253 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700254 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700255 advanceClocks(time::milliseconds(20), 60);
256
257 ClientModule client(m_keyChain);
258 ClientCaItem item;
Suyong Won256c9062020-05-11 02:45:56 -0700259 item.m_caPrefix = Name("/ndn");
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700260 item.m_anchor = cert;
261 client.getClientConf().m_caItems.push_back(item);
262
swa770de007bc2020-03-24 21:26:21 -0700263 auto data = make_shared<Data>(Name("/ndn/CA/PROBE/123"));
Suyong Won256c9062020-05-11 02:45:56 -0700264 m_keyChain.sign(*data, signingByIdentity(ca.m_config.m_caPrefix));
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700265
266 auto interest = client.generateNewInterest(time::system_clock::now(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700267 time::system_clock::now() + time::days(1),
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700268 Name("/ndn/zhiyi"), data);
269
270 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700271 face.onSendData.connect([&](const Data& response) {
272 count++;
273 BOOST_CHECK(security::verifySignature(response, cert));
274 });
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700275 face.receive(*interest);
276
277 advanceClocks(time::milliseconds(20), 60);
278 BOOST_CHECK_EQUAL(count, 1);
279}
280
tylerliu0b6d0db2020-09-28 17:52:02 -0700281BOOST_AUTO_TEST_CASE(HandleNewWithLongSuffix)
282{
283 auto identity = addIdentity(Name("/ndn"));
284 auto key = identity.getDefaultKey();
285 auto cert = key.getDefaultCertificate();
286
287 util::DummyClientFace face(io, m_keyChain, { true, true });
288 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
289 advanceClocks(time::milliseconds(20), 60);
290
291 ClientModule client(m_keyChain);
292 ClientCaItem item;
293 item.m_caPrefix = Name("/ndn");
294 item.m_anchor = cert;
295 client.getClientConf().m_caItems.push_back(item);
296
297 auto interest = client.generateNewInterest(time::system_clock::now(),
298 time::system_clock::now() + time::days(1),
299 Name("/ndn/a/b/c"));
300
301 int count = 0;
302 face.onSendData.connect([&](const Data& response) {
303 count++;
304 });
305 face.receive(*interest);
306
307 advanceClocks(time::milliseconds(20), 60);
308 BOOST_CHECK_EQUAL(count, 1);
309}
310
311
312BOOST_AUTO_TEST_CASE(HandleNewWithInvalidLength1)
313{
314 auto identity = addIdentity(Name("/ndn"));
315 auto key = identity.getDefaultKey();
316 auto cert = key.getDefaultCertificate();
317
318 util::DummyClientFace face(io, m_keyChain, {true, true});
319 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
320 advanceClocks(time::milliseconds(20), 60);
321
322 ClientModule client(m_keyChain);
323 ClientCaItem item;
324 item.m_caPrefix = Name("/ndn");
325 item.m_anchor = cert;
326 client.getClientConf().m_caItems.push_back(item);
327 auto current_tp = time::system_clock::now();
328 auto interest1 = client.generateNewInterest(current_tp, current_tp + time::days(1),Name("/ndn"));
329 auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(1),Name("/ndn/a/b/c/d"));
330 face.onSendData.connect([&](const Data& response) {
331 BOOST_CHECK(false);
332 });
333 face.receive(*interest1);
334 face.receive(*interest2);
335
336 advanceClocks(time::milliseconds(20), 60);
337}
338
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700339BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800340{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700341 auto identity = addIdentity(Name("/ndn"));
342 auto key = identity.getDefaultKey();
343 auto cert = key.getDefaultCertificate();
344
Zhiyi Zhang22998612020-09-25 14:43:23 -0700345 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700346 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800347 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700348
349 // generate NEW Interest
350 ClientModule client(m_keyChain);
351 ClientCaItem item;
Suyong Won256c9062020-05-11 02:45:56 -0700352 item.m_caPrefix = Name("/ndn");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700353 item.m_anchor = cert;
354 client.getClientConf().m_caItems.push_back(item);
355 auto newInterest = client.generateNewInterest(time::system_clock::now(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700356 time::system_clock::now() + time::days(1), Name("/ndn/zhiyi"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700357
358 // generate CHALLENGE Interest
359 ChallengePin pinChallenge;
360 shared_ptr<Interest> challengeInterest = nullptr;
361 shared_ptr<Interest> challengeInterest2 = nullptr;
362 shared_ptr<Interest> challengeInterest3 = nullptr;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800363
364 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700365 face.onSendData.connect([&](const Data& response) {
swa770de007bc2020-03-24 21:26:21 -0700366 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
tylerliu0e176c32020-09-29 11:39:46 -0700367 client.onNewRenewRevokeResponse(response);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700368 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Suyong Won44d0cce2020-05-10 04:07:43 -0700369 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700370 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700371 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700372 }
swa770de007bc2020-03-24 21:26:21 -0700373 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800374 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700375 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800376
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700377 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700378 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700379 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400380
Zhiyi Zhang46049832020-09-28 17:08:12 -0700381 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Suyong Won44d0cce2020-05-10 04:07:43 -0700382 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700383 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700384 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700385 }
swa770de007bc2020-03-24 21:26:21 -0700386 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700387 count++;
388 BOOST_CHECK(security::verifySignature(response, cert));
389
390 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700391 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700392 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400393
Zhiyi Zhang46049832020-09-28 17:08:12 -0700394 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700395 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700396 auto secret = request.m_challengeSecrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
397 std::get<1>(paramList[0]) = secret;
Suyong Won44d0cce2020-05-10 04:07:43 -0700398 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700399 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700400 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700401 }
swa770de007bc2020-03-24 21:26:21 -0700402 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700403 count++;
404 BOOST_CHECK(security::verifySignature(response, cert));
405
406 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700407 BOOST_CHECK(client.m_status == Status::SUCCESS);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700408 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400409 });
410
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700411 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800412 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700413 face.receive(*challengeInterest);
414 advanceClocks(time::milliseconds(20), 60);
415 face.receive(*challengeInterest2);
416 advanceClocks(time::milliseconds(20), 60);
417 face.receive(*challengeInterest3);
418 advanceClocks(time::milliseconds(20), 60);
419 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800420}
421
tylerliu182bc532020-09-25 01:54:45 -0700422BOOST_AUTO_TEST_CASE(HandleRevoke)
423{
424 auto identity = addIdentity(Name("/ndn"));
425 auto key = identity.getDefaultKey();
426 auto cert = key.getDefaultCertificate();
427
Zhiyi Zhang46049832020-09-28 17:08:12 -0700428 util::DummyClientFace face(io, {true, true});
tylerliu182bc532020-09-25 01:54:45 -0700429 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
430 advanceClocks(time::milliseconds(20), 60);
431
432 //generate a certificate
433 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
434 auto clientKey = clientIdentity.getDefaultKey();
435 security::v2::Certificate clientCert;
436 clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion());
437 clientCert.setContentType(tlv::ContentType_Key);
438 clientCert.setFreshnessPeriod(time::hours(24));
439 clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size());
440 SignatureInfo signatureInfo;
441 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700442 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700443 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700444 CertificateRequest certRequest(Name("/ndn"), "122", RequestType::NEW, Status::SUCCESS, clientCert);
tylerliu182bc532020-09-25 01:54:45 -0700445 auto issuedCert = ca.issueCertificate(certRequest);
446
447 ClientModule client(m_keyChain);
448 ClientCaItem item;
449 item.m_caName = Name("/ndn");
450 item.m_anchor = cert;
451 client.getClientConf().m_caItems.push_back(item);
452
453 auto interest = client.generateRevokeInterest(issuedCert);
454
455 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700456 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700457 count++;
458 BOOST_CHECK(security::verifySignature(response, cert));
459 auto contentBlock = response.getContent();
460 contentBlock.parse();
461
462 BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != "");
463 BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != "");
464 BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != "");
465
466 auto challengeBlockCount = 0;
467 for (auto const& element : contentBlock.elements()) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700468 if (element.type() == tlv_challenge) {
469 challengeBlockCount++;
470 }
tylerliu182bc532020-09-25 01:54:45 -0700471 }
472
473 BOOST_CHECK(challengeBlockCount != 0);
474
tylerliu0e176c32020-09-29 11:39:46 -0700475 client.onNewRenewRevokeResponse(response);
tylerliu182bc532020-09-25 01:54:45 -0700476 BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700477 ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey));
tylerliu182bc532020-09-25 01:54:45 -0700478 });
479 face.receive(*interest);
480
481 advanceClocks(time::milliseconds(20), 60);
482 BOOST_CHECK_EQUAL(count, 1);
483}
484
485BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert)
486{
487 auto identity = addIdentity(Name("/ndn"));
488 auto key = identity.getDefaultKey();
489 auto cert = key.getDefaultCertificate();
490
Zhiyi Zhang46049832020-09-28 17:08:12 -0700491 util::DummyClientFace face(io, {true, true});
tylerliu182bc532020-09-25 01:54:45 -0700492 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
493 advanceClocks(time::milliseconds(20), 60);
494
495 //generate a certificate
496 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
497 auto clientKey = clientIdentity.getDefaultKey();
498 security::v2::Certificate clientCert;
499 clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append(std::to_string(1473283247810732701)));
500 clientCert.setContentType(tlv::ContentType_Key);
501 clientCert.setFreshnessPeriod(time::hours(24));
502 clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size());
503 SignatureInfo signatureInfo;
504 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700505 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700506 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
507
508 ClientModule client(m_keyChain);
509 ClientCaItem item;
510 item.m_caName = Name("/ndn");
511 item.m_anchor = cert;
512 client.getClientConf().m_caItems.push_back(item);
513
514 auto interest = client.generateRevokeInterest(clientCert);
515
516 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700517 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700518 count++;
519 });
520 face.receive(*interest);
521
522 advanceClocks(time::milliseconds(20), 60);
523 BOOST_CHECK_EQUAL(count, 0);
524}
525
Zhiyi Zhang46049832020-09-28 17:08:12 -0700526BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800527
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700528} // namespace tests
529} // namespace ndncert
530} // namespace ndn