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 | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/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 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 29 | Block |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 30 | requesttlv::encodeApplicationParameters(RequestType requestType, const std::vector <uint8_t>& ecdhPub, |
| 31 | const security::Certificate& certRequest) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 32 | { |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 33 | Block |
| 34 | request(ndn::tlv::ApplicationParameters); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 35 | request.push_back(makeBinaryBlock(tlv::EcdhPub, ecdhPub.data(), ecdhPub.size())); |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 36 | if (requestType == RequestType::NEW || requestType == RequestType::RENEW) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 37 | request.push_back(makeNestedBlock(tlv::CertRequest, certRequest)); |
tylerliu | 6563f93 | 2020-10-30 11:13:38 -0700 | [diff] [blame] | 38 | } |
| 39 | else if (requestType == RequestType::REVOKE) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 40 | request.push_back(makeNestedBlock(tlv::CertToRevoke, certRequest)); |
tylerliu | 4889a78 | 2020-09-30 22:47:49 -0700 | [diff] [blame] | 41 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 42 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 43 | return request; |
| 44 | } |
| 45 | |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 46 | void |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 47 | requesttlv::decodeApplicationParameters(const Block& payload, RequestType requestType, |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 48 | std::vector <uint8_t>& ecdhPub, |
| 49 | shared_ptr <security::Certificate>& clientCert) |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 50 | { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 51 | payload.parse(); |
| 52 | |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 53 | const auto& ecdhBlock = payload.get(tlv::EcdhPub); |
| 54 | ecdhPub.resize(ecdhBlock.value_size()); |
| 55 | std::memcpy(ecdhPub.data(), ecdhBlock.value(), ecdhBlock.value_size()); |
| 56 | |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 57 | Block requestPayload; |
| 58 | if (requestType == RequestType::NEW) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 59 | requestPayload = payload.get(tlv::CertRequest); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 60 | } |
| 61 | else if (requestType == RequestType::REVOKE) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 62 | requestPayload = payload.get(tlv::CertToRevoke); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 63 | } |
| 64 | requestPayload.parse(); |
| 65 | |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 66 | security::Certificate cert = security::Certificate(requestPayload.get(ndn::tlv::Data)); |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 67 | clientCert = std::make_shared<security::Certificate>(cert); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 70 | Block |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 71 | requesttlv::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 Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 74 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 75 | Block response(ndn::tlv::Content); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 76 | response.push_back(makeBinaryBlock(tlv::EcdhPub, ecdhKey.data(), ecdhKey.size())); |
Zhiyi Zhang | c9f9fac | 2020-10-18 19:51:51 -0700 | [diff] [blame] | 77 | response.push_back(makeBinaryBlock(tlv::Salt, salt.data(), salt.size())); |
Zhiyi Zhang | 121aae6 | 2020-10-26 13:44:33 -0700 | [diff] [blame] | 78 | response.push_back(makeBinaryBlock(tlv::RequestId, requestId.data(), requestId.size())); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 79 | for (const auto& entry: challenges) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 80 | response.push_back(makeStringBlock(tlv::Challenge, entry)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 81 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 82 | response.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 83 | return response; |
| 84 | } |
| 85 | |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 86 | std::list <std::string> |
| 87 | requesttlv::decodeDataContent(const Block& content, std::vector <uint8_t>& ecdhKey, |
| 88 | std::array<uint8_t, 32>& salt, RequestId& requestId) |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 89 | { |
| 90 | content.parse(); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 91 | |
| 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 Zhang | c9f9fac | 2020-10-18 19:51:51 -0700 | [diff] [blame] | 95 | |
| 96 | const auto& saltBlock = content.get(tlv::Salt); |
Zhiyi Zhang | c9f9fac | 2020-10-18 19:51:51 -0700 | [diff] [blame] | 97 | std::memcpy(salt.data(), saltBlock.value(), saltBlock.value_size()); |
| 98 | |
| 99 | const auto& requestIdBlock = content.get(tlv::RequestId); |
Zhiyi Zhang | c9f9fac | 2020-10-18 19:51:51 -0700 | [diff] [blame] | 100 | std::memcpy(requestId.data(), requestIdBlock.value(), requestIdBlock.value_size()); |
| 101 | |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame^] | 102 | std::list <std::string> challenges; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 103 | for (auto const& element : content.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 104 | if (element.type() == tlv::Challenge) { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 105 | challenges.push_back(readString(element)); |
| 106 | } |
| 107 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 108 | return challenges; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 111 | } // namespace ndncert |
| 112 | } // namespace ndn |