blob: 0a7c9e1fb4a9fe4dbe2e635917747162949d685c [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
22EncryptedContent::EncryptedContent(tlv::AlgorithmTypeValue type, const KeyLocator& keyLocator, const ConstBufferPtr& payload)
23 : m_type(type)
24 , m_hasKeyLocator(true)
25 , m_keyLocator(keyLocator)
26 , m_payload(payload)
27{
28}
29
30EncryptedContent::EncryptedContent(const Block& block)
31{
32 wireDecode(block);
33}
34
35void
36EncryptedContent::setAlgorithmType(tlv::AlgorithmTypeValue type)
37{
38 m_wire.reset();
39 m_type = type;
40}
41
42void
43EncryptedContent::setKeyLocator(const KeyLocator& keyLocator)
44{
45 m_wire.reset();
46 m_keyLocator = keyLocator;
47 m_hasKeyLocator = true;
48}
49
50const KeyLocator&
51EncryptedContent::getKeyLocator() const
52{
53 if (m_hasKeyLocator)
54 return m_keyLocator;
55 else
56 throw Error("KeyLocator does not exist");
57}
58
59void
60EncryptedContent::setPayload(const ConstBufferPtr& payload)
61{
62 m_wire.reset();
63 m_payload = payload;
64}
65
66const ConstBufferPtr
67EncryptedContent::getPayload() const
68{
69 return m_payload;
70}
71
72template<encoding::Tag TAG>
73size_t
74EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const
75{
76 size_t totalLength = 0;
77
78 totalLength += block.appendByteArrayBlock(tlv::EncryptedPayload, m_payload->buf(), m_payload->size());
79
80 if (m_type != -1)
81 totalLength += prependNonNegativeIntegerBlock(block, tlv::EncryptionAlgorithm, m_type);
82 else
83 throw Error("EncryptedContent does not have an encryption algorithm");
84
85 if (m_hasKeyLocator)
86 totalLength += m_keyLocator.wireEncode(block);
87 else
88 throw Error("EncryptedContent does not have key locator");
89
90 totalLength += block.prependVarNumber(totalLength);
91 totalLength += block.prependVarNumber(tlv::EncryptedContent);
92 return totalLength;
93}
94
95const Block&
96EncryptedContent::wireEncode() const
97{
98 if (m_wire.hasWire())
99 return m_wire;
100
101 EncodingEstimator estimator;
102 size_t estimatedSize = wireEncode(estimator);
103
104 EncodingBuffer buffer(estimatedSize, 0);
105 wireEncode(buffer);
106
107 m_wire = buffer.block();
108 return m_wire;
109}
110
111void
112EncryptedContent::wireDecode(const Block& wire)
113{
114 if (!wire.hasWire()) {
115 throw Error("The supplied block does not contain wire format");
116 }
117
118 m_hasKeyLocator = false;
119
120 m_wire = wire;
121 m_wire.parse();
122
123 if (m_wire.type() != tlv::EncryptedContent)
124 throw Error("Unexpected TLV type when decoding Name");
125
126 Block::element_const_iterator it = m_wire.elements_begin();
127
128 if (it != m_wire.elements_end() && it->type() == ndn::tlv::KeyLocator) {
129 m_keyLocator.wireDecode(*it);
130 m_hasKeyLocator = true;
131 it++;
132 }
133 else
134 throw Error("EncryptedContent does not have key locator");
135
136 if (it != m_wire.elements_end() && it->type() == tlv::EncryptionAlgorithm) {
137 m_type = readNonNegativeInteger(*it);
138 it++;
139 }
140 else
141 throw Error("EncryptedContent does not have encryption algorithm");
142
143 if (it != m_wire.elements_end() && it->type() == tlv::EncryptedPayload) {
144 m_payload = make_shared<Buffer>(it->value_begin(),it->value_end());
145 it++;
146 }
147 else
148 throw Error("EncryptedContent has missing payload");
149
150 if (it != m_wire.elements_end()) {
151 throw Error("EncryptedContent has extraneous sub-TLVs");
152 }
153}
154
155bool
156EncryptedContent::operator==(const EncryptedContent& rhs) const
157{
158 return (wireEncode() == rhs.wireEncode());
159}
160
161} // namespace gep
162} // namespace ndn