i'm fairly new to OpenSSL and my current task at work is updating the code of a project from OpenSSL 1.1.1 to OpenSSL 3.0 and I'm stuck on a really weird problem.
I'm trying to create an RSA key from given raw data, precisely binary modulus and exponent that are converted to BIGNUM. I've tried to make it as it is described in the manual and the key creation works, but the problem is whatever I try to do with that key (or CTX based on it) fails, whether it's signature decryption or verification.
Here's the code where I create the key:
/* function set up above */
ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
if ( ctx == NULL) {
ERROR("Error: failed to initialize CTX from name.\n");
goto OPENSSL_ERROR;
}
modulus = BN_bin2bn(pubkey->data, pubkey->size, NULL);
exponent = BN_bin2bn(exp_arr, 3, NULL);
if ( modulus == NULL || exponent == NULL ) {
goto OPENSSL_ERROR;
}
OSSL_PARAM params[] = {
OSSL_PARAM_BN("n", &modulus, BN_num_bytes(modulus)*8),
OSSL_PARAM_BN("e", &exponent, BN_num_bytes(exponent)*8),
OSSL_PARAM_BN("d", NULL, 0),
OSSL_PARAM_END
};
status = EVP_PKEY_fromdata_init(ctx);
if ( status <= 0 ) {
ERROR("Error: failed to initialize key creation.\n");
goto OPENSSL_ERROR;
}
status = EVP_PKEY_fromdata(ctx, &evp_key, EVP_PKEY_PUBLIC_KEY, params);
if ( status <= 0 || evp_key == NULL ) {
ERROR("Error: failed to create key.\n");
goto OPENSSL_ERROR;
}
/* goes on to decrypt signature and later verify it against data - both fail */
It goes through swiftly, but trying to do whatever with that key or CTX based on that key fails miserably. I tried creating CTX with EVP_PKEY_CTX_new_id(), but it changed nothing. I tried omitting the parameter "d", but it changed nothing.
If anyone knows what I'm doing wrong and what should be changed - all help will be greatly appreciated.
P.S.
- pubkey is passed to the function as argument, data is a binary array, size is obviously it's size (size_t)
- the params given to the key are the same as in the previous version of the code which used the
RSA_set0_key()
function unsigned char exp_arr[] = {0x01, 0x00, 0x01};
RSA_Generate_Key
. Maybe this post could help? – WhorledRSA_Generate_Key
is deprecated in OSSL 3.0 (as it is low-level), the function I'm using is pointed to by the official migration guide. – Percyperdido