blob: c32a4f42bb9a962313566c5944174eefc0f07aa2 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
swa770de007bc2020-03-24 21:26:21 -07002/**
Davide Pesaventob48bbda2020-07-27 19:41:37 -04003 * 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
21#include "ca-module.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070022#include "challenge-module.hpp"
23#include "crypto-support/enc-tlv.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070024#include "protocol-detail/challenge.hpp"
25#include "protocol-detail/error.hpp"
26#include "protocol-detail/info.hpp"
tylerliu4889a782020-09-30 22:47:49 -070027#include "protocol-detail/new-renew-revoke.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070028#include "protocol-detail/probe.hpp"
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070029#include <ndn-cxx/metadata-object.hpp>
30#include <ndn-cxx/security/signing-helpers.hpp>
31#include <ndn-cxx/security/verification-helpers.hpp>
32#include <ndn-cxx/util/io.hpp>
33#include <ndn-cxx/util/random.hpp>
tylerliu8d9e7122020-10-06 18:57:06 -070034#include <name-assignments/assignment-funcs.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080035
36namespace ndn {
37namespace ndncert {
38
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070039static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -070040static const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080042_LOG_INIT(ndncert.ca);
43
44CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
45 const std::string& configPath, const std::string& storageType)
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070046 : m_face(face)
47 , m_keyChain(keyChain)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080048{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070049 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050 m_config.load(configPath);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070051 m_storage = CaStorage::createCaStorage(storageType, m_config.m_caItem.m_caPrefix, "");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070052 random::generateSecureBytes(m_requestIdGenKey, 32);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080053 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080054}
55
56CaModule::~CaModule()
57{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070058 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080060 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070061 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080063 }
64}
65
66void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080067CaModule::registerPrefix()
68{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069 // register localhop discovery prefix
swa77020643ac2020-03-26 02:24:45 -070070 Name localhopInfoPrefix("/localhop/CA/INFO");
71 auto prefixId = m_face.setInterestFilter(InterestFilter(localhopInfoPrefix),
72 bind(&CaModule::onInfo, this, _2),
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080073 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 m_registeredPrefixHandles.push_back(prefixId);
swa77020643ac2020-03-26 02:24:45 -070075 _LOG_TRACE("Prefix " << localhopInfoPrefix << " got registered");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080076
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077 // register prefixes
Zhiyi Zhang9829da92020-09-30 16:19:34 -070078 Name prefix = m_config.m_caItem.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080080
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070081 prefixId = m_face.registerPrefix(
82 prefix,
83 [&](const Name& name) {
84 // register INFO prefix
85 auto filterId = m_face.setInterestFilter(Name(name).append("INFO"),
86 bind(&CaModule::onInfo, this, _2));
87 m_interestFilterHandles.push_back(filterId);
swa77020643ac2020-03-26 02:24:45 -070088
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070089 // register PROBE prefix
90 filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
91 bind(&CaModule::onProbe, this, _2));
92 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070093
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070094 // register NEW prefix
95 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070096 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070097 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070099 // register SELECT prefix
100 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
101 bind(&CaModule::onChallenge, this, _2));
102 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700104 // register REVOKE prefix
105 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700106 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700107 m_interestFilterHandles.push_back(filterId);
108 _LOG_TRACE("Prefix " << name << " got registered");
109 },
110 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800112}
113
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700114void
115CaModule::setNameAssignmentFunction(const NameAssignmentFunc& handler)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800116{
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700117 m_config.m_nameAssignmentFunc = handler;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800118}
119
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700120void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800122{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800124}
125
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700126shared_ptr<Data>
127CaModule::generateCaConfigMetaData()
swa77020643ac2020-03-26 02:24:45 -0700128{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700129 // @TODO
130 // make metadata a class member variable m_infoMetadata
131 // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one
132 // otherwise, directly reply m_infoMetadata.makeData
133
134 auto infoPacket = generateCaConfigData();
135 MetadataObject metadata;
136 metadata.setVersionedName(infoPacket->getName().getPrefix(-1));
137 Name discoveryInterestName(infoPacket->getName().getPrefix(-2));
138 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
139 discoveryInterestName.append(metadataComponent);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700140 auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700141 return make_shared<Data>(metadataData);
142}
143
144shared_ptr<Data>
145CaModule::generateCaConfigData()
146{
147 // @TODO
148 // make CaInfo Data packet a class member variable m_infoData
149 // check whether the m_infoData is still valid, if not, then generate a new one
150 // otherwise, directly reply m_infoData
Suyong Won19fba4d2020-05-09 13:39:46 -0700151
152 const auto& pib = m_keyChain.getPib();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700153 const auto& identity = pib.getIdentity(m_config.m_caItem.m_caPrefix);
Suyong Won19fba4d2020-05-09 13:39:46 -0700154 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700155 Block contentTLV = INFO::encodeDataContent(m_config.m_caItem, cert);
swa77020643ac2020-03-26 02:24:45 -0700156
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700157 Name infoPacketName(m_config.m_caItem.m_caPrefix);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700158 infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
159 Data infoData(infoPacketName);
160 infoData.setContent(contentTLV);
161 infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700162 m_keyChain.sign(infoData, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700163 return make_shared<Data>(infoData);
164}
swa77020643ac2020-03-26 02:24:45 -0700165
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700166void
167CaModule::onInfo(const Interest& request)
168{
169 _LOG_TRACE("Received INFO request");
170
171 if (request.getName().get(-1).type() == 32) {
172 m_face.put(*generateCaConfigMetaData());
173 }
174 else {
175 m_face.put(*generateCaConfigData());
176 }
swa77020643ac2020-03-26 02:24:45 -0700177
178 _LOG_TRACE("Handle INFO: send out the INFO response");
179}
180
181void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700182CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800183{
swa77020643ac2020-03-26 02:24:45 -0700184 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
185 _LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800186
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700187 // process PROBE requests: collect probe parameters
188 auto parameters = PROBE::decodeApplicationParameters(request.getApplicationParameters());
tylerliu8fc08072020-10-06 15:37:40 -0700189 std::vector<PartialName> availableComponents;
tylerliu355370e2020-10-07 14:56:33 -0700190 if (m_config.m_nameAssignmentFunc) {
191 try {
192 availableComponents = m_config.m_nameAssignmentFunc(parameters);
193 }
194 catch (const std::exception &e) {
195 _LOG_TRACE("Cannot parse probe parameters: " << e.what());
196 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
197 "Cannot parse probe parameters: " + std::string(e.what())));
198 return;
199 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700200 }
201 std::vector<Name> availableNames;
202 for (const auto& component : availableComponents) {
203 Name newIdentityName = m_config.m_caItem.m_caPrefix;
204 newIdentityName.append(component);
205 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700206 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700207
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700208 Data result;
209 result.setName(request.getName());
Suyong Wone2afeb52020-10-04 03:05:39 +0900210 result.setContent(PROBE::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength, m_config.m_redirection));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700211 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
212 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
213 m_face.put(result);
214 _LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800215}
216
217void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700218CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800219{
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700220 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700221 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700222 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700223 const auto& parameterTLV = request.getApplicationParameters();
tylerliu0790bdb2020-10-02 00:50:51 -0700224 std::string ecdhPub;
225 shared_ptr<security::v2::Certificate> clientCert;
226 try {
227 NEW_RENEW_REVOKE::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert);
228 } catch (const std::exception& e) {
229 if (!parameterTLV.hasValue()) {
230 _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
231 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
232 "Empty TLV obtained from the Interest parameter."));
233 return;
234 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700235
tylerliu0790bdb2020-10-02 00:50:51 -0700236 _LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700237 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
tylerliu0790bdb2020-10-02 00:50:51 -0700238 "Unrecognized self-signed certificate."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700239 return;
240 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700241
tylerliu0790bdb2020-10-02 00:50:51 -0700242 if (ecdhPub == "") {
Suyong Won19fba4d2020-05-09 13:39:46 -0700243 _LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700244 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
245 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700246 return;
247 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700248
249 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700250 ECDHState ecdh;
251 auto myEcdhPubKeyBase64 = ecdh.getBase64PubKey();
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700252 try {
tylerliu0790bdb2020-10-02 00:50:51 -0700253 ecdh.deriveSecret(ecdhPub);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700254 }
255 catch (const std::exception& e) {
256 _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700257 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
258 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700259 return;
260 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700261 // generate salt for HKDF
262 auto saltInt = random::generateSecureWord64();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700263 // hkdf
tylerliu8e170d62020-09-30 01:31:53 -0700264 uint8_t aesKey[AES_128_KEY_LEN];
265 hkdf(ecdh.context->sharedSecret, ecdh.context->sharedSecretLen,
266 (uint8_t*)&saltInt, sizeof(saltInt), aesKey, sizeof(aesKey));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700267
tylerliud9239f52020-09-30 17:25:16 -0700268 // verify identity name
269 if (!m_config.m_caItem.m_caPrefix.isPrefixOf(clientCert->getIdentity())
270 || !security::v2::Certificate::isValidName(clientCert->getName())
271 || clientCert->getIdentity().size() <= m_config.m_caItem.m_caPrefix.size()) {
272 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
273 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
274 "An invalid certificate name is being requested."));
275 return;
276 }
277 if (m_config.m_caItem.m_maxSuffixLength) {
278 if (clientCert->getIdentity().size() > m_config.m_caItem.m_caPrefix.size() + *m_config.m_caItem.m_maxSuffixLength) {
279 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
280 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
281 "An invalid certificate name is being requested."));
282 return;
283 }
284 }
285
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700286 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700287 // check the validity period
288 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
289 auto currentTime = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700290 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700291 expectedPeriod.second > currentTime + m_config.m_caItem.m_maxValidityPeriod ||
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700292 expectedPeriod.second <= expectedPeriod.first) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700293 _LOG_ERROR("An invalid validity period is being requested.");
294 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
295 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700296 return;
297 }
tylerliud9239f52020-09-30 17:25:16 -0700298
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700299 // verify signature
tylerliu4a00aad2020-09-26 02:03:17 -0700300 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700301 _LOG_ERROR("Invalid signature in the self-signed certificate.");
302 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
303 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700304 return;
305 }
306 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700307 _LOG_ERROR("Invalid signature in the Interest packet.");
308 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
309 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700310 return;
311 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700312 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700313 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700314 //verify cert is from this CA
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700315 const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caItem.m_caPrefix).getDefaultKey().getDefaultCertificate();
tylerliu4a00aad2020-09-26 02:03:17 -0700316 if (!security::verifySignature(*clientCert, cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700317 _LOG_ERROR("Invalid signature in the certificate to revoke.");
318 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
319 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700320 return;
321 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700322 }
323
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700324 // create new request instance
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700325 uint8_t requestIdData[32];
326 Block certNameTlv = clientCert->getName().wireEncode();
327 ndn_compute_hmac_sha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData);
328 CaState requestState(m_config.m_caItem.m_caPrefix, hexlify(requestIdData, 32),
329 requestType, Status::BEFORE_CHALLENGE, *clientCert,
330 makeBinaryBlock(tlv::ContentType_Key, aesKey, sizeof(aesKey)));
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700331 try {
332 m_storage->addRequest(requestState);
333 }
334 catch (const std::runtime_error& e) {
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700335 _LOG_ERROR("Duplicate Request ID: The same request has been seen before.");
336 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
337 "Duplicate Request ID: The same request has been seen before.."));
338 return;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700339 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800340 Data result;
341 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700342 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu4889a782020-09-30 22:47:49 -0700343 result.setContent(NEW_RENEW_REVOKE::encodeDataContent(myEcdhPubKeyBase64,
tylerliu4a00aad2020-09-26 02:03:17 -0700344 std::to_string(saltInt),
tylerliu63900d52020-09-30 03:01:18 -0700345 requestState,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700346 m_config.m_caItem.m_supportedChallenges));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700347 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800348 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700349 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700350 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800351 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800352}
353
354void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700355CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800356{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700357 // get certificate request state
tylerliu8704d032020-06-23 10:18:15 -0700358 CaState requestState = getCertificateRequest(request);
tylerliu63900d52020-09-30 03:01:18 -0700359 if (requestState.m_requestId == "") {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700360 _LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700361 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700362 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800363 return;
364 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700365 // verify signature
tylerliu63900d52020-09-30 03:01:18 -0700366 if (!security::verifySignature(request, requestState.m_cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700367 _LOG_ERROR("Invalid Signature in the Interest packet.");
368 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
369 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800370 return;
371 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700372 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700373 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700374 try {
tylerliu63900d52020-09-30 03:01:18 -0700375 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), requestState.m_encryptionKey.value(),
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700376 (uint8_t*)"test", strlen("test"));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700377 }
378 catch (const std::exception& e) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700379 _LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu63900d52020-09-30 03:01:18 -0700380 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700381 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700382 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700383 return;
384 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700385 if (paramTLVPayload.size() == 0) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700386 _LOG_ERROR("No parameters are found after decryption.");
tylerliu63900d52020-09-30 03:01:18 -0700387 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700388 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700389 "No parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700390 return;
391 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700392 Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size());
393 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800394
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700395 // load the corresponding challenge module
Suyong Won19fba4d2020-05-09 13:39:46 -0700396 std::string challengeType = readString(paramTLV.get(tlv_selected_challenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800397 auto challenge = ChallengeModule::createChallengeModule(challengeType);
398 if (challenge == nullptr) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700399 _LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700400 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700401 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700402 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800403 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700404
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700405 _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700406 auto errorInfo = challenge->handleChallengeRequest(paramTLV, requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700407 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu63900d52020-09-30 03:01:18 -0700408 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700409 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
410 return;
411 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700412
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700413 Block payload;
tylerliu63900d52020-09-30 03:01:18 -0700414 if (requestState.m_status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700415 // if challenge succeeded
tylerliu63900d52020-09-30 03:01:18 -0700416 if (requestState.m_requestType == RequestType::NEW) {
417 auto issuedCert = issueCertificate(requestState);
418 requestState.m_cert = issuedCert;
419 requestState.m_status = Status::SUCCESS;
tylerliu63900d52020-09-30 03:01:18 -0700420 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700421
tylerliu63900d52020-09-30 03:01:18 -0700422 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700423 payload.parse();
424 payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
425 payload.encode();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700426 _LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800427 }
tylerliu63900d52020-09-30 03:01:18 -0700428 else if (requestState.m_requestType == RequestType::REVOKE) {
429 requestState.m_status = Status::SUCCESS;
430 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700431
tylerliu63900d52020-09-30 03:01:18 -0700432 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700433 _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
434 }
435 }
436 else {
tylerliu63900d52020-09-30 03:01:18 -0700437 m_storage->updateRequest(requestState);
438 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700439 _LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800440 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700441
442 Data result;
443 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700444 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu63900d52020-09-30 03:01:18 -0700445 auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, requestState.m_encryptionKey.value(), payload.value(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700446 payload.value_size(), (uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700447 result.setContent(contentBlock);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700448 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700449 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700450 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700451 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800452 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700453}
454
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800455security::v2::Certificate
tylerliu8704d032020-06-23 10:18:15 -0700456CaModule::issueCertificate(const CaState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800457{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700458 auto expectedPeriod =
tylerliu63900d52020-09-30 03:01:18 -0700459 requestState.m_cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700460 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800461 security::v2::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700462
tylerliu63900d52020-09-30 03:01:18 -0700463 Name certName = requestState.m_cert.getKeyName();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700464 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800465 newCert.setName(certName);
tylerliu63900d52020-09-30 03:01:18 -0700466 newCert.setContent(requestState.m_cert.getContent());
467 _LOG_TRACE("cert request content " << requestState.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800468 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800469 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700470 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700471 m_config.m_caItem.m_caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800472
473 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700474 _LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800475 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800476}
477
tylerliu8704d032020-06-23 10:18:15 -0700478CaState
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700479CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800480{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400481 std::string requestId;
tylerliu8704d032020-06-23 10:18:15 -0700482 CaState requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800483 try {
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700484 requestId = readString(request.getName().at(m_config.m_caItem.m_caPrefix.size() + 2));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700485 }
486 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700487 _LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700488 }
489 try {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700490 _LOG_TRACE("Request Id to query the database " << requestId);
tylerliu63900d52020-09-30 03:01:18 -0700491 requestState = m_storage->getRequest(requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800492 }
493 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700494 _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800495 }
tylerliu63900d52020-09-30 03:01:18 -0700496 return requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800497}
498
499void
500CaModule::onRegisterFailed(const std::string& reason)
501{
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700502 _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800503}
504
505Block
506CaModule::dataContentFromJson(const JsonSection& jsonSection)
507{
508 std::stringstream ss;
509 boost::property_tree::write_json(ss, jsonSection);
510 return makeStringBlock(ndn::tlv::Content, ss.str());
511}
512
513JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700514CaModule::jsonFromBlock(const Block& block)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800515{
516 std::string jsonString;
517 try {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700518 jsonString = encoding::readString(block);
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700519 std::istringstream ss(jsonString);
520 JsonSection json;
521 boost::property_tree::json_parser::read_json(ss, json);
522 return json;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800523 }
524 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700525 _LOG_ERROR("Cannot read JSON string from TLV Value: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800526 return JsonSection();
527 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800528}
529
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700530Data
531CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
532{
533 Data result;
534 result.setName(name);
535 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
536 result.setContent(ErrorTLV::encodeDataContent(error, errorInfo));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700537 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700538 return result;
539}
540
541} // namespace ndncert
542} // namespace ndn