blob: 4368700bcaf9f2a4254be65642e0bd8f4d549c59 [file] [log] [blame]
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017-2020, Regents of the University of California.
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 "requester.hpp"
Zhiyi Zhangdbd9d432020-10-07 15:56:27 -070022#include "identity-challenge/challenge-module.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070023#include "detail/crypto-helper.hpp"
24#include "detail/challenge-encoder.hpp"
25#include "detail/error-encoder.hpp"
26#include "detail/info-encoder.hpp"
27#include "detail/new-renew-revoke-encoder.hpp"
28#include "detail/probe-encoder.hpp"
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070029#include <ndn-cxx/security/signing-helpers.hpp>
30#include <ndn-cxx/security/transform/base64-encode.hpp>
31#include <ndn-cxx/security/transform/buffer-source.hpp>
32#include <ndn-cxx/security/transform/stream-sink.hpp>
33#include <ndn-cxx/security/verification-helpers.hpp>
34#include <ndn-cxx/util/io.hpp>
35#include <ndn-cxx/util/random.hpp>
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070036#include <ndn-cxx/metadata-object.hpp>
tylerliu96a67e82020-10-15 13:37:12 -070037#include <boost/lexical_cast.hpp>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070038
39namespace ndn {
40namespace ndncert {
41
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070042NDN_LOG_INIT(ndncert.client);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070043
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070044shared_ptr<Interest>
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070045Requester::genCaProfileDiscoveryInterest(const Name& caName)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070046{
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070047 Name contentName = caName;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070048 if (readString(caName.at(-1)) != "CA")
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070049 contentName.append("CA");
50 contentName.append("INFO");
51 return std::make_shared<Interest>(MetadataObject::makeDiscoveryInterest(contentName));
52}
53
54shared_ptr<Interest>
55Requester::genCaProfileInterestFromDiscoveryResponse(const Data& reply)
56{
57 auto metaData = MetadataObject(reply);
58 auto interestName= metaData.getVersionedName();
59 interestName.appendSegment(0);
60 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070061 interest->setCanBePrefix(false);
62 return interest;
63}
64
65boost::optional<CaProfile>
66Requester::onCaProfileResponse(const Data& reply)
67{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070068 auto caItem = InfoEncoder::decodeDataContent(reply.getContent());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070069 if (!security::verifySignature(reply, *caItem.m_cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070070 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -070071 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070072 }
73 return caItem;
74}
75
Zhiyi Zhang837406d2020-10-05 22:01:31 -070076
77boost::optional<CaProfile>
78Requester::onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName)
79{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070080 auto caItem = InfoEncoder::decodeDataContent(reply.getContent());
Zhiyi Zhang837406d2020-10-05 22:01:31 -070081 auto certBlock = caItem.m_cert->wireEncode();
tylerliua7bea662020-10-08 18:51:02 -070082 caItem.m_cert = std::make_shared<security::Certificate>(certBlock);
Zhiyi Zhang837406d2020-10-05 22:01:31 -070083 if (caItem.m_cert->getFullName() != caCertFullName) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070084 NDN_LOG_ERROR("Ca profile does not match the certificate information offered by the original CA.");
tylerliu41c11532020-10-10 16:14:45 -070085 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang837406d2020-10-05 22:01:31 -070086 }
87 return onCaProfileResponse(reply);
88}
89
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070090shared_ptr<Interest>
91Requester::genProbeInterest(const CaProfile& ca, std::vector<std::tuple<std::string, std::string>>&& probeInfo)
92{
93 Name interestName = ca.m_caPrefix;
94 interestName.append("CA").append("PROBE");
Zhiyi Zhang32437282020-10-10 16:15:37 -070095 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070096 interest->setMustBeFresh(true);
97 interest->setCanBePrefix(false);
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070098 interest->setApplicationParameters(ProbeEncoder::encodeApplicationParameters(std::move(probeInfo)));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070099 return interest;
100}
101
102void
103Requester::onProbeResponse(const Data& reply, const CaProfile& ca,
tylerliub47dad72020-10-08 21:36:55 -0700104 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700105{
106 if (!security::verifySignature(reply, *ca.m_cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700107 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700108 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700109 return;
110 }
111 processIfError(reply);
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700112 ProbeEncoder::decodeDataContent(reply.getContent(), identityNames, otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700113}
114
115shared_ptr<Interest>
116Requester::genNewInterest(RequesterState& state, const Name& identityName,
117 const time::system_clock::TimePoint& notBefore,
118 const time::system_clock::TimePoint& notAfter)
119{
120 if (!state.m_caItem.m_caPrefix.isPrefixOf(identityName)) {
121 return nullptr;
122 }
123 if (identityName.empty()) {
124 NDN_LOG_TRACE("Randomly create a new name because identityName is empty and the param is empty.");
125 state.m_identityName = state.m_caItem.m_caPrefix;
126 state.m_identityName.append(std::to_string(random::generateSecureWord64()));
127 }
128 else {
129 state.m_identityName = identityName;
130 }
131
132 // generate a newly key pair or use an existing key
133 const auto& pib = state.m_keyChain.getPib();
134 security::pib::Identity identity;
135 try {
136 identity = pib.getIdentity(state.m_identityName);
137 }
138 catch (const security::Pib::Error& e) {
139 identity = state.m_keyChain.createIdentity(state.m_identityName);
140 state.m_isNewlyCreatedIdentity = true;
141 state.m_isNewlyCreatedKey = true;
142 }
143 try {
144 state.m_keyPair = identity.getDefaultKey();
145 }
146 catch (const security::Pib::Error& e) {
147 state.m_keyPair = state.m_keyChain.createKey(identity);
148 state.m_isNewlyCreatedKey = true;
149 }
150 auto& keyName = state.m_keyPair.getName();
151
152 // generate certificate request
tylerliua7bea662020-10-08 18:51:02 -0700153 security::Certificate certRequest;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700154 certRequest.setName(Name(keyName).append("cert-request").appendVersion());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700155 certRequest.setContentType(ndn::tlv::ContentType_Key);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700156 certRequest.setContent(state.m_keyPair.getPublicKey().data(), state.m_keyPair.getPublicKey().size());
157 SignatureInfo signatureInfo;
158 signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter));
159 state.m_keyChain.sign(certRequest, signingByKey(keyName).setSignatureInfo(signatureInfo));
160
161 // generate Interest packet
162 Name interestName = state.m_caItem.m_caPrefix;
163 interestName.append("CA").append("NEW");
Zhiyi Zhang32437282020-10-10 16:15:37 -0700164 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700165 interest->setMustBeFresh(true);
166 interest->setCanBePrefix(false);
167 interest->setApplicationParameters(
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700168 NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::NEW, state.m_ecdh.getBase64PubKey(), certRequest));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700169
170 // sign the Interest packet
171 state.m_keyChain.sign(*interest, signingByKey(keyName));
172 return interest;
173}
174
175shared_ptr<Interest>
tylerliua7bea662020-10-08 18:51:02 -0700176Requester::genRevokeInterest(RequesterState& state, const security::Certificate& certificate)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700177{
178 if (!state.m_caItem.m_caPrefix.isPrefixOf(certificate.getName())) {
179 return nullptr;
180 }
181 // generate Interest packet
182 Name interestName = state.m_caItem.m_caPrefix;
183 interestName.append("CA").append("REVOKE");
Zhiyi Zhang32437282020-10-10 16:15:37 -0700184 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700185 interest->setMustBeFresh(true);
186 interest->setCanBePrefix(false);
187 interest->setApplicationParameters(
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700188 NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::REVOKE, state.m_ecdh.getBase64PubKey(), certificate));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700189 return interest;
190}
191
192std::list<std::string>
193Requester::onNewRenewRevokeResponse(RequesterState& state, const Data& reply)
194{
195 if (!security::verifySignature(reply, *state.m_caItem.m_cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700196 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700197 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700198 }
199 processIfError(reply);
200
201 auto contentTLV = reply.getContent();
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700202 const auto& content = NewRenewRevokeEncoder::decodeDataContent(contentTLV);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700203
Zhiyi Zhang91f86ab2020-10-05 15:36:35 -0700204 // ECDH and HKDF
tylerliu0790bdb2020-10-02 00:50:51 -0700205 state.m_ecdh.deriveSecret(content.ecdhKey);
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -0700206 hkdf(state.m_ecdh.m_sharedSecret, state.m_ecdh.m_sharedSecretLen,
Zhiyi Zhang91f86ab2020-10-05 15:36:35 -0700207 (uint8_t*)&content.salt, sizeof(content.salt), state.m_aesKey, sizeof(state.m_aesKey));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700208
209 // update state
tylerliu0790bdb2020-10-02 00:50:51 -0700210 state.m_status = content.requestStatus;
211 state.m_requestId = content.requestId;
tylerliu0790bdb2020-10-02 00:50:51 -0700212 return content.challenges;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700213}
214
215std::vector<std::tuple<std::string, std::string>>
216Requester::selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected)
217{
218 auto challenge = ChallengeModule::createChallengeModule(challengeSelected);
219 if (challenge == nullptr) {
tylerliu41c11532020-10-10 16:14:45 -0700220 NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700221 }
222 state.m_challengeType = challengeSelected;
223 return challenge->getRequestedParameterList(state.m_status, state.m_challengeStatus);
224}
225
226shared_ptr<Interest>
227Requester::genChallengeInterest(const RequesterState& state,
228 std::vector<std::tuple<std::string, std::string>>&& parameters)
229{
230 if (state.m_challengeType == "") {
tylerliu41c11532020-10-10 16:14:45 -0700231 NDN_THROW(std::runtime_error("The challenge has not been selected."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700232 }
233 auto challenge = ChallengeModule::createChallengeModule(state.m_challengeType);
234 if (challenge == nullptr) {
tylerliu41c11532020-10-10 16:14:45 -0700235 NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700236 }
237 auto challengeParams = challenge->genChallengeRequestTLV(state.m_status, state.m_challengeStatus, std::move(parameters));
238
239 Name interestName = state.m_caItem.m_caPrefix;
240 interestName.append("CA").append("CHALLENGE").append(state.m_requestId);
Zhiyi Zhang32437282020-10-10 16:15:37 -0700241 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700242 interest->setMustBeFresh(true);
243 interest->setCanBePrefix(false);
244
245 // encrypt the Interest parameters
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700246 auto paramBlock = encodeBlockWithAesGcm128(ndn::tlv::ApplicationParameters, state.m_aesKey,
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700247 challengeParams.value(), challengeParams.value_size(),
248 (const uint8_t*)"test", strlen("test"));
249 interest->setApplicationParameters(paramBlock);
250 state.m_keyChain.sign(*interest, signingByKey(state.m_keyPair.getName()));
251 return interest;
252}
253
254void
255Requester::onChallengeResponse(RequesterState& state, const Data& reply)
256{
257 if (!security::verifySignature(reply, *state.m_caItem.m_cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700258 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700259 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700260 }
261 processIfError(reply);
262 auto result = decodeBlockWithAesGcm128(reply.getContent(), state.m_aesKey, (const uint8_t*)"test", strlen("test"));
tylerliu50d679e2020-10-14 14:08:39 -0700263 Block contentTLV = makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700264 ChallengeEncoder::decodeDataContent(contentTLV, state);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700265}
266
267shared_ptr<Interest>
268Requester::genCertFetchInterest(const RequesterState& state)
269{
270 Name interestName = state.m_issuedCertName;
Zhiyi Zhang32437282020-10-10 16:15:37 -0700271 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700272 interest->setMustBeFresh(false);
273 interest->setCanBePrefix(false);
274 return interest;
275}
276
tylerliua7bea662020-10-08 18:51:02 -0700277shared_ptr<security::Certificate>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700278Requester::onCertFetchResponse(const Data& reply)
279{
280 try {
tylerliua7bea662020-10-08 18:51:02 -0700281 return std::make_shared<security::Certificate>(reply);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700282 }
283 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700284 NDN_LOG_ERROR("Cannot parse replied certificate ");
tylerliu41c11532020-10-10 16:14:45 -0700285 NDN_THROW(std::runtime_error("Cannot parse replied certificate "));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700286 return nullptr;
287 }
288}
289
290void
291Requester::endSession(RequesterState& state)
292{
293 if (state.m_status == Status::SUCCESS || state.m_status == Status::ENDED) {
294 return;
295 }
296 if (state.m_isNewlyCreatedIdentity) {
297 // put the identity into the if scope is because it may cause an error
298 // outside since when endSession is called, identity may not have been created yet.
299 auto identity = state.m_keyChain.getPib().getIdentity(state.m_identityName);
300 state.m_keyChain.deleteIdentity(identity);
301 }
302 else if (state.m_isNewlyCreatedKey) {
303 auto identity = state.m_keyChain.getPib().getIdentity(state.m_identityName);
304 state.m_keyChain.deleteKey(identity, state.m_keyPair);
305 }
306 state.m_status = Status::ENDED;
307}
308
309void
310Requester::processIfError(const Data& data)
311{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700312 auto errorInfo = ErrorEncoder::decodefromDataContent(data.getContent());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700313 if (std::get<0>(errorInfo) == ErrorCode::NO_ERROR) {
314 return;
315 }
Zhiyi Zhang1a222692020-10-16 11:35:49 -0700316 NDN_LOG_ERROR("Error info replied from the CA with Error code: " << std::get<0>(errorInfo) <<
317 " and Error Info: " << std::get<1>(errorInfo));
tylerliu41c11532020-10-10 16:14:45 -0700318 NDN_THROW(std::runtime_error("Error info replied from the CA with Error code: " +
Zhiyi Zhang1a222692020-10-16 11:35:49 -0700319 boost::lexical_cast<std::string>(std::get<0>(errorInfo)) +
320 " and Error Info: " + std::get<1>(errorInfo)));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700321}
322
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700323} // namespace ndncert
324} // namespace ndn