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 | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 25 | #include "protocol-detail/info.hpp" |
| 26 | #include "protocol-detail/probe.hpp" |
| 27 | #include "protocol-detail/new.hpp" |
| 28 | #include "protocol-detail/challenge.hpp" |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 29 | #include "protocol-detail/revoke.hpp" |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 30 | #include <ndn-cxx/util/io.hpp> |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 31 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 32 | #include <ndn-cxx/security/signing-helpers.hpp> |
Junxiao Shi | 7c06803 | 2017-05-28 13:40:47 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/metadata-object.hpp> |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace ndncert { |
| 38 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 39 | static const int IS_SUBNAME_MIN_OFFSET = 5; |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 40 | static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s; |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 41 | 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] | 42 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 43 | _LOG_INIT(ndncert.ca); |
| 44 | |
| 45 | CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain, |
| 46 | const std::string& configPath, const std::string& storageType) |
| 47 | : m_face(face) |
| 48 | , m_keyChain(keyChain) |
| 49 | { |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 50 | // load the config and create storage |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 51 | m_config.load(configPath); |
| 52 | m_storage = CaStorage::createCaStorage(storageType); |
| 53 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 54 | registerPrefix(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | CaModule::~CaModule() |
| 58 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | for (auto handle : m_interestFilterHandles) { |
| 60 | handle.cancel(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 61 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | for (auto handle : m_registeredPrefixHandles) { |
| 63 | handle.unregister(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | void |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 68 | CaModule::registerPrefix() |
| 69 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 70 | // register localhop discovery prefix |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 71 | Name localhopInfoPrefix("/localhop/CA/INFO"); |
| 72 | auto prefixId = m_face.setInterestFilter(InterestFilter(localhopInfoPrefix), |
| 73 | bind(&CaModule::onInfo, this, _2), |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 74 | bind(&CaModule::onRegisterFailed, this, _2)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | m_registeredPrefixHandles.push_back(prefixId); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 76 | _LOG_TRACE("Prefix " << localhopInfoPrefix << " got registered"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 77 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 78 | // register prefixes |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 79 | Name prefix = m_config.m_caPrefix; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 80 | prefix.append("CA"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 81 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 82 | prefixId = m_face.registerPrefix(prefix, |
| 83 | [&] (const Name& name) { |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 84 | // register INFO prefix |
| 85 | auto filterId = m_face.setInterestFilter(Name(name).append("INFO"), |
| 86 | bind(&CaModule::onInfo, this, _2)); |
| 87 | m_interestFilterHandles.push_back(filterId); |
| 88 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | // register PROBE prefix |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 90 | filterId = m_face.setInterestFilter(Name(name).append("PROBE"), |
| 91 | bind(&CaModule::onProbe, this, _2)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 92 | m_interestFilterHandles.push_back(filterId); |
| 93 | |
| 94 | // register NEW prefix |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 95 | filterId = m_face.setInterestFilter(Name(name).append("NEW"), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | bind(&CaModule::onNew, this, _2)); |
| 97 | m_interestFilterHandles.push_back(filterId); |
| 98 | |
| 99 | // register SELECT prefix |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 100 | filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 101 | bind(&CaModule::onChallenge, this, _2)); |
| 102 | m_interestFilterHandles.push_back(filterId); |
| 103 | |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 104 | // register REVOKE prefix |
| 105 | filterId = m_face.setInterestFilter(Name(name).append("REVOKE"), |
| 106 | bind(&CaModule::onRevoke, this, _2)); |
| 107 | m_interestFilterHandles.push_back(filterId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 108 | _LOG_TRACE("Prefix " << name << " got registered"); |
| 109 | }, |
| 110 | bind(&CaModule::onRegisterFailed, this, _2)); |
| 111 | m_registeredPrefixHandles.push_back(prefixId); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 112 | } |
| 113 | |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 114 | void |
| 115 | CaModule::setNameAssignmentFunction(const NameAssignmentFunc& handler) |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 116 | { |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 117 | m_config.m_nameAssignmentFunc = handler; |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 120 | bool |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 121 | CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback) |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 122 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | m_config.m_statusUpdateCallback = onUpdateCallback; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 124 | return false; |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 127 | shared_ptr<Data> |
| 128 | CaModule::generateCaConfigMetaData() |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 129 | { |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 130 | // @TODO |
| 131 | // make metadata a class member variable m_infoMetadata |
| 132 | // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one |
| 133 | // otherwise, directly reply m_infoMetadata.makeData |
| 134 | |
| 135 | auto infoPacket = generateCaConfigData(); |
| 136 | MetadataObject metadata; |
| 137 | metadata.setVersionedName(infoPacket->getName().getPrefix(-1)); |
| 138 | Name discoveryInterestName(infoPacket->getName().getPrefix(-2)); |
| 139 | name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata")); |
| 140 | discoveryInterestName.append(metadataComponent); |
| 141 | auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caPrefix)); |
| 142 | return make_shared<Data>(metadataData); |
| 143 | } |
| 144 | |
| 145 | shared_ptr<Data> |
| 146 | CaModule::generateCaConfigData() |
| 147 | { |
| 148 | // @TODO |
| 149 | // make CaInfo Data packet a class member variable m_infoData |
| 150 | // check whether the m_infoData is still valid, if not, then generate a new one |
| 151 | // otherwise, directly reply m_infoData |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 152 | |
| 153 | const auto& pib = m_keyChain.getPib(); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 154 | const auto& identity = pib.getIdentity(m_config.m_caPrefix); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 155 | const auto& cert = identity.getDefaultKey().getDefaultCertificate(); |
| 156 | Block contentTLV = INFO::encodeContentFromCAConfig(m_config, cert); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 157 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 158 | Name infoPacketName(m_config.m_caPrefix); |
| 159 | infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0); |
| 160 | Data infoData(infoPacketName); |
| 161 | infoData.setContent(contentTLV); |
| 162 | infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
| 163 | m_keyChain.sign(infoData, signingByIdentity(m_config.m_caPrefix)); |
| 164 | return make_shared<Data>(infoData); |
| 165 | } |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 166 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 167 | void |
| 168 | CaModule::onInfo(const Interest& request) |
| 169 | { |
| 170 | _LOG_TRACE("Received INFO request"); |
| 171 | |
| 172 | if (request.getName().get(-1).type() == 32) { |
| 173 | m_face.put(*generateCaConfigMetaData()); |
| 174 | } |
| 175 | else { |
| 176 | m_face.put(*generateCaConfigData()); |
| 177 | } |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 178 | |
| 179 | _LOG_TRACE("Handle INFO: send out the INFO response"); |
| 180 | } |
| 181 | |
| 182 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 183 | CaModule::onProbe(const Interest& request) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 184 | { |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 185 | // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent] |
| 186 | _LOG_TRACE("Received PROBE request"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 187 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 188 | // process PROBE requests: find an available name |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 189 | std::string availableId = ""; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 190 | const auto& parameterTLV = request.getApplicationParameters(); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 191 | parameterTLV.parse(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 192 | if (!parameterTLV.hasValue()) { |
| 193 | _LOG_ERROR("Empty TLV obtained from the Interest parameter."); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 194 | return; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 195 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 196 | |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 197 | // if (m_config.m_nameAssignmentFunc) { |
| 198 | // try { |
| 199 | // availableId = m_config.m_nameAssignmentFunc(parameterTLV); |
| 200 | // } |
| 201 | // catch (const std::exception& e) { |
| 202 | // _LOG_TRACE("Cannot find PROBE input from PROBE parameters: " << e.what()); |
| 203 | // return; |
| 204 | // } |
| 205 | // } |
| 206 | // else { |
| 207 | // // if there is no app-specified name lookup, use a random name id |
| 208 | // availableId = std::to_string(random::generateSecureWord64()); |
| 209 | // } |
| 210 | // Name newIdentityName = m_config.m_caPrefix; |
| 211 | // newIdentityName.append(availableId); |
| 212 | // _LOG_TRACE("Handle PROBE: generate an identity " << newIdentityName); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 213 | |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 214 | // Block contentTLV = PROBE::encodeDataContent(newIdentityName.toUri(), m_config.m_probe, parameterTLV); |
| 215 | |
| 216 | // Data result; |
| 217 | // result.setName(request.getName()); |
| 218 | // result.setContent(contentTLV); |
| 219 | // result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
| 220 | // m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix)); |
| 221 | // m_face.put(result); |
| 222 | // _LOG_TRACE("Handle PROBE: send out the PROBE response"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 226 | CaModule::onNew(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 227 | { |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 228 | // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest] |
| 229 | onRequestInit(request, REQUEST_TYPE_NEW); |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | CaModule::onRevoke(const Interest& request) |
| 234 | { |
| 235 | // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest] |
| 236 | onRequestInit(request, REQUEST_TYPE_REVOKE); |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | CaModule::onRequestInit(const Interest& request, int requestType) |
| 241 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 242 | // get ECDH pub key and cert request |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 243 | const auto& parameterTLV = request.getApplicationParameters(); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 244 | parameterTLV.parse(); |
| 245 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 246 | if (!parameterTLV.hasValue()) { |
| 247 | _LOG_ERROR("Empty TLV obtained from the Interest parameter."); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 248 | return; |
| 249 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 250 | |
| 251 | std::string peerKeyBase64 = readString(parameterTLV.get(tlv_ecdh_pub)); |
| 252 | |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 253 | if (peerKeyBase64 == "") { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 254 | _LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter."); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 255 | return; |
| 256 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 257 | |
| 258 | // get server's ECDH pub key |
| 259 | auto myEcdhPubKeyBase64 = m_ecdh.getBase64PubKey(); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 260 | try { |
| 261 | m_ecdh.deriveSecret(peerKeyBase64); |
| 262 | } |
| 263 | catch (const std::exception& e) { |
| 264 | _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what()); |
| 265 | return; |
| 266 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 267 | // generate salt for HKDF |
| 268 | auto saltInt = random::generateSecureWord64(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 269 | // hkdf |
| 270 | hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen, |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 271 | (uint8_t*)&saltInt, sizeof(saltInt), m_aesKey, sizeof(m_aesKey)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 272 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 273 | shared_ptr<security::v2::Certificate> clientCert = nullptr; |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 274 | |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 275 | if (requestType == REQUEST_TYPE_NEW) { |
| 276 | // parse certificate request |
| 277 | Block cert_req = parameterTLV.get(tlv_cert_request); |
| 278 | cert_req.parse(); |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 279 | |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 280 | try { |
| 281 | security::v2::Certificate cert = security::v2::Certificate(cert_req.get(tlv::Data)); |
| 282 | clientCert = make_shared<security::v2::Certificate>(cert); |
| 283 | } |
| 284 | catch (const std::exception &e) { |
| 285 | _LOG_ERROR("Unrecognized certificate request: " << e.what()); |
| 286 | return; |
| 287 | } |
| 288 | // check the validity period |
| 289 | auto expectedPeriod = clientCert->getValidityPeriod().getPeriod(); |
| 290 | auto currentTime = time::system_clock::now(); |
| 291 | if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD) { |
| 292 | _LOG_ERROR("Client requests a too old notBefore timepoint."); |
| 293 | return; |
| 294 | } |
| 295 | if (expectedPeriod.second > currentTime + m_config.m_maxValidityPeriod || |
| 296 | expectedPeriod.second <= expectedPeriod.first) { |
| 297 | _LOG_ERROR("Client requests an invalid validity period or a notAfter timepoint beyond the allowed time period."); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | // verify the self-signed certificate, the request, and the token |
| 302 | if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName()) // under ca prefix |
| 303 | || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 304 | || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET) { |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 305 | _LOG_ERROR("Invalid self-signed certificate name " << clientCert->getName()); |
| 306 | return; |
| 307 | } |
| 308 | if (!security::verifySignature(*clientCert, *clientCert)) { |
| 309 | _LOG_ERROR("Cert request with bad signature."); |
| 310 | return; |
| 311 | } |
| 312 | if (!security::verifySignature(request, *clientCert)) { |
| 313 | _LOG_ERROR("Interest with bad signature."); |
| 314 | return; |
| 315 | } |
| 316 | } else if (requestType == REQUEST_TYPE_REVOKE) { |
| 317 | // parse certificate request |
| 318 | Block cert_revoke = parameterTLV.get(tlv_cert_to_revoke); |
| 319 | cert_revoke.parse(); |
| 320 | |
| 321 | try { |
| 322 | security::v2::Certificate cert = security::v2::Certificate(cert_revoke.get(tlv::Data)); |
| 323 | clientCert = make_shared<security::v2::Certificate>(cert); |
| 324 | } |
| 325 | catch (const std::exception &e) { |
| 326 | _LOG_ERROR("Unrecognized certificate: " << e.what()); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | // verify the certificate |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 331 | if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName()) // under ca prefix |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 332 | || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 333 | || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET) { |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 334 | _LOG_ERROR("Invalid certificate name " << clientCert->getName()); |
| 335 | return; |
| 336 | } |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame^] | 337 | const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caPrefix).getDefaultKey().getDefaultCertificate(); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 338 | if (!security::verifySignature(*clientCert, cert)) { |
| 339 | _LOG_ERROR("Cert request with bad signature."); |
| 340 | return; |
| 341 | } |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 344 | // create new request instance |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 345 | std::string requestId = std::to_string(random::generateWord64()); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 346 | CertificateRequest certRequest(m_config.m_caPrefix, requestId, requestType, STATUS_BEFORE_CHALLENGE, *clientCert); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 347 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 348 | try { |
| 349 | m_storage->addRequest(certRequest); |
| 350 | } |
| 351 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 352 | _LOG_ERROR("Cannot add new request instance into the storage: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | Data result; |
| 357 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 358 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 359 | if (requestType == REQUEST_TYPE_NEW) { |
| 360 | result.setContent(NEW::encodeDataContent(myEcdhPubKeyBase64, |
| 361 | std::to_string(saltInt), |
| 362 | certRequest, |
| 363 | m_config.m_supportedChallenges)); |
| 364 | } else if (requestType == REQUEST_TYPE_REVOKE) { |
| 365 | result.setContent(REVOKE::encodeDataContent(myEcdhPubKeyBase64, |
| 366 | std::to_string(saltInt), |
| 367 | certRequest, |
| 368 | m_config.m_supportedChallenges)); |
| 369 | } |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 370 | m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 371 | m_face.put(result); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 372 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 373 | if (m_config.m_statusUpdateCallback) { |
| 374 | m_config.m_statusUpdateCallback(certRequest); |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 375 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 379 | CaModule::onChallenge(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 380 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 381 | // get certificate request state |
| 382 | CertificateRequest certRequest = getCertificateRequest(request); |
| 383 | if (certRequest.m_requestId == "") { |
| 384 | // cannot get the request state |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 385 | _LOG_ERROR("Cannot find certificate request state from CA's storage."); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 386 | return; |
| 387 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 388 | // verify signature |
| 389 | if (!security::verifySignature(request, certRequest.m_cert)) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 390 | _LOG_ERROR("Challenge Interest with bad signature."); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 391 | return; |
| 392 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 393 | // decrypt the parameters |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 394 | Buffer paramTLVPayload; |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 395 | try { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 396 | paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), m_aesKey, |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 397 | (uint8_t*)"test", strlen("test")); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 398 | } |
| 399 | catch (const std::exception& e) { |
| 400 | _LOG_ERROR("Cannot successfully decrypt the Interest parameters: " << e.what()); |
| 401 | return; |
| 402 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 403 | if (paramTLVPayload.size() == 0) { |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 404 | _LOG_ERROR("Got an empty buffer from content decryption."); |
| 405 | return; |
| 406 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 407 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 408 | Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size()); |
| 409 | paramTLV.parse(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 410 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 411 | // load the corresponding challenge module |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 412 | std::string challengeType = readString(paramTLV.get(tlv_selected_challenge)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 413 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 414 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 415 | Block payload; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 416 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 417 | if (challenge == nullptr) { |
| 418 | _LOG_TRACE("Unrecognized challenge type " << challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 419 | certRequest.m_status = STATUS_FAILURE; |
| 420 | certRequest.m_challengeStatus = CHALLENGE_STATUS_UNKNOWN_CHALLENGE; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 421 | payload = CHALLENGE::encodeDataPayload(certRequest); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 422 | } |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 423 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 424 | _LOG_TRACE("CHALLENGE module to be load: " << challengeType); |
| 425 | // let challenge module handle the request |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 426 | challenge->handleChallengeRequest(paramTLV, certRequest); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 427 | if (certRequest.m_status == STATUS_FAILURE) { |
| 428 | // if challenge failed |
| 429 | m_storage->deleteRequest(certRequest.m_requestId); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 430 | payload = CHALLENGE::encodeDataPayload(certRequest); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 431 | _LOG_TRACE("Challenge failed"); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 432 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 433 | else if (certRequest.m_status == STATUS_PENDING) { |
| 434 | // if challenge succeeded |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 435 | if (certRequest.m_requestType == REQUEST_TYPE_NEW) { |
| 436 | auto issuedCert = issueCertificate(certRequest); |
| 437 | certRequest.m_cert = issuedCert; |
| 438 | certRequest.m_status = STATUS_SUCCESS; |
| 439 | try { |
| 440 | m_storage->addCertificate(certRequest.m_requestId, issuedCert); |
| 441 | m_storage->deleteRequest(certRequest.m_requestId); |
| 442 | _LOG_TRACE("New Certificate Issued " << issuedCert.getName()); |
| 443 | } |
| 444 | catch (const std::exception& e) { |
| 445 | _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what()); |
| 446 | return; |
| 447 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 448 | |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 449 | payload = CHALLENGE::encodeDataPayload(certRequest); |
| 450 | payload.parse(); |
| 451 | payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName())); |
| 452 | payload.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 453 | |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 454 | //contentJson.add(JSON_CA_CERT_ID, readString(issuedCert.getName().at(-1))); |
| 455 | _LOG_TRACE("Challenge succeeded. Certificate has been issued"); |
| 456 | } |
| 457 | else if (certRequest.m_requestType == REQUEST_TYPE_REVOKE) { |
| 458 | certRequest.m_status = STATUS_SUCCESS; |
| 459 | try { |
| 460 | m_storage->deleteRequest(certRequest.m_requestId); |
| 461 | _LOG_TRACE("Certificate Revoked"); |
| 462 | } |
| 463 | catch (const std::exception& e) { |
| 464 | _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what()); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | payload = CHALLENGE::encodeDataPayload(certRequest); |
| 469 | payload.parse(); |
| 470 | _LOG_TRACE("Challenge succeeded. Certificate has been revoked"); |
| 471 | } |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 472 | } |
| 473 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 474 | try { |
| 475 | m_storage->updateRequest(certRequest); |
| 476 | } |
| 477 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 478 | _LOG_TRACE("Cannot update request instance: " << e.what()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 479 | return; |
| 480 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 481 | payload = CHALLENGE::encodeDataPayload(certRequest); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 482 | _LOG_TRACE("No failure no success. Challenge moves on"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 483 | } |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 484 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 485 | |
| 486 | Data result; |
| 487 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 488 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 489 | |
| 490 | // encrypt the content |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 491 | auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, m_aesKey, payload.value(), |
| 492 | payload.value_size(), (uint8_t*)"test", strlen("test")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 493 | result.setContent(contentBlock); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 494 | m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 495 | m_face.put(result); |
| 496 | |
| 497 | if (m_config.m_statusUpdateCallback) { |
| 498 | m_config.m_statusUpdateCallback(certRequest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 499 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 502 | security::v2::Certificate |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 503 | CaModule::issueCertificate(const CertificateRequest& certRequest) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 504 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 505 | auto expectedPeriod = |
| 506 | certRequest.m_cert.getValidityPeriod().getPeriod(); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 507 | security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 508 | security::v2::Certificate newCert; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 509 | |
| 510 | Name certName = certRequest.m_cert.getKeyName(); |
| 511 | certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64())); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 512 | newCert.setName(certName); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 513 | newCert.setContent(certRequest.m_cert.getContent()); |
| 514 | _LOG_TRACE("cert request content " << certRequest.m_cert); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 515 | SignatureInfo signatureInfo; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 516 | signatureInfo.setValidityPeriod(period); |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 517 | security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID, |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 518 | m_config.m_caPrefix, signatureInfo); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 519 | |
| 520 | m_keyChain.sign(newCert, signingInfo); |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 521 | _LOG_TRACE("new cert got signed" << newCert); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 522 | return newCert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | CertificateRequest |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 526 | CaModule::getCertificateRequest(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 527 | { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 528 | std::string requestId; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 529 | CertificateRequest certRequest; |
| 530 | try { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 531 | requestId = readString(request.getName().at(m_config.m_caPrefix.size() + 2)); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 532 | } |
| 533 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 534 | _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] | 535 | } |
| 536 | try { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 537 | _LOG_TRACE("Request Id to query the database " << requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 538 | certRequest = m_storage->getRequest(requestId); |
| 539 | } |
| 540 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 541 | _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 542 | } |
| 543 | return certRequest; |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | CaModule::onRegisterFailed(const std::string& reason) |
| 548 | { |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 549 | _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] | 550 | } |
| 551 | |
| 552 | Block |
| 553 | CaModule::dataContentFromJson(const JsonSection& jsonSection) |
| 554 | { |
| 555 | std::stringstream ss; |
| 556 | boost::property_tree::write_json(ss, jsonSection); |
| 557 | return makeStringBlock(ndn::tlv::Content, ss.str()); |
| 558 | } |
| 559 | |
| 560 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 561 | CaModule::jsonFromBlock(const Block& block) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 562 | { |
| 563 | std::string jsonString; |
| 564 | try { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 565 | jsonString = encoding::readString(block); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 566 | std::istringstream ss(jsonString); |
| 567 | JsonSection json; |
| 568 | boost::property_tree::json_parser::read_json(ss, json); |
| 569 | return json; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 570 | } |
| 571 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 572 | _LOG_ERROR("Cannot read JSON string from TLV Value: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 573 | return JsonSection(); |
| 574 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | } // namespace ndncert |
| 578 | } // namespace ndn |