Esempio 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));
        }
        public void TestGenCSR()
        {
            var pem = File.ReadAllText("openssl-rsagen-privatekey.txt");
            var rsa = CryptoKey.FromPrivateKey(pem, null);
            //pem = File.ReadAllText("openssl-rsagen-publickey.txt");
            //rsa = CryptoKey.FromPublicKey(pem, null);

            var nam = new X509Name();

            nam.Common  = "FOOBAR";
            nam.Country = "US";



            var csr = new X509Request();

            csr.PublicKey = rsa;
            csr.Subject   = nam;
            csr.Sign(rsa, MessageDigest.SHA256);

            File.WriteAllText("openssl-requ-csr.txt", csr.PEM);
            using (var bioOut = BIO.MemoryBuffer())
            {
                csr.Write_DER(bioOut);
                var arr = bioOut.ReadBytes((int)bioOut.BytesPending);

                File.WriteAllBytes("openssl-requ-csr.der", arr.Array);
            }

            //using (var bioIn = BIO.MemoryBuffer())
            //{
            //    var pem2 = File.ReadAllText("openssl-requ-csr.txt");
            //    bioIn.Write(pem2);

            //    var csr = new X509Request()
            //    var x509 = new X509Certificate(bioIn);

            //}
        }
Esempio n. 3
0
        private Csr GenerateCsr(CsrDetails csrDetails, RsaPrivateKey rsaKeyPair, string messageDigest = "SHA256")
        {
            var rsaKeys = CryptoKey.FromPrivateKey(rsaKeyPair.Pem, null);

            // Translate from our external form to our OpenSSL internal form
            // Ref:  https://www.openssl.org/docs/manmaster/crypto/X509_NAME_new.html
            var xn = new X509Name();

            if (!string.IsNullOrEmpty(csrDetails.CommonName /**/))
            {
                xn.Common = csrDetails.CommonName;                                                                  // CN;
            }
            if (!string.IsNullOrEmpty(csrDetails.Country /**/))
            {
                xn.Country = csrDetails.Country;                                                                     // C;
            }
            if (!string.IsNullOrEmpty(csrDetails.StateOrProvince /**/))
            {
                xn.StateOrProvince = csrDetails.StateOrProvince;                                                             // ST;
            }
            if (!string.IsNullOrEmpty(csrDetails.Locality /**/))
            {
                xn.Locality = csrDetails.Locality;                                                                    // L;
            }
            if (!string.IsNullOrEmpty(csrDetails.Organization /**/))
            {
                xn.Organization = csrDetails.Organization;                                                                // O;
            }
            if (!string.IsNullOrEmpty(csrDetails.OrganizationUnit /**/))
            {
                xn.OrganizationUnit = csrDetails.OrganizationUnit;                                                            // OU;
            }
            if (!string.IsNullOrEmpty(csrDetails.Description /**/))
            {
                xn.Description = csrDetails.Description;                                                                 // D;
            }
            if (!string.IsNullOrEmpty(csrDetails.Surname /**/))
            {
                xn.Surname = csrDetails.Surname;                                                                     // S;
            }
            if (!string.IsNullOrEmpty(csrDetails.GivenName /**/))
            {
                xn.Given = csrDetails.GivenName;                                                                   // G;
            }
            if (!string.IsNullOrEmpty(csrDetails.Initials /**/))
            {
                xn.Initials = csrDetails.Initials;                                                                    // I;
            }
            if (!string.IsNullOrEmpty(csrDetails.Title /**/))
            {
                xn.Title = csrDetails.Title;                                                                       // T;
            }
            if (!string.IsNullOrEmpty(csrDetails.SerialNumber /**/))
            {
                xn.SerialNumber = csrDetails.SerialNumber;                                                                // SN;
            }
            if (!string.IsNullOrEmpty(csrDetails.UniqueIdentifier /**/))
            {
                xn.UniqueIdentifier = csrDetails.UniqueIdentifier;                                                            // UID;
            }
            var xr = new X509Request(0, xn, rsaKeys);
            var md = MessageDigest.CreateByName(messageDigest);

            xr.Sign(rsaKeys, md);
            using (var bio = BIO.MemoryBuffer())
            {
                xr.Write(bio);
                return(new Csr(bio.ReadString()));
            }
        }
