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.

258 lines
7.4 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000
  3. File: CCertificatePolicies.cpp
  4. Content: Implementation of CCertificatePolicies.
  5. History: 11-17-2001 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #include "StdAfx.h"
  8. #include "CAPICOM.h"
  9. #include "CertificatePolicies.h"
  10. #include "Common.h"
  11. ////////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Exported functions.
  14. //
  15. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  16. Function : CreateCertificatePoliciesObject
  17. Synopsis : Create a CertificatePolicies collection object and populate the
  18. collection with policy information from the specified certificate
  19. policies.
  20. Parameter: LPSTR pszOid - OID string.
  21. CRYPT_DATA_BLOB * pEncodedBlob - Pointer to encoded data blob.
  22. IDispatch ** ppICertificatePolicies - Pointer to pointer
  23. IDispatch to recieve the
  24. interface pointer.
  25. Remark :
  26. ------------------------------------------------------------------------------*/
  27. HRESULT CreateCertificatePoliciesObject (LPSTR pszOid,
  28. CRYPT_DATA_BLOB * pEncodedBlob,
  29. IDispatch ** ppICertificatePolicies)
  30. {
  31. HRESULT hr = S_OK;
  32. CComObject<CCertificatePolicies> * pCCertificatePolicies = NULL;
  33. DebugTrace("Entering CreateCCertificatePoliciesObject().\n");
  34. //
  35. // Sanity check.
  36. //
  37. ATLASSERT(pszOid);
  38. ATLASSERT(pEncodedBlob);
  39. ATLASSERT(ppICertificatePolicies);
  40. try
  41. {
  42. //
  43. // Create the object. Note that the ref count will still be 0
  44. // after the object is created.
  45. //
  46. if (FAILED(hr = CComObject<CCertificatePolicies>::CreateInstance(&pCCertificatePolicies)))
  47. {
  48. DebugTrace("Error [%#x]: CComObject<CCertificatePolicies>::CreateInstance() failed.\n", hr);
  49. goto ErrorExit;
  50. }
  51. //
  52. // Initialize object.
  53. //
  54. if (FAILED(hr = pCCertificatePolicies->Init(pszOid, pEncodedBlob)))
  55. {
  56. DebugTrace("Error [%#x]: pCCertificatePolicies->Init() failed.\n", hr);
  57. goto ErrorExit;
  58. }
  59. //
  60. // Return interface pointer to caller.
  61. //
  62. if (FAILED(hr = pCCertificatePolicies->QueryInterface(IID_IDispatch,
  63. (void **) ppICertificatePolicies)))
  64. {
  65. DebugTrace("Unexpected error [%#x]: pCCertificatePolicies->QueryInterface() failed.\n", hr);
  66. goto ErrorExit;
  67. }
  68. }
  69. catch(...)
  70. {
  71. hr = E_POINTER;
  72. DebugTrace("Exception: invalid parameter.\n");
  73. goto ErrorExit;
  74. }
  75. CommonExit:
  76. DebugTrace("Leaving CreateCCertificatePoliciesObject().\n");
  77. return hr;
  78. ErrorExit:
  79. //
  80. // Sanity check.
  81. //
  82. ATLASSERT(FAILED(hr));
  83. if (pCCertificatePolicies)
  84. {
  85. delete pCCertificatePolicies;
  86. }
  87. goto CommonExit;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////
  90. //
  91. // CCertificatePolicies
  92. //
  93. ////////////////////////////////////////////////////////////////////////////////
  94. //
  95. // Non COM functions.
  96. //
  97. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  98. Function : CCertificatePolicies::Init
  99. Synopsis : Initialize the CCertificatePolicies collection object by adding all
  100. individual qualifier object to the collection.
  101. Parameter: LPSTR pszOid - OID string.
  102. CRYPT_DATA_BLOB * pEncodedBlob - Pointer to encoded data blob.
  103. Remark : This method is not part of the COM interface (it is a normal C++
  104. member function). We need it to initialize the object created
  105. internally by us.
  106. Since it is only a normal C++ member function, this function can
  107. only be called from a C++ class pointer, not an interface pointer.
  108. ------------------------------------------------------------------------------*/
  109. STDMETHODIMP CCertificatePolicies::Init (LPSTR pszOid,
  110. CRYPT_DATA_BLOB * pEncodedBlob)
  111. {
  112. HRESULT hr = S_OK;
  113. DATA_BLOB DataBlob = {0, NULL};
  114. PCERT_POLICIES_INFO pCertPoliciesInfo = NULL;
  115. DWORD i;
  116. DebugTrace("Entering CCertificatePolicies::Init().\n");
  117. //
  118. // Sanity check.
  119. //
  120. ATLASSERT(pszOid);
  121. ATLASSERT(pEncodedBlob);
  122. ATLASSERT(pEncodedBlob->cbData);
  123. ATLASSERT(pEncodedBlob->pbData);
  124. try
  125. {
  126. //
  127. // Decode the extension.
  128. //
  129. if (FAILED(hr = ::DecodeObject(szOID_CERT_POLICIES,
  130. pEncodedBlob->pbData,
  131. pEncodedBlob->cbData,
  132. &DataBlob)))
  133. {
  134. DebugTrace("Error [%#x]: DecodeObject() failed.\n", hr);
  135. goto ErrorExit;
  136. }
  137. pCertPoliciesInfo = (PCERT_POLICIES_INFO) DataBlob.pbData;
  138. //
  139. // Add all CCertificatePolicies to the map.
  140. //
  141. for (i = 0; i < pCertPoliciesInfo->cPolicyInfo; i++)
  142. {
  143. CComBSTR bstrIndex;
  144. CComPtr<IPolicyInformation> pIPolicyInformation = NULL;
  145. //
  146. // Create the qualifier object.
  147. //
  148. if (FAILED(hr = ::CreatePolicyInformationObject(&pCertPoliciesInfo->rgPolicyInfo[i],
  149. &pIPolicyInformation)))
  150. {
  151. DebugTrace("Error [%#x]: CreatePolicyInformationObject() failed.\n", hr);
  152. goto ErrorExit;
  153. }
  154. //
  155. // BSTR index of OID.
  156. //
  157. if (!(bstrIndex = pCertPoliciesInfo->rgPolicyInfo[i].pszPolicyIdentifier))
  158. {
  159. hr = E_OUTOFMEMORY;
  160. DebugTrace("Error [%#x]: bstrIndex = pCertPoliciesInfo->rgPolicyInfo[i].pszPolicyIdentifier failed.\n", hr);
  161. goto ErrorExit;
  162. }
  163. //
  164. // Now add object to collection map.
  165. //
  166. // Note that the overloaded = operator for CComPtr will
  167. // automatically AddRef to the object. Also, when the CComPtr
  168. // is deleted (happens when the Remove or map destructor is called),
  169. // the CComPtr destructor will automatically Release the object.
  170. //
  171. m_coll[bstrIndex] = pIPolicyInformation;
  172. }
  173. }
  174. catch(...)
  175. {
  176. hr = E_POINTER;
  177. DebugTrace("Exception: invalid parameter.\n");
  178. goto ErrorExit;
  179. }
  180. CommonExit:
  181. //
  182. // Free resources.
  183. //
  184. if (DataBlob.pbData)
  185. {
  186. ::CoTaskMemFree(DataBlob.pbData);
  187. }
  188. DebugTrace("Leaving CCertificatePolicies::Init().\n");
  189. return hr;
  190. ErrorExit:
  191. //
  192. // Sanity check.
  193. //
  194. ATLASSERT(FAILED(hr));
  195. //
  196. // Free resource.
  197. //
  198. m_coll.clear();
  199. goto CommonExit;
  200. }