[openssl-users] An error occurred while using the d2i_RSAPublicKey function.

Sangsub i_chacha at naver.com
Sun Jun 10 00:38:23 UTC 2018

I am getting RSA publickey from the server.
I need to extract the modulus and public exponent from the key to do the RSA
I have to solve with C language code, not shell command.
I have written the following test code, and an error has occurred in the
d2i_RSAPublicKey function.
A null pointer is returned as the result of the d2i_RSAPublicKey function.

void fnStr2Hex(unsigned char* out, char* in) { 
    int data_len = strlen(in); 
    unsigned char * pStr = in; 
    int i; 

    for(i=0; i<data_len/2; i++) { 
    	char buf[3] = {0,}; 
        memcpy(buf, pStr, 2); 
        out[i] = (unsigned char)strtol(buf, NULL, 16); 

int der_test() { 
    BIO *STDout = NULL; 
    RSA *pub_rsa = NULL; 
    char raw_data[] =

    int data_len = strlen(raw_data);  
    unsigned char * pArr = (unsigned char *)malloc(data_len); 
    memset(pArr, 0x00, data_len); 

    // raw_data is a string. Therefore, conversion to hexa form is
    fnStr2Hex(pArr, raw_data); 



	if(pub_rsa == NULL) { 
        printf("error : failed d2i_RSAPublicKey \n"); 
    	return -1; 

    BN_print(STDout,pub_rsa->n);   // print modulus bignum 
    BN_print(STDout,pub_rsa->e);  //  print exponent bignum 

    return 0; 

int main() {

	return 0;

Please let me know if you know what you need to do in order to get
d2i_RSAPublicKey to work properly.
Note that the RSA public key in raw_data has the key at the following site:
This seems to be a key that operates normally.

Sent from: http://openssl.6102.n7.nabble.com/OpenSSL-User-f3.html

More information about the openssl-users mailing list