/** * Returns the VM's default SSL socket factory, using {@code trustManager} for trusted root * certificates. */ private SSLSocketFactory defaultSslSocketFactory(X509TrustManager trustManager) throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { trustManager }, null); return sslContext.getSocketFactory(); }
public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException { if (certificates.length == 0) { throw new SSLPeerUnverifiedException("peer not authenticated"); } else { return certificates; } }
@Override public String[] getDefaultCipherSuites() { return delegate.getDefaultCipherSuites(); }
private void processHandshakeFailure(Socket raw) throws Exception { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[] {UNTRUSTED_TRUST_MANAGER}, new SecureRandom()); SSLSocketFactory sslSocketFactory = context.getSocketFactory(); SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket( raw, raw.getInetAddress().getHostAddress(), raw.getPort(), true); try { socket.startHandshake(); // we're testing a handshake failure throw new AssertionError(); } catch (IOException expected) { } socket.close(); }
public SSLContext getSSLContext() { try { return SSLContext.getInstance("TLS"); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("No TLS provider", e); } }
public void run() throws IOException { MockWebServer server = new MockWebServer(); server.useHttps(sslContext.getSocketFactory(), false); server.setDispatcher(this); server.start(port); }
@Override public Socket createSocket( InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { return configureSocket((SSLSocket) delegate.createSocket( address, port, localAddress, localPort)); }
protected int netReadBufferSize() { return sslEngine.getSession().getPacketBufferSize(); }
/** Applies this spec to {@code sslSocket}. */ void apply(SSLSocket sslSocket, boolean isFallback) { ConnectionSpec specToApply = supportedSpec(sslSocket, isFallback); if (specToApply.tlsVersions != null) { sslSocket.setEnabledProtocols(specToApply.tlsVersions); } if (specToApply.cipherSuites != null) { sslSocket.setEnabledCipherSuites(specToApply.cipherSuites); } }
protected int applicationBufferSize() { return sslEngine.getSession().getApplicationBufferSize(); }
/** * returns a SSL Session after the handshake is established * throws IllegalStateException if the handshake is not established */ public SSLSession sslSession() throws IllegalStateException { return sslEngine.getSession(); }
void close() { sslEngine.closeOutbound(); try { sslEngine.closeInbound(); } catch (Exception e) { // ignore } } }
@Override public boolean verify(String host, SSLSession session) { try { Certificate[] certificates = session.getPeerCertificates(); return verify(host, (X509Certificate) certificates[0]); } catch (SSLException e) { return false; } }
@Override public String[] getSupportedCipherSuites() { return delegate.getSupportedCipherSuites(); }
/** * Creates a new server using the default SSL context. * @param lc logger context for received events * @param port port on which the server is to listen * @throws NoSuchAlgorithmException if the default SSL context cannot be * created */ public SimpleSSLSocketServer(LoggerContext lc, int port) throws NoSuchAlgorithmException { this(lc, port, SSLContext.getDefault()); }
@Override protected SSLSocket configureSocket(SSLSocket socket) throws IOException { socket.setEnabledCipherSuites(javaNames(spec.cipherSuites())); return socket; } };
/** * returns delegatedTask for the SSLEngine. */ protected Runnable delegatedTask() { return sslEngine.getDelegatedTask(); }
@Override public Socket createSocket( Socket socket, String host, int port, boolean autoClose) throws IOException { return configureSocket((SSLSocket) delegate.createSocket(socket, host, port, autoClose)); }
protected int netWriteBufferSize() { return sslEngine.getSession().getPacketBufferSize(); }
@Override public Socket createSocket( String host, int port, InetAddress localHost, int localPort) throws IOException { return configureSocket((SSLSocket) delegate.createSocket(host, port, localHost, localPort)); }