Java 8 SSLSocket - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 SSLSocket class.

This code example shows how to use the following methods: setEnabledCipherSuites, setEnabledProtocols, setNeedClientAuth
        InetAddress address = InetAddress.getByName(host);
 
        SSLSocketFactory socketFactory = this.sslContext.getSocketFactory();
        SSLSocket socket = (SSLSocket) socketFactory.createSocket(address, port);
        if (this.jsseSecurityDomain.getProtocols() != null)
            socket.setEnabledProtocols(this.jsseSecurityDomain.getProtocols());
        if (this.jsseSecurityDomain.getCipherSuites() != null)
            socket.setEnabledCipherSuites(this.jsseSecurityDomain.getCipherSuites());
        socket.setNeedClientAuth(this.jsseSecurityDomain.isClientAuth());
        return socket;
    } 
 
    @Override 
    public Socket createSocket(String host, int port, int timeout) throws IOException, TIMEOUT {
        this.initSSLContext();
        InetAddress address = InetAddress.getByName(host);
 
        SSLSocketFactory socketFactory = this.sslContext.getSocketFactory();
        SSLSocket socket = (SSLSocket) socketFactory.createSocket();
        socket.connect(new InetSocketAddress(address, port), timeout);
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: setEnableSessionCreation, setEnabledCipherSuites, setEnabledProtocols, setNeedClientAuth, setUseClientMode
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: getSession, startHandshake
      */ 
    private void verifyHostname(Socket socket, String hostname) throws IOException {
        // 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");
        } 
        // TODO: Instead of reporting the name of the server we think we're connecting to, 
        // we should be reporting the bad name in the certificate.  Unfortunately this is buried 
        // in the verifier code and is not available in the verifier API, and extracting the 
        // CN & alts is beyond the scope of this patch. 
        if (!HOSTNAME_VERIFIER.verify(hostname, session)) {
            throw new SSLPeerUnverifiedException(
                    "Certificate hostname not useable for server: " + hostname);
        } 
    } 
