• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kgpg

  • sources
  • kde-4.14
  • kdeutils
  • kgpg
  • transactions
kgpggeneratekey.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008,2009,2010,2011,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
3  */
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "kgpggeneratekey.h"
15 
16 #include "gpgproc.h"
17 
18 #include <KLocale>
19 #include <KMessageBox>
20 #include <kpimutils/email.h>
21 #include <QApplication>
22 
23 KGpgGenerateKey::KGpgGenerateKey(QObject *parent, const QString &name, const QString &email, const QString &comment,
24  const KgpgCore::KgpgKeyAlgo &algorithm, const uint size, const unsigned int expire,
25  const char expireunit, const KgpgCore::KgpgSubKeyType capabilities)
26  : KGpgTransaction(parent),
27  m_name(name),
28  m_email(email),
29  m_comment(comment),
30  m_algorithm(algorithm),
31  m_capabilities(capabilities),
32  m_size(size),
33  m_expire(expire),
34  m_expireunit(expireunit)
35 {
36  Q_ASSERT((expireunit == 'd') || (expireunit == 'w') ||
37  (expireunit == 'm') || (expireunit == 'y'));
38 
39  addArgument(QLatin1String("--status-fd=1"));
40  addArgument(QLatin1String("--command-fd=0"));
41  addArgument(QLatin1String("--no-verbose"));
42  addArgument(QLatin1String("--gen-key"));
43  addArgument(QLatin1String("--batch"));
44 
45  getProcess()->setOutputChannelMode(KProcess::SeparateChannels);
46 }
47 
48 KGpgGenerateKey::~KGpgGenerateKey()
49 {
50 }
51 
52 bool
53 KGpgGenerateKey::preStart()
54 {
55  if (!m_email.isEmpty() && !KPIMUtils::isValidSimpleAddress(m_email)) {
56  setSuccess(TS_INVALID_EMAIL);
57  return false;
58  }
59 
60  m_fingerprint.clear();
61 
62  setSuccess(TS_MSG_SEQUENCE);
63 
64  setDescription(i18n("Generating New Key for %1", m_name));
65 
66  return true;
67 }
68 
69 void
70 KGpgGenerateKey::postStart()
71 {
72  QByteArray keymessage = "Key-Type: ";
73 
74  switch (m_algorithm) {
75  case KgpgCore::ALGO_RSA:
76  keymessage.append("RSA");
77  break;
78  case KgpgCore::ALGO_RSA_RSA:
79  keymessage.append("RSA\nSubkey-Type: RSA");
80  break;
81  case KgpgCore::ALGO_DSA_ELGAMAL:
82  keymessage.append("DSA\nSubkey-Type: ELG-E");
83  break;
84  default:
85  Q_ASSERT(m_algorithm == KgpgCore::ALGO_RSA);
86  return;
87  }
88 
89  const QByteArray keylen = QByteArray::number(m_size);
90 
91  keymessage.append("\nKey-Length: ");
92  keymessage.append(keylen);
93  keymessage.append("\nSubkey-Length: ");
94  keymessage.append(keylen);
95  keymessage.append("\nName-Real: ");
96  keymessage.append(m_name.toUtf8());
97 
98  if (!m_email.isEmpty()) {
99  keymessage.append("\nName-Email: ");
100  keymessage.append(m_email.toAscii());
101  }
102 
103  if (!m_comment.isEmpty()) {
104  keymessage.append("\nName-Comment: ");
105  keymessage.append(m_comment.toUtf8());
106  }
107 
108  if (m_expire != 0) {
109  keymessage.append("\nExpire-Date: ");
110  keymessage.append(QByteArray::number(m_expire));
111  keymessage.append(m_expireunit);
112  }
113 
114  if (m_capabilities) {
115  keymessage.append("\nKey-Usage: ");
116  QStringList usage;
117 #if 0
118  // GnuPG always adds cert, but it does not allow this to be
119  // explicitly specified
120  if (m_capabilities & KgpgCore::SKT_CERTIFICATION)
121  usage << QLatin1String("cert");
122 #endif
123  if (m_capabilities & KgpgCore::SKT_AUTHENTICATION)
124  usage << QLatin1String("auth");
125  if (m_capabilities & KgpgCore::SKT_ENCRYPTION)
126  usage << QLatin1String("encrypt");
127  if (m_capabilities & KgpgCore::SKT_SIGNATURE)
128  usage << QLatin1String("sign");
129  keymessage.append(usage.join(QLatin1String(" ")).toAscii());
130  }
131 
132  keymessage.append("\nPassphrase: ");
133  write(keymessage, false);
134 
135  QString passdlgmessage;
136  if (!m_email.isEmpty()) {
137  passdlgmessage = i18n("<p><b>Enter passphrase for %1 &lt;%2&gt;</b>:<br />Passphrase should include non alphanumeric characters and random sequences.</p>",
138  m_name, m_email);
139  } else {
140  passdlgmessage = i18n("<p><b>Enter passphrase for %1</b>:<br />Passphrase should include non alphanumeric characters and random sequences.</p>",
141  m_name);
142  }
143 
144  QApplication::restoreOverrideCursor();
145  askNewPassphrase(passdlgmessage);
146 }
147 
148 bool
149 KGpgGenerateKey::nextLine(const QString &line)
150 {
151  QString msg = i18n("Generating Key");
152 
153  if (!line.startsWith(QLatin1String("[GNUPG:] ")))
154  return false;
155 
156  int result = false;
157 
158  if (line.contains(QLatin1String( "PROGRESS" ))) {
159  const QStringList parts = line.mid(18).split(QLatin1Char(' '));
160 
161  if (parts.count() >= 4) {
162  const QString p0(parts.at(0));
163  if (p0 == QLatin1String( "primegen" )) {
164  msg = i18n("Generating prime numbers");
165  } else if (p0 == QLatin1String( "pk_dsa" )) {
166  msg = i18n("Generating DSA key");
167  } else if (p0 == QLatin1String( "pk_elg" )) {
168  msg = i18n("Generating ElGamal key");
169  } else if (p0 == QLatin1String( "need_entropy" )) {
170  msg = i18n("Waiting for entropy");
171 
172  // This message is currenlty not displayed. Nevertheless it's
173  // included here so string freeze is not broken if it will be
174  // displayed later on.
175  QString msglong = i18n("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.");
176  }
177  if (parts.at(3) != QLatin1String( "0" ))
178  emit infoProgress(parts.at(2).toUInt(), parts.at(3).toUInt());
179  }
180  } else if (line.contains(QLatin1String( "GOOD_PASSPHRASE" ))) {
181  setSuccess(TS_MSG_SEQUENCE);
182  } else if (line.contains(QLatin1String( "KEY_CREATED" ))) {
183  m_fingerprint = line.right(40);
184  setSuccess(TS_OK);
185  result = true;
186  } else if (line.contains(QLatin1String( "NEED_PASSPHRASE" ))) {
187  setSuccess(TS_USER_ABORTED);
188  } else if (line.contains(QLatin1String( "GET_" ))) {
189  setSuccess(TS_MSG_SEQUENCE);
190  result = true;
191  } else if (line.contains(QLatin1String("KEY_NOT_CREATED"))) {
192  result = true;
193  }
194 
195  emit statusMessage(msg);
196 
197  return result;
198 }
199 
200 void
201 KGpgGenerateKey::finish()
202 {
203  switch (getSuccess()) {
204  case TS_BAD_PASSPHRASE:
205  emit statusMessage(i18n("Bad passphrase. Cannot generate a new key pair."));
206  break;
207  case TS_USER_ABORTED:
208  emit statusMessage(i18n("Aborted by the user. Cannot generate a new key pair."));
209  break;
210  case TS_INVALID_EMAIL:
211  emit statusMessage(i18n("The email address is not valid. Cannot generate a new key pair."));
212  break;
213  case TS_INVALID_NAME:
214  emit statusMessage(i18n("The name is not accepted by gpg. Cannot generate a new key pair."));
215  break;
216  case TS_OK:
217  emit statusMessage(i18n("Key %1 generated", getFingerprint()));
218  break;
219  default:
220  {
221  QStringList errorLines;
222 
223  while (getProcess()->hasLineStandardError()) {
224  QByteArray b;
225  getProcess()->readLineStandardError(&b);
226  errorLines << QString::fromUtf8(b);
227  }
228 
229  m_errorOutput = errorLines.join(QLatin1String("\n"));
230  emit statusMessage(i18n("gpg process did not finish. Cannot generate a new key pair."));
231  }
232  }
233 }
234 
235 void
236 KGpgGenerateKey::newPassphraseEntered()
237 {
238  QApplication::setOverrideCursor(Qt::BusyCursor);
239  write("%commit");
240 }
241 
242 QString
243 KGpgGenerateKey::getName() const
244 {
245  return m_name;
246 }
247 
248 QString
249 KGpgGenerateKey::getEmail() const
250 {
251  return m_email;
252 }
253 
254 QString
255 KGpgGenerateKey::getFingerprint() const
256 {
257  return m_fingerprint;
258 }
259 
260 QString
261 KGpgGenerateKey::gpgErrorMessage() const
262 {
263  return m_errorOutput;
264 }
265 
266 #include "kgpggeneratekey.moc"
KGpgTransaction::getSuccess
int getSuccess() const
get the success value that will be returned with the done signal
Definition: kgpgtransaction.cpp:442
KgpgCore::SKT_SIGNATURE
Definition: kgpgkey.h:73
KGpgTransaction::addArgument
int addArgument(const QString &arg)
add a command line argument to gpg process
Definition: kgpgtransaction.cpp:562
KgpgCore::SKT_ENCRYPTION
Definition: kgpgkey.h:72
KGpgGenerateKey::getFingerprint
QString getFingerprint() const
return the fingerprint of the generated key
Definition: kgpggeneratekey.cpp:255
KGpgTransaction::TS_OK
everything went fine
Definition: kgpgtransaction.h:60
KGpgTransaction::write
void write(const QByteArray &a, const bool lf=true)
write data to standard input of gpg process
Definition: kgpgtransaction.cpp:413
QByteArray
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KGpgGenerateKey::getEmail
QString getEmail() const
Definition: kgpggeneratekey.cpp:249
KGpgTransaction::infoProgress
void infoProgress(qulonglong processedAmount, qulonglong totalAmount)
emits procentual status information
QList::at
const T & at(int i) const
KGpgGenerateKey::nextLine
virtual bool nextLine(const QString &line)
Called for every line the gpg process writes.
Definition: kgpggeneratekey.cpp:149
QStringList::join
QString join(const QString &separator) const
KGpgTransaction::getProcess
GPGProc * getProcess()
get a reference to the gpg process object
Definition: kgpgtransaction.cpp:556
QString::clear
void clear()
QList::count
int count(const T &value) const
QString::fromUtf8
QString fromUtf8(const char *str, int size)
KGpgTransaction::statusMessage
void statusMessage(const QString &msg)
emits textual status information
QObject
KGpgTransaction::askNewPassphrase
void askNewPassphrase(const QString &text)
Ask user for passphrase and send it to gpg process.
Definition: kgpgtransaction.cpp:428
KGpgGenerateKey::newPassphraseEntered
virtual void newPassphraseEntered()
called when the user entered a new passphrase
Definition: kgpggeneratekey.cpp:236
QString::isEmpty
bool isEmpty() const
QByteArray::number
QByteArray number(int n, int base)
KGpgGenerateKey::finish
virtual void finish()
Called when the gpg process finishes.
Definition: kgpggeneratekey.cpp:201
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
gpgproc.h
QApplication::setOverrideCursor
void setOverrideCursor(const QCursor &cursor)
QApplication::restoreOverrideCursor
void restoreOverrideCursor()
KGpgGenerateKey::gpgErrorMessage
QString gpgErrorMessage() const
get error output of GnuPG
Definition: kgpggeneratekey.cpp:261
KGpgGenerateKey::postStart
virtual void postStart()
Called when the gpg process is up and running.
Definition: kgpggeneratekey.cpp:70
QString
KgpgCore::SKT_CERTIFICATION
Definition: kgpgkey.h:75
KGpgTransaction::TS_MSG_SEQUENCE
unexpected sequence of GnuPG messages
Definition: kgpgtransaction.h:62
QStringList
QString::right
QString right(int n) const
QByteArray::append
QByteArray & append(char ch)
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QLatin1Char
KGpgTransaction::TS_USER_ABORTED
the user aborted the transaction
Definition: kgpgtransaction.h:63
KGpgGenerateKey::getName
QString getName() const
Definition: kgpggeneratekey.cpp:243
KgpgCore::ALGO_RSA_RSA
Definition: kgpgkey.h:40
KGpgGenerateKey::~KGpgGenerateKey
virtual ~KGpgGenerateKey()
Definition: kgpggeneratekey.cpp:48
QString::mid
QString mid(int position, int n) const
QLatin1String
KGpgTransaction::setSuccess
void setSuccess(const int v)
set the success value that will be returned with the done signal
Definition: kgpgtransaction.cpp:448
KLineBufferedProcess::readLineStandardError
bool readLineStandardError(QByteArray *line)
Reads a line of text (excluding '\n') from stderr.
Definition: klinebufferedprocess.cpp:103
KGpgTransaction::TS_INVALID_EMAIL
the given email address is invalid
Definition: kgpgtransaction.h:64
KGpgTransaction::TS_BAD_PASSPHRASE
the passphrase was not correct
Definition: kgpgtransaction.h:61
kgpggeneratekey.h
KGpgTransaction
Process one GnuPG operation.
Definition: kgpgtransaction.h:44
KgpgCore::SKT_AUTHENTICATION
Definition: kgpgkey.h:74
KGpgTransaction::setDescription
void setDescription(const QString &description)
set the description returned in getDescription()
Definition: kgpgtransaction.cpp:489
QString::toAscii
QByteArray toAscii() const
KgpgCore::ALGO_RSA
Definition: kgpgkey.h:36
KGpgGenerateKey::TS_INVALID_NAME
the owners name is not accepted by GnuPG
Definition: kgpggeneratekey.h:35
KGpgGenerateKey::preStart
virtual bool preStart()
Called before the gpg process is started.
Definition: kgpggeneratekey.cpp:53
KgpgCore::ALGO_DSA_ELGAMAL
Definition: kgpgkey.h:39
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal