Reputation: 12581
I am trying to use add RSA encryption to an app using OpenSSL, but I cannot get a certificate to load. In my app, a public key certificate will be distributed from a server and I need to take that key and use it to decrypt content from encrypted barcodes that my app will scan.
The reason why I am choosing OpenSSL over the security library is that I also require ECDSA validation, which is currently unsupported natively.
I have tried numerous methods to get the key to load. The OpenSSL PEM_read_RSA_PUBKEY method requires a file pointer. However when I run the code below, it crashes with EXC_BAD_ACCESS
as soon as I attempt to access the key.
NSString * path = [[NSBundle mainBundle] pathForResource: @"public" ofType: @"pem"];
FILE *f = fopen([path cStringUsingEncoding:1],"r");
if (f == NULL)
NSLog(@"%@", [path stringByAppendingString:@" not found"]);
RSA *rsa = PEM_read_RSA_PUBKEY(f,NULL,NULL,NULL);
fclose(f);
NSLog (@"%d KeyCheck", RSA_check_key(rsa));
I have the included the following headers from the OpenSSL library
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
I have other OpenSSL methods such as MD5, Base64 encode/decode, SHA1 and the ECDSA code working OK, it's just passing in the RSA certificate that has got me stumped!
The public.pem key file contains this key:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+ynZ7EvJ0F+IoGmlme/j/MpH4
7BxrIuDTJCOS99j82IL3Ww9Ubm28yOMHYPdi23WPDhR80ugaBWAnmqUZWvYKjqd4
Z4D0sJ0NVW3DDgZ4gS57zFqlvGgdVhzaVimfs7qDxIJ1o8GMuXWseZV2ZpmIjdnF
ZBol5zZTqNfk89RNnQIDAQAB
-----END PUBLIC KEY-----
Upvotes: 4
Views: 2055
Reputation: 9395
Your program to read RSA public key is fine.
Problem is with
RSA_check_key
Please read about RSA_check_key here.
It does not work on public key. You need private key for this function. That is why it is crashing since it does have its private key. Sorry for the inconvenience due to previous answers.
http://www.openssl.org/docs/crypto/RSA_check_key.html
Upvotes: 2