PEM_read problem - Openssl

This is a discussion on PEM_read problem - Openssl ; Hi I need to read a RSA public key from a file... I genrerated the key with the command line tool (openssl -genrsa...) then I extracted the public key in a different file. Both key are in PEM format. However ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: PEM_read problem

  1. PEM_read problem

    Hi I need to read a RSA public key from a file...
    I genrerated the key with the command line tool (openssl -genrsa...)
    then I extracted the public key in a different file. Both key are in
    PEM format.
    However in a program if I try to read from the file with
    PEM_read_RSA_PUBKEY it doesent work. It gets the key but if I try to
    use it give me segmentation fault.
    Here is my code:
    #include
    #include
    #include
    #include

    #define MAXSIZE 512

    int
    main(){
    RSA *public;
    FILE *fd;
    unsigned char plain[] = "m'accattai n'antinna mannaia san bitignu\n";
    unsigned char *cipher;

    cipher = malloc(sizeof(plain));

    if ((fd = fopen("pubkey.pem", "r")) == NULL){
    printf("errore\n");
    exit(0);
    }

    RSA_free(public);
    if (!(public = PEM_read_RSA_PUBKEY(fd, &public, NULL, NULL))){
    printf("errore!!!!!\n");
    exit(0);
    }
    fclose(fd);

    RSA_public_encrypt(sizeof(plain), plain, cipher, public,
    RSA_PKCS1_PADDING);
    return(1);
    }
    I tried to do the same but getting the private key and it work right!
    Can anyone help me?
    Thanks in advance!
    Matteo


  2. Re: PEM_read problem

    There is an error if I put:
    cipher = malloc(RSA_size(public));
    before calling RSA_public_encrypt it doesn't give me segmentation fault
    but when I try to decrypt it I get this error(I put the cipher in a
    file):
    openssl rsautl -in cipher -inkey key.pem -decrypt
    RSA operation error
    26865:error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block
    type is not 02:rsa_pk1.c:190:
    26865:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPTadding check
    failed:rsa_eay.c:481:


+ Reply to Thread