Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read DER-encoded RSA public key in memory?

1,754 views
Skip to first unread message

Jeremy R.

unread,
Jul 20, 2009, 3:26:55 PM7/20/09
to
I'm trying to make a simple application which uses a 4096-bit RSA
public key (encoded in DER format, statically compiled into the
program itself. I generated this key with OpenSSL itself and I am able
to do operations with it from the command-line. And I know I encoded
it in the program correctly, because if I ask it to write
TESTING_PUBLIC_KEY to disk, OpenSSL continues to accept it.

However, when I try to use d2i_PublicKey to load it, it returns NULL.
Anyone have any hints that might help me?

My code is at http://pastebin.ca/1501265 and I'm compiling with VC++
(in C mode), for what it's worth.

--
Jeremy R.
NovaWave Solutions
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openss...@openssl.org
Automated List Manager majo...@openssl.org

Mounir IDRASSI

unread,
Jul 20, 2009, 5:10:18 PM7/20/09
to
Hi,

The public key in your source is encoded as a SubjectPublicKeyInfo, so
you can't use d2i_PublicKey which only handles RSA public keys encoded
in the PKCS#1 format. In your case, you have to use the function
d2i_PUBKEY_bio to read your hard-coded key.
Here is how you can do it using the same variables of your code :

BIO* keyBio = BIO_new_mem_buf(TESTING_PUBLIC_KEY,
sizeof(TESTING_PUBLIC_KEY));
public_key = d2i_PUBKEY_bio(keyBio, NULL);

That's it!
I hope this will help.

Cheers,
--
Mounir IDRASSI
IDRIX
http://www.idrix.fr


Jeremy R. wrote:
> I'm trying to make a simple application which uses a 4096-bit RSA
> public key (encoded in DER format, statically compiled into the
> program itself. I generated this key with OpenSSL itself and I am able
> to do operations with it from the command-line. And I know I encoded
> it in the program correctly, because if I ask it to write
> TESTING_PUBLIC_KEY to disk, OpenSSL continues to accept it.
>
> However, when I try to use d2i_PublicKey to load it, it returns NULL.
> Anyone have any hints that might help me?
>
> My code is at http://pastebin.ca/1501265 and I'm compiling with VC++
> (in C mode), for what it's worth.
>

______________________________________________________________________

Jeremy R.

unread,
Jul 20, 2009, 5:54:00 PM7/20/09
to
Thanks. I switched to using d2i_PUBKEY (it really is hard-coded, so I =20=

don't think there's a reason to use BIO =96 if I'm mistaken, please tell =
=20
me) and it now returns a valid address in memory.

On 20-Jul-09, at 4:59 PM, Mounir IDRASSI wrote:

> Hi,
>
> The public key in your source is encoded as a SubjectPublicKeyInfo, =20=

> so you can't use d2i_PublicKey which only handles RSA public keys =20
> encoded in the PKCS#1 format. In your case, you have to use the =20


> function d2i_PUBKEY_bio to read your hard-coded key.
> Here is how you can do it using the same variables of your code :
>

> BIO* keyBio =3D BIO_new_mem_buf(TESTING_PUBLIC_KEY, =20
> sizeof(TESTING_PUBLIC_KEY));
> public_key =3D d2i_PUBKEY_bio(keyBio, NULL);


>
> That's it!
> I hope this will help.
>
> Cheers,
> --
> Mounir IDRASSI
> IDRIX
> http://www.idrix.fr
>
>
> Jeremy R. wrote:

>> I'm trying to make a simple application which uses a 4096-bit RSA =20
>> public key (encoded in DER format, statically compiled into the =20
>> program itself. I generated this key with OpenSSL itself and I am =20
>> able to do operations with it from the command-line. And I know I =20
>> encoded it in the program correctly, because if I ask it to write =20


>> TESTING_PUBLIC_KEY to disk, OpenSSL continues to accept it.
>>

>> However, when I try to use d2i_PublicKey to load it, it returns =20


>> NULL. Anyone have any hints that might help me?
>>

>> My code is at http://pastebin.ca/1501265 and I'm compiling with VC+=20=

Mounir IDRASSI

unread,
Jul 20, 2009, 6:23:22 PM7/20/09
to

Yes, d2i_PUBKEY is sufficient.

--
Mounir IDRASSI
IDRIX
http://www.idrix.fr

Jeremy R. wrote:
> Thanks. I switched to using d2i_PUBKEY (it really is hard-coded, so I

> don't think there's a reason to use BIO � if I'm mistaken, please tell

> me) and it now returns a valid address in memory.
>
> On 20-Jul-09, at 4:59 PM, Mounir IDRASSI wrote:
>
>> Hi,
>>
>> The public key in your source is encoded as a SubjectPublicKeyInfo,

>> so you can't use d2i_PublicKey which only handles RSA public keys

>> encoded in the PKCS#1 format. In your case, you have to use the

>> function d2i_PUBKEY_bio to read your hard-coded key.
>> Here is how you can do it using the same variables of your code :
>>

>> BIO* keyBio = BIO_new_mem_buf(TESTING_PUBLIC_KEY,
>> sizeof(TESTING_PUBLIC_KEY));

>> public_key = d2i_PUBKEY_bio(keyBio, NULL);


>>
>> That's it!
>> I hope this will help.
>>
>> Cheers,
>> --
>> Mounir IDRASSI
>> IDRIX
>> http://www.idrix.fr
>>
>>
>> Jeremy R. wrote:
>>> I'm trying to make a simple application which uses a 4096-bit RSA

>>> public key (encoded in DER format, statically compiled into the

>>> program itself. I generated this key with OpenSSL itself and I am

>>> able to do operations with it from the command-line. And I know I

>>> encoded it in the program correctly, because if I ask it to write

>>> TESTING_PUBLIC_KEY to disk, OpenSSL continues to accept it.
>>>
>>> However, when I try to use d2i_PublicKey to load it, it returns

>>> NULL. Anyone have any hints that might help me?
>>>

>>> My code is at http://pastebin.ca/1501265 and I'm compiling with VC++

0 new messages