more advanced make check
[org.ibex.crypto.git] / src / org / ibex / net / ssl / Test.java
1 package org.ibex.net.ssl;
2
3 import org.ibex.net.SSL;
4 import java.io.*;
5
6 public class Test {
7     public static void main(String[] args) throws Exception {
8         SSL.debugOn = true;
9         if(args.length == 1 && args[0].equals("check")) System.exit(check());
10         if(args.length < 2) { System.err.println("Usage: SSL host port"); }
11         String host = args[0];
12         int port = Integer.parseInt(args[1]);
13         SSL ssl = new SSL(host,port);
14         //ssl.setTLS(false);
15         ssl.getOutputStream().write(SSL.getBytes("GET / HTTP/1.0\r\nHost: " + host + "\r\n\r\n"));
16         cat(ssl.getInputStream());
17         ssl.close();
18         
19         // try to resume
20         ssl = new SSL(host,port,ssl.getSessionState());
21         ssl.getOutputStream().write(SSL.getBytes("GET / HTTP/1.0\r\nHost: " + host + "\r\n\r\n"));
22         cat(ssl.getInputStream());
23         ssl.close();
24     }
25     private static void cat(InputStream is) throws IOException { cat(is,100,null); }
26     private static void cat(InputStream is, int count, String check) throws IOException {
27         BufferedReader br = new BufferedReader(new InputStreamReader(is));
28         String line;
29         try {
30             while((line = br.readLine()) != null && --count >= 0) {
31                 if(check != null) {
32                     if(!line.startsWith(check)) throw new Error("\"" + check + "\" check failed");
33                     check = null;
34                 }
35                 System.out.println(line);
36             }
37         } catch(SSL.PrematureCloseExn e) { /* ignore */ }
38     }
39     public static int check() throws Exception {
40         byte[] ciphers = new byte[] {
41             SSL.TLS_RSA_WITH_AES_256_CBC_SHA,SSL.TLS_RSA_WITH_AES_128_CBC_SHA,
42             SSL.SSL_RSA_WITH_RC4_128_SHA,SSL.SSL_RSA_WITH_RC4_128_MD5
43         };
44         String[] hosts = new String[] { 
45             "ssl.brianweb.net", "www.zaks.com", "www.paypal.com", "www99.americanexpress.com", "www.cnbank.com", "gmail.google.com"
46         };
47         int[] blacklisted = new int[] { 0,3,2,3,3,0 };
48         for(int i=0;i<hosts.length;i++) {
49             for(int j=0;j<ciphers.length;j++) {
50                 for(int tls=0;tls<2;tls++) {
51                     String host = hosts[i];
52                     byte[] cipherPref = new byte[] {ciphers[j]};
53                     if((blacklisted[i]&(1<<j)) != 0) continue;
54                     if(j<2 && tls==0) continue;
55                     System.err.println("Testing " + host + " with " + Integer.toHexString(ciphers[j]) + " tls: " + (tls==0?"off":"on"));
56                     SSL ssl = new SSL(host,443,false);
57                     ssl.setTLS(tls!=0);
58                     ssl.negotiate(cipherPref);
59                     ssl.getOutputStream().write(SSL.getBytes("GET / HTTP/1.0\r\nHost: " + host + "\r\n\r\n"));
60                     cat(ssl.getInputStream(),5,"HTTP/1.");
61                     ssl.close();
62                 }
63             }
64         }
65         return 0;
66     }
67 }