private-key.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
4  *
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 
27 
28 namespace ndn {
29 
30 class KeyParams;
31 
32 namespace security {
33 namespace transform {
34 
38 class PrivateKey : noncopyable
39 {
40 public:
41  class Error : public std::runtime_error
42  {
43  public:
44  using std::runtime_error::runtime_error;
45  };
46 
55  using PasswordCallback = std::function<int(char* buf, size_t bufSize, bool shouldConfirm)>;
56 
57 public:
63  PrivateKey();
64 
66 
70  KeyType
71  getKeyType() const;
72 
76  size_t
77  getKeySize() const;
78 
85  getKeyDigest(DigestAlgorithm algo) const;
86 
92  void
93  loadRaw(KeyType type, span<const uint8_t> buf);
94 
101  [[deprecated("use the overload that takes a span<>")]]
102  void
103  loadRaw(KeyType type, const uint8_t* buf, size_t size)
104  {
105  loadRaw(type, {buf, size});
106  }
107 
111  void
112  loadPkcs1(span<const uint8_t> buf);
113 
118  [[deprecated("use the overload that takes a span<>")]]
119  void
120  loadPkcs1(const uint8_t* buf, size_t size)
121  {
122  loadPkcs1({buf, size});
123  }
124 
128  void
129  loadPkcs1(std::istream& is);
130 
134  void
135  loadPkcs1Base64(span<const uint8_t> buf);
136 
141  [[deprecated("use the overload that takes a span<>")]]
142  void
143  loadPkcs1Base64(const uint8_t* buf, size_t size)
144  {
145  loadPkcs1Base64({buf, size});
146  }
147 
151  void
152  loadPkcs1Base64(std::istream& is);
153 
158  void
159  loadPkcs8(span<const uint8_t> buf, const char* pw, size_t pwLen);
160 
167  void
168  loadPkcs8(span<const uint8_t> buf, PasswordCallback pwCallback = nullptr);
169 
177  [[deprecated("use the overload that takes a span<>")]]
178  void
179  loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr)
180  {
181  loadPkcs8({buf, size}, std::move(pwCallback));
182  }
183 
188  void
189  loadPkcs8(std::istream& is, const char* pw, size_t pwLen);
190 
197  void
198  loadPkcs8(std::istream& is, PasswordCallback pwCallback = nullptr);
199 
205  void
206  loadPkcs8Base64(span<const uint8_t> buf, const char* pw, size_t pwLen);
207 
214  void
215  loadPkcs8Base64(span<const uint8_t> buf, PasswordCallback pwCallback = nullptr);
216 
224  [[deprecated("use the overload that takes a span<>")]]
225  void
226  loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr)
227  {
228  loadPkcs8Base64({buf, size}, std::move(pwCallback));
229  }
230 
236  void
237  loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen);
238 
245  void
246  loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback = nullptr);
247 
251  void
252  savePkcs1(std::ostream& os) const;
253 
257  void
258  savePkcs1Base64(std::ostream& os) const;
259 
263  void
264  savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const;
265 
272  void
273  savePkcs8(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
274 
278  void
279  savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const;
280 
287  void
288  savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
289 
294  derivePublicKey() const;
295 
302  decrypt(span<const uint8_t> cipherText) const;
303 
310  [[deprecated("use the overload that takes a span<>")]]
312  decrypt(const uint8_t* cipherText, size_t cipherLen) const
313  {
314  return decrypt({cipherText, cipherLen});
315  }
316 
317 private:
318  friend class SignerFilter;
319  friend class VerifierFilter;
320 
326  void*
327  getEvpPkey() const;
328 
329 private:
331  toPkcs1() const;
332 
334  toPkcs8(const char* pw, size_t pwLen) const;
335 
337  toPkcs8(PasswordCallback pwCallback = nullptr) const;
338 
340  rsaDecrypt(span<const uint8_t> cipherText) const;
341 
342 private:
343  friend unique_ptr<PrivateKey> generatePrivateKey(const KeyParams&);
344 
345  static unique_ptr<PrivateKey>
346  generateRsaKey(uint32_t keySize);
347 
348  static unique_ptr<PrivateKey>
349  generateEcKey(uint32_t keySize);
350 
351  static unique_ptr<PrivateKey>
352  generateHmacKey(uint32_t keySize);
353 
354 private:
355  class Impl;
356  const unique_ptr<Impl> m_impl;
357 };
358 
367 unique_ptr<PrivateKey>
368 generatePrivateKey(const KeyParams& keyParams);
369 
370 } // namespace transform
371 } // namespace security
372 } // namespace ndn
373 
374 #endif // NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP
Base class for key parameters.
Definition: key-params.hpp:36
Abstraction of private key in crypto transformation.
Definition: private-key.hpp:39
friend unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &)
Generate a private key according to keyParams.
void loadPkcs1(const uint8_t *buf, size_t size)
Load the private key in PKCS#1 format from a buffer buf.
void savePkcs8(std::ostream &os, const char *pw, size_t pwLen) const
Save the private key in encrypted PKCS#8 format into a stream os.
void savePkcs1(std::ostream &os) const
Save the private key in PKCS#1 format into a stream os.
std::function< int(char *buf, size_t bufSize, bool shouldConfirm)> PasswordCallback
Callback for application to handle password input.
Definition: private-key.hpp:55
ConstBufferPtr getKeyDigest(DigestAlgorithm algo) const
Returns a digest of the private key.
void loadRaw(KeyType type, span< const uint8_t > buf)
Load a raw private key from a buffer buf.
void loadPkcs8Base64(span< const uint8_t > buf, const char *pw, size_t pwLen)
Load the private key in base64-encoded encrypted PKCS#8 format from a buffer buf with passphrase pw.
void loadPkcs1Base64(span< const uint8_t > buf)
Load the private key in base64-encoded PKCS#1 format from a buffer buf.
void savePkcs1Base64(std::ostream &os) const
Save the private key in base64-encoded PKCS#1 format into a stream os.
void loadPkcs1Base64(const uint8_t *buf, size_t size)
Load the private key in base64-encoded PKCS#1 format from a buffer buf.
void loadPkcs1(span< const uint8_t > buf)
Load the private key in PKCS#1 format from a buffer buf.
ConstBufferPtr derivePublicKey() const
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherLen) const
void loadPkcs8(span< const uint8_t > buf, const char *pw, size_t pwLen)
Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase pw.
void savePkcs8Base64(std::ostream &os, const char *pw, size_t pwLen) const
Save the private key in base64-encoded encrypted PKCS#8 format into a stream os.
void loadRaw(KeyType type, const uint8_t *buf, size_t size)
Load a raw private key from a buffer buf.
ConstBufferPtr decrypt(span< const uint8_t > cipherText) const
KeyType getKeyType() const
Returns the type of the private key.
Definition: private-key.cpp:90
void loadPkcs8(const uint8_t *buf, size_t size, PasswordCallback pwCallback=nullptr)
Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase obtained from pwCal...
void loadPkcs8Base64(const uint8_t *buf, size_t size, PasswordCallback pwCallback=nullptr)
Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase obtained from pwCal...
PrivateKey()
Creates an empty private key instance.
Definition: private-key.cpp:82
size_t getKeySize() const
Returns the size of the private key in bits.
The module to verify signatures.
unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &keyParams)
Generate a private key according to keyParams.
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
KeyType
The type of a cryptographic key.