Search in sources :

Example 1 with JettyKeystoreConvertorBuilder

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);
}
Also used : JettyKeystoreConvertorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder) InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Example 2 with JettyKeystoreConvertorBuilder

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);
}
Also used : JettyKeystoreConvertorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder) InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Example 3 with JettyKeystoreConvertorBuilder

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);
}
Also used : JettyKeystoreConvertorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder) InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Example 4 with JettyKeystoreConvertorBuilder

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);
}
Also used : JettyKeystoreConvertorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder) InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Example 5 with JettyKeystoreConvertorBuilder

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()]);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) JettyConnector(org.teknux.jettybootstrap.configuration.JettyConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) JettyKeystoreConvertorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) File(java.io.File)

Aggregations

InputStream (java.io.InputStream)11 KeyStore (java.security.KeyStore)11 JettyKeystoreConvertorBuilder (org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder)11 Test (org.junit.Test)10 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Connector (org.eclipse.jetty.server.Connector)1 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)1 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)1 JettyConnector (org.teknux.jettybootstrap.configuration.JettyConnector)1