~ubuntu-branches/ubuntu/natty/icedtea-web/natty-security

« back to all changes in this revision

Viewing changes to netx/net/sourceforge/jnlp/security/SecurityWarningDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-02-24 12:57:25 UTC
  • mto: (18.1.1 experimental) (19.1.1 natty-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20110224125725-8zq5v35r6o27w8ku
Tags: upstream-1.1~20110320
ImportĀ upstreamĀ versionĀ 1.1~20110320

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* SecurityWarningDialog.java
2
 
   Copyright (C) 2008 Red Hat, Inc.
3
 
 
4
 
This file is part of IcedTea.
5
 
 
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.
9
 
 
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.
14
 
 
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
18
 
02110-1301 USA.
19
 
 
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
23
 
combination.
24
 
 
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.
36
 
*/
37
 
 
38
 
package net.sourceforge.jnlp.security;
39
 
 
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;
44
 
 
45
 
import java.awt.*;
46
 
 
47
 
import javax.swing.*;
48
 
 
49
 
import java.awt.event.*;
50
 
import java.security.cert.X509Certificate;
51
 
import java.util.concurrent.CopyOnWriteArrayList;
52
 
 
53
 
import java.util.List;
54
 
 
55
 
/**
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.
60
 
 *
61
 
 * @author <a href="mailto:jsumali@redhat.com">Joshua Sumali</a>
62
 
 */
63
 
public class SecurityWarningDialog extends JDialog {
64
 
 
65
 
    /** The type of dialog we want to show */
66
 
    private DialogType dialogType;
67
 
 
68
 
    /** The type of access that this dialog is for */
69
 
    private AccessType accessType;
70
 
 
71
 
    private SecurityDialogPanel panel;
72
 
 
73
 
    /** The application file associated with this security warning */
74
 
    private JNLPFile file;
75
 
 
76
 
    private CertVerifier certVerifier;
77
 
 
78
 
    private X509Certificate cert;
79
 
 
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).
83
 
     */
84
 
    private Object[] extras;
85
 
 
86
 
    /** Whether or not this object has been fully initialized */
87
 
    private boolean initialized = false;
88
 
 
89
 
    /**
90
 
     * the return value of this dialog. result: 0 = Yes, 1 = No, 2 = Cancel,
91
 
     * null = Window closed.
92
 
     */
93
 
    private Object value;
94
 
 
95
 
    SecurityWarningDialog(DialogType dialogType, AccessType accessType,
96
 
                JNLPFile file, CertVerifier jarSigner, X509Certificate cert, Object[] extras) {
97
 
        super();
98
 
        this.dialogType = dialogType;
99
 
        this.accessType = accessType;
100
 
        this.file = file;
101
 
        this.certVerifier = jarSigner;
102
 
        this.cert = cert;
103
 
        this.extras = extras;
104
 
        initialized = true;
105
 
 
106
 
        initDialog();
107
 
    }
108
 
 
109
 
    /**
110
 
     * Construct a SecurityWarningDialog to display some sort of access warning
111
 
     */
112
 
    SecurityWarningDialog(DialogType dialogType, AccessType accessType,
113
 
                        JNLPFile file) {
114
 
        this(dialogType, accessType, file, null, null, null);
115
 
    }
116
 
 
117
 
    /**
118
 
     * Create a SecurityWarningDialog to display a certificate-related warning
119
 
     */
120
 
    SecurityWarningDialog(DialogType dialogType, AccessType accessType,
121
 
                        JNLPFile file, CertVerifier jarSigner) {
122
 
        this(dialogType, accessType, file, jarSigner, null, null);
123
 
    }
124
 
 
125
 
    /**
126
 
     * Create a SecurityWarningDialog to display a certificate-related warning
127
 
     */
128
 
    SecurityWarningDialog(DialogType dialogType, AccessType accessType,
129
 
                CertVerifier certVerifier) {
130
 
        this(dialogType, accessType, null, certVerifier, null, null);
131
 
    }
132
 
 
133
 
    /**
134
 
     * Create a SecurityWarningDialog to display some sort of access warning
135
 
     * with more information
136
 
     */
137
 
    SecurityWarningDialog(DialogType dialogType, AccessType accessType,
138
 
                        JNLPFile file, Object[] extras) {
139
 
        this(dialogType, accessType, file, null, null, extras);
140
 
    }
141
 
 
142
 
    /**
143
 
     * Create a SecurityWarningDailog to display information about a single
144
 
     * certificate
145
 
     */
146
 
    SecurityWarningDialog(DialogType dialogType, X509Certificate c) {
147
 
        this(dialogType, null, null, null, c, null);
148
 
    }
149
 
 
150
 
    /**
151
 
     * Returns if this dialog has been fully initialized yet.
152
 
     * @return true if this dialog has been initialized, and false otherwise.
153
 
     */
154
 
    public boolean isInitialized() {
155
 
        return initialized;
156
 
    }
157
 
 
158
 
    /**
159
 
     * Shows more information regarding jar code signing
160
 
     *
161
 
     * @param jarSigner the JarSigner used to verify this application
162
 
     * @param parent the parent option pane
163
 
     */
164
 
    public static void showMoreInfoDialog(
165
 
                CertVerifier jarSigner, SecurityWarningDialog parent) {
166
 
 
167
 
        SecurityWarningDialog dialog =
168
 
                        new SecurityWarningDialog(DialogType.MORE_INFO, null, null,
169
 
                                jarSigner);
170
 
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
171
 
        dialog.setVisible(true);
172
 
        dialog.dispose();
173
 
    }
174
 
 
175
 
    /**
176
 
     * Displays CertPath information in a readable table format.
177
 
     *
178
 
     * @param jarSigner the JarSigner used to verify this application
179
 
     * @param parent the parent option pane
180
 
     */
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);
188
 
        dialog.dispose();
