blob: 043bb836021e28e383fb6e31e8f97aec0e89876d [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/new-renew-revoke-encoder.hpp"
Suyong Won19fba4d2020-05-09 13:39:46 -070022#include <ndn-cxx/security/transform/base64-encode.hpp>
23#include <ndn-cxx/security/transform/buffer-source.hpp>
24#include <ndn-cxx/security/transform/stream-sink.hpp>
Suyong Won19fba4d2020-05-09 13:39:46 -070025
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070029NDN_LOG_INIT(ndncert.encoding.new_renew_revoke);
Suyong Won19fba4d2020-05-09 13:39:46 -070030
31Block
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070032NewRenewRevokeEncoder::encodeApplicationParameters(RequestType requestType, const std::vector<uint8_t>& ecdhPub,
33 const security::Certificate& certRequest)
Suyong Won19fba4d2020-05-09 13:39:46 -070034{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070035 Block request = makeEmptyBlock(ndn::tlv::ApplicationParameters);
Suyong Won19fba4d2020-05-09 13:39:46 -070036 std::stringstream ss;
37 try {
38 security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size())
39 >> security::transform::base64Encode(false)
40 >> security::transform::streamSink(ss);
41 }
42 catch (const security::transform::Error& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070043 NDN_LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
Suyong Won19fba4d2020-05-09 13:39:46 -070044 return request;
45 }
46
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070047 request.push_back(makeBinaryBlock(tlv::EcdhPub, ecdhPub.data(), ecdhPub.size()));
tylerliu4889a782020-09-30 22:47:49 -070048 if (requestType == RequestType::NEW || requestType == RequestType::RENEW) {
tylerliu50d679e2020-10-14 14:08:39 -070049 request.push_back(makeNestedBlock(tlv::CertRequest, certRequest));
tylerliu4889a782020-09-30 22:47:49 -070050 } else if (requestType == RequestType::REVOKE) {
tylerliu50d679e2020-10-14 14:08:39 -070051 request.push_back(makeNestedBlock(tlv::CertToRevoke, certRequest));
tylerliu4889a782020-09-30 22:47:49 -070052 }
Suyong Won44d0cce2020-05-10 04:07:43 -070053 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070054 return request;
55}
56
tylerliu0790bdb2020-10-02 00:50:51 -070057void
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070058NewRenewRevokeEncoder::decodeApplicationParameters(const Block& payload, RequestType requestType,
59 std::vector<uint8_t>& ecdhPub,
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070060 shared_ptr<security::Certificate>& clientCert)
61{
tylerliu0790bdb2020-10-02 00:50:51 -070062 payload.parse();
63
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070064 const auto& ecdhBlock = payload.get(tlv::EcdhPub);
65 ecdhPub.resize(ecdhBlock.value_size());
66 std::memcpy(ecdhPub.data(), ecdhBlock.value(), ecdhBlock.value_size());
67
tylerliu0790bdb2020-10-02 00:50:51 -070068 Block requestPayload;
69 if (requestType == RequestType::NEW) {
tylerliu50d679e2020-10-14 14:08:39 -070070 requestPayload = payload.get(tlv::CertRequest);
tylerliu0790bdb2020-10-02 00:50:51 -070071 }
72 else if (requestType == RequestType::REVOKE) {
tylerliu50d679e2020-10-14 14:08:39 -070073 requestPayload = payload.get(tlv::CertToRevoke);
tylerliu0790bdb2020-10-02 00:50:51 -070074 }
75 requestPayload.parse();
76
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070077 security::Certificate cert = security::Certificate(requestPayload.get(ndn::tlv::Data));
Zhiyi Zhang32437282020-10-10 16:15:37 -070078 clientCert =std::make_shared<security::Certificate>(cert);
tylerliu0790bdb2020-10-02 00:50:51 -070079}
80
Suyong Won19fba4d2020-05-09 13:39:46 -070081Block
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070082NewRenewRevokeEncoder::encodeDataContent(const std::vector<uint8_t>& ecdhKey, const std::array<uint8_t, 32>& salt,
Zhiyi Zhang121aae62020-10-26 13:44:33 -070083 const RequestID& requestId, const Status& status,
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070084 const std::list<std::string>& challenges)
Suyong Won19fba4d2020-05-09 13:39:46 -070085{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070086 Block response = makeEmptyBlock(ndn::tlv::Content);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070087 response.push_back(makeBinaryBlock(tlv::EcdhPub, ecdhKey.data(), ecdhKey.size()));
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -070088 response.push_back(makeBinaryBlock(tlv::Salt, salt.data(), salt.size()));
Zhiyi Zhang121aae62020-10-26 13:44:33 -070089 response.push_back(makeBinaryBlock(tlv::RequestId, requestId.data(), requestId.size()));
90 response.push_back(makeNonNegativeIntegerBlock(tlv::Status, static_cast<size_t>(status)));
Suyong Won19fba4d2020-05-09 13:39:46 -070091 for (const auto& entry: challenges) {
tylerliu50d679e2020-10-14 14:08:39 -070092 response.push_back(makeStringBlock(tlv::Challenge, entry));
Suyong Won19fba4d2020-05-09 13:39:46 -070093 }
Suyong Won44d0cce2020-05-10 04:07:43 -070094 response.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070095 return response;
96}
97
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070098std::list<std::string>
99NewRenewRevokeEncoder::decodeDataContent(const Block& content, std::vector<uint8_t>& ecdhKey,
100 std::array<uint8_t, 32>& salt, RequestID& requestId, Status& status)
tylerliu0790bdb2020-10-02 00:50:51 -0700101{
102 content.parse();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700103 status = static_cast<Status>(readNonNegativeInteger(content.get(tlv::Status)));
104
105 const auto& ecdhBlock = content.get(tlv::EcdhPub);
106 ecdhKey.resize(ecdhBlock.value_size());
107 std::memcpy(ecdhKey.data(), ecdhBlock.value(), ecdhBlock.value_size());
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -0700108
109 const auto& saltBlock = content.get(tlv::Salt);
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -0700110 std::memcpy(salt.data(), saltBlock.value(), saltBlock.value_size());
111
112 const auto& requestIdBlock = content.get(tlv::RequestId);
Zhiyi Zhangc9f9fac2020-10-18 19:51:51 -0700113 std::memcpy(requestId.data(), requestIdBlock.value(), requestIdBlock.value_size());
114
tylerliu0790bdb2020-10-02 00:50:51 -0700115 std::list<std::string> challenges;
116 for (auto const& element : content.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -0700117 if (element.type() == tlv::Challenge) {
tylerliu0790bdb2020-10-02 00:50:51 -0700118 challenges.push_back(readString(element));
119 }
120 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700121 return challenges;
tylerliu0790bdb2020-10-02 00:50:51 -0700122}
123
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700124} // namespace ndncert
125} // namespace ndn