ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
safe-bag.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2023 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 <[email protected]>
22 */
23
27
28namespace ndn::security {
29
30SafeBag::SafeBag() = default;
31
33{
34 this->wireDecode(wire);
35}
36
37SafeBag::SafeBag(const Data& certificate, span<const uint8_t> encryptedKey)
38 : m_certificate(certificate)
39 , m_encryptedKey(encryptedKey.begin(), encryptedKey.end())
40{
41}
42
43template<encoding::Tag TAG>
44size_t
46{
47 size_t totalLength = 0;
48
49 // EncryptedKey
50 totalLength += prependBinaryBlock(encoder, tlv::security::EncryptedKey, m_encryptedKey);
51
52 // Certificate
53 totalLength += m_certificate.wireEncode(encoder);
54
55 totalLength += encoder.prependVarNumber(totalLength);
56 totalLength += encoder.prependVarNumber(tlv::security::SafeBag);
57 return totalLength;
58}
59
61
62const Block&
64{
65 EncodingEstimator estimator;
66 size_t estimatedSize = wireEncode(estimator);
67
68 EncodingBuffer buffer(estimatedSize, 0);
69 wireEncode(buffer);
70
71 m_wire = buffer.block();
72 return m_wire;
73}
74
75void
77{
78 if (wire.type() != tlv::security::SafeBag) {
79 NDN_THROW(tlv::Error("SafeBag", wire.type()));
80 }
81
82 m_wire = wire;
83 m_wire.parse();
84 auto it = m_wire.elements_begin();
85
86 // Certificate must be the first part
87 if (it != m_wire.elements_end()) {
88 m_certificate.wireDecode(*it);
89 it++;
90 }
91 else {
92 NDN_THROW(tlv::Error("Unexpected TLV structure when decoding Certificate"));
93 }
94
95 // EncryptedKey
96 if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKey) {
97 m_encryptedKey = Buffer(it->value(), it->value_size());
98 it++;
99 }
100 else {
101 NDN_THROW(tlv::Error("Unexpected TLV structure when decoding EncryptedKey"));
102 }
103
104 // Check if end
105 if (it != m_wire.elements_end()) {
106 NDN_THROW(tlv::Error("Unexpected TLV element at the end of SafeBag"));
107 }
108}
109
110} // namespace ndn::security
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition block.hpp:433
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition block.hpp:442
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
General-purpose automatically managed/resized buffer.
Definition buffer.hpp:43
Represents a Data packet.
Definition data.hpp:39
void wireDecode(const Block &wire)
Decode from wire.
Definition data.cpp:118
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder.
Definition data.cpp:39
A secured container for sensitive information (certificate, private key)
Definition safe-bag.hpp:38
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition safe-bag.cpp:76
SafeBag()
Create a new empty SafeBag object.
const Block & wireEncode() const
Encode to wire format.
Definition safe-bag.cpp:63
Represents an error in TLV encoding or decoding.
Definition tlv.hpp:54
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition exception.hpp:56
Contains the ndn-cxx security framework.