in src/utils/crypto_utils/src/crypto_lib.c [220:412]
CryptoKeyHandle RSAKey_ObjFromModulusBytesExponentInt(const uint8_t* N, size_t N_len, const unsigned int e)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
int status = 0;
EVP_PKEY* result = NULL;
EVP_PKEY_CTX* ctx = NULL;
OSSL_PARAM_BLD* param_bld = NULL;
OSSL_PARAM* params = NULL;
BIGNUM* bn_N = NULL;
BIGNUM* bn_e = NULL;
ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
if (ctx == NULL)
{
goto done;
}
bn_N = BN_new();
if (bn_N == NULL)
{
goto done;
}
bn_e = BN_new();
if (bn_e == NULL)
{
goto done;
}
if (BN_bin2bn(N, (int)N_len, bn_N) == 0)
{
goto done;
}
if (BN_set_word(bn_e, e) == 0)
{
goto done;
}
param_bld = OSSL_PARAM_BLD_new();
if (param_bld == NULL)
{
goto done;
}
status = OSSL_PARAM_BLD_push_BN(param_bld, "n", bn_N);
if (status != 1)
{
goto done;
}
status = OSSL_PARAM_BLD_push_BN(param_bld, "e", bn_e);
if (status != 1)
{
goto done;
}
status = OSSL_PARAM_BLD_push_BN(param_bld, "d", NULL);
if (status != 1)
{
goto done;
}
params = OSSL_PARAM_BLD_to_param(param_bld);
if (params == NULL)
{
goto done;
}
status = EVP_PKEY_fromdata_init(ctx);
if (status != 1)
{
goto done;
}
status = EVP_PKEY_fromdata(ctx, &result, EVP_PKEY_PUBLIC_KEY, params);
if (status != 1)
{
goto done;
}
done:
if (ctx != NULL)
{
EVP_PKEY_CTX_free(ctx);
}
if (param_bld != NULL)
{
OSSL_PARAM_BLD_free(param_bld);
}
if (params != NULL)
{
OSSL_PARAM_free(params);
}
if (bn_N != NULL)
{
BN_free(bn_N);
}
if (bn_e != NULL)
{
BN_free(bn_e);
}
if (status == 0 && result != NULL)
{
EVP_PKEY_free(result);
result = NULL;
}
return CryptoKeyHandleToEVP_PKEY(result);
#else
_Bool success = false;
EVP_PKEY* pkey = NULL;
BIGNUM* rsa_N = NULL;
BIGNUM* rsa_e = NULL;
RSA* rsa = RSA_new();
if (rsa == NULL)
{
goto done;
}
rsa_N = BN_bin2bn(N, (int)N_len, NULL);
if (rsa_N == NULL)
{
goto done;
}
rsa_e = BN_new();
if (rsa_e == NULL)
{
goto done;
}
if (BN_set_word(rsa_e, e) == 0)
{
goto done;
}
if (RSA_set0_key(rsa, rsa_N, rsa_e, NULL) == 0)
{
goto done;
}
pkey = EVP_PKEY_new();
if (pkey == NULL)
{
goto done;
}
if (EVP_PKEY_assign_RSA(pkey, rsa) == 0)
{
goto done;
}
success = true;
done:
if (!success)
{
if (pkey != NULL)
{
EVP_PKEY_free(pkey);
}
else if (rsa != NULL)
{
RSA_free(rsa);
}
else
{
BN_free(rsa_N);
BN_free(rsa_e);
}
}
return CryptoKeyHandleToEVP_PKEY(pkey);
#endif
}