28 #include "ndn-cxx/security/impl/openssl-helper.hpp" 31 #define ENSURE_PUBLIC_KEY_LOADED(key) \ 33 if ((key) == nullptr) \ 34 NDN_THROW(Error("Public key has not been loaded yet")); \ 37 #define ENSURE_PUBLIC_KEY_NOT_LOADED(key) \ 39 if ((key) != nullptr) \ 40 NDN_THROW(Error("Public key has already been loaded")); \ 65 : m_impl(make_unique<Impl>())
77 switch (detail::getEvpPkeyType(m_impl->key)) {
92 if (d2i_PUBKEY(&m_impl->key, &buf, static_cast<long>(size)) ==
nullptr)
137 int keyType = detail::getEvpPkeyType(m_impl->key);
142 return rsaEncrypt(plainText, plainLen);
149 PublicKey::getEvpPkey()
const 155 PublicKey::toPkcs8()
const 159 uint8_t* pkcs8 =
nullptr;
160 int len = i2d_PUBKEY(m_impl->key, &pkcs8);
164 auto buffer = make_shared<Buffer>(pkcs8, len);
171 PublicKey::rsaEncrypt(
const uint8_t* plainText,
size_t plainLen)
const 173 detail::EvpPkeyCtx ctx(m_impl->key);
175 if (EVP_PKEY_encrypt_init(ctx) <= 0)
178 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
183 if (EVP_PKEY_encrypt(ctx,
nullptr, &outlen, plainText, plainLen) <= 0)
186 auto out = make_shared<Buffer>(outlen);
187 if (EVP_PKEY_encrypt(ctx, out->data(), &outlen, plainText, plainLen) <= 0)
Unknown or unsupported key type.
std::string to_string(const T &val)
RSA key, supports sign/verify and encrypt/decrypt operations.
KeyType
The type of a cryptographic key.
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
#define ENSURE_PUBLIC_KEY_NOT_LOADED(key)
implements an output stream that constructs ndn::Buffer
#define ENSURE_PUBLIC_KEY_LOADED(key)
shared_ptr< const Buffer > ConstBufferPtr