1

I am given a code from my instructor which include this, and I am instructed to add rt.jar to Eclipse which have sun package in it. The goal of this code is to generate a digital certificate.

static public X509Certificate generateCertificate(String dn, PublicKey pubKey, int days) {
    System.out.println("Generating Certificate...");
    String algorithm = "SHA1withRSA";

    try {
        X509CertInfo info = new X509CertInfo();
          Date from = new Date();
          Date to = new Date(from.getTime() + days * 86400000l);
          CertificateValidity interval = new CertificateValidity(from, to);
          BigInteger sn = new BigInteger(64, new SecureRandom());
          X500Name owner = new X500Name(dn);
          X500Name CAName = new X500Name(CADN);

          info.set(X509CertInfo.VALIDITY, interval);
          info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
          info.set(X509CertInfo.SUBJECT, owner);
          info.set(X509CertInfo.ISSUER, CAName);
          info.set(X509CertInfo.KEY, new CertificateX509Key(pubKey));
          info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
          AlgorithmId algo = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
          info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo));

          // Sign the cert to identify the algorithm that's used.
          X509CertImpl cert = new X509CertImpl(info);
          cert.sign(keyCA, algorithm);

          // Update the algorithm, and resign.
          algo = (AlgorithmId)cert.get(X509CertImpl.SIG_ALG);
          info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo);
          cert = new X509CertImpl(info);
          cert.sign(keyCA, algorithm);
          System.out.println("Certificate Generated!");
          return cert;

    } catch(Exception e) {
        System.out.println("Exception in certificate generation.");
        System.out.println("Message: " + e.getMessage());
        e.printStackTrace();
        return null;
    }
}

However, I did ask a question on here before on how to import rt.jar properly, and a user advised me against the sun package per multiple sources. Therefore, my goal is to recreate something look like the code above but without any sun package. I did some researches and that same user advised to use javax.security.cert, but may I know would it be possible to recreate the code above with that package? For example, I have not found anything similar to X509CertInfo yet.

thehollow
  • 17
  • 6
  • 1
    "import `rt.jar` properly" I was under the impression that `rt.jar` was automagically resolved by the JVM, since it contains some rather important classes like `Object` – PiRocks May 01 '20 at 02:06
  • Ah I actually received `rt.jar` from him and was instructed to add that as external jar if I do not have `sun`, which I did not so... – thehollow May 01 '20 at 02:08
  • Oh boy, uh that's what I would describe as "sketch". There's absolutely no guarantee that a random `rt.jar` will work with a random jvm, though you'll probably be fine b/c most stuff is openjdk based. – PiRocks May 01 '20 at 02:11
  • As far as actually answering your question perhaps `javax.security` has what you need : https://docs.oracle.com/javase/8/docs/api/javax/security/cert/X509Certificate.html – PiRocks May 01 '20 at 02:13
  • Haha that is why I want something that is not dependent on `sun` or that `rt.jar`, at least it should be portable with the generals. – thehollow May 01 '20 at 02:13
  • 1
    @PiRocks [10 years ago...](https://stackoverflow.com/a/4070685/2970947). As for doing this without `sun` classes, I suggest [The Legion of the Bouncy Castle](https://bouncycastle.org/java.html) and [this answer](https://stackoverflow.com/a/26782357/2970947). But I'm sure there's more than one solution. – Elliott Frisch May 01 '20 at 02:22
  • Thank you! I will carefully research them! – thehollow May 01 '20 at 02:24

0 Answers0