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.

975 lines
21 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000
  3. File: KeyUsage.cpp
  4. Content: Implementation of CKeyUsage.
  5. History: 11-15-99 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #include "StdAfx.h"
  8. #include "CAPICOM.h"
  9. #include "KeyUsage.h"
  10. ////////////////////////////////////////////////////////////////////////////////
  11. //
  12. // Exported functions.
  13. //
  14. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  15. Function : CreateKeyUsageObject
  16. Synopsis : Create a IKeyUsage object and populate the porperties with
  17. data from the key usage extension of the specified certificate.
  18. Parameter: PCCERT_CONTEXT pCertContext - Pointer to CERT_CONTEXT to be used
  19. to initialize the IKeyUsage object.
  20. IKeyUsage ** ppIKeyUsage - Pointer to pointer IKeyUsage object.
  21. Remark :
  22. ------------------------------------------------------------------------------*/
  23. HRESULT CreateKeyUsageObject (PCCERT_CONTEXT pCertContext,
  24. IKeyUsage ** ppIKeyUsage)
  25. {
  26. HRESULT hr = S_OK;
  27. CComObject<CKeyUsage> * pCKeyUsage = NULL;
  28. DebugTrace("Entering CreateKeyUsageObject().\n");
  29. //
  30. // Sanity check.
  31. //
  32. ATLASSERT(pCertContext);
  33. ATLASSERT(ppIKeyUsage);
  34. try
  35. {
  36. //
  37. // Create the object. Note that the ref count will still be 0
  38. // after the object is created.
  39. //
  40. if (FAILED(hr = CComObject<CKeyUsage>::CreateInstance(&pCKeyUsage)))
  41. {
  42. DebugTrace("Error [%#x]: CComObject<CKeyUsage>::CreateInstance() failed.\n", hr);
  43. goto ErrorExit;
  44. }
  45. //
  46. // Init the object.
  47. //
  48. if (FAILED(hr = pCKeyUsage->Init(pCertContext)))
  49. {
  50. DebugTrace("Error [%#x]: pCKeyUsage->Init() failed.\n", hr);
  51. goto ErrorExit;
  52. }
  53. //
  54. // Return IKeyUsage pointer to caller.
  55. //
  56. if (FAILED(hr = pCKeyUsage->QueryInterface(ppIKeyUsage)))
  57. {
  58. DebugTrace("Error [%#x]: pCKeyUsage->QueryInterface() failed.\n", hr);
  59. goto ErrorExit;
  60. }
  61. }
  62. catch(...)
  63. {
  64. hr = E_POINTER;
  65. DebugTrace("Exception: invalid parameter.\n");
  66. goto ErrorExit;
  67. }
  68. CommonExit:
  69. DebugTrace("Leaving CreateKeyUsageObject().\n");
  70. return hr;
  71. ErrorExit:
  72. //
  73. // Sanity check.
  74. //
  75. ATLASSERT(FAILED(hr));
  76. if (pCKeyUsage)
  77. {
  78. delete pCKeyUsage;
  79. }
  80. goto CommonExit;
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////
  83. //
  84. // CKeyUsage
  85. //
  86. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  87. Function : CKeyUsage::get_IsPresent
  88. Synopsis : Check to see if the KeyUsage extension is present.
  89. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  90. Remark :
  91. ------------------------------------------------------------------------------*/
  92. STDMETHODIMP CKeyUsage::get_IsPresent (VARIANT_BOOL * pVal)
  93. {
  94. HRESULT hr = S_OK;
  95. DebugTrace("Entering CKeyUsage::get_IsPresent().\n");
  96. // Lock access to this object.
  97. //
  98. m_Lock.Lock();
  99. try
  100. {
  101. //
  102. // Check parameters.
  103. //
  104. if (NULL == pVal)
  105. {
  106. hr = E_INVALIDARG;
  107. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  108. goto ErrorExit;
  109. }
  110. //
  111. // Return result.
  112. //
  113. *pVal = m_bIsPresent;
  114. }
  115. catch(...)
  116. {
  117. hr = E_POINTER;
  118. DebugTrace("Exception: invalid parameter.\n");
  119. goto ErrorExit;
  120. }
  121. UnlockExit:
  122. //
  123. // Unlock access to this object.
  124. //
  125. m_Lock.Unlock();
  126. DebugTrace("Leaving CKeyUsage::get_IsPresent().\n");
  127. return hr;
  128. ErrorExit:
  129. //
  130. // Sanity check.
  131. //
  132. ATLASSERT(FAILED(hr));
  133. ReportError(hr);
  134. goto UnlockExit;
  135. }
  136. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  137. Function : CKeyUsage::get_IsCritical
  138. Synopsis : Check to see if the KeyUsage extension is marked critical.
  139. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  140. Remark :
  141. ------------------------------------------------------------------------------*/
  142. STDMETHODIMP CKeyUsage::get_IsCritical (VARIANT_BOOL * pVal)
  143. {
  144. HRESULT hr = S_OK;
  145. DebugTrace("Entering CKeyUsage::get_IsCritical().\n");
  146. // Lock access to this object.
  147. //
  148. m_Lock.Lock();
  149. try
  150. {
  151. //
  152. // Check parameters.
  153. //
  154. if (NULL == pVal)
  155. {
  156. hr = E_INVALIDARG;
  157. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  158. goto ErrorExit;
  159. }
  160. //
  161. // Return result.
  162. //
  163. *pVal = m_bIsCritical;
  164. }
  165. catch(...)
  166. {
  167. hr = E_POINTER;
  168. DebugTrace("Exception: invalid parameter.\n");
  169. goto ErrorExit;
  170. }
  171. UnlockExit:
  172. //
  173. // Unlock access to this object.
  174. //
  175. m_Lock.Unlock();
  176. DebugTrace("Leaving CKeyUsage::get_IsCritical().\n");
  177. return hr;
  178. ErrorExit:
  179. //
  180. // Sanity check.
  181. //
  182. ATLASSERT(FAILED(hr));
  183. ReportError(hr);
  184. goto UnlockExit;
  185. }
  186. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  187. Function : CKeyUsage::get_IsDigitalSignatureEnabled
  188. Synopsis : Check to see if Digital Signature bit is set in KeyUsage extension.
  189. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  190. Remark :
  191. ------------------------------------------------------------------------------*/
  192. STDMETHODIMP CKeyUsage::get_IsDigitalSignatureEnabled (VARIANT_BOOL * pVal)
  193. {
  194. HRESULT hr = S_OK;
  195. DebugTrace("Entering CKeyUsage::get_IsDigitalSignatureEnabled().\n");
  196. // Lock access to this object.
  197. //
  198. m_Lock.Lock();
  199. try
  200. {
  201. //
  202. // Check parameters.
  203. //
  204. if (NULL == pVal)
  205. {
  206. hr = E_INVALIDARG;
  207. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  208. goto ErrorExit;
  209. }
  210. //
  211. // Return result.
  212. //
  213. *pVal = m_dwKeyUsages & CERT_DIGITAL_SIGNATURE_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  214. }
  215. catch(...)
  216. {
  217. hr = E_POINTER;
  218. DebugTrace("Exception: invalid parameter.\n");
  219. goto ErrorExit;
  220. }
  221. UnlockExit:
  222. //
  223. // Unlock access to this object.
  224. //
  225. m_Lock.Unlock();
  226. DebugTrace("Leaving CKeyUsage::get_IsDigitalSignatureEnabled().\n");
  227. return hr;
  228. ErrorExit:
  229. //
  230. // Sanity check.
  231. //
  232. ATLASSERT(FAILED(hr));
  233. ReportError(hr);
  234. goto UnlockExit;
  235. }
  236. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  237. Function : CKeyUsage::get_IsNonRepudiationEnabled
  238. Synopsis : Check to see if Non Repudiation bit is set in KeyUsage extension.
  239. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  240. Remark :
  241. ------------------------------------------------------------------------------*/
  242. STDMETHODIMP CKeyUsage::get_IsNonRepudiationEnabled (VARIANT_BOOL * pVal)
  243. {
  244. HRESULT hr = S_OK;
  245. DebugTrace("Entering CKeyUsage::get_IsNonRepudiationEnabled().\n");
  246. // Lock access to this object.
  247. //
  248. m_Lock.Lock();
  249. try
  250. {
  251. //
  252. // Check parameters.
  253. //
  254. if (NULL == pVal)
  255. {
  256. hr = E_INVALIDARG;
  257. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  258. goto ErrorExit;
  259. }
  260. //
  261. // Return result.
  262. //
  263. *pVal = m_dwKeyUsages & CERT_NON_REPUDIATION_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  264. }
  265. catch(...)
  266. {
  267. hr = E_POINTER;
  268. DebugTrace("Exception: invalid parameter.\n");
  269. goto ErrorExit;
  270. }
  271. UnlockExit:
  272. //
  273. // Unlock access to this object.
  274. //
  275. m_Lock.Unlock();
  276. DebugTrace("Leaving CKeyUsage::get_IsNonRepudiationEnabled().\n");
  277. return hr;
  278. ErrorExit:
  279. //
  280. // Sanity check.
  281. //
  282. ATLASSERT(FAILED(hr));
  283. ReportError(hr);
  284. goto UnlockExit;
  285. }
  286. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  287. Function : CKeyUsage::get_IsKeyEnciphermentEnabled
  288. Synopsis : Check to see if Key Encipherment bit is set in KeyUsage extension.
  289. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  290. Remark :
  291. ------------------------------------------------------------------------------*/
  292. STDMETHODIMP CKeyUsage::get_IsKeyEnciphermentEnabled (VARIANT_BOOL * pVal)
  293. {
  294. HRESULT hr = S_OK;
  295. DebugTrace("Entering CKeyUsage::get_IsKeyEnciphermentEnabled().\n");
  296. // Lock access to this object.
  297. //
  298. m_Lock.Lock();
  299. try
  300. {
  301. //
  302. // Check parameters.
  303. //
  304. if (NULL == pVal)
  305. {
  306. hr = E_INVALIDARG;
  307. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  308. goto ErrorExit;
  309. }
  310. //
  311. // Return result.
  312. //
  313. *pVal = m_dwKeyUsages & CERT_KEY_ENCIPHERMENT_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  314. }
  315. catch(...)
  316. {
  317. hr = E_POINTER;
  318. DebugTrace("Exception: invalid parameter.\n");
  319. goto ErrorExit;
  320. }
  321. UnlockExit:
  322. //
  323. // Unlock access to this object.
  324. //
  325. m_Lock.Unlock();
  326. DebugTrace("Leaving CKeyUsage::get_IsKeyEnciphermentEnabled().\n");
  327. return hr;
  328. ErrorExit:
  329. //
  330. // Sanity check.
  331. //
  332. ATLASSERT(FAILED(hr));
  333. ReportError(hr);
  334. goto UnlockExit;
  335. }
  336. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  337. Function : CKeyUsage::get_IsDataEnciphermentEnabled
  338. Synopsis : Check to see if Data Encipherment bit is set in KeyUsage extension.
  339. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  340. Remark :
  341. ------------------------------------------------------------------------------*/
  342. STDMETHODIMP CKeyUsage::get_IsDataEnciphermentEnabled (VARIANT_BOOL * pVal)
  343. {
  344. HRESULT hr = S_OK;
  345. DebugTrace("Entering CKeyUsage::get_IsDataEnciphermentEnabled().\n");
  346. // Lock access to this object.
  347. //
  348. m_Lock.Lock();
  349. try
  350. {
  351. //
  352. // Check parameters.
  353. //
  354. if (NULL == pVal)
  355. {
  356. hr = E_INVALIDARG;
  357. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  358. goto ErrorExit;
  359. }
  360. //
  361. // Return result.
  362. //
  363. *pVal = m_dwKeyUsages & CERT_DATA_ENCIPHERMENT_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  364. }
  365. catch(...)
  366. {
  367. hr = E_POINTER;
  368. DebugTrace("Exception: invalid parameter.\n");
  369. goto ErrorExit;
  370. }
  371. UnlockExit:
  372. //
  373. // Unlock access to this object.
  374. //
  375. m_Lock.Unlock();
  376. DebugTrace("Leaving CKeyUsage::get_IsDataEnciphermentEnabled().\n");
  377. return hr;
  378. ErrorExit:
  379. //
  380. // Sanity check.
  381. //
  382. ATLASSERT(FAILED(hr));
  383. ReportError(hr);
  384. goto UnlockExit;
  385. }
  386. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  387. Function : CKeyUsage::get_IsKeyAgreementEnabled
  388. Synopsis : Check to see if Key Agreement bit is set in KeyUsage extension.
  389. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  390. Remark :
  391. ------------------------------------------------------------------------------*/
  392. STDMETHODIMP CKeyUsage::get_IsKeyAgreementEnabled (VARIANT_BOOL * pVal)
  393. {
  394. HRESULT hr = S_OK;
  395. DebugTrace("Entering CKeyUsage::get_IsKeyAgreementEnabled().\n");
  396. // Lock access to this object.
  397. //
  398. m_Lock.Lock();
  399. try
  400. {
  401. //
  402. // Check parameters.
  403. //
  404. if (NULL == pVal)
  405. {
  406. hr = E_INVALIDARG;
  407. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  408. goto ErrorExit;
  409. }
  410. //
  411. // Return result.
  412. //
  413. *pVal = m_dwKeyUsages & CERT_KEY_AGREEMENT_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  414. }
  415. catch(...)
  416. {
  417. hr = E_POINTER;
  418. DebugTrace("Exception: invalid parameter.\n");
  419. goto ErrorExit;
  420. }
  421. UnlockExit:
  422. //
  423. // Unlock access to this object.
  424. //
  425. m_Lock.Unlock();
  426. DebugTrace("Leaving CKeyUsage::get_IsKeyAgreementEnabled().\n");
  427. return hr;
  428. ErrorExit:
  429. //
  430. // Sanity check.
  431. //
  432. ATLASSERT(FAILED(hr));
  433. ReportError(hr);
  434. goto UnlockExit;
  435. }
  436. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  437. Function : CKeyUsage::get_IsKeyCertSignEnabled
  438. Synopsis : Check to see if Key Cert Sign bit is set in KeyUsage extension.
  439. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  440. Remark :
  441. ------------------------------------------------------------------------------*/
  442. STDMETHODIMP CKeyUsage::get_IsKeyCertSignEnabled (VARIANT_BOOL * pVal)
  443. {
  444. HRESULT hr = S_OK;
  445. DebugTrace("Entering CKeyUsage::get_IsKeyCertSignEnabled().\n");
  446. // Lock access to this object.
  447. //
  448. m_Lock.Lock();
  449. try
  450. {
  451. //
  452. // Check parameters.
  453. //
  454. if (NULL == pVal)
  455. {
  456. hr = E_INVALIDARG;
  457. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  458. goto ErrorExit;
  459. }
  460. //
  461. // Return result.
  462. //
  463. *pVal = m_dwKeyUsages & CERT_KEY_CERT_SIGN_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  464. }
  465. catch(...)
  466. {
  467. hr = E_POINTER;
  468. DebugTrace("Exception: invalid parameter.\n");
  469. goto ErrorExit;
  470. }
  471. UnlockExit:
  472. //
  473. // Unlock access to this object.
  474. //
  475. m_Lock.Unlock();
  476. DebugTrace("Leaving CKeyUsage::get_IsKeyCertSignEnabled().\n");
  477. return hr;
  478. ErrorExit:
  479. //
  480. // Sanity check.
  481. //
  482. ATLASSERT(FAILED(hr));
  483. ReportError(hr);
  484. goto UnlockExit;
  485. }
  486. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  487. Function : CKeyUsage::get_IsCRLSignEnabled
  488. Synopsis : Check to see if CRL Sign bit is set in KeyUsage extension.
  489. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  490. Remark :
  491. ------------------------------------------------------------------------------*/
  492. STDMETHODIMP CKeyUsage::get_IsCRLSignEnabled (VARIANT_BOOL * pVal)
  493. {
  494. HRESULT hr = S_OK;
  495. DebugTrace("Entering CKeyUsage::get_IsCRLSignEnabled().\n");
  496. // Lock access to this object.
  497. //
  498. m_Lock.Lock();
  499. try
  500. {
  501. //
  502. // Check parameters.
  503. //
  504. if (NULL == pVal)
  505. {
  506. hr = E_INVALIDARG;
  507. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  508. goto ErrorExit;
  509. }
  510. //
  511. // Return result.
  512. //
  513. *pVal = m_dwKeyUsages & CERT_CRL_SIGN_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  514. }
  515. catch(...)
  516. {
  517. hr = E_POINTER;
  518. DebugTrace("Exception: invalid parameter.\n");
  519. goto ErrorExit;
  520. }
  521. UnlockExit:
  522. //
  523. // Unlock access to this object.
  524. //
  525. m_Lock.Unlock();
  526. DebugTrace("Leaving CKeyUsage::get_IsCRLSignEnabled().\n");
  527. return hr;
  528. ErrorExit:
  529. //
  530. // Sanity check.
  531. //
  532. ATLASSERT(FAILED(hr));
  533. ReportError(hr);
  534. goto UnlockExit;
  535. }
  536. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  537. Function : CKeyUsage::get_IsEncipherOnlyEnabled
  538. Synopsis : Check to see if Encipher Only bit is set in KeyUsage extension.
  539. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  540. Remark :
  541. ------------------------------------------------------------------------------*/
  542. STDMETHODIMP CKeyUsage::get_IsEncipherOnlyEnabled (VARIANT_BOOL * pVal)
  543. {
  544. HRESULT hr = S_OK;
  545. DebugTrace("Entering CKeyUsage::get_IsEncipherOnlyEnabled().\n");
  546. // Lock access to this object.
  547. //
  548. m_Lock.Lock();
  549. try
  550. {
  551. //
  552. // Check parameters.
  553. //
  554. if (NULL == pVal)
  555. {
  556. hr = E_INVALIDARG;
  557. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  558. goto ErrorExit;
  559. }
  560. //
  561. // Return result.
  562. //
  563. *pVal = m_dwKeyUsages & CERT_ENCIPHER_ONLY_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  564. }
  565. catch(...)
  566. {
  567. hr = E_POINTER;
  568. DebugTrace("Exception: invalid parameter.\n");
  569. goto ErrorExit;
  570. }
  571. UnlockExit:
  572. //
  573. // Unlock access to this object.
  574. //
  575. m_Lock.Unlock();
  576. DebugTrace("Leaving CKeyUsage::get_IsEncipherOnlyEnabled().\n");
  577. return hr;
  578. ErrorExit:
  579. //
  580. // Sanity check.
  581. //
  582. ATLASSERT(FAILED(hr));
  583. ReportError(hr);
  584. goto UnlockExit;
  585. }
  586. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  587. Function : CKeyUsage::get_IsDecipherOnlyEnabled
  588. Synopsis : Check to see if Decipher Only bit is set in KeyUsage extension.
  589. Parameter: VARIANT_BOOL * pVal - Pointer to VARIANT_BOOL to receive result.
  590. Remark :
  591. ------------------------------------------------------------------------------*/
  592. STDMETHODIMP CKeyUsage::get_IsDecipherOnlyEnabled (VARIANT_BOOL * pVal)
  593. {
  594. HRESULT hr = S_OK;
  595. DebugTrace("Entering CKeyUsage::get_IsDecipherOnlyEnabled().\n");
  596. // Lock access to this object.
  597. //
  598. m_Lock.Lock();
  599. try
  600. {
  601. //
  602. // Check parameters.
  603. //
  604. if (NULL == pVal)
  605. {
  606. hr = E_INVALIDARG;
  607. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  608. goto ErrorExit;
  609. }
  610. //
  611. // Return result.
  612. //
  613. *pVal = m_dwKeyUsages & CERT_DECIPHER_ONLY_KEY_USAGE ? VARIANT_TRUE : VARIANT_FALSE;
  614. }
  615. catch(...)
  616. {
  617. hr = E_POINTER;
  618. DebugTrace("Exception: invalid parameter.\n");
  619. goto ErrorExit;
  620. }
  621. UnlockExit:
  622. //
  623. // Unlock access to this object.
  624. //
  625. m_Lock.Unlock();
  626. DebugTrace("Leaving CKeyUsage::get_IsDecipherOnlyEnabled().\n");
  627. return hr;
  628. ErrorExit:
  629. //
  630. // Sanity check.
  631. //
  632. ATLASSERT(FAILED(hr));
  633. ReportError(hr);
  634. goto UnlockExit;
  635. }
  636. ////////////////////////////////////////////////////////////////////////////////
  637. //
  638. // Private methods.
  639. //
  640. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  641. Function : CKeyUsage::Init
  642. Synopsis : Initialize the object.
  643. Parameter: PCCERT_CONTEXT pCertContext - Pointer to CERT_CONTEXT.
  644. Remark : This method is not part of the COM interface (it is a normal C++
  645. member function). We need it to initialize the object created
  646. internally by us with CERT_CONTEXT.
  647. Since it is only a normal C++ member function, this function can
  648. only be called from a C++ class pointer, not an interface pointer.
  649. ------------------------------------------------------------------------------*/
  650. STDMETHODIMP CKeyUsage::Init (PCCERT_CONTEXT pCertContext)
  651. {
  652. HRESULT hr = S_OK;
  653. DWORD dwKeyUsages = 0;
  654. VARIANT_BOOL bIsPresent = VARIANT_FALSE;
  655. VARIANT_BOOL bIsCritical = VARIANT_FALSE;
  656. DebugTrace("Entering CKeyUsage::Init().\n");
  657. //
  658. // Sanity check.
  659. //
  660. ATLASSERT(pCertContext);
  661. //
  662. // Check the key usage.
  663. //
  664. if (::CertGetIntendedKeyUsage(CAPICOM_ASN_ENCODING,
  665. pCertContext->pCertInfo,
  666. (BYTE *) &dwKeyUsages,
  667. sizeof(dwKeyUsages)))
  668. {
  669. CERT_EXTENSION * pCertExtension;
  670. bIsPresent = VARIANT_TRUE;
  671. //
  672. // Find the extension to see if mark critical.
  673. //
  674. pCertExtension = ::CertFindExtension(szOID_KEY_USAGE ,
  675. pCertContext->pCertInfo->cExtension,
  676. pCertContext->pCertInfo->rgExtension);
  677. if (NULL != pCertExtension)
  678. {
  679. if (pCertExtension->fCritical)
  680. {
  681. bIsCritical = VARIANT_TRUE;
  682. }
  683. }
  684. }
  685. else
  686. {
  687. //
  688. // Could be extension not present or an error.
  689. //
  690. DWORD dwWinError = ::GetLastError();
  691. if (dwWinError)
  692. {
  693. hr = HRESULT_FROM_WIN32(dwWinError);
  694. DebugTrace("Error [%#x]: CertGetIntendedKeyUsage() failed.\n", hr);
  695. goto CommonExit;
  696. }
  697. }
  698. //
  699. // Update member variables.
  700. //
  701. m_bIsPresent = bIsPresent;
  702. m_bIsCritical = bIsCritical;
  703. m_dwKeyUsages = dwKeyUsages;
  704. CommonExit:
  705. DebugTrace("Leaving CKeyUsage::Init().\n");
  706. return hr;
  707. }