aboutsummaryrefslogtreecommitdiffstats
path: root/kgpg/transactions/kgpggeneratekey.cpp
blob: e863a5d1264fca96ddfdf1808ec8364f3708c048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2008,2009,2010,2011,2012 Rolf Eike Beer <[email protected]>
 */

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kgpggeneratekey.h"

#include "../gpgproc.h"

//#include <KLocale>
//#include <KMessageBox>
//#include <kpimutils/email.h>
#include <QDebug>
#include <QApplication>

KGpgGenerateKey::KGpgGenerateKey(QObject *parent, const QString &name, const QString &email, const QString &comment,
		const KgpgCore::KgpgKeyAlgo &algorithm, const uint size, const unsigned int expire,
        const char expireunit, const QString &password)
	: KGpgTransaction(parent)
{
	addArgument(QLatin1String( "--status-fd=1" ));
	addArgument(QLatin1String( "--command-fd=0" ));
	addArgument(QLatin1String( "--no-verbose" ));
	addArgument(QLatin1String( "--gen-key" ));
	addArgument(QLatin1String( "--batch" ));

	setName(name);
	setEmail(email);
	setComment(comment);
	setAlgorithm(algorithm);
	setSize(size);
	setExpire(expire, expireunit);
    setPassword(password);

	getProcess()->setOutputChannelMode(KProcess::SeparateChannels);
}

KGpgGenerateKey::~KGpgGenerateKey()
{
}

bool
KGpgGenerateKey::preStart()
{
    /*if (!m_email.isEmpty() && !KPIMUtils::isValidSimpleAddress(m_email)) {
		setSuccess(TS_INVALID_EMAIL);
		return false;
    }*/

	m_fingerprint.clear();
	m_namesent = false;

	setSuccess(TS_MSG_SEQUENCE);

    setDescription(QObject::tr("Generating New Key for %1").arg(m_name));

	return true;
}

void
KGpgGenerateKey::postStart()
{
	QByteArray keymessage("Key-Type: ");
	switch (m_algorithm) {
	case KgpgCore::ALGO_RSA:
		keymessage.append("RSA");
		break;
	case KgpgCore::ALGO_RSA_RSA:
		keymessage.append("RSA\nSubkey-Type: RSA");
		break;
	case KgpgCore::ALGO_DSA_ELGAMAL:
		keymessage.append("DSA\nSubkey-Type: ELG-E");
		break;
	default:
		Q_ASSERT(m_algorithm == KgpgCore::ALGO_RSA);
		return;
	}

	const QByteArray keylen = QByteArray::number(m_size);

	keymessage.append("\nKey-Length: ");
	keymessage.append(keylen);
	keymessage.append("\nSubkey-Length: ");
	keymessage.append(keylen);
	keymessage.append("\nName-Real: ");
	keymessage.append(m_name.toUtf8());
	if (!m_email.isEmpty()) {
		keymessage.append("\nName-Email: ");
		keymessage.append(m_email.toAscii());
	}
	if (!m_comment.isEmpty()) {
		keymessage.append("\nName-Comment: ");
		keymessage.append(m_comment.toUtf8());
	}
	if (m_expire != 0) {
		keymessage.append("\nExpire-Date: ");
		keymessage.append(QByteArray::number(m_expire));
		keymessage.append(m_expireunit);
	}

    if(!m_password.isEmpty()) {
        keymessage.append("\nPassphrase: ");
        keymessage.append(m_password.toUtf8());
    }
    write(keymessage, true);
    write("%commit");

    /*QString passdlgmessage;
	if (!m_email.isEmpty()) {
        passdlgmessage = QObject::tr("<p><b>Enter passphrase for %1 &lt;%2&gt;</b>:<br />Passphrase should include non alphanumeric characters and random sequences.</p>").arg(m_name).arg(m_email);
	} else {
        passdlgmessage = QObject::tr("<p><b>Enter passphrase for %1</b>:<br />Passphrase should include non alphanumeric characters and random sequences.</p>").arg(m_name);
	}

	QApplication::restoreOverrideCursor();
    askNewPassphrase(passdlgmessage);*/
}

