[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
operation.
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); 
	pStr+=2; 
    } 
}


int der_test() { 
    BIO *STDout = NULL; 
    RSA *pub_rsa = NULL; 
    char raw_data[] =
   	
"30819F300D06092A864886F70D010101050003818D0030818902818100AA1"\
		"8ABA43B50DEEF38598FAF87D2AB634E4571C130A9BCA7B878267414FAAB8B"\
		"471BD8965F5C9FC3818485EAF529C26246F3055064A8DE19C8C338BE5496C"\
		"BAEB059DC0B358143B44A35449EB264113121A455BD7FDE3FAC919E94B56F"\
		"B9BB4F651CDB23EAD439D6CD523EB08191E75B35FD13A7419B3090F24787B"\
		"D4F4E19670203010001"; 

    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
necessary..
    fnStr2Hex(pArr, raw_data); 

    STDout=BIO_new_fp(stdout,BIO_NOCLOSE); 

    pub_rsa=d2i_RSAPublicKey(NULL,&pArr,(long)data_len); 

	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() {
	der_test();

	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:
https://crypto.stackexchange.com/questions/18031/how-to-find-modulus-from-a-rsa-public-key
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