Search in sources :

Example 11 with SSLSession

use of javax.net.ssl.SSLSession in project android_frameworks_base by ResurrectionRemix.

the class RootTrustManager method checkServerTrusted.

@Override
public void checkServerTrusted(X509Certificate[] certs, String authType, SSLEngine engine) throws CertificateException {
    SSLSession session = engine.getHandshakeSession();
    if (session == null) {
        throw new CertificateException("Not in handshake; no session available");
    }
    String host = session.getPeerHost();
    NetworkSecurityConfig config = mConfig.getConfigForHostname(host);
    config.getTrustManager().checkServerTrusted(certs, authType, engine);
}
Also used : SSLSession(javax.net.ssl.SSLSession) CertificateException(java.security.cert.CertificateException)

Example 12 with SSLSession

use of javax.net.ssl.SSLSession in project CloudStack-archive by CloudStack-extras.

the class Link method doWrite.

/**
     * No user, so comment it out.
     * 
     * Static methods for reading from a channel in case
     * you need to add a client that doesn't require nio.
     * @param ch channel to read from.
     * @param bytebuffer to use.
     * @return bytes read
     * @throws IOException if not read to completion.
    public static byte[] read(SocketChannel ch, ByteBuffer buff) throws IOException {
    	synchronized(buff) {
	    	buff.clear();
	    	buff.limit(4);
	    	
	    	while (buff.hasRemaining()) {
		    	if (ch.read(buff) == -1) {
		    		throw new IOException("Connection closed with -1 on reading size.");
		    	}
	    	}
	    	
	    	buff.flip();
	    	
	    	int length = buff.getInt();
	    	ByteArrayOutputStream output = new ByteArrayOutputStream(length);
	    	WritableByteChannel outCh = Channels.newChannel(output);
	    	
	    	int count = 0;
	    	while (count < length) {
	        	buff.clear();
	    		int read = ch.read(buff);
	    		if (read < 0) {
	    			throw new IOException("Connection closed with -1 on reading data.");
	    		}
	    		count += read;
	    		buff.flip();
	    		outCh.write(buff);
	    	}
	    	
	        return output.toByteArray();
    	}
    }
    */
private static void doWrite(SocketChannel ch, ByteBuffer[] buffers, SSLEngine sslEngine) throws IOException {
    SSLSession sslSession = sslEngine.getSession();
    ByteBuffer pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize() + 40);
    SSLEngineResult engResult;
    ByteBuffer headBuf = ByteBuffer.allocate(4);
    int totalLen = 0;
    for (ByteBuffer buffer : buffers) {
        totalLen += buffer.limit();
    }
    int processedLen = 0;
    while (processedLen < totalLen) {
        headBuf.clear();
        pkgBuf.clear();
        engResult = sslEngine.wrap(buffers, pkgBuf);
        if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING && engResult.getStatus() != SSLEngineResult.Status.OK) {
            throw new IOException("SSL: SSLEngine return bad result! " + engResult);
        }
        processedLen = 0;
        for (ByteBuffer buffer : buffers) {
            processedLen += buffer.position();
        }
        int dataRemaining = pkgBuf.position();
        int header = dataRemaining;
        int headRemaining = 4;
        pkgBuf.flip();
        if (processedLen < totalLen) {
            header = header | HEADER_FLAG_FOLLOWING;
        }
        headBuf.putInt(header);
        headBuf.flip();
        while (headRemaining > 0) {
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("Writing Header " + headRemaining);
            }
            long count = ch.write(headBuf);
            headRemaining -= count;
        }
        while (dataRemaining > 0) {
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("Writing Data " + dataRemaining);
            }
            long count = ch.write(pkgBuf);
            dataRemaining -= count;
        }
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 13 with SSLSession

use of javax.net.ssl.SSLSession in project CloudStack-archive by CloudStack-extras.

the class Link method read.

