pivcardwidget.cpp 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*  view/pivcardwiget.cpp

    This file is part of Kleopatra, the KDE keymanager
    SPDX-FileCopyrightText: 2020 g10 Code GmbH
    SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "pivcardwidget.h"

12
13
#include "tooltippreferences.h"

14
#include "commands/certificatetopivcardcommand.h"
15
#include "commands/changepincommand.h"
16
#include "commands/importcertificatefrompivcardcommand.h"
17
#include "commands/keytocardcommand.h"
18
#include "commands/pivgeneratecardkeycommand.h"
19
#include "commands/setpivcardapplicationadministrationkeycommand.h"
20

21
#include "smartcard/pivcard.h"
22
#include "smartcard/readerstatus.h"
23

24
25
26
27
28
29
30
#include <Libkleo/Dn>
#include <Libkleo/Formatting>
#include <Libkleo/KeyCache>

#include <KLocalizedString>
#include <KMessageBox>

31
32
33
#include <QFrame>
#include <QGridLayout>
#include <QLabel>
34
#include <QPushButton>
35
36
37
#include <QScrollArea>
#include <QVBoxLayout>

38
using namespace GpgME;
39
using namespace Kleo;
40
using namespace Kleo::Commands;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using namespace Kleo::SmartCard;

namespace {
static QString formatVersion(int value)
{
    if (value < 0) {
        return QLatin1String("n/a");
    }

    const unsigned int a = ((value >> 24) & 0xff);
    const unsigned int b = ((value >> 16) & 0xff);
    const unsigned int c = ((value >>  8) & 0xff);
    const unsigned int d = ((value      ) & 0xff);
    if (a) {
        return QStringLiteral("%1.%2.%3.%4").arg(QString::number(a), QString::number(b), QString::number(c), QString::number(d));
    } else if (b) {
        return QStringLiteral("%1.%2.%3").arg(QString::number(b), QString::number(c), QString::number(d));
    } else if (c) {
        return QStringLiteral("%1.%2").arg(QString::number(c), QString::number(d));
    }
    return QString::number(d);
}
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

static void layoutKeyWidgets(QGridLayout *grid, const QString &keyName, const PIVCardWidget::KeyWidgets &keyWidgets)
{
    int row = grid->rowCount();
    grid->addWidget(new QLabel(keyName), row, 0);
    grid->addWidget(keyWidgets.certificateInfo, row, 1, 1, 2);
    grid->addWidget(keyWidgets.generateButton, row, 3);
    if (keyWidgets.writeKeyButton) {
        grid->addWidget(keyWidgets.writeKeyButton, row, 4);
    }
    row++;
    grid->addWidget(keyWidgets.keyGrip, row, 1);
    grid->addWidget(keyWidgets.keyAlgorithm, row, 2);
    grid->addWidget(keyWidgets.writeCertificateButton, row, 3);
    grid->addWidget(keyWidgets.importCertificateButton, row, 4);
}

static int toolTipOptions()
{
    using namespace Kleo::Formatting;
    static const int validityFlags = Validity | Issuer | ExpiryDates | CertificateUsage;
    static const int ownerFlags = Subject | UserIDs | OwnerTrust;
    static const int detailsFlags = StorageLocation | CertificateType | SerialNumber | Fingerprint;

    const TooltipPreferences prefs;

    int flags = KeyID;
    flags |= prefs.showValidity() ? validityFlags : 0;
    flags |= prefs.showOwnerInformation() ? ownerFlags : 0;
    flags |= prefs.showCertificateDetails() ? detailsFlags : 0;
    return flags;
}
95
}
96
97
98
99

PIVCardWidget::PIVCardWidget(QWidget *parent):
    QWidget(parent),
    mSerialNumber(new QLabel(this)),
100
    mVersionLabel(new QLabel(this))
101
{
102
103
    // Set up the scroll area
    auto myLayout = new QVBoxLayout(this);
104
105
106
107

    auto area = new QScrollArea;
    area->setFrameShape(QFrame::NoFrame);
    area->setWidgetResizable(true);
108
109
    myLayout->addWidget(area);

110
    auto areaWidget = new QWidget;
111
112
    area->setWidget(areaWidget);

113
    auto areaVLay = new QVBoxLayout(areaWidget);
114
115

    auto grid = new QGridLayout;
116
117
    areaVLay->addLayout(grid);
    areaVLay->addStretch(1);
118
119
120

    const int columnCount = 5;
    int row = 0;
121
122
123
124
125

    // Version and Serialnumber
    grid->addWidget(mVersionLabel, row++, 0, 1, 2);
    mVersionLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);

