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