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.

453 lines
10 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 2000
  5. //
  6. // File: request.cpp
  7. //
  8. // Contents: Cert Server Policy Module implementation
  9. //
  10. //---------------------------------------------------------------------------
  11. #include "pch.cpp"
  12. #pragma hdrstop
  13. #include <assert.h>
  14. #include "celib.h"
  15. #include "policy.h"
  16. #include "module.h"
  17. HRESULT
  18. ReqInitialize(
  19. IN ICertServerPolicy *pServer)
  20. {
  21. HRESULT hr;
  22. hr = S_OK;
  23. return(hr);
  24. }
  25. VOID
  26. ReqCleanup()
  27. {
  28. }
  29. CRequestInstance::~CRequestInstance()
  30. {
  31. _Cleanup();
  32. }
  33. VOID
  34. CRequestInstance::_Cleanup()
  35. {
  36. if (NULL != m_strTemplateName)
  37. {
  38. SysFreeString(m_strTemplateName);
  39. m_strTemplateName = NULL;
  40. }
  41. if (NULL != m_strTemplateObjId)
  42. {
  43. SysFreeString(m_strTemplateObjId);
  44. m_strTemplateObjId = NULL;
  45. }
  46. }
  47. static WCHAR const *s_apwszCATypes[] =
  48. {
  49. wszCERTTYPE_SUBORDINATE_CA,
  50. wszCERTTYPE_CROSS_CA,
  51. };
  52. //+--------------------------------------------------------------------------
  53. // CRequestInstance::Initialize
  54. //
  55. // Returns S_OK on success.
  56. //+--------------------------------------------------------------------------
  57. HRESULT
  58. CRequestInstance::Initialize(
  59. IN CCertPolicySample *pPolicy,
  60. IN ICertServerPolicy *pServer,
  61. OUT BOOL *pfEnableEnrolleeExtensions)
  62. {
  63. HRESULT hr;
  64. HRESULT hrTemplate = S_OK;
  65. CERT_TEMPLATE_EXT *pTemplate = NULL;
  66. CERT_NAME_VALUE *pName = NULL;
  67. BSTR strTemplateObjId = NULL; // from V2 template extension
  68. BSTR strTemplateName = NULL; // from V1 template extension
  69. BSTR strTemplateRA = NULL; // from request attributes
  70. WCHAR const *pwszTemplateName;
  71. WCHAR const *pwszTemplateObjId;
  72. WCHAR const *pwszV1TemplateClass;
  73. VARIANT varValue;
  74. DWORD cbType;
  75. DWORD i;
  76. BOOL fConflict;
  77. BOOL f;
  78. BOOL fTemplateMissing;
  79. BOOL fRAObjId = FALSE;
  80. VariantInit(&varValue);
  81. *pfEnableEnrolleeExtensions = TRUE
  82. ;
  83. m_pPolicy = pPolicy;
  84. m_fCA = FALSE;
  85. // Retrieve the template ObjId from the V2 cert template info extension
  86. m_dwTemplateMajorVersion = 0;
  87. m_dwTemplateMinorVersion = 0;
  88. hr = polGetCertificateExtension(
  89. pServer,
  90. TEXT(szOID_CERTIFICATE_TEMPLATE),
  91. PROPTYPE_BINARY,
  92. &varValue);
  93. _PrintIfErrorStr2(
  94. hr,
  95. "Policy:polGetCertificateExtension",
  96. TEXT(szOID_CERTIFICATE_TEMPLATE),
  97. CERTSRV_E_PROPERTY_EMPTY);
  98. if (S_OK == hr)
  99. {
  100. // There was a cert type indicator.
  101. // varValue points to an encoded string
  102. if (VT_BSTR != varValue.vt)
  103. {
  104. hr = E_INVALIDARG;
  105. _JumpError(hr, error, "Policy:varValue.vt");
  106. }
  107. if (!ceDecodeObject(
  108. X509_ASN_ENCODING,
  109. X509_CERTIFICATE_TEMPLATE,
  110. (BYTE *) varValue.bstrVal,
  111. SysStringByteLen(varValue.bstrVal),
  112. FALSE,
  113. (VOID **) &pTemplate,
  114. &cbType))
  115. {
  116. hr = ceHLastError();
  117. _JumpError(hr, error, "Policy:ceDecodeObject");
  118. }
  119. if (!ceConvertSzToBstr(&strTemplateObjId, pTemplate->pszObjId, -1))
  120. {
  121. hr = E_OUTOFMEMORY;
  122. _JumpError(hr, error, "Policy:ceConvertSzToBstr");
  123. }
  124. m_dwTemplateMajorVersion = pTemplate->dwMajorVersion;
  125. m_dwTemplateMinorVersion = pTemplate->dwMinorVersion;
  126. DBGPRINT((
  127. fDebug,
  128. pTemplate->fMinorVersion?
  129. "Extension Template Info: %ws V%u.%u\n" :
  130. "Extension Template Info: %ws V%u%\n",
  131. strTemplateObjId,
  132. m_dwTemplateMajorVersion,
  133. m_dwTemplateMinorVersion));
  134. }
  135. VariantClear(&varValue);
  136. // Retrieve template Name from the V1 cert template name extension
  137. hr = polGetCertificateExtension(
  138. pServer,
  139. TEXT(szOID_ENROLL_CERTTYPE_EXTENSION),
  140. PROPTYPE_BINARY,
  141. &varValue);
  142. _PrintIfErrorStr2(
  143. hr,
  144. "Policy:polGetCertificateExtension",
  145. TEXT(szOID_ENROLL_CERTTYPE_EXTENSION),
  146. CERTSRV_E_PROPERTY_EMPTY);
  147. if (S_OK == hr)
  148. {
  149. // There was a cert type indicator.
  150. // varValue points to an encoded string
  151. if (VT_BSTR != varValue.vt)
  152. {
  153. hr = E_INVALIDARG;
  154. _JumpError(hr, error, "Policy:varValue.vt");
  155. }
  156. if (!ceDecodeObject(
  157. X509_ASN_ENCODING,
  158. X509_UNICODE_ANY_STRING,
  159. (BYTE *) varValue.bstrVal,
  160. SysStringByteLen(varValue.bstrVal),
  161. FALSE,
  162. (VOID **) &pName,
  163. &cbType))
  164. {
  165. hr = ceHLastError();
  166. _JumpError(hr, error, "Policy:ceDecodeObject");
  167. }
  168. strTemplateName = SysAllocString((WCHAR *) pName->Value.pbData);
  169. if (NULL == strTemplateName)
  170. {
  171. hr = E_OUTOFMEMORY;
  172. _JumpError(hr, error, "Policy:SysAllocString");
  173. }
  174. DBGPRINT((fDebug, "Extension Template: %ws\n", strTemplateName));
  175. }
  176. fConflict = FALSE;
  177. fTemplateMissing = FALSE;
  178. // Retrieve the template from the request attributes
  179. hr = polGetRequestAttribute(pServer, wszPROPCERTTEMPLATE, &strTemplateRA);
  180. if (S_OK != hr)
  181. {
  182. _PrintErrorStr2(
  183. hr,
  184. "Policy:polGetRequestAttribute",
  185. wszPROPCERTTEMPLATE,
  186. CERTSRV_E_PROPERTY_EMPTY);
  187. hr = S_OK;
  188. }
  189. else
  190. {
  191. DBGPRINT((fDebug, "Attribute Template: %ws\n", strTemplateRA));
  192. if (NULL != strTemplateObjId &&
  193. !_TemplateNamesMatch(strTemplateObjId, strTemplateRA, &f))
  194. {
  195. fConflict = TRUE;
  196. if (f)
  197. {
  198. fTemplateMissing = TRUE;
  199. }
  200. }
  201. if (NULL != strTemplateName &&
  202. !_TemplateNamesMatch(strTemplateName, strTemplateRA, &f))
  203. {
  204. fConflict = TRUE;
  205. if (f)
  206. {
  207. fTemplateMissing = TRUE;
  208. }
  209. }
  210. hr = ceVerifyObjId(strTemplateRA);
  211. fRAObjId = S_OK == hr;
  212. }
  213. if (NULL != strTemplateObjId &&
  214. NULL != strTemplateName &&
  215. !_TemplateNamesMatch(strTemplateObjId, strTemplateName, &f))
  216. {
  217. fConflict = TRUE;
  218. if (f)
  219. {
  220. fTemplateMissing = TRUE;
  221. }
  222. }
  223. if (fConflict)
  224. {
  225. hrTemplate = CERTSRV_E_TEMPLATE_CONFLICT;
  226. if (NULL != strTemplateObjId)
  227. {
  228. _PrintErrorStr(
  229. hrTemplate,
  230. "Policy:Extension Template ObjId",
  231. strTemplateObjId);
  232. }
  233. if (NULL != strTemplateName)
  234. {
  235. _PrintErrorStr(
  236. hrTemplate,
  237. "Policy:Extension Template Name",
  238. strTemplateName);
  239. }
  240. if (NULL != strTemplateRA)
  241. {
  242. _PrintErrorStr(
  243. hrTemplate,
  244. "Policy:Attribute Template",
  245. strTemplateRA);
  246. }
  247. }
  248. pwszTemplateName = strTemplateName;
  249. pwszTemplateObjId = strTemplateObjId;
  250. if (fRAObjId)
  251. {
  252. if (NULL == pwszTemplateObjId)
  253. {
  254. pwszTemplateObjId = strTemplateRA;
  255. }
  256. }
  257. else
  258. {
  259. if (NULL == pwszTemplateName)
  260. {
  261. pwszTemplateName = strTemplateRA;
  262. }
  263. }
  264. {
  265. if (NULL != pwszTemplateName)
  266. {
  267. for (i = 0; i < ARRAYSIZE(s_apwszCATypes); i++)
  268. {
  269. if (0 == celstrcmpiL(s_apwszCATypes[i], pwszTemplateName))
  270. {
  271. m_fCA = TRUE;
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. hr = SetTemplateName(pServer, pwszTemplateName, pwszTemplateObjId);
  278. _JumpIfError(hr, error, "Policy:SetTemplateName");
  279. pwszV1TemplateClass = pwszTemplateName;
  280. hr = pPolicy->AddV1TemplateNameExtension(pServer, pwszV1TemplateClass);
  281. _JumpIfError(hr, error, "AddTemplateNameExtension");
  282. error:
  283. if (S_OK != hrTemplate)
  284. {
  285. hr = hrTemplate; // override secondary errors
  286. }
  287. VariantClear(&varValue);
  288. if (NULL != pName)
  289. {
  290. LocalFree(pName);
  291. }
  292. if (NULL != pTemplate)
  293. {
  294. LocalFree(pTemplate);
  295. }
  296. if (NULL != strTemplateObjId)
  297. {
  298. SysFreeString(strTemplateObjId);
  299. }
  300. if (NULL != strTemplateName)
  301. {
  302. SysFreeString(strTemplateName);
  303. }
  304. if (NULL != strTemplateRA)
  305. {
  306. SysFreeString(strTemplateRA);
  307. }
  308. return(hr);
  309. }
  310. BOOL
  311. CRequestInstance::_TemplateNamesMatch(
  312. IN WCHAR const *pwszTemplateName1,
  313. IN WCHAR const *pwszTemplateName2,
  314. OUT BOOL *pfTemplateMissing)
  315. {
  316. HRESULT hr1;
  317. HRESULT hr2;
  318. BOOL fMatch = TRUE;
  319. *pfTemplateMissing = FALSE;
  320. if (0 == celstrcmpiL(pwszTemplateName1, pwszTemplateName2))
  321. {
  322. goto done; // identical names
  323. }
  324. {
  325. hr1 = ceVerifyObjId(pwszTemplateName1);
  326. hr2 = ceVerifyObjId(pwszTemplateName2);
  327. if ((S_OK == hr1) ^ (S_OK == hr2))
  328. {
  329. goto done;
  330. }
  331. }
  332. fMatch = FALSE;
  333. done:
  334. return(fMatch);
  335. }
  336. //+--------------------------------------------------------------------------
  337. // CRequestInstance::SetTemplateName
  338. //
  339. // Returns S_OK on success.
  340. //+--------------------------------------------------------------------------
  341. HRESULT
  342. CRequestInstance::SetTemplateName(
  343. IN ICertServerPolicy *pServer,
  344. IN OPTIONAL WCHAR const *pwszTemplateName,
  345. IN OPTIONAL WCHAR const *pwszTemplateObjId)
  346. {
  347. HRESULT hr;
  348. BSTR strProp = NULL;
  349. BSTR strTemplateName = NULL;
  350. if (NULL != pwszTemplateName)
  351. {
  352. m_strTemplateName = SysAllocString(pwszTemplateName);
  353. if (NULL == m_strTemplateName)
  354. {
  355. hr = E_OUTOFMEMORY;
  356. _JumpError(hr, error, "Policy:SysAllocString");
  357. }
  358. strTemplateName = m_strTemplateName;
  359. }
  360. if (NULL != pwszTemplateObjId)
  361. {
  362. m_strTemplateObjId = SysAllocString(pwszTemplateObjId);
  363. if (NULL == m_strTemplateObjId)
  364. {
  365. hr = E_OUTOFMEMORY;
  366. _JumpError(hr, error, "Policy:SysAllocString");
  367. }
  368. strTemplateName = m_strTemplateObjId;
  369. }
  370. if (NULL != strTemplateName)
  371. {
  372. VARIANT var;
  373. strProp = SysAllocString(wszPROPCERTIFICATETEMPLATE);
  374. if (NULL == strProp)
  375. {
  376. hr = E_OUTOFMEMORY;
  377. _JumpError(hr, error, "Policy:SysAllocString");
  378. }
  379. var.vt = VT_BSTR;
  380. var.bstrVal = strTemplateName;
  381. hr = pServer->SetCertificateProperty(strProp, PROPTYPE_STRING, &var);
  382. _JumpIfError(hr, error, "Policy:SetCertificateProperty");
  383. }
  384. hr = S_OK;
  385. error:
  386. if (NULL != strProp)
  387. {
  388. SysFreeString(strProp);
  389. }
  390. return(hr);
  391. }