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