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.

394 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1997
  6. //
  7. // File: tctlfunc.cpp
  8. //
  9. // Contents: CertVerifyCTLUsage Function Tests
  10. //
  11. // See Usage() for a list of test options.
  12. //
  13. //
  14. // Functions: main
  15. //
  16. // History: 08-May-97 philh created
  17. //--------------------------------------------------------------------------
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include "wincrypt.h"
  21. #include "certtest.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <memory.h>
  26. #include <time.h>
  27. static void Usage(void)
  28. {
  29. printf("Usage: tctlfunc [options] <SubjectCertFile1> <SubjectCertFile2> ...\n");
  30. printf("Options are:\n");
  31. printf(" -h - This message\n");
  32. printf(" -U<ObjectID> - Usage Identifier\n");
  33. printf(" -L<text> - List Identifier\n");
  34. printf(" -A - Test via AnySubjectType\n");
  35. printf(" -c<filename> - CTL store file\n");
  36. printf(" -C<storename> - CTL system store\n");
  37. printf(" -s<filename> - Signer store file\n");
  38. printf(" -S<storename> - Signer system store\n");
  39. printf(" -e<Expected Error> - For example, -e0x0\n");
  40. printf(" -f<number> - Verify dwFlags\n");
  41. printf(" -b - Brief\n");
  42. printf(" -v - Verbose\n");
  43. printf("\n");
  44. }
  45. static HCERTSTORE OpenSignerStore(
  46. LPSTR pszStore,
  47. BOOL fSystemStore
  48. )
  49. {
  50. HCERTSTORE hStore;
  51. if (fSystemStore)
  52. hStore = CertOpenSystemStore(NULL, pszStore);
  53. else
  54. hStore = CertOpenStore(
  55. CERT_STORE_PROV_FILENAME_A,
  56. dwMsgAndCertEncodingType,
  57. 0, // hCryptProv
  58. 0, // dwFlags
  59. (const void *) pszStore
  60. );
  61. return hStore;
  62. }
  63. // Attempt to read as a file containing an encoded CTL.
  64. static HCERTSTORE OpenCtlStoreFile(
  65. LPSTR pszStoreFilename)
  66. {
  67. HCERTSTORE hStore;
  68. BYTE *pbEncoded;
  69. DWORD cbEncoded;
  70. if (!ReadDERFromFile(pszStoreFilename, &pbEncoded, &cbEncoded))
  71. return NULL;
  72. if (NULL == (hStore = CertOpenStore(
  73. CERT_STORE_PROV_MEMORY,
  74. 0, // dwEncodingType
  75. 0, // hCryptProv
  76. 0, // dwFlags
  77. NULL // pvPara
  78. )))
  79. goto CommonReturn;
  80. if (!CertAddEncodedCTLToStore(
  81. hStore,
  82. dwMsgAndCertEncodingType,
  83. pbEncoded,
  84. cbEncoded,
  85. CERT_STORE_ADD_ALWAYS,
  86. NULL // ppCtlContext
  87. )) {
  88. CertCloseStore(hStore, 0);
  89. hStore = NULL;
  90. }
  91. CommonReturn:
  92. TestFree(pbEncoded);
  93. return hStore;
  94. }
  95. static HCERTSTORE OpenCtlStore(
  96. LPSTR pszStore,
  97. BOOL fSystemStore
  98. )
  99. {
  100. HCERTSTORE hStore;
  101. if (!fSystemStore)
  102. hStore = OpenCtlStoreFile(pszStore);
  103. else
  104. hStore = NULL;
  105. if (NULL == hStore)
  106. hStore = OpenSignerStore(pszStore, fSystemStore);
  107. return hStore;
  108. }
  109. static PCCERT_CONTEXT OpenSubjectCert(
  110. LPSTR pszFilename
  111. )
  112. {
  113. BYTE *pbEncoded;
  114. DWORD cbEncoded;
  115. PCCERT_CONTEXT pCert;
  116. if (!ReadDERFromFile(pszFilename, &pbEncoded, &cbEncoded))
  117. return NULL;
  118. pCert = CertCreateCertificateContext(
  119. dwCertEncodingType,
  120. pbEncoded,
  121. cbEncoded
  122. );
  123. TestFree(pbEncoded);
  124. return pCert;
  125. }
  126. int _cdecl main(int argc, char * argv[])
  127. {
  128. int status;
  129. DWORD dwError = 0;
  130. DWORD dwFlags = 0;
  131. DWORD dwSubjectType = CTL_CERT_SUBJECT_TYPE;
  132. DWORD dwDisplayFlags = 0;
  133. #define MAX_USAGE_ID 20
  134. LPSTR rgpszUsageId[MAX_USAGE_ID];
  135. CTL_USAGE SubjectUsage = {0, rgpszUsageId};
  136. #define MAX_CTL_STORE_COUNT 16
  137. HCERTSTORE rghCtlStore[MAX_CTL_STORE_COUNT];
  138. #define MAX_SIGNER_STORE_COUNT 16
  139. HCERTSTORE rghSignerStore[MAX_SIGNER_STORE_COUNT];
  140. #define MAX_SUBJECT_COUNT 16
  141. PCCERT_CONTEXT rgpSubject[MAX_SUBJECT_COUNT];
  142. DWORD cSubject = 0;
  143. CTL_VERIFY_USAGE_PARA VerifyPara;
  144. CTL_VERIFY_USAGE_STATUS VerifyStatus;
  145. CTL_ANY_SUBJECT_INFO AnySubjectInfo;
  146. BYTE rgbHash[MAX_HASH_LEN];
  147. PCCTL_CONTEXT pCtl = NULL;
  148. PCCERT_CONTEXT pSigner = NULL;
  149. memset(&VerifyPara, 0, sizeof(VerifyPara));
  150. VerifyPara.cbSize = sizeof(VerifyPara);
  151. VerifyPara.rghCtlStore = rghCtlStore;
  152. VerifyPara.rghSignerStore = rghSignerStore;
  153. memset(&VerifyStatus, 0, sizeof(VerifyStatus));
  154. VerifyStatus.cbSize = sizeof(VerifyStatus);
  155. VerifyStatus.ppCtl = &pCtl;
  156. VerifyStatus.ppSigner = &pSigner;
  157. DWORD i;
  158. while (--argc>0) {
  159. if (**++argv == '-')
  160. {
  161. BOOL fSystemStore = FALSE;
  162. switch(argv[0][1])
  163. {
  164. case 'U':
  165. if (SubjectUsage.cUsageIdentifier >= MAX_USAGE_ID) {
  166. printf("Maximum number of Usage Identifiers: %d\n",
  167. MAX_USAGE_ID);
  168. goto BadUsage;
  169. }
  170. rgpszUsageId[SubjectUsage.cUsageIdentifier++] = argv[0] + 2;
  171. break;
  172. case 'L':
  173. if (0 == strlen(argv[0] + 2))
  174. VerifyPara.ListIdentifier.cbData =
  175. CTL_FIND_NO_LIST_ID_CBDATA;
  176. else {
  177. VerifyPara.ListIdentifier.cbData = strlen(argv[0] + 2);
  178. VerifyPara.ListIdentifier.pbData = (BYTE *) argv[0] + 2;
  179. }
  180. break;
  181. case 'A':
  182. dwSubjectType = CTL_ANY_SUBJECT_TYPE;
  183. break;
  184. case 'C':
  185. fSystemStore = TRUE;
  186. case 'c':
  187. if (VerifyPara.cCtlStore >= MAX_CTL_STORE_COUNT) {
  188. printf("Maximum number of CTL Stores: %d\n",
  189. MAX_CTL_STORE_COUNT);
  190. goto BadUsage;
  191. }
  192. if (rghCtlStore[VerifyPara.cCtlStore] = OpenCtlStore(
  193. argv[0] + 2, fSystemStore))
  194. VerifyPara.cCtlStore++;
  195. else {
  196. printf("OpenCtlStore(%s) failed\n", argv[0] + 2);
  197. goto BadUsage;
  198. }
  199. break;
  200. case 'S':
  201. fSystemStore = TRUE;
  202. case 's':
  203. if (VerifyPara.cSignerStore >= MAX_SIGNER_STORE_COUNT) {
  204. printf("Maximum number of Signer Stores: %d\n",
  205. MAX_SIGNER_STORE_COUNT);
  206. goto BadUsage;
  207. }
  208. if (rghSignerStore[VerifyPara.cSignerStore] = OpenSignerStore(
  209. argv[0] + 2, fSystemStore))
  210. VerifyPara.cSignerStore++;
  211. else {
  212. printf("OpenSignerStore(%s) failed\n", argv[0] + 2);
  213. goto BadUsage;
  214. }
  215. break;
  216. case 'e':
  217. dwError = strtoul(argv[0]+2, NULL, 0);
  218. break;
  219. case 'f':
  220. dwFlags = strtoul(argv[0]+2, NULL, 0);
  221. break;
  222. case 'b':
  223. dwDisplayFlags |= DISPLAY_BRIEF_FLAG;
  224. break;
  225. case 'v':
  226. dwDisplayFlags |= DISPLAY_VERBOSE_FLAG;
  227. break;
  228. case 'h':
  229. default:
  230. goto BadUsage;
  231. }
  232. } else {
  233. if (cSubject >= MAX_SUBJECT_COUNT) {
  234. printf("Exceeded maximum Subject count of %d\n",
  235. MAX_SUBJECT_COUNT);
  236. goto BadUsage;
  237. }
  238. if (rgpSubject[cSubject] = OpenSubjectCert(argv[0]))
  239. cSubject++;
  240. else {
  241. printf("OpenSubjectCert(%s) failed\n", argv[0]);
  242. goto BadUsage;
  243. }
  244. }
  245. }
  246. if (cSubject == 0) {
  247. printf("Missing Subject Filename\n");
  248. goto BadUsage;
  249. }
  250. printf("command line: %s\n", GetCommandLine());
  251. for (i = 0; i < cSubject; i++) {
  252. BOOL fResult;
  253. void *pvSubject;
  254. printf("===== Subject[%d] =====\n", i);
  255. if (CTL_ANY_SUBJECT_TYPE == dwSubjectType) {
  256. memset(&AnySubjectInfo, 0, sizeof(AnySubjectInfo));
  257. AnySubjectInfo.SubjectAlgorithm.pszObjId = szOID_OIWSEC_sha1;
  258. AnySubjectInfo.SubjectIdentifier.cbData = MAX_HASH_LEN;
  259. if (!CertGetCertificateContextProperty(
  260. rgpSubject[i],
  261. CERT_SHA1_HASH_PROP_ID,
  262. rgbHash,
  263. &AnySubjectInfo.SubjectIdentifier.cbData) ||
  264. 0 == AnySubjectInfo.SubjectIdentifier.cbData) {
  265. printf("failed => unable to get SHA1 hash for Subject[%d]\n",
  266. i);
  267. continue;
  268. }
  269. AnySubjectInfo.SubjectIdentifier.pbData = rgbHash;
  270. pvSubject = &AnySubjectInfo;
  271. } else
  272. pvSubject = (void *) rgpSubject[i];
  273. fResult = CertVerifyCTLUsage(
  274. dwCertEncodingType,
  275. dwSubjectType,
  276. pvSubject,
  277. &SubjectUsage,
  278. dwFlags,
  279. &VerifyPara,
  280. &VerifyStatus);
  281. if (fResult) {
  282. if (pCtl) {
  283. printf("----- CTL -----\n");
  284. DisplayCtl(pCtl, dwDisplayFlags | DISPLAY_NO_ISSUER_FLAG, 0);
  285. printf("\nSubject Index:: %d\n", VerifyStatus.dwCtlEntryIndex);
  286. } else
  287. printf("Failed, CertVerifyCTLUsage didn't return CTL\n");
  288. if (pSigner) {
  289. printf("----- Signer [%d] -----\n",
  290. VerifyStatus.dwSignerIndex);
  291. DisplayCert(pSigner, 0);
  292. if (pCtl && (dwDisplayFlags & DISPLAY_VERBOSE_FLAG))
  293. DisplaySignerInfo(
  294. pCtl->hCryptMsg,
  295. VerifyStatus.dwSignerIndex,
  296. dwDisplayFlags);
  297. CertFreeCertificateContext(pSigner);
  298. pSigner = NULL;
  299. } else
  300. printf("Failed, CertVerifyCTLUsage didn't return Signer\n");
  301. if (pCtl) {
  302. CertFreeCTLContext(pCtl);
  303. pCtl = NULL;
  304. }
  305. if (0 != VerifyStatus.dwError)
  306. printf("CertVerifyCTLUsage returned dwError: 0x%x (%d)\n",
  307. VerifyStatus.dwError, VerifyStatus.dwError);
  308. if (0 != VerifyStatus.dwFlags)
  309. printf("CertVerifyCTLUsage returned dwFlags: 0x%x\n",
  310. VerifyStatus.dwFlags);
  311. if (0 != dwError)
  312. printf("Failed, CertVerifyCTLUsage returned Success, not the expected dwError: 0x%x (%d)\n",
  313. dwError, dwError);
  314. } else {
  315. printf("CertVerifyCTLUsage:: dwError: 0x%x (%d)\n",
  316. VerifyStatus.dwError, VerifyStatus.dwError);
  317. if (dwError != VerifyStatus.dwError)
  318. printf("Failed, CertVerifyCTLUsage didn't return the expected dwError: 0x%x (%d)\n",
  319. dwError, dwError);
  320. }
  321. }
  322. printf("Passed\n");
  323. status = 0;
  324. CommonReturn:
  325. while (cSubject--)
  326. CertFreeCertificateContext(rgpSubject[cSubject]);
  327. while (VerifyPara.cCtlStore--) {
  328. if (!CertCloseStore(VerifyPara.rghCtlStore[VerifyPara.cCtlStore],
  329. CERT_CLOSE_STORE_CHECK_FLAG))
  330. PrintLastError("CertCloseStore(CtlStore)");
  331. }
  332. while (VerifyPara.cSignerStore--) {
  333. if (!CertCloseStore(VerifyPara.rghSignerStore[VerifyPara.cSignerStore],
  334. CERT_CLOSE_STORE_CHECK_FLAG))
  335. PrintLastError("CertCloseStore(SignerStore)");
  336. }
  337. return status;
  338. BadUsage:
  339. Usage();
  340. printf("Failed\n");
  341. status = -1;
  342. goto CommonReturn;
  343. }