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