> From: [email protected] On Behalf Of Mrigank
> Sent: Friday, 20 February, 2009 03:11
> I wanted to write an application wherein I had to "send" a server's public
key (RSA) to a client.
> So I generated the RSA keys with RSA_generate_key and checked it with
RSA_check_key.
> I then tried copying the "entire" RSA structure pointer returned by
RSA_generate_key
> to a send buffer and tried to send it over a TCP connection.
> (For the first cut I had decided to send the entire structure containing
both the keys
> and then "narrow" it down to only the public key later on..)
openssl's RSA = struct rsa_st doesn't contain the actual key data,
only pointers to it. And openssl uses the same struct for RSA public
and private keys; a public instance just has the private fields null.
You need to 'marshal' (encode) the actual values into one data block,
send that, and decode/parse/unmarshal on the receiver. The canonical
encoding is ASN.1 and provided by {i2d,d2i}_RSA{Public,Private}Key.
('i' means internal, and 'd' means DER which is the ASN.1 form used.)
You could create and write your own encoding if you want, but why bother?
Note that a public key by itself is often subject to substitution attacks.
Unless the channel you are sending it over already has authentication
and integrity, you may want to consider adding authentication. This
is why often a public key is encapsulated into a (signed) certificate
and the certificate sent, and used, instead of just the bare key.
And of course you don't ever want to send your private key to another party;
then it isn't private. Although you may want to send it to another system
that also "is" (represents) you, and in that case you want to do so only
over secure = confidential (AND tamperproof) means.
> The key was not getting "received" properly at the other end -
> some of the fields were modified and hence the key was not valid.
I would expect all of the fields to basically be garbage!
> I then tried writing the keys to .pem files using PEM_write_RSAPrivateKey
> and PEM_write_RSAPublicKey. Both the functions simply "hanged" upon
calling
> them (i.e. they never returned control..).
write_RSAPublicKey should definitely work, and write_RSAPrivateKey should
unless you give it a password callback that hangs or otherwise fails.
> I am at a loss to understand why is this happening..
> I want to have the following information in general:
> Sample methods to send a public key over a TCP connection.
See above.
> What could have possibly made the PEM_write_RSAPrivateKey
> and PEM_write_RSAPublicKey fail..?
No idea. Unless there's weird problems with your filesystem,
like for example a long-distance NFS mount or something.
Actually, that's an idea. Is this on Windows with MS compiler
or compatible e.g. mingw, and if so, did you either use the same
C library option for your code as for openssl library, or use
the (recentish) 'applink' feature to set the correct C library?
If not, file I/O in openssl could malfunction in bizarre ways.
> Why possibly was the RSA key-structure not getting received properly at
the client end?
See above.
> Finally, I would like to store the keys in an encrypted fashion in .pem
files
> instead of storing them in cleartext..
PEM_write_RSAPrivateKey will encrypt (and read will decrypt) using
passphrase-based-encryption (PBE) if you give it a cipher and a passphrase
either directly or via a callback, and this is the recommended practice.
(Also the more general privatekey routines like *PKCS8PrivateKey.)
If you want something else, be more specific.
Public keys aren't usually encrypted since, well, they're public.
If you want it, you'll have to come up with the format yourself.
Probably the most straightforward would the same as the approach used
for privatekey.pem -- i2d, PBE, b64, and add (distinct) labels.
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [email protected]
Automated List Manager [email protected]