Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import java.security.cert.X509Certificate;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSocket;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- public class MySSLSocketFactory extends SSLSocketFactory {
- SSLSocketFactory factory;
- String cipher[] = null;
- String protocol[] = null;
- public MySSLSocketFactory(int cipher, int protocol) throws NoSuchAlgorithmException, KeyManagementException {
- super();
- TrustManager[] byPassTrustManagers = new TrustManager[] { new X509TrustManager() {
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
- public void checkClientTrusted(X509Certificate[] chain, String authType) {
- }
- public void checkServerTrusted(X509Certificate[] chain, String authType) {
- for (X509Certificate cert : chain) {
- System.out.println("srv: " + cert.getSubjectDN());
- }
- }
- } };
- //http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext
- int c = (cipher > Main.ciphers.length - 1) ? Main.ciphers.length - 1 : cipher;
- int p = (protocol > Main.protocols.length - 1) ? Main.protocols.length - 1 : protocol;
- this.cipher = new String[1];
- this.cipher[0] = Main.ciphers[c];
- this.protocol = new String[1];
- this.protocol[0] = Main.protocols[p];
- SSLContext sslContext = SSLContext.getInstance(this.protocol[0]);
- sslContext.init(null, byPassTrustManagers, new SecureRandom());
- factory = sslContext.getSocketFactory();
- System.out.println("SSL: cipher= "+this.cipher[0]+" protocol="+this.protocol[0]);
- }
- @Override
- public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
- SSLSocket socket=(SSLSocket) factory.createSocket(s, host, port, autoClose);
- socket.setEnabledCipherSuites(cipher);
- socket.setEnabledProtocols(protocol);
- return socket;
- }
- @Override
- public String[] getDefaultCipherSuites() {
- return cipher;
- }
- @Override
- public String[] getSupportedCipherSuites() {
- return cipher;
- }
- @Override
- public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException {
- SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1);
- socket.setEnabledCipherSuites(cipher);
- socket.setEnabledProtocols(protocol);
- return socket;
- }
- @Override
- public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
- SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1);
- socket.setEnabledCipherSuites(cipher);
- socket.setEnabledProtocols(protocol);
- return socket;
- }
- @Override
- public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException {
- SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1, arg2, arg3);
- socket.setEnabledCipherSuites(cipher);
- socket.setEnabledProtocols(protocol);
- return socket;
- }
- @Override
- public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
- SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1, arg2, arg3);
- socket.setEnabledCipherSuites(cipher);
- socket.setEnabledProtocols(protocol);
- return socket;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment