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