28 #include "../../encoding/buffer-stream.hpp" 29 #include "../detail/openssl-helper.hpp" 30 #include "../key-params.hpp" 34 #define ENSURE_PRIVATE_KEY_LOADED(key) \ 37 BOOST_THROW_EXCEPTION(Error("Private key has not been loaded yet")); \ 72 BIO_write(mem.
get(), buf, size);
74 d2i_PrivateKey_bio(mem.
get(), &m_impl->key);
106 BOOST_ASSERT(std::strlen(pw) == pwLen);
109 BIO_write(mem.
get(), buf, size);
111 m_impl->key = d2i_PKCS8PrivateKey_bio(mem.
get(), &m_impl->key,
nullptr,
const_cast<char*
>(pw));
120 return (*cb)(buf, size, rwflag);
126 OpenSSL_add_all_algorithms();
128 BIO_write(mem.
get(), buf, size);
131 m_impl->key = d2i_PKCS8PrivateKey_bio(mem.
get(), &m_impl->key,
passwordCallback, &pwCallback);
133 m_impl->key = d2i_PKCS8PrivateKey_bio(mem.
get(), &m_impl->key,
nullptr,
nullptr);
227 uint8_t* pkcs8 =
nullptr;
228 int len = i2d_PUBKEY(m_impl->key, &pkcs8);
231 BOOST_THROW_EXCEPTION(
Error(
"Failed to derive public key"));
233 auto result = make_shared<Buffer>(pkcs8, len);
244 #if OPENSSL_VERSION_NUMBER < 0x1010000fL 245 switch (EVP_PKEY_type(m_impl->key->type)) {
247 switch (EVP_PKEY_base_id(m_impl->key)) {
248 #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL 250 return rsaDecrypt(cipherText, cipherLen);
252 BOOST_THROW_EXCEPTION(
Error(
"Decryption is not supported for this key type"));
257 PrivateKey::getEvpPkey()
const 263 PrivateKey::toPkcs1()
const 267 OpenSSL_add_all_algorithms();
269 int ret = i2d_PrivateKey_bio(mem.
get(), m_impl->key);
271 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert key into PKCS1 format"));
273 int len8 = BIO_pending(mem.
get());
274 auto buffer = make_shared<Buffer>(len8);
275 BIO_read(mem.
get(), buffer->buf(), len8);
281 PrivateKey::toPkcs8(
const char* pw,
size_t pwLen)
const 285 BOOST_ASSERT(std::strlen(pw) == pwLen);
287 OpenSSL_add_all_algorithms();
289 int ret = i2d_PKCS8PrivateKey_bio(mem.
get(), m_impl->key, EVP_des_cbc(),
290 const_cast<char*
>(pw), pwLen,
nullptr,
nullptr);
292 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert key into PKCS8 format"));
294 int len8 = BIO_pending(mem.
get());
295 auto buffer = make_shared<Buffer>(len8);
296 BIO_read(mem.
get(), buffer->buf(), len8);
306 OpenSSL_add_all_algorithms();
308 int ret = i2d_PKCS8PrivateKey_bio(mem.
get(), m_impl->key, EVP_des_cbc(),
312 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert key into PKCS8 format"));
314 int len8 = BIO_pending(mem.
get());
315 auto buffer = make_shared<Buffer>(len8);
316 BIO_read(mem.
get(), buffer->buf(), len8);
322 PrivateKey::rsaDecrypt(
const uint8_t* cipherText,
size_t cipherLen)
const 326 if (EVP_PKEY_decrypt_init(ctx.
get()) <= 0)
327 BOOST_THROW_EXCEPTION(
Error(
"Failed to initialize decryption context"));
329 if (EVP_PKEY_CTX_set_rsa_padding(ctx.
get(), RSA_PKCS1_OAEP_PADDING) <= 0)
330 BOOST_THROW_EXCEPTION(
Error(
"Failed to set padding"));
334 if (EVP_PKEY_decrypt(ctx.
get(),
nullptr, &outlen, cipherText, cipherLen) <= 0)
335 BOOST_THROW_EXCEPTION(
Error(
"Failed to estimate output length"));
337 auto out = make_shared<Buffer>(outlen);
339 if (EVP_PKEY_decrypt(ctx.
get(), out->buf(), &outlen, cipherText, cipherLen) <= 0)
340 BOOST_THROW_EXCEPTION(
Error(
"Failed to decrypt cipher text"));
346 static unique_ptr<PrivateKey>
351 int ret = EVP_PKEY_keygen_init(kctx.
get());
355 ret = EVP_PKEY_CTX_set_rsa_keygen_bits(kctx.
get(), keySize);
360 ret = EVP_PKEY_keygen(kctx.
get(), &key);
365 i2d_PrivateKey_bio(mem.
get(), key.
get());
366 int len = BIO_pending(mem.
get());
368 BIO_read(mem.
get(), buffer.buf(), len);
370 auto privateKey = make_unique<PrivateKey>();
371 privateKey->loadPkcs1(buffer.buf(), buffer.size());
376 static unique_ptr<PrivateKey>
381 int ret = EVP_PKEY_paramgen_init(ctx.
get());
387 ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.
get(), NID_X9_62_prime256v1);
390 ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.
get(), NID_secp384r1);
399 ret = EVP_PKEY_paramgen(ctx.
get(), ¶ms);
404 ret = EVP_PKEY_keygen_init(kctx.get());
409 ret = EVP_PKEY_keygen(kctx.get(), &key);
414 i2d_PrivateKey_bio(mem.
get(), key.
get());
415 int len = BIO_pending(mem.
get());
417 BIO_read(mem.
get(), buffer.buf(), len);
419 auto privateKey = make_unique<PrivateKey>();
420 privateKey->loadPkcs1(buffer.buf(), buffer.size());
425 unique_ptr<PrivateKey>
438 BOOST_THROW_EXCEPTION(std::invalid_argument(
"Unsupported asymmetric key type"));
Copyright (c) 2011-2015 Regents of the University of California.
EVP_PKEY_CTX * get() const
#define ENSURE_PRIVATE_KEY_LOADED(key)
Copyright (c) 2013-2016 Regents of the University of California.
KeyType getKeyType() const
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Base class of key parameters.
uint32_t getKeySize() const
implements an output stream that constructs ndn::Buffer
shared_ptr< const Buffer > ConstBufferPtr
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Class representing a general-use automatically managed/resized buffer.