remove dependency on Swing
[org.ibex.net.git] / src / org / ibex / net / ssl / SwingVerifyCallback.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.net.ssl;
6
7 import javax.swing.*;
8
9 import java.awt.*;
10
11 import org.ibex.net.SSL;
12 import org.ibex.crypto.*;
13
14 public class SwingVerifyCallback /*extends JDialog*/ implements SSL.VerifyCallback {
15     //private Component owner;
16     
17     public SwingVerifyCallback(/*Component*/Object owner) {
18         //this.owner = owner;
19     }
20     /*
21         super(owner,"Certificate Verification",true);
22         setModal(true);
23         
24         JTextPane tp = new JTextPane();
25         doc = tp.getStyledDocument();
26         JScrollPane sp = new JScrollPane();
27         sp.setPreferredSize(new Dimension(400,300));
28         sp.setViewportView(tp);
29         sp.setAutoscrolls(false);
30
31         this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
32         JComponent bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
33         JButton accept = new JButton("Accept");
34         JButton reject = new JButton("Reject");
35         accept.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
36             accepted = true; 
37             hide();
38         }});
39         reject.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
40             accepted = false; 
41             hide();
42         }});
43         bottom.add(accept);
44         bottom.add(reject);
45         getContentPane().add(BorderLayout.CENTER,sp);
46         getContentPane().add(BorderLayout.SOUTH,bottom);
47         pack();
48     }*/
49     
50     public static String prettyFingerprint(byte[] fp) {
51         StringBuffer sb = new StringBuffer(fp.length*3);
52         for(int i=0;i<fp.length;i++) {
53             if(i>0) sb.append(":");
54             sb.append("0123456789abcdef".charAt((fp[i] & 0xf0) >>> 4));
55             sb.append("0123456789abcdef".charAt((fp[i] & 0x0f) >>> 0));
56         }
57         return sb.toString();
58     }
59     
60     public synchronized boolean checkCerts(X509.Certificate[] certs, String hostname, SSL.Exn exn) {
61         /*
62         final boolean[] ret = new boolean[1];
63         JTextArea ta = new JTextArea();
64         ta.append("Subject: " + certs[0].subject + "\n");
65         ta.append("Issuer: " + certs[0].issuer + "\n");
66         ta.append("Start Date: " + certs[0].startDate + "\n");
67         ta.append("End Date: " + certs[0].endDate + "\n");
68         ta.append("MD5: " + prettyFingerprint(certs[0].getMD5Fingerprint()) + "\n");
69         ta.append("SHA1: " + prettyFingerprint(certs[0].getSHA1Fingerprint()) + "\n");
70         ta.setEditable(false);
71         ta.setOpaque(false);
72         JScrollPane sp = new JScrollPane(ta);
73         sp.setPreferredSize(new Dimension(300,150));
74         final Object[] messages = new Object[] {
75                 "The SSL Certificate the server presented could not be verified.",
76                 exn.getMessage(),
77                 sp,
78         };
79         Runnable r = new Runnable() { public void run() {
80             int n = JOptionPane.showOptionDialog(
81                     owner,
82                     messages,
83                     "Confirm Server Certificate",
84                     0,
85                     JOptionPane.WARNING_MESSAGE,
86                     null, 
87                     new Object[] { "Accept", "Reject" },
88                     "Accept");
89             ret[0] = n == 0;
90                     
91         } };
92         if(SwingUtilities.isEventDispatchThread()) {
93             r.run();
94         } else {
95             try {
96                 SwingUtilities.invokeAndWait(r);
97             } catch(Exception e) {
98                 e.printStackTrace();
99             }
100         }
101         return ret[0];
102         */
103         return false;
104     }
105
106 }