blob: 2d327ba800dc08efb4a033b21e1b4f1c154a0450 [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 Pesavento0d1d11c2022-04-11 22:11:34 -040023namespace ndncert::infotlv {
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070024
25Block
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070027{
tylerliu1f480be2020-11-10 13:02:53 -080028 Block content(ndn::tlv::Content);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080029 content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.caPrefix));
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040030 std::string caInfo;
31 if (caConfig.caInfo.empty()) {
tylerliua7bea662020-10-08 18:51:02 -070032 caInfo = "Issued by " + certificate.getSignatureInfo().getKeyLocator().getName().toUri();
Zhiyi Zhang9829da92020-09-30 16:19:34 -070033 }
34 else {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080035 caInfo = caConfig.caInfo;
Suyong Won19fba4d2020-05-09 13:39:46 -070036 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050037 content.push_back(ndn::makeStringBlock(tlv::CaInfo, caInfo));
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080038 for (const auto& key : caConfig.probeParameterKeys) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050039 content.push_back(ndn::makeStringBlock(tlv::ParameterKey, key));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070040 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050041 content.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxValidityPeriod, caConfig.maxValidityPeriod.count()));
tylerliu50d679e2020-10-14 14:08:39 -070042 content.push_back(makeNestedBlock(tlv::CaCertificate, certificate));
Suyong Won44d0cce2020-05-10 04:07:43 -070043 content.encode();
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070044 return content;
45}
46
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070047CaProfile
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040048decodeDataContent(const Block& block)
49{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070050 CaProfile result;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070051 block.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -080052 for (auto const &item : block.elements()) {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070053 switch (item.type()) {
Tianyuan Yu13aac732022-03-03 20:59:54 -080054 case tlv::CaPrefix:
55 item.parse();
56 result.caPrefix.wireDecode(item.get(ndn::tlv::Name));
57 break;
58 case tlv::CaInfo:
59 result.caInfo = readString(item);
60 break;
61 case tlv::ParameterKey:
62 result.probeParameterKeys.push_back(readString(item));
63 break;
64 case tlv::MaxValidityPeriod:
65 result.maxValidityPeriod = time::seconds(readNonNegativeInteger(item));
66 break;
67 case tlv::CaCertificate:
68 item.parse();
69 result.cert = std::make_shared<Certificate>(item.get(ndn::tlv::Data));
70 break;
71 default:
72 if (ndn::tlv::isCriticalType(item.type())) {
73 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
74 }
Tianyuan Yu13aac732022-03-03 20:59:54 -080075 break;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070076 }
77 }
78 return result;
79}
80
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040081} // namespace ndncert::infotlv