Home > Back-end >  OpenSSL 1.0.0 RSA parameters in C
OpenSSL 1.0.0 RSA parameters in C

Time:10-18

I realized that I cannot use the function RSA_get0_key in OpenSSL 1.0.0 to extract the values of n, e, d by reading the private key from a file and passing it as parameter to the aforementioned function.

It is not a programming issue, I mean, I know how to use the functions, but I don't know if there is an alternative to do this.

Indeed, the warning that stops me during the compiling operation is the following:

warning: implicit declaration of function ‘RSA_get0_key’; did you mean ‘RSA_check_key’? [-Wimplicit-function-declaration]

Do you know how to do that? I check the manual here (https://www.openssl.org/docs/man1.0.2/man3/) but it seems that there isn't a properly function to make this. Further, I need to be compliant to OpenSSL 1.0.0.

Code

#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/obj_mac.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/pem.h>

int main()
{
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    RSA *privkey = RSA_new();
    FILE *privkeyfile = fopen("private.key", "rb");

    PEM_read_RSAPrivateKey(privkeyfile, &privkey, NULL, NULL);
    fclose(privkeyfile);

    BIGNUM *n, *e, *d = NULL;
    RSA_get0_key(privkey,&n,&e,&d);
    
    return 0;
}

CodePudding user response:

The RSA_get0_key function was added in OpenSSL 1.1.0 as an abstraction to retrieve the n, e, and d values for an RSA key. For earlier versions you need to access the fields directly.

n = privkey->n;
e = privkey->e;
d = privkey->d;

If you want your code to be able to handle 1.0.x and 1.1.x, you can check the value of OPENSSL_VERSION_NUMBER:

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
    RSA_get0_key(privkey, &n, &e, &d);
#else
    n = privkey->n;
    e = privkey->e;
    d = privkey->d;
#endif
  • Related