1
/* SecurityWarningDialog.java
2
Copyright (C) 2008 Red Hat, Inc.
4
This file is part of IcedTea.
6
IcedTea is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, version 2.
10
IcedTea is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with IcedTea; see the file COPYING. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
Linking this library statically or dynamically with other modules is
21
making a combined work based on this library. Thus, the terms and
22
conditions of the GNU General Public License cover the whole
25
As a special exception, the copyright holders of this library give you
26
permission to link this library with independent modules to produce an
27
executable, regardless of the license terms of these independent
28
modules, and to copy and distribute the resulting executable under
29
terms of your choice, provided that you also meet, for each linked
30
independent module, the terms and conditions of the license of that
31
module. An independent module is a module which is not derived from
32
or based on this library. If you modify this library, you may extend
33
this exception to your version of the library, but you are not
34
obligated to do so. If you do not wish to do so, delete this
35
exception statement from your version.
38
package net.sourceforge.jnlp.security;
40
import net.sourceforge.jnlp.JNLPFile;
41
import net.sourceforge.jnlp.runtime.JNLPRuntime;
42
import net.sourceforge.jnlp.security.SecurityWarning.AccessType;
43
import net.sourceforge.jnlp.security.SecurityWarning.DialogType;
49
import java.awt.event.*;
50
import java.security.cert.X509Certificate;
51
import java.util.concurrent.CopyOnWriteArrayList;
53
import java.util.List;
56
* Provides methods for showing security warning dialogs for a wide range of
57
* JNLP security issues. Note that the security dialogs should be running in the
58
* secure AppContext - this class should not be used directly from an applet or
59
* application. See {@link SecurityWarning} for a way to show security dialogs.
61
* @author <a href="mailto:jsumali@redhat.com">Joshua Sumali</a>
63
public class SecurityWarningDialog extends JDialog {
65
/** The type of dialog we want to show */
66
private DialogType dialogType;
68
/** The type of access that this dialog is for */
69
private AccessType accessType;
71
private SecurityDialogPanel panel;
73
/** The application file associated with this security warning */
74
private JNLPFile file;
76
private CertVerifier certVerifier;
78
private X509Certificate cert;
80
/** An optional String array that's only necessary when a dialog
81
* label requires some parameters (e.g. showing which address an application
82
* is trying to connect to).
84
private Object[] extras;
86
/** Whether or not this object has been fully initialized */
87
private boolean initialized = false;
90
* the return value of this dialog. result: 0 = Yes, 1 = No, 2 = Cancel,
91
* null = Window closed.
95
SecurityWarningDialog(DialogType dialogType, AccessType accessType,
96
JNLPFile file, CertVerifier jarSigner, X509Certificate cert, Object[] extras) {
98
this.dialogType = dialogType;
99
this.accessType = accessType;
101
this.certVerifier = jarSigner;
103
this.extras = extras;
110
* Construct a SecurityWarningDialog to display some sort of access warning
112
SecurityWarningDialog(DialogType dialogType, AccessType accessType,
114
this(dialogType, accessType, file, null, null, null);
118
* Create a SecurityWarningDialog to display a certificate-related warning
120
SecurityWarningDialog(DialogType dialogType, AccessType accessType,
121
JNLPFile file, CertVerifier jarSigner) {
122
this(dialogType, accessType, file, jarSigner, null, null);
126
* Create a SecurityWarningDialog to display a certificate-related warning
128
SecurityWarningDialog(DialogType dialogType, AccessType accessType,
129
CertVerifier certVerifier) {
130
this(dialogType, accessType, null, certVerifier, null, null);
134
* Create a SecurityWarningDialog to display some sort of access warning
135
* with more information
137
SecurityWarningDialog(DialogType dialogType, AccessType accessType,
138
JNLPFile file, Object[] extras) {
139
this(dialogType, accessType, file, null, null, extras);
143
* Create a SecurityWarningDailog to display information about a single
146
SecurityWarningDialog(DialogType dialogType, X509Certificate c) {
147
this(dialogType, null, null, null, c, null);
151
* Returns if this dialog has been fully initialized yet.
152
* @return true if this dialog has been initialized, and false otherwise.
154
public boolean isInitialized() {
159
* Shows more information regarding jar code signing
161
* @param jarSigner the JarSigner used to verify this application
162
* @param parent the parent option pane
164
public static void showMoreInfoDialog(
165
CertVerifier jarSigner, SecurityWarningDialog parent) {
167
SecurityWarningDialog dialog =
168
new SecurityWarningDialog(DialogType.MORE_INFO, null, null,
170
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
171
dialog.setVisible(true);
176
* Displays CertPath information in a readable table format.
178
* @param jarSigner the JarSigner used to verify this application
179
* @param parent the parent option pane
181
public static void showCertInfoDialog(CertVerifier jarSigner,
182
SecurityWarningDialog parent) {
183
SecurityWarningDialog dialog = new SecurityWarningDialog(DialogType.CERT_INFO,
184
null, null, jarSigner);
185
dialog.setLocationRelativeTo(parent);
186
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
187
dialog.setVisible(true);
192
* Displays a single certificate's information.
194
* @param c the X509 certificate.
195
* @param parent the parent pane.
197
public static void showSingleCertInfoDialog(X509Certificate c,
199
SecurityWarningDialog dialog = new SecurityWarningDialog(DialogType.SINGLE_CERT_INFO, c);
200
dialog.setLocationRelativeTo(parent);
201
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
202
dialog.setVisible(true);
206
private void initDialog() {
207
setSystemLookAndFeel();
209
String dialogTitle = "";
210
if (dialogType == DialogType.CERT_WARNING)
211
dialogTitle = "Warning - Security";
212
else if (dialogType == DialogType.MORE_INFO)
213
dialogTitle = "More Information";
214
else if (dialogType == DialogType.CERT_INFO)
215
dialogTitle = "Details - Certificate";
216
else if (dialogType == DialogType.ACCESS_WARNING)
217
dialogTitle = "Security Warning";
218
else if (dialogType == DialogType.APPLET_WARNING)
219
dialogTitle = "Applet Warning";
220
else if (dialogType == DialogType.NOTALLSIGNED_WARNING)
221
dialogTitle = "Security Warning";
223
setTitle(dialogTitle);
224
setModalityType(ModalityType.MODELESS);
226
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
232
WindowAdapter adapter = new WindowAdapter() {
233
private boolean gotFocus = false;
236
public void windowGainedFocus(WindowEvent we) {
237
// Once window gets focus, set initial focus
239
selectDefaultButton();
245
public void windowOpened(WindowEvent e) {
246
if (e.getSource() instanceof SecurityWarningDialog) {
247
SecurityWarningDialog dialog = (SecurityWarningDialog) e.getSource();
248
dialog.setResizable(true);
249
centerDialog(dialog);
250
dialog.setValue(null);
254
addWindowListener(adapter);
255
addWindowFocusListener(adapter);
259
public AccessType getAccessType() {
263
public JNLPFile getFile() {
267
public CertVerifier getJarSigner() {
271
public X509Certificate getCert() {
276
* Adds the appropriate JPanel to this Dialog, based on {@link DialogType}.
278
private void installPanel() {
280
if (dialogType == DialogType.CERT_WARNING)
281
panel = new CertWarningPane(this, this.certVerifier);
282
else if (dialogType == DialogType.MORE_INFO)
283
panel = new MoreInfoPane(this, this.certVerifier);
284
else if (dialogType == DialogType.CERT_INFO)
285
panel = new CertsInfoPane(this, this.certVerifier);
286
else if (dialogType == DialogType.SINGLE_CERT_INFO)
287
panel = new SingleCertInfoPane(this, this.certVerifier);
288
else if (dialogType == DialogType.ACCESS_WARNING)
289
panel = new AccessWarningPane(this, extras, this.certVerifier);
290
else if (dialogType == DialogType.APPLET_WARNING)
291
panel = new AppletWarningPane(this, this.certVerifier);
292
else if (dialogType == DialogType.NOTALLSIGNED_WARNING)
293
panel = new NotAllSignedWarningPane(this);
295
add(panel, BorderLayout.CENTER);
298
private static void centerDialog(JDialog dialog) {
299
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
300
Dimension dialogSize = dialog.getSize();
302
dialog.setLocation((screen.width - dialogSize.width) / 2,
303
(screen.height - dialogSize.height) / 2);
306
private void selectDefaultButton() {
308
System.out.println("initial value panel is null");
310
panel.requestFocusOnDefaultButton();
313
protected void setValue(Object value) {
314
if (JNLPRuntime.isDebug()) {
315
System.out.println("Setting value:" + value);
320
public Object getValue() {
321
if (JNLPRuntime.isDebug()) {
322
System.out.println("Returning value:" + value);
328
* Called when the SecurityWarningDialog is hidden - either because the user
329
* made a choice (Ok, Cancel, etc) or closed the window
332
public void dispose() {
333
notifySelectionMade();
338
* Updates the look and feel of the window to be the system look and feel
340
protected void setSystemLookAndFeel() {
342
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
343
} catch (Exception e) {
344
//don't worry if we can't.
348
private List<ActionListener> listeners = new CopyOnWriteArrayList<ActionListener>();
351
* Notify all the listeners that the user has made a decision using this
354
public void notifySelectionMade() {
355
for (ActionListener listener : listeners) {
356
listener.actionPerformed(null);
361
* Adds an {@link ActionListener} which will be notified if the user makes a
362
* choice using this SecurityWarningDialog. The listener should use {@link #getValue()}
363
* to actually get the user's response.
365
public void addActionListener(ActionListener listener) {
366
listeners.add(listener);