42 lines
1.4 KiB
C
42 lines
1.4 KiB
C
#include "openssl-util.h"
|
|
#include "alloc-util.h"
|
|
|
|
#if HAVE_OPENSSL
|
|
int rsa_encrypt_bytes(
|
|
EVP_PKEY *pkey,
|
|
const void *decrypted_key,
|
|
size_t decrypted_key_size,
|
|
void **ret_encrypt_key,
|
|
size_t *ret_encrypt_key_size) {
|
|
|
|
_cleanup_(EVP_PKEY_CTX_freep) EVP_PKEY_CTX *ctx = NULL;
|
|
_cleanup_free_ void *b = NULL;
|
|
size_t l;
|
|
|
|
ctx = EVP_PKEY_CTX_new(pkey, NULL);
|
|
if (!ctx)
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to allocate public key context");
|
|
|
|
if (EVP_PKEY_encrypt_init(ctx) <= 0)
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to initialize public key context");
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to configure PKCS#1 padding");
|
|
|
|
if (EVP_PKEY_encrypt(ctx, NULL, &l, decrypted_key, decrypted_key_size) <= 0)
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to determine encrypted key size");
|
|
|
|
b = malloc(l);
|
|
if (!b)
|
|
return -ENOMEM;
|
|
|
|
if (EVP_PKEY_encrypt(ctx, b, &l, decrypted_key, decrypted_key_size) <= 0)
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to determine encrypted key size");
|
|
|
|
*ret_encrypt_key = TAKE_PTR(b);
|
|
*ret_encrypt_key_size = l;
|
|
|
|
return 0;
|
|
}
|
|
#endif
|