aboutsummaryrefslogblamecommitdiffstats
path: root/embed/mozilla/GtkNSSDialogs.cpp
blob: 8b3613d2b75fa52053112a249cc121b9a195f46b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444



























































































































































































































































































































































































































































                                                                                              
/*
 *  GtkNSSDialogs.cpp
 *
 *  Copyright (C) 2003 Crispin Flowerday <gnome@flowerday.cx>
 *
 *  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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * This file provides Gtk implementations of the mozilla Certificate dialogs
 * such as the ones displayed when connecting to a site with a self-signed
 * or expired certificate.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_MOZILLA_PSM

#include "MozillaPrivate.h"

#include "nsIX509Cert.h"
#include "nsICertificateDialogs.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIX509CertValidity.h"

#include <gtk/gtkdialog.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkcheckbutton.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtkalignment.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkimage.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkeditable.h>
#include <gtk/gtktable.h>

#include <libgnome/gnome-i18n.h>

#include "GtkNSSDialogs.h"

#include <time.h>

enum
{
    NSSDIALOG_RESPONSE_VIEW_CERT = 10
};

GtkNSSDialogs::GtkNSSDialogs ()
{
}

GtkNSSDialogs::~GtkNSSDialogs ()
{
}

NS_IMPL_ISUPPORTS1 (GtkNSSDialogs, nsIBadCertListener)

/**
 *  Call the mozilla service to display a certificate
 */
static void
view_certificate (nsIInterfaceRequestor *ctx, nsIX509Cert *cert)
{
    nsresult rv;
    nsCOMPtr<nsICertificateDialogs> certDialogs =
        do_GetService (NS_CERTIFICATEDIALOGS_CONTRACTID, &rv);
    g_return_if_fail (NS_SUCCEEDED (rv));

    certDialogs->ViewCert (ctx, cert);
}

/**
 *  Display a dialog box, showing 'View Certificate', 'Cancel',
 *  and 'Accept' buttons. Optionally a checkbox can be shown,
 *  or the text can be NULL to avoid it being displayed
 * 
 *  @returns: GTK_RESPONSE_ACCEPT if the user clicked Accept
 */
static gint
display_cert_warning_box (nsIInterfaceRequestor *ctx, 
                          nsIX509Cert *cert,
                          const char *markup_text,
                          const char *checkbox_text,
                          gboolean *checkbox_value,
              const char *affirmative_text)
{
        GtkWidget *dialog, *hbox, *label, *image, *checkbox;
    int res;

    nsCOMPtr<nsIDOMWindow> parent = do_GetInterface (ctx);
    GtkWidget *gparent = MozillaFindGtkParent (parent);

        g_return_val_if_fail (GTK_IS_WINDOW (gparent), GTK_RESPONSE_CANCEL);
        g_return_val_if_fail (markup_text, GTK_RESPONSE_CANCEL);
        g_return_val_if_fail (!checkbox_text || checkbox_value, GTK_RESPONSE_CANCEL);

    dialog = gtk_dialog_new_with_buttons ("",
                          GTK_WINDOW (gparent),
                          GTK_DIALOG_NO_SEPARATOR,
                          NULL);

    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
    gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);

    /* Add the buttons */
        gtk_dialog_add_button (GTK_DIALOG (dialog), _("_View Certificate"),
                               NSSDIALOG_RESPONSE_VIEW_CERT);

        gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL,
                               GTK_RESPONSE_CANCEL);

    if (affirmative_text == NULL)
    {
        affirmative_text = _("_Accept");
    }

        gtk_dialog_add_button (GTK_DIALOG (dialog), 
                   affirmative_text,
                               GTK_RESPONSE_ACCEPT);

        /* Create the actual widgets that go in the display part of the dialog */
        hbox = gtk_hbox_new (FALSE, 12);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
    gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), hbox);

        image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING,
                                          GTK_ICON_SIZE_DIALOG);
    gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
    gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

        label = gtk_label_new (NULL);
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
        gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);

        if (checkbox_text)
    {
        GtkWidget *vbox = gtk_vbox_new (FALSE, 12);
        gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
        
        checkbox = gtk_check_button_new_with_mnemonic (checkbox_text);
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox), 
                          *checkbox_value);
        gtk_box_pack_start (GTK_BOX (vbox), checkbox, TRUE, TRUE, 0);
    }
    else
    {
        checkbox = 0;
        gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
    }

    gtk_label_set_markup (GTK_LABEL (label), markup_text);
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
    gtk_widget_show_all (dialog);

    while (1)
    {
        res = gtk_dialog_run (GTK_DIALOG (dialog));
        if (res == NSSDIALOG_RESPONSE_VIEW_CERT)
        {
                      view_certificate (ctx, cert);
              continue;
        }
    
        break;
    }

    if (res == GTK_RESPONSE_ACCEPT && checkbox)
    {
        *checkbox_value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbox));
    }

    gtk_widget_destroy (dialog);
        return res;
}


