1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-07-03 10:42:28 +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

@ -1,3 +1,2 @@
add_subdirectory(Gpg)
add_subdirectory(Pass)

View File

@ -1,47 +0,0 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PLUGIN "Gpg")
set(
SRC
plugin.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)
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)
execute_process(
COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
OUTPUT_VARIABLE ARCH_TRIPLET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
install(FILES qmldir DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)

View File

@ -1,24 +0,0 @@
#ifndef PASS_H
#define PASS_H
#include <QObject>
class Gpg : public QObject
{
Q_OBJECT
public:
Gpg();
~Gpg() override = default;
Q_INVOKABLE void listDir();
Q_INVOKABLE QString getKeyId(QString uid);
Q_INVOKABLE QStringList getAllKeysId();
Q_INVOKABLE bool importKey(QString path);
Q_INVOKABLE QString decrypt(QByteArray plainText);
Q_INVOKABLE QString decryptFile(QString path);
Q_INVOKABLE QByteArray encrypt(QString str);
Q_INVOKABLE bool encryptFile(QString str, QString path);
};
#endif

View File

@ -1,10 +0,0 @@
#include <QtQml>
#include "plugin.h"
#include "gpg.h"
void GpgPlugin::registerTypes(const char *uri)
{
//@uri Pass
qmlRegisterSingletonType<Gpg>(uri, 1, 0, "Gpg", [](QQmlEngine *, QJSEngine *) -> QObject * { return new Gpg; });
}

View File

@ -1,16 +0,0 @@
#ifndef GPGPLUGIN_H
#define GPGPLUGIN_H
#include <QQmlExtensionPlugin>
class GpgPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID
"org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri);
};
#endif

View File

@ -1,2 +0,0 @@
module Gpg
plugin Gpg

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}/)

View File

@ -2,6 +2,7 @@
#include <QFile>
#include <QDir>
#include <QtCore/QStandardPaths>
#include <memory>
#include "gpg.h"
#include "gpgme++/global.h"
@ -13,6 +14,7 @@
#include "qgpgme/importjob.h"
#include "gpgme++/importresult.h"
#include "qgpgme/decryptjob.h"
#include "qgpgme/encryptjob.h"
using namespace GpgME;
using namespace QGpgME;
@ -26,13 +28,7 @@ Gpg::Gpg()
qFatal("GpgME init fail");
}
QString gnuhome = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/gpghome");
QDir dir(gnuhome);
if (!dir.exists())
dir.mkpath(".");
qputenv("GNUPGHOME", gnuhome.toStdString().c_str());
qDebug() << "GNUPGHOME is :" << qgetenv("GNUPGHOME");
setGpghome();
error = checkEngine(OpenPGP);
if (error) {
@ -43,86 +39,99 @@ Gpg::Gpg()
qDebug() << "GpgME Engine Version :" << engineInfo(OpenPGP).version();
}
void Gpg::listDir()
bool Gpg::setGpghome(QString path)
{
qDebug() << "hello world!";
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);*/
/*
auto decJob = openpgp()->decryptJob();
auto ctx = DecryptJob::context(decJob);
/* TODO
* TestPassphraseProvider provider;
TestPassphraseProvider provider;
ctx->setPassphraseProvider(&provider);
ctx->setPinentryMode(Context::PinentryLoopback);*/
ctx->setPinentryMode(Context::PinentryLoopback);
/*QByteArray plainText;
QByteArray plainText;
decJob->exec(cipherText, plainText);
return QString::fromUtf8(plainText);*/
}
QString Gpg::decryptFile(QString path)
QString Gpg::decryptFromFile(QString path)
{
/*QFile file(path);
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qErrnoWarning("Can't open the File");
qWarning() << "Can't open the File";
return nullptr;
}
QByteArray plainText = file.readAll();
return this->decrypt(plainText);*/
return decrypt(plainText);
}
QByteArray Gpg::encrypt(const QString str)
QByteArray Gpg::encrypt(QString str, QString uid, bool ascii_armor, bool text_mode)
{
/*auto listjob = openpgp()->keyListJob(false, false, false);
std::vector<Key> keys;
auto keylistresult = listjob->exec(QStringList() << QStringLiteral("alfa@example.net"), false, keys);
qDebug() << "Encrypt to QByteArray";
auto keys = getKeys(uid);
auto job = openpgp()
->encryptJob(
true, //ASCII Armor
true //Textmode
);
->encryptJob(
ascii_armor,
text_mode
);
QByteArray cipherText;
auto result = job->exec(keys, QStringLiteral("Hello World").toUtf8(), Context::AlwaysTrust, cipherText);
return cipherText;*/
//auto result = job->exec(keys, str.toUtf8(), Context::AlwaysTrust, cipherText);
return cipherText;
}
bool Gpg::encryptFile(QString str, QString path)
bool Gpg::encryptToFile(QString str, QString path, const QString uid, bool ascii_armor,
bool text_mode)
{
/*QFile file(path);
qDebug() << "Encrypt to file " << path;
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
qErrnoWarning("Can't open the File");
qWarning() << "Can't open the file to write it" ;
return false;
}
file.write(Pass::encrypt(str));
return true;*/
file.write(encrypt(str, uid, ascii_armor, text_mode));
return true;
}
QString Gpg::getKeyId(QString uid)
std::vector<Key> Gpg::getKeys(QString pattern_uid, bool remote, bool include_sigs, bool validate)
{
qDebug() << "Getting the key id " << uid;
auto *job = openpgp()->keyListJob(false, false, false);
qDebug() << "Getting the key " << pattern_uid;
auto *job = openpgp()->keyListJob(remote, include_sigs, validate);
std::vector<Key> keys;
auto result = job->exec(QStringList() << uid, false, keys);
auto result = job->exec(QStringList() << pattern_uid, false, keys);
delete job;
if (keys.empty()) {
qDebug() << "No key found for" << uid;
return nullptr;
}
const QString kId = QLatin1String(keys.front().keyID());
qDebug() << "Id key for " << uid << "is : " << kId;
return kId;
return keys;
}
QStringList Gpg::getAllKeysId()
QStringList Gpg::getAllKeysId(bool remote, bool include_sigs, bool validate)
{
qDebug() << "Show all available key";
auto job = openpgp()->keyListJob(false, false, false);
auto job = openpgp()->keyListJob(remote, remote, validate);
std::vector<Key> keys;
auto result = job->exec(QStringList(""), false, keys);
delete job;
@ -140,7 +149,7 @@ QStringList Gpg::getAllKeysId()
return r;
}
bool Gpg::importKey(QString path)
bool Gpg::importKeyFromFile(QString path)
{
qDebug() << "Importing the key file" << path;
QFile file(path);
@ -159,15 +168,5 @@ bool Gpg::importKey(QString path)
qWarning() << "Import go wrong";
return false;
}
qDebug() << "Key imported" << result.numImported();
qDebug() << "Key not imported" << result.notImported();
qDebug() << "Key unchanged" << result.numUnchanged();
qDebug() << "Result null" << result.isNull();
qDebug() << "newUserIDs" << result.newUserIDs();
for (const auto &key : result.imports())
qDebug() << "Key" << key.fingerprint();
return true;
}
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