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

Complete rewrite from gpgme to rnp

This commit is contained in:
2025-02-03 21:46:21 +01:00
parent e56c16f27b
commit ba52ddac5c
20 changed files with 333 additions and 171 deletions

View File

@ -16,12 +16,13 @@ DecryptJob::DecryptJob(QDir rnp_homedir, QString path):
void DecryptJob::run()
{
qDebug() << "[DecryptJob] Starting";
this->load_sec_keyring(NULL);
this->loadFullKeyring(NULL);
rnp_input_t input = NULL;
rnp_output_t output = NULL;
uint8_t * buf = NULL;
uint8_t *buf = NULL;
size_t buf_len = 0;
QString data = QString::Null();
auto ret = rnp_input_from_path(&input, this->m_encrypted_file_path.toLocal8Bit().data());
if (ret == RNP_SUCCESS) {
@ -34,14 +35,13 @@ void DecryptJob::run()
ret = rnp_output_memory_get_buf(output, &buf, &buf_len, false);
}
if (ret == RNP_SUCCESS) {
emit resultSuccess(this->m_encrypted_file_path, QString::fromUtf8((char*)buf));
data = QString::fromUtf8((char*)buf);
}
rnp_input_destroy(input);
rnp_output_destroy(output);
terminateOnError(ret);
emit resultSuccess(this->m_encrypted_file_path, QString::fromUtf8((char*)buf));
emit resultSuccess(this->m_encrypted_file_path, data);
qDebug() << "[DecryptJob] Finished Successfully ";
}

View File

@ -18,8 +18,7 @@ class DecryptJob : public RnpJob
* @brief Executes the decryption operation.
*
* This method performs the actual decryption of the encrypted file specified during
* object construction. The operation is carried out in a separate background thread
* to prevent blocking the main application thread.
* object construction.
*/
void run() override;

View File

@ -0,0 +1,42 @@
#include <QDebug>
#include <QString>
#include <QJsonDocument>
#include "deletekeyjob.h"
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
DeleteKeyJob::DeleteKeyJob(QDir rnp_homedir, QString fingerprint):
RnpJob(rnp_homedir),
m_fingerprint(fingerprint)
{
this->setObjectName("ImportKeyJob");
}
void DeleteKeyJob::run()
{
qDebug() << "[DeleteKeyJob] Starting";
// Loading keyring
this->loadFullKeyring(NULL);
// Delete key
rnp_key_handle_t key = NULL;
auto ret = rnp_locate_key(this->m_ffi, "fingerprint", this->m_fingerprint.toLocal8Bit().data(), &key);
if (ret == RNP_SUCCESS) {
ret = rnp_key_remove(key, RNP_KEY_REMOVE_PUBLIC | RNP_KEY_REMOVE_SECRET | RNP_KEY_REMOVE_SUBKEYS);
};
rnp_key_handle_destroy(key);
// Save resulting keyring
this->saveFullKeyring();
// Emit result
terminateOnError(ret);
emit resultSuccess();
qDebug() << "[DeleteKeyJob] Finished Successfully ";
}

View File

@ -0,0 +1,46 @@
#ifndef DELETEKEYJOB_H
#define DELETEKEYJOB_H
#include "rnpjob.h"
/**
* @class DeleteKeyJob
* @brief A job to handle the deletion of a key in a separate thread.
*
*/
class DeleteKeyJob : public RnpJob
{
Q_OBJECT
/**
* @brief Executes the key deletion operation.
*
* This function performs the actual process of deleting the GPG key from the keyring.
*/
void run() override;
signals:
/**
* @brief Emitted when the key deletion operation is successful.
*
* This signal is emitted when the key is successfully deleted from the keyring..
*/
void resultSuccess();
private:
QString m_fingerprint; /**< The fingerprint of the key to delete. */
public:
/**
* @brief Constructs a DeleteKeyJob object with the specified fingerprint and keyring directory.
*
* This constructor initializes the DeleteKeyJob instance with the directory containing
* the keyrings and the fingerprint of the GPG key to delete.
*
* @param rnp_homedir The directory containing the keyrings where the key will be deleted.
* @param fingerprint The fingerprint of the key to delete.
*/
DeleteKeyJob(QDir rnp_homedir, QString fingerprint);
};
#endif // DELETEKEYJOB_H

View File

@ -30,7 +30,7 @@ void GetKeysJob::run()
// Loading keyring
QSet<QString> fingerprints = QSet<QString>();
this->load_full_keyring(&fingerprints);
this->loadFullKeyring(&fingerprints);
//Get infos keys
auto key_infos = QList<QJsonDocument>();

