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, |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 23 | const uint8_t* payload, size_t payloadLen, |
| 24 | const uint8_t* iv, size_t ivLen) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 25 | : m_type(type) |
| 26 | , m_hasKeyLocator(true) |
| 27 | , m_keyLocator(keyLocator) |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 28 | , m_payload(payload, payloadLen) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 29 | { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 30 | if (iv != nullptr && ivLen != 0) |
| 31 | m_iv = Buffer(iv, ivLen); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | EncryptedContent::EncryptedContent(const Block& block) |
| 35 | { |
| 36 | wireDecode(block); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | EncryptedContent::setAlgorithmType(tlv::AlgorithmTypeValue type) |
| 41 | { |
| 42 | m_wire.reset(); |
| 43 | m_type = type; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | EncryptedContent::setKeyLocator(const KeyLocator& keyLocator) |
| 48 | { |
| 49 | m_wire.reset(); |
| 50 | m_keyLocator = keyLocator; |
| 51 | m_hasKeyLocator = true; |
| 52 | } |
| 53 | |
| 54 | const KeyLocator& |
| 55 | EncryptedContent::getKeyLocator() const |
| 56 | { |
| 57 | if (m_hasKeyLocator) |
| 58 | return m_keyLocator; |
| 59 | else |
| 60 | throw Error("KeyLocator does not exist"); |
| 61 | } |
| 62 | |
| 63 | void |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 64 | EncryptedContent::setInitialVector(const uint8_t* iv, size_t ivLen) |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 65 | { |
| 66 | m_wire.reset(); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 67 | m_iv = Buffer(iv, ivLen); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 70 | const Buffer& |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 71 | EncryptedContent::getInitialVector() const |
| 72 | { |
| 73 | return m_iv; |
| 74 | } |
| 75 | |
| 76 | void |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 77 | EncryptedContent::setPayload(const uint8_t* payload, size_t payloadLen) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 78 | { |
| 79 | m_wire.reset(); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 80 | m_payload = Buffer(payload, payloadLen); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 83 | const Buffer& |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 84 | EncryptedContent::getPayload() const |
| 85 | { |
| 86 | return m_payload; |
| 87 | } |
| 88 | |
| 89 | template<encoding::Tag TAG> |
| 90 | size_t |
| 91 | EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const |
| 92 | { |
| 93 | size_t totalLength = 0; |
| 94 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 95 | if (m_payload.size() != 0) |
| 96 | totalLength += block.prependByteArrayBlock(tlv::EncryptedPayload, m_payload.buf(), m_payload.size()); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 97 | else |
| 98 | throw Error("EncryptedContent does not have a payload"); |
| 99 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 100 | if (m_iv.size() != 0) { |
| 101 | totalLength += block.prependByteArrayBlock(tlv::InitialVector, m_iv.buf(), m_iv.size()); |
| 102 | } |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 103 | |
| 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 Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 112 | throw Error("EncryptedContent does not have a key locator"); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 113 | |
| 114 | totalLength += block.prependVarNumber(totalLength); |
| 115 | totalLength += block.prependVarNumber(tlv::EncryptedContent); |
| 116 | return totalLength; |
| 117 | } |
| 118 | |
| 119 | const Block& |
| 120 | EncryptedContent::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 | |
| 135 | void |
| 136 | EncryptedContent::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 Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 167 | if (it != m_wire.elements_end() && it->type() == tlv::InitialVector) { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 168 | m_iv = Buffer(it->value_begin(), it->value_end()); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 169 | it++; |
| 170 | } |
| 171 | else |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 172 | m_iv = Buffer(); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 173 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 174 | if (it != m_wire.elements_end() && it->type() == tlv::EncryptedPayload) { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 175 | m_payload = Buffer(it->value_begin(), it->value_end()); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 176 | 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 | |
| 186 | bool |
| 187 | EncryptedContent::operator==(const EncryptedContent& rhs) const |
| 188 | { |
| 189 | return (wireEncode() == rhs.wireEncode()); |
| 190 | } |
| 191 | |
| 192 | } // namespace gep |
| 193 | } // namespace ndn |