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