b1250c6b32ec057d5d0a457db2deb309e9bffc74
[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 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         final boolean[] ret = new boolean[1];
62         JTextArea ta = new JTextArea();
63         ta.append("Subject: " + certs[0].subject + "\n");
64         ta.append("Issuer: " + certs[0].issuer + "\n");
65         ta.append("Start Date: " + certs[0].startDate + "\n");
66         ta.append("End Date: " + certs[0].endDate + "\n");
67         ta.append("MD5: " + prettyFingerprint(certs[0].getMD5Fingerprint()) + "\n");
68         ta.append("SHA1: " + prettyFingerprint(certs[0].getSHA1Fingerprint()) + "\n");
69         ta.setEditable(false);
70         ta.setOpaque(false);
71         JScrollPane sp = new JScrollPane(ta);
72         sp.setPreferredSize(new Dimension(300,150));
73         final Object[] messages = new Object[] {
74                 "The SSL Certificate the server presented could not be verified.",
75                 exn.getMessage(),
76                 sp,
77         };
78         Runnable r = new Runnable() { public void run() {
79             int n = JOptionPane.showOptionDialog(
80                     owner,
81                     messages,
82                     "Confirm Server Certificate",
83                     0,
84                     JOptionPane.WARNING_MESSAGE,
85                     null, 
86                     new Object[] { "Accept", "Reject" },
87                     "Accept");
88             ret[0] = n == 0;
89                     
90         } };
91         if(SwingUtilities.isEventDispatchThread()) {
92             r.run();
93         } else {
94             try {
95                 SwingUtilities.invokeAndWait(r);
96             } catch(Exception e) {
97                 e.printStackTrace();
98             }
99         }
100         return ret[0];
101     }
102
103 }