Esempio n. 4
0
        protected Csr GenerateCsr(CsrDetails csrDetails, RsaPrivateKey rsaKeyPair, string messageDigest = "SHA256")
        {
            var rsaKeys = CryptoKey.FromPrivateKey(rsaKeyPair.Pem, null);

            // Translate from our external form to our OpenSSL internal form
            // Ref:  https://www.openssl.org/docs/manmaster/crypto/X509_NAME_new.html
            var xn = new X509Name();

            if (!string.IsNullOrEmpty(csrDetails.CommonName /**/))
            {
                xn.Common = csrDetails.CommonName;                                                                  // CN;
            }
            if (!string.IsNullOrEmpty(csrDetails.Country /**/))
            {
                xn.Country = csrDetails.Country;                                                                     // C;
            }
            if (!string.IsNullOrEmpty(csrDetails.StateOrProvince /**/))
            {
                xn.StateOrProvince = csrDetails.StateOrProvince;                                                             // ST;
            }
            if (!string.IsNullOrEmpty(csrDetails.Locality /**/))
            {
                xn.Locality = csrDetails.Locality;                                                                    // L;
            }
            if (!string.IsNullOrEmpty(csrDetails.Organization /**/))
            {
                xn.Organization = csrDetails.Organization;                                                                // O;
            }
            if (!string.IsNullOrEmpty(csrDetails.OrganizationUnit /**/))
            {
                xn.OrganizationUnit = csrDetails.OrganizationUnit;                                                            // OU;
            }
            if (!string.IsNullOrEmpty(csrDetails.Description /**/))
            {
                xn.Description = csrDetails.Description;                                                                 // D;
            }
            if (!string.IsNullOrEmpty(csrDetails.Surname /**/))
            {
                xn.Surname = csrDetails.Surname;                                                                     // S;
            }
            if (!string.IsNullOrEmpty(csrDetails.GivenName /**/))
            {
                xn.Given = csrDetails.GivenName;                                                                   // G;
            }
            if (!string.IsNullOrEmpty(csrDetails.Initials /**/))
            {
                xn.Initials = csrDetails.Initials;                                                                    // I;
            }
            if (!string.IsNullOrEmpty(csrDetails.Title /**/))
            {
                xn.Title = csrDetails.Title;                                                                       // T;
            }
            if (!string.IsNullOrEmpty(csrDetails.SerialNumber /**/))
            {
                xn.SerialNumber = csrDetails.SerialNumber;                                                                // SN;
            }
            if (!string.IsNullOrEmpty(csrDetails.UniqueIdentifier /**/))
            {
                xn.UniqueIdentifier = csrDetails.UniqueIdentifier;                                                            // UID;
            }
            var xr = new X509Request(0, xn, rsaKeys);

            if (csrDetails.AlternativeNames != null)
            {
                // Format the common name as the first alternative name
                var commonName = $"{EXT_SAN_PREFIX_DNS}:{xn.Common}";

                // Concat with all subsequent alternative names
                var altNames = commonName + string.Join("", csrDetails.AlternativeNames.Select(
                                                            x => $",{EXT_SAN_PREFIX_DNS}:{x}"));

                // Assemble and add the SAN extension value
                var extensions = new OpenSSL.Core.Stack <X509Extension>();
                extensions.Add(new X509Extension(xr, EXT_NAME_SAN, false, altNames));
                xr.AddExtensions(extensions);
            }

            var md = MessageDigest.CreateByName(messageDigest);

            xr.Sign(rsaKeys, md);
            using (var bio = BIO.MemoryBuffer())
            {
                xr.Write(bio);
                return(new Csr(bio.ReadString()));
            }
        }
Esempio n. 5
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));
		}