Example usage for javax.net.ssl SSLSession getValue

List of usage examples for javax.net.ssl SSLSession getValue

Introduction

In this page you can find the example usage for javax.net.ssl SSLSession getValue.

Prototype

public Object getValue(String name);

Source Link

Document

Returns the object bound to the given name in the session's application layer data.

Usage

From source file:ee.ria.xroad.proxy.clientproxy.FastestConnectionSelectingSSLSocketFactory.java

private static boolean isSessionHost(SSLSession session, URI host) {
    try {//w  ww . j a va2s  .  c  om
        URI sslHost = (URI) session.getValue(ID_SELECTED_TARGET);
        return sslHost != null && sslHost.equals(host);
    } catch (Exception e) {
        log.error("Error checking if host {} is in session ({}).", host, session);
        log.error("Exception :{}", e);
    }

    return false;
}

From source file:net.lightbody.bmp.proxy.jetty.http.JsseListener.java

/**
 * Allow the Listener a chance to customise the request. before the server does its stuff. <br>
 * This allows the required attributes to be set for SSL requests. <br>
 * The requirements of the Servlet specs are:
 * <ul>//  ww  w  . j  a v a 2s .com
 * <li> an attribute named "javax.servlet.request.cipher_suite" of type String.</li>
 * <li> an attribute named "javax.servlet.request.key_size" of type Integer.</li>
 * <li> an attribute named "javax.servlet.request.X509Certificate" of type
 * java.security.cert.X509Certificate[]. This is an array of objects of type X509Certificate,
 * the order of this array is defined as being in ascending order of trust. The first
 * certificate in the chain is the one set by the client, the next is the one used to
 * authenticate the first, and so on. </li>
 * </ul>
 * 
 * @param socket The Socket the request arrived on. This should be a javax.net.ssl.SSLSocket.
 * @param request HttpRequest to be customised.
 */
protected void customizeRequest(Socket socket, HttpRequest request) {
    super.customizeRequest(socket, request);

    if (!(socket instanceof javax.net.ssl.SSLSocket))
        return; // I'm tempted to let it throw an exception...

    try {
        SSLSocket sslSocket = (SSLSocket) socket;
        SSLSession sslSession = sslSocket.getSession();
        String cipherSuite = sslSession.getCipherSuite();
        Integer keySize;
        X509Certificate[] certs;

        CachedInfo cachedInfo = (CachedInfo) sslSession.getValue(CACHED_INFO_ATTR);
        if (cachedInfo != null) {
            keySize = cachedInfo.getKeySize();
            certs = cachedInfo.getCerts();
        } else {
            keySize = new Integer(ServletSSL.deduceKeyLength(cipherSuite));
            certs = getCertChain(sslSession);
            cachedInfo = new CachedInfo(keySize, certs);
            sslSession.putValue(CACHED_INFO_ATTR, cachedInfo);
        }

        if (certs != null)
            request.setAttribute("javax.servlet.request.X509Certificate", certs);
        else if (_needClientAuth) // Sanity check
            throw new HttpException(HttpResponse.__403_Forbidden);

        request.setAttribute("javax.servlet.request.cipher_suite", cipherSuite);
        request.setAttribute("javax.servlet.request.key_size", keySize);
    } catch (Exception e) {
        log.warn(LogSupport.EXCEPTION, e);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.http.SslListener.java

/**
 * Allow the Listener a chance to customise the request. before the server does its stuff. <br>
 * This allows the required attributes to be set for SSL requests. <br>
 * The requirements of the Servlet specs are:
 * <ul>/* ww  w  . j a  v  a 2  s .c o  m*/
 * <li>an attribute named "javax.servlet.request.cipher_suite" of type String.</li>
 * <li>an attribute named "javax.servlet.request.key_size" of type Integer.</li>
 * <li>an attribute named "javax.servlet.request.X509Certificate" of type
 * java.security.cert.X509Certificate[]. This is an array of objects of type X509Certificate,
 * the order of this array is defined as being in ascending order of trust. The first
 * certificate in the chain is the one set by the client, the next is the one used to
 * authenticate the first, and so on.</li>
 * </ul>
 * 
 * @param socket The Socket the request arrived on. This should be a javax.net.ssl.SSLSocket.
 * @param request HttpRequest to be customised.
 */
protected void customizeRequest(Socket socket, HttpRequest request) {
    super.customizeRequest(socket, request);

    if (!(socket instanceof javax.net.ssl.SSLSocket))
        return; // I'm tempted to let it throw an
                // exception...

    try {
        SSLSocket sslSocket = (SSLSocket) socket;
        SSLSession sslSession = sslSocket.getSession();
        String cipherSuite = sslSession.getCipherSuite();
        Integer keySize;
        X509Certificate[] certs;

        CachedInfo cachedInfo = (CachedInfo) sslSession.getValue(CACHED_INFO_ATTR);
        if (cachedInfo != null) {
            keySize = cachedInfo.getKeySize();
            certs = cachedInfo.getCerts();
        } else {
            keySize = new Integer(ServletSSL.deduceKeyLength(cipherSuite));
            certs = getCertChain(sslSession);
            cachedInfo = new CachedInfo(keySize, certs);
            sslSession.putValue(CACHED_INFO_ATTR, cachedInfo);
        }

        if (certs != null)
            request.setAttribute("javax.servlet.request.X509Certificate", certs);
        else if (_needClientAuth) // Sanity check
            throw new HttpException(HttpResponse.__403_Forbidden);

        request.setAttribute("javax.servlet.request.cipher_suite", cipherSuite);
        request.setAttribute("javax.servlet.request.key_size", keySize);
    } catch (Exception e) {
        log.warn(LogSupport.EXCEPTION, e);
    }
}

From source file:org.apache.tomcat.util.net.jsse.JSSESupport.java

/**
 * Copied from <code>org.apache.catalina.valves.CertificateValve</code>
 *///from w ww. ja  v a  2  s  . co m
public Integer getKeySize() throws IOException {
    // Look up the current SSLSession
    SSLSession session = ssl.getSession();
    SSLSupport.CipherData c_aux[] = ciphers;
    if (session == null)
        return null;
    Integer keySize = (Integer) session.getValue(KEY_SIZE_KEY);
    if (keySize == null) {
        int size = 0;
        String cipherSuite = session.getCipherSuite();
        for (int i = 0; i < c_aux.length; i++) {
            if (cipherSuite.indexOf(c_aux[i].phrase) >= 0) {
                size = c_aux[i].keySize;
                break;
            }
        }
        keySize = new Integer(size);
        session.putValue(KEY_SIZE_KEY, keySize);
    }
    return keySize;
}