blob: 6cae7ffb05b5749328dbecb7b2b7e462fa864fa4 [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
tylerliu4889a782020-09-30 22:47:49 -070021#include "new-renew-revoke.hpp"
Suyong Won19fba4d2020-05-09 13:39:46 -070022#include "../ndncert-common.hpp"
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>
26#include <ndn-cxx/util/logger.hpp>
27
28namespace ndn {
29namespace ndncert {
30
tylerliu0790bdb2020-10-02 00:50:51 -070031_LOG_INIT(ndncert.encoding.new_renew_revoke);
Suyong Won19fba4d2020-05-09 13:39:46 -070032
33Block
tylerliu4889a782020-09-30 22:47:49 -070034NEW_RENEW_REVOKE::encodeApplicationParameters(RequestType requestType, const std::string& ecdhPub, const security::v2::Certificate& certRequest)
Suyong Won19fba4d2020-05-09 13:39:46 -070035{
36 Block request = makeEmptyBlock(tlv::ApplicationParameters);
37 std::stringstream ss;
38 try {
39 security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size())
40 >> security::transform::base64Encode(false)
41 >> security::transform::streamSink(ss);
42 }
43 catch (const security::transform::Error& e) {
44 _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
45 return request;
46 }
47
48 request.push_back(makeStringBlock(tlv_ecdh_pub, ecdhPub));
tylerliu4889a782020-09-30 22:47:49 -070049 if (requestType == RequestType::NEW || requestType == RequestType::RENEW) {
50 request.push_back(makeNestedBlock(tlv_cert_request, certRequest));
51 } else if (requestType == RequestType::REVOKE) {
52 request.push_back(makeNestedBlock(tlv_cert_to_revoke, certRequest));
53 }
Suyong Won44d0cce2020-05-10 04:07:43 -070054 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070055 return request;
56}
57
tylerliu0790bdb2020-10-02 00:50:51 -070058void
59NEW_RENEW_REVOKE::decodeApplicationParameters(const Block& payload, RequestType requestType, std::string& ecdhPub,
60 shared_ptr<security::v2::Certificate>& clientCert) {
61 payload.parse();
62
63 ecdhPub = readString(payload.get(tlv_ecdh_pub));
64 Block requestPayload;
65 if (requestType == RequestType::NEW) {
66 requestPayload = payload.get(tlv_cert_request);
67 }
68 else if (requestType == RequestType::REVOKE) {
69 requestPayload = payload.get(tlv_cert_to_revoke);
70 }
71 requestPayload.parse();
72
73 security::v2::Certificate cert = security::v2::Certificate(requestPayload.get(tlv::Data));
74 clientCert = make_shared<security::v2::Certificate>(cert);
75}
76
Suyong Won19fba4d2020-05-09 13:39:46 -070077Block
tylerliu4889a782020-09-30 22:47:49 -070078NEW_RENEW_REVOKE::encodeDataContent(const std::string& ecdhKey, const std::string& salt,
79 const RequestState& request,
80 const std::list<std::string>& challenges)
Suyong Won19fba4d2020-05-09 13:39:46 -070081{
82 Block response = makeEmptyBlock(tlv::Content);
83 response.push_back(makeStringBlock(tlv_ecdh_pub, ecdhKey));
84 response.push_back(makeStringBlock(tlv_salt, salt));
85 response.push_back(makeStringBlock(tlv_request_id, request.m_requestId));
Zhiyi Zhang48f23782020-09-28 12:11:24 -070086 response.push_back(makeNonNegativeIntegerBlock(tlv_status, static_cast<size_t>(request.m_status)));
Suyong Won19fba4d2020-05-09 13:39:46 -070087 for (const auto& entry: challenges) {
88 response.push_back(makeStringBlock(tlv_challenge, entry));
89 }
Suyong Won44d0cce2020-05-10 04:07:43 -070090 response.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070091 return response;
92}
93
tylerliu0790bdb2020-10-02 00:50:51 -070094NEW_RENEW_REVOKE::DecodedData
95NEW_RENEW_REVOKE::decodeDataContent(const Block& content)
96{
97 content.parse();
98 const auto& ecdhKey = readString(content.get(tlv_ecdh_pub));
99 const auto& salt = readString(content.get(tlv_salt));
100 const auto& requestStatus = static_cast<Status>(readNonNegativeInteger(content.get(tlv_status)));
101 const auto& requestId = readString(content.get(tlv_request_id));
102 std::list<std::string> challenges;
103 for (auto const& element : content.elements()) {
104 if (element.type() == tlv_challenge) {
105 challenges.push_back(readString(element));
106 }
107 }
108 return DecodedData{ecdhKey, salt, requestId, requestStatus, challenges};
109}
110
Suyong Won19fba4d2020-05-09 13:39:46 -0700111} // namespace ndncert
112} // namespace ndn