Search in sources :

Example 46 with SSLSession

use of javax.net.ssl.SSLSession in project XobotOS by xamarin.

the class AbstractSessionContext method setSessionTimeout.

public void setSessionTimeout(int seconds) throws IllegalArgumentException {
    if (seconds < 0) {
        throw new IllegalArgumentException("seconds < 0");
    }
    timeout = seconds;
    synchronized (sessions) {
        Iterator<SSLSession> i = sessions.values().iterator();
        while (i.hasNext()) {
            SSLSession session = i.next();
            // timeout as part of their validity condition.
            if (!session.isValid()) {
                i.remove();
                sessionRemoved(session);
            }
        }
    }
}
Also used : SSLSession(javax.net.ssl.SSLSession)

Example 47 with SSLSession

use of javax.net.ssl.SSLSession in project XobotOS by xamarin.

the class AbstractSessionContext method getIds.

public final Enumeration getIds() {
    final Iterator<SSLSession> i = sessionIterator();
    return new Enumeration<byte[]>() {

        private SSLSession next;

        public boolean hasMoreElements() {
            if (next != null) {
                return true;
            }
            while (i.hasNext()) {
                SSLSession session = i.next();
                if (session.isValid()) {
                    next = session;
                    return true;
                }
            }
            next = null;
            return false;
        }

        public byte[] nextElement() {
            if (hasMoreElements()) {
                byte[] id = next.getId();
                next = null;
                return id;
            }
            throw new NoSuchElementException();
        }
    };
}
Also used : Enumeration(java.util.Enumeration) SSLSession(javax.net.ssl.SSLSession) NoSuchElementException(java.util.NoSuchElementException)

Example 48 with SSLSession

use of javax.net.ssl.SSLSession in project XobotOS by xamarin.

the class AbstractVerifier method verify.

public final void verify(String host, SSLSocket ssl) throws IOException {
    if (host == null) {
        throw new NullPointerException("host to verify is null");
    }
    SSLSession session = ssl.getSession();
    Certificate[] certs = session.getPeerCertificates();
    X509Certificate x509 = (X509Certificate) certs[0];
    verify(host, x509);
}
Also used : SSLSession(javax.net.ssl.SSLSession) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 49 with SSLSession

use of javax.net.ssl.SSLSession in project XobotOS by xamarin.

the class CertificateChainValidator method closeSocketThrowException.

private void closeSocketThrowException(SSLSocket socket, String errorMessage) throws IOException {
    if (HttpLog.LOGV) {
        HttpLog.v("validation error: " + errorMessage);
    }
    if (socket != null) {
        SSLSession session = socket.getSession();
        if (session != null) {
            session.invalidate();
        }
        socket.close();
    }
    throw new SSLHandshakeException(errorMessage);
}
Also used : SSLSession(javax.net.ssl.SSLSession) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 50 with SSLSession

use of javax.net.ssl.SSLSession in project XobotOS by xamarin.

the class SSLCertificateSocketFactory method verifyHostname.

/**
     * Verify the hostname of the certificate used by the other end of a
     * connected socket.  You MUST call this if you did not supply a hostname
     * to {@link #createSocket()}.  It is harmless to call this method
     * redundantly if the hostname has already been verified.
     *
     * <p>Wildcard certificates are allowed to verify any matching hostname,
     * so "foo.bar.example.com" is verified if the peer has a certificate
     * for "*.example.com".
     *
     * @param socket An SSL socket which has been connected to a server
     * @param hostname The expected hostname of the remote server
     * @throws IOException if something goes wrong handshaking with the server
     * @throws SSLPeerUnverifiedException if the server cannot prove its identity
     *
     * @hide
     */
public static void verifyHostname(Socket socket, String hostname) throws IOException {
    if (!(socket instanceof SSLSocket)) {
        throw new IllegalArgumentException("Attempt to verify non-SSL socket");
    }
    if (!isSslCheckRelaxed()) {
        // The code at the start of OpenSSLSocketImpl.startHandshake()
        // ensures that the call is idempotent, so we can safely call it.
        SSLSocket ssl = (SSLSocket) socket;
        ssl.startHandshake();
        SSLSession session = ssl.getSession();
        if (session == null) {
            throw new SSLException("Cannot verify SSL socket without session");
        }
        if (!HOSTNAME_VERIFIER.verify(hostname, session)) {
            throw new SSLPeerUnverifiedException("Cannot verify hostname: " + hostname);
        }
    }
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) SSLSession(javax.net.ssl.SSLSession) SSLException(javax.net.ssl.SSLException)

Aggregations

SSLSession (javax.net.ssl.SSLSession)171 HostnameVerifier (javax.net.ssl.HostnameVerifier)41 SSLSocket (javax.net.ssl.SSLSocket)31 Test (org.junit.Test)28 X509Certificate (java.security.cert.X509Certificate)25 IOException (java.io.IOException)23 CertificateException (java.security.cert.CertificateException)23 SSLContext (javax.net.ssl.SSLContext)23 SSLException (javax.net.ssl.SSLException)17 Certificate (java.security.cert.Certificate)16 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)14 X509TrustManager (javax.net.ssl.X509TrustManager)13 FakeSSLSession (okhttp3.FakeSSLSession)13 TrustManager (javax.net.ssl.TrustManager)11 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)10 SecureRandom (java.security.SecureRandom)9 SSLEngine (javax.net.ssl.SSLEngine)9 Socket (java.net.Socket)8 URL (java.net.URL)8 ByteBuffer (java.nio.ByteBuffer)8