I am trying to use adding RSA encryption to an application using OpenSSL, but I cannot get the certificate to download. In my application, the public key certificate will be distributed from the server, and I need to take this key and use it to decrypt the contents of the encrypted barcodes that my application scans.
The reason I choose OpenSSL over the security library is because I also need an ECDSA check, which is not currently supported.
I tried many ways to get the key to download. The OpenSSL method PEM_read_RSA_PUBKEY requires a file pointer. However, when I run the code below, it is reset using EXC_BAD_ACCESS as soon as I try 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 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 like MD5, Base64 encode / decode, SHA1 and the ECDSA code that works fine, it just passes in the RSA certificate, which puzzled me!
The public.pem key contains this key:
-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+ynZ7EvJ0F+IoGmlme/j/MpH4 7BxrIuDTJCOS99j82IL3Ww9Ubm28yOMHYPdi23WPDhR80ugaBWAnmqUZWvYKjqd4 Z4D0sJ0NVW3DDgZ4gS57zFqlvGgdVhzaVimfs7qDxIJ1o8GMuXWseZV2ZpmIjdnF ZBol5zZTqNfk89RNnQIDAQAB -----END PUBLIC KEY-----