Code Monkey home page Code Monkey logo

Comments (2)

lapo-luchini avatar lapo-luchini commented on July 17, 2024 1

Full example:

import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;

import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.KeyUsage;
import org.bouncycastle.asn1.x509.X509Extensions;
import org.bouncycastle.jce.X509Principal;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.X509CertParser;
import org.bouncycastle.x509.X509V3CertificateGenerator;

public class TestBCBug2 {

    public static void main(String[] args) throws Exception {
        KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
        gen.initialize(1024);
        KeyPair kp = gen.generateKeyPair();
        PublicKey pubkey = kp.getPublic();
        PrivateKey privkey = kp.getPrivate();
        X509V3CertificateGenerator g = new X509V3CertificateGenerator();
        g.setSerialNumber(BigInteger.valueOf(7));
        g.setSubjectDN(new X509Principal("CN=Subject"));
        g.setIssuerDN(new X509Principal("CN=Issuer"));
        g.setSignatureAlgorithm("SHA256withRSA");
        g.setNotAfter(new Date());
        g.setNotBefore(new Date());
        g.setPublicKey(pubkey);
        g.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(true).getEncoded());
        g.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.keyCertSign).getEncoded());
        X509Certificate c = g.generate(privkey);
        System.out.println("PRE-LOAD");
        System.out.println("Certificate: " + c.getClass().getName());
        System.out.println("Public key: " + c.getPublicKey()); // this is null
        System.out.println();
        {
            X509CertParser p = new X509CertParser();
            p.engineInit(new ByteArrayInputStream(c.getEncoded()));
            c = (X509Certificate) p.engineRead();
        }
        System.out.println("DECODED by BC");
        System.out.println("Certificate: " + c.getClass().getName());
        System.out.println("Public key: " + c.getPublicKey()); // this is null
        System.out.println();
        c = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(c.getEncoded()));
        System.out.println("DECODED by Java");
        System.out.println("Certificate: " + c.getClass().getName());
        System.out.println("Public key: " + c.getPublicKey()); // this is valid data
        System.out.println();
        new BouncyCastleProvider(); // now we do it properly
        c = g.generate(privkey);
        System.out.println("POST-LOAD");
        System.out.println("Certificate: " + c.getClass().getName());
        System.out.println("Public key: " + c.getPublicKey()); // this is valid data
    }

}

from bc-java.

 avatar commented on July 17, 2024

I think I've fixed this, but my advice is not to use the X509V3CertificateGenerator as the class will disappear.

from bc-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.