Neil Dugan wrote:

Dr. Stephen Henson wrote:
On Mon, Jul 27, 2009, Neil Dugan wrote:

Hi,

I have been trying to read the keys generated by "openssl genrsa ..." and "openssl rsa -pubout ..." commands.

I successfully (according to the return code) read the private key with

if (in = BIO_new_file("rsakey.pem", "r")) {
    int ok;
    printf ("Created private BIO\n");
    ok = (PEM_read_bio_RSAPrivateKey(in, &rsa, NULL, NULL) != NULL);
    printf ("ok = %s\n", (ok != 0) ? "true":"false");
    BIO_free(in);
}

but the similar code using PEM_read_bio_RSAPublicKey() doesn't want to work.


Use PEM_read_bio_RSA_PUBKEY() instead.

Steve.

Thanks that seemed to work.
But the "PEM_write_RSAPublicKey(stdout, rsa);" call doesn't output the same data as what is in the key file. So did it get read properly?

I found that "PEM_write_RSA_PUBKEY(stdout, rsa);" does print the
correct key.

Now I have updated the code, to try and encrypt/decrypt some test
data.  Why is it causing a segmentation fault at line 39 the
"RSA_private_decrypt(size, from, to, rsa, RSA_NO_PADDING);" call?





#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <stdio.h>

BIO *bio_err=0;

void print_data(unsigned char *data, int size)
{
	int x;
	printf ("0000 : ");
	for (x = 0; x < size; x++) {
		if ((x > 0) && (x % 16 == 0)) 
			printf ("\n%04x : ", x);
		printf ("%02x ", data[x]);
	}
	printf ("\n------------------------------------------------------\n");
}

unsigned char * public_encrypt(RSA *rsa, unsigned char *from)
{
	int size = RSA_size(rsa);
	unsigned char *to;
	if (to = malloc(size)) {
		int x;
		RSA_public_encrypt(size, from, to, rsa, RSA_NO_PADDING);
		return (to);
	}
	else {
		printf("memory allocation error\n");
		return (NULL);
	}
}

unsigned char * private_decrypt(RSA *rsa, unsigned char *from)
{
	int size = RSA_size(rsa);
	unsigned char *to;
	if (to = malloc(size)) {
		int x;
		RSA_private_decrypt(size, from, to, rsa, RSA_NO_PADDING);
		return (to);
	}
	else {
		printf("memory allocation error\n");
		return (NULL);
	}
}

int main (int argc, char**argv)
{
	RSA *rsa;
	int size;

	if ((rsa = RSA_new()) != NULL) {
		printf ("Allocated new RSA structure\n");
		BIO *in;
		if (in = BIO_new_file("rsakey.pem", "r")) {
			int ok;
			printf ("Created private BIO\n");
			ok = (PEM_read_bio_RSAPrivateKey(in, &rsa, NULL, NULL) != NULL);
			printf ("ok = %s\n", (ok != 0) ? "true":"false");
			BIO_free(in);
		}

		if (in = BIO_new_file("pubkey.pem", "r")) {
			int ok;
			printf ("Created public BIO\n");
			ok = (PEM_read_bio_RSA_PUBKEY(in, &rsa, NULL, NULL) != NULL);
			printf ("ok = %s\n", (ok != 0) ? "true":"false");
			BIO_free(in);
		}

		//PEM_write_RSAPublicKey(stdout, rsa);
		PEM_write_RSA_PUBKEY(stdout, rsa);

		size = RSA_size(rsa);
		if (size > 0) {
			unsigned char *encrypted;
			unsigned char *plain;
			int x;

			// generate and print the plain text
			plain = malloc(size);
			for (x = 0; x < size; x++) plain[x] = x & 0xFF;
			print_data(plain,size);

			// encrypt and print
			encrypted = public_encrypt(rsa,plain);
			print_data(encrypted,size);
			free (plain);

			// decrypt and print
			plain = private_decrypt(rsa,encrypted);
			print_data(plain,size);

			// cleanup
			free(encrypted);
			free(plain);
		}
		RSA_free(rsa);
	}
	
}

Reply via email to