Hi there,
I think I follow your question. An RSA private key implicitly contains the
public key already[1]. So if you have generated a key-pair and saved them
to disk - you're already most of the way there. If you don't still have
the private key in memory, load the private key back up. Either way, find
your private key (an (RSA*) pointer) and use any function that outputs an
RSAPublicKey and you'll have what you want (the public key functions
simply ignore the private key components - you can test all this using
"openssl asn1parse" to see for yourself once the files are created).
As for which public key function, you have some choices;
/* Writing an RSA's public key components to an opened file, in PEM format
* (text-based and readable). */
PEM_write_RSAPublicKey(FILE *fp, RSA *rsa);
/* Doing the same if you use BIOs to access files */
PEM_write_bio_RSAPublicKey(BIO *bio, RSA *rsa);
/* Outputing the RSA's public key components in DER format (raw binary -
* PEM takes this and textifies this, but if you don't need text
* readability then this can do). You need to save the resulting output to
* a file.
*
* NB: Call this with pp==NULL if you want to find out how large the
* output will be before you allocate memory.
* NB: After calling this function (*pp) will point to the first byte
* *after* the last byte of the output - so pass a *copy* of the original
* pointer.[2]
*/
int i2d_RSAPublicKey(const RSA *a, unsigned char **pp);
And there's probably others too ...
Hope that helps,
Geoff
[1] It's possible for this to not be the case but never happens in reality
and would take a warped imagination to construct a reason why this
should happen in any remotely normal situation. It won't happen to you
unless you do it to yourself intentionally.
[2] Eg.
int len;
unsigned char *ptr, *copied_ptr;
FILE *fp = fopen("privatekey.der", "w"); /* Open output file */
len = i2d_RSAPublicKey(rsa, NULL); /* Figure out the length */
copied_ptr = ptr = OPENSSL_malloc(len); /* Make room */
if(i2d_RSAPublicKey(rsa, &copied_ptr) != len) /* Generate output */
/* Go mad ... */
/* NB: Here we don't use copied_ptr because it was changed! */
if(fwrite(ptr, 1, len, fp) != len) /* Save output to an open file */
/* Go mad ... */
OPENSSL_free(ptr); /* Release the allocated memory */
fclose(fp); /* Close the private key file */
On Fri, 1 Dec 2000, Thijs Volders wrote:
> Hi,
>
> I recently posted a question about reading and writing RSA keys.
> I want to write the public key to a seperate file because that file needs
> to be distributed. But when i read it, I first of all get "expecting
> public key" error from PEM_read_bio_PUBKEY(). Won't the original RSA
> structure be destroyed (so I lose the private key)??
> or is it maybe possible to encrypt with an RSA key without the private
> key (I am ,obviously ,encrypting only with the given public key) inside.
>
> Just for clearing up why I want the above construction:
>
> I want an RSA keypair, The private and public keys should be stored to
> disk seperatly (maybe a private file with public inside, but public must
> also be alone in a file). The public key will be distributed and must be
> readable by another application to be used for encrypting data which has
> to be send back to the originator.
>
> Maybe anybody can help with the top problem,
>
> Thanks,
>
> Thijs Volders.
>
>
>
>
>
> ______________________________________________________________________
> OpenSSL Project http://www.openssl.org
> User Support Mailing List [EMAIL PROTECTED]
> Automated List Manager [EMAIL PROTECTED]
>
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]