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.
5 package org.ibex.net.ssl;
11 import org.ibex.net.SSL;
12 import org.ibex.crypto.*;
14 public class SwingVerifyCallback /*extends JDialog*/ implements SSL.VerifyCallback {
15 //private Component owner;
17 public SwingVerifyCallback(/*Component*/Object owner) {
21 super(owner,"Certificate Verification",true);
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);
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) {
39 reject.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
45 getContentPane().add(BorderLayout.CENTER,sp);
46 getContentPane().add(BorderLayout.SOUTH,bottom);
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));
60 public synchronized boolean checkCerts(X509.Certificate[] certs, String hostname, SSL.Exn exn) {
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);
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.",
79 Runnable r = new Runnable() { public void run() {
80 int n = JOptionPane.showOptionDialog(
83 "Confirm Server Certificate",
85 JOptionPane.WARNING_MESSAGE,
87 new Object[] { "Accept", "Reject" },
92 if(SwingUtilities.isEventDispatchThread()) {
96 SwingUtilities.invokeAndWait(r);
97 } catch(Exception e) {