Openssl Initializes RSA Public Key

I have an RSA base64 public key that I must use to verify my digital signature. I do not understand how to initialize RSA with a public key.

My code looks something like this:

unsigned char *signature = ""; //signature string
char *original = ""; // my original string
unsigned char sha2HashDigest[SHA256_DIGEST_LENGTH];
SHA256(original, strlen(original), sha2HashDigest);

 char *key = "base64encodedKey";

 RSA *r = RSA_new();
//SET RSA public key?! how?

int result = RSA_verify(NID_sha256, sha2HashDigest, SHA256_DIGEST_LENGTH,
           signature, strlen(signatrue), r);
if (result != 1) // handle error

Note. I am doing this in an iOS application, but I think it does not matter for the question.

UPDATE: I ended up using EVP as suggested by vond. The public key is a PEM file. This is my code:

   FILE *fp = fopen([keyFilePath UTF8String], "r");
    if (!fp) return NO;


    EVP_PKEY *pubKey = PEM_read_PUBKEY(fp,NULL,NULL,NULL);
    EVP_MD_CTX     md_ctx;
    EVP_MD_CTX_init(&md_ctx);

    EVP_VerifyInit(&md_ctx, EVP_sha256());
    EVP_VerifyUpdate (&md_ctx, (unsigned char*)[msgData bytes], [msgData length]);
    int  err = EVP_VerifyFinal (&md_ctx, (unsigned char*) sigData, (unsigned int)[sigData length], pubKey);
    EVP_PKEY_free (pubKey);
+4
source share
1 answer

You can try the following:

const char *pub_key_pem = ...;

BIO *bio = BIO_new_mem_buf((void*)pub_key_pem, strlen(pub_key_pem));
RSA *rsa_pub = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL);

: PEM_read_bio_RSAPublicKey() PKCS # 1 PEM ( "BEGIN/END RSA PUBLIC KEY" / ); PEM "BEGIN/END PUBLIC KEY", PEM_read_bio_RSA_PUBKEY(). . .

- base64, base64 , d2i_RSAPublicKey() d2i_RSA_PUBKEY(), RSA* .

+9

All Articles