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.

956 lines
30 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. (void) StringCchPrintf(szBuffer,
  189. sizeof(szBuffer) / sizeof(szBuffer[0]),
  190. TEXT("(HRESULT: 0x%lX)"),
  191. hr);
  192. }
  193. szMessage += szBuffer;
  194. MessageBox(szMessage,
  195. NULL,
  196. MB_OK | MB_ICONEXCLAMATION);
  197. }
  198. //+--------------------------------------------------------------------------
  199. //
  200. // Function: AddMSIToCertStore
  201. //
  202. // Synopsis: Gets a certificate from an MSI file and adds it to the
  203. // certificate store.
  204. //
  205. // Arguments: [lpFileName] - path to the MSI file
  206. // [lpFileStore] - path to the certificate store
  207. //
  208. // Returns:
  209. //
  210. // Modifies:
  211. //
  212. // History: 07-26-2000 stevebl Created
  213. //
  214. // Notes:
  215. //
  216. //---------------------------------------------------------------------------
  217. HRESULT CSignatures::AddMSIToCertStore(LPWSTR lpFileName, LPWSTR lpFileStore)
  218. {
  219. PCCERT_CONTEXT pcc = NULL;
  220. HCERTSTORE hCertStore = NULL;
  221. BOOL bRet;
  222. HRESULT hrRet = MsiGetFileSignatureInformation(lpFileName,
  223. 0,
  224. &pcc,
  225. NULL,
  226. NULL);
  227. if (SUCCEEDED(hrRet))
  228. {
  229. //
  230. // Open the certificate store
  231. //
  232. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  233. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  234. NULL,
  235. CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  236. lpFileStore);
  237. if (hCertStore == NULL) {
  238. DebugMsg((DM_WARNING, L"AddMSIToCertStore: CertOpenStore failed with %u",GetLastError()));
  239. hrRet = HRESULT_FROM_WIN32(GetLastError());
  240. goto Exit;
  241. }
  242. //
  243. // add the given certificate to the store
  244. //
  245. bRet = CertAddCertificateContextToStore(hCertStore,
  246. pcc,
  247. CERT_STORE_ADD_NEW,
  248. NULL);
  249. if (!bRet) {
  250. DebugMsg((DM_WARNING, L"AddToCertStore: CertAddCertificateContextToStore failed with %u", GetLastError()));
  251. hrRet = HRESULT_FROM_WIN32(GetLastError());
  252. goto Exit;
  253. }
  254. //
  255. // Save the store
  256. //
  257. bRet = CertCloseStore(hCertStore, 0);
  258. hCertStore = NULL; // Make the store handle NULL, Nothing more we can do
  259. if (!bRet) {
  260. DebugMsg((DM_WARNING, L"AddToCertStore: CertCloseStore failed with %u", GetLastError()));
  261. hrRet = HRESULT_FROM_WIN32(GetLastError());
  262. }
  263. hrRet = S_OK;
  264. Exit:
  265. if (hCertStore) {
  266. //
  267. // No need to get the error code
  268. //
  269. CertCloseStore(hCertStore, 0);
  270. }
  271. CertFreeCertificateContext(pcc);
  272. if (FAILED(hrRet))
  273. {
  274. ReportFailure(IDS_ADDCERTFAILED, hrRet);
  275. }
  276. }
  277. else
  278. {
  279. ReportFailure(IDS_CERTFROMMSIFAILED, hrRet);
  280. DebugMsg((DM_WARNING, L"AddMSIToCertStore: MsiGetFileSignatureInformation failed with 0x%x", hrRet));
  281. }
  282. return hrRet;
  283. }
  284. //+-------------------------------------------------------------------------
  285. // AddToCertStore
  286. //
  287. // Purpose:
  288. // Adds the certificate from the given filename to the certificate store
  289. // and saves it to the given location
  290. //
  291. //
  292. // Parameters
  293. // lpFIleName - Location of the certificate file
  294. // lpFileStore - Location where the resultant cetrtficate path should
  295. // be stored
  296. //
  297. //
  298. // Return Value:
  299. // S_OK if successful or the corresponding error code
  300. //
  301. // Comments: Shamefully stolen from Shaji's code.
  302. //+-------------------------------------------------------------------------
  303. HRESULT CSignatures::AddToCertStore(LPWSTR lpFileName, LPWSTR lpFileStore)
  304. {
  305. CRYPTUI_WIZ_IMPORT_SRC_INFO cui_src;
  306. HCERTSTORE hCertStore = NULL;
  307. BOOL bRet = FALSE;
  308. HRESULT hrRet = S_OK;
  309. //
  310. // Need to make the store usable and saveable from
  311. // multiple admin consoles..
  312. //
  313. // For that the file has to be saved and kept on a temp file
  314. // and then modified..
  315. //
  316. if (!lpFileName || !lpFileName[0] || !lpFileStore || !lpFileStore[0]) {
  317. return E_INVALIDARG;
  318. }
  319. //
  320. // Open the certificate store
  321. //
  322. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  323. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  324. NULL,
  325. CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  326. lpFileStore);
  327. if (hCertStore == NULL) {
  328. DebugMsg((DM_WARNING, L"AddToCertStore: CertOpenStore failed with %u",GetLastError()));
  329. hrRet = HRESULT_FROM_WIN32(GetLastError());
  330. goto Exit;
  331. }
  332. //
  333. // add the given certificate to the store
  334. //
  335. cui_src.dwFlags = 0;
  336. cui_src.dwSize = sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  337. cui_src.dwSubjectChoice = CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  338. cui_src.pwszFileName = lpFileName;
  339. cui_src.pwszPassword = NULL;
  340. bRet = CryptUIWizImport(CRYPTUI_WIZ_NO_UI, NULL, NULL, &cui_src, hCertStore);
  341. if (!bRet) {
  342. DebugMsg((DM_WARNING, L"AddToCertStore: CryptUIWizImport failed with %u", GetLastError()));
  343. hrRet = HRESULT_FROM_WIN32(GetLastError());
  344. goto Exit;
  345. }
  346. //
  347. // Save the store
  348. //
  349. bRet = CertCloseStore(hCertStore, 0);
  350. hCertStore = NULL; // Make the store handle NULL, Nothing more we can do
  351. if (!bRet) {
  352. DebugMsg((DM_WARNING, L"AddToCertStore: CertCloseStore failed with %u", GetLastError()));
  353. hrRet = HRESULT_FROM_WIN32(GetLastError());
  354. }
  355. hrRet = S_OK;
  356. Exit:
  357. if (hCertStore) {
  358. //
  359. // No need to get the error code
  360. //
  361. CertCloseStore(hCertStore, 0);
  362. }
  363. if (FAILED(hrRet))
  364. {
  365. ReportFailure(IDS_ADDCERTFAILED, hrRet);
  366. }
  367. return hrRet;
  368. }
  369. void CSignatures::AddCertificate(CString &szStore)
  370. {
  371. CString szExtension;
  372. CString szFilter;
  373. szExtension.LoadString(IDS_CERT_DEF_EXT);
  374. szFilter.LoadString(IDS_CERT_EXT_FILT);
  375. OPENFILENAME ofn;
  376. memset(&ofn, 0, sizeof(ofn));
  377. ofn.lStructSize = sizeof(ofn);
  378. ofn.hwndOwner = m_hWnd;
  379. ofn.hInstance = ghInstance;
  380. TCHAR lpstrFilter[MAX_PATH];
  381. wcsncpy(lpstrFilter, szFilter, MAX_PATH);
  382. ofn.lpstrFilter = lpstrFilter;
  383. TCHAR szFileTitle[MAX_PATH];
  384. TCHAR szFile[MAX_PATH];
  385. szFile[0] = NULL;
  386. ofn.lpstrFile = szFile;
  387. ofn.nMaxFile = MAX_PATH;
  388. ofn.lpstrFileTitle = szFileTitle;
  389. ofn.nMaxFileTitle = MAX_PATH;
  390. ofn.lpstrInitialDir = m_pScopePane->m_ToolDefaults.szStartPath;
  391. ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_EXPLORER;
  392. ofn.lpstrDefExt = szExtension;
  393. int iBreak = 0;
  394. while (lpstrFilter[iBreak])
  395. {
  396. if (lpstrFilter[iBreak] == TEXT('|'))
  397. {
  398. lpstrFilter[iBreak] = 0;
  399. }
  400. iBreak++;
  401. }
  402. if (GetOpenFileName(&ofn))
  403. {
  404. CHourglass hourglass;
  405. CString szPackagePath;
  406. HRESULT hr = E_FAIL;
  407. if ((ofn.nFileExtension > 0) &&
  408. (0 == _wcsicmp(ofn.lpstrFile + ofn.nFileExtension, L"msi")))
  409. {
  410. // this is an MSI package
  411. HRESULT hr = AddMSIToCertStore(ofn.lpstrFile, (LPWSTR)((LPCWSTR)szStore));
  412. if (SUCCEEDED(hr))
  413. {
  414. RefreshData();
  415. SetModified();
  416. }
  417. }
  418. else
  419. {
  420. // this is a simple certificate
  421. HRESULT hr = AddToCertStore(ofn.lpstrFile, (LPWSTR)((LPCWSTR)szStore));
  422. if (SUCCEEDED(hr))
  423. {
  424. RefreshData();
  425. SetModified();
  426. }
  427. }
  428. }
  429. }
  430. void CSignatures::OnAddAllow()
  431. {
  432. AddCertificate(m_szTempInstallableStore);
  433. }
  434. void CSignatures::OnDeleteAllow()
  435. {
  436. RemoveCertificate(m_szTempInstallableStore, m_list1);
  437. }
  438. void CSignatures::OnPropertiesAllow()
  439. {
  440. CertificateProperties(m_szTempInstallableStore, m_list1);
  441. }
  442. void CSignatures::OnAddDisallow()
  443. {
  444. AddCertificate(m_szTempNonInstallableStore);
  445. }
  446. void CSignatures::OnDeleteDisallow()
  447. {
  448. RemoveCertificate(m_szTempNonInstallableStore, m_list2);
  449. }
  450. void CSignatures::OnPropertiesDisallow()
  451. {
  452. CertificateProperties(m_szTempNonInstallableStore, m_list2);
  453. }
  454. void CSignatures::OnAllowChanged()
  455. {
  456. BOOL fAllow = IsDlgButtonChecked(IDC_CHECK1);
  457. if (m_fAllow != fAllow)
  458. {
  459. SetModified();
  460. }
  461. m_fAllow = fAllow;
  462. GetDlgItem(IDC_BUTTON1)->EnableWindow(m_fAllow);
  463. GetDlgItem(IDC_BUTTON2)->EnableWindow(m_fAllow);
  464. GetDlgItem(IDC_BUTTON3)->EnableWindow(m_fAllow);
  465. GetDlgItem(IDC_LIST1)->EnableWindow(m_fAllow);
  466. }
  467. void CSignatures::OnIgnoreChanged()
  468. {
  469. BOOL fIgnoreForAdmins = IsDlgButtonChecked(IDC_CHECK2);
  470. if (m_fIgnoreForAdmins != fIgnoreForAdmins)
  471. {
  472. SetModified();
  473. }
  474. m_fIgnoreForAdmins = fIgnoreForAdmins;
  475. }
  476. BOOL CSignatures::OnInitDialog()
  477. {
  478. // create temporary store files
  479. BOOL fFilesCreated = FALSE;
  480. TCHAR szTempPath[MAX_PATH];
  481. if (GetTempPath(MAX_PATH, szTempPath))
  482. {
  483. TCHAR szTempFile[MAX_PATH];
  484. if (GetTempFileName(szTempPath,
  485. NULL,
  486. 0,
  487. szTempFile))
  488. {
  489. m_szTempInstallableStore = szTempFile;
  490. if (GetTempFileName(szTempPath,
  491. NULL,
  492. 0,
  493. szTempFile))
  494. {
  495. m_szTempNonInstallableStore = szTempFile;
  496. fFilesCreated = TRUE;
  497. }
  498. }
  499. }
  500. if (fFilesCreated)
  501. {
  502. CString szPath = m_pScopePane->m_szGPT_Path;
  503. szPath += TEXT("\\msi_installable_certs");
  504. CopyFile(szPath, m_szTempInstallableStore, FALSE);
  505. szPath = m_pScopePane->m_szGPT_Path;
  506. szPath += TEXT("\\msi_non_installable_certs");
  507. CopyFile(szPath, m_szTempNonInstallableStore, FALSE);
  508. }
  509. else
  510. {
  511. DebugMsg((DM_WARNING, TEXT("CSignatures::OnInitDialog : Failed to create temporary certificate stores.")));
  512. }
  513. CPropertyPage::OnInitDialog();
  514. // add columns to the lists
  515. RECT rect;
  516. m_list1.GetClientRect(&rect);
  517. CString szTemp;
  518. szTemp.LoadString(IDS_SIGS_COL1);
  519. m_list1.InsertColumn(0, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  520. m_list2.InsertColumn(0, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  521. szTemp.LoadString(IDS_SIGS_COL2);
  522. m_list1.InsertColumn(1, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  523. m_list2.InsertColumn(1, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.35);
  524. szTemp.LoadString(IDS_SIGS_COL3);
  525. m_list1.InsertColumn(2, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.25);
  526. m_list2.InsertColumn(2, szTemp, LVCFMT_LEFT, (rect.right - rect.left) * 0.25);
  527. // add image lists
  528. CImageList * pil = NULL;
  529. pil = new CImageList;
  530. if (pil)
  531. {
  532. pil->Create(IDB_CERTIFICATE, 16, 0, RGB(255, 0, 255));
  533. m_list1.SetImageList(pil, LVSIL_SMALL);
  534. }
  535. pil = new CImageList;
  536. if (pil)
  537. {
  538. pil->Create(IDB_CERTIFICATE, 16, 0, RGB(255, 0, 255));
  539. m_list2.SetImageList(pil, LVSIL_SMALL);
  540. }
  541. // retrieve initial registry key setting
  542. HKEY hKey;
  543. HRESULT hr = m_pIGPEInformation->GetRegistryKey(m_pScopePane->m_fMachine ?
  544. GPO_SECTION_MACHINE :
  545. GPO_SECTION_USER, &hKey);
  546. if (SUCCEEDED(hr))
  547. {
  548. HKEY hSubKey;
  549. if(ERROR_SUCCESS == RegOpenKeyEx(hKey,
  550. TEXT("Microsoft\\Windows\\Installer"),
  551. 0,
  552. KEY_ALL_ACCESS,
  553. &hSubKey))
  554. {
  555. DWORD dw;
  556. DWORD dwSize = sizeof(DWORD);
  557. if (ERROR_SUCCESS == RegQueryValueEx(hSubKey,
  558. TEXT("InstallKnownPackagesOnly"),
  559. NULL,
  560. NULL,
  561. (BYTE *)&dw,
  562. &dwSize))
  563. {
  564. m_fAllow = (dw == 1) ? TRUE : FALSE;
  565. CheckDlgButton(IDC_CHECK1, m_fAllow);
  566. }
  567. dwSize = sizeof(DWORD);
  568. if (ERROR_SUCCESS == RegQueryValueEx(hSubKey,
  569. TEXT("IgnoreSignaturePolicyForAdmins"),
  570. NULL,
  571. NULL,
  572. (BYTE *)&dw,
  573. &dwSize))
  574. {
  575. m_fIgnoreForAdmins = (dw == 1) ? TRUE : FALSE;
  576. CheckDlgButton(IDC_CHECK2, m_fIgnoreForAdmins);
  577. }
  578. RegCloseKey(hSubKey);
  579. }
  580. RegCloseKey(hKey);
  581. }
  582. RefreshData();
  583. return TRUE; // return TRUE unless you set the focus to a control
  584. // EXCEPTION: OCX Property Pages should return FALSE
  585. }
  586. BOOL CSignatures::OnApply()
  587. {
  588. HRESULT hr = E_NOTIMPL;
  589. HKEY hKey;
  590. hr = m_pIGPEInformation->GetRegistryKey(m_pScopePane->m_fMachine ?
  591. GPO_SECTION_MACHINE :
  592. GPO_SECTION_USER, &hKey);
  593. if (SUCCEEDED(hr))
  594. {
  595. hr = E_FAIL;
  596. HKEY hSubKey;
  597. if(ERROR_SUCCESS == RegCreateKeyEx(hKey,
  598. TEXT("Microsoft\\Windows\\Installer"),
  599. 0,
  600. NULL,
  601. REG_OPTION_NON_VOLATILE,
  602. KEY_ALL_ACCESS,
  603. NULL,
  604. &hSubKey,
  605. NULL))
  606. {
  607. DWORD dw = m_fAllow ? 1 : 0;
  608. DWORD dwSize = sizeof(DWORD);
  609. if (ERROR_SUCCESS == RegSetValueEx(hSubKey,
  610. TEXT("InstallKnownPackagesOnly"),
  611. 0,
  612. REG_DWORD,
  613. (BYTE *)&dw,
  614. dwSize))
  615. {
  616. hr = S_OK;
  617. }
  618. dw = m_fIgnoreForAdmins ? 1 : 0;
  619. dwSize = sizeof(DWORD);
  620. if (ERROR_SUCCESS == RegSetValueEx(hSubKey,
  621. TEXT("IgnoreSignaturePolicyForAdmins"),
  622. 0,
  623. REG_DWORD,
  624. (BYTE *)&dw,
  625. dwSize))
  626. {
  627. hr = S_OK;
  628. }
  629. RegCloseKey(hSubKey);
  630. }
  631. RegCloseKey(hKey);
  632. }
  633. // copy back the certificate stores
  634. if (SUCCEEDED(hr))
  635. {
  636. m_list1.DeleteAllItems();
  637. m_list2.DeleteAllItems();
  638. CString szPath = m_pScopePane->m_szGPT_Path;
  639. szPath += TEXT("\\msi_installable_certs");
  640. CopyFile(m_szTempInstallableStore, szPath, FALSE);
  641. szPath = m_pScopePane->m_szGPT_Path;
  642. szPath += TEXT("\\msi_non_installable_certs");
  643. CopyFile(m_szTempNonInstallableStore, szPath, FALSE);
  644. RefreshData();
  645. }
  646. if (FAILED(hr))
  647. {
  648. CString sz;
  649. sz.LoadString(IDS_CHANGEFAILED);
  650. ReportGeneralPropertySheetError(m_hWnd, sz, hr);
  651. return FALSE;
  652. }
  653. else
  654. {
  655. GUID guid = REGISTRY_EXTENSION_GUID;
  656. if (FAILED(m_pIGPEInformation->PolicyChanged(m_pScopePane->m_fMachine,
  657. TRUE,
  658. &guid,
  659. m_pScopePane->m_fMachine ? &guidMachSnapin
  660. : &guidUserSnapin)))
  661. {
  662. ReportPolicyChangedError(m_hWnd);
  663. }
  664. // need to call PolicyChanged for Shaji's extension too.
  665. // REMOVE THIS LINE WHEN SHAJI CHECKS IN HIS GUID
  666. #define GUID_MSICERT_CSE { 0x000c10f4, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }
  667. GUID guid2 = GUID_MSICERT_CSE;
  668. if (FAILED(m_pIGPEInformation->PolicyChanged(m_pScopePane->m_fMachine,
  669. TRUE,
  670. &guid2,
  671. m_pScopePane->m_fMachine ? &guidMachSnapin
  672. : &guidUserSnapin)))
  673. {
  674. ReportPolicyChangedError(m_hWnd);
  675. }
  676. }
  677. return CPropertyPage::OnApply();
  678. }
  679. LRESULT CSignatures::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  680. {
  681. switch (message)
  682. {
  683. case WM_HELP:
  684. StandardHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, IDD);
  685. return 0;
  686. case WM_USER_REFRESH:
  687. RefreshData();
  688. return 0;
  689. case WM_USER_CLOSE:
  690. return GetOwner()->SendMessage(WM_CLOSE);
  691. case WM_NOTIFY:
  692. {
  693. LPNMLISTVIEW pnmh = (LPNMLISTVIEW) lParam;
  694. if (pnmh->hdr.code == LVN_DELETEITEM)
  695. {
  696. switch(wParam)
  697. {
  698. case IDC_LIST1:
  699. CertFreeCertificateContext((PCCERT_CONTEXT)m_list1.GetItemData(pnmh->iItem));
  700. break;
  701. case IDC_LIST2:
  702. CertFreeCertificateContext((PCCERT_CONTEXT)m_list2.GetItemData(pnmh->iItem));
  703. break;
  704. }
  705. }
  706. }
  707. return CPropertyPage::WindowProc(message, wParam, lParam);
  708. default:
  709. return CPropertyPage::WindowProc(message, wParam, lParam);
  710. }
  711. }
  712. void CSignatures::RefreshData(void)
  713. {
  714. // populate the listview controls
  715. m_list1.DeleteAllItems();
  716. m_list2.DeleteAllItems();
  717. HCERTSTORE hCertStore = NULL;;
  718. PCCERT_CONTEXT pcLocalCert = NULL;
  719. //
  720. // open the local cert store
  721. //
  722. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  723. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  724. NULL,
  725. // CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  726. CERT_STORE_READONLY_FLAG,
  727. m_szTempInstallableStore);
  728. if (hCertStore)
  729. {
  730. //
  731. // Enumerate the cert store
  732. //
  733. int i = 0;
  734. for (;;) {
  735. pcLocalCert = CertEnumCertificatesInStore(hCertStore, pcLocalCert);
  736. if (!pcLocalCert) {
  737. if (GetLastError() != CRYPT_E_NOT_FOUND )
  738. {
  739. DebugMsg((DM_WARNING, TEXT("CSignatures::RefreshData : Failed to enumerate certificate store.")));
  740. }
  741. break;
  742. }
  743. TCHAR szCertName[1024];
  744. TCHAR szIssuerName[1024];
  745. // crack open the returned certificate and display the data
  746. CertGetNameString(pcLocalCert,
  747. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  748. 0,
  749. NULL,
  750. szCertName,
  751. sizeof(szCertName) / sizeof(szCertName[0]));
  752. CertGetNameString(pcLocalCert,
  753. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  754. CERT_NAME_ISSUER_FLAG,
  755. NULL,
  756. szIssuerName,
  757. sizeof(szCertName) / sizeof(szCertName[0]));
  758. CTime tExpires(pcLocalCert->pCertInfo->NotAfter);
  759. CString szExpires = tExpires.Format(TEXT("%x"));
  760. i = m_list1.InsertItem(i, szCertName, 0);
  761. m_list1.SetItem(i, 1, LVIF_TEXT, szIssuerName, 0, 0, 0, 0);
  762. m_list1.SetItem(i, 2, LVIF_TEXT, szExpires, 0, 0, 0, 0);
  763. m_list1.SetItemData(i, (DWORD_PTR)CertDuplicateCertificateContext(pcLocalCert));
  764. //pcLocalCert should get deleted when it is repassed into CertEnumCerti..
  765. }
  766. CertCloseStore(hCertStore, 0);
  767. }
  768. //
  769. // open the local cert store
  770. //
  771. hCertStore = CertOpenStore( CERT_STORE_PROV_FILENAME,
  772. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  773. NULL,
  774. // CERT_FILE_STORE_COMMIT_ENABLE_FLAG,
  775. CERT_STORE_READONLY_FLAG,
  776. m_szTempNonInstallableStore);
  777. if (hCertStore)
  778. {
  779. //
  780. // Enumerate the cert store
  781. //
  782. int i = 0;
  783. for (;;) {
  784. pcLocalCert = CertEnumCertificatesInStore(hCertStore, pcLocalCert);
  785. if (!pcLocalCert) {
  786. if (GetLastError() != CRYPT_E_NOT_FOUND )
  787. {
  788. DebugMsg((DM_WARNING, TEXT("CSignatures::RefreshData : Failed to enumerate certificate store.")));
  789. }
  790. break;
  791. }
  792. TCHAR szCertName[1024];
  793. TCHAR szIssuerName[1024];
  794. // crack open the returned certificate and display the data
  795. CertGetNameString(pcLocalCert,
  796. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  797. 0,
  798. NULL,
  799. szCertName,
  800. sizeof(szCertName) / sizeof(szCertName[0]));
  801. CertGetNameString(pcLocalCert,
  802. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  803. CERT_NAME_ISSUER_FLAG,
  804. NULL,
  805. szIssuerName,
  806. sizeof(szCertName) / sizeof(szCertName[0]));
  807. CTime tExpires(pcLocalCert->pCertInfo->NotAfter);
  808. CString szExpires = tExpires.Format(TEXT("%x"));
  809. i = m_list2.InsertItem(i, szCertName, 0);
  810. m_list2.SetItem(i, 1, LVIF_TEXT, szIssuerName, 0, 0, 0, 0);
  811. m_list2.SetItem(i, 2, LVIF_TEXT, szExpires, 0, 0, 0, 0);
  812. m_list2.SetItemData(i, (DWORD_PTR)CertDuplicateCertificateContext(pcLocalCert));
  813. //pcLocalCert should get deleted when it is repassed into CertEnumCerti..
  814. }
  815. CertCloseStore(hCertStore, 0);
  816. }
  817. OnAllowChanged();
  818. SetModified(FALSE);
  819. }
  820. void CSignatures::OnContextMenu(CWnd* pWnd, CPoint point)
  821. {
  822. StandardContextMenu(pWnd->m_hWnd, IDD_FILE_EXT);
  823. }
  824. #endif // DIGITAL_SIGNATURES