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