blob: 34e2721872d0a17ea0dbb13b922445af752a6834 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080027#include <ndn-cxx/util/dummy-client-face.hpp>
28#include <ndn-cxx/security/signing-helpers.hpp>
29#include <ndn-cxx/security/transform/public-key.hpp>
30#include <ndn-cxx/security/verification-helpers.hpp>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070031#include <iostream>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080032
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{
41 util::DummyClientFace face(m_io, {true, true});
42 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043 BOOST_CHECK_EQUAL(ca.getCaConf().m_caName.toUri(), "/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
62 util::DummyClientFace face(m_io, {true, true});
63 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064 ca.setProbeHandler([&] (const std::string& probeInfo) {
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080065 return probeInfo + "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);
80 BOOST_CHECK_EQUAL(contentJson.get<std::string>(JSON_CA_NAME), "/ndn/zhiyiexample");
81 });
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
94 util::DummyClientFace face(m_io, {true, true});
95 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
96 ca.setProbeHandler([&] (const std::string& probeInfo) {
97 return probeInfo + "example";
98 });
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);
110 BOOST_CHECK_EQUAL(caItem.m_caName.toUri(), "/ndn");
111 BOOST_CHECK_EQUAL(caItem.m_probe, "input email address");
112 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
127 util::DummyClientFace face(m_io, {true, true});
128 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
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
156 util::DummyClientFace face(m_io, {true, true});
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800157 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
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 Zhangaf7c2902019-03-14 22:13:21 -0700165 auto interest = client.generateNewInterest(time::system_clock::now(),
166 time::system_clock::now() + time::days(10), Name("/ndn/zhiyi"));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800167
168 int count = 0;
169 face.onSendData.connect([&] (const Data& response) {
170 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700171 BOOST_CHECK(security::verifySignature(response, cert));
172 auto contentJson = ClientModule::getJsonFromData(response);
173 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_ECDH) != "");
174 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_SALT) != "");
175 BOOST_CHECK(contentJson.get<std::string>(JSON_CA_EQUEST_ID) != "");
176 auto challengesJson = contentJson.get_child(JSON_CA_CHALLENGES);
177 BOOST_CHECK(challengesJson.size() != 0);
178
179 client.onNewResponse(response);
180 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 +0800181 });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700182 face.receive(*interest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800183
184 advanceClocks(time::milliseconds(20), 60);
185 BOOST_CHECK_EQUAL(count, 1);
186}
187
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700188BOOST_AUTO_TEST_CASE(HandleChallenge)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800189{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700190 auto identity = addIdentity(Name("/ndn"));
191 auto key = identity.getDefaultKey();
192 auto cert = key.getDefaultCertificate();
193
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800194 util::DummyClientFace face(m_io, {true, true});
195 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800196 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700197
198 // generate NEW Interest
199 ClientModule client(m_keyChain);
200 ClientCaItem item;
201 item.m_caName = Name("/ndn");
202 item.m_anchor = cert;
203 client.getClientConf().m_caItems.push_back(item);
204 auto newInterest = client.generateNewInterest(time::system_clock::now(),
205 time::system_clock::now() + time::days(10), Name("/ndn/zhiyi"));
206
207 // generate CHALLENGE Interest
208 ChallengePin pinChallenge;
209 shared_ptr<Interest> challengeInterest = nullptr;
210 shared_ptr<Interest> challengeInterest2 = nullptr;
211 shared_ptr<Interest> challengeInterest3 = nullptr;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800212
213 int count = 0;
214 face.onSendData.connect([&] (const Data& response) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700215 if (Name("/ndn/CA/_NEW").isPrefixOf(response.getName())) {
216 auto contentJson = ClientModule::getJsonFromData(response);
217 std::cout << "Request ID " << contentJson.get<std::string>(JSON_CA_EQUEST_ID) << std::endl;
218 client.onNewResponse(response);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800219
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700220 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
221 for (auto& item : paramJson) {
222 std::cout << "JSON attribute" << item.first;
223 std::cout << " : " << item.second.get<std::string>("") << std::endl;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800224 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700225 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status,
226 client.m_challengeStatus,
227 paramJson));
228 }
229 else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800230 count++;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700231 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800232
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700233 client.onChallengeResponse(response);
234 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
235 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
236 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
237 for (auto& item : paramJson) {
238 std::cout << "JSON attribute" << item.first;
239 std::cout << " : " << item.second.get<std::string>("") << std::endl;
240 }
241 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status,
242 client.m_challengeStatus,
243 paramJson));
244 }
245 else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
246 count++;
247 BOOST_CHECK(security::verifySignature(response, cert));
248
249 client.onChallengeResponse(response);
250 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
251 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
252 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
253 auto request = ca.getCertificateRequest(*challengeInterest2);
254 auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, "");
255 for (auto& item : paramJson) {
256 std::cout << "JSON attribute" << item.first;
257 std::cout << " : " << item.second.get<std::string>("") << std::endl;
258 if (item.first == ChallengePin::JSON_PIN_CODE)
259 item.second.put("", secret);
260 }
261 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status,
262 client.m_challengeStatus,
263 paramJson));
264 }
265 else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
266 count++;
267 BOOST_CHECK(security::verifySignature(response, cert));
268
269 client.onChallengeResponse(response);
270 BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS);
271 BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
272 }
273 });
274 face.receive(*newInterest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800275 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700276 face.receive(*challengeInterest);
277 advanceClocks(time::milliseconds(20), 60);
278 face.receive(*challengeInterest2);
279 advanceClocks(time::milliseconds(20), 60);
280 face.receive(*challengeInterest3);
281 advanceClocks(time::milliseconds(20), 60);
282 BOOST_CHECK_EQUAL(count, 3);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800283}
284
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800285BOOST_AUTO_TEST_SUITE_END() // TestCaModule
286
287} // namespace tests
288} // namespace ndncert
289} // namespace ndn