Self signed X509 Certificate with Bouncy Castle in Java

后端 未结 4 618
旧时难觅i
旧时难觅i 2020-11-27 18:43

I need to create a self signed X509 Certificate with Bouncy Castle in Java, but every class I try to include is deprecated. How can I solve this? Is there some other class

4条回答
  •  有刺的猬
    2020-11-27 19:08

    This is the code used from BouncyCastle it self to generate X.509 Certificates. You will need this and this library from BC to use it. For further details on how to use it, maybe take a look at this question (Main class).

        public class BCCertGen {
        public static String _country = "Westeros",
                             _organisation = "Targaryen",
                             _location = "Valyria",
                             _state = "Essos",
                             _issuer = "Some Trusted CA";
    
        public BCCertGen(String country, String organisation, String location, String state, String issuer){
            _country = country;
            _organisation = organisation;
            _location = location;
            _state = state;
            _issuer = issuer;
        }
        public static X509Certificate generate(PrivateKey privKey, PublicKey pubKey, int duration, String signAlg, boolean isSelfSigned) throws Exception{
            Provider BC = new BouncyCastleProvider();
    
            // distinguished name table.
            X500NameBuilder builder = createStdBuilder();
    
            // create the certificate
            ContentSigner sigGen = new JcaContentSignerBuilder(signAlg).build(privKey);
            X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(
                    new X500Name("cn="+_issuer),    //Issuer
                    BigInteger.valueOf(1),      //Serial
                    new Date(System.currentTimeMillis() - 50000),   //Valid from
                    new Date((long)(System.currentTimeMillis() + duration*8.65*Math.pow(10,7))),    //Valid to
                    builder.build(),    //Subject
                    pubKey              //Publickey to be associated with the certificate
            );
    
            X509Certificate cert = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certGen.build(sigGen));
    
            cert.checkValidity(new Date());
    
            if (isSelfSigned) {
                // check verifies in general
                cert.verify(pubKey);
                // check verifies with contained key
                cert.verify(cert.getPublicKey());
            }
    
            ByteArrayInputStream bIn = new ByteArrayInputStream(cert.getEncoded());
            CertificateFactory fact = CertificateFactory.getInstance("X.509", BC);
    
            return (X509Certificate) fact.generateCertificate(bIn);
        }
    
        private static X500NameBuilder createStdBuilder() {
            X500NameBuilder builder = new X500NameBuilder(RFC4519Style.INSTANCE);
    
            builder.addRDN(RFC4519Style.c, _country);
            builder.addRDN(RFC4519Style.o, _organisation);
            builder.addRDN(RFC4519Style.l, _location);
            builder.addRDN(RFC4519Style.st, _state);
    
            return builder;
        }
    }
    

    EDIT: I can't remember from which BC test I took it exactly, but here is something similar https://github.com/bcgit/bc-java/blob/master/prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/KeyStoreTest.java

提交回复
热议问题