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.

235 lines
6.1 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000
  3. File: EKUs.cpp
  4. Content: Implementation of CEKUs.
  5. History: 11-15-99 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #include "StdAfx.h"
  8. #include "CAPICOM.h"
  9. #include "EKUs.h"
  10. ////////////////////////////////////////////////////////////////////////////////
  11. //
  12. // Exported functions.
  13. //
  14. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  15. Function : CreateEKUsObject
  16. Synopsis : Create a IEKUs collection object and populate the collection with
  17. EKUs from the specified certificate.
  18. Parameter: PCERT_ENHKEY_USAGE pUsage - Pointer to CERT_ENHKEY_USAGE.
  19. IEKUs ** ppIEKUs - Pointer to pointer IEKUs object.
  20. Remark :
  21. ------------------------------------------------------------------------------*/
  22. HRESULT CreateEKUsObject (PCERT_ENHKEY_USAGE pUsage,
  23. IEKUs ** ppIEKUs)
  24. {
  25. HRESULT hr = S_OK;
  26. CComObject<CEKUs> * pCEKUs = NULL;
  27. DebugTrace("Entering CreateEKUsObject().\n");
  28. //
  29. // Sanity check.
  30. //
  31. ATLASSERT(ppIEKUs);
  32. try
  33. {
  34. //
  35. // Create the object. Note that the ref count will still be 0
  36. // after the object is created.
  37. //
  38. if (FAILED(hr = CComObject<CEKUs>::CreateInstance(&pCEKUs)))
  39. {
  40. DebugTrace("Error [%#x]: CComObject<CEKUs>::CreateInstance() failed.\n", hr);
  41. goto ErrorExit;
  42. }
  43. //
  44. // Initialize object.
  45. //
  46. if (FAILED(hr = pCEKUs->Init(pUsage)))
  47. {
  48. DebugTrace("Error [%#x]: pCEKUs->Init() failed.\n", hr);
  49. goto ErrorExit;
  50. }
  51. //
  52. // Return interface pointer to caller.
  53. //
  54. if (FAILED(hr = pCEKUs->QueryInterface(ppIEKUs)))
  55. {
  56. DebugTrace("Unexpected error [%#x]: pCEKUs->QueryInterface() failed.\n", hr);
  57. goto ErrorExit;
  58. }
  59. }
  60. catch(...)
  61. {
  62. hr = E_POINTER;
  63. DebugTrace("Exception: invalid parameter.\n");
  64. goto ErrorExit;
  65. }
  66. CommonExit:
  67. DebugTrace("Leaving CreateEKUsObject().\n");
  68. return hr;
  69. ErrorExit:
  70. //
  71. // Sanity check.
  72. //
  73. ATLASSERT(FAILED(hr));
  74. if (pCEKUs)
  75. {
  76. delete pCEKUs;
  77. }
  78. goto CommonExit;
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////
  81. //
  82. // CEKUs
  83. //
  84. ////////////////////////////////////////////////////////////////////////////////
  85. //
  86. // Non COM functions.
  87. //
  88. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  89. Function : CEKUs::Init
  90. Synopsis : Initialize the EKUs collection object by adding all individual
  91. EKU object to the collection.
  92. Parameter: PCERT_ENHKEY_USAGE pUsage - Pointer to CERT_ENHKEY_USAGE.
  93. Remark : This method is not part of the COM interface (it is a normal C++
  94. member function). We need it to initialize the object created
  95. internally by us.
  96. Since it is only a normal C++ member function, this function can
  97. only be called from a C++ class pointer, not an interface pointer.
  98. ------------------------------------------------------------------------------*/
  99. STDMETHODIMP CEKUs::Init (PCERT_ENHKEY_USAGE pUsage)
  100. {
  101. HRESULT hr = S_OK;
  102. DebugTrace("Entering CEKUs::Init().\n");
  103. try
  104. {
  105. //
  106. // Add all EKU OIDs to the map.
  107. //
  108. if (pUsage)
  109. {
  110. //
  111. // Debug Log.
  112. //
  113. DebugTrace("Creating %d EKU object(s) for the EKUs collection.\n", pUsage->cUsageIdentifier);
  114. //
  115. // Make sure we have room to add.
  116. //
  117. if ((m_coll.size() + pUsage->cUsageIdentifier) > m_coll.max_size())
  118. {
  119. hr = CAPICOM_E_OUT_OF_RESOURCE;
  120. DebugTrace("Error [%#x]: Maximum entries (%#x) reached for EKUs collection.\n",
  121. hr, pUsage->cUsageIdentifier);
  122. goto ErrorExit;
  123. }
  124. for (DWORD i = 0; i < pUsage->cUsageIdentifier; i++)
  125. {
  126. //
  127. // Create the IEKU object for each of the EKU found in the certificate.
  128. //
  129. char szIndex[33];
  130. CComBSTR bstrIndex;
  131. CComPtr<IEKU> pIEKU;
  132. if (FAILED(hr = ::CreateEKUObject(pUsage->rgpszUsageIdentifier[i], &pIEKU)))
  133. {
  134. DebugTrace("Error [%#x]: CreateEKUObject() failed.\n", hr);
  135. goto ErrorExit;
  136. }
  137. //
  138. // BSTR index of numeric value.
  139. //
  140. wsprintfA(szIndex, "%#08x", m_coll.size() + 1);
  141. if (!(bstrIndex = szIndex))
  142. {
  143. hr = E_OUTOFMEMORY;
  144. DebugTrace("Error [%#x]: bstrIndex = szIndex failed.\n", hr);
  145. goto ErrorExit;
  146. }
  147. //
  148. // Now add object to collection map.
  149. //
  150. // Note that the overloaded = operator for CComPtr will
  151. // automatically AddRef to the object. Also, when the CComPtr
  152. // is deleted (happens when the Remove or map destructor is called),
  153. // the CComPtr destructor will automatically Release the object.
  154. //
  155. m_coll[bstrIndex] = pIEKU;
  156. }
  157. }
  158. }
  159. catch(...)
  160. {
  161. hr = E_POINTER;
  162. DebugTrace("Exception: invalid parameter.\n");
  163. goto ErrorExit;
  164. }
  165. CommonExit:
  166. DebugTrace("Leaving CEKUs::Init().\n");
  167. return hr;
  168. ErrorExit:
  169. //
  170. // Sanity check.
  171. //
  172. ATLASSERT(FAILED(hr));
  173. //
  174. // Free resource.
  175. //
  176. m_coll.clear();
  177. goto CommonExit;
  178. }