コード例 #1
0
        private void btnCreateSigned_Click(object sender, EventArgs e)
        {
            if (!isOpened)
            {
                return;
            }

            if (lvCertificates.SelectedIndices.Count == 0)
            {
                return;
            }

            CreateCertForm dlg = new CreateCertForm();

            dlg.edtStoreName.Text = cbLocation.Text + "; " + cbName.Text;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                PrepareStore(dlg);

                Certificate issuer = certificateStore1.Items[lvCertificates.SelectedIndices[0]];
                Certificate cert   = certificateStore1.CreateSigned(issuer, dlg.BuildSubjectString(), Convert.ToInt32(dlg.edtSerial.Text));
                certificateStore1.Items.Add(cert);
                cert.FriendlyName = dlg.edtFriendlyName.Text;

                InstallCerts();

                btnLoad_Click(null, null);
            }
        }
コード例 #2
0
        private void PrepareStore(CreateCertForm dlg)
        {
            certificateStore1.ValidFrom = Convert.ToDateTime(dlg.edtValidFrom.Text);
            certificateStore1.ValidTo   = Convert.ToDateTime(dlg.edtValidTo.Text);
            certificateStore1.KeyName   = dlg.edtKeyName.Text;
            certificateStore1.KeyLength = Convert.ToInt32(dlg.edtKeyLength.Text);

            StringCollectionEx usage = new StringCollectionEx();

            if (dlg.cbServerAuth.Checked)
            {
                usage.Add(CertificateEnhancedUsage.OID_PKIX_KP_SERVER_AUTH);
            }
            if (dlg.cbClientAuth.Checked)
            {
                usage.Add(CertificateEnhancedUsage.OID_PKIX_KP_CLIENT_AUTH);
            }
            if (dlg.cbCodeSigning.Checked)
            {
                usage.Add(CertificateEnhancedUsage.OID_PKIX_KP_CODE_SIGNING);
            }
            if (dlg.cbEmailProtection.Checked)
            {
                usage.Add(CertificateEnhancedUsage.OID_PKIX_KP_EMAIL_PROTECTION);
            }

            certificateStore1.EnhancedKeyUsage = usage.ToArray();

            if (!certificateStore1.KeyExists(certificateStore1.KeyName))
            {
                certificateStore1.CreateKey(certificateStore1.KeyName);
            }
        }