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.

663 lines
16 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: csprop2.cpp
  8. //
  9. // Contents: ICertAdmin2 & ICertRequest2 CA Property methods
  10. //
  11. //--------------------------------------------------------------------------
  12. #define __DIR__ "include"
  13. #define __dwFILE__ __dwFILE_INCLUDE_CSPROP2_CPP__
  14. #if defined(CCERTADMIN)
  15. # define CCertProp CCertAdmin
  16. # define wszCCertProp L"CCertAdmin"
  17. # define m_pICertPropD m_pICertAdminD
  18. # define fRPCARG(fRPC)
  19. #elif defined(CCERTREQUEST)
  20. # define CCertProp CCertRequest
  21. # define wszCCertProp L"CCertRequest"
  22. # define m_pICertPropD m_pICertRequestD
  23. # define fRPCARG(fRPC) (fRPC),
  24. #else
  25. # error -- CCERTADMIN or CCERTREQUEST must be defined
  26. #endif
  27. //+--------------------------------------------------------------------------
  28. // CCertProp::_InitCAPropInfo -- Initialize CA Prop Info
  29. //
  30. // Initialize CA Prop Info member varaibles
  31. //+--------------------------------------------------------------------------
  32. VOID
  33. CCertProp::_InitCAPropInfo()
  34. {
  35. m_pbKRACertState = NULL;
  36. m_pbForwardCrossCertState = NULL;
  37. m_pbBackwardCrossCertState = NULL;
  38. m_pbCACertState = NULL;
  39. m_pbCACertVersion = NULL;
  40. m_pbCRLState = NULL;
  41. m_pCAPropInfo = NULL;
  42. m_pCAInfo = NULL;
  43. }
  44. //+--------------------------------------------------------------------------
  45. // CCertProp::_CleanupCAPropInfo -- free memory
  46. //
  47. // free memory associated with this instance
  48. //+--------------------------------------------------------------------------
  49. VOID
  50. CCertProp::_CleanupCAPropInfo()
  51. {
  52. // Memory returned from DCOM calls were MIDL_user_allocate'd
  53. if (NULL != m_pbKRACertState)
  54. {
  55. MIDL_user_free(m_pbKRACertState);
  56. m_pbKRACertState = NULL;
  57. }
  58. if (NULL != m_pbForwardCrossCertState)
  59. {
  60. MIDL_user_free(m_pbForwardCrossCertState);
  61. m_pbForwardCrossCertState = NULL;
  62. }
  63. if (NULL != m_pbBackwardCrossCertState)
  64. {
  65. MIDL_user_free(m_pbBackwardCrossCertState);
  66. m_pbBackwardCrossCertState = NULL;
  67. }
  68. if (NULL != m_pbCACertState)
  69. {
  70. MIDL_user_free(m_pbCACertState);
  71. m_pbCACertState = NULL;
  72. }
  73. if (NULL != m_pbCACertVersion)
  74. {
  75. MIDL_user_free(m_pbCACertVersion);
  76. m_pbCACertVersion = NULL;
  77. }
  78. if (NULL != m_pbCRLState)
  79. {
  80. MIDL_user_free(m_pbCRLState);
  81. m_pbCRLState = NULL;
  82. }
  83. if (NULL != m_pCAInfo)
  84. {
  85. MIDL_user_free(m_pCAInfo);
  86. m_pCAInfo = NULL;
  87. }
  88. if (NULL != m_pCAPropInfo)
  89. {
  90. LocalFree(m_pCAPropInfo);
  91. m_pCAPropInfo = NULL;
  92. }
  93. m_cCAPropInfo = 0;
  94. }
  95. //+--------------------------------------------------------------------------
  96. // CCertProp::GetCAProperty -- Get a CA property
  97. //
  98. // Returns S_OK on success.
  99. //+--------------------------------------------------------------------------
  100. STDMETHODIMP
  101. CCertProp::GetCAProperty(
  102. /* [in] */ BSTR const strConfig,
  103. /* [in] */ LONG PropId, // CR_PROP_*
  104. /* [in] */ LONG PropIndex,
  105. /* [in] */ LONG PropType, // PROPTYPE_*
  106. /* [in] */ LONG Flags, // CR_OUT_*
  107. /* [out, retval] */ VARIANT *pvarPropertyValue)
  108. {
  109. HRESULT hr;
  110. WCHAR const *pwszAuthority;
  111. CERTTRANSBLOB ctbCAProp = { 0, NULL };
  112. DWORD dwCAInfoOffset = MAXDWORD;
  113. BYTE const *pb;
  114. DWORD cb;
  115. BYTE **ppb = NULL;
  116. DWORD *pcb;
  117. DWORD cbCachedVal = sizeof(BYTE);
  118. DWORD dwVal;
  119. if (NULL == pvarPropertyValue)
  120. {
  121. hr = E_POINTER;
  122. _JumpError(hr, error, "NULL parm");
  123. }
  124. VariantInit(pvarPropertyValue);
  125. hr = _OpenConnection(fRPCARG(FALSE) strConfig, 2, &pwszAuthority);
  126. _JumpIfError(hr, error, "_OpenConnection");
  127. // Check for cached data:
  128. pcb = NULL;
  129. switch (PropId)
  130. {
  131. case CR_PROP_CATYPE:
  132. dwCAInfoOffset = FIELD_OFFSET(CAINFO, CAType);
  133. break;
  134. case CR_PROP_CASIGCERTCOUNT:
  135. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cCASignatureCerts);
  136. break;
  137. case CR_PROP_CAXCHGCERTCOUNT:
  138. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cCAExchangeCerts);
  139. break;
  140. case CR_PROP_EXITCOUNT:
  141. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cExitModules);
  142. break;
  143. case CR_PROP_CAPROPIDMAX:
  144. dwCAInfoOffset = FIELD_OFFSET(CAINFO, lPropIdMax);
  145. break;
  146. case CR_PROP_CACERTSTATE:
  147. ppb = &m_pbCACertState;
  148. pcb = &m_cbCACertState;
  149. break;
  150. case CR_PROP_CACERTVERSION:
  151. ppb = &m_pbCACertVersion;
  152. pcb = &m_cbCACertVersion;
  153. cbCachedVal = sizeof(DWORD);
  154. break;
  155. case CR_PROP_CRLSTATE:
  156. ppb = &m_pbCRLState;
  157. pcb = &m_cbCRLState;
  158. break;
  159. case CR_PROP_ROLESEPARATIONENABLED:
  160. dwCAInfoOffset = FIELD_OFFSET(CAINFO, lRoleSeparationEnabled);
  161. break;
  162. case CR_PROP_KRACERTUSEDCOUNT:
  163. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cKRACertUsedCount);
  164. break;
  165. case CR_PROP_KRACERTCOUNT:
  166. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cKRACertCount);
  167. break;
  168. case CR_PROP_ADVANCEDSERVER:
  169. dwCAInfoOffset = FIELD_OFFSET(CAINFO, fAdvancedServer);
  170. break;
  171. case CR_PROP_KRACERTSTATE:
  172. ppb = &m_pbKRACertState;
  173. pcb = &m_cbKRACertState;
  174. break;
  175. case CR_PROP_CAFORWARDCROSSCERTSTATE:
  176. ppb = &m_pbForwardCrossCertState;
  177. pcb = &m_cbForwardCrossCertState;
  178. break;
  179. case CR_PROP_CABACKWARDCROSSCERTSTATE:
  180. ppb = &m_pbBackwardCrossCertState;
  181. pcb = &m_cbBackwardCrossCertState;
  182. break;
  183. }
  184. // Call server if:
  185. // non-cached property ||
  186. // cached state is empty ||
  187. // cached CAInfo is empty
  188. pb = NULL;
  189. cb = 0;
  190. if ((NULL == ppb && MAXDWORD == dwCAInfoOffset) ||
  191. (NULL != ppb && NULL == *ppb) ||
  192. (MAXDWORD != dwCAInfoOffset && NULL == m_pCAInfo))
  193. {
  194. __try
  195. {
  196. hr = m_pICertPropD->GetCAProperty(
  197. pwszAuthority,
  198. PropId,
  199. PropIndex,
  200. PropType,
  201. &ctbCAProp);
  202. }
  203. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  204. {
  205. }
  206. if (S_OK != hr)
  207. {
  208. DBGPRINT((
  209. E_INVALIDARG == hr? DBG_SS_CERTLIBI : DBG_SS_ERROR,
  210. "GetCAProperty(Propid=%u, PropIndex=%u, PropType=%u) -> %x\n",
  211. PropId,
  212. PropIndex,
  213. PropType,
  214. hr));
  215. }
  216. _JumpIfError3(
  217. hr,
  218. error,
  219. "GetCAProperty",
  220. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
  221. E_INVALIDARG);
  222. DBGDUMPHEX((DBG_SS_CERTLIBI, DH_NOADDRESS, ctbCAProp.pb, ctbCAProp.cb));
  223. if (NULL != ctbCAProp.pb)
  224. {
  225. myRegisterMemAlloc(ctbCAProp.pb, ctbCAProp.cb, CSM_COTASKALLOC);
  226. }
  227. pb = ctbCAProp.pb;
  228. cb = ctbCAProp.cb;
  229. // populate CAInfo cache
  230. if (MAXDWORD != dwCAInfoOffset)
  231. {
  232. if (CCSIZEOF_STRUCT(CAINFO, cbSize) >
  233. ((CAINFO *) ctbCAProp.pb)->cbSize ||
  234. cb != ((CAINFO *) ctbCAProp.pb)->cbSize)
  235. {
  236. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  237. _JumpError(hr, error, "CAINFO size");
  238. }
  239. m_cbCAInfo = ctbCAProp.cb;
  240. m_pCAInfo = (CAINFO *) ctbCAProp.pb;
  241. ctbCAProp.pb = NULL;
  242. }
  243. // populate Cert or CRL state cache
  244. else if (NULL != ppb)
  245. {
  246. *pcb = ctbCAProp.cb;
  247. *ppb = ctbCAProp.pb;
  248. ctbCAProp.pb = NULL;
  249. }
  250. }
  251. // fetch from CAInfo cache
  252. if (MAXDWORD != dwCAInfoOffset)
  253. {
  254. pb = (BYTE const *) Add2Ptr(m_pCAInfo, dwCAInfoOffset);
  255. cb = sizeof(DWORD);
  256. if (dwCAInfoOffset + sizeof(DWORD) > m_cbCAInfo)
  257. {
  258. hr = E_NOTIMPL;
  259. _JumpError(hr, error, "CAINFO size");
  260. }
  261. }
  262. // fetch from Cert or CRL state cache
  263. else if (NULL != ppb)
  264. {
  265. DWORD obCache = PropIndex * cbCachedVal;
  266. if (obCache + cbCachedVal > *pcb)
  267. {
  268. hr = E_INVALIDARG;
  269. _JumpError(hr, error, "PropIndex");
  270. }
  271. CSASSERT(sizeof(dwVal) >= cbCachedVal);
  272. dwVal = 0;
  273. CopyMemory(&dwVal, &(*ppb)[obCache], cbCachedVal);
  274. pb = (BYTE const *) &dwVal;
  275. cb = sizeof(dwVal);
  276. }
  277. __try
  278. {
  279. hr = myUnmarshalFormattedVariant(
  280. Flags,
  281. PropId,
  282. PropType,
  283. cb,
  284. pb,
  285. pvarPropertyValue);
  286. }
  287. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  288. {
  289. }
  290. _JumpIfError(hr, error, "myUnmarshalFormattedVariant");
  291. error:
  292. if (S_OK != hr && NULL != pvarPropertyValue)
  293. {
  294. VariantClear(pvarPropertyValue);
  295. }
  296. if (NULL != ctbCAProp.pb)
  297. {
  298. MIDL_user_free(ctbCAProp.pb);
  299. }
  300. return(_SetErrorInfo(hr, wszCCertProp L"::GetCAProperty"));
  301. }
  302. //+--------------------------------------------------------------------------
  303. // CCertProp::_FindCAPropInfo -- Get a CA property's CAPROP info pointer
  304. //
  305. // Returns S_OK on success.
  306. //+--------------------------------------------------------------------------
  307. HRESULT
  308. CCertProp::_FindCAPropInfo(
  309. IN BSTR const strConfig,
  310. IN LONG PropId, // CR_PROP_*
  311. OUT CAPROP const **ppcap)
  312. {
  313. HRESULT hr;
  314. WCHAR const *pwszAuthority;
  315. CERTTRANSBLOB ctbCAPropInfo = { 0, NULL };
  316. CSASSERT(NULL != ppcap);
  317. *ppcap = NULL;
  318. hr = _OpenConnection(fRPCARG(FALSE) strConfig, 2, &pwszAuthority);
  319. _JumpIfError(hr, error, "_OpenConnection");
  320. if (NULL == m_pCAPropInfo)
  321. {
  322. __try
  323. {
  324. hr = m_pICertPropD->GetCAPropertyInfo(
  325. pwszAuthority,
  326. &m_cCAPropInfo,
  327. &ctbCAPropInfo);
  328. }
  329. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  330. {
  331. }
  332. _JumpIfError(hr, error, "GetCAPropertyInfo");
  333. if (NULL != ctbCAPropInfo.pb)
  334. {
  335. myRegisterMemAlloc(
  336. ctbCAPropInfo.pb,
  337. ctbCAPropInfo.cb,
  338. CSM_COTASKALLOC);
  339. }
  340. __try
  341. {
  342. hr = myCAPropInfoUnmarshal(
  343. (CATRANSPROP *) ctbCAPropInfo.pb,
  344. m_cCAPropInfo,
  345. ctbCAPropInfo.cb,
  346. &m_pCAPropInfo);
  347. }
  348. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  349. {
  350. }
  351. _JumpIfError(hr, error, "myCAPropInfoUnmarshal");
  352. }
  353. hr = myCAPropInfoLookup(m_pCAPropInfo, m_cCAPropInfo, PropId, ppcap);
  354. _JumpIfError(hr, error, "myCAPropInfoLookup");
  355. error:
  356. if (NULL != ctbCAPropInfo.pb)
  357. {
  358. MIDL_user_free(ctbCAPropInfo.pb);
  359. }
  360. return(hr);
  361. }
  362. //+--------------------------------------------------------------------------
  363. // CCertProp::GetCAPropertyFlags -- Get a CA property's type and flags
  364. //
  365. // Returns S_OK on success.
  366. //+--------------------------------------------------------------------------
  367. STDMETHODIMP
  368. CCertProp::GetCAPropertyFlags(
  369. /* [in] */ BSTR const strConfig,
  370. /* [in] */ LONG PropId, // CR_PROP_*
  371. /* [out, retval] */ LONG *pPropFlags)
  372. {
  373. HRESULT hr;
  374. CAPROP const *pcap;
  375. if (NULL == pPropFlags)
  376. {
  377. hr = E_POINTER;
  378. _JumpError(hr, error, "NULL parm");
  379. }
  380. hr = _FindCAPropInfo(strConfig, PropId, &pcap);
  381. _JumpIfError(hr, error, "_FindCAPropInfo");
  382. *pPropFlags = pcap->lPropFlags;
  383. error:
  384. return(_SetErrorInfo(hr, wszCCertProp L"::GetCAPropertyFlags"));
  385. }
  386. //+--------------------------------------------------------------------------
  387. // CCertProp::GetCAPropertyDisplayName -- Get a CA property's display name
  388. //
  389. // Returns S_OK on success.
  390. //+--------------------------------------------------------------------------
  391. STDMETHODIMP
  392. CCertProp::GetCAPropertyDisplayName(
  393. /* [in] */ BSTR const strConfig,
  394. /* [in] */ LONG PropId, // CR_PROP_*
  395. /* [out, retval] */ BSTR *pstrDisplayName)
  396. {
  397. HRESULT hr;
  398. CAPROP const *pcap;
  399. if (NULL == pstrDisplayName)
  400. {
  401. hr = E_POINTER;
  402. _JumpError(hr, error, "NULL parm");
  403. }
  404. *pstrDisplayName = NULL;
  405. hr = _FindCAPropInfo(strConfig, PropId, &pcap);
  406. _JumpIfError(hr, error, "_FindCAPropInfo");
  407. if (!ConvertWszToBstr(pstrDisplayName, pcap->pwszDisplayName, -1))
  408. {
  409. hr = E_OUTOFMEMORY;
  410. _JumpError(hr, error, "ConvertWszToBstr");
  411. }
  412. error:
  413. return(_SetErrorInfo(hr, wszCCertProp L"::GetCAPropertyDisplayName"));
  414. }
  415. #if defined(CCERTADMIN)
  416. //+--------------------------------------------------------------------------
  417. // CCertProp::SetCAProperty -- Set a CA property
  418. //
  419. // Returns S_OK on success.
  420. //+--------------------------------------------------------------------------
  421. STDMETHODIMP
  422. CCertProp::SetCAProperty(
  423. /* [in] */ BSTR const strConfig,
  424. /* [in] */ LONG PropId, // CR_PROP_*
  425. /* [in] */ LONG PropIndex,
  426. /* [in] */ LONG PropType, // PROPTYPE_*
  427. /* [in] */ VARIANT *pvarPropertyValue)
  428. {
  429. HRESULT hr;
  430. WCHAR const *pwszAuthority;
  431. CERTTRANSBLOB ctbValue;
  432. ctbValue.pb = NULL;
  433. if (NULL == pvarPropertyValue)
  434. {
  435. hr = E_POINTER;
  436. _JumpError(hr, error, "NULL parm");
  437. }
  438. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  439. _JumpIfError(hr, error, "_OpenConnection");
  440. hr = myMarshalVariant(pvarPropertyValue, PropType, &ctbValue.cb, &ctbValue.pb);
  441. _JumpIfError(hr, error, "myMarshalVariant");
  442. __try
  443. {
  444. hr = m_pICertAdminD->SetCAProperty(
  445. pwszAuthority,
  446. PropId,
  447. PropIndex,
  448. PropType,
  449. &ctbValue);
  450. }
  451. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  452. {
  453. }
  454. _JumpIfError(hr, error, "SetCAProperty");
  455. error:
  456. if (NULL != ctbValue.pb)
  457. {
  458. LocalFree(ctbValue.pb);
  459. }
  460. hr = myHError(hr);
  461. return(_SetErrorInfo(hr, L"CCertAdmin::SetCAProperty"));
  462. }
  463. #endif // defined(CCERTADMIN)
  464. HRESULT
  465. myCAPropInfoUnmarshal(
  466. IN CATRANSPROP const *pCATransPropInfo,
  467. IN LONG cCAPropInfo,
  468. IN DWORD cbCATransPropInfo,
  469. OUT CAPROP **ppCAPropInfo)
  470. {
  471. HRESULT hr;
  472. CATRANSPROP const *pcatp;
  473. CATRANSPROP const *pcatpEnd;
  474. CAPROP *rgcap = NULL;
  475. CAPROP *pcap;
  476. BYTE *pbEnd;
  477. DWORD cb;
  478. DWORD cbAlloc;
  479. DWORD cbT;
  480. WCHAR const *pwszDisplayName;
  481. if (NULL == pCATransPropInfo || NULL == ppCAPropInfo)
  482. {
  483. hr = E_POINTER;
  484. _JumpError(hr, error, "NULL parm");
  485. }
  486. *ppCAPropInfo = NULL;
  487. pbEnd = (BYTE *) Add2Ptr(pCATransPropInfo, cbCATransPropInfo);
  488. pcatpEnd = &pCATransPropInfo[cCAPropInfo];
  489. cb = sizeof(CAPROP) * cCAPropInfo;
  490. for (pcatp = pCATransPropInfo; pcatp < pcatpEnd; pcatp++)
  491. {
  492. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  493. if ((BYTE *) (pcatp + 1) > pbEnd)
  494. {
  495. _JumpError(hr, error, "bad marshalled data");
  496. }
  497. pwszDisplayName = NULL;
  498. if (0 != pcatp->obwszDisplayName)
  499. {
  500. pwszDisplayName = (WCHAR const *) Add2Ptr(
  501. pCATransPropInfo,
  502. pcatp->obwszDisplayName);
  503. if ((BYTE *) pwszDisplayName < (BYTE *) (pcatp + 1) ||
  504. (BYTE *) pwszDisplayName >= pbEnd)
  505. {
  506. _JumpError(hr, error, "bad marshalled pointer");
  507. }
  508. cbT = sizeof(WCHAR) * (wcslen(pwszDisplayName) + 1);
  509. if ((BYTE *) Add2Ptr(pwszDisplayName, cbT) > pbEnd)
  510. {
  511. _JumpError(hr, error, "bad marshalled string");
  512. }
  513. hr = myCAPropGetDisplayName(pcatp->lPropId, &pwszDisplayName);
  514. _PrintIfError(hr, "myCAPropGetDisplayName");
  515. if (S_OK != hr)
  516. {
  517. cb += DWORDROUND(cbT);
  518. }
  519. }
  520. }
  521. cbAlloc = cb;
  522. rgcap = (CAPROP *) LocalAlloc(LMEM_FIXED, cb);
  523. if (NULL == rgcap)
  524. {
  525. hr = E_OUTOFMEMORY;
  526. _JumpError(hr, error, "LocalAlloc");
  527. }
  528. cb = sizeof(CAPROP) * cCAPropInfo;
  529. pcap = rgcap;
  530. for (pcatp = pCATransPropInfo; pcatp < pcatpEnd; pcap++,pcatp++)
  531. {
  532. WCHAR *pwszT;
  533. pcap->lPropId = pcatp->lPropId;
  534. pcap->lPropFlags = pcatp->lPropFlags;
  535. pcap->pwszDisplayName = NULL;
  536. hr = myCAPropGetDisplayName(pcatp->lPropId, &pwszDisplayName);
  537. _PrintIfError(hr, "myCAPropGetDisplayName");
  538. if (S_OK != hr && 0 != pcatp->obwszDisplayName)
  539. {
  540. pwszDisplayName = (WCHAR const *) Add2Ptr(
  541. pCATransPropInfo,
  542. pcatp->obwszDisplayName);
  543. pwszT = (WCHAR *) Add2Ptr(rgcap, cb);
  544. cbT = sizeof(WCHAR) * (wcslen(pwszDisplayName) + 1);
  545. CopyMemory(pwszT, pwszDisplayName, cbT);
  546. cb += DWORDROUND(cbT);
  547. pwszDisplayName = pwszT;
  548. }
  549. if (NULL != pwszDisplayName)
  550. {
  551. pcap->pwszDisplayName = const_cast<WCHAR *>(pwszDisplayName);
  552. }
  553. DBGPRINT((
  554. DBG_SS_CERTLIBI,
  555. "RequestGetCAPropertyInfo: ielt=%d idx=%x t=%x \"%ws\"\n",
  556. SAFE_SUBTRACT_POINTERS(pcatp, pCATransPropInfo),
  557. pcap->lPropId,
  558. pcap->lPropFlags,
  559. pcap->pwszDisplayName));
  560. }
  561. CSASSERT(cbAlloc == cb);
  562. *ppCAPropInfo = rgcap;
  563. rgcap = NULL;
  564. hr = S_OK;
  565. error:
  566. if (NULL != rgcap)
  567. {
  568. LocalFree(rgcap);
  569. }
  570. return(hr);
  571. }
  572. #undef __DIR__
  573. #undef __dwFILE__