2011-04-29 15:18:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 by Diego '[Po]lentino' Casella <polentino911@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "signing.h"
|
|
|
|
#include "signing_p.h"
|
|
|
|
|
|
|
|
#include <gpgme.h>
|
|
|
|
#include <gpgme++/gpgmefw.h>
|
|
|
|
#include <gpgme++/global.h>
|
|
|
|
#include <gpgme++/context.h>
|
|
|
|
#include <gpgme++/error.h>
|
|
|
|
#include <gpgme++/engineinfo.h>
|
|
|
|
#include <gpgme++/key.h>
|
|
|
|
#include <gpgme++/keylistresult.h>
|
|
|
|
#include <gpgme++/keygenerationresult.h>
|
|
|
|
#include <gpgme++/importresult.h>
|
|
|
|
#include <gpgme++/data.h>
|
|
|
|
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
#include <QtCore/QStringList>
|
|
|
|
|
|
|
|
#include <kdirwatch.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <kprocess.h>
|
|
|
|
#include <kuser.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdio> // FILE
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2011-05-24 11:16:38 +02:00
|
|
|
SigningPrivate::SigningPrivate(Signing *auth, const QString &path)
|
|
|
|
: q(auth),
|
|
|
|
m_keystorePath(path)
|
2011-04-29 15:18:35 +02:00
|
|
|
{
|
|
|
|
GpgME::initializeLibrary();
|
|
|
|
GpgME::Error error = GpgME::checkEngine(GpgME::OpenPGP);
|
|
|
|
if (error) {
|
|
|
|
kDebug() << "OpenPGP engine not found: authentication will not work.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_gpgContext = GpgME::Context::createForProtocol(GpgME::OpenPGP);
|
|
|
|
m_gpgContext->setKeyListMode(GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS);
|
|
|
|
|
2011-05-24 11:16:38 +02:00
|
|
|
m_keystorePath = path;
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 10:40:36 +02:00
|
|
|
if (m_keystorePath.isEmpty()) {
|
2011-04-29 15:18:35 +02:00
|
|
|
// From the gpgme doc: if the homeDirectory() is null, it means we are using the standard dir,
|
|
|
|
// that is "/home/$USER/.gnupg/ ; so let's retrieve it.
|
|
|
|
KUser user;
|
|
|
|
m_keystorePath.append(user.homeDir()).append("/.gnupg/");
|
|
|
|
} else {
|
|
|
|
error = m_gpgContext->setEngineHomeDirectory(m_keystorePath.toAscii().data());
|
|
|
|
if (error) {
|
|
|
|
kDebug() << "Failed setting custom gpg keystore directory: using default.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_keystoreDir = new KDirWatch();
|
|
|
|
m_keystoreDir->addDir(m_keystorePath);
|
2011-05-24 11:52:11 +02:00
|
|
|
m_keystoreDir->addDir(ultimateKeyStoragePath());
|
2011-04-29 15:18:35 +02:00
|
|
|
m_keystoreDir->startScan(true);
|
|
|
|
|
|
|
|
// Start watching the keystore and the dir with the kde keys, and notify for changed
|
2011-05-24 12:19:28 +02:00
|
|
|
q->connect(m_keystoreDir, SIGNAL(created(const QString &)), q, SLOT(processKeystore(const QString &)));
|
2011-05-24 11:52:11 +02:00
|
|
|
q->connect(m_keystoreDir, SIGNAL(dirty(const QString &)), q, SLOT(keyAdded(const QString &)));
|
|
|
|
q->connect(m_keystoreDir, SIGNAL(deleted(const QString &)), q, SLOT(keyRemoved(const QString &)));
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 11:16:38 +02:00
|
|
|
SigningPrivate::~SigningPrivate()
|
|
|
|
{
|
|
|
|
delete m_keystoreDir;
|
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 11:37:24 +02:00
|
|
|
QString SigningPrivate::ultimateKeyStoragePath() const
|
|
|
|
{
|
|
|
|
return KStandardDirs::installPath("data") + "plasmakeys/";
|
|
|
|
}
|
|
|
|
|
2011-05-24 11:18:03 +02:00
|
|
|
void SigningPrivate::registerUltimateTrustKeys()
|
2011-04-29 15:18:35 +02:00
|
|
|
{
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> tmp;
|
2011-05-24 11:52:11 +02:00
|
|
|
keys[UltimatelyTrusted] = tmp;
|
|
|
|
|
2011-04-29 15:18:35 +02:00
|
|
|
if (!m_gpgContext) {
|
|
|
|
kDebug() << "GPGME context not valid: please re-initialize the library.";
|
|
|
|
return;
|
|
|
|
}
|
2011-05-24 11:52:11 +02:00
|
|
|
|
2011-05-24 11:37:24 +02:00
|
|
|
QString path = ultimateKeyStoragePath();
|
2011-04-29 15:18:35 +02:00
|
|
|
QDir dir(path);
|
2011-05-24 10:40:36 +02:00
|
|
|
if (!dir.exists() || path.isEmpty()) {
|
2011-04-29 15:18:35 +02:00
|
|
|
kDebug() << "Directory with KDE keys not found: aborting";
|
|
|
|
return;
|
|
|
|
}
|
2011-05-24 11:52:11 +02:00
|
|
|
|
|
|
|
const QStringList keyFiles = dir.entryList(QDir::Files); // QDir::Files is rendundant in this stage
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
// Foreach file found, open it and import the key into the gpg keyring.
|
|
|
|
// First avoid firing multiple entryWritten() signals
|
|
|
|
m_keystoreDir->stopScan();
|
|
|
|
|
2011-05-24 11:16:38 +02:00
|
|
|
foreach (QString keyFile, keyFiles) {
|
2011-04-29 15:18:35 +02:00
|
|
|
FILE *fp;
|
|
|
|
fp = fopen(keyFile.toAscii().data(), "r");
|
|
|
|
GpgME::Data data(fp);
|
|
|
|
GpgME::ImportResult iRes = m_gpgContext->importKeys(data);
|
|
|
|
if (iRes.error()) {
|
|
|
|
kDebug() << "Error while importing the key located at: " << keyFile;
|
|
|
|
kDebug() << " The error is:" << iRes.error().asString() << "; Skipping.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first fingerprint listed is the one we need to save
|
2011-05-24 12:10:03 +02:00
|
|
|
tmp.insert(iRes.import(0).fingerprint());
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
keys[UltimatelyTrusted] = tmp;
|
|
|
|
|
|
|
|
// Restore scanning folders
|
|
|
|
m_keystoreDir->startScan(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SigningPrivate::splitKeysByTrustLevel()
|
|
|
|
{
|
|
|
|
if (!m_gpgContext) {
|
|
|
|
kDebug() << "GPGME context not valid: please re-initialize the library.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> temp = keys[UltimatelyTrusted];
|
2011-04-29 15:18:35 +02:00
|
|
|
keys.clear();
|
|
|
|
keys[UltimatelyTrusted] = temp;
|
|
|
|
|
|
|
|
// Splitting the keys by their trust level is a boring task, since we have to distinguish
|
|
|
|
// `which key has been signed with an other given key` :P
|
|
|
|
//
|
2011-05-24 11:18:03 +02:00
|
|
|
// Loop 1: import and load the KDE keys, already done in registerUltimateTrustKeys()
|
2011-04-29 15:18:35 +02:00
|
|
|
//
|
|
|
|
// Loop 2: load the user keyring (private keys only), and loop for:
|
|
|
|
// - a: a key not yet expired;
|
|
|
|
// - b: a key not already present in the keys[UltimatelyTrusted];
|
|
|
|
//
|
|
|
|
// Loop 3: load the user keyring, and loop for:
|
|
|
|
// - a: a key not yet expired;
|
|
|
|
// - b: a key not already present in the keys[UltimatelyTrusted];
|
|
|
|
// - c: a key not yet in keys[SelfTrusted]
|
|
|
|
//
|
|
|
|
// After Loop 3, the tmp object contains the remaining keys not yet processed.
|
|
|
|
//
|
|
|
|
// Loop 4: foreach key not yet classified, inspect their signatures and:
|
2011-05-24 14:10:59 +02:00
|
|
|
// - a: if contains a key from keys[UltimatelyTrusted], save it in keys[FullyTrusted];
|
2011-04-29 15:18:35 +02:00
|
|
|
// - b: if contains a key from keys[SelfTrusted], save it in keys[UserTrusted];
|
|
|
|
// - c: if the signature is unknown, let's save it in keys[UnknownTrusted].
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> tmp;
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
GpgME::Error error = m_gpgContext->startKeyListing("", true);
|
|
|
|
while (!error) { // Loop 2
|
|
|
|
GpgME::Key key = m_gpgContext->nextKey(error);
|
2011-05-24 11:56:10 +02:00
|
|
|
if (error) {
|
2011-04-29 15:18:35 +02:00
|
|
|
break;
|
2011-05-24 11:56:10 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
QByteArray data(key.subkey(0).fingerprint());
|
|
|
|
|
|
|
|
// If the key is disabled, expired, invalid or revoked, put it in the untrusted list
|
|
|
|
if (key.isDisabled() || key.isExpired() || key.isInvalid() || key.isRevoked()) {
|
2011-05-24 12:10:03 +02:00
|
|
|
keys[CompletelyUntrusted].insert(data);
|
2011-05-24 12:43:47 +02:00
|
|
|
} else if (!keys[UltimatelyTrusted].contains(data)) {
|
|
|
|
// Ensure we are not processing twice the trusted KDE keys
|
|
|
|
// The keys is new, valid and private: save it !
|
|
|
|
keys[SelfTrusted].insert(data);
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GpgME::KeyListResult lRes = m_gpgContext->endKeyListing();
|
2011-05-24 14:10:59 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
Plasma::TrustLevel SigningPrivate::addKeyToCache(const QByteArray &fingerprint)
|
|
|
|
{
|
|
|
|
if (!m_gpgContext) {
|
|
|
|
kDebug() << "GPGME context not valid: please re-initialize the library.";
|
|
|
|
return UnknownTrusted;
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
GpgME::Error error;
|
|
|
|
GpgME::Key key = m_gpgContext->key(fingerprint.data(), error);
|
|
|
|
if (error) {
|
|
|
|
keys[UnknownTrusted].insert(fingerprint);
|
|
|
|
return UnknownTrusted;
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
if (keys[UltimatelyTrusted].contains(fingerprint)) {
|
|
|
|
return UltimatelyTrusted;
|
|
|
|
} else if (keys[SelfTrusted].contains(fingerprint)) {
|
|
|
|
return SelfTrusted;
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
2011-05-24 12:12:46 +02:00
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
// If the key is disabled, expired, invalid or revoked, put it in the untrusted list
|
|
|
|
if (key.isDisabled() || key.isExpired() || key.isInvalid() || key.isRevoked()) {
|
|
|
|
keys[CompletelyUntrusted].insert(fingerprint);
|
|
|
|
return CompletelyUntrusted;
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
for (unsigned int i = 0; i < key.numUserIDs(); ++i) {
|
|
|
|
foreach (const GpgME::UserID::Signature &signature, key.userID(i).signatures()) {
|
|
|
|
if (keys[UltimatelyTrusted].contains(signature.signerKeyID())) {
|
2011-04-29 15:18:35 +02:00
|
|
|
// if the unknown key has a signer that is a kde key, let's trust it
|
2011-05-24 14:10:59 +02:00
|
|
|
keys[FullyTrusted].insert(fingerprint);
|
|
|
|
return FullyTrusted;
|
|
|
|
} else if (keys[SelfTrusted].contains(signature.signerKeyID())) {
|
2011-04-29 15:18:35 +02:00
|
|
|
// if the unknown key has a signer that is a user key, let's trust it
|
2011-05-24 14:10:59 +02:00
|
|
|
keys[UserTrusted].insert(fingerprint);
|
|
|
|
return UserTrusted;
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
// We didn't stored the unknown key in the previous loop, which means that we
|
|
|
|
// don't know the hey al all.
|
|
|
|
keys[UnknownTrusted].insert(fingerprint);
|
|
|
|
return UnknownTrusted;
|
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
void SigningPrivate::dumpKeysToDebug()
|
|
|
|
{
|
|
|
|
kDebug() << "UltimatelyTrusted = " << keys[UltimatelyTrusted];
|
|
|
|
kDebug() << "FullyTrusted = " << keys[FullyTrusted];
|
|
|
|
kDebug() << "SelfTrusted = " << keys[SelfTrusted];
|
|
|
|
kDebug() << "UserTrusted = " << keys[UserTrusted];
|
|
|
|
kDebug() << "UnknownTrusted = " << keys[UnknownTrusted];
|
|
|
|
kDebug() << "CompletelyUntrusted = " << keys[CompletelyUntrusted];
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SigningPrivate::keysID(const bool returnPrivate) const
|
|
|
|
{
|
|
|
|
QStringList result;
|
|
|
|
|
|
|
|
if (!m_gpgContext) {
|
|
|
|
kDebug() << "GPGME context not valid: please re-initialize the library.";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GpgME::Error error = m_gpgContext->startKeyListing("", returnPrivate);
|
|
|
|
while (!error) {
|
|
|
|
GpgME::Key k = m_gpgContext->nextKey(error);
|
2011-05-24 11:56:10 +02:00
|
|
|
if (error) {
|
2011-04-29 15:18:35 +02:00
|
|
|
break;
|
2011-05-24 11:56:10 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
result.append(k.subkey(0).fingerprint());
|
|
|
|
}
|
|
|
|
GpgME::KeyListResult lRes = m_gpgContext->endKeyListing();
|
|
|
|
if (lRes.error()) {
|
|
|
|
kDebug() << "Error while ending the keyListing operation: " << lRes.error().asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SigningPrivate::signerOf(const QString &messagePath, const QString &signaturePath) const
|
|
|
|
{
|
|
|
|
FILE *pFile;
|
|
|
|
FILE *pSig;
|
|
|
|
|
|
|
|
pFile = fopen(messagePath.toAscii().data(), "r");
|
|
|
|
pSig = fopen(signaturePath.toAscii().data(), "r");
|
|
|
|
|
|
|
|
GpgME::Data file(pFile);
|
|
|
|
GpgME::Data sig(pSig);
|
|
|
|
|
|
|
|
GpgME::VerificationResult vRes = m_gpgContext->verifyDetachedSignature(sig, file);
|
|
|
|
if (!vRes.error()) {
|
|
|
|
kDebug() << "message " << messagePath << " and signature " << signaturePath << "matched! The fingerprint of the signer is: " << vRes.signature(0).fingerprint();
|
|
|
|
return QString(vRes.signature(0).fingerprint());
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2011-05-24 11:52:11 +02:00
|
|
|
void SigningPrivate::processKeystore(const QString &path)
|
2011-04-29 15:18:35 +02:00
|
|
|
{
|
2011-05-24 11:52:11 +02:00
|
|
|
if (path != m_keystorePath) {
|
|
|
|
registerUltimateTrustKeys();
|
|
|
|
return;
|
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> oldValues;
|
|
|
|
oldValues += keys[UnverifiableTrust];
|
|
|
|
oldValues += keys[CompletelyUntrusted];
|
|
|
|
oldValues += keys[UnknownTrusted];
|
|
|
|
oldValues += keys[SelfTrusted];
|
2011-05-24 14:10:59 +02:00
|
|
|
oldValues += keys[FullyTrusted];
|
2011-05-24 12:10:03 +02:00
|
|
|
oldValues += keys[UltimatelyTrusted];
|
2011-05-24 11:52:11 +02:00
|
|
|
|
|
|
|
splitKeysByTrustLevel();
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> newValues;
|
|
|
|
newValues += keys[UnverifiableTrust];
|
|
|
|
newValues += keys[CompletelyUntrusted];
|
|
|
|
newValues += keys[UnknownTrusted];
|
|
|
|
newValues += keys[SelfTrusted];
|
2011-05-24 14:10:59 +02:00
|
|
|
newValues += keys[FullyTrusted];
|
2011-05-24 12:10:03 +02:00
|
|
|
newValues += keys[UltimatelyTrusted];
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
QString result;
|
|
|
|
bool keystoreIncreased = (newValues.size() >= oldValues.size());
|
|
|
|
if (keystoreIncreased) {
|
2011-05-24 12:12:46 +02:00
|
|
|
foreach (QByteArray value, newValues) {
|
2011-04-29 15:18:35 +02:00
|
|
|
if (!oldValues.contains(value)) {
|
|
|
|
// Found the key added
|
|
|
|
result.append(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-24 12:12:46 +02:00
|
|
|
foreach (QByteArray value, oldValues) {
|
2011-04-29 15:18:35 +02:00
|
|
|
if (!newValues.contains(value)) {
|
|
|
|
// Found the removed key
|
|
|
|
result.append(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-24 12:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.isEmpty()) {
|
|
|
|
if (keystoreIncreased) {
|
|
|
|
emit q->keyAdded(result);
|
|
|
|
} else {
|
|
|
|
emit q->keyRemoved(result);
|
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-24 11:52:11 +02:00
|
|
|
void SigningPrivate::keyAdded(const QString &path)
|
2011-04-29 15:18:35 +02:00
|
|
|
{
|
2011-05-24 11:52:11 +02:00
|
|
|
if (path == m_keystorePath) {
|
|
|
|
// we don't worry about keys added to the key store path,
|
|
|
|
// just the ultimate store dir
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-29 15:18:35 +02:00
|
|
|
// Avoid firing multiple signals by kdirwatch instances
|
|
|
|
m_keystoreDir->stopScan();
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
fp = fopen(path.toAscii().data(), "r");
|
|
|
|
GpgME::Data data(fp);
|
|
|
|
GpgME::ImportResult iRes = m_gpgContext->importKeys(data);
|
|
|
|
|
|
|
|
bool alreadyInMap = false;
|
|
|
|
|
|
|
|
// Ensure we don't already have the key
|
2011-05-24 12:12:46 +02:00
|
|
|
foreach (QByteArray sec, keys[UltimatelyTrusted]) {
|
2011-04-29 15:18:35 +02:00
|
|
|
if (strcmp(sec.data(), iRes.import(0).fingerprint())) {
|
|
|
|
alreadyInMap = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alreadyInMap) {
|
|
|
|
keys[UltimatelyTrusted] << QByteArray(iRes.import(0).fingerprint());
|
|
|
|
splitKeysByTrustLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore scanning folders
|
|
|
|
m_keystoreDir->startScan(true, true);
|
|
|
|
|
|
|
|
QString result(iRes.import(0).fingerprint());
|
|
|
|
emit(q->keyAdded(result));
|
|
|
|
}
|
|
|
|
|
2011-05-24 11:52:11 +02:00
|
|
|
void SigningPrivate::keyRemoved(const QString &path)
|
2011-04-29 15:18:35 +02:00
|
|
|
{
|
|
|
|
// Avoid firing multiple signals by kdirwatch instances
|
|
|
|
m_keystoreDir->stopScan();
|
|
|
|
|
2011-05-24 11:52:11 +02:00
|
|
|
if (path == m_keystorePath) {
|
|
|
|
// Restore scanning folders
|
|
|
|
m_keystoreDir->startScan(true, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> oldKeys = keys[UltimatelyTrusted];
|
2011-05-24 11:18:03 +02:00
|
|
|
registerUltimateTrustKeys();
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> newkeys = keys[UltimatelyTrusted];
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
QString result;
|
2011-05-24 12:12:46 +02:00
|
|
|
foreach (QByteArray key, oldKeys) {
|
2011-04-29 15:18:35 +02:00
|
|
|
if (!newkeys.contains(key)) {
|
|
|
|
// We found the missing key :)
|
|
|
|
result.append(key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GpgME::Error error = m_gpgContext->startKeyListing("");
|
|
|
|
while (!error) {
|
|
|
|
GpgME::Key k = m_gpgContext->nextKey(error);
|
2011-05-24 11:56:10 +02:00
|
|
|
if (error) {
|
2011-04-29 15:18:35 +02:00
|
|
|
break;
|
2011-05-24 11:56:10 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
if (result.contains(k.subkey(0).fingerprint())) {
|
|
|
|
error = m_gpgContext->startKeyDeletion(k, true); // GG
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
kDebug() << "Can't delete key with fingerprint: " << result;
|
|
|
|
m_gpgContext->endKeyListing();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GpgME::KeyListResult lRes = m_gpgContext->endKeyListing();
|
|
|
|
if (lRes.error()) {
|
|
|
|
kDebug() << "Error while ending the keyListing operation: " << lRes.error().asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
splitKeysByTrustLevel();
|
|
|
|
|
|
|
|
// Restore scanning folders
|
|
|
|
m_keystoreDir->startScan(true, true);
|
|
|
|
|
|
|
|
emit(q->keyRemoved(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SigningPrivate::signersOf(const QString id) const
|
|
|
|
{
|
|
|
|
QStringList result;
|
2011-05-24 14:10:59 +02:00
|
|
|
GpgME::Error error;
|
|
|
|
GpgME::Key key = m_gpgContext->key(id.toAscii().data(), error);
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
for (unsigned int i = 0; i < key.numUserIDs(); ++i) {
|
|
|
|
foreach (const GpgME::UserID::Signature &signature, key.userID(i).signatures()) {
|
|
|
|
QString sig(signature.signerKeyID());
|
|
|
|
if (!result.contains(sig) && id != sig) {
|
2011-05-24 12:12:46 +02:00
|
|
|
result.append(sig);
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-24 11:38:25 +02:00
|
|
|
//kDebug() << "Hey, the key " << id << " has been signed with " << result;
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Signing::Signing(const QString &keystorePath)
|
|
|
|
: QObject(),
|
|
|
|
d(new SigningPrivate(this, keystorePath))
|
|
|
|
{
|
2011-05-24 11:18:03 +02:00
|
|
|
d->registerUltimateTrustKeys();
|
2011-04-29 15:18:35 +02:00
|
|
|
d->splitKeysByTrustLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
Signing::~Signing()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Signing::keysByTrustLevel(TrustLevel trustLevel) const
|
|
|
|
{
|
|
|
|
if (trustLevel == UnverifiableTrust) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> s = d->keys[trustLevel];
|
2011-04-29 15:18:35 +02:00
|
|
|
QStringList tmp;
|
2011-05-24 12:12:46 +02:00
|
|
|
foreach (const QByteArray &sa, s) {
|
2011-04-29 15:18:35 +02:00
|
|
|
tmp.append(sa);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
TrustLevel Signing::trustLevelOf(const QString &keyID) const
|
|
|
|
{
|
2011-05-24 10:40:36 +02:00
|
|
|
if (keyID.isEmpty()) {
|
2011-04-29 15:18:35 +02:00
|
|
|
return Plasma::UnverifiableTrust;
|
2011-05-24 10:40:36 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
|
|
|
for (int i = (int)Plasma::UnverifiableTrust; i <= (int)Plasma::UltimatelyTrusted; ++i) {
|
2011-05-24 12:10:03 +02:00
|
|
|
QSet<QByteArray> tmp = d->keys[(Plasma::TrustLevel)i];
|
2011-05-24 12:12:46 +02:00
|
|
|
foreach (QByteArray key, tmp) {
|
2011-05-24 12:43:47 +02:00
|
|
|
if (key.contains(keyID.toAscii().data())) {
|
2011-04-29 15:18:35 +02:00
|
|
|
return (Plasma::TrustLevel)i;
|
2011-05-24 12:43:47 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
return d->addKeyToCache(keyID.toAscii());
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Signing::signerOf(const KUrl &plasmoidPath, const KUrl &plasmoidSignaturePath) const
|
|
|
|
{
|
|
|
|
kDebug() << "Checking existence of " << plasmoidPath.pathOrUrl();
|
|
|
|
kDebug() << "Checking existence of " << plasmoidSignaturePath.pathOrUrl();
|
|
|
|
// Original file and signature to verify against
|
|
|
|
QFile fileHanlder(plasmoidPath.path());
|
|
|
|
QFile signatureHandler(plasmoidSignaturePath.isEmpty() ?
|
|
|
|
plasmoidPath.path().append(".asc") :
|
|
|
|
plasmoidSignaturePath.path());
|
|
|
|
|
|
|
|
|
|
|
|
fileHanlder.open(QIODevice::ReadOnly);
|
|
|
|
signatureHandler.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
if (!fileHanlder.exists()) {
|
|
|
|
kDebug() << "Plasmoid package doesn't exists: signature verification aborted.";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
if (!signatureHandler.exists()) {
|
|
|
|
kDebug() << "Plasmoid signature doesn't exists: signature verification aborted.";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << "Cheking if " << plasmoidPath.pathOrUrl() << " and " << plasmoidSignaturePath.pathOrUrl() << " matches";
|
|
|
|
|
|
|
|
return d->signerOf(plasmoidPath.pathOrUrl(), plasmoidSignaturePath.pathOrUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Signing::keyStorePath() const
|
|
|
|
{
|
2011-05-24 11:16:38 +02:00
|
|
|
return d->m_keystorePath;
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Signing::descriptiveString(const QString &keyID) const
|
|
|
|
{
|
2011-05-24 10:40:36 +02:00
|
|
|
if (keyID.isEmpty()) {
|
2011-04-29 15:18:35 +02:00
|
|
|
return QString();
|
2011-05-24 10:40:36 +02:00
|
|
|
}
|
2011-04-29 15:18:35 +02:00
|
|
|
|
2011-05-24 14:10:59 +02:00
|
|
|
if (!d->m_gpgContext) {
|
|
|
|
kDebug() << "GPGME context not valid: please re-initialize the library.";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
GpgME::Error error;
|
|
|
|
GpgME::Key key = d->m_gpgContext->key(keyID.toAscii().data(), error);
|
|
|
|
if (error) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return key.userID(0).id();
|
2011-04-29 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_signing.cpp"
|