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