blob: 6210eff024ef1ed1ba6fa2fea1294a28becc5b9c [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shia9181d72017-08-13 16:47:45 +00002/*
Alexander Afanasyev41469752017-01-10 21:51:55 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu4a557052014-07-09 16:40:37 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library 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 Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "signature-info.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070023#include "encoding/block-helpers.hpp"
Junxiao Shic2b8d242014-11-04 08:35:29 -070024#include "util/concepts.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070025
Yingdi Yu4a557052014-07-09 16:40:37 -070026namespace ndn {
27
Junxiao Shic2b8d242014-11-04 08:35:29 -070028BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
29BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070030BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070031BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
32static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value,
33 "SignatureInfo::Error must inherit from tlv::Error");
34
Yingdi Yu4a557052014-07-09 16:40:37 -070035SignatureInfo::SignatureInfo()
36 : m_type(-1)
37 , m_hasKeyLocator(false)
38{
39}
40
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060041SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070042 : m_type(type)
43 , m_hasKeyLocator(false)
44{
45}
46
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060047SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator)
Yingdi Yu4a557052014-07-09 16:40:37 -070048 : m_type(type)
49 , m_hasKeyLocator(true)
50 , m_keyLocator(keyLocator)
51{
52}
53
54SignatureInfo::SignatureInfo(const Block& block)
55{
56 wireDecode(block);
57}
58
Alexander Afanasyev74633892015-02-08 18:08:46 -080059template<encoding::Tag TAG>
Yingdi Yu4a557052014-07-09 16:40:37 -070060size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070061SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const
Yingdi Yu4a557052014-07-09 16:40:37 -070062{
Junxiao Shi605671d2017-08-26 13:41:06 +000063 if (m_type == -1) {
64 BOOST_THROW_EXCEPTION(Error("Cannot encode invalid SignatureInfo"));
65 }
66
Junxiao Shia9181d72017-08-13 16:47:45 +000067 // SignatureInfo ::= SIGNATURE-INFO-TLV TLV-LENGTH
68 // SignatureType
69 // KeyLocator?
70 // ValidityPeriod? (if present, stored as first item of m_otherTlvs)
71 // other SignatureType-specific sub-elements*
72
Yingdi Yu4a557052014-07-09 16:40:37 -070073 size_t totalLength = 0;
74
Junxiao Shia9181d72017-08-13 16:47:45 +000075 for (auto i = m_otherTlvs.rbegin(); i != m_otherTlvs.rend(); i++) {
Alexander Afanasyev41469752017-01-10 21:51:55 -080076 totalLength += encoder.prependBlock(*i);
Yingdi Yu4a557052014-07-09 16:40:37 -070077 }
78
79 if (m_hasKeyLocator)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070080 totalLength += m_keyLocator.wireEncode(encoder);
Yingdi Yu4a557052014-07-09 16:40:37 -070081
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040082 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType,
83 static_cast<uint64_t>(m_type));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070084 totalLength += encoder.prependVarNumber(totalLength);
85 totalLength += encoder.prependVarNumber(tlv::SignatureInfo);
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040086
Yingdi Yu4a557052014-07-09 16:40:37 -070087 return totalLength;
88}
89
Davide Pesavento88a0d812017-08-19 21:31:42 -040090NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SignatureInfo);
Yingdi Yu4a557052014-07-09 16:40:37 -070091
Yingdi Yu4a557052014-07-09 16:40:37 -070092const Block&
93SignatureInfo::wireEncode() const
94{
95 if (m_wire.hasWire())
96 return m_wire;
97
98 EncodingEstimator estimator;
99 size_t estimatedSize = wireEncode(estimator);
100
101 EncodingBuffer buffer(estimatedSize, 0);
102 wireEncode(buffer);
103
104 m_wire = buffer.block();
105 return m_wire;
106}
107
108void
109SignatureInfo::wireDecode(const Block& wire)
110{
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400111 m_type = -1;
Yingdi Yu4a557052014-07-09 16:40:37 -0700112 m_hasKeyLocator = false;
Junxiao Shia9181d72017-08-13 16:47:45 +0000113 m_otherTlvs.clear();
Yingdi Yu4a557052014-07-09 16:40:37 -0700114
115 m_wire = wire;
116 m_wire.parse();
117
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600118 if (m_wire.type() != tlv::SignatureInfo)
Junxiao Shia9181d72017-08-13 16:47:45 +0000119 BOOST_THROW_EXCEPTION(Error("Decoding SignatureInfo, but TLV-TYPE is " + to_string(m_wire.type())));
Yingdi Yu4a557052014-07-09 16:40:37 -0700120
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400121 auto it = m_wire.elements_begin();
Yingdi Yu4a557052014-07-09 16:40:37 -0700122
Junxiao Shi605671d2017-08-26 13:41:06 +0000123 // the first sub-element must be SignatureType
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400124 if (it == m_wire.elements_end() || it->type() != tlv::SignatureType)
Junxiao Shia9181d72017-08-13 16:47:45 +0000125 BOOST_THROW_EXCEPTION(Error("Missing SignatureType in SignatureInfo"));
Yingdi Yu4a557052014-07-09 16:40:37 -0700126
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400127 m_type = readNonNegativeIntegerAs<tlv::SignatureTypeValue>(*it);
128 ++it;
129
Junxiao Shi605671d2017-08-26 13:41:06 +0000130 // the second sub-element could be KeyLocator
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600131 if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700132 m_keyLocator.wireDecode(*it);
133 m_hasKeyLocator = true;
Junxiao Shia9181d72017-08-13 16:47:45 +0000134 ++it;
Yingdi Yu4a557052014-07-09 16:40:37 -0700135 }
136
Junxiao Shia9181d72017-08-13 16:47:45 +0000137 // store SignatureType-specific sub-elements, if any
Yingdi Yu4a557052014-07-09 16:40:37 -0700138 while (it != m_wire.elements_end()) {
Zhiyi Zhang387e57b2016-08-01 18:50:00 -0700139 m_otherTlvs.push_back(*it);
Junxiao Shia9181d72017-08-13 16:47:45 +0000140 ++it;
Yingdi Yu4a557052014-07-09 16:40:37 -0700141 }
142}
143
Junxiao Shia9181d72017-08-13 16:47:45 +0000144void
145SignatureInfo::setSignatureType(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -0700146{
Junxiao Shia9181d72017-08-13 16:47:45 +0000147 m_wire.reset();
148 m_type = type;
149}
150
151const KeyLocator&
152SignatureInfo::getKeyLocator() const
153{
154 if (m_hasKeyLocator)
155 return m_keyLocator;
156 else
157 BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist in SignatureInfo"));
158}
159
160void
161SignatureInfo::setKeyLocator(const KeyLocator& keyLocator)
162{
163 m_wire.reset();
164 m_keyLocator = keyLocator;
165 m_hasKeyLocator = true;
166}
167
168void
169SignatureInfo::unsetKeyLocator()
170{
171 m_wire.reset();
172 m_keyLocator = KeyLocator();
173 m_hasKeyLocator = false;
174}
175
176security::ValidityPeriod
177SignatureInfo::getValidityPeriod() const
178{
179 if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) {
180 BOOST_THROW_EXCEPTION(Error("ValidityPeriod does not exist in SignatureInfo"));
181 }
182
183 return security::ValidityPeriod(m_otherTlvs.front());
184}
185
186void
187SignatureInfo::setValidityPeriod(const security::ValidityPeriod& validityPeriod)
188{
189 unsetValidityPeriod();
190 m_otherTlvs.push_front(validityPeriod.wireEncode());
191}
192
193void
194SignatureInfo::unsetValidityPeriod()
195{
196 if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) {
197 m_otherTlvs.pop_front();
198 m_wire.reset();
199 }
200}
201
202const Block&
203SignatureInfo::getTypeSpecificTlv(uint32_t type) const
204{
205 for (const Block& block : m_otherTlvs) {
206 if (block.type() == type)
207 return block;
208 }
209
210 BOOST_THROW_EXCEPTION(Error("TLV-TYPE " + to_string(type) + " sub-element does not exist in SignatureInfo"));
211}
212
213void
214SignatureInfo::appendTypeSpecificTlv(const Block& block)
215{
216 m_wire.reset();
217 m_otherTlvs.push_back(block);
218}
219
220bool
221operator==(const SignatureInfo& lhs, const SignatureInfo& rhs)
222{
223 return lhs.m_type == rhs.m_type &&
224 lhs.m_hasKeyLocator == rhs.m_hasKeyLocator &&
225 lhs.m_keyLocator == rhs.m_keyLocator &&
226 lhs.m_otherTlvs == rhs.m_otherTlvs;
Yingdi Yu4a557052014-07-09 16:40:37 -0700227}
228
Alexander Afanasyev41469752017-01-10 21:51:55 -0800229std::ostream&
230operator<<(std::ostream& os, const SignatureInfo& info)
231{
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400232 if (info.getSignatureType() == -1)
233 return os << "Invalid SignatureInfo";
234
Alexander Afanasyev41469752017-01-10 21:51:55 -0800235 os << static_cast<tlv::SignatureTypeValue>(info.getSignatureType());
236 if (info.hasKeyLocator()) {
237 os << " " << info.getKeyLocator();
238 }
239 if (!info.m_otherTlvs.empty()) {
240 os << " { ";
241 for (const auto& block : info.m_otherTlvs) {
242 os << block.type() << " ";
243 }
244 os << "}";
245 }
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400246
Alexander Afanasyev41469752017-01-10 21:51:55 -0800247 return os;
248}
249
Yingdi Yu4a557052014-07-09 16:40:37 -0700250} // namespace ndn