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