blob: 29f8cebab5fe03eab51ccce4df40a541c3ac4120 [file] [log] [blame]
Prashanthc0029b62015-04-27 14:00:08 -07001#include "encrypted-content.hpp"
2#include <ndn-cxx/encoding/block-helpers.hpp>
3#include <ndn-cxx/util/concepts.hpp>
4
5#include <boost/lexical_cast.hpp>
6
7namespace ndn {
8namespace gep {
9
10BOOST_CONCEPT_ASSERT((boost::EqualityComparable<EncryptedContent>));
11BOOST_CONCEPT_ASSERT((WireEncodable<EncryptedContent>));
12BOOST_CONCEPT_ASSERT((WireDecodable<EncryptedContent>));
13static_assert(std::is_base_of<ndn::tlv::Error, EncryptedContent::Error>::value,
14 "EncryptedContent::Error must inherit from tlv::Error");
15
16EncryptedContent::EncryptedContent()
17 : m_type(-1)
18 , m_hasKeyLocator(false)
19{
20}
21
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070022EncryptedContent::EncryptedContent(tlv::AlgorithmTypeValue type, const KeyLocator& keyLocator,
23 ConstBufferPtr payload, ConstBufferPtr iv)
Prashanthc0029b62015-04-27 14:00:08 -070024 : m_type(type)
25 , m_hasKeyLocator(true)
26 , m_keyLocator(keyLocator)
27 , m_payload(payload)
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070028 , m_iv(iv)
Prashanthc0029b62015-04-27 14:00:08 -070029{
30}
31
32EncryptedContent::EncryptedContent(const Block& block)
33{
34 wireDecode(block);
35}
36
37void
38EncryptedContent::setAlgorithmType(tlv::AlgorithmTypeValue type)
39{
40 m_wire.reset();
41 m_type = type;
42}
43
44void
45EncryptedContent::setKeyLocator(const KeyLocator& keyLocator)
46{
47 m_wire.reset();
48 m_keyLocator = keyLocator;
49 m_hasKeyLocator = true;
50}
51
52const KeyLocator&
53EncryptedContent::getKeyLocator() const
54{
55 if (m_hasKeyLocator)
56 return m_keyLocator;
57 else
58 throw Error("KeyLocator does not exist");
59}
60
61void
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070062EncryptedContent::setInitialVector(ConstBufferPtr iv)
63{
64 m_wire.reset();
65 m_iv = iv;
66}
67
68ConstBufferPtr
69EncryptedContent::getInitialVector() const
70{
71 return m_iv;
72}
73
74void
75EncryptedContent::setPayload(ConstBufferPtr payload)
Prashanthc0029b62015-04-27 14:00:08 -070076{
77 m_wire.reset();
78 m_payload = payload;
79}
80
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070081ConstBufferPtr
Prashanthc0029b62015-04-27 14:00:08 -070082EncryptedContent::getPayload() const
83{
84 return m_payload;
85}
86
87template<encoding::Tag TAG>
88size_t
89EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const
90{
91 size_t totalLength = 0;
92
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070093 if (m_payload != nullptr)
94 totalLength += block.prependByteArrayBlock(tlv::EncryptedPayload, m_payload->buf(), m_payload->size());
95 else
96 throw Error("EncryptedContent does not have a payload");
97
98 if (m_iv != nullptr)
99 totalLength += block.prependByteArrayBlock(tlv::InitialVector, m_iv->buf(), m_iv->size());
Prashanthc0029b62015-04-27 14:00:08 -0700100
101 if (m_type != -1)
102 totalLength += prependNonNegativeIntegerBlock(block, tlv::EncryptionAlgorithm, m_type);
103 else
104 throw Error("EncryptedContent does not have an encryption algorithm");
105
106 if (m_hasKeyLocator)
107 totalLength += m_keyLocator.wireEncode(block);
108 else
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700109 throw Error("EncryptedContent does not have a key locator");
Prashanthc0029b62015-04-27 14:00:08 -0700110
111 totalLength += block.prependVarNumber(totalLength);
112 totalLength += block.prependVarNumber(tlv::EncryptedContent);
113 return totalLength;
114}
115
116const Block&
117EncryptedContent::wireEncode() const
118{
119 if (m_wire.hasWire())
120 return m_wire;
121
122 EncodingEstimator estimator;
123 size_t estimatedSize = wireEncode(estimator);
124
125 EncodingBuffer buffer(estimatedSize, 0);
126 wireEncode(buffer);
127
128 m_wire = buffer.block();
129 return m_wire;
130}
131
132void
133EncryptedContent::wireDecode(const Block& wire)
134{
135 if (!wire.hasWire()) {
136 throw Error("The supplied block does not contain wire format");
137 }
138
139 m_hasKeyLocator = false;
140
141 m_wire = wire;
142 m_wire.parse();
143
144 if (m_wire.type() != tlv::EncryptedContent)
145 throw Error("Unexpected TLV type when decoding Name");
146
147 Block::element_const_iterator it = m_wire.elements_begin();
148
149 if (it != m_wire.elements_end() && it->type() == ndn::tlv::KeyLocator) {
150 m_keyLocator.wireDecode(*it);
151 m_hasKeyLocator = true;
152 it++;
153 }
154 else
155 throw Error("EncryptedContent does not have key locator");
156
157 if (it != m_wire.elements_end() && it->type() == tlv::EncryptionAlgorithm) {
158 m_type = readNonNegativeInteger(*it);
159 it++;
160 }
161 else
162 throw Error("EncryptedContent does not have encryption algorithm");
163
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700164 if (it != m_wire.elements_end() && it->type() == tlv::InitialVector) {
165 m_iv = make_shared<Buffer>(it->value_begin(), it->value_end());
166 it++;
167 }
168 else
169 m_iv = nullptr;
170
Prashanthc0029b62015-04-27 14:00:08 -0700171 if (it != m_wire.elements_end() && it->type() == tlv::EncryptedPayload) {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700172 m_payload = make_shared<Buffer>(it->value_begin(), it->value_end());
Prashanthc0029b62015-04-27 14:00:08 -0700173 it++;
174 }
175 else
176 throw Error("EncryptedContent has missing payload");
177
178 if (it != m_wire.elements_end()) {
179 throw Error("EncryptedContent has extraneous sub-TLVs");
180 }
181}
182
183bool
184EncryptedContent::operator==(const EncryptedContent& rhs) const
185{
186 return (wireEncode() == rhs.wireEncode());
187}
188
189} // namespace gep
190} // namespace ndn