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.

953 lines
29 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1998.
  5. //
  6. // File: Sigs.cpp
  7. //
  8. // Contents: Digital Signatures property page
  9. //
  10. // Classes: CSignatures
  11. //
  12. // History: 07-10-2000 stevebl Created
  13. //
  14. //---------------------------------------------------------------------------
  15. #include "precomp.hxx"
  16. #ifdef DIGITAL_SIGNATURES
  17. #include "wincrypt.h"
  18. #include "cryptui.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CSignatures property page
  26. IMPLEMENT_DYNCREATE(CSignatures, CPropertyPage)
  27. CSignatures::CSignatures() : CPropertyPage(CSignatures::IDD)
  28. {
  29. //{{AFX_DATA_INIT(CSignatures)
  30. // NOTE: the ClassWizard will add member initialization here
  31. //}}AFX_DATA_INIT
  32. m_fAllow = FALSE;
  33. m_fIgnoreForAdmins = FALSE;
  34. m_pIClassAdmin = NULL;
  35. m_nSortedColumn = 0;
  36. }
  37. CSignatures::~CSignatures()
  38. {
  39. *m_ppThis = NULL;
  40. if (m_pIClassAdmin)
  41. {
  42. m_pIClassAdmin->Release();
  43. }
  44. // delete temporary stores
  45. m_list1.DeleteAllItems();
  46. m_list2.DeleteAllItems();
  47. DeleteFile(m_szTempInstallableStore);
  48. DeleteFile(m_szTempNonInstallableStore);
  49. }
  50. void CSignatures::DoDataExchange(CDataExchange* pDX)
  51. {
  52. CPropertyPage::DoDataExchange(pDX);
  53. //{{AFX_DATA_MAP(CSignatures)
  54. DDX_Check(pDX, IDC_CHECK1, m_fAllow);
  55. DDX_Check(pDX, IDC_CHECK2, m_fIgnoreForAdmins);
  56. DDX_Control(pDX, IDC_LIST1, m_list1);
  57. DDX_Control(pDX, IDC_LIST2, m_list2);
  58. //}}AFX_DATA_MAP
  59. }
  60. BEGIN_MESSAGE_MAP(CSignatures, CPropertyPage)
  61. //{{AFX_MSG_MAP(CSignatures)
  62. ON_BN_CLICKED(IDC_BUTTON1, OnAddAllow)
  63. ON_BN_CLICKED(IDC_BUTTON2, OnDeleteAllow)
  64. ON_BN_CLICKED(IDC_BUTTON3, OnPropertiesAllow)
  65. ON_BN_CLICKED(IDC_BUTTON4, OnAddDisallow)
  66. ON_BN_CLICKED(IDC_BUTTON5, OnDeleteDisallow)
  67. ON_BN_CLICKED(IDC_BUTTON6, OnPropertiesDisallow)
  68. ON_BN_CLICKED(IDC_CHECK1, OnAllowChanged)
  69. ON_BN_CLICKED(IDC_CHECK2, OnIgnoreChanged)
  70. ON_WM_CONTEXTMENU()
  71. //}}AFX_MSG_MAP
  72. END_MESSAGE_MAP()
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CSignatures message handlers
  75. void CSignatures::RemoveCertificate(CString &szStore, CListCtrl &List)
  76. {
  77. int nItem = -1;
  78. for (;;)
  79. {
  80. nItem = List.GetNextItem(nItem, LVNI_SELECTED);
  81. if (-1 == nItem)
  82. {
  83. break;
  84. }
  85. //
  86. // Open the certificate store
  87. //
  88. PCCERT_CONTEXT pcLocalCert = NULL;
  89. PCCERT_CONTEXT pcItemCert = (PCCERT_CONTEXT) List.GetItemData(nItem);
  90. HCERTSTORE hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  91. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  92. NULL,
  93. CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  94. szStore);
  95. if (hCertStore)
  96. {
  97. //
  98. // Enumerate the cert store looking for the match
  99. //
  100. int i = 0;
  101. for (;;) {
  102. pcLocalCert = CertEnumCertificatesInStore(hCertStore, pcLocalCert);
  103. if (!pcLocalCert) {
  104. if (GetLastError() != CRYPT_E_NOT_FOUND )
  105. {
  106. DebugMsg((DM_WARNING, TEXT("CSignatures::RemoveCertificate: Failed to find certificate to delete.")));
  107. }
  108. break;
  109. }
  110. if (CertCompareCertificate(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  111. pcLocalCert->pCertInfo ,
  112. pcItemCert->pCertInfo))
  113. {
  114. CertDeleteCertificateFromStore(pcLocalCert);
  115. break;
  116. }
  117. //pcLocalCert should get deleted when it is repassed into CertEnumCerti..
  118. }
  119. CertCloseStore(hCertStore, 0);
  120. }
  121. }
  122. RefreshData();
  123. SetModified();
  124. }
  125. void CSignatures::CertificateProperties(CString &szStore, CListCtrl &List)
  126. {
  127. int nItem = -1;
  128. for (;;)
  129. {
  130. nItem = List.GetNextItem(nItem, LVNI_SELECTED);
  131. if (-1 == nItem)
  132. {
  133. break;
  134. }
  135. PCCERT_CONTEXT pcc = (PCCERT_CONTEXT) List.GetItemData(nItem);
  136. // display the property sheet for this item
  137. CRYPTUI_VIEWCERTIFICATE_STRUCT cvs;
  138. memset(&cvs, 0, sizeof(cvs));
  139. cvs.dwSize = sizeof(cvs);
  140. cvs.hwndParent = m_hWnd;
  141. cvs.pCertContext = pcc;
  142. cvs.dwFlags = CRYPTUI_DISABLE_EDITPROPERTIES |
  143. CRYPTUI_DISABLE_ADDTOSTORE;
  144. BOOL fChanged = FALSE;
  145. CryptUIDlgViewCertificate(&cvs, &fChanged);
  146. }
  147. }
  148. //+--------------------------------------------------------------------------
  149. //
  150. // Member: CSignatures::ReportFailure
  151. //
  152. // Synopsis: General failure reporting mechanism.
  153. //
  154. // Arguments: [dwMessage] - resource ID of the root message string
  155. // [hr] - HRESULT encountered
  156. //
  157. // Returns:
  158. //
  159. // Modifies:
  160. //
  161. // Derivation:
  162. //
  163. // History: 07-26-2000 stevebl Created
  164. //
  165. // Notes: Builds an error message with a line of text determined by
  166. // dwMessage, and followed by text returned by Format Message
  167. // string.
  168. // The error message is then displayed in a message box.
  169. //
  170. //---------------------------------------------------------------------------
  171. void CSignatures::ReportFailure(DWORD dwMessage, HRESULT hr)
  172. {
  173. CString szMessage;
  174. szMessage.LoadString(dwMessage);
  175. szMessage += TEXT("\n");
  176. TCHAR szBuffer[256];
  177. DWORD dw = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  178. NULL,
  179. hr,
  180. 0,
  181. szBuffer,
  182. sizeof(szBuffer) / sizeof(szBuffer[0]),
  183. NULL);
  184. if (0 == dw)
  185. {
  186. // FormatMessage failed.
  187. // We'll have to come up with some sort of reasonable message.
  188. wsprintf(szBuffer, TEXT("(HRESULT: 0x%lX)"), hr);
  189. }
  190. szMessage += szBuffer;
  191. MessageBox(szMessage,
  192. NULL,
  193. MB_OK | MB_ICONEXCLAMATION);
  194. }
  195. //+--------------------------------------------------------------------------
  196. //
  197. // Function: AddMSIToCertStore
  198. //
  199. // Synopsis: Gets a certificate from an MSI file and adds it to the
  200. // certificate store.
  201. //
  202. // Arguments: [lpFileName] - path to the MSI file
  203. // [lpFileStore] - path to the certificate store
  204. //
  205. // Returns:
  206. //
  207. // Modifies:
  208. //
  209. // History: 07-26-2000 stevebl Created
  210. //
  211. // Notes:
  212. //
  213. //---------------------------------------------------------------------------
  214. HRESULT CSignatures::AddMSIToCertStore(LPWSTR lpFileName, LPWSTR lpFileStore)
  215. {
  216. PCCERT_CONTEXT pcc = NULL;
  217. HCERTSTORE hCertStore = NULL;
  218. BOOL bRet;
  219. HRESULT hrRet = MsiGetFileSignatureInformation(lpFileName,
  220. 0,
  221. &pcc,
  222. NULL,
  223. NULL);
  224. if (SUCCEEDED(hrRet))
  225. {
  226. //
  227. // Open the certificate store
  228. //
  229. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  230. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  231. NULL,
  232. CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  233. lpFileStore);
  234. if (hCertStore == NULL) {
  235. DebugMsg((DM_WARNING, L"AddMSIToCertStore: CertOpenStore failed with %u",GetLastError()));
  236. hrRet = HRESULT_FROM_WIN32(GetLastError());
  237. goto Exit;
  238. }
  239. //
  240. // add the given certificate to the store
  241. //
  242. bRet = CertAddCertificateContextToStore(hCertStore,
  243. pcc,
  244. CERT_STORE_ADD_NEW,
  245. NULL);
  246. if (!bRet) {
  247. DebugMsg((DM_WARNING, L"AddToCertStore: CertAddCertificateContextToStore failed with %u", GetLastError()));
  248. hrRet = HRESULT_FROM_WIN32(GetLastError());
  249. goto Exit;
  250. }
  251. //
  252. // Save the store
  253. //
  254. bRet = CertCloseStore(hCertStore, 0);
  255. hCertStore = NULL; // Make the store handle NULL, Nothing more we can do
  256. if (!bRet) {
  257. DebugMsg((DM_WARNING, L"AddToCertStore: CertCloseStore failed with %u", GetLastError()));
  258. hrRet = HRESULT_FROM_WIN32(GetLastError());
  259. }
  260. hrRet = S_OK;
  261. Exit:
  262. if (hCertStore) {
  263. //
  264. // No need to get the error code
  265. //
  266. CertCloseStore(hCertStore, 0);
  267. }
  268. CertFreeCertificateContext(pcc);
  269. if (FAILED(hrRet))
  270. {
  271. ReportFailure(IDS_ADDCERTFAILED, hrRet);
  272. }
  273. }
  274. else
  275. {
  276. ReportFailure(IDS_CERTFROMMSIFAILED, hrRet);
  277. DebugMsg((DM_WARNING, L"AddMSIToCertStore: MsiGetFileSignatureInformation failed with 0x%x", hrRet));
  278. }
  279. return hrRet;
  280. }
  281. //+-------------------------------------------------------------------------
  282. // AddToCertStore
  283. //
  284. // Purpose:
  285. // Adds the certificate from the given filename to the certificate store
  286. // and saves it to the given location
  287. //
  288. //
  289. // Parameters
  290. // lpFIleName - Location of the certificate file
  291. // lpFileStore - Location where the resultant cetrtficate path should
  292. // be stored
  293. //
  294. //
  295. // Return Value:
  296. // S_OK if successful or the corresponding error code
  297. //
  298. // Comments: Shamefully stolen from Shaji's code.
  299. //+-------------------------------------------------------------------------
  300. HRESULT CSignatures::AddToCertStore(LPWSTR lpFileName, LPWSTR lpFileStore)
  301. {
  302. CRYPTUI_WIZ_IMPORT_SRC_INFO cui_src;
  303. HCERTSTORE hCertStore = NULL;
  304. BOOL bRet = FALSE;
  305. HRESULT hrRet = S_OK;
  306. //
  307. // Need to make the store usable and saveable from
  308. // multiple admin consoles..
  309. //
  310. // For that the file has to be saved and kept on a temp file
  311. // and then modified..
  312. //
  313. if (!lpFileName || !lpFileName[0] || !lpFileStore || !lpFileStore[0]) {
  314. return E_INVALIDARG;
  315. }
  316. //
  317. // Open the certificate store
  318. //
  319. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  320. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  321. NULL,
  322. CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  323. lpFileStore);
  324. if (hCertStore == NULL) {
  325. DebugMsg((DM_WARNING, L"AddToCertStore: CertOpenStore failed with %u",GetLastError()));
  326. hrRet = HRESULT_FROM_WIN32(GetLastError());
  327. goto Exit;
  328. }
  329. //
  330. // add the given certificate to the store
  331. //
  332. cui_src.dwFlags = 0;
  333. cui_src.dwSize = sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  334. cui_src.dwSubjectChoice = CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  335. cui_src.pwszFileName = lpFileName;
  336. cui_src.pwszPassword = NULL;
  337. bRet = CryptUIWizImport(CRYPTUI_WIZ_NO_UI, NULL, NULL, &cui_src, hCertStore);
  338. if (!bRet) {
  339. DebugMsg((DM_WARNING, L"AddToCertStore: CryptUIWizImport failed with %u", GetLastError()));
  340. hrRet = HRESULT_FROM_WIN32(GetLastError());
  341. goto Exit;
  342. }
  343. //
  344. // Save the store
  345. //
  346. bRet = CertCloseStore(hCertStore, 0);
  347. hCertStore = NULL; // Make the store handle NULL, Nothing more we can do
  348. if (!bRet) {
  349. DebugMsg((DM_WARNING, L"AddToCertStore: CertCloseStore failed with %u", GetLastError()));
  350. hrRet = HRESULT_FROM_WIN32(GetLastError());
  351. }
  352. hrRet = S_OK;
  353. Exit:
  354. if (hCertStore) {
  355. //
  356. // No need to get the error code
  357. //
  358. CertCloseStore(hCertStore, 0);
  359. }
  360. if (FAILED(hrRet))
  361. {
  362. ReportFailure(IDS_ADDCERTFAILED, hrRet);
  363. }
  364. return hrRet;
  365. }
  366. void CSignatures::AddCertificate(CString &szStore)
  367. {
  368. CString szExtension;
  369. CString szFilter;
  370. szExtension.LoadString(IDS_CERT_DEF_EXT);
  371. szFilter.LoadString(IDS_CERT_EXT_FILT);
  372. OPENFILENAME ofn;
  373. memset(&ofn, 0, sizeof(ofn));
  374. ofn.lStructSize = sizeof(ofn);
  375. ofn.hwndOwner = m_hWnd;
  376. ofn.hInstance = ghInstance;
  377. TCHAR lpstrFilter[MAX_PATH];
  378. wcsncpy(lpstrFilter, szFilter, MAX_PATH);
  379. ofn.lpstrFilter = lpstrFilter;
  380. TCHAR szFileTitle[MAX_PATH];
  381. TCHAR szFile[MAX_PATH];
  382. szFile[0] = NULL;
  383. ofn.lpstrFile = szFile;
  384. ofn.nMaxFile = MAX_PATH;
  385. ofn.lpstrFileTitle = szFileTitle;
  386. ofn.nMaxFileTitle = MAX_PATH;
  387. ofn.lpstrInitialDir = m_pScopePane->m_ToolDefaults.szStartPath;
  388. ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_EXPLORER;
  389. ofn.lpstrDefExt = szExtension;
  390. int iBreak = 0;
  391. while (lpstrFilter[iBreak])
  392. {
  393. if (lpstrFilter[iBreak] == TEXT('|'))
  394. {
  395. lpstrFilter[iBreak] = 0;
  396. }
  397. iBreak++;
  398. }
  399. if (GetOpenFileName(&ofn))
  400. {
  401. CHourglass hourglass;
  402. CString szPackagePath;
  403. HRESULT hr = E_FAIL;
  404. if ((ofn.nFileExtension > 0) &&
  405. (0 == _wcsicmp(ofn.lpstrFile + ofn.nFileExtension, L"msi")))
  406. {
  407. // this is an MSI package
  408. HRESULT hr = AddMSIToCertStore(ofn.lpstrFile, (LPWSTR)((LPCWSTR)szStore));
  409. if (SUCCEEDED(hr))
  410. {
  411. RefreshData();
  412. SetModified();
  413. }
  414. }
  415. else
  416. {
  417. // this is a simple certificate
  418. HRESULT hr = AddToCertStore(ofn.lpstrFile, (LPWSTR)((LPCWSTR)szStore));
  419. if (SUCCEEDED(hr))
  420. {
  421. RefreshData();
  422. SetModified();
  423. }
  424. }
  425. }
  426. }
  427. void CSignatures::OnAddAllow()
  428. {
  429. AddCertificate(m_szTempInstallableStore);
  430. }
  431. void CSignatures::OnDeleteAllow()
  432. {
  433. RemoveCertificate(m_szTempInstallableStore, m_list1);
  434. }
  435. void CSignatures::OnPropertiesAllow()
  436. {
  437. CertificateProperties(m_szTempInstallableStore, m_list1);
  438. }
  439. void CSignatures::OnAddDisallow()
  440. {
  441. AddCertificate(m_szTempNonInstallableStore);
  442. }
  443. void CSignatures::OnDeleteDisallow()
  444. {
  445. RemoveCertificate(m_szTempNonInstallableStore, m_list2);
  446. }
  447. void CSignatures::OnPropertiesDisallow()
  448. {
  449. CertificateProperties(m_szTempNonInstallableStore, m_list2);
  450. }
  451. void CSignatures::OnAllowChanged()
  452. {
  453. BOOL fAllow = IsDlgButtonChecked(IDC_CHECK1);
  454. if (m_fAllow != fAllow)
  455. {
  456. SetModified();
  457. }
  458. m_fAllow = fAllow;
  459. GetDlgItem(IDC_BUTTON1)->EnableWindow(m_fAllow);
  460. GetDlgItem(IDC_BUTTON2)->EnableWindow(m_fAllow);
  461. GetDlgItem(IDC_BUTTON3)->EnableWindow(m_fAllow);
  462. GetDlgItem(IDC_LIST1)->EnableWindow(m_fAllow);
  463. }
  464. void CSignatures::OnIgnoreChanged()
  465. {
  466. BOOL fIgnoreForAdmins = IsDlgButtonChecked(IDC_CHECK2);
  467. if (m_fIgnoreForAdmins != fIgnoreForAdmins)
  468. {
  469. SetModified();
  470. }
  471. m_fIgnoreForAdmins = fIgnoreForAdmins;
  472. }
  473. BOOL CSignatures::OnInitDialog()
  474. {
  475. // create temporary store files
  476. BOOL fFilesCreated = FALSE;
  477. TCHAR szTempPath[MAX_PATH];
  478. if (GetTempPath(MAX_PATH, szTempPath))
  479. {
  480. TCHAR szTempFile[MAX_PATH];
  481. if (GetTempFileName(szTempPath,
  482. NULL,
  483. 0,
  484. szTempFile))
  485. {
  486. m_szTempInstallableStore = szTempFile;
  487. if (GetTempFileName(szTempPath,
  488. NULL,
  489. 0,
  490. szTempFile))
  491. {
  492. m_szTempNonInstallableStore = szTempFile;
  493. fFilesCreated = TRUE;
  494. }
  495. }
  496. }
  497. if (fFilesCreated)
  498. {
  499. CString szPath = m_pScopePane->m_szGPT_Path;
  500. szPath += TEXT("\\msi_installable_certs");
  501. CopyFile(szPath, m_szTempInstallableStore, FALSE);
  502. szPath = m_pScopePane->m_szGPT_Path;
  503. szPath += TEXT("\\msi_non_installable_certs");
  504. CopyFile(szPath, m_szTempNonInstallableStore, FALSE);
  505. }
  506. else
  507. {
  508. DebugMsg((DM_WARNING, TEXT("CSignatures::OnInitDialog : Failed to create temporary certificate stores.")));
  509. }
  510. CPropertyPage::OnInitDialog();
  511. // add columns to the lists
  512. RECT rect;
  513. m_list1.GetClientRect(&rect);
  514. CString szTemp;
  515. szTemp.LoadString(IDS_SIGS_COL1);
  516. m_list1.InsertColumn(0, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  517. m_list2.InsertColumn(0, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  518. szTemp.LoadString(IDS_SIGS_COL2);
  519. m_list1.InsertColumn(1, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  520. m_list2.InsertColumn(1, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  521. szTemp.LoadString(IDS_SIGS_COL3);
  522. m_list1.InsertColumn(2, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.25);
  523. m_list2.InsertColumn(2, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.25);
  524. // add image lists
  525. CImageList * pil = NULL;
  526. pil = new CImageList;
  527. if (pil)
  528. {
  529. pil->Create(IDB_CERTIFICATE, 16, 0, RGB(255, 0, 255));
  530. m_list1.SetImageList(pil, LVSIL_SMALL);
  531. }
  532. pil = new CImageList;
  533. if (pil)
  534. {
  535. pil->Create(IDB_CERTIFICATE, 16, 0, RGB(255, 0, 255));
  536. m_list2.SetImageList(pil, LVSIL_SMALL);
  537. }
  538. // retrieve initial registry key setting
  539. HKEY hKey;
  540. HRESULT hr = m_pIGPEInformation->GetRegistryKey(m_pScopePane->m_fMachine ?
  541. GPO_SECTION_MACHINE :
  542. GPO_SECTION_USER, &hKey);
  543. if (SUCCEEDED(hr))
  544. {
  545. HKEY hSubKey;
  546. if(ERROR_SUCCESS == RegOpenKeyEx(hKey,
  547. TEXT("Microsoft\\Windows\\Installer"),
  548. 0,
  549. KEY_ALL_ACCESS,
  550. &hSubKey))
  551. {
  552. DWORD dw;
  553. DWORD dwSize = sizeof(DWORD);
  554. if (ERROR_SUCCESS == RegQueryValueEx(hSubKey,
  555. TEXT("InstallKnownPackagesOnly"),
  556. NULL,
  557. NULL,
  558. (BYTE *)&dw,
  559. &dwSize))
  560. {
  561. m_fAllow = (dw == 1) ? TRUE : FALSE;
  562. CheckDlgButton(IDC_CHECK1, m_fAllow);
  563. }
  564. dwSize = sizeof(DWORD);
  565. if (ERROR_SUCCESS == RegQueryValueEx(hSubKey,
  566. TEXT("IgnoreSignaturePolicyForAdmins"),
  567. NULL,
  568. NULL,
  569. (BYTE *)&dw,
  570. &dwSize))
  571. {
  572. m_fIgnoreForAdmins = (dw == 1) ? TRUE : FALSE;
  573. CheckDlgButton(IDC_CHECK2, m_fIgnoreForAdmins);
  574. }
  575. RegCloseKey(hSubKey);
  576. }
  577. RegCloseKey(hKey);
  578. }
  579. RefreshData();
  580. return TRUE; // return TRUE unless you set the focus to a control
  581. // EXCEPTION: OCX Property Pages should return FALSE
  582. }
  583. BOOL CSignatures::OnApply()
  584. {
  585. HRESULT hr = E_NOTIMPL;
  586. HKEY hKey;
  587. hr = m_pIGPEInformation->GetRegistryKey(m_pScopePane->m_fMachine ?
  588. GPO_SECTION_MACHINE :
  589. GPO_SECTION_USER, &hKey);
  590. if (SUCCEEDED(hr))
  591. {
  592. hr = E_FAIL;
  593. HKEY hSubKey;
  594. if(ERROR_SUCCESS == RegCreateKeyEx(hKey,
  595. TEXT("Microsoft\\Windows\\Installer"),
  596. 0,
  597. NULL,
  598. REG_OPTION_NON_VOLATILE,
  599. KEY_ALL_ACCESS,
  600. NULL,
  601. &hSubKey,
  602. NULL))
  603. {
  604. DWORD dw = m_fAllow ? 1 : 0;
  605. DWORD dwSize = sizeof(DWORD);
  606. if (ERROR_SUCCESS == RegSetValueEx(hSubKey,
  607. TEXT("InstallKnownPackagesOnly"),
  608. 0,
  609. REG_DWORD,
  610. (BYTE *)&dw,
  611. dwSize))
  612. {
  613. hr = S_OK;
  614. }
  615. dw = m_fIgnoreForAdmins ? 1 : 0;
  616. dwSize = sizeof(DWORD);
  617. if (ERROR_SUCCESS == RegSetValueEx(hSubKey,
  618. TEXT("IgnoreSignaturePolicyForAdmins"),
  619. 0,
  620. REG_DWORD,
  621. (BYTE *)&dw,
  622. dwSize))
  623. {
  624. hr = S_OK;
  625. }
  626. RegCloseKey(hSubKey);
  627. }
  628. RegCloseKey(hKey);
  629. }
  630. // copy back the certificate stores
  631. if (SUCCEEDED(hr))
  632. {
  633. m_list1.DeleteAllItems();
  634. m_list2.DeleteAllItems();
  635. CString szPath = m_pScopePane->m_szGPT_Path;
  636. szPath += TEXT("\\msi_installable_certs");
  637. CopyFile(m_szTempInstallableStore, szPath, FALSE);
  638. szPath = m_pScopePane->m_szGPT_Path;
  639. szPath += TEXT("\\msi_non_installable_certs");
  640. CopyFile(m_szTempNonInstallableStore, szPath, FALSE);
  641. RefreshData();
  642. }
  643. if (FAILED(hr))
  644. {
  645. CString sz;
  646. sz.LoadString(IDS_CHANGEFAILED);
  647. ReportGeneralPropertySheetError(m_hWnd, sz, hr);
  648. return FALSE;
  649. }
  650. else
  651. {
  652. GUID guid = REGISTRY_EXTENSION_GUID;
  653. if (FAILED(m_pIGPEInformation->PolicyChanged(m_pScopePane->m_fMachine,
  654. TRUE,
  655. &guid,
  656. m_pScopePane->m_fMachine ? &guidMachSnapin
  657. : &guidUserSnapin)))
  658. {
  659. ReportPolicyChangedError(m_hWnd);
  660. }
  661. // need to call PolicyChanged for Shaji's extension too.
  662. // REMOVE THIS LINE WHEN SHAJI CHECKS IN HIS GUID
  663. #define GUID_MSICERT_CSE { 0x000c10f4, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }
  664. GUID guid2 = GUID_MSICERT_CSE;
  665. if (FAILED(m_pIGPEInformation->PolicyChanged(m_pScopePane->m_fMachine,
  666. TRUE,
  667. &guid2,
  668. m_pScopePane->m_fMachine ? &guidMachSnapin
  669. : &guidUserSnapin)))
  670. {
  671. ReportPolicyChangedError(m_hWnd);
  672. }
  673. }
  674. return CPropertyPage::OnApply();
  675. }
  676. LRESULT CSignatures::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  677. {
  678. switch (message)
  679. {
  680. case WM_HELP:
  681. StandardHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, IDD);
  682. return 0;
  683. case WM_USER_REFRESH:
  684. RefreshData();
  685. return 0;
  686. case WM_USER_CLOSE:
  687. return GetOwner()->SendMessage(WM_CLOSE);
  688. case WM_NOTIFY:
  689. {
  690. LPNMLISTVIEW pnmh = (LPNMLISTVIEW) lParam;
  691. if (pnmh->hdr.code == LVN_DELETEITEM)
  692. {
  693. switch(wParam)
  694. {
  695. case IDC_LIST1:
  696. CertFreeCertificateContext((PCCERT_CONTEXT)m_list1.GetItemData(pnmh->iItem));
  697. break;
  698. case IDC_LIST2:
  699. CertFreeCertificateContext((PCCERT_CONTEXT)m_list2.GetItemData(pnmh->iItem));
  700. break;
  701. }
  702. }
  703. }
  704. return CPropertyPage::WindowProc(message, wParam, lParam);
  705. default:
  706. return CPropertyPage::WindowProc(message, wParam, lParam);
  707. }
  708. }
  709. void CSignatures::RefreshData(void)
  710. {
  711. // populate the listview controls
  712. m_list1.DeleteAllItems();
  713. m_list2.DeleteAllItems();
  714. HCERTSTORE hCertStore = NULL;;
  715. PCCERT_CONTEXT pcLocalCert = NULL;
  716. //
  717. // open the local cert store
  718. //
  719. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  720. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  721. NULL,
  722. // CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  723. CERT_STORE_READONLY_FLAG,
  724. m_szTempInstallableStore);
  725. if (hCertStore)
  726. {
  727. //
  728. // Enumerate the cert store
  729. //
  730. int i = 0;
  731. for (;;) {
  732. pcLocalCert = CertEnumCertificatesInStore(hCertStore, pcLocalCert);
  733. if (!pcLocalCert) {
  734. if (GetLastError() != CRYPT_E_NOT_FOUND )
  735. {
  736. DebugMsg((DM_WARNING, TEXT("CSignatures::RefreshData : Failed to enumerate certificate store.")));
  737. }
  738. break;
  739. }
  740. TCHAR szCertName[1024];
  741. TCHAR szIssuerName[1024];
  742. // crack open the returned certificate and display the data
  743. CertGetNameString(pcLocalCert,
  744. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  745. 0,
  746. NULL,
  747. szCertName,
  748. sizeof(szCertName) / sizeof(szCertName[0]));
  749. CertGetNameString(pcLocalCert,
  750. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  751. CERT_NAME_ISSUER_FLAG,
  752. NULL,
  753. szIssuerName,
  754. sizeof(szCertName) / sizeof(szCertName[0]));
  755. CTime tExpires(pcLocalCert->pCertInfo->NotAfter);
  756. CString szExpires = tExpires.Format(TEXT("%x"));
  757. i = m_list1.InsertItem(i, szCertName, 0);
  758. m_list1.SetItem(i, 1, LVIF_TEXT, szIssuerName, 0, 0, 0, 0);
  759. m_list1.SetItem(i, 2, LVIF_TEXT, szExpires, 0, 0, 0, 0);
  760. m_list1.SetItemData(i, (DWORD_PTR)CertDuplicateCertificateContext(pcLocalCert));
  761. //pcLocalCert should get deleted when it is repassed into CertEnumCerti..
  762. }
  763. CertCloseStore(hCertStore, 0);
  764. }
  765. //
  766. // open the local cert store
  767. //
  768. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  769. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  770. NULL,
  771. // CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  772. CERT_STORE_READONLY_FLAG,
  773. m_szTempNonInstallableStore);
  774. if (hCertStore)
  775. {
  776. //
  777. // Enumerate the cert store
  778. //
  779. int i = 0;
  780. for (;;) {
  781. pcLocalCert = CertEnumCertificatesInStore(hCertStore, pcLocalCert);
  782. if (!pcLocalCert) {
  783. if (GetLastError() != CRYPT_E_NOT_FOUND )
  784. {
  785. DebugMsg((DM_WARNING, TEXT("CSignatures::RefreshData : Failed to enumerate certificate store.")));
  786. }
  787. break;
  788. }
  789. TCHAR szCertName[1024];
  790. TCHAR szIssuerName[1024];
  791. // crack open the returned certificate and display the data
  792. CertGetNameString(pcLocalCert,
  793. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  794. 0,
  795. NULL,
  796. szCertName,
  797. sizeof(szCertName) / sizeof(szCertName[0]));
  798. CertGetNameString(pcLocalCert,
  799. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  800. CERT_NAME_ISSUER_FLAG,
  801. NULL,
  802. szIssuerName,
  803. sizeof(szCertName) / sizeof(szCertName[0]));
  804. CTime tExpires(pcLocalCert->pCertInfo->NotAfter);
  805. CString szExpires = tExpires.Format(TEXT("%x"));
  806. i = m_list2.InsertItem(i, szCertName, 0);
  807. m_list2.SetItem(i, 1, LVIF_TEXT, szIssuerName, 0, 0, 0, 0);
  808. m_list2.SetItem(i, 2, LVIF_TEXT, szExpires, 0, 0, 0, 0);
  809. m_list2.SetItemData(i, (DWORD_PTR)CertDuplicateCertificateContext(pcLocalCert));
  810. //pcLocalCert should get deleted when it is repassed into CertEnumCerti..
  811. }
  812. CertCloseStore(hCertStore, 0);
  813. }
  814. OnAllowChanged();
  815. SetModified(FALSE);
  816. }
  817. void CSignatures::OnContextMenu(CWnd* pWnd, CPoint point)
  818. {
  819. StandardContextMenu(pWnd->m_hWnd, IDD_FILE_EXT);
  820. }
  821. #endif // DIGITAL_SIGNATURES