blob: 559e1aadc15d7dc7cb67fb1c58a906cfef2842fe [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7809e302016-07-23 14:11:25 +00003 * Copyright (c) 2014-2016, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
Yingdi Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Yingdi Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Yingdi Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
22#include "pib-encoding.hpp"
23#include <ndn-cxx/encoding/block-helpers.hpp>
24#include <ndn-cxx/encoding/encoding-buffer.hpp>
25#include <vector>
26#include <string>
27
28namespace ndn {
29namespace pib {
30
31using std::vector;
32using std::string;
33
34PibIdentity::PibIdentity()
35{
36}
37
38PibIdentity::PibIdentity(const Name& identity)
39 : m_identity(identity)
40{
41}
42
43PibIdentity::PibIdentity(const Block& wire)
44{
45 wireDecode(wire);
46}
47
48template<bool T>
49size_t
50PibIdentity::wireEncode(EncodingImpl<T>& block) const
51{
52 size_t totalLength = m_identity.wireEncode(block);
53 totalLength += block.prependVarNumber(totalLength);
54 totalLength += block.prependVarNumber(tlv::pib::Identity);
55
56 return totalLength;
57}
58
59template size_t
60PibIdentity::wireEncode<true>(EncodingImpl<true>& block) const;
61
62template size_t
63PibIdentity::wireEncode<false>(EncodingImpl<false>& block) const;
64
65const Block&
66PibIdentity::wireEncode() const
67{
68 if (m_wire.hasWire())
69 return m_wire;
70
71 EncodingEstimator estimator;
72 size_t estimatedSize = wireEncode(estimator);
73
74 EncodingBuffer buffer(estimatedSize, 0);
75 wireEncode(buffer);
76
77 m_wire = buffer.block();
78 return m_wire;
79}
80
81void
82PibIdentity::wireDecode(const Block& wire)
83{
84 if (!wire.hasWire()) {
85 throw tlv::Error("The supplied block does not contain wire format");
86 }
87
88 if (wire.type() != tlv::pib::Identity)
89 throw tlv::Error("Unexpected TLV type when decoding PibIdentity");
90
91 m_wire = wire;
92 m_identity.wireDecode(m_wire.blockFromValue());
93}
94
95
96
97PibPublicKey::PibPublicKey()
98 : m_isValueSet(false)
99{
100}
101
102PibPublicKey::PibPublicKey(const Name& keyName, const PublicKey& key)
103 : m_isValueSet(true)
104 , m_keyName(keyName)
105 , m_key(key)
106{
107}
108
109PibPublicKey::PibPublicKey(const Block& wire)
110{
111 wireDecode(wire);
112}
113
114const Name&
115PibPublicKey::getKeyName() const
116{
117 if (m_isValueSet)
118 return m_keyName;
119 else
120 throw tlv::Error("PibPublicKey::getKeyName: keyName is not set");
121}
122
123const PublicKey&
124PibPublicKey::getPublicKey() const
125{
126 if (m_isValueSet)
127 return m_key;
128 else
129 throw tlv::Error("PibPublicKey::getPublicKey: key is not set");
130}
131
132template<bool T>
133size_t
134PibPublicKey::wireEncode(EncodingImpl<T>& block) const
135{
Junxiao Shi7809e302016-07-23 14:11:25 +0000136 size_t totalLength = block.prependByteArrayBlock(tlv::pib::Bytes,
137 m_key.get().buf(), m_key.get().size());
Yingdi Yu77627ab2015-07-21 16:13:49 -0700138 totalLength += m_keyName.wireEncode(block);
139 totalLength += block.prependVarNumber(totalLength);
140 totalLength += block.prependVarNumber(tlv::pib::PublicKey);
141
142 return totalLength;
143}
144
145template size_t
146PibPublicKey::wireEncode<true>(EncodingImpl<true>& block) const;
147
148template size_t
149PibPublicKey::wireEncode<false>(EncodingImpl<false>& block) const;
150
151const Block&
152PibPublicKey::wireEncode() const
153{
154 if (m_wire.hasWire())
155 return m_wire;
156
157 EncodingEstimator estimator;
158 size_t estimatedSize = wireEncode(estimator);
159
160 EncodingBuffer buffer(estimatedSize, 0);
161 wireEncode(buffer);
162
163 m_wire = buffer.block();
164 return m_wire;
165}
166
167void
168PibPublicKey::wireDecode(const Block& wire)
169{
170 if (!wire.hasWire()) {
171 throw tlv::Error("The supplied block does not contain wire format");
172 }
173
174 if (wire.type() != tlv::pib::PublicKey)
175 throw tlv::Error("Unexpected TLV type when decoding PibPublicKey");
176
177 m_wire = wire;
178 m_wire.parse();
179
180 Block::element_const_iterator it = m_wire.elements_begin();
181 if (it != m_wire.elements_end() && it->type() == tlv::Name) {
182 m_keyName.wireDecode(*it);
183 it++;
184 }
185 else
186 throw tlv::Error("PibPublicKey requires the first sub-TLV to be Name");
187
188 if (it != m_wire.elements_end() && it->type() == tlv::pib::Bytes) {
189 m_key = PublicKey(it->value(), it->value_size());
190 it++;
191 }
192 else
193 throw tlv::Error("PibPublicKey requires the second sub-TLV to be Bytes");
194
195 m_isValueSet = true;
196 if (it != m_wire.elements_end())
197 throw tlv::Error("PibPublicKey must contain only two sub-TLVs");
198}
199
200
201PibCertificate::PibCertificate()
202 : m_isValueSet(false)
203{
204}
205
206PibCertificate::PibCertificate(const IdentityCertificate& certificate)
207 : m_isValueSet(true)
208 , m_certificate(certificate)
209{
210}
211
212PibCertificate::PibCertificate(const Block& wire)
213{
214 wireDecode(wire);
215}
216
217const IdentityCertificate&
218PibCertificate::getCertificate() const
219{
220 if (m_isValueSet)
221 return m_certificate;
222 else
223 throw tlv::Error("PibCertificate::getCertificate: certificate is not set");
224}
225
226template<bool T>
227size_t
228PibCertificate::wireEncode(EncodingImpl<T>& block) const
229{
Junxiao Shi7809e302016-07-23 14:11:25 +0000230 size_t totalLength = block.prependBlock(m_certificate.wireEncode());
Yingdi Yu77627ab2015-07-21 16:13:49 -0700231 totalLength += block.prependVarNumber(totalLength);
232 totalLength += block.prependVarNumber(tlv::pib::Certificate);
233
234 return totalLength;
235}
236
237template size_t
238PibCertificate::wireEncode<true>(EncodingImpl<true>& block) const;
239
240template size_t
241PibCertificate::wireEncode<false>(EncodingImpl<false>& block) const;
242
243const Block&
244PibCertificate::wireEncode() const
245{
246 if (m_wire.hasWire())
247 return m_wire;
248
249 EncodingEstimator estimator;
250 size_t estimatedSize = wireEncode(estimator);
251
252 EncodingBuffer buffer(estimatedSize, 0);
253 wireEncode(buffer);
254
255 m_wire = buffer.block();
256 return m_wire;
257}
258
259void
260PibCertificate::wireDecode(const Block& wire)
261{
262 if (!wire.hasWire()) {
263 throw tlv::Error("The supplied block does not contain wire format");
264 }
265
266 if (wire.type() != tlv::pib::Certificate)
267 throw tlv::Error("Unexpected TLV type when decoding PibCertificate");
268
269 m_wire = wire;
270 m_certificate.wireDecode(m_wire.blockFromValue());
271
272 m_isValueSet = true;
273}
274
275
276PibNameList::PibNameList()
277{
278}
279
280PibNameList::PibNameList(const std::vector<Name>& names)
281 : m_names(names)
282{
283}
284
285PibNameList::PibNameList(const Block& wire)
286{
287 wireDecode(wire);
288}
289
290template<bool T>
291size_t
292PibNameList::wireEncode(EncodingImpl<T>& block) const
293{
294 size_t totalLength = 0;
295
296 for (vector<Name>::const_reverse_iterator it = m_names.rbegin();
297 it != m_names.rend(); it++) {
298 totalLength += it->wireEncode(block);
299 }
300
301 totalLength += block.prependVarNumber(totalLength);
302 totalLength += block.prependVarNumber(tlv::pib::NameList);
303 return totalLength;
304}
305
306template size_t
307PibNameList::wireEncode<true>(EncodingImpl<true>& block) const;
308
309template size_t
310PibNameList::wireEncode<false>(EncodingImpl<false>& block) const;
311
312
313const Block&
314PibNameList::wireEncode() const
315{
316 if (m_wire.hasWire())
317 return m_wire;
318
319 EncodingEstimator estimator;
320 size_t estimatedSize = wireEncode(estimator);
321
322 EncodingBuffer buffer(estimatedSize, 0);
323 wireEncode(buffer);
324
325 m_wire = buffer.block();
326 return m_wire;
327}
328
329void
330PibNameList::wireDecode(const Block& wire)
331{
332 if (!wire.hasWire()) {
333 throw tlv::Error("The supplied block does not contain wire format");
334 }
335
336 if (wire.type() != tlv::pib::NameList)
337 throw tlv::Error("Unexpected TLV type when decoding PibNameList");
338
339 m_wire = wire;
340 m_wire.parse();
341 for (Block::element_const_iterator it = m_wire.elements_begin();
342 it != m_wire.elements_end(); it++) {
343 if (it->type() == tlv::Name) {
344 Name name;
345 name.wireDecode(*it);
346 m_names.push_back(name);
347 }
348 }
349}
350
351PibError::PibError()
352 : m_errCode(ERR_SUCCESS)
353{
354}
355
356PibError::PibError(const pib::ErrCode errCode, const std::string& msg)
357 : m_errCode(errCode)
358 , m_msg(msg)
359{
360}
361
362PibError::PibError(const Block& wire)
363{
364 wireDecode(wire);
365}
366
367template<bool T>
368size_t
369PibError::wireEncode(EncodingImpl<T>& block) const
370{
371 size_t totalLength = 0;
Junxiao Shi7809e302016-07-23 14:11:25 +0000372 totalLength += block.prependByteArrayBlock(tlv::pib::Bytes,
373 reinterpret_cast<const uint8_t*>(m_msg.c_str()),
374 m_msg.size());
Yingdi Yu77627ab2015-07-21 16:13:49 -0700375 totalLength += prependNonNegativeIntegerBlock(block, tlv::pib::ErrorCode, m_errCode);
376 totalLength += block.prependVarNumber(totalLength);
377 totalLength += block.prependVarNumber(tlv::pib::Error);
378 return totalLength;
379}
380
381template size_t
382PibError::wireEncode<true>(EncodingImpl<true>& block) const;
383
384template size_t
385PibError::wireEncode<false>(EncodingImpl<false>& block) const;
386
387
388const Block&
389PibError::wireEncode() const
390{
391 if (m_wire.hasWire())
392 return m_wire;
393
394 EncodingEstimator estimator;
395 size_t estimatedSize = wireEncode(estimator);
396
397 EncodingBuffer buffer(estimatedSize, 0);
398 wireEncode(buffer);
399
400 m_wire = buffer.block();
401 return m_wire;
402}
403
404void
405PibError::wireDecode(const Block& wire)
406{
407 if (!wire.hasWire()) {
408 throw tlv::Error("The supplied block does not contain wire format");
409 }
410
411 if (wire.type() != tlv::pib::Error)
412 throw tlv::Error("Unexpected TLV type when decoding Error");
413
414 m_wire = wire;
415 m_wire.parse();
416 Block::element_const_iterator it = m_wire.elements_begin();
417
418 if (it != m_wire.elements_end() && it->type() == tlv::pib::ErrorCode) {
419 m_errCode = static_cast<pib::ErrCode>(readNonNegativeInteger(*it));
420 it++;
421 }
422 else
423 throw tlv::Error("PibError requires the first sub-TLV to be ErrorCode");
424
425 if (it != m_wire.elements_end() && it->type() == tlv::pib::Bytes) {
426 m_msg = string(reinterpret_cast<const char*>(it->value()), it->value_size());
427 it++;
428 }
429 else
430 throw tlv::Error("PibError requires the second sub-TLV to be Bytes");
431}
432
433PibUser::PibUser()
434{
435}
436
437PibUser::PibUser(const Block& wire)
438{
439 wireDecode(wire);
440}
441
442void
443PibUser::setMgmtCert(const IdentityCertificate& mgmtCert)
444{
445 m_wire.reset();
446 m_mgmtCert = mgmtCert;
447}
448
449void
450PibUser::setTpmLocator(const std::string& tpmLocator)
451{
452 m_wire.reset();
453 m_tpmLocator = tpmLocator;
454}
455
456template<bool T>
457size_t
458PibUser::wireEncode(EncodingImpl<T>& block) const
459{
460 size_t totalLength = 0;
461
462 if (!m_tpmLocator.empty())
Junxiao Shi7809e302016-07-23 14:11:25 +0000463 totalLength = block.prependByteArrayBlock(tlv::pib::TpmLocator,
464 reinterpret_cast<const uint8_t*>(m_tpmLocator.c_str()),
465 m_tpmLocator.size());
Yingdi Yu77627ab2015-07-21 16:13:49 -0700466
Junxiao Shi7809e302016-07-23 14:11:25 +0000467 totalLength += block.prependBlock(m_mgmtCert.wireEncode());
Yingdi Yu77627ab2015-07-21 16:13:49 -0700468
469 totalLength += block.prependVarNumber(totalLength);
470 totalLength += block.prependVarNumber(tlv::pib::User);
471
472 return totalLength;
473}
474
475template size_t
476PibUser::wireEncode<true>(EncodingImpl<true>& block) const;
477
478template size_t
479PibUser::wireEncode<false>(EncodingImpl<false>& block) const;
480
481
482const Block&
483PibUser::wireEncode() const
484{
485 if (m_wire.hasWire())
486 return m_wire;
487
488 EncodingEstimator estimator;
489 size_t estimatedSize = wireEncode(estimator);
490
491 EncodingBuffer buffer(estimatedSize, 0);
492 wireEncode(buffer);
493
494 m_wire = buffer.block();
495 return m_wire;
496}
497
498void
499PibUser::wireDecode(const Block& wire)
500{
501 if (!wire.hasWire()) {
502 throw tlv::Error("The supplied block does not contain wire format");
503 }
504
505 if (wire.type() != tlv::pib::User)
506 throw tlv::Error("Unexpected TLV type when decoding Content");
507
508 m_wire = wire;
509 m_wire.parse();
510
511 Block::element_const_iterator it = m_wire.elements_begin();
512 if (it != m_wire.elements_end() && it->type() == tlv::Data) {
513 m_mgmtCert.wireDecode(*it);
514 it++;
515 }
516 else
517 throw tlv::Error("PibError requires the first sub-TLV to be Data");
518
519 if (it != m_wire.elements_end() && it->type() == tlv::pib::TpmLocator) {
520 m_tpmLocator = string(reinterpret_cast<const char*>(it->value()), it->value_size());
521 }
522}
523
524} // namespace pib
525} // namespace ndn