NS_IMETHODIMP
GtkNSSDialogs::ConfirmMismatchDomain (nsIInterfaceRequestor *ctx,
                                      const nsACString &targetURL,
                                      nsIX509Cert *cert, PRBool *_retval)
{
    nsAutoString commonName;
    char *ttTargetUrl, *ttCommonName, *first, *second, *msg;
    int res;

    cert->GetCommonName (commonName);

    ttTargetUrl = g_strdup_printf ("\"<tt>%s</tt>\"", 
                                        PromiseFlatCString(targetURL).get());

    ttCommonName = g_strdup_printf ("\"<tt>%s</tt>\"", 
                                         NS_ConvertUCS2toUTF8(commonName).get());

        first = g_strdup_printf (_("The site %s returned security information for "
                   "%s. It is possible that someone is intercepting "
                   "your communication to obtain your confidential "
                   "information."),
                 ttTargetUrl, ttCommonName);

        second = g_strdup_printf (_("You should only accept the security information if you "
                                    "trust %s and %s."),
                                  ttTargetUrl, ttCommonName);

    msg = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s",
                   _("Accept incorrect security information?"),
                   first, second);

        res = display_cert_warning_box (ctx, cert, msg, NULL, NULL, NULL);

    g_free (ttTargetUrl);
    g_free (ttCommonName);
        g_free (second);
        g_free (first);
        g_free (msg);

    *_retval = (res == GTK_RESPONSE_ACCEPT);
    return NS_OK;
}


NS_IMETHODIMP
GtkNSSDialogs::ConfirmUnknownIssuer (nsIInterfaceRequestor *ctx,
                                     nsIX509Cert *cert, PRInt16 *outAddType,
                                     PRBool *_retval)
{
    gboolean accept_perm = FALSE;
    nsAutoString commonName;
    char *ttCommonName, *secondary, *tertiary, *msg;
    int res;

    cert->GetCommonName (commonName);

    ttCommonName = g_strdup_printf ("\"<tt>%s</tt>\"", 
                    NS_ConvertUCS2toUTF8(commonName).get());

    secondary = g_strdup_printf
                   (_("Your browser was unable to trust %s. "
                  "It is possible that someone is intercepting your "
                  "communication to obtain your confidential information."),
                  ttCommonName);

        tertiary = g_strdup_printf
                   (_("You should only connect to the site if you are certain "
                  "you are connected to %s."),
                ttCommonName);

    msg = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s",
                   _("Connect to untrusted site?"),
                   secondary, tertiary);

    res = display_cert_warning_box (ctx, cert, msg, 
                    _("_Don't show this message again for this site"),
                    &accept_perm, _("Co_nnect"));
    g_free (ttCommonName);
        g_free (tertiary);
        g_free (secondary);
        g_free (msg);

        if (res != GTK_RESPONSE_ACCEPT)
    {
        *_retval = PR_FALSE;
        *outAddType = UNINIT_ADD_FLAG;
    }
    else
    {
        if (accept_perm)
        {
            *_retval    = PR_TRUE;
            *outAddType = ADD_TRUSTED_PERMANENTLY;
        }
        else
        {
            *_retval    = PR_TRUE;
            *outAddType = ADD_TRUSTED_FOR_SESSION;
        }
    }

    return NS_OK;
}


/* boolean confirmCertExpired (in nsIInterfaceRequestor socketInfo, 
   in nsIX509Cert cert); */
