Source code of Windows XP (NT5)
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.

1099 lines
25 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: admin.cpp
  7. //
  8. // Contents: Cert Server admin implementation
  9. //
  10. // History: 24-Aug-96 vich created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "pch.cpp"
  14. #pragma hdrstop
  15. #include <objbase.h>
  16. #include "certsrvd.h"
  17. #include "certbcli.h"
  18. #include "csprop.h"
  19. #include "csdisp.h"
  20. #include "admin.h"
  21. #include "certadmp.h"
  22. #include "config.h"
  23. #define __dwFILE__ __dwFILE_CERTADM_ADMIN_CPP__
  24. //+--------------------------------------------------------------------------
  25. // CCertAdmin::~CCertAdmin -- destructor
  26. //
  27. // free memory associated with this instance
  28. //+--------------------------------------------------------------------------
  29. CCertAdmin::~CCertAdmin()
  30. {
  31. _Cleanup();
  32. }
  33. //+--------------------------------------------------------------------------
  34. // CCertAdmin::_CleanupOldConnection -- free memory
  35. //
  36. // free memory associated with this instance
  37. //+--------------------------------------------------------------------------
  38. VOID
  39. CCertAdmin::_CleanupOldConnection()
  40. {
  41. _CleanupCAPropInfo();
  42. }
  43. //+--------------------------------------------------------------------------
  44. // CCertAdmin::_Cleanup -- free memory
  45. //
  46. // free memory associated with this instance
  47. //+--------------------------------------------------------------------------
  48. VOID
  49. CCertAdmin::_Cleanup()
  50. {
  51. _CloseConnection();
  52. _CleanupOldConnection();
  53. }
  54. //+--------------------------------------------------------------------------
  55. // CCertAdmin::_OpenConnection -- get DCOM object interface
  56. //
  57. //+--------------------------------------------------------------------------
  58. HRESULT
  59. CCertAdmin::_OpenConnection(
  60. IN WCHAR const *pwszConfig,
  61. IN DWORD RequiredVersion,
  62. OUT WCHAR const **ppwszAuthority)
  63. {
  64. HRESULT hr;
  65. hr = myOpenAdminDComConnection(
  66. pwszConfig,
  67. ppwszAuthority,
  68. &m_pwszServerName,
  69. &m_dwServerVersion,
  70. &m_pICertAdminD);
  71. _JumpIfError(hr, error, "myOpenAdminDComConnection");
  72. CSASSERT(NULL != m_pICertAdminD);
  73. CSASSERT(0 != m_dwServerVersion);
  74. if (m_dwServerVersion < RequiredVersion)
  75. {
  76. hr = RPC_E_VERSION_MISMATCH;
  77. _JumpError(hr, error, "old server");
  78. }
  79. error:
  80. return(hr);
  81. }
  82. //+--------------------------------------------------------------------------
  83. // CCertAdmin::_CloseConnection -- release DCOM object
  84. //
  85. //+--------------------------------------------------------------------------
  86. VOID
  87. CCertAdmin::_CloseConnection()
  88. {
  89. myCloseDComConnection((IUnknown **) &m_pICertAdminD, &m_pwszServerName);
  90. m_dwServerVersion = 0;
  91. }
  92. //+--------------------------------------------------------------------------
  93. // CCertAdmin::IsValidCertificate -- Verify certificate validity
  94. //
  95. // ...
  96. //
  97. // Returns S_OK on success.
  98. //+--------------------------------------------------------------------------
  99. STDMETHODIMP
  100. CCertAdmin::IsValidCertificate(
  101. /* [in] */ BSTR const strConfig,
  102. /* [in] */ BSTR const strSerialNumber,
  103. /* [out, retval] */ LONG *pDisposition)
  104. {
  105. HRESULT hr;
  106. WCHAR const *pwszAuthority;
  107. if (NULL == strSerialNumber || NULL == pDisposition)
  108. {
  109. hr = E_POINTER;
  110. _JumpError(hr, error, "NULL parm");
  111. }
  112. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  113. _JumpIfError(hr, error, "_OpenConnection");
  114. m_fRevocationReasonValid = FALSE;
  115. __try
  116. {
  117. hr = m_pICertAdminD->IsValidCertificate(
  118. pwszAuthority,
  119. strSerialNumber,
  120. &m_RevocationReason,
  121. pDisposition);
  122. if (S_OK != hr || CA_DISP_REVOKED != *pDisposition)
  123. {
  124. m_fRevocationReasonValid = FALSE;
  125. }
  126. else
  127. {
  128. m_fRevocationReasonValid = TRUE;
  129. }
  130. }
  131. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  132. {
  133. }
  134. _JumpIfError(hr, error, "IsValidCertificate");
  135. m_fRevocationReasonValid = TRUE;
  136. error:
  137. hr = myHError(hr);
  138. return(_SetErrorInfo(hr, L"CCertAdmin::IsValidCertificate"));
  139. }
  140. //+--------------------------------------------------------------------------
  141. // CCertAdmin::GetRevocationReason -- Get Revocation Reason
  142. //
  143. // ...
  144. //
  145. // Returns S_OK on success.
  146. //+--------------------------------------------------------------------------
  147. STDMETHODIMP
  148. CCertAdmin::GetRevocationReason(
  149. /* [out, retval] */ LONG *pReason)
  150. {
  151. HRESULT hr = S_OK;
  152. if (NULL == pReason)
  153. {
  154. hr = E_POINTER;
  155. _JumpError(hr, error, "NULL parm");
  156. }
  157. if (!m_fRevocationReasonValid)
  158. {
  159. hr = E_UNEXPECTED;
  160. _JumpError(hr, error, "m_fRevocationReasonValid");
  161. }
  162. *pReason = m_RevocationReason;
  163. error:
  164. return(_SetErrorInfo(hr, L"CCertAdmin::GetRevocationReason"));
  165. }
  166. //+--------------------------------------------------------------------------
  167. // CCertAdmin::RevokeCertificate -- Revoke a certificate
  168. //
  169. // ...
  170. //
  171. // Returns S_OK on success.
  172. //+--------------------------------------------------------------------------
  173. STDMETHODIMP
  174. CCertAdmin::RevokeCertificate(
  175. /* [in] */ BSTR const strConfig,
  176. /* [in] */ BSTR const strSerialNumber,
  177. /* [in] */ LONG Reason,
  178. /* [in] */ DATE Date)
  179. {
  180. HRESULT hr;
  181. WCHAR const *pwszAuthority;
  182. FILETIME ft;
  183. if (NULL == strConfig || NULL == strSerialNumber)
  184. {
  185. hr = E_POINTER;
  186. _JumpError(hr, error, "NULL parm");
  187. }
  188. hr = myDateToFileTime(&Date, &ft);
  189. _JumpIfError(hr, error, "myDateToFileTime");
  190. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  191. _JumpIfError(hr, error, "_OpenConnection");
  192. __try
  193. {
  194. hr = m_pICertAdminD->RevokeCertificate(
  195. pwszAuthority,
  196. strSerialNumber,
  197. Reason,
  198. ft);
  199. }
  200. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  201. {
  202. }
  203. _JumpIfError(hr, error, "RevokeCertificate");
  204. error:
  205. hr = myHError(hr);
  206. return(_SetErrorInfo(hr, L"CCertAdmin::RevokeCertificate"));
  207. }
  208. //+--------------------------------------------------------------------------
  209. // CCertAdmin::SetRequestAttributes -- Add request attributes
  210. //
  211. // ...
  212. //
  213. // Returns S_OK on success.
  214. //+--------------------------------------------------------------------------
  215. STDMETHODIMP
  216. CCertAdmin::SetRequestAttributes(
  217. /* [in] */ BSTR const strConfig,
  218. /* [in] */ LONG RequestId,
  219. /* [in] */ BSTR const strAttributes)
  220. {
  221. HRESULT hr;
  222. WCHAR const *pwszAuthority;
  223. if (NULL == strAttributes)
  224. {
  225. hr = E_POINTER;
  226. _JumpError(hr, error, "NULL parm");
  227. }
  228. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  229. _JumpIfError(hr, error, "_OpenConnection");
  230. __try
  231. {
  232. hr = m_pICertAdminD->SetAttributes(
  233. pwszAuthority,
  234. (DWORD) RequestId,
  235. strAttributes);
  236. }
  237. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  238. {
  239. }
  240. _JumpIfError(hr, error, "SetAttributes");
  241. error:
  242. hr = myHError(hr);
  243. return(_SetErrorInfo(hr, L"CCertAdmin::SetRequestAttributes"));
  244. }
  245. //+--------------------------------------------------------------------------
  246. // CCertAdmin::SetCertificateExtension -- Set a Certificate Extension
  247. //
  248. // ...
  249. //
  250. // Returns S_OK on success.
  251. //+--------------------------------------------------------------------------
  252. STDMETHODIMP
  253. CCertAdmin::SetCertificateExtension(
  254. /* [in] */ BSTR const strConfig,
  255. /* [in] */ LONG RequestId,
  256. /* [in] */ BSTR const strExtensionName,
  257. /* [in] */ LONG Type,
  258. /* [in] */ LONG Flags,
  259. /* [in] */ VARIANT const *pvarValue)
  260. {
  261. HRESULT hr;
  262. WCHAR const *pwszAuthority;
  263. BSTR str;
  264. CERTTRANSBLOB ctbValue;
  265. LONG lval;
  266. ctbValue.pb = NULL;
  267. if (NULL == strExtensionName || NULL == pvarValue)
  268. {
  269. hr = E_POINTER;
  270. _JumpError(hr, error, "NULL parm");
  271. }
  272. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  273. _JumpIfError(hr, error, "_OpenConnection");
  274. hr = myMarshalVariant(pvarValue, Type, &ctbValue.cb, &ctbValue.pb);
  275. _JumpIfError(hr, error, "myMarshalVariant");
  276. __try
  277. {
  278. hr = m_pICertAdminD->SetExtension(
  279. pwszAuthority,
  280. (DWORD) RequestId,
  281. strExtensionName,
  282. Type,
  283. Flags,
  284. &ctbValue);
  285. }
  286. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  287. {
  288. }
  289. _JumpIfError(hr, error, "SetExtension");
  290. error:
  291. if (NULL != ctbValue.pb)
  292. {
  293. LocalFree(ctbValue.pb);
  294. }
  295. hr = myHError(hr);
  296. return(_SetErrorInfo(hr, L"CCertAdmin::SetCertificateExtension"));
  297. }
  298. //+--------------------------------------------------------------------------
  299. // CCertAdmin::ResubmitRequest -- Resubmit a certificate request
  300. //
  301. // ...
  302. //
  303. // Returns S_OK on success.
  304. //+--------------------------------------------------------------------------
  305. STDMETHODIMP
  306. CCertAdmin::ResubmitRequest(
  307. /* [in] */ BSTR const strConfig,
  308. /* [in] */ LONG RequestId,
  309. /* [out, retval] */ LONG *pDisposition)
  310. {
  311. HRESULT hr;
  312. WCHAR const *pwszAuthority;
  313. if (NULL == pDisposition)
  314. {
  315. hr = E_POINTER;
  316. _JumpError(hr, error, "NULL parm");
  317. }
  318. *pDisposition = 0;
  319. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  320. _JumpIfError(hr, error, "_OpenConnection");
  321. __try
  322. {
  323. hr = m_pICertAdminD->ResubmitRequest(
  324. pwszAuthority,
  325. (DWORD) RequestId,
  326. (DWORD *) pDisposition);
  327. }
  328. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  329. {
  330. }
  331. _JumpIfError(hr, error, "ResubmitRequest");
  332. error:
  333. hr = myHError(hr);
  334. return(_SetErrorInfo(hr, L"CCertAdmin::ResubmitRequest"));
  335. }
  336. //+--------------------------------------------------------------------------
  337. // CCertAdmin::DenyRequest -- Deny a certificate request
  338. //
  339. // ...
  340. //
  341. // Returns S_OK on success.
  342. //+--------------------------------------------------------------------------
  343. STDMETHODIMP
  344. CCertAdmin::DenyRequest(
  345. /* [in] */ BSTR const strConfig,
  346. /* [in] */ LONG RequestId)
  347. {
  348. HRESULT hr;
  349. WCHAR const *pwszAuthority;
  350. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  351. _JumpIfError(hr, error, "_OpenConnection");
  352. __try
  353. {
  354. hr = m_pICertAdminD->DenyRequest(pwszAuthority, (DWORD) RequestId);
  355. }
  356. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  357. {
  358. }
  359. _JumpIfError(hr, error, "DenyRequest");
  360. error:
  361. hr = myHError(hr);
  362. return(_SetErrorInfo(hr, L"CCertAdmin::DenyRequest"));
  363. }
  364. //+--------------------------------------------------------------------------
  365. // CCertAdmin::PublishCRL -- Puhlish a new CRL
  366. //
  367. // ...
  368. //
  369. // Returns S_OK on success.
  370. //+--------------------------------------------------------------------------
  371. STDMETHODIMP
  372. CCertAdmin::PublishCRL(
  373. /* [in] */ BSTR const strConfig,
  374. /* [in] */ DATE Date)
  375. {
  376. HRESULT hr;
  377. WCHAR const *pwszAuthority;
  378. FILETIME ft;
  379. // Date = 0.0 means pass ft = 0 to the DCOM interface
  380. if (Date == 0.0)
  381. {
  382. ZeroMemory(&ft, sizeof(FILETIME));
  383. }
  384. else // translate date to ft
  385. {
  386. hr = myDateToFileTime(&Date, &ft);
  387. _JumpIfError(hr, error, "myDateToFileTime");
  388. }
  389. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  390. _JumpIfError(hr, error, "_OpenConnection");
  391. __try
  392. {
  393. hr = m_pICertAdminD->PublishCRL(pwszAuthority, ft);
  394. }
  395. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  396. {
  397. }
  398. _JumpIfError(hr, error, "PublishCRL");
  399. error:
  400. hr = myHError(hr);
  401. return(_SetErrorInfo(hr, L"CCertAdmin::PublishCRL"));
  402. }
  403. //+--------------------------------------------------------------------------
  404. // CCertAdmin::PublishCRLs -- Publish new base CRL, delta CRL or both
  405. //
  406. // ...
  407. //
  408. // Returns S_OK on success.
  409. //+--------------------------------------------------------------------------
  410. HRESULT
  411. CCertAdmin::PublishCRLs(
  412. /* [in] */ BSTR const strConfig,
  413. /* [in] */ DATE Date,
  414. /* [in] */ LONG CRLFlags) // CA_CRL_*
  415. {
  416. HRESULT hr;
  417. WCHAR const *pwszAuthority;
  418. FILETIME ft;
  419. // Date = 0.0 means pass ft = 0 to the DCOM interface
  420. if (Date == 0.0)
  421. {
  422. ZeroMemory(&ft, sizeof(FILETIME));
  423. }
  424. else // translate date to ft
  425. {
  426. hr = myDateToFileTime(&Date, &ft);
  427. _JumpIfError(hr, error, "myDateToFileTime");
  428. }
  429. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  430. _JumpIfError(hr, error, "_OpenConnection");
  431. __try
  432. {
  433. hr = m_pICertAdminD->PublishCRLs(pwszAuthority, ft, CRLFlags);
  434. }
  435. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  436. {
  437. }
  438. _JumpIfError(hr, error, "PublishCRLs");
  439. error:
  440. hr = myHError(hr);
  441. return(_SetErrorInfo(hr, L"CCertAdmin::PublishCRLs"));
  442. }
  443. //+--------------------------------------------------------------------------
  444. // CCertAdmin::GetCRL -- Get the latest CRL
  445. //
  446. // ...
  447. //
  448. // Returns S_OK on success.
  449. //+--------------------------------------------------------------------------
  450. STDMETHODIMP
  451. CCertAdmin::GetCRL(
  452. /* [in] */ BSTR const strConfig,
  453. /* [in] */ LONG Flags,
  454. /* [out, retval] */ BSTR *pstrCRL)
  455. {
  456. HRESULT hr;
  457. WCHAR const *pwszAuthority;
  458. CERTTRANSBLOB ctbCRL;
  459. ctbCRL.pb = NULL;
  460. if (NULL == pstrCRL)
  461. {
  462. hr = E_POINTER;
  463. _JumpError(hr, error, "NULL parm");
  464. }
  465. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  466. _JumpIfError(hr, error, "_OpenConnection");
  467. __try
  468. {
  469. hr = m_pICertAdminD->GetCRL(pwszAuthority, &ctbCRL);
  470. }
  471. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  472. {
  473. }
  474. _JumpIfError(hr, error, "GetCRL");
  475. myRegisterMemAlloc(ctbCRL.pb, ctbCRL.cb, CSM_COTASKALLOC);
  476. CSASSERT(CR_OUT_BASE64HEADER == CRYPT_STRING_BASE64HEADER);
  477. if (CR_OUT_BASE64HEADER == Flags)
  478. {
  479. Flags = CRYPT_STRING_BASE64X509CRLHEADER;
  480. }
  481. hr = EncodeCertString(ctbCRL.pb, ctbCRL.cb, Flags, pstrCRL);
  482. _JumpIfError(hr, error, "EncodeCertString");
  483. error:
  484. if (NULL != ctbCRL.pb)
  485. {
  486. CoTaskMemFree(ctbCRL.pb);
  487. }
  488. hr = myHError(hr);
  489. return(_SetErrorInfo(hr, L"CCertAdmin::GetCRL"));
  490. }
  491. #define CCERTADMIN
  492. #include "csprop2.cpp"
  493. //+--------------------------------------------------------------------------
  494. // CCertAdmin::ImportCertificate -- Import a certificate into the database
  495. //
  496. // ...
  497. //
  498. // Returns S_OK on success.
  499. //+--------------------------------------------------------------------------
  500. STDMETHODIMP
  501. CCertAdmin::ImportCertificate(
  502. /* [in] */ BSTR const strConfig,
  503. /* [in] */ BSTR const strCertificate,
  504. /* [in] */ LONG Flags,
  505. /* [out, retval] */ LONG *pRequestId)
  506. {
  507. HRESULT hr;
  508. WCHAR const *pwszAuthority;
  509. CERTTRANSBLOB ctbCert;
  510. ctbCert.pb = NULL;
  511. if (NULL == strCertificate)
  512. {
  513. hr = E_POINTER;
  514. _JumpError(hr, error, "NULL parm");
  515. }
  516. hr = DecodeCertString(
  517. strCertificate,
  518. Flags & CR_IN_ENCODEMASK,
  519. &ctbCert.pb,
  520. &ctbCert.cb);
  521. _JumpIfError(hr, error, "DecodeCertString");
  522. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  523. _JumpIfError(hr, error, "_OpenConnection");
  524. __try
  525. {
  526. hr = m_pICertAdminD->ImportCertificate(
  527. pwszAuthority,
  528. &ctbCert,
  529. Flags,
  530. pRequestId);
  531. }
  532. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  533. {
  534. }
  535. _JumpIfError3(
  536. hr,
  537. error,
  538. "ImportCertificate",
  539. NTE_BAD_SIGNATURE,
  540. HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS));
  541. error:
  542. if (NULL != ctbCert.pb)
  543. {
  544. LocalFree(ctbCert.pb);
  545. }
  546. hr = myHError(hr);
  547. return(_SetErrorInfo(hr, L"CCertAdmin::ImportCertificate"));
  548. }
  549. //+--------------------------------------------------------------------------
  550. // CCertAdmin::GetArchivedKey -- Get archived, encrypted key in a PKCS7
  551. //
  552. // ...
  553. //
  554. // Returns S_OK on success.
  555. //+--------------------------------------------------------------------------
  556. STDMETHODIMP
  557. CCertAdmin::GetArchivedKey(
  558. /* [in] */ BSTR const strConfig,
  559. /* [in] */ LONG RequestId,
  560. /* [in] */ LONG Flags,
  561. /* [out, retval] */ BSTR *pstrArchivedKey)
  562. {
  563. HRESULT hr;
  564. WCHAR const *pwszAuthority;
  565. CERTTRANSBLOB ctbArchivedKey;
  566. ctbArchivedKey.pb = NULL;
  567. if (NULL == pstrArchivedKey)
  568. {
  569. hr = E_POINTER;
  570. _JumpError(hr, error, "NULL parm");
  571. }
  572. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  573. _JumpIfError(hr, error, "_OpenConnection");
  574. __try
  575. {
  576. hr = m_pICertAdminD->GetArchivedKey(
  577. pwszAuthority,
  578. RequestId,
  579. &ctbArchivedKey);
  580. }
  581. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  582. {
  583. }
  584. _JumpIfError(hr, error, "GetArchivedKey");
  585. myRegisterMemAlloc(
  586. ctbArchivedKey.pb,
  587. ctbArchivedKey.cb,
  588. CSM_COTASKALLOC);
  589. CSASSERT(CR_OUT_BASE64HEADER == CRYPT_STRING_BASE64HEADER);
  590. hr = EncodeCertString(
  591. ctbArchivedKey.pb,
  592. ctbArchivedKey.cb,
  593. Flags,
  594. pstrArchivedKey);
  595. _JumpIfError(hr, error, "EncodeCertString");
  596. error:
  597. if (NULL != ctbArchivedKey.pb)
  598. {
  599. CoTaskMemFree(ctbArchivedKey.pb);
  600. }
  601. hr = myHError(hr);
  602. return(_SetErrorInfo(hr, L"CCertAdmin::GetArchivedKey"));
  603. }
  604. //+--------------------------------------------------------------------------
  605. // CCertAdmin::GetConfigEntry -- get CA configuration entry
  606. //
  607. // ...
  608. //
  609. // Returns S_OK on success.
  610. //+--------------------------------------------------------------------------
  611. STDMETHODIMP
  612. CCertAdmin::GetConfigEntry(
  613. /* [in] */ BSTR const strConfig,
  614. /* [in] */ BSTR const strNodePath,
  615. /* [in] */ BSTR const strEntryName,
  616. /* [out, retval] */ VARIANT *pvarEntry)
  617. {
  618. HRESULT hr;
  619. WCHAR const *pwszAuthority;
  620. if (NULL == strEntryName || NULL == pvarEntry)
  621. {
  622. hr = E_POINTER;
  623. _JumpError(hr, error, "NULL parm");
  624. }
  625. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  626. __try
  627. {
  628. if(S_OK != hr)
  629. {
  630. hr = _GetConfigEntryFromRegistry(
  631. strConfig,
  632. strNodePath,
  633. strEntryName,
  634. pvarEntry);
  635. _JumpIfError(hr, error, "_GetConfigEntryFromRegistry");
  636. }
  637. else
  638. {
  639. hr = m_pICertAdminD->GetConfigEntry(
  640. pwszAuthority,
  641. strNodePath,
  642. strEntryName,
  643. pvarEntry);
  644. }
  645. }
  646. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  647. {
  648. }
  649. _JumpIfError2(hr, error, "GetConfigEntry",
  650. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
  651. error:
  652. hr = myHError(hr);
  653. return(_SetErrorInfo(hr, L"CCertAdmin::GetConfigEntry"));
  654. }
  655. //+--------------------------------------------------------------------------
  656. // CCertAdmin::SetConfigEntry -- set CA configuration entry
  657. //
  658. // ...
  659. //
  660. // Returns S_OK on success.
  661. //+--------------------------------------------------------------------------
  662. STDMETHODIMP
  663. CCertAdmin::SetConfigEntry(
  664. /* [in] */ BSTR const strConfig,
  665. /* [in] */ BSTR const strNodePath,
  666. /* [in] */ BSTR const strEntryName,
  667. /* [in] */ VARIANT *pvarEntry)
  668. {
  669. HRESULT hr;
  670. WCHAR const *pwszAuthority;
  671. if (NULL == strEntryName || NULL == pvarEntry)
  672. {
  673. hr = E_POINTER;
  674. _JumpError(hr, error, "NULL parm");
  675. }
  676. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  677. __try
  678. {
  679. if(S_OK != hr)
  680. {
  681. hr = _SetConfigEntryFromRegistry(
  682. strConfig,
  683. strNodePath,
  684. strEntryName,
  685. pvarEntry);
  686. }
  687. else
  688. {
  689. hr = m_pICertAdminD->SetConfigEntry(
  690. pwszAuthority,
  691. strNodePath,
  692. strEntryName,
  693. pvarEntry);
  694. }
  695. }
  696. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  697. {
  698. }
  699. _JumpIfError(hr, error, "SetConfigEntry");
  700. error:
  701. hr = myHError(hr);
  702. return(_SetErrorInfo(hr, L"CCertAdmin::SetConfigEntry"));
  703. }
  704. //+--------------------------------------------------------------------------
  705. // CCertAdmin::ImportKey -- Archive Private Key
  706. //
  707. // ...
  708. //
  709. // Returns S_OK on success.
  710. //+--------------------------------------------------------------------------
  711. STDMETHODIMP
  712. CCertAdmin::ImportKey(
  713. /* [in] */ BSTR const strConfig,
  714. /* [in] */ LONG RequestId,
  715. /* [in] */ BSTR const strCertHash,
  716. /* [in] */ LONG Flags,
  717. /* [in] */ BSTR const strKey)
  718. {
  719. HRESULT hr;
  720. WCHAR const *pwszAuthority;
  721. CERTTRANSBLOB ctbKey;
  722. ctbKey.pb = NULL;
  723. if (NULL == strKey)
  724. {
  725. hr = E_POINTER;
  726. _JumpError(hr, error, "NULL parm");
  727. }
  728. hr = DecodeCertString(
  729. strKey,
  730. Flags & CR_IN_ENCODEMASK,
  731. &ctbKey.pb,
  732. &ctbKey.cb);
  733. _JumpIfError(hr, error, "DecodeCertString");
  734. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  735. _JumpIfError(hr, error, "_OpenConnection");
  736. __try
  737. {
  738. hr = m_pICertAdminD->ImportKey(
  739. pwszAuthority,
  740. RequestId,
  741. strCertHash,
  742. Flags,
  743. &ctbKey);
  744. }
  745. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  746. {
  747. }
  748. _JumpIfError2(
  749. hr,
  750. error,
  751. "ImportKey",
  752. HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS));
  753. error:
  754. if (NULL != ctbKey.pb)
  755. {
  756. LocalFree(ctbKey.pb);
  757. }
  758. hr = myHError(hr);
  759. return(_SetErrorInfo(hr, L"CCertAdmin::ImportKey"));
  760. }
  761. HRESULT
  762. CCertAdmin::_SetErrorInfo(
  763. IN HRESULT hrError,
  764. IN WCHAR const *pwszDescription)
  765. {
  766. CSASSERT(FAILED(hrError) || S_OK == hrError || S_FALSE == hrError);
  767. if (FAILED(hrError))
  768. {
  769. HRESULT hr;
  770. hr = DispatchSetErrorInfo(
  771. hrError,
  772. pwszDescription,
  773. wszCLASS_CERTADMIN,
  774. &IID_ICertAdmin);
  775. CSASSERT(hr == hrError);
  776. }
  777. return(hrError);
  778. }
  779. //+--------------------------------------------------------------------------
  780. // CCertAdmin::GetMyRoles -- Gets current user roles
  781. //
  782. // ...
  783. //
  784. // Returns S_OK on success.
  785. //+--------------------------------------------------------------------------
  786. STDMETHODIMP
  787. CCertAdmin::GetMyRoles(
  788. /* [in] */ BSTR const strConfig,
  789. /* [out, retval] */ LONG *pRoles)
  790. {
  791. HRESULT hr;
  792. WCHAR const *pwszAuthority;
  793. LONG Roles = 0;
  794. if (NULL == strConfig || NULL == pRoles)
  795. {
  796. hr = E_POINTER;
  797. _JumpError(hr, error, "NULL parm");
  798. }
  799. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  800. _JumpIfError(hr, error, "_OpenConnection");
  801. __try
  802. {
  803. hr = m_pICertAdminD->GetMyRoles(
  804. pwszAuthority,
  805. pRoles);
  806. }
  807. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  808. {
  809. }
  810. _JumpIfError(hr, error, "GetMyRoles");
  811. error:
  812. hr = myHError(hr);
  813. return(_SetErrorInfo(hr, L"CCertAdmin::GetMyRoles"));
  814. }
  815. //+--------------------------------------------------------------------------
  816. // CCertAdmin::DeleteRow -- Delete row from database
  817. //
  818. // ...
  819. //
  820. // Returns S_OK on success.
  821. //+--------------------------------------------------------------------------
  822. STDMETHODIMP
  823. CCertAdmin::DeleteRow(
  824. /* [in] */ BSTR const strConfig,
  825. /* [in] */ LONG Flags, // CDR_*
  826. /* [in] */ DATE Date,
  827. /* [in] */ LONG Table, // CVRC_TABLE_*
  828. /* [in] */ LONG RowId,
  829. /* [out, retval]*/ LONG *pcDeleted)
  830. {
  831. HRESULT hr;
  832. FILETIME ft;
  833. WCHAR const *pwszAuthority;
  834. if (NULL == strConfig)
  835. {
  836. hr = E_POINTER;
  837. _JumpError(hr, error, "NULL parm");
  838. }
  839. // Date = 0.0 means pass ft = 0 to the DCOM interface
  840. if (Date == 0.0)
  841. {
  842. ZeroMemory(&ft, sizeof(FILETIME));
  843. }
  844. else // translate date to ft
  845. {
  846. hr = myDateToFileTime(&Date, &ft);
  847. _JumpIfError(hr, error, "myDateToFileTime");
  848. }
  849. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  850. _JumpIfError(hr, error, "_OpenConnection");
  851. __try
  852. {
  853. hr = m_pICertAdminD->DeleteRow(
  854. pwszAuthority,
  855. Flags,
  856. ft,
  857. Table,
  858. RowId,
  859. pcDeleted);
  860. }
  861. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  862. {
  863. }
  864. _JumpIfError(hr, error, "DeleteRow");
  865. error:
  866. hr = myHError(hr);
  867. return(_SetErrorInfo(hr, L"CCertAdmin::DeleteRow"));
  868. }
  869. HRESULT
  870. CCertAdmin::_GetConfigEntryFromRegistry(
  871. IN BSTR const strConfig,
  872. IN BSTR const strNodePath,
  873. IN BSTR const strEntryName,
  874. IN OUT VARIANT *pvarEntry)
  875. {
  876. HRESULT hr;
  877. CertSrv::CConfigStorage stg;
  878. LPWSTR pwszMachine = NULL;
  879. LPWSTR pwszCAName = NULL;
  880. hr = mySplitConfigString(
  881. strConfig,
  882. &pwszMachine,
  883. &pwszCAName);
  884. _JumpIfErrorStr(hr, error, "mySplitConfigString", strConfig);
  885. hr = stg.InitMachine(pwszMachine);
  886. _JumpIfError(hr, error, "CConfigStorage::InitMachine");
  887. hr = stg.GetEntry(
  888. pwszCAName,
  889. strNodePath,
  890. strEntryName,
  891. pvarEntry);
  892. _JumpIfError(hr, error, "CConfigStorage::GetEntry");
  893. error:
  894. LOCAL_FREE(pwszMachine);
  895. LOCAL_FREE(pwszCAName);
  896. return hr;
  897. }
  898. HRESULT
  899. CCertAdmin::_SetConfigEntryFromRegistry(
  900. IN BSTR const strConfig,
  901. IN BSTR const strNodePath,
  902. IN BSTR const strEntryName,
  903. IN const VARIANT *pvarEntry)
  904. {
  905. HRESULT hr;
  906. CertSrv::CConfigStorage stg;
  907. LPWSTR pwszMachine = NULL;
  908. LPWSTR pwszCAName = NULL;
  909. hr = mySplitConfigString(
  910. strConfig,
  911. &pwszMachine,
  912. &pwszCAName);
  913. _JumpIfErrorStr(hr, error, "mySplitConfigString", strConfig);
  914. hr = stg.InitMachine(pwszMachine);
  915. _JumpIfError(hr, error, "CConfigStorage::InitMachine");
  916. hr = stg.SetEntry(
  917. pwszCAName,
  918. strNodePath,
  919. strEntryName,
  920. const_cast<VARIANT*>(pvarEntry));
  921. _JumpIfError(hr, error, "CConfigStorage::GetEntry");
  922. error:
  923. LOCAL_FREE(pwszMachine);
  924. LOCAL_FREE(pwszCAName);
  925. return hr;
  926. }