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.

200 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: 642bin <Base64 Encoded File> <Binary 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 cbT = 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 pb = NULL;
  55. DWORD cb = 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(!CryptStringToBinaryA(
  106. (const char *) pbFile,
  107. cbFile,
  108. CRYPT_STRING_ANY,
  109. NULL,
  110. &cb,
  111. NULL,
  112. NULL)) {
  113. err = GetLastError();
  114. PRINTERROR("CryptStringToBinaryA", err);
  115. goto ErrCleanUp;
  116. }
  117. if( (pb = (PBYTE) malloc(cb)) == NULL ) {
  118. PRINTERROR("malloc", ERROR_OUTOFMEMORY);
  119. goto ErrCleanUp;
  120. }
  121. if(!CryptStringToBinaryA(
  122. (const char *) pbFile,
  123. cbFile,
  124. CRYPT_STRING_ANY,
  125. pb,
  126. &cb,
  127. NULL,
  128. NULL)) {
  129. err = GetLastError();
  130. PRINTERROR("CryptStringToBinaryA", err);
  131. goto ErrCleanUp;
  132. }
  133. // write out the clear text file
  134. if(
  135. // open the output file
  136. (hFileOut = CreateFileA(
  137. argv[2], // pointer to name of the file
  138. GENERIC_WRITE, // access (read-write) mode
  139. FILE_SHARE_READ, // share mode
  140. NULL, // pointer to security descriptor
  141. CREATE_ALWAYS, // how to create
  142. FILE_ATTRIBUTE_NORMAL, //file attributes
  143. NULL // handle to file with attributes to copy
  144. )) == INVALID_HANDLE_VALUE ||
  145. //write to the decrypted data to the file
  146. !WriteFile(
  147. hFileOut, // handle to file to write to
  148. pb, // pointer to data to write to file
  149. cb, // number of bytes to write
  150. &cbT, // pointer to number of bytes written
  151. NULL // pointer to structure needed for overlapped I/O
  152. )
  153. )
  154. {
  155. PRINTERROR("File Write", GetLastError());
  156. goto ErrCleanUp;
  157. }
  158. CleanUp:
  159. if(hMap != NULL)
  160. CloseHandle(hMap);
  161. if(hFile != INVALID_HANDLE_VALUE && hFile != NULL)
  162. CloseHandle(hFile);
  163. if(hFileOut != INVALID_HANDLE_VALUE && hFile != NULL)
  164. CloseHandle(hFileOut);
  165. if(pb != NULL)
  166. free(pb);
  167. return(dwExitValue);
  168. ErrCleanUp:
  169. dwExitValue = 1;
  170. goto CleanUp;
  171. }