Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -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 | /* |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2017-2024, Regents of the University of California. |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -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 | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/info-encoder.hpp" |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame^] | 23 | #include <ndn-cxx/util/logger.hpp> |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 25 | namespace ndncert::infotlv { |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame^] | 27 | NDN_LOG_INIT(ndncert.encode.info); |
| 28 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 29 | Block |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 30 | encodeDataContent(const CaProfile& caConfig, const Certificate& certificate) |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 31 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 32 | Block content(ndn::tlv::Content); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 33 | content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.caPrefix)); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 34 | std::string caInfo; |
| 35 | if (caConfig.caInfo.empty()) { |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 36 | caInfo = "Issued by " + certificate.getSignatureInfo().getKeyLocator().getName().toUri(); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 37 | } |
| 38 | else { |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 39 | caInfo = caConfig.caInfo; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 40 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 41 | content.push_back(ndn::makeStringBlock(tlv::CaInfo, caInfo)); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 42 | for (const auto& key : caConfig.probeParameterKeys) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 43 | content.push_back(ndn::makeStringBlock(tlv::ParameterKey, key)); |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 44 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 45 | content.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxValidityPeriod, caConfig.maxValidityPeriod.count())); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 46 | content.push_back(makeNestedBlock(tlv::CaCertificate, certificate)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 47 | content.encode(); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 48 | NDN_LOG_TRACE("Encoding INFO packet with certificate " << certificate.getFullName()); |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 49 | return content; |
| 50 | } |
| 51 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 52 | CaProfile |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 53 | decodeDataContent(const Block& block) |
| 54 | { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 55 | CaProfile result; |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 56 | block.parse(); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 57 | for (auto const &item : block.elements()) { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 58 | switch (item.type()) { |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 59 | case tlv::CaPrefix: |
| 60 | item.parse(); |
| 61 | result.caPrefix.wireDecode(item.get(ndn::tlv::Name)); |
| 62 | break; |
| 63 | case tlv::CaInfo: |
| 64 | result.caInfo = readString(item); |
| 65 | break; |
| 66 | case tlv::ParameterKey: |
| 67 | result.probeParameterKeys.push_back(readString(item)); |
| 68 | break; |
| 69 | case tlv::MaxValidityPeriod: |
| 70 | result.maxValidityPeriod = time::seconds(readNonNegativeInteger(item)); |
| 71 | break; |
| 72 | case tlv::CaCertificate: |
| 73 | item.parse(); |
| 74 | result.cert = std::make_shared<Certificate>(item.get(ndn::tlv::Data)); |
| 75 | break; |
| 76 | default: |
| 77 | if (ndn::tlv::isCriticalType(item.type())) { |
| 78 | NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type()))); |
| 79 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 80 | break; |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 86 | } // namespace ndncert::infotlv |