Java Code Examples for javax.net.ssl.SSLSocket#setEnabledProtocols()

The following examples show how to use javax.net.ssl.SSLSocket#setEnabledProtocols() . 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: SslRMIServerSocketFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Creates a server socket that accepts SSL connections
 * configured according to this factory's SSL socket configuration
 * parameters.</p>
 */
public ServerSocket createServerSocket(int port) throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    return new ServerSocket(port) {
        public Socket accept() throws IOException {
            Socket socket = super.accept();
            SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
                    socket, socket.getInetAddress().getHostName(),
                    socket.getPort(), true);
            sslSocket.setUseClientMode(false);
            if (enabledCipherSuites != null) {
                sslSocket.setEnabledCipherSuites(enabledCipherSuites);
            }
            if (enabledProtocols != null) {
                sslSocket.setEnabledProtocols(enabledProtocols);
            }
            sslSocket.setNeedClientAuth(needClientAuth);
            return sslSocket;
        }
    };
}
 
Example 2
Source File: SslContextFactory.java    From cloudhopper-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get an SSLSocket from this context.
 * {@link SSLContext#getSocketFactory()}
 */
public SSLSocket newSslSocket() throws IOException {
    SSLSocketFactory factory = sslContext.getSocketFactory();
    
    SSLSocket socket = (SSLSocket)factory.createSocket();
    
    if (sslConfig.getWantClientAuth())
        socket.setWantClientAuth(sslConfig.getWantClientAuth());
    if (sslConfig.getNeedClientAuth())
        socket.setNeedClientAuth(sslConfig.getNeedClientAuth());

    socket.setEnabledCipherSuites(selectCipherSuites(socket.getEnabledCipherSuites(),
			 socket.getSupportedCipherSuites()));   
    socket.setEnabledProtocols(selectProtocols(socket.getEnabledProtocols(),socket.getSupportedProtocols()));

    return socket;
}
 
Example 3
Source File: TCPIPProviderBase.java    From perf-harness with MIT License 6 votes vote down vote up
public Socket getSSLSocket() throws IOException {
	getHostname();
	final SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
	if (portRange > 1)
		System.out.println("About to connect to port " + currentPort + " for thread " + Thread.currentThread());

	final SSLSocket socket = (SSLSocket)sf.createSocket();
	if (SECURE_PROTO != null)
		socket.setEnabledProtocols(SECURE_PROTO);
	socket.setReuseAddress(true);
	socket.setSoLinger(true, 0);
	socket.setSoTimeout(timeoutIntervalLength);
	socket.connect(new InetSocketAddress(addr, currentPort), 0);

	// Check to see if the user has requested a range of ports to be used
	if (portRange > 1) {
		// For a range of ports, keep incrementing up to the max range and then
		// loop back to the start
		if (currentPort >= port + portRange - 1)
			currentPort = port;
		else
			currentPort++;
	}

	return socket;
}
 
Example 4
Source File: ConnectorBootstrap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Socket accept() throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    Socket socket = super.accept();
    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
            socket, socket.getInetAddress().getHostName(),
            socket.getPort(), true);
    sslSocket.setUseClientMode(false);
    if (enabledCipherSuites != null) {
        sslSocket.setEnabledCipherSuites(enabledCipherSuites);
    }
    if (enabledProtocols != null) {
        sslSocket.setEnabledProtocols(enabledProtocols);
    }
    sslSocket.setNeedClientAuth(needClientAuth);
    return sslSocket;
}
 
Example 5
Source File: SslRMIServerSocketFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Creates a server socket that accepts SSL connections
 * configured according to this factory's SSL socket configuration
 * parameters.</p>
 */
