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-2017 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 
25 #include "../security-common.hpp"
26 #include "../../encoding/buffer.hpp"
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  explicit
45  Error(const std::string& what)
46  : std::runtime_error(what)
47  {
48  }
49  };
50 
59  typedef function<int(char* buf, size_t bufSize, bool shouldConfirm)> PasswordCallback;
60 
61 public:
67  PrivateKey();
68 
69  ~PrivateKey();
70 
74  KeyType
75  getKeyType() const;
76 
80  void
81  loadPkcs1(const uint8_t* buf, size_t size);
82 
86  void
87  loadPkcs1(std::istream& is);
88 
92  void
93  loadPkcs1Base64(const uint8_t* buf, size_t size);
94 
98  void
99  loadPkcs1Base64(std::istream& is);
100 
105  void
106  loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
107 
114  void
115  loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
116 
121  void
122  loadPkcs8(std::istream& is, const char* pw, size_t pwLen);
123 
130  void
131  loadPkcs8(std::istream& is, PasswordCallback pwCallback = nullptr);
132 
138  void
139  loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
140 
147  void
148  loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
149 
155  void
156  loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen);
157 
164  void
165  loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback = nullptr);
166 
170  void
171  savePkcs1(std::ostream& os) const;
172 
176  void
177  savePkcs1Base64(std::ostream& os) const;
178 
182  void
183  savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const;
184 
191  void
192  savePkcs8(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
193 
197  void
198  savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const;
199 
206  void
207  savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
208 
213  derivePublicKey() const;
214 
221  decrypt(const uint8_t* cipherText, size_t cipherLen) const;
222 
223 private:
224  friend class SignerFilter;
225 
231  void*
232  getEvpPkey() const;
233 
234 private:
236  toPkcs1() const;
237 
239  toPkcs8(const char* pw, size_t pwLen) const;
240 
242  toPkcs8(PasswordCallback pwCallback = nullptr) const;
243 
245  rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const;
246 
247 private:
248  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 
256 private:
257  class Impl;
258  const unique_ptr<Impl> m_impl;
259 };
260 
269 unique_ptr<PrivateKey>
270 generatePrivateKey(const KeyParams& keyParams);
271 
272 } // namespace transform
273 } // namespace security
274 } // namespace ndn
275 
276 #endif // NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
void loadPkcs8(const uint8_t *buf, size_t size, const char *pw, size_t pwLen)
Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase pw...
void loadPkcs1Base64(const uint8_t *buf, size_t size)
Load the private key in base64-encoded PKCS#1 format from a buffer buf.
function< int(char *buf, size_t bufSize, bool shouldConfirm)> PasswordCallback
Callback for application to handle password input.
Definition: private-key.hpp:59
void loadPkcs1(const uint8_t *buf, size_t size)
Load the private key in 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.
STL namespace.
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.
PrivateKey()
Create an empty private key instance.
Definition: private-key.cpp:80
KeyType
The type of a cryptographic key.
void loadPkcs8Base64(const uint8_t *buf, size_t size, const char *pw, size_t pwLen)
Load the private key in base64-encoded encrypted PKCS#8 format from a buffer buf with passphrase pw...
ConstBufferPtr derivePublicKey() const
KeyType getKeyType() const
Get the type of the private key.
Definition: private-key.cpp:88
void savePkcs1(std::ostream &os) const
Save the private key in PKCS#1 format into a stream os.
Abstraction of private key in crypto transformation.
Definition: private-key.hpp:38
friend unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &)
Generate a private key according to keyParams.
Base class of key parameters.
Definition: key-params.hpp:35
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherLen) const
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.
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:89