blob: 13e5be3fc296bb8c031df86961312c2f57118947 [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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>));
32BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
33static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value,
34 "SignatureInfo::Error must inherit from tlv::Error");
35
Yingdi Yu4a557052014-07-09 16:40:37 -070036SignatureInfo::SignatureInfo()
37 : m_type(-1)
38 , m_hasKeyLocator(false)
39{
40}
41
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060042SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070043 : m_type(type)
44 , m_hasKeyLocator(false)
45{
46}
47
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060048SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator)
Yingdi Yu4a557052014-07-09 16:40:37 -070049 : m_type(type)
50 , m_hasKeyLocator(true)
51 , m_keyLocator(keyLocator)
52{
53}
54
55SignatureInfo::SignatureInfo(const Block& block)
56{
57 wireDecode(block);
58}
59
60void
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060061SignatureInfo::setSignatureType(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070062{
63 m_wire.reset();
64 m_type = type;
65}
66
67void
68SignatureInfo::setKeyLocator(const KeyLocator& keyLocator)
69{
70 m_wire.reset();
71 m_keyLocator = keyLocator;
72 m_hasKeyLocator = true;
73}
74
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -070075void
76SignatureInfo::unsetKeyLocator()
77{
78 m_wire.reset();
79 m_keyLocator = KeyLocator();
80 m_hasKeyLocator = false;
81}
82
Yingdi Yu4a557052014-07-09 16:40:37 -070083const KeyLocator&
84SignatureInfo::getKeyLocator() const
85{
86 if (m_hasKeyLocator)
87 return m_keyLocator;
88 else
89 throw Error("KeyLocator does not exist");
90}
91
92void
93SignatureInfo::appendTypeSpecificTlv(const Block& block)
94{
95 m_otherTlvs.push_back(block);
96}
97
98
99const Block&
100SignatureInfo::getTypeSpecificTlv(uint32_t type) const
101{
102 for (std::list<Block>::const_iterator i = m_otherTlvs.begin();
103 i != m_otherTlvs.end(); i++) {
104 if (i->type() == type)
105 return *i;
106 }
107
108 throw Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" +
109 boost::lexical_cast<std::string>(type) + "] from SignatureInfo");
110}
111
112template<bool T>
113size_t
114SignatureInfo::wireEncode(EncodingImpl<T>& block) const
115{
116 size_t totalLength = 0;
117
118 for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
119 i != m_otherTlvs.rend(); i++) {
120 totalLength += block.appendBlock(*i);
121 }
122
123 if (m_hasKeyLocator)
124 totalLength += m_keyLocator.wireEncode(block);
125
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600126 totalLength += prependNonNegativeIntegerBlock(block, tlv::SignatureType, m_type);
Yingdi Yu4a557052014-07-09 16:40:37 -0700127
128 totalLength += block.prependVarNumber(totalLength);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600129 totalLength += block.prependVarNumber(tlv::SignatureInfo);
Yingdi Yu4a557052014-07-09 16:40:37 -0700130 return totalLength;
131}
132
133template size_t
134SignatureInfo::wireEncode<true>(EncodingImpl<true>& block) const;
135
136template size_t
137SignatureInfo::wireEncode<false>(EncodingImpl<false>& block) const;
138
139
140const Block&
141SignatureInfo::wireEncode() const
142{
143 if (m_wire.hasWire())
144 return m_wire;
145
146 EncodingEstimator estimator;
147 size_t estimatedSize = wireEncode(estimator);
148
149 EncodingBuffer buffer(estimatedSize, 0);
150 wireEncode(buffer);
151
152 m_wire = buffer.block();
153 return m_wire;
154}
155
156void
157SignatureInfo::wireDecode(const Block& wire)
158{
159 if (!wire.hasWire()) {
160 throw Error("The supplied block does not contain wire format");
161 }
162
163 m_hasKeyLocator = false;
164
165 m_wire = wire;
166 m_wire.parse();
167
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600168 if (m_wire.type() != tlv::SignatureInfo)
169 throw tlv::Error("Unexpected TLV type when decoding Name");
Yingdi Yu4a557052014-07-09 16:40:37 -0700170
171 Block::element_const_iterator it = m_wire.elements_begin();
172
173 // the first block must be SignatureType
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600174 if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700175 m_type = readNonNegativeInteger(*it);
176 it++;
177 }
178 else
179 throw Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not SignatureType");
180
181 // the second block could be KeyLocator
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600182 if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700183 m_keyLocator.wireDecode(*it);
184 m_hasKeyLocator = true;
185 it++;
186 }
187
188 // Decode the rest of type-specific TLVs, if any
189 while (it != m_wire.elements_end()) {
190 appendTypeSpecificTlv(*it);
191 it++;
192 }
193}
194
195bool
196SignatureInfo::operator==(const SignatureInfo& rhs) const
197{
198 return (m_type == rhs.m_type &&
199 m_hasKeyLocator == rhs.m_hasKeyLocator &&
200 m_keyLocator == rhs.m_keyLocator &&
201 m_otherTlvs == rhs.m_otherTlvs);
202}
203
204} // namespace ndn