Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 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 | 7cca76a | 2021-02-17 14:57:42 -0800 | [diff] [blame] | 21 | #include "detail/request-encoder.hpp" |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 22 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 23 | #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 Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 26 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 27 | namespace ndncert { |
| 28 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 29 | Block |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 30 | requesttlv::encodeApplicationParameters(RequestType requestType, |
| 31 | const std::vector<uint8_t>& ecdhPub, |
| 32 | const Certificate& certRequest) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 33 | { |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 34 | Block request(ndn::tlv::ApplicationParameters); |
| 35 | request.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhPub)); |
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, |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 48 | std::vector<uint8_t>& ecdhPub, |
| 49 | std::shared_ptr<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 | |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 53 | int ecdhPubCount = 0; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 54 | Block requestPayload; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 55 | int requestPayloadCount = 0; |
| 56 | for (const auto &item : payload.elements()) { |
| 57 | if (item.type() == tlv::EcdhPub) { |
| 58 | ecdhPub.resize(item.value_size()); |
| 59 | std::memcpy(ecdhPub.data(), item.value(), item.value_size()); |
| 60 | ecdhPubCount++; |
| 61 | } |
| 62 | else if ((requestType == RequestType::NEW && item.type() == tlv::CertRequest) || |
| 63 | (requestType == RequestType::REVOKE && item.type() == tlv::CertToRevoke)) { |
| 64 | requestPayload = item; |
| 65 | requestPayloadCount++; |
| 66 | requestPayload.parse(); |
| 67 | clientCert = std::make_shared<Certificate>(requestPayload.get(ndn::tlv::Data)); |
| 68 | } |
| 69 | else if (ndn::tlv::isCriticalType(item.type())) { |
| 70 | NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type()))); |
| 71 | } |
| 72 | else { |
| 73 | //ignore |
| 74 | } |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 75 | } |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 76 | |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 77 | if (ecdhPubCount != 1 || requestPayloadCount != 1) { |
| 78 | NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(ecdhPubCount) + " ecdh public param(s) and " + |
| 79 | std::to_string(requestPayloadCount) + |
| 80 | "request payload(s), instead of expected 1 times each.")); |
| 81 | } |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 84 | Block |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 85 | requesttlv::encodeDataContent(const std::vector<uint8_t>& ecdhKey, |
| 86 | const std::array<uint8_t, 32>& salt, |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame] | 87 | const RequestId& requestId, |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 88 | const std::vector<std::string>& challenges) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 89 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 90 | Block response(ndn::tlv::Content); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 91 | response.push_back(ndn::makeBinaryBlock(tlv::EcdhPub, ecdhKey)); |
| 92 | response.push_back(ndn::makeBinaryBlock(tlv::Salt, salt)); |
| 93 | response.push_back(ndn::makeBinaryBlock(tlv::RequestId, requestId)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 94 | for (const auto& entry: challenges) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 95 | response.push_back(ndn::makeStringBlock(tlv::Challenge, entry)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 96 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 97 | response.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 98 | return response; |
| 99 | } |
| 100 | |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame] | 101 | std::list <std::string> |
| 102 | requesttlv::decodeDataContent(const Block& content, std::vector <uint8_t>& ecdhKey, |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 103 | std::array<uint8_t, 32>& salt, RequestId& requestId) { |
| 104 | std::list<std::string> challenges; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 105 | content.parse(); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 106 | int ecdhPubCount = 0, saltCount = 0, requestIdCount = 0; |
| 107 | for (auto const &element : content.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 108 | if (element.type() == tlv::Challenge) { |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 109 | challenges.push_back(readString(element)); |
| 110 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 111 | else if (element.type() == tlv::EcdhPub) { |
| 112 | ecdhKey.resize(element.value_size()); |
| 113 | std::memcpy(ecdhKey.data(), element.value(), element.value_size()); |
| 114 | ecdhPubCount++; |
| 115 | } |
| 116 | else if (element.type() == tlv::Salt) { |
| 117 | std::memcpy(salt.data(), element.value(), element.value_size()); |
| 118 | saltCount++; |
| 119 | } |
| 120 | else if (element.type() == tlv::RequestId) { |
| 121 | std::memcpy(requestId.data(), element.value(), element.value_size()); |
| 122 | requestIdCount++; |
| 123 | } |
| 124 | else if (ndn::tlv::isCriticalType(element.type())) { |
| 125 | NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(element.type()))); |
| 126 | } |
| 127 | else { |
| 128 | //ignore |
| 129 | } |
| 130 | } |
| 131 | if (ecdhPubCount != 1 || saltCount != 1 || requestIdCount != 1) { |
| 132 | NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(ecdhPubCount) + " ecdh public param(s), " + |
| 133 | std::to_string(saltCount) + " salt(s) and " + std::to_string(requestIdCount) + |
| 134 | "request id(s), instead of expected 1 times each.")); |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 135 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 136 | return challenges; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 139 | } // namespace ndncert |