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