mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Maxim Thomas
27.52.2022 4b1bcd45a69d24170872af32154801883a424090
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2009 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.quicksetup.ui;
 
import static org.opends.messages.QuickSetupMessages.*;
 
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.DN;
import org.opends.quicksetup.UserDataCertificateException;
import org.opends.quicksetup.event.MinimumSizeComponentListener;
 
/**
 * This class is used to present the user a certificate to the user in order
 * it to be accepted.
 */
public class CertificateDialog extends JDialog implements HyperlinkListener
{
  /**
   * The enumeration that defines the different answers that the user can
   * provide for this dialog.
   */
  public enum ReturnType
  {
    /** The user did not accept the certificate. */
    NOT_ACCEPTED,
    /** The user accepted the certificate only for this session. */
    ACCEPTED_FOR_SESSION,
    /** The user accepted the certificate permanently. */
    ACCEPTED_PERMANENTLY
  }
  private static final long serialVersionUID = -8989965057591475064L;
  private ReturnType returnValue = ReturnType.NOT_ACCEPTED;
  private final UserDataCertificateException ce;
  private JButton doNotAcceptButton;
  private JComponent certificateDetails;
  private JEditorPane explanationPane;
  private boolean detailsAlreadyClicked;
  private String explanationWithHideDetails;
  private String explanationWithShowDetails;
 
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  /**
   * Constructor of the certificate dialog.
   * @param parent the parent frame for this dialog.
   * @param ce the UserDataCertificateException we use to get the information
   * about the certificate that was presented and the reason why it was
   * rejected.
   */
  public CertificateDialog(JFrame parent, UserDataCertificateException ce)
  {
    super(parent);
    this.ce = ce;
    setTitle(INFO_CERTIFICATE_DIALOG_TITLE.get().toString());
    getContentPane().add(createPanel());
    setModal(true);
    pack();
    if (parent != null
        && getPreferredSize().width > parent.getWidth())
    {
      setPreferredSize(new Dimension(Math.max(parent.getWidth() - 20, 600),
          getPreferredSize().height));
    }
    pack();
    int minWidth = (int) getPreferredSize().getWidth();
    int minHeight = (int) getPreferredSize().getHeight();
    addComponentListener(new MinimumSizeComponentListener(this, minWidth,
        minHeight));
    getRootPane().setDefaultButton(doNotAcceptButton);
 
    addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosing(WindowEvent e)
      {
        doNotAccept();
      }
    });
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 
    Utilities.centerOnComponent(this, parent);
  }
 
  /**
   * Wheter the user accepted the certificate or not.
   * @return the ReturnType object defining what the user chose to do with the
   * certificate.
   */
  public ReturnType getUserAnswer()
  {
    return returnValue;
  }
 
  /**
   * Implements HyperlinkListener.  When the user clicks on a link we assume
   * that is the show details/hide details and we update the visible components
   * accordingly.
   *
   * @param e the HyperlinkEvent.
   */
  @Override
  public void hyperlinkUpdate(HyperlinkEvent e)
  {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    {
      boolean detailsVisible = !certificateDetails.isVisible();
      explanationPane.setText(detailsVisible?
          explanationWithHideDetails:explanationWithShowDetails);
      certificateDetails.setVisible(detailsVisible);
      if (detailsVisible && !detailsAlreadyClicked)
      {
        detailsAlreadyClicked = true;
        pack();
      }
    }
  }
 
  /**
   * Creates and returns the panel of the dialog.
   * @return the panel of the dialog.
   */
  private JPanel createPanel()
  {
    GridBagConstraints gbc = new GridBagConstraints();
 
    JPanel contentPanel = new JPanel(new GridBagLayout());
    contentPanel.setBackground(UIFactory.DEFAULT_BACKGROUND);
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.weightx = 1.0;
 
    JPanel topPanel = new JPanel(new GridBagLayout());
    topPanel.setBorder(UIFactory.DIALOG_PANEL_BORDER);
    topPanel.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
 
    gbc.weighty = 0.0;
    gbc.insets = UIFactory.getCurrentStepPanelInsets();
    topPanel.add(createTitlePanel(), gbc);
    gbc.insets.top = UIFactory.TOP_INSET_INSTRUCTIONS_SUBPANEL;
    topPanel.add(createTextPane(), gbc);
    certificateDetails = createCertificateDetailsPane();
    gbc.insets.top = 0;
    gbc.insets.bottom = 0;
    topPanel.add(Box.createHorizontalStrut(
        certificateDetails.getPreferredSize().width), gbc);
    gbc.insets.top = 0;
    gbc.weighty = 1.0;
    JPanel auxPanel = UIFactory.makeJPanel();
    auxPanel.setLayout(new GridBagLayout());
    gbc.weightx = 0.0;
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    auxPanel.add(Box.createVerticalStrut(100), gbc);
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    auxPanel.add(certificateDetails, gbc);
    gbc.insets = UIFactory.getCurrentStepPanelInsets();
    gbc.insets.bottom = UIFactory.TOP_INSET_INPUT_SUBPANEL;
    topPanel.add(auxPanel, gbc);
    certificateDetails.setVisible(false);
    gbc.weighty = 0.2;
    gbc.insets = UIFactory.getEmptyInsets();
    topPanel.add(Box.createVerticalGlue(), gbc);
    contentPanel.add(topPanel, gbc);
    gbc.weighty = 0.0;
    gbc.insets = UIFactory.getButtonsPanelInsets();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    contentPanel.add(createButtonsPanel(), gbc);
 
    return contentPanel;
  }
 
  /**
   * Creates and returns the title sub panel.
   * @return the title sub panel.
   */
  private Component createTitlePanel()
  {
    JPanel titlePanel = UIFactory.makeJPanel();
    titlePanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.0;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
 
    LocalizableMessage title = INFO_CERTIFICATE_TITLE.get();
    JLabel l =
        UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, title,
            UIFactory.TextStyle.TITLE);
    l.setOpaque(false);
    titlePanel.add(l, gbc);
 
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.insets.left = 0;
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    titlePanel.add(Box.createHorizontalGlue(), gbc);
 
    return titlePanel;
  }
 
  /**
   * Creates and returns the text sub panel.
   * @return the text sub panel.
   */
  private Component createTextPane()
  {
    LocalizableMessage text;
    if (ce.getType() == UserDataCertificateException.Type.NOT_TRUSTED)
    {
      text = INFO_CERTIFICATE_NOT_TRUSTED_TEXT.get(
          ce.getHost(), ce.getPort(),
          ce.getHost(), ce.getPort());
    }
    else
    {
      text = INFO_CERTIFICATE_NAME_MISMATCH_TEXT.get(
              ce.getHost(), ce.getPort(),
              ce.getHost(),
              ce.getHost(), ce.getPort(),
              ce.getHost(), ce.getPort());
    }
    JPanel p = UIFactory.makeJPanel();
    p.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    p.add(UIFactory.makeJLabel(UIFactory.IconType.WARNING_LARGE, null,
        UIFactory.TextStyle.NO_STYLE), gbc);
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
    gbc.insets.bottom = 0;
    explanationPane = UIFactory.makeHtmlPane(null, UIFactory.INSTRUCTIONS_FONT);
    explanationPane.setOpaque(false);
    explanationPane.setEditable(false);
    explanationPane.addHyperlinkListener(this);
    p.add(explanationPane, gbc);
    if (ce.getChain() != null && ce.getChain().length > 0)
    {
      LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
      mb.append(text);
      mb.append(INFO_CERTIFICATE_SHOW_DETAILS_TEXT.get());
      explanationWithShowDetails = UIFactory.applyFontToHtml(
              mb.toString(), UIFactory.INSTRUCTIONS_FONT);
      LocalizableMessageBuilder mb2 = new LocalizableMessageBuilder();
      mb2.append(text);
      mb2.append(INFO_CERTIFICATE_HIDE_DETAILS_TEXT.get());
      explanationWithHideDetails = UIFactory.applyFontToHtml(
              mb2.toString(), UIFactory.INSTRUCTIONS_FONT);
 
      explanationPane.setText(explanationWithShowDetails);
    }
    else
    {
      explanationPane.setText(text.toString());
    }
    return p;
  }
 
  /**
   * Creates and returns the buttons DO NOT ACCEPT/ACCEPT FOR THIS SESSION/
   * ACCEPT PERMANENTLY sub panel.
   * @return the buttons DO NOT ACCEPT/ACCEPT FOR THIS SESSION/ACCEPT
   * PERMANENTLY sub panel.
   */
  private Component createButtonsPanel()
  {
    JPanel buttonsPanel = UIFactory.makeJPanel();
    buttonsPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 4;
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.insets.left = UIFactory.getCurrentStepPanelInsets().left;
    buttonsPanel.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
        null, UIFactory.TextStyle.NO_STYLE), gbc);
    gbc.weightx = 1.0;
    gbc.gridwidth--;
    gbc.insets.left = 0;
    buttonsPanel.add(Box.createHorizontalGlue(), gbc);
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0.0;
    JButton acceptSessionButton = UIFactory.makeJButton(
        INFO_CERTIFICATE_DIALOG_ACCEPT_FOR_SESSION_BUTTON_LABEL.get(),
        INFO_CERTIFICATE_DIALOG_ACCEPT_FOR_SESSION_BUTTON_TOOLTIP.get());
    buttonsPanel.add(acceptSessionButton, gbc);
    acceptSessionButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent ev) {
        acceptForSession();
      }
    });
 
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
    JButton acceptPermanentlyButton = UIFactory.makeJButton(
        INFO_CERTIFICATE_DIALOG_ACCEPT_PERMANENTLY_BUTTON_LABEL.get(),
        INFO_CERTIFICATE_DIALOG_ACCEPT_PERMANENTLY_BUTTON_TOOLTIP.get());
    buttonsPanel.add(acceptPermanentlyButton, gbc);
    acceptPermanentlyButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent ev) {
        acceptPermanently();
      }
    });
 
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    doNotAcceptButton =
      UIFactory.makeJButton(
          INFO_CERTIFICATE_DIALOG_DO_NOT_ACCEPT_BUTTON_LABEL.get(),
          INFO_CERTIFICATE_DIALOG_DO_NOT_ACCEPT_BUTTON_TOOLTIP.get());
    buttonsPanel.add(doNotAcceptButton, gbc);
    doNotAcceptButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent ev)
      {
        doNotAccept();
      }
    });
 
    return buttonsPanel;
  }
 
  /**
   * Creates the panel containing a representation of the certificate chain.
   * @return the panel containing a representation of the certificate chain.
   */
  private JComponent createCertificateDetailsPane()
  {
    JPanel p = UIFactory.makeJPanel();
    p.setLayout(new GridBagLayout());
    if (ce.getChain() != null && ce.getChain().length > 0)
    {
      final JComboBox<String> combo = new JComboBox<String>();
      combo.setToolTipText(
              INFO_CERTIFICATE_CHAIN_COMBO_TOOLTIP.get().toString());
      final CardLayout cl = new CardLayout();
      final JPanel cardPanel = new JPanel(cl);
      final Map<String, JPanel> hmPanels = new HashMap<>();
 
      LocalizableMessage[] labels =
      {
          INFO_CERTIFICATE_SUBJECT_LABEL.get(),
          INFO_CERTIFICATE_ISSUED_BY_LABEL.get(),
          INFO_CERTIFICATE_VALID_FROM_LABEL.get(),
          INFO_CERTIFICATE_EXPIRES_ON_LABEL.get(),
          INFO_CERTIFICATE_TYPE_LABEL.get(),
          INFO_CERTIFICATE_SERIAL_NUMBER_LABEL.get(),
          INFO_CERTIFICATE_MD5_FINGERPRINT_LABEL.get(),
          INFO_CERTIFICATE_SHA1_FINGERPRINT_LABEL.get()
      };
 
      for (int i=0; i<ce.getChain().length; i++)
      {
        X509Certificate cert = ce.getChain()[i];
        JComponent[] components =
        {
            createSubjectComponent(cert),
            createIssuedByComponent(cert),
            createValidFromComponent(cert),
            createExpiresOnComponent(cert),
            createTypeComponent(cert),
            createSerialNumberComponent(cert),
            createMD5FingerprintComponent(cert),
            createSHA1FingerprintComponent(cert)
        };
        JPanel certPanel = UIFactory.makeJPanel();
        certPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
 
        for (int j=0; j<labels.length; j++)
        {
          JLabel l = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
              labels[j], UIFactory.TextStyle.PRIMARY_FIELD_VALID);
 
          l.setLabelFor(components[j]);
          if (j > 0)
          {
            gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
          }
          gbc.gridwidth = GridBagConstraints.RELATIVE;
          gbc.weightx = 0.0;
          gbc.insets.left = 0;
          certPanel.add(l, gbc);
          gbc.gridwidth = GridBagConstraints.REMAINDER;
          gbc.weightx = 1.0;
          gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
          certPanel.add(components[j], gbc);
        }
        String name = getName(cert);
        hmPanels.put(name, certPanel);
        cardPanel.add(name, certPanel);
        combo.addItem(name);
      }
      GridBagConstraints gbc = new GridBagConstraints();
      if (ce.getChain().length == 1)
      {
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        p.add(cardPanel, gbc);
 
        gbc.weighty = 1.0;
        p.add(Box.createVerticalGlue(), gbc);
      }
      else
      {
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JPanel auxPanel = UIFactory.makeJPanel();
        auxPanel.setLayout(new GridBagLayout());
        JLabel l = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
            INFO_CERTIFICATE_CHAIN_LABEL.get(),
            UIFactory.TextStyle.PRIMARY_FIELD_VALID);
        auxPanel.add(l, gbc);
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
        auxPanel.add(combo, gbc);
        l.setLabelFor(combo);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets.left = 0;
        gbc.weightx = 1.0;
        auxPanel.add(Box.createHorizontalGlue(), gbc);
 
        p.add(auxPanel, gbc);
 
        gbc.insets.top = UIFactory.TOP_INSET_PRIMARY_FIELD;
        gbc.fill = GridBagConstraints.BOTH;
        p.add(cardPanel, gbc);
 
        gbc.weighty = 1.0;
        p.add(Box.createVerticalGlue(), gbc);
      }
 
      combo.addActionListener(new ActionListener()
      {
        @Override
        public void actionPerformed(ActionEvent ev)
        {
          String selectedItem = (String)combo.getSelectedItem();
          cl.show(hmPanels.get(selectedItem), selectedItem);
        }
      });
    }
    JScrollPane scroll = new JScrollPane(p);
    scroll.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
    scroll.setOpaque(false);
    scroll.getViewport().setOpaque(false);
    scroll.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
        175));
    return scroll;
  }
 
  private JComponent createSubjectComponent(X509Certificate cert)
  {
    LocalizableMessage dn = LocalizableMessage.raw(cert.getSubjectX500Principal().getName());
    return makeValueLabel(dn);
  }
 
  private JComponent createIssuedByComponent(X509Certificate cert)
  {
    LocalizableMessage dn = LocalizableMessage.raw(cert.getIssuerX500Principal().getName());
    return makeValueLabel(dn);
  }
 
  private JComponent createValidFromComponent(X509Certificate cert)
  {
    JComponent c;
 
    Date date = cert.getNotBefore();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
        DateFormat.SHORT);
    LocalizableMessage value = LocalizableMessage.raw(df.format(date));
    long t1 = System.currentTimeMillis();
    long t2 = date.getTime();
    boolean isNotValidYet = t1 < t2;
 
    if (isNotValidYet)
    {
      c = UIFactory.makeJLabel(UIFactory.IconType.ERROR,
          INFO_CERTIFICATE_NOT_VALID_YET.get(value),
          UIFactory.TextStyle.SECONDARY_FIELD_INVALID);
    }
    else
    {
      c = makeValueLabel(value);
    }
    return c;
  }
 
 
  private JComponent createExpiresOnComponent(X509Certificate cert)
  {
    JComponent c;
 
    Date date = cert.getNotAfter();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
    DateFormat.SHORT);
    LocalizableMessage value = LocalizableMessage.raw(df.format(date));
    long t1 = System.currentTimeMillis();
    long t2 = date.getTime();
    boolean isExpired = t1 > t2;
 
    if (isExpired)
    {
      c = UIFactory.makeJLabel(UIFactory.IconType.ERROR,
          INFO_CERTIFICATE_EXPIRED.get(value),
          UIFactory.TextStyle.SECONDARY_FIELD_INVALID);
    }
    else
    {
      c = makeValueLabel(value);
    }
    return c;
  }
 
 
  private JComponent createTypeComponent(X509Certificate cert)
  {
    LocalizableMessage type = LocalizableMessage.raw(cert.getType());
    return makeValueLabel(type);
  }
 
  private JComponent createSerialNumberComponent(X509Certificate cert)
  {
    LocalizableMessage serialNumber = LocalizableMessage.raw(String.valueOf(cert.getSerialNumber()));
    return makeValueLabel(serialNumber);
  }
 
 
  /**
   * Returns the LocalizableMessage representation of the SHA1 fingerprint.
   * @param cert the certificate object.
   * @return the LocalizableMessage representation of the SHA1 fingerprint.
   */
  private static LocalizableMessage getSHA1FingerPrint(X509Certificate cert)
  {
    return getFingerPrint(cert, "SHA1");
  }
 
  /**
   * Returns the LocalizableMessage representation of the MD5 fingerprint.
   * @param cert the certificate object.
   * @return the LocalizableMessage representation of the MD5 fingerprint.
   */
  private static LocalizableMessage getMD5FingerPrint(X509Certificate cert)
  {
    return getFingerPrint(cert, "MD5");
  }
 
  private static LocalizableMessage getFingerPrint(X509Certificate cert, String algorithm)
  {
    try {
      MessageDigest md = MessageDigest.getInstance(algorithm);
      byte[] b = md.digest(cert.getEncoded());
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < b.length; i++)
      {
        if (i > 0)
        {
          sb.append(":");
        }
        sb.append(Integer.toHexString(b[i] & 0xFF));
      }
      return LocalizableMessage.raw(sb);
    }
    catch (NoSuchAlgorithmException nsae) {
      logger.warn(LocalizableMessage.raw(algorithm + " algorithm not supported: " + nsae, nsae));
      return null;
    }
    catch (CertificateEncodingException cee) {
      logger.warn(LocalizableMessage.raw("Certificate encoding exception: "+cee, cee));
      return null;
    }
  }
 
  private JComponent createSHA1FingerprintComponent(X509Certificate cert)
  {
    return UIFactory.makeTextPane(getSHA1FingerPrint(cert),
        UIFactory.TextStyle.SECONDARY_FIELD_VALID);
  }
 
  private JComponent createMD5FingerprintComponent(X509Certificate cert)
  {
    return UIFactory.makeTextPane(getMD5FingerPrint(cert),
        UIFactory.TextStyle.SECONDARY_FIELD_VALID);
  }
 
  private JLabel makeValueLabel(LocalizableMessage value)
  {
    if (value == null)
    {
      value = INFO_NOT_AVAILABLE_LABEL.get();
    }
    return UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, value,
        UIFactory.TextStyle.SECONDARY_FIELD_VALID);
  }
 
  private String getName(X509Certificate cert)
  {
    String name = cert.getSubjectX500Principal().getName();
    try
    {
      DN dn = DN.valueOf(name);
      return dn.rdn().getFirstAVA().getAttributeValue().toString();
    }
    catch (Throwable t)
    {
      logger.warn(LocalizableMessage.raw("Error parsing subject dn: "+
          cert.getSubjectX500Principal(), t));
      return name;
    }
  }
 
  /** Method called when user clicks on ok. */
  private void acceptForSession()
  {
    returnValue = ReturnType.ACCEPTED_FOR_SESSION;
    dispose();
  }
 
  /** Method called when user clicks on cancel. */
  private void doNotAccept()
  {
    returnValue = ReturnType.NOT_ACCEPTED;
    dispose();
  }
 
  /** Method called when user clicks on ok. */
  private void acceptPermanently()
  {
    returnValue = ReturnType.ACCEPTED_PERMANENTLY;
    dispose();
  }
}