Java Code Examples for javax.net.ssl.SSLSession#invalidate()

The following examples show how to use javax.net.ssl.SSLSession#invalidate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SslTcpConnectorHandshaker.java    From linstor-server with GNU General Public License v3.0 6 votes vote down vote up
public SslTcpConnectorHandshaker(
    SslTcpConnectorPeer peerRef,
    SSLEngine sslEngine,
    HandshakeFinishedListener... finishedListenersRef
)
{
    SSLSession session = sslEngine.getSession();
    myAppData = ByteBuffer.allocate(session.getApplicationBufferSize() * 2);
    myNetData = ByteBuffer.allocate(session.getPacketBufferSize() * 2);
    peerAppData = ByteBuffer.allocate(session.getApplicationBufferSize() * 2);
    peerNetData = ByteBuffer.allocate(session.getPacketBufferSize() * 2);
    session.invalidate();

    peer = peerRef;
    finishedListeners = finishedListenersRef;
}
 
Example 2
Source File: PrivilegedMasterSecretValidator.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Checks the validity of an SSLSession's master secret. Should be run within a doPrivileged
 * block
 */
private boolean privilegedIsMasterSecretValid(final Socket socket) {
    if (socket instanceof SSLSocket) {
        SSLSession session = getSslSession(socket);
        if (session != null) {
            String className = session.getClass().getName();
            if ("sun.security.ssl.SSLSessionImpl".equals(className)) {
                try {
                    Object masterSecret = getMasterSecret(session, className);
                    if (masterSecret == null) {
                        session.invalidate();
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Invalidated session " + session);
                        }
                        return false;
                    }
                } catch (Exception e) {
                    failedToVerifyMasterSecret(e);
                }
            }
        }
    }
    return true;

}
 
Example 3
Source File: SdkTLSSocketFactory.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Invalidates all SSL/TLS sessions in {@code sessionContext} associated with {@code remoteAddress}.
 *
 * @param sessionContext collection of SSL/TLS sessions to be (potentially) invalidated
 * @param remoteAddress  associated with sessions to invalidate
 */
private void clearSessionCache(final SSLSessionContext sessionContext, final InetSocketAddress remoteAddress) {
    final String hostName = remoteAddress.getHostName();
    final int port = remoteAddress.getPort();
    final Enumeration<byte[]> ids = sessionContext.getIds();

    if (ids == null) {
        return;
    }

    while (ids.hasMoreElements()) {
        final byte[] id = ids.nextElement();
        final SSLSession session = sessionContext.getSession(id);
        if (session != null && session.getPeerHost() != null && session.getPeerHost().equalsIgnoreCase(hostName)
                && session.getPeerPort() == port) {
            session.invalidate();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Invalidated session " + session);
            }
        }
    }
}
 
Example 4
Source File: SSLSessionContextImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 5
Source File: SSLSessionContextImpl.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
private boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 6
Source File: SSLSessionContextImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 7
Source File: SSLSessionContextImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 8
Source File: SSLSessionContextImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 9
Source File: SSLSessionContextImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 10
Source File: SSLSessionContextImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 11
Source File: SSLSessionContextImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 12
Source File: SSLSessionContextImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 13
Source File: SSLSessionContextImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 14
Source File: SSLSessionContextImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean isTimedout(SSLSession sess) {
    if (timeout == 0) {
        return false;
    }

    if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
                                    <= (System.currentTimeMillis()))) {
        sess.invalidate();
        return true;
    }

    return false;
}
 
Example 15
Source File: StartTlsResponseImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Negotiates a TLS session using an SSL socket factory.
 * <p>
 * Creates an SSL socket using the supplied SSL socket factory and
 * attaches it to the existing connection. Performs the TLS handshake
 * and returns the negotiated session information.
 * <p>
 * If cipher suites have been set via <tt>setEnabledCipherSuites</tt>
 * then they are enabled before the TLS handshake begins.
 * <p>
 * Hostname verification is performed after the TLS handshake completes.
 * The default check performs a case insensitive match of the server's
 * hostname against that in the server's certificate. The server's
 * hostname is extracted from the subjectAltName in the server's
 * certificate (if present). Otherwise the value of the common name
 * attribute of the subject name is used. If a callback has
 * been set via <tt>setHostnameVerifier</tt> then that verifier is used if
 * the default check fails.
 * <p>
 * If an error occurs then the SSL socket is closed and an IOException
 * is thrown. The underlying connection remains intact.
 *
 * @param factory The possibly null SSL socket factory to use.
 * If null, the default SSL socket factory is used.
 * @return The negotiated SSL session
 * @throw IOException If an IO error was encountered while establishing
 * the TLS session.
 * @see #setEnabledCipherSuites
 * @see #setHostnameVerifier
 */
