NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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  typedef function<int(char* buf, size_t bufSize, bool shouldConfirm)> PasswordCallback;
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, const uint8_t* buf, size_t size);
94 
98  void
99  loadPkcs1(const uint8_t* buf, size_t size);
100 
104  void
105  loadPkcs1(std::istream& is);
106 
110  void
111  loadPkcs1Base64(const uint8_t* buf, size_t size);
112 
116  void
117  loadPkcs1Base64(std::istream& is);
118 
123  void
124  loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
125 
132  void
133  loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
134 
139  void
140  loadPkcs8(std::istream& is, const char* pw, size_t pwLen);
141 
148  void
149  loadPkcs8(std::istream& is, PasswordCallback pwCallback = nullptr);
150 
156  void
157  loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen);
158 
165  void
166  loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback = nullptr);
167 
173  void
174  loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen);
175 
182  void
183  loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback = nullptr);
184 
188  void
189  savePkcs1(std::ostream& os) const;
190 
194  void
195  savePkcs1Base64(std::ostream& os) const;
196 
200  void
201  savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const;
202 
209  void
210  savePkcs8(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
211 
215  void
216  savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const;
217 
224  void
225  savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback = nullptr) const;
226 
231  derivePublicKey() const;
232 
239  decrypt(const uint8_t* cipherText, size_t cipherLen) const;
240 
241 private:
242  friend class SignerFilter;
243  friend class VerifierFilter;
244 
250  void*
251  getEvpPkey() const;
252 
253 private:
255  toPkcs1() const;
256 
258  toPkcs8(const char* pw, size_t pwLen) const;
259 
261  toPkcs8(PasswordCallback pwCallback = nullptr) const;
262 
264  rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const;
265 
266 private:
267  friend unique_ptr<PrivateKey> generatePrivateKey(const KeyParams&);
268 
269  static unique_ptr<PrivateKey>
270  generateRsaKey(uint32_t keySize);
271 
272  static unique_ptr<PrivateKey>
273  generateEcKey(uint32_t keySize);
274 
275  static unique_ptr<PrivateKey>
276  generateHmacKey(uint32_t keySize);
277 
278 private:
279  class Impl;
280  const unique_ptr<Impl> m_impl;
281 };
282 
291 unique_ptr<PrivateKey>
292 generatePrivateKey(const KeyParams& keyParams);
293 
294 } // namespace transform
295 } // namespace security
296 } // namespace ndn
297 
298 #endif // NDN_CXX_SECURITY_TRANSFORM_PRIVATE_KEY_HPP
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::KeyType
KeyType
The type of a cryptographic key.
Definition: security-common.hpp:85
ndn::KeyParams
Base class for key parameters.
Definition: key-params.hpp:36
ndn::security::transform::PrivateKey::savePkcs8
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.
Definition: private-key.cpp:318
ndn::security::transform::PrivateKey::loadPkcs1Base64
void loadPkcs1Base64(const uint8_t *buf, size_t size)
Load the private key in base64-encoded PKCS#1 format from a buffer buf.
Definition: private-key.cpp:200
transform
security-common.hpp
ndn::security::transform::VerifierFilter
The module to verify signatures.
Definition: verifier-filter.hpp:41
ndn::security::transform::PrivateKey::savePkcs1Base64
void savePkcs1Base64(std::ostream &os) const
Save the private key in base64-encoded PKCS#1 format into a stream os.
Definition: private-key.cpp:312
ndn::security::transform::generatePrivateKey
unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &keyParams)
Generate a private key according to keyParams.
Definition: private-key.cpp:536
ndn::security::transform::PrivateKey::loadRaw
void loadRaw(KeyType type, const uint8_t *buf, size_t size)
Load a raw private key from a buffer buf.
Definition: private-key.cpp:154
ndn::security::transform::PrivateKey::savePkcs8Base64
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.
Definition: private-key.cpp:330
ndn::security::transform::SignerFilter
The module to sign data.
Definition: signer-filter.hpp:38
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::transform::PrivateKey::Error
Definition: private-key.hpp:42
ndn::security::transform::PrivateKey::decrypt
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherLen) const
Definition: private-key.cpp:358
ndn::security::transform::PrivateKey::~PrivateKey
~PrivateKey()
ndn::security::transform::PrivateKey::loadPkcs8
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.
Definition: private-key.cpp:216
ndn::security::transform::PrivateKey::savePkcs1
void savePkcs1(std::ostream &os) const
Save the private key in PKCS#1 format into a stream os.
Definition: private-key.cpp:306
ndn::security::transform::PrivateKey::derivePublicKey
ConstBufferPtr derivePublicKey() const
Definition: private-key.cpp:342
ndn::security::transform::PrivateKey::loadPkcs1
void loadPkcs1(const uint8_t *buf, size_t size)
Load the private key in PKCS#1 format from a buffer buf.
Definition: private-key.cpp:182
ndn::security::transform::PrivateKey::getKeyDigest
ConstBufferPtr getKeyDigest(DigestAlgorithm algo) const
Returns a digest of the private key.
Definition: private-key.cpp:128
ndn::security::transform::PrivateKey::getKeySize
size_t getKeySize() const
Returns the size of the private key in bits.
Definition: private-key.cpp:107
buffer.hpp
ndn::security::transform::PrivateKey::PasswordCallback
function< int(char *buf, size_t bufSize, bool shouldConfirm)> PasswordCallback
Callback for application to handle password input.
Definition: private-key.hpp:55
ndn::security::transform::PrivateKey::getKeyType
KeyType getKeyType() const
Returns the type of the private key.
Definition: private-key.cpp:89
ndn::security::transform::PrivateKey::loadPkcs8Base64
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.
Definition: private-key.cpp:274
ndn::security::transform::PrivateKey::PrivateKey
PrivateKey()
Creates an empty private key instance.
Definition: private-key.cpp:81
ndn::security::transform::PrivateKey::generatePrivateKey
friend unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &)
Generate a private key according to keyParams.
Definition: private-key.cpp:536
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn::security::transform::PrivateKey
Abstraction of private key in crypto transformation.
Definition: private-key.hpp:39
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34