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.

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