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

@ -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);