NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
private-key.cpp
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 
32 #include "ndn-cxx/util/random.hpp"
33 
34 #include <boost/lexical_cast.hpp>
35 #include <cstring>
36 
37 #define ENSURE_PRIVATE_KEY_LOADED(key) \
38  do { \
39  if ((key) == nullptr) \
40  NDN_THROW(Error("Private key has not been loaded yet")); \
41  } while (false)
42 
43 #define ENSURE_PRIVATE_KEY_NOT_LOADED(key) \
44  do { \
45  if ((key) != nullptr) \
46  NDN_THROW(Error("Private key has already been loaded")); \
47  } while (false)
48 
49 namespace ndn {
50 namespace security {
51 namespace transform {
52 
53 static void
55 {
56 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
57  static bool isInitialized = false;
58  if (!isInitialized) {
59  OpenSSL_add_all_algorithms();
60  isInitialized = true;
61  }
62 #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
63 }
64 
65 class PrivateKey::Impl : noncopyable
66 {
67 public:
69  {
70  EVP_PKEY_free(key);
71  }
72 
73 public:
74  EVP_PKEY* key = nullptr;
75 
76 #if OPENSSL_VERSION_NUMBER < 0x1010100fL
77  size_t keySize = 0; // in bits, used only for HMAC
78 #endif
79 };
80 
82  : m_impl(make_unique<Impl>())
83 {
84 }
85 
86 PrivateKey::~PrivateKey() = default;
87 
88 KeyType
90 {
91  if (!m_impl->key)
92  return KeyType::NONE;
93 
94  switch (detail::getEvpPkeyType(m_impl->key)) {
95  case EVP_PKEY_RSA:
96  return KeyType::RSA;
97  case EVP_PKEY_EC:
98  return KeyType::EC;
99  case EVP_PKEY_HMAC:
100  return KeyType::HMAC;
101  default:
102  return KeyType::NONE;
103  }
104 }
105 
106 size_t
108 {
109  switch (getKeyType()) {
110  case KeyType::RSA:
111  case KeyType::EC:
112  return static_cast<size_t>(EVP_PKEY_bits(m_impl->key));
113  case KeyType::HMAC: {
114 #if OPENSSL_VERSION_NUMBER >= 0x1010100fL
115  size_t nBytes = 0;
116  EVP_PKEY_get_raw_private_key(m_impl->key, nullptr, &nBytes);
117  return nBytes * 8;
118 #else
119  return m_impl->keySize;
120 #endif
121  }
122  default:
123  return 0;
124  }
125 }
126 
129 {
130  if (getKeyType() != KeyType::HMAC)
131  NDN_THROW(Error("Digest is not supported for key type " +
132  boost::lexical_cast<std::string>(getKeyType())));
133 
134  const uint8_t* buf = nullptr;
135  size_t len = 0;
136 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
137  buf = EVP_PKEY_get0_hmac(m_impl->key, &len);
138 #else
139  const auto* octstr = reinterpret_cast<ASN1_OCTET_STRING*>(EVP_PKEY_get0(m_impl->key));
140  buf = octstr->data;
141  len = octstr->length;
142 #endif
143  if (buf == nullptr)
144  NDN_THROW(Error("Failed to obtain raw key pointer"));
145  if (len * 8 != getKeySize())
146  NDN_THROW(Error("Key length mismatch"));
147 
148  OBufferStream os;
149  bufferSource(buf, len) >> digestFilter(algo) >> streamSink(os);
150  return os.buf();
151 }
152 
153 void
154 PrivateKey::loadRaw(KeyType type, const uint8_t* buf, size_t size)
155 {
156  ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key);
157 
158  int pkeyType;
159  switch (type) {
160  case KeyType::HMAC:
161  pkeyType = EVP_PKEY_HMAC;
162  break;
163  default:
164  NDN_THROW(std::invalid_argument("Unsupported key type " + boost::lexical_cast<std::string>(type)));
165  }
166 
167  m_impl->key =
168 #if OPENSSL_VERSION_NUMBER >= 0x1010100fL
169  EVP_PKEY_new_raw_private_key(pkeyType, nullptr, buf, size);
170 #else
171  EVP_PKEY_new_mac_key(pkeyType, nullptr, buf, static_cast<int>(size));
172 #endif
173  if (m_impl->key == nullptr)
174  NDN_THROW(Error("Failed to load private key"));
175 
176 #if OPENSSL_VERSION_NUMBER < 0x1010100fL
177  m_impl->keySize = size * 8;
178 #endif
179 }
180 
181 void
182 PrivateKey::loadPkcs1(const uint8_t* buf, size_t size)
183 {
184  ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key);
186 
187  if (d2i_AutoPrivateKey(&m_impl->key, &buf, static_cast<long>(size)) == nullptr)
188  NDN_THROW(Error("Failed to load private key"));
189 }
190 
191 void
192 PrivateKey::loadPkcs1(std::istream& is)
193 {
194  OBufferStream os;
195  streamSource(is) >> streamSink(os);
196  this->loadPkcs1(os.buf()->data(), os.buf()->size());
197 }
198 
199 void
200 PrivateKey::loadPkcs1Base64(const uint8_t* buf, size_t size)
201 {
202  OBufferStream os;
203  bufferSource(buf, size) >> base64Decode() >> streamSink(os);
204  this->loadPkcs1(os.buf()->data(), os.buf()->size());
205 }
206 
207 void
209 {
210  OBufferStream os;
211  streamSource(is) >> base64Decode() >> streamSink(os);
212  this->loadPkcs1(os.buf()->data(), os.buf()->size());
213 }
214 
215 void
216 PrivateKey::loadPkcs8(const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
217 {
218  BOOST_ASSERT(std::strlen(pw) == pwLen);
219  ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key);
221 
222  detail::Bio membio(BIO_s_mem());
223  if (!membio.write(buf, size))
224  NDN_THROW(Error("Failed to copy buffer"));
225 
226  if (d2i_PKCS8PrivateKey_bio(membio, &m_impl->key, nullptr, const_cast<char*>(pw)) == nullptr)
227  NDN_THROW(Error("Failed to load private key"));
228 }
229 
230 static inline int
231 passwordCallbackWrapper(char* buf, int size, int rwflag, void* u)
232 {
233  BOOST_ASSERT(size >= 0);
234  auto cb = reinterpret_cast<PrivateKey::PasswordCallback*>(u);
235  return (*cb)(buf, static_cast<size_t>(size), rwflag);
236 }
237 
238 void
239 PrivateKey::loadPkcs8(const uint8_t* buf, size_t size, PasswordCallback pwCallback)
240 {
241  ENSURE_PRIVATE_KEY_NOT_LOADED(m_impl->key);
243 
244  detail::Bio membio(BIO_s_mem());
245  if (!membio.write(buf, size))
246  NDN_THROW(Error("Failed to copy buffer"));
247 
248  if (pwCallback)
249  m_impl->key = d2i_PKCS8PrivateKey_bio(membio, nullptr, &passwordCallbackWrapper, &pwCallback);
250  else
251  m_impl->key = d2i_PKCS8PrivateKey_bio(membio, nullptr, nullptr, nullptr);
252 
253  if (m_impl->key == nullptr)
254  NDN_THROW(Error("Failed to load private key"));
255 }
256 
257 void
258 PrivateKey::loadPkcs8(std::istream& is, const char* pw, size_t pwLen)
259 {
260  OBufferStream os;
261  streamSource(is) >> streamSink(os);
262  this->loadPkcs8(os.buf()->data(), os.buf()->size(), pw, pwLen);
263 }
264 
265 void
266 PrivateKey::loadPkcs8(std::istream& is, PasswordCallback pwCallback)
267 {
268  OBufferStream os;
269  streamSource(is) >> streamSink(os);
270  this->loadPkcs8(os.buf()->data(), os.buf()->size(), pwCallback);
271 }
272 
273 void
274 PrivateKey::loadPkcs8Base64(const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
275 {
276  OBufferStream os;
277  bufferSource(buf, size) >> base64Decode() >> streamSink(os);
278  this->loadPkcs8(os.buf()->data(), os.buf()->size(), pw, pwLen);
279 }
280 
281 void
282 PrivateKey::loadPkcs8Base64(const uint8_t* buf, size_t size, PasswordCallback pwCallback)
283 {
284  OBufferStream os;
285  bufferSource(buf, size) >> base64Decode() >> streamSink(os);
286  this->loadPkcs8(os.buf()->data(), os.buf()->size(), pwCallback);
287 }
288 
289 void
290 PrivateKey::loadPkcs8Base64(std::istream& is, const char* pw, size_t pwLen)
291 {
292  OBufferStream os;
293  streamSource(is) >> base64Decode() >> streamSink(os);
294  this->loadPkcs8(os.buf()->data(), os.buf()->size(), pw, pwLen);
295 }
296 
297 void
298 PrivateKey::loadPkcs8Base64(std::istream& is, PasswordCallback pwCallback)
299 {
300  OBufferStream os;
301  streamSource(is) >> base64Decode() >> streamSink(os);
302  this->loadPkcs8(os.buf()->data(), os.buf()->size(), pwCallback);
303 }
304 
305 void
306 PrivateKey::savePkcs1(std::ostream& os) const
307 {
308  bufferSource(*this->toPkcs1()) >> streamSink(os);
309 }
310 
311 void
312 PrivateKey::savePkcs1Base64(std::ostream& os) const
313 {
314  bufferSource(*this->toPkcs1()) >> base64Encode() >> streamSink(os);
315 }
316 
317 void
318 PrivateKey::savePkcs8(std::ostream& os, const char* pw, size_t pwLen) const
319 {
320  bufferSource(*this->toPkcs8(pw, pwLen)) >> streamSink(os);
321 }
322 
323 void
324 PrivateKey::savePkcs8(std::ostream& os, PasswordCallback pwCallback) const
325 {
326  bufferSource(*this->toPkcs8(pwCallback)) >> streamSink(os);
327 }
328 
329 void
330 PrivateKey::savePkcs8Base64(std::ostream& os, const char* pw, size_t pwLen) const
331 {
332  bufferSource(*this->toPkcs8(pw, pwLen)) >> base64Encode() >> streamSink(os);
333 }
334 
335 void
336 PrivateKey::savePkcs8Base64(std::ostream& os, PasswordCallback pwCallback) const
337 {
338  bufferSource(*this->toPkcs8(pwCallback)) >> base64Encode() >> streamSink(os);
339 }
340 
343 {
344  ENSURE_PRIVATE_KEY_LOADED(m_impl->key);
345 
346  uint8_t* pkcs8 = nullptr;
347  int len = i2d_PUBKEY(m_impl->key, &pkcs8);
348  if (len < 0)
349  NDN_THROW(Error("Failed to derive public key"));
350 
351  auto result = make_shared<Buffer>(pkcs8, len);
352  OPENSSL_free(pkcs8);
353 
354  return result;
355 }
356 
358 PrivateKey::decrypt(const uint8_t* cipherText, size_t cipherLen) const
359 {
360  ENSURE_PRIVATE_KEY_LOADED(m_impl->key);
361 
362  int keyType = detail::getEvpPkeyType(m_impl->key);
363  switch (keyType) {
364  case EVP_PKEY_NONE:
365  NDN_THROW(Error("Failed to determine key type"));
366  case EVP_PKEY_RSA:
367  return rsaDecrypt(cipherText, cipherLen);
368  default:
369  NDN_THROW(Error("Decryption is not supported for key type " + to_string(keyType)));
370  }
371 }
372 
373 void*
374 PrivateKey::getEvpPkey() const
375 {
376  return m_impl->key;
377 }
378 
380 PrivateKey::toPkcs1() const
381 {
382  ENSURE_PRIVATE_KEY_LOADED(m_impl->key);
384 
385  detail::Bio membio(BIO_s_mem());
386  if (!i2d_PrivateKey_bio(membio, m_impl->key))
387  NDN_THROW(Error("Cannot convert key to PKCS #1 format"));
388 
389  auto buffer = make_shared<Buffer>(BIO_pending(membio));
390  if (!membio.read(buffer->data(), buffer->size()))
391  NDN_THROW(Error("Read error during PKCS #1 conversion"));
392 
393  return buffer;
394 }
395 
397 PrivateKey::toPkcs8(const char* pw, size_t pwLen) const
398 {
399  BOOST_ASSERT(std::strlen(pw) == pwLen);
400  ENSURE_PRIVATE_KEY_LOADED(m_impl->key);
402 
403  detail::Bio membio(BIO_s_mem());
404  if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_aes_256_cbc(), nullptr, 0,
405  nullptr, const_cast<char*>(pw)))
406  NDN_THROW(Error("Cannot convert key to PKCS #8 format"));
407 
408  auto buffer = make_shared<Buffer>(BIO_pending(membio));
409  if (!membio.read(buffer->data(), buffer->size()))
410  NDN_THROW(Error("Read error during PKCS #8 conversion"));
411 
412  return buffer;
413 }
414 
416 PrivateKey::toPkcs8(PasswordCallback pwCallback) const
417 {
418  ENSURE_PRIVATE_KEY_LOADED(m_impl->key);
420 
421  detail::Bio membio(BIO_s_mem());
422  if (!i2d_PKCS8PrivateKey_bio(membio, m_impl->key, EVP_aes_256_cbc(), nullptr, 0,
423  &passwordCallbackWrapper, &pwCallback))
424  NDN_THROW(Error("Cannot convert key to PKCS #8 format"));
425 
426  auto buffer = make_shared<Buffer>(BIO_pending(membio));
427  if (!membio.read(buffer->data(), buffer->size()))
428  NDN_THROW(Error("Read error during PKCS #8 conversion"));
429 
430  return buffer;
431 }
432 
434 PrivateKey::rsaDecrypt(const uint8_t* cipherText, size_t cipherLen) const
435 {
436  detail::EvpPkeyCtx ctx(m_impl->key);
437 
438  if (EVP_PKEY_decrypt_init(ctx) <= 0)
439  NDN_THROW(Error("Failed to initialize decryption context"));
440 
441  if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
442  NDN_THROW(Error("Failed to set padding"));
443 
444  size_t outlen = 0;
445  // Determine buffer length
446  if (EVP_PKEY_decrypt(ctx, nullptr, &outlen, cipherText, cipherLen) <= 0)
447  NDN_THROW(Error("Failed to estimate output length"));
448 
449  auto out = make_shared<Buffer>(outlen);
450  if (EVP_PKEY_decrypt(ctx, out->data(), &outlen, cipherText, cipherLen) <= 0)
451  NDN_THROW(Error("Failed to decrypt ciphertext"));
452 
453  out->resize(outlen);
454  return out;
455 }
456 
457 unique_ptr<PrivateKey>
458 PrivateKey::generateRsaKey(uint32_t keySize)
459 {
460  detail::EvpPkeyCtx kctx(EVP_PKEY_RSA);
461 
462  if (EVP_PKEY_keygen_init(kctx) <= 0)
463  NDN_THROW(PrivateKey::Error("Failed to initialize RSA keygen context"));
464 
465  if (EVP_PKEY_CTX_set_rsa_keygen_bits(kctx, static_cast<int>(keySize)) <= 0)
466  NDN_THROW(PrivateKey::Error("Failed to set RSA key length"));
467 
468  auto privateKey = make_unique<PrivateKey>();
469  if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0)
470  NDN_THROW(PrivateKey::Error("Failed to generate RSA key"));
471 
472  return privateKey;
473 }
474 
475 unique_ptr<PrivateKey>
476 PrivateKey::generateEcKey(uint32_t keySize)
477 {
478  detail::EvpPkeyCtx pctx(EVP_PKEY_EC);
479 
480  if (EVP_PKEY_paramgen_init(pctx) <= 0)
481  NDN_THROW(PrivateKey::Error("Failed to initialize EC paramgen context"));
482 
483  int ret;
484  switch (keySize) {
485  case 224:
486  ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp224r1);
487  break;
488  case 256:
489  ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1); // same as secp256r1
490  break;
491  case 384:
492  ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp384r1);
493  break;
494  case 521:
495  ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_secp521r1);
496  break;
497  default:
498  NDN_THROW(std::invalid_argument("Unsupported EC key length " + to_string(keySize)));
499  }
500  if (ret <= 0)
501  NDN_THROW(PrivateKey::Error("Failed to set EC curve"));
502 
503  Impl params;
504  if (EVP_PKEY_paramgen(pctx, &params.key) <= 0)
505  NDN_THROW(PrivateKey::Error("Failed to generate EC parameters"));
506 
507  detail::EvpPkeyCtx kctx(params.key);
508  if (EVP_PKEY_keygen_init(kctx) <= 0)
509  NDN_THROW(PrivateKey::Error("Failed to initialize EC keygen context"));
510 
511  auto privateKey = make_unique<PrivateKey>();
512  if (EVP_PKEY_keygen(kctx, &privateKey->m_impl->key) <= 0)
513  NDN_THROW(PrivateKey::Error("Failed to generate EC key"));
514 
515  return privateKey;
516 }
517 
518 unique_ptr<PrivateKey>
519 PrivateKey::generateHmacKey(uint32_t keySize)
520 {
521  std::vector<uint8_t> rawKey(keySize / 8);
522  random::generateSecureBytes(rawKey.data(), rawKey.size());
523 
524  auto privateKey = make_unique<PrivateKey>();
525  try {
526  privateKey->loadRaw(KeyType::HMAC, rawKey.data(), rawKey.size());
527  }
528  catch (const PrivateKey::Error&) {
529  NDN_THROW(PrivateKey::Error("Failed to generate HMAC key"));
530  }
531 
532  return privateKey;
533 }
534 
535 unique_ptr<PrivateKey>
536 generatePrivateKey(const KeyParams& keyParams)
537 {
538  switch (keyParams.getKeyType()) {
539  case KeyType::RSA: {
540  const RsaKeyParams& rsaParams = static_cast<const RsaKeyParams&>(keyParams);
541  return PrivateKey::generateRsaKey(rsaParams.getKeySize());
542  }
543  case KeyType::EC: {
544  const EcKeyParams& ecParams = static_cast<const EcKeyParams&>(keyParams);
545  return PrivateKey::generateEcKey(ecParams.getKeySize());
546  }
547  case KeyType::HMAC: {
548  const HmacKeyParams& hmacParams = static_cast<const HmacKeyParams&>(keyParams);
549  return PrivateKey::generateHmacKey(hmacParams.getKeySize());
550  }
551  default:
552  NDN_THROW(std::invalid_argument("Unsupported key type " +
553  boost::lexical_cast<std::string>(keyParams.getKeyType())));
554  }
555 }
556 
557 } // namespace transform
558 } // namespace security
559 } // namespace ndn
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
buffer-source.hpp
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
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::base64Decode
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
Definition: base64-decode.cpp:129
random.hpp
ENSURE_PRIVATE_KEY_NOT_LOADED
#define ENSURE_PRIVATE_KEY_NOT_LOADED(key)
Definition: private-key.cpp:43
ndn::security::transform::bufferSource
BufferSource bufferSource
Definition: buffer-source.hpp:73
ENSURE_PRIVATE_KEY_LOADED
#define ENSURE_PRIVATE_KEY_LOADED(key)
Definition: private-key.cpp:37
base64-decode.hpp
transform::PrivateKey::Impl::keySize
size_t keySize
Definition: private-key.cpp:77
ndn::KeyType::EC
@ EC
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
ndn::security::detail::Bio::write
NDN_CXX_NODISCARD bool write(const uint8_t *buf, size_t buflen) noexcept
Definition: openssl-helper.cpp:132
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::detail::Bio
Definition: openssl-helper.hpp:75
ndn::SimplePublicKeyParams::getKeySize
uint32_t getKeySize() const
Definition: key-params.hpp:176
digest-filter.hpp
ndn::security::transform::PrivateKey::Error
Definition: private-key.hpp:42
transform::PrivateKey::Impl::key
EVP_PKEY * key
Definition: private-key.cpp:74
ndn::security::transform::PrivateKey::decrypt
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherLen) const
Definition: private-key.cpp:358
ndn::security::detail::getEvpPkeyType
int getEvpPkeyType(EVP_PKEY *key)
Definition: openssl-helper.cpp:62
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
private-key.hpp
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::KeyParams::getKeyType
KeyType getKeyType() const
Definition: key-params.hpp:48
ndn::SimpleSymmetricKeyParams::getKeySize
uint32_t getKeySize() const
Definition: key-params.hpp:283
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::KeyType::HMAC
@ HMAC
HMAC key, supports sign/verify operations.
openssl-helper.hpp
ndn::security::transform::streamSource
StreamSource streamSource
Definition: stream-source.hpp:62
stream-sink.hpp
ndn::security::transform::base64Encode
unique_ptr< Transform > base64Encode(bool needBreak)
Definition: base64-encode.cpp:129
stream-source.hpp
ndn::security::transform::PrivateKey::derivePublicKey
ConstBufferPtr derivePublicKey() const
Definition: private-key.cpp:342
ndn::security::transform::passwordCallbackWrapper
static int passwordCallbackWrapper(char *buf, int size, int rwflag, void *u)
Definition: private-key.cpp:231
transform::PrivateKey::Impl
Definition: private-key.cpp:66
ndn::OBufferStream
implements an output stream that constructs ndn::Buffer
Definition: buffer-stream.hpp:71
ndn::security::transform::opensslInitAlgorithms
static void opensslInitAlgorithms()
Definition: private-key.cpp:54
ndn::SimplePublicKeyParams
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Definition: key-params.hpp:150
ndn::security::transform::digestFilter
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
Definition: digest-filter.cpp:76
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::random::generateSecureBytes
void generateSecureBytes(uint8_t *bytes, size_t size)
Fill bytes of size with cryptographically secure random bytes.
Definition: random.cpp:45
ndn::security::transform::streamSink
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
ndn::security::transform::PrivateKey::getKeyDigest
ConstBufferPtr getKeyDigest(DigestAlgorithm algo) const
Returns a digest of the private key.
Definition: private-key.cpp:128
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::KeyType::NONE
@ NONE
Unknown or unsupported key type.
ndn::security::transform::PrivateKey::getKeySize
size_t getKeySize() const
Returns the size of the private key in bits.
Definition: private-key.cpp:107
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::SimpleSymmetricKeyParams
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
Definition: key-params.hpp:257
buffer-stream.hpp
key-params.hpp
ndn::OBufferStream::buf
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition: buffer-stream.cpp:54
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
transform::PrivateKey::Impl::~Impl
~Impl()
Definition: private-key.cpp:68
base64-encode.hpp
ndn::KeyType::RSA
@ RSA
RSA key, supports sign/verify and encrypt/decrypt operations.
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34