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.

294 lines
9.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: fdecrypt.cpp
  8. //
  9. // Contents: File Decryption tool. Decrypts a file looking in the MY
  10. // system certificate store for private keys.
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <windows.h>
  14. #include <assert.h>
  15. #include "wincrypt.h"
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <memory.h>
  20. //+-------------------------------------------------------------------------
  21. // Helper function to allocated the output buffer
  22. // and call CryptDecryptMessage.
  23. //--------------------------------------------------------------------------
  24. BOOL
  25. WINAPI
  26. MCryptDecryptMessage(
  27. IN PCRYPT_DECRYPT_MESSAGE_PARA pDecryptPara,
  28. IN const BYTE *pbEncryptedBlob,
  29. IN DWORD cbEncryptedBlob,
  30. OUT OPTIONAL BYTE ** ppbDecrypted,
  31. IN OUT OPTIONAL DWORD *pcbDecrypted,
  32. OUT OPTIONAL PCCERT_CONTEXT *ppXchgCert
  33. )
  34. {
  35. assert(ppbDecrypted != NULL);
  36. *ppbDecrypted = NULL;
  37. assert(pcbDecrypted != NULL);
  38. *pcbDecrypted = 0;
  39. // get the size
  40. if(!CryptDecryptMessage(
  41. pDecryptPara,
  42. pbEncryptedBlob,
  43. cbEncryptedBlob,
  44. NULL,
  45. pcbDecrypted,
  46. NULL
  47. ))
  48. return(FALSE);
  49. // allocate the buffer
  50. if( (*ppbDecrypted = (BYTE *) malloc(*pcbDecrypted)) == NULL )
  51. {
  52. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  53. return(FALSE);
  54. }
  55. // Decrypt the data
  56. if(!CryptDecryptMessage(
  57. pDecryptPara,
  58. pbEncryptedBlob,
  59. cbEncryptedBlob,
  60. *ppbDecrypted,
  61. pcbDecrypted,
  62. ppXchgCert))
  63. {
  64. free(*ppbDecrypted);
  65. *ppbDecrypted = NULL;
  66. *pcbDecrypted = 0;
  67. return(FALSE);
  68. }
  69. return(TRUE);
  70. }
  71. //+-------------------------------------------------------------------------
  72. // Display FDecrypt usage.
  73. //--------------------------------------------------------------------------
  74. void
  75. Usage(void)
  76. {
  77. printf("Usage: FDecrypt [options] <EncryptedFileName> <ClearTextFileName> \n");
  78. printf("Options are:\n");
  79. printf(" -FIX - Fix by loading sp3crmsg.dll\n");
  80. exit(1);
  81. }
  82. //+-------------------------------------------------------------------------
  83. // Generalized error routine
  84. //--------------------------------------------------------------------------
  85. #define PRINTERROR(psz, err) _PrintError((psz), (err), __LINE__)
  86. void
  87. _PrintError(char *pszMsg, DWORD err, DWORD line)
  88. {
  89. printf("%s failed on line %u: %u(%x)\n", pszMsg, line, err, err);
  90. }
  91. //+-------------------------------------------------------------------------
  92. // Main program. Open a file to decyrpt,
  93. // decrypts it and then writes the clear text
  94. // file out.
  95. //--------------------------------------------------------------------------
  96. int __cdecl
  97. main(int argc, char * argv[])
  98. {
  99. DWORD dwExitValue = 0;
  100. DWORD i, j;
  101. HCERTSTORE hMyStore = NULL;
  102. HANDLE hFileOut = INVALID_HANDLE_VALUE;
  103. HANDLE hFile = INVALID_HANDLE_VALUE;
  104. DWORD cbFile = 0;
  105. HANDLE hMap = NULL;
  106. PBYTE pbFile = NULL;
  107. PBYTE pbDecryptedBlob = NULL;
  108. DWORD cbDecryptedBlob = 0;
  109. CRYPT_DECRYPT_MESSAGE_PARA decryptInfo;
  110. DWORD cb = 0;
  111. HMODULE hDll = NULL;
  112. BOOL fFix = FALSE;
  113. // Advance past fdencrypt.exe and check for leading options
  114. while (--argc > 0) {
  115. if (**++argv != '-')
  116. break;
  117. if (0 == _stricmp(argv[0], "-FIX"))
  118. fFix = TRUE;
  119. else {
  120. printf("Bad option: %s\n", argv[0]);
  121. Usage();
  122. }
  123. }
  124. // must have the parameters
  125. if(argc != 2)
  126. Usage();
  127. if (fFix) {
  128. if (NULL == (hDll = LoadLibraryA("sp3crmsg.dll")))
  129. {
  130. PRINTERROR("LoadLibraryA(sp3crmsg.dll)", GetLastError());
  131. goto ErrCleanUp;
  132. }
  133. }
  134. // Open the MY store
  135. if( (hMyStore = CertOpenSystemStore(NULL, "My")) == NULL )
  136. {
  137. PRINTERROR("CertOpenSystemStore", GetLastError());
  138. goto ErrCleanUp;
  139. }
  140. // Read in the file.
  141. if(
  142. // open the file to decrypt
  143. (hFile = CreateFileA(
  144. argv[0], // pointer to name of the file
  145. GENERIC_READ, // access (read-write) mode
  146. FILE_SHARE_READ, // share mode
  147. NULL, // pointer to security descriptor
  148. OPEN_EXISTING, // how to create
  149. FILE_ATTRIBUTE_NORMAL, // file attributes
  150. NULL // handle to file with attributes to copy
  151. )) == INVALID_HANDLE_VALUE ||
  152. // create a file mapping object
  153. (hMap = CreateFileMapping(
  154. hFile, // handle to file to map
  155. NULL, // optional security attributes
  156. PAGE_READONLY, // protection for mapping object
  157. 0, // high-order 32 bits of object size
  158. 0, // low-order 32 bits of object size
  159. NULL // name of file-mapping object
  160. )) == NULL ||
  161. // Map the file into the address space
  162. (pbFile = (PBYTE) MapViewOfFileEx(
  163. hMap, // file-mapping object to map into address space
  164. FILE_MAP_READ, // access mode
  165. 0, // high-order 32 bits of file offset
  166. 0, // low-order 32 bits of file offset
  167. 0, // number of bytes to map
  168. NULL // suggested starting address for mapped view
  169. )) == NULL
  170. )
  171. {
  172. PRINTERROR("File Open", GetLastError());
  173. goto ErrCleanUp;
  174. }
  175. // get the size of the file
  176. if( (cbFile = GetFileSize(
  177. hFile, // handle of file to get size of
  178. NULL // address of high-order word for file size
  179. )) == 0
  180. )
  181. {
  182. printf("File %s has a 0 length.\n", argv[0]);
  183. goto ErrCleanUp;
  184. }
  185. // at this point we have a file mapping, go ahead and decrypt the file
  186. // Initialize the decryption structure.
  187. // Since the MY store is the store with
  188. // the private keys, only check the MY store
  189. memset(&decryptInfo, 0, sizeof(CRYPT_DECRYPT_MESSAGE_PARA));
  190. decryptInfo.cbSize = sizeof(CRYPT_DECRYPT_MESSAGE_PARA);
  191. decryptInfo.dwMsgAndCertEncodingType =
  192. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
  193. decryptInfo.cCertStore = 1;
  194. decryptInfo.rghCertStore = &hMyStore;
  195. // decrypt the data
  196. if(!MCryptDecryptMessage(
  197. &decryptInfo,
  198. pbFile,
  199. cbFile,
  200. &pbDecryptedBlob,
  201. &cbDecryptedBlob,
  202. NULL
  203. )
  204. )
  205. {
  206. PRINTERROR("MCryptEncryptMessage", GetLastError());
  207. goto ErrCleanUp;
  208. }
  209. // write out the clear text file
  210. if(
  211. // open the output file
  212. (hFileOut = CreateFileA(
  213. argv[1], // pointer to name of the file
  214. GENERIC_WRITE, // access (read-write) mode
  215. FILE_SHARE_READ, // share mode
  216. NULL, // pointer to security descriptor
  217. CREATE_ALWAYS, // how to create
  218. FILE_ATTRIBUTE_NORMAL, // file attributes
  219. NULL // handle to file with attributes to copy
  220. )) == INVALID_HANDLE_VALUE ||
  221. //write to the decrypted data to the file
  222. !WriteFile(
  223. hFileOut, // handle to file to write to
  224. pbDecryptedBlob, // pointer to data to write to file
  225. cbDecryptedBlob, // number of bytes to write
  226. &cb, // pointer to number of bytes written
  227. NULL // pointer to structure needed for overlapped I/O
  228. )
  229. )
  230. {
  231. PRINTERROR("File Write", GetLastError());
  232. goto ErrCleanUp;
  233. }
  234. CleanUp:
  235. if(hDll)
  236. FreeLibrary(hDll);
  237. if(hMap != NULL)
  238. CloseHandle(hMap);
  239. if(hFile != INVALID_HANDLE_VALUE && hFile != NULL)
  240. CloseHandle(hFile);
  241. if(hFileOut != INVALID_HANDLE_VALUE && hFile != NULL)
  242. CloseHandle(hFileOut);
  243. if(hMyStore != NULL)
  244. CertCloseStore(hMyStore, 0);
  245. if(pbDecryptedBlob != NULL)
  246. free(pbDecryptedBlob);
  247. return(dwExitValue);
  248. ErrCleanUp:
  249. dwExitValue = 1;
  250. goto CleanUp;
  251. }