File indexing completed on 2024-05-26 05:24:20

0001 /*  This file is part of Kleopatra, the KDE keymanager
0002     SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <config-libkleo.h>
0008 
0009 #include "defaultkeygenerationjob.h"
0010 
0011 #include <QGpgME/KeyGenerationJob>
0012 #include <QGpgME/Protocol>
0013 
0014 #include <QEvent>
0015 #include <QPointer>
0016 
0017 using namespace Kleo;
0018 
0019 namespace Kleo
0020 {
0021 
0022 class DefaultKeyGenerationJob::DefaultKeyGenerationJobPrivate
0023 {
0024 public:
0025     DefaultKeyGenerationJobPrivate()
0026     {
0027     }
0028 
0029     ~DefaultKeyGenerationJobPrivate()
0030     {
0031         if (job) {
0032             job->deleteLater();
0033         }
0034     }
0035 
0036     QString passphrase;
0037     QPointer<QGpgME::KeyGenerationJob> job;
0038 };
0039 }
0040 
0041 DefaultKeyGenerationJob::DefaultKeyGenerationJob(QObject *parent)
0042     : Job(parent)
0043     , d(new DefaultKeyGenerationJob::DefaultKeyGenerationJobPrivate())
0044 {
0045 }
0046 
0047 DefaultKeyGenerationJob::~DefaultKeyGenerationJob() = default;
0048 
0049 QString DefaultKeyGenerationJob::auditLogAsHtml() const
0050 {
0051     return d->job ? d->job->auditLogAsHtml() : QString();
0052 }
0053 
0054 GpgME::Error DefaultKeyGenerationJob::auditLogError() const
0055 {
0056     return d->job ? d->job->auditLogError() : GpgME::Error();
0057 }
0058 
0059 void DefaultKeyGenerationJob::slotCancel()
0060 {
0061     if (d->job) {
0062         d->job->slotCancel();
0063     }
0064 }
0065 
0066 void DefaultKeyGenerationJob::setPassphrase(const QString &passphrase)
0067 {
0068     // null QString = ask for passphrase
0069     // empty QString = empty passphrase
0070     // non-empty QString = passphrase
0071     d->passphrase = passphrase.isNull() ? QLatin1StringView("") : passphrase;
0072 }
0073 
0074 namespace
0075 {
0076 QString passphraseParameter(const QString &passphrase)
0077 {
0078     if (passphrase.isNull()) {
0079         return QStringLiteral("%ask-passphrase");
0080     } else if (passphrase.isEmpty()) {
0081         return QStringLiteral("%no-protection");
0082     } else {
0083         return QStringLiteral("passphrase: %1").arg(passphrase);
0084     };
0085 }
0086 }
0087 
0088 GpgME::Error DefaultKeyGenerationJob::start(const QString &email, const QString &name)
0089 {
0090     const QString passphrase = passphraseParameter(d->passphrase);
0091     const QString args = QStringLiteral(
0092                              "<GnupgKeyParms format=\"internal\">\n"
0093                              "key-type:      RSA\n"
0094                              "key-length:    2048\n"
0095                              "key-usage:     sign\n"
0096                              "subkey-type:   RSA\n"
0097                              "subkey-length: 2048\n"
0098                              "subkey-usage:  encrypt\n"
0099                              "%1\n"
0100                              "name-email:    %2\n"
0101                              "name-real:     %3\n"
0102                              "</GnupgKeyParms>")
0103                              .arg(passphrase, email, name);
0104 
0105     d->job = QGpgME::openpgp()->keyGenerationJob();
0106     d->job->installEventFilter(this);
0107     connect(d->job.data(), &QGpgME::KeyGenerationJob::result, this, &DefaultKeyGenerationJob::result);
0108     connect(d->job.data(), &QGpgME::KeyGenerationJob::done, this, &DefaultKeyGenerationJob::done);
0109     connect(d->job.data(), &QGpgME::KeyGenerationJob::done, this, &QObject::deleteLater);
0110     return d->job->start(args);
0111 }
0112 
0113 bool DefaultKeyGenerationJob::eventFilter(QObject *watched, QEvent *event)
0114 {
0115     // Intercept the KeyGenerationJob's deferred delete event. We want the job
0116     // to live at least as long as we do so we can delegate calls to it. We will
0117     // delete the job manually afterwards.
0118     if (watched == d->job && event->type() == QEvent::DeferredDelete) {
0119         return true;
0120     }
0121 
0122     return Job::eventFilter(watched, event);
0123 }
0124 
0125 #include "moc_defaultkeygenerationjob.cpp"