You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
10 KiB
345 lines
10 KiB
15 years ago
|
/***************************************************************************
|
||
|
security.cpp - description
|
||
|
-------------------
|
||
|
begin : Thu Jun 24 11:22:12 2004
|
||
|
copyright : (C) 2004, 2005 by Andras Mantia <amantia@kde.org>
|
||
|
***************************************************************************/
|
||
|
|
||
|
/***************************************************************************
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU Library General Public License as *
|
||
|
* published by the Free Software Foundation; version 2 of the License. *
|
||
|
* *
|
||
|
***************************************************************************/
|
||
|
|
||
|
//qt includes
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqfileinfo.h>
|
||
|
#include <tqstringlist.h>
|
||
|
#include <tqtimer.h>
|
||
15 years ago
|
|
||
|
//kde includes
|
||
|
#include <kdebug.h>
|
||
|
#include <kinputdialog.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
#include <kmdcodec.h>
|
||
12 years ago
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <kpassdlg.h>
|
||
|
#include <kprocio.h>
|
||
|
|
||
|
//app includes
|
||
|
#include "security.h"
|
||
|
|
||
|
using namespace KNS;
|
||
|
|
||
|
Security::Security()
|
||
|
{
|
||
|
m_keysRead = false;
|
||
|
m_gpgRunning = false;
|
||
|
readKeys();
|
||
|
readSecretKeys();
|
||
|
}
|
||
|
|
||
|
|
||
|
Security::~Security()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void Security::readKeys()
|
||
|
{
|
||
|
if (m_gpgRunning)
|
||
|
{
|
||
15 years ago
|
TQTimer::singleShot(5, this, TQT_SLOT(readKeys()));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
m_runMode = List;
|
||
|
m_keys.clear();
|
||
|
KProcIO *readProcess=new KProcIO();
|
||
|
*readProcess << "gpg"<<"--no-secmem-warning"<<"--no-tty"<<"--with-colon"<<"--list-keys";
|
||
12 years ago
|
connect(readProcess, TQT_SIGNAL(processExited(TDEProcess *)), this, TQT_SLOT(slotProcessExited(TDEProcess *)));
|
||
15 years ago
|
connect(readProcess, TQT_SIGNAL(readReady(KProcIO *)) ,this, TQT_SLOT(slotDataArrived(KProcIO *)));
|
||
12 years ago
|
if (!readProcess->start(TDEProcess::NotifyOnExit, true))
|
||
15 years ago
|
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>"));
|
||
|
else
|
||
|
m_gpgRunning = true;
|
||
|
}
|
||
|
|
||
|
void Security::readSecretKeys()
|
||
|
{
|
||
|
if (m_gpgRunning)
|
||
|
{
|
||
15 years ago
|
TQTimer::singleShot(5, this, TQT_SLOT(readSecretKeys()));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
m_runMode = ListSecret;
|
||
|
KProcIO *readProcess=new KProcIO();
|
||
|
*readProcess << "gpg"<<"--no-secmem-warning"<<"--no-tty"<<"--with-colon"<<"--list-secret-keys";
|
||
12 years ago
|
connect(readProcess, TQT_SIGNAL(processExited(TDEProcess *)), this, TQT_SLOT(slotProcessExited(TDEProcess *)));
|
||
15 years ago
|
connect(readProcess, TQT_SIGNAL(readReady(KProcIO *)) ,this, TQT_SLOT(slotDataArrived(KProcIO *)));
|
||
12 years ago
|
if (readProcess->start(TDEProcess::NotifyOnExit, true))
|
||
15 years ago
|
m_gpgRunning = true;
|
||
|
}
|
||
|
|
||
12 years ago
|
void Security::slotProcessExited(TDEProcess *process)
|
||
15 years ago
|
{
|
||
|
switch (m_runMode)
|
||
|
{
|
||
|
case ListSecret:
|
||
|
m_keysRead = true;
|
||
|
break;
|
||
|
case Verify: emit validityResult(m_result);
|
||
|
break;
|
||
|
case Sign: emit fileSigned(m_result);
|
||
|
break;
|
||
|
|
||
|
}
|
||
|
m_gpgRunning = false;
|
||
|
delete process;
|
||
|
}
|
||
|
|
||
|
void Security::slotDataArrived(KProcIO *procIO)
|
||
|
{
|
||
15 years ago
|
TQString data;
|
||
15 years ago
|
while (procIO->readln(data, true) != -1)
|
||
|
{
|
||
|
switch (m_runMode)
|
||
|
{
|
||
|
case List:
|
||
|
case ListSecret:
|
||
|
if (data.startsWith("pub") || data.startsWith("sec"))
|
||
|
{
|
||
|
KeyStruct key;
|
||
|
if (data.startsWith("pub"))
|
||
|
key.secret = false;
|
||
|
else
|
||
|
key.secret = true;
|
||
15 years ago
|
TQStringList line = TQStringList::split(":", data, true);
|
||
15 years ago
|
key.id = line[4];
|
||
15 years ago
|
TQString shortId = key.id.right(8);
|
||
|
TQString trustStr = line[1];
|
||
15 years ago
|
key.trusted = false;
|
||
|
if (trustStr == "u" || trustStr == "f")
|
||
|
key.trusted = true;
|
||
|
data = line[9];
|
||
|
key.mail=data.section('<', -1, -1);
|
||
|
key.mail.truncate(key.mail.length() - 1);
|
||
|
key.name=data.section('<',0,0);
|
||
13 years ago
|
if (key.name.find("(")!=-1)
|
||
15 years ago
|
key.name=key.name.section('(',0,0);
|
||
|
m_keys[shortId] = key;
|
||
|
}
|
||
|
break;
|
||
|
case Verify:
|
||
14 years ago
|
data = TQString(data.section("]",1,-1)).stripWhiteSpace();
|
||
15 years ago
|
if (data.startsWith("GOODSIG"))
|
||
|
{
|
||
|
m_result &= SIGNED_BAD_CLEAR;
|
||
|
m_result |= SIGNED_OK;
|
||
15 years ago
|
TQString id = data.section(" ", 1 , 1).right(8);
|
||
13 years ago
|
if (!m_keys.contains(id))
|
||
15 years ago
|
{
|
||
|
m_result |= UNKNOWN;
|
||
|
} else
|
||
|
{
|
||
|
m_signatureKey = m_keys[id];
|
||
|
}
|
||
|
} else
|
||
|
if (data.startsWith("NO_PUBKEY"))
|
||
|
{
|
||
|
m_result &= SIGNED_BAD_CLEAR;
|
||
|
m_result |= UNKNOWN;
|
||
|
} else
|
||
|
if (data.startsWith("BADSIG"))
|
||
|
{
|
||
|
m_result |= SIGNED_BAD;
|
||
15 years ago
|
TQString id = data.section(" ", 1 , 1).right(8);
|
||
13 years ago
|
if (!m_keys.contains(id))
|
||
15 years ago
|
{
|
||
|
m_result |= UNKNOWN;
|
||
|
} else
|
||
|
{
|
||
|
m_signatureKey = m_keys[id];
|
||
|
}
|
||
|
} else
|
||
|
if (data.startsWith("TRUST_ULTIMATE"))
|
||
|
{
|
||
|
m_result &= SIGNED_BAD_CLEAR;
|
||
|
m_result |= TRUSTED;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case Sign:
|
||
13 years ago
|
if (data.find("passphrase.enter") != -1)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString password;
|
||
15 years ago
|
KeyStruct key = m_keys[m_secretKey];
|
||
|
int result = KPasswordDialog::getPassword(password, i18n("<qt>Enter passphrase for key <b>0x%1</b>, belonging to<br><i>%2<%3></i>:</qt>").arg(m_secretKey).arg(key.name).arg(key.mail));
|
||
|
if (result == KPasswordDialog::Accepted)
|
||
|
{
|
||
|
procIO->writeStdin(password, true);
|
||
|
password.fill(' ');
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
m_result |= BAD_PASSPHRASE;
|
||
|
slotProcessExited(procIO);
|
||
|
return;
|
||
|
}
|
||
|
} else
|
||
13 years ago
|
if (data.find("BAD_PASSPHRASE") != -1)
|
||
15 years ago
|
{
|
||
|
m_result |= BAD_PASSPHRASE;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void Security::checkValidity(const TQString& filename)
|
||
15 years ago
|
{
|
||
|
m_fileName = filename;
|
||
|
slotCheckValidity();
|
||
|
}
|
||
|
|
||
|
void Security::slotCheckValidity()
|
||
|
{
|
||
|
if (!m_keysRead || m_gpgRunning)
|
||
|
{
|
||
15 years ago
|
TQTimer::singleShot(5, this, TQT_SLOT(slotCheckValidity()));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
if (m_keys.count() == 0)
|
||
|
{
|
||
|
emit validityResult(-1);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
m_result = 0;
|
||
|
m_runMode = Verify;
|
||
15 years ago
|
TQFileInfo f(m_fileName);
|
||
15 years ago
|
//check the MD5 sum
|
||
15 years ago
|
TQString md5sum;
|
||
15 years ago
|
const char* c = "";
|
||
|
KMD5 context(c);
|
||
15 years ago
|
TQFile file(m_fileName);
|
||
15 years ago
|
if (file.open(IO_ReadOnly))
|
||
|
{
|
||
|
context.reset();
|
||
14 years ago
|
context.update(TQT_TQIODEVICE_OBJECT(file));
|
||
15 years ago
|
md5sum = context.hexDigest();
|
||
|
file.close();
|
||
|
}
|
||
|
file.setName(f.dirPath() + "/md5sum");
|
||
|
if (file.open(IO_ReadOnly))
|
||
|
{
|
||
15 years ago
|
TQString md5sum_file;
|
||
15 years ago
|
file.readLine(md5sum_file, 50);
|
||
|
if (!md5sum.isEmpty() && !md5sum_file.isEmpty() && md5sum_file.startsWith(md5sum))
|
||
|
m_result |= MD5_OK;
|
||
|
file.close();
|
||
|
}
|
||
|
m_result |= SIGNED_BAD;
|
||
|
m_signatureKey.id = "";
|
||
|
m_signatureKey.name = "";
|
||
|
m_signatureKey.mail = "";
|
||
|
m_signatureKey.trusted = false;
|
||
|
|
||
|
//verify the signature
|
||
|
KProcIO *verifyProcess=new KProcIO();
|
||
|
*verifyProcess<<"gpg"<<"--no-secmem-warning"<<"--status-fd=2"<<"--command-fd=0"<<"--verify" << f.dirPath() + "/signature"<< m_fileName;
|
||
12 years ago
|
connect(verifyProcess, TQT_SIGNAL(processExited(TDEProcess *)),this, TQT_SLOT(slotProcessExited(TDEProcess *)));
|
||
15 years ago
|
connect(verifyProcess, TQT_SIGNAL(readReady(KProcIO *)),this, TQT_SLOT(slotDataArrived(KProcIO *)));
|
||
12 years ago
|
if (verifyProcess->start(TDEProcess::NotifyOnExit,true))
|
||
15 years ago
|
m_gpgRunning = true;
|
||
|
else
|
||
|
{
|
||
|
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>"));
|
||
|
emit validityResult(0);
|
||
|
delete verifyProcess;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void Security::signFile(const TQString &fileName)
|
||
15 years ago
|
{
|
||
|
m_fileName = fileName;
|
||
|
slotSignFile();
|
||
|
}
|
||
|
|
||
|
void Security::slotSignFile()
|
||
|
{
|
||
|
if (!m_keysRead || m_gpgRunning)
|
||
|
{
|
||
15 years ago
|
TQTimer::singleShot(5, this, TQT_SLOT(slotSignFile()));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList secretKeys;
|
||
|
for (TQMap<TQString, KeyStruct>::Iterator it = m_keys.begin(); it != m_keys.end(); ++it)
|
||
15 years ago
|
{
|
||
|
if (it.data().secret)
|
||
|
secretKeys.append(it.key());
|
||
|
}
|
||
|
|
||
|
if (secretKeys.count() == 0)
|
||
|
{
|
||
|
emit fileSigned(-1);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
m_result = 0;
|
||
15 years ago
|
TQFileInfo f(m_fileName);
|
||
15 years ago
|
|
||
|
//create the MD5 sum
|
||
15 years ago
|
TQString md5sum;
|
||
15 years ago
|
const char* c = "";
|
||
|
KMD5 context(c);
|
||
15 years ago
|
TQFile file(m_fileName);
|
||
15 years ago
|
if (file.open(IO_ReadOnly))
|
||
|
{
|
||
|
context.reset();
|
||
14 years ago
|
context.update(TQT_TQIODEVICE_OBJECT(file));
|
||
15 years ago
|
md5sum = context.hexDigest();
|
||
|
file.close();
|
||
|
}
|
||
|
file.setName(f.dirPath() + "/md5sum");
|
||
|
if (file.open(IO_WriteOnly))
|
||
|
{
|
||
15 years ago
|
TQTextStream stream(&file);
|
||
15 years ago
|
stream << md5sum;
|
||
|
m_result |= MD5_OK;
|
||
|
file.close();
|
||
|
}
|
||
|
|
||
|
if (secretKeys.count() > 1)
|
||
|
{
|
||
|
bool ok;
|
||
|
secretKeys = KInputDialog::getItemList(i18n("Select Signing Key"), i18n("Key used for signing:"), secretKeys, secretKeys[0], false, &ok);
|
||
|
if (ok)
|
||
|
m_secretKey = secretKeys[0];
|
||
|
else
|
||
|
{
|
||
|
emit fileSigned(0);
|
||
|
return;
|
||
|
}
|
||
|
} else
|
||
|
m_secretKey = secretKeys[0];
|
||
|
|
||
|
//verify the signature
|
||
|
KProcIO *signProcess=new KProcIO();
|
||
|
*signProcess<<"gpg"<<"--no-secmem-warning"<<"--status-fd=2"<<"--command-fd=0"<<"--no-tty"<<"--detach-sign" << "-u" << m_secretKey << "-o" << f.dirPath() + "/signature" << m_fileName;
|
||
12 years ago
|
connect(signProcess, TQT_SIGNAL(processExited(TDEProcess *)),this, TQT_SLOT(slotProcessExited(TDEProcess *)));
|
||
15 years ago
|
connect(signProcess, TQT_SIGNAL(readReady(KProcIO *)),this, TQT_SLOT(slotDataArrived(KProcIO *)));
|
||
15 years ago
|
m_runMode = Sign;
|
||
12 years ago
|
if (signProcess->start(TDEProcess::NotifyOnExit,true))
|
||
15 years ago
|
m_gpgRunning = true;
|
||
|
else
|
||
|
{
|
||
|
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>"));
|
||
|
emit fileSigned(0);
|
||
|
delete signProcess;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#include "security.moc"
|