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