Advertisement
Guest User

Untitled

a guest
Jun 18th, 2013
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include "opensslhashsign.h"
  2.  
  3. #ifndef MAX_LEN
  4. #define MAX_LEN 256
  5. #endif
  6.  
  7. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. // Entry point for the program
  9. //
  10. /* DATA WHICH IS SIGNED IS -> Hello
  11.    Have pem file converted using openssl from certificate
  12.    Pls Check http://pastebin.com/ExYDcJ2t for data.
  13. */
  14. int main()
  15. {
  16.     //
  17.     // Local variables definition
  18.     //
  19.     const char      cert_filestr[]      = "./TestCert.pem";
  20.     EVP_PKEY*       evpkey              = NULL;
  21.     BIO*            certbio             = NULL;
  22.     BIO*            outbio              = NULL;
  23.     X509*           cert                = NULL;
  24.     EVP_MD_CTX*     ctx                 = NULL;
  25.     unsigned char   sigBuf[MAX_LEN]     = {0};
  26.     char            buf[]               = "Hello";
  27.     int             bufSize             = sizeof (buf);
  28.     int             ret                 = 1;
  29.     int             bytesRet            = 0;
  30.  
  31.     //
  32.     // Filling sigBuf with zeros
  33.     //
  34.     memset(sigBuf, 0, MAX_LEN);
  35.  
  36.     //
  37.     // Function logic begins here
  38.     //
  39.     for (;;)
  40.     {
  41.         //
  42.         //  These function calls initialize openssl for correct work
  43.         //
  44.         OpenSSL_add_all_algorithms();
  45.         ERR_load_BIO_strings();
  46.         ERR_load_crypto_strings();
  47.  
  48.         //
  49.         //  Create the Input/Output BIO's
  50.         //
  51.         certbio = BIO_new(BIO_s_file());
  52.         outbio  = BIO_new(BIO_s_file());
  53.         outbio  = BIO_new_fp(stdout, BIO_NOCLOSE);
  54.  
  55.         //
  56.         // Loading the certificate from file (PEM)
  57.         //
  58.         ret = BIO_read_filename(certbio, cert_filestr);
  59.         cert = PEM_read_bio_X509(certbio, NULL, 0, NULL);
  60.         if (NULL == cert)
  61.         {
  62.             BIO_printf(outbio, "Error loading cert into memory\n");
  63.             break;
  64.         } // if
  65.         printf("Certificate loaded from the .pem file\n\n");
  66.  
  67.         //
  68.         // Extract the certificate's public key data
  69.         //
  70.         evpkey = X509_get_pubkey(cert);
  71.         if (NULL == evpkey)
  72.         {
  73.             BIO_printf(outbio, "Error getting public key from certificate\n");
  74.             break;
  75.         } // if
  76.         printf("Public key extracted from the certificate\n\n");
  77.  
  78.         //
  79.         // Allocating memory for EVP_MD_CTX Context object
  80.         //
  81.         ctx = (EVP_MD_CTX *) malloc(sizeof (EVP_MD_CTX));
  82.         if (NULL == ctx)
  83.         {
  84.             printf("Memory is not allocated for EVP_MD_CTX object\n");
  85.             break;
  86.         } // if
  87.         printf("Memory allocated for EVP_MD_CTX object\n\n");
  88.  
  89.         //
  90.         // Initializing EVP_MD_CTX Context object
  91.         EVP_MD_CTX_init(ctx);
  92.  
  93.         FILE *fp = fopen("./hellosigneddata.txt", "r");
  94.         bytesRet = fread(sigBuf, 1, MAX_LEN, fp);
  95.         if (MAX_LEN != bytesRet)
  96.         {
  97.             printf("Signed buffer has no proper data\n");
  98.             break;
  99.         } // if
  100.         printf("Data in the signed buffer is : %s\n\n", sigBuf);
  101.  
  102.         //
  103.         // Calling EVP_VerifyInit_ex() function to initialize context
  104.         // for verification
  105.         //
  106.         EVP_VerifyInit_ex(ctx, EVP_sha256(), NULL);
  107.  
  108.         //
  109.         // After initializing the context, the signed data to be verified
  110.         // is fed into context. This is done with EVP_VerifyUpdate() function
  111.         //
  112.         EVP_VerifyUpdate(ctx, buf, bufSize);
  113.  
  114.         //
  115.         // Performing the actual verification of the signature using
  116.         // EVP_VerifyFinal() function
  117.         //
  118.         ret = EVP_VerifyFinal(ctx, sigBuf, MAX_LEN, evpkey);
  119.         if (0 == ret)
  120.         {
  121.             printf("Signature doesn't match\n\n");
  122.             break;
  123.         } // if
  124.         printf("Signature verified successfully\n\n");
  125.  
  126.         //
  127.         // Final break statement
  128.         //
  129.         break;
  130.     } // for
  131.  
  132.     getchar();
  133.  
  134.     return 0;
  135. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement