Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Authorities();
            return;

            SimpleSerialNumber       seq = new SimpleSerialNumber();
            X509CertificateAuthority ca  = X509CertificateAuthority.SelfSigned(
                seq,
                new X509Name("CN=."),
                TimeSpan.FromDays(10)
                );

            Console.WriteLine(ca.Certificate);

            DSA         dsa = new DSA(new DSAParameters(512));
            CryptoKey   key = new CryptoKey(dsa);
            X509Request req = new X509Request(0, new X509Name("CN=com."), key);

            req.Sign(key, MessageDigest.DSS1);

            X509Certificate cert = ca.ProcessRequest(req, TimeSpan.FromDays(10));

            Console.WriteLine(cert);
            Console.WriteLine("CA Verified: " + cert.Verify(ca.Key));
            Console.WriteLine("Self Verified: " + cert.Verify(key));

            SimpleSerialNumber       serial2 = new SimpleSerialNumber();
            X509CertificateAuthority caSelf  = new X509CertificateAuthority(
                cert,
                key,
                serial2);

            X509Request req2    = cert.CreateRequest(key, MessageDigest.DSS1);
            X509Name    subject = req2.Subject;

            Console.WriteLine("Request1: " + req);
            Console.WriteLine("Request2: " + req2);

            X509Certificate cert2 = caSelf.ProcessRequest(req2, TimeSpan.FromDays(10));

            Console.WriteLine("Cert2: " + cert2);

            DH dh = new DH(128, 5);

            MessageDigestContext mdc = new MessageDigestContext(MessageDigest.DSS1);

            byte[] msg = dh.PublicKey;
            byte[] sig = mdc.Sign(msg, key);

            Console.WriteLine(dh);
            Console.WriteLine("DH P         : " + BitConverter.ToString(dh.P));
            Console.WriteLine("DH G         : " + BitConverter.ToString(dh.G));
            Console.WriteLine("DH Secret Key: " + BitConverter.ToString(dh.PrivateKey));
            Console.WriteLine("DH Public Key: " + BitConverter.ToString(msg));
            Console.WriteLine("DH Signature : " + BitConverter.ToString(sig));

            Console.WriteLine(mdc.Verify(msg, sig, key));
        }
Ejemplo n.º 2
0
        void DoWork()
        {
            try
            {
                //var certificateGenerator = new X509V3CertificateGenerator();
                // Step 1
                if (nameFrm1.GetNames())
                {
                    X509Name subject = new X509Name();

                    // TODO FIX NAME FRM
                    int progress = 0;
                    int cur      = 0;
                    foreach (KeyValuePair <string, string> keyp in nameFrm1.bind.Vals)
                    {
                        progress = (int)((double)((cur / nameFrm1.bind.Vals.Count) * 100));
                        backgroundWorker1.ReportProgress(progress, 1);
                        subject.AddEntryByName(keyp.Key.ToUpper(), keyp.Value);
                        cur++;
                    }



                    backgroundWorker1.ReportProgress(100, 1);

                    // Step 2
                    progress = 0;
                    cur      = 0;

                    if (keyGenerationFrm1.GenerateKey())
                    {
                        Al.Security.CA.KeyGenerationBind keyb = keyGenerationFrm1.bind;
                        CryptoKey KeyPair = keyb.KeyPair;
                        CSReq = new X509Request(2, subject, KeyPair);
                        // CUSTOM SERIAL NUMBER
                        SimpleSerialNumber serial = null;

                        if (keyGenerationFrm1.serialnumber.Value != null && keyGenerationFrm1.serialnumber.Value != 0)
                        {
                            serial = new SimpleSerialNumber(keyGenerationFrm1.serialnumber.Value);
                        }



                        backgroundWorker1.ReportProgress(100, 2);

                        // STEP 3
                        if (!configbox.Checked)
                        {
                            ExtensionsWork();
                        }
                        Configuration config = new Configuration(Application.StartupPath + @"\ext.cfg");
                        backgroundWorker1.ReportProgress(100, 3);
                        DateTime notbe = nameFrm1.notbefore.Value;
                        DateTime vali  = nameFrm1.notafter.Value;
                        // STEP 4 SAVE
                        //generate
                        SimpleSerialNumber       caserial   = new SimpleSerialNumber(PFX.Certificate.SerialNumber);
                        X509CertificateAuthority ca         = new X509CertificateAuthority(PFX.Certificate, PFX.PrivateKey, caserial, config);
                        X509Certificate          signedCert = null;
                        if (serial != null)
                        {
                            signedCert = ca.ProcessRequest(serial, config, CSReq, DateTime.UtcNow, vali, keyb.SignatureAlgorithm);
                        }

                        else
                        {
                            signedCert = ca.ProcessRequest(config, CSReq, DateTime.UtcNow, vali, keyb.SignatureAlgorithm);
                        }

                        //     CertExportFrm cfrm = new CertExportFrm();
                        cfrm.certificate = signedCert;
                        cfrm.Key         = KeyPair;
                        //   cfrm.ShowDialog();

                        backgroundWorker1.ReportProgress(100, 4);
                    }
                    else
                    {
                        SelectTab(superTabItem5);
                    }
                }
                else
                {
                    SelectTab(superTabItem4);
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Ejemplo n.º 3
0
        void DoWork()
        {
            try
            {
                // Step 1
                if (nameFrm1.GetNames())
                {
                    X509Name subject = new X509Name();

                    // TODO FIX NAME FRM
                    int progress = 0;
                    int cur      = 0;
                    foreach (KeyValuePair <string, string> keyp in nameFrm1.bind.Vals)
                    {
                        progress = (int)((double)((cur / nameFrm1.bind.Vals.Count) * 100));
                        backgroundWorker1.ReportProgress(progress, 1);

                        subject.AddEntryByName(keyp.Key.ToUpper(), keyp.Value);
                        cur++;
                    }



                    backgroundWorker1.ReportProgress(100, 1);

                    // Step 2
                    progress = 0;
                    cur      = 0;

                    if (keyGenerationFrm1.GenerateKey())
                    {
                        Al.Security.CA.KeyGenerationBind keyb = keyGenerationFrm1.bind;
                        CryptoKey KeyPair = keyb.KeyPair;

                        //               var serialNumber =
                        //BigIntegers.CreateRandomInRange(
                        //    BigInteger.One, BigInteger.ValueOf(Int64.MaxValue), keyb.SRandom);
                        // CUTOM SERIAL
                        SimpleSerialNumber serial = null;
                        int serialn = 0;
                        if (keyGenerationFrm1.serialnumber.Value != null && keyGenerationFrm1.serialnumber.Value != 0)
                        {
                            serial = new SimpleSerialNumber(keyGenerationFrm1.serialnumber.Value);
                        }


                        backgroundWorker1.ReportProgress(100, 2);

                        // STEP 3
                        if (!configbox.Checked)
                        {
                            ExtensionsWork();
                        }
                        Configuration config = new Configuration(Application.StartupPath + @"\ext.cfg");
                        backgroundWorker1.ReportProgress(100, 3);
                        DateTime notbe = nameFrm1.notbefore.Value;
                        TimeSpan vali  = nameFrm1.notafter.Value.Subtract(notbe);
                        // STEP 4 SAVE
                        X509CertificateAuthority ca = null;
                        if (serial != null)
                        {
                            ca = X509CertificateAuthority.SelfSigned(config, serial,
                                                                     KeyPair, keyb.SignatureAlgorithm, subject, notbe,
                                                                     vali);
                        }
                        else
                        {
                            ca = X509CertificateAuthority.SelfSigned(config, new SimpleSerialNumber(),
                                                                     KeyPair, keyb.SignatureAlgorithm, subject, notbe,
                                                                     vali);
                        }


                        //  cfrm = new CertExportFrm();
                        cfrm.certificate = ca.Certificate;
                        cfrm.Key         = KeyPair;

                        backgroundWorker1.ReportProgress(100, 4);
                    }
                    else
                    {
                        SelectTab(superTabItem5);
                    }
                }
                else
                {
                    SelectTab(superTabItem4);
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Ejemplo n.º 4
0
		static void Main(string[] args)
		{
			Authorities();
			return;

			SimpleSerialNumber seq = new SimpleSerialNumber();
			X509CertificateAuthority ca = X509CertificateAuthority.SelfSigned(
				seq,
				new X509Name("CN=."),
				TimeSpan.FromDays(10)
			);

			Console.WriteLine(ca.Certificate);

			DSA dsa = new DSA(new DSAParameters(512));
			CryptoKey key = new CryptoKey(dsa);
			X509Request req = new X509Request(0, new X509Name("CN=com."), key);
			req.Sign(key, MessageDigest.DSS1);

			X509Certificate cert = ca.ProcessRequest(req, TimeSpan.FromDays(10));
			Console.WriteLine(cert);
			Console.WriteLine("CA Verified: " + cert.Verify(ca.Key));
			Console.WriteLine("Self Verified: " + cert.Verify(key));

			SimpleSerialNumber serial2 = new SimpleSerialNumber();
			X509CertificateAuthority caSelf = new X509CertificateAuthority(
				cert,
				key,
				serial2);

			X509Request req2 = cert.CreateRequest(key, MessageDigest.DSS1);
			X509Name subject = req2.Subject;
			Console.WriteLine("Request1: " + req);
			Console.WriteLine("Request2: " + req2);

			X509Certificate cert2 = caSelf.ProcessRequest(req2, TimeSpan.FromDays(10));
			Console.WriteLine("Cert2: " + cert2);

			DH dh = new DH(128, 5);

			MessageDigestContext mdc = new MessageDigestContext(MessageDigest.DSS1);
			byte[] msg = dh.PublicKey;
			byte[] sig = mdc.Sign(msg, key);

			Console.WriteLine(dh);
			Console.WriteLine("DH P         : " + BitConverter.ToString(dh.P));
			Console.WriteLine("DH G         : " + BitConverter.ToString(dh.G));
			Console.WriteLine("DH Secret Key: " + BitConverter.ToString(dh.PrivateKey));
			Console.WriteLine("DH Public Key: " + BitConverter.ToString(msg));
			Console.WriteLine("DH Signature : " + BitConverter.ToString(sig));

			Console.WriteLine(mdc.Verify(msg, sig, key));
		}