blob: 3e0879fe6cf9eda6b187aaa87ac40a7a3b8ea1d5 [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,
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070023 const uint8_t* payload, size_t payloadLen,
24 const uint8_t* iv, size_t ivLen)
Prashanthc0029b62015-04-27 14:00:08 -070025 : m_type(type)
26 , m_hasKeyLocator(true)
27 , m_keyLocator(keyLocator)
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070028 , m_payload(payload, payloadLen)
Prashanthc0029b62015-04-27 14:00:08 -070029{
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070030 if (iv != nullptr && ivLen != 0)
31 m_iv = Buffer(iv, ivLen);
Prashanthc0029b62015-04-27 14:00:08 -070032}
33
34EncryptedContent::EncryptedContent(const Block& block)
35{
36 wireDecode(block);
37}
38
39void
40EncryptedContent::setAlgorithmType(tlv::AlgorithmTypeValue type)
41{
42 m_wire.reset();
43 m_type = type;
44}
45
46void
47EncryptedContent::setKeyLocator(const KeyLocator& keyLocator)
48{
49 m_wire.reset();
50 m_keyLocator = keyLocator;
51 m_hasKeyLocator = true;
52}
53
54const KeyLocator&
55EncryptedContent::getKeyLocator() const
56{
57 if (m_hasKeyLocator)
58 return m_keyLocator;
59 else
60 throw Error("KeyLocator does not exist");
61}
62
63void
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070064EncryptedContent::setInitialVector(const uint8_t* iv, size_t ivLen)
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070065{
66 m_wire.reset();
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070067 m_iv = Buffer(iv, ivLen);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070068}
69
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070070const Buffer&
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070071EncryptedContent::getInitialVector() const
72{
73 return m_iv;
74}
75
76void
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070077EncryptedContent::setPayload(const uint8_t* payload, size_t payloadLen)
Prashanthc0029b62015-04-27 14:00:08 -070078{
79 m_wire.reset();
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070080 m_payload = Buffer(payload, payloadLen);
Prashanthc0029b62015-04-27 14:00:08 -070081}
82
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070083const Buffer&
Prashanthc0029b62015-04-27 14:00:08 -070084EncryptedContent::getPayload() const
85{
86 return m_payload;
87}
88
89template<encoding::Tag TAG>
90size_t
91EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const
92{
93 size_t totalLength = 0;
94
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070095 if (m_payload.size() != 0)
96 totalLength += block.prependByteArrayBlock(tlv::EncryptedPayload, m_payload.buf(), m_payload.size());
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070097 else
98 throw Error("EncryptedContent does not have a payload");
99
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700100 if (m_iv.size() != 0) {
101 totalLength += block.prependByteArrayBlock(tlv::InitialVector, m_iv.buf(), m_iv.size());
102 }
Prashanthc0029b62015-04-27 14:00:08 -0700103
104 if (m_type != -1)
105 totalLength += prependNonNegativeIntegerBlock(block, tlv::EncryptionAlgorithm, m_type);
106 else
107 throw Error("EncryptedContent does not have an encryption algorithm");
108
109 if (m_hasKeyLocator)
110 totalLength += m_keyLocator.wireEncode(block);
111 else
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700112 throw Error("EncryptedContent does not have a key locator");
Prashanthc0029b62015-04-27 14:00:08 -0700113
114 totalLength += block.prependVarNumber(totalLength);
115 totalLength += block.prependVarNumber(tlv::EncryptedContent);
116 return totalLength;
117}
118
119const Block&
120EncryptedContent::wireEncode() const
121{
122 if (m_wire.hasWire())
123 return m_wire;
124
125 EncodingEstimator estimator;
126 size_t estimatedSize = wireEncode(estimator);
127
128 EncodingBuffer buffer(estimatedSize, 0);
129 wireEncode(buffer);
130
131 m_wire = buffer.block();
132 return m_wire;
133}
134
135void
136EncryptedContent::wireDecode(const Block& wire)
137{
138 if (!wire.hasWire()) {
139 throw Error("The supplied block does not contain wire format");
140 }
141
142 m_hasKeyLocator = false;
143
144 m_wire = wire;
145 m_wire.parse();
146
147 if (m_wire.type() != tlv::EncryptedContent)
148 throw Error("Unexpected TLV type when decoding Name");
149
150 Block::element_const_iterator it = m_wire.elements_begin();
151
152 if (it != m_wire.elements_end() && it->type() == ndn::tlv::KeyLocator) {
153 m_keyLocator.wireDecode(*it);
154 m_hasKeyLocator = true;
155 it++;
156 }
157 else
158 throw Error("EncryptedContent does not have key locator");
159
160 if (it != m_wire.elements_end() && it->type() == tlv::EncryptionAlgorithm) {
161 m_type = readNonNegativeInteger(*it);
162 it++;
163 }
164 else
165 throw Error("EncryptedContent does not have encryption algorithm");
166
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700167 if (it != m_wire.elements_end() && it->type() == tlv::InitialVector) {
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700168 m_iv = Buffer(it->value_begin(), it->value_end());
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700169 it++;
170 }
171 else
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700172 m_iv = Buffer();
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700173
Prashanthc0029b62015-04-27 14:00:08 -0700174 if (it != m_wire.elements_end() && it->type() == tlv::EncryptedPayload) {
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700175 m_payload = Buffer(it->value_begin(), it->value_end());
Prashanthc0029b62015-04-27 14:00:08 -0700176 it++;
177 }
178 else
179 throw Error("EncryptedContent has missing payload");
180
181 if (it != m_wire.elements_end()) {
182 throw Error("EncryptedContent has extraneous sub-TLVs");
183 }
184}
185
186bool
187EncryptedContent::operator==(const EncryptedContent& rhs) const
188{
189 return (wireEncode() == rhs.wireEncode());
190}
191
192} // namespace gep
193} // namespace ndn