CryptoKeyHandle RSAKey_ObjFromStrings()

in src/utils/crypto_utils/src/crypto_lib.c [618:801]


CryptoKeyHandle RSAKey_ObjFromStrings(const char* N, const char* 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_hex2bn(&bn_N, N) == 0)
    {
        goto done;
    }

    if (BN_hex2bn(&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
    EVP_PKEY* result = NULL;
    EVP_PKEY* pkey = NULL;
    BIGNUM* M = NULL;
    BIGNUM* E = NULL;

    RSA* rsa = RSA_new();
    if (rsa == NULL)
    {
        goto done;
    }

    M = BN_new();
    if (M == NULL)
    {
        goto done;
    }

    E = BN_new();
    if (E == NULL)
    {
        goto done;
    }

    if (BN_hex2bn(&M, N) == 0)
    {
        goto done;
    }

    if (BN_hex2bn(&E, e) == 0)
    {
        goto done;
    }

    if (RSA_set0_key(rsa, M, E, NULL) == 0)
    {
        goto done;
    }

    pkey = EVP_PKEY_new();
    if (EVP_PKEY_assign_RSA(pkey, rsa) == 0)
    {
        goto done;
    }

    result = pkey;

done:
    if (result == NULL)
    {
        if (pkey != NULL)
        {
            EVP_PKEY_free(pkey);
        }
        else if (rsa != NULL)
        {
            RSA_free(rsa);
        }
        else
        {
            BN_free(M);
            BN_free(E);
        }
    }

    return CryptoKeyHandleToEVP_PKEY(result);
#endif
}