189
 
    }
190
 
 
191
 
    /**
192
 
     * Displays a single certificate's information.
193
 
     *
194
 
     * @param c the X509 certificate.
195
 
     * @param parent the parent pane.
196
 
     */
197
 
    public static void showSingleCertInfoDialog(X509Certificate c,
198
 
                        JDialog parent) {
199
 
        SecurityWarningDialog dialog = new SecurityWarningDialog(DialogType.SINGLE_CERT_INFO, c);
200
 
        dialog.setLocationRelativeTo(parent);
201
 
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
202
 
        dialog.setVisible(true);
203
 
        dialog.dispose();
204
 
    }
205
 
 
206
 
    private void initDialog() {
207
 
        setSystemLookAndFeel();
208
 
 
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";
222
 
 
223
 
        setTitle(dialogTitle);
224
 
        setModalityType(ModalityType.MODELESS);
225
 
 
226
 
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
227
 
 
228
 
        installPanel();
229
 
 
230
 
        pack();
231
 
 
232
 
        WindowAdapter adapter = new WindowAdapter() {
233
 
            private boolean gotFocus = false;
234
 
 
235
 
            @Override
236
 
            public void windowGainedFocus(WindowEvent we) {
237
 
                // Once window gets focus, set initial focus
238
 
                if (!gotFocus) {
239
 
                    selectDefaultButton();
240
 
                    gotFocus = true;
241
 
                }
242
 
            }
243
 
 
244
 
            @Override
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);
251
 
                }
252
 
            }
253
 
        };
254
 
        addWindowListener(adapter);
255
 
        addWindowFocusListener(adapter);
256
 
 
257
 
    }
258
 
 
259
 
    public AccessType getAccessType() {
260
 
        return accessType;
261
 
    }
262
 
 
263
 
    public JNLPFile getFile() {
264
 
        return file;
265
 
    }
266
 
 
267
 
    public CertVerifier getJarSigner() {
268
 
        return certVerifier;
269
 
    }
270
 
 
271
 
    public X509Certificate getCert() {
272
 
        return cert;
273
 
    }
274
 
 
275
 
    /**
276
 
     * Adds the appropriate JPanel to this Dialog, based on {@link DialogType}.
277
 
     */
278
 
    private void installPanel() {
279
 
 
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);
294
 
 
295
 
        add(panel, BorderLayout.CENTER);
296
 
    }
297
 
 
298
 
    private static void centerDialog(JDialog dialog) {
299
 
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
300
 
        Dimension dialogSize = dialog.getSize();
301
 
 
302
 
        dialog.setLocation((screen.width - dialogSize.width) / 2,
303
 
                        (screen.height - dialogSize.height) / 2);
304
 
    }
305
 
 
306
 
    private void selectDefaultButton() {
307
 
        if (panel == null) {
308
 
            System.out.println("initial value panel is null");
309
 
        }
310
 
        panel.requestFocusOnDefaultButton();
311
 
    }
312
 
 
313
 
    protected void setValue(Object value) {
314
 
        if (JNLPRuntime.isDebug()) {
315
 
            System.out.println("Setting value:" + value);
316
 
        }
317
 
        this.value = value;
318
 
    }
319
 
 
320
 
    public Object getValue() {
321
 
        if (JNLPRuntime.isDebug()) {
322
 
            System.out.println("Returning value:" + value);
323
 
        }
324
 
        return value;
325
 
    }
326
 
 
327
 
    /**
328
 
     * Called when the SecurityWarningDialog is hidden - either because the user
329
 
     * made a choice (Ok, Cancel, etc) or closed the window
330
 
     */
331
 
    @Override
332
 
    public void dispose() {
333
 
        notifySelectionMade();
334
 
        super.dispose();
335
 
    }
336
 
 
337
 
    /**
338
 
     * Updates the look and feel of the window to be the system look and feel
339
 
     */
340
 
    protected void setSystemLookAndFeel() {
341
 
        try {
342
 
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
343
 
        } catch (Exception e) {
344
 
            //don't worry if we can't.
345
 
        }
346
 
    }
347
 
 
348
 
    private List<ActionListener> listeners = new CopyOnWriteArrayList<ActionListener>();
349
 
 
350
 
    /**
351
 
     * Notify all the listeners that the user has made a decision using this
352
 
     * security dialog.
353
 
     */
354
 
    public void notifySelectionMade() {
355
 
        for (ActionListener listener : listeners) {
356
 
            listener.actionPerformed(null);
357
 
        }
358
 
    }
359
 
 
360
 
    /**
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.
364
 
     */
365
 
    public void addActionListener(ActionListener listener) {
366
 
        listeners.add(listener);
367
 
    }
368
 
 
369
 
}