private void createNewCertToolStripMenuItem_Click(object sender, EventArgs e) { using (CreateCertForm frm = new CreateCertForm()) { if (frm.ShowDialog(this) == DialogResult.OK) { try { using (SaveFileDialog dlg = new SaveFileDialog()) { dlg.Filter = Properties.Resources.CreateCert_SaveFilter; if (dlg.ShowDialog(this) == DialogResult.OK) { GetPasswordForm getPass = new GetPasswordForm(); SecureString password = null; if (getPass.ShowDialog(this) == DialogResult.OK) { password = getPass.Password; if (password.Length == 0) { password = null; } } string ext = Path.GetExtension(dlg.FileName); if (ext.Equals(".pfx") || ext.Equals(".p12")) { File.WriteAllBytes(dlg.FileName, frm.Certificate.Export(X509ContentType.Pfx, password)); } else { File.WriteAllText(dlg.FileName, CertificateUtils.ExportToPEM(frm.Certificate) + CertificateUtils.ExportToPEM((RSA)frm.Certificate.PrivateKey, password)); } } } } catch (IOException ex) { MessageBox.Show(this, ex.Message, Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (CryptographicException ex) { MessageBox.Show(this, ex.Message, Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (ArgumentException ex) { MessageBox.Show(this, ex.Message, Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
private X509Certificate2 LoadFromStore(bool needPrivateKey) { X509Certificate2 ret = null; try { X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); X509Certificate2Collection pcollection = new X509Certificate2Collection(); foreach (var cert in fcollection) { if (!needPrivateKey || cert.HasPrivateKey) { pcollection.Add(cert); } } if (pcollection.Count > 0) { X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(pcollection, CANAPE.Properties.Resources.CreateCertForm_CertSelect, CANAPE.Properties.Resources.CreateCertForm_CertSelectTitle, X509SelectionFlag.SingleSelection); if (scollection.Count > 0) { ret = scollection[0]; if (CertificateUtils.IsProtectedPrivateKey(ret)) { if (MessageBox.Show(this, Properties.Resources.CreateCertForm_SpecifyPassword, Properties.Resources.CreateCertForm_SpecifyPasswordCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using (GetPasswordForm frm = new GetPasswordForm()) { if (frm.ShowDialog(this) == DialogResult.OK) { CertificateUtils.ReloadProtectedPrivateKey(ret, frm.Password); } } } } } } else { MessageBox.Show(this, CANAPE.Properties.Resources.CreateCertForm_NoStoreCerts, CANAPE.Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error); } store.Close(); } catch (CryptographicException) { MessageBox.Show(this, CANAPE.Properties.Resources.CreateCertForm_StoreError, CANAPE.Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error); } return ret; }