public ServerSocket createServerSocket(int port) throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    return new ServerSocket(port) {
        public Socket accept() throws IOException {
            Socket socket = super.accept();
            SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
                    socket, socket.getInetAddress().getHostName(),
                    socket.getPort(), true);
            sslSocket.setUseClientMode(false);
            if (enabledCipherSuites != null) {
                sslSocket.setEnabledCipherSuites(enabledCipherSuites);
            }
            if (enabledProtocols != null) {
                sslSocket.setEnabledProtocols(enabledProtocols);
            }
            sslSocket.setNeedClientAuth(needClientAuth);
            return sslSocket;
        }
    };
}
 
Example 6
Source File: ConnectorBootstrap.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Socket accept() throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    Socket socket = super.accept();
    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
            socket, socket.getInetAddress().getHostName(),
            socket.getPort(), true);
    sslSocket.setUseClientMode(false);
    if (enabledCipherSuites != null) {
        sslSocket.setEnabledCipherSuites(enabledCipherSuites);
    }
    if (enabledProtocols != null) {
        sslSocket.setEnabledProtocols(enabledProtocols);
    }
    sslSocket.setNeedClientAuth(needClientAuth);
    return sslSocket;
}
 
Example 7
Source File: SslRMIServerSocketFactory.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Creates a server socket that accepts SSL connections
 * configured according to this factory's SSL socket configuration
 * parameters.</p>
 */
public ServerSocket createServerSocket(int port) throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    return new ServerSocket(port) {
        public Socket accept() throws IOException {
            Socket socket = super.accept();
            SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
                    socket, socket.getInetAddress().getHostName(),
                    socket.getPort(), true);
            sslSocket.setUseClientMode(false);
            if (enabledCipherSuites != null) {
                sslSocket.setEnabledCipherSuites(enabledCipherSuites);
            }
            if (enabledProtocols != null) {
                sslSocket.setEnabledProtocols(enabledProtocols);
            }
            sslSocket.setNeedClientAuth(needClientAuth);
            return sslSocket;
        }
    };
}
 
Example 8
Source File: ConnectorBootstrap.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Socket accept() throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    Socket socket = super.accept();
    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
            socket, socket.getInetAddress().getHostName(),
            socket.getPort(), true);
    sslSocket.setUseClientMode(false);
    if (enabledCipherSuites != null) {
        sslSocket.setEnabledCipherSuites(enabledCipherSuites);
    }
    if (enabledProtocols != null) {
        sslSocket.setEnabledProtocols(enabledProtocols);
    }
    sslSocket.setNeedClientAuth(needClientAuth);
    return sslSocket;
}
 
Example 9
Source File: SslRMIServerSocketFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Creates a server socket that accepts SSL connections
 * configured according to this factory's SSL socket configuration
 * parameters.</p>
 */
public ServerSocket createServerSocket(int port) throws IOException {
    final SSLSocketFactory sslSocketFactory =
            context == null ?
                getDefaultSSLSocketFactory() : context.getSocketFactory();
    return new ServerSocket(port) {
        public Socket accept() throws IOException {
            Socket socket = super.accept();
            SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
                    socket, socket.getInetAddress().getHostName(),
                    socket.getPort(), true);
            sslSocket.setUseClientMode(false);
            if (enabledCipherSuites != null) {
                sslSocket.setEnabledCipherSuites(enabledCipherSuites);
            }
            if (enabledProtocols != null) {
                sslSocket.setEnabledProtocols(enabledProtocols);
            }
            sslSocket.setNeedClientAuth(needClientAuth);
            return sslSocket;
        }
    };
}
 
Example 10
Source File: SSLSocketFactory.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
/**
   * @param params Optional parameters. Parameters passed to this method will have no effect.
   *               This method will create a unconnected instance of {@link Socket} class
   *               using {@link javax.net.ssl.SSLSocketFactory#createSocket()} method.
   * @since 4.1
   */
  @SuppressWarnings("cast")
  public Socket createSocket(final HttpParams params) throws IOException {
      // the cast makes sure that the factory is working as expected
SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket();
sslSocket.setEnabledProtocols(getProtocols(sslSocket));
sslSocket.setEnabledCipherSuites(getCiphers(sslSocket));
return sslSocket;
  }
 
Example 11
Source File: BinarySecureClientPoolFactory.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
@Override
public Object createClient(String protocol, String hostName, int port) throws DataEndpointException {
    if (protocol.equalsIgnoreCase(DataEndpointConfiguration.Protocol.SSL.toString())) {
        int timeout = AgentHolder.getInstance().getDataEndpointAgent().getAgentConfiguration()
                .getSocketTimeoutMS();
        String sslProtocols = AgentHolder.getInstance().getDataEndpointAgent().getAgentConfiguration()
                .getSslEnabledProtocols();
        String ciphers = AgentHolder.getInstance().getDataEndpointAgent().getAgentConfiguration().getCiphers();

        try {
            SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(hostName, port);
            sslSocket.setSoTimeout(timeout);

            if (sslProtocols != null && sslProtocols.length() != 0) {
                String[] sslProtocolsArray = sslProtocols.split(",");
                sslSocket.setEnabledProtocols(sslProtocolsArray);
            }

            if (ciphers != null && ciphers.length() != 0) {
                String[] ciphersArray = ciphers.replaceAll(" ", "").split(",");
                sslSocket.setEnabledCipherSuites(ciphersArray);
            } else {
                sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
            }
            return sslSocket;
        } catch (IOException e) {
            throw new DataEndpointException("Error while opening socket to " + hostName + ":" + port + ". " +
                    e.getMessage(), e);
        }
    } else {
        throw new DataEndpointException("Unsupported protocol: " + protocol + ". Currently only " +
                DataEndpointConfiguration.Protocol.SSL.toString() + " supported.");
    }
}
 
Example 12
Source File: TesterSupport.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private SSLSocket filterProtocols(SSLSocket socket) {
    List<String> protocols = new ArrayList<String>();
    protocols.addAll(Arrays.asList(socket.getSupportedProtocols()));
    Iterator<String> protocolsIter = protocols.iterator();
    while (protocolsIter.hasNext()) {
        String protocol = protocolsIter.next();
        if (protocol.contains("SSLv2")) {
            protocolsIter.remove();
        }
    }
    socket.setEnabledProtocols(protocols.toArray(new String[protocols.size()]));
    return socket;
}
 
Example 13
Source File: SslRMIClientSocketFactorySecure.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public Socket createSocket(String host, int port) throws IOException {
  SSLSocket socket = (SSLSocket) super.createSocket(host, port);
  ArrayList<String> secureProtocols = new ArrayList<>();
  for (String p : socket.getEnabledProtocols()) {
    if (!p.contains("SSLv3")) {
      secureProtocols.add(p);
    }
  }
  socket.setEnabledProtocols(secureProtocols.toArray(
          new String[secureProtocols.size()]));
  return socket;
}
 
Example 14
Source File: SSLSocketHelper.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static void setSecurity(final SSLSocket sslSocket) {
    final String[] supportProtocols;
    final Collection<String> supportedProtocols = new LinkedList<>(
            Arrays.asList(sslSocket.getSupportedProtocols()));
    supportedProtocols.remove("SSLv3");
    supportProtocols = supportedProtocols.toArray(new String[supportedProtocols.size()]);

    sslSocket.setEnabledProtocols(supportProtocols);

    final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
            sslSocket.getSupportedCipherSuites());
    if (cipherSuites.length > 0) {
        sslSocket.setEnabledCipherSuites(cipherSuites);
    }
}
 
Example 15
Source File: SSLSocketFactoryCompat.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 5 votes vote down vote up
private void upgradeTLS(SSLSocket ssl) {
    // Android 5.0+ (API level21) provides reasonable default settings
    // but it still allows SSLv3
    // https://developer.android.com/about/versions/android-5.0-changes.html#ssl
    if (protocols != null) {
        ssl.setEnabledProtocols(protocols);
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && cipherSuites != null) {
        ssl.setEnabledCipherSuites(cipherSuites);
    }
}
 
