List of usage examples for javax.net.ssl SSLSocket startHandshake
public abstract void startHandshake() throws IOException;
From source file:Main.java
public static void main(String[] argv) throws Exception { SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(8080); String[] suites = serverSocket.getSupportedCipherSuites(); for (int i = 0; i < suites.length; i++) { System.out.println(suites[i]); }//from ww w.ja v a 2 s .c o m serverSocket.setEnabledCipherSuites(suites); String[] protocols = serverSocket.getSupportedProtocols(); for (int i = 0; i < protocols.length; i++) { System.out.println(protocols[i]); } SSLSocket socket = (SSLSocket) serverSocket.accept(); socket.startHandshake(); System.out.println(socket.getRemoteSocketAddress()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { int port = 443; String hostname = "hostname"; SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port); socket.startHandshake(); // Retrieve the server's certificate chain Certificate[] serverCerts = socket.getSession().getPeerCertificates(); socket.close();//from w w w. j ava2 s .c om }
From source file:MainClass.java
public static void main(String args[]) throws Exception { SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999); socket.startHandshake(); SSLSession session = socket.getSession(); java.security.cert.Certificate[] servercerts = session.getPeerCertificates(); List mylist = new ArrayList(); for (int i = 0; i < servercerts.length; i++) { mylist.add(servercerts[i]);/* www. ja v a2 s. c o m*/ } CertificateFactory cf = CertificateFactory.getInstance("X.509"); CertPath cp = cf.generateCertPath(mylist); FileOutputStream f = new FileOutputStream("CertPath.dat"); ObjectOutputStream b = new ObjectOutputStream(f); b.writeObject(cp); }
From source file:MainClass.java
public static void main(String[] args) { int port = Integer.parseInt(args[0]); try {/* www. j a va 2s . co m*/ System.out.println("Locating server socket factory for SSL..."); SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); System.out.println("Creating a server socket on port " + port); SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(port); String[] suites = serverSocket.getSupportedCipherSuites(); System.out.println("Support cipher suites are:"); for (int i = 0; i < suites.length; i++) { System.out.println(suites[i]); } serverSocket.setEnabledCipherSuites(suites); System.out.println("Support protocols are:"); String[] protocols = serverSocket.getSupportedProtocols(); for (int i = 0; i < protocols.length; i++) { System.out.println(protocols[i]); } System.out.println("Waiting for client..."); SSLSocket socket = (SSLSocket) serverSocket.accept(); System.out.println("Starting handshake..."); socket.startHandshake(); System.out.println("Just connected to " + socket.getRemoteSocketAddress()); } catch (IOException e) { e.printStackTrace(); } }
From source file:MyHandshakeListener.java
public static void main(String[] args) throws Exception { SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 8080); String[] suites = socket.getSupportedCipherSuites(); socket.setEnabledCipherSuites(suites); socket.addHandshakeCompletedListener(new MyHandshakeListener()); socket.startHandshake(); System.out.println("Just connected to " + socket.getRemoteSocketAddress()); }
From source file:MainClass.java
public static void main(String[] args) { String host = args[0];//from www. ja v a 2 s .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:net.sf.jsignpdf.InstallCert.java
/** * The main - whole logic of Install Cert Tool. * //from w w w.j a v a2 s .c o m * @param args * @throws Exception */ public static void main(String[] args) { String host; int port; char[] passphrase; System.out.println("InstallCert - Install CA certificate to Java Keystore"); System.out.println("====================================================="); final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { if ((args.length == 1) || (args.length == 2)) { String[] c = args[0].split(":"); host = c[0]; port = (c.length == 1) ? 443 : Integer.parseInt(c[1]); String p = (args.length == 1) ? "changeit" : args[1]; passphrase = p.toCharArray(); } else { String tmpStr; do { System.out.print("Enter hostname or IP address: "); tmpStr = StringUtils.defaultIfEmpty(reader.readLine(), null); } while (tmpStr == null); host = tmpStr; System.out.print("Enter port number [443]: "); tmpStr = StringUtils.defaultIfEmpty(reader.readLine(), null); port = tmpStr == null ? 443 : Integer.parseInt(tmpStr); System.out.print("Enter keystore password [changeit]: "); tmpStr = reader.readLine(); String p = "".equals(tmpStr) ? "changeit" : tmpStr; passphrase = p.toCharArray(); } char SEP = File.separatorChar; final File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security"); final File file = new File(dir, "cacerts"); System.out.println("Loading KeyStore " + file + "..."); InputStream in = new FileInputStream(file); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(in, passphrase); in.close(); SSLContext context = SSLContext.getInstance("TLS"); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0]; SavingTrustManager tm = new SavingTrustManager(defaultTrustManager); context.init(null, new TrustManager[] { tm }, null); SSLSocketFactory factory = context.getSocketFactory(); System.out.println("Opening connection to " + host + ":" + port + "..."); SSLSocket socket = (SSLSocket) factory.createSocket(host, port); socket.setSoTimeout(10000); try { System.out.println("Starting SSL handshake..."); socket.startHandshake(); socket.close(); System.out.println(); System.out.println("No errors, certificate is already trusted"); } catch (SSLException e) { System.out.println(); System.out.println("Certificate is not yet trusted."); // e.printStackTrace(System.out); } X509Certificate[] chain = tm.chain; if (chain == null) { System.out.println("Could not obtain server certificate chain"); return; } System.out.println(); System.out.println("Server sent " + chain.length + " certificate(s):"); System.out.println(); MessageDigest sha1 = MessageDigest.getInstance("SHA1"); MessageDigest md5 = MessageDigest.getInstance("MD5"); for (int i = 0; i < chain.length; i++) { X509Certificate cert = chain[i]; System.out.println(" " + (i + 1) + " Subject " + cert.getSubjectDN()); System.out.println(" Issuer " + cert.getIssuerDN()); sha1.update(cert.getEncoded()); System.out.println(" sha1 " + toHexString(sha1.digest())); md5.update(cert.getEncoded()); System.out.println(" md5 " + toHexString(md5.digest())); System.out.println(); } System.out.print("Enter certificate to add to trusted keystore or 'q' to quit [1]: "); String line = reader.readLine().trim(); int k = -1; try { k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1; } catch (NumberFormatException e) { } if (k < 0 || k >= chain.length) { System.out.println("KeyStore not changed"); } else { try { System.out.println("Creating keystore backup"); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); final File backupFile = new File(dir, CACERTS_KEYSTORE + "." + dateFormat.format(new java.util.Date())); final FileInputStream fis = new FileInputStream(file); final FileOutputStream fos = new FileOutputStream(backupFile); IOUtils.copy(fis, fos); fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Installing certificate..."); X509Certificate cert = chain[k]; String alias = host + "-" + (k + 1); ks.setCertificateEntry(alias, cert); OutputStream out = new FileOutputStream(file); ks.store(out, passphrase); out.close(); System.out.println(); System.out.println(cert); System.out.println(); System.out.println("Added certificate to keystore '" + file + "' using alias '" + alias + "'"); } } catch (Exception e) { System.out.println(); System.out.println("----------------------------------------------"); System.out.println("Problem occured during installing certificate:"); e.printStackTrace(); System.out.println("----------------------------------------------"); } System.out.println("Press Enter to finish..."); try { reader.readLine(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.tc.simple.apn.quicktests.Test.java
/** * @param args//from ww w.j a v a 2s.co m */ public static void main(String[] args) { SSLSocket socket = null; try { String host = "gateway.sandbox.push.apple.com"; int port = 2195; String token = "de7f197546e41a76684f8e2d89f397ed165298d7772f4bd9b0f39c674b185b0f"; System.out.println(token.toCharArray().length); //String token = "8cebc7c08f79fa62f0994eb4298387ff930857ff8d14a50de431559cf476b223"; KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(Test.class.getResourceAsStream("egram-dev-apn.p12"), "xxxxxxxxx".toCharArray()); KeyManagerFactory keyMgrFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyMgrFactory.init(keyStore, "xxxxxxxxx".toCharArray()); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyMgrFactory.getKeyManagers(), null, null); SSLSocketFactory socketFactory = sslContext.getSocketFactory(); socket = (SSLSocket) socketFactory.createSocket(host, port); String[] cipherSuites = socket.getSupportedCipherSuites(); socket.setEnabledCipherSuites(cipherSuites); socket.startHandshake(); char[] t = token.toCharArray(); byte[] b = Hex.decodeHex(t); OutputStream outputstream = socket.getOutputStream(); String payload = "{\"aps\":{\"alert\":\"yabadabadooo\"}}"; int expiry = (int) ((System.currentTimeMillis() / 1000L) + 7200); ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bout); //command dos.writeByte(1); //id dos.writeInt(900); //expiry dos.writeInt(expiry); //token length. dos.writeShort(b.length); //token dos.write(b); //payload length dos.writeShort(payload.length()); //payload. dos.write(payload.getBytes()); byte[] byteMe = bout.toByteArray(); socket.getOutputStream().write(byteMe); socket.setSoTimeout(900); InputStream in = socket.getInputStream(); System.out.println(APNErrors.getError(in.read())); in.close(); outputstream.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:cache.reverseproxy.CacheableServicesReverseProxy.java
public static Socket createSecureSocket(String host, int port) throws IOException { SSLSocketFactory sslFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket newSocket = (SSLSocket) sslFactory.createSocket(host, port); newSocket.setUseClientMode(true);/*from www. j a va2s. c o m*/ newSocket.startHandshake(); return newSocket; }
From source file:de.fun2code.google.cloudprint.push.PushReceiver.java
/** * Starts the TSL (SSL) handshake./*from w w w .j a v a 2 s.co m*/ * * @param socket The Socket to use. * @return The SSL Socket. * @throws KeyManagementException * @throws NoSuchAlgorithmException * @throws UnrecoverableKeyException * @throws KeyStoreException * @throws IOException */ public static SSLSocket startTLSHandshake(Socket socket) throws KeyManagementException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, IOException { SSLSocket sslsocket = setupSSLSocket(socket); sslsocket.startHandshake(); return sslsocket; }