24#include <QtCore/QFile>
25#include <QtCore/QFileInfo>
26#include <QtCore/QPointer>
27#include <QtCore/QStringList>
28#include <QtCore/QTextIStream>
29#include <QtCore/QTimer>
46 if ( gpgExe.isEmpty() )
48 if ( gpgExe.isEmpty() )
49 return QLatin1String(
"gpg" );
69 QTimer::singleShot(5,
this, SLOT(
readKeys()));
76 <<
"--no-secmem-warning"
80 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
81 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
82 connect(m_process, SIGNAL(readyReadStandardOutput()),
83 this, SLOT(slotReadyReadStandardOutput()));
85 if (!m_process->waitForStarted()) {
86 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>"));
99 m_runMode = ListSecret;
102 <<
"--no-secmem-warning"
105 <<
"--list-secret-keys";
106 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
107 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
108 connect(m_process, SIGNAL(readyReadStandardOutput()),
109 this, SLOT(slotReadyReadStandardOutput()));
111 if (!m_process->waitForStarted()) {
118void Security::slotFinished(
int exitCode, QProcess::ExitStatus exitStatus)
120 if (exitStatus != QProcess::NormalExit) {
121 m_gpgRunning =
false;
136 m_gpgRunning =
false;
143void Security::slotReadyReadStandardOutput()
146 while (m_process->canReadLine()) {
147 data = QString::fromLocal8Bit(m_process->readLine());
151 if (data.startsWith(QLatin1String(
"pub")) || data.startsWith(QLatin1String(
"sec"))) {
153 if (data.startsWith(QLatin1String(
"pub")))
157 QStringList line = data.split(
':', QString::KeepEmptyParts);
159 QString shortId = key.
id.right(8);
160 QString trustStr = line[1];
162 if (trustStr ==
"u" || trustStr ==
"f")
165 key.
mail = data.section(
'<', -1, -1);
166 key.
mail.truncate(key.
mail.length() - 1);
167 key.
name = data.section(
'<', 0, 0);
168 if (key.
name.contains(
"("))
169 key.
name = key.
name.section(
'(', 0, 0);
170 m_keys[shortId] = key;
174 data = data.section(
']', 1, -1).trimmed();
175 if (data.startsWith(QLatin1String(
"GOODSIG"))) {
178 QString
id = data.section(
' ', 1 , 1).right(8);
179 if (!m_keys.contains(
id)) {
182 m_signatureKey = m_keys[id];
185 if (data.startsWith(QLatin1String(
"NO_PUBKEY"))) {
189 if (data.startsWith(QLatin1String(
"BADSIG"))) {
191 QString
id = data.section(
' ', 1 , 1).right(8);
192 if (!m_keys.contains(
id)) {
195 m_signatureKey = m_keys[id];
198 if (data.startsWith(QLatin1String(
"TRUST_ULTIMATE"))) {
205 if (data.contains(
"passphrase.enter")) {
206 KeyStruct key = m_keys[m_secretKey];
207 QPointer<KPasswordDialog> dlg =
new KPasswordDialog(NULL);
208 dlg->setPrompt(
i18n(
"<qt>Enter passphrase for key <b>0x%1</b>, belonging to<br /><i>%2<%3></i><br />:</qt>", m_secretKey, key.
name, key.
mail));
210 m_process->write(dlg->password().toLocal8Bit() +
'\n');
219 if (data.contains(
"BAD_PASSPHRASE")) {
229 m_fileName = filename;
235 if (!m_keysRead || m_gpgRunning) {
239 if (m_keys.count() == 0) {
246 QFileInfo
f(m_fileName);
251 QFile file(m_fileName);
252 if (file.open(QIODevice::ReadOnly)) {
258 file.setFileName(
f.path() +
"/md5sum");
259 if (file.open(QIODevice::ReadOnly)) {
260 QByteArray md5sum_file;
261 file.readLine(md5sum_file.data(), 50);
262 if (!md5sum_file.isEmpty() && QString(md5sum_file).startsWith(md5sum))
267 m_signatureKey.id =
"";
268 m_signatureKey.name =
"";
269 m_signatureKey.mail =
"";
270 m_signatureKey.trusted =
false;
275 <<
"--no-secmem-warning"
279 <<
f.path() +
"/signature"
281 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
282 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
283 connect(m_process, SIGNAL(readyReadStandardOutput()),
284 this, SLOT(slotReadyReadStandardOutput()));
286 if (m_process->waitForStarted())
289 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>"));
298 m_fileName = fileName;
304 if (!m_keysRead || m_gpgRunning) {
309 QStringList secretKeys;
311 if (it.value().secret)
312 secretKeys.append(it.key());
315 if (secretKeys.count() == 0) {
321 QFileInfo
f(m_fileName);
327 QFile file(m_fileName);
328 if (file.open(QIODevice::ReadOnly)) {
334 file.setFileName(
f.path() +
"/md5sum");
335 if (file.open(QIODevice::WriteOnly)) {
336 QTextStream stream(&file);
342 if (secretKeys.count() > 1) {
346 m_secretKey = secretKeys[0];
352 m_secretKey = secretKeys[0];
357 <<
"--no-secmem-warning"
365 <<
f.path() +
"/signature"
367 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
368 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
369 connect(m_process, SIGNAL(readyReadStandardOutput()),
370 this, SLOT(slotReadyReadStandardOutput()));
373 if (m_process->waitForStarted())
376 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>"));
383#include "security.moc"
void update(const char *in, int len=-1)
static void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
void readSecretKeys()
Reads the available secret keys.
void validityResult(int result)
Sent when the validity check is done.
void readKeys()
Reads the available public keys.
void fileSigned(int result)
Sent when the signing is done.
void checkValidity(const QString &fileName)
Verifies the integrity and the signature of a tarball file.
@ SIGNED_OK
The MD5 sum check is OK.
@ UNKNOWN
The signature is trusted.
@ BAD_PASSPHRASE
used to clear the SIGNED_BAD flag
@ SIGNED_BAD_CLEAR
The key is unknown.
@ TRUSTED
The file is signed with a bad signature.
@ SIGNED_BAD
The file is signed with a good signature.
void slotCheckValidity()
Verifies the integrity and the signature of a tarball file (see m_fileName).
void signFile(const QString &fileName)
Creates a signature and an md5sum file for the fileName and packs everything into a gzipped tarball.
void slotSignFile()
Creates a signature and an md5sum file for the m_fileName and packs everything into a gzipped tarball...
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
QString i18n(const char *text)
static QString gpgExecutable()
static QString gpgExecutable()