LibCrypto: Make RSA class easily configurable

This is a small change to allow subclasses of `RSA` to configure the
`EVP_PKEY_CTX` without rewriting everything.
This commit is contained in:
devgianlu 2024-12-25 22:24:14 +01:00 committed by Ali Mohammad Pur
parent 91c393ea98
commit 6e721110f9
Notes: github-actions[bot] 2025-01-13 16:01:42 +00:00
2 changed files with 10 additions and 2 deletions

View file

@ -223,6 +223,12 @@ ErrorOr<OpenSSL_PKEY> RSA::private_key_to_openssl_pkey(PrivateKeyType const& pri
#undef OPENSSL_SET_KEY_PARAM_NOT_ZERO
ErrorOr<void> RSA::configure(OpenSSL_PKEY_CTX& ctx)
{
OPENSSL_TRY(EVP_PKEY_CTX_set_rsa_padding(ctx.ptr(), RSA_NO_PADDING));
return {};
}
ErrorOr<ByteBuffer> RSA::encrypt(ReadonlyBytes in)
{
auto key = TRY(public_key_to_openssl_pkey(m_public_key));
@ -230,7 +236,7 @@ ErrorOr<ByteBuffer> RSA::encrypt(ReadonlyBytes in)
auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
OPENSSL_TRY(EVP_PKEY_encrypt_init(ctx.ptr()));
OPENSSL_TRY(EVP_PKEY_CTX_set_rsa_padding(ctx.ptr(), RSA_NO_PADDING));
TRY(configure(ctx));
size_t out_size = 0;
OPENSSL_TRY(EVP_PKEY_encrypt(ctx.ptr(), nullptr, &out_size, in.data(), in.size()));
@ -247,7 +253,7 @@ ErrorOr<ByteBuffer> RSA::decrypt(ReadonlyBytes in)
auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
OPENSSL_TRY(EVP_PKEY_decrypt_init(ctx.ptr()));
OPENSSL_TRY(EVP_PKEY_CTX_set_rsa_padding(ctx.ptr(), RSA_NO_PADDING));
TRY(configure(ctx));
size_t out_size = 0;
OPENSSL_TRY(EVP_PKEY_decrypt(ctx.ptr(), nullptr, &out_size, in.data(), in.size()));

View file

@ -222,6 +222,8 @@ public:
void set_private_key(PrivateKeyType const& key) { m_private_key = key; }
protected:
virtual ErrorOr<void> configure(OpenSSL_PKEY_CTX& ctx);
static ErrorOr<OpenSSL_PKEY> public_key_to_openssl_pkey(PublicKeyType const& public_key);
static ErrorOr<OpenSSL_PKEY> private_key_to_openssl_pkey(PrivateKeyType const& private_key);
};