bool
KGpgGenerateKey::nextLine(const QString &line)
{
    QString msg(QObject::tr("Generating Key"));

	if (!line.startsWith(QLatin1String("[GNUPG:] ")))
		return false;

	int result = false;

    //qDebug() << "line:" << line;

	if (line.contains(QLatin1String( "PROGRESS" ))) {
            QStringList parts(line.mid(18).split(QLatin1Char( ' ' )));
		if (parts.count() >= 4) {
			const QString p0(parts.at(0));
			if (p0 == QLatin1String( "primegen" )) {
                msg = tr("Generating prime numbers");
			} else if (p0 == QLatin1String( "pk_dsa" )) {
                msg = tr("Generating DSA key");
			} else if (p0 == QLatin1String( "pk_elg" )) {
                msg = tr("Generating ElGamal key");
			} else if (p0 == QLatin1String( "need_entropy" )) {
                msg = tr("Waiting for entropy");

				// This message is currenlty not displayed. Nevertheless it's
				// included here so string freeze is not broken if it will be
				// displayed later on.
                QString msglong = tr("The entropy pool ran empty. The key generation process is stalled until enough entropy is present. You can generate entropy e.g. by moving the mouse or typing at the keyboard. The easiest way is by using another application until the key generation continues.");
			}
			if (parts.at(3) != QLatin1String( "0" ))
				emit infoProgress(parts.at(2).toUInt(), parts.at(3).toUInt());
		}
	} else if (line.contains(QLatin1String( "GOOD_PASSPHRASE" ))) {
		setSuccess(TS_MSG_SEQUENCE);
	} else if (line.contains(QLatin1String( "KEY_CREATED" ))) {
		m_fingerprint = line.right(40);
		setSuccess(TS_OK);
		result = true;
	} else if (line.contains(QLatin1String( "NEED_PASSPHRASE" ))) {
		setSuccess(TS_USER_ABORTED);
	} else if (line.contains(QLatin1String( "GET_" ))) {
		setSuccess(TS_MSG_SEQUENCE);
		result = true;
	} else if (line.contains(QLatin1String("KEY_NOT_CREATED"))) {
		result = true;
	}

	emit statusMessage(msg);
    //qDebug() << "generateKey says: " << msg;

	return result;
}

void
KGpgGenerateKey::finish()
{
	switch (getSuccess()) {
	case TS_BAD_PASSPHRASE:
        emit statusMessage(tr("Bad passphrase. Cannot generate a new key pair."));
		break;
	case TS_USER_ABORTED:
        emit statusMessage(tr("Aborted by the user. Cannot generate a new key pair."));
		break;
	case TS_INVALID_EMAIL:
        emit statusMessage(tr("The email address is not valid. Cannot generate a new key pair."));
		break;
	case TS_INVALID_NAME:
        emit statusMessage(tr("The name is not accepted by gpg. Cannot generate a new key pair."));
		break;
	case TS_OK:
        emit statusMessage(tr("Key %1 generated").arg(getFingerprint()));
		break;
	default:
		{
			QStringList errorLines;

			while (getProcess()->hasLineStandardError()) {
				QByteArray b;
				getProcess()->readLineStandardError(&b);
				errorLines << QString::fromUtf8(b);
			}

			m_errorOutput = errorLines.join(QLatin1String("\n"));
            emit statusMessage(tr("gpg process did not finish. Cannot generate a new key pair."));
		}
	}
}

void
KGpgGenerateKey::newPasswordEntered()
{
	QApplication::setOverrideCursor(Qt::BusyCursor);
	write("%commit");
}

void
KGpgGenerateKey::setName(const QString &name)
{
	m_name = name;
}

QString
KGpgGenerateKey::getName() const
{
	return m_name;
}

void
KGpgGenerateKey::setEmail(const QString &email)
{
	m_email = email;
}

QString
KGpgGenerateKey::getEmail() const
{
	return m_email;
}

void
KGpgGenerateKey::setComment(const QString &comment)
{
	m_comment = comment;
}

void
KGpgGenerateKey::setAlgorithm(const KgpgCore::KgpgKeyAlgo &algorithm)
{
	m_algorithm = algorithm;
}

void
KGpgGenerateKey::setSize(const unsigned int size)
{
	m_size = size;
}

void
KGpgGenerateKey::setExpire(const unsigned int expire, const char expireunit)
{
	Q_ASSERT((expireunit == 'd') || (expireunit == 'w') ||
			(expireunit == 'm') || (expireunit == 'y'));
	m_expire = expire;
	m_expireunit = expireunit;
}

void
KGpgGenerateKey::setPassword(const QString &password)
{
    m_password = password;
}

QString
KGpgGenerateKey::getFingerprint() const
{
	return m_fingerprint;
}

QString
KGpgGenerateKey::gpgErrorMessage() const
{
	return m_errorOutput;
}

//#include "kgpggeneratekey.moc"