126
    grid->addWidget(new QLabel(i18n("Serial number:")), row, 0);
127
128
129
    grid->addWidget(mSerialNumber, row++, 1);
    mSerialNumber->setTextInteractionFlags(Qt::TextBrowserInteraction);

130
131
132
133
134
135
    {
        auto line = new QFrame();
        line->setFrameShape(QFrame::HLine);
        grid->addWidget(line, row++, 0, 1, columnCount);
    }

136
137
138
    // The keys
    grid->addWidget(new QLabel(QStringLiteral("<b>%1</b>").arg(i18n("Keys:"))), row++, 0);

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    {
        KeyWidgets keyWidgets = createKeyWidgets(PIVCard::pivAuthenticationKeyRef());
        layoutKeyWidgets(grid, i18n("PIV authentication:"), keyWidgets);
    }
    {
        KeyWidgets keyWidgets = createKeyWidgets(PIVCard::cardAuthenticationKeyRef());
        layoutKeyWidgets(grid, i18n("Card authentication:"), keyWidgets);
    }
    {
        KeyWidgets keyWidgets = createKeyWidgets(PIVCard::digitalSignatureKeyRef());
        layoutKeyWidgets(grid, i18n("Digital signature:"), keyWidgets);
    }
    {
        KeyWidgets keyWidgets = createKeyWidgets(PIVCard::keyManagementKeyRef());
        layoutKeyWidgets(grid, i18n("Key management:"), keyWidgets);
    }
    row = grid->rowCount();
156

157
158
159
160
161
    {
        auto line = new QFrame();
        line->setFrameShape(QFrame::HLine);
        grid->addWidget(line, row++, 0, 1, columnCount);
    }
162

163
164
    auto actionLayout = new QHBoxLayout;

165
    {
166
167
168
        auto button = new QPushButton(i18nc("@action:button", "Change PIN"));
        button->setToolTip(i18nc("@info:tooltip", "Change the PIV Card Application PIN that activates the PIV Card "
                                 "and enables private key operations using the stored keys."));
169
170
171
172
        actionLayout->addWidget(button);
        connect(button, &QPushButton::clicked, this, [this] () { changePin(PIVCard::pinKeyRef()); });
    }
    {
173
174
        auto button = new QPushButton(i18nc("@action:button", "Change PUK"));
        button->setToolTip(i18nc("@info:tooltip", "Change the PIN Unblocking Key that enables a reset of the PIN."));
175
176
177
178
        actionLayout->addWidget(button);
        connect(button, &QPushButton::clicked, this, [this] () { changePin(PIVCard::pukKeyRef()); });
    }
    {
179
180
181
182
        auto button = new QPushButton(i18nc("@action:button", "Change Admin Key"));
        button->setToolTip(i18nc("@info:tooltip", "Change the PIV Card Application Administration Key that is used by the "
                                 "PIV Card Application to authenticate the PIV Card Application Administrator and by the "
                                 "administrator (resp. Kleopatra) to authenticate the PIV Card Application."));
183
184
185
        actionLayout->addWidget(button);
        connect(button, &QPushButton::clicked, this, [this] () { setAdminKey(); });
    }
186
187

    actionLayout->addStretch(-1);
188
    grid->addLayout(actionLayout, row++, 0, 1, columnCount);
189

190
191
192
    grid->setColumnStretch(4, -1);
}

