Hi,

i had tried to generating the key and writing in the pem file...but it is
giving segmentation fault...without .readprivatekey and readpublickey
functions these is generating pem file...i dont know why?

any knows guide me

#include<stdio.h>
#include<openssl/pem.h>
#include<openssl/bio.h>
#include<openssl/rsa.h>

RSA *generatersa()
{
RSA *rsa;
rsa=RSA_generate_key(2048,RSA_F4,NULL,NULL);
return rsa;

}
writekey(RSA *key2)
{
        EVP_PKEY *pkey;
        FILE *fp;
    //BIO *file;
        OpenSSL_add_all_ciphers();
        OpenSSL_add_all_algorithms();
        //file = BIO_new_file(filename, "w");
        pkey = EVP_PKEY_new();
        EVP_PKEY_assign_RSA(pkey,key2);
//      WRITE PRIVATE KEY
        if(!(fp = fopen("private1.pem", "w"))) {
                fprintf(stderr, "Error opening PEM file %s\n",
"private1.pem");
                exit(1);
        }
        if(!PEM_write_PrivateKey(fp, pkey,NULL,NULL,0,NULL,NULL)){
                fprintf(stderr, "Error writing PEM file %s\n",
"private1.pem");
                exit(1);
        }
        close(fp);
//     WRITE PUBLIC KEY
        if(!(fp = fopen("public1.pem", "w"))) {
        fprintf(stderr, "Error opening PEM file %s\n", "public1.pem");
        exit(1);
        }
        if(!PEM_write_PUBKEY(fp, pkey)){
        fprintf(stderr, "Error writing PEM file %s\n", "public1.pem");
        exit(1);
        }
 close(fp);
}


RSA * readPrivKey(char *filename)
{
 RSA *key;
 BIO *bp;
 OpenSSL_add_all_ciphers();
 OpenSSL_add_all_algorithms();

 bp=BIO_new(BIO_s_file());
 if (BIO_read_filename(bp,filename) <= 0)
 {
 perror("ERROR: rsakey.pem");
 exit(0);
 }
 if ((key=(RSA *)PEM_read_bio_RSAPrivateKey(bp,NULL,NULL,NULL)) == NULL) {
 ERR_print_errors_fp(stderr);
 key = NULL;
 }

 BIO_free(bp);
 return key;

}



RSA * readPubKey(char *filename)
 {
 RSA *key;
 BIO *bp;

 ERR_load_crypto_strings();
 bp=BIO_new(BIO_s_file());

 if (BIO_read_filename(bp,filename) <= 0)
 {
 perror("ERROR: public.pem");
 exit(0);
 }

 if ((key=(RSA *)PEM_read_bio_RSA_PUBKEY(bp,NULL,NULL,NULL)) == NULL)
 {
 ERR_print_errors_fp(stderr);
 key = NULL;
 }

 BIO_free(bp);
 return key;
 }
int main(void)
{
    RSA *key1;
    FILE *fp;
    RSA *pubkey;
     RSA *privkey;
     char **key;
    key1=generatersa();
    writekey(key1);
    pubkey = readPubKey("public1.pem");
     privkey = readPrivKey("private1.pem");
    printf("size of (in byte)s pu:pr :: %d:%dn",
RSA_size(pubkey),RSA_size(privkey));
    RSA_free(key1);
}
ki...@kicha-laptop:~/Downloads$ ./output2
2438:error:0906D06C:PEM routines:PEM_read_bio:no start
line:pem_lib.c:650:Expecting: PUBLIC KEY
2438:error:0906D06C:PEM routines:PEM_read_bio:no start
line:pem_lib.c:650:Expecting: ANY PRIVATE KEY
Segmentation fault
ki...@kicha-laptop:~/Downloads$ ls
function1.c  function2.c  function3.c  function4.c  openssl.c  output1
output2  private1.pem  public1.pem  read.c
ki...@kicha-laptop:~/Downloads$ cat private.pem
cat: private.pem: No such file or directory
ki...@kicha-laptop:~/Downloads$ cat public1.pem
ki...@kicha-laptop:~/Downloads$ cat public1.pem
ki...@kicha-laptop:~/Downloads$ cat private1.pem
ki...@kicha-laptop:~/Downloads$


Thanks for your time,
Krishnamurthy

Reply via email to