Full Snippet Info
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: getEnabledCipherSuites, getSession, setEnabledCipherSuites
        String hostname = (remoteAddress!=null) ? remoteAddress.getHostname() : null;
        int port = (remoteAddress!=null) ? remoteAddress.getPort() : 0;
        SSLSocket resultSocket = (SSLSocket)f.createSocket(lowerLayerSocket, hostname, port, autoClose);
         
        // set properties 
        log.finer("default enabledCipherSuites="+Arrays.toString(resultSocket.getEnabledCipherSuites()));
        if (enabledCipherSuites!=null) {
            resultSocket.setEnabledCipherSuites(enabledCipherSuites);
            log.fine("set enabledCipherSuites="+Arrays.toString(enabledCipherSuites));
        } 
         
        return new TLSNetSocket(new Socket2NetSocket(resultSocket), resultSocket.getSession(), ""+lowerLayerNetSocket);
    } 
} 
Full Snippet Info
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: getEnableSessionCreation, getEnabledCipherSuites, getEnabledProtocols, getNeedClientAuth, getWantClientAuth
            // to subclassing for the purpose of overriding this behavior (private methods, fields, etc.). 
            // As such, we create a socket (preconfigured by SSLContextParameters) from the context 
            // we gave to FTPSClient and then setup FTPSClient to reuse the already configured configuration 
            // from the socket for all future sockets it creates.  Not sexy and a little brittle, but it works. 
            SSLSocket socket = (SSLSocket)context.getSocketFactory().createSocket();
            client.setEnabledCipherSuites(socket.getEnabledCipherSuites());
            client.setEnabledProtocols(socket.getEnabledProtocols());
            client.setNeedClientAuth(socket.getNeedClientAuth());
            client.setWantClientAuth(socket.getWantClientAuth());
            client.setEnabledSessionCreation(socket.getEnableSessionCreation());
        } else { 
            client = new FTPSClient(getFtpsConfiguration().getSecurityProtocol(),
                                               getFtpsConfiguration().isImplicit()); 
             
            if (ftpClientKeyStoreParameters != null) {
                String type = (ftpClientKeyStoreParameters.containsKey("type"))
                        ? (String) ftpClientKeyStoreParameters.get("type") : KeyStore.getDefaultType();
                String file = (String) ftpClientKeyStoreParameters.get("file");
                String password = (String) ftpClientKeyStoreParameters.get("password");
                String algorithm = (ftpClientKeyStoreParameters.containsKey("algorithm"))
Full Snippet Info
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: close, getInputStream, getOutputStream, getSession
	 */ 
	private SSLSocket createSocket() throws UnknownHostException, IOException, NoSuchAlgorithmException {
		SSLSocketFactory factory = this.getSSLCtx().getSocketFactory();
		SSLSocket socket = (SSLSocket) factory.createSocket(this.getHostname(), this.getPort());
		String seeHash = CryptoTool.SHA1toHex(
				socket.getSession().getPeerCertificates()[0].
				getPublicKey().getEncoded());
		if (this.getPubKeyHash()!=null && !seeHash.equals(this.getPubKeyHash())) {
			throw new UnknownHostException("Detected man-in-the-middle! Expect="+this.getPubKeyHash()+", See="+seeHash);
		} 
		return socket;
	} 
	 
	/** 
	 * Calls a remote method 
	 * Marshalls entire Java Objects using the builtin ObjectOutputStream. 
	 * Notice that each method call requires its own SSLSocket. 
	 * ObjectInputStream requires that the entire stream is read to EOF before it will  
	 * convert what it read into Objects. 
	 * Inefficient, but working 

6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: getSession, setUseClientMode, startHandshake
            String hostName = config.getHostName();
            int port = config.getHostPort();
            // create the wrapper over connected socket 
            sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket,
                    hostName, port, true);
            sslSocket.setUseClientMode(true);
            sslSocket.startHandshake();
            if (!hostnameVerifier.verify(hostName, sslSocket.getSession())) {
                throw new IOException(Messages.getString("luni.02", hostName)); //$NON-NLS-1$
            } 
            usingSecureSocket = true;
        } 
        return sslSocket;
    } 
 
    Socket getSocket() {
        return socket;
    } 
     
    /* 
Full Snippet Info
5
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: setSoTimeout, setEnabledProtocols, setUseClientMode
            SSLSocket socket;
            
            if(timeout >= 0)
            { 
                socket = (SSLSocket)socketFactory.createSocket(host,port);
                socket.setSoTimeout(timeout);
            } 
            else 
            { 
                socket = (SSLSocket)socketFactory.createSocket(InetAddress.getByName(host), port);
            } 
             
            // set protocols 
            { 
                socket.setEnabledProtocols(new String[]{
                    contextWrapper.getContext().getProtocol()
                    }); 
            } 
             
            socket.setUseClientMode(true);

6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: close, getInetAddress, getInputStream, getOutputStream
sock.close();
            System.out.println("Server socket streams reset.");
            System.out.printf("Server listening on port %d.\n", servSock.getLocalPort());
            sock = (SSLSocket) servSock.accept();
            System.out.printf("Client connected from %s.\n", sock.getInetAddress());
            out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        } catch (IOException ex) {
            System.out.println("Error while attempting to reset the socket.");
            ex.printStackTrace();
        } 
    } 
 
    /** 
     * Destroys the socket entirely, killing the server. 
     */ 
    public void killSocket() { 
        try { 
            out.close();
Full Snippet Info
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: getSSLParameters, getHandshakeSession
    @Override 
    public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket)
    { 
        SSLSocket sslSocket = (SSLSocket)socket;
 
        String alias = chooseServerAlias(keyType,issuers,sslSocket.getSSLParameters().getSNIMatchers(),sslSocket.getHandshakeSession());
        if (alias==NO_MATCHERS)
            alias=_delegate.chooseServerAlias(keyType,issuers,socket);
        if (LOG.isDebugEnabled())
            LOG.debug("Chose alias {}/{} on {}",alias,keyType,socket);
        return alias;
    } 
 
    @Override 
    public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine)
    { 
        String alias = chooseServerAlias(keyType,issuers,engine.getSSLParameters().getSNIMatchers(),engine.getHandshakeSession());
        if (alias==NO_MATCHERS)
            alias=_delegate.chooseEngineServerAlias(keyType,issuers,engine);
        if (LOG.isDebugEnabled())
Full Snippet Info
5
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE

Related Java 8 SSLSocket Questions & Answers:

See Code Examples for Java 8 SSLSocket Methods: