blob: 2a9df67b3c21a61a52043c63fa26cba229731ff8 [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/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, 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
Tianyuan Yu13aac732022-03-03 20:59:54 -080023NDN_LOG_INIT(ndncert.encode.info);
24
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025namespace ndncert::infotlv {
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070026
27Block
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040028encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070029{
tylerliu1f480be2020-11-10 13:02:53 -080030 Block content(ndn::tlv::Content);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080031 content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.caPrefix));
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040032 std::string caInfo;
33 if (caConfig.caInfo.empty()) {
tylerliua7bea662020-10-08 18:51:02 -070034 caInfo = "Issued by " + certificate.getSignatureInfo().getKeyLocator().getName().toUri();
Zhiyi Zhang9829da92020-09-30 16:19:34 -070035 }
36 else {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080037 caInfo = caConfig.caInfo;
Suyong Won19fba4d2020-05-09 13:39:46 -070038 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050039 content.push_back(ndn::makeStringBlock(tlv::CaInfo, caInfo));
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080040 for (const auto& key : caConfig.probeParameterKeys) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050041 content.push_back(ndn::makeStringBlock(tlv::ParameterKey, key));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070042 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050043 content.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxValidityPeriod, caConfig.maxValidityPeriod.count()));
tylerliu50d679e2020-10-14 14:08:39 -070044 content.push_back(makeNestedBlock(tlv::CaCertificate, certificate));
Suyong Won44d0cce2020-05-10 04:07:43 -070045 content.encode();
Tianyuan Yu13aac732022-03-03 20:59:54 -080046 NDN_LOG_TRACE("Encoding INFO packet with certificate " << certificate.getFullName());
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070047 return content;
48}
49
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070050CaProfile
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040051decodeDataContent(const Block& block)
52{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070053 CaProfile result;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070054 block.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -080055 for (auto const &item : block.elements()) {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070056 switch (item.type()) {
Tianyuan Yu13aac732022-03-03 20:59:54 -080057 case tlv::CaPrefix:
58 item.parse();
59 result.caPrefix.wireDecode(item.get(ndn::tlv::Name));
60 break;
61 case tlv::CaInfo:
62 result.caInfo = readString(item);
63 break;
64 case tlv::ParameterKey:
65 result.probeParameterKeys.push_back(readString(item));
66 break;
67 case tlv::MaxValidityPeriod:
68 result.maxValidityPeriod = time::seconds(readNonNegativeInteger(item));
69 break;
70 case tlv::CaCertificate:
71 item.parse();
72 result.cert = std::make_shared<Certificate>(item.get(ndn::tlv::Data));
73 break;
74 default:
75 if (ndn::tlv::isCriticalType(item.type())) {
76 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
77 }
Tianyuan Yu13aac732022-03-03 20:59:54 -080078 break;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070079 }
80 }
81 return result;
82}
83
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040084} // namespace ndncert::infotlv