View File

@ -16,8 +16,7 @@ class GetKeysJob : public RnpJob
/**
* @brief Executes the process of fetching all GPG keys.
*
* This function performs the task of retrieving all keys from the keyrings. It is executed
* in a separate thread to avoid blocking the main application thread.
* This function performs the task of retrieving all keys from the keyrings.
*/
void run() override;

View File

@ -19,6 +19,11 @@ ImportKeyJob::ImportKeyJob(QDir rnp_homedir, QString key_file_path):
void ImportKeyJob::run()
{
qDebug() << "[ImportKeyJob] Starting";
// Loading keyring
this->loadFullKeyring(NULL);
// Import new key
rnp_input_t input = NULL;
auto ret = rnp_input_from_path(&input, this->m_key_file_path.toLocal8Bit().constData());
if (ret == RNP_SUCCESS) {
@ -35,29 +40,33 @@ void ImportKeyJob::run()
rnp_input_destroy(input);
terminateOnError(ret);
rnp_output_t output = NULL;
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);
// Save resulting keyring
this->saveFullKeyring();
// rnp_output_t output = NULL;
// 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);
}
if (ret == RNP_SUCCESS) {
ret = rnp_output_finish(output);
}
rnp_output_destroy(output);
terminateOnError(ret);
// }
// if (ret == RNP_SUCCESS) {
// ret = rnp_output_finish(output);
// }
// rnp_output_destroy(output);
// 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);
}
// 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);
// rnp_output_destroy(output);
// terminateOnError(ret);
// Emit result
emit resultSuccess();
qDebug() << "[ImportKeyJob] Finished Successfully ";
}

View File

@ -16,8 +16,7 @@ class ImportKeyJob : public RnpJob
* @brief Executes the key import operation.
*
* This function handles the actual process of importing the GPG key file into the
* keyring. It is executed in a separate thread to prevent UI freezing or blocking
* of the main application thread during the import.
* keyring.
*/
void run() override;

View File

@ -17,8 +17,6 @@ class RmJob : public QThread
* @brief Executes the recursive remove operation.
*
* This method performs the recursive removal of the specified target path.
* The operation is performed in the background to prevent blocking of the main
* application thread.
*/
void run() override;

View File

