2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2008-10-31 13:12:30 -04:00
|
|
|
package davmail.ui;
|
|
|
|
|
2011-06-08 15:58:29 -04:00
|
|
|
import davmail.BundleMessage;
|
2008-10-31 13:12:30 -04:00
|
|
|
import davmail.http.DavGatewayX509TrustManager;
|
2009-04-03 03:38:31 -04:00
|
|
|
import davmail.ui.tray.DavGatewayTray;
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.security.cert.X509Certificate;
|
2009-04-07 08:39:09 -04:00
|
|
|
import java.text.DateFormat;
|
2008-11-03 07:33:57 -05:00
|
|
|
import java.util.Date;
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept certificate dialog
|
|
|
|
*/
|
|
|
|
public class AcceptCertificateDialog extends JDialog {
|
|
|
|
protected boolean accepted;
|
|
|
|
|
2009-09-17 05:22:19 -04:00
|
|
|
/**
|
|
|
|
* Accept status.
|
|
|
|
*
|
|
|
|
* @return true if user accepted certificate
|
|
|
|
*/
|
2008-10-31 13:12:30 -04:00
|
|
|
public boolean isAccepted() {
|
|
|
|
return accepted;
|
|
|
|
}
|
|
|
|
|
2009-09-17 05:22:19 -04:00
|
|
|
/**
|
|
|
|
* Add a new JLabel to panel with <b>label</b>: value text.
|
|
|
|
*
|
|
|
|
* @param panel certificate details panel
|
|
|
|
* @param label certificate attribute label
|
|
|
|
* @param value certificate attribute value
|
|
|
|
*/
|
|
|
|
protected void addFieldValue(JPanel panel, String label, String value) {
|
2008-10-31 13:12:30 -04:00
|
|
|
JPanel fieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
2014-03-10 16:45:13 -04:00
|
|
|
fieldPanel.add(new JLabel("<html><b>" + label + ":</b></html>"));
|
2008-10-31 13:12:30 -04:00
|
|
|
fieldPanel.add(new JLabel(value));
|
|
|
|
panel.add(fieldPanel);
|
|
|
|
}
|
|
|
|
|
2009-09-17 05:22:19 -04:00
|
|
|
/**
|
|
|
|
* Accept certificate dialog.
|
|
|
|
*
|
|
|
|
* @param certificate certificate sent by server
|
|
|
|
*/
|
2008-10-31 13:12:30 -04:00
|
|
|
public AcceptCertificateDialog(X509Certificate certificate) {
|
2008-11-03 05:56:57 -05:00
|
|
|
setAlwaysOnTop(true);
|
2009-02-17 09:21:16 -05:00
|
|
|
String sha1Hash = DavGatewayX509TrustManager.getFormattedHash(certificate);
|
2009-04-07 08:39:09 -04:00
|
|
|
DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM);
|
2008-10-31 13:12:30 -04:00
|
|
|
|
2009-04-23 10:54:06 -04:00
|
|
|
setTitle(BundleMessage.format("UI_ACCEPT_CERTIFICATE"));
|
2008-11-03 05:56:57 -05:00
|
|
|
try {
|
|
|
|
setIconImage(DavGatewayTray.getFrameIcon());
|
|
|
|
} catch (NoSuchMethodError error) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_SET_ICON_IMAGE"));
|
2008-11-03 05:56:57 -05:00
|
|
|
}
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
JPanel subjectPanel = new JPanel();
|
|
|
|
subjectPanel.setLayout(new BoxLayout(subjectPanel, BoxLayout.Y_AXIS));
|
2009-04-23 10:54:06 -04:00
|
|
|
subjectPanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_SERVER_CERTIFICATE")));
|
|
|
|
addFieldValue(subjectPanel, BundleMessage.format("UI_ISSUED_TO"), DavGatewayX509TrustManager.getRDN(certificate.getSubjectDN()));
|
|
|
|
addFieldValue(subjectPanel, BundleMessage.format("UI_ISSUED_BY"), DavGatewayX509TrustManager.getRDN(certificate.getIssuerDN()));
|
2008-11-03 07:33:57 -05:00
|
|
|
Date now = new Date();
|
|
|
|
String notBefore = formatter.format(certificate.getNotBefore());
|
|
|
|
if (now.before(certificate.getNotBefore())) {
|
2009-09-17 05:22:19 -04:00
|
|
|
notBefore = "<html><font color=\"#FF0000\">" + notBefore + "</font></html>";
|
2008-11-03 07:33:57 -05:00
|
|
|
}
|
2009-04-23 10:54:06 -04:00
|
|
|
addFieldValue(subjectPanel, BundleMessage.format("UI_VALID_FROM"), notBefore);
|
2008-11-03 07:33:57 -05:00
|
|
|
String notAfter = formatter.format(certificate.getNotAfter());
|
|
|
|
if (now.after(certificate.getNotAfter())) {
|
2009-09-17 05:22:19 -04:00
|
|
|
notAfter = "<html><font color=\"#FF0000\">" + notAfter + "</font></html>";
|
2008-11-03 07:33:57 -05:00
|
|
|
}
|
2009-04-23 10:54:06 -04:00
|
|
|
addFieldValue(subjectPanel, BundleMessage.format("UI_VALID_UNTIL"), notAfter);
|
|
|
|
addFieldValue(subjectPanel, BundleMessage.format("UI_SERIAL"), DavGatewayX509TrustManager.getFormattedSerial(certificate));
|
|
|
|
addFieldValue(subjectPanel, BundleMessage.format("UI_FINGERPRINT"), sha1Hash);
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
JPanel warningPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
|
|
|
JLabel imageLabel = new JLabel();
|
|
|
|
imageLabel.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
|
2009-04-23 10:54:06 -04:00
|
|
|
imageLabel.setText(BundleMessage.format("UI_UNTRUSTED_CERTIFICATE_HTML"));
|
2008-10-31 13:12:30 -04:00
|
|
|
warningPanel.add(imageLabel);
|
|
|
|
add(warningPanel, BorderLayout.NORTH);
|
|
|
|
add(subjectPanel, BorderLayout.CENTER);
|
|
|
|
add(getButtonPanel(), BorderLayout.SOUTH);
|
|
|
|
setModal(true);
|
|
|
|
|
|
|
|
pack();
|
|
|
|
// center frame
|
|
|
|
setLocation(getToolkit().getScreenSize().width / 2 -
|
|
|
|
getSize().width / 2,
|
|
|
|
getToolkit().getScreenSize().height / 2 -
|
|
|
|
getSize().height / 2);
|
2012-06-01 06:39:14 -04:00
|
|
|
setAlwaysOnTop(true);
|
|
|
|
setVisible(true);
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected JPanel getButtonPanel() {
|
|
|
|
JPanel buttonPanel = new JPanel();
|
2009-04-23 10:54:06 -04:00
|
|
|
JButton accept = new JButton(BundleMessage.format("UI_BUTTON_ACCEPT"));
|
|
|
|
JButton deny = new JButton(BundleMessage.format("UI_BUTTON_DENY"));
|
2008-10-31 13:12:30 -04:00
|
|
|
accept.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent evt) {
|
|
|
|
accepted = true;
|
2010-08-06 19:07:21 -04:00
|
|
|
setVisible(false);
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
deny.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent evt) {
|
|
|
|
accepted = false;
|
2010-08-06 19:07:21 -04:00
|
|
|
setVisible(false);
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
buttonPanel.add(accept);
|
|
|
|
buttonPanel.add(deny);
|
|
|
|
return buttonPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-17 05:22:19 -04:00
|
|
|
/**
|
|
|
|
* Display certificate accept dialog and get user answer.
|
|
|
|
*
|
|
|
|
* @param certificate certificate sent by server
|
|
|
|
* @return true if user accepted certificate
|
|
|
|
*/
|
2008-10-31 13:12:30 -04:00
|
|
|
public static boolean isCertificateTrusted(final X509Certificate certificate) {
|
|
|
|
final boolean[] answer = new boolean[1];
|
|
|
|
try {
|
|
|
|
SwingUtilities.invokeAndWait(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
AcceptCertificateDialog certificateDialog = new AcceptCertificateDialog(certificate);
|
|
|
|
answer[0] = certificateDialog.isAccepted();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (InterruptedException ie) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.error(new BundleMessage("UI_ERROR_WAITING_FOR_CERTIFICATE_CHECK"), ie);
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (InvocationTargetException ite) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.error(new BundleMessage("UI_ERROR_WAITING_FOR_CERTIFICATE_CHECK"), ite);
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return answer[0];
|
|
|
|
}
|
|
|
|
}
|