Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include "encrypted-content.hpp" |
| 21 | |
| 22 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 23 | #include <ndn-cxx/util/concepts.hpp> |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/util/exception.hpp> |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace nac { |
| 28 | |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 29 | BOOST_CONCEPT_ASSERT((WireEncodable<EncryptedContent>)); |
| 30 | BOOST_CONCEPT_ASSERT((WireDecodable<EncryptedContent>)); |
| 31 | static_assert(std::is_base_of<ndn::tlv::Error, EncryptedContent::Error>::value, |
| 32 | "EncryptedContent::Error must inherit from tlv::Error"); |
| 33 | |
| 34 | EncryptedContent::EncryptedContent(const Block& block) |
| 35 | { |
| 36 | wireDecode(block); |
| 37 | } |
| 38 | |
| 39 | EncryptedContent& |
| 40 | EncryptedContent::setPayload(Block payload) |
| 41 | { |
| 42 | m_wire.reset(); |
| 43 | if (payload.type() != tlv::EncryptedPayload) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 44 | m_payload = Block(tlv::EncryptedPayload, payload); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 45 | } |
| 46 | else { |
| 47 | m_payload = std::move(payload); |
| 48 | } |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | EncryptedContent& |
| 53 | EncryptedContent::setPayload(ConstBufferPtr payload) |
| 54 | { |
| 55 | m_wire.reset(); |
| 56 | m_payload = Block(tlv::EncryptedPayload, std::move(payload)); |
| 57 | return *this; |
| 58 | } |
| 59 | |
| 60 | EncryptedContent& |
| 61 | EncryptedContent::setIv(Block iv) |
| 62 | { |
| 63 | m_wire.reset(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 64 | if (iv.type() != tlv::InitializationVector) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 65 | m_iv = Block(tlv::InitializationVector, iv); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 66 | } |
| 67 | else { |
| 68 | m_iv = std::move(iv); |
| 69 | } |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | EncryptedContent& |
| 74 | EncryptedContent::setIv(ConstBufferPtr iv) |
| 75 | { |
| 76 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 77 | m_iv = Block(tlv::InitializationVector, std::move(iv)); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | EncryptedContent& |
| 82 | EncryptedContent::unsetIv() |
| 83 | { |
| 84 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 85 | m_iv = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | EncryptedContent& |
| 90 | EncryptedContent::setPayloadKey(Block key) |
| 91 | { |
| 92 | m_wire.reset(); |
| 93 | if (key.type() != tlv::EncryptedPayloadKey) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 94 | m_payloadKey = Block(tlv::EncryptedPayloadKey, key); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 95 | } |
| 96 | else { |
| 97 | m_payloadKey = std::move(key); |
| 98 | } |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | EncryptedContent& |
| 103 | EncryptedContent::setPayloadKey(ConstBufferPtr key) |
| 104 | { |
| 105 | m_wire.reset(); |
| 106 | m_payloadKey = Block(tlv::EncryptedPayloadKey, std::move(key)); |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | EncryptedContent& |
| 111 | EncryptedContent::unsetPayloadKey() |
| 112 | { |
| 113 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 114 | m_payloadKey = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | EncryptedContent& |
| 119 | EncryptedContent::setKeyLocator(Name keyLocator) |
| 120 | { |
| 121 | m_wire.reset(); |
| 122 | m_keyLocator = std::move(keyLocator); |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | EncryptedContent& |
| 127 | EncryptedContent::unsetKeyLocator() |
| 128 | { |
| 129 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 130 | m_keyLocator = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 131 | return *this; |
| 132 | } |
| 133 | |
| 134 | template<encoding::Tag TAG> |
| 135 | size_t |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 136 | EncryptedContent::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 137 | { |
| 138 | size_t totalLength = 0; |
| 139 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 140 | if (hasKeyLocator()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 141 | totalLength += m_keyLocator.wireEncode(encoder); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 144 | if (hasPayloadKey()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 145 | totalLength += prependBlock(encoder, m_payloadKey); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 148 | if (hasIv()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 149 | totalLength += prependBlock(encoder, m_iv); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 152 | if (m_payload.isValid()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 153 | totalLength += prependBlock(encoder, m_payload); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 154 | } |
| 155 | else { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 156 | NDN_THROW(Error("Required EncryptedPayload is not set on EncryptedContent")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 159 | totalLength += encoder.prependVarNumber(totalLength); |
| 160 | totalLength += encoder.prependVarNumber(tlv::EncryptedContent); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 161 | return totalLength; |
| 162 | } |
| 163 | |
| 164 | const Block& |
| 165 | EncryptedContent::wireEncode() const |
| 166 | { |
| 167 | if (m_wire.hasWire()) |
| 168 | return m_wire; |
| 169 | |
| 170 | EncodingEstimator estimator; |
| 171 | size_t estimatedSize = wireEncode(estimator); |
| 172 | |
| 173 | EncodingBuffer buffer(estimatedSize, 0); |
| 174 | wireEncode(buffer); |
| 175 | |
| 176 | m_wire = buffer.block(); |
| 177 | return m_wire; |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | EncryptedContent::wireDecode(const Block& wire) |
| 182 | { |
| 183 | if (!wire.hasWire()) { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 184 | NDN_THROW(Error("The supplied block does not contain wire format")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | m_payload.reset(); |
| 188 | m_iv.reset(); |
| 189 | m_payloadKey.reset(); |
| 190 | |
| 191 | m_wire = wire; |
| 192 | m_wire.parse(); |
| 193 | |
| 194 | if (m_wire.type() != tlv::EncryptedContent) { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 195 | NDN_THROW(Error("Unexpected TLV type (expecting EncryptedContent, got " + |
| 196 | ndn::to_string(m_wire.type()) + ")")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | auto block = m_wire.find(tlv::EncryptedPayload); |
| 200 | if (block != m_wire.elements_end()) { |
| 201 | m_payload = *block; |
| 202 | } |
| 203 | else { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 204 | NDN_THROW(Error("Required EncryptedPayload not found in EncryptedContent")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 207 | block = m_wire.find(tlv::InitializationVector); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 208 | if (block != m_wire.elements_end()) { |
| 209 | m_iv = *block; |
| 210 | } |
| 211 | |
| 212 | block = m_wire.find(tlv::EncryptedPayloadKey); |
| 213 | if (block != m_wire.elements_end()) { |
| 214 | m_payloadKey = *block; |
| 215 | } |
| 216 | |
| 217 | block = m_wire.find(tlv::Name); |
| 218 | if (block != m_wire.elements_end()) { |
| 219 | m_keyLocator.wireDecode(*block); |
| 220 | } |
| 221 | } |
| 222 | |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 223 | } // namespace nac |
| 224 | } // namespace ndn |