public SSLSession negotiate(SSLSocketFactory factory) throws IOException {

    if (isClosed && sslSocket != null) {
        throw new IOException("TLS connection is closed.");
    }

    if (factory == null) {
        factory = getDefaultFactory();
    }

    if (debug) {
        System.out.println("StartTLS: About to start handshake");
    }

    SSLSession sslSession = startHandshake(factory).getSession();

    if (debug) {
        System.out.println("StartTLS: Completed handshake");
    }

    SSLPeerUnverifiedException verifExcep = null;
    try {
        if (verify(hostname, sslSession)) {
            isClosed = false;
            return sslSession;
        }
    } catch (SSLPeerUnverifiedException e) {
        // Save to return the cause
        verifExcep = e;
    }
    if ((verifier != null) &&
            verifier.verify(hostname, sslSession)) {
        isClosed = false;
        return sslSession;
    }

    // Verification failed
    close();
    sslSession.invalidate();
    if (verifExcep == null) {
        verifExcep = new SSLPeerUnverifiedException(
                    "hostname of the server '" + hostname +
                    "' does not match the hostname in the " +
                    "server's certificate.");
    }
    throw verifExcep;
}
 
Example 16
Source File: StartTlsResponseImpl.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Negotiates a TLS session using an SSL socket factory.
 * <p>
 * Creates an SSL socket using the supplied SSL socket factory and
 * attaches it to the existing connection. Performs the TLS handshake
 * and returns the negotiated session information.
 * <p>
 * If cipher suites have been set via <tt>setEnabledCipherSuites</tt>
 * then they are enabled before the TLS handshake begins.
 * <p>
 * Hostname verification is performed after the TLS handshake completes.
 * The default check performs a case insensitive match of the server's
 * hostname against that in the server's certificate. The server's
 * hostname is extracted from the subjectAltName in the server's
 * certificate (if present). Otherwise the value of the common name
 * attribute of the subject name is used. If a callback has
 * been set via <tt>setHostnameVerifier</tt> then that verifier is used if
 * the default check fails.
 * <p>
 * If an error occurs then the SSL socket is closed and an IOException
 * is thrown. The underlying connection remains intact.
 *
 * @param factory The possibly null SSL socket factory to use.
 * If null, the default SSL socket factory is used.
 * @return The negotiated SSL session
 * @throw IOException If an IO error was encountered while establishing
 * the TLS session.
 * @see #setEnabledCipherSuites
 * @see #setHostnameVerifier
 */
public SSLSession negotiate(SSLSocketFactory factory) throws IOException {

    if (isClosed && sslSocket != null) {
        throw new IOException("TLS connection is closed.");
    }

    if (factory == null) {
        factory = getDefaultFactory();
    }

    if (debug) {
        System.out.println("StartTLS: About to start handshake");
    }

    SSLSession sslSession = startHandshake(factory).getSession();

    if (debug) {
        System.out.println("StartTLS: Completed handshake");
    }

    SSLPeerUnverifiedException verifExcep = null;
    try {
        if (verify(hostname, sslSession)) {
            isClosed = false;
            return sslSession;
        }
    } catch (SSLPeerUnverifiedException e) {
        // Save to return the cause
        verifExcep = e;
    }
    if ((verifier != null) &&
            verifier.verify(hostname, sslSession)) {
        isClosed = false;
        return sslSession;
    }

    // Verification failed
    close();
    sslSession.invalidate();
    if (verifExcep == null) {
        verifExcep = new SSLPeerUnverifiedException(
                    "hostname of the server '" + hostname +
                    "' does not match the hostname in the " +
                    "server's certificate.");
    }
    throw verifExcep;
}
 
Example 17
Source File: StartTlsResponseImpl.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Negotiates a TLS session using an SSL socket factory.
 * <p>
 * Creates an SSL socket using the supplied SSL socket factory and
 * attaches it to the existing connection. Performs the TLS handshake
 * and returns the negotiated session information.
 * <p>
 * If cipher suites have been set via <tt>setEnabledCipherSuites</tt>
 * then they are enabled before the TLS handshake begins.
 * <p>
 * Hostname verification is performed after the TLS handshake completes.
 * The default check performs a case insensitive match of the server's
 * hostname against that in the server's certificate. The server's
 * hostname is extracted from the subjectAltName in the server's
 * certificate (if present). Otherwise the value of the common name
 * attribute of the subject name is used. If a callback has
 * been set via <tt>setHostnameVerifier</tt> then that verifier is used if
 * the default check fails.
 * <p>
 * If an error occurs then the SSL socket is closed and an IOException
 * is thrown. The underlying connection remains intact.
 *
 * @param factory The possibly null SSL socket factory to use.
 * If null, the default SSL socket factory is used.
 * @return The negotiated SSL session
 * @throw IOException If an IO error was encountered while establishing
 * the TLS session.
 * @see #setEnabledCipherSuites
 * @see #setHostnameVerifier
 */
