blob: 11872432900575a3f907cc6ca42d184d78d0a0b3 [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{
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040034 Block request(ndn::tlv::ApplicationParameters);
35 request.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhPub));
tylerliu4889a782020-09-30 22:47:49 -070036 if (requestType == RequestType::NEW || requestType == RequestType::RENEW) {
tylerliu50d679e2020-10-14 14:08:39 -070037 request.push_back(makeNestedBlock(tlv::CertRequest, certRequest));
tylerliu6563f932020-10-30 11:13:38 -070038 }
39 else if (requestType == RequestType::REVOKE) {
tylerliu50d679e2020-10-14 14:08:39 -070040 request.push_back(makeNestedBlock(tlv::CertToRevoke, certRequest));
tylerliu4889a782020-09-30 22:47:49 -070041 }
Suyong Won44d0cce2020-05-10 04:07:43 -070042 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070043 return request;
44}
45
tylerliu0790bdb2020-10-02 00:50:51 -070046void
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080047requesttlv::decodeApplicationParameters(const Block& payload, RequestType requestType,
Davide Pesavento0dc02012021-11-23 22:55:03 -050048 std::vector<uint8_t>& ecdhPub,
49 std::shared_ptr<Certificate>& clientCert)
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070050{
tylerliu0790bdb2020-10-02 00:50:51 -070051 payload.parse();
52
Tianyuan Yu13aac732022-03-03 20:59:54 -080053 int ecdhPubCount = 0;
tylerliu0790bdb2020-10-02 00:50:51 -070054 Block requestPayload;
Tianyuan Yu13aac732022-03-03 20:59:54 -080055 int requestPayloadCount = 0;
56 for (const auto &item : payload.elements()) {
57 if (item.type() == tlv::EcdhPub) {
58 ecdhPub.resize(item.value_size());
59 std::memcpy(ecdhPub.data(), item.value(), item.value_size());
60 ecdhPubCount++;
61 }
62 else if ((requestType == RequestType::NEW && item.type() == tlv::CertRequest) ||
63 (requestType == RequestType::REVOKE && item.type() == tlv::CertToRevoke)) {
64 requestPayload = item;
65 requestPayloadCount++;
66 requestPayload.parse();
67 clientCert = std::make_shared<Certificate>(requestPayload.get(ndn::tlv::Data));
68 }
69 else if (ndn::tlv::isCriticalType(item.type())) {
70 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
71 }
72 else {
73 //ignore
74 }
tylerliu0790bdb2020-10-02 00:50:51 -070075 }
tylerliu0790bdb2020-10-02 00:50:51 -070076
Tianyuan Yu13aac732022-03-03 20:59:54 -080077 if (ecdhPubCount != 1 || requestPayloadCount != 1) {
78 NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(ecdhPubCount) + " ecdh public param(s) and " +
79 std::to_string(requestPayloadCount) +
80 "request payload(s), instead of expected 1 times each."));
81 }
tylerliu0790bdb2020-10-02 00:50:51 -070082}
83
Suyong Won19fba4d2020-05-09 13:39:46 -070084Block
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040085requesttlv::encodeDataContent(const std::vector<uint8_t>& ecdhKey,
86 const std::array<uint8_t, 32>& salt,
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080087 const RequestId& requestId,
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040088 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 Pesavento6f1a2ab2022-03-17 03:57:21 -040091 response.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhKey));
92 response.push_back(ndn::makeBinaryBlock(tlv::Salt, salt));
93 response.push_back(ndn::makeBinaryBlock(tlv::RequestId, requestId));
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