blob: 500386b3145550f71b4b1a6dfc151e5bdcf7fa31 [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Luca Keidel941fd8c2017-07-24 15:21:22 +02002/*
Davide Pesavento794f6872017-05-15 23:33:38 -04003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu202a2e92015-07-12 16:49:25 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP
23#define NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP
24
25#include "public-key.hpp"
26#include "../../encoding/buffer.hpp"
27
28namespace ndn {
29
30class KeyParams;
31
32namespace security {
33namespace transform {
34
35/**
36 * @brief Abstraction of private key in crypto transformation
37 */
38class PrivateKey : noncopyable
39{
40public:
41 class Error : public std::runtime_error
42 {
43 public:
44 explicit
45 Error(const std::string& what)
46 : std::runtime_error(what)
47 {
48 }
49 };
50
51 friend class SignerFilter;
52
53 /**
54 * @brief Callback for application to handle password input
55 *
Luca Keidel941fd8c2017-07-24 15:21:22 +020056 * The password must be written to @p buf and must not be longer than @p bufSize chars.
57 * It is recommended to ask the user to verify the password if @p shouldConfirm is true,
58 * e.g., by prompting for it twice. The callback must return the number of characters
59 * in the password or 0 if an error occurred.
Yingdi Yu202a2e92015-07-12 16:49:25 -070060 */
61 typedef function<int(char* buf, size_t bufSize, bool shouldConfirm)> PasswordCallback;
62
63public:
64 /**
65 * @brief Create a private key instance
66 *
67 * One must call loadXXXX(...) to load private key.
68 */
69 PrivateKey();
70
71 ~PrivateKey();
72
73 /**
74 * @brief Load the private key in PKCS#1 format from a buffer @p buf
75 */
76 void
77 loadPkcs1(const uint8_t* buf, size_t size);
78
79 /**
80 * @brief Load the private key in PKCS#1 format from a stream @p is
81 */
82 void
83 loadPkcs1(std::istream& is);
84
85 /**
86 * @brief Load the private key in base64-encoded PKCS#1 format from a buffer @p buf
87 */
88 void
89 loadPkcs1Base64(const uint8_t* buf, size_t size);
90
91 /**
92 * @brief Load the private key in base64-encoded PKCS#1 format from a stream @p is
93 */
94 void
95 loadPkcs1Base64(std::istream& is);
96
97 /**
98 * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with passphrase @p pw
99 *
100 * @pre strlen(pw) == pwLen
101 */
102 void
103 loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
104
105 /**
106 * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with
107 * passphrase obtained from @p pwCallback
108 *
109 * The default password callback is provided by OpenSSL
110 */
111 void
112 loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
113
114 /**
115 * @brief Load the private key in encrypted PKCS#8 format from a stream @p is with passphrase @p pw
116 *
117 * @pre strlen(pw) == pwLen
118 */
119 void
120 loadPkcs8(std::istream& is, const char* pw, size_t pwLen);
121
122 /**
123 * @brief Load the private key in encrypted PKCS#8 format from a stream @p is with passphrase
124 * obtained from @p pwCallback
125 *
126 * The default password callback is provided by OpenSSL
127 */
128 void
129 loadPkcs8(std::istream& is, PasswordCallback pwCallback = nullptr);
130
131 /**
132 * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a buffer @p buf
133 * with passphrase @p pw
134 *
135 * @pre strlen(pw) == pwLen
136 */
137 void
138 loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
139
140 /**
141 * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with
142 * passphrase obtained from @p pwCallback
143 *
144 * The default password callback is provided by OpenSSL
145 */
146 void
147 loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
148
149 /**
150 * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a stream @p is
151 * with passphrase @p pw
152 *
153 * @pre strlen(pw) == pwLen
154 */
155 void
156 loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen);
157
158 /**
159 * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a stream @p is
160 * with passphrase obtained from @p pwCallback
161 *
162 * The default password callback is provided by OpenSSL
163 */
164 void
165 loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback = nullptr);
166
167 /**
168 * @brief Save the private key in PKCS#1 format into a stream @p os
169 */
170 void
171 savePkcs1(std::ostream& os) const;
172
173 /**
174 * @brief Save the private key in base64-encoded PKCS#1 format into a stream @p os
175 */
176 void
177 savePkcs1Base64(std::ostream& os) const;
178
179 /**
180 * @brief Save the private key in encrypted PKCS#8 format into a stream @p os
181 */
182 void
183 savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const;
184
185 /**
186 * @brief Save the private key in encrypted PKCS#8 format into a stream @p os with passphrase
187 * obtained from @p pwCallback
188 *
189 * The default password callback is provided by OpenSSL
190 */
191 void
192 savePkcs8(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
193
194 /**
195 * @brief Save the private key in base64-encoded encrypted PKCS#8 format into a stream @p os
196 */
197 void
198 savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const;
199
200 /**
201 * @brief Save the private key in base64-encoded encrypted PKCS#8 format into a stream @p os
202 * with passphrase obtained from @p pwCallback
203 *
204 * The default password callback is provided by OpenSSL
205 */
206 void
207 savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
208
209 /**
210 * @return Public key bits in PKCS#8 format
211 */
212 ConstBufferPtr
213 derivePublicKey() const;
214
215 /**
216 * @return Plain text of @p cipherText decrypted using the private key.
217 *
218 * Only RSA encryption is supported for now.
219 */
220 ConstBufferPtr
221 decrypt(const uint8_t* cipherText, size_t cipherLen) const;
222
223private:
224 /**
225 * @return A pointer to an EVP_PKEY instance.
226 *
227 * One need to explicitly cast the return value to EVP_PKEY*.
228 */
229 void*
230 getEvpPkey() const;
231
232private:
233 ConstBufferPtr
234 toPkcs1() const;
235
236 ConstBufferPtr
237 toPkcs8(const char* pw, size_t pwLen) const;
238
239 ConstBufferPtr
240 toPkcs8(PasswordCallback pwCallback = nullptr) const;
241
242 ConstBufferPtr
243 rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const;
244
245private:
246 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400247 const unique_ptr<Impl> m_impl;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700248};
249
250/**
251 * @brief generate a private key according to @p keyParams.
252 *
253 * @note the public key can be derived from the private key
254 *
255 * @throw std::argument_error if the key type is not supported
256 * @throw std::runtime_error when failing to generate the key
257 */
258unique_ptr<PrivateKey>
259generatePrivateKey(const KeyParams& keyParams);
260
261} // namespace transform
262} // namespace security
263} // namespace ndn
264
265#endif // NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP