Search in sources :

Example 6 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project jetty.project by eclipse.

the class SslUploadTest method test.

@Test
@Ignore
public void test() throws Exception {
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    SslContextFactory ctx = connector.getConnectionFactory(SslConnectionFactory.class).getSslContextFactory();
    try (InputStream stream = new FileInputStream(ctx.getKeyStorePath())) {
        keystore.load(stream, "storepwd".toCharArray());
    }
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keystore);
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
    final SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort());
    // Simulate async close
    /*
        new Thread()
        {
            @Override
            public void run()
            {
                try
                {
                    sleep(100);
                    socket.close();
                }
                catch (IOException x)
                {
                    x.printStackTrace();
                }
                catch (InterruptedException x)
                {
                    Thread.currentThread().interrupt();
                }
            }
        }.start();
        */
    long start = System.nanoTime();
    OutputStream out = socket.getOutputStream();
    out.write("POST / HTTP/1.1\r\n".getBytes());
    out.write("Host: localhost\r\n".getBytes());
    out.write("Content-Length: 16777216\r\n".getBytes());
    out.write("Content-Type: bytes\r\n".getBytes());
    out.write("Connection: close\r\n".getBytes());
    out.write("\r\n".getBytes());
    out.flush();
    byte[] requestContent = new byte[16777216];
    Arrays.fill(requestContent, (byte) 120);
    out.write(requestContent);
    out.flush();
    InputStream in = socket.getInputStream();
    String response = IO.toString(in);
    assertTrue(response.indexOf("200") > 0);
    // System.err.println(response);
    // long end = System.nanoTime();
    // System.out.println("upload time: " + TimeUnit.NANOSECONDS.toMillis(end - start));
    assertEquals(requestContent.length, total);
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project jetty.project by eclipse.

the class SSLSelectChannelConnectorLoadTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    String keystorePath = System.getProperty("basedir", ".") + "/src/test/resources/keystore";
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystorePath);
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    sslContextFactory.setTrustStorePath(keystorePath);
    sslContextFactory.setTrustStorePassword("storepwd");
    server = new Server();
    connector = new ServerConnector(server, sslContextFactory);
    server.addConnector(connector);
    server.setHandler(new EmptyHandler());
    server.start();
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (InputStream stream = new FileInputStream(keystorePath)) {
        keystore.load(stream, "storepwd".toCharArray());
    }
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keystore);
    sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) BeforeClass(org.junit.BeforeClass)

Example 8 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project vert.x by eclipse.

the class KeyStoreHelper method getTrustMgrFactory.

public TrustManagerFactory getTrustMgrFactory(VertxInternal vertx) throws Exception {
    TrustManagerFactory fact = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore ts = loadStore(vertx);
    fact.init(ts);
    return fact;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore)

Example 9 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project qi4j-sdk by Qi4j.

the class AbstractSecureJettyTest method beforeSecureClass.

@BeforeClass
public static void beforeSecureClass() throws IOException, GeneralSecurityException {
    defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String string, SSLSession ssls) {
            return true;
        }
    });
    KeyStore truststore = KeyStore.getInstance("JCEKS");
    truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance(getX509Algorithm());
    caTrustManagerFactory.init(truststore);
    sslCtx.init(null, caTrustManagerFactory.getTrustManagers(), null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) BeforeClass(org.junit.BeforeClass)

Example 10 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project ribbon by Netflix.

the class MockHttpServer method before.

public void before(final Description description) throws Exception {
    this.service = Executors.newFixedThreadPool(threadCount, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TestHttpServer-%d").build());
    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);
    if (hasSsl) {
        byte[] sampleTruststore1 = Base64.decode(TEST_TS1);
        byte[] sampleKeystore1 = Base64.decode(TEST_KS1);
        keystore = File.createTempFile("SecureAcceptAllGetTest", ".keystore");
        truststore = File.createTempFile("SecureAcceptAllGetTest", ".truststore");
        FileOutputStream keystoreFileOut = new FileOutputStream(keystore);
        try {
            keystoreFileOut.write(sampleKeystore1);
        } finally {
            keystoreFileOut.close();
        }
        FileOutputStream truststoreFileOut = new FileOutputStream(truststore);
        try {
            truststoreFileOut.write(sampleTruststore1);
        } finally {
            truststoreFileOut.close();
        }
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new FileInputStream(keystore), PASSWORD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, PASSWORD.toCharArray());
        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(new FileInputStream(truststore), PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        HttpsServer secureServer = HttpsServer.create(inetSocketAddress, 0);
        secureServer.setHttpsConfigurator(new HttpsConfigurator(sc) {

            public void configure(HttpsParameters params) {
                SSLContext c = getSSLContext();
                SSLParameters sslparams = c.getDefaultSSLParameters();
                params.setSSLParameters(sslparams);
            }
        });
        server = secureServer;
    } else {
        server = HttpServer.create(inetSocketAddress, 0);
    }
    server.setExecutor(service);
    for (Entry<String, HttpHandler> handler : handlers.entrySet()) {
        server.createContext(handler.getKey(), handler.getValue());
    }
    server.start();
    localHttpServerPort = server.getAddress().getPort();
    System.out.println(description.getClassName() + " TestServer is started: " + getServerUrl());
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) SSLParameters(javax.net.ssl.SSLParameters) FileOutputStream(java.io.FileOutputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpsServer(com.sun.net.httpserver.HttpsServer)

Aggregations

TrustManagerFactory (javax.net.ssl.TrustManagerFactory)196 KeyStore (java.security.KeyStore)118 SSLContext (javax.net.ssl.SSLContext)95 TrustManager (javax.net.ssl.TrustManager)77 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)69 X509TrustManager (javax.net.ssl.X509TrustManager)49 FileInputStream (java.io.FileInputStream)48 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)46 KeyStoreException (java.security.KeyStoreException)39 IOException (java.io.IOException)36 InputStream (java.io.InputStream)31 CertificateException (java.security.cert.CertificateException)24 KeyManagementException (java.security.KeyManagementException)22 X509Certificate (java.security.cert.X509Certificate)21 SecureRandom (java.security.SecureRandom)19 KeyManager (javax.net.ssl.KeyManager)14 GeneralSecurityException (java.security.GeneralSecurityException)13 CertificateFactory (java.security.cert.CertificateFactory)13 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 File (java.io.File)10