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