Hi all,
This is the code i am using for writing the private key into pem file . But when i am trying to use PEM_write_PrivateKey() function to write the private keys into the file. It is not doing it. The console screen get closed automatically after this function call. And the private.pem file doesn't contains anything.Please give some solution for this problem.
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/pkcs12.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <openssl/buffer.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#include <openssl/rand.h>
int _tmain(int argc, _TCHAR* argv[])
{
exrsa obj_rsa;
/* Simple PKCS#12 file reader */
FILE *fp;
PKCS12 *p12_cert = NULL;
EVP_PKEY *pkey;
X509 *cert;
STACK_OF(X509) *ca = NULL;
PKCS12 *p12;
int i;
OpenSSL_add_all_algorithms();
SSL_library_init();
ERR_load_crypto_strings();
if (!(fp= fopen("D://rsa//rsa.p12", "rb+"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
////////////////////////////////
BIO * in=NULL;
X509 * x509=NULL;
if((in=BIO_new_file("D://rsa//rs.p12", "r")) == NULL)
return NULL;
p12 = d2i_PKCS12_bio(in, NULL);
fclose (fp);
fflush(fp);
if (!p12) {
fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit (1);
}
if(!PKCS12_verify_mac(p12,"password",8))
{
exit(1);
}
if (!PKCS12_parse(p12, "password", &pkey, &cert, &ca)) {
fprintf(stderr, "Error parsing PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit (1);
}
PKCS12_free(p12);
FILE *fpp;
if (!(fpp = fopen("D://rsa//rsop.pem", "w"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
if (pkey) {
fprintf(fpp, "***Private Key***\n");
if(! PEM_write_PrivateKey(fpp, pkey, NULL,NULL, 0,0, NULL))
{
exit(1);
}
}
if (cert) {
fprintf(fp, "***User Certificate***\n");
PEM_write_X509_AUfp, cert);
}
if (ca && sk_X509_num(ca)) {
fprintf(fp, "***Other Certificates***\n");
for (i = 0; i < sk_X509_num(ca); i++)
PEM_write_X509_AUfp, sk_X509_value(ca, i));
}
sk_X509_pop_free(ca, X509_free);
X509_free(cert);
EVP_PKEY_free(pkey);
fclose(fp);
return 0;
}
Regards,Revathy SB