Skip to content

Instantly share code, notes, and snippets.

@sakamoto-poteko
Last active November 18, 2020 15:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakamoto-poteko/396f289682089e1d767e to your computer and use it in GitHub Desktop.
Save sakamoto-poteko/396f289682089e1d767e to your computer and use it in GitHub Desktop.
OpenSSL verify RSA signature, read RSA public key from X509 PEM certificate
#include <stdio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
void verifyRSASignature(unsigned char *originalMessage, unsigned int om_length,
unsigned char *signature, unsigned siglen)
{
int result;
FILE *file;
X509 *x509;
EVP_PKEY *evp_pubkey;
RSA *rsa_pubkey;
file = fopen("developer_signer.pem", "r");
x509 = PEM_read_X509(file, NULL, NULL, NULL);
evp_pubkey = X509_get_pubkey(x509);
rsa_pubkey = EVP_PKEY_get1_RSA(evp_pubkey);
result = RSA_verify(NID_md5, originalMessage, om_length,
signature, siglen, rsa_pubkey);
printf("Signature is %s\n", result == 1 ? "valid" : "invalid");
RSA_free(rsa_pubkey);
EVP_PKEY_free(evp_pubkey);
X509_free(x509);
fclose(file);
}
@paxter
Copy link

paxter commented Aug 19, 2019

This function is wrong. originalMessage has to be the message digest. Check out the manpage: https://www.openssl.org/docs/man1.1.1/man3/RSA_verify.html

@sakamoto-poteko
Copy link
Author

sakamoto-poteko commented Aug 20, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment