blob: 9ab567a374dc3c961a41c48e7ca6394e70f42177 [file] [log] [blame]
Suyong Won19fba4d2020-05-09 13:39:46 -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
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/challenge-encoder.hpp"
Suyong Won19fba4d2020-05-09 13:39:46 -070022
23namespace ndn {
24namespace ndncert {
25
26Block
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080027challengetlv::encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
Suyong Won19fba4d2020-05-09 13:39:46 -070028{
tylerliu1f480be2020-11-10 13:02:53 -080029 Block response(tlv::EncryptedPayload);
tylerliu7b9185c2020-11-24 12:15:18 -080030 response.push_back(makeNonNegativeIntegerBlock(tlv::Status, static_cast<uint64_t>(request.status)));
31 if (request.challengeState) {
32 response.push_back(makeStringBlock(tlv::ChallengeStatus, request.challengeState->challengeStatus));
tylerliuab9cba22020-10-09 00:13:46 -070033 response.push_back(
tylerliu7b9185c2020-11-24 12:15:18 -080034 makeNonNegativeIntegerBlock(tlv::RemainingTries, request.challengeState->remainingTries));
tylerliuab9cba22020-10-09 00:13:46 -070035 response.push_back(
tylerliu7b9185c2020-11-24 12:15:18 -080036 makeNonNegativeIntegerBlock(tlv::RemainingTime, request.challengeState->remainingTime.count()));
tylerliuf51e3162020-12-20 19:22:59 -080037 if (request.challengeState->challengeStatus == "need-proof") {
38 response.push_back(
39 makeStringBlock(tlv::ParameterKey, "nonce")
40 );
41 auto nonce = fromHex(request.challengeState->secrets.get("nonce", ""));
42 response.push_back(
43 makeBinaryBlock(tlv::ParameterValue, nonce->data(), 16)
44 );
45 }
tylerliuab9cba22020-10-09 00:13:46 -070046 }
tylerliubb630362020-11-10 11:31:35 -080047 if (!issuedCertName.empty()) {
48 response.push_back(makeNestedBlock(tlv::IssuedCertName, issuedCertName));
Zhiyi Zhanga2db7192020-10-30 09:08:19 -070049 }
Suyong Won44d0cce2020-05-10 04:07:43 -070050 response.encode();
tylerliu7b9185c2020-11-24 12:15:18 -080051 return encodeBlockWithAesGcm128(ndn::tlv::Content, request.encryptionKey.data(),
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070052 response.value(), response.value_size(),
tylerliu7b9185c2020-11-24 12:15:18 -080053 request.requestId.data(), request.requestId.size(), request.aesBlockCounter);
Suyong Won19fba4d2020-05-09 13:39:46 -070054}
55
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070056void
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080057challengetlv::decodeDataContent(const Block& contentBlock, requester::RequestState& state)
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070058{
tylerliuf2e6bb52020-12-13 13:23:05 -080059 auto result = decodeBlockWithAesGcm128(contentBlock, state.aesKey.data(),
60 state.requestId.data(), state.requestId.size());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070061 auto data = makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size());
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070062 data.parse();
tylerliuf2e6bb52020-12-13 13:23:05 -080063 state.status = statusFromBlock(data.get(tlv::Status));
tylerliu50d679e2020-10-14 14:08:39 -070064 if (data.find(tlv::ChallengeStatus) != data.elements_end()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080065 state.challengeStatus = readString(data.get(tlv::ChallengeStatus));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070066 }
tylerliu50d679e2020-10-14 14:08:39 -070067 if (data.find(tlv::RemainingTries) != data.elements_end()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080068 state.remainingTries = readNonNegativeInteger(data.get(tlv::RemainingTries));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070069 }
tylerliu50d679e2020-10-14 14:08:39 -070070 if (data.find(tlv::RemainingTime) != data.elements_end()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080071 state.freshBefore = time::system_clock::now() + 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);
tylerliuf2e6bb52020-12-13 13:23:05 -080075 state.issuedCertName = Name(issuedCertNameBlock.blockFromValue());
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070076 }
tylerliuf51e3162020-12-20 19:22:59 -080077 if (data.find(tlv::ParameterKey) != data.elements_end() && readString(data.get(tlv::ParameterKey)) == "nonce") {
78 if (data.find(tlv::ParameterKey) == data.elements_end()) {
79 NDN_THROW(std::runtime_error("Parameter Key found, but no value found"));
80 }
81 Block nonceBlock = data.get(tlv::ParameterValue);
82 if (nonceBlock.value_size() != 16) {
83 NDN_THROW(std::runtime_error("Wrong nonce length"));
84 }
85 memcpy(state.nonce.data(), nonceBlock.value(), 16);
86 }
tylerliu0790bdb2020-10-02 00:50:51 -070087}
88
Zhiyi Zhange4891b72020-10-10 15:11:57 -070089} // namespace ndncert
90} // namespace ndn