blob: 2b9fcb858a088080ac4be88777a06441496da962 [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 Zhangdc25ddf2020-10-20 14:28:55 -070022#include "detail/crypto-helpers.hpp"
Zhiyi Zhang84e11842020-11-19 20:03:23 -080023#include "challenge/challenge-module.hpp"
Zhiyi Zhangc2c4ed22020-10-14 17:16:28 -070024#include "name-assignment/assignment-func.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070025#include "detail/challenge-encoder.hpp"
26#include "detail/error-encoder.hpp"
27#include "detail/info-encoder.hpp"
28#include "detail/new-renew-revoke-encoder.hpp"
29#include "detail/probe-encoder.hpp"
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070030#include <ndn-cxx/metadata-object.hpp>
31#include <ndn-cxx/security/signing-helpers.hpp>
32#include <ndn-cxx/security/verification-helpers.hpp>
33#include <ndn-cxx/util/io.hpp>
34#include <ndn-cxx/util/random.hpp>
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070035#include <ndn-cxx/util/string-helper.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080036
37namespace ndn {
38namespace ndncert {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070039namespace ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080040
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070041static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -070042static const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070044NDN_LOG_INIT(ndncert.ca);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080045
tylerliua7bea662020-10-08 18:51:02 -070046CaModule::CaModule(Face& face, security::KeyChain& keyChain,
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047 const std::string& configPath, const std::string& storageType)
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070048 : m_face(face)
49 , m_keyChain(keyChain)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070051 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080052 m_config.load(configPath);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080053 m_storage = CaStorage::createCaStorage(storageType, m_config.caProfile.caPrefix, "");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070054 random::generateSecureBytes(m_requestIdGenKey, 32);
tylerliuf2e6bb52020-12-13 13:23:05 -080055 if (m_config.nameAssignmentFuncs.size() == 0) {
56 m_config.nameAssignmentFuncs.push_back(NameAssignmentFunc::createNameAssignmentFunc("random"));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070057 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080058 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080059}
60
61CaModule::~CaModule()
62{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070063 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080065 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070066 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080068 }
69}
70
71void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080072CaModule::registerPrefix()
73{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 // register prefixes
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080075 Name prefix = m_config.caProfile.caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080077
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070078 auto prefixId = m_face.registerPrefix(
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070079 prefix,
80 [&](const Name& name) {
Zhiyi Zhang696cd042020-10-07 21:27:36 -070081 // register INFO RDR metadata prefix
82 name::Component metaDataComp(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
83 auto filterId = m_face.setInterestFilter(Name(name).append("INFO").append(metaDataComp),
84 bind(&CaModule::onCaProfileDiscovery, this, _2));
85 m_interestFilterHandles.push_back(filterId);
86
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070087 // register PROBE prefix
Zhiyi Zhang696cd042020-10-07 21:27:36 -070088 filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070089 bind(&CaModule::onProbe, this, _2));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070090 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070092 // register NEW prefix
93 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070094 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070095 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070097 // register SELECT prefix
98 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
99 bind(&CaModule::onChallenge, this, _2));
100 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700102 // register REVOKE prefix
103 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700104 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700105 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700106 NDN_LOG_TRACE("Prefix " << name << " got registered");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700107 },
108 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800110}
111
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700112void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700113CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800114{
tylerliu1f480be2020-11-10 13:02:53 -0800115 m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800116}
117
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700118Data
119CaModule::getCaProfileData()
swa77020643ac2020-03-26 02:24:45 -0700120{
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700121 if (m_profileData == nullptr) {
122 const auto& pib = m_keyChain.getPib();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800123 const auto& identity = pib.getIdentity( m_config.caProfile.caPrefix);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700124 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
tylerliuf2e6bb52020-12-13 13:23:05 -0800125 Block contentTLV = infotlv::encodeDataContent(m_config.caProfile, cert);
swa77020643ac2020-03-26 02:24:45 -0700126
Zhiyi Zhang615b6b72021-01-11 14:25:32 -0800127 // set naming convention to be typed
128 auto convention = name::getConventionEncoding();
129 name::setConventionEncoding(name::Convention::TYPED);
130
131 Name infoPacketName(m_config.caProfile.caPrefix);
132 auto segmentComp = name::Component::fromSegment(0);
133 infoPacketName.append("CA").append("INFO").appendVersion().append(segmentComp);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700134 m_profileData = std::make_unique<Data>(infoPacketName);
Zhiyi Zhang615b6b72021-01-11 14:25:32 -0800135 m_profileData->setFinalBlock(segmentComp);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700136 m_profileData->setContent(contentTLV);
137 m_profileData->setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800138 m_keyChain.sign(*m_profileData, signingByIdentity( m_config.caProfile.caPrefix));
Zhiyi Zhang615b6b72021-01-11 14:25:32 -0800139
140 // set back the convention
141 name::setConventionEncoding(convention);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700142 }
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700143 return *m_profileData;
144}
145
146void
147CaModule::onCaProfileDiscovery(const Interest& request)
148{
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700149 NDN_LOG_TRACE("Received CA Profile MetaData discovery Interest");
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700150 if (m_profileData == nullptr) {
151 m_profileData = std::make_unique<Data>(getCaProfileData());
152 }
153 MetadataObject metadata;
154 metadata.setVersionedName(m_profileData->getName().getPrefix(-1));
155 Name discoveryInterestName(m_profileData->getName().getPrefix(-2));
156 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
157 discoveryInterestName.append(metadataComponent);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800158 m_face.put(metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity( m_config.caProfile.caPrefix)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700159}
swa77020643ac2020-03-26 02:24:45 -0700160
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700161void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700162CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800163{
swa77020643ac2020-03-26 02:24:45 -0700164 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700165 NDN_LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800166
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700167 // process PROBE requests: collect probe parameters
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800168 auto parameters = probetlv::decodeApplicationParameters(request.getApplicationParameters());
tylerliu8fc08072020-10-06 15:37:40 -0700169 std::vector<PartialName> availableComponents;
tylerliuf2e6bb52020-12-13 13:23:05 -0800170 for (auto& item : m_config.nameAssignmentFuncs) {
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700171 auto names = item->assignName(parameters);
172 availableComponents.insert(availableComponents.end(), names.begin(), names.end());
173 }
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700174 if (availableComponents.size() == 0) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700175 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700176 "Cannot generate available names from parameters provided."));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700177 return;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700178 }
179 std::vector<Name> availableNames;
180 for (const auto& component : availableComponents) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800181 Name newIdentityName = m_config.caProfile.caPrefix;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700182 newIdentityName.append(component);
183 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700184 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700185
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700186 Data result;
187 result.setName(request.getName());
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800188 result.setContent(probetlv::encodeDataContent(availableNames, m_config.caProfile.maxSuffixLength, m_config.redirection));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700189 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800190 m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700191 m_face.put(result);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700192 NDN_LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800193}
194
195void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700196CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800197{
tylerliuf62cfeb2021-01-28 11:01:33 -0800198
199 //verify ca cert validity
200 const auto& caCert = m_keyChain.getPib().getIdentity( m_config.caProfile.caPrefix).getDefaultKey().getDefaultCertificate();
201 if (!caCert.isValid()) {
202 NDN_LOG_ERROR("Server certificate invalid/expired");
203 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
204 "Server certificate invalid/expired"));
205 return;
206 }
207
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700208 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700209 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700210 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700211 const auto& parameterTLV = request.getApplicationParameters();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700212 std::vector<uint8_t> ecdhPub;
tylerliua7bea662020-10-08 18:51:02 -0700213 shared_ptr<security::Certificate> clientCert;
tylerliu0790bdb2020-10-02 00:50:51 -0700214 try {
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800215 requesttlv::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700216 }
217 catch (const std::exception& e) {
tylerliu0790bdb2020-10-02 00:50:51 -0700218 if (!parameterTLV.hasValue()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700219 NDN_LOG_ERROR("Empty TLV obtained from the Interest parameter.");
tylerliu0790bdb2020-10-02 00:50:51 -0700220 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
221 "Empty TLV obtained from the Interest parameter."));
222 return;
223 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700224
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700225 NDN_LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700226 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
tylerliu0790bdb2020-10-02 00:50:51 -0700227 "Unrecognized self-signed certificate."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700228 return;
229 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700230
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700231 if (ecdhPub.empty()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700232 NDN_LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700233 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
234 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700235 return;
236 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700237
238 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700239 ECDHState ecdh;
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700240 auto myEcdhPubKeyBase64 = ecdh.getSelfPubKey();
241 std::vector<uint8_t> sharedSecret;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700242 try {
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700243 sharedSecret = ecdh.deriveSecret(ecdhPub);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700244 }
245 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700246 NDN_LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700247 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
248 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700249 return;
250 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700251 // generate salt for HKDF
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -0700252 std::array<uint8_t, 32> salt;
253 random::generateSecureBytes(salt.data(), salt.size());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700254 // hkdf
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700255 std::array<uint8_t, 16> aesKey;
256 hkdf(sharedSecret.data(), sharedSecret.size(), salt.data(), salt.size(), aesKey.data(), aesKey.size());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700257
tylerliud9239f52020-09-30 17:25:16 -0700258 // verify identity name
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800259 if (! m_config.caProfile.caPrefix.isPrefixOf(clientCert->getIdentity())
tylerliua7bea662020-10-08 18:51:02 -0700260 || !security::Certificate::isValidName(clientCert->getName())
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800261 || clientCert->getIdentity().size() <= m_config.caProfile.caPrefix.size()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700262 NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
tylerliud9239f52020-09-30 17:25:16 -0700263 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
264 "An invalid certificate name is being requested."));
265 return;
266 }
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800267 if ( m_config.caProfile.maxSuffixLength) {
268 if (clientCert->getIdentity().size() > m_config.caProfile.caPrefix.size() + * m_config.caProfile.maxSuffixLength) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700269 NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
tylerliud9239f52020-09-30 17:25:16 -0700270 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
271 "An invalid certificate name is being requested."));
272 return;
273 }
274 }
275
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700276 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700277 // check the validity period
278 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
279 auto currentTime = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700280 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800281 expectedPeriod.second > currentTime + m_config.caProfile.maxValidityPeriod ||
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700282 expectedPeriod.second <= expectedPeriod.first) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700283 NDN_LOG_ERROR("An invalid validity period is being requested.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700284 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
285 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700286 return;
287 }
tylerliud9239f52020-09-30 17:25:16 -0700288
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700289 // verify signature
tylerliu4a00aad2020-09-26 02:03:17 -0700290 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700291 NDN_LOG_ERROR("Invalid signature in the self-signed certificate.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700292 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
293 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700294 return;
295 }
296 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700297 NDN_LOG_ERROR("Invalid signature in the Interest packet.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700298 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
299 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700300 return;
301 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700302 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700303 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700304 //verify cert is from this CA
tylerliuf62cfeb2021-01-28 11:01:33 -0800305 if (!security::verifySignature(*clientCert, caCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700306 NDN_LOG_ERROR("Invalid signature in the certificate to revoke.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700307 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
308 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700309 return;
310 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700311 }
312
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700313 // create new request instance
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700314 uint8_t requestIdData[32];
315 Block certNameTlv = clientCert->getName().wireEncode();
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700316 try {
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700317 hmacSha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData);
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700318 }
319 catch (const std::runtime_error& e) {
320 NDN_LOG_ERROR("Error computing the request ID: " << std::string(e.what()));
321 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
322 "Error computing the request ID."));
323 return;
324 }
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700325 RequestId id;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700326 std::memcpy(id.data(), requestIdData, id.size());
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800327 // initialize request state
328 RequestState requestState;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800329 requestState.caPrefix = m_config.caProfile.caPrefix;
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800330 requestState.requestId = id;
331 requestState.requestType = requestType;
332 requestState.cert = *clientCert;
333 requestState.encryptionKey = aesKey;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700334 try {
335 m_storage->addRequest(requestState);
336 }
337 catch (const std::runtime_error& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700338 NDN_LOG_ERROR("Duplicate Request ID: The same request has been seen before.");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700339 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700340 "Duplicate Request ID: The same request has been seen before."));
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700341 return;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700342 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800343 Data result;
344 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700345 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800346 result.setContent(requesttlv::encodeDataContent(myEcdhPubKeyBase64,
tylerliu7b9185c2020-11-24 12:15:18 -0800347 salt,
348 requestState.requestId, requestState.status,
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800349 m_config.caProfile.supportedChallenges));
350 m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800351 m_face.put(result);
tylerliu1f480be2020-11-10 13:02:53 -0800352 if (m_statusUpdateCallback) {
353 m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800354 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800355}
356
357void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700358CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800359{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700360 // get certificate request state
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700361 auto requestState = getCertificateRequest(request);
Zhiyi Zhang84207c52020-10-28 21:45:44 -0700362 if (requestState == nullptr) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700363 NDN_LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700364 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700365 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800366 return;
367 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700368 // verify signature
tylerliu7b9185c2020-11-24 12:15:18 -0800369 if (!security::verifySignature(request, requestState->cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700370 NDN_LOG_ERROR("Invalid Signature in the Interest packet.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700371 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
372 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800373 return;
374 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700375 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700376 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700377 try {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700378 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(),
tylerliu7b9185c2020-11-24 12:15:18 -0800379 requestState->encryptionKey.data(),
380 requestState->requestId.data(),
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800381 requestState->requestId.size(),
382 requestState->decryptionIv);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700383 }
384 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700385 NDN_LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu7b9185c2020-11-24 12:15:18 -0800386 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700387 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700388 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700389 return;
390 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700391 if (paramTLVPayload.size() == 0) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700392 NDN_LOG_ERROR("No parameters are found after decryption.");
tylerliu7b9185c2020-11-24 12:15:18 -0800393 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700394 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700395 "No parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700396 return;
397 }
tylerliu50d679e2020-10-14 14:08:39 -0700398 Block paramTLV = makeBinaryBlock(tlv::EncryptedPayload, paramTLVPayload.data(), paramTLVPayload.size());
Suyong Won44d0cce2020-05-10 04:07:43 -0700399 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800400
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700401 // load the corresponding challenge module
tylerliu50d679e2020-10-14 14:08:39 -0700402 std::string challengeType = readString(paramTLV.get(tlv::SelectedChallenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800403 auto challenge = ChallengeModule::createChallengeModule(challengeType);
404 if (challenge == nullptr) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700405 NDN_LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu7b9185c2020-11-24 12:15:18 -0800406 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700407 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700408 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800409 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700410
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700411 NDN_LOG_TRACE("CHALLENGE module to be load: " << challengeType);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700412 auto errorInfo = challenge->handleChallengeRequest(paramTLV, *requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700413 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu7b9185c2020-11-24 12:15:18 -0800414 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700415 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
416 return;
417 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700418
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700419 Block payload;
tylerliu7b9185c2020-11-24 12:15:18 -0800420 if (requestState->status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700421 // if challenge succeeded
tylerliud82cc5c2020-12-21 23:58:39 -0800422 if (requestState->requestType == RequestType::NEW || requestState->requestType == RequestType::RENEW) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700423 auto issuedCert = issueCertificate(*requestState);
tylerliu7b9185c2020-11-24 12:15:18 -0800424 requestState->cert = issuedCert;
425 requestState->status = Status::SUCCESS;
426 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700427
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800428 payload = challengetlv::encodeDataContent(*requestState, issuedCert.getName());
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700429 NDN_LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800430 }
tylerliu7b9185c2020-11-24 12:15:18 -0800431 else if (requestState->requestType == RequestType::REVOKE) {
432 requestState->status = Status::SUCCESS;
433 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -0800434 // TODO: where is the code to revoke?
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800435 payload = challengetlv::encodeDataContent(*requestState);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700436 NDN_LOG_TRACE("Challenge succeeded. Certificate has been revoked");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700437 }
438 }
439 else {
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800440 payload = challengetlv::encodeDataContent(*requestState);
Zhiyi Zhangc1e98152020-12-21 16:15:39 -0800441 m_storage->updateRequest(*requestState);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700442 NDN_LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800443 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700444
445 Data result;
446 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700447 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700448 result.setContent(payload);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800449 m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700450 m_face.put(result);
tylerliu1f480be2020-11-10 13:02:53 -0800451 if (m_statusUpdateCallback) {
452 m_statusUpdateCallback(*requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800453 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700454}
455
tylerliua7bea662020-10-08 18:51:02 -0700456security::Certificate
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700457CaModule::issueCertificate(const RequestState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800458{
tylerliu7b9185c2020-11-24 12:15:18 -0800459 auto expectedPeriod = requestState.cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700460 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
tylerliua7bea662020-10-08 18:51:02 -0700461 security::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700462
tylerliu7b9185c2020-11-24 12:15:18 -0800463 Name certName = requestState.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);
tylerliu7b9185c2020-11-24 12:15:18 -0800466 newCert.setContent(requestState.cert.getContent());
467 NDN_LOG_TRACE("cert request content " << requestState.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 Zhang44c6a352020-12-14 10:57:17 -0800471 m_config.caProfile.caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800472
473 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700474 NDN_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
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700478std::unique_ptr<RequestState>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700479CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800480{
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700481 RequestId requestId;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800482 try {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800483 auto& component = request.getName().at(m_config.caProfile.caPrefix.size() + 2);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700484 std::memcpy(requestId.data(), component.value(), component.value_size());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700485 }
486 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700487 NDN_LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700488 return nullptr;
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700489 }
490 try {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700491 NDN_LOG_TRACE("Request Id to query the database " << toHex(requestId.data(), requestId.size()));
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700492 return std::make_unique<RequestState>(m_storage->getRequest(requestId));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800493 }
494 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700495 NDN_LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700496 return nullptr;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800497 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800498}
499
500void
501CaModule::onRegisterFailed(const std::string& reason)
502{
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700503 NDN_LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800504}
505
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700506Data
507CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
508{
509 Data result;
510 result.setName(name);
511 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800512 result.setContent(errortlv::encodeDataContent(error, errorInfo));
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800513 m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700514 return result;
515}
516
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700517} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700518} // namespace ndncert
519} // namespace ndn