public SSLSession negotiate(SSLSocketFactory factory) throws IOException {

    if (isClosed && sslSocket != null) {
        throw new IOException("TLS connection is closed.");
    }

    if (factory == null) {
        factory = getDefaultFactory();
    }

    if (debug) {
        System.out.println("StartTLS: About to start handshake");
    }

    SSLSession sslSession = startHandshake(factory).getSession();

    if (debug) {
        System.out.println("StartTLS: Completed handshake");
    }

    SSLPeerUnverifiedException verifExcep = null;
    try {
        if (verify(hostname, sslSession)) {
            isClosed = false;
            return sslSession;
        }
    } catch (SSLPeerUnverifiedException e) {
        // Save to return the cause
        verifExcep = e;
    }
    if ((verifier != null) &&
            verifier.verify(hostname, sslSession)) {
        isClosed = false;
        return sslSession;
    }

    // Verification failed
    close();
    sslSession.invalidate();
    if (verifExcep == null) {
        verifExcep = new SSLPeerUnverifiedException(
                    "hostname of the server '" + hostname +
                    "' does not match the hostname in the " +
                    "server's certificate.");
    }
    throw verifExcep;
}
 
Example 18
Source File: StartTlsResponseImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Negotiates a TLS session using an SSL socket factory.
 * <p>
 * Creates an SSL socket using the supplied SSL socket factory and
 * attaches it to the existing connection. Performs the TLS handshake
 * and returns the negotiated session information.
 * <p>
 * If cipher suites have been set via {@code setEnabledCipherSuites}
 * then they are enabled before the TLS handshake begins.
 * <p>
 * Hostname verification is performed after the TLS handshake completes.
 * The default check performs a case insensitive match of the server's
 * hostname against that in the server's certificate. The server's
 * hostname is extracted from the subjectAltName in the server's
 * certificate (if present). Otherwise the value of the common name
 * attribute of the subject name is used. If a callback has
 * been set via {@code setHostnameVerifier} then that verifier is used if
 * the default check fails.
 * <p>
 * If an error occurs then the SSL socket is closed and an IOException
 * is thrown. The underlying connection remains intact.
 *
 * @param factory The possibly null SSL socket factory to use.
 * If null, the default SSL socket factory is used.
 * @return The negotiated SSL session
 * @throws IOException If an IO error was encountered while establishing
 * the TLS session.
 * @see #setEnabledCipherSuites
 * @see #setHostnameVerifier
 */
public SSLSession negotiate(SSLSocketFactory factory) throws IOException {

    if (isClosed && sslSocket != null) {
        throw new IOException("TLS connection is closed.");
    }

    if (factory == null) {
        factory = getDefaultFactory();
    }

    if (debug) {
        System.out.println("StartTLS: About to start handshake");
    }

    SSLSession sslSession = startHandshake(factory).getSession();

    if (debug) {
        System.out.println("StartTLS: Completed handshake");
    }

    SSLPeerUnverifiedException verifExcep = null;
    try {
        if (verify(hostname, sslSession)) {
            isClosed = false;
            return sslSession;
        }
    } catch (SSLPeerUnverifiedException e) {
        // Save to return the cause
        verifExcep = e;
    }
    if ((verifier != null) &&
            verifier.verify(hostname, sslSession)) {
        isClosed = false;
        return sslSession;
    }

    // Verification failed
    close();
    sslSession.invalidate();
    if (verifExcep == null) {
        verifExcep = new SSLPeerUnverifiedException(
                    "hostname of the server '" + hostname +
                    "' does not match the hostname in the " +
                    "server's certificate.");
    }
    throw verifExcep;
}
 
