UTPassMirror/plugins/Pass/gpg.cpp

173 lines
4.4 KiB
C++
Raw Permalink Normal View History

2019-03-18 07:10:58 +00:00
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QtCore/QStandardPaths>
2019-03-23 19:53:02 +00:00
#include <memory>
2019-03-18 07:10:58 +00:00
#include "gpg.h"
#include "gpgme++/global.h"
#include "gpgme++/context.h"
#include "gpgme++/engineinfo.h"
#include "qgpgme/protocol.h"
#include "qgpgme/keylistjob.h"
#include "gpgme++/keylistresult.h"
#include "qgpgme/importjob.h"
#include "gpgme++/importresult.h"
#include "qgpgme/decryptjob.h"
2019-03-23 19:53:02 +00:00
#include "qgpgme/encryptjob.h"
2019-03-18 07:10:58 +00:00
using namespace GpgME;
using namespace QGpgME;
Gpg::Gpg()
{
auto error = initializeLibrary(OpenPGP);
if (error) {
qDebug() << "Code Error : " << error.code();
qDebug() << "Error str : " << error.asString();
qFatal("GpgME init fail");
}
2019-03-23 19:53:02 +00:00
setGpghome();
error = checkEngine(OpenPGP);
if (error) {
qDebug() << "Code Error : " << error.code();
qDebug() << "Error str : " << error.asString();
qFatal("Engine check Fail");
}
qDebug() << "GpgME Engine Version :" << engineInfo(OpenPGP).version();
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
bool Gpg::setGpghome(QString path)
{
2019-03-23 19:53:02 +00:00
QFileInfo file(path);
if (file.isFile()) {
qWarning() << "Not a directory GNUPGHOME not change";
return false;
}
if (file.isDir() and !file.isWritable()) {
qWarning() << "Not a writable directory GNUPGHOME not change";
return false;
}
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(".");
}
m_gpghome = dir;
qputenv("GNUPGHOME", m_gpghome.absolutePath().toStdString().c_str());
qDebug() << "GNUPGHOME is :" << qgetenv("GNUPGHOME");
2019-03-18 07:10:58 +00:00
}
QString Gpg::decrypt(const QByteArray cipherText)
{
2019-03-23 19:53:02 +00:00
/*
auto decJob = openpgp()->decryptJob();
auto ctx = DecryptJob::context(decJob);
2019-03-18 07:10:58 +00:00
2019-03-23 19:53:02 +00:00
TestPassphraseProvider provider;
ctx->setPassphraseProvider(&provider);
2019-03-23 19:53:02 +00:00
ctx->setPinentryMode(Context::PinentryLoopback);
2019-03-18 07:10:58 +00:00
2019-03-23 19:53:02 +00:00
QByteArray plainText;
decJob->exec(cipherText, plainText);
2019-03-18 07:10:58 +00:00
return QString::fromUtf8(plainText);*/
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
QString Gpg::decryptFromFile(QString path)
{
2019-03-23 19:53:02 +00:00
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
2019-03-23 19:53:02 +00:00
qWarning() << "Can't open the File";
return nullptr;
}
QByteArray plainText = file.readAll();
2019-03-23 19:53:02 +00:00
return decrypt(plainText);
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
QByteArray Gpg::encrypt(QString str, QString uid, bool ascii_armor, bool text_mode)
{
2019-03-23 19:53:02 +00:00
qDebug() << "Encrypt to QByteArray";
auto keys = getKeys(uid);
auto job = openpgp()
2019-03-23 19:53:02 +00:00
->encryptJob(
ascii_armor,
text_mode
);
QByteArray cipherText;
2019-03-23 19:53:02 +00:00
//auto result = job->exec(keys, str.toUtf8(), Context::AlwaysTrust, cipherText);
return cipherText;
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
bool Gpg::encryptToFile(QString str, QString path, const QString uid, bool ascii_armor,
bool text_mode)
{
2019-03-23 19:53:02 +00:00
qDebug() << "Encrypt to file " << path;
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
2019-03-23 19:53:02 +00:00
qWarning() << "Can't open the file to write it" ;
return false;
}
2019-03-23 19:53:02 +00:00
file.write(encrypt(str, uid, ascii_armor, text_mode));
return true;
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
std::vector<Key> Gpg::getKeys(QString pattern_uid, bool remote, bool include_sigs, bool validate)
{
2019-03-23 19:53:02 +00:00
qDebug() << "Getting the key " << pattern_uid;
auto *job = openpgp()->keyListJob(remote, include_sigs, validate);
std::vector<Key> keys;
2019-03-23 19:53:02 +00:00
auto result = job->exec(QStringList() << pattern_uid, false, keys);
delete job;
2019-03-23 19:53:02 +00:00
return keys;
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
QStringList Gpg::getAllKeysId(bool remote, bool include_sigs, bool validate)
{
qDebug() << "Show all available key";
2019-03-23 19:53:02 +00:00
auto job = openpgp()->keyListJob(remote, remote, validate);
std::vector<Key> keys;
auto result = job->exec(QStringList(""), false, keys);
delete job;
if (keys.empty()) {
qDebug() << "No key found";
return QStringList();
}
auto r = QStringList();
for (const auto &key : keys) {
r.append(QLatin1String(key.keyID()));
qDebug() << "Key" << QLatin1String(key.keyID());
}
return r;
2019-03-18 07:10:58 +00:00
}
2019-03-23 19:53:02 +00:00
bool Gpg::importKeyFromFile(QString path)
{
qDebug() << "Importing the key file" << path;
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Can't open the File";
return false;
}
2019-03-20 23:04:05 +00:00
auto job = openpgp()->importJob();
auto result = job->exec(file.readAll());
2019-03-20 23:04:05 +00:00
delete job;
file.close();
2019-03-20 23:04:05 +00:00
if (result.error()) {
qWarning() << "Import go wrong";
return false;
}
2019-03-23 19:53:02 +00:00
return result.imports().size() == 1;
}