Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opensslhashsign.h"
- #ifndef MAX_LEN
- #define MAX_LEN 256
- #endif
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Entry point for the program
- //
- /* DATA WHICH IS SIGNED IS -> Hello
- Have pem file converted using openssl from certificate
- Pls Check http://pastebin.com/ExYDcJ2t for data.
- */
- int main()
- {
- //
- // Local variables definition
- //
- const char cert_filestr[] = "./TestCert.pem";
- EVP_PKEY* evpkey = NULL;
- BIO* certbio = NULL;
- BIO* outbio = NULL;
- X509* cert = NULL;
- EVP_MD_CTX* ctx = NULL;
- unsigned char sigBuf[MAX_LEN] = {0};
- char buf[] = "Hello";
- int bufSize = sizeof (buf);
- int ret = 1;
- int bytesRet = 0;
- //
- // Filling sigBuf with zeros
- //
- memset(sigBuf, 0, MAX_LEN);
- //
- // Function logic begins here
- //
- for (;;)
- {
- //
- // These function calls initialize openssl for correct work
- //
- OpenSSL_add_all_algorithms();
- ERR_load_BIO_strings();
- ERR_load_crypto_strings();
- //
- // Create the Input/Output BIO's
- //
- certbio = BIO_new(BIO_s_file());
- outbio = BIO_new(BIO_s_file());
- outbio = BIO_new_fp(stdout, BIO_NOCLOSE);
- //
- // Loading the certificate from file (PEM)
- //
- ret = BIO_read_filename(certbio, cert_filestr);
- cert = PEM_read_bio_X509(certbio, NULL, 0, NULL);
- if (NULL == cert)
- {
- BIO_printf(outbio, "Error loading cert into memory\n");
- break;
- } // if
- printf("Certificate loaded from the .pem file\n\n");
- //
- // Extract the certificate's public key data
- //
- evpkey = X509_get_pubkey(cert);
- if (NULL == evpkey)
- {
- BIO_printf(outbio, "Error getting public key from certificate\n");
- break;
- } // if
- printf("Public key extracted from the certificate\n\n");
- //
- // Allocating memory for EVP_MD_CTX Context object
- //
- ctx = (EVP_MD_CTX *) malloc(sizeof (EVP_MD_CTX));
- if (NULL == ctx)
- {
- printf("Memory is not allocated for EVP_MD_CTX object\n");
- break;
- } // if
- printf("Memory allocated for EVP_MD_CTX object\n\n");
- //
- // Initializing EVP_MD_CTX Context object
- EVP_MD_CTX_init(ctx);
- FILE *fp = fopen("./hellosigneddata.txt", "r");
- bytesRet = fread(sigBuf, 1, MAX_LEN, fp);
- if (MAX_LEN != bytesRet)
- {
- printf("Signed buffer has no proper data\n");
- break;
- } // if
- printf("Data in the signed buffer is : %s\n\n", sigBuf);
- //
- // Calling EVP_VerifyInit_ex() function to initialize context
- // for verification
- //
- EVP_VerifyInit_ex(ctx, EVP_sha256(), NULL);
- //
- // After initializing the context, the signed data to be verified
- // is fed into context. This is done with EVP_VerifyUpdate() function
- //
- EVP_VerifyUpdate(ctx, buf, bufSize);
- //
- // Performing the actual verification of the signature using
- // EVP_VerifyFinal() function
- //
- ret = EVP_VerifyFinal(ctx, sigBuf, MAX_LEN, evpkey);
- if (0 == ret)
- {
- printf("Signature doesn't match\n\n");
- break;
- } // if
- printf("Signature verified successfully\n\n");
- //
- // Final break statement
- //
- break;
- } // for
- getchar();
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement