Line data Source code
1 : /*
2 : qgpgmesecretexportjob.cpp
3 :
4 : This file is part of qgpgme, the Qt API binding for gpgme
5 : Copyright (c) 2004 Klarävdalens Datakonsult AB
6 : Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
7 : Software engineering by Intevation GmbH
8 :
9 : QGpgME is free software; you can redistribute it and/or
10 : modify it under the terms of the GNU General Public License as
11 : published by the Free Software Foundation; either version 2 of the
12 : License, or (at your option) any later version.
13 :
14 : QGpgME is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 :
23 : In addition, as a special exception, the copyright holders give
24 : permission to link the code of this program with any edition of
25 : the Qt library by Trolltech AS, Norway (or with modified versions
26 : of Qt that use the same license as Qt), and distribute linked
27 : combinations including the two. You must obey the GNU General
28 : Public License in all respects for all of the code used other than
29 : Qt. If you modify this file, you may extend this exception to
30 : your version of the file, but you are not obligated to do so. If
31 : you do not wish to do so, delete this exception statement from
32 : your version.
33 : */
34 :
35 : #ifdef HAVE_CONFIG_H
36 : #include "config.h"
37 : #endif
38 :
39 : #include "qgpgmesecretkeyexportjob.h"
40 :
41 : #include <QDebug>
42 : #include "gpgme_backend_debug.h"
43 :
44 : #include "context.h"
45 : #include "data.h"
46 :
47 : #include <QStringList>
48 :
49 : #include <gpg-error.h>
50 :
51 : #include <string.h>
52 : #include <assert.h>
53 :
54 0 : QGpgME::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob(bool armour, const QString &charset)
55 : : ExportJob(0),
56 : mProcess(0),
57 : mError(0),
58 : mArmour(armour),
59 0 : mCharset(charset)
60 : {
61 :
62 0 : }
63 :
64 0 : QGpgME::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob()
65 : {
66 :
67 0 : }
68 :
69 0 : GpgME::Error QGpgME::QGpgMESecretKeyExportJob::start(const QStringList &patterns)
70 : {
71 0 : assert(mKeyData.isEmpty());
72 :
73 0 : if (patterns.size() != 1 || patterns.front().isEmpty()) {
74 0 : deleteLater();
75 0 : return mError = GpgME::Error::fromCode(GPG_ERR_INV_VALUE, GPG_ERR_SOURCE_GPGSM);
76 : }
77 :
78 : // create and start gpgsm process:
79 0 : mProcess = new QProcess(this);
80 0 : mProcess->setObjectName(QStringLiteral("gpgsm --export-secret-key-p12"));
81 :
82 : // FIXME: obtain the path to gpgsm from gpgme, so we use the same instance.
83 0 : mProcess->setProgram("gpgsm");
84 0 : QStringList arguments;
85 0 : arguments << QStringLiteral("--export-secret-key-p12");
86 0 : if (mArmour) {
87 0 : arguments << QStringLiteral("--armor");
88 : }
89 0 : if (!mCharset.isEmpty()) {
90 0 : arguments << QStringLiteral("--p12-charset") << mCharset;
91 : }
92 0 : arguments << QLatin1String(patterns.front().toUtf8());
93 :
94 0 : mProcess->setArguments(arguments);
95 0 : connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
96 0 : SLOT(slotProcessExited(int,QProcess::ExitStatus)));
97 0 : connect(mProcess, &QProcess::readyReadStandardOutput,
98 0 : this, &QGpgMESecretKeyExportJob::slotStdout);
99 0 : connect(mProcess, &QProcess::readyReadStandardError,
100 0 : this, &QGpgMESecretKeyExportJob::slotStderr);
101 :
102 0 : mProcess->start();
103 0 : if (!mProcess->waitForStarted()) {
104 0 : mError = GpgME::Error::fromCode(GPG_ERR_ENOENT, GPG_ERR_SOURCE_GPGSM); // what else?
105 0 : deleteLater();
106 0 : return mError;
107 : } else {
108 0 : return GpgME::Error();
109 : }
110 : }
111 :
112 0 : void QGpgME::QGpgMESecretKeyExportJob::slotCancel()
113 : {
114 0 : if (mProcess) {
115 0 : mProcess->kill();
116 : }
117 0 : mProcess = 0;
118 0 : mError = GpgME::Error::fromCode(GPG_ERR_CANCELED, GPG_ERR_SOURCE_GPGSM);
119 0 : }
120 :
121 0 : void QGpgME::QGpgMESecretKeyExportJob::slotStdout()
122 : {
123 0 : QString line = QString::fromLocal8Bit(mProcess->readLine());
124 0 : if (!line.isEmpty()) {
125 0 : return;
126 : }
127 0 : const unsigned int oldlen = mKeyData.size();
128 0 : mKeyData.resize(oldlen + line.length());
129 0 : memcpy(mKeyData.data() + oldlen, line.toLatin1(), line.length());
130 : }
131 :
132 0 : void QGpgME::QGpgMESecretKeyExportJob::slotStderr()
133 : {
134 : // implement? or not?
135 0 : }
136 :
137 0 : void QGpgME::QGpgMESecretKeyExportJob::slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
138 : {
139 0 : Q_EMIT done();
140 0 : if (!mError &&
141 0 : (exitStatus != QProcess::NormalExit || exitCode != 0)) {
142 0 : mError = GpgME::Error::fromCode(GPG_ERR_GENERAL, GPG_ERR_SOURCE_GPGSM);
143 : }
144 0 : Q_EMIT result(mError, mKeyData);
145 0 : deleteLater();
146 0 : }
147 : #include "qgpgmesecretkeyexportjob.moc"
|