blob: 30e252357e318c5dd4500d4aae6d428d4c4eb2df [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/*
3 * Copyright (c) 2017-2021, 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
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070054 const auto& ecdhBlock = payload.get(tlv::EcdhPub);
55 ecdhPub.resize(ecdhBlock.value_size());
56 std::memcpy(ecdhPub.data(), ecdhBlock.value(), ecdhBlock.value_size());
57
tylerliu0790bdb2020-10-02 00:50:51 -070058 Block requestPayload;
59 if (requestType == RequestType::NEW) {
tylerliu50d679e2020-10-14 14:08:39 -070060 requestPayload = payload.get(tlv::CertRequest);
tylerliu0790bdb2020-10-02 00:50:51 -070061 }
62 else if (requestType == RequestType::REVOKE) {
tylerliu50d679e2020-10-14 14:08:39 -070063 requestPayload = payload.get(tlv::CertToRevoke);
tylerliu0790bdb2020-10-02 00:50:51 -070064 }
65 requestPayload.parse();
66
Davide Pesavento0dc02012021-11-23 22:55:03 -050067 clientCert = std::make_shared<Certificate>(requestPayload.get(ndn::tlv::Data));
tylerliu0790bdb2020-10-02 00:50:51 -070068}
69
Suyong Won19fba4d2020-05-09 13:39:46 -070070Block
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080071requesttlv::encodeDataContent(const std::vector <uint8_t>& ecdhKey, const std::array<uint8_t, 32>& salt,
72 const RequestId& requestId,
73 const std::vector <std::string>& challenges)
Suyong Won19fba4d2020-05-09 13:39:46 -070074{
tylerliu1f480be2020-11-10 13:02:53 -080075 Block response(ndn::tlv::Content);
Davide Pesavento0dc02012021-11-23 22:55:03 -050076 response.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhKey.data(), ecdhKey.size()));
77 response.push_back(ndn::makeBinaryBlock(tlv::Salt, salt.data(), salt.size()));
78 response.push_back(ndn::makeBinaryBlock(tlv::RequestId, requestId.data(), requestId.size()));
Suyong Won19fba4d2020-05-09 13:39:46 -070079 for (const auto& entry: challenges) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050080 response.push_back(ndn::makeStringBlock(tlv::Challenge, entry));
Suyong Won19fba4d2020-05-09 13:39:46 -070081 }
Suyong Won44d0cce2020-05-10 04:07:43 -070082 response.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070083 return response;
84}
85
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080086std::list <std::string>
87requesttlv::decodeDataContent(const Block& content, std::vector <uint8_t>& ecdhKey,
88 std::array<uint8_t, 32>& salt, RequestId& requestId)
tylerliu0790bdb2020-10-02 00:50:51 -070089{
90 content.parse();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070091
92 const auto& ecdhBlock = content.get(tlv::EcdhPub);
93 ecdhKey.resize(ecdhBlock.value_size());
94 std::memcpy(ecdhKey.data(), ecdhBlock.value(), ecdhBlock.value_size());
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -070095
96 const auto& saltBlock = content.get(tlv::Salt);
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -070097 std::memcpy(salt.data(), saltBlock.value(), saltBlock.value_size());
98
99 const auto& requestIdBlock = content.get(tlv::RequestId);
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -0700100 std::memcpy(requestId.data(), requestIdBlock.value(), requestIdBlock.value_size());
101
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800102 std::list <std::string> challenges;
tylerliu0790bdb2020-10-02 00:50:51 -0700103 for (auto const& element : content.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700104 if (element.type() == tlv::Challenge) {
tylerliu0790bdb2020-10-02 00:50:51 -0700105 challenges.push_back(readString(element));
106 }
107 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700108 return challenges;
tylerliu0790bdb2020-10-02 00:50:51 -0700109}
110
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700111} // namespace ndncert