blob: 6b93ee4089cb3de89f041bad2bee680cc2c9c013 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
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
21#include "ca-module.hpp"
22#include "challenge-module.hpp"
23#include "logging.hpp"
24#include <ndn-cxx/face.hpp>
25#include <ndn-cxx/security/verification-helpers.hpp>
26#include <ndn-cxx/security/signing-helpers.hpp>
27
28namespace ndn {
29namespace ndncert {
30
31_LOG_INIT(ndncert.ca);
32
33CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
34 const std::string& configPath, const std::string& storageType)
35 : m_face(face)
36 , m_keyChain(keyChain)
37{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070038 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080039 m_config.load(configPath);
40 m_storage = CaStorage::createCaStorage(storageType);
41
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070042 // set default handler and callback
43 m_probeHandler = [&] (const std::string& probeInfo) {
44 return probeInfo;
45 };
46 m_requestUpdateCallback = [&] (const CertificateRequest& CertRequest) {
47 // do nothing
48 };
49
50 // register prefix
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080051 for (const auto& item : m_config.m_caItems) {
52 Name prefix = item.m_caName;
53 prefix.append("CA");
54 try {
55 const RegisteredPrefixId* prefixId = m_face.registerPrefix(prefix,
56 [&] (const Name& name) {
57 const InterestFilterId* filterId = m_face.setInterestFilter(Name(name).append("_PROBE"),
58 bind(&CaModule::handleProbe, this, _2, item));
59 m_interestFilterIds.push_back(filterId);
60
61 filterId = m_face.setInterestFilter(Name(name).append("_NEW"),
62 bind(&CaModule::handleNew, this, _2, item));
63 m_interestFilterIds.push_back(filterId);
64
65 filterId = m_face.setInterestFilter(Name(name).append("_SELECT"),
66 bind(&CaModule::handleSelect, this, _2, item));
67 m_interestFilterIds.push_back(filterId);
68
69 filterId = m_face.setInterestFilter(Name(name).append("_VALIDATE"),
70 bind(&CaModule::handleValidate, this, _2, item));
71 m_interestFilterIds.push_back(filterId);
72
73 filterId = m_face.setInterestFilter(Name(name).append("_STATUS"),
74 bind(&CaModule::handleStatus, this, _2, item));
75 m_interestFilterIds.push_back(filterId);
76
77 filterId = m_face.setInterestFilter(Name(name).append("_DOWNLOAD"),
78 bind(&CaModule::handleDownload, this, _2, item));
79 m_interestFilterIds.push_back(filterId);
80
81 _LOG_TRACE("Prefix " << name << " got registered");
82 },
83 bind(&CaModule::onRegisterFailed, this, _2));
84 m_registeredPrefixIds.push_back(prefixId);
85 }
86 catch (const std::exception& e) {
87 _LOG_TRACE("Error: " << e.what());
88 }
89 }
90}
91
92CaModule::~CaModule()
93{
94 for (auto prefixId : m_interestFilterIds) {
95 m_face.unsetInterestFilter(prefixId);
96 }
97 for (auto prefixId : m_registeredPrefixIds) {
98 m_face.unregisterPrefix(prefixId, nullptr, nullptr);
99 }
100}
101
102void
103CaModule::handleProbe(const Interest& request, const CaItem& caItem)
104{
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700105 // PROBE Naming Convention: /CA-prefix/CA/_PROBE/<Probe Information>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800106 _LOG_TRACE("Handle PROBE request");
107
108 std::string identifier;
109 try {
110 identifier = m_probeHandler(readString(request.getName().at(caItem.m_caName.size() + 2)));
111 }
112 catch (const std::exception& e) {
113 _LOG_TRACE("Cannot generate identifier for PROBE request " << e.what());
114 return;
115 }
116 Name identityName = caItem.m_caName;
117 identityName.append(identifier);
118
119 Data result;
120 result.setName(request.getName());
121 result.setContent(dataContentFromJson(genResponseProbeJson(identityName, "")));
122 m_keyChain.sign(result, signingByCertificate(caItem.m_anchor));
123 m_face.put(result);
124
125 _LOG_TRACE("Handle PROBE: generate identity " << identityName);
126}
127
128void
129CaModule::handleNew(const Interest& request, const CaItem& caItem)
130{
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700131 // NEW Naming Convention: /CA-prefix/CA/_NEW/<certificate-request>/[signature]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800132 _LOG_TRACE("Handle NEW request");
133
134 security::v2::Certificate clientCert;
135 try {
136 clientCert.wireDecode(request.getName().at(caItem.m_caName.size() + 2).blockFromValue());
137 }
138 catch (const std::exception& e) {
139 _LOG_TRACE("Unrecognized certificate request " << e.what());
140 return;
141 }
142 std::string requestId = std::to_string(random::generateWord64());
143 CertificateRequest certRequest(caItem.m_caName, requestId, clientCert);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700144 certRequest.setStatus(ChallengeModule::WAIT_SELECTION);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800145 try {
146 m_storage->addRequest(certRequest);
147 }
148 catch (const std::exception& e) {
149 _LOG_TRACE("Cannot add new request instance " << e.what());
150 return;
151 }
152
153 Data result;
154 result.setName(request.getName());
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700155 result.setContent(dataContentFromJson(genResponseNewJson(requestId, certRequest.getStatus(),
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800156 caItem.m_supportedChallenges)));
157 m_keyChain.sign(result, signingByCertificate(caItem.m_anchor));
158 m_face.put(result);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700159
160 m_requestUpdateCallback(certRequest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800161}
162
163void
164CaModule::handleSelect(const Interest& request, const CaItem& caItem)
165{
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700166 // SELECT Naming Convention: /CA-prefix/CA/_SELECT/{Request-ID JSON}/<ChallengeID>/
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800167 // {Param JSON}/[Signature components]
168 _LOG_TRACE("Handle SELECT request");
169
170 CertificateRequest certRequest = getCertificateRequest(request, caItem.m_caName);
171 if (certRequest.getRequestId().empty()) {
172 return;
173 }
174
175 if (!security::verifySignature(request, certRequest.getCert())) {
176 _LOG_TRACE("Error: Interest with bad signature.");
177 return;
178 }
179
180 std::string challengeType;
181 try {
182 challengeType = readString(request.getName().at(caItem.m_caName.size() + 3));
183 }
184 catch (const std::exception& e) {
185 _LOG_TRACE(e.what());
186 return;
187 }
Zhiyi Zhange30eb352017-04-13 15:26:14 -0700188 _LOG_TRACE("SELECT request choosing challenge " << challengeType);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800189 auto challenge = ChallengeModule::createChallengeModule(challengeType);
190 if (challenge == nullptr) {
191 _LOG_TRACE("Unrecognized challenge type " << challengeType);
192 return;
193 }
194 JsonSection contentJson = challenge->handleChallengeRequest(request, certRequest);
195 try {
196 m_storage->updateRequest(certRequest);
197 }
198 catch (const std::exception& e) {
199 _LOG_TRACE("Cannot update request instance " << e.what());
200 return;
201 }
202
203 Data result;
204 result.setName(request.getName());
205 result.setContent(dataContentFromJson(contentJson));
206 m_keyChain.sign(result, signingByCertificate(caItem.m_anchor));
207 m_face.put(result);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700208
209 m_requestUpdateCallback(certRequest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800210}
211
212void
213CaModule::handleValidate(const Interest& request, const CaItem& caItem)
214{
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700215 // VALIDATE Naming Convention: /CA-prefix/CA/_VALIDATE/{Request-ID JSON}/<ChallengeID>/
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800216 // {Param JSON}/[Signature components]
217 _LOG_TRACE("Handle VALIDATE request");
218
219 CertificateRequest certRequest = getCertificateRequest(request, caItem.m_caName);
220 if (certRequest.getRequestId().empty()) {
221 return;
222 }
223
224 if (!security::verifySignature(request, certRequest.getCert())) {
225 _LOG_TRACE("Error: Interest with bad signature.");
226 return;
227 }
228
229 std::string challengeType = certRequest.getChallengeType();
230 auto challenge = ChallengeModule::createChallengeModule(challengeType);
231 if (challenge == nullptr) {
232 _LOG_TRACE("Unrecognized challenge type " << challengeType);
233 return;
234 }
235 JsonSection contentJson = challenge->handleChallengeRequest(request, certRequest);
236 try {
237 m_storage->updateRequest(certRequest);
238 }
239 catch (const std::exception& e) {
240 _LOG_TRACE("Cannot update request instance " << e.what());
241 return;
242 }
243
244 Data result;
245 result.setName(request.getName());
246 result.setContent(dataContentFromJson(contentJson));
247 m_keyChain.sign(result, signingByCertificate(caItem.m_anchor));
248 m_face.put(result);
249
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700250 m_requestUpdateCallback(certRequest);
251
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800252 if (certRequest.getStatus() == ChallengeModule::SUCCESS) {
253 issueCertificate(certRequest, caItem);
254 }
255}
256
257void
258CaModule::handleStatus(const Interest& request, const CaItem& caItem)
259{
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700260 // STATUS Naming Convention: /CA-prefix/CA/_STATUS/{Request-ID JSON}/[Signature components]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800261 _LOG_TRACE("Handle STATUS request");
262
263 CertificateRequest certRequest = getCertificateRequest(request, caItem.m_caName);
264 if (certRequest.getRequestId().empty()) {
265 return;
266 }
267
268 if (!security::verifySignature(request, certRequest.getCert())) {
269 _LOG_TRACE("Error: Interest with bad signature.");
270 return;
271 }
272
273 std::string challengeType = certRequest.getChallengeType();
274 auto challenge = ChallengeModule::createChallengeModule(challengeType);
275 if (challenge == nullptr) {
276 _LOG_TRACE("Unrecognized challenge type " << challengeType);
277 return;
278 }
279 JsonSection contentJson = challenge->handleChallengeRequest(request, certRequest);
280
281 Data result;
282 result.setName(request.getName());
283 result.setContent(dataContentFromJson(contentJson));
284 m_keyChain.sign(result, signingByCertificate(caItem.m_anchor));
285 m_face.put(result);
286}
287
288void
289CaModule::handleDownload(const Interest& request, const CaItem& caItem)
290{
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700291 // DOWNLOAD Naming Convention: /CA-prefix/CA/_DOWNLOAD/{Request-ID JSON}
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800292 _LOG_TRACE("Handle DOWNLOAD request");
293
294 JsonSection requestIdJson = jsonFromNameComponent(request.getName(), caItem.m_caName.size() + 2);
295 std::string requestId = requestIdJson.get(JSON_REQUEST_ID, "");
296 security::v2::Certificate signedCert;
297 try {
298 signedCert = m_storage->getCertificate(requestId);
299 }
300 catch (const std::exception& e) {
301 _LOG_TRACE("Error: " << e.what());
302 return;
303 }
304
305 Data result;
306 result.setName(request.getName());
307 result.setContent(signedCert.wireEncode());
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700308 m_keyChain.sign(result, signingByCertificate(caItem.m_anchor));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800309 m_face.put(result);
310}
311
312void
313CaModule::issueCertificate(const CertificateRequest& certRequest, const CaItem& caItem)
314{
315 Name certName = certRequest.getCert().getKeyName();
316 certName.append("NDNCERT").appendVersion();
317 security::v2::Certificate newCert;
318 newCert.setName(certName);
319 newCert.setContent(certRequest.getCert().getContent());
320 SignatureInfo signatureInfo;
321 security::ValidityPeriod period(time::system_clock::now(),
322 time::system_clock::now() + caItem.m_validityPeriod);
323 signatureInfo.setValidityPeriod(period);
324 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_CERT,
325 caItem.m_anchor, signatureInfo);
326 newCert.setFreshnessPeriod(caItem.m_freshnessPeriod);
327
328 m_keyChain.sign(newCert, signingInfo);
329 try {
330 m_storage->addCertificate(certRequest.getRequestId(), newCert);
331 m_storage->deleteRequest(certRequest.getRequestId());
332 _LOG_TRACE("New Certificate Issued " << certName);
333 }
334 catch (const std::exception& e) {
335 _LOG_TRACE("Error: Cannot add issued cert and remove the request " << e.what());
336 return;
337 }
338}
339
340CertificateRequest
341CaModule::getCertificateRequest(const Interest& request, const Name& caName)
342{
343 JsonSection requestIdJson = jsonFromNameComponent(request.getName(), caName.size() + 2);
344 std::string requestId = requestIdJson.get(JSON_REQUEST_ID, "");
345 CertificateRequest certRequest;
346 try {
347 certRequest = m_storage->getRequest(requestId);
348 }
349 catch (const std::exception& e) {
350 _LOG_TRACE("Error: " << e.what());
351 }
352 return certRequest;
353}
354
355void
356CaModule::onRegisterFailed(const std::string& reason)
357{
358 _LOG_TRACE("Error: failed to register prefix in local hub's daemon, REASON: " << reason);
359}
360
361Block
362CaModule::dataContentFromJson(const JsonSection& jsonSection)
363{
364 std::stringstream ss;
365 boost::property_tree::write_json(ss, jsonSection);
366 return makeStringBlock(ndn::tlv::Content, ss.str());
367}
368
369JsonSection
370CaModule::jsonFromNameComponent(const Name& name, int pos)
371{
372 std::string jsonString;
373 try {
374 jsonString = encoding::readString(name.at(pos));
375 }
376 catch (const std::exception& e) {
377 _LOG_TRACE(e.what());
378 return JsonSection();
379 }
380 std::istringstream ss(jsonString);
381 JsonSection json;
382 boost::property_tree::json_parser::read_json(ss, json);
383 return json;
384}
385
386} // namespace ndncert
387} // namespace ndn