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