Skip to content

Commit

Permalink
set up server cert
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Dec 12, 2023
1 parent a9ebe36 commit 3e8c1ae
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/main/java/io/cryostat/agent/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -44,7 +46,6 @@
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManagerFactory;

import io.cryostat.agent.remote.RemoteContext;
Expand Down Expand Up @@ -118,23 +119,29 @@ void start() throws IOException, NoSuchAlgorithmException {
SSLContext sslContext = SSLContext.getInstance("TLS");

// initialize keystore
FileInputStream passwordFile = new FileInputStream("keystore.pass");
char[] password = new String(passwordFile.readAllBytes()).toCharArray();
passwordFile.close();
FileInputStream passwordFis = new FileInputStream("keystore.pass");
char[] password = new String(passwordFis.readAllBytes()).toCharArray();
passwordFis.close();
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream("cryostat-keystore.p12");
ks.load(fis, password);

FileInputStream keystoreFis = new FileInputStream("cryostat-keystore.p12");
ks.load(keystoreFis, password);

// set up certificate factory
FileInputStream certFis = new FileInputStream("server.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(certFis);
ks.setCertificateEntry("serverCert", cert);

// set up key manager factory
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password);

// set up trust manager factory
// TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// tmf.init(ks);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);

// set up HTTPS context
sslContext.init(kmf.getKeyManagers(), null, null);
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
this.https.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
try {
Expand Down

0 comments on commit 3e8c1ae

Please sign in to comment.