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.

199 lines
4.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: makecat.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // Functions: wmain
  12. //
  13. // History: 05-May-1997 pberkman created
  14. //
  15. //--------------------------------------------------------------------------
  16. #include <stdio.h>
  17. #include <windows.h>
  18. #include <io.h>
  19. #include <wchar.h>
  20. #include <malloc.h>
  21. #include <memory.h>
  22. #include "unicode.h"
  23. #include "wincrypt.h"
  24. #include "wintrust.h"
  25. #include "mssip.h"
  26. #include "mscat.h"
  27. #include "dbgdef.h"
  28. #include "gendefs.h"
  29. #include "printfu.hxx"
  30. #include "cwargv.hxx"
  31. #include "resource.h"
  32. WCHAR *pwszFile = NULL;
  33. PrintfU_ *pPrint = NULL;
  34. int iRet = 0;
  35. WCHAR gszUsage[] = L"usage: calchash filename\n -?: this screen\n" ;
  36. const char RgchHex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
  37. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  38. //////////////////////////////////////////////////////////////////////////////////////
  39. //
  40. //////////////////////////////////////////////////////////////////////////////////////
  41. void FormatHashString(LPSTR *ppString, DWORD cbBlob, BYTE *pblob)
  42. {
  43. DWORD i, j = 0;
  44. BYTE *pb = NULL;
  45. DWORD numCharsInserted = 0;
  46. LPSTR psz;
  47. *ppString = NULL;
  48. pb = pblob;
  49. // fill the buffer
  50. i=0;
  51. while (j < cbBlob)
  52. {
  53. if ((*ppString) == NULL)
  54. {
  55. psz = NULL;
  56. *ppString = (LPSTR) malloc(3 * sizeof(char));
  57. }
  58. else
  59. {
  60. psz = *ppString;
  61. #pragma prefast(suppress:308, "the pointer was saved above (PREfast bug 506)")
  62. *ppString = (LPSTR) realloc(*ppString, (j+1) * 3 * sizeof(char));
  63. }
  64. if (*ppString == NULL)
  65. {
  66. if (psz != NULL)
  67. {
  68. free(psz);
  69. }
  70. return;
  71. }
  72. (*ppString)[i++] = RgchHex[(*pb & 0xf0) >> 4];
  73. (*ppString)[i++] = RgchHex[*pb & 0x0f];
  74. (*ppString)[i++] = ' ';
  75. pb++;
  76. j++;
  77. }
  78. (*ppString)[i-1] = 0;
  79. }
  80. extern "C" int __cdecl wmain(int argc, WCHAR **wargv)
  81. {
  82. int cMember;
  83. cWArgv_ *pArgs;
  84. BOOL fFailed;
  85. CRYPTCATCDF *pCDF;
  86. CRYPTCATMEMBER *pMember;
  87. LPWSTR pwszMemberTag;
  88. CRYPTCATATTRIBUTE *pAttr;
  89. BOOL fContinueOnError;
  90. BYTE pbHash[40];
  91. DWORD cbHash = sizeof(pbHash);
  92. HANDLE hFile;
  93. LPSTR psz;
  94. pCDF = NULL;
  95. if (!(pArgs = new cWArgv_((HINSTANCE)GetModuleHandle(NULL), &fFailed)))
  96. {
  97. goto MemoryError;
  98. }
  99. if (fFailed)
  100. {
  101. goto MemoryError;
  102. }
  103. pArgs->AddUsageText(IDS_USAGETEXT_USAGE, IDS_USAGETEXT_OPTIONS,
  104. IDS_USAGETEXT_OPTPARAM, IDS_USAGETEXT_FILENAME, IDS_USAGETEXT_OPTPARAM);
  105. pArgs->Add2List(IDS_PARAM_HELP, IDS_PARAMTEXT_HELP, WARGV_VALUETYPE_BOOL, (void *)FALSE);
  106. pArgs->Fill(argc, wargv);
  107. if (!(pArgs->Fill(argc, wargv)) ||
  108. (pArgs->GetValue(IDS_PARAM_HELP)))
  109. {
  110. wprintf(L"%s", gszUsage);
  111. goto NeededHelp;
  112. }
  113. if (!(pwszFile = pArgs->GetFileName()))
  114. {
  115. wprintf(L"%s",gszUsage);
  116. goto ParamError;
  117. }
  118. pPrint = new PrintfU_;
  119. SetLastError(0);
  120. if ((hFile = CreateFileU(pwszFile,
  121. GENERIC_READ,
  122. FILE_SHARE_READ,
  123. NULL,
  124. OPEN_EXISTING,
  125. FILE_ATTRIBUTE_NORMAL,
  126. NULL)) == INVALID_HANDLE_VALUE)
  127. {
  128. wprintf(L"Cannot open file - GLE = %lx\n", GetLastError());
  129. goto CATCloseError;
  130. }
  131. if (!CryptCATAdminCalcHashFromFileHandle(hFile,
  132. &cbHash,
  133. pbHash,
  134. 0))
  135. {
  136. goto CATCloseError;
  137. }
  138. FormatHashString(&psz, cbHash, pbHash);
  139. if (psz != NULL)
  140. {
  141. printf("%s\n", psz);
  142. free(psz);
  143. }
  144. else
  145. {
  146. goto MemoryError;
  147. }
  148. CommonReturn:
  149. DELETE_OBJECT(pArgs);
  150. DELETE_OBJECT(pPrint);
  151. return(iRet);
  152. ErrorReturn:
  153. iRet = 1;
  154. goto CommonReturn;
  155. TRACE_ERROR_EX(DBG_SS_APP, MemoryError);
  156. TRACE_ERROR_EX(DBG_SS_APP, ParamError);
  157. TRACE_ERROR_EX(DBG_SS_APP, NeededHelp);
  158. TRACE_ERROR_EX(DBG_SS_APP, CATCloseError);
  159. }