blob: 3870c959ec4931c9dcdb02d32ad5a6dbff8f9800 [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));
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -080033 response.push_back(makeNonNegativeIntegerBlock(tlv::RemainingTries,
34 request.challengeState->remainingTries));
35 response.push_back(makeNonNegativeIntegerBlock(tlv::RemainingTime,
36 request.challengeState->remainingTime.count()));
tylerliuf51e3162020-12-20 19:22:59 -080037 if (request.challengeState->challengeStatus == "need-proof") {
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -080038 response.push_back(makeStringBlock(tlv::ParameterKey, "nonce"));
tylerliuf51e3162020-12-20 19:22:59 -080039 auto nonce = fromHex(request.challengeState->secrets.get("nonce", ""));
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -080040 response.push_back(makeBinaryBlock(tlv::ParameterValue, nonce->data(), 16));
tylerliuf51e3162020-12-20 19:22:59 -080041 }
tylerliuab9cba22020-10-09 00:13:46 -070042 }
tylerliubb630362020-11-10 11:31:35 -080043 if (!issuedCertName.empty()) {
44 response.push_back(makeNestedBlock(tlv::IssuedCertName, issuedCertName));
Zhiyi Zhanga2db7192020-10-30 09:08:19 -070045 }
Suyong Won44d0cce2020-05-10 04:07:43 -070046 response.encode();
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(),
58 state.m_decryptionIv);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070059 auto data = makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size());
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070060 data.parse();
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080061 state.m_status = statusFromBlock(data.get(tlv::Status));
tylerliu50d679e2020-10-14 14:08:39 -070062 if (data.find(tlv::ChallengeStatus) != data.elements_end()) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080063 state.m_challengeStatus = readString(data.get(tlv::ChallengeStatus));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070064 }
tylerliu50d679e2020-10-14 14:08:39 -070065 if (data.find(tlv::RemainingTries) != data.elements_end()) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080066 state.m_remainingTries = readNonNegativeInteger(data.get(tlv::RemainingTries));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070067 }
tylerliu50d679e2020-10-14 14:08:39 -070068 if (data.find(tlv::RemainingTime) != data.elements_end()) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080069 state.m_freshBefore = time::system_clock::now() +
70 time::seconds(readNonNegativeInteger(data.get(tlv::RemainingTime)));
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070071 }
tylerliu50d679e2020-10-14 14:08:39 -070072 if (data.find(tlv::IssuedCertName) != data.elements_end()) {
73 Block issuedCertNameBlock = data.get(tlv::IssuedCertName);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080074 state.m_issuedCertName = Name(issuedCertNameBlock.blockFromValue());
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070075 }
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -080076 if (data.find(tlv::ParameterKey) != data.elements_end() &&
77 readString(data.get(tlv::ParameterKey)) == "nonce") {
tylerliuf51e3162020-12-20 19:22:59 -080078 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 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -080085 memcpy(state.m_nonce.data(), nonceBlock.value(), 16);
tylerliuf51e3162020-12-20 19:22:59 -080086 }
tylerliu0790bdb2020-10-02 00:50:51 -070087}
88
Zhiyi Zhange4891b72020-10-10 15:11:57 -070089} // namespace ndncert
90} // namespace ndn