コード例 #1
0
ファイル: MainForm.cs プロジェクト: tyranid/canape
        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);
                    }
                }
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: tyranid/canape
        private void exportRootCertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.Filter = Properties.Resources.MainForm_CertSaveFilter;

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        X509Certificate2 cert = CertManager.GetRootCert();

                        File.WriteAllText(dlg.FileName, CertificateUtils.ExportToPEM(cert), Encoding.ASCII);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
コード例 #3
0
        private void CloneCertChain(Uri url, string destination)
        {
            IProxyClientFactory factory = proxyClientControl.Client;

            if (factory == null)
            {
                factory = new IpProxyClientFactory();
            }

            ProxyClient client = factory.Create(new Logger());

            collection = new X509Certificate2Collection();

            using (IDataAdapter adapter = client.Connect(new IpProxyToken(null, url.Host, url.Port, IpProxyToken.IpClientType.Tcp, false),
                                                         new Logger(), new Nodes.MetaDictionary(), new Nodes.MetaDictionary(), new PropertyBag(), new Security.CredentialsManagerService()))
            {
                DataAdapterToStream stm = new DataAdapterToStream(adapter);

                using (SslStream ssl = new SslStream(stm, false, VerifyCallback))
                {
                    ssl.AuthenticateAsClient(url.Host);
                }
            }

            if (collection.Count > 0)
            {
                File.WriteAllBytes(Path.Combine(destination, String.Format("certchain_{0}.pfx", url.Host)), collection.Export(X509ContentType.Pfx));
                int count = 1;

                foreach (X509Certificate2 cert in collection)
                {
                    string path = Path.Combine(destination, String.Format("cert_{0}_{1}.cer", url.Host, count++));

                    File.WriteAllText(path, CertificateUtils.ExportToPEM(cert) +
                                      CertificateUtils.ExportToPEM((RSA)cert.PrivateKey, null));
                }
            }
        }