blob: 03b85d5073633a2d6f173cfe0958210b6ba85e07 [file] [log] [blame]
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento61a80032020-06-08 18:56:32 -04002/*
Davide Pesavento714dba02022-03-17 20:46:28 -04003 * Copyright (c) 2014-2022, Regents of the University of California
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -04004 *
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 Pesaventobde084f2022-04-17 00:21:35 -040027namespace ndn::nac {
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040028
29/**
30 * @brief Encrypted content
31 *
Davide Pesaventobde084f2022-04-17 00:21:35 -040032 * @verbatim
33 * EncryptedContent ::= ENCRYPTED-CONTENT-TYPE TLV-LENGTH
34 * InitializationVector
35 * EncryptedPayload
36 * EncryptedPayloadKey
37 * Name
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040038 *
Davide Pesaventobde084f2022-04-17 00:21:35 -040039 * 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 Afanasyev0db0feb2018-06-13 20:33:10 -040043 */
44class EncryptedContent
45{
46public:
47 class Error : public ndn::tlv::Error
48 {
49 public:
50 using ndn::tlv::Error::Error;
51 };
52
53public:
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 Pesavento61a80032020-06-08 18:56:32 -040072 hasIv() const noexcept
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040073 {
Davide Pesavento61a80032020-06-08 18:56:32 -040074 return m_iv.isValid();
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040075 }
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 Pesavento61a80032020-06-08 18:56:32 -040093 hasPayloadKey() const noexcept
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040094 {
Davide Pesavento61a80032020-06-08 18:56:32 -040095 return m_payloadKey.isValid();
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040096 }
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 Afanasyev0db0feb2018-06-13 20:33:10 -0400141private:
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 Pesaventobde084f2022-04-17 00:21:35 -0400151} // namespace ndn::nac
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -0400152
153#endif // NDN_NAC_ENCRYPTED_CONTENT_HPP