I have generated a rsa public and private key with.

openssl genrsa -out server.pem 2048
openssl rsa -in server.pem -pubout -out server.pub

When I use the PEM_read_RSAPublicKey() command to load that public key I get the error "error:0906D06C:PEM routines:PEM_read_bio:no start line"

I don't understand this, there is clearly a line at the beginning of the file "-----BEGIN PUBLIC KEY-----"

Attached are the is the key file and the function I am using to load the file.

This prints the following output.

$ ./client localhost 10000
loading server.pub
rsa = 0xa052b0
opened the file
key file = 451 bytes
*************************************************
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyRkV19YoR3OydwYTrj7S
6Cl4n7P06j6A7OtEUVtcpx9OEdoYm08Di28TtphmC4c/n3nqFljXLOIimTGuzHPI
oTtX83X05InUSTHgfEZhSv++tIayUrKdHjSkhfD3yvKJh5FyVoZtnm7VU6yEMJ92
1blNDc3AWaYYsQjII/0A6a26mYvjroBqWiO7TeeEx3uOp1pQIWLf1SvZy1uVW1cg
i7tEIK4BQqnMyiki+OYBliYFBdzzCzVdSq5BMSzVg/ZsAkThwIqTWLT5PGeQhlpi
+WEncstLr80OA/xYX7WQWnpcezx92pgm7lsH0LBLJA1DmLheSP9y1BVWbH2HwFJo
RQIDAQAB
-----END PUBLIC KEY-----
*************************************************
read the public key
ret = (nil)
failed to read any key: error:0906D06C:PEM routines:PEM_read_bio:no start line

What is wrong?  How do I fix it?

Regards Neil
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyRkV19YoR3OydwYTrj7S
6Cl4n7P06j6A7OtEUVtcpx9OEdoYm08Di28TtphmC4c/n3nqFljXLOIimTGuzHPI
oTtX83X05InUSTHgfEZhSv++tIayUrKdHjSkhfD3yvKJh5FyVoZtnm7VU6yEMJ92
1blNDc3AWaYYsQjII/0A6a26mYvjroBqWiO7TeeEx3uOp1pQIWLf1SvZy1uVW1cg
i7tEIK4BQqnMyiki+OYBliYFBdzzCzVdSq5BMSzVg/ZsAkThwIqTWLT5PGeQhlpi
+WEncstLr80OA/xYX7WQWnpcezx92pgm7lsH0LBLJA1DmLheSP9y1BVWbH2HwFJo
RQIDAQAB
-----END PUBLIC KEY-----
RSA *setup_rsa (char *keyfile, int32 key)
{
	BIO *in;
	RSA *rsa = RSA_new();
	int ok;

	printf ("loading %s\n", keyfile);
	rsa = RSA_new();
	if (rsa == NULL) {
		fprintf(stderr, "ERROR: failed to setup rsa\n");
		return (NULL);
	} 
	printf ("rsa = %p\n", rsa);
	ERR_load_crypto_strings(); 

	{
		FILE *fp;
		if (fp = fopen(keyfile, "r")) {
			RSA *ret;
			#ifdef PRINT_KEY_FILE
			struct stat st;
			#endif
			printf ("opened the file\n");
			
			#ifdef PRINT_KEY_FILE
			if (fstat(fileno(fp), &st) == 0) {
				int32 length;
				uint8 *file;
				length = st.st_size;
				if (file = malloc(length)) {
					if (fread(file, length, 1, fp)) {
						printf ("key file = %d bytes\n", length);
						printf ("*************************************************\n");
						fwrite (file, length, 1, stdout);
						printf ("*************************************************\n");
						rewind(fp);
					}
					free (file);
				}
			}
			#endif
			
			if (key == KEY_PUBLIC) {
				ret = PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL); 
				printf ("read the public key\n");
			}
			else if (key == KEY_PRIVATE) {
				ret = PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL);
				printf ("read the private key\n");
			}
			printf ("ret = %p\n", ret);
			if (ret == NULL) {
				int error = ERR_get_error();
				char buffer[256];
				ERR_error_string_n(error, buffer, sizeof(buffer));
				printf ("failed to read any key: %s\n", buffer);
			}

			fclose(fp);
		}
	}

	return (rsa);
}


Reply via email to