private void ItemToControls(SslConfigItem item) { SetGuidText(item.AppId); addressTextBox.Text = item.Address.ToString(); portTextBox.Text = item.Port.ToString(); refreshTimeTextBox.Text = item.RevocationFreshnessTime.ToString(); retrievalTimeoutTextBox.Text = item.RevocationUrlRetrievalTimeout.ToString(); ctlIdTextBox.Text = item.SslCtlIdentifier; ctlStoreTextBox.Text = item.SslCtlStoreName; noRevocationCheckBox.Checked = (item.CertCheckMode & HttpApi.ClientCertCheckMode.NoVerifyRevocation) != 0; onlyCachedRevocationCheckBox.Checked = (item.CertCheckMode & HttpApi.ClientCertCheckMode.CachedRevocationOnly) != 0; useFreshnessTimeCheckBox.Checked = (item.CertCheckMode & HttpApi.ClientCertCheckMode.UseRevocationFreshnessTime) != 0; noUsageCheckCheckBox.Checked = (item.CertCheckMode & HttpApi.ClientCertCheckMode.NoUsageCheck) != 0; dsMapperCheckBox.Checked = (item.Flags & HttpApi.SslConfigFlag.UseDSMapper) != 0; clientCertCheckBox.Checked = (item.Flags & HttpApi.SslConfigFlag.NegotiateClientCertificates) != 0; noRouteCheckBox.Checked = (item.Flags & HttpApi.SslConfigFlag.DoNotRouteToRawIsapiFilters) != 0; string storeName = (item.CertStoreName == null) ? "MY" : item.CertStoreName; if ((item.Hash != null) && (item.Hash.Length > 0)) { certStoreTextBox.Text = storeName; certHashTextBox.Text = CertUtil.BytesToHex(item.Hash); certNameTextBox.Text = CertUtil.GetCertNameFromStoreAndHash(storeName, item.Hash); _certHashBytes = item.Hash; } }
private void certBrowseButton_Click(object sender, System.EventArgs e) { IntPtr pCert = IntPtr.Zero; IntPtr pCsc = IntPtr.Zero; IntPtr[] stores = new IntPtr[2]; IntPtr pStores = Marshal.AllocHGlobal(2 * Marshal.SizeOf(typeof(IntPtr))); try { stores[0] = CertUtil.CertOpenStore(CertUtil.CERT_STORE_PROV_SYSTEM_A, 0, 0, (int)CertUtil.CertStoreLocation.LocalMachine, "MY"); if (stores[0] == IntPtr.Zero) { int error = Marshal.GetLastWin32Error(); throw new Exception("CertOpenStore failed. Error = " + error.ToString()); } stores[1] = CertUtil.CertOpenStore(CertUtil.CERT_STORE_PROV_SYSTEM_A, 0, 0, (int)CertUtil.CertStoreLocation.LocalMachine, "TRUST"); if (stores[1] == IntPtr.Zero) { int error = Marshal.GetLastWin32Error(); throw new Exception("CertOpenStore failed. Error = " + error.ToString()); } Marshal.WriteIntPtr(pStores, 0, stores[0]); Marshal.WriteIntPtr(pStores, Marshal.SizeOf(typeof(IntPtr)), stores[1]); CertUtil.CRYPTUI_SELECTCERTIFICATE_STRUCT csc = new CertUtil.CRYPTUI_SELECTCERTIFICATE_STRUCT(); csc.dwSize = (uint)Marshal.SizeOf(typeof(CertUtil.CRYPTUI_SELECTCERTIFICATE_STRUCT)); csc.hwndParent = this.Handle; csc.cDisplayStores = 2; csc.rghDisplayStores = pStores; pCsc = Marshal.AllocHGlobal((int)(csc.dwSize)); Marshal.StructureToPtr(csc, pCsc, false); pCert = CertUtil.CryptUIDlgSelectCertificate(pCsc); if (pCert != IntPtr.Zero) { CertUtil.CERT_CONTEXT context = (CertUtil.CERT_CONTEXT)Marshal.PtrToStructure(pCert, typeof(CertUtil.CERT_CONTEXT)); certStoreTextBox.Text = context.hCertStore == stores[0] ? "MY" : "TRUST"; certNameTextBox.Text = CertUtil.GetCertNameAttribute(pCert, CertUtil.CertNameType.CERT_NAME_FRIENDLY_DISPLAY_TYPE); _certHashBytes = CertUtil.GetCertHash(pCert); certHashTextBox.Text = CertUtil.BytesToHex(_certHashBytes); } } catch (Exception ex) { MessageBox.Show(this, ex.ToString(), "An error has occurred"); } finally { if (pCsc != IntPtr.Zero) { Marshal.FreeHGlobal(pCsc); } if (pCert != IntPtr.Zero) { CertUtil.CertFreeCertificateContext(pCert); } foreach (IntPtr store in stores) { if (store != IntPtr.Zero) { CertUtil.CertCloseStore(store, 0); } } Marshal.FreeHGlobal(pStores); } }