blob: 9331f1f46a2b7b818bff21beba2a4d7ca56d340b [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
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070025namespace ndncert {
26
27Block
Davide Pesavento0dc02012021-11-23 22:55:03 -050028infotlv::encodeDataContent(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));
Suyong Won19fba4d2020-05-09 13:39:46 -070032 std::string caInfo = "";
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080033 if (caConfig.caInfo == "") {
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
Tianyuan Yu13aac732022-03-03 20:59:54 -080051infotlv::decodeDataContent(const Block& block) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070052 CaProfile result;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070053 block.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -080054 for (auto const &item : block.elements()) {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070055 switch (item.type()) {
Tianyuan Yu13aac732022-03-03 20:59:54 -080056 case tlv::CaPrefix:
57 item.parse();
58 result.caPrefix.wireDecode(item.get(ndn::tlv::Name));
59 break;
60 case tlv::CaInfo:
61 result.caInfo = readString(item);
62 break;
63 case tlv::ParameterKey:
64 result.probeParameterKeys.push_back(readString(item));
65 break;
66 case tlv::MaxValidityPeriod:
67 result.maxValidityPeriod = time::seconds(readNonNegativeInteger(item));
68 break;
69 case tlv::CaCertificate:
70 item.parse();
71 result.cert = std::make_shared<Certificate>(item.get(ndn::tlv::Data));
72 break;
73 default:
74 if (ndn::tlv::isCriticalType(item.type())) {
75 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
76 }
77 else {
78 //ignore
79 }
80 break;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081 }
82 }
83 return result;
84}
85
Zhiyi Zhange4891b72020-10-10 15:11:57 -070086} // namespace ndncert