示例#1
0
        private void ExportPrivateKeyBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Select Certificate to Export";

                CertificateStoreIdentifier store = new CertificateStoreIdentifier();
                store.StoreType = ManagedStoreCTRL.StoreType;
                store.StorePath = ManagedStoreCTRL.StorePath;

                CertificateIdentifier id = new CertificateListDlg().ShowDialog(store, true);

                if (id == null)
                {
                    return;
                }

                X509Certificate2 certificate = id.Find(false);

                if (certificate == null)
                {
                    MessageBox.Show(
                        this,
                        "Certificate does not exist or its private key cannot be accessed.",
                        caption,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);

                    return;
                }

                string displayName = null;

                foreach (string element in Utils.ParseDistinguishedName(certificate.Subject))
                {
                    if (element.StartsWith("CN="))
                    {
                        displayName = element.Substring(3);
                        break;
                    }
                }

                StringBuilder filePath = new StringBuilder();

                if (!String.IsNullOrEmpty(displayName))
                {
                    filePath.Append(displayName);
                    filePath.Append(" ");
                }

                filePath.Append("[");
                filePath.Append(certificate.Thumbprint);
                filePath.Append("].pfx");

                SaveFileDialog dialog = new SaveFileDialog();

                dialog.CheckFileExists = false;
                dialog.CheckPathExists = true;
                dialog.DefaultExt = ".pfx";
                dialog.Filter = "PKCS#12 Files (*.pfx)|*.pfx|All Files (*.*)|*.*";
                dialog.ValidateNames = true;
                dialog.Title = "Save Private File";
                dialog.FileName = filePath.ToString();
                dialog.InitialDirectory = m_currentDirectory;

                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string password = new PasswordDlg().ShowDialog(null, "Password recommended");

                FileInfo fileInfo = new FileInfo(dialog.FileName);
                m_currentDirectory = fileInfo.DirectoryName;

                // save the file.
                using (Stream ostrm = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    byte[] data = certificate.Export(X509ContentType.Pkcs12, password);
                    ostrm.Write(data, 0, data.Length);
                }

                // save the public key.
                string fileRoot = fileInfo.FullName.Substring(0, fileInfo.FullName.Length - fileInfo.Extension.Length);
                fileRoot += ".der";

                using (Stream ostrm = File.Open(fileRoot, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    byte[] data = certificate.RawData;
                    ostrm.Write(data, 0, data.Length);
                }

                // check if original certificate should be deleted.
                if (new YesNoDlg().ShowDialog("Delete original certificate?", caption) == DialogResult.Yes)
                {                    
                    ICertificateStore physicalStore = id.OpenStore();

                    try
                    {
                        physicalStore.Delete(certificate.Thumbprint);
                    }
                    finally
                    {
                        physicalStore.Close();
                    }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#2
0
        private void SelectAndIssueCertificateBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Select Certificate to Issue";

                if (m_currentStore == null)
                {
                    m_currentStore = new CertificateStoreIdentifier();
                    m_currentStore.StoreType = Utils.DefaultStoreType;
                    m_currentStore.StorePath = Utils.DefaultStorePath;
                }

                CertificateIdentifier id = new CertificateListDlg().ShowDialog(m_currentStore, true);

                if (id == null)
                {
                    return;
                }

                m_currentStore.StoreType = id.StoreType;
                m_currentStore.StorePath = id.StorePath;

                X509Certificate2 certificate = id.Find();

                if (certificate == null)
                {
                    return;
                }

                CertificateIdentifier newId = new CreateCertificateDlg().ShowDialog(m_currentStore, IssuerKeyFilePathTB.Text, certificate);

                if (newId == null)
                {
                    return;
                }

                X509Certificate2 newCertificate = id.Find();

                MessageBox.Show(
                    this,
                    newCertificate.Subject + " issued.",
                    caption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                // check if original certificate should be deleted.
                if (new YesNoDlg().ShowDialog("Delete orginal certificate?", caption) == DialogResult.Yes)
                {
                    ICertificateStore physicalStore = id.OpenStore();

                    try
                    {
                        physicalStore.Delete(certificate.Thumbprint);
                    }
                    finally
                    {
                        physicalStore.Close();
                    }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }