1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-07-04 03:02:28 +00:00

First draft Rewrite get all key with rnp

This commit is contained in:
2025-01-30 16:25:29 +01:00
parent 2c9d82e0b1
commit 4bec3dcbc9
25 changed files with 558 additions and 122 deletions

View File

@ -8,6 +8,7 @@ set(
passkeymodel.h
jobs/rmjob.cpp
jobs/rnpjob.cpp
jobs/getkeysjob.cpp
jobs/importkeyjob.cpp
)
@ -32,6 +33,7 @@ qt5_use_modules(${PLUGIN} Qml Quick DBus)
set(RNP_BUILD_DIR "${CMAKE_SOURCE_DIR}/build/${ARCH_TRIPLET}/rnp/install")
find_package(OpenSSL REQUIRED)
find_package(JSON-C 0.11)
INCLUDE_DIRECTORIES(${RNP_BUILD_DIR}/include)
@ -57,7 +59,7 @@ add_library(libqgpgme SHARED IMPORTED)
set_property(TARGET libqgpgme PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libqgpgme.so")
target_link_libraries(${PLUGIN} rnp sexpp gpgerror libassuan libgpgme libgpgmepp libqgpgme OpenSSL::Crypto)
target_link_libraries(${PLUGIN} rnp sexpp gpgerror libassuan libgpgme libgpgmepp libqgpgme OpenSSL::Crypto JSON-C::JSON-C)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)

View File

@ -0,0 +1,29 @@
#include <QDebug>
#include "getkeysjob.h"
#include <QJsonDocument>
#include <QJsonObject>
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
GetKeysJob::GetKeysJob(QDir rnp_homedir):
RnpJob(rnp_homedir)
{
this->setObjectName("GetKeysJob");
}
void GetKeysJob::run()
{
qDebug() << "[GetKeysJob] Starting";
// Loading keyring
QSet<QString> fingerprints = QSet<QString>();
this->load_full_keyring(&fingerprints);
//Get all infos keys
emit resultSuccess(fingerprints);
qDebug() << "[GetKeysJob] Finished Successfully ";
}

View File

@ -0,0 +1,38 @@
#ifndef GETKEYSJOB_H
#define GETKEYSJOB_H
#include <QJsonDocument>
#include "rnpjob.h"
/**
* @class GetKeysJob
* @brief A class to handle get all gpg keys from rings in a separate thread.
*
*/
class GetKeysJob : public RnpJob
{
Q_OBJECT
/**
* @brief The main function that performs the get all keys operation.
*
* Handles the process of removing recursively a target path.
*/
void run() override;
signals:
void resultError(const rnp_result_t err);
void resultSuccess(const QSet<QString> result);
public:
/**
* @brief Constructor for the GetKeysJob class.
*
* @param rnp_homedir Rnp home dir that contains the keyrings.
*/
GetKeysJob(QDir rnp_homedir);
};
#endif // GETKEYSJOB_H

View File

@ -1,5 +1,8 @@
#include <QDebug>
#include <QString>
#include <QJsonDocument>
#include "importkeyjob.h"
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
@ -9,38 +12,52 @@ ImportKeyJob::ImportKeyJob(QDir rnp_homedir, QString key_file_path):
RnpJob(rnp_homedir),
m_key_file_path(key_file_path)
{
this->setObjectName("DecryptJob");
this->setObjectName("ImportKeyJob");
}
void ImportKeyJob::run()
{
qDebug() << "ImportKeyJob Starting ";
qDebug() << "[ImportKeyJob] Starting";
rnp_input_t input = NULL;
auto ret = rnp_input_from_path(&input, this->m_key_file_path.toLocal8Bit().constData());
if(ret == RNP_SUCCESS) {
ret = rnp_load_keys(this->m_ffi,
"GPG",
input,
RNP_LOAD_SAVE_PUBLIC_KEYS | RNP_LOAD_SAVE_SECRET_KEYS);
if (ret == RNP_SUCCESS) {
char *r = NULL;
ret = rnp_import_keys(this->m_ffi,
input,
RNP_LOAD_SAVE_PUBLIC_KEYS | RNP_LOAD_SAVE_SECRET_KEYS,
&r);
qDebug() << "[ImportKeyJob]" << QJsonDocument::fromJson(r);
rnp_buffer_destroy(r);
}
rnp_input_destroy(input);
terminateWithError(ret);
terminateOnError(ret);
rnp_output_t output = NULL;
ret = rnp_output_to_file(&output, this->pubringPath().toLocal8Bit().constData(), RNP_OUTPUT_FILE_RANDOM);
if(ret == RNP_SUCCESS) {
ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, RNP_LOAD_SAVE_SECRET_KEYS);
}
rnp_output_destroy(output);
terminateWithError(ret);
qDebug() << "[ImportKeyJob] Writing pubring to " << this->pubringPath();
ret = rnp_output_to_file(&output, this->pubringPath().toLocal8Bit().constData(), RNP_OUTPUT_FILE_OVERWRITE);
if (ret == RNP_SUCCESS) {
qDebug() << "[ImportKeyJob] Saving key pubring ";
ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, RNP_LOAD_SAVE_PUBLIC_KEYS);
ret = rnp_output_to_file(&output, this->secringPath().toLocal8Bit().constData(), RNP_OUTPUT_FILE_OVERWRITE);
if(ret == RNP_SUCCESS) {
ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, RNP_LOAD_SAVE_SECRET_KEYS);
}
if (ret == RNP_SUCCESS) {
ret = rnp_output_finish(output);
}
rnp_output_destroy(output);
terminateWithError(ret);
terminateOnError(ret);
qDebug() << "[ImportKeyJob] Writing secring to " << this->secringPath();
ret = rnp_output_to_file(&output, this->secringPath().toLocal8Bit().constData(), RNP_OUTPUT_FILE_OVERWRITE);
if (ret == RNP_SUCCESS) {
qDebug() << "[ImportKeyJob] Saving key secring ";
ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, RNP_LOAD_SAVE_SECRET_KEYS);
}
rnp_output_destroy(output);
terminateOnError(ret);
emit resultSuccess();
qDebug() << "ImportKeyJob Finished Successfully ";
qDebug() << "[ImportKeyJob] Finished Successfully ";
}

View File

@ -20,19 +20,22 @@ class ImportKeyJob : public RnpJob
*/
void run() override;
signals:
void resultSuccess();
private:
QString m_key_file_path; ///< The path of the key file to import.
public:
/**
* @brief Constructor for the RmJob class.
* @brief Constructor for the ImportKeyJob class.
*
* Initializes the ImportKeyJob with the file to import.
*
* @param rnp_homedir Rnp home dir that contains the keyrings.
* @param path Path of the key file to import.
*/
ImportKeyJob(QDir rnp_homedir, QString key_file_path);
ImportKeyJob(QDir rnp_homedir, QString path);
};
#endif // IMPORTKEYJOB_H

View File

