mirror of
https://github.com/QRouland/UTPass.git
synced 2025-06-30 01:02:29 +00:00
Inital base for use GPG && GpgmE
This commit is contained in:
@ -1,3 +1,3 @@
|
||||
add_subdirectory(FileSystem)
|
||||
add_subdirectory(Gpg)
|
||||
add_subdirectory(Pass)
|
||||
|
||||
|
@ -1,25 +0,0 @@
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
set(PLUGIN "FileSystem")
|
||||
|
||||
set(
|
||||
SRC
|
||||
plugin.cpp
|
||||
filesystem.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
|
||||
)
|
||||
|
||||
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
|
||||
|
||||
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
|
||||
install(FILES qmldir DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
|
@ -1,11 +0,0 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
FileSystem::FileSystem() {
|
||||
|
||||
}
|
||||
|
||||
void FileSystem::list_dir() {
|
||||
qDebug() << "hello world!";
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
#ifndef PASS_H
|
||||
#define PASS_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class FileSystem: public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FileSystem();
|
||||
~FileSystem() = default;
|
||||
|
||||
Q_INVOKABLE void list_dir();
|
||||
};
|
||||
|
||||
#endif
|
@ -1,10 +0,0 @@
|
||||
#include <QtQml>
|
||||
#include <QtQml/QQmlContext>
|
||||
|
||||
#include "plugin.h"
|
||||
#include "filesystem.h"
|
||||
|
||||
void FileSystemPlugin::registerTypes(const char *uri) {
|
||||
//@uri Pass
|
||||
qmlRegisterSingletonType<FileSystem>(uri, 1, 0, "FileSystem", [](QQmlEngine*, QJSEngine*) -> QObject* { return new FileSystem; });
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
module FileSystem
|
||||
plugin FileSystem
|
48
plugins/Gpg/CMakeLists.txt
Normal file
48
plugins/Gpg/CMakeLists.txt
Normal file
@ -0,0 +1,48 @@
|
||||
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}/local")
|
||||
set(THIRD_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
INCLUDE_DIRECTORIES(${EXTERNAL_LIBS}/include)
|
||||
add_dependencies(${PLUGIN} LibGpgme)
|
||||
|
||||
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}/)
|
166
plugins/Gpg/gpg.cpp
Normal file
166
plugins/Gpg/gpg.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QtCore/QDirIterator>
|
||||
#include <QtCore/QStandardPaths>
|
||||
|
||||
#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"
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void Gpg::list_dir() {
|
||||
qDebug() << "hello world!";
|
||||
}
|
||||
|
||||
QString Gpg::decrypt(const QByteArray cipherText) {
|
||||
/*auto decJob = openpgp()->decryptJob();
|
||||
auto ctx = DecryptJob::context(decJob);*/
|
||||
|
||||
/* TODO
|
||||
* TestPassphraseProvider provider;
|
||||
ctx->setPassphraseProvider(&provider);
|
||||
ctx->setPinentryMode(Context::PinentryLoopback);*/
|
||||
|
||||
/*QByteArray plainText;
|
||||
decJob->exec(cipherText, plainText);
|
||||
|
||||
return QString::fromUtf8(plainText);*/
|
||||
}
|
||||
|
||||
QString Gpg::decrypt_file(const QString path) {
|
||||
/*QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qErrnoWarning("Can't open the File");
|
||||
return nullptr;
|
||||
}
|
||||
QByteArray plainText = file.readAll();
|
||||
return this->decrypt(plainText);*/
|
||||
}
|
||||
|
||||
QByteArray Gpg::encrypt(const QString str) {
|
||||
/*auto listjob = openpgp()->keyListJob(false, false, false);
|
||||
std::vector<Key> keys;
|
||||
auto keylistresult = listjob->exec(QStringList() << QStringLiteral("alfa@example.net"), false, keys);
|
||||
|
||||
auto job = openpgp()
|
||||
->encryptJob(
|
||||
true, //ASCII Armor
|
||||
true //Textmode
|
||||
);
|
||||
QByteArray cipherText;
|
||||
auto result = job->exec(keys, QStringLiteral("Hello World").toUtf8(), Context::AlwaysTrust, cipherText);
|
||||
return cipherText;*/
|
||||
}
|
||||
|
||||
bool Gpg::encrypt_file(const QString str, const QString path) {
|
||||
/*QFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
qErrnoWarning("Can't open the File");
|
||||
return false;
|
||||
}
|
||||
file.write(Pass::encrypt(str));
|
||||
return true;*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
QString Gpg::get_key_id(QString uid) {
|
||||
qDebug() << "Getting the key id " << uid;
|
||||
auto *job = openpgp()->keyListJob(false, false, false);
|
||||
std::vector<Key> keys;
|
||||
auto result = job->exec(QStringList() << 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;
|
||||
}
|
||||
|
||||
QStringList Gpg::get_all_keys_id() {
|
||||
qDebug() << "Show all available key";
|
||||
auto job = openpgp()->keyListJob(false, false, false);
|
||||
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::import_key(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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
23
plugins/Gpg/gpg.h
Normal file
23
plugins/Gpg/gpg.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef PASS_H
|
||||
#define PASS_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class Gpg: public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Gpg();
|
||||
~Gpg() = default;
|
||||
|
||||
Q_INVOKABLE void list_dir();
|
||||
Q_INVOKABLE QString get_key_id(QString uid);
|
||||
Q_INVOKABLE QStringList get_all_keys_id();
|
||||
Q_INVOKABLE bool import_key(QString path);
|
||||
Q_INVOKABLE QString decrypt(QByteArray plainText);
|
||||
Q_INVOKABLE QString decrypt_file(QString path);
|
||||
Q_INVOKABLE QByteArray encrypt(QString str);
|
||||
Q_INVOKABLE bool encrypt_file(QString str, QString path);
|
||||
};
|
||||
|
||||
#endif
|
10
plugins/Gpg/plugin.cpp
Normal file
10
plugins/Gpg/plugin.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include <QtQml>
|
||||
#include <QtQml/QQmlContext>
|
||||
|
||||
#include "plugin.h"
|
||||
#include "gpg.h"
|
||||
|
||||
void FileSystemPlugin::registerTypes(const char *uri) {
|
||||
//@uri Pass
|
||||
qmlRegisterSingletonType<Gpg>(uri, 1, 0, "Gpg", [](QQmlEngine*, QJSEngine*) -> QObject* { return new Gpg; });
|
||||
}
|
2
plugins/Gpg/qmldir
Normal file
2
plugins/Gpg/qmldir
Normal file
@ -0,0 +1,2 @@
|
||||
module Gpg
|
||||
plugin Gpg
|
@ -13,28 +13,6 @@ 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}/local")
|
||||
set(THIRD_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
INCLUDE_DIRECTORIES(${EXTERNAL_LIBS}/include)
|
||||
add_dependencies(${PLUGIN} LibGpgme)
|
||||
|
||||
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
|
||||
|
@ -1,71 +1,16 @@
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include "pass.h"
|
||||
#include "gpgme++/global.h"
|
||||
#include "gpgme++/engineinfo.h"
|
||||
#include "qgpgme/protocol.h"
|
||||
#include "qgpgme/encryptjob.h"
|
||||
|
||||
#include "pass.h"
|
||||
|
||||
|
||||
Pass::Pass() {
|
||||
qDebug() << "Initializing GpgMe";
|
||||
if(GpgME::initializeLibrary(GpgME::OpenPGP))
|
||||
qFatal("Fatal error when initializing GpgMe");
|
||||
qDebug() << "GpgME Engine Version :" << GpgME::engineInfo(GpgME::GpgEngine).version();
|
||||
|
||||
}
|
||||
|
||||
void Pass::speak() {
|
||||
qDebug() << "Starting app from main.cpp";
|
||||
}
|
||||
|
||||
QString Pass::decrypt(const QByteArray cipherText) {
|
||||
/*auto decJob = QGpgME::openpgp()->decryptJob();
|
||||
auto ctx = QGpgME::Job::context(decJob);
|
||||
|
||||
TestPassphraseProvider provider;
|
||||
ctx->setPassphraseProvider(&provider);
|
||||
ctx->setPinentryMode(Context::PinentryLoopback);
|
||||
|
||||
QByteArray plainText;
|
||||
decJob->exec(cipherText, plainText);
|
||||
|
||||
return QString::fromUtf8(plainText);*/
|
||||
}
|
||||
|
||||
QString Pass::decrypt_file(const QString path) {
|
||||
/*QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qErrnoWarning("Can't open the File");
|
||||
return nullptr;
|
||||
}
|
||||
QByteArray plainText = file.readAll();
|
||||
return Pass::decrypt(plainText);*/
|
||||
}
|
||||
|
||||
QByteArray Pass::encrypt(const QString str) {
|
||||
/*auto listjob = QGpgME::openpgp()->keyListJob(false, false, false);
|
||||
std::vector<GpgME::Key> keys;
|
||||
auto keylistresult = listjob->exec(QStringList() << QStringLiteral("alfa@example.net"), false, keys);
|
||||
|
||||
|
||||
|
||||
auto job = QGpgME::openpgp()
|
||||
->encryptJob(
|
||||
true, //ASCII Armor
|
||||
true //Textmode
|
||||
);
|
||||
QByteArray cipherText;
|
||||
auto result = job->exec(keys, QStringLiteral("Hello World").toUtf8(), GpgME::Context::AlwaysTrust, cipherText);
|
||||
return cipherText;*/
|
||||
}
|
||||
|
||||
bool Pass::encrypt_file(const QString str, const QString path) {
|
||||
/*QFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
qErrnoWarning("Can't open the File");
|
||||
return false;
|
||||
}
|
||||
file.write(Pass::encrypt(str));
|
||||
return true;*/
|
||||
}
|
||||
|
@ -12,10 +12,6 @@ public:
|
||||
~Pass() override = default;
|
||||
|
||||
Q_INVOKABLE void speak();
|
||||
Q_INVOKABLE QString decrypt(QByteArray plainText);
|
||||
Q_INVOKABLE QString decrypt_file(QString path);
|
||||
Q_INVOKABLE QByteArray encrypt(QString str);
|
||||
Q_INVOKABLE bool encrypt_file(QString str, QString path);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user