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.

479 lines
11 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. #if defined(CCERTADMIN)
  13. # define CCertProp CCertAdmin
  14. # define wszCCertProp L"CCertAdmin"
  15. # define m_pICertPropD m_pICertAdminD
  16. # define fRPCARG(fRPC)
  17. #elif defined(CCERTREQUEST)
  18. # define CCertProp CCertRequest
  19. # define wszCCertProp L"CCertRequest"
  20. # define m_pICertPropD m_pICertRequestD
  21. # define fRPCARG(fRPC) (fRPC),
  22. #else
  23. # error -- CCERTADMIN or CCERTREQUEST must be defined
  24. #endif
  25. //+--------------------------------------------------------------------------
  26. // CCertProp::_InitCAPropInfo -- Initialize CA Prop Info
  27. //
  28. // Initialize CA Prop Info member varaibles
  29. //+--------------------------------------------------------------------------
  30. VOID
  31. CCertProp::_InitCAPropInfo()
  32. {
  33. m_pbKRACertState = NULL;
  34. m_pbCACertState = NULL;
  35. m_pbCRLState = NULL;
  36. m_pCAPropInfo = NULL;
  37. m_pCAInfo = NULL;
  38. }
  39. //+--------------------------------------------------------------------------
  40. // CCertProp::_CleanupCAPropInfo -- free memory
  41. //
  42. // free memory associated with this instance
  43. //+--------------------------------------------------------------------------
  44. VOID
  45. CCertProp::_CleanupCAPropInfo()
  46. {
  47. // Memory returned from DCOM calls were MIDL_user_allocate'd
  48. if (NULL != m_pbKRACertState)
  49. {
  50. MIDL_user_free(m_pbKRACertState);
  51. m_pbKRACertState = NULL;
  52. }
  53. if (NULL != m_pbCACertState)
  54. {
  55. MIDL_user_free(m_pbCACertState);
  56. m_pbCACertState = NULL;
  57. }
  58. if (NULL != m_pbCRLState)
  59. {
  60. MIDL_user_free(m_pbCRLState);
  61. m_pbCRLState = NULL;
  62. }
  63. if (NULL != m_pCAInfo)
  64. {
  65. MIDL_user_free(m_pCAInfo);
  66. m_pCAInfo = NULL;
  67. }
  68. m_cCAPropInfo = 0;
  69. }
  70. //+--------------------------------------------------------------------------
  71. // CCertProp::GetCAProperty -- Get a CA property
  72. //
  73. // Returns S_OK on success.
  74. //+--------------------------------------------------------------------------
  75. STDMETHODIMP
  76. CCertProp::GetCAProperty(
  77. /* [in] */ BSTR const strConfig,
  78. /* [in] */ LONG PropId, // CR_PROP_*
  79. /* [in] */ LONG PropIndex,
  80. /* [in] */ LONG PropType, // PROPTYPE_*
  81. /* [in] */ LONG Flags, // CR_OUT_*
  82. /* [out, retval] */ VARIANT *pvarPropertyValue)
  83. {
  84. HRESULT hr;
  85. WCHAR const *pwszAuthority;
  86. CERTTRANSBLOB ctbCAProp = { 0, NULL };
  87. DWORD dwCAInfoOffset = MAXDWORD;
  88. BYTE const *pb;
  89. DWORD cb;
  90. BYTE **ppb = NULL;
  91. DWORD *pcb;
  92. DWORD dwState;
  93. if (NULL == pvarPropertyValue)
  94. {
  95. hr = E_POINTER;
  96. _JumpError(hr, error, "NULL parm");
  97. }
  98. VariantInit(pvarPropertyValue);
  99. hr = _OpenConnection(fRPCARG(FALSE) strConfig, 2, &pwszAuthority);
  100. _JumpIfError(hr, error, "_OpenConnection");
  101. // Check for cached data:
  102. switch (PropId)
  103. {
  104. case CR_PROP_CATYPE:
  105. dwCAInfoOffset = FIELD_OFFSET(CAINFO, CAType);
  106. break;
  107. case CR_PROP_CASIGCERTCOUNT:
  108. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cCASignatureCerts);
  109. break;
  110. case CR_PROP_CAXCHGCERTCOUNT:
  111. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cCAExchangeCerts);
  112. break;
  113. case CR_PROP_EXITCOUNT:
  114. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cExitModules);
  115. break;
  116. case CR_PROP_CAPROPIDMAX:
  117. dwCAInfoOffset = FIELD_OFFSET(CAINFO, lPropIdMax);
  118. break;
  119. case CR_PROP_CACERTSTATE:
  120. ppb = &m_pbCACertState;
  121. pcb = &m_cbCACertState;
  122. break;
  123. case CR_PROP_CRLSTATE:
  124. ppb = &m_pbCRLState;
  125. pcb = &m_cbCRLState;
  126. break;
  127. case CR_PROP_ROLESEPARATIONENABLED:
  128. dwCAInfoOffset = FIELD_OFFSET(CAINFO, lRoleSeparationEnabled);
  129. break;
  130. case CR_PROP_KRACERTUSEDCOUNT:
  131. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cKRACertUsedCount);
  132. break;
  133. case CR_PROP_KRACERTCOUNT:
  134. dwCAInfoOffset = FIELD_OFFSET(CAINFO, cKRACertCount);
  135. break;
  136. case CR_PROP_ADVANCEDSERVER:
  137. dwCAInfoOffset = FIELD_OFFSET(CAINFO, fAdvancedServer);
  138. break;
  139. case CR_PROP_KRACERTSTATE:
  140. ppb = &m_pbKRACertState;
  141. pcb = &m_cbKRACertState;
  142. break;
  143. }
  144. // Call server if:
  145. // non-cached property ||
  146. // cached state is empty ||
  147. // cached CAInfo is empty
  148. if ((NULL == ppb && MAXDWORD == dwCAInfoOffset) ||
  149. (NULL != ppb && NULL == *ppb) ||
  150. (MAXDWORD != dwCAInfoOffset && NULL == m_pCAInfo))
  151. {
  152. __try
  153. {
  154. hr = m_pICertPropD->GetCAProperty(
  155. pwszAuthority,
  156. PropId,
  157. PropIndex,
  158. PropType,
  159. &ctbCAProp);
  160. }
  161. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  162. {
  163. }
  164. if (S_OK != hr)
  165. {
  166. DBGPRINT((
  167. DBG_SS_ERROR,
  168. "GetCAProperty(Propid=%x, PropIndex=%x, PropType=%x) -> %x\n",
  169. PropId,
  170. PropIndex,
  171. PropType,
  172. hr));
  173. }
  174. _JumpIfError3(
  175. hr,
  176. error,
  177. "GetCAProperty",
  178. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
  179. E_INVALIDARG);
  180. DBGDUMPHEX((DBG_SS_CERTLIBI, DH_NOADDRESS, ctbCAProp.pb, ctbCAProp.cb));
  181. if (NULL != ctbCAProp.pb)
  182. {
  183. myRegisterMemAlloc(ctbCAProp.pb, ctbCAProp.cb, CSM_COTASKALLOC);
  184. }
  185. pb = ctbCAProp.pb;
  186. cb = ctbCAProp.cb;
  187. // populate CAInfo cache
  188. if (MAXDWORD != dwCAInfoOffset)
  189. {
  190. if (CCSIZEOF_STRUCT(CAINFO, cbSize) >
  191. ((CAINFO *) ctbCAProp.pb)->cbSize ||
  192. cb != ((CAINFO *) ctbCAProp.pb)->cbSize)
  193. {
  194. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  195. _JumpError(hr, error, "CAINFO size");
  196. }
  197. m_cbCAInfo = ctbCAProp.cb;
  198. m_pCAInfo = (CAINFO *) ctbCAProp.pb;
  199. ctbCAProp.pb = NULL;
  200. }
  201. // populate Cert or CRL state cache
  202. else if (NULL != ppb)
  203. {
  204. *pcb = ctbCAProp.cb;
  205. *ppb = ctbCAProp.pb;
  206. ctbCAProp.pb = NULL;
  207. }
  208. }
  209. // fetch from CAInfo cache
  210. if (MAXDWORD != dwCAInfoOffset)
  211. {
  212. pb = (BYTE const *) Add2Ptr(m_pCAInfo, dwCAInfoOffset);
  213. cb = sizeof(DWORD);
  214. if (dwCAInfoOffset + sizeof(DWORD) > m_cbCAInfo)
  215. {
  216. hr = E_NOTIMPL;
  217. _JumpError(hr, error, "CAINFO size");
  218. }
  219. }
  220. // fetch from Cert or CRL state cache
  221. else if (NULL != ppb)
  222. {
  223. if ((DWORD) PropIndex > *pcb)
  224. {
  225. hr = E_INVALIDARG;
  226. _JumpError(hr, error, "PropIndex");
  227. }
  228. dwState = *(BYTE const *) Add2Ptr(*ppb, PropIndex);
  229. pb = (BYTE const *) &dwState;
  230. cb = sizeof(dwState);
  231. }
  232. __try
  233. {
  234. hr = myUnmarshalFormattedVariant(
  235. Flags,
  236. PropId,
  237. PropType,
  238. cb,
  239. pb,
  240. pvarPropertyValue);
  241. }
  242. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  243. {
  244. }
  245. _JumpIfError(hr, error, "myUnmarshalFormattedVariant");
  246. error:
  247. if (S_OK != hr && NULL != pvarPropertyValue)
  248. {
  249. VariantClear(pvarPropertyValue);
  250. }
  251. if (NULL != ctbCAProp.pb)
  252. {
  253. MIDL_user_free(ctbCAProp.pb);
  254. }
  255. return(_SetErrorInfo(hr, wszCCertProp L"::GetCAProperty"));
  256. }
  257. //+--------------------------------------------------------------------------
  258. // CCertProp::_FindCAPropInfo -- Get a CA property's CAPROP info pointer
  259. //
  260. // Returns S_OK on success.
  261. //+--------------------------------------------------------------------------
  262. HRESULT
  263. CCertProp::_FindCAPropInfo(
  264. IN BSTR const strConfig,
  265. IN LONG PropId, // CR_PROP_*
  266. OUT CAPROP const **ppcap)
  267. {
  268. HRESULT hr;
  269. WCHAR const *pwszAuthority;
  270. CERTTRANSBLOB ctbCAPropInfo = { 0, NULL };
  271. CSASSERT(NULL != ppcap);
  272. *ppcap = NULL;
  273. hr = _OpenConnection(fRPCARG(FALSE) strConfig, 2, &pwszAuthority);
  274. _JumpIfError(hr, error, "_OpenConnection");
  275. if (NULL == m_pCAPropInfo)
  276. {
  277. __try
  278. {
  279. hr = m_pICertPropD->GetCAPropertyInfo(
  280. pwszAuthority,
  281. &m_cCAPropInfo,
  282. &ctbCAPropInfo);
  283. }
  284. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  285. {
  286. }
  287. _JumpIfError(hr, error, "GetCAPropertyInfo");
  288. if (NULL != ctbCAPropInfo.pb)
  289. {
  290. myRegisterMemAlloc(
  291. ctbCAPropInfo.pb,
  292. ctbCAPropInfo.cb,
  293. CSM_COTASKALLOC);
  294. }
  295. __try
  296. {
  297. hr = myCAPropInfoUnmarshal(
  298. (CAPROP *) ctbCAPropInfo.pb,
  299. m_cCAPropInfo,
  300. ctbCAPropInfo.cb);
  301. }
  302. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  303. {
  304. }
  305. _JumpIfError(hr, error, "myCAPropInfoUnmarshal");
  306. m_pCAPropInfo = (CAPROP *) ctbCAPropInfo.pb;
  307. ctbCAPropInfo.pb = NULL;
  308. }
  309. hr = myCAPropInfoLookup(m_pCAPropInfo, m_cCAPropInfo, PropId, ppcap);
  310. _JumpIfError(hr, error, "myCAPropInfoLookup");
  311. error:
  312. if (NULL != ctbCAPropInfo.pb)
  313. {
  314. MIDL_user_free(ctbCAPropInfo.pb);
  315. }
  316. return(hr);
  317. }
  318. //+--------------------------------------------------------------------------
  319. // CCertProp::GetCAPropertyFlags -- Get a CA property's type and flags
  320. //
  321. // Returns S_OK on success.
  322. //+--------------------------------------------------------------------------
  323. STDMETHODIMP
  324. CCertProp::GetCAPropertyFlags(
  325. /* [in] */ BSTR const strConfig,
  326. /* [in] */ LONG PropId, // CR_PROP_*
  327. /* [out, retval] */ LONG *pPropFlags)
  328. {
  329. HRESULT hr;
  330. CAPROP const *pcap;
  331. hr = _FindCAPropInfo(strConfig, PropId, &pcap);
  332. _JumpIfError(hr, error, "_FindCAPropInfo");
  333. *pPropFlags = pcap->lPropFlags;
  334. error:
  335. return(_SetErrorInfo(hr, wszCCertProp L"::GetCAPropertyFlags"));
  336. }
  337. //+--------------------------------------------------------------------------
  338. // CCertProp::GetCAPropertyDisplayName -- Get a CA property's display name
  339. //
  340. // Returns S_OK on success.
  341. //+--------------------------------------------------------------------------
  342. STDMETHODIMP
  343. CCertProp::GetCAPropertyDisplayName(
  344. /* [in] */ BSTR const strConfig,
  345. /* [in] */ LONG PropId, // CR_PROP_*
  346. /* [out, retval] */ BSTR *pstrDisplayName)
  347. {
  348. HRESULT hr;
  349. CAPROP const *pcap;
  350. hr = _FindCAPropInfo(strConfig, PropId, &pcap);
  351. _JumpIfError(hr, error, "_FindCAPropInfo");
  352. if (!ConvertWszToBstr(pstrDisplayName, pcap->pwszDisplayName, -1))
  353. {
  354. hr = E_OUTOFMEMORY;
  355. _JumpError(hr, error, "ConvertWszToBstr");
  356. }
  357. error:
  358. return(_SetErrorInfo(hr, wszCCertProp L"::GetCAPropertyDisplayName"));
  359. }
  360. #if defined(CCERTADMIN)
  361. //+--------------------------------------------------------------------------
  362. // CCertProp::SetCAProperty -- Set a CA property
  363. //
  364. // Returns S_OK on success.
  365. //+--------------------------------------------------------------------------
  366. STDMETHODIMP
  367. CCertProp::SetCAProperty(
  368. /* [in] */ BSTR const strConfig,
  369. /* [in] */ LONG PropId, // CR_PROP_*
  370. /* [in] */ LONG PropIndex,
  371. /* [in] */ LONG PropType, // PROPTYPE_*
  372. /* [in] */ VARIANT *pvarPropertyValue)
  373. {
  374. HRESULT hr;
  375. WCHAR const *pwszAuthority;
  376. CERTTRANSBLOB ctbValue;
  377. LONG lval;
  378. ctbValue.pb = NULL;
  379. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  380. _JumpIfError(hr, error, "_OpenConnection");
  381. hr = myMarshalVariant(pvarPropertyValue, PropType, &ctbValue.cb, &ctbValue.pb);
  382. _JumpIfError(hr, error, "myMarshalVariant");
  383. __try
  384. {
  385. hr = m_pICertAdminD->SetCAProperty(
  386. pwszAuthority,
  387. PropId,
  388. PropIndex,
  389. PropType,
  390. &ctbValue);
  391. }
  392. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  393. {
  394. }
  395. _JumpIfError(hr, error, "SetCAProperty");
  396. error:
  397. if (NULL != ctbValue.pb)
  398. {
  399. LocalFree(ctbValue.pb);
  400. }
  401. hr = myHError(hr);
  402. return(_SetErrorInfo(hr, L"CCertAdmin::SetCAProperty"));
  403. }
  404. #endif // defined(CCERTADMIN)