aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910>2012-08-04 00:20:14 +0000
committerubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910>2012-08-04 00:20:14 +0000
commit340ef42b7ec3d11c6568eb3341dc3eae2ffce7bb (patch)
treeaea47670d7b2b3a1717a68993a7d01dff0b3a96a
parentdeleting keys works (diff)
downloadgpg4usb-340ef42b7ec3d11c6568eb3341dc3eae2ffce7bb.tar.gz
gpg4usb-340ef42b7ec3d11c6568eb3341dc3eae2ffce7bb.zip
generate key
git-svn-id: http://cpunk.de/svn/src/gpg4usb/branches/0.3.2-mac@939 34ebc366-c3a9-4b3c-9f84-69acf7962910
-rw-r--r--gpg4usb.pro6
-rw-r--r--keygendialog.cpp21
-rw-r--r--keygendialog.h1
-rw-r--r--kgpg/transactions/kgpggeneratekey.cpp276
-rw-r--r--kgpg/transactions/kgpggeneratekey.h99
5 files changed, 398 insertions, 5 deletions
diff --git a/gpg4usb.pro b/gpg4usb.pro
index 21d89e5..78720fd 100644
--- a/gpg4usb.pro
+++ b/gpg4usb.pro
@@ -69,7 +69,8 @@ HEADERS += attachments.h \
kgpg/transactions/kgpgdecrypt.h \
kgpg/transactions/kgpgexport.h \
kgpg/transactions/kgpgimport.h \
- kgpg/transactions/kgpgdelkey.h
+ kgpg/transactions/kgpgdelkey.h \
+ kgpg/transactions/kgpggeneratekey.h
SOURCES += attachments.cpp \
@@ -122,7 +123,8 @@ SOURCES += attachments.cpp \
kgpg/transactions/kgpgdecrypt.cpp \
kgpg/transactions/kgpgexport.cpp \
kgpg/transactions/kgpgimport.cpp \
- kgpg/transactions/kgpgdelkey.cpp
+ kgpg/transactions/kgpgdelkey.cpp \
+ kgpg/transactions/kgpggeneratekey.cpp
RC_FILE = gpg4usb.rc
diff --git a/keygendialog.cpp b/keygendialog.cpp
index ef07e61..6f8a5e8 100644
--- a/keygendialog.cpp
+++ b/keygendialog.cpp
@@ -124,7 +124,7 @@ void KeyGenDialog::keyGenAccept()
/**
* create the string for key generation
*/
- keyGenParams = "<GnupgKeyParms format=\"internal\">\n"
+ /*keyGenParams = "<GnupgKeyParms format=\"internal\">\n"
"Key-Type: DSA\n"
"Key-Length: 1024\n"
"Subkey-Type: ELG-E\n"
@@ -148,7 +148,20 @@ void KeyGenDialog::keyGenAccept()
keyGenParams += "</GnupgKeyParms>";
KeyGenThread *kg = new KeyGenThread(keyGenParams, mCtx);
- kg->start();
+ kg->start();*/
+
+
+ // TODO: expdate
+ KGpgGenerateKey *genkey = new KGpgGenerateKey(this,
+ nameEdit->text(),
+ emailEdit->text(),
+ commentEdit->text(),
+ KgpgCore::ALGO_RSA_RSA,
+ keySizeSpinBox->cleanText().toInt(),
+ 0,
+ 'd');
+
+ genkey->start();
this->accept();
@@ -167,7 +180,9 @@ void KeyGenDialog::keyGenAccept()
dialog->show();
- while (kg->isRunning()) {
+ //genkey->thread()->isRunning()
+
+ while (genkey->thread()->isRunning()) {
QCoreApplication::processEvents();
}
diff --git a/keygendialog.h b/keygendialog.h
index f2de274..e528d77 100644
--- a/keygendialog.h
+++ b/keygendialog.h
@@ -24,6 +24,7 @@
#include "keygenthread.h"
#include "gpgcontext.h"
+#include "kgpg/transactions/kgpggeneratekey.h"
#include <QtGui>
QT_BEGIN_NAMESPACE
diff --git a/kgpg/transactions/kgpggeneratekey.cpp b/kgpg/transactions/kgpggeneratekey.cpp
new file mode 100644
index 0000000..d95f1e0
--- /dev/null
+++ b/kgpg/transactions/kgpggeneratekey.cpp
@@ -0,0 +1,276 @@
+/*
+ * 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 <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)
+ : 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);
+
+ 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);
+ }
+ keymessage.append("\nPassphrase: ");
+ write(keymessage, false);
+
+ 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;
+
+ 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);
+
+ 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;
+}
+
+QString
+KGpgGenerateKey::getFingerprint() const
+{
+ return m_fingerprint;
+}
+
+QString
+KGpgGenerateKey::gpgErrorMessage() const
+{
+ return m_errorOutput;
+}
+
+//#include "kgpggeneratekey.moc"
diff --git a/kgpg/transactions/kgpggeneratekey.h b/kgpg/transactions/kgpggeneratekey.h
new file mode 100644
index 0000000..3649d8f
--- /dev/null
+++ b/kgpg/transactions/kgpggeneratekey.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2008,2009,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. *
+ * *
+ ***************************************************************************/
+
+#ifndef KGPGGENERATEKEY_H
+#define KGPGGENERATEKEY_H
+
+#include "kgpgtransaction.h"
+
+#include "../core/kgpgkey.h"
+
+#include <QObject>
+
+class QString;
+
+/**
+ * @brief generate a new key pair
+ */
+class KGpgGenerateKey: public KGpgTransaction {
+ Q_OBJECT
+
+ Q_DISABLE_COPY(KGpgGenerateKey)
+ KGpgGenerateKey(); // = delete C++0x
+public:
+ enum ts_generatekey {
+ TS_INVALID_NAME = TS_COMMON_END + 1 ///< the owners name is not accepted by GnuPG
+ };
+ /**
+ * @brief KGpgGenerateKey's constructor
+ * @param parent parent object
+ * @param name the name of the key, it is also the user's name.
+ * @param email email MUST be a valid email address or an empty string.
+ * @param comment is a comment, it can be an empty string
+ * @param algorithm this is the type of the key, RSA or DSA & ELGAMAL (\see Kgpg::KeyAlgo ).
+ * @param size this is the length of the key (1024, 2048, ...)
+ * @param expire defines the key expiry time together with \em expireunit, 0 for unlimited key lifetime
+ * @param expireunit is the unit of the number given as \em expire. \see setExpire
+ */
+ KGpgGenerateKey(QObject *parent, const QString &name, const QString &email, const QString &comment,
+ const KgpgCore::KgpgKeyAlgo &algorithm, const uint size, const unsigned int expire = 0,
+ const char expireunit = 'd');
+ virtual ~KGpgGenerateKey();
+
+ void setName(const QString &name);
+ QString getName() const;
+ void setEmail(const QString &email);
+ QString getEmail() const;
+ void setComment(const QString &comment);
+ void setAlgorithm(const KgpgCore::KgpgKeyAlgo &algorithm);
+ void setSize(const unsigned int size);
+ /**
+ * @brief set expire date for key
+ * @param expire defines the key expiry time together with \em expireunit, 0 for unlimited key lifetime
+ * @param expireunit is the unit of the number given as \em expire.
+ *
+ * Valid units are 'd', 'w', 'm' and 'y'. The unit is ignored if expire is 0.
+ */
+ void setExpire(const unsigned int expire, const char expireunit);
+
+ QString getFingerprint() const;
+
+ /**
+ * @brief get error output of GnuPG
+ * @return the messages GnuPG printed to standard error
+ *
+ * This will only return data after the done() signal has been emitted.
+ */
+ QString gpgErrorMessage() const;
+
+protected:
+ virtual bool preStart();
+ virtual void postStart();
+ virtual bool nextLine(const QString &line);
+ virtual void finish();
+ virtual void newPasswordEntered();
+
+private:
+ QString m_name;
+ QString m_email;
+ QString m_comment;
+ KgpgCore::KgpgKeyAlgo m_algorithm;
+ unsigned int m_size;
+ unsigned int m_expire;
+ unsigned int m_expireunit;
+ QString m_fingerprint;
+ bool m_namesent;
+ QString m_errorOutput;
+};
+
+#endif // KGPGGENERATEKEY_H