Exemplo n.º 1
0
        X509Certificate2 CloneAndExportCert(X509Certificate2 rootCert, X509Certificate toClone)
        {
            X509Certificate2 cert = CertificateUtils.CloneAndSignCertificate(toClone, rootCert, false);

            collection.Add(cert);

            return(cert);
        }
Exemplo n.º 2
0
        private void buttonCreate_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            int rsaKeySize = 0;

            if (radioButtonSimpleCN.Checked && String.IsNullOrWhiteSpace(textBoxCN.Text))
            {
                MessageBox.Show(this, CANAPE.Properties.Resources.CreateCertForm_MustSpecifyCN,
                                CANAPE.Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (radioButtonTemplate.Checked && _templateCert == null)
            {
                MessageBox.Show(this, CANAPE.Properties.Resources.CreateCertForm_MustSpecifyTemplate,
                                CANAPE.Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (radioButtonSpecifyCA.Checked && _specifyCert == null)
            {
                MessageBox.Show(this, CANAPE.Properties.Resources.CreateCertForm_MustSpecifyCA,
                                CANAPE.Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!int.TryParse(comboBoxRsaKeySize.Text, out rsaKeySize))
            {
                MessageBox.Show(this, CANAPE.Properties.Resources.CreateCertForm_MustSpecifyAValidRSAKeySize,
                                CANAPE.Properties.Resources.MessageBox_ErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                    X509Certificate2 rootCert = null;

                    if (radioButtonSpecifyCA.Checked)
                    {
                        rootCert = _specifyCert;
                    }
                    else if (radioButtonDefaultCA.Checked)
                    {
                        rootCert = CertManager.GetRootCert();
                    }
                    else
                    {
                        // Self signed
                    }

                    if (radioButtonTemplate.Checked)
                    {
                        Certificate = CertificateUtils.CloneAndSignCertificate(_templateCert, rootCert, false, rsaKeySize, (CertificateHashAlgorithm)comboBoxHash.SelectedItem);
                    }
                    else
                    {
                        X509ExtensionCollection exts = new X509ExtensionCollection();
                        if (checkBoxCA.Checked)
                        {
                            exts.Add(new X509BasicConstraintsExtension(true, false, 0, true));
                        }

                        DateTime notBefore = DateTime.Now.Subtract(TimeSpan.FromDays(1));
                        Certificate = CertificateUtils.CreateCert(rootCert,
                                                                  new X500DistinguishedName(radioButtonSubject.Checked ? textBoxCN.Text : String.Format("CN={0}", textBoxCN.Text)), null, false, rsaKeySize,
                                                                  (CertificateHashAlgorithm)comboBoxHash.SelectedItem, notBefore, notBefore.AddYears(10), exts);
                    }
                }
                catch (Win32Exception ex)
                {
                    MessageBox.Show(ex.Message, CANAPE.Properties.Resources.MessageBox_ErrorString,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (CryptographicException ex)
                {
                    MessageBox.Show(ex.Message, CANAPE.Properties.Resources.MessageBox_ErrorString,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }