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.

1734 lines
42 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: tcertcli.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <certca.h>
  15. #include <winldap.h>
  16. #include <dsrole.h>
  17. #include <dsgetdc.h>
  18. #include <lmaccess.h>
  19. #include <lmapibuf.h>
  20. //--------------------------------------------------------------------
  21. HRESULT
  22. myRobustLdapBindEx(
  23. OUT LDAP ** ppldap,
  24. OPTIONAL OUT LPWSTR* ppszForestDNSName,
  25. IN BOOL fGC)
  26. {
  27. HRESULT hr;
  28. BOOL fForceRediscovery = FALSE;
  29. DWORD dwGetDCFlags = DS_RETURN_DNS_NAME;
  30. PDOMAIN_CONTROLLER_INFO pDomainInfo = NULL;
  31. LDAP *pld = NULL;
  32. WCHAR const *pwszDomainControllerName = NULL;
  33. ULONG ldaperr;
  34. if (fGC)
  35. {
  36. dwGetDCFlags |= DS_GC_SERVER_REQUIRED;
  37. }
  38. do {
  39. if (fForceRediscovery)
  40. {
  41. dwGetDCFlags |= DS_FORCE_REDISCOVERY;
  42. }
  43. ldaperr = LDAP_SERVER_DOWN;
  44. // netapi32!DsGetDcName is delay loaded, so wrap
  45. __try
  46. {
  47. // Get the GC location
  48. hr = DsGetDcName(
  49. NULL, // Delayload wrapped
  50. NULL,
  51. NULL,
  52. NULL,
  53. dwGetDCFlags,
  54. &pDomainInfo);
  55. }
  56. __except(EXCEPTION_EXECUTE_HANDLER)
  57. {
  58. hr=E_UNEXPECTED;
  59. }
  60. if (S_OK != hr)
  61. {
  62. hr = HRESULT_FROM_WIN32(hr);
  63. if (fForceRediscovery)
  64. {
  65. goto error;
  66. }
  67. fForceRediscovery = TRUE;
  68. continue;
  69. }
  70. if (NULL == pDomainInfo ||
  71. (fGC && 0 == (DS_GC_FLAG & pDomainInfo->Flags)) ||
  72. 0 == (DS_DNS_CONTROLLER_FLAG & pDomainInfo->Flags) ||
  73. NULL == pDomainInfo->DomainControllerName)
  74. {
  75. if (!fForceRediscovery)
  76. {
  77. fForceRediscovery = TRUE;
  78. continue;
  79. }
  80. hr = HRESULT_FROM_WIN32(ERROR_CANT_ACCESS_DOMAIN_INFO);
  81. goto error;
  82. }
  83. pwszDomainControllerName = pDomainInfo->DomainControllerName;
  84. // skip past forward slashes (why are they there?)
  85. while (L'\\' == *pwszDomainControllerName)
  86. {
  87. pwszDomainControllerName++;
  88. }
  89. // bind to ds
  90. pld = ldap_init(
  91. const_cast<WCHAR *>(pwszDomainControllerName),
  92. fGC? LDAP_GC_PORT : LDAP_PORT);
  93. if (NULL == pld)
  94. {
  95. ldaperr = LdapGetLastError();
  96. }
  97. else
  98. {
  99. // do this because we're explicitly setting DC name
  100. ldaperr = ldap_set_option(pld, LDAP_OPT_AREC_EXCLUSIVE, LDAP_OPT_ON);
  101. ldaperr = ldap_bind_s(pld, NULL, NULL, LDAP_AUTH_NEGOTIATE);
  102. }
  103. hr = HRESULT_FROM_WIN32(LdapMapErrorToWin32(ldaperr));
  104. if (fForceRediscovery)
  105. {
  106. break;
  107. }
  108. fForceRediscovery = TRUE;
  109. } while (LDAP_SERVER_DOWN == ldaperr);
  110. // everything's cool, party down
  111. if (S_OK == hr)
  112. {
  113. *ppldap = pld;
  114. pld = NULL;
  115. }
  116. error:
  117. if (NULL != pld)
  118. {
  119. ldap_unbind(pld);
  120. }
  121. // we know netapi32 was already loaded safely (that's where we got
  122. // pDomainInfo), so no need to wrap
  123. if (NULL != pDomainInfo)
  124. {
  125. NetApiBufferFree(pDomainInfo); // Delayload wrapped
  126. }
  127. return(hr);
  128. }
  129. //--------------------------------------------------------------------
  130. HRESULT
  131. myRobustLdapBind(
  132. OUT LDAP ** ppldap,
  133. IN BOOL fGC)
  134. {
  135. return(myRobustLdapBindEx(ppldap, NULL, fGC));
  136. }
  137. //--------------------------------------------------------------------
  138. void PrintHelp(void) {
  139. wprintf(
  140. L"tcertcli <testID>\n"
  141. L" Available tests:\n"
  142. L" OID - test CAOIDxxxx functions\n"
  143. L" Template - test CACertTypexxxx functions\n"
  144. L" Query - test CACertTypeQuery functions without pld\n"
  145. L" QueryLDAP - test CACertTypeQuery functions with pld\n"
  146. L" CAEnum <CAName> - test CAEnumCertTypesForCA functions without pld\n"
  147. L" CAEnumLDAP <CAName> - test CAEnumCertTypesForCAEx functions with pld\n"
  148. L" TemplateDes - test the description property of templates\n"
  149. L" Clone <TemplateName> - test the clone without pld\n"
  150. L" CloneLDAP <TemplateName> - test the clone with pld\n"
  151. L" ACRS - test create/delete autoenrollment object from ACRS store\n"
  152. L" OIDURL - test URL code for OID container\n"
  153. );
  154. }
  155. //--------------------------------------------------------------------
  156. BOOL TemplateTest()
  157. {
  158. BOOL fSuccess=FALSE;
  159. HRESULT hr=S_OK;
  160. DWORD dwProp=0;
  161. LPWSTR rgwszProp[4];
  162. CERT_ENHKEY_USAGE KeyUsage;
  163. LPSTR szOID="1.2.3.4.5.6";
  164. FILETIME time1;
  165. FILETIME time2;
  166. DWORD dwNameFlag;
  167. HCERTTYPE hCertType=NULL;
  168. HANDLE hClientToken=NULL;
  169. HANDLE hHandle = NULL;
  170. PCERT_EXTENSIONS pCertExtensions=NULL;
  171. LPWSTR *pwszProp=NULL;
  172. LPWSTR *pwszProp1=NULL;
  173. PSECURITY_DESCRIPTOR pSD=NULL;
  174. LPWSTR pwszOID=NULL;
  175. LPWSTR pwsz=NULL;
  176. DWORD dwType=0;
  177. //get the client token
  178. hHandle = GetCurrentThread();
  179. if (NULL == hHandle)
  180. {
  181. hr = HRESULT_FROM_WIN32(GetLastError());
  182. }
  183. else
  184. {
  185. if (!OpenThreadToken(hHandle,
  186. TOKEN_QUERY,
  187. TRUE, // open as self
  188. &hClientToken))
  189. {
  190. hr = HRESULT_FROM_WIN32(GetLastError());
  191. CloseHandle(hHandle);
  192. hHandle = NULL;
  193. }
  194. }
  195. if(hr != S_OK)
  196. {
  197. hHandle = GetCurrentProcess();
  198. if (NULL == hHandle)
  199. {
  200. hr = HRESULT_FROM_WIN32(GetLastError());
  201. }
  202. else
  203. {
  204. HANDLE hProcessToken = NULL;
  205. hr = S_OK;
  206. if (!OpenProcessToken(hHandle,
  207. TOKEN_DUPLICATE,
  208. &hProcessToken))
  209. {
  210. hr = HRESULT_FROM_WIN32(GetLastError());
  211. CloseHandle(hHandle);
  212. hHandle = NULL;
  213. }
  214. else
  215. {
  216. if(!DuplicateToken(hProcessToken,
  217. SecurityImpersonation,
  218. &hClientToken))
  219. {
  220. hr = HRESULT_FROM_WIN32(GetLastError());
  221. CloseHandle(hHandle);
  222. hHandle = NULL;
  223. }
  224. CloseHandle(hProcessToken);
  225. }
  226. }
  227. }
  228. if(S_OK != hr)
  229. goto error;
  230. //find a certifcate type admin
  231. if(S_OK != CAFindCertTypeByName(
  232. wszCERTTYPE_ADMIN,
  233. NULL,
  234. CT_ENUM_USER_TYPES,
  235. &hCertType
  236. ))
  237. {
  238. wprintf(L"Can not find template %ws\n", wszCERTTYPE_ADMIN);
  239. goto error;
  240. }
  241. //get the name flag
  242. if(S_OK != CAGetCertTypeFlagsEx(
  243. hCertType,
  244. CERTTYPE_SUBJECT_NAME_FLAG,
  245. &dwNameFlag
  246. ))
  247. {
  248. wprintf(L"Can not find template %ws\n", wszCERTTYPE_ADMIN);
  249. goto error;
  250. }
  251. //get all extensions
  252. if(S_OK != CAGetCertTypeExtensionsEx(
  253. hCertType,
  254. 0,
  255. NULL,
  256. &pCertExtensions
  257. ))
  258. {
  259. wprintf(L"Can not find extensions %ws\n", wszCERTTYPE_ADMIN);
  260. goto error;
  261. }
  262. if(S_OK != (CAFreeCertTypeExtensions(hCertType, pCertExtensions)))
  263. goto error;
  264. pCertExtensions=NULL;
  265. //get template extensions
  266. if(S_OK != CAGetCertTypeExtensionsEx(
  267. hCertType,
  268. CT_EXTENSION_TEMPLATE,
  269. NULL,
  270. &pCertExtensions
  271. ))
  272. {
  273. wprintf(L"Can not find extensions %ws\n", wszCERTTYPE_ADMIN);
  274. goto error;
  275. }
  276. if(S_OK != (CAFreeCertTypeExtensions(hCertType, pCertExtensions)))
  277. goto error;
  278. pCertExtensions=NULL;
  279. //get selected extension
  280. if(S_OK != CAGetCertTypeExtensionsEx(
  281. hCertType,
  282. CT_EXTENSION_BASIC_CONTRAINTS | CT_EXTENSION_APPLICATION_POLICY,
  283. NULL,
  284. &pCertExtensions
  285. ))
  286. {
  287. wprintf(L"Can not find extensions %ws\n", wszCERTTYPE_ADMIN);
  288. }
  289. else
  290. {
  291. if(S_OK != (CAFreeCertTypeExtensions(hCertType, pCertExtensions)))
  292. goto error;
  293. }
  294. pCertExtensions=NULL;
  295. //get all extension from the old way
  296. if(S_OK != CAGetCertTypeExtensions(
  297. hCertType,
  298. &pCertExtensions
  299. ))
  300. {
  301. wprintf(L"Can not find extensions %ws\n", wszCERTTYPE_ADMIN);
  302. goto error;
  303. }
  304. if(S_OK != (CAFreeCertTypeExtensions(hCertType, pCertExtensions)))
  305. goto error;
  306. pCertExtensions=NULL;
  307. if(S_OK != CAGetCertTypeFlagsEx(
  308. hCertType,
  309. CERTTYPE_SUBJECT_NAME_FLAG,
  310. &dwNameFlag
  311. ))
  312. {
  313. wprintf(L"Can not find template %ws\n", wszCERTTYPE_ADMIN);
  314. goto error;
  315. }
  316. if(S_OK != CAGetCertTypePropertyEx(
  317. hCertType,
  318. CERTTYPE_PROP_FRIENDLY_NAME,
  319. &pwszProp))
  320. {
  321. wprintf(L"Can not get friendly name for template %ws\n", wszCERTTYPE_ADMIN);
  322. goto error;
  323. }
  324. wprintf(L"The friendly name for %ws is %ws\n", wszCERTTYPE_ADMIN, pwszProp[0]);
  325. CAFreeCertTypeProperty(hCertType, pwszProp);
  326. pwszProp=NULL;
  327. CACloseCertType(hCertType);
  328. hCertType=NULL;
  329. //delete a certifcate type
  330. if(S_OK != CAFindCertTypeByName(
  331. wszCERTTYPE_USER,
  332. NULL,
  333. CT_ENUM_USER_TYPES,
  334. &hCertType
  335. ))
  336. goto error;
  337. if(S_OK != CADeleteCertType(hCertType))
  338. goto error;
  339. if(S_OK != CACloseCertType(hCertType))
  340. goto error;
  341. hCertType=NULL;
  342. //testing find cert type by oid
  343. if(S_OK != CAFindCertTypeByName(
  344. wszCERTTYPE_CA_EXCHANGE,
  345. NULL,
  346. CT_ENUM_USER_TYPES | CT_FLAG_NO_CACHE_LOOKUP | CT_ENUM_MACHINE_TYPES,
  347. &hCertType
  348. ))
  349. goto error;
  350. if(S_OK != CAGetCertTypePropertyEx(
  351. hCertType,
  352. CERTTYPE_PROP_OID,
  353. &pwszProp))
  354. goto error;
  355. if(S_OK != CACloseCertType(hCertType))
  356. goto error;
  357. hCertType=NULL;
  358. if(S_OK != CAFindCertTypeByName(
  359. pwszProp[0],
  360. NULL,
  361. CT_FIND_BY_OID | CT_ENUM_MACHINE_TYPES,
  362. &hCertType
  363. ))
  364. goto error;
  365. if(S_OK != CAGetCertTypePropertyEx(
  366. hCertType,
  367. CERTTYPE_PROP_OID,
  368. &pwszProp1))
  369. goto error;
  370. if(0!=wcscmp(pwszProp[0], pwszProp1[0]))
  371. goto error;
  372. if(S_OK != CAFreeCertTypeProperty(hCertType, pwszProp))
  373. goto error;
  374. if(S_OK != CAFreeCertTypeProperty(hCertType, pwszProp1))
  375. goto error;
  376. if(S_OK != CACloseCertType(hCertType))
  377. goto error;
  378. hCertType=NULL;
  379. //create a certificate type
  380. if(S_OK != CACreateCertType(L"NewCertType",
  381. NULL,
  382. 0,
  383. &hCertType))
  384. goto error;
  385. if(S_OK != CAUpdateCertType(hCertType))
  386. goto error;
  387. if(S_OK != CACloseCertType(hCertType))
  388. goto error;
  389. hCertType=NULL;
  390. //retrieve V1 certifcate type: EFS
  391. //access check on the EFS cert type
  392. if(S_OK != CAFindCertTypeByName(
  393. wszCERTTYPE_EFS,
  394. NULL,
  395. CT_ENUM_USER_TYPES,
  396. &hCertType
  397. ))
  398. goto error;
  399. if(S_OK != CAGetCertTypeFlagsEx(
  400. hCertType,
  401. CERTTYPE_GENERAL_FLAG,
  402. &dwProp))
  403. goto error;
  404. printf("The general flag for EFS is: %d\n", dwProp);
  405. if(S_OK != CAGetCertTypeFlagsEx(
  406. hCertType,
  407. CERTTYPE_PRIVATE_KEY_FLAG,
  408. &dwProp))
  409. goto error;
  410. printf("The private key flag for EFS is: %d\n", dwProp);
  411. if(S_OK != CAGetCertTypePropertyEx(
  412. hCertType,
  413. CERTTYPE_PROP_SCHEMA_VERSION,
  414. &dwProp))
  415. goto error;
  416. printf("The schema version for EFS is: %d\n", dwProp);
  417. if(S_OK != CACertTypeAccessCheckEx(
  418. hCertType,
  419. hClientToken,
  420. CERTTYPE_ACCESS_CHECK_ENROLL))
  421. goto error;
  422. //no autoenrollment
  423. if(S_OK == CACertTypeAccessCheckEx(
  424. hCertType,
  425. hClientToken,
  426. CERTTYPE_ACCESS_CHECK_AUTO_ENROLL))
  427. goto error;
  428. if(S_OK != CACloseCertType(hCertType))
  429. goto error;
  430. hCertType=NULL;
  431. //retrieve V2 certifcate type: EFS
  432. //access check on the EFS cert type
  433. if(S_OK != CAFindCertTypeByName(
  434. wszCERTTYPE_CROSS_CA,
  435. NULL,
  436. CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES,
  437. &hCertType
  438. ))
  439. goto error;
  440. if(S_OK != CACertTypeAccessCheckEx(
  441. hCertType,
  442. hClientToken,
  443. CERTTYPE_ACCESS_CHECK_ENROLL))
  444. goto error;
  445. //no autoenrollment
  446. if(S_OK == CACertTypeAccessCheckEx(
  447. hCertType,
  448. hClientToken,
  449. CERTTYPE_ACCESS_CHECK_AUTO_ENROLL))
  450. goto error;
  451. if(S_OK != CACloseCertType(hCertType))
  452. goto error;
  453. hCertType=NULL;
  454. //clone a certificate type
  455. if(S_OK != CAFindCertTypeByName(
  456. wszCERTTYPE_SUBORDINATE_CA,
  457. NULL,
  458. CT_ENUM_MACHINE_TYPES,
  459. &hCertType
  460. ))
  461. goto error;
  462. rgwszProp[0]=L"ClonedCertType";
  463. rgwszProp[1]=NULL;
  464. if(S_OK != CASetCertTypePropertyEx(
  465. hCertType,
  466. CERTTYPE_PROP_CN,
  467. rgwszProp))
  468. goto error;
  469. rgwszProp[0]=L"ClonedCertType Friendly";
  470. rgwszProp[1]=NULL;
  471. if(S_OK != CASetCertTypePropertyEx(
  472. hCertType,
  473. CERTTYPE_PROP_FRIENDLY_NAME,
  474. rgwszProp))
  475. goto error;
  476. rgwszProp[0]=L"1.2.3.4.5";
  477. rgwszProp[1]=NULL;
  478. if(S_OK != CASetCertTypePropertyEx(
  479. hCertType,
  480. CERTTYPE_PROP_OID,
  481. rgwszProp))
  482. goto error;
  483. rgwszProp[0]=L"1.2.3.4.5.6.7.8.9.10";
  484. rgwszProp[1]=NULL;
  485. if(S_OK != CASetCertTypePropertyEx(
  486. hCertType,
  487. CERTTYPE_PROP_APPLICATION_POLICY,
  488. rgwszProp))
  489. goto error;
  490. rgwszProp[0]=L"1.2.3.4.5.6.7.8.9.10.11";
  491. rgwszProp[1]=NULL;
  492. if(S_OK != CASetCertTypePropertyEx(
  493. hCertType,
  494. CERTTYPE_PROP_RA_APPLICATION_POLICY,
  495. rgwszProp))
  496. goto error;
  497. rgwszProp[0]=L"1.2.3.4.5.6.7.8.9.10.11.12";
  498. rgwszProp[1]=NULL;
  499. if(S_OK != CASetCertTypePropertyEx(
  500. hCertType,
  501. CERTTYPE_PROP_RA_POLICY,
  502. rgwszProp))
  503. goto error;
  504. rgwszProp[0]=NULL;
  505. if(S_OK != CASetCertTypePropertyEx(
  506. hCertType,
  507. CERTTYPE_PROP_SUPERSEDE,
  508. rgwszProp))
  509. goto error;
  510. rgwszProp[0]=L"CloneSuper1";
  511. rgwszProp[1]=L"CloneSuper2";
  512. rgwszProp[2]=L"CloneSuper3";
  513. rgwszProp[3]=NULL;
  514. if(S_OK != CASetCertTypePropertyEx(
  515. hCertType,
  516. CERTTYPE_PROP_SUPERSEDE,
  517. rgwszProp))
  518. goto error;
  519. dwProp=2048;
  520. if(S_OK != CASetCertTypePropertyEx(
  521. hCertType,
  522. CERTTYPE_PROP_MIN_KEY_SIZE,
  523. &dwProp))
  524. goto error;
  525. if(S_OK != CAGetCertTypeExtensions(
  526. hCertType,
  527. &pCertExtensions))
  528. goto error;
  529. KeyUsage.cUsageIdentifier=1;
  530. KeyUsage.rgpszUsageIdentifier=&szOID;
  531. if(S_OK != CASetCertTypeExtension(
  532. hCertType,
  533. TEXT(szOID_ENHANCED_KEY_USAGE),
  534. CA_EXT_FLAG_CRITICAL,
  535. &KeyUsage))
  536. goto error;
  537. if(S_OK != CAUpdateCertType(hCertType))
  538. goto error;
  539. if(S_OK != CACloseCertType(hCertType))
  540. goto error;
  541. hCertType=NULL;
  542. //edit V2 certificate type: KeyRecoveryAgent
  543. //update SD, Expiration,
  544. if(S_OK != CAFindCertTypeByName(
  545. wszCERTTYPE_KEY_RECOVERY_AGENT,
  546. NULL,
  547. CT_ENUM_USER_TYPES,
  548. &hCertType
  549. ))
  550. goto error;
  551. if(S_OK != CACertTypeAccessCheckEx(
  552. hCertType,
  553. hClientToken,
  554. CERTTYPE_ACCESS_CHECK_ENROLL))
  555. goto error;
  556. dwProp=103;
  557. if(S_OK != CASetCertTypePropertyEx(
  558. hCertType,
  559. CERTTYPE_PROP_REVISION,
  560. &dwProp))
  561. goto error;
  562. if(S_OK != CASetCertTypeFlagsEx(
  563. hCertType,
  564. CERTTYPE_ENROLLMENT_FLAG,
  565. 0))
  566. goto error;
  567. if(S_OK != CASetCertTypeFlagsEx(
  568. hCertType,
  569. CERTTYPE_SUBJECT_NAME_FLAG,
  570. 0))
  571. goto error;
  572. if(S_OK != CAGetCertTypeExpiration(
  573. hCertType,
  574. &time1,
  575. &time2))
  576. goto error;
  577. if(S_OK != CASetCertTypeExpiration(
  578. hCertType,
  579. &time1,
  580. &time2))
  581. goto error;
  582. if(S_OK != CACertTypeGetSecurity(
  583. hCertType,
  584. &pSD))
  585. goto error;
  586. if(S_OK != CACertTypeSetSecurity(
  587. hCertType,
  588. pSD))
  589. goto error;
  590. if(S_OK != CAGetCertTypePropertyEx(
  591. hCertType,
  592. CERTTYPE_PROP_CSP_LIST,
  593. &pwszProp))
  594. goto error;
  595. if(pwszProp && pwszProp[0])
  596. printf("The CSP for KRA is: %S\n", pwszProp[0]);
  597. if(S_OK != CAFreeCertTypeProperty(
  598. hCertType,
  599. pwszProp))
  600. goto error;
  601. pwszProp=NULL;
  602. if(S_OK != CAGetCertTypePropertyEx(
  603. hCertType,
  604. CERTTYPE_PROP_RA_POLICY,
  605. &pwszProp))
  606. goto error;
  607. if(pwszProp && pwszProp[0])
  608. printf("The RAPolicy for KRA is: %S\n", pwszProp[0]);
  609. if(S_OK != CAFreeCertTypeProperty(
  610. hCertType,
  611. pwszProp))
  612. goto error;
  613. pwszProp=NULL;
  614. rgwszProp[0]=L"1.2.3.4.5.6.7.8.9.10";
  615. rgwszProp[1]=L"2.2.3.4.5.6.7.8.9.10";
  616. rgwszProp[2]=NULL;
  617. if(S_OK != CASetCertTypePropertyEx(
  618. hCertType,
  619. CERTTYPE_PROP_APPLICATION_POLICY,
  620. rgwszProp))
  621. goto error;
  622. rgwszProp[0]=L"1.2.3.4.5.6.7.8.9.10.11";
  623. rgwszProp[1]=L"2.2.3.4.5.6.7.8.9.10.11";
  624. rgwszProp[2]=NULL;
  625. if(S_OK != CASetCertTypePropertyEx(
  626. hCertType,
  627. CERTTYPE_PROP_RA_APPLICATION_POLICY,
  628. rgwszProp))
  629. goto error;
  630. if(S_OK != CAGetCertTypePropertyEx(
  631. hCertType,
  632. CERTTYPE_PROP_RA_APPLICATION_POLICY,
  633. &pwszProp))
  634. goto error;
  635. if(pwszProp && pwszProp[0])
  636. printf("The RAAppPolicy for KRA is: %S\n", pwszProp[0]);
  637. if(S_OK != CAFreeCertTypeProperty(
  638. hCertType,
  639. pwszProp))
  640. goto error;
  641. pwszProp=NULL;
  642. if(S_OK != CAGetCertTypePropertyEx(
  643. hCertType,
  644. CERTTYPE_PROP_APPLICATION_POLICY,
  645. &pwszProp))
  646. goto error;
  647. if(pwszProp && pwszProp[0])
  648. printf("The AppPolicy for KRA is: %S\n", pwszProp[0]);
  649. if(S_OK != CAFreeCertTypeProperty(
  650. hCertType,
  651. pwszProp))
  652. goto error;
  653. pwszProp=NULL;
  654. if(S_OK != CAUpdateCertType(hCertType))
  655. goto error;
  656. if(S_OK != CACloseCertType(hCertType))
  657. goto error;
  658. hCertType=NULL;
  659. //get the KRA properties again
  660. if(S_OK != CAFindCertTypeByName(
  661. wszCERTTYPE_KEY_RECOVERY_AGENT,
  662. NULL,
  663. CT_ENUM_USER_TYPES,
  664. &hCertType
  665. ))
  666. goto error;
  667. if(S_OK != CAGetCertTypePropertyEx(
  668. hCertType,
  669. CERTTYPE_PROP_RA_APPLICATION_POLICY,
  670. &pwszProp))
  671. goto error;
  672. if(pwszProp && pwszProp[0])
  673. printf("The RAAppPolicy for KRA is: %S\n", pwszProp[0]);
  674. if(S_OK != CAFreeCertTypeProperty(
  675. hCertType,
  676. pwszProp))
  677. goto error;
  678. pwszProp=NULL;
  679. if(S_OK != CAGetCertTypePropertyEx(
  680. hCertType,
  681. CERTTYPE_PROP_APPLICATION_POLICY,
  682. &pwszProp))
  683. goto error;
  684. if(pwszProp && pwszProp[0])
  685. printf("The AppPolicy for KRA is: %S\n", pwszProp[0]);
  686. if(S_OK != CAFreeCertTypeProperty(
  687. hCertType,
  688. pwszProp))
  689. goto error;
  690. pwszProp=NULL;
  691. fSuccess=TRUE;
  692. error:
  693. if(pwszOID)
  694. LocalFree(pwszOID);
  695. if(pCertExtensions)
  696. CAFreeCertTypeExtensions(hCertType,pCertExtensions);
  697. if(hCertType)
  698. CACloseCertType(hCertType);
  699. if(pSD)
  700. LocalFree(pSD);
  701. if(hHandle)
  702. CloseHandle(hHandle);
  703. if(hClientToken)
  704. CloseHandle(hClientToken);
  705. return fSuccess;
  706. }
  707. //--------------------------------------------------------------------
  708. BOOL OIDTest()
  709. {
  710. BOOL fSuccess=FALSE;
  711. HRESULT hr=S_OK;
  712. DWORD dwProp=0;
  713. LPWSTR rgwszProp[4];
  714. CERT_ENHKEY_USAGE KeyUsage;
  715. LPSTR szOID="1.2.3.4.5.6";
  716. FILETIME time1;
  717. FILETIME time2;
  718. LPWSTR *pwszProp=NULL;
  719. LPWSTR *pwszProp1=NULL;
  720. LPWSTR pwszOID=NULL;
  721. LPWSTR pwsz=NULL;
  722. DWORD dwType=0;
  723. //oid manipulation
  724. //create
  725. if(S_OK != CAOIDCreateNew(CERT_OID_TYPE_TEMPLATE, 0,
  726. &pwszOID))
  727. goto error;
  728. //set/get property test
  729. if(S_OK != CAOIDSetProperty(
  730. pwszOID,
  731. CERT_OID_PROPERTY_DISPLAY_NAME,
  732. L"MyNewOIDFriendlyName"))
  733. goto error;
  734. if(S_OK != CAOIDSetProperty(
  735. pwszOID,
  736. CERT_OID_PROPERTY_CPS,
  737. L"MYCSPStatement"))
  738. goto error;
  739. if(S_OK != CAOIDGetProperty(
  740. pwszOID,
  741. CERT_OID_PROPERTY_CPS,
  742. &pwsz))
  743. goto error;
  744. printf("The CPS statement is: %S\n", pwsz);
  745. if(S_OK != CAOIDFreeProperty(pwsz))
  746. goto error;
  747. pwsz=NULL;
  748. if(S_OK != CAOIDGetProperty(
  749. pwszOID,
  750. CERT_OID_PROPERTY_TYPE,
  751. &dwType))
  752. goto error;
  753. printf("The property type is: %d\n", dwType);
  754. if(S_OK == CAOIDSetProperty(
  755. pwszOID,
  756. CERT_OID_PROPERTY_TYPE,
  757. L"MyNewOIDFriendlyName"))
  758. goto error;
  759. if(S_OK == CAOIDSetProperty(
  760. L"1.2",
  761. CERT_OID_PROPERTY_DISPLAY_NAME,
  762. L"MyNewOIDFriendlyName"))
  763. goto error;
  764. //add and delete
  765. if(S_OK != CAOIDAdd(CERT_OID_TYPE_ISSUER_POLICY, 0, L"1.2.3"))
  766. goto error;
  767. if(S_OK != CAOIDAdd(CERT_OID_TYPE_ISSUER_POLICY, 0, L"1.2.3.4"))
  768. goto error;
  769. if(S_OK != CAOIDSetProperty(L"1.2.3.4",
  770. CERT_OID_PROPERTY_DISPLAY_NAME,
  771. L"MyNewIssuerPolicyOid"))
  772. goto error;
  773. if(S_OK != CAOIDGetProperty(L"1.2.3.4",
  774. CERT_OID_PROPERTY_DISPLAY_NAME,
  775. &pwsz))
  776. goto error;
  777. printf("The display name is: %S\n", pwsz);
  778. if(S_OK != CAOIDSetProperty(L"1.2.3.4",
  779. CERT_OID_PROPERTY_CPS,
  780. L"The DS Issuer Policy String"))
  781. goto error;
  782. if(S_OK != CAOIDSetProperty(L"1.2.3.4",
  783. CERT_OID_PROPERTY_CPS,
  784. NULL))
  785. goto error;
  786. if(S_OK != CAOIDSetProperty(L"1.2.3.4",
  787. CERT_OID_PROPERTY_CPS,
  788. L"New CPS"))
  789. goto error;
  790. if(S_OK != CAOIDFreeProperty(pwsz))
  791. goto error;
  792. pwsz=NULL;
  793. if(CRYPT_E_EXISTS != CAOIDAdd(CERT_OID_TYPE_ISSUER_POLICY, 0, L"1.2.3"))
  794. goto error;
  795. if(S_OK != CAOIDDelete(L"1.2.3"))
  796. goto error;
  797. if(S_OK != CAOIDDelete(L"1.2.3.4"))
  798. goto error;
  799. //URL testing
  800. if(S_OK != CAOIDGetLdapURL(CERT_OID_TYPE_TEMPLATE,
  801. 0,
  802. &pwsz))
  803. goto error;
  804. printf("The URL for template is: %S\n", pwsz);
  805. if(S_OK != CAOIDFreeLdapURL(pwsz))
  806. goto error;
  807. if(S_OK != CAOIDGetLdapURL(CERT_OID_TYPE_ALL,
  808. 0,
  809. &pwsz))
  810. goto error;
  811. printf("The URL for all is: %S\n", pwsz);
  812. if(S_OK != CAOIDFreeLdapURL(pwsz))
  813. goto error;
  814. if(S_OK != CAOIDGetLdapURL(CERT_OID_TYPE_APPLICATION_POLICY,
  815. 0,
  816. &pwsz))
  817. goto error;
  818. printf("The URL for application policy is: %S\n", pwsz);
  819. if(S_OK != CAOIDFreeLdapURL(pwsz))
  820. goto error;
  821. fSuccess=TRUE;
  822. error:
  823. if(pwszOID)
  824. LocalFree(pwszOID);
  825. return fSuccess;
  826. }
  827. //--------------------------------------------------------------------
  828. BOOL QueryTest(BOOL fBind)
  829. {
  830. HRESULT hr=E_FAIL;
  831. BOOL fResult = FALSE;
  832. DWORD dwNumber = 0;
  833. DWORD dwIndex=0;
  834. LDAP *pldap=NULL;
  835. HCERTTYPEQUERY hCertTypeQuery=NULL;
  836. if(fBind)
  837. {
  838. if(S_OK != (hr = myRobustLdapBind(&pldap, FALSE)))
  839. {
  840. wprintf(L"myRobustLdapBind failed with 0x%08X. \n",hr);
  841. goto error;
  842. }
  843. }
  844. if(S_OK != CACertTypeRegisterQuery(0, pldap, &hCertTypeQuery))
  845. {
  846. wprintf(L"CACertTypeRegisterQury failed with 0x%08X. \n",hr);
  847. goto error;
  848. }
  849. for(dwIndex=0; dwIndex < 3; dwIndex++)
  850. {
  851. if(S_OK != CACertTypeQuery(hCertTypeQuery, &dwNumber))
  852. {
  853. wprintf(L"CACertTypeQuery failed with 0x%08X. \n",hr);
  854. goto error;
  855. }
  856. wprintf(L"CACertTypeQuery returned %d. \n", dwNumber);
  857. wprintf(L"Wait for 20 seconds. \n");
  858. Sleep(20 * 1000);
  859. }
  860. fResult = TRUE;
  861. error:
  862. if(hCertTypeQuery)
  863. CACertTypeUnregisterQuery(hCertTypeQuery);
  864. if(pldap)
  865. ldap_unbind(pldap);
  866. return fResult;
  867. }
  868. //--------------------------------------------------------------------
  869. //
  870. //
  871. // CAEnumTest
  872. //
  873. //
  874. //--------------------------------------------------------------------
  875. BOOL CAEnumTest(BOOL fBind, LPWSTR pwszCA)
  876. {
  877. HRESULT hr=E_FAIL;
  878. BOOL fResult = FALSE;
  879. DWORD dwCount = 0;
  880. HCAINFO hCAInfo = NULL;
  881. HCERTTYPE hCertType = NULL;
  882. LDAP *pldap=NULL;
  883. LPWSTR *awszProp=NULL;
  884. if(fBind)
  885. {
  886. if(S_OK != (hr = myRobustLdapBind(&pldap, FALSE)))
  887. {
  888. wprintf(L"myRobustLdapBind failed with 0x%08X. \n",hr);
  889. goto error;
  890. }
  891. }
  892. if(S_OK != (hr = CAFindByName(
  893. pwszCA,
  894. NULL,
  895. CA_FIND_LOCAL_SYSTEM,
  896. &hCAInfo)))
  897. {
  898. wprintf(L"CAFindByName failed with 0x%08X. \n",hr);
  899. goto error;
  900. }
  901. if(NULL==hCAInfo)
  902. {
  903. wprintf(L"CAFindByName return NULL hCAInfo. \n");
  904. goto error;
  905. }
  906. if(fBind)
  907. {
  908. hr = CAEnumCertTypesForCAEx(
  909. hCAInfo,
  910. (LPCWSTR)pldap,
  911. CT_FIND_LOCAL_SYSTEM | CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES |
  912. CT_FLAG_SCOPE_IS_LDAP_HANDLE | CT_FLAG_NO_CACHE_LOOKUP,
  913. &hCertType);
  914. }
  915. else
  916. {
  917. hr = CAEnumCertTypesForCA(
  918. hCAInfo,
  919. CT_FIND_LOCAL_SYSTEM | CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES,
  920. &hCertType);
  921. }
  922. if( (S_OK != hr) || (NULL == hCertType))
  923. {
  924. wprintf(L"CAEnumCertTyes failed with 0x%08X. \n",hr);
  925. goto error;
  926. }
  927. dwCount = CACountCertTypes(hCertType);
  928. if(0 == dwCount)
  929. {
  930. wprintf(L"Error: CACountCertTypes returns 0 templates.\n");
  931. goto error;
  932. }
  933. wprintf(L"CACountCertTypes returns %d templates.\n", dwCount);
  934. //get the CA properties
  935. hr=CAGetCAProperty(hCAInfo,
  936. CA_PROP_DNSNAME,
  937. &awszProp);
  938. if((S_OK != hr) || (NULL==awszProp) || (NULL==awszProp[0]))
  939. {
  940. wprintf(L"CAGetCAProperty failed with 0x%08X. \n",hr);
  941. goto error;
  942. }
  943. wprintf(L"CA's DNS name is %ws.\n", awszProp[0]);
  944. hr=CAFreeCAProperty(hCAInfo, awszProp);
  945. if(S_OK != hr)
  946. {
  947. wprintf(L"CAFreeCAProperty failed with 0x%08X. \n",hr);
  948. goto error;
  949. }
  950. //name
  951. hr=CAGetCAProperty(hCAInfo,
  952. CA_PROP_NAME,
  953. &awszProp);
  954. if((S_OK != hr) || (NULL==awszProp) || (NULL==awszProp[0]))
  955. {
  956. wprintf(L"CAGetCAProperty failed with 0x%08X. \n",hr);
  957. goto error;
  958. }
  959. wprintf(L"CA's CN name is %ws.\n", awszProp[0]);
  960. hr=CAFreeCAProperty(hCAInfo, awszProp);
  961. if(S_OK != hr)
  962. {
  963. wprintf(L"CAFreeCAProperty failed with 0x%08X. \n",hr);
  964. goto error;
  965. }
  966. //display name
  967. hr=CAGetCAProperty(hCAInfo,
  968. CA_PROP_DISPLAY_NAME,
  969. &awszProp);
  970. if((S_OK != hr) || (NULL==awszProp) || (NULL==awszProp[0]))
  971. {
  972. wprintf(L"CAGetCAProperty failed with 0x%08X. \n",hr);
  973. goto error;
  974. }
  975. wprintf(L"CA's display name is %ws.\n", awszProp[0]);
  976. hr=CAFreeCAProperty(hCAInfo, awszProp);
  977. if(S_OK != hr)
  978. {
  979. wprintf(L"CAFreeCAProperty failed with 0x%08X. \n",hr);
  980. goto error;
  981. }
  982. //cert types
  983. hr=CAGetCAProperty(hCAInfo,
  984. CA_PROP_CERT_TYPES,
  985. &awszProp);
  986. if((S_OK != hr) || (NULL==awszProp) || (NULL==awszProp[0]))
  987. {
  988. wprintf(L"CAGetCAProperty failed with 0x%08X. \n",hr);
  989. goto error;
  990. }
  991. wprintf(L"CA's cert types name is %ws.\n", awszProp[0]);
  992. hr=CAFreeCAProperty(hCAInfo, awszProp);
  993. if(S_OK != hr)
  994. {
  995. wprintf(L"CAFreeCAProperty failed with 0x%08X. \n",hr);
  996. goto error;
  997. }
  998. fResult = TRUE;
  999. error:
  1000. if(hCertType)
  1001. CACloseCertType(hCertType);
  1002. if(hCAInfo)
  1003. {
  1004. CACloseCA(hCAInfo);
  1005. }
  1006. if(pldap)
  1007. ldap_unbind(pldap);
  1008. return fResult;
  1009. }
  1010. //--------------------------------------------------------------------
  1011. //
  1012. // CloneTest
  1013. //
  1014. //
  1015. //--------------------------------------------------------------------
  1016. BOOL CloneTest(BOOL fBind, LPWSTR pwszCertType)
  1017. {
  1018. HRESULT hr=E_FAIL;
  1019. BOOL fResult = FALSE;
  1020. DWORD dwFindCT=CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES;
  1021. WCHAR wszName[100];
  1022. WCHAR wszFriendlyName[100];
  1023. LPWSTR *awszFriendlyName=NULL;
  1024. LDAP *pldap=NULL;
  1025. HCERTTYPE hCertType=NULL;
  1026. HCERTTYPE hNewCertType=NULL;
  1027. if(fBind)
  1028. {
  1029. if(S_OK != (hr = myRobustLdapBind(&pldap, FALSE)))
  1030. {
  1031. wprintf(L"myRobustLdapBind failed with 0x%08X. \n",hr);
  1032. goto error;
  1033. }
  1034. }
  1035. if(S_OK != (hr = CAFindCertTypeByName(
  1036. pwszCertType,
  1037. NULL,
  1038. dwFindCT,
  1039. &hCertType)))
  1040. {
  1041. wprintf(L"CAFindCertTypeByName failed with 0x%08X. \n",hr);
  1042. goto error;
  1043. }
  1044. wcscpy(wszName, pwszCertType);
  1045. wcscat(wszName, L"_Clone");
  1046. wcscpy(wszFriendlyName, pwszCertType);
  1047. wcscat(wszFriendlyName, L"_CloneFriendly");
  1048. if(S_OK != (hr=CACloneCertType(hCertType,
  1049. wszName,
  1050. wszFriendlyName,
  1051. pldap,
  1052. fBind? CT_CLONE_KEEP_AUTOENROLLMENT_SETTING | CT_CLONE_KEEP_SUBJECT_NAME_SETTING : 0,
  1053. &hNewCertType)))
  1054. {
  1055. wprintf(L"CACloneCertType failed with 0x%08X. \n",hr);
  1056. goto error;
  1057. }
  1058. if(S_OK != (hr=CAUpdateCertType(hNewCertType)))
  1059. {
  1060. wprintf(L"CAUpdateCertType failed with 0x%08X. \n",hr);
  1061. goto error;
  1062. }
  1063. //close the tempate
  1064. if(S_OK != (hr=CACloseCertType(hNewCertType)))
  1065. {
  1066. hNewCertType=NULL;
  1067. wprintf(L"CACloseCertType failed with 0x%08X. \n",hr);
  1068. goto error;
  1069. }
  1070. hNewCertType=NULL;
  1071. if(S_OK != (hr = CAFindCertTypeByName(
  1072. wszName,
  1073. NULL,
  1074. dwFindCT,
  1075. &hNewCertType)))
  1076. {
  1077. wprintf(L"CAFindCertTypeByName for the cloned template failed with 0x%08X. \n",hr);
  1078. goto error;
  1079. }
  1080. if(S_OK != (hr=CAGetCertTypePropertyEx(
  1081. hNewCertType,
  1082. CERTTYPE_PROP_FRIENDLY_NAME,
  1083. &awszFriendlyName)))
  1084. {
  1085. wprintf(L"CAGetCertTypePropertyEx for the cloned template failed with 0x%08X. \n",hr);
  1086. goto error;
  1087. }
  1088. if(0 != (wcscmp(awszFriendlyName[0], wszFriendlyName)))
  1089. {
  1090. wprintf(L"The friendly name for the cloned template does not match the original. \n");
  1091. hr=E_FAIL;
  1092. goto error;
  1093. }
  1094. fResult = TRUE;
  1095. error:
  1096. if(pldap)
  1097. ldap_unbind(pldap);
  1098. if(awszFriendlyName)
  1099. CAFreeCertTypeProperty(hNewCertType, awszFriendlyName);
  1100. if(hCertType)
  1101. CACloseCertType(hCertType);
  1102. if(hNewCertType)
  1103. CACloseCertType(hNewCertType);
  1104. return fResult;
  1105. }
  1106. //--------------------------------------------------------------------
  1107. //
  1108. // TemplateDesTest
  1109. //
  1110. //
  1111. //--------------------------------------------------------------------
  1112. BOOL TemplateDesTest()
  1113. {
  1114. BOOL fResult = FALSE;
  1115. HRESULT hr = E_FAIL;
  1116. DWORD dwCount=0;
  1117. DWORD dwIndex=0;
  1118. HCERTTYPE hCertType=NULL;
  1119. HCERTTYPE hNextCertType=NULL;
  1120. LPWSTR *pwszFriendlyName=NULL;
  1121. LPWSTR *pwszDescription=NULL;
  1122. HANDLE hClientToken=NULL;
  1123. HANDLE hHandle = NULL;
  1124. HCAINFO hCAInfo=NULL;
  1125. //get the client token
  1126. /* hHandle = GetCurrentThread();
  1127. if (NULL == hHandle)
  1128. {
  1129. hr = HRESULT_FROM_WIN32(GetLastError());
  1130. }
  1131. else
  1132. {
  1133. if (!OpenThreadToken(hHandle,
  1134. TOKEN_QUERY,
  1135. TRUE, // open as self
  1136. &hClientToken))
  1137. {
  1138. hr = HRESULT_FROM_WIN32(GetLastError());
  1139. CloseHandle(hHandle);
  1140. hHandle = NULL;
  1141. }
  1142. }
  1143. if(hr != S_OK)
  1144. {
  1145. hHandle = GetCurrentProcess();
  1146. if (NULL == hHandle)
  1147. {
  1148. hr = HRESULT_FROM_WIN32(GetLastError());
  1149. }
  1150. else
  1151. {
  1152. HANDLE hProcessToken = NULL;
  1153. hr = S_OK;
  1154. if (!OpenProcessToken(hHandle,
  1155. TOKEN_DUPLICATE,
  1156. &hProcessToken))
  1157. {
  1158. hr = HRESULT_FROM_WIN32(GetLastError());
  1159. CloseHandle(hHandle);
  1160. hHandle = NULL;
  1161. }
  1162. else
  1163. {
  1164. if(!DuplicateToken(hProcessToken,
  1165. SecurityImpersonation,
  1166. &hClientToken))
  1167. {
  1168. hr = HRESULT_FROM_WIN32(GetLastError());
  1169. CloseHandle(hHandle);
  1170. hHandle = NULL;
  1171. }
  1172. CloseHandle(hProcessToken);
  1173. }
  1174. }
  1175. }
  1176. if(S_OK != hr)
  1177. goto error;
  1178. hr = CAFindCertTypeByName(L"WindowsTestBuildSigning",
  1179. NULL,
  1180. CT_ENUM_USER_TYPES,
  1181. &hCertType);
  1182. if(S_OK != hr)
  1183. {
  1184. wprintf(L"CAFindCertTypeByName failed with 0x%08X. \n",hr);
  1185. goto error;
  1186. }
  1187. hr = CACertTypeAccessCheck(
  1188. hCertType,
  1189. hClientToken
  1190. );
  1191. if(S_OK != hr)
  1192. {
  1193. wprintf(L"CACertTypeAccessCheck failed with 0x%08X. \n",hr);
  1194. goto error;
  1195. }
  1196. hr = CAFindByName(
  1197. L"Microsoft Windows VBL03 !0028DS!0029",
  1198. NULL,
  1199. 0,
  1200. &hCAInfo);
  1201. if((S_OK != hr) || (NULL==hCAInfo))
  1202. {
  1203. wprintf(L"CAFindByName for %ws failed with 0x%08X. \n", hr);
  1204. goto error;
  1205. }
  1206. hr=CAAccessCheck(hCAInfo,
  1207. hClientToken);
  1208. if(S_OK != hr)
  1209. {
  1210. wprintf(L"CAAccessCheck failed with 0x%08X. \n",hr);
  1211. goto error;
  1212. } */
  1213. hr = CAEnumCertTypes(
  1214. CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES,
  1215. &hCertType);
  1216. if((S_OK != hr) || (NULL==hCertType))
  1217. {
  1218. wprintf(L"CAEnumCertTyes failed with 0x%08X. \n",hr);
  1219. goto error;
  1220. }
  1221. dwCount = CACountCertTypes(hCertType);
  1222. wprintf(L"We have 0x%d cert types. \n", dwCount);
  1223. if(0 == dwCount)
  1224. goto error;
  1225. for(dwIndex=0; dwIndex < dwCount; dwIndex++)
  1226. {
  1227. if(dwIndex!=0)
  1228. {
  1229. hr = CAEnumNextCertType(hCertType, &hNextCertType);
  1230. if(S_OK != hr)
  1231. {
  1232. wprintf(L"CAEnumNextCertType failed with 0x%08X. \n",hr);
  1233. goto error;
  1234. }
  1235. CACloseCertType(hCertType);
  1236. hCertType=hNextCertType;
  1237. }
  1238. hr = CAGetCertTypePropertyEx(hCertType,
  1239. CERTTYPE_PROP_FRIENDLY_NAME,
  1240. &pwszFriendlyName);
  1241. if((S_OK != hr) || (NULL==pwszFriendlyName) || (NULL==pwszFriendlyName[0]))
  1242. {
  1243. wprintf(L"Friendly Name property failed with 0x%08X. \n",hr);
  1244. goto error;
  1245. }
  1246. hr = CAGetCertTypePropertyEx(hCertType,
  1247. CERTTYPE_PROP_DESCRIPTION,
  1248. &pwszDescription);
  1249. if((S_OK != hr) || (NULL==pwszDescription) || (NULL==pwszDescription[0]))
  1250. {
  1251. wprintf(L"Description property failed with 0x%08X. \n",hr);
  1252. goto error;
  1253. }
  1254. wprintf(L"%ws has the description of %ws. \n",pwszFriendlyName[0], pwszDescription[0]);
  1255. CAFreeCertTypeProperty(hCertType, pwszFriendlyName);
  1256. CAFreeCertTypeProperty(hCertType, pwszDescription);
  1257. }
  1258. fResult = TRUE;
  1259. error:
  1260. if(hHandle)
  1261. CloseHandle(hHandle);
  1262. if(hClientToken)
  1263. CloseHandle(hClientToken);
  1264. return fResult;
  1265. }
  1266. //--------------------------------------------------------------------
  1267. //
  1268. // OIDURLTest
  1269. //
  1270. //
  1271. //--------------------------------------------------------------------
  1272. BOOL OIDURLTest()
  1273. {
  1274. BOOL fResult=FALSE;
  1275. HRESULT hr=E_FAIL;
  1276. DWORD dwIndex=0;
  1277. LPWSTR pwsz=NULL;
  1278. for(dwIndex=0; dwIndex < 14; dwIndex++)
  1279. {
  1280. //URL testing
  1281. if(S_OK != (hr=CAOIDGetLdapURL(CERT_OID_TYPE_TEMPLATE,
  1282. 0,
  1283. &pwsz)))
  1284. {
  1285. wprintf(L"CAOIDGetLdapURL failed with 0x%08X. \n",hr);
  1286. }
  1287. printf("The URL for template is: %S\n", pwsz);
  1288. if(S_OK != (hr=CAOIDFreeLdapURL(pwsz)))
  1289. {
  1290. wprintf(L"CAOIDFreeLdapURL failed with 0x%08X. \n",hr);
  1291. goto error;
  1292. }
  1293. //sleep for 1 second
  1294. Sleep(1000);
  1295. }
  1296. fResult=TRUE;
  1297. error:
  1298. return fResult;
  1299. }
  1300. //--------------------------------------------------------------------
  1301. //
  1302. // ACRSTest
  1303. //
  1304. //
  1305. //--------------------------------------------------------------------
  1306. BOOL ACRSTest()
  1307. {
  1308. BOOL fResult=FALSE;
  1309. HRESULT hr=E_FAIL;
  1310. /* hr = CACreateLocalAutoEnrollmentObject(
  1311. wszCERTTYPE_DC, // DC certificate
  1312. NULL, // any CA
  1313. NULL, // reserved
  1314. CERT_SYSTEM_STORE_LOCAL_MACHINE);
  1315. if(S_OK != hr)
  1316. {
  1317. wprintf(L"CreateLocalAutoEnrollmentObject failed with 0x%08X. \n",hr);
  1318. goto error;
  1319. } */
  1320. hr = CADeleteLocalAutoEnrollmentObject(
  1321. wszCERTTYPE_DC, // DC certificate
  1322. NULL, // any CA
  1323. NULL, // reserved
  1324. CERT_SYSTEM_STORE_LOCAL_MACHINE);
  1325. if(S_OK != hr)
  1326. {
  1327. wprintf(L"DeleteLocalAutoEnrollmentObject failed with 0x%08X. \n",hr);
  1328. goto error;
  1329. }
  1330. fResult=TRUE;
  1331. error:
  1332. return fResult;
  1333. }
  1334. //--------------------------------------------------------------------
  1335. extern "C" int __cdecl wmain(int nArgs, WCHAR ** rgwszArgs)
  1336. {
  1337. BOOL fResult;
  1338. if (1 == nArgs || 0==wcscmp(rgwszArgs[1], L"/?") || 0==wcscmp(rgwszArgs[1], L"-?")) {
  1339. PrintHelp();
  1340. goto done;
  1341. }
  1342. if (0==_wcsicmp(L"OID", rgwszArgs[1])) {
  1343. fResult=OIDTest();
  1344. } else if (0==_wcsicmp(L"Template", rgwszArgs[1])) {
  1345. fResult=TemplateTest();
  1346. } else if (0==_wcsicmp(L"Query", rgwszArgs[1])) {
  1347. fResult=QueryTest(FALSE);
  1348. } else if (0==_wcsicmp(L"QueryLDAP", rgwszArgs[1])) {
  1349. fResult=QueryTest(TRUE);
  1350. } else if (0==_wcsicmp(L"CAEnum", rgwszArgs[1])) {
  1351. fResult=CAEnumTest(FALSE, rgwszArgs[2]);
  1352. } else if (0==_wcsicmp(L"CAEnumLDAP", rgwszArgs[1])) {
  1353. fResult=CAEnumTest(TRUE, rgwszArgs[2]);
  1354. } else if (0==_wcsicmp(L"Clone", rgwszArgs[1])) {
  1355. fResult=CloneTest(FALSE, rgwszArgs[2]);
  1356. } else if (0==_wcsicmp(L"CloneLDAP", rgwszArgs[1])) {
  1357. fResult=CloneTest(TRUE, rgwszArgs[2]);
  1358. } else if (0==_wcsicmp(L"TemplateDes", rgwszArgs[1])) {
  1359. fResult=TemplateDesTest();
  1360. } else if (0==_wcsicmp(L"ACRS", rgwszArgs[1])) {
  1361. fResult=ACRSTest();
  1362. } else if (0==_wcsicmp(L"OIDURL", rgwszArgs[1])) {
  1363. fResult=OIDURLTest();
  1364. } else {
  1365. wprintf(L"Command '%ws' unknown.\n", rgwszArgs[1]);
  1366. goto done;
  1367. }
  1368. if (!fResult) {
  1369. wprintf(L"Command '%ws' failed \n", rgwszArgs[1]);
  1370. } else {
  1371. wprintf(L"Command '%ws' completed successfully.\n", rgwszArgs[1]);
  1372. }
  1373. done:
  1374. return fResult;
  1375. }