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 | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 22 | #include "detail/crypto-helpers.hpp" |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 23 | #include "challenge/challenge-module.hpp" |
Zhiyi Zhang | c2c4ed2 | 2020-10-14 17:16:28 -0700 | [diff] [blame] | 24 | #include "name-assignment/assignment-func.hpp" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 25 | #include "detail/challenge-encoder.hpp" |
| 26 | #include "detail/error-encoder.hpp" |
| 27 | #include "detail/info-encoder.hpp" |
| 28 | #include "detail/new-renew-revoke-encoder.hpp" |
| 29 | #include "detail/probe-encoder.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 | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/util/string-helper.hpp> |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace ndncert { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 39 | namespace ca { |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 40 | |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 41 | static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s; |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 42 | 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] | 43 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 44 | NDN_LOG_INIT(ndncert.ca); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 45 | |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 46 | CaModule::CaModule(Face& face, security::KeyChain& keyChain, |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 47 | const std::string& configPath, const std::string& storageType) |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 48 | : m_face(face) |
| 49 | , m_keyChain(keyChain) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 50 | { |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 51 | // load the config and create storage |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 52 | m_config.load(configPath); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 53 | m_storage = CaStorage::createCaStorage(storageType, m_config.caProfile.caPrefix, ""); |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 54 | random::generateSecureBytes(m_requestIdGenKey, 32); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 55 | if (m_config.nameAssignmentFuncs.size() == 0) { |
| 56 | m_config.nameAssignmentFuncs.push_back(NameAssignmentFunc::createNameAssignmentFunc("random")); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 57 | } |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 58 | registerPrefix(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | CaModule::~CaModule() |
| 62 | { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 63 | for (auto& handle : m_interestFilterHandles) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 64 | handle.cancel(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 65 | } |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 66 | for (auto& handle : m_registeredPrefixHandles) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 67 | handle.unregister(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | void |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 72 | CaModule::registerPrefix() |
| 73 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 74 | // register prefixes |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 75 | Name prefix = m_config.caProfile.caPrefix; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 76 | prefix.append("CA"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 77 | |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 78 | auto prefixId = m_face.registerPrefix( |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 79 | prefix, |
| 80 | [&](const Name& name) { |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 81 | // register INFO RDR metadata prefix |
| 82 | name::Component metaDataComp(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata")); |
| 83 | auto filterId = m_face.setInterestFilter(Name(name).append("INFO").append(metaDataComp), |
| 84 | bind(&CaModule::onCaProfileDiscovery, this, _2)); |
| 85 | m_interestFilterHandles.push_back(filterId); |
| 86 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 87 | // register PROBE prefix |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 88 | filterId = m_face.setInterestFilter(Name(name).append("PROBE"), |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 89 | bind(&CaModule::onProbe, this, _2)); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 90 | m_interestFilterHandles.push_back(filterId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 91 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 92 | // register NEW prefix |
| 93 | filterId = m_face.setInterestFilter(Name(name).append("NEW"), |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 94 | bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW)); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 95 | m_interestFilterHandles.push_back(filterId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 97 | // register SELECT prefix |
| 98 | filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"), |
| 99 | bind(&CaModule::onChallenge, this, _2)); |
| 100 | m_interestFilterHandles.push_back(filterId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 101 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 102 | // register REVOKE prefix |
| 103 | filterId = m_face.setInterestFilter(Name(name).append("REVOKE"), |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 104 | bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE)); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 105 | m_interestFilterHandles.push_back(filterId); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 106 | NDN_LOG_TRACE("Prefix " << name << " got registered"); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 107 | }, |
| 108 | bind(&CaModule::onRegisterFailed, this, _2)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 109 | m_registeredPrefixHandles.push_back(prefixId); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 110 | } |
| 111 | |
Zhiyi Zhang | f6c5d27 | 2020-09-28 10:17:32 -0700 | [diff] [blame] | 112 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 113 | CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback) |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 114 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 115 | m_statusUpdateCallback = onUpdateCallback; |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 118 | Data |
| 119 | CaModule::getCaProfileData() |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 120 | { |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 121 | if (m_profileData == nullptr) { |
| 122 | const auto& pib = m_keyChain.getPib(); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 123 | const auto& identity = pib.getIdentity( m_config.caProfile.caPrefix); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 124 | const auto& cert = identity.getDefaultKey().getDefaultCertificate(); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 125 | Block contentTLV = infotlv::encodeDataContent(m_config.caProfile, cert); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 126 | |
Zhiyi Zhang | 615b6b7 | 2021-01-11 14:25:32 -0800 | [diff] [blame] | 127 | // set naming convention to be typed |
| 128 | auto convention = name::getConventionEncoding(); |
| 129 | name::setConventionEncoding(name::Convention::TYPED); |
| 130 | |
| 131 | Name infoPacketName(m_config.caProfile.caPrefix); |
| 132 | auto segmentComp = name::Component::fromSegment(0); |
| 133 | infoPacketName.append("CA").append("INFO").appendVersion().append(segmentComp); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 134 | m_profileData = std::make_unique<Data>(infoPacketName); |
Zhiyi Zhang | 615b6b7 | 2021-01-11 14:25:32 -0800 | [diff] [blame] | 135 | m_profileData->setFinalBlock(segmentComp); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 136 | m_profileData->setContent(contentTLV); |
| 137 | m_profileData->setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 138 | m_keyChain.sign(*m_profileData, signingByIdentity( m_config.caProfile.caPrefix)); |
Zhiyi Zhang | 615b6b7 | 2021-01-11 14:25:32 -0800 | [diff] [blame] | 139 | |
| 140 | // set back the convention |
| 141 | name::setConventionEncoding(convention); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 142 | } |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 143 | return *m_profileData; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | CaModule::onCaProfileDiscovery(const Interest& request) |
| 148 | { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 149 | NDN_LOG_TRACE("Received CA Profile MetaData discovery Interest"); |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 150 | if (m_profileData == nullptr) { |
| 151 | m_profileData = std::make_unique<Data>(getCaProfileData()); |
| 152 | } |
| 153 | MetadataObject metadata; |
| 154 | metadata.setVersionedName(m_profileData->getName().getPrefix(-1)); |
| 155 | Name discoveryInterestName(m_profileData->getName().getPrefix(-2)); |
| 156 | name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata")); |
| 157 | discoveryInterestName.append(metadataComponent); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 158 | m_face.put(metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity( m_config.caProfile.caPrefix))); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 159 | } |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 160 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 161 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 162 | CaModule::onProbe(const Interest& request) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 163 | { |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 164 | // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent] |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 165 | NDN_LOG_TRACE("Received PROBE request"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 166 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 167 | // process PROBE requests: collect probe parameters |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 168 | auto parameters = probetlv::decodeApplicationParameters(request.getApplicationParameters()); |
tylerliu | 8fc0807 | 2020-10-06 15:37:40 -0700 | [diff] [blame] | 169 | std::vector<PartialName> availableComponents; |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 170 | for (auto& item : m_config.nameAssignmentFuncs) { |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 171 | auto names = item->assignName(parameters); |
| 172 | availableComponents.insert(availableComponents.end(), names.begin(), names.end()); |
| 173 | } |
Zhiyi Zhang | cb8acf2 | 2020-10-07 19:35:16 -0700 | [diff] [blame] | 174 | if (availableComponents.size() == 0) { |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 175 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | cb8acf2 | 2020-10-07 19:35:16 -0700 | [diff] [blame] | 176 | "Cannot generate available names from parameters provided.")); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 177 | return; |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 178 | } |
| 179 | std::vector<Name> availableNames; |
| 180 | for (const auto& component : availableComponents) { |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 181 | Name newIdentityName = m_config.caProfile.caPrefix; |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 182 | newIdentityName.append(component); |
| 183 | availableNames.push_back(newIdentityName); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 184 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 185 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 186 | Data result; |
| 187 | result.setName(request.getName()); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 188 | result.setContent(probetlv::encodeDataContent(availableNames, m_config.caProfile.maxSuffixLength, m_config.redirection)); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 189 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 190 | m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix)); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 191 | m_face.put(result); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 192 | NDN_LOG_TRACE("Handle PROBE: send out the PROBE response"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 196 | CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 197 | { |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame^] | 198 | |
| 199 | //verify ca cert validity |
| 200 | const auto& caCert = m_keyChain.getPib().getIdentity( m_config.caProfile.caPrefix).getDefaultKey().getDefaultCertificate(); |
| 201 | if (!caCert.isValid()) { |
| 202 | NDN_LOG_ERROR("Server certificate invalid/expired"); |
| 203 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD, |
| 204 | "Server certificate invalid/expired")); |
| 205 | return; |
| 206 | } |
| 207 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 208 | // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest] |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 209 | // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest] |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 210 | // get ECDH pub key and cert request |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 211 | const auto& parameterTLV = request.getApplicationParameters(); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 212 | std::vector<uint8_t> ecdhPub; |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 213 | shared_ptr<security::Certificate> clientCert; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 214 | try { |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 215 | requesttlv::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert); |
Zhiyi Zhang | cd57da8 | 2020-10-08 20:35:40 -0700 | [diff] [blame] | 216 | } |
| 217 | catch (const std::exception& e) { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 218 | if (!parameterTLV.hasValue()) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 219 | NDN_LOG_ERROR("Empty TLV obtained from the Interest parameter."); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 220 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 221 | "Empty TLV obtained from the Interest parameter.")); |
| 222 | return; |
| 223 | } |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 224 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 225 | NDN_LOG_ERROR("Unrecognized self-signed certificate: " << e.what()); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 226 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 227 | "Unrecognized self-signed certificate.")); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 228 | return; |
| 229 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 230 | |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 231 | if (ecdhPub.empty()) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 232 | NDN_LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter."); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 233 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 234 | "Empty ECDH PUB obtained from the Interest parameter.")); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 237 | |
| 238 | // get server's ECDH pub key |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 239 | ECDHState ecdh; |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 240 | auto myEcdhPubKeyBase64 = ecdh.getSelfPubKey(); |
| 241 | std::vector<uint8_t> sharedSecret; |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 242 | try { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 243 | sharedSecret = ecdh.deriveSecret(ecdhPub); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 244 | } |
| 245 | catch (const std::exception& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 246 | NDN_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] | 247 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 248 | "Cannot derive a shared secret using the provided ECDH key.")); |
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 | // generate salt for HKDF |
Zhiyi Zhang | c9f9fac | 2020-10-18 19:51:51 -0700 | [diff] [blame] | 252 | std::array<uint8_t, 32> salt; |
| 253 | random::generateSecureBytes(salt.data(), salt.size()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 254 | // hkdf |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 255 | std::array<uint8_t, 16> aesKey; |
| 256 | hkdf(sharedSecret.data(), sharedSecret.size(), salt.data(), salt.size(), aesKey.data(), aesKey.size()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 257 | |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 258 | // verify identity name |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 259 | if (! m_config.caProfile.caPrefix.isPrefixOf(clientCert->getIdentity()) |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 260 | || !security::Certificate::isValidName(clientCert->getName()) |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 261 | || clientCert->getIdentity().size() <= m_config.caProfile.caPrefix.size()) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 262 | NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName()); |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 263 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED, |
| 264 | "An invalid certificate name is being requested.")); |
| 265 | return; |
| 266 | } |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 267 | if ( m_config.caProfile.maxSuffixLength) { |
| 268 | if (clientCert->getIdentity().size() > m_config.caProfile.caPrefix.size() + * m_config.caProfile.maxSuffixLength) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 269 | NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName()); |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 270 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED, |
| 271 | "An invalid certificate name is being requested.")); |
| 272 | return; |
| 273 | } |
| 274 | } |
| 275 | |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 276 | if (requestType == RequestType::NEW) { |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 277 | // check the validity period |
| 278 | auto expectedPeriod = clientCert->getValidityPeriod().getPeriod(); |
| 279 | auto currentTime = time::system_clock::now(); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 280 | if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD || |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 281 | expectedPeriod.second > currentTime + m_config.caProfile.maxValidityPeriod || |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 282 | expectedPeriod.second <= expectedPeriod.first) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 283 | NDN_LOG_ERROR("An invalid validity period is being requested."); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 284 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD, |
| 285 | "An invalid validity period is being requested.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 286 | return; |
| 287 | } |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 288 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 289 | // verify signature |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 290 | if (!security::verifySignature(*clientCert, *clientCert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 291 | NDN_LOG_ERROR("Invalid signature in the self-signed certificate."); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 292 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 293 | "Invalid signature in the self-signed certificate.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | if (!security::verifySignature(request, *clientCert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 297 | NDN_LOG_ERROR("Invalid signature in the Interest packet."); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 298 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 299 | "Invalid signature in the Interest packet.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 300 | return; |
| 301 | } |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 302 | } |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 303 | else if (requestType == RequestType::REVOKE) { |
tylerliu | d9239f5 | 2020-09-30 17:25:16 -0700 | [diff] [blame] | 304 | //verify cert is from this CA |
tylerliu | f62cfeb | 2021-01-28 11:01:33 -0800 | [diff] [blame^] | 305 | if (!security::verifySignature(*clientCert, caCert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 306 | NDN_LOG_ERROR("Invalid signature in the certificate to revoke."); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 307 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 308 | "Invalid signature in the certificate to revoke.")); |
tylerliu | 4a00aad | 2020-09-26 02:03:17 -0700 | [diff] [blame] | 309 | return; |
| 310 | } |
Zhiyi Zhang | 693c127 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 313 | // create new request instance |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 314 | uint8_t requestIdData[32]; |
| 315 | Block certNameTlv = clientCert->getName().wireEncode(); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 316 | try { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 317 | hmacSha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 318 | } |
| 319 | catch (const std::runtime_error& e) { |
| 320 | NDN_LOG_ERROR("Error computing the request ID: " << std::string(e.what())); |
| 321 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
| 322 | "Error computing the request ID.")); |
| 323 | return; |
| 324 | } |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 325 | RequestId id; |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 326 | std::memcpy(id.data(), requestIdData, id.size()); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 327 | // initialize request state |
| 328 | RequestState requestState; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 329 | requestState.caPrefix = m_config.caProfile.caPrefix; |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 330 | requestState.requestId = id; |
| 331 | requestState.requestType = requestType; |
| 332 | requestState.cert = *clientCert; |
| 333 | requestState.encryptionKey = aesKey; |
Zhiyi Zhang | 21d52b5 | 2020-10-05 18:29:15 -0700 | [diff] [blame] | 334 | try { |
| 335 | m_storage->addRequest(requestState); |
| 336 | } |
| 337 | catch (const std::runtime_error& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 338 | NDN_LOG_ERROR("Duplicate Request ID: The same request has been seen before."); |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 339 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 340 | "Duplicate Request ID: The same request has been seen before.")); |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 341 | return; |
Zhiyi Zhang | 21d52b5 | 2020-10-05 18:29:15 -0700 | [diff] [blame] | 342 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 343 | Data result; |
| 344 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 345 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 346 | result.setContent(requesttlv::encodeDataContent(myEcdhPubKeyBase64, |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 347 | salt, |
| 348 | requestState.requestId, requestState.status, |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 349 | m_config.caProfile.supportedChallenges)); |
| 350 | m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 351 | m_face.put(result); |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 352 | if (m_statusUpdateCallback) { |
| 353 | m_statusUpdateCallback(requestState); |
Zhiyi Zhang | 7420cf5 | 2017-12-18 18:59:21 +0800 | [diff] [blame] | 354 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 358 | CaModule::onChallenge(const Interest& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 359 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 360 | // get certificate request state |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 361 | auto requestState = getCertificateRequest(request); |
Zhiyi Zhang | 84207c5 | 2020-10-28 21:45:44 -0700 | [diff] [blame] | 362 | if (requestState == nullptr) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 363 | NDN_LOG_ERROR("No certificate request state can be found."); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 364 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 365 | "No certificate request state can be found.")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 366 | return; |
| 367 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 368 | // verify signature |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 369 | if (!security::verifySignature(request, requestState->cert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 370 | NDN_LOG_ERROR("Invalid Signature in the Interest packet."); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 371 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, |
| 372 | "Invalid Signature in the Interest packet.")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 373 | return; |
| 374 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 375 | // decrypt the parameters |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 376 | Buffer paramTLVPayload; |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 377 | try { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 378 | paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 379 | requestState->encryptionKey.data(), |
| 380 | requestState->requestId.data(), |
Zhiyi Zhang | 4f1c010 | 2020-12-21 15:08:09 -0800 | [diff] [blame] | 381 | requestState->requestId.size(), |
| 382 | requestState->decryptionIv); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 383 | } |
| 384 | catch (const std::exception& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 385 | NDN_LOG_ERROR("Interest paramaters decryption failed: " << e.what()); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 386 | m_storage->deleteRequest(requestState->requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 387 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 388 | "Interest paramaters decryption failed.")); |
Zhiyi Zhang | bc2d412 | 2019-09-10 12:10:54 -0700 | [diff] [blame] | 389 | return; |
| 390 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 391 | if (paramTLVPayload.size() == 0) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 392 | NDN_LOG_ERROR("No parameters are found after decryption."); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 393 | m_storage->deleteRequest(requestState->requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 394 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 395 | "No parameters are found after decryption.")); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 396 | return; |
| 397 | } |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 398 | Block paramTLV = makeBinaryBlock(tlv::EncryptedPayload, paramTLVPayload.data(), paramTLVPayload.size()); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 399 | paramTLV.parse(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 400 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 401 | // load the corresponding challenge module |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 402 | std::string challengeType = readString(paramTLV.get(tlv::SelectedChallenge)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 403 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
| 404 | if (challenge == nullptr) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 405 | NDN_LOG_TRACE("Unrecognized challenge type: " << challengeType); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 406 | m_storage->deleteRequest(requestState->requestId); |
Zhiyi Zhang | 01e91a3 | 2020-09-29 12:10:00 -0700 | [diff] [blame] | 407 | m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type.")); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 408 | return; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 409 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 410 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 411 | NDN_LOG_TRACE("CHALLENGE module to be load: " << challengeType); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 412 | auto errorInfo = challenge->handleChallengeRequest(paramTLV, *requestState); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 413 | if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 414 | m_storage->deleteRequest(requestState->requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 415 | m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo))); |
| 416 | return; |
| 417 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 418 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 419 | Block payload; |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 420 | if (requestState->status == Status::PENDING) { |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 421 | // if challenge succeeded |
tylerliu | d82cc5c | 2020-12-21 23:58:39 -0800 | [diff] [blame] | 422 | if (requestState->requestType == RequestType::NEW || requestState->requestType == RequestType::RENEW) { |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 423 | auto issuedCert = issueCertificate(*requestState); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 424 | requestState->cert = issuedCert; |
| 425 | requestState->status = Status::SUCCESS; |
| 426 | m_storage->deleteRequest(requestState->requestId); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 427 | |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 428 | payload = challengetlv::encodeDataContent(*requestState, issuedCert.getName()); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 429 | NDN_LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName()); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 430 | } |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 431 | else if (requestState->requestType == RequestType::REVOKE) { |
| 432 | requestState->status = Status::SUCCESS; |
| 433 | m_storage->deleteRequest(requestState->requestId); |
Zhiyi Zhang | f9d94eb | 2020-12-21 15:39:38 -0800 | [diff] [blame] | 434 | // TODO: where is the code to revoke? |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 435 | payload = challengetlv::encodeDataContent(*requestState); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 436 | NDN_LOG_TRACE("Challenge succeeded. Certificate has been revoked"); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | else { |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 440 | payload = challengetlv::encodeDataContent(*requestState); |
Zhiyi Zhang | c1e9815 | 2020-12-21 16:15:39 -0800 | [diff] [blame] | 441 | m_storage->updateRequest(*requestState); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 442 | NDN_LOG_TRACE("No failure no success. Challenge moves on"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 443 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 444 | |
| 445 | Data result; |
| 446 | result.setName(request.getName()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 447 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 448 | result.setContent(payload); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 449 | m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 450 | m_face.put(result); |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 451 | if (m_statusUpdateCallback) { |
| 452 | m_statusUpdateCallback(*requestState); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 453 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 454 | } |
| 455 | |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 456 | security::Certificate |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 457 | CaModule::issueCertificate(const RequestState& requestState) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 458 | { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 459 | auto expectedPeriod = requestState.cert.getValidityPeriod().getPeriod(); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 460 | security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 461 | security::Certificate newCert; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 462 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 463 | Name certName = requestState.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 | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 466 | newCert.setContent(requestState.cert.getContent()); |
| 467 | NDN_LOG_TRACE("cert request content " << requestState.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 | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 471 | m_config.caProfile.caPrefix, signatureInfo); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 472 | |
| 473 | m_keyChain.sign(newCert, signingInfo); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 474 | NDN_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 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 478 | std::unique_ptr<RequestState> |
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 | { |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 481 | RequestId requestId; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 482 | try { |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 483 | auto& component = request.getName().at(m_config.caProfile.caPrefix.size() + 2); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 484 | std::memcpy(requestId.data(), component.value(), component.value_size()); |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 485 | } |
| 486 | catch (const std::exception& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 487 | NDN_LOG_ERROR("Cannot read the request ID out from the request: " << e.what()); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 488 | return nullptr; |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 489 | } |
| 490 | try { |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 491 | NDN_LOG_TRACE("Request Id to query the database " << toHex(requestId.data(), requestId.size())); |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 492 | return std::make_unique<RequestState>(m_storage->getRequest(requestId)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 493 | } |
| 494 | catch (const std::exception& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 495 | NDN_LOG_ERROR("Cannot get certificate request record from the storage: " << e.what()); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 496 | return nullptr; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 497 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void |
| 501 | CaModule::onRegisterFailed(const std::string& reason) |
| 502 | { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 503 | NDN_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] | 504 | } |
| 505 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 506 | Data |
| 507 | CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo) |
| 508 | { |
| 509 | Data result; |
| 510 | result.setName(name); |
| 511 | result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 512 | result.setContent(errortlv::encodeDataContent(error, errorInfo)); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 513 | m_keyChain.sign(result, signingByIdentity( m_config.caProfile.caPrefix)); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 514 | return result; |
| 515 | } |
| 516 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 517 | } // namespace ca |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 518 | } // namespace ndncert |
| 519 | } // namespace ndn |