blob: 3860060e5e86df637dcb68e4319d61d864de2252 [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
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700204 client.onNewResponse(response);
205 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
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700281BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800282{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700283 auto identity = addIdentity(Name("/ndn"));
284 auto key = identity.getDefaultKey();
285 auto cert = key.getDefaultCertificate();
286
Zhiyi Zhang22998612020-09-25 14:43:23 -0700287 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700288 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800289 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700290
291 // generate NEW Interest
292 ClientModule client(m_keyChain);
293 ClientCaItem item;
Suyong Won256c9062020-05-11 02:45:56 -0700294 item.m_caPrefix = Name("/ndn");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700295 item.m_anchor = cert;
296 client.getClientConf().m_caItems.push_back(item);
297 auto newInterest = client.generateNewInterest(time::system_clock::now(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700298 time::system_clock::now() + time::days(1), Name("/ndn/zhiyi"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700299
300 // generate CHALLENGE Interest
301 ChallengePin pinChallenge;
302 shared_ptr<Interest> challengeInterest = nullptr;
303 shared_ptr<Interest> challengeInterest2 = nullptr;
304 shared_ptr<Interest> challengeInterest3 = nullptr;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800305
306 int count = 0;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700307 face.onSendData.connect([&](const Data& response) {
swa770de007bc2020-03-24 21:26:21 -0700308 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700309 client.onNewResponse(response);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700310 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Suyong Won44d0cce2020-05-10 04:07:43 -0700311 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700312 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700313 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700314 }
swa770de007bc2020-03-24 21:26:21 -0700315 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800316 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700317 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800318
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700319 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700320 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700321 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400322
Zhiyi Zhang46049832020-09-28 17:08:12 -0700323 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Suyong Won44d0cce2020-05-10 04:07:43 -0700324 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700325 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700326 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700327 }
swa770de007bc2020-03-24 21:26:21 -0700328 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700329 count++;
330 BOOST_CHECK(security::verifySignature(response, cert));
331
332 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700333 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700334 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400335
Zhiyi Zhang46049832020-09-28 17:08:12 -0700336 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700337 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700338 auto secret = request.m_challengeSecrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
339 std::get<1>(paramList[0]) = secret;
Suyong Won44d0cce2020-05-10 04:07:43 -0700340 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700341 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700342 std::move(paramList)));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700343 }
swa770de007bc2020-03-24 21:26:21 -0700344 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700345 count++;
346 BOOST_CHECK(security::verifySignature(response, cert));
347
348 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700349 BOOST_CHECK(client.m_status == Status::SUCCESS);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700350 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400351 });
352
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700353 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800354 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700355 face.receive(*challengeInterest);
356 advanceClocks(time::milliseconds(20), 60);
357 face.receive(*challengeInterest2);
358 advanceClocks(time::milliseconds(20), 60);
359 face.receive(*challengeInterest3);
360 advanceClocks(time::milliseconds(20), 60);
361 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800362}
363
tylerliu182bc532020-09-25 01:54:45 -0700364BOOST_AUTO_TEST_CASE(HandleRevoke)
365{
366 auto identity = addIdentity(Name("/ndn"));
367 auto key = identity.getDefaultKey();
368 auto cert = key.getDefaultCertificate();
369
Zhiyi Zhang46049832020-09-28 17:08:12 -0700370 util::DummyClientFace face(io, {true, true});
tylerliu182bc532020-09-25 01:54:45 -0700371 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
372 advanceClocks(time::milliseconds(20), 60);
373
374 //generate a certificate
375 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
376 auto clientKey = clientIdentity.getDefaultKey();
377 security::v2::Certificate clientCert;
378 clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion());
379 clientCert.setContentType(tlv::ContentType_Key);
380 clientCert.setFreshnessPeriod(time::hours(24));
381 clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size());
382 SignatureInfo signatureInfo;
383 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700384 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700385 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700386 CertificateRequest certRequest(Name("/ndn"), "122", REQUEST_TYPE_NEW, Status::SUCCESS, clientCert);
tylerliu182bc532020-09-25 01:54:45 -0700387 auto issuedCert = ca.issueCertificate(certRequest);
388
389 ClientModule client(m_keyChain);
390 ClientCaItem item;
391 item.m_caName = Name("/ndn");
392 item.m_anchor = cert;
393 client.getClientConf().m_caItems.push_back(item);
394
395 auto interest = client.generateRevokeInterest(issuedCert);
396
397 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700398 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700399 count++;
400 BOOST_CHECK(security::verifySignature(response, cert));
401 auto contentBlock = response.getContent();
402 contentBlock.parse();
403
404 BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != "");
405 BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != "");
406 BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != "");
407
408 auto challengeBlockCount = 0;
409 for (auto const& element : contentBlock.elements()) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700410 if (element.type() == tlv_challenge) {
411 challengeBlockCount++;
412 }
tylerliu182bc532020-09-25 01:54:45 -0700413 }
414
415 BOOST_CHECK(challengeBlockCount != 0);
416
417 client.onRevokeResponse(response);
418 BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700419 ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey));
tylerliu182bc532020-09-25 01:54:45 -0700420 });
421 face.receive(*interest);
422
423 advanceClocks(time::milliseconds(20), 60);
424 BOOST_CHECK_EQUAL(count, 1);
425}
426
427BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert)
428{
429 auto identity = addIdentity(Name("/ndn"));
430 auto key = identity.getDefaultKey();
431 auto cert = key.getDefaultCertificate();
432
Zhiyi Zhang46049832020-09-28 17:08:12 -0700433 util::DummyClientFace face(io, {true, true});
tylerliu182bc532020-09-25 01:54:45 -0700434 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
435 advanceClocks(time::milliseconds(20), 60);
436
437 //generate a certificate
438 auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
439 auto clientKey = clientIdentity.getDefaultKey();
440 security::v2::Certificate clientCert;
441 clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append(std::to_string(1473283247810732701)));
442 clientCert.setContentType(tlv::ContentType_Key);
443 clientCert.setFreshnessPeriod(time::hours(24));
444 clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size());
445 SignatureInfo signatureInfo;
446 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
Zhiyi Zhang46049832020-09-28 17:08:12 -0700447 time::system_clock::now() + time::hours(10)));
tylerliu182bc532020-09-25 01:54:45 -0700448 m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo));
449
450 ClientModule client(m_keyChain);
451 ClientCaItem item;
452 item.m_caName = Name("/ndn");
453 item.m_anchor = cert;
454 client.getClientConf().m_caItems.push_back(item);
455
456 auto interest = client.generateRevokeInterest(clientCert);
457
458 int count = 0;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700459 face.onSendData.connect([&](const Data& response) {
tylerliu182bc532020-09-25 01:54:45 -0700460 count++;
461 });
462 face.receive(*interest);
463
464 advanceClocks(time::milliseconds(20), 60);
465 BOOST_CHECK_EQUAL(count, 0);
466}
467
Zhiyi Zhang46049832020-09-28 17:08:12 -0700468BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800469
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700470} // namespace tests
471} // namespace ndncert
472} // namespace ndn