aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/gui/RevokeDialog.java
blob: 19c7f1b1cbd330b991fa46907dd9519c357c2298 (plain)
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
package ui.gui;

import annotations.Assoc;
import model.asn1.ASN1Object;
import model.asn1.UtcTime;
import model.pki.cert.Certificate;
import model.pki.crl.Reason;
import model.pki.crl.RevokedCertificate;
import ui.gui.widgets.GCBuilder;
import ui.gui.widgets.UIUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;

import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.WEST;
import static ui.gui.widgets.UIUtils.alert;

/**
 * A dialog that presents user with cert info, revocation reason, and revocation time.
 * +----------------------------+
 * |Revoking: CN=xyz (Serial: 1)|
 * |Reason:   (Drop Down)       |
 * |Time:     (ISO-8601 text)   |
 * |                            |
 * |               Revoke Cancel|
 * +----------------------------+
 */
public class RevokeDialog extends JDialog {
    /**
     * ISO8601
     */
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

    /**
     * The incoming certificate.
     */
    private final Certificate crt;

    /**
     * The result.
     */
    private RevokedCertificate res;


    /**
     * Root pane
     */
    @Assoc(partOf = true)
    private JPanel contentPane;

    /**
     * OK button
     */
    @Assoc(partOf = true)
    private JButton buttonOK;

    /**
     * Cancel button
     */
    @Assoc(partOf = true)
    private JButton buttonCancel;

    /**
     * Reason
     */
    @Assoc(partOf = true)
    private JComboBox<String> comboBoxReason;

    /**
     * Subject (not editable)
     */
    @Assoc(partOf = true)
    private JTextField textFieldSubject;

    /**
     * Time
     */
    @Assoc(partOf = true)
    private JFormattedTextField formattedTextFieldTime;

    /**
     * EFFECTS: Init GUI with the given cert.
     */
    public RevokeDialog(Certificate crt) {
        this.crt = crt;

        contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        contentPane.add(renderForm());

        contentPane.add(UIUtils.createActionsPane(buttonOK = new JButton("Revoke"),
                        buttonCancel = new JButton("Cancel")));
        buttonOK.addActionListener(this::onOK);
        buttonCancel.addActionListener(this::onCancel);

        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        setTitle("Revoke certificate");

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel(null);
            }
        });

        contentPane.registerKeyboardAction(this::onCancel,
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    /**
     * EFFECTS: Init GUI.
     * MODIFIES: this
     */
    private JPanel renderForm() {
        final JPanel panelForm = new JPanel();
        panelForm.setLayout(new GridBagLayout());

        panelForm.add(new JLabel("Revoking: "), new GCBuilder().anchor(WEST).build());
        panelForm.add(new JLabel("Reason: "), new GCBuilder().gridY(1).anchor(WEST).build());
        panelForm.add(new JLabel("Time: "), new GCBuilder().gridY(2).anchor(WEST).build());

        textFieldSubject = new JTextField(String.format("%s (Serial: %d)",
                crt.getCertificate().getSubject().toString(), crt.getCertificate().getSerialNumber().getLong()));
        textFieldSubject.setEditable(false);
        panelForm.add(textFieldSubject, new GCBuilder().gridX(1).anchor(WEST).fill(HORIZONTAL).build());

        comboBoxReason = new JComboBox<>(Arrays.stream(Reason.values()).map(Enum::toString).toArray(String[]::new));
        panelForm.add(comboBoxReason, new GCBuilder().gridXY(1, 1).anchor(WEST).fill(HORIZONTAL).build());

        formattedTextFieldTime = new JFormattedTextField(DATE_FORMAT.toFormat());
        formattedTextFieldTime.setText(ZonedDateTime.now().format(DATE_FORMAT));
        panelForm.add(formattedTextFieldTime, new GCBuilder().gridXY(1, 2).anchor(WEST).fill(HORIZONTAL).build());

        panelForm.add(new JPanel(), new GCBuilder().gridXY(1, 3).expandXY().fill(HORIZONTAL).build());
        return panelForm;
    }

    /**
     * EFFECTS: Validate form, set result, and close dialog.
     *          Time must be a valid ISO-8601 string with offset, and it will be converted into UTC.
     *          Reason must be supported.
     * MODIFIES: this
     */
    private void onOK(ActionEvent ev) {
        try {
            res = new RevokedCertificate(ASN1Object.TAG_SEQUENCE, null,
                    crt.getCertificate().getSerialNumber(),
                    new UtcTime(UtcTime.TAG, null,
                            ZonedDateTime.parse(formattedTextFieldTime.getText(), DATE_FORMAT)
                                    .withZoneSameInstant(ZoneId.of("UTC"))),
                    Reason.valueOf(comboBoxReason.getSelectedItem().toString()));
            dispose();
        } catch (DateTimeParseException e) {
            alert(rootPane, "Revoke certificate", "Invalid time: " + formattedTextFieldTime.getText()
                    + ". It must be a valid ISO8601 time with offset, like '1919-08-10T11:45:14+09:00'.");
        } catch (IllegalArgumentException e) {
            alert(rootPane, "Revoke certificate", "Invalid reason.");
        }
    }

    /**
     * EFFECTS: Clear the result and close dialog.
     * MODIFIES: this
     */
    private void onCancel(ActionEvent ev) {
        res = null;
        dispose();
    }

    /**
     * EFFECTS: Get result.
     */
    public RevokedCertificate getRes() {
        return res;
    }
}