Esempio n. 1
0
        private static X509Certificate createCertificate(
            AsymmetricCipherKeyPair pair,
            string country,
            string organization,
            string locality,
            string state,
            string emailAddress,
            string commonName,
            BigInteger serialNumber,
            AsymmetricCipherKeyPair signer)
        {
            Hashtable attrs = new Hashtable();

            attrs.Add(X509Name.C, country);                     // Country
            attrs.Add(X509Name.O, organization);                // Organization
            attrs.Add(X509Name.L, locality);                    // Locality
            attrs.Add(X509Name.ST, state);                      // State/Province
            attrs.Add(X509Name.EmailAddress, emailAddress);
            attrs.Add(X509Name.CN, commonName);                 // Common Name

            // Create a certificate
            X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();

            certGen.setSerialNumber(serialNumber);
            certGen.setIssuerDN(new X509Name(attrs));
            certGen.setNotBefore(DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0)));
            certGen.setNotAfter(DateTime.Today.AddDays(365));
            certGen.setSubjectDN(new X509Name(attrs));
            certGen.setPublicKey(pair.getPublic());
            certGen.setSignatureAlgorithm("SHA1WithRSAEncryption");

            return(certGen.generateX509Certificate(signer.getPrivate()));
        }
Esempio n. 2
0
        private X509Certificate generateCertificate(String country,
                                                    String organization, String locality, String stateOrProvince,
                                                    String emailAddress, String commonName, RSAKeyParameters privateKey,
                                                    RSAKeyParameters publicKey)
        {
            string     dateString   = DateTime.Now.ToString("yyyyMMddHHmmssfff", DateTimeFormatInfo.InvariantInfo);
            BigInteger serialNumber = new BigInteger(dateString);

            DateTime notBefore = DateTime.Today.AddDays(-1);
            DateTime notAfter  = DateTime.Today.AddDays(730);

            Hashtable attrs = new Hashtable();

            attrs.Add(X509Name.C, country);             // Country
            attrs.Add(X509Name.O, organization);        // Organization
            attrs.Add(X509Name.L, locality);            // Locality
            attrs.Add(X509Name.ST, stateOrProvince);    // State/Province
            attrs.Add(X509Name.EmailAddress, emailAddress);
            attrs.Add(X509Name.CN, commonName);         // Common Name

            // Create a certificate
            X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();

            certGen.setSerialNumber(serialNumber);
            certGen.setIssuerDN(new X509Name(attrs));
            certGen.setNotBefore(notBefore);
            certGen.setNotAfter(notAfter);
            certGen.setSubjectDN(new X509Name(attrs));
            certGen.setPublicKey(publicKey);
            certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

            X509Certificate certificate = certGen
                                          .generateX509Certificate(privateKey);

            return(certificate);
        }