blob: 374c847d4790df81c9d565bc03e25d6ac403ea74 [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/*
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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 Zhangad6cf932017-10-26 16:19:15 -070022#include "database-fixture.hpp"
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080023#include "client-module.hpp"
24#include "challenge-module.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025#include "challenge-module/challenge-pin.hpp"
26#include "challenge-module/challenge-email.hpp"
Davide Pesavento914d05f2019-07-13 16:20:19 -040027
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080028#include <ndn-cxx/util/dummy-client-face.hpp>
29#include <ndn-cxx/security/signing-helpers.hpp>
30#include <ndn-cxx/security/transform/public-key.hpp>
31#include <ndn-cxx/security/verification-helpers.hpp>
32
33namespace ndn {
34namespace ndncert {
35namespace tests {
36
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070037BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080038
39BOOST_AUTO_TEST_CASE(Initialization)
40{
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070041 util::DummyClientFace face(io, {true, true});
42 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Davide Pesavento914d05f2019-07-13 16:20:19 -040043 BOOST_CHECK_EQUAL(ca.getCaConf().m_caName, "/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080044
45 auto identity = addIdentity(Name("/ndn/site2"));
46 auto key = identity.getDefaultKey();
47 auto cert = key.getDefaultCertificate();
48 ca.getCaStorage()->addCertificate("111", cert);
49 BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2"));
50
51 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052 BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 2);
53 BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 4);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080054}
55
56BOOST_AUTO_TEST_CASE(HandleProbe)
57{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080059 auto key = identity.getDefaultKey();
60 auto cert = key.getDefaultCertificate();
61
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070062 util::DummyClientFace face(io, {true, true});
63 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Yufeng Zhang424d0362019-06-12 16:48:27 -070064 ca.setProbeHandler([&] (const JsonSection& probeInfo) {
65 return "example";
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080066 });
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080067 advanceClocks(time::milliseconds(20), 60);
68
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069 Interest interest("/ndn/CA/_PROBE");
70 interest.setCanBePrefix(false);
71 JsonSection paramJson;
72 paramJson.add(JSON_CLIENT_PROBE_INFO, "zhiyi");
73 interest.setApplicationParameters(ClientModule::paramFromJson(paramJson));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080074
75 int count = 0;
76 face.onSendData.connect([&] (const Data& response) {
77 count++;
78 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079 auto contentJson = ClientModule::getJsonFromData(response);
Yufeng Zhang424d0362019-06-12 16:48:27 -070080 BOOST_CHECK_EQUAL(contentJson.get<std::string>(JSON_CA_NAME), "/ndn/example");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081 });
82 face.receive(interest);
83
84 advanceClocks(time::milliseconds(20), 60);
85 BOOST_CHECK_EQUAL(count, 1);
86}
87
88BOOST_AUTO_TEST_CASE(HandleProbeInfo)
89{
90 auto identity = addIdentity(Name("/ndn"));
91 auto key = identity.getDefaultKey();
92 auto cert = key.getDefaultCertificate();
93
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070094 util::DummyClientFace face(io, {true, true});
95 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Yufeng Zhang424d0362019-06-12 16:48:27 -070096 ca.setProbeHandler([&] (const JsonSection& probeInfo) {
97 return "example";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 });
99 advanceClocks(time::milliseconds(20), 60);
100
101 Interest interest("/ndn/CA/_PROBE/INFO");
102 interest.setCanBePrefix(false);
103
104 int count = 0;
105 face.onSendData.connect([&] (const Data& response) {
106 count++;
107 BOOST_CHECK(security::verifySignature(response, cert));
108 auto contentJson = ClientModule::getJsonFromData(response);
109 auto caItem = ClientConfig::extractCaItem(contentJson);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400110 BOOST_CHECK_EQUAL(caItem.m_caName, "/ndn");
Zhiyi Zhanga1fc6232019-06-13 14:56:59 -0700111 BOOST_CHECK_EQUAL(caItem.m_probe, "");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700112 BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
113 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800114 });
115 face.receive(interest);
116
117 advanceClocks(time::milliseconds(20), 60);
118 BOOST_CHECK_EQUAL(count, 1);
119}
120
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700121BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler)
122{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700124 auto key = identity.getDefaultKey();
125 auto cert = key.getDefaultCertificate();
126
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700127 util::DummyClientFace face(io, {true, true});
128 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700129 advanceClocks(time::milliseconds(20), 60);
130
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700131 Interest interest("/ndn/CA/_PROBE");
132 interest.setCanBePrefix(false);
133 JsonSection paramJson;
134 paramJson.add(JSON_CLIENT_PROBE_INFO, "zhiyi");
135 interest.setApplicationParameters(ClientModule::paramFromJson(paramJson));
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700136
137 int count = 0;
138 face.onSendData.connect([&] (const Data& response) {
139 count++;
140 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700141 auto contentJson = ClientModule::getJsonFromData(response);
142 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_NAME) != "");
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700143 });
144 face.receive(interest);
145
146 advanceClocks(time::milliseconds(20), 60);
147 BOOST_CHECK_EQUAL(count, 1);
148}
149
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800150BOOST_AUTO_TEST_CASE(HandleNew)
151{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700152 auto identity = addIdentity(Name("/ndn"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800153 auto key = identity.getDefaultKey();
154 auto cert = key.getDefaultCertificate();
155
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700156 util::DummyClientFace face(io, {true, true});
157 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800158 advanceClocks(time::milliseconds(20), 60);
159
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700160 ClientModule client(m_keyChain);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800161 ClientCaItem item;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700162 item.m_caName = Name("/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800163 item.m_anchor = cert;
164 client.getClientConf().m_caItems.push_back(item);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700165
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700166 auto interest = client.generateNewInterest(time::system_clock::now(),
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700167 time::system_clock::now() + time::days(10),
168 Name("/ndn/zhiyi"));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800169
170 int count = 0;
171 face.onSendData.connect([&] (const Data& response) {
172 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700173 BOOST_CHECK(security::verifySignature(response, cert));
174 auto contentJson = ClientModule::getJsonFromData(response);
175 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_ECDH) != "");
176 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_SALT) != "");
Zhiyi Zhangff4bcb62019-09-08 12:57:42 -0700177 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_REQUEST_ID) != "");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700178 auto challengesJson = contentJson.get_child(JSON_CA_CHALLENGES);
179 BOOST_CHECK(challengesJson.size() != 0);
180
181 client.onNewResponse(response);
182 BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + 32, ca.m_aesKey, ca.m_aesKey + 32);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800183 });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700184 face.receive(*interest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800185
186 advanceClocks(time::milliseconds(20), 60);
187 BOOST_CHECK_EQUAL(count, 1);
188}
189
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700190BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1)
191{
192 auto identity = addIdentity(Name("/ndn"));
193 auto key = identity.getDefaultKey();
194 auto cert = key.getDefaultCertificate();
195
196 util::DummyClientFace face(io, {true, true});
197 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
198 advanceClocks(time::milliseconds(20), 60);
199
200 ClientModule client(m_keyChain);
201 ClientCaItem item;
202 item.m_caName = Name("/ndn");
203 item.m_anchor = cert;
204 client.getClientConf().m_caItems.push_back(item);
205 auto current_tp = time::system_clock::now();
206 auto interest1 = client.generateNewInterest(current_tp, current_tp - time::hours(1),
207 Name("/ndn/zhiyi"));
208 auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(361),
209 Name("/ndn/zhiyi"));
210 auto interest3 = client.generateNewInterest(current_tp - time::hours(1),
211 current_tp + time::hours(2),
212 Name("/ndn/zhiyi"));
213 face.onSendData.connect([&] (const Data& response) {
214 BOOST_CHECK(false);
215 });
216 face.receive(*interest1);
217 face.receive(*interest2);
218 face.receive(*interest3);
219
220 advanceClocks(time::milliseconds(20), 60);
221}
222
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700223BOOST_AUTO_TEST_CASE(HandleNewWithProbeToken)
224{
225 auto identity = addIdentity(Name("/ndn"));
226 auto key = identity.getDefaultKey();
227 auto cert = key.getDefaultCertificate();
228
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700229 util::DummyClientFace face(io, {true, true});
230 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhanga1fc6232019-06-13 14:56:59 -0700231 ca.m_config.m_probe = "email";
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700232 advanceClocks(time::milliseconds(20), 60);
233
234 ClientModule client(m_keyChain);
235 ClientCaItem item;
236 item.m_caName = Name("/ndn");
237 item.m_anchor = cert;
238 client.getClientConf().m_caItems.push_back(item);
239
Zhiyi Zhanga1fc6232019-06-13 14:56:59 -0700240 auto data = make_shared<Data>(Name("/ndn/CA/_PROBE/123"));
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700241 m_keyChain.sign(*data, signingByIdentity(ca.m_config.m_caName));
242
243 auto interest = client.generateNewInterest(time::system_clock::now(),
244 time::system_clock::now() + time::days(10),
245 Name("/ndn/zhiyi"), data);
246
247 int count = 0;
248 face.onSendData.connect([&] (const Data& response) {
249 count++;
250 BOOST_CHECK(security::verifySignature(response, cert));
251 });
252 face.receive(*interest);
253
254 advanceClocks(time::milliseconds(20), 60);
255 BOOST_CHECK_EQUAL(count, 1);
256}
257
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700258BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800259{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700260 auto identity = addIdentity(Name("/ndn"));
261 auto key = identity.getDefaultKey();
262 auto cert = key.getDefaultCertificate();
263
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700264 util::DummyClientFace face(io, {true, true});
265 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800266 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700267
268 // generate NEW Interest
269 ClientModule client(m_keyChain);
270 ClientCaItem item;
271 item.m_caName = Name("/ndn");
272 item.m_anchor = cert;
273 client.getClientConf().m_caItems.push_back(item);
274 auto newInterest = client.generateNewInterest(time::system_clock::now(),
275 time::system_clock::now() + time::days(10), Name("/ndn/zhiyi"));
276
277 // generate CHALLENGE Interest
278 ChallengePin pinChallenge;
279 shared_ptr<Interest> challengeInterest = nullptr;
280 shared_ptr<Interest> challengeInterest2 = nullptr;
281 shared_ptr<Interest> challengeInterest3 = nullptr;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800282
283 int count = 0;
284 face.onSendData.connect([&] (const Data& response) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700285 if (Name("/ndn/CA/_NEW").isPrefixOf(response.getName())) {
286 auto contentJson = ClientModule::getJsonFromData(response);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700287 client.onNewResponse(response);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700288 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700289 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status,
290 client.m_challengeStatus,
291 paramJson));
292 }
293 else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800294 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700295 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800296
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700297 client.onChallengeResponse(response);
298 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
299 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400300
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700301 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700302 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status,
303 client.m_challengeStatus,
304 paramJson));
305 }
306 else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
307 count++;
308 BOOST_CHECK(security::verifySignature(response, cert));
309
310 client.onChallengeResponse(response);
311 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
312 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
Davide Pesavento914d05f2019-07-13 16:20:19 -0400313
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700314 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
315 auto request = ca.getCertificateRequest(*challengeInterest2);
316 auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, "");
Davide Pesavento914d05f2019-07-13 16:20:19 -0400317 for (auto& i : paramJson) {
318 if (i.first == ChallengePin::JSON_PIN_CODE)
319 i.second.put("", secret);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700320 }
321 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status,
322 client.m_challengeStatus,
323 paramJson));
324 }
325 else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
326 count++;
327 BOOST_CHECK(security::verifySignature(response, cert));
328
329 client.onChallengeResponse(response);
330 BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS);
331 BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
332 }
Davide Pesavento914d05f2019-07-13 16:20:19 -0400333 });
334
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700335 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800336 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700337 face.receive(*challengeInterest);
338 advanceClocks(time::milliseconds(20), 60);
339 face.receive(*challengeInterest2);
340 advanceClocks(time::milliseconds(20), 60);
341 face.receive(*challengeInterest3);
342 advanceClocks(time::milliseconds(20), 60);
343 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800344}
345
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800346BOOST_AUTO_TEST_SUITE_END() // TestCaModule
347
348} // namespace tests
349} // namespace ndncert
350} // namespace ndn