blob: d96aab3c0d697e4e8bc95b056e314764edc975a2 [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev74633892015-02-08 18:08:46 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -070026#include <boost/lexical_cast.hpp>
Yingdi Yu4a557052014-07-09 16:40:37 -070027
28namespace ndn {
29
Junxiao Shic2b8d242014-11-04 08:35:29 -070030BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
31BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070032BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070033BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
34static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value,
35 "SignatureInfo::Error must inherit from tlv::Error");
36
Yingdi Yu4a557052014-07-09 16:40:37 -070037SignatureInfo::SignatureInfo()
38 : m_type(-1)
39 , m_hasKeyLocator(false)
40{
41}
42
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060043SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070044 : m_type(type)
45 , m_hasKeyLocator(false)
46{
47}
48
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060049SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator)
Yingdi Yu4a557052014-07-09 16:40:37 -070050 : m_type(type)
51 , m_hasKeyLocator(true)
52 , m_keyLocator(keyLocator)
53{
54}
55
56SignatureInfo::SignatureInfo(const Block& block)
57{
58 wireDecode(block);
59}
60
61void
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060062SignatureInfo::setSignatureType(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070063{
64 m_wire.reset();
65 m_type = type;
66}
67
68void
69SignatureInfo::setKeyLocator(const KeyLocator& keyLocator)
70{
71 m_wire.reset();
72 m_keyLocator = keyLocator;
73 m_hasKeyLocator = true;
74}
75
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -070076void
77SignatureInfo::unsetKeyLocator()
78{
79 m_wire.reset();
80 m_keyLocator = KeyLocator();
81 m_hasKeyLocator = false;
82}
83
Yingdi Yu4a557052014-07-09 16:40:37 -070084const KeyLocator&
85SignatureInfo::getKeyLocator() const
86{
87 if (m_hasKeyLocator)
88 return m_keyLocator;
89 else
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070090 BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist"));
Yingdi Yu4a557052014-07-09 16:40:37 -070091}
92
93void
Yingdi Yu6be43f32015-06-09 14:19:54 -070094SignatureInfo::setValidityPeriod(const security::ValidityPeriod& validityPeriod)
95{
96 unsetValidityPeriod();
97 m_otherTlvs.push_front(validityPeriod.wireEncode());
98}
99
100void
101SignatureInfo::unsetValidityPeriod()
102{
103 m_wire.reset();
104 if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) {
105 m_otherTlvs.erase(m_otherTlvs.begin());
106 }
107}
108
109security::ValidityPeriod
110SignatureInfo::getValidityPeriod() const
111{
112 if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700113 BOOST_THROW_EXCEPTION(Error("SignatureInfo does not contain the requested ValidityPeriod field"));
Yingdi Yu6be43f32015-06-09 14:19:54 -0700114 }
115
116 return security::ValidityPeriod(m_otherTlvs.front());
117}
118
119void
Yingdi Yu4a557052014-07-09 16:40:37 -0700120SignatureInfo::appendTypeSpecificTlv(const Block& block)
121{
122 m_otherTlvs.push_back(block);
123}
124
125
126const Block&
127SignatureInfo::getTypeSpecificTlv(uint32_t type) const
128{
129 for (std::list<Block>::const_iterator i = m_otherTlvs.begin();
130 i != m_otherTlvs.end(); i++) {
131 if (i->type() == type)
132 return *i;
133 }
134
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700135 BOOST_THROW_EXCEPTION(Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" +
136 boost::lexical_cast<std::string>(type) + "] from SignatureInfo"));
Yingdi Yu4a557052014-07-09 16:40:37 -0700137}
138
Alexander Afanasyev74633892015-02-08 18:08:46 -0800139template<encoding::Tag TAG>
Yingdi Yu4a557052014-07-09 16:40:37 -0700140size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700141SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const
Yingdi Yu4a557052014-07-09 16:40:37 -0700142{
143 size_t totalLength = 0;
144
145 for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
146 i != m_otherTlvs.rend(); i++) {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700147 totalLength += encoder.appendBlock(*i);
Yingdi Yu4a557052014-07-09 16:40:37 -0700148 }
149
150 if (m_hasKeyLocator)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700151 totalLength += m_keyLocator.wireEncode(encoder);
Yingdi Yu4a557052014-07-09 16:40:37 -0700152
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700153 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType, m_type);
Yingdi Yu4a557052014-07-09 16:40:37 -0700154
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700155 totalLength += encoder.prependVarNumber(totalLength);
156 totalLength += encoder.prependVarNumber(tlv::SignatureInfo);
Yingdi Yu4a557052014-07-09 16:40:37 -0700157 return totalLength;
158}
159
160template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700161SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
Yingdi Yu4a557052014-07-09 16:40:37 -0700162
163template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700164SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
Yingdi Yu4a557052014-07-09 16:40:37 -0700165
166
167const Block&
168SignatureInfo::wireEncode() const
169{
170 if (m_wire.hasWire())
171 return m_wire;
172
173 EncodingEstimator estimator;
174 size_t estimatedSize = wireEncode(estimator);
175
176 EncodingBuffer buffer(estimatedSize, 0);
177 wireEncode(buffer);
178
179 m_wire = buffer.block();
180 return m_wire;
181}
182
183void
184SignatureInfo::wireDecode(const Block& wire)
185{
186 if (!wire.hasWire()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700187 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yu4a557052014-07-09 16:40:37 -0700188 }
189
190 m_hasKeyLocator = false;
191
192 m_wire = wire;
193 m_wire.parse();
194
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600195 if (m_wire.type() != tlv::SignatureInfo)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700196 BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name"));
Yingdi Yu4a557052014-07-09 16:40:37 -0700197
198 Block::element_const_iterator it = m_wire.elements_begin();
199
200 // the first block must be SignatureType
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600201 if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700202 m_type = readNonNegativeInteger(*it);
203 it++;
204 }
205 else
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700206 BOOST_THROW_EXCEPTION(Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not "
207 "SignatureType"));
Yingdi Yu4a557052014-07-09 16:40:37 -0700208
209 // the second block could be KeyLocator
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600210 if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700211 m_keyLocator.wireDecode(*it);
212 m_hasKeyLocator = true;
213 it++;
214 }
215
216 // Decode the rest of type-specific TLVs, if any
217 while (it != m_wire.elements_end()) {
218 appendTypeSpecificTlv(*it);
219 it++;
220 }
221}
222
223bool
224SignatureInfo::operator==(const SignatureInfo& rhs) const
225{
226 return (m_type == rhs.m_type &&
227 m_hasKeyLocator == rhs.m_hasKeyLocator &&
228 m_keyLocator == rhs.m_keyLocator &&
229 m_otherTlvs == rhs.m_otherTlvs);
230}
231
232} // namespace ndn