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.

1103 lines
26 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. CERTTRANSBLOB ctbValue;
  264. ctbValue.pb = NULL;
  265. if (NULL == strExtensionName || NULL == pvarValue)
  266. {
  267. hr = E_POINTER;
  268. _JumpError(hr, error, "NULL parm");
  269. }
  270. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  271. _JumpIfError(hr, error, "_OpenConnection");
  272. hr = myMarshalVariant(pvarValue, Type, &ctbValue.cb, &ctbValue.pb);
  273. _JumpIfError(hr, error, "myMarshalVariant");
  274. __try
  275. {
  276. hr = m_pICertAdminD->SetExtension(
  277. pwszAuthority,
  278. (DWORD) RequestId,
  279. strExtensionName,
  280. Type,
  281. Flags,
  282. &ctbValue);
  283. }
  284. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  285. {
  286. }
  287. _JumpIfError(hr, error, "SetExtension");
  288. error:
  289. if (NULL != ctbValue.pb)
  290. {
  291. LocalFree(ctbValue.pb);
  292. }
  293. hr = myHError(hr);
  294. return(_SetErrorInfo(hr, L"CCertAdmin::SetCertificateExtension"));
  295. }
  296. //+--------------------------------------------------------------------------
  297. // CCertAdmin::ResubmitRequest -- Resubmit a certificate request
  298. //
  299. // ...
  300. //
  301. // Returns S_OK on success.
  302. //+--------------------------------------------------------------------------
  303. STDMETHODIMP
  304. CCertAdmin::ResubmitRequest(
  305. /* [in] */ BSTR const strConfig,
  306. /* [in] */ LONG RequestId,
  307. /* [out, retval] */ LONG *pDisposition)
  308. {
  309. HRESULT hr;
  310. WCHAR const *pwszAuthority;
  311. if (NULL == pDisposition)
  312. {
  313. hr = E_POINTER;
  314. _JumpError(hr, error, "NULL parm");
  315. }
  316. *pDisposition = 0;
  317. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  318. _JumpIfError(hr, error, "_OpenConnection");
  319. __try
  320. {
  321. hr = m_pICertAdminD->ResubmitRequest(
  322. pwszAuthority,
  323. (DWORD) RequestId,
  324. (DWORD *) pDisposition);
  325. }
  326. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  327. {
  328. }
  329. _JumpIfError(hr, error, "ResubmitRequest");
  330. error:
  331. hr = myHError(hr);
  332. return(_SetErrorInfo(hr, L"CCertAdmin::ResubmitRequest"));
  333. }
  334. //+--------------------------------------------------------------------------
  335. // CCertAdmin::DenyRequest -- Deny a certificate request
  336. //
  337. // ...
  338. //
  339. // Returns S_OK on success.
  340. //+--------------------------------------------------------------------------
  341. STDMETHODIMP
  342. CCertAdmin::DenyRequest(
  343. /* [in] */ BSTR const strConfig,
  344. /* [in] */ LONG RequestId)
  345. {
  346. HRESULT hr;
  347. WCHAR const *pwszAuthority;
  348. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  349. _JumpIfError(hr, error, "_OpenConnection");
  350. __try
  351. {
  352. hr = m_pICertAdminD->DenyRequest(pwszAuthority, (DWORD) RequestId);
  353. }
  354. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  355. {
  356. }
  357. _JumpIfError(hr, error, "DenyRequest");
  358. error:
  359. hr = myHError(hr);
  360. return(_SetErrorInfo(hr, L"CCertAdmin::DenyRequest"));
  361. }
  362. //+--------------------------------------------------------------------------
  363. // CCertAdmin::PublishCRL -- Puhlish a new CRL
  364. //
  365. // ...
  366. //
  367. // Returns S_OK on success.
  368. //+--------------------------------------------------------------------------
  369. STDMETHODIMP
  370. CCertAdmin::PublishCRL(
  371. /* [in] */ BSTR const strConfig,
  372. /* [in] */ DATE Date)
  373. {
  374. HRESULT hr;
  375. WCHAR const *pwszAuthority;
  376. FILETIME ft;
  377. // Date = 0.0 means pass ft = 0 to the DCOM interface
  378. if (Date == 0.0)
  379. {
  380. ZeroMemory(&ft, sizeof(FILETIME));
  381. }
  382. else // translate date to ft
  383. {
  384. hr = myDateToFileTime(&Date, &ft);
  385. _JumpIfError(hr, error, "myDateToFileTime");
  386. }
  387. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  388. _JumpIfError(hr, error, "_OpenConnection");
  389. __try
  390. {
  391. hr = m_pICertAdminD->PublishCRL(pwszAuthority, ft);
  392. }
  393. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  394. {
  395. }
  396. _JumpIfError(hr, error, "PublishCRL");
  397. error:
  398. hr = myHError(hr);
  399. return(_SetErrorInfo(hr, L"CCertAdmin::PublishCRL"));
  400. }
  401. //+--------------------------------------------------------------------------
  402. // CCertAdmin::PublishCRLs -- Publish new base CRL, delta CRL or both
  403. //
  404. // ...
  405. //
  406. // Returns S_OK on success.
  407. //+--------------------------------------------------------------------------
  408. HRESULT
  409. CCertAdmin::PublishCRLs(
  410. /* [in] */ BSTR const strConfig,
  411. /* [in] */ DATE Date,
  412. /* [in] */ LONG CRLFlags) // CA_CRL_*
  413. {
  414. HRESULT hr;
  415. WCHAR const *pwszAuthority;
  416. FILETIME ft;
  417. // Date = 0.0 means pass ft = 0 to the DCOM interface
  418. if (Date == 0.0)
  419. {
  420. ZeroMemory(&ft, sizeof(FILETIME));
  421. }
  422. else // translate date to ft
  423. {
  424. hr = myDateToFileTime(&Date, &ft);
  425. _JumpIfError(hr, error, "myDateToFileTime");
  426. }
  427. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  428. _JumpIfError(hr, error, "_OpenConnection");
  429. __try
  430. {
  431. hr = m_pICertAdminD->PublishCRLs(pwszAuthority, ft, CRLFlags);
  432. }
  433. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  434. {
  435. }
  436. _JumpIfError(hr, error, "PublishCRLs");
  437. error:
  438. hr = myHError(hr);
  439. return(_SetErrorInfo(hr, L"CCertAdmin::PublishCRLs"));
  440. }
  441. //+--------------------------------------------------------------------------
  442. // CCertAdmin::GetCRL -- Get the latest CRL
  443. //
  444. // ...
  445. //
  446. // Returns S_OK on success.
  447. //+--------------------------------------------------------------------------
  448. STDMETHODIMP
  449. CCertAdmin::GetCRL(
  450. /* [in] */ BSTR const strConfig,
  451. /* [in] */ LONG Flags,
  452. /* [out, retval] */ BSTR *pstrCRL)
  453. {
  454. HRESULT hr;
  455. WCHAR const *pwszAuthority;
  456. CERTTRANSBLOB ctbCRL;
  457. ctbCRL.pb = NULL;
  458. if (NULL == pstrCRL)
  459. {
  460. hr = E_POINTER;
  461. _JumpError(hr, error, "NULL parm");
  462. }
  463. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  464. _JumpIfError(hr, error, "_OpenConnection");
  465. ctbCRL.cb = 0;
  466. __try
  467. {
  468. hr = m_pICertAdminD->GetCRL(pwszAuthority, &ctbCRL);
  469. }
  470. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  471. {
  472. }
  473. _JumpIfError(hr, error, "GetCRL");
  474. myRegisterMemAlloc(ctbCRL.pb, ctbCRL.cb, CSM_COTASKALLOC);
  475. CSASSERT(CR_OUT_BASE64HEADER == CRYPT_STRING_BASE64HEADER);
  476. if (CR_OUT_BASE64HEADER == Flags)
  477. {
  478. Flags = CRYPT_STRING_BASE64X509CRLHEADER;
  479. }
  480. hr = EncodeCertString(ctbCRL.pb, ctbCRL.cb, Flags, pstrCRL);
  481. _JumpIfError(hr, error, "EncodeCertString");
  482. error:
  483. if (NULL != ctbCRL.pb)
  484. {
  485. CoTaskMemFree(ctbCRL.pb);
  486. }
  487. hr = myHError(hr);
  488. return(_SetErrorInfo(hr, L"CCertAdmin::GetCRL"));
  489. }
  490. //+--------------------------------------------------------------------------
  491. // CCertAdmin::ImportCertificate -- Import a certificate into the database
  492. //
  493. // ...
  494. //
  495. // Returns S_OK on success.
  496. //+--------------------------------------------------------------------------
  497. STDMETHODIMP
  498. CCertAdmin::ImportCertificate(
  499. /* [in] */ BSTR const strConfig,
  500. /* [in] */ BSTR const strCertificate,
  501. /* [in] */ LONG Flags,
  502. /* [out, retval] */ LONG *pRequestId)
  503. {
  504. HRESULT hr;
  505. WCHAR const *pwszAuthority;
  506. CERTTRANSBLOB ctbCert;
  507. ctbCert.pb = NULL;
  508. if (NULL == strCertificate)
  509. {
  510. hr = E_POINTER;
  511. _JumpError(hr, error, "NULL parm");
  512. }
  513. hr = DecodeCertString(
  514. strCertificate,
  515. Flags & CR_IN_ENCODEMASK,
  516. &ctbCert.pb,
  517. &ctbCert.cb);
  518. _JumpIfError(hr, error, "DecodeCertString");
  519. hr = _OpenConnection(strConfig, 1, &pwszAuthority);
  520. _JumpIfError(hr, error, "_OpenConnection");
  521. __try
  522. {
  523. hr = m_pICertAdminD->ImportCertificate(
  524. pwszAuthority,
  525. &ctbCert,
  526. Flags,
  527. pRequestId);
  528. }
  529. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  530. {
  531. }
  532. _JumpIfError3(
  533. hr,
  534. error,
  535. "ImportCertificate",
  536. NTE_BAD_SIGNATURE,
  537. HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS));
  538. error:
  539. if (NULL != ctbCert.pb)
  540. {
  541. LocalFree(ctbCert.pb);
  542. }
  543. hr = myHError(hr);
  544. return(_SetErrorInfo(hr, L"CCertAdmin::ImportCertificate"));
  545. }
  546. //+--------------------------------------------------------------------------
  547. // CCertAdmin::GetArchivedKey -- Get archived, encrypted key in a PKCS7
  548. //
  549. // ...
  550. //
  551. // Returns S_OK on success.
  552. //+--------------------------------------------------------------------------
  553. STDMETHODIMP
  554. CCertAdmin::GetArchivedKey(
  555. /* [in] */ BSTR const strConfig,
  556. /* [in] */ LONG RequestId,
  557. /* [in] */ LONG Flags,
  558. /* [out, retval] */ BSTR *pstrArchivedKey)
  559. {
  560. HRESULT hr;
  561. WCHAR const *pwszAuthority;
  562. CERTTRANSBLOB ctbArchivedKey;
  563. ctbArchivedKey.pb = NULL;
  564. if (NULL == pstrArchivedKey)
  565. {
  566. hr = E_POINTER;
  567. _JumpError(hr, error, "NULL parm");
  568. }
  569. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  570. _JumpIfError(hr, error, "_OpenConnection");
  571. ctbArchivedKey.cb = 0;
  572. __try
  573. {
  574. hr = m_pICertAdminD->GetArchivedKey(
  575. pwszAuthority,
  576. RequestId,
  577. &ctbArchivedKey);
  578. }
  579. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  580. {
  581. }
  582. _JumpIfError(hr, error, "GetArchivedKey");
  583. myRegisterMemAlloc(
  584. ctbArchivedKey.pb,
  585. ctbArchivedKey.cb,
  586. CSM_COTASKALLOC);
  587. CSASSERT(CR_OUT_BASE64HEADER == CRYPT_STRING_BASE64HEADER);
  588. hr = EncodeCertString(
  589. ctbArchivedKey.pb,
  590. ctbArchivedKey.cb,
  591. Flags,
  592. pstrArchivedKey);
  593. _JumpIfError(hr, error, "EncodeCertString");
  594. error:
  595. if (NULL != ctbArchivedKey.pb)
  596. {
  597. CoTaskMemFree(ctbArchivedKey.pb);
  598. }
  599. hr = myHError(hr);
  600. return(_SetErrorInfo(hr, L"CCertAdmin::GetArchivedKey"));
  601. }
  602. //+--------------------------------------------------------------------------
  603. // CCertAdmin::GetConfigEntry -- get CA configuration entry
  604. //
  605. // ...
  606. //
  607. // Returns S_OK on success.
  608. //+--------------------------------------------------------------------------
  609. STDMETHODIMP
  610. CCertAdmin::GetConfigEntry(
  611. /* [in] */ BSTR const strConfig,
  612. /* [in] */ BSTR const strNodePath,
  613. /* [in] */ BSTR const strEntryName,
  614. /* [out, retval] */ VARIANT *pvarEntry)
  615. {
  616. HRESULT hr;
  617. WCHAR const *pwszAuthority;
  618. if (NULL == strEntryName || NULL == pvarEntry)
  619. {
  620. hr = E_POINTER;
  621. _JumpError(hr, error, "NULL parm");
  622. }
  623. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  624. __try
  625. {
  626. if(S_OK != hr)
  627. {
  628. hr = _GetConfigEntryFromRegistry(
  629. strConfig,
  630. strNodePath,
  631. strEntryName,
  632. pvarEntry);
  633. _LeaveIfError(hr, "_GetConfigEntryFromRegistry");
  634. }
  635. else
  636. {
  637. hr = m_pICertAdminD->GetConfigEntry(
  638. pwszAuthority,
  639. strNodePath,
  640. strEntryName,
  641. pvarEntry);
  642. _LeaveIfError(hr, "GetConfigEntry");
  643. myRegisterMemAlloc(pvarEntry, 0, CSM_VARIANT);
  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. if (NULL == strConfig || NULL == pRoles)
  794. {
  795. hr = E_POINTER;
  796. _JumpError(hr, error, "NULL parm");
  797. }
  798. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  799. _JumpIfError(hr, error, "_OpenConnection");
  800. __try
  801. {
  802. hr = m_pICertAdminD->GetMyRoles(
  803. pwszAuthority,
  804. pRoles);
  805. }
  806. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  807. {
  808. }
  809. _JumpIfError(hr, error, "GetMyRoles");
  810. error:
  811. hr = myHError(hr);
  812. return(_SetErrorInfo(hr, L"CCertAdmin::GetMyRoles"));
  813. }
  814. //+--------------------------------------------------------------------------
  815. // CCertAdmin::DeleteRow -- Delete row from database
  816. //
  817. // ...
  818. //
  819. // Returns S_OK on success.
  820. //+--------------------------------------------------------------------------
  821. STDMETHODIMP
  822. CCertAdmin::DeleteRow(
  823. /* [in] */ BSTR const strConfig,
  824. /* [in] */ LONG Flags, // CDR_*
  825. /* [in] */ DATE Date,
  826. /* [in] */ LONG Table, // CVRC_TABLE_*
  827. /* [in] */ LONG RowId,
  828. /* [out, retval]*/ LONG *pcDeleted)
  829. {
  830. HRESULT hr;
  831. FILETIME ft;
  832. WCHAR const *pwszAuthority;
  833. if (NULL == strConfig)
  834. {
  835. hr = E_POINTER;
  836. _JumpError(hr, error, "NULL parm");
  837. }
  838. // Date = 0.0 means pass ft = 0 to the DCOM interface
  839. if (Date == 0.0)
  840. {
  841. ZeroMemory(&ft, sizeof(FILETIME));
  842. }
  843. else // translate date to ft
  844. {
  845. hr = myDateToFileTime(&Date, &ft);
  846. _JumpIfError(hr, error, "myDateToFileTime");
  847. }
  848. hr = _OpenConnection(strConfig, 2, &pwszAuthority);
  849. _JumpIfError(hr, error, "_OpenConnection");
  850. __try
  851. {
  852. hr = m_pICertAdminD->DeleteRow(
  853. pwszAuthority,
  854. Flags,
  855. ft,
  856. Table,
  857. RowId,
  858. pcDeleted);
  859. }
  860. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  861. {
  862. }
  863. _JumpIfError(hr, error, "DeleteRow");
  864. error:
  865. hr = myHError(hr);
  866. return(_SetErrorInfo(hr, L"CCertAdmin::DeleteRow"));
  867. }
  868. HRESULT
  869. CCertAdmin::_GetConfigEntryFromRegistry(
  870. IN BSTR const strConfig,
  871. IN BSTR const strNodePath,
  872. IN BSTR const strEntryName,
  873. IN OUT VARIANT *pvarEntry)
  874. {
  875. HRESULT hr;
  876. CertSrv::CConfigStorage stg;
  877. LPWSTR pwszMachine = NULL;
  878. LPWSTR pwszCAName = NULL;
  879. hr = mySplitConfigString(
  880. strConfig,
  881. &pwszMachine,
  882. &pwszCAName);
  883. _JumpIfErrorStr(hr, error, "mySplitConfigString", strConfig);
  884. hr = stg.InitMachine(pwszMachine);
  885. _JumpIfError(hr, error, "CConfigStorage::InitMachine");
  886. hr = stg.GetEntry(
  887. pwszCAName,
  888. strNodePath,
  889. strEntryName,
  890. pvarEntry);
  891. _JumpIfError(hr, error, "CConfigStorage::GetEntry");
  892. error:
  893. LOCAL_FREE(pwszMachine);
  894. LOCAL_FREE(pwszCAName);
  895. return hr;
  896. }
  897. HRESULT
  898. CCertAdmin::_SetConfigEntryFromRegistry(
  899. IN BSTR const strConfig,
  900. IN BSTR const strNodePath,
  901. IN BSTR const strEntryName,
  902. IN const VARIANT *pvarEntry)
  903. {
  904. HRESULT hr;
  905. CertSrv::CConfigStorage stg;
  906. LPWSTR pwszMachine = NULL;
  907. LPWSTR pwszCAName = NULL;
  908. hr = mySplitConfigString(
  909. strConfig,
  910. &pwszMachine,
  911. &pwszCAName);
  912. _JumpIfErrorStr(hr, error, "mySplitConfigString", strConfig);
  913. hr = stg.InitMachine(pwszMachine);
  914. _JumpIfError(hr, error, "CConfigStorage::InitMachine");
  915. hr = stg.SetEntry(
  916. pwszCAName,
  917. strNodePath,
  918. strEntryName,
  919. const_cast<VARIANT*>(pvarEntry));
  920. _JumpIfError(hr, error, "CConfigStorage::GetEntry");
  921. error:
  922. LOCAL_FREE(pwszMachine);
  923. LOCAL_FREE(pwszCAName);
  924. return hr;
  925. }
  926. #undef __DIR__
  927. #undef __dwFILE__
  928. #define CCERTADMIN
  929. #include "csprop2.cpp"