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 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 26 | namespace ndn::nac { |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 27 | |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((WireEncodable<EncryptedContent>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireDecodable<EncryptedContent>)); |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 30 | static_assert(std::is_base_of_v<ndn::tlv::Error, EncryptedContent::Error>, |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 31 | "EncryptedContent::Error must inherit from tlv::Error"); |
| 32 | |
| 33 | EncryptedContent::EncryptedContent(const Block& block) |
| 34 | { |
| 35 | wireDecode(block); |
| 36 | } |
| 37 | |
| 38 | EncryptedContent& |
| 39 | EncryptedContent::setPayload(Block payload) |
| 40 | { |
| 41 | m_wire.reset(); |
| 42 | if (payload.type() != tlv::EncryptedPayload) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 43 | m_payload = Block(tlv::EncryptedPayload, payload); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 44 | } |
| 45 | else { |
| 46 | m_payload = std::move(payload); |
| 47 | } |
| 48 | return *this; |
| 49 | } |
| 50 | |
| 51 | EncryptedContent& |
| 52 | EncryptedContent::setPayload(ConstBufferPtr payload) |
| 53 | { |
| 54 | m_wire.reset(); |
| 55 | m_payload = Block(tlv::EncryptedPayload, std::move(payload)); |
| 56 | return *this; |
| 57 | } |
| 58 | |
| 59 | EncryptedContent& |
| 60 | EncryptedContent::setIv(Block iv) |
| 61 | { |
| 62 | m_wire.reset(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 63 | if (iv.type() != tlv::InitializationVector) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 64 | m_iv = Block(tlv::InitializationVector, iv); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 65 | } |
| 66 | else { |
| 67 | m_iv = std::move(iv); |
| 68 | } |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | EncryptedContent& |
| 73 | EncryptedContent::setIv(ConstBufferPtr iv) |
| 74 | { |
| 75 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 76 | m_iv = Block(tlv::InitializationVector, std::move(iv)); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | EncryptedContent& |
| 81 | EncryptedContent::unsetIv() |
| 82 | { |
| 83 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 84 | m_iv = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | EncryptedContent& |
| 89 | EncryptedContent::setPayloadKey(Block key) |
| 90 | { |
| 91 | m_wire.reset(); |
| 92 | if (key.type() != tlv::EncryptedPayloadKey) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 93 | m_payloadKey = Block(tlv::EncryptedPayloadKey, key); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 94 | } |
| 95 | else { |
| 96 | m_payloadKey = std::move(key); |
| 97 | } |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | EncryptedContent& |
| 102 | EncryptedContent::setPayloadKey(ConstBufferPtr key) |
| 103 | { |
| 104 | m_wire.reset(); |
| 105 | m_payloadKey = Block(tlv::EncryptedPayloadKey, std::move(key)); |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | EncryptedContent& |
| 110 | EncryptedContent::unsetPayloadKey() |
| 111 | { |
| 112 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 113 | m_payloadKey = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | EncryptedContent& |
| 118 | EncryptedContent::setKeyLocator(Name keyLocator) |
| 119 | { |
| 120 | m_wire.reset(); |
| 121 | m_keyLocator = std::move(keyLocator); |
| 122 | return *this; |
| 123 | } |
| 124 | |
| 125 | EncryptedContent& |
| 126 | EncryptedContent::unsetKeyLocator() |
| 127 | { |
| 128 | m_wire.reset(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 129 | m_keyLocator = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | template<encoding::Tag TAG> |
| 134 | size_t |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 135 | EncryptedContent::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 136 | { |
| 137 | size_t totalLength = 0; |
| 138 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 139 | if (hasKeyLocator()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 140 | totalLength += m_keyLocator.wireEncode(encoder); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 143 | if (hasPayloadKey()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 144 | totalLength += prependBlock(encoder, m_payloadKey); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 147 | if (hasIv()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 148 | totalLength += prependBlock(encoder, m_iv); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 151 | if (m_payload.isValid()) { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 152 | totalLength += prependBlock(encoder, m_payload); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 153 | } |
| 154 | else { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 155 | NDN_THROW(Error("Required EncryptedPayload is not set on EncryptedContent")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 158 | totalLength += encoder.prependVarNumber(totalLength); |
| 159 | totalLength += encoder.prependVarNumber(tlv::EncryptedContent); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 160 | return totalLength; |
| 161 | } |
| 162 | |
| 163 | const Block& |
| 164 | EncryptedContent::wireEncode() const |
| 165 | { |
| 166 | if (m_wire.hasWire()) |
| 167 | return m_wire; |
| 168 | |
| 169 | EncodingEstimator estimator; |
| 170 | size_t estimatedSize = wireEncode(estimator); |
| 171 | |
| 172 | EncodingBuffer buffer(estimatedSize, 0); |
| 173 | wireEncode(buffer); |
| 174 | |
| 175 | m_wire = buffer.block(); |
| 176 | return m_wire; |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | EncryptedContent::wireDecode(const Block& wire) |
| 181 | { |
| 182 | if (!wire.hasWire()) { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 183 | NDN_THROW(Error("The supplied block does not contain wire format")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 184 | } |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 185 | if (wire.type() != tlv::EncryptedContent) { |
| 186 | NDN_THROW(Error("EncryptedContent", wire.type())); |
| 187 | } |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 188 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 189 | *this = {}; |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 190 | m_wire = wire; |
| 191 | m_wire.parse(); |
| 192 | |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 193 | auto block = m_wire.find(tlv::EncryptedPayload); |
| 194 | if (block != m_wire.elements_end()) { |
| 195 | m_payload = *block; |
| 196 | } |
| 197 | else { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 198 | NDN_THROW(Error("Required EncryptedPayload not found in EncryptedContent")); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 201 | block = m_wire.find(tlv::InitializationVector); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 202 | if (block != m_wire.elements_end()) { |
| 203 | m_iv = *block; |
| 204 | } |
| 205 | |
| 206 | block = m_wire.find(tlv::EncryptedPayloadKey); |
| 207 | if (block != m_wire.elements_end()) { |
| 208 | m_payloadKey = *block; |
| 209 | } |
| 210 | |
| 211 | block = m_wire.find(tlv::Name); |
| 212 | if (block != m_wire.elements_end()) { |
| 213 | m_keyLocator.wireDecode(*block); |
| 214 | } |
| 215 | } |
| 216 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 217 | } // namespace ndn::nac |