Skip to content

Commit

Permalink
why no build?
Browse files Browse the repository at this point in the history
  • Loading branch information
codybum committed Nov 10, 2023
1 parent 7cf0a41 commit e69b962
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
4 changes: 1 addition & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<maven.compiler.version>3.8.1</maven.compiler.version>
<maven.bundle.compiler.version>4.2.1</maven.bundle.compiler.version>
<jetty.version>9.4.18.v20190429</jetty.version>
<bouncycastle.version>1.61</bouncycastle.version>
<bouncycastle.version>1.66</bouncycastle.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -306,8 +306,6 @@
<scope>provided</scope>
</dependency>



</dependencies>


Expand Down
77 changes: 71 additions & 6 deletions src/main/java/io/cresco/wsapi/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.x509.X509V1CertificateGenerator;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.FilterHolder;
Expand All @@ -30,15 +31,14 @@
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.*;

import javax.security.auth.x500.X500Principal;
import javax.servlet.DispatcherType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.*;
import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

Expand Down Expand Up @@ -248,7 +248,9 @@ private KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProvide
kpGen.initialize(1024, new SecureRandom());
return kpGen.generateKeyPair();
}
private void generateCertChainKeyStore(Path kyStorePath) {


private void generateCertChainKeyStore_old(Path kyStorePath) {

try {

Expand All @@ -269,9 +271,9 @@ private void generateCertChainKeyStore(Path kyStorePath) {

String agentName = "wsapi-" + UUID.randomUUID().toString();

//start gen
KeyPair rootCAKeyPair = generateKeyPair();


X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(
new X500Name("CN=rootCA-" + agentName), // issuer authority
BigInteger.valueOf(new Random().nextInt()), //serial number of certificate
Expand All @@ -289,6 +291,7 @@ private void generateCertChainKeyStore(Path kyStorePath) {

X509Certificate[] chain = new X509Certificate[1];
chain[0]=rootCA;
//

ks.setKeyEntry("wsapi", rootCAKeyPair.getPrivate(), password, chain);

Expand All @@ -303,6 +306,68 @@ private void generateCertChainKeyStore(Path kyStorePath) {

}

private void generateCertChainKeyStore(Path kyStorePath) {

try {

Path pluginDataDir = Paths.get(pluginBuilder.getPluginDataDirectory());
if (!pluginDataDir.toFile().exists()) {

pluginDataDir.toFile().mkdirs();

}

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

char[] password = "cresco".toCharArray();
ks.load(null, password);


// Create self signed Root CA certificate

String agentName = "wsapi-" + UUID.randomUUID().toString();

// yesterday
Date validityBeginDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
// in 2 years
Date validityEndDate = new Date(System.currentTimeMillis() + 2 * 365 * 24 * 60 * 60 * 1000);

// GENERATE THE PUBLIC/PRIVATE RSA KEY PAIR
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(1024, new SecureRandom());

java.security.KeyPair keyPair = keyPairGenerator.generateKeyPair();

// GENERATE THE X509 CERTIFICATE
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
X500Principal dnName = new X500Principal("CN=rootCA-" + agentName);

certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setSubjectDN(dnName);
certGen.setIssuerDN(dnName); // use the same
certGen.setNotBefore(validityBeginDate);
certGen.setNotAfter(validityEndDate);
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

X509Certificate cert = certGen.generate(keyPair.getPrivate(), "BC");

X509Certificate[] chain = new X509Certificate[1];
chain[0]=cert;

ks.setKeyEntry("wsapi", keyPair.getPrivate(), password, chain);

// Store away the keystore.
FileOutputStream fos = new FileOutputStream(kyStorePath.toString());
ks.store(fos, password);
fos.close();

} catch (Exception ex) {
ex.printStackTrace();
}

}


@Override
public boolean isStopped() {
Expand Down

0 comments on commit e69b962

Please sign in to comment.