hello
im trying to write the private key into the file
and read back the key . im using
PEM_read_bio_Privatekey,PEM_write_bio_Privatekey
im also using password so the key is encrypted and
kept
please see the code . the code is working fine for
writing and problem is i cannot read back the key.
so please make suggestions.
the code generate the keys store the private key in
the file and tries to read back
thanx in advance
-------------------------------------------------------
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
EVP_PKEY *publicKey,*privateKey;
void generate()
{
RSA *rsa, *pub_rsa, *priv_rsa;
int len;
unsigned char *buf, *p;
X509 *x509;
rsa = RSA_generate_key (1024, RSA_F4, NULL,NULL);
buf = (unsigned char *) malloc (2048);
p = buf;
/* Save the public key into buffer, we know it will
be big enough
* but we should really check how much space we need
by calling the
* i2d functions with a NULL second parameter */
len = i2d_RSAPublicKey (rsa, &p);
len += i2d_RSAPrivateKey (rsa, &p);
printf ("The public and private key are now both in
a char array\n");
printf ("and are taking up %d bytes\n", len);
RSA_free (rsa);
p = buf;
pub_rsa = d2i_RSAPublicKey (NULL, &p, (long) len);
len -= (p - buf);
priv_rsa = d2i_RSAPrivateKey (NULL, &p, (long) len);
if ((pub_rsa == NULL) || (priv_rsa == NULL))
ERR_print_errors_fp (stderr);
publicKey= EVP_PKEY_new ();
privateKey = EVP_PKEY_new ();
/* convert rsa public and private keys into EVP
PUBLIC AND PRIVATE KEYS */
printf ("\n the public key result code ....%d\n",
EVP_PKEY_set1_RSA (publicKey, pub_rsa));
printf ("\n the private key result code...%d\n",
EVP_PKEY_set1_RSA (privateKey, priv_rsa));
/* check private key and public key consistencyd */
x509 = X509_new ();
X509_set_pubkey (x509, publicKey);
printf ("\n checking private and public key
consistency..%d\n",
X509_check_private_key (x509, privateKey));
RSA_free (pub_rsa);
RSA_free (priv_rsa);
}
EVP_PKEY *loadkey(unsigned char * file,unsigned char*
pass)
{
BIO *key=NULL;
EVP_PKEY *pkey=NULL;
key=BIO_new(BIO_s_file());
if(key==NULL)
return -1;
if(BIO_read_filename(key,file)==-1)
return -1;
pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,pass);
return pkey;
}
int setkey(unsigned char *file,EVP_PKEY
*prikey,unsigned char *pass)
{
BIO *key=NULL;
key=BIO_new(BIO_s_file());
if(key==NULL)
return -1;
if(BIO_write_filename(key,file)==-1)
return -1;
PEM_write_bio_PrivateKey(key,prikey,NULL,NULL,0,NULL,pass);
return 1;
}
int main()
{
EVP_PKEY *tempkey;
generate();
setkey("priv21.pem",privateKey,NULL);
tempkey=loadkey("priv21.pem",NULL);
if(tempkey==NULL)
{
printf("\n hey this is not the way to do it \n");
exit(1);
}
return 1;
}
-------------------------------------------------------
____________________________________________________________
Do You Yahoo!?
For regular News updates go to http://in.news.yahoo.com
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]