Blame lang/qt/src/qgpgmesecretkeyexportjob.cpp

Packit d7e8d0
/*
Packit d7e8d0
    qgpgmesecretexportjob.cpp
Packit d7e8d0
Packit d7e8d0
    This file is part of qgpgme, the Qt API binding for gpgme
Packit d7e8d0
    Copyright (c) 2004 Klarävdalens Datakonsult AB
Packit d7e8d0
    Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Packit d7e8d0
    Software engineering by Intevation GmbH
Packit d7e8d0
Packit d7e8d0
    QGpgME is free software; you can redistribute it and/or
Packit d7e8d0
    modify it under the terms of the GNU General Public License as
Packit d7e8d0
    published by the Free Software Foundation; either version 2 of the
Packit d7e8d0
    License, or (at your option) any later version.
Packit d7e8d0
Packit d7e8d0
    QGpgME is distributed in the hope that it will be useful,
Packit d7e8d0
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d7e8d0
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit d7e8d0
    General Public License for more details.
Packit d7e8d0
Packit d7e8d0
    You should have received a copy of the GNU General Public License
Packit d7e8d0
    along with this program; if not, write to the Free Software
Packit d7e8d0
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit d7e8d0
Packit d7e8d0
    In addition, as a special exception, the copyright holders give
Packit d7e8d0
    permission to link the code of this program with any edition of
Packit d7e8d0
    the Qt library by Trolltech AS, Norway (or with modified versions
Packit d7e8d0
    of Qt that use the same license as Qt), and distribute linked
Packit d7e8d0
    combinations including the two.  You must obey the GNU General
Packit d7e8d0
    Public License in all respects for all of the code used other than
Packit d7e8d0
    Qt.  If you modify this file, you may extend this exception to
Packit d7e8d0
    your version of the file, but you are not obligated to do so.  If
Packit d7e8d0
    you do not wish to do so, delete this exception statement from
Packit d7e8d0
    your version.
Packit d7e8d0
*/
Packit d7e8d0
Packit d7e8d0
#ifdef HAVE_CONFIG_H
Packit d7e8d0
 #include "config.h"
Packit d7e8d0
#endif
Packit d7e8d0
Packit d7e8d0
#include "qgpgmesecretkeyexportjob.h"
Packit d7e8d0
Packit d7e8d0
#include <QDebug>
Packit d7e8d0
#include "gpgme_backend_debug.h"
Packit d7e8d0
Packit d7e8d0
#include "context.h"
Packit d7e8d0
#include "data.h"
Packit d7e8d0
Packit d7e8d0
#include <QStringList>
Packit d7e8d0
Packit d7e8d0
#include <gpg-error.h>
Packit d7e8d0
Packit d7e8d0
#include <string.h>
Packit d7e8d0
#include <assert.h>
Packit d7e8d0
Packit d7e8d0
QGpgME::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob(bool armour, const QString &charset)
Packit d7e8d0
    : ExportJob(0),
Packit d7e8d0
      mProcess(0),
Packit d7e8d0
      mError(0),
Packit d7e8d0
      mArmour(armour),
Packit d7e8d0
      mCharset(charset)
Packit d7e8d0
{
Packit d7e8d0
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
QGpgME::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob()
Packit d7e8d0
{
Packit d7e8d0
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
GpgME::Error QGpgME::QGpgMESecretKeyExportJob::start(const QStringList &patterns)
Packit d7e8d0
{
Packit d7e8d0
    assert(mKeyData.isEmpty());
Packit d7e8d0
Packit d7e8d0
    if (patterns.size() != 1 || patterns.front().isEmpty()) {
Packit d7e8d0
        deleteLater();
Packit d7e8d0
        return mError = GpgME::Error::fromCode(GPG_ERR_INV_VALUE, GPG_ERR_SOURCE_GPGSM);
Packit d7e8d0
    }
Packit d7e8d0
Packit d7e8d0
    // create and start gpgsm process:
Packit d7e8d0
    mProcess = new QProcess(this);
Packit d7e8d0
    mProcess->setObjectName(QStringLiteral("gpgsm --export-secret-key-p12"));
Packit d7e8d0
Packit d7e8d0
    // FIXME: obtain the path to gpgsm from gpgme, so we use the same instance.
Packit d7e8d0
    mProcess->setProgram("gpgsm");
Packit d7e8d0
    QStringList arguments;
Packit d7e8d0
    arguments << QStringLiteral("--export-secret-key-p12");
Packit d7e8d0
    if (mArmour) {
Packit d7e8d0
        arguments << QStringLiteral("--armor");
Packit d7e8d0
    }
Packit d7e8d0
    if (!mCharset.isEmpty()) {
Packit d7e8d0
        arguments << QStringLiteral("--p12-charset") << mCharset;
Packit d7e8d0
    }
Packit d7e8d0
    arguments << QLatin1String(patterns.front().toUtf8());
Packit d7e8d0
Packit d7e8d0
    mProcess->setArguments(arguments);
Packit d7e8d0
    connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
Packit d7e8d0
            SLOT(slotProcessExited(int,QProcess::ExitStatus)));
Packit d7e8d0
    connect(mProcess, &QProcess::readyReadStandardOutput,
Packit d7e8d0
            this, &QGpgMESecretKeyExportJob::slotStdout);
Packit d7e8d0
    connect(mProcess, &QProcess::readyReadStandardError,
Packit d7e8d0
            this, &QGpgMESecretKeyExportJob::slotStderr);
Packit d7e8d0
Packit d7e8d0
    mProcess->start();
Packit d7e8d0
    if (!mProcess->waitForStarted()) {
Packit d7e8d0
        mError = GpgME::Error::fromCode(GPG_ERR_ENOENT, GPG_ERR_SOURCE_GPGSM);   // what else?
Packit d7e8d0
        deleteLater();
Packit d7e8d0
        return mError;
Packit d7e8d0
    } else {
Packit d7e8d0
        return GpgME::Error();
Packit d7e8d0
    }
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
void QGpgME::QGpgMESecretKeyExportJob::slotCancel()
Packit d7e8d0
{
Packit d7e8d0
    if (mProcess) {
Packit d7e8d0
        mProcess->kill();
Packit d7e8d0
    }
Packit d7e8d0
    mProcess = 0;
Packit d7e8d0
    mError = GpgME::Error::fromCode(GPG_ERR_CANCELED, GPG_ERR_SOURCE_GPGSM);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
void QGpgME::QGpgMESecretKeyExportJob::slotStdout()
Packit d7e8d0
{
Packit d7e8d0
    QString line = QString::fromLocal8Bit(mProcess->readLine());
Packit d7e8d0
    if (!line.isEmpty()) {
Packit d7e8d0
        return;
Packit d7e8d0
    }
Packit d7e8d0
    const unsigned int oldlen = mKeyData.size();
Packit d7e8d0
    mKeyData.resize(oldlen + line.length());
Packit d7e8d0
    memcpy(mKeyData.data() + oldlen, line.toLatin1(), line.length());
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
void QGpgME::QGpgMESecretKeyExportJob::slotStderr()
Packit d7e8d0
{
Packit d7e8d0
    // implement? or not?
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
void QGpgME::QGpgMESecretKeyExportJob::slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
Packit d7e8d0
{
Packit d7e8d0
    Q_EMIT done();
Packit d7e8d0
    if (!mError &&
Packit d7e8d0
            (exitStatus != QProcess::NormalExit || exitCode != 0)) {
Packit d7e8d0
        mError = GpgME::Error::fromCode(GPG_ERR_GENERAL, GPG_ERR_SOURCE_GPGSM);
Packit d7e8d0
    }
Packit d7e8d0
    Q_EMIT result(mError, mKeyData);
Packit d7e8d0
    deleteLater();
Packit d7e8d0
}
Packit d7e8d0
#include "qgpgmesecretkeyexportjob.moc"