NS_IMETHODIMP 
GtkNSSDialogs::ConfirmCertExpired (nsIInterfaceRequestor *ctx,
                                   nsIX509Cert *cert, PRBool *_retval)
{
    nsresult rv;
    PRTime now = PR_Now();
    PRTime notAfter, notBefore, timeToUse;
    PRInt64 normalizedTime;
    nsAutoString commonName;
    time_t t;
    struct tm tm;
    char formattedDate[32];
    const char *primary, *text;
    char *ttCommonName, *secondary, *msg;

    *_retval = PR_FALSE;
    
    nsCOMPtr<nsIX509CertValidity> validity;
    rv = cert->GetValidity (getter_AddRefs(validity));
    if (NS_FAILED(rv)) return rv;
    
    rv = validity->GetNotAfter (&notAfter);
    if (NS_FAILED(rv)) return rv;
    
    rv = validity->GetNotBefore (&notBefore);
    if (NS_FAILED(rv)) return rv;
    
    if (LL_CMP(now, >, notAfter)) {
        primary = _("Accept expired security information?");
        text    = _("The security information for %s "
                "expired on %s.");
        timeToUse = notAfter;
    } else {
        primary = _("Accept not yet valid security information?");
        text    = _("The security information for %s isn't valid until %s.");
        timeToUse = notBefore;
    }
    
    cert->GetCommonName (commonName);

    LL_DIV (normalizedTime, timeToUse, PR_USEC_PER_SEC);
    LL_L2UI (t, normalizedTime);
    strftime (formattedDate, sizeof(formattedDate), _("%a %-d %b %Y"), 
          localtime_r (&t, &tm));

    ttCommonName = g_strdup_printf ("\"<tt>%s</tt>\"", 
                                        NS_ConvertUCS2toUTF8(commonName).get());

    secondary = g_strdup_printf (text, ttCommonName, formattedDate);

    msg = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s",
                   primary, secondary, 
                   _("You should ensure that your computer's time is correct."));

        int res = display_cert_warning_box (ctx, cert, msg, NULL, NULL, NULL);

    g_free (msg);
    g_free (secondary);
    g_free (ttCommonName);

    *_retval = (res == GTK_RESPONSE_ACCEPT);
    
    return NS_OK;
}

/* void notifyCrlNextupdate (in nsIInterfaceRequestor socketInfo, 
   in AUTF8String targetURL, in nsIX509Cert cert); */
NS_IMETHODIMP 
GtkNSSDialogs::NotifyCrlNextupdate (nsIInterfaceRequestor *ctx,
                                    const nsACString & targetURL, nsIX509Cert *cert)
{
    GtkWidget *dialog, *image, *hbox, *label;
        char *ttCommonName, *ttTargetUrl, *primary, *secondary, *msg;
        nsAutoString commonName;

    nsCOMPtr<nsIDOMWindow> parent = do_GetInterface (ctx);
    GtkWidget *gparent = MozillaFindGtkParent (parent);

    dialog = gtk_dialog_new_with_buttons ("",
                          GTK_WINDOW (gparent),
                          GTK_DIALOG_NO_SEPARATOR,
                          GTK_STOCK_OK,
                          GTK_RESPONSE_OK,
                          NULL);

    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
    gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);

        hbox = gtk_hbox_new (FALSE, 12);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
    gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), hbox);

        image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR, 
                                          GTK_ICON_SIZE_DIALOG);
    gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
    gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

        label = gtk_label_new (NULL);
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
        gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
    gtk_misc_set_alignment (GTK_MISC (image), 0.0, 0.0);
    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);

    cert->GetCommonName (commonName);

    ttCommonName = g_strdup_printf ("\"<tt>%s</tt>\"", 
                                        NS_ConvertUCS2toUTF8(commonName).get());

    ttTargetUrl = g_strdup_printf ("\"<tt>%s</tt>\"", 
                                       PromiseFlatCString(targetURL).get());

    primary = g_strdup_printf (_("Cannot establish connection to %s"),
                                   ttTargetUrl);

    secondary = g_strdup_printf (_("The certificate revocation list (CRL) from %s "
                                       "needs to be updated."),
                                     ttCommonName);
    msg = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s",
                               primary, secondary,
                               _("Please ask your system administrator for assistance."));
    
    gtk_label_set_markup (GTK_LABEL (label), msg);
    
    g_free (msg);
    g_free (primary);
    g_free (secondary);
    g_free (ttCommonName);
    g_free (ttTargetUrl);

    gtk_widget_show_all (dialog);

    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
    return NS_OK;
}

#endif