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.

3902 lines
96 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000
  3. File: Certificate.cpp
  4. Content: Implementation of CCertificate.
  5. History: 11-15-99 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #include "StdAfx.h"
  8. #include "CAPICOM.h"
  9. #include "Certificate.h"
  10. #include "CertHlpr.h"
  11. #include "Convert.h"
  12. #include "Common.h"
  13. #include "PFXHlpr.h"
  14. #include "PrivateKey.h"
  15. #include "Settings.h"
  16. ////////////////////////////////////////////////////////////////////////////////
  17. //
  18. // typedefs.
  19. //
  20. typedef BOOL (WINAPI * PCRYPTUIDLGVIEWCERTIFICATEW)
  21. (IN PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
  22. OUT BOOL *pfPropertiesChanged);
  23. ////////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Exported functions.
  26. //
  27. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  28. Function : CreateCertificateObject
  29. Synopsis : Create an ICertificate object.
  30. Parameter: PCCERT_CONTEXT pCertContext - Pointer to CERT_CONTEXT to be used
  31. to initialize the ICertificate
  32. object.
  33. DWORD dwCurrentSafety - Current safety setting.
  34. ICertificate2 ** ppICertificate - Pointer to pointer ICertificate
  35. object.
  36. Remark :
  37. ------------------------------------------------------------------------------*/
  38. HRESULT CreateCertificateObject (PCCERT_CONTEXT pCertContext,
  39. DWORD dwCurrentSafety,
  40. ICertificate2 ** ppICertificate)
  41. {
  42. HRESULT hr = S_OK;
  43. CComObject<CCertificate> * pCCertificate = NULL;
  44. DebugTrace("Entering CreateCertificateObject().\n", hr);
  45. //
  46. // Sanity check.
  47. //
  48. ATLASSERT(pCertContext);
  49. ATLASSERT(ppICertificate);
  50. try
  51. {
  52. //
  53. // Create the object. Note that the ref count will still be 0
  54. // after the object is created.
  55. //
  56. if (FAILED(hr = CComObject<CCertificate>::CreateInstance(&pCCertificate)))
  57. {
  58. DebugTrace("Error [%#x]: CComObject<CCertificate>::CreateInstance() failed.\n", hr);
  59. goto ErrorExit;
  60. }
  61. //
  62. // Initialize object.
  63. //
  64. if (FAILED(hr = pCCertificate->PutContext(pCertContext, dwCurrentSafety)))
  65. {
  66. DebugTrace("Error [%#x]: pCCertificate->PutContext() failed.\n", hr);
  67. goto ErrorExit;
  68. }
  69. //
  70. // Return interface pointer to caller.
  71. //
  72. if (FAILED(hr = pCCertificate->QueryInterface(ppICertificate)))
  73. {
  74. DebugTrace("Error [%#x]: pCCertificate->QueryInterface() failed.\n", hr);
  75. goto ErrorExit;
  76. }
  77. }
  78. catch(...)
  79. {
  80. hr = E_POINTER;
  81. DebugTrace("Exception: invalid parameter.\n");
  82. goto ErrorExit;
  83. }
  84. CommonExit:
  85. DebugTrace("Leaving CreateCertificateObject().\n");
  86. return hr;
  87. ErrorExit:
  88. //
  89. // Sanity check.
  90. //
  91. ATLASSERT(FAILED(hr));
  92. if (pCCertificate)
  93. {
  94. delete pCCertificate;
  95. }
  96. goto CommonExit;
  97. }
  98. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  99. Function : GetCertContext
  100. Synopsis : Return the certificate's PCERT_CONTEXT.
  101. Parameter: ICertificate * pICertificate - Pointer to ICertificate for which
  102. the PCERT_CONTEXT is to be returned.
  103. PCCERT_CONTEXT * ppCertContext - Pointer to PCERT_CONTEXT.
  104. Remark :
  105. ------------------------------------------------------------------------------*/
  106. HRESULT GetCertContext (ICertificate * pICertificate,
  107. PCCERT_CONTEXT * ppCertContext)
  108. {
  109. HRESULT hr = S_OK;
  110. CComPtr<ICertContext> pICertContext = NULL;
  111. DebugTrace("Entering GetCertContext().\n");
  112. //
  113. // Sanity check.
  114. //
  115. ATLASSERT(pICertificate);
  116. ATLASSERT(ppCertContext);
  117. //
  118. // Get ICCertificate interface pointer.
  119. //
  120. if (FAILED(hr = pICertificate->QueryInterface(IID_ICertContext, (void **) &pICertContext)))
  121. {
  122. DebugTrace("Error [%#x]: pICertificate->QueryInterface() failed.\n", hr);
  123. goto ErrorExit;
  124. }
  125. //
  126. // Get the CERT_CONTEXT.
  127. //
  128. if (FAILED(hr = pICertContext->get_CertContext((long *) ppCertContext)))
  129. {
  130. DebugTrace("Error [%#x]: pICertContext->get_CertContext() failed.\n", hr);
  131. goto ErrorExit;
  132. }
  133. CommonExit:
  134. DebugTrace("Leaving GetCertContext().\n");
  135. return hr;
  136. ErrorExit:
  137. //
  138. // Sanity check.
  139. //
  140. ATLASSERT(FAILED(hr));
  141. goto CommonExit;
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////
  144. //
  145. // Local functions.
  146. //
  147. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  148. Function : GetCertNameInfo
  149. Synopsis : Return the name for the subject or issuer field.
  150. Parameter: PCCERT_CONTEXT pCertContext - Pointer to CERT_CONTEXT.
  151. DWORD dwNameType - 0 for subject name or CERT_NAME_ISSUER_FLAG
  152. for issuer name.
  153. DWORD dwDisplayType - Display type.
  154. BSTR * pbstrName - Pointer to BSTR to receive resulting name
  155. string.
  156. Remark : It is the caller's responsibility to free the BSTR.
  157. No checking of any of the flags is done, so be sure to call
  158. with the right flags.
  159. ------------------------------------------------------------------------------*/
  160. static HRESULT GetCertNameInfo (PCCERT_CONTEXT pCertContext,
  161. DWORD dwNameType,
  162. DWORD dwDisplayType,
  163. BSTR * pbstrName)
  164. {
  165. HRESULT hr = S_OK;
  166. DWORD cbNameLen = 0;
  167. LPWSTR pwszName = NULL;
  168. DWORD dwStrType = CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG;
  169. DebugTrace("Entering GetCertNameInfo().\n");
  170. //
  171. // Sanity check.
  172. //
  173. ATLASSERT(pCertContext);
  174. ATLASSERT(pbstrName);
  175. //
  176. // Get the length needed.
  177. //
  178. if (!(cbNameLen = ::CertGetNameStringW(pCertContext,
  179. dwDisplayType,
  180. dwNameType,
  181. dwDisplayType == CERT_NAME_RDN_TYPE ? (LPVOID) &dwStrType : NULL,
  182. NULL,
  183. 0)))
  184. {
  185. hr = HRESULT_FROM_WIN32(CRYPT_E_NOT_FOUND);
  186. DebugTrace("Error [%#x]: CertGetNameStringW() failed.\n", hr);
  187. goto ErrorExit;
  188. }
  189. //
  190. // Create returned BSTR.
  191. //
  192. if (!(pwszName = (LPWSTR) ::CoTaskMemAlloc(cbNameLen * sizeof(WCHAR))))
  193. {
  194. hr = E_OUTOFMEMORY;
  195. DebugTrace("Error: out of memory.\n");
  196. goto ErrorExit;
  197. }
  198. //
  199. // Now actually get the name string.
  200. //
  201. if (!::CertGetNameStringW(pCertContext,
  202. dwDisplayType,
  203. dwNameType,
  204. dwDisplayType == CERT_NAME_RDN_TYPE ? (LPVOID) &dwStrType : NULL,
  205. (LPWSTR) pwszName,
  206. cbNameLen))
  207. {
  208. hr = HRESULT_FROM_WIN32(CRYPT_E_NOT_FOUND);
  209. DebugTrace("Error [%#x]: CertGetNameStringW() failed.\n", hr);
  210. goto ErrorExit;
  211. }
  212. //
  213. // Return BSTR to caller.
  214. //
  215. if (!(*pbstrName = ::SysAllocString(pwszName)))
  216. {
  217. hr = E_OUTOFMEMORY;
  218. DebugTrace("Error: out of memory.\n");
  219. goto ErrorExit;
  220. }
  221. CommonExit:
  222. //
  223. // Free resource.
  224. //
  225. if (pwszName)
  226. {
  227. ::CoTaskMemFree(pwszName);
  228. }
  229. DebugTrace("Leaving GetCertNameInfo().\n");
  230. return hr;
  231. ErrorExit:
  232. //
  233. // Sanity check.
  234. //
  235. ATLASSERT(FAILED(hr));
  236. goto CommonExit;
  237. }
  238. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  239. Function : CertToStore
  240. Synopsis : Add the cert, optionally with the chain, to the store.
  241. Parameter: PCCERT_CONTEXT pCertContext - Pointer to CERT_CONTEXT.
  242. CAPICOM_CERTIFICATE_INCLUDE_OPTION IncludeOption - Include option.
  243. HCERTSTORE hCertStore - Store to add to.
  244. Remark :
  245. ------------------------------------------------------------------------------*/
  246. static HRESULT CertToStore(PCCERT_CONTEXT pCertContext,
  247. CAPICOM_CERTIFICATE_INCLUDE_OPTION IncludeOption,
  248. HCERTSTORE hCertStore)
  249. {
  250. HRESULT hr = S_OK;
  251. PCCERT_CHAIN_CONTEXT pChainContext = NULL;
  252. DebugTrace("Entering CertToStore().\n");
  253. //
  254. // Sanity check.
  255. //
  256. ATLASSERT(pCertContext);
  257. ATLASSERT(hCertStore);
  258. //
  259. // No need to build chain if only including end cert.
  260. //
  261. if (CAPICOM_CERTIFICATE_INCLUDE_END_ENTITY_ONLY == IncludeOption)
  262. {
  263. //
  264. // Add the only cert to store.
  265. //
  266. if (!::CertAddCertificateContextToStore(hCertStore,
  267. pCertContext,
  268. CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES,
  269. NULL))
  270. {
  271. hr = HRESULT_FROM_WIN32(::GetLastError());
  272. DebugTrace("Error [%#x]: CertAddCertificateContextToStore() failed.\n", hr);
  273. goto ErrorExit;
  274. }
  275. }
  276. else
  277. {
  278. DWORD i;
  279. BOOL bAddRoot;
  280. CERT_CHAIN_PARA ChainPara = {0};
  281. //
  282. // Initialize.
  283. //
  284. ChainPara.cbSize = sizeof(ChainPara);
  285. switch (IncludeOption)
  286. {
  287. case CAPICOM_CERTIFICATE_INCLUDE_WHOLE_CHAIN:
  288. {
  289. bAddRoot = TRUE;
  290. break;
  291. }
  292. case CAPICOM_CERTIFICATE_INCLUDE_CHAIN_EXCEPT_ROOT:
  293. //
  294. // Fall thru to default.
  295. //
  296. default:
  297. {
  298. bAddRoot = FALSE;
  299. break;
  300. }
  301. }
  302. //
  303. // Build the chain.
  304. //
  305. if (!::CertGetCertificateChain(NULL,
  306. pCertContext,
  307. NULL,
  308. NULL,
  309. &ChainPara,
  310. 0,
  311. NULL,
  312. &pChainContext))
  313. {
  314. hr = HRESULT_FROM_WIN32(::GetLastError());
  315. DebugTrace("Error [%#x]: CertGetCertificateChain() failed.\n", hr);
  316. goto ErrorExit;
  317. }
  318. //
  319. // Sanity check.
  320. //
  321. ATLASSERT(pChainContext->cChain);
  322. //
  323. // Now add the chain to store and skip root cert if requested.
  324. //
  325. for (i = 0; i < pChainContext->rgpChain[0]->cElement; i++)
  326. {
  327. //
  328. // Skip the root cert, if requested.
  329. //
  330. if (!bAddRoot &&
  331. (pChainContext->rgpChain[0]->rgpElement[i]->TrustStatus.dwInfoStatus & CERT_TRUST_IS_SELF_SIGNED))
  332. {
  333. continue;
  334. }
  335. //
  336. // Add to store.
  337. //
  338. if (!::CertAddCertificateContextToStore(hCertStore,
  339. pChainContext->rgpChain[0]->rgpElement[i]->pCertContext,
  340. CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES,
  341. NULL))
  342. {
  343. hr = HRESULT_FROM_WIN32(::GetLastError());
  344. DebugTrace("Error [%#x]: CertAddCertificateContextToStore() failed.\n", hr);
  345. goto ErrorExit;
  346. }
  347. }
  348. }
  349. CommonExit:
  350. //
  351. // Free resource.
  352. //
  353. if (pChainContext)
  354. {
  355. ::CertFreeCertificateChain(pChainContext);
  356. }
  357. DebugTrace("Leaving CertToStore().\n");
  358. return hr;
  359. ErrorExit:
  360. //
  361. // Sanity check.
  362. //
  363. ATLASSERT(FAILED(hr));
  364. goto CommonExit;
  365. }
  366. ////////////////////////////////////////////////////////////////////////////////
  367. //
  368. // CCertificate
  369. //
  370. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  371. Function : CCertificate::get_Version
  372. Synopsis : Return the cert version number.
  373. Parameter: long * pVersion - Pointer to long to receive version number.
  374. Remark : The returned value is 1 for V1, 2 for V2, 3 for V3, and so on.
  375. ------------------------------------------------------------------------------*/
  376. STDMETHODIMP CCertificate::get_Version (long * pVal)
  377. {
  378. HRESULT hr = S_OK;
  379. DebugTrace("Entering CCertificate::get_Version().\n");
  380. try
  381. {
  382. //
  383. // Lock access to this object.
  384. //
  385. m_Lock.Lock();
  386. //
  387. // Check parameters.
  388. //
  389. if (NULL == pVal)
  390. {
  391. hr = E_INVALIDARG;
  392. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  393. goto ErrorExit;
  394. }
  395. //
  396. // Make sure cert is already initialized.
  397. //
  398. if (!m_pCertContext)
  399. {
  400. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  401. DebugTrace("Error [%#x]: Certificate object has not been initalized.\n", hr);
  402. goto ErrorExit;
  403. }
  404. *pVal = (long) m_pCertContext->pCertInfo->dwVersion + 1;
  405. }
  406. catch(...)
  407. {
  408. hr = E_POINTER;
  409. DebugTrace("Exception: invalid parameter.\n");
  410. goto ErrorExit;
  411. }
  412. UnlockExit:
  413. //
  414. // Unlock access to this object.
  415. //
  416. m_Lock.Unlock();
  417. DebugTrace("Leaving CCertificate::get_Version().\n");
  418. return hr;
  419. ErrorExit:
  420. //
  421. // Sanity check.
  422. //
  423. ATLASSERT(FAILED(hr));
  424. ReportError(hr);
  425. goto UnlockExit;
  426. }
  427. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  428. Function : CCertificate::get_SerialNumber
  429. Synopsis : Return the Serial Number field as HEX string in BSTR.
  430. Parameter: BSTR * pVal - Pointer to BSTR to receive the serial number.
  431. Remark : Upper case 'A' - 'F' is used for the returned HEX string with
  432. no embeded space (i.e. 46A2FC01).
  433. ------------------------------------------------------------------------------*/
  434. STDMETHODIMP CCertificate::get_SerialNumber (BSTR * pVal)
  435. {
  436. HRESULT hr = S_OK;
  437. DebugTrace("Entering CCertificate::get_SerialNumber().\n");
  438. try
  439. {
  440. //
  441. // Lock access to this object.
  442. //
  443. m_Lock.Lock();
  444. //
  445. // Check parameters.
  446. //
  447. if (NULL == pVal)
  448. {
  449. hr = E_INVALIDARG;
  450. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  451. goto ErrorExit;
  452. }
  453. //
  454. // Make sure cert is already initialized.
  455. //
  456. if (!m_pCertContext)
  457. {
  458. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  459. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  460. goto ErrorExit;
  461. }
  462. //
  463. // Convert integer blob to BSTR.
  464. //
  465. if (FAILED(hr = ::IntBlobToHexString(&m_pCertContext->pCertInfo->SerialNumber, pVal)))
  466. {
  467. DebugTrace("Error [%#x]: IntBlobToHexString() failed.\n", hr);
  468. goto ErrorExit;
  469. }
  470. }
  471. catch(...)
  472. {
  473. hr = E_POINTER;
  474. DebugTrace("Exception: invalid parameter.\n");
  475. goto ErrorExit;
  476. }
  477. UnlockExit:
  478. //
  479. // Unlock access to this object.
  480. //
  481. m_Lock.Unlock();
  482. DebugTrace("Leaving CCertificate::get_SerialNumber().\n");
  483. return hr;
  484. ErrorExit:
  485. //
  486. // Sanity check.
  487. //
  488. ATLASSERT(FAILED(hr));
  489. ReportError(hr);
  490. goto UnlockExit;
  491. }
  492. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  493. Function : CCertificate::get_SubjectName
  494. Synopsis : Return the Subject field.
  495. Parameter: BSTR * pVal - Pointer to BSTR to receive the subject's name.
  496. Remark : This method returns the full DN in the SubjectName field in the
  497. form of "CN = Daniel Sie OU = Outlook O = Microsoft L = Redmond
  498. S = WA C = US"
  499. The returned name has the same format as specifying
  500. CERT_NAME_RDN_TYPE for the CertGetNameString() API..
  501. ------------------------------------------------------------------------------*/
  502. STDMETHODIMP CCertificate::get_SubjectName (BSTR * pVal)
  503. {
  504. HRESULT hr = S_OK;
  505. DebugTrace("Entering CCertificate::get_SubjectName().\n");
  506. try
  507. {
  508. //
  509. // Lock access to this object.
  510. //
  511. m_Lock.Lock();
  512. //
  513. // Check parameters.
  514. //
  515. if (NULL == pVal)
  516. {
  517. hr = E_INVALIDARG;
  518. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  519. goto ErrorExit;
  520. }
  521. //
  522. // Make sure cert is already initialized.
  523. //
  524. if (!m_pCertContext)
  525. {
  526. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  527. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  528. goto ErrorExit;
  529. }
  530. //
  531. // Return requested name string.
  532. //
  533. if (FAILED(hr = ::GetCertNameInfo(m_pCertContext,
  534. 0,
  535. CERT_NAME_RDN_TYPE,
  536. pVal)))
  537. {
  538. DebugTrace("Error [%#x]: GetCertNameInfo() failed.\n", hr);
  539. goto ErrorExit;
  540. }
  541. }
  542. catch(...)
  543. {
  544. hr = E_POINTER;
  545. DebugTrace("Exception: invalid parameter.\n");
  546. goto ErrorExit;
  547. }
  548. UnlockExit:
  549. //
  550. // Unlock access to this object.
  551. //
  552. m_Lock.Unlock();
  553. DebugTrace("Leaving CCertificate::get_SubjectName().\n");
  554. return hr;
  555. ErrorExit:
  556. //
  557. // Sanity check.
  558. //
  559. ATLASSERT(FAILED(hr));
  560. ReportError(hr);
  561. goto UnlockExit;
  562. }
  563. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  564. Function : CCertificate::get_IssuerName
  565. Synopsis : Return the Issuer field.
  566. Parameter: BSTR * pVal - Pointer to BSTR to receive the issuer's name.
  567. Remark : This method returns the full DN in the IssuerName field in the
  568. form of "CN = Daniel Sie OU = Outlook O = Microsoft L = Redmond
  569. S = WA C = US"
  570. The returned name has the same format as specifying
  571. CERT_NAME_RDN_TYPE for the CertGetNameString() API.
  572. ------------------------------------------------------------------------------*/
  573. STDMETHODIMP CCertificate::get_IssuerName (BSTR * pVal)
  574. {
  575. HRESULT hr = S_OK;
  576. DebugTrace("Entering CCertificate::get_IssuerName().\n");
  577. try
  578. {
  579. //
  580. // Lock access to this object.
  581. //
  582. m_Lock.Lock();
  583. //
  584. // Check parameters.
  585. //
  586. if (NULL == pVal)
  587. {
  588. hr = E_INVALIDARG;
  589. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  590. goto ErrorExit;
  591. }
  592. //
  593. // Make sure cert is already initialized.
  594. //
  595. if (!m_pCertContext)
  596. {
  597. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  598. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  599. goto ErrorExit;
  600. }
  601. //
  602. // Return requested name string.
  603. //
  604. if (FAILED(hr = ::GetCertNameInfo(m_pCertContext,
  605. CERT_NAME_ISSUER_FLAG,
  606. CERT_NAME_RDN_TYPE,
  607. pVal)))
  608. {
  609. DebugTrace("Error [%#x]: GetCertNameInfo() failed.\n", hr);
  610. goto ErrorExit;
  611. }
  612. }
  613. catch(...)
  614. {
  615. hr = E_POINTER;
  616. DebugTrace("Exception: invalid parameter.\n");
  617. goto ErrorExit;
  618. }
  619. UnlockExit:
  620. //
  621. // Unlock access to this object.
  622. //
  623. m_Lock.Unlock();
  624. DebugTrace("Leaving CCertificate::get_IssuerName().\n");
  625. return hr;
  626. ErrorExit:
  627. //
  628. // Sanity check.
  629. //
  630. ATLASSERT(FAILED(hr));
  631. ReportError(hr);
  632. goto UnlockExit;
  633. }
  634. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  635. Function : CCertificate::get_ValidFromDate
  636. Synopsis : Return the NotBefore field.
  637. Parameter: DATE * pDate - Pointer to DATE to receive the valid from date.
  638. Remark :
  639. ------------------------------------------------------------------------------*/
  640. STDMETHODIMP CCertificate::get_ValidFromDate (DATE * pVal)
  641. {
  642. HRESULT hr = S_OK;
  643. FILETIME ftLocal;
  644. SYSTEMTIME stLocal;
  645. DebugTrace("Entering CCertificate::get_ValidFromDate().\n");
  646. try
  647. {
  648. //
  649. // Lock access to this object.
  650. //
  651. m_Lock.Lock();
  652. //
  653. // Check parameters.
  654. //
  655. if (NULL == pVal)
  656. {
  657. hr = E_INVALIDARG;
  658. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  659. goto ErrorExit;
  660. }
  661. //
  662. // Make sure cert is already initialized.
  663. //
  664. if (!m_pCertContext)
  665. {
  666. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  667. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  668. goto ErrorExit;
  669. }
  670. //
  671. // Convert to local time.
  672. //
  673. if (!(::FileTimeToLocalFileTime(&m_pCertContext->pCertInfo->NotBefore, &ftLocal) &&
  674. ::FileTimeToSystemTime(&ftLocal, &stLocal) &&
  675. ::SystemTimeToVariantTime(&stLocal, pVal)))
  676. {
  677. hr = HRESULT_FROM_WIN32(::GetLastError());
  678. DebugTrace("Error [%#x]: unable to convert FILETIME to DATE.\n", hr);
  679. goto ErrorExit;
  680. }
  681. }
  682. catch(...)
  683. {
  684. hr = E_POINTER;
  685. DebugTrace("Exception: invalid parameter.\n");
  686. goto ErrorExit;
  687. }
  688. UnlockExit:
  689. //
  690. // Unlock access to this object.
  691. //
  692. m_Lock.Unlock();
  693. DebugTrace("Leaving CCertificate::get_ValidFromDate().\n");
  694. return hr;
  695. ErrorExit:
  696. //
  697. // Sanity check.
  698. //
  699. ATLASSERT(FAILED(hr));
  700. ReportError(hr);
  701. goto UnlockExit;
  702. }
  703. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  704. Function : CCertificate::get_ValidToDate
  705. Synopsis : Return the NotAfter field.
  706. Parameter: DATE * pDate - Pointer to DATE to receive valid to date.
  707. Remark :
  708. ------------------------------------------------------------------------------*/
  709. STDMETHODIMP CCertificate::get_ValidToDate (DATE * pVal)
  710. {
  711. HRESULT hr = S_OK;
  712. FILETIME ftLocal;
  713. SYSTEMTIME stLocal;
  714. DebugTrace("Entering CCertificate::get_ValidToDate().\n");
  715. try
  716. {
  717. //
  718. // Lock access to this object.
  719. //
  720. m_Lock.Lock();
  721. //
  722. // Check parameters.
  723. //
  724. if (NULL == pVal)
  725. {
  726. hr = E_INVALIDARG;
  727. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  728. goto ErrorExit;
  729. }
  730. //
  731. // Make sure cert is already initialized.
  732. //
  733. if (!m_pCertContext)
  734. {
  735. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  736. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  737. goto ErrorExit;
  738. }
  739. //
  740. // Convert to local time.
  741. //
  742. if (!(::FileTimeToLocalFileTime(&m_pCertContext->pCertInfo->NotAfter, &ftLocal) &&
  743. ::FileTimeToSystemTime(&ftLocal, &stLocal) &&
  744. ::SystemTimeToVariantTime(&stLocal, pVal)))
  745. {
  746. hr = HRESULT_FROM_WIN32(::GetLastError());
  747. DebugTrace("Error [%#x]: unable to convert FILETIME to DATE.\n", hr);
  748. goto ErrorExit;
  749. }
  750. }
  751. catch(...)
  752. {
  753. hr = E_POINTER;
  754. DebugTrace("Exception: invalid parameter.\n");
  755. goto ErrorExit;
  756. }
  757. UnlockExit:
  758. //
  759. // Unlock access to this object.
  760. //
  761. m_Lock.Unlock();
  762. DebugTrace("Leaving CCertificate::get_ValidToDate().\n");
  763. return hr;
  764. ErrorExit:
  765. //
  766. // Sanity check.
  767. //
  768. ATLASSERT(FAILED(hr));
  769. ReportError(hr);
  770. goto UnlockExit;
  771. }
  772. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  773. Function : CCertificate::get_Thumbprint
  774. Synopsis : Return the SHA1 hash as HEX string.
  775. Parameter: BSTR * pVal - Pointer to BSTR to receive hash.
  776. Remark :
  777. ------------------------------------------------------------------------------*/
  778. STDMETHODIMP CCertificate::get_Thumbprint (BSTR * pVal)
  779. {
  780. HRESULT hr = S_OK;
  781. BYTE * pbHash = NULL;
  782. DWORD cbHash = 0;
  783. DebugTrace("Entering CCertificate::get_Thumbprint().\n");
  784. try
  785. {
  786. //
  787. // Lock access to this object.
  788. //
  789. m_Lock.Lock();
  790. //
  791. // Check parameters.
  792. //
  793. if (NULL == pVal)
  794. {
  795. hr = E_INVALIDARG;
  796. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  797. goto ErrorExit;
  798. }
  799. //
  800. // Make sure cert is already initialized.
  801. //
  802. if (!m_pCertContext)
  803. {
  804. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  805. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  806. goto ErrorExit;
  807. }
  808. //
  809. // Calculate length needed.
  810. //
  811. if (!::CertGetCertificateContextProperty(m_pCertContext,
  812. CERT_SHA1_HASH_PROP_ID,
  813. NULL,
  814. &cbHash))
  815. {
  816. hr = HRESULT_FROM_WIN32(::GetLastError());
  817. DebugTrace("Error [%#x]: CertGetCertificateContextProperty() failed.\n", hr);
  818. goto ErrorExit;
  819. }
  820. //
  821. // Allocate memory.
  822. //
  823. if (!(pbHash = (BYTE *) ::CoTaskMemAlloc(cbHash)))
  824. {
  825. hr = E_OUTOFMEMORY;
  826. DebugTrace("Error: out of memory.\n");
  827. goto ErrorExit;
  828. }
  829. //
  830. // Now get the hash.
  831. //
  832. if (!::CertGetCertificateContextProperty(m_pCertContext,
  833. CERT_SHA1_HASH_PROP_ID,
  834. (LPVOID) pbHash,
  835. &cbHash))
  836. {
  837. hr = HRESULT_FROM_WIN32(GetLastError());
  838. DebugTrace("Error [%#x]: CertGetCertificateContextProperty() failed.\n", hr);
  839. goto ErrorExit;
  840. }
  841. //
  842. // Conver to HEX BSTR.
  843. //
  844. if (FAILED(hr = ::BinaryToHexString(pbHash, cbHash, pVal)))
  845. {
  846. DebugTrace("Error [%#x]: BinaryToHexString() failed.\n", hr);
  847. goto ErrorExit;
  848. }
  849. }
  850. catch(...)
  851. {
  852. hr = E_POINTER;
  853. DebugTrace("Exception: invalid parameter.\n");
  854. goto ErrorExit;
  855. }
  856. UnlockExit:
  857. //
  858. // Free resource.
  859. //
  860. if (pbHash)
  861. {
  862. ::CoTaskMemFree((LPVOID) pbHash);
  863. }
  864. //
  865. // Unlock access to this object.
  866. //
  867. m_Lock.Unlock();
  868. DebugTrace("Leaving CCertificate::get_Thumbprint().\n");
  869. return hr;
  870. ErrorExit:
  871. //
  872. // Sanity check.
  873. //
  874. ATLASSERT(FAILED(hr));
  875. ReportError(hr);
  876. goto UnlockExit;
  877. }
  878. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  879. Function : CCertificate::HasPrivateKey
  880. Synopsis : Check to see if the cert has the associated private key.
  881. Parameter: VARIANT_BOOL * pVal - Pointer to BOOL to receive result.
  882. Remark :
  883. ------------------------------------------------------------------------------*/
  884. STDMETHODIMP CCertificate::HasPrivateKey (VARIANT_BOOL * pVal)
  885. {
  886. HRESULT hr = S_OK;
  887. DWORD cb = 0;
  888. DebugTrace("Entering CCertificate::HasPrivateKey().\n");
  889. try
  890. {
  891. //
  892. // Lock access to this object.
  893. //
  894. m_Lock.Lock();
  895. //
  896. // Check parameters.
  897. //
  898. if (NULL == pVal)
  899. {
  900. hr = E_INVALIDARG;
  901. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  902. goto ErrorExit;
  903. }
  904. //
  905. // Make sure cert is already initialized.
  906. //
  907. if (!m_pCertContext)
  908. {
  909. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  910. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  911. goto ErrorExit;
  912. }
  913. //
  914. // Return result.
  915. //
  916. *pVal = ::CertGetCertificateContextProperty(m_pCertContext,
  917. CERT_KEY_PROV_INFO_PROP_ID,
  918. NULL,
  919. &cb) ? VARIANT_TRUE : VARIANT_FALSE;
  920. }
  921. catch(...)
  922. {
  923. hr = E_POINTER;
  924. DebugTrace("Exception: invalid parameter.\n");
  925. goto ErrorExit;
  926. }
  927. UnlockExit:
  928. //
  929. // Unlock access to this object.
  930. //
  931. m_Lock.Unlock();
  932. DebugTrace("Leaving CCertificate::HasPrivateKey().\n");
  933. return hr;
  934. ErrorExit:
  935. //
  936. // Sanity check.
  937. //
  938. ATLASSERT(FAILED(hr));
  939. ReportError(hr);
  940. goto UnlockExit;
  941. }
  942. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  943. Function : CCertificate::GetInfo
  944. Synopsis : Get other simple info from the certificate.
  945. Parameter: CAPICOM_CERT_INFO_TYPE InfoType - Info type
  946. BSTR * pVal - Pointer to BSTR to receive the result.
  947. Remark : Note that an empty string "" is returned if the requested info is
  948. not available in the certificate.
  949. ------------------------------------------------------------------------------*/
  950. STDMETHODIMP CCertificate::GetInfo (CAPICOM_CERT_INFO_TYPE InfoType,
  951. BSTR * pVal)
  952. {
  953. HRESULT hr = S_OK;
  954. DWORD dwFlags = 0;
  955. DWORD dwDisplayType = 0;
  956. DebugTrace("Entering CCertificate::GetInfo().\n");
  957. try
  958. {
  959. //
  960. // Lock access to this object.
  961. //
  962. m_Lock.Lock();
  963. //
  964. // Check parameters.
  965. //
  966. if (NULL == pVal)
  967. {
  968. hr = E_INVALIDARG;
  969. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  970. goto ErrorExit;
  971. }
  972. //
  973. // Make sure cert is already initialized.
  974. //
  975. if (!m_pCertContext)
  976. {
  977. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  978. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  979. goto ErrorExit;
  980. }
  981. //
  982. // Process request.
  983. //
  984. switch (InfoType)
  985. {
  986. case CAPICOM_CERT_INFO_ISSUER_SIMPLE_NAME:
  987. {
  988. dwFlags = CERT_NAME_ISSUER_FLAG;
  989. //
  990. // Warning: dropping thru.
  991. //
  992. }
  993. case CAPICOM_CERT_INFO_SUBJECT_SIMPLE_NAME:
  994. {
  995. //
  996. // Get requested simple name string.
  997. //
  998. dwDisplayType = CERT_NAME_SIMPLE_DISPLAY_TYPE;
  999. break;
  1000. }
  1001. case CAPICOM_CERT_INFO_ISSUER_EMAIL_NAME:
  1002. {
  1003. dwFlags = CERT_NAME_ISSUER_FLAG;
  1004. //
  1005. // Warning: dropping thru.
  1006. //
  1007. }
  1008. case CAPICOM_CERT_INFO_SUBJECT_EMAIL_NAME:
  1009. {
  1010. //
  1011. // Get requested email name string.
  1012. //
  1013. dwDisplayType = CERT_NAME_EMAIL_TYPE;
  1014. break;
  1015. }
  1016. case CAPICOM_CERT_INFO_ISSUER_UPN:
  1017. {
  1018. dwFlags = CERT_NAME_ISSUER_FLAG;
  1019. //
  1020. // Warning: dropping thru.
  1021. //
  1022. }
  1023. case CAPICOM_CERT_INFO_SUBJECT_UPN:
  1024. {
  1025. //
  1026. // Get requested UPN name string.
  1027. //
  1028. dwDisplayType = CERT_NAME_UPN_TYPE;
  1029. break;
  1030. }
  1031. case CAPICOM_CERT_INFO_ISSUER_DNS_NAME:
  1032. {
  1033. dwFlags = CERT_NAME_ISSUER_FLAG;
  1034. //
  1035. // Warning: dropping thru.
  1036. //
  1037. }
  1038. case CAPICOM_CERT_INFO_SUBJECT_DNS_NAME:
  1039. {
  1040. //
  1041. // Get requested DNS name string.
  1042. //
  1043. dwDisplayType = CERT_NAME_DNS_TYPE;
  1044. break;
  1045. }
  1046. default:
  1047. {
  1048. hr = E_INVALIDARG;
  1049. DebugTrace("Error [%#x]: Unknown cert info type (%#x).\n", hr, InfoType);
  1050. goto ErrorExit;
  1051. }
  1052. }
  1053. if (FAILED(hr = ::GetCertNameInfo(m_pCertContext,
  1054. dwFlags,
  1055. dwDisplayType,
  1056. pVal)))
  1057. {
  1058. DebugTrace("Error [%#x]: GetCertNameInfo() failed for display type = %d.\n", hr, dwDisplayType);
  1059. goto ErrorExit;
  1060. }
  1061. }
  1062. catch(...)
  1063. {
  1064. hr = E_POINTER;
  1065. DebugTrace("Exception: invalid parameter.\n");
  1066. goto ErrorExit;
  1067. }
  1068. UnlockExit:
  1069. //
  1070. // Unlock access to this object.
  1071. //
  1072. m_Lock.Unlock();
  1073. DebugTrace("Leaving CCertificate::GetInfo().\n");
  1074. return hr;
  1075. ErrorExit:
  1076. //
  1077. // Sanity check.
  1078. //
  1079. ATLASSERT(FAILED(hr));
  1080. ReportError(hr);
  1081. goto UnlockExit;
  1082. }
  1083. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1084. Function : CCertificate::IsValid
  1085. Synopsis : Return an ICertificateStatus object for certificate validity check.
  1086. Parameter: ICertificateStatus ** pVal - Pointer to pointer to
  1087. ICertificateStatus object to receive
  1088. the interface pointer.
  1089. Remark :
  1090. ------------------------------------------------------------------------------*/
  1091. STDMETHODIMP CCertificate::IsValid (ICertificateStatus ** pVal)
  1092. {
  1093. HRESULT hr = S_OK;
  1094. DebugTrace("Entering CCertificate::IsValid().\n");
  1095. try
  1096. {
  1097. //
  1098. // Lock access to this object.
  1099. //
  1100. m_Lock.Lock();
  1101. //
  1102. // Check parameters.
  1103. //
  1104. if (NULL == pVal)
  1105. {
  1106. hr = E_INVALIDARG;
  1107. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1108. goto ErrorExit;
  1109. }
  1110. //
  1111. // Make sure cert is already initialized.
  1112. //
  1113. if (!m_pCertContext)
  1114. {
  1115. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1116. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1117. goto ErrorExit;
  1118. }
  1119. //
  1120. // Create the embeded ICertificateStatus object, if not already done so.
  1121. //
  1122. if (!m_pICertificateStatus)
  1123. {
  1124. if (FAILED(hr = ::CreateCertificateStatusObject(m_pCertContext, &m_pICertificateStatus)))
  1125. {
  1126. DebugTrace("Error [%#x]: CreateCertificateStatusObject() failed.\n", hr);
  1127. goto ErrorExit;
  1128. }
  1129. }
  1130. //
  1131. // Sanity check.
  1132. //
  1133. ATLASSERT(m_pICertificateStatus);
  1134. //
  1135. // Return interface pointer to user.
  1136. //
  1137. if (FAILED(hr = m_pICertificateStatus->QueryInterface(pVal)))
  1138. {
  1139. DebugTrace("Error [%#x]: m_pICertificateStatus->QueryInterface() failed.\n", hr);
  1140. goto ErrorExit;
  1141. }
  1142. }
  1143. catch(...)
  1144. {
  1145. hr = E_POINTER;
  1146. DebugTrace("Exception: invalid parameter.\n");
  1147. goto ErrorExit;
  1148. }
  1149. UnlockExit:
  1150. //
  1151. // Unlock access to this object.
  1152. //
  1153. m_Lock.Unlock();
  1154. DebugTrace("Leaving CCertificate::IsValid().\n");
  1155. return hr;
  1156. ErrorExit:
  1157. //
  1158. // Sanity check.
  1159. //
  1160. ATLASSERT(FAILED(hr));
  1161. ReportError(hr);
  1162. goto UnlockExit;
  1163. }
  1164. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1165. Function : CCertificate::KeyUsage
  1166. Synopsis : Return the Key Usage extension as an IKeyUsage object.
  1167. Parameter: IKeyUsage ** pVal - Pointer to pointer to IKeyUsage to receive the
  1168. interface pointer.
  1169. Remark :
  1170. ------------------------------------------------------------------------------*/
  1171. STDMETHODIMP CCertificate::KeyUsage (IKeyUsage ** pVal)
  1172. {
  1173. HRESULT hr = S_OK;
  1174. DebugTrace("Entering CCertificate::KeyUsage().\n");
  1175. try
  1176. {
  1177. //
  1178. // Lock access to this object.
  1179. //
  1180. m_Lock.Lock();
  1181. //
  1182. // Check parameters.
  1183. //
  1184. if (NULL == pVal)
  1185. {
  1186. hr = E_INVALIDARG;
  1187. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1188. goto ErrorExit;
  1189. }
  1190. //
  1191. // Make sure cert is already initialized.
  1192. //
  1193. if (!m_pCertContext)
  1194. {
  1195. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1196. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1197. goto ErrorExit;
  1198. }
  1199. //
  1200. // Create the embeded IKeyUsage object, if not already done so.
  1201. //
  1202. if (!m_pIKeyUsage)
  1203. {
  1204. if (FAILED(hr = ::CreateKeyUsageObject(m_pCertContext, &m_pIKeyUsage)))
  1205. {
  1206. DebugTrace("Error [%#x]: CreateKeyUsageObject() failed.\n", hr);
  1207. goto ErrorExit;
  1208. }
  1209. }
  1210. //
  1211. // Sanity check.
  1212. //
  1213. ATLASSERT(m_pIKeyUsage);
  1214. //
  1215. // Return interface pointer to user.
  1216. //
  1217. if (FAILED(hr = m_pIKeyUsage->QueryInterface(pVal)))
  1218. {
  1219. DebugTrace("Error [%#x]: m_pIKeyUsage->QueryInterface() failed.\n", hr);
  1220. goto ErrorExit;
  1221. }
  1222. }
  1223. catch(...)
  1224. {
  1225. hr = E_POINTER;
  1226. DebugTrace("Exception: invalid parameter.\n");
  1227. goto ErrorExit;
  1228. }
  1229. UnlockExit:
  1230. //
  1231. // Unlock access to this object.
  1232. //
  1233. m_Lock.Unlock();
  1234. DebugTrace("Leaving CCertificate::KeyUsage().\n");
  1235. return hr;
  1236. ErrorExit:
  1237. //
  1238. // Sanity check.
  1239. //
  1240. ATLASSERT(FAILED(hr));
  1241. ReportError(hr);
  1242. goto UnlockExit;
  1243. }
  1244. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1245. Function : CCertificate::ExtendedKeyUsage
  1246. Synopsis : Return the EKU extension as an IExtendedKeyUsage object.
  1247. Parameter: IExtendedKeyUsage ** pVal - Pointer to pointer to IExtendedKeyUsage
  1248. to receive the interface pointer.
  1249. Remark :
  1250. ------------------------------------------------------------------------------*/
  1251. STDMETHODIMP CCertificate::ExtendedKeyUsage (IExtendedKeyUsage ** pVal)
  1252. {
  1253. HRESULT hr = S_OK;
  1254. DebugTrace("Entering CCertificate::ExtendedKeyUsage().\n");
  1255. try
  1256. {
  1257. //
  1258. // Lock access to this object.
  1259. //
  1260. m_Lock.Lock();
  1261. //
  1262. // Check parameters.
  1263. //
  1264. if (NULL == pVal)
  1265. {
  1266. hr = E_INVALIDARG;
  1267. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1268. goto ErrorExit;
  1269. }
  1270. //
  1271. // Make sure cert is already initialized.
  1272. //
  1273. if (!m_pCertContext)
  1274. {
  1275. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1276. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1277. goto ErrorExit;
  1278. }
  1279. //
  1280. // Create the embeded IExtendedKeyUsage object, if not already done so.
  1281. //
  1282. if (!m_pIExtendedKeyUsage)
  1283. {
  1284. if (FAILED(hr = ::CreateExtendedKeyUsageObject(m_pCertContext, &m_pIExtendedKeyUsage)))
  1285. {
  1286. DebugTrace("Error [%#x]: CreateExtendedKeyUsageObject() failed.\n", hr);
  1287. goto ErrorExit;
  1288. }
  1289. }
  1290. //
  1291. // Sanity check.
  1292. //
  1293. ATLASSERT(m_pIExtendedKeyUsage);
  1294. //
  1295. // Return interface pointer to user.
  1296. //
  1297. if (FAILED(hr = m_pIExtendedKeyUsage->QueryInterface(pVal)))
  1298. {
  1299. DebugTrace("Error [%#x]: m_pIExtendedKeyUsage->QueryInterface() failed.\n", hr);
  1300. goto ErrorExit;
  1301. }
  1302. }
  1303. catch(...)
  1304. {
  1305. hr = E_POINTER;
  1306. DebugTrace("Exception: invalid parameter.\n");
  1307. goto ErrorExit;
  1308. }
  1309. UnlockExit:
  1310. //
  1311. // Unlock access to this object.
  1312. //
  1313. m_Lock.Unlock();
  1314. DebugTrace("Leaving CCertificate::ExtendedKeyUsage().\n");
  1315. return hr;
  1316. ErrorExit:
  1317. //
  1318. // Sanity check.
  1319. //
  1320. ATLASSERT(FAILED(hr));
  1321. ReportError(hr);
  1322. goto UnlockExit;
  1323. }
  1324. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1325. Function : CCertificate::BasicConstraints
  1326. Synopsis : Return the BasicConstraints extension as an IBasicConstraints
  1327. object.
  1328. Parameter: IBasicConstraints ** pVal - Pointer to pointer to IBasicConstraints
  1329. to receive the interface pointer.
  1330. Remark :
  1331. ------------------------------------------------------------------------------*/
  1332. STDMETHODIMP CCertificate::BasicConstraints (IBasicConstraints ** pVal)
  1333. {
  1334. HRESULT hr = S_OK;
  1335. DebugTrace("Entering CCertificate::BasicConstraints().\n");
  1336. try
  1337. {
  1338. //
  1339. // Lock access to this object.
  1340. //
  1341. m_Lock.Lock();
  1342. //
  1343. // Check parameters.
  1344. //
  1345. if (NULL == pVal)
  1346. {
  1347. hr = E_INVALIDARG;
  1348. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1349. goto ErrorExit;
  1350. }
  1351. //
  1352. // Make sure cert is already initialized.
  1353. //
  1354. if (!m_pCertContext)
  1355. {
  1356. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1357. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1358. goto ErrorExit;
  1359. }
  1360. //
  1361. // Create the embeded IBasicConstraints object, if not already done so.
  1362. //
  1363. if (!m_pIBasicConstraints)
  1364. {
  1365. if (FAILED(hr = ::CreateBasicConstraintsObject(m_pCertContext, &m_pIBasicConstraints)))
  1366. {
  1367. DebugTrace("Error [%#x]: CreateBasicConstraintsObject() failed.\n", hr);
  1368. goto ErrorExit;
  1369. }
  1370. }
  1371. //
  1372. // Sanity check.
  1373. //
  1374. ATLASSERT(m_pIBasicConstraints);
  1375. //
  1376. // Return interface pointer to user.
  1377. //
  1378. if (FAILED(hr = m_pIBasicConstraints->QueryInterface(pVal)))
  1379. {
  1380. DebugTrace("Error [%#x]: m_pIBasicConstraints->QueryInterface() failed.\n", hr);
  1381. goto ErrorExit;
  1382. }
  1383. }
  1384. catch(...)
  1385. {
  1386. hr = E_POINTER;
  1387. DebugTrace("Exception: invalid parameter.\n");
  1388. goto ErrorExit;
  1389. }
  1390. UnlockExit:
  1391. //
  1392. // Unlock access to this object.
  1393. //
  1394. m_Lock.Unlock();
  1395. DebugTrace("Leaving CCertificate::BasicConstraints().\n");
  1396. return hr;
  1397. ErrorExit:
  1398. //
  1399. // Sanity check.
  1400. //
  1401. ATLASSERT(FAILED(hr));
  1402. ReportError(hr);
  1403. goto UnlockExit;
  1404. }
  1405. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1406. Function : CCertificate::Export
  1407. Synopsis : Export the certificate.
  1408. Parameter: CAPICOM_ENCODING_TYPE EncodingType - Encoding type.
  1409. BSTR * pVal - Pointer to BSTR to receive the certificate blob.
  1410. Remark :
  1411. ------------------------------------------------------------------------------*/
  1412. STDMETHODIMP CCertificate::Export (CAPICOM_ENCODING_TYPE EncodingType,
  1413. BSTR * pVal)
  1414. {
  1415. HRESULT hr = S_OK;
  1416. DATA_BLOB CertBlob = {0, NULL};
  1417. DebugTrace("Entering CCertificate::Export().\n");
  1418. try
  1419. {
  1420. //
  1421. // Lock access to this object.
  1422. //
  1423. m_Lock.Lock();
  1424. //
  1425. // Check parameters.
  1426. //
  1427. if (NULL == pVal)
  1428. {
  1429. hr = E_INVALIDARG;
  1430. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1431. goto ErrorExit;
  1432. }
  1433. //
  1434. // Make sure cert is already initialized.
  1435. //
  1436. if (!m_pCertContext)
  1437. {
  1438. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1439. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1440. goto ErrorExit;
  1441. }
  1442. //
  1443. // Determine encoding type.
  1444. //
  1445. CertBlob.cbData = m_pCertContext->cbCertEncoded;
  1446. CertBlob.pbData = m_pCertContext->pbCertEncoded;
  1447. //
  1448. // Export certificate.
  1449. //
  1450. if (FAILED(hr = ::ExportData(CertBlob, EncodingType, pVal)))
  1451. {
  1452. DebugTrace("Error [%#x]: ExportData() failed.\n", hr);
  1453. goto ErrorExit;
  1454. }
  1455. }
  1456. catch(...)
  1457. {
  1458. hr = E_POINTER;
  1459. DebugTrace("Exception: invalid parameter.\n");
  1460. goto ErrorExit;
  1461. }
  1462. UnlockExit:
  1463. //
  1464. // Unlock access to this object.
  1465. //
  1466. m_Lock.Unlock();
  1467. DebugTrace("Leaving CCertificate::Export().\n");
  1468. return hr;
  1469. ErrorExit:
  1470. //
  1471. // Sanity check.
  1472. //
  1473. ATLASSERT(FAILED(hr));
  1474. ReportError(hr);
  1475. goto UnlockExit;
  1476. }
  1477. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1478. Function : CCertificate::Import
  1479. Synopsis : Imoprt a certificate.
  1480. Parameter: BSTR EncodedCertificate - BSTR containing the encoded certificate
  1481. blob.
  1482. Remark :
  1483. ------------------------------------------------------------------------------*/
  1484. STDMETHODIMP CCertificate::Import (BSTR EncodedCertificate)
  1485. {
  1486. HRESULT hr = S_OK;
  1487. DATA_BLOB CertBlob = {0, NULL};
  1488. DebugTrace("Entering CCertificate::Import().\n");
  1489. try
  1490. {
  1491. //
  1492. // Lock access to this object.
  1493. //
  1494. m_Lock.Lock();
  1495. //
  1496. // Make sure parameter is valid.
  1497. //
  1498. if ((NULL == (CertBlob.pbData = (LPBYTE) EncodedCertificate)) ||
  1499. (0 == (CertBlob.cbData = ::SysStringByteLen(EncodedCertificate))))
  1500. {
  1501. hr = E_INVALIDARG;
  1502. DebugTrace("Error [%#x]: EncodedCertificate = %#x, SysStringByteLen(EncodedCertificate) = %d.\n",
  1503. hr, EncodedCertificate, EncodedCertificate);
  1504. goto ErrorExit;
  1505. }
  1506. //
  1507. // Now import the blob.
  1508. //
  1509. if (FAILED(hr = ImportBlob(&CertBlob,
  1510. FALSE,
  1511. (CAPICOM_KEY_LOCATION) 0,
  1512. NULL,
  1513. (CAPICOM_KEY_STORAGE_FLAG) 0)))
  1514. {
  1515. DebugTrace("Error [%#x]: CCertificate::ImportBlob() failed.\n", hr);
  1516. goto ErrorExit;
  1517. }
  1518. }
  1519. catch(...)
  1520. {
  1521. hr = E_POINTER;
  1522. DebugTrace("Exception: invalid parameter.\n");
  1523. goto ErrorExit;
  1524. }
  1525. UnlockExit:
  1526. //
  1527. // Unlock access to this object.
  1528. //
  1529. m_Lock.Unlock();
  1530. DebugTrace("Entering CCertificate::Import().\n");
  1531. return hr;
  1532. ErrorExit:
  1533. //
  1534. // Sanity check.
  1535. //
  1536. ATLASSERT(FAILED(hr));
  1537. ReportError(hr);
  1538. goto UnlockExit;
  1539. }
  1540. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1541. Function : CCertificate::Display
  1542. Synopsis : Display the certificate using CryptUIDlgViewCertificateW() API.
  1543. Parameter: None
  1544. Remark :
  1545. ------------------------------------------------------------------------------*/
  1546. STDMETHODIMP CCertificate::Display()
  1547. {
  1548. HRESULT hr = S_OK;
  1549. HINSTANCE hDLL = NULL;
  1550. PCRYPTUIDLGVIEWCERTIFICATEW pCryptUIDlgViewCertificateW = NULL;
  1551. CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo;
  1552. DebugTrace("Entering CCertificate::Display().\n");
  1553. //
  1554. // Lock access to this object.
  1555. //
  1556. m_Lock.Lock();
  1557. //
  1558. // Make sure cert is already initialized.
  1559. //
  1560. if (!m_pCertContext)
  1561. {
  1562. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1563. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1564. goto ErrorExit;
  1565. }
  1566. //
  1567. // Get pointer to CryptUIDlgViewCertificateW().
  1568. //
  1569. if (hDLL = ::LoadLibrary("CryptUI.dll"))
  1570. {
  1571. pCryptUIDlgViewCertificateW = (PCRYPTUIDLGVIEWCERTIFICATEW) ::GetProcAddress(hDLL, "CryptUIDlgViewCertificateW");
  1572. }
  1573. //
  1574. // Is CryptUIDlgViewCertificateW() available?
  1575. //
  1576. if (!pCryptUIDlgViewCertificateW)
  1577. {
  1578. hr = CAPICOM_E_NOT_SUPPORTED;
  1579. DebugTrace("Error: CryptUIDlgViewCertificateW() API not available.\n");
  1580. goto ErrorExit;
  1581. }
  1582. //
  1583. // Initialize view structure.
  1584. //
  1585. ::ZeroMemory((void *) &ViewInfo, sizeof(ViewInfo));
  1586. ViewInfo.dwSize = sizeof(ViewInfo);
  1587. ViewInfo.pCertContext = m_pCertContext;
  1588. //
  1589. // View it.
  1590. //
  1591. if (!pCryptUIDlgViewCertificateW(&ViewInfo, 0))
  1592. {
  1593. //
  1594. // CryptUIDlgViewCertificateW() returns ERROR_CANCELLED if user closed
  1595. // the window through the x button!!!
  1596. //
  1597. DWORD dwWinError = ::GetLastError();
  1598. if (ERROR_CANCELLED != dwWinError)
  1599. {
  1600. hr = HRESULT_FROM_WIN32(dwWinError);
  1601. DebugTrace("Error [%#x]: CryptUIDlgViewCertificateW() failed.\n", hr);
  1602. goto ErrorExit;
  1603. }
  1604. }
  1605. UnlockExit:
  1606. //
  1607. // Release resources.
  1608. //
  1609. if (hDLL)
  1610. {
  1611. ::FreeLibrary(hDLL);
  1612. }
  1613. //
  1614. // Unlock access to this object.
  1615. //
  1616. m_Lock.Unlock();
  1617. DebugTrace("Leaving CCertificate::Display().\n");
  1618. return hr;
  1619. ErrorExit:
  1620. //
  1621. // Sanity check.
  1622. //
  1623. ATLASSERT(FAILED(hr));
  1624. ReportError(hr);
  1625. goto UnlockExit;
  1626. }
  1627. ////////////////////////////////////////////////////////////////////////////////
  1628. //
  1629. // Certificate2
  1630. //
  1631. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1632. Function : CCertificate::get_Archived
  1633. Synopsis : Return the archived status.
  1634. Parameter: VARIANT_BOOL * pVal - Pointer to BOOL to receive result.
  1635. Remark :
  1636. ------------------------------------------------------------------------------*/
  1637. STDMETHODIMP CCertificate::get_Archived (VARIANT_BOOL * pVal)
  1638. {
  1639. HRESULT hr = S_OK;
  1640. DWORD cb = 0;
  1641. DebugTrace("Entering CCertificate::get_Archived().\n");
  1642. try
  1643. {
  1644. //
  1645. // Lock access to this object.
  1646. //
  1647. m_Lock.Lock();
  1648. //
  1649. // Check parameters.
  1650. //
  1651. if (NULL == pVal)
  1652. {
  1653. hr = E_INVALIDARG;
  1654. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1655. goto ErrorExit;
  1656. }
  1657. //
  1658. // Make sure cert is already initialized.
  1659. //
  1660. if (!m_pCertContext)
  1661. {
  1662. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1663. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1664. goto ErrorExit;
  1665. }
  1666. //
  1667. // Return result.
  1668. //
  1669. *pVal = ::CertGetCertificateContextProperty(m_pCertContext,
  1670. CERT_ARCHIVED_PROP_ID,
  1671. NULL,
  1672. &cb) ? VARIANT_TRUE : VARIANT_FALSE;
  1673. }
  1674. catch(...)
  1675. {
  1676. hr = E_POINTER;
  1677. DebugTrace("Exception: invalid parameter.\n");
  1678. goto ErrorExit;
  1679. }
  1680. UnlockExit:
  1681. //
  1682. // Unlock access to this object.
  1683. //
  1684. m_Lock.Unlock();
  1685. DebugTrace("Leaving CCertificate::get_Archived().\n");
  1686. return hr;
  1687. ErrorExit:
  1688. //
  1689. // Sanity check.
  1690. //
  1691. ATLASSERT(FAILED(hr));
  1692. ReportError(hr);
  1693. goto UnlockExit;
  1694. }
  1695. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1696. Function : CCertificate::put_Archived
  1697. Synopsis : Set the archived status.
  1698. Parameter: VARIANT_BOOL newVal - Pointer to BOOL to receive result.
  1699. Remark :
  1700. ------------------------------------------------------------------------------*/
  1701. STDMETHODIMP CCertificate::put_Archived (VARIANT_BOOL newVal)
  1702. {
  1703. HRESULT hr = S_OK;
  1704. LPVOID pvData = NULL;
  1705. DATA_BLOB DataBlob = {0, NULL};
  1706. DebugTrace("Entering CCertificate::put_Archived().\n");
  1707. try
  1708. {
  1709. //
  1710. // Lock access to this object.
  1711. //
  1712. m_Lock.Lock();
  1713. //
  1714. // Make sure cert is already initialized.
  1715. //
  1716. if (!m_pCertContext)
  1717. {
  1718. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1719. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1720. goto ErrorExit;
  1721. }
  1722. //
  1723. // Not allowed if called from WEB script.
  1724. //
  1725. if (m_dwCurrentSafety)
  1726. {
  1727. hr = CAPICOM_E_NOT_ALLOWED;
  1728. DebugTrace("Error [%#x]: Changing archived bit from within WEB script is not allowed.\n", hr);
  1729. goto ErrorExit;
  1730. }
  1731. //
  1732. // Set/Reset archive property.
  1733. //
  1734. if (newVal)
  1735. {
  1736. pvData = (LPVOID) &DataBlob;
  1737. }
  1738. if (!::CertSetCertificateContextProperty(m_pCertContext,
  1739. CERT_ARCHIVED_PROP_ID,
  1740. 0,
  1741. pvData))
  1742. {
  1743. hr = HRESULT_FROM_WIN32(::GetLastError());
  1744. DebugTrace("Error [%#x]: CertSetCertificateContextProperty() failed.\n", hr);
  1745. goto ErrorExit;
  1746. }
  1747. }
  1748. catch(...)
  1749. {
  1750. hr = E_POINTER;
  1751. DebugTrace("Exception: invalid parameter.\n");
  1752. goto ErrorExit;
  1753. }
  1754. UnlockExit:
  1755. //
  1756. // Unlock access to this object.
  1757. //
  1758. m_Lock.Unlock();
  1759. DebugTrace("Leaving CCertificate::put_Archived().\n");
  1760. return hr;
  1761. ErrorExit:
  1762. //
  1763. // Sanity check.
  1764. //
  1765. ATLASSERT(FAILED(hr));
  1766. ReportError(hr);
  1767. goto UnlockExit;
  1768. }
  1769. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1770. Function : CCertificate::Template
  1771. Synopsis : Return the ITemplate object.
  1772. Parameter: ITemplate ** pVal - Pointer to pointer to ITemplate
  1773. to receive the interface pointer.
  1774. Remark :
  1775. ------------------------------------------------------------------------------*/
  1776. STDMETHODIMP CCertificate::Template (ITemplate ** pVal)
  1777. {
  1778. HRESULT hr = S_OK;
  1779. DebugTrace("Entering CCertificate::Template().\n");
  1780. try
  1781. {
  1782. //
  1783. // Lock access to this object.
  1784. //
  1785. m_Lock.Lock();
  1786. //
  1787. // Check parameters.
  1788. //
  1789. if (NULL == pVal)
  1790. {
  1791. hr = E_INVALIDARG;
  1792. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1793. goto ErrorExit;
  1794. }
  1795. //
  1796. // Make sure cert is already initialized.
  1797. //
  1798. if (!m_pCertContext)
  1799. {
  1800. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1801. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1802. goto ErrorExit;
  1803. }
  1804. //
  1805. // Create the embeded ITemplate object, if not already done so.
  1806. //
  1807. if (!m_pITemplate)
  1808. {
  1809. if (FAILED(hr = ::CreateTemplateObject(m_pCertContext, &m_pITemplate)))
  1810. {
  1811. DebugTrace("Error [%#x]: CreateTemplateObject() failed.\n", hr);
  1812. goto ErrorExit;
  1813. }
  1814. }
  1815. //
  1816. // Sanity check.
  1817. //
  1818. ATLASSERT(m_pITemplate);
  1819. //
  1820. // Return interface pointer to user.
  1821. //
  1822. if (FAILED(hr = m_pITemplate->QueryInterface(pVal)))
  1823. {
  1824. DebugTrace("Error [%#x]: m_pITemplate->QueryInterface() failed.\n", hr);
  1825. goto ErrorExit;
  1826. }
  1827. ATLASSERT(*pVal);
  1828. DebugTrace("Info: ITemplate vtable value = %#x\n", (PVOID) *pVal);
  1829. }
  1830. catch(...)
  1831. {
  1832. hr = E_POINTER;
  1833. DebugTrace("Exception: invalid parameter.\n");
  1834. goto ErrorExit;
  1835. }
  1836. UnlockExit:
  1837. //
  1838. // Unlock access to this object.
  1839. //
  1840. m_Lock.Unlock();
  1841. DebugTrace("Leaving CCertificate::Template().\n");
  1842. return hr;
  1843. ErrorExit:
  1844. //
  1845. // Sanity check.
  1846. //
  1847. ATLASSERT(FAILED(hr));
  1848. ReportError(hr);
  1849. goto UnlockExit;
  1850. }
  1851. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1852. Function : CCertificate::PublicKey
  1853. Synopsis : Return the IPublicKey object.
  1854. Parameter: IPublicKey ** pVal - Pointer to pointer to IPublicKey
  1855. to receive the interface pointer.
  1856. Remark :
  1857. ------------------------------------------------------------------------------*/
  1858. STDMETHODIMP CCertificate::PublicKey (IPublicKey ** pVal)
  1859. {
  1860. HRESULT hr = S_OK;
  1861. DebugTrace("Entering CCertificate::PublicKey().\n");
  1862. try
  1863. {
  1864. //
  1865. // Lock access to this object.
  1866. //
  1867. m_Lock.Lock();
  1868. //
  1869. // Check parameters.
  1870. //
  1871. if (NULL == pVal)
  1872. {
  1873. hr = E_INVALIDARG;
  1874. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1875. goto ErrorExit;
  1876. }
  1877. //
  1878. // Make sure cert is already initialized.
  1879. //
  1880. if (!m_pCertContext)
  1881. {
  1882. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1883. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1884. goto ErrorExit;
  1885. }
  1886. //
  1887. // Create the embeded IPublicKey object, if not already done so.
  1888. //
  1889. if (!m_pIPublicKey)
  1890. {
  1891. if (FAILED(hr = ::CreatePublicKeyObject(m_pCertContext, &m_pIPublicKey)))
  1892. {
  1893. DebugTrace("Error [%#x]: CreatePublicKeybject() failed.\n", hr);
  1894. goto ErrorExit;
  1895. }
  1896. }
  1897. //
  1898. // Sanity check.
  1899. //
  1900. ATLASSERT(m_pIPublicKey);
  1901. //
  1902. // Return interface pointer to user.
  1903. //
  1904. if (FAILED(hr = m_pIPublicKey->QueryInterface(pVal)))
  1905. {
  1906. DebugTrace("Error [%#x]: m_pIPublicKey->QueryInterface() failed.\n", hr);
  1907. goto ErrorExit;
  1908. }
  1909. ATLASSERT(*pVal);
  1910. DebugTrace("Info: IPublicKey vtable value = %#x\n", (PVOID) *pVal);
  1911. }
  1912. catch(...)
  1913. {
  1914. hr = E_POINTER;
  1915. DebugTrace("Exception: invalid parameter.\n");
  1916. goto ErrorExit;
  1917. }
  1918. UnlockExit:
  1919. //
  1920. // Unlock access to this object.
  1921. //
  1922. m_Lock.Unlock();
  1923. DebugTrace("Leaving CCertificate::PublicKey().\n");
  1924. return hr;
  1925. ErrorExit:
  1926. //
  1927. // Sanity check.
  1928. //
  1929. ATLASSERT(FAILED(hr));
  1930. ReportError(hr);
  1931. goto UnlockExit;
  1932. }
  1933. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1934. Function : CCertificate::get_PrivateKey
  1935. Synopsis : Return the IPrivateKey object.
  1936. Parameter: IPrivateKey ** pVal - Pointer to pointer to IPrivateKey
  1937. to receive the interface pointer.
  1938. Remark :
  1939. ------------------------------------------------------------------------------*/
  1940. STDMETHODIMP CCertificate::get_PrivateKey (IPrivateKey ** pVal)
  1941. {
  1942. HRESULT hr = S_OK;
  1943. CComPtr<IPrivateKey> pIPrivateKey = NULL;
  1944. DebugTrace("Entering CCertificate::get_PrivateKey().\n");
  1945. try
  1946. {
  1947. //
  1948. // Lock access to this object.
  1949. //
  1950. m_Lock.Lock();
  1951. //
  1952. // Check parameters.
  1953. //
  1954. if (NULL == pVal)
  1955. {
  1956. hr = E_INVALIDARG;
  1957. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  1958. goto ErrorExit;
  1959. }
  1960. //
  1961. // Make sure cert is already initialized.
  1962. //
  1963. if (!m_pCertContext)
  1964. {
  1965. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  1966. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  1967. goto ErrorExit;
  1968. }
  1969. //
  1970. // Create the object on the fly (read-only if called from WEB script).
  1971. //
  1972. if (FAILED(hr = ::CreatePrivateKeyObject(m_pCertContext, m_dwCurrentSafety ? TRUE : FALSE, &pIPrivateKey)))
  1973. {
  1974. DebugTrace("Error [%#x]: CreatePrivateKeybject() failed.\n", hr);
  1975. goto ErrorExit;
  1976. }
  1977. //
  1978. // Return interface pointer to user.
  1979. //
  1980. if (FAILED(hr = pIPrivateKey->QueryInterface(pVal)))
  1981. {
  1982. DebugTrace("Error [%#x]: pIPrivateKey->QueryInterface() failed.\n", hr);
  1983. goto ErrorExit;
  1984. }
  1985. }
  1986. catch(...)
  1987. {
  1988. hr = E_POINTER;
  1989. DebugTrace("Exception: invalid parameter.\n");
  1990. goto ErrorExit;
  1991. }
  1992. UnlockExit:
  1993. //
  1994. // Unlock access to this object.
  1995. //
  1996. m_Lock.Unlock();
  1997. DebugTrace("Leaving CCertificate::get_PrivateKey().\n");
  1998. return hr;
  1999. ErrorExit:
  2000. //
  2001. // Sanity check.
  2002. //
  2003. ATLASSERT(FAILED(hr));
  2004. ReportError(hr);
  2005. goto UnlockExit;
  2006. }
  2007. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2008. Function : CCertificate::put_PrivateKey
  2009. Synopsis : Set the IPrivateKey object.
  2010. Parameter: IPrivateKey * newVal - Pointer to IPrivateKey.
  2011. Remark :
  2012. ------------------------------------------------------------------------------*/
  2013. STDMETHODIMP CCertificate::put_PrivateKey (IPrivateKey * newVal)
  2014. {
  2015. HRESULT hr = S_OK;
  2016. PCRYPT_KEY_PROV_INFO pKeyProvInfo = NULL;
  2017. DebugTrace("Entering CCertificate::put_PrivateKey().\n");
  2018. try
  2019. {
  2020. //
  2021. // Lock access to this object.
  2022. //
  2023. m_Lock.Lock();
  2024. //
  2025. // Not allowed if called from WEB script.
  2026. //
  2027. if (m_dwCurrentSafety)
  2028. {
  2029. hr = CAPICOM_E_NOT_ALLOWED;
  2030. DebugTrace("Error [%#x]: Changing PrivateKey from within WEB script is not allowed.\n", hr);
  2031. goto ErrorExit;
  2032. }
  2033. //
  2034. // Check parameters.
  2035. //
  2036. if (NULL == newVal)
  2037. {
  2038. hr = E_INVALIDARG;
  2039. DebugTrace("Error [%#x]: Parameter newVal is NULL.\n", hr);
  2040. goto ErrorExit;
  2041. }
  2042. //
  2043. // Make sure cert is already initialized.
  2044. //
  2045. if (!m_pCertContext)
  2046. {
  2047. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  2048. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  2049. goto ErrorExit;
  2050. }
  2051. //
  2052. // NULL to disassociate.
  2053. //
  2054. if (newVal)
  2055. {
  2056. //
  2057. // Get copy of key prov info.
  2058. //
  2059. if (FAILED(hr = ::GetKeyProvInfo(newVal, &pKeyProvInfo)))
  2060. {
  2061. DebugTrace("Error [%#x]: GetKeyProvInfo() failed.\n", hr);
  2062. goto ErrorExit;
  2063. }
  2064. //
  2065. // Make sure public keys match.
  2066. //
  2067. if (FAILED(hr = ::CompareCertAndContainerPublicKey(m_pCertContext,
  2068. pKeyProvInfo->pwszContainerName,
  2069. pKeyProvInfo->pwszProvName,
  2070. pKeyProvInfo->dwProvType,
  2071. pKeyProvInfo->dwKeySpec,
  2072. pKeyProvInfo->dwFlags & CRYPT_MACHINE_KEYSET)))
  2073. {
  2074. DebugTrace("Error [%#x]: CompareCertAndContainerPublicKey() failed.\n", hr);
  2075. goto ErrorExit;
  2076. }
  2077. }
  2078. //
  2079. // Set the association.
  2080. //
  2081. if (!::CertSetCertificateContextProperty(m_pCertContext,
  2082. CERT_KEY_PROV_INFO_PROP_ID,
  2083. 0,
  2084. pKeyProvInfo))
  2085. {
  2086. hr = HRESULT_FROM_WIN32(::GetLastError());
  2087. DebugTrace("Error [%#x]: CertSetCertificateContextProperty() failed.\n", hr);
  2088. goto ErrorExit;
  2089. }
  2090. }
  2091. catch(...)
  2092. {
  2093. hr = E_POINTER;
  2094. DebugTrace("Exception: invalid parameter.\n");
  2095. goto ErrorExit;
  2096. }
  2097. UnlockExit:
  2098. //
  2099. // Free resources.
  2100. //
  2101. if (pKeyProvInfo)
  2102. {
  2103. ::CoTaskMemFree(pKeyProvInfo);
  2104. }
  2105. //
  2106. // Unlock access to this object.
  2107. //
  2108. m_Lock.Unlock();
  2109. DebugTrace("Leaving CCertificate::put_PrivateKey().\n");
  2110. return hr;
  2111. ErrorExit:
  2112. //
  2113. // Sanity check.
  2114. //
  2115. ATLASSERT(FAILED(hr));
  2116. ReportError(hr);
  2117. goto UnlockExit;
  2118. }
  2119. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2120. Function : CCertificate::Extensions
  2121. Synopsis : Return the IExtensions collection object.
  2122. Parameter: IExtensions ** pVal - Pointer to pointer to IExtensions
  2123. to receive the interface pointer.
  2124. Remark :
  2125. ------------------------------------------------------------------------------*/
  2126. STDMETHODIMP CCertificate::Extensions (IExtensions ** pVal)
  2127. {
  2128. HRESULT hr = S_OK;
  2129. DebugTrace("Entering CCertificate::Extensions().\n");
  2130. try
  2131. {
  2132. //
  2133. // Lock access to this object.
  2134. //
  2135. m_Lock.Lock();
  2136. //
  2137. // Check parameters.
  2138. //
  2139. if (NULL == pVal)
  2140. {
  2141. hr = E_INVALIDARG;
  2142. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  2143. goto ErrorExit;
  2144. }
  2145. //
  2146. // Make sure cert is already initialized.
  2147. //
  2148. if (!m_pCertContext)
  2149. {
  2150. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  2151. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  2152. goto ErrorExit;
  2153. }
  2154. //
  2155. // Create the embeded IExtensions object, if not already done so.
  2156. //
  2157. if (!m_pIExtensions)
  2158. {
  2159. if (FAILED(hr = ::CreateExtensionsObject(m_pCertContext, &m_pIExtensions)))
  2160. {
  2161. DebugTrace("Error [%#x]: CreateExtensionsObject() failed.\n", hr);
  2162. goto ErrorExit;
  2163. }
  2164. }
  2165. //
  2166. // Sanity check.
  2167. //
  2168. ATLASSERT(m_pIExtensions);
  2169. //
  2170. // Return interface pointer to user.
  2171. //
  2172. if (FAILED(hr = m_pIExtensions->QueryInterface(pVal)))
  2173. {
  2174. DebugTrace("Error [%#x]: m_pIExtensions->QueryInterface() failed.\n", hr);
  2175. goto ErrorExit;
  2176. }
  2177. }
  2178. catch(...)
  2179. {
  2180. hr = E_POINTER;
  2181. DebugTrace("Exception: invalid parameter.\n");
  2182. goto ErrorExit;
  2183. }
  2184. UnlockExit:
  2185. //
  2186. // Unlock access to this object.
  2187. //
  2188. m_Lock.Unlock();
  2189. DebugTrace("Leaving CCertificate::Extensions().\n");
  2190. return hr;
  2191. ErrorExit:
  2192. //
  2193. // Sanity check.
  2194. //
  2195. ATLASSERT(FAILED(hr));
  2196. ReportError(hr);
  2197. goto UnlockExit;
  2198. }
  2199. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2200. Function : CCertificate::ExtendedProperties
  2201. Synopsis : Return the IExtendedProperties collection object.
  2202. Parameter: IExtendedProperties ** pVal - Pointer to pointer to
  2203. IExtendedProperties to receive the
  2204. interface pointer.
  2205. Remark :
  2206. ------------------------------------------------------------------------------*/
  2207. STDMETHODIMP CCertificate::ExtendedProperties (IExtendedProperties ** pVal)
  2208. {
  2209. HRESULT hr = S_OK;
  2210. DebugTrace("Entering CCertificate::ExtendedProperties().\n");
  2211. try
  2212. {
  2213. //
  2214. // Lock access to this object.
  2215. //
  2216. m_Lock.Lock();
  2217. //
  2218. // Check parameters.
  2219. //
  2220. if (NULL == pVal)
  2221. {
  2222. hr = E_INVALIDARG;
  2223. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  2224. goto ErrorExit;
  2225. }
  2226. //
  2227. // Make sure cert is already initialized.
  2228. //
  2229. if (!m_pCertContext)
  2230. {
  2231. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  2232. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  2233. goto ErrorExit;
  2234. }
  2235. //
  2236. // Create the dynamic IExtendedProperties object.
  2237. //
  2238. if (FAILED(hr = ::CreateExtendedPropertiesObject(m_pCertContext,
  2239. m_dwCurrentSafety ? TRUE : FALSE,
  2240. pVal)))
  2241. {
  2242. DebugTrace("Error [%#x]: CreateExtendedPropertiesObject() failed.\n", hr);
  2243. goto ErrorExit;
  2244. }
  2245. }
  2246. catch(...)
  2247. {
  2248. hr = E_POINTER;
  2249. DebugTrace("Exception: invalid parameter.\n");
  2250. goto ErrorExit;
  2251. }
  2252. UnlockExit:
  2253. //
  2254. // Unlock access to this object.
  2255. //
  2256. m_Lock.Unlock();
  2257. DebugTrace("Leaving CCertificate::ExtendedProperties().\n");
  2258. return hr;
  2259. ErrorExit:
  2260. //
  2261. // Sanity check.
  2262. //
  2263. ATLASSERT(FAILED(hr));
  2264. ReportError(hr);
  2265. goto UnlockExit;
  2266. }
  2267. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2268. Function : CCertificate::Load
  2269. Synopsis : Method to load certificate(s) from a file.
  2270. Parameter: BSTR FileName - File name.
  2271. BSTR Password - Password (required for PFX file.)
  2272. CAPICOM_KEY_STORAGE_FLAG KeyStorageFlag - Key storage flag.
  2273. CAPICOM_KEY_LOCATION KeyLocation - Key location.
  2274. Remark :
  2275. ------------------------------------------------------------------------------*/
  2276. STDMETHODIMP CCertificate::Load (BSTR FileName,
  2277. BSTR Password,
  2278. CAPICOM_KEY_STORAGE_FLAG KeyStorageFlag,
  2279. CAPICOM_KEY_LOCATION KeyLocation)
  2280. {
  2281. HRESULT hr = S_OK;
  2282. DATA_BLOB CertBlob = {0, NULL};
  2283. DebugTrace("Entering CCertificate::Load().\n");
  2284. try
  2285. {
  2286. //
  2287. // Lock access to this object.
  2288. //
  2289. m_Lock.Lock();
  2290. //
  2291. // Not allowed if called from WEB script.
  2292. //
  2293. if (m_dwCurrentSafety)
  2294. {
  2295. hr = CAPICOM_E_NOT_ALLOWED;
  2296. DebugTrace("Error [%#x]: Loading cert file from WEB script is not allowed.\n", hr);
  2297. goto ErrorExit;
  2298. }
  2299. //
  2300. // Check parameters.
  2301. //
  2302. if (0 == ::SysStringLen(FileName))
  2303. {
  2304. hr = E_INVALIDARG;
  2305. DebugTrace("Error [%#x]: Parameter FileName is NULL or empty.\n", hr);
  2306. goto ErrorExit;
  2307. }
  2308. //
  2309. // Work around MIDL problem.
  2310. //
  2311. if (0 == ::SysStringLen(Password))
  2312. {
  2313. Password = NULL;
  2314. }
  2315. //
  2316. // Read entire file in.
  2317. //
  2318. if (FAILED(hr = ::ReadFileContent((LPWSTR) FileName, &CertBlob)))
  2319. {
  2320. DebugTrace("Error [%#x]: ReadFileContent() failed.\n", hr);
  2321. goto ErrorExit;
  2322. }
  2323. //
  2324. // Now import the blob.
  2325. //
  2326. if (FAILED(hr = ImportBlob(&CertBlob, TRUE, KeyLocation, Password, KeyStorageFlag)))
  2327. {
  2328. DebugTrace("Error [%#x]: CCertificate::ImportBlob() failed.\n", hr);
  2329. goto ErrorExit;
  2330. }
  2331. }
  2332. catch(...)
  2333. {
  2334. hr = E_POINTER;
  2335. DebugTrace("Exception: invalid parameter.\n");
  2336. goto ErrorExit;
  2337. }
  2338. UnlockExit:
  2339. //
  2340. // Free resource.
  2341. //
  2342. if (CertBlob.pbData)
  2343. {
  2344. ::UnmapViewOfFile(CertBlob.pbData);
  2345. }
  2346. //
  2347. // Unlock access to this object.
  2348. //
  2349. m_Lock.Unlock();
  2350. DebugTrace("Leaving CCertificate::Load().\n");
  2351. return hr;
  2352. ErrorExit:
  2353. //
  2354. // Sanity check.
  2355. //
  2356. ATLASSERT(FAILED(hr));
  2357. ReportError(hr);
  2358. goto UnlockExit;
  2359. }
  2360. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2361. Function : CCertificate::Save
  2362. Synopsis : Method to save certificate(s) to a file.
  2363. Parameter: BSTR FileName - File name.
  2364. BSTR Password - Password (required for PFX file.)
  2365. CAPICOM_CERTIFICATE_SAVE_AS_TYPE FileType - SaveAs type.
  2366. CAPICOM_CERTIFICATE_INCLUDE_OPTION IncludeOption - Include option.
  2367. Remark :
  2368. ------------------------------------------------------------------------------*/
  2369. STDMETHODIMP CCertificate::Save (BSTR FileName,
  2370. BSTR Password,
  2371. CAPICOM_CERTIFICATE_SAVE_AS_TYPE SaveAs,
  2372. CAPICOM_CERTIFICATE_INCLUDE_OPTION IncludeOption)
  2373. {
  2374. HRESULT hr = S_OK;
  2375. HCERTSTORE hCertStore = NULL;
  2376. DebugTrace("Entering CCertificate::Save().\n");
  2377. try
  2378. {
  2379. //
  2380. // Lock access to this object.
  2381. //
  2382. m_Lock.Lock();
  2383. //
  2384. // Not allowed if called from WEB script.
  2385. //
  2386. if (m_dwCurrentSafety)
  2387. {
  2388. hr = CAPICOM_E_NOT_ALLOWED;
  2389. DebugTrace("Error [%#x]: Saving cert file from WEB script is not allowed.\n", hr);
  2390. goto ErrorExit;
  2391. }
  2392. //
  2393. // Check parameters.
  2394. //
  2395. if (0 == ::SysStringLen(FileName))
  2396. {
  2397. hr = E_INVALIDARG;
  2398. DebugTrace("Error [%#x]: Parameter FileName is NULL or empty.\n", hr);
  2399. goto ErrorExit;
  2400. }
  2401. //
  2402. // Work around MIDL problem.
  2403. //
  2404. if (0 == ::SysStringLen(Password))
  2405. {
  2406. Password = NULL;
  2407. }
  2408. //
  2409. // Make sure cert is already initialized.
  2410. //
  2411. if (!m_pCertContext)
  2412. {
  2413. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  2414. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  2415. goto ErrorExit;
  2416. }
  2417. //
  2418. // Check file type.
  2419. //
  2420. switch (SaveAs)
  2421. {
  2422. case CAPICOM_CERTIFICATE_SAVE_AS_CER:
  2423. {
  2424. DATA_BLOB DataBlob;
  2425. //
  2426. // Simply write the encoded cert blob to file.
  2427. //
  2428. DataBlob.cbData = m_pCertContext->cbCertEncoded;
  2429. DataBlob.pbData = m_pCertContext->pbCertEncoded;
  2430. if (FAILED(hr = ::WriteFileContent(FileName, DataBlob)))
  2431. {
  2432. DebugTrace("Error [%#x]: WriteFileContent() failed.\n", hr);
  2433. goto ErrorExit;
  2434. }
  2435. break;
  2436. }
  2437. case CAPICOM_CERTIFICATE_SAVE_AS_PFX:
  2438. {
  2439. //
  2440. // Create a memory store.
  2441. //
  2442. if (!(hCertStore = ::CertOpenStore(CERT_STORE_PROV_MEMORY,
  2443. CAPICOM_ASN_ENCODING,
  2444. 0,
  2445. 0,
  2446. NULL)))
  2447. {
  2448. hr = HRESULT_FROM_WIN32(::GetLastError());
  2449. DebugTrace("Error [%#x]: CertOpenStore() failed.\n", hr);
  2450. goto ErrorExit;
  2451. }
  2452. //
  2453. // Add all requested certs to the store.
  2454. //
  2455. if (FAILED(hr = ::CertToStore(m_pCertContext, IncludeOption, hCertStore)))
  2456. {
  2457. DebugTrace("Error [%#x]: CertToStore() failed.\n", hr);
  2458. goto ErrorExit;
  2459. }
  2460. //
  2461. // Save as PFX file.
  2462. //
  2463. if (FAILED(hr = ::PFXSaveStore(hCertStore,
  2464. FileName,
  2465. Password,
  2466. EXPORT_PRIVATE_KEYS | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY)))
  2467. {
  2468. DebugTrace("Error [%#x]: PFXSaveStore() failed.\n", hr);
  2469. goto ErrorExit;
  2470. }
  2471. break;
  2472. }
  2473. default:
  2474. {
  2475. hr = E_INVALIDARG;
  2476. DebugTrace("Error: invalid parameter, unknown save as type.\n");
  2477. goto ErrorExit;
  2478. }
  2479. }
  2480. }
  2481. catch(...)
  2482. {
  2483. hr = E_POINTER;
  2484. DebugTrace("Exception: invalid parameter.\n");
  2485. goto ErrorExit;
  2486. }
  2487. UnlockExit:
  2488. //
  2489. // Free resources.
  2490. //
  2491. if (hCertStore)
  2492. {
  2493. ::CertCloseStore(hCertStore, 0);
  2494. }
  2495. //
  2496. // Unlock access to this object.
  2497. //
  2498. m_Lock.Unlock();
  2499. DebugTrace("Leaving CCertificate::Save().\n");
  2500. return hr;
  2501. ErrorExit:
  2502. //
  2503. // Sanity check.
  2504. //
  2505. ATLASSERT(FAILED(hr));
  2506. ReportError(hr);
  2507. goto UnlockExit;
  2508. }
  2509. ////////////////////////////////////////////////////////////////////////////////
  2510. //
  2511. // Custom interfaces.
  2512. //
  2513. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2514. Function : CCertificate::get_CertContext
  2515. Synopsis : Return the certificate's PCCERT_CONTEXT.
  2516. Parameter: long * ppCertContext - Pointer to PCCERT_CONTEXT disguished in a
  2517. long.
  2518. Remark : We need to use long instead of PCCERT_CONTEXT because VB can't
  2519. handle double indirection (i.e. vb would bark on this
  2520. PCCERT_CONTEXT * ppCertContext).
  2521. ------------------------------------------------------------------------------*/
  2522. STDMETHODIMP CCertificate::get_CertContext (long * ppCertContext)
  2523. {
  2524. HRESULT hr = S_OK;
  2525. DebugTrace("Entering CCertificate::get_CertContext().\n");
  2526. try
  2527. {
  2528. //
  2529. // Lock access to this object.
  2530. //
  2531. m_Lock.Lock();
  2532. //
  2533. // Return cert context to caller.
  2534. //
  2535. if (FAILED(hr = GetContext((PCCERT_CONTEXT *) ppCertContext)))
  2536. {
  2537. DebugTrace("Error [%#x]: CCertificate::GetContext() failed.\n", hr);
  2538. goto ErrorExit;
  2539. }
  2540. }
  2541. catch(...)
  2542. {
  2543. hr = E_POINTER;
  2544. DebugTrace("Exception: invalid parameter.\n");
  2545. goto ErrorExit;
  2546. }
  2547. UnlockExit:
  2548. //
  2549. // Unlock access to this object.
  2550. //
  2551. m_Lock.Unlock();
  2552. DebugTrace("Leaving CCertificate::get_CertContext().\n");
  2553. return hr;
  2554. ErrorExit:
  2555. //
  2556. // Sanity check.
  2557. //
  2558. ATLASSERT(FAILED(hr));
  2559. ReportError(hr);
  2560. goto UnlockExit;
  2561. }
  2562. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2563. Function : CCertificate::put_CertContext
  2564. Synopsis : Initialize the object with a CERT_CONTEXT.
  2565. Parameter: long pCertContext - Poiner to CERT_CONTEXT, disguised in a long,
  2566. used to initialize this object.
  2567. Remark : Note that this is NOT 64-bit compatiable. Plese see remark of
  2568. get_CertContext for more detail.
  2569. ------------------------------------------------------------------------------*/
  2570. STDMETHODIMP CCertificate::put_CertContext (long pCertContext)
  2571. {
  2572. HRESULT hr = S_OK;
  2573. DebugTrace("Entering CCertificate::put_CertContext().\n");
  2574. try
  2575. {
  2576. //
  2577. // Lock access to this object.
  2578. //
  2579. m_Lock.Lock();
  2580. //
  2581. // Reset the object with this context.
  2582. //
  2583. if (FAILED(hr = PutContext((PCCERT_CONTEXT) pCertContext, m_dwCurrentSafety)))
  2584. {
  2585. DebugTrace("Error [%#x]: CCertificate::PutContext() failed.\n", hr);
  2586. goto ErrorExit;
  2587. }
  2588. }
  2589. catch(...)
  2590. {
  2591. hr = E_POINTER;
  2592. DebugTrace("Exception: invalid parameter.\n");
  2593. goto ErrorExit;
  2594. }
  2595. UnlockExit:
  2596. //
  2597. // Unlock access to this object.
  2598. //
  2599. m_Lock.Unlock();
  2600. DebugTrace("Leaving CCertificate::put_CertContext().\n");
  2601. return hr;
  2602. ErrorExit:
  2603. //
  2604. // Sanity check.
  2605. //
  2606. ATLASSERT(FAILED(hr));
  2607. ReportError(hr);
  2608. goto UnlockExit;
  2609. }
  2610. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2611. Function : CCertificate::FreeContext
  2612. Synopsis : Free a CERT_CONTEXT.
  2613. Parameter: long pCertContext - Poiner to CERT_CONTEXT, disguised in a long,
  2614. to be freed.
  2615. Remark : Note that this is NOT 64-bit compatiable. Plese see remark of
  2616. get_CertContext for more detail.
  2617. ------------------------------------------------------------------------------*/
  2618. STDMETHODIMP CCertificate::FreeContext (long pCertContext)
  2619. {
  2620. HRESULT hr = S_OK;
  2621. DebugTrace("Entering CCertificate::FreeContext().\n");
  2622. try
  2623. {
  2624. //
  2625. // Lock access to this object.
  2626. //
  2627. m_Lock.Lock();
  2628. //
  2629. // Free the context.
  2630. //
  2631. if (!::CertFreeCertificateContext((PCCERT_CONTEXT) pCertContext))
  2632. {
  2633. hr = HRESULT_FROM_WIN32(::GetLastError());
  2634. DebugTrace("Error [%#x]: CertFreeCertificateContext() failed.\n", hr);
  2635. goto ErrorExit;
  2636. }
  2637. }
  2638. catch(...)
  2639. {
  2640. hr = E_POINTER;
  2641. DebugTrace("Exception: invalid parameter.\n");
  2642. goto ErrorExit;
  2643. }
  2644. UnlockExit:
  2645. //
  2646. // Unlock access to this object.
  2647. //
  2648. m_Lock.Unlock();
  2649. DebugTrace("Leaving CCertificate::FreeContext().\n");
  2650. return hr;
  2651. ErrorExit:
  2652. //
  2653. // Sanity check.
  2654. //
  2655. ATLASSERT(FAILED(hr));
  2656. ReportError(hr);
  2657. goto UnlockExit;
  2658. }
  2659. ////////////////////////////////////////////////////////////////////////////////
  2660. //
  2661. // Private methods.
  2662. //
  2663. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2664. Function : CCertificate::ImportBlob
  2665. Synopsis : Private function to load a certificate from blob.
  2666. Parameter: DATA_BLOB * pCertBlob
  2667. BOOL bAllowPfx
  2668. CAPICOM_KEY_LOCATION KeyLocation - Key location.
  2669. BSTR pwszPassword - Password (required for PFX file.)
  2670. CAPICOM_KEY_STORAGE_FLAG KeyStorageFlag - Key storage flag.
  2671. Remark :
  2672. ------------------------------------------------------------------------------*/
  2673. STDMETHODIMP CCertificate::ImportBlob (DATA_BLOB * pCertBlob,
  2674. BOOL bAllowPfx,
  2675. CAPICOM_KEY_LOCATION KeyLocation,
  2676. BSTR pwszPassword,
  2677. CAPICOM_KEY_STORAGE_FLAG KeyStorageFlag)
  2678. {
  2679. HRESULT hr = S_OK;
  2680. HCERTSTORE hCertStore = NULL;
  2681. PCCERT_CONTEXT pEnumContext = NULL;
  2682. PCCERT_CONTEXT pCertContext = NULL;
  2683. DWORD dwContentType = 0;
  2684. DWORD cb = 0;
  2685. DWORD dwFlags = 0;
  2686. DWORD dwExpectedType = CERT_QUERY_CONTENT_FLAG_CERT |
  2687. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT;
  2688. DebugTrace("Entering CCertificate::ImportBlob().\n");
  2689. //
  2690. // Sanity check.
  2691. //
  2692. ATLASSERT(pCertBlob);
  2693. //
  2694. // Set PFX flag, if allowed.
  2695. //
  2696. if (bAllowPfx)
  2697. {
  2698. dwExpectedType |= CERT_QUERY_CONTENT_FLAG_PFX;
  2699. }
  2700. //
  2701. // Crack the blob.
  2702. //
  2703. if (!::CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
  2704. (LPCVOID) pCertBlob,
  2705. dwExpectedType,
  2706. CERT_QUERY_FORMAT_FLAG_ALL,
  2707. 0,
  2708. NULL,
  2709. &dwContentType,
  2710. NULL,
  2711. &hCertStore,
  2712. NULL,
  2713. NULL))
  2714. {
  2715. hr = HRESULT_FROM_WIN32(::GetLastError());
  2716. DebugTrace("Error [%#x]: CryptQueryObject() failed.\n", hr);
  2717. goto ErrorExit;
  2718. }
  2719. DebugTrace("Info: CryptQueryObject() returns dwContentType = %#x.\n", dwContentType);
  2720. //
  2721. // Need to import it ourselves for PFX file.
  2722. //
  2723. if (CERT_QUERY_CONTENT_PFX == dwContentType)
  2724. {
  2725. //
  2726. // Make sure PFX is allowed.
  2727. //
  2728. if (!bAllowPfx)
  2729. {
  2730. hr = CAPICOM_E_NOT_SUPPORTED;
  2731. DebugTrace("Error [%#x]: Importing PFX where not supported.\n", hr);
  2732. goto ErrorExit;
  2733. }
  2734. //
  2735. // Setup import flags.
  2736. //
  2737. if (CAPICOM_LOCAL_MACHINE_KEY == KeyLocation)
  2738. {
  2739. dwFlags |= CRYPT_MACHINE_KEYSET;
  2740. }
  2741. else if (IsWin2KAndAbove())
  2742. {
  2743. dwFlags |= CRYPT_USER_KEYSET;
  2744. }
  2745. if (KeyStorageFlag & CAPICOM_KEY_STORAGE_EXPORTABLE)
  2746. {
  2747. dwFlags |= CRYPT_EXPORTABLE;
  2748. }
  2749. if (KeyStorageFlag & CAPICOM_KEY_STORAGE_USER_PROTECTED)
  2750. {
  2751. dwFlags |= CRYPT_USER_PROTECTED;
  2752. }
  2753. DebugTrace("Info: dwFlags = %#x.", dwFlags);
  2754. //
  2755. // Now import the blob to store.
  2756. //
  2757. if (!(hCertStore = ::PFXImportCertStore((CRYPT_DATA_BLOB *) pCertBlob,
  2758. pwszPassword,
  2759. dwFlags)))
  2760. {
  2761. hr = HRESULT_FROM_WIN32(::GetLastError());
  2762. DebugTrace("Error [%#x]: PFXImportCertStore() failed.\n", hr);
  2763. goto ErrorExit;
  2764. }
  2765. //
  2766. // Sanity check.
  2767. //
  2768. ATLASSERT(hCertStore);
  2769. //
  2770. // Find the first cert with private key, if none, then simply take
  2771. // the very first cert.
  2772. //
  2773. while (pEnumContext = ::CertEnumCertificatesInStore(hCertStore, pEnumContext))
  2774. {
  2775. //
  2776. // Does this cert has a private key?
  2777. //
  2778. if (::CertGetCertificateContextProperty(pEnumContext,
  2779. CERT_KEY_PROV_INFO_PROP_ID,
  2780. NULL,
  2781. &cb))
  2782. {
  2783. //
  2784. // Yes, so free the one without private key, had we found one previously.
  2785. //
  2786. if (pCertContext)
  2787. {
  2788. if (!::CertFreeCertificateContext(pCertContext))
  2789. {
  2790. hr = HRESULT_FROM_WIN32(::GetLastError());
  2791. DebugTrace("Error [%#x]: CertFreeCertificateContext() failed.\n", hr);
  2792. goto ErrorExit;
  2793. }
  2794. }
  2795. if (!(pCertContext = ::CertDuplicateCertificateContext(pEnumContext)))
  2796. {
  2797. hr = HRESULT_FROM_WIN32(::GetLastError());
  2798. DebugTrace("Error [%#x]: CertDuplicateCertificateContext() failed.\n", hr);
  2799. goto ErrorExit;
  2800. }
  2801. //
  2802. // Set last error before we break out of loop.
  2803. //
  2804. ::SetLastError((DWORD) CRYPT_E_NOT_FOUND);
  2805. break;
  2806. }
  2807. else
  2808. {
  2809. //
  2810. // Keep the first one.
  2811. //
  2812. if (!pCertContext)
  2813. {
  2814. if (!(pCertContext = ::CertDuplicateCertificateContext(pEnumContext)))
  2815. {
  2816. hr = HRESULT_FROM_WIN32(::GetLastError());
  2817. DebugTrace("Error [%#x]: CertDuplicateCertificateContext() failed.\n", hr);
  2818. goto ErrorExit;
  2819. }
  2820. }
  2821. }
  2822. }
  2823. //
  2824. // Above loop can exit either because there is no more certificate in
  2825. // the store or an error. Need to check last error to be certain.
  2826. //
  2827. if (CRYPT_E_NOT_FOUND != ::GetLastError())
  2828. {
  2829. hr = HRESULT_FROM_WIN32(::GetLastError());
  2830. DebugTrace("Error [%#x]: CertEnumCertificatesInStore() failed.\n", hr);
  2831. goto ErrorExit;
  2832. }
  2833. }
  2834. else
  2835. {
  2836. //
  2837. // It is a CER file, so it must have only 1 cert.
  2838. //
  2839. if (!(pCertContext = ::CertEnumCertificatesInStore(hCertStore, NULL)))
  2840. {
  2841. hr = HRESULT_FROM_WIN32(::GetLastError());
  2842. DebugTrace("Error [%#x]: CertEnumCertificatesInStore() failed.\n", hr);
  2843. goto ErrorExit;
  2844. }
  2845. }
  2846. //
  2847. // Sanity check.
  2848. //
  2849. ATLASSERT(pCertContext);
  2850. //
  2851. // Now initialize the object with the found cert.
  2852. //
  2853. if (FAILED(hr = PutContext(pCertContext, m_dwCurrentSafety)))
  2854. {
  2855. DebugTrace("Error [%#x]: CCertificate::PutContext() failed.\n", hr);
  2856. goto ErrorExit;
  2857. }
  2858. CommonExit:
  2859. //
  2860. // Free resource.
  2861. //
  2862. if (pCertContext)
  2863. {
  2864. ::CertFreeCertificateContext(pCertContext);
  2865. }
  2866. if (pEnumContext)
  2867. {
  2868. ::CertFreeCertificateContext(pEnumContext);
  2869. }
  2870. if (hCertStore)
  2871. {
  2872. ::CertCloseStore(hCertStore, 0);
  2873. }
  2874. DebugTrace("Leaving CCertificate::ImportBlob().\n");
  2875. return hr;
  2876. ErrorExit:
  2877. //
  2878. // Sanity check.
  2879. //
  2880. ATLASSERT(FAILED(hr));
  2881. goto CommonExit;
  2882. }
  2883. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2884. Function : CCertificate::GetContext
  2885. Synopsis : Return the certificate's PCCERT_CONTEXT.
  2886. Parameter: PCCERT_CONTEXT * ppCertContext - Pointer to PCCERT_CONTEXT.
  2887. Remark : This method is designed for internal use only, and therefore,
  2888. should not be exposed to user.
  2889. Note that this is a custom interface, not a dispinterface.
  2890. Note that the cert context ref count is incremented by
  2891. CertDuplicateCertificateContext(), so it is the caller's
  2892. responsibility to free the context.
  2893. ------------------------------------------------------------------------------*/
  2894. STDMETHODIMP CCertificate::GetContext (PCCERT_CONTEXT * ppCertContext)
  2895. {
  2896. HRESULT hr = S_OK;
  2897. DebugTrace("Entering CCertificate::GetContext().\n");
  2898. //
  2899. // Make sure cert is already initialized.
  2900. //
  2901. if (!m_pCertContext)
  2902. {
  2903. hr = CAPICOM_E_CERTIFICATE_NOT_INITIALIZED;
  2904. DebugTrace("Error [%#x]: object does not represent an initialized certificate.\n", hr);
  2905. goto ErrorExit;
  2906. }
  2907. //
  2908. // Sanity check.
  2909. //
  2910. ATLASSERT(ppCertContext);
  2911. //
  2912. // Duplicate the cert context.
  2913. //
  2914. if (!(*ppCertContext = ::CertDuplicateCertificateContext(m_pCertContext)))
  2915. {
  2916. hr = HRESULT_FROM_WIN32(::GetLastError());
  2917. DebugTrace("Error [%#x]: CertDuplicateCertificateContext() failed.\n");
  2918. goto ErrorExit;
  2919. }
  2920. CommonExit:
  2921. DebugTrace("Leaving CCertificate::GetContext().\n");
  2922. return hr;
  2923. ErrorExit:
  2924. //
  2925. // Sanity check.
  2926. //
  2927. ATLASSERT(FAILED(hr));
  2928. goto CommonExit;
  2929. }
  2930. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2931. Function : CCertificate::PutContext
  2932. Synopsis : Initialize the object with a CERT_CONTEXT.
  2933. Parameter: PCERT_CONTEXT pCertContext - Poiner to CERT_CONTEXT used to
  2934. initialize this object.
  2935. DWORD dwCurrentSafety - Current safety setting.
  2936. Remark : This method is not part of the COM interface (it is a normal C++
  2937. member function). We need it to initialize the object created
  2938. internally by us.
  2939. Since it is only a normal C++ member function, this function can
  2940. only be called from a C++ class pointer, not an interface pointer.
  2941. ------------------------------------------------------------------------------*/
  2942. STDMETHODIMP CCertificate::PutContext (PCCERT_CONTEXT pCertContext,
  2943. DWORD dwCurrentSafety)
  2944. {
  2945. HRESULT hr = S_OK;
  2946. PCCERT_CONTEXT pCertContext2 = NULL;
  2947. DebugTrace("Entering CCertificate::PutContext().\n");
  2948. //
  2949. // Sanity check.
  2950. //
  2951. ATLASSERT(pCertContext);
  2952. //
  2953. // Duplicate the cert context.
  2954. //
  2955. if (!(pCertContext2 = ::CertDuplicateCertificateContext(pCertContext)))
  2956. {
  2957. hr = HRESULT_FROM_WIN32(::GetLastError());
  2958. DebugTrace("Error [%#x]: CertDupliacteCertificateContext() failed.\n");
  2959. goto ErrorExit;
  2960. }
  2961. //
  2962. // Free previous context, if any.
  2963. //
  2964. if (m_pCertContext)
  2965. {
  2966. if (!::CertFreeCertificateContext(m_pCertContext))
  2967. {
  2968. hr = HRESULT_FROM_WIN32(::GetLastError());
  2969. DebugTrace("Error [%#x]: CertFreeCertificateContext() failed.\n");
  2970. goto ErrorExit;
  2971. }
  2972. }
  2973. //
  2974. // Reset.
  2975. //
  2976. m_pCertContext = pCertContext2;
  2977. m_pIKeyUsage.Release();
  2978. m_pIExtendedKeyUsage.Release();
  2979. m_pIBasicConstraints.Release();
  2980. m_pICertificateStatus.Release();
  2981. m_pITemplate.Release();
  2982. m_pIPublicKey.Release();
  2983. m_pIExtensions.Release();
  2984. m_dwCurrentSafety = dwCurrentSafety;
  2985. CommonExit:
  2986. DebugTrace("Leaving CCertificate::PutContext().\n");
  2987. return hr;
  2988. ErrorExit:
  2989. //
  2990. // Sanity check.
  2991. //
  2992. ATLASSERT(FAILED(hr));
  2993. //
  2994. // Free resources.
  2995. //
  2996. if (pCertContext2)
  2997. {
  2998. ::CertFreeCertificateContext(pCertContext2);
  2999. }
  3000. goto CommonExit;
  3001. }