Example usage for javax.net.ssl SSLSocketFactory getDefault

List of usage examples for javax.net.ssl SSLSocketFactory getDefault

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocketFactory getDefault.

Prototype

public static SocketFactory getDefault() 

Source Link

Document

Returns the default SSL socket factory.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    String host = args[0];//from  w  w  w.ja v a2s.co  m
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Locating socket factory for SSL...");
        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

        System.out.println("Creating secure socket to " + host + ":" + port);
        SSLSocket socket = (SSLSocket) factory.createSocket(host, port);

        System.out.println("Enabling all available cipher suites...");
        String[] suites = socket.getSupportedCipherSuites();
        socket.setEnabledCipherSuites(suites);

        System.out.println("Registering a handshake listener...");
        socket.addHandshakeCompletedListener(new MyHandshakeListener());

        System.out.println("Starting handshaking...");
        socket.startHandshake();

        System.out.println("Just connected to " + socket.getRemoteSocketAddress());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

    String hostName = "hostName";
    String fileName = "fileName";

    SSLSocket sslsock = (SSLSocket) factory.createSocket(hostName, 443);

    SSLSession session = sslsock.getSession();
    X509Certificate cert;//w  ww .  jav  a2  s.co  m
    try {
        cert = (X509Certificate) session.getPeerCertificates()[0];
    } catch (SSLPeerUnverifiedException e) {
        System.err.println(session.getPeerHost() + " did not present a valid certificate.");
        return;
    }

    System.out.println(session.getPeerHost() + " has presented a certificate belonging to:");
    Principal p = cert.getSubjectDN();
    System.out.println("\t[" + p.getName() + "]");
    System.out.println("The certificate bears the valid signature of:");
    System.out.println("\t[" + cert.getIssuerDN().getName() + "]");

    System.out.print("Do you trust this certificate (y/n)? ");
    System.out.flush();
    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    if (Character.toLowerCase(console.readLine().charAt(0)) != 'y')
        return;

    PrintWriter out = new PrintWriter(sslsock.getOutputStream());

    out.print("GET " + fileName + " HTTP/1.0\r\n\r\n");
    out.flush();

    BufferedReader in = new BufferedReader(new InputStreamReader(sslsock.getInputStream()));
    String line;
    while ((line = in.readLine()) != null)
        System.out.println(line);

    sslsock.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    System.setProperty("javax.net.ssl.trustStore", "clienttrust");

    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket s = ssf.createSocket("127.0.0.1", 5432);

    SSLSession session = ((SSLSocket) s).getSession();
    Certificate[] cchain = session.getPeerCertificates();
    System.out.println("The Certificates used by peer");
    for (int i = 0; i < cchain.length; i++) {
        System.out.println(((X509Certificate) cchain[i]).getSubjectDN());
    }//from   w ww. j a  va2 s  . c  o  m
    System.out.println("Peer host is " + session.getPeerHost());
    System.out.println("Cipher is " + session.getCipherSuite());
    System.out.println("Protocol is " + session.getProtocol());
    System.out.println("ID is " + new BigInteger(session.getId()));
    System.out.println("Session created in " + session.getCreationTime());
    System.out.println("Session accessed in " + session.getLastAccessedTime());

    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String x = in.readLine();
    System.out.println(x);
    in.close();

}

From source file:com.granita.icloudcalsync.webdav.TlsSniSocketFactory.java

public static TlsSniSocketFactory getSocketFactory() {
    return new TlsSniSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(),
            new BrowserCompatHostnameVerifierHC4() // use BrowserCompatHostnameVerifier to allow IP addresses in the Common Name
    );/*from w  ww .j a v  a2  s .  com*/
}

From source file:LoginClient.java

public LoginClient() {
    try {//from  w  ww  .ja  v a  2  s.  c o  m
        SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", 7070);
        PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        String userName = "MyName";
        output.println(userName);
        String password = "MyPass";
        output.println(password);
        output.flush();
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String response = input.readLine();
        System.out.println(response);

        output.close();
        input.close();
        socket.close();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        System.exit(0);
    }
}

From source file:EZShare.SubscribeCommandConnection.java

public static void establishPersistentConnection(Boolean secure, int port, String ip, int id,
        JSONObject unsubscribJsonObject, String commandname, String name, String owner, String description,
        String channel, String uri, List<String> tags, String ezserver, String secret, Boolean relay,
        String servers) throws URISyntaxException {
    //secure = false;
    try {/*from  ww w.  j  ava2s  .  c  o  m*/
        System.out.print(port + ip);
        Socket socket = null;
        if (secure) {

            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            socket = (SSLSocket) sslsocketfactory.createSocket(ip, port);
        } else {
            socket = new Socket(ip, port);
        }
        BufferedReader Reader = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream output = new DataOutputStream(socket.getOutputStream());
        DataInputStream input = new DataInputStream(socket.getInputStream());

        JSONObject command = new JSONObject();
        Resource resource = new Resource();
        command = resource.inputToJSON(commandname, id, name, owner, description, channel, uri, tags, ezserver,
                secret, relay, servers);

        output.writeUTF(command.toJSONString());
        Client.debug("SEND", command.toJSONString());
        //output.writeUTF(command.toJSONString());
        new Thread(new Runnable() {
            @Override
            public void run() {
                String string = null;
                try {
                    while (true/*(string = input.readUTF()) != null*/) {
                        String serverResponse = input.readUTF();
                        JSONParser parser = new JSONParser();
                        JSONObject response = (JSONObject) parser.parse(serverResponse);
                        Client.debug("RECEIVE", response.toJSONString());
                        //System.out.println(serverResponse);          
                        //                     if((string = Reader.readLine()) != null){
                        //                        if(onKeyPressed(output,string,unsubscribJsonObject)) break;
                        //                     }
                        if (flag == 1) {
                            break;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String string = null;
                try {
                    while ((string = Reader.readLine()) != null) {
                        flag = 1;
                        if (onKeyPressed(output, string, unsubscribJsonObject))
                            break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.panlab.tgw.restclient.PtmInfoParser.java

private static byte[] getClientHello() {
    long time = System.currentTimeMillis();
    time /= 1000;/*w  w w. j  a  va2 s  .  c  o  m*/
    //System.out.println(time + " ");
    int index = 0;
    String[] suite = ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();
    for (int j = 0; j < suite.length; j++) {

        if (suites.containsKey(suite[j])) {
            index++;
            //System.out.println(suite[j]);
        }
    }
    int tempIndex = 0;
    byte[] suiteBytes = new byte[index * 3];
    for (int j = 0; j < suite.length; j++) {
        //log.info(j+": ");
        if (suites.containsKey(suite[j])) {
            //log.info(suite[j]);
            suiteBytes[3 * (index - tempIndex) - 3] = suites.get(suite[j])[0];
            suiteBytes[3 * (index - tempIndex) - 2] = suites.get(suite[j])[1];
            suiteBytes[3 * (index - tempIndex) - 1] = suites.get(suite[j])[2];
            tempIndex++;
        }
    }

    byte[] clientHello = { (byte) 0x80, (byte) (41 + suiteBytes.length), //Length
            0x01, //Message Type: Client Hello
            0x03, 0x01, //Version TLSv1.0
            (byte) (suiteBytes.length / 256), (byte) (suiteBytes.length % 256), //Cipher Spec Length
            0x00, 0x00, //Session ID Length
            0x00, 0x20, //Challenge Length
    };
    byte[] clientRandom = { (byte) (time / (256 * 256 * 256)),
            (byte) ((time % (256 * 256 * 256)) / (256 * 256)),
            (byte) (((time % (256 * 256 * 256)) % (256 * 256)) / 256), (byte) (time % 256), 0x00, 0x01, 0x02,
            0x03, 0x04, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x01, 0x02, 0x03,
            0x04, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x01, 0x0f };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(clientHello, 0, clientHello.length);
    baos.write(suiteBytes, 0, suiteBytes.length);
    baos.write(clientRandom, 0, clientRandom.length);

    return baos.toByteArray();
}

From source file:edu.gmu.isa681.client.model.ClientImpl.java

public void connect() throws IOException {
    if (isConnected()) {
        return;// w  ww  .j  av a  2  s  .c  o  m
    }

    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    ;
    sslSocket = (SSLSocket) ssf.createSocket(host, port);
    sslSocket.startHandshake();

    chnl = new EncodedChannel(sslSocket.getInetAddress().toString(), sslSocket.getInputStream(), 20,
            sslSocket.getOutputStream(), 20);

    shuttingDown = false;
}

From source file:org.hashes.HttpsClient.java

private static SocketFactory buildSocketFactory() {
    SocketFactory socketFactory;// www .  ja v a 2s .c  o m

    try {
        final SSLContext sslContext = SSLContext.getInstance(SSL_CONTEXT_PROTOCOL);
        sslContext.init(null, new TrustManager[] { new NaiveX509TrustManager() }, null);
        socketFactory = sslContext.getSocketFactory();
    } catch (final Exception e) {
        LOG.warn("Could not disable certificate validation", e);
        socketFactory = SSLSocketFactory.getDefault();
    }

    return socketFactory;
}

From source file:com.thoughtworks.go.server.GoServer.java

public GoServer() {
    this(new SystemEnvironment(), (SSLSocketFactory) SSLSocketFactory.getDefault());
}