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.

247 lines
7.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1996
  6. //
  7. // File: trevfunc.cpp
  8. //
  9. // Contents: CertVerifyRevocation Function Tests
  10. //
  11. // See Usage() for a list of test options.
  12. //
  13. //
  14. // Functions: main
  15. //
  16. // History: 21-Dec-96 philh created
  17. //--------------------------------------------------------------------------
  18. #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS 1
  19. #include <windows.h>
  20. #include <assert.h>
  21. #include "wincrypt.h"
  22. #include "certtest.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <memory.h>
  27. #include <time.h>
  28. static void Usage(void)
  29. {
  30. printf("Usage: trevfunc [options] <FileName1> <FileName2> ...\n");
  31. printf("Options are:\n");
  32. printf(" -h - This message\n");
  33. printf(" -r<RevocationType Number> - For example, -r1 (Certificate)\n");
  34. printf(" -e<Expected Error> - For example, -e0x0\n");
  35. printf(" -i<Expected Index> - For example, -i0\n");
  36. printf(" -s<SystemStore> - Additional System Store\n");
  37. printf(" -S<FileSystemStore> - Additional File System Store\n");
  38. printf(" -L<Loop Count> - Defaults to 1 iteration\n");
  39. printf(" -t<Number> - Url timeout (milliseconds)\n");
  40. printf(" -T<Number> - Accumulative Url timeout (milliseconds)\n");
  41. printf(" -f<Number> - Freshness (seconds)\n");
  42. printf("\n");
  43. printf("Defaults:\n");
  44. printf(" -r%d (Certificate)\n", CERT_CONTEXT_REVOCATION_TYPE);
  45. printf(" -e0 (All files successfully verified)\n");
  46. printf("\n");
  47. }
  48. int _cdecl main(int argc, char * argv[])
  49. {
  50. int status;
  51. BOOL fResult;
  52. DWORD dwError = 0;
  53. DWORD dwIndex = 0;
  54. DWORD dwRevType = CERT_CONTEXT_REVOCATION_TYPE;
  55. #define MAX_CONTEXT_COUNT 16
  56. DWORD cFileName = 0;
  57. LPSTR rgpszFileName[MAX_CONTEXT_COUNT];
  58. DWORD cContext = 0;
  59. PVOID rgpvContext[MAX_CONTEXT_COUNT];
  60. CERT_REVOCATION_STATUS RevStatus;
  61. CERT_REVOCATION_PARA RevPara;
  62. PCERT_REVOCATION_PARA pRevPara = NULL;
  63. HCERTSTORE hAdditionalStore = NULL;
  64. DWORD i;
  65. DWORD dwLoopCnt = 1;
  66. DWORD dwUrlRetrievalTimeout = 0;
  67. BOOL fCheckFreshnessTime = FALSE;
  68. DWORD dwFreshnessTime;
  69. DWORD dwFlags = 0;
  70. while (--argc>0) {
  71. if (**++argv == '-')
  72. {
  73. switch(argv[0][1])
  74. {
  75. case 'r':
  76. dwRevType = strtoul(argv[0]+2, NULL, 0);
  77. break;
  78. case 'e':
  79. dwError = strtoul(argv[0]+2, NULL, 0);
  80. break;
  81. case 'i':
  82. dwIndex = strtoul(argv[0]+2, NULL, 0);
  83. break;
  84. case 's':
  85. case 'S':
  86. if (NULL == (hAdditionalStore =
  87. OpenSystemStoreOrFile(
  88. argv[0][1] == 's', // fSystemStore
  89. argv[0]+2,
  90. 0 // dwFlags
  91. )))
  92. goto BadUsage;
  93. break;
  94. case 'L':
  95. dwLoopCnt = strtoul(argv[0]+2, NULL, 0);
  96. break;
  97. case 'T':
  98. dwFlags |= CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG;
  99. case 't':
  100. dwUrlRetrievalTimeout = (DWORD) strtoul(argv[0]+2, NULL, 0);
  101. break;
  102. case 'f':
  103. fCheckFreshnessTime = TRUE;
  104. dwFreshnessTime = (DWORD) strtoul(argv[0]+2, NULL, 0);
  105. break;
  106. case 'h':
  107. default:
  108. goto BadUsage;
  109. }
  110. } else {
  111. if (cFileName >= MAX_CONTEXT_COUNT) {
  112. printf("Exceeded maximum FileName count of %d\n",
  113. MAX_CONTEXT_COUNT);
  114. goto BadUsage;
  115. }
  116. rgpszFileName[cFileName++] = argv[0];
  117. }
  118. }
  119. if (cFileName == 0) {
  120. printf("Missing FileNames\n");
  121. goto BadUsage;
  122. }
  123. if (dwRevType != CERT_CONTEXT_REVOCATION_TYPE) {
  124. printf("Currently only support revocation type (-r%d) (certificates)\n",
  125. CERT_CONTEXT_REVOCATION_TYPE);
  126. goto BadUsage;
  127. }
  128. printf("command line: %s\n", GetCommandLine());
  129. for (i = 0; i < cFileName; i++) {
  130. BYTE *pbDER;
  131. DWORD cbDER;
  132. PCCERT_CONTEXT pCert;
  133. if (!ReadDERFromFile(
  134. rgpszFileName[i],
  135. &pbDER,
  136. &cbDER)) goto ErrorReturn;
  137. pCert = CertCreateCertificateContext(dwCertEncodingType, pbDER, cbDER);
  138. TestFree(pbDER);
  139. if (pCert == NULL) {
  140. printf("Unable to create certificate context for: %s\n",
  141. rgpszFileName[i]);
  142. PrintLastError("CertCreateCertificateContext");
  143. goto ErrorReturn;
  144. }
  145. rgpvContext[cContext++] = (void *) pCert;
  146. }
  147. memset(&RevPara, 0, sizeof(RevPara));
  148. RevPara.cbSize = sizeof(RevPara);
  149. if ( hAdditionalStore != NULL )
  150. {
  151. RevPara.cCertStore = 1;
  152. RevPara.rgCertStore = &hAdditionalStore;
  153. RevPara.hCrlStore = hAdditionalStore;
  154. pRevPara = &RevPara;
  155. }
  156. if (dwUrlRetrievalTimeout || fCheckFreshnessTime) {
  157. RevPara.dwUrlRetrievalTimeout = dwUrlRetrievalTimeout;
  158. RevPara.fCheckFreshnessTime = fCheckFreshnessTime;
  159. RevPara.dwFreshnessTime = dwFreshnessTime;
  160. pRevPara = &RevPara;
  161. }
  162. while (dwLoopCnt--)
  163. {
  164. memset(&RevStatus, 0, sizeof(RevStatus));
  165. RevStatus.cbSize = sizeof(RevStatus);
  166. fResult = CertVerifyRevocation(
  167. dwCertEncodingType | dwMsgEncodingType,
  168. dwRevType,
  169. cContext,
  170. rgpvContext,
  171. dwFlags,
  172. pRevPara, // pvReserved
  173. &RevStatus);
  174. if (fResult) {
  175. if (0 == dwError) {
  176. printf("CertVerifyRevocation:: verified all files\n");
  177. if (RevStatus.fHasFreshnessTime)
  178. printf("FreshnessTime: %d\n", RevStatus.dwFreshnessTime);
  179. } else {
  180. printf("Failed, CertVerifyRevocation returned Success, not the expected dwError: 0x%x (%d)\n",
  181. dwError, dwError);
  182. if (dwLoopCnt == 0)
  183. goto ErrorReturn;
  184. }
  185. } else {
  186. printf("CertVerifyRevocation:: dwIndex: %d dwError: 0x%x (%d), dwReason: %d\n",
  187. RevStatus.dwIndex, RevStatus.dwError, RevStatus.dwError,
  188. RevStatus.dwReason);
  189. if (RevStatus.fHasFreshnessTime)
  190. printf("FreshnessTime: %d\n", RevStatus.dwFreshnessTime);
  191. if (dwError != RevStatus.dwError || dwIndex != RevStatus.dwIndex) {
  192. printf("Failed, CertVerifyRevocation didn't return the expected dwIndex: %d or dwError: 0x%x (%d)\n",
  193. dwIndex, dwError, dwError);
  194. if (dwLoopCnt == 0)
  195. goto ErrorReturn;
  196. }
  197. }
  198. }
  199. printf("Passed\n");
  200. status = 0;
  201. CommonReturn:
  202. while (cContext--)
  203. CertFreeCertificateContext((PCCERT_CONTEXT) rgpvContext[cContext]);
  204. if (hAdditionalStore)
  205. CertCloseStore(hAdditionalStore, 0);
  206. return status;
  207. BadUsage:
  208. Usage();
  209. status = 0;
  210. goto CommonReturn;
  211. ErrorReturn:
  212. printf("Failed\n");
  213. status = -1;
  214. goto CommonReturn;
  215. }