blob: e6e65819fb9c8a2910a4cb0afc5b9ea8e794abcc [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/*
3 * Copyright (c) 2017-2021, 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
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070023namespace ndncert {
24
25Block
Davide Pesavento0dc02012021-11-23 22:55:03 -050026infotlv::encodeDataContent(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));
Suyong Won19fba4d2020-05-09 13:39:46 -070030 std::string caInfo = "";
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080031 if (caConfig.caInfo == "") {
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
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080048infotlv::decodeDataContent(const Block& block)
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070049{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070050 CaProfile result;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070051 block.parse();
52 for (auto const& item : block.elements()) {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070053 switch (item.type()) {
tylerliu50d679e2020-10-14 14:08:39 -070054 case tlv::CaPrefix:
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070055 item.parse();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080056 result.caPrefix.wireDecode(item.get(ndn::tlv::Name));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070057 break;
tylerliu50d679e2020-10-14 14:08:39 -070058 case tlv::CaInfo:
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080059 result.caInfo = readString(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070060 break;
tylerliu50d679e2020-10-14 14:08:39 -070061 case tlv::ParameterKey:
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080062 result.probeParameterKeys.push_back(readString(item));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070063 break;
tylerliu50d679e2020-10-14 14:08:39 -070064 case tlv::MaxValidityPeriod:
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080065 result.maxValidityPeriod = time::seconds(readNonNegativeInteger(item));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070066 break;
tylerliu50d679e2020-10-14 14:08:39 -070067 case tlv::CaCertificate:
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070068 item.parse();
Davide Pesavento0dc02012021-11-23 22:55:03 -050069 result.cert = std::make_shared<Certificate>(item.get(ndn::tlv::Data));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070070 break;
71 default:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070072 continue;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070073 break;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070074 }
75 }
76 return result;
77}
78
Zhiyi Zhange4891b72020-10-10 15:11:57 -070079} // namespace ndncert