Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 1 | /* -*- 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 Zhang | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/challenge-encoder.hpp" |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 22 | |
| 23 | namespace ndn { |
| 24 | namespace ndncert { |
| 25 | |
| 26 | Block |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 27 | challengetlv::encodeDataContent(ca::RequestState& request, const Name& issuedCertName) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 28 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 29 | Block response(tlv::EncryptedPayload); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 30 | 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)); |
tylerliu | ab9cba2 | 2020-10-09 00:13:46 -0700 | [diff] [blame] | 33 | response.push_back( |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 34 | makeNonNegativeIntegerBlock(tlv::RemainingTries, request.challengeState->remainingTries)); |
tylerliu | ab9cba2 | 2020-10-09 00:13:46 -0700 | [diff] [blame] | 35 | response.push_back( |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 36 | makeNonNegativeIntegerBlock(tlv::RemainingTime, request.challengeState->remainingTime.count())); |
tylerliu | ab9cba2 | 2020-10-09 00:13:46 -0700 | [diff] [blame] | 37 | } |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 38 | if (!issuedCertName.empty()) { |
| 39 | response.push_back(makeNestedBlock(tlv::IssuedCertName, issuedCertName)); |
Zhiyi Zhang | a2db719 | 2020-10-30 09:08:19 -0700 | [diff] [blame] | 40 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 41 | response.encode(); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 42 | return encodeBlockWithAesGcm128(ndn::tlv::Content, request.encryptionKey.data(), |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 43 | response.value(), response.value_size(), |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 44 | request.requestId.data(), request.requestId.size(), request.aesBlockCounter); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 47 | void |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 48 | challengetlv::decodeDataContent(const Block& contentBlock, requester::RequestState& state) |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 49 | { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame^] | 50 | auto result = decodeBlockWithAesGcm128(contentBlock, state.aesKey.data(), |
| 51 | state.requestId.data(), state.requestId.size()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 52 | auto data = makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size()); |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 53 | data.parse(); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame^] | 54 | state.status = statusFromBlock(data.get(tlv::Status)); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 55 | if (data.find(tlv::ChallengeStatus) != data.elements_end()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame^] | 56 | state.challengeStatus = readString(data.get(tlv::ChallengeStatus)); |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 57 | } |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 58 | if (data.find(tlv::RemainingTries) != data.elements_end()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame^] | 59 | state.remainingTries = readNonNegativeInteger(data.get(tlv::RemainingTries)); |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 60 | } |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 61 | if (data.find(tlv::RemainingTime) != data.elements_end()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame^] | 62 | state.freshBefore = time::system_clock::now() + time::seconds(readNonNegativeInteger(data.get(tlv::RemainingTime))); |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 63 | } |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 64 | if (data.find(tlv::IssuedCertName) != data.elements_end()) { |
| 65 | Block issuedCertNameBlock = data.get(tlv::IssuedCertName); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame^] | 66 | state.issuedCertName = Name(issuedCertNameBlock.blockFromValue()); |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 67 | } |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 70 | } // namespace ndncert |
| 71 | } // namespace ndn |