blob: 5d9d46b45ec8e870bff636e8a85a824c6a4c5279 [file] [log] [blame]
Suyong Won19fba4d2020-05-09 13:39:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
3 * Copyright (c) 2017-2021, Regents of the University of California.
Suyong Won19fba4d2020-05-09 13:39:46 -07004 *
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
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/challenge-encoder.hpp"
Suyong Won19fba4d2020-05-09 13:39:46 -070022
Suyong Won19fba4d2020-05-09 13:39:46 -070023namespace ndncert {
24
25Block
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080026challengetlv::encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
Suyong Won19fba4d2020-05-09 13:39:46 -070027{
tylerliu1f480be2020-11-10 13:02:53 -080028 Block response(tlv::EncryptedPayload);
Davide Pesavento0dc02012021-11-23 22:55:03 -050029 response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::Status, static_cast<uint64_t>(request.status)));
tylerliu7b9185c2020-11-24 12:15:18 -080030 if (request.challengeState) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050031 response.push_back(ndn::makeStringBlock(tlv::ChallengeStatus, request.challengeState->challengeStatus));
32 response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::RemainingTries,
33 request.challengeState->remainingTries));
34 response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::RemainingTime,
35 request.challengeState->remainingTime.count()));
tylerliuf51e3162020-12-20 19:22:59 -080036 if (request.challengeState->challengeStatus == "need-proof") {
Davide Pesavento0dc02012021-11-23 22:55:03 -050037 response.push_back(ndn::makeStringBlock(tlv::ParameterKey, "nonce"));
38 auto nonce = ndn::fromHex(request.challengeState->secrets.get("nonce", ""));
39 response.push_back(ndn::makeBinaryBlock(tlv::ParameterValue, nonce->data(), 16));
tylerliuf51e3162020-12-20 19:22:59 -080040 }
tylerliuab9cba22020-10-09 00:13:46 -070041 }
tylerliubb630362020-11-10 11:31:35 -080042 if (!issuedCertName.empty()) {
43 response.push_back(makeNestedBlock(tlv::IssuedCertName, issuedCertName));
Zhiyi Zhanga2db7192020-10-30 09:08:19 -070044 }
Suyong Won44d0cce2020-05-10 04:07:43 -070045 response.encode();
Davide Pesavento0dc02012021-11-23 22:55:03 -050046
tylerliu7b9185c2020-11-24 12:15:18 -080047 return encodeBlockWithAesGcm128(ndn::tlv::Content, request.encryptionKey.data(),
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070048 response.value(), response.value_size(),
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -080049 request.requestId.data(), request.requestId.size(),
50 request.encryptionIv);
Suyong Won19fba4d2020-05-09 13:39:46 -070051}
52
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070053void
tylerliu4140fe82021-01-27 15:45:44 -080054challengetlv::decodeDataContent(const Block& contentBlock, requester::Request& state)
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070055{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080056 auto result = decodeBlockWithAesGcm128(contentBlock, state.m_aesKey.data(),
57 state.m_requestId.data(), state.m_requestId.size(),
Zhiyi Zhang3b9a5032021-02-18 11:15:09 -080058 state.m_decryptionIv, state.m_encryptionIv);
Davide Pesavento0dc02012021-11-23 22:55:03 -050059 auto data = ndn::makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size());
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070060 data.parse();
Davide Pesavento0dc02012021-11-23 22:55:03 -050061
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080062 state.m_status = statusFromBlock(data.get(tlv::Status));
tylerliu50d679e2020-10-14 14:08:39 -070063 if (data.find(tlv::ChallengeStatus) != data.elements_end()) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080064 state.m_challengeStatus = readString(data.get(tlv::ChallengeStatus));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070065 }
tylerliu50d679e2020-10-14 14:08:39 -070066 if (data.find(tlv::RemainingTries) != data.elements_end()) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080067 state.m_remainingTries = readNonNegativeInteger(data.get(tlv::RemainingTries));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070068 }
tylerliu50d679e2020-10-14 14:08:39 -070069 if (data.find(tlv::RemainingTime) != data.elements_end()) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080070 state.m_freshBefore = time::system_clock::now() +
71 time::seconds(readNonNegativeInteger(data.get(tlv::RemainingTime)));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070072 }
tylerliu50d679e2020-10-14 14:08:39 -070073 if (data.find(tlv::IssuedCertName) != data.elements_end()) {
74 Block issuedCertNameBlock = data.get(tlv::IssuedCertName);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080075 state.m_issuedCertName = Name(issuedCertNameBlock.blockFromValue());
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070076 }
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -080077 if (data.find(tlv::ParameterKey) != data.elements_end() &&
78 readString(data.get(tlv::ParameterKey)) == "nonce") {
tylerliuf51e3162020-12-20 19:22:59 -080079 if (data.find(tlv::ParameterKey) == data.elements_end()) {
80 NDN_THROW(std::runtime_error("Parameter Key found, but no value found"));
81 }
82 Block nonceBlock = data.get(tlv::ParameterValue);
83 if (nonceBlock.value_size() != 16) {
84 NDN_THROW(std::runtime_error("Wrong nonce length"));
85 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080086 memcpy(state.m_nonce.data(), nonceBlock.value(), 16);
tylerliuf51e3162020-12-20 19:22:59 -080087 }
tylerliu0790bdb2020-10-02 00:50:51 -070088}
89
Zhiyi Zhange4891b72020-10-10 15:11:57 -070090} // namespace ndncert