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