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" |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 27 | #include "protocol-detail/new-renew-revoke.hpp" |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 28 | #include "protocol-detail/probe.hpp" |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/metadata-object.hpp> |
| 30 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 31 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 32 | #include <ndn-cxx/util/io.hpp> |
| 33 | #include <ndn-cxx/util/random.hpp> |
tylerliu | 8d9e712 | 2020-10-06 18:57:06 -0700 | [diff] [blame] | 34 | #include <name-assignments/assignment-funcs.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); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 51 | m_storage = CaStorage::createCaStorage(storageType, m_config.m_caItem.m_caPrefix, ""); |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 52 | random::generateSecureBytes(m_requestIdGenKey, 32); |
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 |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 78 | Name prefix = m_config.m_caItem.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); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 140 | auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caItem.m_caPrefix)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 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(); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 153 | const auto& identity = pib.getIdentity(m_config.m_caItem.m_caPrefix); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 154 | const auto& cert = identity.getDefaultKey().getDefaultCertificate(); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 155 | Block contentTLV = INFO::encodeDataContent(m_config.m_caItem, cert); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 156 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 157 | Name infoPacketName(m_config.m_caItem.m_caPrefix); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 158 | infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0); |
| 159 | Data infoData(infoPacketName); |
| 160 | infoData.setContent(contentTLV); |
| 161 | infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 162 | m_keyChain.sign(infoData, signingByIdentity(m_config.m_caItem.m_caPrefix)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 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 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 187 | // process PROBE requests: collect probe parameters |
| 188 | auto parameters = PROBE::decodeApplicationParameters(request.getApplicationParameters()); |
tylerliu | 8fc0807 | 2020-10-06 15:37:40 -0700 | [diff] [blame] | 189 | std::vector<PartialName> availableComponents; |
tylerliu | 355370e | 2020-10-07 14:56:33 -0700 | [diff] [blame^] | 190 | if (m_config.m_nameAssignmentFunc) { |
| 191 | try { |
| 192 | availableComponents = m_config.m_nameAssignmentFunc(parameters); |
| 193 | } |
| 194 | catch (const std::exception &e) { |
| 195 | _LOG_TRACE("Cannot parse probe parameters: " << e.what()); |
| 196 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 197 | "Cannot parse probe parameters: " + std::string(e.what()))); |
| 198 | return; |
| 199 | } |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 200 | } |
| 201 | std::vector<Name> availableNames; |
| 202 | for (const auto& component : availableComponents) { |
| 203 | Name newIdentityName = m_config.m_caItem.m_caPrefix; |
| 204 | newIdentityName.append(component); |
| 205 | availableNames.push_back(newIdentityName); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 206 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 207 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 208 | Data result; |
| 209 | result.setName(request.getName()); |
Suyong Won | e2afeb5 | 2020-10-04 03:05:39 +0900 | [diff] [blame] | 210 | result.setContent(PROBE::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength, m_config.m_redirection)); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 211 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
| 212 | m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix)); |
| 213 | m_face.put(result); |
| 214 | _LOG_TRACE("Handle PROBE: send out the PROBE response"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 218 | CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 219 | { |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 220 | // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest] |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 221 | // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest] |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 222 | // get ECDH pub key and cert request |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 223 | const auto& parameterTLV = request.getApplicationParameters(); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 224 | std::string ecdhPub; |
| 225 | shared_ptr<security::v2::Certificate> clientCert; |
| 226 | try { |
| 227 | NEW_RENEW_REVOKE::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert); |
| 228 | } catch (const std::exception& e) { |
| 229 | if (!parameterTLV.hasValue()) { |
| 230 | _LOG_ERROR("Empty TLV obtained from the Interest parameter."); |
| 231 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 232 | "Empty TLV obtained from the Interest parameter.")); |
| 233 | return; |
| 234 | } |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 235 | |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 236 | _LOG_ERROR("Unrecognized self-signed certificate: " << e.what()); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 237 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 238 | "Unrecognized self-signed certificate.")); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 239 | return; |
| 240 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 241 | |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 242 | if (ecdhPub == "") { |
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 |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 250 | ECDHState ecdh; |
| 251 | auto myEcdhPubKeyBase64 = ecdh.getBase64PubKey(); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 252 | try { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 253 | ecdh.deriveSecret(ecdhPub); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 254 | } |
| 255 | catch (const std::exception& e) { |
| 256 | _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] | 257 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 258 | "Cannot derive a shared secret using the provided ECDH key.")); |
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 | // generate salt for HKDF |
| 262 | auto saltInt = random::generateSecureWord64(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 263 | // hkdf |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 264 | uint8_t aesKey[AES_128_KEY_LEN]; |
| 265 | hkdf(ecdh.context->sharedSecret, ecdh.context->sharedSecretLen, |
| 266 | (uint8_t*)&saltInt, sizeof(saltInt), aesKey, sizeof(aesKey)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 267 | |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 268 | // verify identity name |
| 269 | if (!m_config.m_caItem.m_caPrefix.isPrefixOf(clientCert->getIdentity()) |
| 270 | || !security::v2::Certificate::isValidName(clientCert->getName()) |
| 271 | || clientCert->getIdentity().size() <= m_config.m_caItem.m_caPrefix.size()) { |
| 272 | _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName()); |
| 273 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED, |
| 274 | "An invalid certificate name is being requested.")); |
| 275 | return; |
| 276 | } |
| 277 | if (m_config.m_caItem.m_maxSuffixLength) { |
| 278 | if (clientCert->getIdentity().size() > m_config.m_caItem.m_caPrefix.size() + *m_config.m_caItem.m_maxSuffixLength) { |
| 279 | _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName()); |
| 280 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED, |
| 281 | "An invalid certificate name is being requested.")); |
| 282 | return; |
| 283 | } |
| 284 | } |
| 285 | |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 286 | if (requestType == RequestType::NEW) { |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 287 | // check the validity period |
| 288 | auto expectedPeriod = clientCert->getValidityPeriod().getPeriod(); |
| 289 | auto currentTime = time::system_clock::now(); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 290 | if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD || |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 291 | expectedPeriod.second > currentTime + m_config.m_caItem.m_maxValidityPeriod || |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 292 | expectedPeriod.second <= expectedPeriod.first) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 293 | _LOG_ERROR("An invalid validity period is being requested."); |
| 294 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD, |
| 295 | "An invalid validity period is being requested.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 298 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 299 | // verify signature |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 300 | if (!security::verifySignature(*clientCert, *clientCert)) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 301 | _LOG_ERROR("Invalid signature in the self-signed certificate."); |
| 302 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 303 | "Invalid signature in the self-signed certificate.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | if (!security::verifySignature(request, *clientCert)) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 307 | _LOG_ERROR("Invalid signature in the Interest packet."); |
| 308 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 309 | "Invalid signature in the Interest packet.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 310 | return; |
| 311 | } |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 312 | } |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 313 | else if (requestType == RequestType::REVOKE) { |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 314 | //verify cert is from this CA |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 315 | const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caItem.m_caPrefix).getDefaultKey().getDefaultCertificate(); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 316 | if (!security::verifySignature(*clientCert, cert)) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 317 | _LOG_ERROR("Invalid signature in the certificate to revoke."); |
| 318 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 319 | "Invalid signature in the certificate to revoke.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 320 | return; |
| 321 | } |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 324 | // create new request instance |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 325 | uint8_t requestIdData[32]; |
| 326 | Block certNameTlv = clientCert->getName().wireEncode(); |
| 327 | ndn_compute_hmac_sha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData); |
| 328 | CaState requestState(m_config.m_caItem.m_caPrefix, hexlify(requestIdData, 32), |
| 329 | requestType, Status::BEFORE_CHALLENGE, *clientCert, |
| 330 | makeBinaryBlock(tlv::ContentType_Key, aesKey, sizeof(aesKey))); |
Zhiyi Zhang | 21d52b5 | 2020-10-05 18:29:15 -0700 | [diff] [blame] | 331 | try { |
| 332 | m_storage->addRequest(requestState); |
| 333 | } |
| 334 | catch (const std::runtime_error& e) { |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 335 | _LOG_ERROR("Duplicate Request ID: The same request has been seen before."); |
| 336 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 337 | "Duplicate Request ID: The same request has been seen before..")); |
| 338 | return; |
Zhiyi Zhang | 21d52b5 | 2020-10-05 18:29:15 -0700 | [diff] [blame] | 339 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 340 | Data result; |
| 341 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 342 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 343 | result.setContent(NEW_RENEW_REVOKE::encodeDataContent(myEcdhPubKeyBase64, |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 344 | std::to_string(saltInt), |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 345 | requestState, |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 346 | m_config.m_caItem.m_supportedChallenges)); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 347 | m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 348 | m_face.put(result); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 349 | if (m_config.m_statusUpdateCallback) { |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 350 | m_config.m_statusUpdateCallback(requestState); |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 351 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 355 | CaModule::onChallenge(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 356 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 357 | // get certificate request state |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 358 | CaState requestState = getCertificateRequest(request); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 359 | if (requestState.m_requestId == "") { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 360 | _LOG_ERROR("No certificate request state can be found."); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 361 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 362 | "No certificate request state can be found.")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 363 | return; |
| 364 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 365 | // verify signature |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 366 | if (!security::verifySignature(request, requestState.m_cert)) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 367 | _LOG_ERROR("Invalid Signature in the Interest packet."); |
| 368 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 369 | "Invalid Signature in the Interest packet.")); |
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 | // decrypt the parameters |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 373 | Buffer paramTLVPayload; |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 374 | try { |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 375 | paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), requestState.m_encryptionKey.value(), |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 376 | (uint8_t*)"test", strlen("test")); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 377 | } |
| 378 | catch (const std::exception& e) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 379 | _LOG_ERROR("Interest paramaters decryption failed: " << e.what()); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 380 | m_storage->deleteRequest(requestState.m_requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 381 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 382 | "Interest paramaters decryption failed.")); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 385 | if (paramTLVPayload.size() == 0) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 386 | _LOG_ERROR("No parameters are found after decryption."); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 387 | m_storage->deleteRequest(requestState.m_requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 388 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 389 | "No parameters are found after decryption.")); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 392 | Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size()); |
| 393 | paramTLV.parse(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 394 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 395 | // load the corresponding challenge module |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 396 | std::string challengeType = readString(paramTLV.get(tlv_selected_challenge)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 397 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
| 398 | if (challenge == nullptr) { |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 399 | _LOG_TRACE("Unrecognized challenge type: " << challengeType); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 400 | m_storage->deleteRequest(requestState.m_requestId); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 401 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type.")); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 402 | return; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 403 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 404 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 405 | _LOG_TRACE("CHALLENGE module to be load: " << challengeType); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 406 | auto errorInfo = challenge->handleChallengeRequest(paramTLV, requestState); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 407 | if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) { |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 408 | m_storage->deleteRequest(requestState.m_requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 409 | m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo))); |
| 410 | return; |
| 411 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 412 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 413 | Block payload; |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 414 | if (requestState.m_status == Status::PENDING) { |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 415 | // if challenge succeeded |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 416 | if (requestState.m_requestType == RequestType::NEW) { |
| 417 | auto issuedCert = issueCertificate(requestState); |
| 418 | requestState.m_cert = issuedCert; |
| 419 | requestState.m_status = Status::SUCCESS; |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 420 | m_storage->deleteRequest(requestState.m_requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 421 | |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 422 | payload = CHALLENGE::encodeDataPayload(requestState); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 423 | payload.parse(); |
| 424 | payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName())); |
| 425 | payload.encode(); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 426 | _LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName()); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 427 | } |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 428 | else if (requestState.m_requestType == RequestType::REVOKE) { |
| 429 | requestState.m_status = Status::SUCCESS; |
| 430 | m_storage->deleteRequest(requestState.m_requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 431 | |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 432 | payload = CHALLENGE::encodeDataPayload(requestState); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 433 | _LOG_TRACE("Challenge succeeded. Certificate has been revoked"); |
| 434 | } |
| 435 | } |
| 436 | else { |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 437 | m_storage->updateRequest(requestState); |
| 438 | payload = CHALLENGE::encodeDataPayload(requestState); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 439 | _LOG_TRACE("No failure no success. Challenge moves on"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 440 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 441 | |
| 442 | Data result; |
| 443 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 444 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 445 | auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, requestState.m_encryptionKey.value(), payload.value(), |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 446 | payload.value_size(), (uint8_t*)"test", strlen("test")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 447 | result.setContent(contentBlock); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 448 | m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 449 | m_face.put(result); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 450 | if (m_config.m_statusUpdateCallback) { |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 451 | m_config.m_statusUpdateCallback(requestState); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 452 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 455 | security::v2::Certificate |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 456 | CaModule::issueCertificate(const CaState& requestState) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 457 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 458 | auto expectedPeriod = |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 459 | requestState.m_cert.getValidityPeriod().getPeriod(); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 460 | security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 461 | security::v2::Certificate newCert; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 462 | |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 463 | Name certName = requestState.m_cert.getKeyName(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 464 | certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64())); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 465 | newCert.setName(certName); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 466 | newCert.setContent(requestState.m_cert.getContent()); |
| 467 | _LOG_TRACE("cert request content " << requestState.m_cert); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 468 | SignatureInfo signatureInfo; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 469 | signatureInfo.setValidityPeriod(period); |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 470 | security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID, |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 471 | m_config.m_caItem.m_caPrefix, signatureInfo); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 472 | |
| 473 | m_keyChain.sign(newCert, signingInfo); |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 474 | _LOG_TRACE("new cert got signed" << newCert); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 475 | return newCert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 476 | } |
| 477 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 478 | CaState |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 479 | CaModule::getCertificateRequest(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 480 | { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 481 | std::string requestId; |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 482 | CaState requestState; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 483 | try { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 484 | requestId = readString(request.getName().at(m_config.m_caItem.m_caPrefix.size() + 2)); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 485 | } |
| 486 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 487 | _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] | 488 | } |
| 489 | try { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 490 | _LOG_TRACE("Request Id to query the database " << requestId); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 491 | requestState = m_storage->getRequest(requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 492 | } |
| 493 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 494 | _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 495 | } |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 496 | return requestState; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void |
| 500 | CaModule::onRegisterFailed(const std::string& reason) |
| 501 | { |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 502 | _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] | 503 | } |
| 504 | |
| 505 | Block |
| 506 | CaModule::dataContentFromJson(const JsonSection& jsonSection) |
| 507 | { |
| 508 | std::stringstream ss; |
| 509 | boost::property_tree::write_json(ss, jsonSection); |
| 510 | return makeStringBlock(ndn::tlv::Content, ss.str()); |
| 511 | } |
| 512 | |
| 513 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 514 | CaModule::jsonFromBlock(const Block& block) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 515 | { |
| 516 | std::string jsonString; |
| 517 | try { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 518 | jsonString = encoding::readString(block); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 519 | std::istringstream ss(jsonString); |
| 520 | JsonSection json; |
| 521 | boost::property_tree::json_parser::read_json(ss, json); |
| 522 | return json; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 523 | } |
| 524 | catch (const std::exception& e) { |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 525 | _LOG_ERROR("Cannot read JSON string from TLV Value: " << e.what()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 526 | return JsonSection(); |
| 527 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 530 | Data |
| 531 | CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo) |
| 532 | { |
| 533 | Data result; |
| 534 | result.setName(name); |
| 535 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
| 536 | result.setContent(ErrorTLV::encodeDataContent(error, errorInfo)); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 537 | m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix)); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 538 | return result; |
| 539 | } |
| 540 | |
| 541 | } // namespace ndncert |
| 542 | } // namespace ndn |