Hi,

Your file pk.der contains a public key encoded as a SubjectPublicKeyInfo
and NOT as a PKCS#1 encoding. So, you should use the function
d2i_RSA_PUBKEY instead of d2i_RSAPublicKey in order to read the public
key.

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

> Dear all,
>
> I am working on a project which has to encrypt datas thanks to an RSA
> public key stored in DER format.
>
> I tried to write a test program using the d2i_RSAPublicKey, but the
> function returns NULL. However I'm pretty sure the file is correct,
> since I could use openssl command-line to obtain a public key in PEM
> format.
>
> The code looks like this:
>
> [...]
>
> int main()
> {
>   RSA *rsa = NULL;
>   int fd;
>   unsigned char buf[160];
>   const unsigned char *p = buf;
>   fd = open("pk.der", O_RDONLY);
>   if (fd<0)
>   {
>     exit(1);
>   }
>   if (read(fd, buf, sizeof(buf)) != sizeof(buf))
>   {
>     close(fd);
>     exit(2);
>   }
>   close(fd);
>   rsa = d2i_RSAPublicKey(NULL, &p, sizeof(buf));
>   if (rsa==NULL)
>   {
>     ERR_print_errors_fp(stderr);
>     return 1;
>   }
>   return 0;
> }
>
> Running the program gives back the following message:
> 23354:error:0D0680A8:lib(13):func(104):reason(168):tasn_dec.c:1306:
> 23354:error:0D06C03A:lib(13):func(108):reason(58):tasn_dec.c:830:
> 23354:error:0D08303A:lib(13):func(131):reason(58):tasn_dec.c:749:Field=n,
> Type=RSA
>
> Also, I noticed that if I replace
>   const unsigned char *p = buf;
> by
>   const unsigned char *p = buf + 22;
> Then the call to d2i_RSAPublicKey returns a non-NULL value.
>
> I assume I am doing something the wrong way but can not figure out what.
> Could someone please help ?
>
> The complete code for the program I used is at
> http://inova.snv.jussieu.fr/ssltest.c
> and the DER file that produced the previously shown error messages is at
> http://inova.snv.jussieu.fr/pk.der
>
> In advance, many thanks for any help.
> Sébastien.
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    openssl-users@openssl.org
> Automated List Manager                           majord...@openssl.org
>


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to