public byte[] read(SocketChannel ch) throws IOException {
    if (_readHeader) {
        // Start of a packet
        if (_readBuffer.position() == 0) {
            _readBuffer.limit(4);
        }
        if (ch.read(_readBuffer) == -1) {
            throw new IOException("Connection closed with -1 on reading size.");
        }
        if (_readBuffer.hasRemaining()) {
            s_logger.trace("Need to read the rest of the packet length");
            return null;
        }
        _readBuffer.flip();
        int header = _readBuffer.getInt();
        int readSize = (short) header;
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Packet length is " + readSize);
        }
        if (readSize > MAX_SIZE_PER_PACKET) {
            throw new IOException("Wrong packet size: " + readSize);
        }
        if (!_gotFollowingPacket) {
            _plaintextBuffer = ByteBuffer.allocate(2000);
        }
        if ((header & HEADER_FLAG_FOLLOWING) != 0) {
            _gotFollowingPacket = true;
        } else {
            _gotFollowingPacket = false;
        }
        _readBuffer.clear();
        _readHeader = false;
        if (_readBuffer.capacity() < readSize) {
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("Resizing the byte buffer from " + _readBuffer.capacity());
            }
            _readBuffer = ByteBuffer.allocate(readSize);
        }
        _readBuffer.limit(readSize);
    }
    if (ch.read(_readBuffer) == -1) {
        throw new IOException("Connection closed with -1 on read.");
    }
    if (_readBuffer.hasRemaining()) {
        // We're not done yet.
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Still has " + _readBuffer.remaining());
        }
        return null;
    }
    _readBuffer.flip();
    ByteBuffer appBuf;
    SSLSession sslSession = _sslEngine.getSession();
    SSLEngineResult engResult;
    int remaining = 0;
    while (_readBuffer.hasRemaining()) {
        remaining = _readBuffer.remaining();
        appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize() + 40);
        engResult = _sslEngine.unwrap(_readBuffer, appBuf);
        if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING && engResult.getStatus() != SSLEngineResult.Status.OK) {
            throw new IOException("SSL: SSLEngine return bad result! " + engResult);
        }
        if (remaining == _readBuffer.remaining()) {
            throw new IOException("SSL: Unable to unwrap received data! still remaining " + remaining + "bytes!");
        }
        appBuf.flip();
        if (_plaintextBuffer.remaining() < appBuf.limit()) {
            // We need to expand _plaintextBuffer for more data
            ByteBuffer newBuffer = ByteBuffer.allocate(_plaintextBuffer.capacity() + appBuf.limit() * 5);
            _plaintextBuffer.flip();
            newBuffer.put(_plaintextBuffer);
            _plaintextBuffer = newBuffer;
        }
        _plaintextBuffer.put(appBuf);
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Done with packet: " + appBuf.limit());
        }
    }
    _readBuffer.clear();
    _readHeader = true;
    if (!_gotFollowingPacket) {
        _plaintextBuffer.flip();
        byte[] result = new byte[_plaintextBuffer.limit()];
        _plaintextBuffer.get(result);
        return result;
    } else {
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Waiting for more packets");
        }
        return null;
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 14 with SSLSession

use of javax.net.ssl.SSLSession in project CloudStack-archive by CloudStack-extras.

the class VmwareContext method getHTTPConnection.

public HttpURLConnection getHTTPConnection(String urlString, String httpMethod) throws Exception {
    String cookieString = getServiceCookie();
    HostnameVerifier hv = new HostnameVerifier() {

        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setAllowUserInteraction(true);
    conn.setRequestProperty(org.apache.axis.transport.http.HTTPConstants.HEADER_COOKIE, cookieString);
    conn.setRequestMethod(httpMethod);
    connectWithRetry(conn);
    return conn;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SSLSession(javax.net.ssl.SSLSession) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 15 with SSLSession

use of javax.net.ssl.SSLSession in project ORCID-Source by ORCID.

the class DevJerseyClientConfig method init.

public void init() {
    SSLContext ctx = createSslContext();
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
    getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession sslSession) {
            if (hostname.equals("localhost")) {
                return true;
            }
            return false;
        }
    }, ctx));
}
Also used : SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Aggregations

SSLSession (javax.net.ssl.SSLSession)340 HostnameVerifier (javax.net.ssl.HostnameVerifier)121 SSLContext (javax.net.ssl.SSLContext)74 IOException (java.io.IOException)65 X509Certificate (java.security.cert.X509Certificate)64 CertificateException (java.security.cert.CertificateException)49 SSLSocket (javax.net.ssl.SSLSocket)49 TrustManager (javax.net.ssl.TrustManager)45 X509TrustManager (javax.net.ssl.X509TrustManager)43 Test (org.junit.Test)39 Certificate (java.security.cert.Certificate)33 SecureRandom (java.security.SecureRandom)31 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)29 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)28 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)28 URL (java.net.URL)24 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)24 KeyManagementException (java.security.KeyManagementException)23 SSLException (javax.net.ssl.SSLException)22 InputStream (java.io.InputStream)18