blob: 5ba0ab1bfe4e623a7e15c3568e7b83fd701c4d79 [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
27namespace ndn {
28namespace nac {
29
30/**
31 * @brief Encrypted content
32 *
33 * <code>
34 * EncryptedContent ::= ENCRYPTED-CONTENT-TYPE TLV-LENGTH
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040035 * InitializationVector
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040036 * EncryptedPayload
37 * EncryptedPayloadKey
38 * Name
39 *
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040040 * InitializationVector ::= INITIALIZATION-VECTOR-TYPE TLV-LENGTH(=N) BYTE{N}
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040041 * EncryptedPayload ::= ENCRYPTED-PAYLOAD-TYPE TLV-LENGTH(=N) BYTE{N}
42 * EncryptedPayloadKey ::= ENCRYPTED-PAYLOAD-KEY-TYPE TLV-LENGTH(=N) BYTE{N}
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040043 * </code>
44 */
45class EncryptedContent
46{
47public:
48 class Error : public ndn::tlv::Error
49 {
50 public:
51 using ndn::tlv::Error::Error;
52 };
53
54public:
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
Davide Pesavento61a80032020-06-08 18:56:32 -040073 hasIv() const noexcept
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040074 {
Davide Pesavento61a80032020-06-08 18:56:32 -040075 return m_iv.isValid();
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040076 }
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
Davide Pesavento61a80032020-06-08 18:56:32 -040094 hasPayloadKey() const noexcept
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040095 {
Davide Pesavento61a80032020-06-08 18:56:32 -040096 return m_payloadKey.isValid();
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -040097 }
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
Alexander Afanasyev0db0feb2018-06-13 20:33:10 -0400142private:
143 Block m_iv;
144 Block m_payload;
145 Block m_payloadKey; ///< for public key encryption, public key encodes a random key that is used
146 ///< for symmetric encryption of the content
147 Name m_keyLocator;
148
149 mutable Block m_wire;
150};
151
152} // namespace nac
153} // namespace ndn
154
155#endif // NDN_NAC_ENCRYPTED_CONTENT_HPP