1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-09-15 23:06:31 +00:00
This commit is contained in:
2019-03-23 20:53:02 +01:00
parent c26443b6f8
commit c044cefc26
25 changed files with 420 additions and 331 deletions

View File

@@ -5,21 +5,48 @@ set(
SRC
plugin.cpp
pass.cpp
gpg.cpp
)
set(CMAKE_AUTOMOC ON)
add_library(${PLUGIN} MODULE ${SRC})
set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
execute_process(
COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
OUTPUT_VARIABLE ARCH_TRIPLET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(ARCH_TRIPLET STREQUAL "")
set(ARCH_TRIPLET x86_64-linux-gnu)
endif()
add_library(${PLUGIN} MODULE ${SRC})
set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
set(EXTERNAL_LIBS "${CMAKE_SOURCE_DIR}/third/local/${ARCH_TRIPLET}")
set(THIRD_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
INCLUDE_DIRECTORIES(${EXTERNAL_LIBS}/include)
add_library(GpgError STATIC IMPORTED)
set_property(TARGET GpgError PROPERTY IMPORTED_LOCATION "${EXTERNAL_LIBS}/lib/libgpg-error.a")
add_library(GpgAssuan STATIC IMPORTED)
set_property(TARGET GpgAssuan PROPERTY IMPORTED_LOCATION "${EXTERNAL_LIBS}/lib/libassuan.a")
add_library(Gpgme STATIC IMPORTED)
set_property(TARGET Gpgme PROPERTY IMPORTED_LOCATION "${EXTERNAL_LIBS}/lib/libgpgme.a")
add_library(Gpgmepp STATIC IMPORTED)
set_property(TARGET Gpgmepp PROPERTY IMPORTED_LOCATION "${EXTERNAL_LIBS}/lib/libgpgmepp.a")
add_library(QGpgme STATIC IMPORTED)
set_property(TARGET QGpgme PROPERTY IMPORTED_LOCATION "${EXTERNAL_LIBS}/lib/libqgpgme.a")
target_link_libraries(${PLUGIN} QGpgme Gpgmepp Gpgme GpgAssuan GpgError)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)

172
plugins/Pass/gpg.cpp Normal file
View File

@@ -0,0 +1,172 @@
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QtCore/QStandardPaths>
#include <memory>
#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"
#include "qgpgme/encryptjob.h"
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");
}
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();
}
bool Gpg::setGpghome(QString path)
{
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");
}
QString Gpg::decrypt(const QByteArray cipherText)
{
/*
auto decJob = openpgp()->decryptJob();
auto ctx = DecryptJob::context(decJob);
TestPassphraseProvider provider;
ctx->setPassphraseProvider(&provider);
ctx->setPinentryMode(Context::PinentryLoopback);
QByteArray plainText;
decJob->exec(cipherText, plainText);
return QString::fromUtf8(plainText);*/
}
QString Gpg::decryptFromFile(QString path)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Can't open the File";
return nullptr;
}
QByteArray plainText = file.readAll();
return decrypt(plainText);
}
QByteArray Gpg::encrypt(QString str, QString uid, bool ascii_armor, bool text_mode)
{
qDebug() << "Encrypt to QByteArray";
auto keys = getKeys(uid);
auto job = openpgp()
->encryptJob(
ascii_armor,
text_mode
);
QByteArray cipherText;
//auto result = job->exec(keys, str.toUtf8(), Context::AlwaysTrust, cipherText);
return cipherText;
}
bool Gpg::encryptToFile(QString str, QString path, const QString uid, bool ascii_armor,
bool text_mode)
{
qDebug() << "Encrypt to file " << path;
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Can't open the file to write it" ;
return false;
}
file.write(encrypt(str, uid, ascii_armor, text_mode));
return true;
}
std::vector<Key> Gpg::getKeys(QString pattern_uid, bool remote, bool include_sigs, bool validate)
{
qDebug() << "Getting the key " << pattern_uid;
auto *job = openpgp()->keyListJob(remote, include_sigs, validate);
std::vector<Key> keys;
auto result = job->exec(QStringList() << pattern_uid, false, keys);
delete job;
return keys;
}
QStringList Gpg::getAllKeysId(bool remote, bool include_sigs, bool validate)
{
qDebug() << "Show all available key";
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;
}
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;
}
auto job = openpgp()->importJob();
auto result = job->exec(file.readAll());
delete job;
file.close();
if (result.error()) {
qWarning() << "Import go wrong";
return false;
}
return result.imports().size() == 1;
}

38
plugins/Pass/gpg.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef GPG_H
#define GPG_H
#include <memory>
#include "gpgme++/key.h"
class Gpg
{
private:
Gpg();
QDir m_gpghome;
public:
~Gpg();
static std::shared_ptr<Gpg> instance()
{
static std::shared_ptr<Gpg> s{new Gpg};
return s;
}
Gpg(Gpg const &) = delete;
void operator=(Gpg const &) = delete;
QString decrypt(QByteArray plainText);
QString decryptFromFile(QString path);
QByteArray encrypt(QString str, QString uid, bool ascii_armor = true, bool text_mode = true);
bool encryptToFile(QString str, QString path, const QString uid, bool ascii_armor = true,
bool text_mode = true);
bool importKeyFromFile(QString path);
std::vector<GpgME::Key> getKeys(QString pattern_uid, bool remote = false, bool include_sigs = false,
bool validate = false);
QStringList getAllKeysId(bool remote = false, bool include_sigs = false, bool validate = false);
bool setGpghome(QString path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/gpghome"));
};
#endif

View File

@@ -4,17 +4,30 @@
#include <QtCore/QDir>
#include "pass.h"
#include "gpg.h"
Pass::Pass(){
pass_store = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.password-store");
QDir dir(pass_store);
Pass::Pass()
{
m_password_store = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.password-store");
QDir dir(m_password_store);
if (!dir.exists())
dir.mkpath(".");
qDebug() << "Password Store is :" << pass_store;
qDebug() << "Password Store is :" << m_password_store;
}
void Pass::speak()
bool Pass::gpgImportKeyFromFile(QString path)
{
qDebug() << "Starting app from main.cpp";
return Gpg::instance()->importKeyFromFile(path);
}
QStringList Pass::gpgListAllKeys()
{
return Gpg::instance()->getAllKeysId();
}
bool Pass::gpgSetGpghome(QString path)
{
return Gpg::instance()->setGpghome(path);
}

View File

@@ -2,17 +2,19 @@
#define PASS_H
#include <QObject>
#include <memory>
class Pass : public QObject
{
Q_OBJECT
QString pass_store;
QString m_password_store;
public:
Pass();
~Pass() override = default;
Q_INVOKABLE void speak();
Q_INVOKABLE bool gpgImportKeyFromFile(QString path);
Q_INVOKABLE QStringList gpgListAllKeys();
Q_INVOKABLE bool gpgSetGpghome(QString path);
};
#endif