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.

239 lines
7.8 KiB

  1. #include <private.h>
  2. #include "rsa.h"
  3. #include "md5.h"
  4. #include <strsafe.h>
  5. //////////////////////////////////////////////////////////////////////
  6. // //
  7. // Digital Signature Stuff //
  8. // //
  9. //////////////////////////////////////////////////////////////////////
  10. LPBSAFE_PUB_KEY PUB;
  11. LPBSAFE_PRV_KEY PRV;
  12. unsigned char pubmodulus[] =
  13. {
  14. 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00,
  16. 0x00, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00,
  19. 0x3d, 0x3a, 0x5e, 0xbd, 0x72, 0x43, 0x3e, 0xc9,
  20. 0x4d, 0xbb, 0xc1, 0x1e, 0x4a, 0xba, 0x5f, 0xcb,
  21. 0x3e, 0x88, 0x20, 0x87, 0xef, 0xf5, 0xc1, 0xe2,
  22. 0xd7, 0xb7, 0x6b, 0x9a, 0xf2, 0x52, 0x45, 0x95,
  23. 0xce, 0x63, 0x65, 0x6b, 0x58, 0x3a, 0xfe, 0xef,
  24. 0x7c, 0xe7, 0xbf, 0xfe, 0x3d, 0xf6, 0x5c, 0x7d,
  25. 0x6c, 0x5e, 0x06, 0x09, 0x1a, 0xf5, 0x61, 0xbb,
  26. 0x20, 0x93, 0x09, 0x5f, 0x05, 0x6d, 0xea, 0x87,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  28. };
  29. unsigned char prvmodulus[] =
  30. {
  31. 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00,
  34. 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00,
  36. 0x3d, 0x3a, 0x5e, 0xbd,
  37. 0x72, 0x43, 0x3e, 0xc9, 0x4d, 0xbb, 0xc1, 0x1e,
  38. 0x4a, 0xba, 0x5f, 0xcb, 0x3e, 0x88, 0x20, 0x87,
  39. 0xef, 0xf5, 0xc1, 0xe2, 0xd7, 0xb7, 0x6b, 0x9a,
  40. 0xf2, 0x52, 0x45, 0x95, 0xce, 0x63, 0x65, 0x6b,
  41. 0x58, 0x3a, 0xfe, 0xef, 0x7c, 0xe7, 0xbf, 0xfe,
  42. 0x3d, 0xf6, 0x5c, 0x7d, 0x6c, 0x5e, 0x06, 0x09,
  43. 0x1a, 0xf5, 0x61, 0xbb, 0x20, 0x93, 0x09, 0x5f,
  44. 0x05, 0x6d, 0xea, 0x87, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x3f, 0xbd, 0x29, 0x20,
  46. 0x57, 0xd2, 0x3b, 0xf1, 0x07, 0xfa, 0xdf, 0xc1,
  47. 0x16, 0x31, 0xe4, 0x95, 0xea, 0xc1, 0x2a, 0x46,
  48. 0x2b, 0xad, 0x88, 0x57, 0x55, 0xf0, 0x57, 0x58,
  49. 0xc6, 0x6f, 0x95, 0xeb, 0x00, 0x00, 0x00, 0x00,
  50. 0x83, 0xdd, 0x9d, 0xd0, 0x03, 0xb1, 0x5a, 0x9b,
  51. 0x9e, 0xb4, 0x63, 0x02, 0x43, 0x3e, 0xdf, 0xb0,
  52. 0x52, 0x83, 0x5f, 0x6a, 0x03, 0xe7, 0xd6, 0x78,
  53. 0x45, 0x83, 0x6a, 0x5b, 0xc4, 0xcb, 0xb1, 0x93,
  54. 0x00, 0x00, 0x00, 0x00, 0x65, 0x9d, 0x43, 0xe8,
  55. 0x48, 0x17, 0xcd, 0x29, 0x7e, 0xb9, 0x26, 0x5c,
  56. 0x79, 0x66, 0x58, 0x61, 0x72, 0x86, 0x6a, 0xa3,
  57. 0x63, 0xad, 0x63, 0xb8, 0xe1, 0x80, 0x4c, 0x0f,
  58. 0x36, 0x7d, 0xd9, 0xa6, 0x00, 0x00, 0x00, 0x00,
  59. 0x75, 0x3f, 0xef, 0x5a, 0x01, 0x5f, 0xf6, 0x0e,
  60. 0xd7, 0xcd, 0x59, 0x1c, 0xc6, 0xec, 0xde, 0xf3,
  61. 0x5a, 0x03, 0x09, 0xff, 0xf5, 0x23, 0xcc, 0x90,
  62. 0x27, 0x1d, 0xaa, 0x29, 0x60, 0xde, 0x05, 0x6e,
  63. 0x00, 0x00, 0x00, 0x00, 0xc0, 0x17, 0x0e, 0x57,
  64. 0xf8, 0x9e, 0xd9, 0x5c, 0xf5, 0xb9, 0x3a, 0xfc,
  65. 0x0e, 0xe2, 0x33, 0x27, 0x59, 0x1d, 0xd0, 0x97,
  66. 0x4a, 0xb1, 0xb1, 0x1f, 0xc3, 0x37, 0xd1, 0xd6,
  67. 0xe6, 0x9b, 0x35, 0xab, 0x00, 0x00, 0x00, 0x00,
  68. 0x87, 0xa7, 0x19, 0x32, 0xda, 0x11, 0x87, 0x55,
  69. 0x58, 0x00, 0x16, 0x16, 0x25, 0x65, 0x68, 0xf8,
  70. 0x24, 0x3e, 0xe6, 0xfa, 0xe9, 0x67, 0x49, 0x94,
  71. 0xcf, 0x92, 0xcc, 0x33, 0x99, 0xe8, 0x08, 0x60,
  72. 0x17, 0x9a, 0x12, 0x9f, 0x24, 0xdd, 0xb1, 0x24,
  73. 0x99, 0xc7, 0x3a, 0xb8, 0x0a, 0x7b, 0x0d, 0xdd,
  74. 0x35, 0x07, 0x79, 0x17, 0x0b, 0x51, 0x9b, 0xb3,
  75. 0xc7, 0x10, 0x01, 0x13, 0xe7, 0x3f, 0xf3, 0x5f,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00
  82. };
  83. BOOL initkey(void)
  84. {
  85. DWORD bits;
  86. PUB = (LPBSAFE_PUB_KEY)pubmodulus;
  87. PUB->magic = RSA1;
  88. PUB->keylen = 0x48;
  89. PUB->bitlen = 0x0200;
  90. PUB->datalen = 0x3f;
  91. PUB->pubexp = 0xc0887b5b;
  92. PRV = (LPBSAFE_PRV_KEY)prvmodulus;
  93. PRV->magic = RSA2;
  94. PRV->keylen = 0x48;
  95. PRV->bitlen = 0x0200;
  96. PRV->datalen = 0x3f;
  97. PRV->pubexp = 0xc0887b5b;
  98. bits = PRV->bitlen;
  99. return TRUE;
  100. }
  101. BOOL SignWithIDWKey(IN LPCSTR FileName,
  102. IN BOOL fVerbose) {
  103. HANDLE hFile;
  104. HANDLE hMapping;
  105. PUCHAR pMap;
  106. HANDLE hSigFile;
  107. DWORD Size;
  108. MD5_CTX HashState;
  109. BYTE SigHash[ 0x48 ];
  110. BYTE Signature[ 0x48 ];
  111. CHAR SigFilePath[ MAX_PATH ];
  112. PSTR pszDot;
  113. BOOL Return = FALSE;
  114. if (!initkey()) {
  115. return( FALSE );
  116. }
  117. hFile = CreateFile( FileName, GENERIC_READ,
  118. FILE_SHARE_READ, NULL,
  119. OPEN_EXISTING, 0, NULL );
  120. if (hFile != INVALID_HANDLE_VALUE) {
  121. hMapping = CreateFileMapping( hFile,
  122. NULL,
  123. PAGE_READONLY,
  124. 0, 0, NULL );
  125. if (hMapping) {
  126. pMap = MapViewOfFileEx( hMapping,
  127. FILE_MAP_READ,
  128. 0, 0, 0, NULL );
  129. if (pMap) {
  130. Size = GetFileSize( hFile, NULL );
  131. MD5Init( &HashState );
  132. MD5Update( &HashState, pMap, Size );
  133. MD5Final( &HashState );
  134. memset(SigHash, 0xff, 0x40);
  135. SigHash[0x40-1] = 0;
  136. SigHash[0x40-2] = 1;
  137. SigHash[16] = 0;
  138. memcpy(SigHash, HashState.digest, 16);
  139. //
  140. // Encrypt the signature data
  141. //
  142. BSafeDecPrivate(PRV, SigHash, Signature );;
  143. //
  144. // Create and store it in a .sig file
  145. //
  146. StringCbCopy( SigFilePath, sizeof(SigFilePath), FileName );
  147. pszDot = strrchr( SigFilePath, '.' );
  148. if (!pszDot) {
  149. pszDot = SigFilePath + strlen( SigFilePath );
  150. }
  151. StringCbCopy( pszDot,
  152. sizeof(SigFilePath) - strlen(pszDot),
  153. ".sig");
  154. hSigFile = CreateFile( SigFilePath, GENERIC_WRITE,
  155. 0, NULL,
  156. CREATE_ALWAYS, 0, NULL );
  157. if (hSigFile != INVALID_HANDLE_VALUE) {
  158. WriteFile( hSigFile,
  159. Signature,
  160. sizeof( Signature ),
  161. &Size, NULL );
  162. CloseHandle( hSigFile );
  163. Return = TRUE ;
  164. if (fVerbose)
  165. fprintf( stdout, "BINPLACE : warning BNP0000: Signature file generated in %s\n", SigFilePath);
  166. } else {
  167. fprintf( stderr, "BINPLACE : error BNP0000: Unable to create file %s, %d\n",
  168. SigFilePath, GetLastError() );
  169. }
  170. UnmapViewOfFile( pMap );
  171. } else {
  172. fprintf(stderr, "BINPLACE : error BNP0000: unable to map view, %d\n", GetLastError());
  173. }
  174. CloseHandle( hMapping );
  175. } else {
  176. fprintf(stderr, "BINPLACE : error BNP0000: CreateFileMapping of %s failed, %d\n",
  177. FileName, GetLastError() );
  178. }
  179. CloseHandle( hFile );
  180. } else {
  181. fprintf( stderr, "BINPLACE : error BNP0000: could not open %s, %d\n",
  182. FileName, GetLastError() );
  183. }
  184. return( Return );
  185. }
  186. BOOL // Keep as BOOL for the future (used by rsa code)
  187. GenRandom (ULONG huid, BYTE *pbBuffer, size_t dwLength)
  188. {
  189. return( FALSE );
  190. }