Example 19
Source File: StartTlsResponseImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Negotiates a TLS session using an SSL socket factory.
 * <p>
 * Creates an SSL socket using the supplied SSL socket factory and
 * attaches it to the existing connection. Performs the TLS handshake
 * and returns the negotiated session information.
 * <p>
 * If cipher suites have been set via <tt>setEnabledCipherSuites</tt>
 * then they are enabled before the TLS handshake begins.
 * <p>
 * Hostname verification is performed after the TLS handshake completes.
 * The default check performs a case insensitive match of the server's
 * hostname against that in the server's certificate. The server's
 * hostname is extracted from the subjectAltName in the server's
 * certificate (if present). Otherwise the value of the common name
 * attribute of the subject name is used. If a callback has
 * been set via <tt>setHostnameVerifier</tt> then that verifier is used if
 * the default check fails.
 * <p>
 * If an error occurs then the SSL socket is closed and an IOException
 * is thrown. The underlying connection remains intact.
 *
 * @param factory The possibly null SSL socket factory to use.
 * If null, the default SSL socket factory is used.
 * @return The negotiated SSL session
 * @throw IOException If an IO error was encountered while establishing
 * the TLS session.
 * @see #setEnabledCipherSuites
 * @see #setHostnameVerifier
 */
public SSLSession negotiate(SSLSocketFactory factory) throws IOException {

    if (isClosed && sslSocket != null) {
        throw new IOException("TLS connection is closed.");
    }

    if (factory == null) {
        factory = getDefaultFactory();
    }

    if (debug) {
        System.out.println("StartTLS: About to start handshake");
    }

    SSLSession sslSession = startHandshake(factory).getSession();

    if (debug) {
        System.out.println("StartTLS: Completed handshake");
    }

    SSLPeerUnverifiedException verifExcep = null;
    try {
        if (verify(hostname, sslSession)) {
            isClosed = false;
            return sslSession;
        }
    } catch (SSLPeerUnverifiedException e) {
        // Save to return the cause
        verifExcep = e;
    }
    if ((verifier != null) &&
            verifier.verify(hostname, sslSession)) {
        isClosed = false;
        return sslSession;
    }

    // Verification failed
    close();
    sslSession.invalidate();
    if (verifExcep == null) {
        verifExcep = new SSLPeerUnverifiedException(
                    "hostname of the server '" + hostname +
                    "' does not match the hostname in the " +
                    "server's certificate.");
    }
    throw verifExcep;
}
 
Example 20
Source File: StartTlsResponseImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Negotiates a TLS session using an SSL socket factory.
 * <p>
 * Creates an SSL socket using the supplied SSL socket factory and
 * attaches it to the existing connection. Performs the TLS handshake
 * and returns the negotiated session information.
 * <p>
 * If cipher suites have been set via <tt>setEnabledCipherSuites</tt>
 * then they are enabled before the TLS handshake begins.
 * <p>
 * Hostname verification is performed after the TLS handshake completes.
 * The default check performs a case insensitive match of the server's
 * hostname against that in the server's certificate. The server's
 * hostname is extracted from the subjectAltName in the server's
 * certificate (if present). Otherwise the value of the common name
 * attribute of the subject name is used. If a callback has
 * been set via <tt>setHostnameVerifier</tt> then that verifier is used if
 * the default check fails.
 * <p>
 * If an error occurs then the SSL socket is closed and an IOException
 * is thrown. The underlying connection remains intact.
 *
 * @param factory The possibly null SSL socket factory to use.
 * If null, the default SSL socket factory is used.
 * @return The negotiated SSL session
 * @throw IOException If an IO error was encountered while establishing
 * the TLS session.
 * @see #setEnabledCipherSuites
 * @see #setHostnameVerifier
 */
public SSLSession negotiate(SSLSocketFactory factory) throws IOException {

    if (isClosed && sslSocket != null) {
        throw new IOException("TLS connection is closed.");
    }

    if (factory == null) {
        factory = getDefaultFactory();
    }

    if (debug) {
        System.out.println("StartTLS: About to start handshake");
    }

    SSLSession sslSession = startHandshake(factory).getSession();

    if (debug) {
        System.out.println("StartTLS: Completed handshake");
    }

    SSLPeerUnverifiedException verifExcep = null;
    try {
        if (verify(hostname, sslSession)) {
            isClosed = false;
            return sslSession;
        }
    } catch (SSLPeerUnverifiedException e) {
        // Save to return the cause
        verifExcep = e;
    }
    if ((verifier != null) &&
            verifier.verify(hostname, sslSession)) {
        isClosed = false;
        return sslSession;
    }

    // Verification failed
    close();
    sslSession.invalidate();
    if (verifExcep == null) {
        verifExcep = new SSLPeerUnverifiedException(
                    "hostname of the server '" + hostname +
                    "' does not match the hostname in the " +
                    "server's certificate.");
    }
    throw verifExcep;
}