blob: 4c260b43920388dbb1191d4c09b3c7873c722e75 [file] [log] [blame]
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Davide Pesavento9510c912024-02-25 17:50:05 -05003 * Copyright (c) 2017-2024, Regents of the University of California.
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -07004 *
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 Zhangb041d442020-10-22 21:57:11 -070021#include "detail/info-encoder.hpp"
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070022
Davide Pesavento9510c912024-02-25 17:50:05 -050023#include <ndn-cxx/util/logger.hpp>
Tianyuan Yu13aac732022-03-03 20:59:54 -080024
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025namespace ndncert::infotlv {
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070026
Davide Pesavento9510c912024-02-25 17:50:05 -050027NDN_LOG_INIT(ndncert.encode.info);
28
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070029Block
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040030encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070031{
tylerliu1f480be2020-11-10 13:02:53 -080032 Block content(ndn::tlv::Content);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080033 content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.caPrefix));
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040034 std::string caInfo;
35 if (caConfig.caInfo.empty()) {
tylerliua7bea662020-10-08 18:51:02 -070036 caInfo = "Issued by " + certificate.getSignatureInfo().getKeyLocator().getName().toUri();
Zhiyi Zhang9829da92020-09-30 16:19:34 -070037 }
38 else {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080039 caInfo = caConfig.caInfo;
Suyong Won19fba4d2020-05-09 13:39:46 -070040 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050041 content.push_back(ndn::makeStringBlock(tlv::CaInfo, caInfo));
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080042 for (const auto& key : caConfig.probeParameterKeys) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050043 content.push_back(ndn::makeStringBlock(tlv::ParameterKey, key));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070044 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050045 content.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxValidityPeriod, caConfig.maxValidityPeriod.count()));
tylerliu50d679e2020-10-14 14:08:39 -070046 content.push_back(makeNestedBlock(tlv::CaCertificate, certificate));
Suyong Won44d0cce2020-05-10 04:07:43 -070047 content.encode();
Tianyuan Yu13aac732022-03-03 20:59:54 -080048 NDN_LOG_TRACE("Encoding INFO packet with certificate " << certificate.getFullName());
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070049 return content;
50}
51
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070052CaProfile
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040053decodeDataContent(const Block& block)
54{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070055 CaProfile result;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070056 block.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -080057 for (auto const &item : block.elements()) {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070058 switch (item.type()) {
Tianyuan Yu13aac732022-03-03 20:59:54 -080059 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 Yu13aac732022-03-03 20:59:54 -080080 break;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081 }
82 }
83 return result;
84}
85
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040086} // namespace ndncert::infotlv