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.