blob: 17d582b2965e2228659bc9924d49313d7bc3f00a [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
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040025#include "../security-common.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070026#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
Yingdi Yu202a2e92015-07-12 16:49:25 -070051 /**
52 * @brief Callback for application to handle password input
53 *
Luca Keidel941fd8c2017-07-24 15:21:22 +020054 * The password must be written to @p buf and must not be longer than @p bufSize chars.
55 * It is recommended to ask the user to verify the password if @p shouldConfirm is true,
56 * e.g., by prompting for it twice. The callback must return the number of characters
57 * in the password or 0 if an error occurred.
Yingdi Yu202a2e92015-07-12 16:49:25 -070058 */
59 typedef function<int(char* buf, size_t bufSize, bool shouldConfirm)> PasswordCallback;
60
61public:
62 /**
Davide Pesaventof45fa212017-09-14 17:23:56 -040063 * @brief Create an empty private key instance
Yingdi Yu202a2e92015-07-12 16:49:25 -070064 *
Davide Pesaventof45fa212017-09-14 17:23:56 -040065 * One must call loadXXXX(...) to load a private key.
Yingdi Yu202a2e92015-07-12 16:49:25 -070066 */
67 PrivateKey();
68
69 ~PrivateKey();
70
71 /**
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040072 * @brief Get the type of the private key
73 */
74 KeyType
75 getKeyType() const;
76
77 /**
Yingdi Yu202a2e92015-07-12 16:49:25 -070078 * @brief Load the private key in PKCS#1 format from a buffer @p buf
79 */
80 void
81 loadPkcs1(const uint8_t* buf, size_t size);
82
83 /**
84 * @brief Load the private key in PKCS#1 format from a stream @p is
85 */
86 void
87 loadPkcs1(std::istream& is);
88
89 /**
90 * @brief Load the private key in base64-encoded PKCS#1 format from a buffer @p buf
91 */
92 void
93 loadPkcs1Base64(const uint8_t* buf, size_t size);
94
95 /**
96 * @brief Load the private key in base64-encoded PKCS#1 format from a stream @p is
97 */
98 void
99 loadPkcs1Base64(std::istream& is);
100
101 /**
102 * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with passphrase @p pw
Yingdi Yu202a2e92015-07-12 16:49:25 -0700103 * @pre strlen(pw) == pwLen
104 */
105 void
106 loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
107
108 /**
109 * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with
110 * passphrase obtained from @p pwCallback
111 *
112 * The default password callback is provided by OpenSSL
113 */
114 void
115 loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
116
117 /**
118 * @brief Load the private key in encrypted PKCS#8 format from a stream @p is with passphrase @p pw
Yingdi Yu202a2e92015-07-12 16:49:25 -0700119 * @pre strlen(pw) == pwLen
120 */
121 void
122 loadPkcs8(std::istream& is, const char* pw, size_t pwLen);
123
124 /**
125 * @brief Load the private key in encrypted PKCS#8 format from a stream @p is with passphrase
126 * obtained from @p pwCallback
127 *
128 * The default password callback is provided by OpenSSL
129 */
130 void
131 loadPkcs8(std::istream& is, PasswordCallback pwCallback = nullptr);
132
133 /**
134 * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a buffer @p buf
135 * with passphrase @p pw
Yingdi Yu202a2e92015-07-12 16:49:25 -0700136 * @pre strlen(pw) == pwLen
137 */
138 void
139 loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
140
141 /**
142 * @brief Load the private key in encrypted PKCS#8 format from a buffer @p buf with
143 * passphrase obtained from @p pwCallback
144 *
145 * The default password callback is provided by OpenSSL
146 */
147 void
148 loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
149
150 /**
151 * @brief Load the private key in base64-encoded encrypted PKCS#8 format from a stream @p is
152 * with passphrase @p pw
Yingdi Yu202a2e92015-07-12 16:49:25 -0700153 * @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 /**
Davide Pesaventof45fa212017-09-14 17:23:56 -0400216 * @return Plain text of @p cipherText decrypted using this private key.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700217 *
218 * Only RSA encryption is supported for now.
219 */
220 ConstBufferPtr
221 decrypt(const uint8_t* cipherText, size_t cipherLen) const;
222
223private:
Davide Pesaventof45fa212017-09-14 17:23:56 -0400224 friend class SignerFilter;
225
Yingdi Yu202a2e92015-07-12 16:49:25 -0700226 /**
Davide Pesaventof45fa212017-09-14 17:23:56 -0400227 * @return A pointer to an OpenSSL EVP_PKEY instance.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700228 *
Davide Pesaventof45fa212017-09-14 17:23:56 -0400229 * The caller needs to explicitly cast the return value to `EVP_PKEY*`.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700230 */
231 void*
232 getEvpPkey() const;
233
234private:
235 ConstBufferPtr
236 toPkcs1() const;
237
238 ConstBufferPtr
239 toPkcs8(const char* pw, size_t pwLen) const;
240
241 ConstBufferPtr
242 toPkcs8(PasswordCallback pwCallback = nullptr) const;
243
244 ConstBufferPtr
245 rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const;
246
247private:
Davide Pesaventof45fa212017-09-14 17:23:56 -0400248 friend unique_ptr<PrivateKey> generatePrivateKey(const KeyParams&);
249
250 static unique_ptr<PrivateKey>
251 generateRsaKey(uint32_t keySize);
252
253 static unique_ptr<PrivateKey>
254 generateEcKey(uint32_t keySize);
255
256private:
Yingdi Yu202a2e92015-07-12 16:49:25 -0700257 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400258 const unique_ptr<Impl> m_impl;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700259};
260
261/**
Davide Pesaventof45fa212017-09-14 17:23:56 -0400262 * @brief Generate a private key according to @p keyParams
Yingdi Yu202a2e92015-07-12 16:49:25 -0700263 *
Davide Pesaventof45fa212017-09-14 17:23:56 -0400264 * @note The public key can be derived from the private key.
Yingdi Yu202a2e92015-07-12 16:49:25 -0700265 *
Davide Pesaventof45fa212017-09-14 17:23:56 -0400266 * @throw std::invalid_argument the specified key type is not supported
267 * @throw std::runtime_error key generation fails
Yingdi Yu202a2e92015-07-12 16:49:25 -0700268 */
269unique_ptr<PrivateKey>
270generatePrivateKey(const KeyParams& keyParams);
271
272} // namespace transform
273} // namespace security
274} // namespace ndn
275
276#endif // NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP