Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 5 | * This file is part of NAC (Name-Based Access Control for NDN). |
| 6 | * See AUTHORS.md for complete list of NAC authors and contributors. |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 18 | * |
| 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 20 | * @author Zhiyi Zhang <zhiyi@cs.ucla.edu> |
| 21 | */ |
| 22 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 23 | #include "encrypted-content.hpp" |
| 24 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 25 | #include <ndn-cxx/util/concepts.hpp> |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 29 | namespace nac { |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 30 | |
| 31 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<EncryptedContent>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodable<EncryptedContent>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireDecodable<EncryptedContent>)); |
| 34 | static_assert(std::is_base_of<ndn::tlv::Error, EncryptedContent::Error>::value, |
| 35 | "EncryptedContent::Error must inherit from tlv::Error"); |
| 36 | |
| 37 | EncryptedContent::EncryptedContent() |
| 38 | : m_type(-1) |
| 39 | , m_hasKeyLocator(false) |
| 40 | { |
| 41 | } |
| 42 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 43 | EncryptedContent::EncryptedContent(tlv::AlgorithmTypeValue type, |
| 44 | const KeyLocator& keyLocator, |
| 45 | const uint8_t* payload, |
| 46 | size_t payloadLen, |
| 47 | const uint8_t* iv, |
| 48 | size_t ivLen) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 49 | : m_type(type) |
| 50 | , m_hasKeyLocator(true) |
| 51 | , m_keyLocator(keyLocator) |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 52 | , m_payload(payload, payloadLen) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 53 | { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 54 | if (iv != nullptr && ivLen != 0) |
| 55 | m_iv = Buffer(iv, ivLen); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | EncryptedContent::EncryptedContent(const Block& block) |
| 59 | { |
| 60 | wireDecode(block); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | EncryptedContent::setAlgorithmType(tlv::AlgorithmTypeValue type) |
| 65 | { |
| 66 | m_wire.reset(); |
| 67 | m_type = type; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | EncryptedContent::setKeyLocator(const KeyLocator& keyLocator) |
| 72 | { |
| 73 | m_wire.reset(); |
| 74 | m_keyLocator = keyLocator; |
| 75 | m_hasKeyLocator = true; |
| 76 | } |
| 77 | |
| 78 | const KeyLocator& |
| 79 | EncryptedContent::getKeyLocator() const |
| 80 | { |
| 81 | if (m_hasKeyLocator) |
| 82 | return m_keyLocator; |
| 83 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 84 | BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 88 | EncryptedContent::setInitialVector(const uint8_t* iv, size_t ivLen) |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 89 | { |
| 90 | m_wire.reset(); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 91 | m_iv = Buffer(iv, ivLen); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 94 | const Buffer& |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 95 | EncryptedContent::getInitialVector() const |
| 96 | { |
| 97 | return m_iv; |
| 98 | } |
| 99 | |
| 100 | void |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 101 | EncryptedContent::setPayload(const uint8_t* payload, size_t payloadLen) |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 102 | { |
| 103 | m_wire.reset(); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 104 | m_payload = Buffer(payload, payloadLen); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 107 | const Buffer& |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 108 | EncryptedContent::getPayload() const |
| 109 | { |
| 110 | return m_payload; |
| 111 | } |
| 112 | |
| 113 | template<encoding::Tag TAG> |
| 114 | size_t |
| 115 | EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const |
| 116 | { |
| 117 | size_t totalLength = 0; |
| 118 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 119 | if (m_payload.size() != 0) |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 120 | totalLength += |
| 121 | block.prependByteArrayBlock(tlv::EncryptedPayload, m_payload.data(), m_payload.size()); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 122 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 123 | BOOST_THROW_EXCEPTION(Error("EncryptedContent does not have a payload")); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 124 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 125 | if (m_iv.size() != 0) { |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 126 | totalLength += block.prependByteArrayBlock(tlv::InitialVector, m_iv.data(), m_iv.size()); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 127 | } |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 128 | |
| 129 | if (m_type != -1) |
| 130 | totalLength += prependNonNegativeIntegerBlock(block, tlv::EncryptionAlgorithm, m_type); |
| 131 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 132 | BOOST_THROW_EXCEPTION(Error("EncryptedContent does not have an encryption algorithm")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 133 | |
| 134 | if (m_hasKeyLocator) |
| 135 | totalLength += m_keyLocator.wireEncode(block); |
| 136 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 137 | BOOST_THROW_EXCEPTION(Error("EncryptedContent does not have a key locator")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 138 | |
| 139 | totalLength += block.prependVarNumber(totalLength); |
| 140 | totalLength += block.prependVarNumber(tlv::EncryptedContent); |
| 141 | return totalLength; |
| 142 | } |
| 143 | |
| 144 | const Block& |
| 145 | EncryptedContent::wireEncode() const |
| 146 | { |
| 147 | if (m_wire.hasWire()) |
| 148 | return m_wire; |
| 149 | |
| 150 | EncodingEstimator estimator; |
| 151 | size_t estimatedSize = wireEncode(estimator); |
| 152 | |
| 153 | EncodingBuffer buffer(estimatedSize, 0); |
| 154 | wireEncode(buffer); |
| 155 | |
| 156 | m_wire = buffer.block(); |
| 157 | return m_wire; |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | EncryptedContent::wireDecode(const Block& wire) |
| 162 | { |
| 163 | if (!wire.hasWire()) { |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 164 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | m_hasKeyLocator = false; |
| 168 | |
| 169 | m_wire = wire; |
| 170 | m_wire.parse(); |
| 171 | |
| 172 | if (m_wire.type() != tlv::EncryptedContent) |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 173 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding Name")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 174 | |
| 175 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 176 | |
| 177 | if (it != m_wire.elements_end() && it->type() == ndn::tlv::KeyLocator) { |
| 178 | m_keyLocator.wireDecode(*it); |
| 179 | m_hasKeyLocator = true; |
| 180 | it++; |
| 181 | } |
| 182 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 183 | BOOST_THROW_EXCEPTION(Error("EncryptedContent does not have key locator")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 184 | |
| 185 | if (it != m_wire.elements_end() && it->type() == tlv::EncryptionAlgorithm) { |
| 186 | m_type = readNonNegativeInteger(*it); |
| 187 | it++; |
| 188 | } |
| 189 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 190 | BOOST_THROW_EXCEPTION(Error("EncryptedContent does not have encryption algorithm")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 191 | |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 192 | if (it != m_wire.elements_end() && it->type() == tlv::InitialVector) { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 193 | m_iv = Buffer(it->value_begin(), it->value_end()); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 194 | it++; |
| 195 | } |
| 196 | else |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 197 | m_iv = Buffer(); |
Prashanth Swaminathan | b1b9596 | 2015-07-06 13:13:08 -0700 | [diff] [blame] | 198 | |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 199 | if (it != m_wire.elements_end() && it->type() == tlv::EncryptedPayload) { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 200 | m_payload = Buffer(it->value_begin(), it->value_end()); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 201 | it++; |
| 202 | } |
| 203 | else |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 204 | BOOST_THROW_EXCEPTION(Error("EncryptedContent has missing payload")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 205 | |
| 206 | if (it != m_wire.elements_end()) { |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 207 | BOOST_THROW_EXCEPTION(Error("EncryptedContent has extraneous sub-TLVs")); |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | bool |
| 212 | EncryptedContent::operator==(const EncryptedContent& rhs) const |
| 213 | { |
| 214 | return (wireEncode() == rhs.wireEncode()); |
| 215 | } |
| 216 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 217 | } // namespace nac |
Prashanth | c0029b6 | 2015-04-27 14:00:08 -0700 | [diff] [blame] | 218 | } // namespace ndn |