Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * 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 Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * 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 Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * 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 Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * 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 Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 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 | |
| 28 | namespace ndn { |
| 29 | namespace pib { |
| 30 | |
| 31 | using std::vector; |
| 32 | using std::string; |
| 33 | |
| 34 | PibIdentity::PibIdentity() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | PibIdentity::PibIdentity(const Name& identity) |
| 39 | : m_identity(identity) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | PibIdentity::PibIdentity(const Block& wire) |
| 44 | { |
| 45 | wireDecode(wire); |
| 46 | } |
| 47 | |
| 48 | template<bool T> |
| 49 | size_t |
| 50 | PibIdentity::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 | |
| 59 | template size_t |
| 60 | PibIdentity::wireEncode<true>(EncodingImpl<true>& block) const; |
| 61 | |
| 62 | template size_t |
| 63 | PibIdentity::wireEncode<false>(EncodingImpl<false>& block) const; |
| 64 | |
| 65 | const Block& |
| 66 | PibIdentity::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 | |
| 81 | void |
| 82 | PibIdentity::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 | |
| 97 | PibPublicKey::PibPublicKey() |
| 98 | : m_isValueSet(false) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | PibPublicKey::PibPublicKey(const Name& keyName, const PublicKey& key) |
| 103 | : m_isValueSet(true) |
| 104 | , m_keyName(keyName) |
| 105 | , m_key(key) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | PibPublicKey::PibPublicKey(const Block& wire) |
| 110 | { |
| 111 | wireDecode(wire); |
| 112 | } |
| 113 | |
| 114 | const Name& |
| 115 | PibPublicKey::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 | |
| 123 | const PublicKey& |
| 124 | PibPublicKey::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 | |
| 132 | template<bool T> |
| 133 | size_t |
| 134 | PibPublicKey::wireEncode(EncodingImpl<T>& block) const |
| 135 | { |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 136 | size_t totalLength = block.prependByteArrayBlock(tlv::pib::Bytes, |
| 137 | m_key.get().buf(), m_key.get().size()); |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 138 | totalLength += m_keyName.wireEncode(block); |
| 139 | totalLength += block.prependVarNumber(totalLength); |
| 140 | totalLength += block.prependVarNumber(tlv::pib::PublicKey); |
| 141 | |
| 142 | return totalLength; |
| 143 | } |
| 144 | |
| 145 | template size_t |
| 146 | PibPublicKey::wireEncode<true>(EncodingImpl<true>& block) const; |
| 147 | |
| 148 | template size_t |
| 149 | PibPublicKey::wireEncode<false>(EncodingImpl<false>& block) const; |
| 150 | |
| 151 | const Block& |
| 152 | PibPublicKey::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 | |
| 167 | void |
| 168 | PibPublicKey::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 | |
| 201 | PibCertificate::PibCertificate() |
| 202 | : m_isValueSet(false) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | PibCertificate::PibCertificate(const IdentityCertificate& certificate) |
| 207 | : m_isValueSet(true) |
| 208 | , m_certificate(certificate) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | PibCertificate::PibCertificate(const Block& wire) |
| 213 | { |
| 214 | wireDecode(wire); |
| 215 | } |
| 216 | |
| 217 | const IdentityCertificate& |
| 218 | PibCertificate::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 | |
| 226 | template<bool T> |
| 227 | size_t |
| 228 | PibCertificate::wireEncode(EncodingImpl<T>& block) const |
| 229 | { |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 230 | size_t totalLength = block.prependBlock(m_certificate.wireEncode()); |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 231 | totalLength += block.prependVarNumber(totalLength); |
| 232 | totalLength += block.prependVarNumber(tlv::pib::Certificate); |
| 233 | |
| 234 | return totalLength; |
| 235 | } |
| 236 | |
| 237 | template size_t |
| 238 | PibCertificate::wireEncode<true>(EncodingImpl<true>& block) const; |
| 239 | |
| 240 | template size_t |
| 241 | PibCertificate::wireEncode<false>(EncodingImpl<false>& block) const; |
| 242 | |
| 243 | const Block& |
| 244 | PibCertificate::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 | |
| 259 | void |
| 260 | PibCertificate::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 | |
| 276 | PibNameList::PibNameList() |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | PibNameList::PibNameList(const std::vector<Name>& names) |
| 281 | : m_names(names) |
| 282 | { |
| 283 | } |
| 284 | |
| 285 | PibNameList::PibNameList(const Block& wire) |
| 286 | { |
| 287 | wireDecode(wire); |
| 288 | } |
| 289 | |
| 290 | template<bool T> |
| 291 | size_t |
| 292 | PibNameList::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 | |
| 306 | template size_t |
| 307 | PibNameList::wireEncode<true>(EncodingImpl<true>& block) const; |
| 308 | |
| 309 | template size_t |
| 310 | PibNameList::wireEncode<false>(EncodingImpl<false>& block) const; |
| 311 | |
| 312 | |
| 313 | const Block& |
| 314 | PibNameList::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 | |
| 329 | void |
| 330 | PibNameList::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 | |
| 351 | PibError::PibError() |
| 352 | : m_errCode(ERR_SUCCESS) |
| 353 | { |
| 354 | } |
| 355 | |
| 356 | PibError::PibError(const pib::ErrCode errCode, const std::string& msg) |
| 357 | : m_errCode(errCode) |
| 358 | , m_msg(msg) |
| 359 | { |
| 360 | } |
| 361 | |
| 362 | PibError::PibError(const Block& wire) |
| 363 | { |
| 364 | wireDecode(wire); |
| 365 | } |
| 366 | |
| 367 | template<bool T> |
| 368 | size_t |
| 369 | PibError::wireEncode(EncodingImpl<T>& block) const |
| 370 | { |
| 371 | size_t totalLength = 0; |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 372 | totalLength += block.prependByteArrayBlock(tlv::pib::Bytes, |
| 373 | reinterpret_cast<const uint8_t*>(m_msg.c_str()), |
| 374 | m_msg.size()); |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 375 | 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 | |
| 381 | template size_t |
| 382 | PibError::wireEncode<true>(EncodingImpl<true>& block) const; |
| 383 | |
| 384 | template size_t |
| 385 | PibError::wireEncode<false>(EncodingImpl<false>& block) const; |
| 386 | |
| 387 | |
| 388 | const Block& |
| 389 | PibError::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 | |
| 404 | void |
| 405 | PibError::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 | |
| 433 | PibUser::PibUser() |
| 434 | { |
| 435 | } |
| 436 | |
| 437 | PibUser::PibUser(const Block& wire) |
| 438 | { |
| 439 | wireDecode(wire); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | PibUser::setMgmtCert(const IdentityCertificate& mgmtCert) |
| 444 | { |
| 445 | m_wire.reset(); |
| 446 | m_mgmtCert = mgmtCert; |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | PibUser::setTpmLocator(const std::string& tpmLocator) |
| 451 | { |
| 452 | m_wire.reset(); |
| 453 | m_tpmLocator = tpmLocator; |
| 454 | } |
| 455 | |
| 456 | template<bool T> |
| 457 | size_t |
| 458 | PibUser::wireEncode(EncodingImpl<T>& block) const |
| 459 | { |
| 460 | size_t totalLength = 0; |
| 461 | |
| 462 | if (!m_tpmLocator.empty()) |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 463 | totalLength = block.prependByteArrayBlock(tlv::pib::TpmLocator, |
| 464 | reinterpret_cast<const uint8_t*>(m_tpmLocator.c_str()), |
| 465 | m_tpmLocator.size()); |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 466 | |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 467 | totalLength += block.prependBlock(m_mgmtCert.wireEncode()); |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 468 | |
| 469 | totalLength += block.prependVarNumber(totalLength); |
| 470 | totalLength += block.prependVarNumber(tlv::pib::User); |
| 471 | |
| 472 | return totalLength; |
| 473 | } |
| 474 | |
| 475 | template size_t |
| 476 | PibUser::wireEncode<true>(EncodingImpl<true>& block) const; |
| 477 | |
| 478 | template size_t |
| 479 | PibUser::wireEncode<false>(EncodingImpl<false>& block) const; |
| 480 | |
| 481 | |
| 482 | const Block& |
| 483 | PibUser::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 | |
| 498 | void |
| 499 | PibUser::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 |