read rsa private key (in pem format) from the memory

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

read rsa private key (in pem format) from the memory

Chong Peng
dear all:

i am trying to read the rsa key from the memory (rather than from a file). here is what i did:

1. use the "openssl genrsa -out key.pem 1024" to generate a rsa key in the file key.pem.
2. copy and paste the key from the "key.pem" to my code.
3. use a mem bio to read the key from the memory.

my code to do this is like the following.

the call to "PEM_ASN1_read_bio" always return null. anybody has any idea or similiar experience? thanks a lot

chong peng

/* copy and paste from key.pem */
char rsakey[] =
"-----BEGIN RSA PRIVATE KEY-----\
MIICXAIBAAKBgQC0SF/4JTo3XzffsPeNPbglZ6sz/f/mlUO/CUtB8hk0DTz3V/9r\
iWagrVHjqaF/xikWFsxbzKecRyDDNyhgMWV8eeAVGpJSvmyJZH43MWO1zCiBXsi2\
MSHqQAJOfT803qTc3tPCb5k4UK5ytvwpQ8ZIyokrnQJS0FYKsonf3ASjKwIDAQAB\
AoGAMR3Sv6lsze8sKs5s81cQV2iCFT0rPegGuAJRNZs+0JaWuJCJ7wNVKYtu1wa9\
EDGtue3mKVB9ja83NthNML/kdOszLc1G6NVnWYSzgBPPsyPAJkSZw8TQKODmw+LF\
sqGFjC73s49/lWO12Tv8qA0Zf4sXRY9dMiqX5kA5m8OWXfECQQDYkv2B1xfNK41v\
PPeggVapasX53ZIiOdjc5UuaOWU7GDLhlyyFUCkDdx4eviBAEclWfNSueJNcK1Me\
pulScGFTAkEA1RoXxsYgFVbZsK1i9hjxEqoWzP7dQBJTWqi/77BaPQvqX12ctVk0\
pa0sR4XEKxGOBr11XJVlloTjpmm1hwLDyQJBAM25o1IpLhTZIDrgoSE4e0fngzQ9\
A0m7xYLf1RclGkIuVHbykXn5kVwXVOdDF4OE4cpkPeuV4fUVuplNWCnVUr0CQBWR\
a4ChwtOGE8hO9ComQhf6gQ5EaU43zJnrZGm09p0hHJqEVf0Ax1RRX57pif4166MA\
/+Tb9gky7/uCzW2ZuQkCQFUoAhZnV9sQoifQpkCE10J3fZNyNLEvHKU3b4/rwvn7\
5W618+Fr0DiwBkH07YSWRCVvi8rsYrK2/25DXSbXbD8=\
-----END RSA PRIVATE KEY-----";

EVP_PKEY *pkey = NULL;

int readRsaKey(void)
{
        BIO *bio;

        if( (bio=BIO_new_mem_buf((void *)rsakey, sizeof(rsakey))) == NULL)
        {
                return(ERROR);
        }

        if( (pkey=PEM_ASN1_read_bio(d2i_PrivateKey, PEM_STRING_EVP_PKEY, bio, NULL, NULL, NULL)) == NULL)
        {
                return(ERROR);
        }

        BIO_free(bio);
}
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: read rsa private key (in pem format) from the memory

Dr. Stephen Henson
On Thu, Feb 02, 2006, Chong Peng wrote:

> dear all:
>
> i am trying to read the rsa key from the memory (rather than from a file). here is what i did:
>
> 1. use the "openssl genrsa -out key.pem 1024" to generate a rsa key in the file key.pem.
> 2. copy and paste the key from the "key.pem" to my code.
> 3. use a mem bio to read the key from the memory.
>
> my code to do this is like the following.
>
> the call to "PEM_ASN1_read_bio" always return null. anybody has any idea or similiar experience? thanks a lot
>
> chong peng
>
> /* copy and paste from key.pem */
> char rsakey[] =
> "-----BEGIN RSA PRIVATE KEY-----\
> MIICXAIBAAKBgQC0SF/4JTo3XzffsPeNPbglZ6sz/f/mlUO/CUtB8hk0DTz3V/9r\
> iWagrVHjqaF/xikWFsxbzKecRyDDNyhgMWV8eeAVGpJSvmyJZH43MWO1zCiBXsi2\

