Generating unsigned RSA Public Key with openssl

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

Generating unsigned RSA Public Key with openssl

Jan Bilek
Hi,

Following code is simplified to demonstrate plain RSA public key with the OpenSSL library:

    RSA_ptr rsa(RSA_new(), ::RSA_free);
    BN_ptr bn(BN_new(), ::BN_free);
    BN_set_word(bn.get(), RSA_F4);  //65535
    RSA_generate_key_ex(rsa.get(), 320, bn.get(), NULL);
    BIO * keybio = BIO_new(BIO_s_mem());
    i2d_RSAPublicKey_bio(keybio, rsa.get());
    char buffer2 [2048];
    size_t pubKeyBufferSize = BIO_read (keybio, buffer2, 320);
    std::cout << Convert::BinToHexString(buffer2, pubKeyBufferSize); //using here our internal routine to print binary data

Output from this will come up with binary data in DER ANS.1 format like this:

    30 ;SEQUENCE
      30 02 29 ;SEQUENCE + size
        00 ;leading zero of INTEGER
        CCEE6526AE9D4380B670A23F55B840F8C5D8CC784E06E123C126753525FDFE1949...
      02 03 ;SEQUENCE + size
        010001

Now, the "leading zero of INTEGER" part is present to indicate that following value is positive value integer. However I need to get rid of it due to some legacy reasons.

I was going through openssl source and found that through the DER construction its presence is decided based on ASN1_VALUE->type & V_ASN1_NEG, but I am unable to track down where to set generated PublicKey as V_ASN1_NEG (or influence it to be generated as negative).

Other way to handle this is to write my own TLV-DER parser and re-pack these few bytes to comply with what I need, but I would rather enforce API to do that for me, if it makes sense.

Would you have any advice on this?

Thank you,
Jan

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

Re: Generating unsigned RSA Public Key with openssl

OpenSSL - User mailing list

The API cannot do it.  The encoding requires that numbers with the high-bit on have a leading zero to avoid being interpreted as negative numbers as you noticed.  You could maybe generate our own RSA numbers with the high-bit off – i.e., make your own RSA_new kind of API.  The BN code can have flags to not require the high bit on.


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