@ -50,7 +50,7 @@ bool RnpJob::passProvider(rnp_ffi_t ffi,
}
void RnpJob::load_key_file(QSet<QString> *result_fingerprints, const QString path, const uint32_t flags)
void RnpJob::loadKeyFile(QSet<QString> *result_fingerprints, const QString path, const uint32_t flags)
{
qDebug() << "[RnpJob] Load keyring at" << path;
rnp_input_t input = NULL;
@ -65,7 +65,7 @@ void RnpJob::load_key_file(QSet<QString> *result_fingerprints, const QString pat
}
QJsonDocument json_document = QJsonDocument::fromJson(json);
qDebug() << "[RnpJob] json" << json_document;
if(result_fingerprints) {
if (result_fingerprints) {
foreach (const QJsonValue fingerprint, json_document.object()["keys"].toArray()) {
qDebug() << "[RnpJob] Add fingerprint" << fingerprint["fingerprint"].toString();
result_fingerprints->insert(fingerprint["fingerprint"].toString());
@ -82,18 +82,53 @@ void RnpJob::load_key_file(QSet<QString> *result_fingerprints, const QString pat
}
void RnpJob::load_pub_keyring(QSet<QString> *result_fingerprints = NULL)
void RnpJob::loadPubKeyring(QSet<QString> *result_fingerprints = NULL)
{
this->load_key_file(result_fingerprints, this->pubringPath(), RNP_LOAD_SAVE_PUBLIC_KEYS);
this->loadKeyFile(result_fingerprints, this->pubringPath(), RNP_LOAD_SAVE_PUBLIC_KEYS);
}
void RnpJob::load_sec_keyring(QSet<QString> *result_fingerprints = NULL)
void RnpJob::loadSecKeyring(QSet<QString> *result_fingerprints = NULL)
{
this->load_key_file(result_fingerprints, this->secringPath(), RNP_LOAD_SAVE_SECRET_KEYS);
this->loadKeyFile(result_fingerprints, this->secringPath(), RNP_LOAD_SAVE_SECRET_KEYS);
}
void RnpJob::load_full_keyring(QSet<QString> *result_fingerprints = NULL)
void RnpJob::loadFullKeyring(QSet<QString> *result_fingerprints = NULL)
{
this->load_pub_keyring(result_fingerprints);
this->load_sec_keyring(result_fingerprints);
this->loadPubKeyring(result_fingerprints);
this->loadSecKeyring(result_fingerprints);
}
void RnpJob::saveKeyFile(const QString path, const uint32_t flags)
{
qDebug() << "[RnpJob] Saving keyring at" << path;
rnp_output_t output = NULL;
auto ret = rnp_output_to_file(&output, path.toLocal8Bit().data(), RNP_OUTPUT_FILE_OVERWRITE);
if (ret == RNP_SUCCESS) {
qDebug() << "[ImportKeyJob] Saving key pubring ";
ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, flags);
}
if (ret == RNP_SUCCESS) {
ret = rnp_output_finish(output);
}
rnp_output_destroy(output);
terminateOnError(ret);
}
void RnpJob::savePubKeyring()
{
this->saveKeyFile(this->pubringPath(), RNP_LOAD_SAVE_PUBLIC_KEYS);
}
void RnpJob::saveSecKeyring()
{
this->saveKeyFile(this->secringPath(), RNP_LOAD_SAVE_SECRET_KEYS);
}
void RnpJob::saveFullKeyring()
{
this->savePubKeyring();
this->saveSecKeyring();
}

View File

@ -16,14 +16,14 @@ if(ret != RNP_SUCCESS) { \
return; \
} \
/**
/**
* @class RnpJob
* @brief A base class that manages OpenPGP-related tasks using the librnp library.
*
* The RnpJob class serves as an abstraction for performing OpenPGP (RNP) operations, such as
* encryption, decryption, and key management, using the RNP library.
*/
class RnpJob : public QThread
class RnpJob : public QThread
{
Q_OBJECT
@ -73,7 +73,19 @@ private:
* @param path The path to the key file.
* @param flags Flags specifying options for loading keys (e.g., overwrite, secret keys, etc.).
*/
void load_key_file(QSet<QString> *result_fingerprints, const QString path, const uint32_t flags);
void loadKeyFile(QSet<QString> *result_fingerprints, const QString path, const uint32_t flags);
/**
* @brief Saves keys to the keyring file in the specified directory.
*
* This method saves a keyring to a file. It allows you to specify options such as overwriting
* existing files or including secret keys.
*
* @param path The path to the keyring file where the keys should be saved.
* @param flags Flags specifying options for saving the keys (e.g., overwrite, include secret keys, etc.).
*/
void saveKeyFile(const QString path, const uint32_t flags);
protected:
rnp_ffi_t m_ffi; /**< RNP FFI (Foreign Function Interface) handle, used for interacting with the RNP library. */
@ -105,34 +117,43 @@ protected:
}
/**
* @brief Loads the secret keyring into the RNP system.
*
* This method loads the secret keyring file (secring.pgp) into the system, adding keys
* to the keyring based on their fingerprints.
* @brief Loads the secret keyring into RNP.
*
* @param result_fingerprints A set that will hold the fingerprints of the loaded secret keys.
*/
void load_sec_keyring(QSet<QString> *result_fingerprints);
void loadSecKeyring(QSet<QString> *result_fingerprints);
/**
* @brief Loads the public keyring into the RNP system.
*
* This method loads the public keyring file (pubring.pgp) into the system, adding keys
* to the keyring based on their fingerprints.
* @brief Loads the public keyring into RNP.
*
* @param result_fingerprints A set that will hold the fingerprints of the loaded public keys.
*/
void load_pub_keyring(QSet<QString> *result_fingerprints);
void loadPubKeyring(QSet<QString> *result_fingerprints);
/**
* @brief Loads both the public and secret keyrings into the RNP system.
*
* This method loads both the public and secret keyring files into the system, adding keys
* to the keyring based on their fingerprints. It is a combined operation for full keyring loading.
* @brief Loads both the public and secret keyrings into RNP.
*
* @param result_fingerprints A set that will hold the fingerprints of all loaded keys.
*/
void load_full_keyring(QSet<QString> *result_fingerprints);
void loadFullKeyring(QSet<QString> *result_fingerprints);
/**
* @brief Saves the secret keyring to the RNP homedir.
*
*/
void saveSecKeyring();
/**
* @brief Saves the public keyring to the RNP homedir.
*
*/
void savePubKeyring();
/**
* @brief Saves both the public and secret keyrings to the RNP homedir.
*
*/
void saveFullKeyring();
public:
/**
@ -155,7 +176,8 @@ public:
~RnpJob();
void setPassProvider(rnp_password_cb pass_provider_cb) {
void setPassProvider(rnp_password_cb pass_provider_cb)
{
rnp_ffi_set_pass_provider(this->m_ffi, pass_provider_cb, NULL);
}
};