aboutsummaryrefslogtreecommitdiff
path: root/src/dialogs/certificatemanagementdialog.cpp
blob: e2db96b83dbd097af7b36b1540920a39e4124bf8 (plain) (blame)
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
#include "certificatemanagementdialog.hpp"
#include "ui_certificatemanagementdialog.h"

#include "kristall.hpp"

#include "newidentitiydialog.hpp"
#include "certificateiodialog.hpp"
#include "ioutil.hpp"

#include <QCryptographicHash>
#include <QMessageBox>

CertificateManagementDialog::CertificateManagementDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CertificateManagementDialog),
    selected_identity { nullptr }
{
    ui->setupUi(this);

    connect( // connect with "this" as context, so the connection will die when the window is destroyed
        kristall::globals().localization.get(), &Localization::translationChanged,
        this, [this]() { this->ui->retranslateUi(this); },
        Qt::DirectConnection
    );

    this->ui->certificates->setModel(&identity_set);
    this->ui->certificates->expandAll();

    connect(
        this->ui->certificates->selectionModel(),
        &QItemSelectionModel::currentChanged,
        this,
        &CertificateManagementDialog::on_certificates_selected
    );
    on_certificates_selected(QModelIndex { }, QModelIndex { });
}

CertificateManagementDialog::~CertificateManagementDialog()
{
    delete ui;
}

IdentityCollection CertificateManagementDialog::identitySet() const
{
    return this->identity_set;
}

void CertificateManagementDialog::setIdentitySet(const IdentityCollection &src)
{
    this->identity_set = src;
    this->ui->certificates->expandAll();

}

void CertificateManagementDialog::on_certificates_selected(QModelIndex const& index, QModelIndex const & previous)
{
    Q_UNUSED(previous);

    selected_identity = identity_set.getMutableIdentity(index);

    this->ui->export_cert_button->setEnabled(selected_identity != nullptr);

    if(selected_identity != nullptr)
    {
        auto & cert = *selected_identity;
        this->ui->groupBox->setEnabled(true);
        this->ui->cert_display_name->setText(cert.display_name);
        this->ui->cert_common_name->setText(cert.certificate.subjectInfo(QSslCertificate::CommonName).join(", "));
        this->ui->cert_expiration_date->setDateTime(cert.certificate.expiryDate());
        auto days = QDateTime::currentDateTime().daysTo(cert.certificate.expiryDate());
        this->ui->cert_livetime->setText(tr("%1 day","%1 days", days).arg(days));
        this->ui->cert_fingerprint->setPlainText(toFingerprintString(cert.certificate));
        this->ui->cert_notes->setPlainText(cert.user_notes);

        this->ui->cert_host_filter->setText(cert.host_filter);
        this->ui->cert_auto_enable->setEnabled(not cert.host_filter.isEmpty());
        this->ui->cert_auto_enable->setChecked(cert.auto_enable);

        this->ui->delete_cert_button->setEnabled(true);
    }
    else
    {
        this->ui->groupBox->setEnabled(false);
        this->ui->cert_display_name->setText("");
        this->ui->cert_common_name->setText("");
        this->ui->cert_expiration_date->setDateTime(QDateTime { });
        this->ui->cert_livetime->setText("");
        this->ui->cert_fingerprint->setPlainText("");
        this->ui->cert_host_filter->setText("");
        this->ui->cert_auto_enable->setChecked(false);

        if(auto group_name = identity_set.group(index); not group_name.isEmpty()) {
            this->ui->delete_cert_button->setEnabled(identity_set.canDeleteGroup(group_name));
        } else {
            this->ui->delete_cert_button->setEnabled(false);
        }
    }
}

void CertificateManagementDialog::on_cert_notes_textChanged()
{
    if(this->selected_identity != nullptr) {
        this->selected_identity->user_notes = this->ui->cert_notes->toPlainText();
    }
}

void CertificateManagementDialog::on_cert_display_name_textChanged(const QString &arg1)
{
    Q_UNUSED(arg1)
    if(this->selected_identity != nullptr) {
        this->selected_identity->display_name = this->ui->cert_display_name->text();
    }
}

void CertificateManagementDialog::on_delete_cert_button_clicked()
{
    auto index = this->ui->certificates->currentIndex();

    if(identity_set.getMutableIdentity(index) != nullptr)
    {
        auto answer = QMessageBox::question(
            this,
            tr("Kristall"),
            tr("Do you really want to delete this certificate?\r\n\r\nYou will not be able to restore the identity after this!"),
            QMessageBox::Yes | QMessageBox::No,
            QMessageBox::No
        );
        if(answer != QMessageBox::Yes)
            return;
        if(not identity_set.destroyIdentity(index)) {
            QMessageBox::warning(this, tr("Kristall"), tr("Could not destroy identity!"));
        }
    }
    else if(auto group_name = identity_set.group(index); not group_name.isEmpty()) {

        auto answer = QMessageBox::question(
            this,
            tr("Kristall"),
            tr("Do you want to delete the group '%1'").arg(group_name)
        );
        if(answer != QMessageBox::Yes)
            return;

        if(not identity_set.deleteGroup(group_name)) {
            QMessageBox::warning(this, tr("Kristall"), tr("Could not delete group!"));
        }
    }
}

