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.

198 lines
6.2 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 <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <memory.h>
  19. #include <wincrypt.h>
  20. //+-------------------------------------------------------------------------
  21. // Display Bin264 usage.
  22. //--------------------------------------------------------------------------
  23. void
  24. Usage(void)
  25. {
  26. printf("Usage: Bin264 <Binary File> <Base64 Encoded File> \n");
  27. exit(1);
  28. }
  29. //+-------------------------------------------------------------------------
  30. // Generalized error routine
  31. //--------------------------------------------------------------------------
  32. #define PRINTERROR(psz, err) _PrintError((psz), (err), __LINE__)
  33. void
  34. _PrintError(char *pszMsg, DWORD err, DWORD line)
  35. {
  36. printf("%s failed on line %u: (%x)\n", pszMsg, line, err);
  37. }
  38. //+-------------------------------------------------------------------------
  39. // Main program. Open a file to decyrpt,
  40. // decrypts it and then writes the clear text
  41. // file out.
  42. //--------------------------------------------------------------------------
  43. int __cdecl
  44. main(int argc, char * argv[])
  45. {
  46. DWORD dwExitValue = 0;
  47. DWORD err = ERROR_SUCCESS;
  48. DWORD cb = 0;
  49. HANDLE hFileOut = INVALID_HANDLE_VALUE;
  50. HANDLE hFile = INVALID_HANDLE_VALUE;
  51. DWORD cbFile = 0;
  52. HANDLE hMap = NULL;
  53. PBYTE pbFile = NULL;
  54. PBYTE pbBase64 = NULL;
  55. DWORD cchBase64 = 0;
  56. // must have the parameters
  57. if(argc != 3)
  58. Usage();
  59. // Read in the file.
  60. if(
  61. // open the file to decrypt
  62. (hFile = CreateFileA(
  63. argv[1], // pointer to name of the file
  64. GENERIC_READ, // access (read-write) mode
  65. FILE_SHARE_READ, // share mode
  66. NULL, // pointer to security descriptor
  67. OPEN_EXISTING, // how to create
  68. FILE_ATTRIBUTE_NORMAL, // file attributes
  69. NULL // handle to file with attributes to copy
  70. )) == INVALID_HANDLE_VALUE ||
  71. // create a file mapping object
  72. (hMap = CreateFileMapping(
  73. hFile, // handle to file to map
  74. NULL, // optional security attributes
  75. PAGE_READONLY, // protection for mapping object
  76. 0, // high-order 32 bits of object size
  77. 0, // low-order 32 bits of object size
  78. NULL // name of file-mapping object
  79. )) == NULL ||
  80. // Map the file into the address space
  81. (pbFile = (PBYTE) MapViewOfFileEx(
  82. hMap, // file-mapping object to map into address space
  83. FILE_MAP_READ, // access mode
  84. 0, // high-order 32 bits of file offset
  85. 0, // low-order 32 bits of file offset
  86. 0, // number of bytes to map
  87. NULL // suggested starting address for mapped view
  88. )) == NULL
  89. )
  90. {
  91. PRINTERROR("File Open", GetLastError());
  92. goto ErrCleanUp;
  93. }
  94. // get the size of the file
  95. if( (cbFile = GetFileSize(
  96. hFile, // handle of file to get size of
  97. NULL // address of high-order word for file size
  98. )) == 0
  99. )
  100. {
  101. printf("File %s has a 0 length.\n", argv[2]);
  102. goto ErrCleanUp;
  103. }
  104. // at this point we have a file mapping, base64 encode the file
  105. if(!CryptBinaryToStringA(
  106. pbFile,
  107. cbFile,
  108. CRYPT_STRING_BASE64,
  109. NULL,
  110. &cchBase64)) {
  111. err = GetLastError();
  112. PRINTERROR("CryptBinaryToStringA", err);
  113. goto ErrCleanUp;
  114. }
  115. if( (pbBase64 = (PBYTE) malloc(cchBase64 * sizeof(char))) == NULL ) {
  116. PRINTERROR("malloc", ERROR_OUTOFMEMORY);
  117. goto ErrCleanUp;
  118. }
  119. if(!CryptBinaryToStringA(
  120. pbFile,
  121. cbFile,
  122. CRYPT_STRING_BASE64,
  123. (char *) pbBase64,
  124. &cchBase64)) {
  125. err = GetLastError();
  126. PRINTERROR("CryptBinaryToStringA", err);
  127. goto ErrCleanUp;
  128. }
  129. // write out the clear text file
  130. if(
  131. // open the output file
  132. (hFileOut = CreateFileA(
  133. argv[2], // pointer to name of the file
  134. GENERIC_WRITE, // access (read-write) mode
  135. FILE_SHARE_READ, // share mode
  136. NULL, // pointer to security descriptor
  137. CREATE_ALWAYS, // how to create
  138. FILE_ATTRIBUTE_NORMAL, //file attributes
  139. NULL // handle to file with attributes to copy
  140. )) == INVALID_HANDLE_VALUE ||
  141. //write to the decrypted data to the file
  142. !WriteFile(
  143. hFileOut, // handle to file to write to
  144. pbBase64, // pointer to data to write to file
  145. cchBase64 * sizeof(char),// number of bytes to write
  146. &cb, // pointer to number of bytes written
  147. NULL // pointer to structure needed for overlapped I/O
  148. )
  149. )
  150. {
  151. PRINTERROR("File Write", GetLastError());
  152. goto ErrCleanUp;
  153. }
  154. CleanUp:
  155. if(hMap != NULL)
  156. CloseHandle(hMap);
  157. if(hFile != INVALID_HANDLE_VALUE && hFile != NULL)
  158. CloseHandle(hFile);
  159. if(hFileOut != INVALID_HANDLE_VALUE && hFile != NULL)
  160. CloseHandle(hFileOut);
  161. if(pbBase64 != NULL)
  162. free(pbBase64);
  163. return(dwExitValue);
  164. ErrCleanUp:
  165. dwExitValue = 1;
  166. goto CleanUp;
  167. }