Skip to content

Commit

Permalink
Merge pull request #271 from hwupathum/java21
Browse files Browse the repository at this point in the history
Remove sun.java dependency
  • Loading branch information
hwupathum authored Sep 24, 2024
2 parents 9074789 + ab465d4 commit a2f7aaa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.core.RegistryResources;
import org.wso2.carbon.core.util.CryptoUtil;
Expand All @@ -34,15 +39,6 @@
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ServerConstants;
import org.wso2.carbon.utils.security.KeystoreUtils;
import sun.security.x509.AlgorithmId;
import sun.security.x509.CertificateAlgorithmId;
import sun.security.x509.CertificateSerialNumber;
import sun.security.x509.CertificateValidity;
import sun.security.x509.CertificateVersion;
import sun.security.x509.CertificateX509Key;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
import sun.security.x509.X500Name;

import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
Expand Down Expand Up @@ -185,27 +181,28 @@ private X509Certificate generateKeyPair(KeyStore keyStore) throws KeyStoreMgtExc

//generate certificates
X500Name distinguishedName = new X500Name(commonName);
X509CertInfo x509CertInfo = new X509CertInfo();

Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30);
Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10));

CertificateValidity interval = new CertificateValidity(notBefore, notAfter);
SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded());
BigInteger serialNumber = BigInteger.valueOf(new SecureRandom().nextInt());

x509CertInfo.set(X509CertInfo.VALIDITY, interval);
x509CertInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(serialNumber));
x509CertInfo.set(X509CertInfo.SUBJECT, distinguishedName);
x509CertInfo.set(X509CertInfo.ISSUER, distinguishedName);
x509CertInfo.set(X509CertInfo.KEY, new CertificateX509Key(keyPair.getPublic()));
x509CertInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder(
distinguishedName,
serialNumber,
notBefore,
notAfter,
distinguishedName,
subPubKeyInfo
);

String algorithmName = getSignatureAlgorithm();
AlgorithmId signatureAlgoId = AlgorithmId.get(algorithmName);
x509CertInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(signatureAlgoId));
JcaContentSignerBuilder signerBuilder =
new JcaContentSignerBuilder(algorithmName).setProvider(getJCEProvider());
PrivateKey privateKey = keyPair.getPrivate();
X509CertImpl x509Cert = new X509CertImpl(x509CertInfo);
x509Cert.sign(privateKey, algorithmName, getJCEProvider());
X509Certificate x509Cert = new JcaX509CertificateConverter().setProvider(getJCEProvider())
.getCertificate(certificateBuilder.build(signerBuilder.build(privateKey)));

//add private key to KS
keyStore.setKeyEntry(tenantDomain, keyPair.getPrivate(), password.toCharArray(),
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@

<bcprov-jdk18.version>1.74.0.wso2v1</bcprov-jdk18.version>
<bcpkix-jdk18.version>1.74.0.wso2v1</bcpkix-jdk18.version>
<org.bouncycastle.imp.pkg.version.range>[1.52.0,2.0.0)</org.bouncycastle.imp.pkg.version.range>
<org.bouncycastle.imp.pkg.version.range>[0.0.0,2.0.0)</org.bouncycastle.imp.pkg.version.range>

<!-- Jasper Reports Version -->
<version.jasper>2.2.2.v201205150955</version.jasper>
Expand Down

0 comments on commit a2f7aaa

Please sign in to comment.