createopenpgpkeyfromcardkeyscommand.cpp 6.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* -*- mode: c++; c-basic-offset:4 -*-
    commands/createopenpgpkeyfromcardkeyscommand.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 <config-kleopatra.h>

#include "createopenpgpkeyfromcardkeyscommand.h"

#include "cardcommand_p.h"

#include "dialogs/adduseriddialog.h"

19
#include "smartcard/netkeycard.h"
20
#include "smartcard/openpgpcard.h"
21
#include "smartcard/pivcard.h"
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
#include "smartcard/readerstatus.h"

#include <Libkleo/Formatting>
#include <Libkleo/KeyCache>

#include <KLocalizedString>

#include <QGpgME/Protocol>
#include <QGpgME/QuickJob>

#include <gpgme++/context.h>
#include <gpgme++/engineinfo.h>

#include <gpgme.h>

#include "kleopatra_debug.h"

using namespace Kleo;
using namespace Kleo::Commands;
using namespace Kleo::Dialogs;
using namespace Kleo::SmartCard;
using namespace GpgME;
using namespace QGpgME;

class CreateOpenPGPKeyFromCardKeysCommand::Private : public CardCommand::Private
{
    friend class ::Kleo::Commands::CreateOpenPGPKeyFromCardKeysCommand;
    CreateOpenPGPKeyFromCardKeysCommand *q_func() const
    {
        return static_cast<CreateOpenPGPKeyFromCardKeysCommand *>(q);
    }
public:
    explicit Private(CreateOpenPGPKeyFromCardKeysCommand *qq, const std::string &serialNumber, const std::string &appName, QWidget *parent);
    ~Private();

private:
    void start();

    void slotDialogAccepted();
    void slotDialogRejected();
    void slotResult(const Error &err);

    void ensureDialogCreated();

private:
    std::string appName;
    QPointer<AddUserIDDialog> dialog;
};

CreateOpenPGPKeyFromCardKeysCommand::Private *CreateOpenPGPKeyFromCardKeysCommand::d_func()
{
    return static_cast<Private *>(d.get());
}
const CreateOpenPGPKeyFromCardKeysCommand::Private *CreateOpenPGPKeyFromCardKeysCommand::d_func() const
{
    return static_cast<const Private *>(d.get());
}

#define d d_func()
#define q q_func()

CreateOpenPGPKeyFromCardKeysCommand::Private::Private(CreateOpenPGPKeyFromCardKeysCommand *qq, const std::string &serialNumber, const std::string &appName_, QWidget *parent)
    : CardCommand::Private(qq, serialNumber, parent)
    , appName(appName_)
{
}

CreateOpenPGPKeyFromCardKeysCommand::Private::~Private()
{
}

void CreateOpenPGPKeyFromCardKeysCommand::Private::start()
{
95
    if (appName != NetKeyCard::AppName && appName != OpenPGPCard::AppName && appName != PIVCard::AppName) {
96
        qCWarning(KLEOPATRA_LOG) << "CreateOpenPGPKeyFromCardKeysCommand does not support card application" << QString::fromStdString(appName);
97
98
99
100
        finished();
        return;
    }

101
102
103
    const auto card = ReaderStatus::instance()->getCard(serialNumber(), appName);
    if (!card) {
        error(i18n("Failed to find the smartcard with the serial number: %1", QString::fromStdString(serialNumber())));
104
105
106
107
        finished();
        return;
    }

108
109
    const auto signingKeyGrip = card->keyInfo(card->signingKeyRef()).grip;
    const Key signingKey = KeyCache::instance()->findSubkeyByKeyGrip(signingKeyGrip, OpenPGP).parent();
110
111
    if (!signingKey.isNull()) {
        const QString message = i18nc("@info",
Yuri Chornoivan's avatar
Fix tag    
Yuri Chornoivan committed
112
            "<p>There is already an OpenPGP key corresponding to the signing key on this card:</p><p>%1</p>"
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            "<p>Do you still want to create an OpenPGP key for the card keys?</p>",
            Formatting::summaryLine(signingKey));
        const auto choice = KMessageBox::warningContinueCancel(parentWidgetOrView(), message,
            i18nc("@title:window", "Create OpenPGP Key"),
            KStandardGuiItem::cont(), KStandardGuiItem::cancel(), QString(), KMessageBox::Notify);
        if (choice != KMessageBox::Continue) {
            finished();
            return;
        }
    }

    ensureDialogCreated();

    dialog->setWindowTitle(i18n("Enter User ID"));
127
    dialog->setName(card->cardHolder());
128
129
130
131
132
133

    dialog->show();
}

void CreateOpenPGPKeyFromCardKeysCommand::Private::slotDialogAccepted()
{
134
135
136
137
    const Error err = ReaderStatus::switchCardAndApp(serialNumber(), appName);
    if (err) {
        finished();
        return;
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
    }

    const auto backend = openpgp();
    if (!backend) {
        finished();
        return;
    }

    QuickJob *const job = backend->quickJob();
    if (!job) {
        finished();
        return;
    }

    connect(job, SIGNAL(result(GpgME::Error)),
            q, SLOT(slotResult(GpgME::Error)));

    const QString userID = Formatting::prettyNameAndEMail(OpenPGP, QString(), dialog->name(), dialog->email(), dialog->comment());
    const QDateTime expires = QDateTime();
    const unsigned int flags = GPGME_CREATE_FORCE;
    job->startCreate(userID, "card", expires, Key(), flags);
}

void CreateOpenPGPKeyFromCardKeysCommand::Private::slotDialogRejected()
{
    canceled();
}

void CreateOpenPGPKeyFromCardKeysCommand::Private::slotResult(const Error &err)
{
    if (err.isCanceled()) {
        // do nothing
    } else if (err) {
        error(i18nc("@info", "Creating an OpenPGP key from the card keys failed: %1", QString::fromUtf8(err.asString())),
              i18nc("@title", "Error"));
    } else {
        information(i18nc("@info", "Successfully generated an OpenPGP key from the card keys."),
                    i18nc("@title", "Success"));
    }

    finished();
}

void CreateOpenPGPKeyFromCardKeysCommand::Private::ensureDialogCreated()
{
    if (dialog) {
        return;
    }

    dialog = new AddUserIDDialog;
    applyWindowID(dialog);
    dialog->setAttribute(Qt::WA_DeleteOnClose);

    connect(dialog, SIGNAL(accepted()), q, SLOT(slotDialogAccepted()));
    connect(dialog, SIGNAL(rejected()), q, SLOT(slotDialogRejected()));
}

CreateOpenPGPKeyFromCardKeysCommand::CreateOpenPGPKeyFromCardKeysCommand(const std::string &serialNumber, const std::string &appName, QWidget *parent)
    : CardCommand(new Private(this, serialNumber, appName, parent))
{
}

CreateOpenPGPKeyFromCardKeysCommand::~CreateOpenPGPKeyFromCardKeysCommand()
{
}

204
205
206
// static
bool CreateOpenPGPKeyFromCardKeysCommand::isSupported()
{
207
    return !(engineInfo(GpgEngine).engineVersion() < "2.3.0");
208
209
}

210
211
212
213
214
215
216
217
218
219
220
221
222
void CreateOpenPGPKeyFromCardKeysCommand::doStart()
{
    d->start();
}

void CreateOpenPGPKeyFromCardKeysCommand::doCancel()
{
}

#undef d
#undef q

#include "moc_createopenpgpkeyfromcardkeyscommand.cpp"