@ -1,5 +1,8 @@
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSet>
#include "qjsonarray.h"
#include "rnpjob.h"
extern "C" {
#include <rnp/rnp.h>
@ -9,29 +12,34 @@ extern "C" {
RnpJob::RnpJob(QDir rnp_homedir):
m_rnp_homedir(rnp_homedir)
{
qRegisterMetaType<rnp_result_t>("rnp_result_t");
qRegisterMetaType<QSet<QString >> ("QSet<QString>");
auto ret = rnp_ffi_create(&this->m_ffi,
RNP_KEYSTORE_GPG,
RNP_KEYSTORE_GPG);
RNP_KEYSTORE_GPG,
RNP_KEYSTORE_GPG);
if(ret != RNP_SUCCESS) {
qDebug() << "Err : " << ret;
qDebug() << "[RnpJob] Err : " << ret;
qFatal("Error on rnp ffi init!");
}
}
RnpJob::~RnpJob(){
RnpJob::~RnpJob()
{
auto ret = rnp_ffi_destroy(this->m_ffi);
if(ret != RNP_SUCCESS) {
qDebug() << "Err : " << ret;
qDebug() << "[RnpJob] Err : " << ret;
qFatal("Something go wrong on rnp ffi detroy");
}
}
bool RnpJob::passProvider(rnp_ffi_t ffi,
void * app_ctx,
rnp_key_handle_t key,
const char * pgp_context,
char buf[],
size_t buf_len)
void *app_ctx,
rnp_key_handle_t key,
const char *pgp_context,
char buf[],
size_t buf_len)
{
if (strcmp(pgp_context, "protect")) {
return false;
@ -40,3 +48,56 @@ bool RnpJob::passProvider(rnp_ffi_t ffi,
strncpy(buf, "password", buf_len);
return true;
}
void RnpJob::load_key_file(QSet<QString> *fingerprints, const QString path, const uint32_t flags)
{
qDebug() << "[RnpJob] load keyring at" << path;
rnp_input_t input = NULL;
if (QFileInfo::exists(this->pubringPath())) {
auto ret = rnp_input_from_path(&input, path.toLocal8Bit().constData());
char *json = NULL;
if (ret == RNP_SUCCESS) {
ret = rnp_import_keys(this->m_ffi,
input,
flags,
&json);
if (ret != RNP_SUCCESS) {
}
}
QJsonDocument json_document = QJsonDocument::fromJson(json);
qDebug() << "[RnpJob] json" << json_document;
foreach (const QJsonValue fingerprint, json_document.object()["keys"].toArray()) {
qDebug() << "[RnpJob] Add fingerprint" << fingerprint["fingerprint"].toString();
fingerprints->insert(fingerprint["fingerprint"].toString());
}
rnp_input_destroy(input);
rnp_buffer_destroy(json);
terminateOnError(ret);
qDebug() << "[RnpJob] keyring loaded successfully";
} else {
qDebug() << "[RnpJob] No keyring" << path << "not found";
}
}
void RnpJob::load_pub_keyring(QSet<QString> *fingerprints)
{
this->load_key_file(fingerprints, this->pubringPath(), RNP_LOAD_SAVE_PUBLIC_KEYS);
qDebug() << "[RnpJob] pub fingerprints" << *fingerprints;
}
void RnpJob::load_sec_keyring(QSet<QString> *fingerprints)
{
this->load_key_file(fingerprints, this->secringPath(), RNP_LOAD_SAVE_SECRET_KEYS);
qDebug() << "[RnpJob] sec fingerprints" << *fingerprints;
}
void RnpJob::load_full_keyring(QSet<QString> *fingerprints)
{
this->load_pub_keyring(fingerprints);
this->load_sec_keyring(fingerprints);
qDebug() << "[RnpJob] full fingerprints" << *fingerprints;
}

View File

@ -9,10 +9,10 @@ extern "C" {
#include <variant>
#define terminateWithError(ret) \
#define terminateOnError(ret) \
if(ret != RNP_SUCCESS) { \
qDebug() << "Err : " << ret; \
qDebug() << "Err Msg : " << rnp_result_to_string(ret); \
qDebug() << "[RnpJob] Err : " << ret; \
qDebug() << "[RnpJob] Err Msg : " << rnp_result_to_string(ret); \
emit resultError(ret); \
return; \
} \
@ -30,16 +30,16 @@ class RnpJob : public QThread
signals:
void resultError(const rnp_result_t err);
void resultSuccess();
private:
static bool passProvider(rnp_ffi_t ffi,
void * app_ctx,
rnp_key_handle_t key,
const char * pgp_context,
char buf[],
size_t buf_len);
void *app_ctx,
rnp_key_handle_t key,
const char *pgp_context,
char buf[],
size_t buf_len);
QDir m_rnp_homedir; ///< rmp ffi.
void load_key_file(QSet<QString> *fingerprints, const QString path, const uint32_t flags);
protected:
rnp_ffi_t m_ffi; ///< rmp ffi.
@ -49,7 +49,8 @@ protected:
*
* @return The path to public keys keyring
*/
QString pubringPath() {
QString pubringPath()
{
return this->m_rnp_homedir.filePath("pubring.pgp");
}
@ -58,16 +59,22 @@ protected:
*
* @return The path to secret keys keyring
*/
QString secringPath() {
QString secringPath()
{
return this->m_rnp_homedir.filePath("secring.pgp");
}
void load_sec_keyring(QSet<QString> *fingerprints);
void load_pub_keyring(QSet<QString> *fingerprints);
void load_full_keyring(QSet<QString> *fingerprints);
public:
/**
* @brief Constructor for the RnpJob class.
*
* Initializes the RnpJob instance.
*
* @param rnp_homedir Rnp home dir that contains the keyrings.
*/
RnpJob(QDir rnp_homedir);

View File

@ -2,6 +2,7 @@
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
#include "jobs/getkeysjob.h"
#include "jobs/importkeyjob.h"
#include "pass.h"
@ -11,33 +12,46 @@ Pass::Pass():
m_password_store (QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.password-store")),
m_gpg_home (QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.rnp")),
QStandardPaths::AppDataLocation).append("/.rnp")),
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1))),
m_show_filename(QString())
{
qRegisterMetaType<rnp_result_t>("rnp_result_t");
}
void Pass::initialize(QObject *window)
{
if (!window) {
qWarning("Window should not be null unless your in testing");
qWarning("[Pass] Window should be null only for testing");
}
this->initGpgHome();
this->initPasswordStore();
}
// this->m_gpg = std::unique_ptr<Gpg>(new Gpg(window));
// UTPassphraseProvider *passphrase_provider = dynamic_cast<UTPassphraseProvider*>(this->m_gpg->passphrase_provider());
// QObject::connect(this, &Pass::responsePassphraseDialogPropagate, passphrase_provider,
// &UTPassphraseProvider::handleResponse);
// QObject::connect(this->m_gpg.get(), &Gpg::getKeysResult, this, &Pass::getAllGPGKeysResult);
// QObject::connect(this->m_gpg.get(), &Gpg::deleteKeyResult, this, &Pass::deleteGPGKeyResult);
// QObject::connect(this->m_gpg.get(), &Gpg::decryptResult, this, &Pass::showResult);
void Pass::initGpgHome()
{
// delete gpghome from previous version using GPGME
QString path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.gpghome");
QDir dir(path);
dir.removeRecursively();
QDir dir(m_password_store);
if (!dir.exists()) {
dir.mkpath(".");
// create gpghome for rnp
QDir dir_gpg_home(this->m_gpg_home);
if (!dir_gpg_home.exists()) {
dir_gpg_home.mkpath(".");
}
qInfo() << "Password Store is :" << m_password_store;
qInfo() << "[Pass] GPG Home is :" << m_gpg_home;
}
void Pass::initPasswordStore()
{
QDir dir_password_store(this->m_password_store);
if (!dir_password_store.exists()) {
dir_password_store.mkpath(".");
}
qInfo() << "[Pass] Password Store is :" << m_password_store;
}
// bool Pass::show(QUrl url)
@ -125,9 +139,9 @@ void Pass::initialize(QObject *window)
bool Pass::importGPGKey(QUrl url)
{
qInfo() << "Import GPG Key from " << url;
qInfo() << "[Pass] Import GPG Key from " << url;
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "A job is already running";
qInfo() << "[Pass] A job is already running";
return false;
}
auto job = new ImportKeyJob(this->m_gpg_home, url.toLocalFile());
@ -140,26 +154,46 @@ bool Pass::importGPGKey(QUrl url)
void Pass::slotImportGPGKeyError(rnp_result_t err)
{
qDebug() << "Import GPG Key Failed";
qInfo() << "[Pass] Import GPG Key Failed";
emit importGPGKeyFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
void Pass::slotImportGPGKeySucceed()
{
qDebug() << "Import GPG Key Failed";
qInfo() << "[Pass] Import GPG Key Succesfull";
emit importGPGKeySucceed();
this->m_sem->release(1);
}
// bool Pass::getAllGPGKeys()
// {
// if (!this->m_sem->tryAcquire(1, 500)) {
// return false;
// }
// qInfo() << "Get GPG keys";
// return this->m_gpg->getAllKeys();
// }
bool Pass::getAllGPGKeys()
{
qInfo() << "[Pass] Get all GPG Keys";
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "[Pass] A job is already running";
return false;
}
auto job = new GetKeysJob(this->m_gpg_home);
QObject::connect(job, &GetKeysJob::resultError, this, &Pass::slotGetAllGPGKeysError);
QObject::connect(job, &GetKeysJob::resultSuccess, this, &Pass::slotGetAllGPGKeysSucceed);
connect(job, &ImportKeyJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::slotGetAllGPGKeysError(rnp_result_t err)
{
qInfo() << "[Pass] Get all GPG Keys Failed";
emit getAllGPGKeysFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
void Pass::slotGetAllGPGKeysSucceed(QSet<QString> result)
{
qInfo() << "[Pass] Get all GPG Keys Succeed";
emit getAllGPGKeysSucceed(result.values());
this->m_sem->release(1);
}
// void Pass::getAllGPGKeysResult(Error err, std::vector<GpgME::Key> keys_info)
// {

View File

@ -49,15 +49,19 @@ private slots:
/**
* @brief Slot to handle the succeed result of a GPG key import operation.
* @param err The error that occurred during the operation.
*/
void slotImportGPGKeySucceed();
/**
* @brief Slot to handle the result of retrieving all GPG keys.
* @param err The error that occurred during the operation.
* @param keys_info The list of GPG keys retrieved.
*/
void getAllGPGKeysResult(Error err, std::vector<GpgME::Key> keys_info);
void slotGetAllGPGKeysError(rnp_result_t err);
/**
* @brief Slot to handle the succeed result of a GPG key get all keys operation.
*/
void slotGetAllGPGKeysSucceed(QSet<QString> result);
/**
* @brief Slot to handle the result of a delete Password Store operation.
@ -93,7 +97,7 @@ signals:
* @brief Emitted when all GPG keys are successfully retrieved.
* @param keys_info The list of retrieved keys.
*/
void getAllGPGKeysSucceed(QVariant keys_info);
void getAllGPGKeysSucceed(QList<QString> keys_info);
/**
* @brief Emitted when retrieving GPG keys fails.
@ -142,10 +146,21 @@ signals:
private:
QString m_password_store; /**< The path to the password store. */
QString m_gpg_home; /**< The path to the gpg home. */
PassphraseProvider* m_passphrase_provider; /**< Semaphore for managing concurrent operations. */
PassphraseProvider *m_passphrase_provider; /**< Semaphore for managing concurrent operations. */
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing concurrent operations. */
QString m_show_filename; /**< The filename associated with the password to show. */
/**
* @brief Initialize gpg home.
*/
void initGpgHome();
/**
* @brief Initialize password store.
*/
void initPasswordStore();
public:
/**
* @brief Constructs the Pass object.
@ -167,7 +182,7 @@ public:
*/
void set_password_store(QString password_store)
{
qInfo() << "Password Store changed to :" << password_store;
qInfo() << "[Pass] Password Store changed to :" << password_store;
this->m_password_store = password_store;
};
@ -186,7 +201,7 @@ public:
*/
void set_gpg_home(QString gpg_home)
{
qInfo() << "GNUPG Home changed to :" << gpg_home;
qInfo() << "[Pass] GPG Home changed to :" << gpg_home;
this->m_gpg_home = gpg_home;
};