blob: fde2d865fa387446dd611965c6150cd6f648c508 [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/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, 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 Zhang7cca76a2021-02-17 14:57:42 -080021#include "detail/request-encoder.hpp"
Davide Pesavento0dc02012021-11-23 22:55:03 -050022
Suyong Won19fba4d2020-05-09 13:39:46 -070023#include <ndn-cxx/security/transform/base64-encode.hpp>
24#include <ndn-cxx/security/transform/buffer-source.hpp>
25#include <ndn-cxx/security/transform/stream-sink.hpp>
Suyong Won19fba4d2020-05-09 13:39:46 -070026
Suyong Won19fba4d2020-05-09 13:39:46 -070027namespace ndncert {
28
Suyong Won19fba4d2020-05-09 13:39:46 -070029Block
Davide Pesavento0dc02012021-11-23 22:55:03 -050030requesttlv::encodeApplicationParameters(RequestType requestType,
31 const std::vector<uint8_t>& ecdhPub,
32 const Certificate& certRequest)
Suyong Won19fba4d2020-05-09 13:39:46 -070033{
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080034 Block
35 request(ndn::tlv::ApplicationParameters);
Davide Pesavento0dc02012021-11-23 22:55:03 -050036 request.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhPub.data(), ecdhPub.size()));
tylerliu4889a782020-09-30 22:47:49 -070037 if (requestType == RequestType::NEW || requestType == RequestType::RENEW) {
tylerliu50d679e2020-10-14 14:08:39 -070038 request.push_back(makeNestedBlock(tlv::CertRequest, certRequest));
tylerliu6563f932020-10-30 11:13:38 -070039 }
40 else if (requestType == RequestType::REVOKE) {
tylerliu50d679e2020-10-14 14:08:39 -070041 request.push_back(makeNestedBlock(tlv::CertToRevoke, certRequest));
tylerliu4889a782020-09-30 22:47:49 -070042 }
Suyong Won44d0cce2020-05-10 04:07:43 -070043 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070044 return request;
45}
46
tylerliu0790bdb2020-10-02 00:50:51 -070047void
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080048requesttlv::decodeApplicationParameters(const Block& payload, RequestType requestType,
Davide Pesavento0dc02012021-11-23 22:55:03 -050049 std::vector<uint8_t>& ecdhPub,
50 std::shared_ptr<Certificate>& clientCert)
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070051{
tylerliu0790bdb2020-10-02 00:50:51 -070052 payload.parse();
53
Tianyuan Yu13aac732022-03-03 20:59:54 -080054 int ecdhPubCount = 0;
tylerliu0790bdb2020-10-02 00:50:51 -070055 Block requestPayload;
Tianyuan Yu13aac732022-03-03 20:59:54 -080056 int requestPayloadCount = 0;
57 for (const auto &item : payload.elements()) {
58 if (item.type() == tlv::EcdhPub) {
59 ecdhPub.resize(item.value_size());
60 std::memcpy(ecdhPub.data(), item.value(), item.value_size());
61 ecdhPubCount++;
62 }
63 else if ((requestType == RequestType::NEW && item.type() == tlv::CertRequest) ||
64 (requestType == RequestType::REVOKE && item.type() == tlv::CertToRevoke)) {
65 requestPayload = item;
66 requestPayloadCount++;
67 requestPayload.parse();
68 clientCert = std::make_shared<Certificate>(requestPayload.get(ndn::tlv::Data));
69 }
70 else if (ndn::tlv::isCriticalType(item.type())) {
71 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
72 }
73 else {
74 //ignore
75 }
tylerliu0790bdb2020-10-02 00:50:51 -070076 }
tylerliu0790bdb2020-10-02 00:50:51 -070077
Tianyuan Yu13aac732022-03-03 20:59:54 -080078 if (ecdhPubCount != 1 || requestPayloadCount != 1) {
79 NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(ecdhPubCount) + " ecdh public param(s) and " +
80 std::to_string(requestPayloadCount) +
81 "request payload(s), instead of expected 1 times each."));
82 }
tylerliu0790bdb2020-10-02 00:50:51 -070083}
84
Suyong Won19fba4d2020-05-09 13:39:46 -070085Block
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080086requesttlv::encodeDataContent(const std::vector <uint8_t>& ecdhKey, const std::array<uint8_t, 32>& salt,
87 const RequestId& requestId,
88 const std::vector <std::string>& challenges)
Suyong Won19fba4d2020-05-09 13:39:46 -070089{
tylerliu1f480be2020-11-10 13:02:53 -080090 Block response(ndn::tlv::Content);
Davide Pesavento0dc02012021-11-23 22:55:03 -050091 response.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhKey.data(), ecdhKey.size()));
92 response.push_back(ndn::makeBinaryBlock(tlv::Salt, salt.data(), salt.size()));
93 response.push_back(ndn::makeBinaryBlock(tlv::RequestId, requestId.data(), requestId.size()));
Suyong Won19fba4d2020-05-09 13:39:46 -070094 for (const auto& entry: challenges) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050095 response.push_back(ndn::makeStringBlock(tlv::Challenge, entry));
Suyong Won19fba4d2020-05-09 13:39:46 -070096 }
Suyong Won44d0cce2020-05-10 04:07:43 -070097 response.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070098 return response;
99}
100
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800101std::list <std::string>
102requesttlv::decodeDataContent(const Block& content, std::vector <uint8_t>& ecdhKey,
Tianyuan Yu13aac732022-03-03 20:59:54 -0800103 std::array<uint8_t, 32>& salt, RequestId& requestId) {
104 std::list<std::string> challenges;
tylerliu0790bdb2020-10-02 00:50:51 -0700105 content.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -0800106 int ecdhPubCount = 0, saltCount = 0, requestIdCount = 0;
107 for (auto const &element : content.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700108 if (element.type() == tlv::Challenge) {
tylerliu0790bdb2020-10-02 00:50:51 -0700109 challenges.push_back(readString(element));
110 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800111 else if (element.type() == tlv::EcdhPub) {
112 ecdhKey.resize(element.value_size());
113 std::memcpy(ecdhKey.data(), element.value(), element.value_size());
114 ecdhPubCount++;
115 }
116 else if (element.type() == tlv::Salt) {
117 std::memcpy(salt.data(), element.value(), element.value_size());
118 saltCount++;
119 }
120 else if (element.type() == tlv::RequestId) {
121 std::memcpy(requestId.data(), element.value(), element.value_size());
122 requestIdCount++;
123 }
124 else if (ndn::tlv::isCriticalType(element.type())) {
125 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(element.type())));
126 }
127 else {
128 //ignore
129 }
130 }
131 if (ecdhPubCount != 1 || saltCount != 1 || requestIdCount != 1) {
132 NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(ecdhPubCount) + " ecdh public param(s), " +
133 std::to_string(saltCount) + " salt(s) and " + std::to_string(requestIdCount) +
134 "request id(s), instead of expected 1 times each."));
tylerliu0790bdb2020-10-02 00:50:51 -0700135 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700136 return challenges;
tylerliu0790bdb2020-10-02 00:50:51 -0700137}
138
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700139} // namespace ndncert