193
194
195
196
197
PIVCardWidget::KeyWidgets PIVCardWidget::createKeyWidgets(const std::string &keyRef)
{
    KeyWidgets keyWidgets;
    keyWidgets.keyGrip = new QLabel(this);
    keyWidgets.keyGrip->setTextInteractionFlags(Qt::TextBrowserInteraction);
198
    keyWidgets.keyAlgorithm = new QLabel(this);
199
200
201
    keyWidgets.keyAlgorithm->setTextInteractionFlags(Qt::TextSelectableByMouse);
    keyWidgets.certificateInfo = new QLabel(this);
    keyWidgets.certificateInfo->setTextInteractionFlags(Qt::TextBrowserInteraction);
202
203
    keyWidgets.generateButton = new QPushButton(i18nc("@action:button", "Generate"), this);
    keyWidgets.generateButton->setEnabled(false);
204
205
    connect(keyWidgets.generateButton, &QPushButton::clicked,
            this, [this, keyRef] () { generateKey(keyRef); });
206
207
208
    keyWidgets.writeCertificateButton = new QPushButton(i18nc("@action:button", "Write Certificate"));
    keyWidgets.writeCertificateButton->setToolTip(i18nc("@info:tooltip", "Write the certificate corresponding to this key to the card"));
    keyWidgets.writeCertificateButton->setEnabled(false);
209
210
211
212
213
214
215
    connect(keyWidgets.writeCertificateButton, &QPushButton::clicked,
            this, [this, keyRef] () { writeCertificateToCard(keyRef); });
    keyWidgets.importCertificateButton = new QPushButton(i18nc("@action:button", "Import Certificate"));
    keyWidgets.importCertificateButton->setToolTip(i18nc("@info:tooltip", "Import the certificate stored on the card"));
    keyWidgets.importCertificateButton->setEnabled(false);
    connect(keyWidgets.importCertificateButton, &QPushButton::clicked,
            this, [this, keyRef] () { importCertificateFromCard(keyRef); });
216
217
218
219
    if (keyRef == PIVCard::keyManagementKeyRef()) {
        keyWidgets.writeKeyButton = new QPushButton(i18nc("@action:button", "Write Key"));
        keyWidgets.writeKeyButton->setToolTip(i18nc("@info:tooltip", "Write the key pair of a certificate to the card"));
        keyWidgets.writeKeyButton->setEnabled(true);
220
221
        connect(keyWidgets.writeKeyButton, &QPushButton::clicked,
                this, [this, keyRef] () { writeKeyToCard(keyRef); });
222
    }
223
    mKeyWidgets.insert(keyRef, keyWidgets);
224
225
226
    return keyWidgets;
}

227
228
229
230
PIVCardWidget::~PIVCardWidget()
{
}

231
232
void PIVCardWidget::setCard(const PIVCard *card)
{
233
    mCardSerialNumber = card->serialNumber();
234
    mVersionLabel->setText(i18nc("%1 version number", "PIV v%1 card", formatVersion(card->appVersion())));
235
236
237
238
239
240
241
242

    if (card->displaySerialNumber() != card->serialNumber()) {
        mSerialNumber->setText(QStringLiteral("%1 (%2)").arg(QString::fromStdString(card->displaySerialNumber()),
                                                             QString::fromStdString(card->serialNumber())));
    } else {
        mSerialNumber->setText(QString::fromStdString(card->serialNumber()));
    }

243
244
245
246
    updateKeyWidgets(PIVCard::pivAuthenticationKeyRef(), card);
    updateKeyWidgets(PIVCard::cardAuthenticationKeyRef(), card);
    updateKeyWidgets(PIVCard::digitalSignatureKeyRef(), card);
    updateKeyWidgets(PIVCard::keyManagementKeyRef(), card);
247
248
}

