Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.6 KiB

  1. /******************************************************************************\
  2. * Adapted by Bruce Fortune (Citrix Systems, Inc.) from MS Online Source
  3. * This is a part of the Microsoft Source Code Samples.
  4. * Copyright (C) 1996 Microsoft Corporation.
  5. * All rights reserved.
  6. * This source code is only intended as a supplement to
  7. * Microsoft Development Tools and/or WinHelp documentation.
  8. * See these sources for detailed information regarding the
  9. * Microsoft samples programs.
  10. \******************************************************************************/
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <wincrypt.h>
  15. #define SIGKEYSIZE 1024
  16. #define PUBBLOBFILE "pubblob.h"
  17. #define PRIVBLOBFILE "privblob.h"
  18. //
  19. // LINE_VALS - maximum number of byte values printed on each line of
  20. // the "blob" files
  21. //
  22. #define LINE_VALS 8
  23. char *pszProgname; // program name - from argv[0]
  24. BOOL
  25. OpenBlobFile(
  26. FILE **file,
  27. CHAR *fname
  28. )
  29. {
  30. *file = fopen( fname, "wt" );
  31. if ( !*file ) {
  32. return(FALSE);
  33. }
  34. return(TRUE);
  35. }
  36. void
  37. DumpKeyBlob(
  38. FILE *file,
  39. DWORD dwBlobType,
  40. HCRYPTKEY hKey,
  41. HCRYPTKEY hExportKey )
  42. {
  43. int dwBlobCount;
  44. if (!CryptExportKey(
  45. hKey,
  46. hExportKey,
  47. dwBlobType,
  48. 0,
  49. NULL,
  50. &dwBlobCount)) {
  51. printf( "Error %x during CryptExportKey 1!\n", GetLastError());
  52. exit(1);
  53. } else {
  54. PBYTE pBlob;
  55. pBlob = (PBYTE) malloc( dwBlobCount );
  56. if ( !pBlob || !CryptExportKey(
  57. hKey,
  58. hExportKey,
  59. dwBlobType,
  60. 0,
  61. pBlob,
  62. &dwBlobCount)) {
  63. printf("Error %x during malloc/CryptExportKey 2!\n",
  64. GetLastError());
  65. exit(1);
  66. } else {
  67. int cnt=0;
  68. fprintf( file, "// This data is generated by %s.\n", pszProgname );
  69. fprintf( file, "// Key Blob - %d bytes\n",
  70. dwBlobCount );
  71. while ( cnt < dwBlobCount ) {
  72. int i;
  73. for ( i=0; (i < LINE_VALS) && (cnt < dwBlobCount); cnt++,i++) {
  74. fprintf( file, "0x%02x, ", *(pBlob+cnt) );
  75. }
  76. fprintf( file, "\n" );
  77. }
  78. free( pBlob );
  79. }
  80. }
  81. }
  82. /*****************************************************************************/
  83. void _cdecl main(int argc, char *argv[])
  84. {
  85. HCRYPTPROV hProv;
  86. HCRYPTKEY hSigKey;
  87. CHAR szUserName[100];
  88. DWORD dwUserNameLen = 100;
  89. FILE *blobfile;
  90. pszProgname = argv[0];
  91. // Attempt to acquire a handle to the default key container.
  92. if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) {
  93. // Some sort of error occured.
  94. // Create default key container.
  95. if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV,
  96. PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
  97. printf("Error creating key container!\n");
  98. exit(1);
  99. }
  100. // Get name of default key container.
  101. if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName,
  102. &dwUserNameLen, 0)) {
  103. // Error getting key container name.
  104. szUserName[0] = 0;
  105. }
  106. printf("Create key container '%s'\n",szUserName);
  107. }
  108. // Attempt to get handle to signature key.
  109. // Commented out the following 2 lines. We always gerenate a new signature key. TSE4.0
  110. // uses the existing key which seems to generate the same key all the time.
  111. // if( !CryptGetUserKey(hProv, AT_SIGNATURE, &hSigKey)) {
  112. // if( GetLastError() == NTE_NO_KEY) {
  113. //
  114. // Create signature key pair.
  115. //
  116. printf("Creating signature key pair...");
  117. if (!CryptGenKey( hProv,
  118. AT_SIGNATURE,
  119. (SIGKEYSIZE << 16 ) | CRYPT_EXPORTABLE,
  120. &hSigKey)) {
  121. printf("Error %x during CryptGenKey!\n", GetLastError());
  122. exit(1);
  123. } else {
  124. // Get Public Key BLOB
  125. if ( !OpenBlobFile( &blobfile, PUBBLOBFILE ) ) {
  126. printf( "Error %x during OpenBlobFile!\n", GetLastError() );
  127. exit(1);
  128. }
  129. fprintf( blobfile, "unsigned char PublicKeySigBlob[] = {\n" );
  130. DumpKeyBlob( blobfile, PUBLICKEYBLOB, hSigKey, 0 );
  131. fprintf( blobfile, "};\n" );
  132. }
  133. #if 0 // Commented out the following code. We always gerenate a new signature key. TSE4.0
  134. // uses the existing key which seems to generate the same key all the time.
  135. } else {
  136. printf("Error %x during CryptGetUserKey!\n", GetLastError());
  137. exit(1);
  138. }
  139. } else {
  140. // Get Public Key BLOB
  141. printf( "Using existing keys..." );
  142. if ( !OpenBlobFile( &blobfile, PUBBLOBFILE ) ) {
  143. printf( "Error %x during OpenBlobFile!\n", GetLastError() );
  144. exit(1);
  145. }
  146. fprintf( blobfile, "unsigned char PublicKeySigBlob[] = {\n" );
  147. DumpKeyBlob( blobfile, PUBLICKEYBLOB, hSigKey, 0 );
  148. fprintf( blobfile, "};\n" );
  149. }
  150. #endif
  151. // Get Private Key BLOB
  152. if ( !OpenBlobFile( &blobfile, PRIVBLOBFILE ) ) {
  153. printf( "Error %x during OpenBlobFile - %s!\n",
  154. GetLastError(),
  155. PRIVBLOBFILE );
  156. exit(1);
  157. }
  158. fprintf( blobfile, "unsigned char PrivateKeySigBlob[] = {\n" );
  159. DumpKeyBlob( blobfile, PRIVATEKEYBLOB, hSigKey, 0 );
  160. fprintf( blobfile, "};\n" );
  161. CryptDestroyKey(hSigKey);
  162. CryptReleaseContext(hProv,0);
  163. printf( " successful.\n" );
  164. exit(0);
  165. }