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.

1320 lines
32 KiB

  1. /**********************************************************************/
  2. /** Microsoft Passport **/
  3. /** Copyright(c) Microsoft Corporation, 1999 - 2001 **/
  4. /**********************************************************************/
  5. /*
  6. admin.cpp
  7. DCOM object for pmconfig -- admin utility to call to configure passport
  8. FILE HISTORY:
  9. */// Admin.cpp : Implementation of CAdmin
  10. #include "stdafx.h"
  11. #include "Passport.h"
  12. #include "Admin.h"
  13. #include "keycrypto.h"
  14. #include <time.h>
  15. #define MAX_CCDPASSWORD_LEN 256
  16. #include "keyver.h"
  17. #define PASSPORT_KEY L"SOFTWARE\\Microsoft\\Passport"
  18. #define KEYDATA_KEY PASSPORT_KEY L"\\KeyData"
  19. #define KEYTIMES_KEY PASSPORT_KEY L"\\KeyTimes"
  20. #define SITES_KEY PASSPORT_KEY L"\\Sites"
  21. #define NEXUS_KEY PASSPORT_KEY L"\\Nexus"
  22. #define KEYDATA_SUBKEY L"KeyData"
  23. #define KEYTIMES_SUBKEY L"KeyTimes"
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAdmin
  26. //===========================================================================
  27. //
  28. // InterfaceSupportsErrorInfo
  29. //
  30. STDMETHODIMP CAdmin::InterfaceSupportsErrorInfo(REFIID riid)
  31. {
  32. static const IID* arr[] =
  33. {
  34. &IID_IPassportAdmin,
  35. &IID_IPassportAdminEx,
  36. };
  37. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  38. {
  39. if (InlineIsEqualGUID(*arr[i], riid))
  40. {
  41. return S_OK;
  42. }
  43. }
  44. return S_FALSE;
  45. }
  46. //===========================================================================
  47. //
  48. // get_IsValid
  49. //
  50. STDMETHODIMP CAdmin::get_IsValid(VARIANT_BOOL *pVal)
  51. {
  52. *pVal = g_config->isValid() ? VARIANT_TRUE : VARIANT_FALSE;
  53. return S_OK;
  54. }
  55. //===========================================================================
  56. //
  57. // get_ErrorDescription
  58. //
  59. STDMETHODIMP CAdmin::get_ErrorDescription(BSTR *pVal)
  60. {
  61. if (g_config->isValid())
  62. {
  63. *pVal = SysAllocString(L"Passport Object OK");
  64. }
  65. else
  66. {
  67. *pVal = SysAllocString(g_config->getFailureString());
  68. }
  69. return S_OK;
  70. }
  71. //===========================================================================
  72. //
  73. // addKey
  74. //
  75. STDMETHODIMP CAdmin::addKey(BSTR keyMaterial, int version, long expires, VARIANT_BOOL *ok)
  76. {
  77. HRESULT hr = S_OK;
  78. *ok = VARIANT_FALSE;
  79. // Must be the appropriate length
  80. if (SysStringLen(keyMaterial) != CKeyCrypto::RAWKEY_SIZE)
  81. {
  82. AtlReportError(CLSID_Admin, L"Key must be 24 characters", IID_IPassportAdmin, E_FAIL);
  83. return E_FAIL;
  84. }
  85. // Must be an appropriate version
  86. if (version > KEY_VERSION_MAX || version < KEY_VERSION_MIN)
  87. {
  88. AtlReportError(CLSID_Admin, L"Key version must be <36 and > 0", IID_IPassportAdmin, E_FAIL);
  89. return E_FAIL;
  90. }
  91. BYTE original[CKeyCrypto::RAWKEY_SIZE];
  92. DATA_BLOB iBlob;
  93. iBlob.cbData = sizeof(original);
  94. iBlob.pbData = &(original[0]);
  95. for (int i = 0; i < CKeyCrypto::RAWKEY_SIZE; i++)
  96. {
  97. original[i] = static_cast<BYTE>(keyMaterial[i] & 0xFF);
  98. }
  99. // Try to encrypt it
  100. CKeyCrypto kc;
  101. DATA_BLOB oBlob = {0};
  102. if (kc.encryptKey(&iBlob, &oBlob) != S_OK)
  103. {
  104. AtlReportError(CLSID_Admin,
  105. L"Failed to encrypt key, couldn't find valid network card?",
  106. IID_IPassportAdmin,
  107. E_FAIL);
  108. return E_FAIL;
  109. }
  110. // Now add it to registry
  111. LONG lResult;
  112. HKEY hkDataKey = NULL, hkTimeKey = NULL;
  113. char szKeyNum[2];
  114. szKeyNum[0] = KeyVerI2C(version);
  115. szKeyNum[1] = '\0';
  116. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEYDATA_KEY, 0, KEY_WRITE, &hkDataKey);
  117. if(lResult != ERROR_SUCCESS)
  118. {
  119. AtlReportError(CLSID_Admin,
  120. L"Couldn't find KeyData key in registry. Reinstall Passport.",
  121. IID_IPassportAdmin,
  122. E_FAIL);
  123. hr = E_FAIL;
  124. goto Cleanup;
  125. }
  126. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEYTIMES_KEY, 0, KEY_WRITE, &hkTimeKey);
  127. if(lResult != ERROR_SUCCESS)
  128. {
  129. AtlReportError(CLSID_Admin,
  130. L"Couldn't find KeyTimes key in registry. Reinstall Passport.",
  131. IID_IPassportAdmin,
  132. E_FAIL);
  133. hr = E_FAIL;
  134. goto Cleanup;
  135. }
  136. lResult = RegSetValueExA(hkDataKey, szKeyNum, 0, REG_BINARY, oBlob.pbData, oBlob.cbData);
  137. if(lResult != ERROR_SUCCESS)
  138. {
  139. AtlReportError(CLSID_Admin,
  140. L"Couldn't write KeyData key to registry.",
  141. IID_IPassportAdmin,
  142. E_FAIL);
  143. hr = E_FAIL;
  144. goto Cleanup;
  145. }
  146. if (expires != 0)
  147. {
  148. DWORD dwTime = expires;
  149. lResult = RegSetValueExA(hkTimeKey, szKeyNum, 0, REG_DWORD, (LPBYTE) &dwTime, sizeof(DWORD));
  150. if(lResult != ERROR_SUCCESS)
  151. {
  152. AtlReportError(CLSID_Admin,
  153. L"Couldn't write KeyTime value to registry.",
  154. IID_IPassportAdmin,
  155. E_FAIL);
  156. hr = E_FAIL;
  157. goto Cleanup;
  158. }
  159. }
  160. else
  161. {
  162. RegDeleteValueA(hkTimeKey, szKeyNum);
  163. }
  164. *ok = VARIANT_TRUE;
  165. Cleanup:
  166. if (hkDataKey)
  167. {
  168. RegCloseKey(hkDataKey);
  169. }
  170. if (hkTimeKey)
  171. {
  172. RegCloseKey(hkTimeKey);
  173. }
  174. if(oBlob.pbData)
  175. {
  176. LocalFree(oBlob.pbData);
  177. }
  178. if (*ok == VARIANT_TRUE)
  179. {
  180. if (g_pAlert)
  181. {
  182. g_pAlert->report(PassportAlertInterface::INFORMATION_TYPE, PM_NEWKEY_INSTALLED);
  183. }
  184. }
  185. return hr;
  186. }
  187. //===========================================================================
  188. //
  189. // addKeyEx
  190. //
  191. STDMETHODIMP CAdmin::addKeyEx(BSTR keyMaterial, int version, long expires, VARIANT vSiteName, VARIANT_BOOL *ok)
  192. {
  193. HRESULT hr = S_OK;
  194. LPSTR szSiteName = NULL;
  195. LONG lResult;
  196. HKEY hkDataKey = NULL, hkTimeKey = NULL;
  197. HKEY hkSites = NULL, hkPassport = NULL;
  198. char szKeyNum[2];
  199. BYTE original[CKeyCrypto::RAWKEY_SIZE];
  200. DATA_BLOB iBlob;
  201. DATA_BLOB oBlob = {0};
  202. CKeyCrypto kc;
  203. int i;
  204. *ok = VARIANT_FALSE;
  205. USES_CONVERSION;
  206. // Must be the appropriate length
  207. if (SysStringLen(keyMaterial) != CKeyCrypto::RAWKEY_SIZE)
  208. {
  209. AtlReportError(CLSID_Admin, L"Key must be 24 characters", IID_IPassportAdminEx, E_FAIL);
  210. return E_FAIL;
  211. }
  212. // Must be an appropriate version
  213. if (version > KEY_VERSION_MAX || version < KEY_VERSION_MIN)
  214. {
  215. AtlReportError(CLSID_Admin, L"Key version must be < 36 and > 0", IID_IPassportAdminEx, E_FAIL);
  216. return E_FAIL;
  217. }
  218. if(vSiteName.vt == VT_ERROR && vSiteName.scode == DISP_E_PARAMNOTFOUND)
  219. {
  220. szSiteName = NULL;
  221. }
  222. else if(vSiteName.vt == VT_BSTR)
  223. {
  224. szSiteName = W2A(vSiteName.bstrVal);
  225. }
  226. else
  227. {
  228. hr = E_INVALIDARG;
  229. goto Cleanup;
  230. }
  231. for (i = 0; i < CKeyCrypto::RAWKEY_SIZE; i++)
  232. {
  233. original[i] = static_cast<BYTE>(keyMaterial[i] & 0xFF);
  234. }
  235. iBlob.cbData = sizeof(original);
  236. iBlob.pbData = &(original[0]);
  237. // Try to encrypt it
  238. if (kc.encryptKey(&iBlob, &oBlob) != S_OK)
  239. {
  240. AtlReportError(CLSID_Admin,
  241. L"Failed to encrypt key, couldn't find valid network card?",
  242. IID_IPassportAdminEx,
  243. E_FAIL);
  244. return E_FAIL;
  245. }
  246. // Get the root key.
  247. if(szSiteName)
  248. {
  249. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  250. SITES_KEY,
  251. 0,
  252. KEY_ALL_ACCESS,
  253. &hkSites);
  254. if(lResult != ERROR_SUCCESS)
  255. {
  256. AtlReportError(CLSID_Admin,
  257. L"Site not found. Please add the site before installing the key.",
  258. IID_IPassportAdminEx,
  259. PP_E_SITE_NOT_EXISTS);
  260. hr = PP_E_SITE_NOT_EXISTS;
  261. goto Cleanup;
  262. }
  263. lResult = RegOpenKeyExA(hkSites,
  264. szSiteName,
  265. 0,
  266. KEY_ALL_ACCESS,
  267. &hkPassport);
  268. if(lResult != ERROR_SUCCESS)
  269. {
  270. AtlReportError(CLSID_Admin,
  271. L"Site not found. Please add the site before installing the key.",
  272. IID_IPassportAdminEx,
  273. PP_E_SITE_NOT_EXISTS);
  274. hr = PP_E_SITE_NOT_EXISTS;
  275. goto Cleanup;
  276. }
  277. }
  278. else
  279. {
  280. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  281. PASSPORT_KEY,
  282. 0,
  283. KEY_ALL_ACCESS,
  284. &hkPassport);
  285. if(lResult != ERROR_SUCCESS)
  286. {
  287. AtlReportError(CLSID_Admin,
  288. L"Couldn't find Passport key in registry. Reinstall Passport.",
  289. IID_IPassportAdminEx,
  290. E_FAIL);
  291. hr = E_FAIL;
  292. goto Cleanup;
  293. }
  294. }
  295. // Now add it to registry
  296. szKeyNum[0] = KeyVerI2C(version);
  297. szKeyNum[1] = '\0';
  298. lResult = RegOpenKeyEx(hkPassport, KEYDATA_SUBKEY, 0, KEY_WRITE, &hkDataKey);
  299. if (lResult != ERROR_SUCCESS)
  300. {
  301. AtlReportError(CLSID_Admin,
  302. L"Couldn't find KeyData key in registry. Reinstall Passport.",
  303. IID_IPassportAdminEx,
  304. E_FAIL);
  305. hr = E_FAIL;
  306. goto Cleanup;
  307. }
  308. lResult = RegOpenKeyEx(hkPassport, KEYTIMES_SUBKEY, 0, KEY_WRITE, &hkTimeKey);
  309. if(lResult != ERROR_SUCCESS)
  310. {
  311. AtlReportError(CLSID_Admin,
  312. L"Couldn't find KeyTimes key in registry. Reinstall Passport.",
  313. IID_IPassportAdminEx,
  314. E_FAIL);
  315. hr = E_FAIL;
  316. goto Cleanup;
  317. }
  318. lResult = RegSetValueExA(hkDataKey, szKeyNum, 0, REG_BINARY, oBlob.pbData, oBlob.cbData);
  319. if(lResult != ERROR_SUCCESS)
  320. {
  321. AtlReportError(CLSID_Admin,
  322. L"Couldn't write KeyData key to registry.",
  323. IID_IPassportAdminEx,
  324. E_FAIL);
  325. hr = E_FAIL;
  326. goto Cleanup;
  327. }
  328. if (expires != 0)
  329. {
  330. DWORD dwTime = expires;
  331. lResult = RegSetValueExA(hkTimeKey, szKeyNum, 0, REG_DWORD, (LPBYTE) &dwTime, sizeof(DWORD));
  332. if(lResult != ERROR_SUCCESS)
  333. {
  334. AtlReportError(CLSID_Admin,
  335. L"Couldn't write KeyTime value to registry.",
  336. IID_IPassportAdminEx,
  337. E_FAIL);
  338. hr = E_FAIL;
  339. goto Cleanup;
  340. }
  341. }
  342. else
  343. {
  344. RegDeleteValueA(hkTimeKey, szKeyNum);
  345. }
  346. *ok = VARIANT_TRUE;
  347. Cleanup:
  348. if (hkPassport)
  349. {
  350. RegCloseKey(hkPassport);
  351. }
  352. if (hkDataKey)
  353. {
  354. RegCloseKey(hkDataKey);
  355. }
  356. if (hkTimeKey)
  357. {
  358. RegCloseKey(hkTimeKey);
  359. }
  360. if (hkSites)
  361. {
  362. RegCloseKey(hkSites);
  363. }
  364. if (oBlob.pbData)
  365. {
  366. ::LocalFree(oBlob.pbData);
  367. }
  368. if (*ok == VARIANT_TRUE)
  369. {
  370. if (g_pAlert)
  371. {
  372. g_pAlert->report(PassportAlertInterface::INFORMATION_TYPE, PM_NEWKEY_INSTALLED);
  373. }
  374. }
  375. return hr;
  376. }
  377. //===========================================================================
  378. //
  379. // deleteKey
  380. //
  381. STDMETHODIMP CAdmin::deleteKey(int version)
  382. {
  383. HRESULT hr = S_OK, lResult;
  384. HKEY hkDataKey = NULL, hkTimeKey = NULL;
  385. char szKeyNum[2];
  386. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEYDATA_KEY, 0, KEY_WRITE, &hkDataKey);
  387. if(lResult != ERROR_SUCCESS)
  388. {
  389. AtlReportError(CLSID_Admin,
  390. L"Couldn't find KeyData key in registry. Reinstall Passport.",
  391. IID_IPassportAdmin,
  392. E_FAIL);
  393. hr = E_FAIL;
  394. goto Cleanup;
  395. }
  396. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEYTIMES_KEY, 0, KEY_WRITE, &hkTimeKey);
  397. if(lResult != ERROR_SUCCESS)
  398. {
  399. AtlReportError(CLSID_Admin,
  400. L"Couldn't find KeyTimes key in registry. Reinstall Passport.",
  401. IID_IPassportAdmin,
  402. E_FAIL);
  403. hr = E_FAIL;
  404. goto Cleanup;
  405. }
  406. szKeyNum[0] = KeyVerI2C(version);
  407. szKeyNum[1] = '\0';
  408. RegDeleteValueA(hkTimeKey, szKeyNum);
  409. RegDeleteValueA(hkDataKey, szKeyNum);
  410. Cleanup:
  411. if (hkDataKey)
  412. {
  413. RegCloseKey(hkDataKey);
  414. }
  415. if (hkTimeKey)
  416. {
  417. RegCloseKey(hkTimeKey);
  418. }
  419. return hr;
  420. }
  421. //===========================================================================
  422. //
  423. // deleteKeyEx
  424. //
  425. STDMETHODIMP CAdmin::deleteKeyEx(
  426. int version,
  427. VARIANT vSiteName
  428. )
  429. {
  430. HRESULT hr = S_OK, lResult;
  431. HKEY hkDataKey = NULL, hkTimeKey = NULL;
  432. HKEY hkPassport = NULL, hkSites = NULL;
  433. char szKeyNum[2];
  434. LPSTR szSiteName = NULL;
  435. USES_CONVERSION;
  436. if(vSiteName.vt == VT_ERROR && vSiteName.scode == DISP_E_PARAMNOTFOUND)
  437. {
  438. szSiteName = NULL;
  439. }
  440. else if(vSiteName.vt == VT_BSTR)
  441. {
  442. szSiteName = W2A(vSiteName.bstrVal);
  443. }
  444. else
  445. {
  446. hr = E_INVALIDARG;
  447. goto Cleanup;
  448. }
  449. if(szSiteName)
  450. {
  451. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  452. SITES_KEY,
  453. 0,
  454. KEY_ALL_ACCESS,
  455. &hkSites);
  456. if(lResult != ERROR_SUCCESS)
  457. {
  458. AtlReportError(CLSID_Admin,
  459. L"Couldn't find Sites key in registry. Reinstall Passport.",
  460. IID_IPassportAdminEx,
  461. PP_E_SITE_NOT_EXISTS);
  462. hr = PP_E_SITE_NOT_EXISTS;
  463. goto Cleanup;
  464. }
  465. lResult = RegOpenKeyExA(hkSites,
  466. szSiteName,
  467. 0,
  468. KEY_ALL_ACCESS,
  469. &hkPassport);
  470. if(lResult != ERROR_SUCCESS)
  471. {
  472. AtlReportError(CLSID_Admin,
  473. L"Couldn't find site key in registry. Reinstall Passport.",
  474. IID_IPassportAdminEx,
  475. PP_E_SITE_NOT_EXISTS);
  476. hr = PP_E_SITE_NOT_EXISTS;
  477. goto Cleanup;
  478. }
  479. }
  480. else
  481. {
  482. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  483. PASSPORT_KEY,
  484. 0,
  485. KEY_ALL_ACCESS,
  486. &hkPassport);
  487. if(lResult != ERROR_SUCCESS)
  488. {
  489. AtlReportError(CLSID_Admin,
  490. L"Couldn't find Passport key in registry. Reinstall Passport.",
  491. IID_IPassportAdminEx,
  492. E_FAIL);
  493. hr = E_FAIL;
  494. goto Cleanup;
  495. }
  496. }
  497. lResult = RegOpenKeyEx(hkPassport, KEYDATA_SUBKEY, 0, KEY_WRITE, &hkDataKey);
  498. if (lResult != ERROR_SUCCESS)
  499. {
  500. AtlReportError(CLSID_Admin,
  501. L"Couldn't find KeyData key in registry. Reinstall Passport.",
  502. IID_IPassportAdminEx,
  503. E_FAIL);
  504. hr = E_FAIL;
  505. goto Cleanup;
  506. }
  507. lResult = RegOpenKeyEx(hkPassport, KEYTIMES_SUBKEY, 0, KEY_WRITE, &hkTimeKey);
  508. if (lResult != ERROR_SUCCESS)
  509. {
  510. AtlReportError(CLSID_Admin,
  511. L"Couldn't find KeyTimes key in registry. Reinstall Passport.",
  512. IID_IPassportAdminEx,
  513. E_FAIL);
  514. hr = E_FAIL;
  515. goto Cleanup;
  516. }
  517. szKeyNum[0] = KeyVerI2C(version);
  518. szKeyNum[1] = '\0';
  519. RegDeleteValueA(hkTimeKey, szKeyNum);
  520. RegDeleteValueA(hkDataKey, szKeyNum);
  521. Cleanup:
  522. if (hkPassport)
  523. {
  524. RegCloseKey(hkPassport);
  525. }
  526. if (hkSites)
  527. {
  528. RegCloseKey(hkSites);
  529. }
  530. if (hkDataKey)
  531. {
  532. RegCloseKey(hkDataKey);
  533. }
  534. if (hkTimeKey)
  535. {
  536. RegCloseKey(hkTimeKey);
  537. }
  538. return hr;
  539. }
  540. //===========================================================================
  541. //
  542. // setKeyTime
  543. //
  544. STDMETHODIMP CAdmin::setKeyTime(int version, int fromNow)
  545. {
  546. HRESULT hr = S_OK, lResult;
  547. HKEY hkDataKey = NULL, hkTimeKey = NULL;
  548. char szKeyNum[2];
  549. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEYTIMES_KEY, 0, KEY_WRITE, &hkTimeKey);
  550. if(lResult != ERROR_SUCCESS)
  551. {
  552. AtlReportError(CLSID_Admin,
  553. L"Couldn't find KeyTimes key in registry. Reinstall Passport.",
  554. IID_IPassportAdmin,
  555. E_FAIL);
  556. hr = E_FAIL;
  557. goto Cleanup;
  558. }
  559. szKeyNum[0] = KeyVerI2C(version);
  560. szKeyNum[1] = '\0';
  561. if (fromNow == 0)
  562. {
  563. RegDeleteValueA(hkTimeKey, szKeyNum);
  564. }
  565. else
  566. {
  567. time_t now;
  568. time(&now);
  569. now += fromNow;
  570. DWORD dwT = now;
  571. lResult = RegSetValueExA(hkTimeKey, szKeyNum, 0, REG_DWORD, (LPBYTE) &dwT, sizeof(DWORD));
  572. if(lResult != ERROR_SUCCESS)
  573. {
  574. AtlReportError(CLSID_Admin,
  575. L"Couldn't write KeyTime key to registry.",
  576. IID_IPassportAdmin,
  577. E_FAIL);
  578. hr = E_FAIL;
  579. goto Cleanup;
  580. }
  581. }
  582. Cleanup:
  583. if (hkTimeKey)
  584. {
  585. RegCloseKey(hkTimeKey);
  586. }
  587. return hr;
  588. }
  589. //===========================================================================
  590. //
  591. // setKeyTimeEx
  592. //
  593. STDMETHODIMP CAdmin::setKeyTimeEx(
  594. int version,
  595. int fromNow,
  596. VARIANT vSiteName
  597. )
  598. {
  599. HRESULT hr = S_OK, lResult;
  600. LPSTR szSiteName = NULL;
  601. HKEY hkDataKey = NULL, hkTimeKey = NULL;
  602. HKEY hkSites = NULL, hkPassport = NULL;
  603. char szKeyNum[2];
  604. USES_CONVERSION;
  605. if(vSiteName.vt == VT_ERROR && vSiteName.scode == DISP_E_PARAMNOTFOUND)
  606. {
  607. szSiteName = NULL;
  608. }
  609. else if(vSiteName.vt == VT_BSTR)
  610. {
  611. szSiteName = W2A(vSiteName.bstrVal);
  612. }
  613. else
  614. {
  615. hr = E_INVALIDARG;
  616. goto Cleanup;
  617. }
  618. if(szSiteName)
  619. {
  620. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  621. SITES_KEY,
  622. 0,
  623. KEY_ALL_ACCESS,
  624. &hkSites);
  625. if(lResult != ERROR_SUCCESS)
  626. {
  627. AtlReportError(CLSID_Admin,
  628. L"Couldn't find Sites key in registry. Reinstall Passport.",
  629. IID_IPassportAdminEx,
  630. PP_E_SITE_NOT_EXISTS);
  631. hr = PP_E_SITE_NOT_EXISTS;
  632. goto Cleanup;
  633. }
  634. lResult = RegOpenKeyExA(hkSites,
  635. szSiteName,
  636. 0,
  637. KEY_ALL_ACCESS,
  638. &hkPassport);
  639. if(lResult != ERROR_SUCCESS)
  640. {
  641. AtlReportError(CLSID_Admin,
  642. L"Couldn't find site key in registry. Reinstall Passport.",
  643. IID_IPassportAdminEx,
  644. PP_E_SITE_NOT_EXISTS);
  645. hr = PP_E_SITE_NOT_EXISTS;
  646. goto Cleanup;
  647. }
  648. }
  649. else
  650. {
  651. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  652. PASSPORT_KEY,
  653. 0,
  654. KEY_ALL_ACCESS,
  655. &hkPassport);
  656. if(lResult != ERROR_SUCCESS)
  657. {
  658. AtlReportError(CLSID_Admin,
  659. L"Couldn't find Passport key in registry. Reinstall Passport.",
  660. IID_IPassportAdminEx,
  661. E_FAIL);
  662. hr = E_FAIL;
  663. goto Cleanup;
  664. }
  665. }
  666. lResult = RegOpenKeyEx(hkPassport, KEYTIMES_SUBKEY, 0, KEY_WRITE, &hkTimeKey);
  667. if(lResult != ERROR_SUCCESS)
  668. {
  669. AtlReportError(CLSID_Admin,
  670. L"Couldn't find KeyTimes key in registry. Reinstall Passport.",
  671. IID_IPassportAdminEx,
  672. E_FAIL);
  673. hr = E_FAIL;
  674. goto Cleanup;
  675. }
  676. szKeyNum[0] = KeyVerI2C(version);
  677. szKeyNum[1] = '\0';
  678. if (fromNow == 0)
  679. {
  680. RegDeleteValueA(hkTimeKey, szKeyNum);
  681. }
  682. else
  683. {
  684. time_t now;
  685. time(&now);
  686. now += fromNow;
  687. DWORD dwT = now;
  688. lResult = RegSetValueExA(hkTimeKey, szKeyNum, 0, REG_DWORD, (LPBYTE) &dwT, sizeof(DWORD));
  689. if (lResult != ERROR_SUCCESS)
  690. {
  691. AtlReportError(CLSID_Admin,
  692. L"Couldn't write KeyTime key to registry.",
  693. IID_IPassportAdminEx,
  694. E_FAIL);
  695. hr = E_FAIL;
  696. goto Cleanup;
  697. }
  698. }
  699. Cleanup:
  700. if (hkSites)
  701. {
  702. RegCloseKey(hkSites);
  703. }
  704. if (hkPassport)
  705. {
  706. RegCloseKey(hkPassport);
  707. }
  708. if (hkTimeKey)
  709. {
  710. RegCloseKey(hkTimeKey);
  711. }
  712. return hr;
  713. }
  714. //===========================================================================
  715. //
  716. // get_currentKeyVersion
  717. //
  718. STDMETHODIMP CAdmin::get_currentKeyVersion(int *pVal)
  719. {
  720. if (!g_config || !g_config->isValid()) // Guarantees config is non-null
  721. {
  722. *pVal = -1;
  723. return S_OK;
  724. }
  725. CRegistryConfig* crc = g_config->checkoutRegistryConfig();
  726. *pVal = crc->getCurrentCryptVersion();
  727. crc->Release();
  728. return S_OK;
  729. }
  730. //===========================================================================
  731. //
  732. // getCurrentKeyVersionEx
  733. //
  734. STDMETHODIMP CAdmin::getCurrentKeyVersionEx(
  735. VARIANT vSiteName,
  736. int *pVal
  737. )
  738. {
  739. HRESULT hr;
  740. LPSTR szSiteName;
  741. CRegistryConfig* crc = NULL;
  742. USES_CONVERSION;
  743. if(vSiteName.vt == VT_ERROR && vSiteName.scode == DISP_E_PARAMNOTFOUND)
  744. {
  745. szSiteName = NULL;
  746. }
  747. else if(vSiteName.vt == VT_BSTR)
  748. {
  749. szSiteName = W2A(vSiteName.bstrVal);
  750. }
  751. else
  752. {
  753. hr = E_INVALIDARG;
  754. goto Cleanup;
  755. }
  756. if (!g_config || !g_config->isValid()) // Guarantees config is non-null
  757. {
  758. *pVal = -1;
  759. hr = S_OK;
  760. goto Cleanup;
  761. }
  762. crc = g_config->checkoutRegistryConfigBySite(szSiteName);
  763. if(szSiteName && crc == NULL)
  764. {
  765. AtlReportError(CLSID_Admin,
  766. L"Couldn't find site key in registry. Reinstall Passport.",
  767. IID_IPassportAdminEx,
  768. PP_E_SITE_NOT_EXISTS);
  769. hr = PP_E_SITE_NOT_EXISTS;
  770. goto Cleanup;
  771. }
  772. if(crc == NULL)
  773. {
  774. hr = E_FAIL;
  775. goto Cleanup;
  776. }
  777. *pVal = crc->getCurrentCryptVersion();
  778. hr = S_OK;
  779. Cleanup:
  780. if(crc)
  781. {
  782. crc->Release();
  783. }
  784. return S_OK;
  785. }
  786. //===========================================================================
  787. //
  788. // putCurrentKeyVersionEx
  789. //
  790. STDMETHODIMP CAdmin::put_currentKeyVersion(int Val)
  791. {
  792. HRESULT hr = S_OK, lResult;
  793. HKEY hkKey = NULL;
  794. DWORD dwCK = Val;
  795. if (Val < KEY_VERSION_MIN || Val > KEY_VERSION_MAX)
  796. {
  797. hr = E_INVALIDARG;
  798. goto Cleanup;
  799. }
  800. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, PASSPORT_KEY, 0, KEY_WRITE, &hkKey);
  801. if(lResult != ERROR_SUCCESS)
  802. {
  803. AtlReportError(CLSID_Admin,
  804. L"Couldn't find Passport key in registry. Reinstall Passport.",
  805. IID_IPassportAdmin,
  806. E_FAIL);
  807. hr = E_FAIL;
  808. goto Cleanup;
  809. }
  810. lResult = RegSetValueExA(hkKey, "CurrentKey", 0, REG_DWORD, (LPBYTE) &dwCK, sizeof(DWORD));
  811. if(lResult != ERROR_SUCCESS)
  812. {
  813. AtlReportError(CLSID_Admin,
  814. L"Couldn't write CurrentKey value to registry.",
  815. IID_IPassportAdmin,
  816. E_FAIL);
  817. hr = E_FAIL;
  818. goto Cleanup;
  819. }
  820. Cleanup:
  821. if (hkKey)
  822. {
  823. RegCloseKey(hkKey);
  824. }
  825. return hr;
  826. }
  827. //===========================================================================
  828. //
  829. // putCurrentKeyVersionEx
  830. //
  831. STDMETHODIMP CAdmin::putCurrentKeyVersionEx(
  832. int Val,
  833. VARIANT vSiteName
  834. )
  835. {
  836. HRESULT hr = S_OK, lResult;
  837. LPSTR szSiteName;
  838. HKEY hkPassport = NULL, hkSites = NULL;
  839. DWORD dwCK = Val;
  840. USES_CONVERSION;
  841. if (Val < KEY_VERSION_MIN || Val > KEY_VERSION_MAX)
  842. {
  843. hr = E_INVALIDARG;
  844. goto Cleanup;
  845. }
  846. if(vSiteName.vt == VT_ERROR && vSiteName.scode == DISP_E_PARAMNOTFOUND)
  847. szSiteName = NULL;
  848. else if(vSiteName.vt == VT_BSTR)
  849. szSiteName = W2A(vSiteName.bstrVal);
  850. else
  851. {
  852. hr = E_INVALIDARG;
  853. goto Cleanup;
  854. }
  855. if(szSiteName)
  856. {
  857. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  858. SITES_KEY,
  859. 0,
  860. KEY_ALL_ACCESS,
  861. &hkSites);
  862. if(lResult != ERROR_SUCCESS)
  863. {
  864. AtlReportError(CLSID_Admin,
  865. L"Couldn't find Sites key in registry. Reinstall Passport.", IID_IPassportAdminEx,
  866. PP_E_SITE_NOT_EXISTS);
  867. hr = PP_E_SITE_NOT_EXISTS;
  868. goto Cleanup;
  869. }
  870. lResult = RegOpenKeyExA(hkSites,
  871. szSiteName,
  872. 0,
  873. KEY_ALL_ACCESS,
  874. &hkPassport);
  875. if(lResult != ERROR_SUCCESS)
  876. {
  877. AtlReportError(CLSID_Admin,
  878. L"Couldn't find site key in registry. Reinstall Passport.", IID_IPassportAdminEx,
  879. PP_E_SITE_NOT_EXISTS);
  880. hr = PP_E_SITE_NOT_EXISTS;
  881. goto Cleanup;
  882. }
  883. }
  884. else
  885. {
  886. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  887. PASSPORT_KEY,
  888. 0,
  889. KEY_ALL_ACCESS,
  890. &hkPassport);
  891. if(lResult != ERROR_SUCCESS)
  892. {
  893. AtlReportError(CLSID_Admin,
  894. L"Couldn't find Passport key in registry. Reinstall Passport.", IID_IPassportAdminEx, E_FAIL);
  895. hr = E_FAIL;
  896. goto Cleanup;
  897. }
  898. }
  899. lResult = RegSetValueExA(hkPassport, "CurrentKey", 0,
  900. REG_DWORD, (LPBYTE) &dwCK, sizeof(DWORD));
  901. if(lResult != ERROR_SUCCESS)
  902. {
  903. AtlReportError(CLSID_Admin,
  904. L"Couldn't write CurrentKey value to registry.", IID_IPassportAdminEx, E_FAIL);
  905. hr = E_FAIL;
  906. goto Cleanup;
  907. }
  908. Cleanup:
  909. if (hkPassport)
  910. RegCloseKey(hkPassport);
  911. if (hkSites)
  912. RegCloseKey(hkSites);
  913. return hr;
  914. }
  915. //===========================================================================
  916. //
  917. // Refresh
  918. //
  919. STDMETHODIMP CAdmin::Refresh(
  920. VARIANT_BOOL bWait,
  921. VARIANT_BOOL* pbSuccess
  922. )
  923. {
  924. HRESULT hr;
  925. if(pbSuccess == NULL)
  926. {
  927. hr = E_INVALIDARG;
  928. goto Cleanup;
  929. }
  930. *pbSuccess = (g_config->UpdateNow(TRUE) ? VARIANT_TRUE : VARIANT_FALSE);
  931. hr = S_OK;
  932. Cleanup:
  933. return hr;
  934. }
  935. //===========================================================================
  936. //
  937. // setNexusPassword
  938. //
  939. STDMETHODIMP CAdmin::setNexusPassword(
  940. BSTR bstrPwd
  941. )
  942. {
  943. HRESULT hr;
  944. BYTE original[CKeyCrypto::RAWKEY_SIZE];
  945. DATA_BLOB iBlob;
  946. DATA_BLOB oBlob = {0};
  947. CKeyCrypto kc;
  948. long lResult;
  949. HKEY hkNexus = NULL;
  950. USES_CONVERSION;
  951. ZeroMemory(original, sizeof(original));
  952. strncpy((char*) original, W2A(bstrPwd), sizeof(original));
  953. original[sizeof(original) - 1] = '\0';
  954. iBlob.cbData = sizeof(original);
  955. iBlob.pbData = &(original[0]);
  956. hr = kc.encryptKey(&iBlob, &oBlob);
  957. if(hr != S_OK)
  958. {
  959. goto Cleanup;
  960. }
  961. //
  962. // Now we have an encrypted key, put it in the registry.
  963. //
  964. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  965. NEXUS_KEY,
  966. 0,
  967. KEY_SET_VALUE,
  968. &hkNexus);
  969. if(lResult != ERROR_SUCCESS)
  970. {
  971. hr = E_FAIL;
  972. goto Cleanup;
  973. }
  974. lResult = RegSetValueEx(hkNexus,
  975. TEXT("CCDPassword"),
  976. 0,
  977. REG_BINARY,
  978. oBlob.pbData,
  979. oBlob.cbData
  980. );
  981. if(lResult != ERROR_SUCCESS)
  982. {
  983. hr = E_FAIL;
  984. goto Cleanup;
  985. }
  986. hr = S_OK;
  987. Cleanup:
  988. if(hkNexus)
  989. RegCloseKey(hkNexus);
  990. if (oBlob.pbData)
  991. ::LocalFree(oBlob.pbData);
  992. return hr;
  993. }
  994. //===========================================================================
  995. //
  996. // Helper routine to create/set the CCDPassword registry value
  997. //
  998. HRESULT SetCCDPassword(VOID)
  999. {
  1000. HRESULT hr;
  1001. LPSTR szString = "La3b$7Q@93P*JX";
  1002. BYTE szResult[MAX_CCDPASSWORD_LEN];
  1003. BYTE szInput[CKeyCrypto::RAWKEY_SIZE] = {0};
  1004. DATA_BLOB iBlob, oBlob = {0};
  1005. CKeyCrypto kc;
  1006. LONG lResult;
  1007. HKEY hKey;
  1008. strncpy((char *) szInput, szString, sizeof(szInput));
  1009. szInput[sizeof(szInput) - 1] = '\0';
  1010. iBlob.cbData = sizeof(szInput);
  1011. iBlob.pbData = (LPBYTE) szInput;
  1012. hr = kc.encryptKey(&iBlob, &oBlob);
  1013. //
  1014. // Setup restricts size of string to be less than 256
  1015. //
  1016. if (hr == S_OK)
  1017. {
  1018. if (oBlob.cbData >= MAX_CCDPASSWORD_LEN)
  1019. {
  1020. hr = E_INVALIDARG;
  1021. }
  1022. else
  1023. {
  1024. memcpy(szResult, oBlob.pbData, oBlob.cbData);
  1025. }
  1026. }
  1027. if (oBlob.pbData)
  1028. {
  1029. LocalFree(oBlob.pbData);
  1030. }
  1031. if (hr != S_OK)
  1032. {
  1033. return hr;
  1034. }
  1035. //
  1036. // Password's encrypted, now set it in the registry
  1037. //
  1038. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  1039. NEXUS_KEY,
  1040. 0,
  1041. KEY_SET_VALUE,
  1042. &hKey);
  1043. if (lResult != ERROR_SUCCESS)
  1044. {
  1045. hr = E_FAIL;
  1046. return hr;
  1047. }
  1048. lResult = RegSetValueExA(hKey,
  1049. "CCDPassword",
  1050. 0,
  1051. REG_BINARY,
  1052. szResult,
  1053. oBlob.cbData);
  1054. if(lResult != ERROR_SUCCESS)
  1055. {
  1056. hr = E_FAIL;
  1057. }
  1058. RegCloseKey(hKey);
  1059. return hr;
  1060. }