Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 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 | * @author Zhiyi Zhang <dreamerbarrychang@gmail.com> |
| 22 | */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 23 | |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 24 | #include "safe-bag.hpp" |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 25 | #include "encoding/encoding-buffer.hpp" |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 26 | #include "encoding/tlv-security.hpp" |
| 27 | #include "util/concepts.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodable<SafeBag>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireDecodable<SafeBag>)); |
| 34 | |
| 35 | SafeBag::SafeBag() = default; |
| 36 | |
| 37 | SafeBag::SafeBag(const Block& wire) |
| 38 | { |
| 39 | this->wireDecode(wire); |
| 40 | } |
| 41 | |
| 42 | SafeBag::SafeBag(const Data& certificate, |
| 43 | const Buffer& encryptedKeyBag) |
| 44 | : m_certificate(certificate) |
| 45 | , m_encryptedKeyBag(encryptedKeyBag) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | SafeBag::SafeBag(const Data& certificate, |
| 50 | const uint8_t* encryptedKey, |
| 51 | size_t encryptedKeyLen) |
| 52 | : m_certificate(certificate) |
| 53 | , m_encryptedKeyBag(encryptedKey, encryptedKeyLen) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | ///////////////////////////////////////////////////// encode & decode |
| 58 | |
| 59 | template<encoding::Tag TAG> |
| 60 | size_t |
| 61 | SafeBag::wireEncode(EncodingImpl<TAG>& encoder) const |
| 62 | { |
| 63 | size_t totalLength = 0; |
| 64 | |
| 65 | // EncryptedKeyBag |
| 66 | totalLength += encoder.prependByteArrayBlock(tlv::security::EncryptedKeyBag, |
| 67 | m_encryptedKeyBag.get(), |
| 68 | m_encryptedKeyBag.size()); |
| 69 | |
| 70 | // Certificate |
| 71 | totalLength += this->m_certificate.wireEncode(encoder); |
| 72 | |
| 73 | totalLength += encoder.prependVarNumber(totalLength); |
| 74 | totalLength += encoder.prependVarNumber(tlv::security::SafeBag); |
| 75 | |
| 76 | return totalLength; |
| 77 | } |
| 78 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 79 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SafeBag); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 80 | |
| 81 | const Block& |
| 82 | SafeBag::wireEncode() const |
| 83 | { |
| 84 | EncodingEstimator estimator; |
| 85 | size_t estimatedSize = wireEncode(estimator); |
| 86 | |
| 87 | EncodingBuffer buffer(estimatedSize, 0); |
| 88 | wireEncode(buffer); |
| 89 | |
| 90 | this->m_wire = buffer.block(); |
| 91 | return m_wire; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | SafeBag::wireDecode(const Block& wire) |
| 96 | { |
| 97 | if (wire.type() != tlv::security::SafeBag) |
| 98 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding safebag")); |
| 99 | |
| 100 | this->m_wire = wire; |
| 101 | m_wire.parse(); |
| 102 | |
| 103 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 104 | |
| 105 | // Certificate must be the first part |
| 106 | if (it != m_wire.elements_end()) { |
| 107 | this->m_certificate.wireDecode(*it); |
| 108 | it++; |
| 109 | } |
| 110 | else |
| 111 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure when decoding certificate")); |
| 112 | |
| 113 | // EncryptedKeyBag |
| 114 | if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKeyBag) { |
| 115 | this->m_encryptedKeyBag = Buffer(it->value(), it->value_size()); |
| 116 | it++; |
| 117 | } |
| 118 | else |
| 119 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure when decoding encryptedkeybag")); |
| 120 | |
| 121 | // Check if end |
| 122 | if (it != m_wire.elements_end()) |
| 123 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure after decoding the block")); |
| 124 | } |
| 125 | |
| 126 | } // namespace security |
| 127 | } // namespace ndn |