Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 1 | /* -*- 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 Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 21 | #include "new-renew-revoke-encoder.hpp" |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 22 | #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 Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 29 | NDN_LOG_INIT(ndncert.encoding.new_renew_revoke); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 30 | |
| 31 | Block |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 32 | NewRenewRevokeEncoder::encodeApplicationParameters(RequestType requestType, const std::string& ecdhPub, const security::Certificate& certRequest) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 33 | { |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 34 | Block request = makeEmptyBlock(ndn::tlv::ApplicationParameters); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 35 | std::stringstream ss; |
| 36 | try { |
| 37 | security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size()) |
| 38 | >> security::transform::base64Encode(false) |
| 39 | >> security::transform::streamSink(ss); |
| 40 | } |
| 41 | catch (const security::transform::Error& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 42 | NDN_LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what()); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 43 | return request; |
| 44 | } |
| 45 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 46 | request.push_back(makeStringBlock(tlv::EcdhPub, ecdhPub)); |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 47 | if (requestType == RequestType::NEW || requestType == RequestType::RENEW) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 48 | request.push_back(makeNestedBlock(tlv::CertRequest, certRequest)); |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 49 | } else if (requestType == RequestType::REVOKE) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 50 | request.push_back(makeNestedBlock(tlv::CertToRevoke, certRequest)); |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 51 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 52 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 53 | return request; |
| 54 | } |
| 55 | |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 56 | void |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 57 | NewRenewRevokeEncoder::decodeApplicationParameters(const Block& payload, RequestType requestType, std::string& ecdhPub, |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame^] | 58 | shared_ptr<security::Certificate>& clientCert) |
| 59 | { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 60 | payload.parse(); |
| 61 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 62 | ecdhPub = readString(payload.get(tlv::EcdhPub)); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 63 | Block requestPayload; |
| 64 | if (requestType == RequestType::NEW) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 65 | requestPayload = payload.get(tlv::CertRequest); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 66 | } |
| 67 | else if (requestType == RequestType::REVOKE) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 68 | requestPayload = payload.get(tlv::CertToRevoke); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 69 | } |
| 70 | requestPayload.parse(); |
| 71 | |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 72 | security::Certificate cert = security::Certificate(requestPayload.get(ndn::tlv::Data)); |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 73 | clientCert =std::make_shared<security::Certificate>(cert); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 76 | Block |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 77 | NewRenewRevokeEncoder::encodeDataContent(const std::string& ecdhKey, const std::string& salt, |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame^] | 78 | const CaState& request, |
| 79 | const std::list<std::string>& challenges) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 80 | { |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 81 | Block response = makeEmptyBlock(ndn::tlv::Content); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 82 | response.push_back(makeStringBlock(tlv::EcdhPub, ecdhKey)); |
| 83 | response.push_back(makeStringBlock(tlv::Salt, salt)); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame^] | 84 | response.push_back(makeBinaryBlock(tlv::RequestId, request.m_requestId.data(), request.m_requestId.size())); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 85 | response.push_back(makeNonNegativeIntegerBlock(tlv::Status, static_cast<size_t>(request.m_status))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 86 | for (const auto& entry: challenges) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 87 | response.push_back(makeStringBlock(tlv::Challenge, entry)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 88 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 89 | response.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 90 | return response; |
| 91 | } |
| 92 | |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 93 | NewRenewRevokeEncoder::DecodedData |
| 94 | NewRenewRevokeEncoder::decodeDataContent(const Block& content) |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 95 | { |
| 96 | content.parse(); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 97 | const auto& ecdhKey = readString(content.get(tlv::EcdhPub)); |
| 98 | const auto& salt = readString(content.get(tlv::Salt)); |
Zhiyi Zhang | 91f86ab | 2020-10-05 15:36:35 -0700 | [diff] [blame] | 99 | uint64_t saltInt = std::stoull(salt); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 100 | const auto& requestStatus = static_cast<Status>(readNonNegativeInteger(content.get(tlv::Status))); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame^] | 101 | RequestID requestId; |
| 102 | std::memcpy(requestId.data(), content.get(tlv::RequestId).value(), content.get(tlv::RequestId).size()); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 103 | std::list<std::string> challenges; |
| 104 | for (auto const& element : content.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 105 | if (element.type() == tlv::Challenge) { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 106 | challenges.push_back(readString(element)); |
| 107 | } |
| 108 | } |
Zhiyi Zhang | 91f86ab | 2020-10-05 15:36:35 -0700 | [diff] [blame] | 109 | return DecodedData{ecdhKey, saltInt, requestId, requestStatus, challenges}; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 112 | } // namespace ndncert |
| 113 | } // namespace ndn |