Source code of Windows XP (NT5)
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.

2730 lines
63 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: config.cpp
  7. //
  8. // Contents: Cert Server client implementation
  9. //
  10. // History: 24-Aug-96 vich created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "pch.cpp"
  14. #pragma hdrstop
  15. #include "csdisp.h"
  16. #include "certca.h"
  17. #include "configp.h"
  18. #include <assert.h>
  19. #include <lmaccess.h>
  20. #include <lmapibuf.h>
  21. #include <lmwksta.h>
  22. #include <limits.h>
  23. #define __dwFILE__ __dwFILE_CERTCLI_CONFIGP_CPP__
  24. WCHAR g_wszConfigFile[] = L"\\certsrv.txt";
  25. WCHAR const g_wszRegDirectory[] = wszREGDIRECTORY;
  26. #define cwcCONFIGLINEMAX (12 * 1024)
  27. extern "C"
  28. {
  29. HWND GetDeskTopWindow(VOID);
  30. }
  31. HRESULT
  32. certRequestGetConfigInfo(
  33. IN WCHAR *pwszSharedFolder,
  34. IN OUT LONG *pIndex,
  35. IN OUT LONG *pCount,
  36. OUT CERT_AUTHORITY_INFO **ppCertAuthorityInfo);
  37. VOID
  38. _CleanupCAInfo(
  39. IN OUT CERT_AUTHORITY_INFO *pCA)
  40. {
  41. if (NULL != pCA->pwszSanitizedName)
  42. {
  43. LocalFree(pCA->pwszSanitizedName);
  44. }
  45. if (NULL != pCA->pwszSanitizedShortName)
  46. {
  47. LocalFree(pCA->pwszSanitizedShortName);
  48. }
  49. if (NULL != pCA->pwszSanitizedOrgUnit)
  50. {
  51. LocalFree(pCA->pwszSanitizedOrgUnit);
  52. }
  53. if (NULL != pCA->pwszSanitizedOrganization)
  54. {
  55. LocalFree(pCA->pwszSanitizedOrganization);
  56. }
  57. if (NULL != pCA->pwszSanitizedLocality)
  58. {
  59. LocalFree(pCA->pwszSanitizedLocality);
  60. }
  61. if (NULL != pCA->pwszSanitizedState)
  62. {
  63. LocalFree(pCA->pwszSanitizedState);
  64. }
  65. if (NULL != pCA->pwszSanitizedCountry)
  66. {
  67. LocalFree(pCA->pwszSanitizedCountry);
  68. }
  69. if (NULL != pCA->pwszSanitizedConfig)
  70. {
  71. LocalFree(pCA->pwszSanitizedConfig);
  72. }
  73. if (NULL != pCA->pwszSanitizedExchangeCertificate)
  74. {
  75. LocalFree(pCA->pwszSanitizedExchangeCertificate);
  76. }
  77. if (NULL != pCA->pwszSanitizedSignatureCertificate)
  78. {
  79. LocalFree(pCA->pwszSanitizedSignatureCertificate);
  80. }
  81. if (NULL != pCA->pwszSanitizedDescription)
  82. {
  83. LocalFree(pCA->pwszSanitizedDescription);
  84. }
  85. ZeroMemory(pCA, sizeof(*pCA));
  86. }
  87. //+--------------------------------------------------------------------------
  88. // CCertConfigPrivate::~CCertConfigPrivate -- destructor
  89. //
  90. // free memory associated with this instance
  91. //+--------------------------------------------------------------------------
  92. CCertConfigPrivate::~CCertConfigPrivate()
  93. {
  94. LONG i;
  95. if (NULL != m_pCertAuthorityInfo)
  96. {
  97. if (0 != m_Count)
  98. {
  99. for (i = 0; i < m_Count; ++i)
  100. {
  101. _CleanupCAInfo(&m_pCertAuthorityInfo[i]);
  102. }
  103. }
  104. LocalFree(m_pCertAuthorityInfo);
  105. }
  106. if (NULL != m_pwszSharedFolder)
  107. {
  108. LocalFree(m_pwszSharedFolder);
  109. }
  110. }
  111. HRESULT
  112. fillEmptyField(
  113. WCHAR **ppwszField)
  114. {
  115. HRESULT hr;
  116. *ppwszField = (WCHAR *) LocalAlloc(LMEM_FIXED, sizeof(WCHAR));
  117. if (NULL == *ppwszField)
  118. {
  119. hr = E_OUTOFMEMORY;
  120. _JumpError(hr, error, "LocalAlloc");
  121. }
  122. *ppwszField[0] = L'\0';
  123. hr = S_OK;
  124. error:
  125. return(hr);
  126. }
  127. VOID
  128. dsFreeCAConfig(
  129. LONG cCA,
  130. CERT_AUTHORITY_INFO *pCA)
  131. {
  132. LONG i;
  133. if (NULL != pCA && 0 < cCA)
  134. {
  135. for (i = 0; i < cCA; ++i)
  136. {
  137. if (NULL != pCA[i].pwszSanitizedName)
  138. {
  139. LocalFree(pCA[i].pwszSanitizedName);
  140. }
  141. if (NULL != pCA[i].pwszSanitizedShortName)
  142. {
  143. LocalFree(pCA[i].pwszSanitizedShortName);
  144. }
  145. if (NULL != pCA[i].pwszSanitizedConfig)
  146. {
  147. LocalFree(pCA[i].pwszSanitizedConfig);
  148. }
  149. if (NULL != pCA[i].pwszSanitizedOrganization)
  150. {
  151. LocalFree(pCA[i].pwszSanitizedOrganization);
  152. }
  153. if (NULL != pCA[i].pwszSanitizedOrgUnit)
  154. {
  155. LocalFree(pCA[i].pwszSanitizedOrgUnit);
  156. }
  157. if (NULL != pCA[i].pwszSanitizedLocality)
  158. {
  159. LocalFree(pCA[i].pwszSanitizedLocality);
  160. }
  161. if (NULL != pCA[i].pwszSanitizedState)
  162. {
  163. LocalFree(pCA[i].pwszSanitizedState);
  164. }
  165. if (NULL != pCA[i].pwszSanitizedCountry)
  166. {
  167. LocalFree(pCA[i].pwszSanitizedCountry);
  168. }
  169. }
  170. LocalFree(pCA);
  171. }
  172. }
  173. HRESULT
  174. ConfigGetCertNameDNInfo(
  175. IN CERT_NAME_INFO const *pCertNameInfo,
  176. IN CHAR const *pszObjId,
  177. IN OUT WCHAR **ppwszDNInfo)
  178. {
  179. HRESULT hr;
  180. WCHAR const *pwszProperty = NULL;
  181. if (NULL != *ppwszDNInfo && L'\0' != **ppwszDNInfo)
  182. {
  183. hr = S_OK;
  184. goto error;
  185. }
  186. hr = myGetCertNameProperty(pCertNameInfo, pszObjId, &pwszProperty);
  187. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  188. {
  189. hr = S_OK;
  190. goto error;
  191. }
  192. _JumpIfError(hr, error, "myGetCertNameProperty");
  193. if (NULL != pwszProperty)
  194. {
  195. WCHAR *pwsz;
  196. hr = myDupString(pwszProperty, &pwsz);
  197. _JumpIfError(hr, error, "myDupString");
  198. if (NULL != *ppwszDNInfo)
  199. {
  200. LocalFree(*ppwszDNInfo);
  201. }
  202. *ppwszDNInfo = pwsz;
  203. }
  204. hr = S_OK;
  205. error:
  206. return(hr);
  207. }
  208. HRESULT
  209. ConfigLoadDNInfo(
  210. IN BYTE const *pbEncoded,
  211. IN DWORD cbEncoded,
  212. IN OUT CERT_AUTHORITY_INFO *pCA)
  213. {
  214. HRESULT hr;
  215. CERT_NAME_INFO *pCertNameInfo = NULL;
  216. DWORD cbCertNameInfo;
  217. // decode to name info
  218. if (!myDecodeName(
  219. X509_ASN_ENCODING,
  220. X509_UNICODE_NAME,
  221. pbEncoded,
  222. cbEncoded,
  223. CERTLIB_USE_LOCALALLOC,
  224. &pCertNameInfo,
  225. &cbCertNameInfo))
  226. {
  227. hr = myHLastError();
  228. _JumpError(hr, error, "myDecodeName");
  229. }
  230. hr = ConfigGetCertNameDNInfo(
  231. pCertNameInfo,
  232. szOID_ORGANIZATION_NAME,
  233. &pCA->pwszSanitizedOrganization);
  234. _JumpIfError(hr, error, "ConfigGetCertNameDNInfo");
  235. hr = ConfigGetCertNameDNInfo(
  236. pCertNameInfo,
  237. szOID_ORGANIZATIONAL_UNIT_NAME,
  238. &pCA->pwszSanitizedOrgUnit);
  239. _JumpIfError(hr, error, "ConfigGetCertNameDNInfo");
  240. hr = ConfigGetCertNameDNInfo(
  241. pCertNameInfo,
  242. szOID_LOCALITY_NAME,
  243. &pCA->pwszSanitizedLocality);
  244. _JumpIfError(hr, error, "ConfigGetCertNameDNInfo");
  245. hr = ConfigGetCertNameDNInfo(
  246. pCertNameInfo,
  247. szOID_STATE_OR_PROVINCE_NAME,
  248. &pCA->pwszSanitizedState);
  249. _JumpIfError(hr, error, "ConfigGetCertNameDNInfo");
  250. hr = ConfigGetCertNameDNInfo(
  251. pCertNameInfo,
  252. szOID_COUNTRY_NAME,
  253. &pCA->pwszSanitizedCountry);
  254. _JumpIfError(hr, error, "ConfigGetCertNameDNInfo");
  255. error:
  256. if (NULL != pCertNameInfo)
  257. {
  258. LocalFree(pCertNameInfo);
  259. }
  260. return(hr);
  261. }
  262. HRESULT
  263. dsGetCAConfig(
  264. OUT CERT_AUTHORITY_INFO **ppCAConfig,
  265. OUT LONG *pcCAConfig)
  266. {
  267. HRESULT hr;
  268. HCAINFO hCACurrent = NULL;
  269. HCAINFO hCANext = NULL;
  270. WCHAR **ppwszCommonName = NULL;
  271. WCHAR **ppwszSanitizedShortName = NULL;
  272. WCHAR **ppwszDescription = NULL;
  273. WCHAR **ppwszMachine = NULL;
  274. WCHAR **ppwszDN = NULL;
  275. LONG cCA;
  276. LONG iDst;
  277. LONG iSrc;
  278. CERT_AUTHORITY_INFO *pCA = NULL;
  279. BYTE *pbEncoded = NULL;
  280. DWORD cbEncoded;
  281. *ppCAConfig = NULL;
  282. *pcCAConfig = 0;
  283. cCA = 0;
  284. hr = CAEnumFirstCA(
  285. NULL,
  286. CA_FIND_INCLUDE_UNTRUSTED | CA_FIND_INCLUDE_NON_TEMPLATE_CA,
  287. &hCACurrent);
  288. if (HRESULT_FROM_WIN32(ERROR_NETWORK_UNREACHABLE) != hr)
  289. {
  290. _PrintIfErrorStr4(
  291. hr,
  292. "CAEnumFirstCA",
  293. L"Ignored!",
  294. HRESULT_FROM_WIN32(ERROR_SERVER_DISABLED),
  295. HRESULT_FROM_WIN32(ERROR_NO_SUCH_DOMAIN),
  296. HRESULT_FROM_WIN32(ERROR_WRONG_PASSWORD));
  297. }
  298. cCA = CACountCAs(hCACurrent); // 0 on error
  299. if (0 < cCA)
  300. {
  301. pCA = (CERT_AUTHORITY_INFO *) LocalAlloc(
  302. LMEM_FIXED | LMEM_ZEROINIT,
  303. cCA * sizeof(CERT_AUTHORITY_INFO));
  304. if (NULL == pCA)
  305. {
  306. hr = E_OUTOFMEMORY;
  307. _JumpError(hr, error, "LocalAlloc");
  308. }
  309. for (iDst = 0, iSrc = 0; iSrc < cCA; iSrc++)
  310. {
  311. CSASSERT(NULL == ppwszCommonName);
  312. CSASSERT(NULL == ppwszSanitizedShortName);
  313. CSASSERT(NULL == ppwszDescription);
  314. CSASSERT(NULL == ppwszMachine);
  315. CSASSERT(NULL == ppwszDN);
  316. CSASSERT(NULL == pCA[iDst].pwszSanitizedName);
  317. CSASSERT(NULL == pCA[iDst].pwszSanitizedShortName);
  318. CSASSERT(NULL == pCA[iDst].pwszSanitizedConfig);
  319. hr = CAGetCAProperty(
  320. hCACurrent,
  321. CA_PROP_DISPLAY_NAME,
  322. &ppwszCommonName);
  323. _JumpIfError(hr, error, "CAGetCAProperty(CA_PROP_DISPLAY_NAME)");
  324. hr = CAGetCAProperty(hCACurrent, CA_PROP_NAME, &ppwszSanitizedShortName);
  325. _JumpIfError(hr, error, "CAGetCAProperty(CA_PROP_NAME)");
  326. hr = CAGetCAProperty(hCACurrent, CA_PROP_DESCRIPTION, &ppwszDescription);
  327. _JumpIfError(hr, error, "CAGetCAProperty(CA_PROP_DESCRIPTION)");
  328. hr = CAGetCAProperty(hCACurrent, CA_PROP_DNSNAME, &ppwszMachine);
  329. _JumpIfError(hr, error, "CAGetCAProperty(CA_PROP_DNSNAME)");
  330. hr = CAGetCAProperty(hCACurrent, CA_PROP_CERT_DN, &ppwszDN);
  331. _JumpIfError(hr, error, "CAGetCAProperty(CA_PROP_CERT_DN)");
  332. if (NULL == ppwszCommonName ||
  333. NULL == ppwszSanitizedShortName ||
  334. NULL == ppwszMachine ||
  335. NULL == ppwszDN)
  336. {
  337. _PrintError(E_INVALIDARG, "missing CA property");
  338. goto skipca; // skip and don't take the CA
  339. }
  340. hr = mySanitizeName(*ppwszCommonName, &pCA[iDst].pwszSanitizedName);
  341. _JumpIfError(hr, error, "mySanitizeName");
  342. // already sanitized:
  343. hr = myDupString(*ppwszSanitizedShortName, &pCA[iDst].pwszSanitizedShortName);
  344. _JumpIfError(hr, error, "myDupString");
  345. if (NULL != ppwszDescription)
  346. {
  347. hr = myDupString(*ppwszDescription, &pCA[iDst].pwszSanitizedDescription);
  348. _JumpIfError(hr, error, "myDupString");
  349. }
  350. pCA[iDst].pwszSanitizedConfig = (WCHAR *) LocalAlloc(
  351. LMEM_FIXED,
  352. (wcslen(*ppwszMachine) + wcslen(pCA[iDst].pwszSanitizedName) + 2) *
  353. sizeof(WCHAR));
  354. if (NULL == pCA[iDst].pwszSanitizedConfig)
  355. {
  356. hr = E_OUTOFMEMORY;
  357. _JumpError(hr, error, "LocalAlloc(pwszSanitizedConfig)");
  358. }
  359. wcscpy(pCA[iDst].pwszSanitizedConfig, *ppwszMachine);
  360. wcscat(pCA[iDst].pwszSanitizedConfig, L"\\");
  361. wcscat(pCA[iDst].pwszSanitizedConfig, pCA[iDst].pwszSanitizedName);
  362. pbEncoded = NULL;
  363. while (TRUE)
  364. {
  365. // convert dn string to name blob
  366. if (!CertStrToName(
  367. X509_ASN_ENCODING,
  368. *ppwszDN,
  369. CERT_X500_NAME_STR,
  370. NULL,
  371. pbEncoded,
  372. &cbEncoded,
  373. NULL))
  374. {
  375. hr = myHLastError();
  376. _JumpIfError(hr, error, "CertStrToName");
  377. }
  378. if (NULL != pbEncoded)
  379. {
  380. // get name
  381. break;
  382. }
  383. pbEncoded = (BYTE*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT,
  384. cbEncoded);
  385. if (NULL == pbEncoded)
  386. {
  387. hr = E_OUTOFMEMORY;
  388. _JumpError(hr, error, "LocalAlloc");
  389. }
  390. }
  391. hr = ConfigLoadDNInfo(pbEncoded, cbEncoded, &pCA[iDst]);
  392. _JumpIfError(hr, error, "ConfigLoadDNInfo");
  393. // fill empty for the rest
  394. CSASSERT(NULL == pCA[iDst].pwszSanitizedExchangeCertificate);
  395. CSASSERT(NULL == pCA[iDst].pwszSanitizedSignatureCertificate);
  396. pCA[iDst].Flags = CAIF_DSENTRY;
  397. ++iDst;
  398. // free dn blobs
  399. if (NULL != pbEncoded)
  400. {
  401. LocalFree(pbEncoded);
  402. pbEncoded = NULL;
  403. }
  404. skipca:
  405. // free ds out properties
  406. if (NULL != ppwszCommonName)
  407. {
  408. CAFreeCAProperty(hCACurrent, ppwszCommonName);
  409. ppwszCommonName = NULL;
  410. }
  411. if (NULL != ppwszSanitizedShortName)
  412. {
  413. CAFreeCAProperty(hCACurrent, ppwszSanitizedShortName);
  414. ppwszSanitizedShortName = NULL;
  415. }
  416. if (NULL != ppwszDescription)
  417. {
  418. CAFreeCAProperty(hCACurrent, ppwszDescription);
  419. ppwszDescription = NULL;
  420. }
  421. if (NULL != ppwszMachine)
  422. {
  423. CAFreeCAProperty(hCACurrent, ppwszMachine);
  424. ppwszMachine = NULL;
  425. }
  426. if (NULL != ppwszDN)
  427. {
  428. CAFreeCAProperty(hCACurrent, ppwszDN);
  429. ppwszDN = NULL;
  430. }
  431. hr = CAEnumNextCA(hCACurrent, &hCANext);
  432. CACloseCA(hCACurrent);
  433. hCACurrent = hCANext;
  434. if (S_OK != hr || NULL == hCACurrent)
  435. {
  436. break;
  437. }
  438. }
  439. *pcCAConfig = iDst;
  440. *ppCAConfig = pCA;
  441. pCA = NULL;
  442. }
  443. hr = S_OK;
  444. error:
  445. if (NULL != hCACurrent)
  446. {
  447. CACloseCA(hCACurrent);
  448. }
  449. if (NULL != pbEncoded)
  450. {
  451. LocalFree(pbEncoded);
  452. }
  453. if (NULL != ppwszCommonName)
  454. {
  455. CAFreeCAProperty(hCACurrent, ppwszCommonName);
  456. }
  457. if (NULL != ppwszSanitizedShortName)
  458. {
  459. CAFreeCAProperty(hCACurrent, ppwszSanitizedShortName);
  460. }
  461. if (NULL != ppwszMachine)
  462. {
  463. CAFreeCAProperty(hCACurrent, ppwszMachine);
  464. }
  465. if (NULL != ppwszDN)
  466. {
  467. CAFreeCAProperty(hCACurrent, ppwszDN);
  468. }
  469. if (NULL != pCA)
  470. {
  471. dsFreeCAConfig(cCA, pCA);
  472. }
  473. CSASSERT(S_OK == hr || FAILED(hr));
  474. return(hr);
  475. }
  476. BOOL
  477. FindSharedCAInDSList(
  478. IN CERT_AUTHORITY_INFO *pDSCA,
  479. IN LONG countDSCA,
  480. IN CERT_AUTHORITY_INFO const *pCAFromShared,
  481. OUT CERT_AUTHORITY_INFO **ppDSCA)
  482. {
  483. BOOL found = FALSE;
  484. LONG i;
  485. *ppDSCA = NULL;
  486. for (i = 0; i < countDSCA; ++i)
  487. {
  488. if (0 == lstrcmpi(pDSCA[i].pwszSanitizedConfig, pCAFromShared->pwszSanitizedConfig) &&
  489. 0 == lstrcmpi(pDSCA[i].pwszSanitizedName, pCAFromShared->pwszSanitizedName))
  490. {
  491. *ppDSCA = &pDSCA[i];
  492. found = TRUE;
  493. break;
  494. }
  495. }
  496. return(found);
  497. }
  498. HRESULT
  499. updateField(
  500. IN WCHAR const *pwszSource,
  501. OUT WCHAR **ppwszDest)
  502. {
  503. HRESULT hr;
  504. if (NULL != pwszSource &&
  505. (NULL == *ppwszDest || L'\0' == **ppwszDest))
  506. {
  507. if (NULL != *ppwszDest)
  508. {
  509. LocalFree(*ppwszDest);
  510. }
  511. hr = myDupString(pwszSource, ppwszDest);
  512. _JumpIfError(hr, error, "myDupString");
  513. }
  514. hr = S_OK;
  515. error:
  516. return(hr);
  517. }
  518. HRESULT
  519. mergeConfigFields(
  520. IN CERT_AUTHORITY_INFO const *pCAIn,
  521. IN OUT CERT_AUTHORITY_INFO *pCA)
  522. {
  523. HRESULT hr;
  524. if (NULL == pCAIn || NULL == pCA)
  525. {
  526. hr = E_POINTER;
  527. _JumpError(hr, error, "Internal Error");
  528. }
  529. hr = updateField(pCAIn->pwszSanitizedName, &pCA->pwszSanitizedName);
  530. _JumpIfError(hr, error, "updateField(pwszSanitizedName)");
  531. hr = updateField(pCAIn->pwszSanitizedOrgUnit, &pCA->pwszSanitizedOrgUnit);
  532. _JumpIfError(hr, error, "updateField(pwszSanitizedOrgUnit)");
  533. hr = updateField(pCAIn->pwszSanitizedOrganization, &pCA->pwszSanitizedOrganization);
  534. _JumpIfError(hr, error, "updateField(pwszSanitizedOrganization)");
  535. hr = updateField(pCAIn->pwszSanitizedLocality, &pCA->pwszSanitizedLocality);
  536. _JumpIfError(hr, error, "updateField(pwszSanitizedLocality)");
  537. hr = updateField(pCAIn->pwszSanitizedState, &pCA->pwszSanitizedState);
  538. _JumpIfError(hr, error, "updateField(pwszSanitizedState)");
  539. hr = updateField(pCAIn->pwszSanitizedCountry, &pCA->pwszSanitizedCountry);
  540. _JumpIfError(hr, error, "updateField(pwszSanitizedCountry)");
  541. hr = updateField(pCAIn->pwszSanitizedConfig, &pCA->pwszSanitizedConfig);
  542. _JumpIfError(hr, error, "updateField(pwszSanitizedConfig)");
  543. hr = updateField(pCAIn->pwszSanitizedExchangeCertificate, &pCA->pwszSanitizedExchangeCertificate);
  544. _JumpIfError(hr, error, "updateField(pwszSanitizedExchangeCertificate)");
  545. hr = updateField(pCAIn->pwszSanitizedSignatureCertificate, &pCA->pwszSanitizedSignatureCertificate);
  546. _JumpIfError(hr, error, "updateField(pwszSanitizedSignatureCertificate)");
  547. hr = updateField(pCAIn->pwszSanitizedDescription, &pCA->pwszSanitizedDescription);
  548. _JumpIfError(hr, error, "updateField(pwszSanitizedDescription)");
  549. hr = mySanitizedNameToDSName(pCAIn->pwszSanitizedName, &pCA->pwszSanitizedShortName);
  550. _JumpIfError(hr, error, "mySanitizedNameToDSName");
  551. pCA->Flags |= pCAIn->Flags;
  552. error:
  553. return(hr);
  554. }
  555. HRESULT
  556. CCertConfigPrivate::_ResizeCAInfo(
  557. IN LONG Count)
  558. {
  559. HRESULT hr = E_OUTOFMEMORY;
  560. if (NULL == m_pCertAuthorityInfo)
  561. {
  562. m_pCertAuthorityInfo = (CERT_AUTHORITY_INFO *) LocalAlloc(
  563. LMEM_FIXED | LMEM_ZEROINIT,
  564. Count * sizeof(CERT_AUTHORITY_INFO));
  565. if (NULL == m_pCertAuthorityInfo)
  566. {
  567. _JumpError(hr, error, "LocalAlloc");
  568. }
  569. }
  570. else
  571. {
  572. CERT_AUTHORITY_INFO *pCAI;
  573. pCAI = (CERT_AUTHORITY_INFO *) LocalReAlloc(
  574. m_pCertAuthorityInfo,
  575. Count * sizeof(CERT_AUTHORITY_INFO),
  576. LMEM_MOVEABLE);
  577. if (NULL == pCAI)
  578. {
  579. _JumpError(hr, error, "LocalReAlloc");
  580. }
  581. m_pCertAuthorityInfo = pCAI;
  582. // LocalReAlloc won't zero memory when old size was already > m_Count
  583. if (Count > m_Count)
  584. {
  585. ZeroMemory(
  586. &m_pCertAuthorityInfo[m_Count],
  587. (Count - m_Count) * sizeof(m_pCertAuthorityInfo[0]));
  588. }
  589. }
  590. hr = S_OK;
  591. error:
  592. return(hr);
  593. }
  594. HRESULT
  595. CCertConfigPrivate::_AddRegistryConfigEntry(
  596. IN WCHAR const *pwszDnsName,
  597. IN WCHAR const *pwszOldName,
  598. IN WCHAR const *pwszSanitizedCAName,
  599. IN BOOL fParentCA,
  600. OPTIONAL IN CERT_CONTEXT const *pccCAChild,
  601. OPTIONAL OUT CERT_CONTEXT const **ppccCAOut) // non-NULL means local CA
  602. {
  603. HRESULT hr;
  604. HCERTSTORE hMyStore = NULL;
  605. LONG i;
  606. BOOL fFoundCA = FALSE;
  607. WCHAR *pwszDescription = NULL;
  608. WCHAR *pwszSanitizedShortName = NULL;
  609. CERT_CONTEXT const *pccCA = NULL;
  610. CERT_AUTHORITY_INFO *pCA = NULL;
  611. WCHAR *pwsz;
  612. WCHAR *pwszRegPath = NULL;
  613. HKEY hKey = NULL;
  614. if (NULL != ppccCAOut)
  615. {
  616. CSASSERT(NULL == pccCAChild);
  617. *ppccCAOut = NULL;
  618. }
  619. for (i = 0; i < m_Count; ++i)
  620. {
  621. BOOL fMachineNameMatch;
  622. hr = myIsConfigLocal2(
  623. m_pCertAuthorityInfo[i].pwszSanitizedConfig,
  624. pwszDnsName,
  625. pwszOldName,
  626. &fMachineNameMatch);
  627. _JumpIfError(hr, error, "myIsConfigLocal2");
  628. if (fMachineNameMatch)
  629. {
  630. if (!fParentCA) // Local CA
  631. {
  632. m_pCertAuthorityInfo[i].Flags |= CAIF_LOCAL;
  633. }
  634. if (0 == lstrcmpi(
  635. pwszSanitizedCAName,
  636. m_pCertAuthorityInfo[i].pwszSanitizedName))
  637. {
  638. fFoundCA = TRUE;
  639. pCA = &m_pCertAuthorityInfo[i];
  640. pCA->Flags |= CAIF_REGISTRY;
  641. if (fParentCA)
  642. {
  643. pCA->Flags |= CAIF_REGISTRYPARENT;
  644. }
  645. break;
  646. }
  647. }
  648. }
  649. if (!fFoundCA)
  650. {
  651. hr = _ResizeCAInfo(m_Count + 1);
  652. _JumpIfError(hr, error, "_ResizeCAInfo");
  653. pCA = &m_pCertAuthorityInfo[m_Count];
  654. hr = myFormConfigString(
  655. pwszDnsName,
  656. pwszSanitizedCAName,
  657. &pCA->pwszSanitizedConfig);
  658. _JumpIfError(hr, error, "myFormConfigString");
  659. hr = myDupString(pwszSanitizedCAName, &pCA->pwszSanitizedName);
  660. _JumpIfError(hr, error, "myDupString");
  661. hr = mySanitizedNameToDSName(
  662. pwszSanitizedCAName,
  663. &pCA->pwszSanitizedShortName);
  664. _JumpIfError(hr, error, "mySanitizedNameToDSName");
  665. pCA->Flags = CAIF_REGISTRY;
  666. if (fParentCA)
  667. {
  668. pCA->Flags |= CAIF_REGISTRYPARENT;
  669. }
  670. else
  671. {
  672. pCA->Flags |= CAIF_LOCAL;
  673. }
  674. }
  675. CSASSERT(NULL != pCA); // either found or added
  676. if (NULL == pCA->pwszSanitizedDescription ||
  677. L'\0' == *pCA->pwszSanitizedDescription)
  678. {
  679. if (!fParentCA) // Local CA
  680. {
  681. hr = myGetCertRegStrValue(
  682. pCA->pwszSanitizedName,
  683. NULL,
  684. NULL,
  685. wszREGCADESCRIPTION,
  686. &pwszDescription);
  687. _PrintIfErrorStr2(
  688. hr,
  689. "myGetCertRegStrValue",
  690. wszREGCADESCRIPTION,
  691. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
  692. }
  693. else
  694. {
  695. hr = myRegOpenRelativeKey(
  696. pwszDnsName,
  697. L"ca",
  698. 0,
  699. &pwszRegPath,
  700. NULL,
  701. &hKey);
  702. _PrintIfErrorStr(hr, "myGetCertRegStrValue", wszREGCADESCRIPTION);
  703. if (S_OK == hr)
  704. {
  705. DWORD cb;
  706. DWORD cwc;
  707. DWORD dwType;
  708. hr = RegQueryValueEx(
  709. hKey,
  710. wszREGCADESCRIPTION,
  711. NULL,
  712. &dwType,
  713. NULL,
  714. &cb);
  715. if (S_OK == hr && REG_SZ == dwType && sizeof(WCHAR) < cb)
  716. {
  717. cwc = cb / sizeof(WCHAR);
  718. pwszDescription = (WCHAR *) LocalAlloc(
  719. LMEM_FIXED,
  720. (cwc + 1) * sizeof(WCHAR));
  721. if (NULL == pwszDescription)
  722. {
  723. hr = E_OUTOFMEMORY;
  724. _JumpError(hr, error, "LocalAlloc");
  725. }
  726. hr = RegQueryValueEx(
  727. hKey,
  728. wszREGCADESCRIPTION,
  729. NULL,
  730. &dwType,
  731. (BYTE *) pwszDescription,
  732. &cb);
  733. _JumpIfError(hr, error, "RegQueryValueEx");
  734. pwszDescription[cwc] = L'\0';
  735. }
  736. }
  737. }
  738. if (NULL != pwszDescription)
  739. {
  740. hr = mySanitizeName(pwszDescription, &pwsz);
  741. _JumpIfError(hr, error, "mySanitizeName");
  742. if (NULL != pCA->pwszSanitizedDescription)
  743. {
  744. LocalFree(pCA->pwszSanitizedDescription);
  745. }
  746. pCA->pwszSanitizedDescription = pwsz;
  747. }
  748. }
  749. if (fParentCA)
  750. {
  751. pccCA = pccCAChild;
  752. }
  753. else
  754. {
  755. DWORD ccert;
  756. CSASSERT(NULL == pccCAChild);
  757. hr = myGetCARegHashCount(
  758. pCA->pwszSanitizedName,
  759. CSRH_CASIGCERT,
  760. &ccert);
  761. _PrintIfError(hr, "myGetCARegHashCount");
  762. if (S_OK == hr && 0 != ccert)
  763. {
  764. DWORD NameId;
  765. // open MY store
  766. hMyStore = CertOpenStore(
  767. CERT_STORE_PROV_SYSTEM_W,
  768. X509_ASN_ENCODING,
  769. NULL, // hProv
  770. CERT_SYSTEM_STORE_LOCAL_MACHINE |
  771. CERT_STORE_READONLY_FLAG,
  772. wszMY_CERTSTORE);
  773. if (NULL == hMyStore)
  774. {
  775. hr = myHLastError();
  776. _JumpError(hr, error, "CertOpenStore");
  777. }
  778. hr = myFindCACertByHashIndex(
  779. hMyStore,
  780. pCA->pwszSanitizedName,
  781. CSRH_CASIGCERT,
  782. ccert - 1,
  783. &NameId,
  784. &pccCA);
  785. }
  786. }
  787. if (NULL != pccCA)
  788. {
  789. CERT_NAME_BLOB *pName = !fParentCA?
  790. &pccCA->pCertInfo->Subject :
  791. &pccCA->pCertInfo->Issuer;
  792. hr = ConfigLoadDNInfo(pName->pbData, pName->cbData, pCA);
  793. _PrintIfError(hr, "ConfigLoadDNInfo");
  794. }
  795. if (NULL == pCA->pwszSanitizedSignatureCertificate ||
  796. L'\0' == *pCA->pwszSanitizedSignatureCertificate)
  797. {
  798. DWORD cwc;
  799. // pwszDnsName + "_" + pwszSanitizedCAName + ".crt"
  800. cwc = wcslen(pwszDnsName) + 1 + wcslen(pwszSanitizedCAName) + 4;
  801. pwsz = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  802. if (NULL == pwsz)
  803. {
  804. hr = E_OUTOFMEMORY;
  805. _JumpError(hr, error, "LocalAlloc");
  806. }
  807. wcscpy(pwsz, pwszDnsName);
  808. wcscat(pwsz, L"_");
  809. wcscat(pwsz, pwszSanitizedCAName);
  810. wcscat(pwsz, L".crt");
  811. CSASSERT(wcslen(pwsz) == cwc);
  812. if (NULL != pCA->pwszSanitizedSignatureCertificate)
  813. {
  814. LocalFree(pCA->pwszSanitizedSignatureCertificate);
  815. }
  816. pCA->pwszSanitizedSignatureCertificate = pwsz;
  817. }
  818. if (!fFoundCA)
  819. {
  820. m_Count++;
  821. }
  822. if (NULL != pccCA && NULL != ppccCAOut)
  823. {
  824. *ppccCAOut = CertDuplicateCertificateContext(pccCA);
  825. }
  826. DBGPRINT((
  827. DBG_SS_CERTLIBI,
  828. "%ws %ws CA entry: %ws\n",
  829. fFoundCA? L"Merged" : L"Added",
  830. fParentCA? L"Parent" : L"Local",
  831. pCA->pwszSanitizedConfig));
  832. pCA = NULL;
  833. hr = S_OK;
  834. error:
  835. if (NULL != hKey)
  836. {
  837. RegCloseKey(hKey);
  838. }
  839. if (NULL != pwszRegPath)
  840. {
  841. LocalFree(pwszRegPath);
  842. }
  843. if (!fFoundCA && NULL != pCA)
  844. {
  845. _CleanupCAInfo(pCA);
  846. }
  847. if (NULL != pwszDescription)
  848. {
  849. LocalFree(pwszDescription);
  850. }
  851. if (NULL != pwszSanitizedShortName)
  852. {
  853. LocalFree(pwszSanitizedShortName);
  854. }
  855. if (NULL != pccCA && pccCAChild != pccCA)
  856. {
  857. CertFreeCertificateContext(pccCA);
  858. }
  859. if (NULL != hMyStore)
  860. {
  861. CertCloseStore(hMyStore, CERT_CLOSE_STORE_CHECK_FLAG);
  862. }
  863. return(hr);
  864. }
  865. //+--------------------------------------------------------------------------
  866. // CCertConfigPrivate::_LoadTable -- load config data into class instance
  867. //
  868. // Returns S_OK on success.
  869. //+--------------------------------------------------------------------------
  870. HRESULT
  871. CCertConfigPrivate::_LoadTable(VOID)
  872. {
  873. HRESULT hr;
  874. LONG Index;
  875. LONG CountShared = 0;
  876. LONG i;
  877. LONG actualSharedCount;
  878. CERT_AUTHORITY_INFO *pCAFromShared = NULL;
  879. BOOL fDSCA = TRUE;
  880. WCHAR *pwszSanitizedCAName = NULL;
  881. WCHAR *pwszDnsName = NULL;
  882. WCHAR *pwszOldName = NULL;
  883. CERT_CONTEXT const *pccCA = NULL;
  884. WCHAR *pwszParentMachine = NULL;
  885. WCHAR *pwszParentMachineOld = NULL;
  886. WCHAR *pwszParentCAName = NULL;
  887. WCHAR *pwszParentSanitizedCAName = NULL;
  888. m_Index = -1;
  889. m_Count = 0;
  890. if (m_fUseDS)
  891. {
  892. hr = dsGetCAConfig(&m_pCertAuthorityInfo, &m_Count);
  893. _JumpIfError(hr, error, "dsGetCAConfig");
  894. }
  895. DBGPRINT((
  896. DBG_SS_CERTLIBI,
  897. "%x DS entries @%x\n",
  898. m_Count,
  899. m_pCertAuthorityInfo));
  900. if (NULL == m_pCertAuthorityInfo && 0 == m_Count)
  901. {
  902. fDSCA = FALSE;
  903. }
  904. Index = 0;
  905. CountShared = INT_MAX;
  906. hr = certRequestGetConfigInfo(
  907. m_pwszSharedFolder,
  908. &Index,
  909. &CountShared,
  910. &pCAFromShared);
  911. // Don't fail if file not found or unc path is bad
  912. CSASSERT(S_OK == hr || FAILED(hr));
  913. _PrintIfError4(
  914. hr,
  915. "certRequestGetConfigInfo",
  916. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
  917. HRESULT_FROM_WIN32(ERROR_NETWORK_UNREACHABLE),
  918. HRESULT_FROM_WIN32(ERROR_INVALID_NETNAME));
  919. DBGPRINT((
  920. DBG_SS_CERTLIBI,
  921. "%x Shared Folder entries @%x\n",
  922. CountShared,
  923. pCAFromShared));
  924. actualSharedCount = 0;
  925. if (0 < CountShared)
  926. {
  927. hr = _ResizeCAInfo(m_Count + CountShared);
  928. _JumpIfError(hr, error, "_ResizeCAInfo");
  929. DBGPRINT((
  930. DBG_SS_CERTLIBI,
  931. "%x %ws @%x\n",
  932. m_Count + CountShared,
  933. fDSCA? L"Total entries" : L"new Shared Folder entries",
  934. m_pCertAuthorityInfo));
  935. for (i = 0; i < CountShared; ++i)
  936. {
  937. CERT_AUTHORITY_INFO *pCAInDS = NULL;
  938. CERT_AUTHORITY_INFO *pCAUpdate;
  939. if (fDSCA)
  940. {
  941. FindSharedCAInDSList(
  942. m_pCertAuthorityInfo,
  943. m_Count,
  944. &pCAFromShared[i],
  945. &pCAInDS);
  946. }
  947. if (NULL != pCAInDS)
  948. {
  949. pCAUpdate = pCAInDS;
  950. }
  951. else
  952. {
  953. pCAUpdate = &m_pCertAuthorityInfo[m_Count + actualSharedCount];
  954. ZeroMemory(pCAUpdate, sizeof(*pCAUpdate));
  955. }
  956. hr = mergeConfigFields(&pCAFromShared[i], pCAUpdate);
  957. _JumpIfError(hr, error, "mergeConfigFields");
  958. DBGPRINT((
  959. DBG_SS_CERTLIBI,
  960. "%hs CA entry: %ws\n",
  961. NULL != pCAInDS? "Merged" : "Added",
  962. pCAUpdate->pwszSanitizedConfig));
  963. if (NULL == pCAInDS)
  964. {
  965. ++actualSharedCount;
  966. }
  967. }
  968. }
  969. m_Count += actualSharedCount;
  970. // Add local CA to the table. Merge entry if it already exists.
  971. // Also flags all local CAs.
  972. hr = myGetCertRegStrValue(
  973. NULL,
  974. NULL,
  975. NULL,
  976. wszREGACTIVE,
  977. &pwszSanitizedCAName);
  978. _PrintIfErrorStr(hr, "myGetCertRegStrValue", wszREGACTIVE);
  979. if (S_OK == hr)
  980. {
  981. hr = myGetComputerNames(&pwszDnsName, &pwszOldName);
  982. _JumpIfError(hr, error, "myGetComputerNames");
  983. hr = _AddRegistryConfigEntry(
  984. pwszDnsName,
  985. pwszOldName,
  986. pwszSanitizedCAName,
  987. FALSE, // fParentCA
  988. NULL,
  989. &pccCA);
  990. _JumpIfError(hr, error, "_AddRegistryConfigEntry");
  991. // Add parent CA to the table. Merge entry if it already exists.
  992. hr = myGetCertRegStrValue(
  993. pwszSanitizedCAName,
  994. NULL,
  995. NULL,
  996. wszREGPARENTCANAME,
  997. &pwszParentCAName);
  998. _PrintIfErrorStr2(
  999. hr,
  1000. "myGetCertRegStrValue",
  1001. wszREGPARENTCANAME,
  1002. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
  1003. if (S_OK == hr)
  1004. {
  1005. hr = myGetCertRegStrValue(
  1006. pwszSanitizedCAName,
  1007. NULL,
  1008. NULL,
  1009. wszREGPARENTCAMACHINE,
  1010. &pwszParentMachine);
  1011. _PrintIfErrorStr(hr, "myGetCertRegStrValue", wszREGPARENTCAMACHINE);
  1012. }
  1013. if (NULL != pwszParentMachine && NULL != pwszParentCAName)
  1014. {
  1015. WCHAR *pwsz;
  1016. hr = mySanitizeName(pwszParentCAName, &pwszParentSanitizedCAName);
  1017. _JumpIfError(hr, error, "mySanitizeName");
  1018. hr = myDupString(pwszParentMachine, &pwszParentMachineOld);
  1019. _JumpIfError(hr, error, "myDupString");
  1020. pwsz = wcschr(pwszParentMachineOld, L'.');
  1021. if (NULL != pwsz)
  1022. {
  1023. *pwsz = L'\0';
  1024. }
  1025. hr = _AddRegistryConfigEntry(
  1026. pwszParentMachine,
  1027. pwszParentMachineOld,
  1028. pwszParentSanitizedCAName,
  1029. TRUE, // fParentCA
  1030. pccCA,
  1031. NULL);
  1032. _JumpIfError(hr, error, "_AddRegistryConfigEntry");
  1033. }
  1034. }
  1035. hr = S_OK;
  1036. error:
  1037. if (NULL != pwszDnsName)
  1038. {
  1039. LocalFree(pwszDnsName);
  1040. }
  1041. if (NULL != pwszOldName)
  1042. {
  1043. LocalFree(pwszOldName);
  1044. }
  1045. if (NULL != pwszSanitizedCAName)
  1046. {
  1047. LocalFree(pwszSanitizedCAName);
  1048. }
  1049. if (NULL != pwszParentCAName)
  1050. {
  1051. LocalFree(pwszParentCAName);
  1052. }
  1053. if (NULL != pwszParentMachine)
  1054. {
  1055. LocalFree(pwszParentMachine);
  1056. }
  1057. if (NULL != pwszParentMachineOld)
  1058. {
  1059. LocalFree(pwszParentMachineOld);
  1060. }
  1061. if (NULL != pwszParentSanitizedCAName)
  1062. {
  1063. LocalFree(pwszParentSanitizedCAName);
  1064. }
  1065. if (NULL != pCAFromShared)
  1066. {
  1067. LocalFree(pCAFromShared);
  1068. }
  1069. if (NULL != pccCA)
  1070. {
  1071. CertFreeCertificateContext(pccCA);
  1072. }
  1073. CSASSERT(S_OK == hr || FAILED(hr));
  1074. return(hr);
  1075. }
  1076. //+--------------------------------------------------------------------------
  1077. // CCertConfigPrivate::Reset -- load config data, reset to indexed entry,
  1078. // return count
  1079. //
  1080. // Load the configuration data if not already loaded. To reload the data after
  1081. // the data have changed, CCertConfigPrivate must be released and reloaded.
  1082. //
  1083. // Resets the current config entry to the Certification Authority configuration
  1084. // listed in the configuration file, indexed by the Index parameter. 0 indexes
  1085. // the first configuration.
  1086. //
  1087. // Upon successful completion, *pCount will be set to the number of Certificate
  1088. // Authority configurations listed in the configuration file.
  1089. //
  1090. // Returns S_FALSE if no entries are available at or after the passed Index.
  1091. // Returns S_OK on success.
  1092. //+--------------------------------------------------------------------------
  1093. HRESULT
  1094. CCertConfigPrivate::Reset(
  1095. /* [in] */ LONG Index,
  1096. /* [retval][out] */ LONG __RPC_FAR *pCount)
  1097. {
  1098. HRESULT hr = S_OK;
  1099. if (NULL == pCount)
  1100. {
  1101. hr = E_POINTER;
  1102. _JumpError(hr, error, "pCount");
  1103. }
  1104. if (NULL == m_pCertAuthorityInfo)
  1105. {
  1106. hr = _LoadTable();
  1107. _JumpIfError(hr, error, "_LoadTable");
  1108. }
  1109. *pCount = m_Count;
  1110. if (0 > Index || Index > m_Count)
  1111. {
  1112. Index = m_Count + 1;
  1113. hr = S_FALSE;
  1114. }
  1115. m_Index = Index - 1;
  1116. error:
  1117. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  1118. return(hr);
  1119. }
  1120. //+--------------------------------------------------------------------------
  1121. // CCertConfigPrivate::Next -- skip to next config entry
  1122. //
  1123. // Changes the current config entry to the next Certification Authority
  1124. // configuration listed in the configuration file.
  1125. //
  1126. // Upon successful completion, *pIndex will be set to the index of Certificate
  1127. // Authority configurations listed in the configuration file.
  1128. //
  1129. // Returns S_FALSE if no more entries are available. *pIndex is set to -1.
  1130. // Returns S_OK on success. *pIndex is set to index the current configuration.
  1131. //+--------------------------------------------------------------------------
  1132. HRESULT
  1133. CCertConfigPrivate::Next(
  1134. /* [retval][out] */ LONG __RPC_FAR *pIndex)
  1135. {
  1136. HRESULT hr = S_OK;
  1137. if (NULL == pIndex)
  1138. {
  1139. hr = E_POINTER;
  1140. _JumpError(hr, error, "pIndex");
  1141. }
  1142. *pIndex = -1;
  1143. if (NULL == m_pCertAuthorityInfo)
  1144. {
  1145. hr = _LoadTable();
  1146. _JumpIfError(hr, error, "_LoadTable");
  1147. }
  1148. if (m_Index < m_Count)
  1149. {
  1150. m_Index++;
  1151. }
  1152. if (m_Index < m_Count)
  1153. {
  1154. *pIndex = m_Index;
  1155. }
  1156. else
  1157. {
  1158. hr = S_FALSE;
  1159. }
  1160. error:
  1161. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  1162. return(hr);
  1163. }
  1164. //+--------------------------------------------------------------------------
  1165. // CCertConfigPrivate::GetField -- return a field from the current config entry.
  1166. //
  1167. // pstrOut points to a BSTR string filled in by this routine. If *pstrOut is
  1168. // non-NULL and this method is successful, the old string is freed. If any
  1169. // value other than S_OK is returned, the string pointer will not be modified.
  1170. //
  1171. // Upon successful completion, *pstrOut will point to a string that contains
  1172. // the requested field from the current config entry.
  1173. //
  1174. // When the caller no longer needs the string, it must be freed by calling
  1175. // SysFreeString().
  1176. //
  1177. // Returns S_OK on success.
  1178. //+--------------------------------------------------------------------------
  1179. HRESULT
  1180. CCertConfigPrivate::GetField(
  1181. /* [in] */ BSTR const strFieldName,
  1182. /* [retval][out] */ BSTR __RPC_FAR *pstrOut)
  1183. {
  1184. HRESULT hr = S_OK;
  1185. CERT_AUTHORITY_INFO *pcai;
  1186. BSTR bstr;
  1187. DWORD cwc;
  1188. WCHAR *pwsz = NULL;
  1189. WCHAR *pwszRevert = NULL;
  1190. WCHAR *pwszT = NULL;
  1191. BOOL fDesanitize = TRUE;
  1192. WCHAR awc[12];
  1193. if (NULL == pstrOut || NULL == strFieldName)
  1194. {
  1195. hr = E_POINTER;
  1196. _JumpError(hr, error, "pstrOut|strFieldName");
  1197. }
  1198. if (NULL != *pstrOut)
  1199. {
  1200. SysFreeString(*pstrOut);
  1201. *pstrOut = NULL;
  1202. }
  1203. if (NULL == m_pCertAuthorityInfo)
  1204. {
  1205. hr = _LoadTable();
  1206. _JumpIfError(hr, error, "_LoadTable");
  1207. }
  1208. if (-1 == m_Index)
  1209. {
  1210. m_Index++; // implicit Next() for compatibility with Version 1
  1211. }
  1212. if (0 > m_Index || m_Index >= m_Count)
  1213. {
  1214. hr = E_INVALIDARG;
  1215. _JumpError(hr, error, "m_Index");
  1216. }
  1217. pcai = &m_pCertAuthorityInfo[m_Index];
  1218. bstr = strFieldName;
  1219. if (0 == lstrcmpi(bstr, wszCONFIG_COMMONNAME))
  1220. {
  1221. pwsz = pcai->pwszSanitizedName;
  1222. }
  1223. else
  1224. if (0 == lstrcmpi(bstr, wszCONFIG_SANITIZEDNAME))
  1225. {
  1226. pwsz = pcai->pwszSanitizedName;
  1227. fDesanitize = FALSE;
  1228. }
  1229. else
  1230. if (0 == lstrcmpi(bstr, wszCONFIG_SHORTNAME))
  1231. {
  1232. pwsz = pcai->pwszSanitizedShortName;
  1233. }
  1234. else
  1235. if (0 == lstrcmpi(bstr, wszCONFIG_SANITIZEDSHORTNAME))
  1236. {
  1237. pwsz = pcai->pwszSanitizedShortName;
  1238. fDesanitize = FALSE;
  1239. }
  1240. else
  1241. if (0 == lstrcmpi(bstr, wszCONFIG_ORGUNIT))
  1242. {
  1243. pwsz = pcai->pwszSanitizedOrgUnit;
  1244. }
  1245. else
  1246. if (0 == lstrcmpi(bstr, wszCONFIG_ORGANIZATION))
  1247. {
  1248. pwsz = pcai->pwszSanitizedOrganization;
  1249. }
  1250. else
  1251. if (0 == lstrcmpi(bstr, wszCONFIG_LOCALITY))
  1252. {
  1253. pwsz = pcai->pwszSanitizedLocality;
  1254. }
  1255. else
  1256. if (0 == lstrcmpi(bstr, wszCONFIG_STATE))
  1257. {
  1258. pwsz = pcai->pwszSanitizedState;
  1259. }
  1260. else
  1261. if (0 == lstrcmpi(bstr, wszCONFIG_COUNTRY))
  1262. {
  1263. pwsz = pcai->pwszSanitizedCountry;
  1264. }
  1265. else
  1266. if (0 == lstrcmpi(bstr, wszCONFIG_CONFIG))
  1267. {
  1268. pwsz = pcai->pwszSanitizedConfig;
  1269. }
  1270. else
  1271. if (0 == lstrcmpi(bstr, wszCONFIG_EXCHANGECERTIFICATE))
  1272. {
  1273. pwsz = pcai->pwszSanitizedExchangeCertificate;
  1274. fDesanitize = FALSE;
  1275. }
  1276. else
  1277. if (0 == lstrcmpi(bstr, wszCONFIG_SIGNATURECERTIFICATE))
  1278. {
  1279. pwsz = pcai->pwszSanitizedSignatureCertificate;
  1280. fDesanitize = FALSE;
  1281. }
  1282. else
  1283. if (0 == lstrcmpi(bstr, wszCONFIG_DESCRIPTION) ||
  1284. 0 == lstrcmpi(bstr, wszCONFIG_COMMENT)) // obsolete
  1285. {
  1286. pwsz = pcai->pwszSanitizedDescription;
  1287. }
  1288. else
  1289. if (0 == lstrcmpi(bstr, wszCONFIG_SERVER))
  1290. {
  1291. pwsz = wcschr(pcai->pwszSanitizedConfig, L'\\');
  1292. if (NULL == pwsz)
  1293. {
  1294. cwc = wcslen(pcai->pwszSanitizedConfig);
  1295. }
  1296. else
  1297. {
  1298. cwc = SAFE_SUBTRACT_POINTERS(pwsz, pcai->pwszSanitizedConfig);
  1299. }
  1300. pwszT = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  1301. if (NULL == pwszT)
  1302. {
  1303. hr = E_OUTOFMEMORY;
  1304. _JumpError(hr, error, "LocalAlloc");
  1305. }
  1306. CopyMemory(pwszT, pcai->pwszSanitizedConfig, cwc * sizeof(WCHAR));
  1307. pwszT[cwc] = L'\0';
  1308. pwsz = pwszT;
  1309. }
  1310. else
  1311. if (0 == lstrcmpi(bstr, wszCONFIG_AUTHORITY))
  1312. {
  1313. pwsz = wcschr(pcai->pwszSanitizedConfig, L'\\');
  1314. if (NULL == pwsz)
  1315. {
  1316. pwsz = L"";
  1317. }
  1318. else
  1319. {
  1320. pwsz++;
  1321. }
  1322. }
  1323. else
  1324. if (0 == lstrcmpi(bstr, wszCONFIG_FLAGS))
  1325. {
  1326. wsprintf(awc, L"%u", pcai->Flags);
  1327. CSASSERT(ARRAYSIZE(awc) > wcslen(awc));
  1328. pwsz = awc;
  1329. }
  1330. if (NULL == pwsz)
  1331. {
  1332. hr = CERTSRV_E_PROPERTY_EMPTY;
  1333. _JumpError2(hr, error, "ConvertWszToBstr", CERTSRV_E_PROPERTY_EMPTY);
  1334. }
  1335. if (fDesanitize)
  1336. {
  1337. hr = myRevertSanitizeName(pwsz, &pwszRevert);
  1338. _JumpIfError(hr, error, "myRevertSanitizeName");
  1339. pwsz = pwszRevert;
  1340. }
  1341. if (!ConvertWszToBstr(pstrOut, pwsz, -1))
  1342. {
  1343. hr = E_OUTOFMEMORY;
  1344. _JumpError(hr, error, "ConvertWszToBstr");
  1345. }
  1346. error:
  1347. if (NULL != pwszT)
  1348. {
  1349. LocalFree(pwszT);
  1350. }
  1351. if (NULL != pwszRevert)
  1352. {
  1353. LocalFree(pwszRevert);
  1354. }
  1355. CSASSERT(S_OK == hr || FAILED(hr));
  1356. return(hr);
  1357. }
  1358. HRESULT
  1359. CCertConfigPrivate::GetConfigFromPicker(
  1360. OPTIONAL IN HWND hwndParent,
  1361. OPTIONAL IN WCHAR const *pwszPrompt,
  1362. OPTIONAL IN WCHAR const *pwszTitle,
  1363. OPTIONAL IN WCHAR const *pwszSharedFolder,
  1364. IN BOOL fUseDS,
  1365. IN BOOL fCountOnly,
  1366. OUT DWORD *pdwCount,
  1367. OUT CRYPTUI_CA_CONTEXT const **ppCAContext)
  1368. {
  1369. HRESULT hr = S_OK;
  1370. CRYPTUI_CA_CONTEXT *prgCAC = NULL;
  1371. const CRYPTUI_CA_CONTEXT **pprgCAC = NULL;
  1372. CRYPTUI_SELECT_CA_STRUCT UISelectCA;
  1373. CRYPTUI_CA_CONTEXT const *pCAContext = NULL;
  1374. LONG i;
  1375. LONG realCount = 0;
  1376. LONG cCA = 0;
  1377. BSTR bsFieldName = NULL;
  1378. BSTR bsFieldValue = NULL;
  1379. WCHAR *pwszPtr;
  1380. WCHAR **ppwszCAName = NULL;
  1381. WCHAR **ppwszCAMachineName = NULL;
  1382. if ((NULL == ppCAContext) || (NULL == pdwCount))
  1383. {
  1384. hr = E_POINTER;
  1385. _JumpError (hr, error, "NULL parm");
  1386. }
  1387. *ppCAContext = NULL;
  1388. *pdwCount = 0;
  1389. m_fUseDS = fUseDS;
  1390. if (NULL != pwszSharedFolder)
  1391. {
  1392. hr = SetSharedFolder(const_cast<WCHAR *>(pwszSharedFolder));
  1393. _JumpIfError(hr, error, "configPrivate.SetSharedFolder");
  1394. }
  1395. hr = Reset(0, &cCA);
  1396. if (S_OK != hr && S_FALSE != hr)
  1397. {
  1398. _JumpError(hr, error, "configPrivate.Reset");
  1399. }
  1400. hr = S_OK; // in case of S_FALSE;
  1401. if (0 < cCA)
  1402. {
  1403. prgCAC = (CRYPTUI_CA_CONTEXT *) LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT,
  1404. cCA * sizeof(CRYPTUI_CA_CONTEXT));
  1405. if (NULL == prgCAC)
  1406. {
  1407. hr = E_OUTOFMEMORY;
  1408. _JumpError(hr, error, "LocalAlloc");
  1409. }
  1410. pprgCAC = (const CRYPTUI_CA_CONTEXT **) LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT,
  1411. cCA * sizeof(const CRYPTUI_CA_CONTEXT *));
  1412. if (NULL == pprgCAC)
  1413. {
  1414. hr = E_OUTOFMEMORY;
  1415. _JumpError(hr, error, "LocalAlloc");
  1416. }
  1417. ppwszCAName = (WCHAR **) LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT, cCA * sizeof(WCHAR *));
  1418. if (NULL == ppwszCAName)
  1419. {
  1420. hr = E_OUTOFMEMORY;
  1421. _JumpError(hr, error, "LocalAlloc");
  1422. }
  1423. ppwszCAMachineName = (WCHAR **) LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT, cCA * sizeof(WCHAR *));
  1424. if (NULL == ppwszCAMachineName)
  1425. {
  1426. hr = E_OUTOFMEMORY;
  1427. _JumpError(hr, error, "LocalAlloc");
  1428. }
  1429. if (!ConvertWszToBstr(&bsFieldName, wszCONFIG_CONFIG, -1))
  1430. {
  1431. hr = E_OUTOFMEMORY;
  1432. _JumpError(hr, error, "ConvertWszToBstr");
  1433. }
  1434. realCount = 0;
  1435. while (realCount < cCA)
  1436. {
  1437. hr = GetField(bsFieldName, &bsFieldValue);
  1438. _JumpIfError(hr, error, "configPrivate.GetField");
  1439. pwszPtr = wcsstr(bsFieldValue, L"\\");
  1440. if (NULL == pwszPtr)
  1441. {
  1442. continue;
  1443. }
  1444. // change \ to null terminator
  1445. pwszPtr[0] = L'\0';
  1446. // point to CA name
  1447. pwszPtr = &pwszPtr[1];
  1448. ppwszCAName[realCount] = (WCHAR *) LocalAlloc(
  1449. LMEM_FIXED,
  1450. (wcslen(pwszPtr) + 1) * sizeof(WCHAR));
  1451. if (NULL == ppwszCAName[realCount])
  1452. {
  1453. hr = E_OUTOFMEMORY;
  1454. _JumpError(hr, error, "LocalAlloc");
  1455. }
  1456. ppwszCAMachineName[realCount] = (WCHAR *) LocalAlloc(
  1457. LMEM_FIXED,
  1458. (wcslen(bsFieldValue) + 1) * sizeof(WCHAR));
  1459. if (NULL == ppwszCAMachineName[realCount])
  1460. {
  1461. hr = E_OUTOFMEMORY;
  1462. _JumpError(hr, error, "LocalAlloc");
  1463. }
  1464. prgCAC[realCount].dwSize = sizeof(CRYPTUI_CA_CONTEXT);
  1465. wcscpy(ppwszCAName[realCount], pwszPtr);
  1466. wcscpy(ppwszCAMachineName[realCount], bsFieldValue);
  1467. prgCAC[realCount].pwszCAName = ppwszCAName[realCount];
  1468. prgCAC[realCount].pwszCAMachineName = ppwszCAMachineName[realCount];
  1469. ++realCount;
  1470. hr = Next(&i);
  1471. if (S_OK != hr)
  1472. {
  1473. if (S_FALSE == hr)
  1474. {
  1475. hr = S_OK; // no more entry is not an error
  1476. }
  1477. break;
  1478. }
  1479. if (NULL != bsFieldValue)
  1480. {
  1481. SysFreeString(bsFieldValue);
  1482. bsFieldValue = NULL;
  1483. }
  1484. }
  1485. for (i = 0; i < realCount; ++i)
  1486. {
  1487. pprgCAC[i] = &prgCAC[i];
  1488. }
  1489. }
  1490. *pdwCount = realCount;
  1491. if (fCountOnly || 1 > realCount)
  1492. {
  1493. // done
  1494. goto error; // normal return
  1495. }
  1496. ZeroMemory(&UISelectCA, sizeof(CRYPTUI_SELECT_CA_STRUCT));
  1497. UISelectCA.cCAContext = realCount;
  1498. UISelectCA.rgCAContext = pprgCAC;
  1499. UISelectCA.dwSize = sizeof(UISelectCA);
  1500. UISelectCA.wszDisplayString = pwszPrompt;
  1501. UISelectCA.wszTitle = pwszTitle;
  1502. UISelectCA.hwndParent = hwndParent;
  1503. // cryptui called via delay load, wrap with try/except
  1504. __try
  1505. {
  1506. // invoke dialog
  1507. pCAContext = (CRYPTUI_CA_CONTEXT const *) CryptUIDlgSelectCA(&UISelectCA); // DelayLoad Wrapped
  1508. if (NULL == pCAContext) // user cancel?
  1509. {
  1510. hr = myHLastError();
  1511. if (S_OK == hr)
  1512. {
  1513. hr = HRESULT_FROM_WIN32(ERROR_CANCELLED);
  1514. }
  1515. _JumpError(hr, error, "CryptUIDlgSelectCA");
  1516. }
  1517. *ppCAContext = pCAContext;
  1518. hr = S_OK;
  1519. }
  1520. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1521. {
  1522. }
  1523. error:
  1524. // change to default
  1525. m_fUseDS = TRUE;
  1526. if (NULL != ppwszCAName)
  1527. {
  1528. for (i = 0; i < realCount; ++i)
  1529. {
  1530. if (NULL != ppwszCAName[i])
  1531. {
  1532. LocalFree(ppwszCAName[i]);
  1533. }
  1534. }
  1535. LocalFree(ppwszCAName);
  1536. }
  1537. if (NULL != ppwszCAMachineName)
  1538. {
  1539. for (i = 0; i < realCount; ++i)
  1540. {
  1541. if (NULL != ppwszCAMachineName[i])
  1542. {
  1543. LocalFree(ppwszCAMachineName[i]);
  1544. }
  1545. }
  1546. LocalFree(ppwszCAMachineName);
  1547. }
  1548. if (NULL != pprgCAC)
  1549. {
  1550. LocalFree(pprgCAC);
  1551. }
  1552. if (NULL != prgCAC)
  1553. {
  1554. LocalFree(prgCAC);
  1555. }
  1556. if (NULL != bsFieldName)
  1557. {
  1558. SysFreeString(bsFieldName);
  1559. }
  1560. if (NULL != bsFieldValue)
  1561. {
  1562. SysFreeString(bsFieldValue);
  1563. }
  1564. CSASSERT(S_OK == hr || FAILED(hr));
  1565. return(hr);
  1566. }
  1567. //+--------------------------------------------------------------------------
  1568. // CCertConfigPrivate::GetConfig -- select a cert issuer, return config data.
  1569. //
  1570. // pstrOut points to a BSTR string filled in by this routine. If *pstrOut is
  1571. // non-NULL and this method is successful, the old string is freed. If any
  1572. // value other than S_OK is returned, the string pointer will not be modified.
  1573. //
  1574. // Flags must be set to 0.
  1575. //
  1576. // Upon successful completion, *pstrOut will point to a string that contains
  1577. // the server name and Certification Authority name.
  1578. //
  1579. // When the caller no longer needs the string, it must be freed by calling
  1580. // SysFreeString().
  1581. //
  1582. // Returns S_OK on success.
  1583. //+--------------------------------------------------------------------------
  1584. HRESULT
  1585. CCertConfigPrivate::GetConfig(
  1586. /* [in] */ LONG Flags,
  1587. /* [retval][out] */ BSTR __RPC_FAR *pstrOut)
  1588. {
  1589. HRESULT hr = S_OK;
  1590. WCHAR *pwszSanitizedConfig = NULL;
  1591. WCHAR *pwszConfigRevert = NULL;
  1592. WCHAR *pwszSanitize = NULL;
  1593. HMODULE hMod = NULL;
  1594. CRYPTUI_CA_CONTEXT const *pCAContext = NULL;
  1595. DWORD dwCACount;
  1596. LONG lIndex;
  1597. LONG cCA;
  1598. LONG i;
  1599. if (NULL == pstrOut)
  1600. {
  1601. hr = E_POINTER;
  1602. _JumpError(hr, error, "pstrOut");
  1603. }
  1604. if (NULL != *pstrOut)
  1605. {
  1606. SysFreeString(*pstrOut);
  1607. *pstrOut = NULL;
  1608. }
  1609. if (NULL == m_pCertAuthorityInfo)
  1610. {
  1611. hr = _LoadTable();
  1612. _JumpIfError(hr, error, "_LoadTable");
  1613. }
  1614. if (0 > m_Count)
  1615. {
  1616. hr = E_INVALIDARG;
  1617. _JumpError(hr, error, "m_Count");
  1618. }
  1619. switch (Flags)
  1620. {
  1621. case CC_UIPICKCONFIG:
  1622. hMod = LoadLibrary(L"cryptui.dll");
  1623. if (NULL != hMod)
  1624. {
  1625. hr = GetConfigFromPicker(
  1626. GetDesktopWindow(), // hDlg
  1627. NULL, // pwszPrompt
  1628. NULL, // pwszTitle
  1629. m_pwszSharedFolder,
  1630. TRUE, // fUseDS
  1631. FALSE, // fCountOnly
  1632. &dwCACount,
  1633. &pCAContext);
  1634. _JumpIfError(hr, error, "GetConfigFromPicker");
  1635. if (NULL == pCAContext)
  1636. {
  1637. hr = HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
  1638. _JumpError(hr, error, "No CA Config");
  1639. }
  1640. // ca name in UI has been reverted so sanitize it
  1641. hr = mySanitizeName(pCAContext->pwszCAName, &pwszSanitize);
  1642. _JumpIfError(hr, error, "mySanitizeName");
  1643. pwszSanitizedConfig = (WCHAR *) LocalAlloc(
  1644. LMEM_FIXED,
  1645. (wcslen(pwszSanitize) +
  1646. wcslen(pCAContext->pwszCAMachineName) + 2) *
  1647. sizeof(WCHAR));
  1648. if (NULL == pwszSanitizedConfig)
  1649. {
  1650. hr = E_OUTOFMEMORY;
  1651. _JumpError(hr, error, "LocalAlloc");
  1652. }
  1653. wcscpy(pwszSanitizedConfig, pCAContext->pwszCAMachineName);
  1654. wcscat(pwszSanitizedConfig, L"\\");
  1655. wcscat(pwszSanitizedConfig, pwszSanitize);
  1656. for (lIndex = 0; lIndex < m_Count; ++lIndex)
  1657. {
  1658. if (0 == lstrcmpi(pwszSanitizedConfig, m_pCertAuthorityInfo[lIndex].pwszSanitizedConfig))
  1659. {
  1660. // update index
  1661. Reset(lIndex + 1, &cCA);
  1662. break;
  1663. }
  1664. }
  1665. break;
  1666. }
  1667. // FALLTHROUGH
  1668. case CC_LOCALCONFIG:
  1669. case CC_LOCALACTIVECONFIG:
  1670. case CC_DEFAULTCONFIG:
  1671. if (NULL == m_pCertAuthorityInfo)
  1672. {
  1673. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  1674. _JumpError(hr, error, "no CAs");
  1675. }
  1676. if (CC_LOCALCONFIG != Flags && CC_LOCALACTIVECONFIG != Flags)
  1677. {
  1678. // Make a recursive call to first look for a local pingable CA.
  1679. // If that fails, pick any pingable CA.
  1680. hr = GetConfig(CC_LOCALACTIVECONFIG, pstrOut);
  1681. if (S_OK == hr)
  1682. {
  1683. break;
  1684. }
  1685. _PrintError(hr, "GetConfig(CC_LOCALACTIVECONFIG)");
  1686. }
  1687. for (i = 0; i < m_Count; i++)
  1688. {
  1689. CSASSERT(NULL != m_pCertAuthorityInfo[i].pwszSanitizedConfig);
  1690. pwszSanitizedConfig = m_pCertAuthorityInfo[i].pwszSanitizedConfig;
  1691. if (CC_LOCALCONFIG == Flags || CC_LOCALACTIVECONFIG == Flags)
  1692. {
  1693. DWORD InfoFlags;
  1694. InfoFlags = m_pCertAuthorityInfo[i].Flags;
  1695. if (0 == (CAIF_LOCAL & InfoFlags))
  1696. {
  1697. continue;
  1698. }
  1699. if (0 == (CAIF_REGISTRY & InfoFlags))
  1700. {
  1701. LONG j;
  1702. for (j = i + 1; j < m_Count; j++)
  1703. {
  1704. InfoFlags = m_pCertAuthorityInfo[j].Flags;
  1705. if ((CAIF_LOCAL & InfoFlags) &&
  1706. (CAIF_REGISTRY & InfoFlags))
  1707. {
  1708. break;
  1709. }
  1710. }
  1711. if (j < m_Count)
  1712. {
  1713. i = j;
  1714. }
  1715. }
  1716. }
  1717. if (CC_LOCALCONFIG == Flags)
  1718. {
  1719. hr = S_OK;
  1720. }
  1721. else
  1722. {
  1723. hr = myRevertSanitizeName(pwszSanitizedConfig, &pwszConfigRevert);
  1724. _JumpIfError(hr, error, "myRevertSanitizeName");
  1725. hr = myPingCertSrv(
  1726. pwszConfigRevert,
  1727. NULL,
  1728. NULL,
  1729. NULL,
  1730. NULL,
  1731. NULL,
  1732. NULL);
  1733. }
  1734. if (S_OK == hr)
  1735. {
  1736. // update index
  1737. Reset(i + 1, &cCA);
  1738. break; // take it
  1739. }
  1740. if (NULL != pwszConfigRevert)
  1741. {
  1742. LocalFree(pwszConfigRevert);
  1743. pwszConfigRevert = NULL;
  1744. }
  1745. }
  1746. if (S_OK != hr || i >= m_Count)
  1747. {
  1748. hr = HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
  1749. _JumpError(hr, error, "No matching CA");
  1750. }
  1751. break;
  1752. case CC_FIRSTCONFIG:
  1753. if (NULL == m_pCertAuthorityInfo)
  1754. {
  1755. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  1756. _JumpError(hr, error, "no CAs");
  1757. }
  1758. pwszSanitizedConfig = m_pCertAuthorityInfo[0].pwszSanitizedConfig;
  1759. // update index
  1760. Reset(1, &cCA);
  1761. break;
  1762. default:
  1763. hr = E_INVALIDARG;
  1764. _JumpError(hr, error, "Flags");
  1765. }
  1766. if(!*pstrOut)
  1767. {
  1768. if (NULL == pwszConfigRevert)
  1769. {
  1770. // always revert
  1771. hr = myRevertSanitizeName(pwszSanitizedConfig, &pwszConfigRevert);
  1772. _JumpIfError(hr, error, "myRevertSanitizeName");
  1773. }
  1774. if (!ConvertWszToBstr(pstrOut, pwszConfigRevert, -1))
  1775. {
  1776. hr = E_OUTOFMEMORY;
  1777. _JumpError(hr, error, "ConvertWszToBstr");
  1778. }
  1779. }
  1780. hr = S_OK;
  1781. error:
  1782. if (NULL != hMod)
  1783. {
  1784. FreeLibrary(hMod);
  1785. if (NULL != pwszSanitizedConfig)
  1786. {
  1787. LocalFree(pwszSanitizedConfig);
  1788. }
  1789. }
  1790. if (NULL != pwszConfigRevert)
  1791. {
  1792. LocalFree(pwszConfigRevert);
  1793. }
  1794. if (NULL != pwszSanitize)
  1795. {
  1796. LocalFree(pwszSanitize);
  1797. }
  1798. if (NULL != pCAContext)
  1799. {
  1800. CryptUIDlgFreeCAContext(pCAContext);
  1801. }
  1802. CSASSERT(S_OK == hr || FAILED(hr));
  1803. return(hr);
  1804. }
  1805. //+--------------------------------------------------------------------------
  1806. // CCertConfigPrivate::SetSharedFolder -- set the shared folder
  1807. //
  1808. // strSharedFolder is the new shared folder dorectory path.
  1809. //
  1810. // Returns S_OK on success.
  1811. //+--------------------------------------------------------------------------
  1812. HRESULT
  1813. CCertConfigPrivate::SetSharedFolder(
  1814. /* [in] */ const BSTR strSharedFolder)
  1815. {
  1816. HRESULT hr;
  1817. WCHAR *pwsz;
  1818. pwsz = NULL;
  1819. if (NULL != strSharedFolder)
  1820. {
  1821. hr = myDupString(strSharedFolder, &pwsz);
  1822. _JumpIfError(hr, error, "myDupString");
  1823. }
  1824. if (NULL != m_pwszSharedFolder)
  1825. {
  1826. LocalFree(m_pwszSharedFolder);
  1827. }
  1828. m_pwszSharedFolder = pwsz;
  1829. hr = S_OK;
  1830. error:
  1831. CSASSERT(S_OK == hr || FAILED(hr));
  1832. return(hr);
  1833. }
  1834. HRESULT WINAPI
  1835. certLoadFile(
  1836. WCHAR const *pwszfn,
  1837. BYTE **ppbData,
  1838. DWORD *pcbData)
  1839. {
  1840. HANDLE hFile;
  1841. DWORD cbLow, cbHigh, cbRead;
  1842. HRESULT hr;
  1843. hFile = CreateFile(
  1844. pwszfn,
  1845. GENERIC_READ,
  1846. FILE_SHARE_READ,
  1847. NULL,
  1848. OPEN_EXISTING,
  1849. FILE_ATTRIBUTE_NORMAL,
  1850. 0);
  1851. if (INVALID_HANDLE_VALUE == hFile)
  1852. {
  1853. hr = myHLastError();
  1854. _JumpErrorStr(hr, error, "CreateFile", pwszfn);
  1855. }
  1856. cbLow = GetFileSize(hFile, &cbHigh);
  1857. if (0xffffffff == cbLow)
  1858. {
  1859. hr = myHLastError();
  1860. _JumpError(hr, error, "GetFileSize");
  1861. }
  1862. *ppbData = (BYTE *) LocalAlloc(LMEM_FIXED, cbLow + 1);
  1863. if (NULL == *ppbData)
  1864. {
  1865. hr = E_OUTOFMEMORY;
  1866. _JumpError(hr, error, "LocalAlloc");
  1867. }
  1868. if (!ReadFile(hFile, *ppbData, cbLow, &cbRead, NULL))
  1869. {
  1870. hr = myHLastError();
  1871. LocalFree(*ppbData);
  1872. *ppbData = NULL;
  1873. _JumpError(hr, error, "ReadFile");
  1874. }
  1875. (*ppbData)[cbLow] = '\0';
  1876. *pcbData = cbLow;
  1877. hr = S_OK;
  1878. error:
  1879. if (INVALID_HANDLE_VALUE != hFile)
  1880. {
  1881. CloseHandle(hFile);
  1882. }
  1883. CSASSERT(S_OK == hr || FAILED(hr));
  1884. return(hr);
  1885. }
  1886. // certTrimToken -- trim white space from the beginning and end of a line.
  1887. // Bump caller's string pointer, reduce the caller's input buffer count,
  1888. // and L'\0' terminate the result -- IN PLACE!
  1889. VOID
  1890. certTrimToken(
  1891. WCHAR **ppwsz)
  1892. {
  1893. WCHAR *pwsz;
  1894. WCHAR *pwszEnd;
  1895. static WCHAR wszTrim[] = L" \t\r\n";
  1896. pwsz = *ppwsz;
  1897. while (L'\0' != *pwsz)
  1898. {
  1899. if (NULL == wcschr(wszTrim, *pwsz))
  1900. {
  1901. break;
  1902. }
  1903. pwsz++;
  1904. }
  1905. pwszEnd = wcschr(pwsz, L'\0');
  1906. assert(NULL != pwszEnd);
  1907. while (pwsz < pwszEnd)
  1908. {
  1909. if (NULL == wcschr(wszTrim, *--pwszEnd))
  1910. {
  1911. break;
  1912. }
  1913. *pwszEnd = L'\0';
  1914. }
  1915. *ppwsz = pwsz;
  1916. }
  1917. BOOL
  1918. certExtractToken(
  1919. WCHAR **ppwszIn,
  1920. WCHAR const **ppwszOut)
  1921. {
  1922. WCHAR *pwsz;
  1923. WCHAR *pwszNext;
  1924. WCHAR *pwsz2;
  1925. BOOL fQuoted = FALSE;
  1926. pwsz = *ppwszIn;
  1927. while (L' ' == *pwsz || L'\t' == *pwsz)
  1928. {
  1929. pwsz++;
  1930. }
  1931. pwszNext = pwsz;
  1932. if (L'"' == *pwsz)
  1933. {
  1934. fQuoted = TRUE;
  1935. pwsz++;
  1936. pwszNext++;
  1937. while (TRUE)
  1938. {
  1939. // Find a mid-string escaped quote or the terminating quote.
  1940. pwszNext = wcschr(pwszNext, L'"');
  1941. if (NULL == pwszNext)
  1942. {
  1943. break; // missing terminating quote!
  1944. }
  1945. // If a terminating quote, terminate the string with L'\0',
  1946. // and point past the quoted string. Break to search for a comma.
  1947. if (L'"' != pwszNext[1])
  1948. {
  1949. *pwszNext++ = L'\0';
  1950. break;
  1951. }
  1952. // Found a mid-string escaped quote. Move the first part of the
  1953. // string forward one character position, overwriting the first
  1954. // quote character. Bump the string pointer to the new location,
  1955. // point pwszNext past the remaining quote character, and loop.
  1956. MoveMemory(
  1957. &pwsz[1],
  1958. pwsz,
  1959. SAFE_SUBTRACT_POINTERS(pwszNext, pwsz) * sizeof(*pwsz));
  1960. pwsz++;
  1961. pwszNext += 2;
  1962. }
  1963. }
  1964. if (NULL != pwszNext)
  1965. {
  1966. pwszNext = wcschr(pwszNext, L',');
  1967. }
  1968. if (NULL != pwszNext)
  1969. {
  1970. *pwszNext++ = L'\0';
  1971. }
  1972. else
  1973. {
  1974. pwszNext = wcschr(pwsz, L'\0');
  1975. assert(NULL != pwszNext);
  1976. }
  1977. certTrimToken(&pwsz);
  1978. *ppwszOut = pwsz;
  1979. *ppwszIn = pwszNext;
  1980. return(fQuoted || L'\0' != *pwsz);
  1981. }
  1982. // certExtractLine -- convert one line to Unicode and return it in a L'\0'
  1983. // terminated output buffer. Bump caller's input buffer pointer and reduce
  1984. // the caller's input buffer count.
  1985. HRESULT
  1986. certExtractLine(
  1987. IN char const **ppchLine,
  1988. IN OUT LONG *pcchLine,
  1989. OUT WCHAR *pwc,
  1990. IN OUT LONG *pcwc)
  1991. {
  1992. char const *pch;
  1993. char const *pchEnd;
  1994. LONG cch;
  1995. HRESULT hr = S_OK;
  1996. int cwc;
  1997. pch = *ppchLine;
  1998. cch = *pcchLine;
  1999. pchEnd = &pch[cch];
  2000. while (pch < pchEnd)
  2001. {
  2002. if ('\n' == *pch++)
  2003. {
  2004. pchEnd = pch;
  2005. cch = SAFE_SUBTRACT_POINTERS(pchEnd, *ppchLine);
  2006. break;
  2007. }
  2008. }
  2009. do
  2010. {
  2011. cwc = MultiByteToWideChar(GetACP(), 0, *ppchLine, cch, pwc, *pcwc - 1);
  2012. if (0 == cwc)
  2013. {
  2014. hr = myHLastError();
  2015. break;
  2016. }
  2017. pwc[cwc] = L'\0';
  2018. *pcwc = cwc;
  2019. *pcchLine -= cch;
  2020. *ppchLine = pchEnd;
  2021. } while (FALSE);
  2022. CSASSERT(S_OK == hr || FAILED(hr));
  2023. return(hr);
  2024. }
  2025. // Format of each line is:
  2026. // <name>, <OrgUnit>, <Organization>, <Locality>, <State>, <Country>, <Server Name>, <Exchange Cert>, <Self signed cert>, <Comment>
  2027. HRESULT
  2028. certParseLine(
  2029. char const **ppchLine,
  2030. LONG *pcchLine,
  2031. CERT_AUTHORITY_INFO *pcai,
  2032. WCHAR *pwsz,
  2033. LONG *pcwc)
  2034. {
  2035. HRESULT hr;
  2036. LONG cwc;
  2037. do
  2038. {
  2039. if (0 == *pcchLine)
  2040. {
  2041. hr = S_OK;
  2042. *pcwc = 0;
  2043. goto error;
  2044. }
  2045. cwc = *pcwc;
  2046. hr = certExtractLine(ppchLine, pcchLine, pwsz, &cwc);
  2047. if (S_OK != hr)
  2048. {
  2049. goto error;
  2050. }
  2051. *pcwc = cwc + 1;
  2052. certTrimToken(&pwsz);
  2053. } while (L'\0' == *pwsz || L'#' == *pwsz);
  2054. if (!certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedName) ||
  2055. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedOrgUnit) ||
  2056. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedOrganization) ||
  2057. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedLocality) ||
  2058. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedState) ||
  2059. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedCountry) ||
  2060. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedConfig) ||
  2061. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedExchangeCertificate) ||
  2062. !certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedSignatureCertificate))
  2063. {
  2064. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  2065. goto error;
  2066. }
  2067. certExtractToken(&pwsz, (WCHAR const **) &pcai->pwszSanitizedDescription);
  2068. pcai->Flags = CAIF_SHAREDFOLDERENTRY;
  2069. error:
  2070. CSASSERT(S_OK == hr || FAILED(hr));
  2071. return(hr);
  2072. }
  2073. LONG
  2074. certSkipLine(
  2075. IN char const **ppchData,
  2076. IN LONG *pcchData)
  2077. {
  2078. LONG cb = 0;
  2079. HRESULT hr;
  2080. do
  2081. {
  2082. CERT_AUTHORITY_INFO cai;
  2083. WCHAR awcLine[cwcCONFIGLINEMAX];
  2084. LONG cwc;
  2085. char const *pchData;
  2086. LONG cchData;
  2087. pchData = *ppchData;
  2088. cchData = *pcchData;
  2089. cwc = sizeof(awcLine)/sizeof(awcLine[0]);
  2090. hr = certParseLine(&pchData, &cchData, &cai, awcLine, &cwc);
  2091. if (S_OK != hr || 0 == cwc)
  2092. {
  2093. break;
  2094. }
  2095. cb = sizeof(cai) + cwc * sizeof(WCHAR);
  2096. *ppchData = pchData;
  2097. *pcchData = cchData;
  2098. } while (FALSE);
  2099. //printf("certSkipLine: %u bytes\n", cb);
  2100. CSASSERT(S_OK == hr || FAILED(hr));
  2101. return(cb);
  2102. }
  2103. VOID
  2104. certAppendBSFileName(
  2105. IN OUT WCHAR *pwszPath,
  2106. IN WCHAR *pwszBSFN) // backslash + filename.ext
  2107. {
  2108. DWORD cwc;
  2109. cwc = wcslen(pwszPath);
  2110. if (0 < cwc--)
  2111. {
  2112. if (L'\\' == pwszPath[cwc])
  2113. {
  2114. pwszPath[cwc] = L'\0';
  2115. }
  2116. }
  2117. wcscat(pwszPath, pwszBSFN);
  2118. }
  2119. HRESULT
  2120. certGetConfigFileName(
  2121. OUT WCHAR **ppwszFileConfig)
  2122. {
  2123. HRESULT hr;
  2124. DWORD dwLen;
  2125. DWORD dwType;
  2126. HKEY hKeyConfig = NULL;
  2127. WCHAR *pwszFileName = NULL;
  2128. *ppwszFileConfig = NULL;
  2129. do
  2130. {
  2131. hr = RegOpenKeyEx(
  2132. HKEY_LOCAL_MACHINE,
  2133. wszREGKEYCONFIGPATH,
  2134. 0,
  2135. KEY_READ,
  2136. &hKeyConfig);
  2137. if (S_OK != hr)
  2138. {
  2139. _PrintErrorStr2(
  2140. hr,
  2141. "RegOpenKeyEx",
  2142. wszREGKEYCONFIGPATH,
  2143. ERROR_FILE_NOT_FOUND);
  2144. break;
  2145. }
  2146. // Get the size of the value first
  2147. hr = RegQueryValueEx(
  2148. hKeyConfig,
  2149. g_wszRegDirectory,
  2150. 0,
  2151. &dwType,
  2152. NULL,
  2153. &dwLen);
  2154. if (S_OK != hr)
  2155. {
  2156. _PrintErrorStr2(
  2157. hr,
  2158. "RegQueryValueEx",
  2159. g_wszRegDirectory,
  2160. ERROR_FILE_NOT_FOUND);
  2161. break;
  2162. }
  2163. if (0 == dwLen)
  2164. {
  2165. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  2166. _PrintErrorStr2(hr, "RegQueryValueEx", g_wszRegDirectory, hr);
  2167. break;
  2168. }
  2169. // Allocate enough memory for the directory path plus the file name.
  2170. pwszFileName = (WCHAR *) LocalAlloc(LMEM_FIXED,
  2171. dwLen + (wcslen(g_wszConfigFile) + 1) * sizeof(WCHAR));
  2172. if (NULL == pwszFileName)
  2173. {
  2174. hr = E_OUTOFMEMORY;
  2175. _PrintError2(hr, "LocalAlloc", hr);
  2176. break;
  2177. }
  2178. hr = RegQueryValueEx(
  2179. hKeyConfig,
  2180. g_wszRegDirectory,
  2181. 0,
  2182. &dwType,
  2183. (BYTE *) pwszFileName,
  2184. &dwLen);
  2185. if (S_OK != hr)
  2186. {
  2187. _PrintErrorStr(hr, "RegQueryValueEx", g_wszRegDirectory);
  2188. break;
  2189. }
  2190. if (L'\0' == *pwszFileName)
  2191. {
  2192. hr = S_OK;
  2193. break;
  2194. }
  2195. // Place config file name on end of path to open
  2196. certAppendBSFileName(pwszFileName, g_wszConfigFile);
  2197. *ppwszFileConfig = pwszFileName;
  2198. pwszFileName = NULL;
  2199. } while (FALSE);
  2200. if (NULL != pwszFileName)
  2201. {
  2202. LocalFree(pwszFileName);
  2203. }
  2204. if (NULL != hKeyConfig)
  2205. {
  2206. RegCloseKey(hKeyConfig);
  2207. }
  2208. return(myHError(hr));
  2209. }
  2210. HRESULT
  2211. certLoadConfigFile(
  2212. IN WCHAR const *pwszSharedFolder,
  2213. OUT BYTE **ppchData,
  2214. OUT DWORD *pcchData)
  2215. {
  2216. HRESULT hr;
  2217. WCHAR *pwszfn = NULL;
  2218. *ppchData = NULL;
  2219. *pcchData = 0;
  2220. if (NULL == pwszSharedFolder)
  2221. {
  2222. hr = certGetConfigFileName(&pwszfn);
  2223. _JumpIfError2(
  2224. hr,
  2225. error,
  2226. "certGetConfigFileName",
  2227. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
  2228. }
  2229. else
  2230. {
  2231. pwszfn = (WCHAR *) LocalAlloc(
  2232. LMEM_FIXED,
  2233. (wcslen(pwszSharedFolder) + wcslen(g_wszConfigFile) + 1) *
  2234. sizeof(WCHAR));
  2235. if (NULL == pwszfn)
  2236. {
  2237. hr = E_OUTOFMEMORY;
  2238. _JumpError(hr, error, "LocalAlloc");
  2239. }
  2240. wcscpy(pwszfn, pwszSharedFolder);
  2241. certAppendBSFileName(pwszfn, g_wszConfigFile);
  2242. }
  2243. if (NULL != pwszfn)
  2244. {
  2245. hr = certLoadFile(pwszfn, ppchData, pcchData);
  2246. _JumpIfError3(
  2247. hr,
  2248. error,
  2249. "certLoadFile",
  2250. HRESULT_FROM_WIN32(ERROR_NETWORK_UNREACHABLE),
  2251. HRESULT_FROM_WIN32(ERROR_INVALID_NETNAME));
  2252. }
  2253. hr = S_OK;
  2254. error:
  2255. if (NULL != pwszfn)
  2256. {
  2257. LocalFree(pwszfn);
  2258. }
  2259. CSASSERT(S_OK == hr || FAILED(hr));
  2260. return(hr);
  2261. }
  2262. //+--------------------------------------------------------------------------
  2263. // certRequestGetConfig -- return config data for all requested authorities.
  2264. //
  2265. // *pIndex should be set to 0 prior to the first call. It will be modified by
  2266. // certRequestGetConfig to index the next entry after those returned.
  2267. //
  2268. // *pCount is a pointer to the number of authorities for which information is
  2269. // requested. Upon successful completion, *pCount will contain the number of
  2270. // authorities for which information is returned. If the returned value is
  2271. // less than requested (or 0) no more entries are available.
  2272. //
  2273. // ppCertAuthorityInfo is a pointer to an uninitialized pointer on input.
  2274. // Upon successful completion, a pointer to an array of CERT_AUTHORITY_INFO
  2275. // structures is returned. When the caller no longer needs the array, it must
  2276. // be freed by calling CertFreeMemory.
  2277. //
  2278. // Returns S_OK on success.
  2279. //+--------------------------------------------------------------------------
  2280. HRESULT
  2281. certRequestGetConfigInfo(
  2282. IN WCHAR *pwszSharedFolder,
  2283. IN OUT LONG *pIndex,
  2284. IN OUT LONG *pCount,
  2285. OUT CERT_AUTHORITY_INFO **ppCertAuthorityInfo)
  2286. {
  2287. HRESULT hr = S_OK;
  2288. LONG cSkip = *pIndex;
  2289. LONG cLine = *pCount;
  2290. DWORD cchData;
  2291. char const *pchData = NULL;
  2292. LONG cLineReturned = 0;
  2293. CERT_AUTHORITY_INFO *pcaiBase = NULL;
  2294. *ppCertAuthorityInfo = NULL;
  2295. do
  2296. {
  2297. char const *pch;
  2298. LONG cch;
  2299. char const *pchSave;
  2300. LONG cchSave;
  2301. LONG cbTotal;
  2302. CERT_AUTHORITY_INFO *pcai;
  2303. WCHAR *pwc;
  2304. LONG cwc;
  2305. LONG cwcRemain;
  2306. LONG i;
  2307. hr = certLoadConfigFile(pwszSharedFolder, (BYTE **) &pchData, &cchData);
  2308. if (S_OK != hr || (NULL == pchData && 0 == cchData))
  2309. {
  2310. break;
  2311. }
  2312. pch = pchData;
  2313. cch = cchData;
  2314. // Skip cSkip entries:
  2315. //printf("Skipping lines:\n");
  2316. for (i = 0; i < cSkip; i++)
  2317. {
  2318. if (0 == certSkipLine(&pch, &cch))
  2319. {
  2320. break; // ran out of config data
  2321. }
  2322. }
  2323. if (i < cSkip)
  2324. {
  2325. break; // no data to return
  2326. }
  2327. pchSave = pch;
  2328. cchSave = cch;
  2329. // Compute total size of cLine entries:
  2330. //printf("Sizing lines:\n");
  2331. cbTotal = 0;
  2332. for (i = 0; i < cLine; i++)
  2333. {
  2334. LONG cb;
  2335. cb = certSkipLine(&pch, &cch);
  2336. if (0 == cb)
  2337. {
  2338. cLine = i; // reduce return line count
  2339. break; // ran out of config data
  2340. }
  2341. cbTotal += cb;
  2342. }
  2343. if (0 == cLine)
  2344. {
  2345. break; // no data to return
  2346. }
  2347. pcaiBase = (CERT_AUTHORITY_INFO *) LocalAlloc(LMEM_FIXED, cbTotal);
  2348. if (NULL == pcaiBase)
  2349. {
  2350. hr = E_OUTOFMEMORY;
  2351. break;
  2352. }
  2353. //printf("Returning lines:\n");
  2354. pch = pchSave;
  2355. cch = cchSave;
  2356. pcai = pcaiBase;
  2357. pwc = (WCHAR *) &pcai[cLine];
  2358. cwcRemain = (cbTotal - sizeof(*pcai) * cLine)/sizeof(*pwc);
  2359. for (i = 0; i < cLine; i++)
  2360. {
  2361. cwc = cwcRemain;
  2362. hr = certParseLine(&pch, &cch, pcai, pwc, &cwc);
  2363. if (S_OK != hr)
  2364. {
  2365. break;
  2366. }
  2367. pcai++;
  2368. pwc += cwc;
  2369. cwcRemain -= cwc;
  2370. }
  2371. if (S_OK != hr)
  2372. {
  2373. break;
  2374. }
  2375. cLineReturned = cLine;
  2376. *ppCertAuthorityInfo = pcaiBase;
  2377. pcaiBase = NULL;
  2378. hr = S_OK;
  2379. } while (FALSE);
  2380. *pIndex += cLineReturned;
  2381. *pCount = cLineReturned;
  2382. if (NULL != pcaiBase)
  2383. {
  2384. LocalFree(pcaiBase);
  2385. }
  2386. if (NULL != pchData)
  2387. {
  2388. LocalFree(const_cast<char *>(pchData));
  2389. }
  2390. CSASSERT(S_OK == hr || FAILED(hr));
  2391. return(hr);
  2392. }
  2393. HRESULT
  2394. myGetConfigFromPicker(
  2395. OPTIONAL IN HWND hwndParent,
  2396. OPTIONAL IN WCHAR const *pwszPrompt,
  2397. OPTIONAL IN WCHAR const *pwszTitle,
  2398. OPTIONAL IN WCHAR const *pwszSharedFolder,
  2399. IN BOOL fUseDS,
  2400. IN BOOL fCountOnly,
  2401. OUT DWORD *pdwCACount,
  2402. OUT CRYPTUI_CA_CONTEXT const **ppCAContext)
  2403. {
  2404. HRESULT hr;
  2405. CCertConfigPrivate configPrivate;
  2406. hr = configPrivate.GetConfigFromPicker(
  2407. hwndParent,
  2408. pwszPrompt,
  2409. pwszTitle,
  2410. pwszSharedFolder,
  2411. fUseDS,
  2412. fCountOnly,
  2413. pdwCACount,
  2414. ppCAContext);
  2415. _JumpIfError(hr, error, "configPrivate.GetConfigFromPicker");
  2416. error:
  2417. CSASSERT(S_OK == hr || FAILED(hr));
  2418. return(hr);
  2419. }