An error occurred while using the d2i_RSAPublicKey function.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

An error occurred while using the d2i_RSAPublicKey function.

Sangsub

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[] =
   
&quot;30819F300D06092A864886F70D010101050003818D0030818902818100AA1&quot;\
                &quot;8ABA43B50DEEF38598FAF87D2AB634E4571C130A9BCA7B878267414FAAB8B&quot;\
                &quot;471BD8965F5C9FC3818485EAF529C26246F3055064A8DE19C8C338BE5496C&quot;\
                &quot;BAEB059DC0B358143B44A35449EB264113121A455BD7FDE3FAC919E94B56F&quot;\
                &quot;B9BB4F651CDB23EAD439D6CD523EB08191E75B35FD13A7419B3090F24787B&quot;\
                &quot;D4F4E19670203010001&quot;;

    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,&amp;pArr,(long)data_len);

        if(pub_rsa == NULL) {
        printf(&quot;error : failed d2i_RSAPublicKey \n&quot;);
    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
--
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users
Reply | Threaded
Open this post in threaded view
|

Re: An error occurred while using the d2i_RSAPublicKey function.

Viktor Dukhovni


> On Jun 9, 2018, at 8:38 PM, Sangsub <[hidden email]> wrote:
>
> 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.

Your input data is not a PKCS#1 RSA public key as understood by
d2i_RSAPublicKey().  Rather, it is an X.509 SPKI public key,
as understood by d2i_RSA_PUBKEY(), or, more generally, d2i_PUBKEY().

--
        Viktor.

--
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users
Reply | Threaded
Open this post in threaded view
|

Re: An error occurred while using the d2i_RSAPublicKey function.

Sangsub
This post was updated on .
Thank you viktor,

Actually, I'm begginer so I don't know well.
You said that the my input data is not a PKCS # 1 public key type.

How do I distinguish between a PKCS # 1 type or an X.509 SPKI Public Key?



and it seems to work well when you use d2i_RSA_PUBKEY as you told me.
I would like to ask you one more question.
What is the modulus value of the key I entered?
The modulus values I have come up with the above code are as follows.

my modulus :
AA 18 AB A4 3B 50 DE EF  38 59 8F AF 87 D2 AB 63
4E 45 71 C1 30 A9 BC A7  B8 78 26 74 14 FA AB 8B
47 1B D8 96 5F 5C 9F C3  81 84 85 EA F5 29 C2 62
46 F3 05 50 64 A8 DE 19  C8 C3 38 BE 54 96 CB AE
B0 59 DC 0B 35 81 43 B4  4A 35 44 9E B2 64 11 31
21 A4 55 BD 7F DE 3F AC  91 9E 94 B5 6F B9 BB 4F
65 1C DB 23 EA D4 39 D6  CD 52 3E B0 81 91 E7 5B
35 FD 13 A7 41 9B 30 90  F2 47 87 BD 4F 4E 19 67

However, the modulus values are different in spite of using the same key in the site below.
https://crypto.stackexchange.com/questions/18031/how-to-find-modulus-from-a-rsa-public-key
The modulus values in the link are as follows, and the 00 is that the first one is the difference.
00 AA 18 AB A4 3B 50 DE EF 38 59 8F AF 87 D2 AB
63 4E 45 71 C1 30 A9 BC A7 B8 78 26 74 14 FA AB
8B 47 1B D8 96 5F 5C 9F C3 81 84 85 EA F5 29 C2
62 46 F3 05 50 64 A8 DE 19 C8 C3 38 BE 54 96 CB
AE B0 59 DC 0B 35 81 43 B4 4A 35 44 9E B2 64 11
31 21 A4 55 BD 7F DE 3F AC 91 9E 94 B5 6F B9 BB
4F 65 1C DB 23 EA D4 39 D6 CD 52 3E B0 81 91 E7
5B 35 FD 13 A7 41 9B 30 90 F2 47 87 BD 4F 4E 19
67

The ans.1 decorder that says on that page is prefixed with 00.
I am confused now what is right.
Please let me know what is correct.

Thank you

--
Sent from: http://openssl.6102.n7.nabble.com/OpenSSL-User-f3.html
--
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users
Reply | Threaded
Open this post in threaded view
|

Re: An error occurred while using the d2i_RSAPublicKey function.

Viktor Dukhovni


> On Jun 9, 2018, at 11:25 PM, Sangsub <[hidden email]> wrote:
>
> Actually, I'm begginer so I don't know well.
> You said that the my input data is not a PKCS # 1 public key type.
>
> How do I distinguish between a PKCS # 1 type or an X.509 SPKI Public Key?

You should not have to distinguish.  The key type will be the same
for a particular method of obtaining the key.  In this case, the
key will almost surely always be SPKI.  You've not explained how
you're obtaining the keys, why it is safe to assume they're RSA
and not (say) ECDSA keys, or why you're looking to work with the
public exponent and modulus, rather than use the existing OpenSSL
signature verification functions.

--
        Viktor.

--
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users