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 | #ifndef NDN_NAC_ENCRYPTED_CONTENT_HPP |
| 21 | #define NDN_NAC_ENCRYPTED_CONTENT_HPP |
| 22 | |
| 23 | #include "common.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/tlv.hpp> |
| 26 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 27 | namespace ndn::nac { |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * @brief Encrypted content |
| 31 | * |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 32 | * @verbatim |
| 33 | * EncryptedContent ::= ENCRYPTED-CONTENT-TYPE TLV-LENGTH |
| 34 | * InitializationVector |
| 35 | * EncryptedPayload |
| 36 | * EncryptedPayloadKey |
| 37 | * Name |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 38 | * |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 39 | * InitializationVector ::= INITIALIZATION-VECTOR-TYPE TLV-LENGTH(=N) BYTE{N} |
| 40 | * EncryptedPayload ::= ENCRYPTED-PAYLOAD-TYPE TLV-LENGTH(=N) BYTE{N} |
| 41 | * EncryptedPayloadKey ::= ENCRYPTED-PAYLOAD-KEY-TYPE TLV-LENGTH(=N) BYTE{N} |
| 42 | * @endverbatim |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 43 | */ |
| 44 | class EncryptedContent |
| 45 | { |
| 46 | public: |
| 47 | class Error : public ndn::tlv::Error |
| 48 | { |
| 49 | public: |
| 50 | using ndn::tlv::Error::Error; |
| 51 | }; |
| 52 | |
| 53 | public: |
| 54 | EncryptedContent() = default; |
| 55 | |
| 56 | explicit |
| 57 | EncryptedContent(const Block& block); |
| 58 | |
| 59 | const Block& |
| 60 | getPayload() const |
| 61 | { |
| 62 | return m_payload; |
| 63 | } |
| 64 | |
| 65 | EncryptedContent& |
| 66 | setPayload(Block payload); |
| 67 | |
| 68 | EncryptedContent& |
| 69 | setPayload(ConstBufferPtr payload); |
| 70 | |
| 71 | bool |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 72 | hasIv() const noexcept |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 73 | { |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 74 | return m_iv.isValid(); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const Block& |
| 78 | getIv() const |
| 79 | { |
| 80 | return m_iv; |
| 81 | } |
| 82 | |
| 83 | EncryptedContent& |
| 84 | unsetIv(); |
| 85 | |
| 86 | EncryptedContent& |
| 87 | setIv(Block iv); |
| 88 | |
| 89 | EncryptedContent& |
| 90 | setIv(ConstBufferPtr iv); |
| 91 | |
| 92 | bool |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 93 | hasPayloadKey() const noexcept |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 94 | { |
Davide Pesavento | 61a8003 | 2020-06-08 18:56:32 -0400 | [diff] [blame] | 95 | return m_payloadKey.isValid(); |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | const Block& |
| 99 | getPayloadKey() const |
| 100 | { |
| 101 | return m_payloadKey; |
| 102 | } |
| 103 | |
| 104 | EncryptedContent& |
| 105 | setPayloadKey(Block key); |
| 106 | |
| 107 | EncryptedContent& |
| 108 | setPayloadKey(ConstBufferPtr key); |
| 109 | |
| 110 | EncryptedContent& |
| 111 | unsetPayloadKey(); |
| 112 | |
| 113 | bool |
| 114 | hasKeyLocator() const |
| 115 | { |
| 116 | return !m_keyLocator.empty(); |
| 117 | } |
| 118 | |
| 119 | const Name& |
| 120 | getKeyLocator() const |
| 121 | { |
| 122 | return m_keyLocator; |
| 123 | } |
| 124 | |
| 125 | EncryptedContent& |
| 126 | setKeyLocator(Name keyLocator); |
| 127 | |
| 128 | EncryptedContent& |
| 129 | unsetKeyLocator(); |
| 130 | |
| 131 | template<encoding::Tag TAG> |
| 132 | size_t |
| 133 | wireEncode(EncodingImpl<TAG>& block) const; |
| 134 | |
| 135 | const Block& |
| 136 | wireEncode() const; |
| 137 | |
| 138 | void |
| 139 | wireDecode(const Block& wire); |
| 140 | |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 141 | private: |
| 142 | Block m_iv; |
| 143 | Block m_payload; |
| 144 | Block m_payloadKey; ///< for public key encryption, public key encodes a random key that is used |
| 145 | ///< for symmetric encryption of the content |
| 146 | Name m_keyLocator; |
| 147 | |
| 148 | mutable Block m_wire; |
| 149 | }; |
| 150 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 151 | } // namespace ndn::nac |
Alexander Afanasyev | 0db0feb | 2018-06-13 20:33:10 -0400 | [diff] [blame] | 152 | |
| 153 | #endif // NDN_NAC_ENCRYPTED_CONTENT_HPP |