Well there aren't any line feeds (\n) in there so you the whole thing is being
incorrectly represented as a single line.

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: read rsa private key (in pem format) from the memory

Chong Peng
In reply to this post by Chong Peng
thanks steve. i got my code working.

actually, this is pretty easy. following is my final code. hopefully, this can do a little bit help to those who wants to do the same thing.
note that char array skey is copy and paste from a pem file generated by using the "openssl genrsa -out key.pem 1024".






EVP_PKEY *pkey = NULL;

const char skey[] =
"-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC0SF/4JTo3XzffsPeNPbglZ6sz/f/mlUO/CUtB8hk0DTz3V/9r
iWagrVHjqaF/xikWFsxbzKecRyDDNyhgMWV8eeAVGpJSvmyJZH43MWO1zCiBXsi2
MSHqQAJOfT803qTc3tPCb5k4UK5ytvwpQ8ZIyokrnQJS0FYKsonf3ASjKwIDAQAB
AoGAMR3Sv6lsze8sKs5s81cQV2iCFT0rPegGuAJRNZs+0JaWuJCJ7wNVKYtu1wa9
EDGtue3mKVB9ja83NthNML/kdOszLc1G6NVnWYSzgBPPsyPAJkSZw8TQKODmw+LF
sqGFjC73s49/lWO12Tv8qA0Zf4sXRY9dMiqX5kA5m8OWXfECQQDYkv2B1xfNK41v
PPeggVapasX53ZIiOdjc5UuaOWU7GDLhlyyFUCkDdx4eviBAEclWfNSueJNcK1Me
pulScGFTAkEA1RoXxsYgFVbZsK1i9hjxEqoWzP7dQBJTWqi/77BaPQvqX12ctVk0
pa0sR4XEKxGOBr11XJVlloTjpmm1hwLDyQJBAM25o1IpLhTZIDrgoSE4e0fngzQ9
A0m7xYLf1RclGkIuVHbykXn5kVwXVOdDF4OE4cpkPeuV4fUVuplNWCnVUr0CQBWR
a4ChwtOGE8hO9ComQhf6gQ5EaU43zJnrZGm09p0hHJqEVf0Ax1RRX57pif4166MA
/+Tb9gky7/uCzW2ZuQkCQFUoAhZnV9sQoifQpkCE10J3fZNyNLEvHKU3b4/rwvn7
5W618+Fr0DiwBkH07YSWRCVvi8rsYrK2/25DXSbXbD8=
-----END RSA PRIVATE KEY-----";

int readKeyFromMem(void)
{
        BIO *bio;

        if( (bio=BIO_new_mem_buf((void *)skey, sizeof(skey))) == NULL)
        {
                return(-1);
        }

        if( (pkey=PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)) == NULL)
        {
                return(-1);
        }

        BIO_free(bio);

        return(0);
}

-----Original Message-----
From: Dr. Stephen Henson [mailto:[hidden email]]
Sent: Thursday, February 02, 2006 1:06 PM
To: [hidden email]
Subject: Re: read rsa private key (in pem format) from the memory


On Thu, Feb 02, 2006, Chong Peng wrote:

> dear all:
>
> i am trying to read the rsa key from the memory (rather than from a file). here is what i did:
>
> 1. use the "openssl genrsa -out key.pem 1024" to generate a rsa key in the file key.pem.
> 2. copy and paste the key from the "key.pem" to my code.
> 3. use a mem bio to read the key from the memory.
>
> my code to do this is like the following.
>
> the call to "PEM_ASN1_read_bio" always return null. anybody has any idea or similiar experience? thanks a lot
>
> chong peng
>
> /* copy and paste from key.pem */
> char rsakey[] =
> "-----BEGIN RSA PRIVATE KEY-----\
> MIICXAIBAAKBgQC0SF/4JTo3XzffsPeNPbglZ6sz/f/mlUO/CUtB8hk0DTz3V/9r\
> iWagrVHjqaF/xikWFsxbzKecRyDDNyhgMWV8eeAVGpJSvmyJZH43MWO1zCiBXsi2\

Well there aren't any line feeds (\n) in there so you the whole thing is being
incorrectly represented as a single line.

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]