Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 2 | /** |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 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" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 24 | #include "crypto-support/enc-tlv.hpp" |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 25 | #include <ndn-cxx/util/io.hpp> |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 26 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 27 | #include <ndn-cxx/security/signing-helpers.hpp> |
Junxiao Shi | 7c06803 | 2017-05-28 13:40:47 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndncert { |
| 32 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 33 | static const int IS_SUBNAME_MIN_OFFSET = 5; |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 34 | static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s; |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 35 | static const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 37 | _LOG_INIT(ndncert.ca); |
| 38 | |
| 39 | CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain, |
| 40 | const std::string& configPath, const std::string& storageType) |
| 41 | : m_face(face) |
| 42 | , m_keyChain(keyChain) |
| 43 | { |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 44 | // load the config and create storage |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 45 | m_config.load(configPath); |
| 46 | m_storage = CaStorage::createCaStorage(storageType); |
| 47 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 48 | registerPrefix(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CaModule::~CaModule() |
| 52 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 53 | for (auto handle : m_interestFilterHandles) { |
| 54 | handle.cancel(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 55 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | for (auto handle : m_registeredPrefixHandles) { |
| 57 | handle.unregister(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | void |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 62 | CaModule::registerPrefix() |
| 63 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 64 | // register localhop discovery prefix |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 65 | Name localhopInfoPrefix("/localhop/CA/INFO"); |
| 66 | auto prefixId = m_face.setInterestFilter(InterestFilter(localhopInfoPrefix), |
| 67 | bind(&CaModule::onInfo, this, _2), |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 68 | bind(&CaModule::onRegisterFailed, this, _2)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 69 | m_registeredPrefixHandles.push_back(prefixId); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 70 | _LOG_TRACE("Prefix " << localhopInfoPrefix << " got registered"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 71 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 72 | // register prefixes |
| 73 | Name prefix = m_config.m_caName; |
| 74 | prefix.append("CA"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 75 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 76 | prefixId = m_face.registerPrefix(prefix, |
| 77 | [&] (const Name& name) { |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 78 | // register INFO prefix |
| 79 | auto filterId = m_face.setInterestFilter(Name(name).append("INFO"), |
| 80 | bind(&CaModule::onInfo, this, _2)); |
| 81 | m_interestFilterHandles.push_back(filterId); |
| 82 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 83 | // register PROBE prefix |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 84 | filterId = m_face.setInterestFilter(Name(name).append("PROBE"), |
| 85 | bind(&CaModule::onProbe, this, _2)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 86 | m_interestFilterHandles.push_back(filterId); |
| 87 | |
| 88 | // register NEW prefix |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 89 | filterId = m_face.setInterestFilter(Name(name).append("NEW"), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 90 | bind(&CaModule::onNew, this, _2)); |
| 91 | m_interestFilterHandles.push_back(filterId); |
| 92 | |
| 93 | // register SELECT prefix |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 94 | filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 95 | bind(&CaModule::onChallenge, this, _2)); |
| 96 | m_interestFilterHandles.push_back(filterId); |
| 97 | |
| 98 | // register DOWNLOAD prefix |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 99 | filterId = m_face.setInterestFilter(Name(name).append("DOWNLOAD"), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 100 | bind(&CaModule::onDownload, this, _2)); |
| 101 | m_interestFilterHandles.push_back(filterId); |
| 102 | _LOG_TRACE("Prefix " << name << " got registered"); |
| 103 | }, |
| 104 | bind(&CaModule::onRegisterFailed, this, _2)); |
| 105 | m_registeredPrefixHandles.push_back(prefixId); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 108 | bool |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 109 | CaModule::setProbeHandler(const ProbeHandler& handler) |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 110 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 111 | m_config.m_probeHandler = handler; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 112 | return false; |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 115 | bool |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 116 | CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback) |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 117 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 118 | m_config.m_statusUpdateCallback = onUpdateCallback; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 119 | return false; |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 123 | CaModule::onInfo(const Interest& request) |
| 124 | { |
| 125 | _LOG_TRACE("Received INFO request"); |
| 126 | JsonSection contentJson = genInfoResponseJson(); |
| 127 | Data result; |
| 128 | |
| 129 | result.setName(request.getName()); |
| 130 | result.setContent(dataContentFromJson(contentJson)); |
| 131 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
| 132 | |
| 133 | m_keyChain.sign(result, signingByIdentity(m_config.m_caName)); |
| 134 | m_face.put(result); |
| 135 | |
| 136 | _LOG_TRACE("Handle INFO: send out the INFO response"); |
| 137 | } |
| 138 | |
| 139 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 140 | CaModule::onProbe(const Interest& request) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 141 | { |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 142 | // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent] |
| 143 | _LOG_TRACE("Received PROBE request"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 144 | JsonSection contentJson; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 145 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 146 | // process PROBE requests: find an available name |
| 147 | std::string availableId; |
| 148 | const auto& parameterJson = jsonFromBlock(request.getApplicationParameters()); |
| 149 | if (parameterJson.empty()) { |
| 150 | _LOG_ERROR("Empty JSON obtained from the Interest parameter."); |
| 151 | return; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 152 | } |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 153 | //std::string probeInfoStr = parameterJson.get(JSON_CLIENT_PROBE_INFO, ""); |
| 154 | if (m_config.m_probeHandler) { |
| 155 | try { |
| 156 | availableId = m_config.m_probeHandler(parameterJson); |
| 157 | } |
| 158 | catch (const std::exception& e) { |
| 159 | _LOG_TRACE("Cannot find PROBE input from PROBE parameters: " << e.what()); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 162 | } |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 163 | else { |
| 164 | // if there is no app-specified name lookup, use a random name id |
| 165 | availableId = std::to_string(random::generateSecureWord64()); |
| 166 | } |
| 167 | Name newIdentityName = m_config.m_caName; |
| 168 | newIdentityName.append(availableId); |
| 169 | _LOG_TRACE("Handle PROBE: generate an identity " << newIdentityName); |
| 170 | contentJson = genProbeResponseJson(newIdentityName.toUri(), m_config.m_probe, parameterJson); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 171 | |
| 172 | Data result; |
| 173 | result.setName(request.getName()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 174 | result.setContent(dataContentFromJson(contentJson)); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 175 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 176 | m_keyChain.sign(result, signingByIdentity(m_config.m_caName)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 177 | m_face.put(result); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 178 | _LOG_TRACE("Handle PROBE: send out the PROBE response"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 182 | CaModule::onNew(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 183 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 184 | // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest] |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 185 | // get ECDH pub key and cert request |
| 186 | const auto& parameterJson = jsonFromBlock(request.getApplicationParameters()); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 187 | if (parameterJson.empty()) { |
| 188 | _LOG_ERROR("Empty JSON obtained from the Interest parameter."); |
| 189 | return; |
| 190 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 191 | std::string peerKeyBase64 = parameterJson.get(JSON_CLIENT_ECDH, ""); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 192 | if (peerKeyBase64 == "") { |
| 193 | _LOG_ERROR("Empty JSON_CLIENT_ECDH obtained from the Interest parameter."); |
| 194 | return; |
| 195 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 196 | |
| 197 | // get server's ECDH pub key |
| 198 | auto myEcdhPubKeyBase64 = m_ecdh.getBase64PubKey(); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 199 | try { |
| 200 | m_ecdh.deriveSecret(peerKeyBase64); |
| 201 | } |
| 202 | catch (const std::exception& e) { |
| 203 | _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what()); |
| 204 | return; |
| 205 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 206 | // generate salt for HKDF |
| 207 | auto saltInt = random::generateSecureWord64(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 208 | // hkdf |
| 209 | hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen, |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 210 | (uint8_t*)&saltInt, sizeof(saltInt), m_aesKey, sizeof(m_aesKey)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 211 | |
| 212 | // parse certificate request |
| 213 | std::string certRequestStr = parameterJson.get(JSON_CLIENT_CERT_REQ, ""); |
| 214 | shared_ptr<security::v2::Certificate> clientCert = nullptr; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 215 | try { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 216 | std::stringstream ss(certRequestStr); |
| 217 | clientCert = io::load<security::v2::Certificate>(ss); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 218 | } |
| 219 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 220 | _LOG_ERROR("Unrecognized certificate request: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 223 | // check the validity period |
| 224 | auto expectedPeriod = clientCert->getValidityPeriod().getPeriod(); |
| 225 | auto currentTime = time::system_clock::now(); |
| 226 | if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD) { |
| 227 | _LOG_ERROR("Client requests a too old notBefore timepoint."); |
| 228 | return; |
| 229 | } |
| 230 | if (expectedPeriod.second > currentTime + m_config.m_validityPeriod || |
| 231 | expectedPeriod.second <= expectedPeriod.first) { |
| 232 | _LOG_ERROR("Client requests an invalid validity period or a notAfter timepoint beyond the allowed time period."); |
| 233 | return; |
| 234 | } |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 235 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 236 | // parse probe token if any |
| 237 | std::string probeTokenStr = parameterJson.get("probe-token", ""); |
| 238 | shared_ptr<Data> probeToken = nullptr; |
| 239 | if (probeTokenStr != "") { |
| 240 | try { |
| 241 | std::stringstream ss(probeTokenStr); |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 242 | probeToken = io::load<Data>(ss); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 243 | } |
| 244 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 245 | _LOG_ERROR("Unrecognized probe token: " << e.what()); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | } |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 249 | if (probeToken == nullptr && m_config.m_probe != "") { |
| 250 | // the CA requires PROBE before NEW |
| 251 | _LOG_ERROR("CA requires PROBE but no PROBE token is found in NEW Interest."); |
| 252 | return; |
| 253 | } |
| 254 | else if (probeToken != nullptr) { |
| 255 | // check whether the carried probe token is a PROBE Data packet |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 256 | Name prefix = m_config.m_caName; |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 257 | prefix.append("CA").append("PROBE"); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 258 | if (!prefix.isPrefixOf(probeToken->getName())) { |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 259 | _LOG_ERROR("Carried PROBE token is not a valid PROBE Data packet."); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // verify the self-signed certificate, the request, and the token |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 265 | if (!m_config.m_caName.isPrefixOf(clientCert->getName()) // under ca prefix |
| 266 | || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name |
| 267 | || clientCert->getName().size() != m_config.m_caName.size() + IS_SUBNAME_MIN_OFFSET) { |
| 268 | _LOG_ERROR("Invalid self-signed certificate name " << clientCert->getName()); |
| 269 | return; |
| 270 | } |
| 271 | if (!security::verifySignature(*clientCert, *clientCert)) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 272 | _LOG_ERROR("Cert request with bad signature."); |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 273 | return; |
| 274 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 275 | if (!security::verifySignature(request, *clientCert)) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 276 | _LOG_ERROR("Interest with bad signature."); |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 277 | return; |
| 278 | } |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 279 | if (probeToken != nullptr) { |
| 280 | const auto& pib = m_keyChain.getPib(); |
| 281 | const auto& key = pib.getIdentity(m_config.m_caName).getDefaultKey(); |
| 282 | const auto& caCert = key.getDefaultCertificate(); |
| 283 | if (!security::verifySignature(*probeToken, caCert)) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 284 | _LOG_ERROR("PROBE Token with bad signature."); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | } |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 288 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 289 | // create new request instance |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 290 | std::string requestId = std::to_string(random::generateWord64()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 291 | CertificateRequest certRequest(m_config.m_caName, requestId, STATUS_BEFORE_CHALLENGE, *clientCert); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 292 | if (probeToken != nullptr) { |
| 293 | certRequest.setProbeToken(probeToken); |
| 294 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 295 | try { |
| 296 | m_storage->addRequest(certRequest); |
| 297 | } |
| 298 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 299 | _LOG_ERROR("Cannot add new request instance into the storage: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
| 303 | Data result; |
| 304 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 305 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 306 | result.setContent(dataContentFromJson(genNewResponseJson(myEcdhPubKeyBase64, |
| 307 | std::to_string(saltInt), |
| 308 | certRequest, |
| 309 | m_config.m_supportedChallenges))); |
| 310 | m_keyChain.sign(result, signingByIdentity(m_config.m_caName)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 311 | m_face.put(result); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 312 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 313 | if (m_config.m_statusUpdateCallback) { |
| 314 | m_config.m_statusUpdateCallback(certRequest); |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 315 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 319 | CaModule::onChallenge(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 320 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 321 | // get certificate request state |
| 322 | CertificateRequest certRequest = getCertificateRequest(request); |
| 323 | if (certRequest.m_requestId == "") { |
| 324 | // cannot get the request state |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 325 | _LOG_ERROR("Cannot find certificate request state from CA's storage."); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 326 | return; |
| 327 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 328 | // verify signature |
| 329 | if (!security::verifySignature(request, certRequest.m_cert)) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 330 | _LOG_ERROR("Challenge Interest with bad signature."); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 331 | return; |
| 332 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 333 | // decrypt the parameters |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 334 | Buffer paramJsonPayload; |
| 335 | try { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 336 | paramJsonPayload = parseEncBlock(m_aesKey, sizeof(m_aesKey), |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 337 | request.getApplicationParameters()); |
| 338 | } |
| 339 | catch (const std::exception& e) { |
| 340 | _LOG_ERROR("Cannot successfully decrypt the Interest parameters: " << e.what()); |
| 341 | return; |
| 342 | } |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 343 | if (paramJsonPayload.size() == 0) { |
| 344 | _LOG_ERROR("Got an empty buffer from content decryption."); |
| 345 | return; |
| 346 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 347 | std::string paramJsonStr((const char*)paramJsonPayload.data(), paramJsonPayload.size()); |
| 348 | std::istringstream ss(paramJsonStr); |
| 349 | JsonSection paramJson; |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 350 | try { |
| 351 | boost::property_tree::json_parser::read_json(ss, paramJson); |
| 352 | } |
| 353 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 354 | _LOG_ERROR("Cannot read JSON from decrypted content: " << e.what()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 355 | return; |
| 356 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 357 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 358 | // load the corresponding challenge module |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 359 | std::string challengeType = paramJson.get(JSON_CLIENT_SELECTED_CHALLENGE, ""); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 360 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 361 | JsonSection contentJson; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 362 | if (challenge == nullptr) { |
| 363 | _LOG_TRACE("Unrecognized challenge type " << challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 364 | certRequest.m_status = STATUS_FAILURE; |
| 365 | certRequest.m_challengeStatus = CHALLENGE_STATUS_UNKNOWN_CHALLENGE; |
| 366 | contentJson = genChallengeResponseJson(certRequest); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 367 | } |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 368 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 369 | _LOG_TRACE("CHALLENGE module to be load: " << challengeType); |
| 370 | // let challenge module handle the request |
| 371 | challenge->handleChallengeRequest(paramJson, certRequest); |
| 372 | if (certRequest.m_status == STATUS_FAILURE) { |
| 373 | // if challenge failed |
| 374 | m_storage->deleteRequest(certRequest.m_requestId); |
| 375 | contentJson = genChallengeResponseJson(certRequest); |
| 376 | _LOG_TRACE("Challenge failed"); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 377 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 378 | else if (certRequest.m_status == STATUS_PENDING) { |
| 379 | // if challenge succeeded |
| 380 | auto issuedCert = issueCertificate(certRequest); |
| 381 | certRequest.m_cert = issuedCert; |
| 382 | certRequest.m_status = STATUS_SUCCESS; |
| 383 | try { |
| 384 | m_storage->addCertificate(certRequest.m_requestId, issuedCert); |
| 385 | m_storage->deleteRequest(certRequest.m_requestId); |
| 386 | _LOG_TRACE("New Certificate Issued " << issuedCert.getName()); |
| 387 | } |
| 388 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 389 | _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | if (m_config.m_statusUpdateCallback) { |
| 393 | m_config.m_statusUpdateCallback(certRequest); |
| 394 | } |
| 395 | contentJson = genChallengeResponseJson(certRequest); |
| 396 | contentJson.add(JSON_CA_CERT_ID, readString(issuedCert.getName().at(-1))); |
| 397 | _LOG_TRACE("Challenge succeeded. Certificate has been issued"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 398 | } |
| 399 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 400 | try { |
| 401 | m_storage->updateRequest(certRequest); |
| 402 | } |
| 403 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 404 | _LOG_TRACE("Cannot update request instance: " << e.what()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 405 | return; |
| 406 | } |
| 407 | contentJson = genChallengeResponseJson(certRequest); |
| 408 | _LOG_TRACE("No failure no success. Challenge moves on"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 409 | } |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 410 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 411 | |
| 412 | Data result; |
| 413 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 414 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 415 | |
| 416 | // encrypt the content |
| 417 | std::stringstream ss2; |
| 418 | boost::property_tree::write_json(ss2, contentJson); |
| 419 | auto payload = ss2.str(); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 420 | auto contentBlock = genEncBlock(tlv::Content, m_aesKey, sizeof(m_aesKey), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 421 | (const uint8_t*)payload.c_str(), payload.size()); |
| 422 | result.setContent(contentBlock); |
| 423 | m_keyChain.sign(result, signingByIdentity(m_config.m_caName)); |
| 424 | m_face.put(result); |
| 425 | |
| 426 | if (m_config.m_statusUpdateCallback) { |
| 427 | m_config.m_statusUpdateCallback(certRequest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 428 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void |
| 432 | CaModule::onDownload(const Interest& request) |
| 433 | { |
| 434 | auto requestId = readString(request.getName().at(-1)); |
| 435 | security::v2::Certificate signedCert; |
| 436 | try { |
| 437 | signedCert = m_storage->getCertificate(requestId); |
| 438 | } |
| 439 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 440 | _LOG_ERROR("Cannot read signed cert " << requestId << " from CA's storage: " << e.what()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | Data result; |
| 444 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 445 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 446 | result.setContent(signedCert.wireEncode()); |
| 447 | m_keyChain.sign(result, signingByIdentity(m_config.m_caName)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 448 | m_face.put(result); |
| 449 | } |
| 450 | |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 451 | security::v2::Certificate |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 452 | CaModule::issueCertificate(const CertificateRequest& certRequest) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 453 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 454 | auto expectedPeriod = |
| 455 | certRequest.m_cert.getValidityPeriod().getPeriod(); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 456 | security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 457 | security::v2::Certificate newCert; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 458 | |
| 459 | Name certName = certRequest.m_cert.getKeyName(); |
| 460 | certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64())); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 461 | newCert.setName(certName); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 462 | newCert.setContent(certRequest.m_cert.getContent()); |
| 463 | _LOG_TRACE("cert request content " << certRequest.m_cert); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 464 | SignatureInfo signatureInfo; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 465 | signatureInfo.setValidityPeriod(period); |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 466 | security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 467 | m_config.m_caName, signatureInfo); |
| 468 | newCert.setFreshnessPeriod(m_config.m_freshnessPeriod); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 469 | |
| 470 | m_keyChain.sign(newCert, signingInfo); |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 471 | _LOG_TRACE("new cert got signed" << newCert); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 472 | return newCert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | CertificateRequest |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 476 | CaModule::getCertificateRequest(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 477 | { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 478 | std::string requestId; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 479 | CertificateRequest certRequest; |
| 480 | try { |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 481 | requestId = readString(request.getName().at(m_config.m_caName.size() + 2)); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 482 | } |
| 483 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 484 | _LOG_ERROR("Cannot read the request ID out from the request: " << e.what()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 485 | } |
| 486 | try { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 487 | _LOG_TRACE("Request Id to query the database " << requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 488 | certRequest = m_storage->getRequest(requestId); |
| 489 | } |
| 490 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 491 | _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 492 | } |
| 493 | return certRequest; |
| 494 | } |
| 495 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 496 | /** |
| 497 | * @brief Generate JSON file to response PROBE insterest |
| 498 | * |
| 499 | * PROBE response JSON format: |
| 500 | * { |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 501 | * "name": "@p identifier" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 502 | * } |
| 503 | */ |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 504 | JsonSection |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 505 | CaModule::genProbeResponseJson(const Name& identifier, const std::string& m_probe, const JsonSection& parameterJson) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 506 | { |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 507 | std::vector<std::string> fields; |
| 508 | std::string delimiter = ":"; |
| 509 | size_t last = 0; |
| 510 | size_t next = 0; |
| 511 | while ((next = m_probe.find(delimiter, last)) != std::string::npos) { |
| 512 | fields.push_back(m_probe.substr(last, next - last)); |
| 513 | last = next + 1; |
| 514 | } |
| 515 | fields.push_back(m_probe.substr(last)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 516 | JsonSection root; |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 517 | |
| 518 | for (size_t i = 0; i < fields.size(); ++i) { |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 519 | root.put(fields.at(i), parameterJson.get(fields.at(i), "")); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 522 | root.put(JSON_CA_NAME, identifier.toUri()); |
| 523 | return root; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * @brief Generate JSON file to response NEW interest |
| 528 | * |
| 529 | * Target JSON format: |
| 530 | * { |
| 531 | * "ecdh-pub": "@p echdPub", |
| 532 | * "salt": "@p salt" |
| 533 | * "request-id": "@p requestId", |
| 534 | * "status": "@p status", |
| 535 | * "challenges": [ |
| 536 | * { |
| 537 | * "challenge-id": "" |
| 538 | * }, |
| 539 | * { |
| 540 | * "challenge-id": "" |
| 541 | * }, |
| 542 | * ... |
| 543 | * ] |
| 544 | * } |
| 545 | */ |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 546 | JsonSection |
| 547 | CaModule::genInfoResponseJson() |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 548 | { |
| 549 | JsonSection root; |
| 550 | // ca-prefix |
| 551 | Name caName = m_config.m_caName; |
| 552 | root.put("ca-prefix", caName.toUri()); |
| 553 | |
| 554 | // ca-info |
| 555 | const auto& pib = m_keyChain.getPib(); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 556 | const auto& identity = pib.getIdentity(m_config.m_caName); |
| 557 | const auto& cert = identity.getDefaultKey().getDefaultCertificate(); |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 558 | std::string caInfo; |
| 559 | if (m_config.m_caInfo.empty()) { |
| 560 | caInfo = "Issued by " + cert.getSignatureInfo().getKeyLocator().getName().toUri(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 561 | } |
| 562 | else { |
| 563 | caInfo = m_config.m_caInfo; |
| 564 | } |
| 565 | root.put("ca-info", caInfo); |
| 566 | |
| 567 | // probe |
| 568 | root.put("probe", m_config.m_probe); |
| 569 | |
| 570 | // certificate |
| 571 | std::stringstream ss; |
| 572 | io::save(cert, ss); |
| 573 | root.put("certificate", ss.str()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 574 | return root; |
| 575 | } |
| 576 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 577 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 578 | CaModule::genNewResponseJson(const std::string& ecdhKey, const std::string& salt, |
| 579 | const CertificateRequest& request, |
| 580 | const std::list<std::string>& challenges) |
| 581 | { |
| 582 | JsonSection root; |
| 583 | JsonSection challengesSection; |
| 584 | root.put(JSON_CA_ECDH, ecdhKey); |
| 585 | root.put(JSON_CA_SALT, salt); |
Zhiyi Zhang | ff4bcb6 | 2019-09-08 12:57:42 -0700 | [diff] [blame] | 586 | root.put(JSON_CA_REQUEST_ID, request.m_requestId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 587 | root.put(JSON_CA_STATUS, std::to_string(request.m_status)); |
| 588 | |
| 589 | for (const auto& entry : challenges) { |
| 590 | JsonSection challenge; |
| 591 | challenge.put(JSON_CA_CHALLENGE_ID, entry); |
| 592 | challengesSection.push_back(std::make_pair("", challenge)); |
| 593 | } |
| 594 | root.add_child(JSON_CA_CHALLENGES, challengesSection); |
| 595 | return root; |
| 596 | } |
| 597 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame^] | 598 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 599 | CaModule::genChallengeResponseJson(const CertificateRequest& request) |
| 600 | { |
| 601 | JsonSection root; |
| 602 | JsonSection challengesSection; |
| 603 | root.put(JSON_CA_STATUS, request.m_status); |
| 604 | root.put(JSON_CHALLENGE_STATUS, request.m_challengeStatus); |
| 605 | root.put(JSON_CHALLENGE_REMAINING_TRIES, std::to_string(request.m_remainingTries)); |
| 606 | root.put(JSON_CHALLENGE_REMAINING_TIME, std::to_string(request.m_remainingTime)); |
| 607 | return root; |
| 608 | } |
| 609 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 610 | void |
| 611 | CaModule::onRegisterFailed(const std::string& reason) |
| 612 | { |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 613 | _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | Block |
| 617 | CaModule::dataContentFromJson(const JsonSection& jsonSection) |
| 618 | { |
| 619 | std::stringstream ss; |
| 620 | boost::property_tree::write_json(ss, jsonSection); |
| 621 | return makeStringBlock(ndn::tlv::Content, ss.str()); |
| 622 | } |
| 623 | |
| 624 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 625 | CaModule::jsonFromBlock(const Block& block) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 626 | { |
| 627 | std::string jsonString; |
| 628 | try { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 629 | jsonString = encoding::readString(block); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 630 | std::istringstream ss(jsonString); |
| 631 | JsonSection json; |
| 632 | boost::property_tree::json_parser::read_json(ss, json); |
| 633 | return json; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 634 | } |
| 635 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 636 | _LOG_ERROR("Cannot read JSON string from TLV Value: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 637 | return JsonSection(); |
| 638 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | } // namespace ndncert |
| 642 | } // namespace ndn |