void CertificateManagementDialog::on_export_cert_button_clicked()
{
    if(this->selected_identity == nullptr)
        return;
    CertificateIoDialog dialog { this };

    dialog.setKeyAlgorithm(this->selected_identity->private_key.algorithm());
    dialog.setIoMode(CertificateIoDialog::Export);

    if(dialog.exec() != QDialog::Accepted)
        return;

    {
        QFile cert_file { dialog.certificateFileName() };
        if(not cert_file.open(QFile::WriteOnly)) {
            QMessageBox::warning(
                this,
                tr("Kristall"),
                tr("The file %1 could not be found!").arg(dialog.certificateFileName())
            );
            return;
        }

        QByteArray cert_blob;
        if(dialog.certificateFileName().endsWith(".der")) {
            cert_blob = this->selected_identity->certificate.toDer();
        } else {
            cert_blob = this->selected_identity->certificate.toPem();
        }

        if(not IoUtil::writeAll(cert_file, cert_blob)) {
            QMessageBox::warning(
                this,
                tr("Kristall"),
                tr("The file %1 could not be created found!").arg(dialog.certificateFileName())
            );
            return;
        }
    }

    {
        QFile key_file { dialog.keyFileName() };
        if(not key_file.open(QFile::WriteOnly)) {
            QMessageBox::warning(
                this,
                tr("Kristall"),
                tr("The file %1 could not be found!").arg(dialog.keyFileName())
            );
            return;
        }

        QByteArray key_blob;
        if(dialog.keyFileName().endsWith(".der")) {
            key_blob = this->selected_identity->private_key.toDer();
        } else {
            key_blob = this->selected_identity->private_key.toPem();
        }

        if(not IoUtil::writeAll(key_file, key_blob)) {
            QMessageBox::warning(
                this,
                tr("Kristall"),
                tr("The file %1 could not be created!").arg(dialog.keyFileName())
            );
            return;
        }
    }
}

void CertificateManagementDialog::on_import_cert_button_clicked()
{
    CertificateIoDialog dialog { this };

    dialog.setIoMode(CertificateIoDialog::Import);

    if(dialog.exec() != QDialog::Accepted)
        return;

    QFile cert_file { dialog.certificateFileName() };
    if(not cert_file.open(QFile::ReadOnly)) {
        QMessageBox::warning(
            this,
            tr("Kristall"),
            tr("The file %1 could not be found!").arg(dialog.certificateFileName())
        );
        return;
    }

    QFile key_file { dialog.keyFileName() };
    if(not key_file.open(QFile::ReadOnly)) {
        QMessageBox::warning(
            this,
            tr("Kristall"),
            tr("The file %1 could not be found!").arg(dialog.keyFileName())
        );
        return;
    }

    CryptoIdentity ident;
    ident.private_key = QSslKey {
        &key_file,
        dialog.keyAlgorithm(),
        dialog.keyFileName().endsWith(".der") ? QSsl::Der : QSsl::Pem,
        QSsl::PrivateKey
    };
    ident.certificate = QSslCertificate {
        &cert_file,
        dialog.keyFileName().endsWith(".der") ? QSsl::Der : QSsl::Pem,
    };
    ident.user_notes = tr("Imported from:\r\nkey: %1\r\n:cert: %2").arg(dialog.keyFileName(), dialog.certificateFileName());
    //: Default name
    ident.display_name = tr("Imported Certificate");
    ident.auto_enable = false;
    ident.host_filter = "";
    ident.is_persistent = true;

    if(ident.private_key.isNull()) {
        QMessageBox::warning(
            this,
            tr("Kristall"),
            tr("The key file %1 could not be loaded. Please verify your key file.").arg(dialog.keyFileName())
        );
        return;
    }

    if(ident.certificate.isNull()) {
        QMessageBox::warning(
            this,
            tr("Kristall"),
            tr("The certificate file %1 could not be loaded. Please verify your certificate.").arg(dialog.keyFileName())
        );
        return;
    }

    if(not identity_set.addCertificate(tr("Imported Certificates"), ident)) {
        QMessageBox::warning(
            this,
            tr("Kristall"),
            tr("Failed to import the certificate.")
        );
    }
}

void CertificateManagementDialog::on_create_cert_button_clicked()
{
    NewIdentitiyDialog dialog { this };

    dialog.setGroupName(identity_set.group(this->ui->certificates->currentIndex()));

    if(dialog.exec() != QDialog::Accepted)
        return;

    auto id = dialog.createIdentity();
    if(not id.isValid())
        return;
    id.is_persistent = true;

    identity_set.addCertificate(
        dialog.groupName(),
        id);
}

void CertificateManagementDialog::on_cert_host_filter_textChanged(const QString &host_filter)
{
    if(this->selected_identity != nullptr) {
        this->ui->cert_auto_enable->setEnabled(not host_filter.isEmpty());
        this->selected_identity->host_filter = host_filter;
    } else {
        this->ui->cert_auto_enable->setEnabled(false);
    }

}

void CertificateManagementDialog::on_cert_auto_enable_clicked(bool checked)
{
    if(this->selected_identity != nullptr) {
        this->selected_identity->auto_enable = checked;
    }
}