use of org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder in project jetty-bootstrap by teknux-org.
the class JettyKeystoreConvertorBuilderTest method do01KeystoreTest.
@Test
public void do01KeystoreTest() throws JettyKeystoreException, InvalidKeyException, KeyStoreException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException {
InputStream inputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/jks/test.jks");
JettyKeystoreConvertorBuilder jettyKeystoreConvertorBuilder = new JettyKeystoreConvertorBuilder().setKeystore(inputStream, "test");
jettyKeystoreConvertorBuilder.checkValidity(true, true);
KeyStore keystore = jettyKeystoreConvertorBuilder.build(KEYSTORE_ALIAS, KEYSTORE_PASSWORD);
JettyKeystoreConvertorBuilder.checkValidity(keystore, KEYSTORE_ALIAS, true, true);
}
use of org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder in project jetty-bootstrap by teknux-org.
the class JettyKeystoreConvertorBuilderTest method do06KeyAndPemTest.
@Test
public void do06KeyAndPemTest() throws JettyKeystoreException, InvalidKeyException, KeyStoreException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException {
InputStream keyInputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/pkcs8/test.key");
InputStream crtInputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/pkcs8/test.pem");
JettyKeystoreConvertorBuilder jettyKeystoreConvertorBuilder = new JettyKeystoreConvertorBuilder().setPrivateKeyFromPKCS8(keyInputStream).setCertificateFromPKCS8(crtInputStream);
jettyKeystoreConvertorBuilder.checkValidity(true, true);
KeyStore keystore = jettyKeystoreConvertorBuilder.build(KEYSTORE_ALIAS, KEYSTORE_PASSWORD);
JettyKeystoreConvertorBuilder.checkValidity(keystore, KEYSTORE_ALIAS, true, true);
}
use of org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder in project jetty-bootstrap by teknux-org.
the class JettyKeystoreConvertorBuilderTest method do10P12AndCrtTest.
@Test
public void do10P12AndCrtTest() throws JettyKeystoreException, InvalidKeyException, KeyStoreException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException {
InputStream keyInputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/pkcs12/test.p12");
InputStream crtInputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/pkcs12/test.crt");
JettyKeystoreConvertorBuilder jettyKeystoreConvertorBuilder = new JettyKeystoreConvertorBuilder().setPrivateKeyFromPKCS12(keyInputStream, "test").setCertificateFromPKCS8(crtInputStream);
jettyKeystoreConvertorBuilder.checkValidity(true, true);
KeyStore keystore = jettyKeystoreConvertorBuilder.build(KEYSTORE_ALIAS, KEYSTORE_PASSWORD);
JettyKeystoreConvertorBuilder.checkValidity(keystore, KEYSTORE_ALIAS, true, true);
}
use of org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder in project jetty-bootstrap by teknux-org.
the class JettyKeystoreConvertorBuilderTest method do03KeystoreAndCrtTest.
@Test
public void do03KeystoreAndCrtTest() throws JettyKeystoreException, InvalidKeyException, KeyStoreException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException {
InputStream keyInputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/jks/test.jks");
InputStream crtInputStream = getClass().getResourceAsStream("/org/teknux/jettybootstrap/test/keystore/jks/test.crt");
JettyKeystoreConvertorBuilder jettyKeystoreConvertorBuilder = new JettyKeystoreConvertorBuilder().setPrivateKeyFromKeystore(keyInputStream, "test").setCertificateFromPKCS8(crtInputStream);
jettyKeystoreConvertorBuilder.checkValidity(true, true);
KeyStore keystore = jettyKeystoreConvertorBuilder.build(KEYSTORE_ALIAS, KEYSTORE_PASSWORD);
JettyKeystoreConvertorBuilder.checkValidity(keystore, KEYSTORE_ALIAS, true, true);
}
use of org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder in project jetty-bootstrap by teknux-org.
the class JettyBootstrap method createConnectors.
/**
* Creates and returns the necessary {@link ServerConnector} based on the given {@link IJettyConfiguration}.
*
* @param iJettyConfiguration
* Jetty Configuration
* @param server
* the server to
* @return Connector[]
* @throws JettyBootstrapException
*/
protected Connector[] createConnectors(IJettyConfiguration iJettyConfiguration, Server server) throws JettyBootstrapException {
LOG.trace("Creating Jetty Connectors...");
List<Connector> connectors = new ArrayList<>();
if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTP)) {
LOG.trace("Adding HTTP Connector...");
ServerConnector serverConnector;
if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTPS)) {
HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration.setSecurePort(iJettyConfiguration.getSslPort());
httpConfiguration.setSecureScheme(HttpScheme.HTTPS.asString());
httpConfiguration.setIdleTimeout(iJettyConfiguration.getIdleTimeout());
httpConfiguration.setBlockingTimeout(iJettyConfiguration.getBlockingTimeout());
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConfiguration);
serverConnector = new ServerConnector(server, httpConnectionFactory);
} else {
serverConnector = new ServerConnector(server);
serverConnector.getConnectionFactories().stream().filter(HttpConnectionFactory.class::isInstance).map(HttpConnectionFactory.class::cast).forEach(httpConnectionFactory -> httpConnectionFactory.getHttpConfiguration().setBlockingTimeout(iJettyConfiguration.getBlockingTimeout()));
}
serverConnector.setIdleTimeout(iJettyConfiguration.getIdleTimeout());
serverConnector.setHost(iJettyConfiguration.getHost());
serverConnector.setPort(iJettyConfiguration.getPort());
connectors.add(serverConnector);
}
if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTPS)) {
LOG.trace("Adding HTTPS Connector...");
SslContextFactory sslContextFactory = new SslContextFactory();
if (iJettyConfiguration.getSslKeyStore() != null) {
// Use keyStore object if available
sslContextFactory.setKeyStore(iJettyConfiguration.getSslKeyStore());
} else if (iJettyConfiguration.getSslPrivateKeyPath() != null && !iJettyConfiguration.getSslPrivateKeyPath().isEmpty() && iJettyConfiguration.getSslCertificatePath() != null && !iJettyConfiguration.getSslCertificatePath().isEmpty()) {
// Use private key and certificate if available
JettyKeystoreConvertorBuilder jettyKeystoreConvertorBuilder = new JettyKeystoreConvertorBuilder();
try {
File sslPrivateKeyFile = new File(iJettyConfiguration.getSslPrivateKeyPath());
if (!sslPrivateKeyFile.exists() || !sslPrivateKeyFile.canRead()) {
throw new JettyBootstrapException("Private key not exists or unreadable");
}
File sslCertificateFile = new File(iJettyConfiguration.getSslCertificatePath());
if (!sslCertificateFile.exists() || !sslCertificateFile.canRead()) {
throw new JettyBootstrapException("Certificate not exists or unreadable");
}
try (InputStream sslPrivateKeyInputStream = new FileInputStream(sslPrivateKeyFile);
InputStream sslCertificateInputStream = new FileInputStream(sslCertificateFile)) {
switch(iJettyConfiguration.getSslCertificateFormat()) {
case PKCS8:
jettyKeystoreConvertorBuilder.setCertificateFromPKCS8(sslCertificateInputStream);
break;
case PKCS12:
jettyKeystoreConvertorBuilder.setCertificateFromPKCS12(sslCertificateInputStream, iJettyConfiguration.getSslCertificatePassword());
break;
case UNKNOWN:
throw new JettyBootstrapException("Unknown Certificate Format");
default:
throw new JettyBootstrapException("Certificate Format not setted");
}
switch(iJettyConfiguration.getSslPrivateKeyFormat()) {
case PKCS8:
jettyKeystoreConvertorBuilder.setPrivateKeyFromPKCS8(sslPrivateKeyInputStream);
break;
case PKCS12:
jettyKeystoreConvertorBuilder.setPrivateKeyFromPKCS12(sslPrivateKeyInputStream, iJettyConfiguration.getSslPrivateKeyPassword());
break;
case UNKNOWN:
throw new JettyBootstrapException("Unknown Private key Format");
default:
throw new JettyBootstrapException("Private key Format not setted");
}
}
KeyStore keyStore = jettyKeystoreConvertorBuilder.build(iJettyConfiguration.getSslKeyStoreAlias(), iJettyConfiguration.getSslKeyStorePassword());
sslContextFactory.setKeyStore(keyStore);
} catch (JettyKeystoreException | IOException e) {
throw new JettyBootstrapException("Can not load SSL private key or SSL certificate", e);
}
} else {
// Use keystore path
sslContextFactory.setKeyStorePath(iJettyConfiguration.getSslKeyStorePath());
}
sslContextFactory.setKeyStorePassword(iJettyConfiguration.getSslKeyStorePassword());
ServerConnector serverConnector = new ServerConnector(server, sslContextFactory);
serverConnector.setIdleTimeout(iJettyConfiguration.getIdleTimeout());
serverConnector.setHost(iJettyConfiguration.getHost());
serverConnector.setPort(iJettyConfiguration.getSslPort());
connectors.add(serverConnector);
}
return connectors.toArray(new Connector[connectors.size()]);
}
Aggregations