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