home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / crypto / sign / signfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-09  |  5.0 KB  |  200 lines

  1. /******************************************************************************\
  2. *       This is a part of the Microsoft Source Code Samples. 
  3. *       Copyright 1996-1997 Microsoft Corporation.
  4. *       All rights reserved. 
  5. *       This source code is only intended as a supplement to 
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the 
  8. *       Microsoft samples programs.
  9. \******************************************************************************/
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <wincrypt.h>
  15.  
  16. static BOOL SignFile(PCHAR szSourceFile, PCHAR szSignatureFile, PCHAR szDescription);
  17.  
  18. /*****************************************************************************/
  19. void _cdecl main(int argc, char *argv[])
  20. {
  21.     PCHAR szSourceFile      = NULL;
  22.     PCHAR szSignatureFile = NULL;
  23.     PCHAR szDescription   = NULL;
  24.  
  25.     // Validate argument count.
  26.     if(argc != 4) {
  27.     printf("USAGE: signfile <source file> <signature file> <description>\n");
  28.     exit(1);
  29.     }
  30.  
  31.     // Parse arguments.
  32.     szSourceFile    = argv[1];
  33.     szSignatureFile = argv[2];
  34.     szDescription   = argv[3];
  35.  
  36.     if(!SignFile(szSourceFile, szSignatureFile, szDescription)) {
  37.     printf("Error signing file!\n");
  38.     exit(1);
  39.     }
  40.  
  41.     exit(0);
  42. }
  43.  
  44. /*****************************************************************************/
  45. static BOOL SignFile(PCHAR szSourceFile, PCHAR szSignatureFile, PCHAR szDescription)
  46. {
  47.     FILE *hSourceFile     = NULL;
  48.     FILE *hSignatureFile = NULL;
  49.     INT eof = 0;
  50.  
  51.     HCRYPTPROV hProv = 0;
  52.     HCRYPTHASH hHash = 0;
  53.  
  54.     PBYTE pbSignature = NULL;
  55.     DWORD dwSignatureLen;
  56.  
  57.     PBYTE pbBuffer    = NULL;
  58.     DWORD dwBufferLen;
  59.     DWORD dwCount;
  60.  
  61.     // Get handle to the default provider.
  62.     if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
  63.         printf("Error %x during CryptAcquireContext!\n", GetLastError());
  64.     goto error;
  65.     }
  66.  
  67.     //
  68.     // Hash source file.
  69.     //
  70.  
  71.     // Determine number of bytes to hash at once.
  72.     dwBufferLen = 1000;
  73.  
  74.     // Allocate memory for 'pbBuffer'.
  75.     if((pbBuffer = malloc(dwBufferLen)) == NULL) {
  76.     printf("Out of memory 1!\n");
  77.     goto error;
  78.     }
  79.  
  80.     // Open source file.
  81.     if((hSourceFile = fopen(szSourceFile,"rb")) == NULL) {
  82.     printf("Error opening source file!\n");
  83.     goto error;
  84.     }
  85.  
  86.     // Create a hash object.
  87.     if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
  88.     printf("Error %x during CryptCreateHash!\n", GetLastError());
  89.     goto error;
  90.     }
  91.  
  92.     // Hash source file.
  93.     do {
  94.     // Read up to 'dwBufferLen' bytes from source file.
  95.     dwCount = fread(pbBuffer, 1, dwBufferLen, hSourceFile);
  96.     if(ferror(hSourceFile)) {
  97.         printf("Error reading Plaintext!\n");
  98.         goto error;
  99.         }
  100.     eof = feof(hSourceFile);
  101.  
  102.     // Add data to hash object.
  103.     if(!CryptHashData(hHash, pbBuffer, dwCount, 0)) {
  104.         printf("Error %x during CryptHashData!\n", GetLastError());
  105.         goto error;
  106.     }
  107.     } while(!feof(hSourceFile));
  108.  
  109.     // Close source file.
  110.     fclose(hSourceFile);
  111.     hSourceFile = 0;
  112.  
  113.     // Free 'pbBuffer' memory.
  114.     free(pbBuffer);
  115.     pbBuffer = NULL;
  116.  
  117.  
  118.     //
  119.     // Sign hash object.
  120.     //
  121.  
  122.     // Determine size of signature.
  123.     dwCount = 0;
  124.     if(!CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL, &dwSignatureLen)) {
  125.     printf("Error %x during CryptSignHash 1!\n", GetLastError());
  126.     goto error;
  127.     }
  128.  
  129.     // Allocate memory for 'pbSignature'.
  130.     if((pbSignature = malloc(dwSignatureLen)) == NULL) {
  131.     printf("Out of memory 2!\n");
  132.     goto error;
  133.     }
  134.  
  135.     // Sign hash object (with signature key).
  136.     if(!CryptSignHash(hHash, AT_SIGNATURE, szDescription, 0, pbSignature, &dwSignatureLen)) {
  137.     printf("Error %x during CryptSignHash 2!\n", GetLastError());
  138.     goto error;
  139.     }
  140.  
  141.  
  142.     //
  143.     // Write signature to signature file.
  144.     //
  145.  
  146.     // Open signature file.
  147.     if((hSignatureFile = fopen(szSignatureFile,"wb")) == NULL) {
  148.     printf("Error opening signature file!\n");
  149.     goto error;
  150.     }
  151.  
  152.     // Write signature to signature file.
  153.     fwrite(pbSignature, 1, dwSignatureLen, hSignatureFile);
  154.     if(ferror(hSignatureFile)) {
  155.     printf("Error writing signature!\n");
  156.     goto error;
  157.     }
  158.  
  159.     // Close signature file.
  160.     fclose(hSignatureFile);
  161.     hSignatureFile = 0;
  162.  
  163.     // Free 'pbSignature' memory.
  164.     free(pbSignature);
  165.     pbSignature = NULL;
  166.  
  167.  
  168.     // Destroy hash object.
  169.     CryptDestroyHash(hHash);
  170.     hHash = 0;
  171.  
  172.     // Release provider handle.
  173.     CryptReleaseContext(hProv, 0);
  174.     hProv = 0;
  175.  
  176.     printf("OK\n");
  177.  
  178.     return TRUE;
  179.  
  180.     error:
  181.  
  182.     //
  183.     // Do cleanup
  184.     //
  185.  
  186.     // Close files.
  187.     if(hSourceFile) fclose(hSourceFile);
  188.     if(hSignatureFile) fclose(hSignatureFile);
  189.  
  190.     // Free memory.
  191.     if(pbSignature) free(pbSignature);
  192.     if(pbBuffer) free(pbBuffer);
  193.  
  194.     // Release crypto handles.
  195.     if(hHash) CryptDestroyHash(hHash);
  196.     if(hProv) CryptReleaseContext(hProv, 0);
  197.  
  198.     return FALSE;
  199. }
  200.