blob: 1e40b1fa6c4a4af3375c154ea92ef389ef3aca74 [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"
24
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -070025#include <boost/lexical_cast.hpp>
Yingdi Yu4a557052014-07-09 16:40:37 -070026
27namespace ndn {
28
29SignatureInfo::SignatureInfo()
30 : m_type(-1)
31 , m_hasKeyLocator(false)
32{
33}
34
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060035SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070036 : m_type(type)
37 , m_hasKeyLocator(false)
38{
39}
40
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060041SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator)
Yingdi Yu4a557052014-07-09 16:40:37 -070042 : m_type(type)
43 , m_hasKeyLocator(true)
44 , m_keyLocator(keyLocator)
45{
46}
47
48SignatureInfo::SignatureInfo(const Block& block)
49{
50 wireDecode(block);
51}
52
53void
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060054SignatureInfo::setSignatureType(tlv::SignatureTypeValue type)
Yingdi Yu4a557052014-07-09 16:40:37 -070055{
56 m_wire.reset();
57 m_type = type;
58}
59
60void
61SignatureInfo::setKeyLocator(const KeyLocator& keyLocator)
62{
63 m_wire.reset();
64 m_keyLocator = keyLocator;
65 m_hasKeyLocator = true;
66}
67
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -070068void
69SignatureInfo::unsetKeyLocator()
70{
71 m_wire.reset();
72 m_keyLocator = KeyLocator();
73 m_hasKeyLocator = false;
74}
75
Yingdi Yu4a557052014-07-09 16:40:37 -070076const KeyLocator&
77SignatureInfo::getKeyLocator() const
78{
79 if (m_hasKeyLocator)
80 return m_keyLocator;
81 else
82 throw Error("KeyLocator does not exist");
83}
84
85void
86SignatureInfo::appendTypeSpecificTlv(const Block& block)
87{
88 m_otherTlvs.push_back(block);
89}
90
91
92const Block&
93SignatureInfo::getTypeSpecificTlv(uint32_t type) const
94{
95 for (std::list<Block>::const_iterator i = m_otherTlvs.begin();
96 i != m_otherTlvs.end(); i++) {
97 if (i->type() == type)
98 return *i;
99 }
100
101 throw Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" +
102 boost::lexical_cast<std::string>(type) + "] from SignatureInfo");
103}
104
105template<bool T>
106size_t
107SignatureInfo::wireEncode(EncodingImpl<T>& block) const
108{
109 size_t totalLength = 0;
110
111 for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
112 i != m_otherTlvs.rend(); i++) {
113 totalLength += block.appendBlock(*i);
114 }
115
116 if (m_hasKeyLocator)
117 totalLength += m_keyLocator.wireEncode(block);
118
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600119 totalLength += prependNonNegativeIntegerBlock(block, tlv::SignatureType, m_type);
Yingdi Yu4a557052014-07-09 16:40:37 -0700120
121 totalLength += block.prependVarNumber(totalLength);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600122 totalLength += block.prependVarNumber(tlv::SignatureInfo);
Yingdi Yu4a557052014-07-09 16:40:37 -0700123 return totalLength;
124}
125
126template size_t
127SignatureInfo::wireEncode<true>(EncodingImpl<true>& block) const;
128
129template size_t
130SignatureInfo::wireEncode<false>(EncodingImpl<false>& block) const;
131
132
133const Block&
134SignatureInfo::wireEncode() const
135{
136 if (m_wire.hasWire())
137 return m_wire;
138
139 EncodingEstimator estimator;
140 size_t estimatedSize = wireEncode(estimator);
141
142 EncodingBuffer buffer(estimatedSize, 0);
143 wireEncode(buffer);
144
145 m_wire = buffer.block();
146 return m_wire;
147}
148
149void
150SignatureInfo::wireDecode(const Block& wire)
151{
152 if (!wire.hasWire()) {
153 throw Error("The supplied block does not contain wire format");
154 }
155
156 m_hasKeyLocator = false;
157
158 m_wire = wire;
159 m_wire.parse();
160
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600161 if (m_wire.type() != tlv::SignatureInfo)
162 throw tlv::Error("Unexpected TLV type when decoding Name");
Yingdi Yu4a557052014-07-09 16:40:37 -0700163
164 Block::element_const_iterator it = m_wire.elements_begin();
165
166 // the first block must be SignatureType
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600167 if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700168 m_type = readNonNegativeInteger(*it);
169 it++;
170 }
171 else
172 throw Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not SignatureType");
173
174 // the second block could be KeyLocator
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600175 if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
Yingdi Yu4a557052014-07-09 16:40:37 -0700176 m_keyLocator.wireDecode(*it);
177 m_hasKeyLocator = true;
178 it++;
179 }
180
181 // Decode the rest of type-specific TLVs, if any
182 while (it != m_wire.elements_end()) {
183 appendTypeSpecificTlv(*it);
184 it++;
185 }
186}
187
188bool
189SignatureInfo::operator==(const SignatureInfo& rhs) const
190{
191 return (m_type == rhs.m_type &&
192 m_hasKeyLocator == rhs.m_hasKeyLocator &&
193 m_keyLocator == rhs.m_keyLocator &&
194 m_otherTlvs == rhs.m_otherTlvs);
195}
196
197} // namespace ndn