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

KNewStuff

  • sources
  • kde-4.14
  • kdelibs
  • knewstuff
  • knewstuff2
  • core
knewstuff2/core/security.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KNewStuff2.
3  Copyright (c) 2004, 2005 Andras Mantia <amantia@kde.org>
4  Copyright (c) 2007 Josef Spillner <spillner@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 //app includes
21 #include "security.h"
22 
23 //qt includes
24 #include <QtCore/QFile>
25 #include <QtCore/QFileInfo>
26 #include <QtCore/QPointer>
27 #include <QtCore/QStringList>
28 #include <QtCore/QTextIStream>
29 #include <QtCore/QTimer>
30 
31 //kde includes
32 #include <kdebug.h>
33 #include <kinputdialog.h>
34 #include <klocale.h>
35 #include <kcodecs.h>
36 #include <kmessagebox.h>
37 #include <kpassworddialog.h>
38 #include <kprocess.h>
39 
40 using namespace KNS;
41 
42 Security::Security()
43 {
44  m_keysRead = false;
45  m_gpgRunning = false;
46  readKeys();
47  readSecretKeys();
48 }
49 
50 
51 Security::~Security()
52 {
53 }
54 
55 void Security::readKeys()
56 {
57  if (m_gpgRunning) {
58  QTimer::singleShot(5, this, SLOT(readKeys()));
59  return;
60  }
61  m_runMode = List;
62  m_keys.clear();
63  m_process = new KProcess();
64  *m_process << "gpg"
65  << "--no-secmem-warning"
66  << "--no-tty"
67  << "--with-colon"
68  << "--list-keys";
69  connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)),
70  this, SLOT(slotFinished(int,QProcess::ExitStatus)));
71  connect(m_process, SIGNAL(readyReadStandardOutput()),
72  this, SLOT(slotReadyReadStandardOutput()));
73  m_process->start();
74  if (!m_process->waitForStarted()) {
75  KMessageBox::error(0L, i18n("<qt>Cannot start <i>gpg</i> and retrieve the available keys. Make sure that <i>gpg</i> is installed, otherwise verification of downloaded resources will not be possible.</qt>"));
76  delete m_process;
77  m_process = 0;
78  } else
79  m_gpgRunning = true;
80 }
81 
82 void Security::readSecretKeys()
83 {
84  if (m_gpgRunning) {
85  QTimer::singleShot(5, this, SLOT(readSecretKeys()));
86  return;
87  }
88  m_runMode = ListSecret;
89  m_process = new KProcess();
90  *m_process << "gpg"
91  << "--no-secmem-warning"
92  << "--no-tty"
93  << "--with-colon"
94  << "--list-secret-keys";
95  connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)),
96  this, SLOT(slotFinished(int,QProcess::ExitStatus)));
97  connect(m_process, SIGNAL(readyReadStandardOutput()),
98  this, SLOT(slotReadyReadStandardOutput()));
99  m_process->start();
100  if (!m_process->waitForStarted()) {
101  delete m_process;
102  m_process = 0;
103  } else
104  m_gpgRunning = true;
105 }
106 
107 void Security::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
108 {
109  if (exitStatus != QProcess::NormalExit) {
110  m_gpgRunning = false;
111  delete m_process;
112  m_process = 0;
113  return;
114  }
115  switch (m_runMode) {
116  case ListSecret:
117  m_keysRead = true;
118  break;
119  case Verify: emit validityResult(m_result);
120  break;
121  case Sign: emit fileSigned(m_result);
122  break;
123 
124  }
125  m_gpgRunning = false;
126  delete m_process;
127  m_process = 0;
128 
129  Q_UNUSED(exitCode);
130 }
131 
132 void Security::slotReadyReadStandardOutput()
133 {
134  QString data;
135  while (m_process->canReadLine()) {
136  data = QString::fromLocal8Bit(m_process->readLine());
137  switch (m_runMode) {
138  case List:
139  case ListSecret:
140  if (data.startsWith(QLatin1String("pub")) || data.startsWith(QLatin1String("sec"))) {
141  KeyStruct key;
142  if (data.startsWith(QLatin1String("pub")))
143  key.secret = false;
144  else
145  key.secret = true;
146  QStringList line = data.split(':', QString::KeepEmptyParts);
147  key.id = line[4];
148  QString shortId = key.id.right(8);
149  QString trustStr = line[1];
150  key.trusted = false;
151  if (trustStr == "u" || trustStr == "f")
152  key.trusted = true;
153  data = line[9];
154  key.mail = data.section('<', -1, -1);
155  key.mail.truncate(key.mail.length() - 1);
156  key.name = data.section('<', 0, 0);
157  if (key.name.contains("("))
158  key.name = key.name.section('(', 0, 0);
159  m_keys[shortId] = key;
160  }
161  break;
162  case Verify:
163  data = data.section(']', 1, -1).trimmed();
164  if (data.startsWith(QLatin1String("GOODSIG"))) {
165  m_result &= SIGNED_BAD_CLEAR;
166  m_result |= SIGNED_OK;
167  QString id = data.section(' ', 1 , 1).right(8);
168  if (!m_keys.contains(id)) {
169  m_result |= UNKNOWN;
170  } else {
171  m_signatureKey = m_keys[id];
172  }
173  } else
174  if (data.startsWith(QLatin1String("NO_PUBKEY"))) {
175  m_result &= SIGNED_BAD_CLEAR;
176  m_result |= UNKNOWN;
177  } else
178  if (data.startsWith(QLatin1String("BADSIG"))) {
179  m_result |= SIGNED_BAD;
180  QString id = data.section(' ', 1 , 1).right(8);
181  if (!m_keys.contains(id)) {
182  m_result |= UNKNOWN;
183  } else {
184  m_signatureKey = m_keys[id];
185  }
186  } else
187  if (data.startsWith(QLatin1String("TRUST_ULTIMATE"))) {
188  m_result &= SIGNED_BAD_CLEAR;
189  m_result |= TRUSTED;
190  }
191  break;
192 
193  case Sign:
194  if (data.contains("passphrase.enter")) {
195  KeyStruct key = m_keys[m_secretKey];
196  QPointer<KPasswordDialog> dlg = new KPasswordDialog(NULL);
197  dlg->setPrompt(i18n("<qt>Enter passphrase for key <b>0x%1</b>, belonging to<br /><i>%2&lt;%3&gt;</i><br />:</qt>", m_secretKey, key.name, key.mail));
198  if (dlg->exec()) {
199  m_process->write(dlg->password().toLocal8Bit() + '\n');
200  } else {
201  m_result |= BAD_PASSPHRASE;
202  m_process->kill();
203  delete dlg;
204  return;
205  }
206  delete dlg;
207  } else
208  if (data.contains("BAD_PASSPHRASE")) {
209  m_result |= BAD_PASSPHRASE;
210  }
211  break;
212  }
213  }
214 }
215 
216 void Security::checkValidity(const QString& filename)
217 {
218  m_fileName = filename;
219  slotCheckValidity();
220 }
221 
222 void Security::slotCheckValidity()
223 {
224  if (!m_keysRead || m_gpgRunning) {
225  QTimer::singleShot(5, this, SLOT(slotCheckValidity()));
226  return;
227  }
228  if (m_keys.count() == 0) {
229  emit validityResult(-1);
230  return;
231  }
232 
233  m_result = 0;
234  m_runMode = Verify;
235  QFileInfo f(m_fileName);
236  //check the MD5 sum
237  QString md5sum;
238  const char* c = "";
239  KMD5 context(c);
240  QFile file(m_fileName);
241  if (file.open(QIODevice::ReadOnly)) {
242  context.reset();
243  context.update(file);
244  md5sum = context.hexDigest();
245  file.close();
246  }
247  file.setFileName(f.path() + "/md5sum");
248  if (file.open(QIODevice::ReadOnly)) {
249  QByteArray md5sum_file;
250  file.readLine(md5sum_file.data(), 50);
251  if (!md5sum_file.isEmpty() && QString(md5sum_file).startsWith(md5sum))
252  m_result |= MD5_OK;
253  file.close();
254  }
255  m_result |= SIGNED_BAD;
256  m_signatureKey.id = "";
257  m_signatureKey.name = "";
258  m_signatureKey.mail = "";
259  m_signatureKey.trusted = false;
260 
261  //verify the signature
262  m_process = new KProcess();
263  *m_process << "gpg"
264  << "--no-secmem-warning"
265  << "--status-fd=2"
266  << "--command-fd=0"
267  << "--verify"
268  << f.path() + "/signature"
269  << m_fileName;
270  connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)),
271  this, SLOT(slotFinished(int,QProcess::ExitStatus)));
272  connect(m_process, SIGNAL(readyReadStandardOutput()),
273  this, SLOT(slotReadyReadStandardOutput()));
274  m_process->start();
275  if (m_process->waitForStarted())
276  m_gpgRunning = true;
277  else {
278  KMessageBox::error(0L, i18n("<qt>Cannot start <i>gpg</i> and check the validity of the file. Make sure that <i>gpg</i> is installed, otherwise verification of downloaded resources will not be possible.</qt>"));
279  emit validityResult(0);
280  delete m_process;
281  m_process = 0;
282  }
283 }
284 
285 void Security::signFile(const QString &fileName)
286 {
287  m_fileName = fileName;
288  slotSignFile();
289 }
290 
291 void Security::slotSignFile()
292 {
293  if (!m_keysRead || m_gpgRunning) {
294  QTimer::singleShot(5, this, SLOT(slotSignFile()));
295  return;
296  }
297 
298  QStringList secretKeys;
299  for (QMap<QString, KeyStruct>::Iterator it = m_keys.begin(); it != m_keys.end(); ++it) {
300  if (it.value().secret)
301  secretKeys.append(it.key());
302  }
303 
304  if (secretKeys.count() == 0) {
305  emit fileSigned(-1);
306  return;
307  }
308 
309  m_result = 0;
310  QFileInfo f(m_fileName);
311 
312  //create the MD5 sum
313  QString md5sum;
314  const char* c = "";
315  KMD5 context(c);
316  QFile file(m_fileName);
317  if (file.open(QIODevice::ReadOnly)) {
318  context.reset();
319  context.update(file);
320  md5sum = context.hexDigest();
321  file.close();
322  }
323  file.setFileName(f.path() + "/md5sum");
324  if (file.open(QIODevice::WriteOnly)) {
325  QTextStream stream(&file);
326  stream << md5sum;
327  m_result |= MD5_OK;
328  file.close();
329  }
330 
331  if (secretKeys.count() > 1) {
332  bool ok;
333  secretKeys = KInputDialog::getItemList(i18n("Select Signing Key"), i18n("Key used for signing:"), secretKeys, QStringList(secretKeys[0]), false, &ok);
334  if (ok)
335  m_secretKey = secretKeys[0];
336  else {
337  emit fileSigned(0);
338  return;
339  }
340  } else
341  m_secretKey = secretKeys[0];
342 
343  //verify the signature
344  m_process = new KProcess();
345  *m_process << "gpg"
346  << "--no-secmem-warning"
347  << "--status-fd=2"
348  << "--command-fd=0"
349  << "--no-tty"
350  << "--detach-sign"
351  << "-u"
352  << m_secretKey
353  << "-o"
354  << f.path() + "/signature"
355  << m_fileName;
356  connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)),
357  this, SLOT(slotFinished(int,QProcess::ExitStatus)));
358  connect(m_process, SIGNAL(readyReadStandardOutput()),
359  this, SLOT(slotReadyReadStandardOutput()));
360  m_runMode = Sign;
361  m_process->start();
362  if (m_process->waitForStarted())
363  m_gpgRunning = true;
364  else {
365  KMessageBox::error(0L, i18n("<qt>Cannot start <i>gpg</i> and sign the file. Make sure that <i>gpg</i> is installed, otherwise signing of the resources will not be possible.</qt>"));
366  emit fileSigned(0);
367  delete m_process;
368  m_process = 0;
369  }
370 }
371 
372 #include "security.moc"
i18n
QString i18n(const char *text)
KNS::Security::MD5_OK
Definition: knewstuff2/core/security.h:85
KProcess
KNS::Security::SIGNED_BAD_CLEAR
The key is unknown.
Definition: knewstuff2/core/security.h:90
QProcess::kill
void kill()
QString::truncate
void truncate(int position)
QFileInfo::path
QString path() const
QMap::contains
bool contains(const Key &key) const
kdebug.h
QByteArray
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QMap
QPointer
QByteArray::isEmpty
bool isEmpty() const
QFile::setFileName
void setFileName(const QString &name)
QMap::clear
void clear()
KNS::Security::readKeys
void readKeys()
Reads the available public keys.
Definition: knewstuff2/core/security.cpp:55
klocale.h
QFile
KNS::Security::slotCheckValidity
void slotCheckValidity()
Verifies the integrity and the signature of a tarball file (see m_fileName).
Definition: knewstuff2/core/security.cpp:222
KNS::Security::BAD_PASSPHRASE
used to clear the SIGNED_BAD flag
Definition: knewstuff2/core/security.h:91
QTextStream
KeyStruct
Definition: knewstuff2/core/security.h:27
KNS::Security::TRUSTED
The file is signed with a bad signature.
Definition: knewstuff2/core/security.h:88
KNS::Security::signFile
void signFile(const QString &fileName)
Creates a signature and an md5sum file for the fileName and packs everything into a gzipped tarball...
Definition: knewstuff2/core/security.cpp:285
kprocess.h
QList::count
int count(const T &value) const
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
QList::append
void append(const T &value)
KInputDialog::getItemList
QStringList getItemList(const QString &caption, const QString &label, const QStringList &list=QStringList(), const QStringList &select=QStringList(), bool multiple=false, bool *ok=0, QWidget *parent=0)
KNS::Security::checkValidity
void checkValidity(const QString &fileName)
Verifies the integrity and the signature of a tarball file.
Definition: knewstuff2/core/security.cpp:216
KNS::Security::SIGNED_OK
The MD5 sum check is OK.
Definition: knewstuff2/core/security.h:86
KNS::Security::UNKNOWN
The signature is trusted.
Definition: knewstuff2/core/security.h:89
QString::trimmed
QString trimmed() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
KNS::Security::slotSignFile
void slotSignFile()
Creates a signature and an md5sum file for the m_fileName and packs everything into a gzipped tarball...
Definition: knewstuff2/core/security.cpp:291
KMD5::update
void update(const char *in, int len=-1)
KeyStruct::id
QString id
Definition: knewstuff2/core/security.h:28
QString
KeyStruct::name
QString name
Definition: knewstuff2/core/security.h:29
QMap::end
iterator end()
KMD5::hexDigest
QByteArray hexDigest()
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QMap::begin
iterator begin()
QStringList
KProcess::start
void start()
QString::right
QString right(int n) const
QFileInfo
KeyStruct::secret
bool secret
Definition: knewstuff2/core/security.h:32
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QProcess::waitForStarted
bool waitForStarted(int msecs)
QFile::close
virtual void close()
KMD5
KNS::Security::SIGNED_BAD
The file is signed with a good signature.
Definition: knewstuff2/core/security.h:87
ok
KGuiItem ok()
kinputdialog.h
KPasswordDialog
kpassworddialog.h
QLatin1String
QProcess::canReadLine
virtual bool canReadLine() const
QString::length
int length() const
QByteArray::data
char * data()
KMD5::reset
void reset()
QString::section
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
QIODevice::write
qint64 write(const char *data, qint64 maxSize)
KeyStruct::trusted
bool trusted
Definition: knewstuff2/core/security.h:31
KNS::Security::readSecretKeys
void readSecretKeys()
Reads the available secret keys.
Definition: knewstuff2/core/security.cpp:82
kcodecs.h
KNS::Security::validityResult
void validityResult(int result)
Sent when the validity check is done.
kmessagebox.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
security.h
KNS::Security::fileSigned
void fileSigned(int result)
Sent when the signing is done.
KNS::Security::~Security
~Security()
Definition: knewstuff2/core/security.cpp:51
QMap::count
int count(const Key &key) const
KMessageBox::error
static void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
KeyStruct::mail
QString mail
Definition: knewstuff2/core/security.h:30
QIODevice::readLine
qint64 readLine(char *data, qint64 maxSize)
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:25:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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