Source code of Windows XP (NT5)
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.

253 lines
5.4 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1999 Microsoft Corporation
  4. //
  5. // File:
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include <windows.h>
  13. #include <wincrypt.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "licekpak.h"
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // The Manufacturing run ID used by this product.
  19. //
  20. GUID g_MfrId = { 0xdedaa678, 0xb83c, 0x11d1, { 0x9c, 0xb3, 0x00, 0xc0, 0x4f, 0xb1, 0x6e, 0x75 } };
  21. void
  22. EncodeDisk(
  23. DWORD dwSerialNum,
  24. char * szKeyContainer,
  25. char * szCertFile,
  26. char * szFileName );
  27. void
  28. DecodeDisk(
  29. char * szFileName );
  30. DWORD
  31. GetCertificate(
  32. char * szCertFile,
  33. PBYTE * ppCert,
  34. PDWORD pcbCert );
  35. ///////////////////////////////////////////////////////////////////////////////
  36. void _cdecl main(int argc, char *argv[])
  37. {
  38. DWORD dwSerialNum;
  39. char szFileName[255];
  40. char szKeyContainer[255];
  41. char szCertFile[255];
  42. if( 0 == _stricmp( "encode", argv[1] ) )
  43. {
  44. if( argc < 5 )
  45. {
  46. goto Usage;
  47. }
  48. //
  49. // encode a disk, get the serial number and file name
  50. //
  51. sscanf( argv[2], "%d", &dwSerialNum );
  52. sscanf( argv[3], "%s", szKeyContainer );
  53. sscanf( argv[4], "%s", szCertFile );
  54. sscanf( argv[5], "%s", szFileName );
  55. EncodeDisk( dwSerialNum, szKeyContainer, szCertFile, szFileName );
  56. }
  57. else if ( 0 == _stricmp( "decode", argv[1] ) )
  58. {
  59. //
  60. // decode the disk
  61. //
  62. if( argc < 3 )
  63. {
  64. goto Usage;
  65. }
  66. sscanf( argv[2], "%s", szFileName );
  67. DecodeDisk( szFileName );
  68. }
  69. return;
  70. Usage:
  71. printf( "Usage: \n" );
  72. printf( "encode serial_number key_container certificate_file filename\n" );
  73. printf( "decode filename\n" );
  74. return;
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. void
  78. EncodeDisk(
  79. DWORD dwSerialNum,
  80. char * szKeyContainer,
  81. char * szCertFile,
  82. char * szFileName )
  83. {
  84. License_KeyPack_Mfr_Info MfrInfo;
  85. DWORD dwRetCode;
  86. TCHAR tszFileName[255];
  87. TCHAR tszKeyContainer[255];
  88. mbstowcs( tszFileName, szFileName, strlen( szFileName ) + 1 );
  89. mbstowcs( tszKeyContainer, szKeyContainer, strlen( szKeyContainer ) + 1 );
  90. //
  91. // read the certificate file
  92. //
  93. MfrInfo.pbMfrCertificate = NULL;
  94. MfrInfo.cbMfrCertificate = 0;
  95. dwRetCode = GetCertificate( szCertFile, &MfrInfo.pbMfrCertificate,
  96. &MfrInfo.cbMfrCertificate );
  97. if( ERROR_SUCCESS != dwRetCode )
  98. {
  99. printf( "cannot get certificate: 0x%x\n", dwRetCode );
  100. goto done;
  101. }
  102. //
  103. // take the manufacturer certificate file name as input and generate
  104. // a disk file containing the manufacturer info.
  105. //
  106. MfrInfo.dwVersion = KEYPACK_MFR_INFO_VERSION_1_0;
  107. MfrInfo.ManufacturerId = g_MfrId;
  108. MfrInfo.dwSequenceNumber = dwSerialNum;
  109. MfrInfo.pbMfrSignature = NULL;
  110. MfrInfo.cbMfrSignature = 0;
  111. dwRetCode = EncodeKeyPackManufacturerInfo( &MfrInfo, tszKeyContainer, tszFileName );
  112. if( ERROR_SUCCESS != dwRetCode )
  113. {
  114. printf( "Cannot encode manufacturer info: 0x%x\n", dwRetCode );
  115. }
  116. done:
  117. if( MfrInfo.pbMfrCertificate )
  118. {
  119. LocalFree( MfrInfo.pbMfrCertificate );
  120. }
  121. return;
  122. }
  123. ///////////////////////////////////////////////////////////////////////////////
  124. void
  125. DecodeDisk(
  126. char * szFileName )
  127. {
  128. TCHAR tszFileName[255];
  129. License_KeyPack_Mfr_Info MfrInfo;
  130. DWORD dwRetCode;
  131. mbstowcs( tszFileName, szFileName, strlen( szFileName ) + 1 );
  132. dwRetCode = DecodeKeyPackManufacturerInfo( &MfrInfo, tszFileName, 0, NULL );
  133. if( ERROR_SUCCESS != dwRetCode )
  134. {
  135. printf( "cannot decode manufacturer info\n" );
  136. }
  137. else
  138. {
  139. printf( "Manufacturer Version: 0x%x\n", MfrInfo.dwVersion );
  140. printf( "Manufacturer Serial Number: %d\n", MfrInfo.dwSequenceNumber );
  141. }
  142. return;
  143. }
  144. ///////////////////////////////////////////////////////////////////////////////
  145. DWORD
  146. GetCertificate(
  147. char * szCertFile,
  148. PBYTE * ppCert,
  149. PDWORD pcbCert )
  150. {
  151. HANDLE hFile = INVALID_HANDLE_VALUE;
  152. DWORD dwRetCode = ERROR_SUCCESS, cbRead = 0;
  153. TCHAR tszCertFile[255];
  154. mbstowcs( tszCertFile, szCertFile, strlen( szCertFile ) + 1 );
  155. hFile = CreateFile( tszCertFile, GENERIC_READ, 0, NULL, OPEN_EXISTING,
  156. FILE_ATTRIBUTE_NORMAL, NULL );
  157. if( INVALID_HANDLE_VALUE == hFile )
  158. {
  159. dwRetCode = GetLastError();
  160. goto done;
  161. }
  162. //
  163. // find out the size of the file
  164. //
  165. *pcbCert = GetFileSize( hFile, NULL );
  166. if( 0xFFFFFFFF == ( *pcbCert ) )
  167. {
  168. dwRetCode = GetLastError();
  169. goto done;
  170. }
  171. //
  172. // allocate memory for reading the file content
  173. //
  174. *ppCert = LocalAlloc( GPTR, *pcbCert );
  175. if( NULL == ( *ppCert ) )
  176. {
  177. dwRetCode = GetLastError();
  178. goto done;
  179. }
  180. //
  181. // read the manufacturer info
  182. //
  183. if( !ReadFile( hFile, *ppCert, *pcbCert, &cbRead, NULL ) )
  184. {
  185. dwRetCode = GetLastError();
  186. }
  187. done:
  188. if( INVALID_HANDLE_VALUE != hFile )
  189. {
  190. CloseHandle( hFile );
  191. }
  192. return( dwRetCode );
  193. }