blob: da8b9699b552c70e19da17e56571f58691d80ea9 [file] [log] [blame]
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017-2020, Regents of the University of California.
4 *
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
21#include "info.hpp"
22
23namespace ndn {
24namespace ndncert {
25
26Block
Zhiyi Zhang9829da92020-09-30 16:19:34 -070027INFO::encodeDataContent(const CaConfigItem& caConfig, const security::v2::Certificate& certificate)
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070028{
29 auto content = makeEmptyBlock(tlv::Content);
30 content.push_back(makeNestedBlock(tlv_ca_prefix, caConfig.m_caPrefix));
Suyong Won19fba4d2020-05-09 13:39:46 -070031 std::string caInfo = "";
32 if (caConfig.m_caInfo == "") {
33 caInfo = "Issued by " + certificate.getSignature().getKeyLocator().getName().toUri();
Zhiyi Zhang9829da92020-09-30 16:19:34 -070034 }
35 else {
Suyong Won19fba4d2020-05-09 13:39:46 -070036 caInfo = caConfig.m_caInfo;
37 }
38 content.push_back(makeStringBlock(tlv_ca_info, caInfo));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070039 for (const auto& key : caConfig.m_probeParameterKeys) {
40 content.push_back(makeStringBlock(tlv_parameter_key, key));
41 }
42 content.push_back(makeNonNegativeIntegerBlock(tlv_max_validity_period, caConfig.m_maxValidityPeriod.count()));
43 content.push_back(makeNestedBlock(tlv_ca_certificate, certificate));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070044 if (caConfig.m_maxSuffixLength) {
45 content.push_back(makeNonNegativeIntegerBlock(tlv_max_suffix_length, *caConfig.m_maxSuffixLength));
46 }
Suyong Won44d0cce2020-05-10 04:07:43 -070047 content.encode();
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070048 return content;
49}
50
Zhiyi Zhang9829da92020-09-30 16:19:34 -070051CaConfigItem
52INFO::decodeDataContentToCaProfile(const Block& block)
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070053{
Zhiyi Zhang9829da92020-09-30 16:19:34 -070054 CaConfigItem result;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070055 block.parse();
56 for (auto const& item : block.elements()) {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070057 switch (item.type()) {
58 case tlv_ca_prefix:
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070059 item.parse();
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070060 result.m_caPrefix.wireDecode(item.get(tlv::Name));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070061 break;
62 case tlv_ca_info:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070063 result.m_caInfo = readString(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070064 break;
65 case tlv_parameter_key:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070066 result.m_probeParameterKeys.push_back(readString(item));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070067 break;
68 case tlv_max_validity_period:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070069 result.m_maxValidityPeriod = time::seconds(readNonNegativeInteger(item));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070070 break;
71 case tlv_max_suffix_length:
tylerliu0b6d0db2020-09-28 17:52:02 -070072 result.m_maxSuffixLength = readNonNegativeInteger(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070073 break;
74 case tlv_ca_certificate:
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070075 item.parse();
76 result.m_cert = std::make_shared<security::v2::Certificate>(item.get(tlv::Data));
Zhiyi Zhang9829da92020-09-30 16:19:34 -070077 break;
78 default:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070079 continue;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070080 break;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081 }
82 }
83 return result;
84}
85
86} // namespace ndncert
87} // namespace ndn