Example 16
Source File: URLConnectionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public SSLSocket createSocket() throws IOException {
    SSLSocket socket = (SSLSocket) delegate.createSocket();
    socket.setEnabledProtocols(protocols);
    return socket;
}
 
Example 17
Source File: PassiveConnection.java    From drftpd with GNU General Public License v2.0 4 votes vote down vote up
public Socket connect(String[] cipherSuites, String[] sslProtocols, int bufferSize) throws IOException {
    // bufferSize has already been set on the ServerSocket
    // just need to accept this param to comply with the Connection class

    if (_serverSocket == null) {
        // can happen if abort() is called before connect()
        throw new SocketException(
                "abort() was called before connect()");
    }

    Socket sock = null;
    try {
        sock = _serverSocket.accept();
    } finally {
        if (_serverSocket != null) {
            _serverSocket.close();
        }
        _serverSocket = null;
    }

    if (sock == null) {
        // can happen if abort() is called while serverSocket.accept() is
        // waiting
        throw new SocketException(
                "abort() was called while waiting for accept()");
    }

    setSockOpts(sock);

    if (sock instanceof SSLSocket) {
        SSLSocket sslsock = (SSLSocket) sock;
        if (cipherSuites != null && cipherSuites.length != 0) {
            sslsock.setEnabledCipherSuites(cipherSuites);
        }
        if (sslProtocols != null && sslProtocols.length != 0) {
            sslsock.setEnabledProtocols(sslProtocols);
        }
        sslsock.setUseClientMode(_useSSLClientMode);
        sslsock.startHandshake();
    }


    return sock;
}
 
Example 18
Source File: FTPSClient.java    From Aria with Apache License 2.0 4 votes vote down vote up
/**
 * SSL/TLS negotiation. Acquires an SSL socket of a control
 * connection and carries out handshake processing.
 *
 * @throws IOException If server negotiation fails
 */
protected void sslNegotiation() throws IOException {
  plainSocket = _socket_;
  initSslContext();

  SSLSocketFactory ssf = context.getSocketFactory();
  String host = (_hostname_ != null) ? _hostname_ : getRemoteAddress().getHostAddress();
  int port = _socket_.getPort();
  SSLSocket socket = (SSLSocket) ssf.createSocket(_socket_, host, port, false);
  socket.setEnableSessionCreation(isCreation);
  socket.setUseClientMode(isClientMode);

  // client mode
  if (isClientMode) {
    if (tlsEndpointChecking) {
      SSLSocketUtils.enableEndpointNameVerification(socket);
    }
  } else { // server mode
    socket.setNeedClientAuth(isNeedClientAuth);
    socket.setWantClientAuth(isWantClientAuth);
  }

  if (protocols != null) {
    socket.setEnabledProtocols(protocols);
  }
  if (suites != null) {
    socket.setEnabledCipherSuites(suites);
  }
  socket.startHandshake();

  // TODO the following setup appears to duplicate that in the super class methods
  _socket_ = socket;
  _controlInput_ =
      new BufferedReader(new InputStreamReader(socket.getInputStream(), getControlEncoding()));
  _controlOutput_ =
      new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), getControlEncoding()));

  if (isClientMode) {
    if (hostnameVerifier != null && !hostnameVerifier.verify(host, socket.getSession())) {
      throw new SSLHandshakeException("Hostname doesn't match certificate");
    }
  }
}
 
Example 19
Source File: HtmlUnitSSLConnectionSocketFactory.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private static void configureSocket(final SSLSocket sslSocket, final HttpContext context) {
    if (isUseSSL3Only(context)) {
        sslSocket.setEnabledProtocols(new String[]{"SSLv3"});
    }
}
 
Example 20
Source File: Tls12SslSocketFactory.java    From android-security with Apache License 2.0 4 votes vote down vote up
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
    SSLSocket s = (SSLSocket) delegate.createSocket(host, port);
    s.setEnabledProtocols(new String[]{tag});
    return s;
}