blob: 20e013f3ef6832a867f17dca8929eabfbe8af1ae [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
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -070066void
67SignatureInfo::unsetKeyLocator()
68{
69 m_wire.reset();
70 m_keyLocator = KeyLocator();
71 m_hasKeyLocator = false;
72}
73
Yingdi Yu4a557052014-07-09 16:40:37 -070074const KeyLocator&
75SignatureInfo::getKeyLocator() const
76{
77 if (m_hasKeyLocator)
78 return m_keyLocator;
79 else
80 throw Error("KeyLocator does not exist");
81}
82
83void
84SignatureInfo::appendTypeSpecificTlv(const Block& block)
85{
86 m_otherTlvs.push_back(block);
87}
88
89
90const Block&
91SignatureInfo::getTypeSpecificTlv(uint32_t type) const
92{
93 for (std::list<Block>::const_iterator i = m_otherTlvs.begin();
94 i != m_otherTlvs.end(); i++) {
95 if (i->type() == type)
96 return *i;
97 }
98
99 throw Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" +
100 boost::lexical_cast<std::string>(type) + "] from SignatureInfo");
101}
102
103template<bool T>
104size_t
105SignatureInfo::wireEncode(EncodingImpl<T>& block) const
106{
107 size_t totalLength = 0;
108
109 for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
110 i != m_otherTlvs.rend(); i++) {
111 totalLength += block.appendBlock(*i);
112 }
113
114 if (m_hasKeyLocator)
115 totalLength += m_keyLocator.wireEncode(block);
116
117 totalLength += prependNonNegativeIntegerBlock(block, Tlv::SignatureType, m_type);
118
119 totalLength += block.prependVarNumber(totalLength);
120 totalLength += block.prependVarNumber(Tlv::SignatureInfo);
121 return totalLength;
122}
123
124template size_t
125SignatureInfo::wireEncode<true>(EncodingImpl<true>& block) const;
126
127template size_t
128SignatureInfo::wireEncode<false>(EncodingImpl<false>& block) const;
129
130
131const Block&
132SignatureInfo::wireEncode() const
133{
134 if (m_wire.hasWire())
135 return m_wire;
136
137 EncodingEstimator estimator;
138 size_t estimatedSize = wireEncode(estimator);
139
140 EncodingBuffer buffer(estimatedSize, 0);
141 wireEncode(buffer);
142
143 m_wire = buffer.block();
144 return m_wire;
145}
146
147void
148SignatureInfo::wireDecode(const Block& wire)
149{
150 if (!wire.hasWire()) {
151 throw Error("The supplied block does not contain wire format");
152 }
153
154 m_hasKeyLocator = false;
155
156 m_wire = wire;
157 m_wire.parse();
158
159 if (m_wire.type() != Tlv::SignatureInfo)
160 throw Tlv::Error("Unexpected TLV type when decoding Name");
161
162 Block::element_const_iterator it = m_wire.elements_begin();
163
164 // the first block must be SignatureType
165 if (it != m_wire.elements_end() && it->type() == Tlv::SignatureType) {
166 m_type = readNonNegativeInteger(*it);
167 it++;
168 }
169 else
170 throw Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not SignatureType");
171
172 // the second block could be KeyLocator
173 if (it != m_wire.elements_end() && it->type() == Tlv::KeyLocator) {
174 m_keyLocator.wireDecode(*it);
175 m_hasKeyLocator = true;
176 it++;
177 }
178
179 // Decode the rest of type-specific TLVs, if any
180 while (it != m_wire.elements_end()) {
181 appendTypeSpecificTlv(*it);
182 it++;
183 }
184}
185
186bool
187SignatureInfo::operator==(const SignatureInfo& rhs) const
188{
189 return (m_type == rhs.m_type &&
190 m_hasKeyLocator == rhs.m_hasKeyLocator &&
191 m_keyLocator == rhs.m_keyLocator &&
192 m_otherTlvs == rhs.m_otherTlvs);
193}
194
195} // namespace ndn