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.

269 lines
6.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: cspsigck.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // cspsigck.cpp : Defines the entry point for the console application.
  11. //
  12. // #ifndef WIN32_LEAN_AND_MEAN
  13. // #define WIN32_LEAN_AND_MEAN
  14. // #endif
  15. // #include <windows.h> // All the Windows definitions.
  16. #include "afx.h"
  17. #include <iostream.h>
  18. #ifndef WINVER
  19. #define WINVER 0x0500
  20. #endif
  21. #include <wincrypt.h>
  22. static LPCTSTR
  23. ErrorString(
  24. DWORD dwErrorCode);
  25. static void
  26. FreeErrorString(
  27. LPCTSTR szErrorString);
  28. DWORD __cdecl
  29. main(
  30. int argc,
  31. char* argv[])
  32. {
  33. static TCHAR szDots[] =
  34. TEXT("........................................................................");
  35. DWORD dwReturn = 0;
  36. HCRYPTPROV hProv = NULL;
  37. DWORD dwIndex, dwLength, dwSts, dwProvType;
  38. BOOL fSts;
  39. CString szProvider;
  40. cout << TEXT("==============================================================================\n")
  41. << TEXT(" Cryptographic Service Provider Signature validation\n")
  42. << TEXT("------------------------------------------------------------------------------\n")
  43. << flush;
  44. dwIndex = 0;
  45. for (;;)
  46. {
  47. dwLength = 0;
  48. fSts = CryptEnumProviders(
  49. dwIndex,
  50. NULL,
  51. 0,
  52. &dwProvType,
  53. NULL,
  54. &dwLength);
  55. if (fSts)
  56. {
  57. fSts = CryptEnumProviders(
  58. dwIndex,
  59. NULL,
  60. 0,
  61. &dwProvType,
  62. szProvider.GetBuffer(dwLength / sizeof(TCHAR)),
  63. &dwLength);
  64. dwSts = GetLastError();
  65. szProvider.ReleaseBuffer();
  66. if (!fSts)
  67. {
  68. cerr << TEXT("\n ERROR Can't obtain provider name: ")
  69. << ErrorString(dwSts)
  70. << endl;
  71. goto ErrorExit;
  72. }
  73. }
  74. else
  75. {
  76. dwSts = GetLastError();
  77. if (ERROR_NO_MORE_ITEMS == dwSts)
  78. break;
  79. cerr << TEXT("\n ERROR Can't obtain provider name length: ")
  80. << ErrorString(dwSts)
  81. << endl;
  82. goto ErrorExit;
  83. }
  84. cout << szProvider << &szDots[szProvider.GetLength()] << flush;
  85. fSts = CryptAcquireContext(
  86. &hProv,
  87. NULL,
  88. szProvider,
  89. dwProvType,
  90. CRYPT_VERIFYCONTEXT);
  91. if (fSts)
  92. {
  93. cout << TEXT("passed") << endl;
  94. fSts = CryptReleaseContext(hProv, 0);
  95. hProv = NULL;
  96. if (!fSts)
  97. {
  98. dwSts = GetLastError();
  99. cerr << TEXT("\n ERROR Can't release context: ")
  100. << ErrorString(dwSts)
  101. << endl;
  102. goto ErrorExit;
  103. }
  104. }
  105. else
  106. {
  107. dwSts = GetLastError();
  108. dwReturn = dwSts;
  109. cout << TEXT("FAILED\n")
  110. << TEXT(" ") << ErrorString(dwSts)
  111. << endl;
  112. ASSERT(NULL == hProv);
  113. }
  114. dwIndex += 1;
  115. }
  116. cout << TEXT("------------------------------------------------------------------------------\n")
  117. << TEXT("Final Status") << &szDots[12]
  118. << (LPCTSTR)((ERROR_SUCCESS == dwReturn) ? TEXT("passed\n") : TEXT("FAILED\n"))
  119. << TEXT("==============================================================================\n")
  120. << flush;
  121. dwReturn = 0;
  122. ErrorExit:
  123. if (hProv != NULL)
  124. CryptReleaseContext(hProv, 0);
  125. return dwReturn;
  126. }
  127. /*++
  128. ErrorString:
  129. This routine does it's very best to translate a given error code into a
  130. text message. Any trailing non-printable characters are striped from the
  131. end of the text message, such as carriage returns and line feeds.
  132. Arguments:
  133. dwErrorCode supplies the error code to be translated.
  134. Return Value:
  135. The address of a freshly allocated text string. Use FreeErrorString to
  136. dispose of it.
  137. Throws:
  138. Errors are thrown as DWORD status codes.
  139. Remarks:
  140. Author:
  141. Doug Barlow (dbarlow) 8/27/1998
  142. --*/
  143. static LPCTSTR
  144. ErrorString(
  145. DWORD dwErrorCode)
  146. {
  147. LPTSTR szErrorString = NULL;
  148. try
  149. {
  150. DWORD dwLen;
  151. LPTSTR szLast;
  152. dwLen = FormatMessage(
  153. FORMAT_MESSAGE_ALLOCATE_BUFFER
  154. | FORMAT_MESSAGE_FROM_SYSTEM,
  155. NULL,
  156. dwErrorCode,
  157. LANG_NEUTRAL,
  158. (LPTSTR)&szErrorString,
  159. 0,
  160. NULL);
  161. if (0 == dwLen)
  162. {
  163. ASSERT(NULL == szErrorString);
  164. dwLen = FormatMessage(
  165. FORMAT_MESSAGE_ALLOCATE_BUFFER
  166. | FORMAT_MESSAGE_FROM_HMODULE,
  167. GetModuleHandle(NULL),
  168. dwErrorCode,
  169. LANG_NEUTRAL,
  170. (LPTSTR)&szErrorString,
  171. 0,
  172. NULL);
  173. if (0 == dwLen)
  174. {
  175. ASSERT(NULL == szErrorString);
  176. szErrorString = (LPTSTR)LocalAlloc(
  177. LMEM_FIXED,
  178. 32 * sizeof(TCHAR));
  179. if (NULL == szErrorString)
  180. throw (DWORD)SCARD_E_NO_MEMORY;
  181. _stprintf(szErrorString, TEXT("0x%08x"), dwErrorCode);
  182. }
  183. }
  184. ASSERT(NULL != szErrorString);
  185. for (szLast = szErrorString + lstrlen(szErrorString) - 1;
  186. szLast > szErrorString;
  187. szLast -= 1)
  188. {
  189. if (_istgraph(*szLast))
  190. break;
  191. *szLast = 0;
  192. }
  193. }
  194. catch (...)
  195. {
  196. FreeErrorString(szErrorString);
  197. throw;
  198. }
  199. return szErrorString;
  200. }
  201. /*++
  202. FreeErrorString:
  203. This routine frees the Error String allocated by the ErrorString service.
  204. Arguments:
  205. szErrorString supplies the error string to be deallocated.
  206. Return Value:
  207. None
  208. Throws:
  209. None
  210. Remarks:
  211. Author:
  212. Doug Barlow (dbarlow) 8/27/1998
  213. --*/
  214. static void
  215. FreeErrorString(
  216. LPCTSTR szErrorString)
  217. {
  218. if (NULL != szErrorString)
  219. LocalFree((LPVOID)szErrorString);
  220. }