Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 1 | #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 | |
| 7 | namespace ndn { |
| 8 | namespace gep { |
| 9 | |
| 10 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<EncryptedContent>)); |
| 11 | BOOST_CONCEPT_ASSERT((WireEncodable<EncryptedContent>)); |
| 12 | BOOST_CONCEPT_ASSERT((WireDecodable<EncryptedContent>)); |
| 13 | static_assert(std::is_base_of<ndn::tlv::Error, EncryptedContent::Error>::value, |
| 14 | "EncryptedContent::Error must inherit from tlv::Error"); |
| 15 | |
| 16 | EncryptedContent::EncryptedContent() |
| 17 | : m_type(-1) |
| 18 | , m_hasKeyLocator(false) |
| 19 | { |
| 20 | } |
| 21 | |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 22 | EncryptedContent::EncryptedContent(tlv::AlgorithmTypeValue type, const KeyLocator& keyLocator, |
| 23 | ConstBufferPtr payload, ConstBufferPtr iv) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 24 | : m_type(type) |
| 25 | , m_hasKeyLocator(true) |
| 26 | , m_keyLocator(keyLocator) |
| 27 | , m_payload(payload) |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 28 | , m_iv(iv) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
| 32 | EncryptedContent::EncryptedContent(const Block& block) |
| 33 | { |
| 34 | wireDecode(block); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | EncryptedContent::setAlgorithmType(tlv::AlgorithmTypeValue type) |
| 39 | { |
| 40 | m_wire.reset(); |
| 41 | m_type = type; |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | EncryptedContent::setKeyLocator(const KeyLocator& keyLocator) |
| 46 | { |
| 47 | m_wire.reset(); |
| 48 | m_keyLocator = keyLocator; |
| 49 | m_hasKeyLocator = true; |
| 50 | } |
| 51 | |
| 52 | const KeyLocator& |
| 53 | EncryptedContent::getKeyLocator() const |
| 54 | { |
| 55 | if (m_hasKeyLocator) |
| 56 | return m_keyLocator; |
| 57 | else |
| 58 | throw Error("KeyLocator does not exist"); |
| 59 | } |
| 60 | |
| 61 | void |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 62 | EncryptedContent::setInitialVector(ConstBufferPtr iv) |
| 63 | { |
| 64 | m_wire.reset(); |
| 65 | m_iv = iv; |
| 66 | } |
| 67 | |
| 68 | ConstBufferPtr |
| 69 | EncryptedContent::getInitialVector() const |
| 70 | { |
| 71 | return m_iv; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | EncryptedContent::setPayload(ConstBufferPtr payload) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 76 | { |
| 77 | m_wire.reset(); |
| 78 | m_payload = payload; |
| 79 | } |
| 80 | |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 81 | ConstBufferPtr |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 82 | EncryptedContent::getPayload() const |
| 83 | { |
| 84 | return m_payload; |
| 85 | } |
| 86 | |
| 87 | template<encoding::Tag TAG> |
| 88 | size_t |
| 89 | EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const |
| 90 | { |
| 91 | size_t totalLength = 0; |
| 92 | |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 93 | 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()); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 100 | |
| 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 Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 109 | throw Error("EncryptedContent does not have a key locator"); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 110 | |
| 111 | totalLength += block.prependVarNumber(totalLength); |
| 112 | totalLength += block.prependVarNumber(tlv::EncryptedContent); |
| 113 | return totalLength; |
| 114 | } |
| 115 | |
| 116 | const Block& |
| 117 | EncryptedContent::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 | |
| 132 | void |
| 133 | EncryptedContent::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 Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 164 | 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 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 171 | if (it != m_wire.elements_end() && it->type() == tlv::EncryptedPayload) { |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame^] | 172 | m_payload = make_shared<Buffer>(it->value_begin(), it->value_end()); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 173 | 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 | |
| 183 | bool |
| 184 | EncryptedContent::operator==(const EncryptedContent& rhs) const |
| 185 | { |
| 186 | return (wireEncode() == rhs.wireEncode()); |
| 187 | } |
| 188 | |
| 189 | } // namespace gep |
| 190 | } // namespace ndn |