249
void PIVCardWidget::updateKeyWidgets(const std::string &keyRef, const PIVCard *card)
250
{
251
252
    KeyWidgets widgets = mKeyWidgets.value(keyRef);
    const std::string grip = card ? card->keyGrip(keyRef) : widgets.keyGrip->text().toStdString();
253
    if (grip.empty()) {
254
255
256
        widgets.certificateInfo->setText(i18nc("@info", "<em>slot empty</em>"));
        widgets.certificateInfo->setToolTip(QString());
        widgets.keyGrip->setText(QString());
257
258
259
260
        widgets.keyAlgorithm->setText(QString());
        widgets.generateButton->setText(i18nc("@action:button", "Generate"));
        widgets.generateButton->setToolTip(
            i18nc("@info:tooltip %1 display name of a key", "Generate %1", PIVCard::keyDisplayName(keyRef)));
261
262
        widgets.writeCertificateButton->setEnabled(false);
        widgets.importCertificateButton->setEnabled(false);
263
    } else {
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
        const Key certificate = KeyCache::instance()->findSubkeyByKeyGrip(grip).parent();
        if (!certificate.isNull() && certificate.protocol() == GpgME::CMS) {
            widgets.certificateInfo->setText(
                i18nc("X.509 certificate DN (validity, created: date)", "%1 (%2, created: %3)",
                      DN(certificate.userID(0).id()).prettyDN(),
                      Formatting::complianceStringShort(certificate),
                      Formatting::creationDateString(certificate)));
            widgets.certificateInfo->setToolTip(Formatting::toolTip(certificate, toolTipOptions()));
            widgets.writeCertificateButton->setEnabled(true);
        } else {
            widgets.certificateInfo->setText(i18nc("@info", "<em>no matching certificate</em>"));
            widgets.certificateInfo->setToolTip(QString());
            widgets.writeCertificateButton->setEnabled(false);
        }
        if (card) {
            // update information if called with card
            widgets.keyGrip->setText(QString::fromStdString(grip));
            const std::string algo = card->keyAlgorithm(keyRef);
            widgets.keyAlgorithm->setText(algo.empty() ? i18nc("@info unknown key algorithm", "unknown") : QString::fromStdString(algo));
            widgets.importCertificateButton->setEnabled(!card->certificateData(keyRef).empty());
        }
285
286
287
288
        widgets.generateButton->setText(i18nc("@action:button", "Replace"));
        widgets.generateButton->setToolTip(
            i18nc("@info:tooltip %1 display name of a key", "Replace %1 with new key", PIVCard::keyDisplayName(keyRef)));
    }
289

290
    widgets.generateButton->setEnabled(true);
291
292
}

293
void PIVCardWidget::generateKey(const std::string &keyref)
294
{
295
    auto cmd = new PIVGenerateCardKeyCommand(mCardSerialNumber, this);
296
297
298
299
300
301
302
303
    this->setEnabled(false);
    connect(cmd, &PIVGenerateCardKeyCommand::finished,
            this, [this]() {
                this->setEnabled(true);
            });
    cmd->setKeyRef(keyref);
    cmd->start();
}
304

305
void PIVCardWidget::writeCertificateToCard(const std::string &keyref)
306
{
307
    auto cmd = new CertificateToPIVCardCommand(keyref, mCardSerialNumber);
308
    this->setEnabled(false);
309
    connect(cmd, &CertificateToPIVCardCommand::finished,
310
311
312
313
314
            this, [this]() {
                this->setEnabled(true);
            });
    cmd->setParentWidget(this);
    cmd->start();
315
316
317
318
319
320
321
}

void PIVCardWidget::importCertificateFromCard(const std::string &keyref)
{
    auto cmd = new ImportCertificateFromPIVCardCommand(keyref, mCardSerialNumber);
    this->setEnabled(false);
    connect(cmd, &ImportCertificateFromPIVCardCommand::finished,
322
323
            this, [this, keyref] () {
                this->updateKeyWidgets(keyref, nullptr);
324
325
326
327
                this->setEnabled(true);
            });
    cmd->setParentWidget(this);
    cmd->start();
328
329
}

330
331
332
333
334
335
336
337
338
339
340
341
void PIVCardWidget::writeKeyToCard(const std::string &keyref)
{
    auto cmd = new KeyToCardCommand(keyref, mCardSerialNumber);
    this->setEnabled(false);
    connect(cmd, &KeyToCardCommand::finished,
            this, [this]() {
                this->setEnabled(true);
            });
    cmd->setParentWidget(this);
    cmd->start();
}

342
343
344
345
346
347
348
349
350
351
352
void PIVCardWidget::changePin(const std::string &keyRef)
{
    auto cmd = new ChangePinCommand(mCardSerialNumber, this);
    this->setEnabled(false);
    connect(cmd, &ChangePinCommand::finished,
            this, [this]() {
                this->setEnabled(true);
            });
    cmd->setKeyRef(keyRef);
    cmd->start();
}
353
354
355
356
357
358
359
360
361
362
363

void PIVCardWidget::setAdminKey()
{
    auto cmd = new SetPIVCardApplicationAdministrationKeyCommand(mCardSerialNumber, this);
    this->setEnabled(false);
    connect(cmd, &SetPIVCardApplicationAdministrationKeyCommand::finished,
            this, [this]() {
                this->setEnabled(true);
            });
    cmd->start();
}