28 #include "../../encoding/buffer-stream.hpp" 29 #include "../detail/openssl-helper.hpp" 31 #define ENSURE_PUBLIC_KEY_LOADED(key) \ 34 BOOST_THROW_EXCEPTION(Error("Public key has not been loaded yet")); \ 70 #if OPENSSL_VERSION_NUMBER < 0x1010000fL 71 switch (EVP_PKEY_type(m_impl->key->type)) {
73 switch (EVP_PKEY_base_id(m_impl->key)) {
74 #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL 80 BOOST_THROW_EXCEPTION(
Error(
"Public key type is not recognized"));
87 m_impl->key = d2i_PUBKEY(
nullptr, &buf, size);
97 using namespace transform;
108 using namespace transform;
119 using namespace transform;
128 using namespace transform;
135 using namespace transform;
144 #if OPENSSL_VERSION_NUMBER < 0x1010000fL 145 switch (EVP_PKEY_type(m_impl->key->type)) {
147 switch (EVP_PKEY_base_id(m_impl->key)) {
148 #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL 150 return rsaEncrypt(plainText, plainLen);
152 BOOST_THROW_EXCEPTION(
Error(
"Encryption is not supported for this key type"));
157 PublicKey::getEvpPkey()
const 163 PublicKey::toPkcs8()
const 167 uint8_t* pkcs8 =
nullptr;
168 int len = i2d_PUBKEY(m_impl->key, &pkcs8);
170 if (pkcs8 ==
nullptr)
171 BOOST_THROW_EXCEPTION(
Error(
"Failed to convert to pkcs8 format"));
173 auto buffer = make_shared<Buffer>(pkcs8, len);
180 PublicKey::rsaEncrypt(
const uint8_t* plainText,
size_t plainLen)
const 184 if (EVP_PKEY_encrypt_init(ctx.
get()) <= 0)
185 BOOST_THROW_EXCEPTION(
Error(
"Failed to initialize encryption context"));
187 if (EVP_PKEY_CTX_set_rsa_padding(ctx.
get(), RSA_PKCS1_OAEP_PADDING) <= 0)
188 BOOST_THROW_EXCEPTION(
Error(
"Failed to set padding"));
192 if (EVP_PKEY_encrypt(ctx.
get(),
nullptr, &outlen, plainText, plainLen) <= 0)
193 BOOST_THROW_EXCEPTION(
Error(
"Failed to estimate output length"));
195 auto out = make_shared<Buffer>(outlen);
197 if (EVP_PKEY_encrypt(ctx.
get(), out->buf(), &outlen, plainText, plainLen) <= 0)
198 BOOST_THROW_EXCEPTION(
Error(
"Failed to decrypt cipher text"));
Copyright (c) 2011-2015 Regents of the University of California.
EVP_PKEY_CTX * get() const
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
implements an output stream that constructs ndn::Buffer
shared_ptr< const Buffer > ConstBufferPtr