2022-03-14 13:42:33 +02:00
|
|
|
/* This file is part of the KDE project
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2022-03-14 18:17:45 +02:00
|
|
|
#include <QScopedPointer>
|
2022-03-14 13:42:33 +02:00
|
|
|
#include <klocale.h>
|
2022-03-14 18:17:45 +02:00
|
|
|
#include <kpassworddialog.h>
|
2022-03-14 18:39:37 +02:00
|
|
|
#include <kmessagebox.h>
|
2022-03-14 13:42:33 +02:00
|
|
|
#include <kapplication.h>
|
|
|
|
#include <klocale.h>
|
|
|
|
#include <kcmdlineargs.h>
|
|
|
|
#include <kaboutdata.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
#include "kgpg.h"
|
|
|
|
|
|
|
|
KGPG::KGPG(QWidget *parent)
|
|
|
|
: KMainWindow(parent),
|
2022-03-14 15:56:34 +02:00
|
|
|
m_mode(KGPG::EncryptMode),
|
2022-03-14 13:42:33 +02:00
|
|
|
m_release(false)
|
|
|
|
{
|
|
|
|
m_ui.setupUi(this);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.startbutton->setEnabled(false);
|
|
|
|
m_ui.progressbar->setVisible(false);
|
2022-03-14 13:42:33 +02:00
|
|
|
|
|
|
|
// required by context
|
|
|
|
kDebug() << gpgme_check_version(NULL);
|
|
|
|
|
|
|
|
gpgme_error_t gpgresult = gpgme_new(&m_gpgctx);
|
|
|
|
if (gpgresult != 0) {
|
2022-03-14 15:11:34 +02:00
|
|
|
setError(gpgme_strerror(gpgresult));
|
2022-03-14 13:42:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_release = true;
|
2022-03-14 17:19:33 +02:00
|
|
|
|
2022-03-14 18:17:45 +02:00
|
|
|
gpgme_set_pinentry_mode(m_gpgctx, GPGME_PINENTRY_MODE_LOOPBACK); // for password callback
|
|
|
|
gpgme_set_passphrase_cb(m_gpgctx, KGPG::gpgPasswordCallback, this);
|
|
|
|
gpgme_set_progress_cb(m_gpgctx, KGPG::gpgProgressCallback, this);
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
connect(m_ui.startbutton, SIGNAL(clicked()), this, SLOT(slotStart()));
|
2022-03-14 13:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KGPG::~KGPG()
|
|
|
|
{
|
|
|
|
// will crash if not initialized
|
|
|
|
if (m_release) {
|
|
|
|
gpgme_release(m_gpgctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:11:34 +02:00
|
|
|
void KGPG::setMode(const KGPGMode mode)
|
|
|
|
{
|
|
|
|
m_mode = mode;
|
|
|
|
switch (mode) {
|
2022-03-14 15:56:34 +02:00
|
|
|
case KGPG::EncryptMode: {
|
|
|
|
updateKeys(GPGME_KEYLIST_MODE_LOCAL, true);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.startbutton->setEnabled(!m_keys.isEmpty());
|
|
|
|
m_ui.destinationrequester->setVisible(true);
|
2022-03-14 15:56:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::DecryptMode: {
|
|
|
|
updateKeys(GPGME_KEYLIST_MODE_LOCAL, false);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.startbutton->setEnabled(!m_keys.isEmpty());
|
|
|
|
m_ui.destinationrequester->setVisible(true);
|
2022-03-14 15:56:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::SignMode: {
|
|
|
|
updateKeys(GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS, true);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.startbutton->setEnabled(!m_keys.isEmpty());
|
|
|
|
m_ui.destinationrequester->setVisible(true);
|
2022-03-14 15:56:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::VerifyMode: {
|
2022-03-14 17:35:00 +02:00
|
|
|
updateKeys(GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS, false);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.startbutton->setEnabled(true);
|
|
|
|
m_ui.destinationrequester->setVisible(false);
|
2022-03-14 15:56:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-03-14 15:11:34 +02:00
|
|
|
default: {
|
2022-03-14 15:56:34 +02:00
|
|
|
Q_ASSERT(false);
|
2022-03-14 15:11:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KGPG::setSource(const QString &source)
|
|
|
|
{
|
|
|
|
const KUrl sourceurl(source);
|
2022-03-14 17:19:33 +02:00
|
|
|
// TODO: invalid source or destination URL should disable start button
|
|
|
|
switch (m_mode) {
|
|
|
|
case KGPG::EncryptMode: {
|
2022-03-14 17:35:00 +02:00
|
|
|
gpgme_set_armor(m_gpgctx, 0);
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
QString destinationstring = sourceurl.prettyUrl();
|
|
|
|
destinationstring.append(QLatin1String(".gpg"));
|
|
|
|
m_ui.sourcerequester->setUrl(sourceurl);
|
|
|
|
m_ui.destinationrequester->setUrl(KUrl(destinationstring));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::DecryptMode: {
|
2022-03-14 17:35:00 +02:00
|
|
|
gpgme_set_armor(m_gpgctx, 0);
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
QString destinationstring = sourceurl.prettyUrl();
|
|
|
|
if (destinationstring.endsWith(QLatin1String(".gpg"))) {
|
|
|
|
destinationstring.chop(4);
|
|
|
|
}
|
|
|
|
m_ui.sourcerequester->setFilter(QString::fromLatin1("application/pgp-encrypted"));
|
|
|
|
m_ui.destinationrequester->setFilter(QString());
|
|
|
|
m_ui.sourcerequester->setUrl(sourceurl);
|
|
|
|
m_ui.destinationrequester->setUrl(KUrl(destinationstring));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::SignMode: {
|
2022-03-14 17:35:00 +02:00
|
|
|
gpgme_set_armor(m_gpgctx, 1);
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
QString destinationstring = sourceurl.prettyUrl();
|
|
|
|
destinationstring.append(QLatin1String(".asc"));
|
|
|
|
m_ui.sourcerequester->setFilter(QString());
|
|
|
|
m_ui.destinationrequester->setFilter(QString::fromLatin1("application/pgp-signature"));
|
|
|
|
m_ui.sourcerequester->setUrl(sourceurl);
|
|
|
|
m_ui.destinationrequester->setUrl(KUrl(destinationstring));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::VerifyMode: {
|
2022-03-14 17:35:00 +02:00
|
|
|
gpgme_set_armor(m_gpgctx, 1);
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.sourcerequester->setFilter(QString::fromLatin1("application/pgp-signature"));
|
|
|
|
m_ui.sourcerequester->setUrl(sourceurl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 15:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KGPG::setError(const char* const error)
|
|
|
|
{
|
2022-03-14 15:56:34 +02:00
|
|
|
setError(QString::fromLocal8Bit(error));
|
|
|
|
}
|
|
|
|
void KGPG::setError(const QString &error)
|
|
|
|
{
|
|
|
|
kWarning() << error;
|
2022-03-14 15:11:34 +02:00
|
|
|
|
2022-03-14 15:56:34 +02:00
|
|
|
const QString errormessage = i18n("Error: %1", error);
|
2022-03-14 15:11:34 +02:00
|
|
|
m_ui.statusbar->showMessage(errormessage);
|
2022-03-14 18:39:37 +02:00
|
|
|
KMessageBox::error(this, errormessage);
|
2022-03-14 17:19:33 +02:00
|
|
|
|
2022-03-14 15:11:34 +02:00
|
|
|
m_ui.progressbar->setMinimum(0);
|
2022-03-14 15:56:34 +02:00
|
|
|
m_ui.progressbar->setMaximum(100);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.progressbar->setVisible(false);
|
|
|
|
m_ui.startbutton->setVisible(true);
|
2022-03-14 15:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 18:45:31 +02:00
|
|
|
void KGPG::setProgress(const int gpgcurrent, const int gpgtotal)
|
|
|
|
{
|
|
|
|
m_ui.progressbar->setMaximum(gpgtotal);
|
|
|
|
m_ui.progressbar->setValue(gpgcurrent);
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:17:45 +02:00
|
|
|
gpgme_error_t KGPG::gpgPasswordCallback(void *opaque, const char *uid_hint,
|
|
|
|
const char *passphrase_info,
|
|
|
|
int prev_was_bad, int fd)
|
|
|
|
{
|
|
|
|
// qDebug() << Q_FUNC_INFO << uid_hint << passphrase_info << prev_was_bad << fd;
|
|
|
|
|
|
|
|
QScopedPointer<KPasswordDialog> kpassdialog(new KPasswordDialog());
|
|
|
|
kpassdialog->setPrompt(i18n("Enter a password"));
|
|
|
|
if (!kpassdialog->exec()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray bytepassword = kpassdialog->password().toLocal8Bit() + "\n";
|
|
|
|
// TODO: ignoring possible errors here
|
|
|
|
gpgme_io_write(fd, bytepassword.constData(), bytepassword.size());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KGPG::gpgProgressCallback(void *opaque, const char *what,
|
|
|
|
int type, int current, int total)
|
|
|
|
{
|
|
|
|
// qDebug() << Q_FUNC_INFO << what << type << current << total;
|
2022-03-14 18:45:31 +02:00
|
|
|
KGPG *kgpg = static_cast<KGPG*>(opaque);
|
|
|
|
kgpg->setProgress(current, total);
|
2022-03-14 18:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 15:11:34 +02:00
|
|
|
void KGPG::start()
|
|
|
|
{
|
2022-03-14 15:56:34 +02:00
|
|
|
m_ui.progressbar->setMinimum(0);
|
|
|
|
m_ui.progressbar->setMaximum(0);
|
2022-03-14 17:19:33 +02:00
|
|
|
m_ui.progressbar->setVisible(true);
|
|
|
|
m_ui.startbutton->setVisible(false);
|
|
|
|
|
2022-03-14 15:11:34 +02:00
|
|
|
switch (m_mode) {
|
2022-03-14 15:56:34 +02:00
|
|
|
case KGPG::EncryptMode: {
|
|
|
|
if (m_keys.isEmpty()) {
|
|
|
|
setError("No key available");
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray kgpgkeyfpr = m_keys.at(m_ui.keysbox->currentIndex()).fpr;
|
|
|
|
const QByteArray gpginputfile = m_ui.sourcerequester->url().toLocalFile().toLocal8Bit();
|
|
|
|
|
|
|
|
gpgme_data_t gpgindata;
|
|
|
|
gpgme_error_t gpgresult = gpgme_data_new_from_file(&gpgindata, gpginputfile.constData(), 1);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t gpgencryptkey;
|
|
|
|
gpgresult = gpgme_get_key(m_gpgctx, kgpgkeyfpr.constData(), &gpgencryptkey, 1);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_data_t gpgoutdata;
|
|
|
|
gpgresult = gpgme_data_new(&gpgoutdata);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t gpgkeys[2] = { gpgencryptkey, NULL };
|
|
|
|
gpgresult = gpgme_op_encrypt(m_gpgctx, gpgkeys, GPGME_ENCRYPT_ALWAYS_TRUST, gpgindata, gpgoutdata);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
gpgme_data_release(gpgoutdata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t gpgbuffersize = 0;
|
|
|
|
char* gpgbuffer = gpgme_data_release_and_get_mem(gpgoutdata, &gpgbuffersize);
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
const QString outputfile = m_ui.destinationrequester->url().toLocalFile();
|
2022-03-14 15:56:34 +02:00
|
|
|
QFile encryptedfile(outputfile);
|
|
|
|
if (!encryptedfile.open(QFile::WriteOnly)) {
|
|
|
|
setError(encryptedfile.errorString());
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (encryptedfile.write(gpgbuffer, gpgbuffersize) != gpgbuffersize) {
|
|
|
|
setError(encryptedfile.errorString());
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
// qDebug() << Q_FUNC_INFO << "encrypted" << gpgbuffer;
|
|
|
|
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::DecryptMode: {
|
2022-03-14 17:19:33 +02:00
|
|
|
if (m_keys.isEmpty()) {
|
|
|
|
setError("No key available");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray gpginputfile = m_ui.sourcerequester->url().toLocalFile().toLocal8Bit();
|
|
|
|
|
|
|
|
gpgme_data_t gpgindata;
|
|
|
|
gpgme_error_t gpgresult = gpgme_data_new_from_file(&gpgindata, gpginputfile.constData(), 1);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_data_t gpgoutdata;
|
|
|
|
gpgresult = gpgme_data_new(&gpgoutdata);
|
2022-03-14 15:56:34 +02:00
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
gpgme_data_release(gpgoutdata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:19:33 +02:00
|
|
|
gpgresult = gpgme_op_decrypt(m_gpgctx, gpgindata, gpgoutdata);
|
2022-03-14 15:56:34 +02:00
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
gpgme_data_release(gpgoutdata);
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t gpgbuffersize = 0;
|
2022-03-14 17:19:33 +02:00
|
|
|
char* gpgbuffer = gpgme_data_release_and_get_mem(gpgoutdata, &gpgbuffersize);
|
|
|
|
|
|
|
|
const QString outputfile = m_ui.destinationrequester->url().toLocalFile();
|
|
|
|
QFile encryptedfile(outputfile);
|
|
|
|
if (!encryptedfile.open(QFile::WriteOnly)) {
|
|
|
|
setError(encryptedfile.errorString());
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encryptedfile.write(gpgbuffer, gpgbuffersize) != gpgbuffersize) {
|
|
|
|
setError(encryptedfile.errorString());
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// qDebug() << Q_FUNC_INFO << "decryption" << gpgbuffer;
|
2022-03-14 15:56:34 +02:00
|
|
|
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::SignMode: {
|
|
|
|
if (m_keys.isEmpty()) {
|
|
|
|
setError("No key available");
|
2022-03-14 17:19:33 +02:00
|
|
|
break;
|
2022-03-14 15:56:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:35:00 +02:00
|
|
|
const QByteArray kgpgkeyfpr = m_keys.at(m_ui.keysbox->currentIndex()).fpr;
|
|
|
|
const QByteArray gpginputfile = m_ui.sourcerequester->url().toLocalFile().toLocal8Bit();
|
|
|
|
|
|
|
|
gpgme_data_t gpgindata;
|
|
|
|
gpgme_error_t gpgresult = gpgme_data_new_from_file(&gpgindata, gpginputfile.constData(), 1);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t gpgencryptkey;
|
|
|
|
gpgresult = gpgme_get_key(m_gpgctx, kgpgkeyfpr.constData(), &gpgencryptkey, 1);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_data_t gpgoutdata;
|
|
|
|
gpgresult = gpgme_data_new(&gpgoutdata);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t gpgkeys[2] = { gpgencryptkey, NULL };
|
|
|
|
// the only important difference from the code for KGPG::EncryptMode mode is the function call bellow
|
|
|
|
gpgresult = gpgme_op_encrypt_sign(m_gpgctx, gpgkeys, GPGME_ENCRYPT_ALWAYS_TRUST, gpgindata, gpgoutdata);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
gpgme_data_release(gpgoutdata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t gpgbuffersize = 0;
|
|
|
|
char* gpgbuffer = gpgme_data_release_and_get_mem(gpgoutdata, &gpgbuffersize);
|
|
|
|
|
|
|
|
const QString outputfile = m_ui.destinationrequester->url().toLocalFile();
|
|
|
|
QFile encryptedfile(outputfile);
|
|
|
|
if (!encryptedfile.open(QFile::WriteOnly)) {
|
|
|
|
setError(encryptedfile.errorString());
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encryptedfile.write(gpgbuffer, gpgbuffersize) != gpgbuffersize) {
|
|
|
|
setError(encryptedfile.errorString());
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// qDebug() << Q_FUNC_INFO << "sign" << gpgbuffer;
|
|
|
|
|
|
|
|
gpgme_free(gpgbuffer);
|
|
|
|
gpgme_data_release(gpgindata);
|
2022-03-14 15:56:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KGPG::VerifyMode: {
|
2022-03-14 17:35:00 +02:00
|
|
|
if (m_keys.isEmpty()) {
|
|
|
|
setError("No key available");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:56:34 +02:00
|
|
|
setError("Not implemented");
|
|
|
|
break;
|
|
|
|
}
|
2022-03-14 15:11:34 +02:00
|
|
|
default: {
|
2022-03-14 15:56:34 +02:00
|
|
|
Q_ASSERT(false);
|
2022-03-14 15:11:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 17:19:33 +02:00
|
|
|
|
|
|
|
m_ui.progressbar->setMinimum(1);
|
|
|
|
m_ui.progressbar->setMaximum(100);
|
|
|
|
m_ui.progressbar->setVisible(false);
|
|
|
|
m_ui.startbutton->setVisible(true);
|
|
|
|
m_ui.statusbar->showMessage("Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
void KGPG::slotStart()
|
|
|
|
{
|
|
|
|
start();
|
2022-03-14 15:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 15:56:34 +02:00
|
|
|
void KGPG::updateKeys(const gpgme_keylist_mode_t gpgmode, const bool secret)
|
|
|
|
{
|
|
|
|
m_keys.clear();
|
|
|
|
m_ui.keysbox->clear();
|
|
|
|
|
|
|
|
gpgme_error_t gpgresult = gpgme_set_keylist_mode(m_gpgctx, gpgmode);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// required by key query
|
|
|
|
gpgresult = gpgme_op_keylist_start(m_gpgctx, NULL, secret);
|
|
|
|
if (gpgresult != 0) {
|
|
|
|
setError(gpgme_strerror(gpgresult));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t gpgkey;
|
|
|
|
gpgresult = gpgme_op_keylist_next(m_gpgctx, &gpgkey);
|
|
|
|
while (gpgresult == 0) {
|
|
|
|
gpgme_user_id_t gpguid = NULL;
|
|
|
|
for (gpguid = gpgkey->uids; gpguid; gpguid = gpguid->next) {
|
|
|
|
KGPGKey kgpgkey;
|
|
|
|
kgpgkey.name = gpguid->name;
|
|
|
|
kgpgkey.email = gpguid->email;
|
|
|
|
kgpgkey.comment = gpguid->comment;
|
|
|
|
kgpgkey.uidhash = gpguid->uidhash;
|
|
|
|
kgpgkey.fpr = gpgkey->fpr;
|
|
|
|
kgpgkey.disabled = gpgkey->disabled;
|
|
|
|
kgpgkey.revoked = gpgkey->revoked;
|
|
|
|
kgpgkey.expired = gpgkey->expired;
|
|
|
|
kgpgkey.canencrypt = gpgkey->can_encrypt;
|
|
|
|
kgpgkey.cansign = gpgkey->can_sign;
|
|
|
|
m_keys.append(kgpgkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_unref(gpgkey);
|
|
|
|
gpgresult = gpgme_op_keylist_next(m_gpgctx, &gpgkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const KGPGKey &kgpgkey, m_keys) {
|
|
|
|
m_ui.keysbox->addItem(kgpgkey.uidhash);
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug() << Q_FUNC_INFO << m_keys.size();
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:42:33 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
KAboutData aboutData("kgpg", 0, ki18n("KGPG"),
|
|
|
|
"1.0.0", ki18n("KDE encryption and decryption utility"), KAboutData::License_GPL,
|
|
|
|
ki18n("(c) 2022 Ivailo Monev"));
|
|
|
|
aboutData.addAuthor(ki18n("Ivailo Monev"), KLocalizedString(), "xakepa10@gmail.com");
|
|
|
|
|
|
|
|
KCmdLineArgs::init(argc, argv, &aboutData);
|
2022-03-14 15:11:34 +02:00
|
|
|
KCmdLineOptions options;
|
|
|
|
options.add("encrypt <url>", ki18n("Encrypt the specified URL"));
|
|
|
|
options.add("decrypt <url>", ki18n("Decrypt the specified URL"));
|
|
|
|
options.add("sign <url>", ki18n("Sign the specified URL"));
|
|
|
|
options.add("verify <url>", ki18n("Verify the specified URL"));
|
|
|
|
KCmdLineArgs::addCmdLineOptions(options);
|
2022-03-14 13:42:33 +02:00
|
|
|
|
|
|
|
KApplication app;
|
|
|
|
|
|
|
|
KGPG* kgpg = new KGPG();
|
2022-03-14 15:11:34 +02:00
|
|
|
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
|
|
|
|
if (args->isSet("encrypt")) {
|
|
|
|
kgpg->setMode(KGPG::EncryptMode);
|
2022-03-14 17:19:33 +02:00
|
|
|
kgpg->setSource(args->getOption("encrypt"));
|
2022-03-14 15:11:34 +02:00
|
|
|
} else if (args->isSet("decrypt")) {
|
|
|
|
kgpg->setMode(KGPG::DecryptMode);
|
2022-03-14 17:19:33 +02:00
|
|
|
kgpg->setSource(args->getOption("decrypt"));
|
2022-03-14 15:11:34 +02:00
|
|
|
} else if (args->isSet("sign")) {
|
|
|
|
kgpg->setMode(KGPG::SignMode);
|
2022-03-14 17:35:00 +02:00
|
|
|
kgpg->setSource(args->getOption("sign"));
|
2022-03-14 15:11:34 +02:00
|
|
|
} else if (args->isSet("verify")) {
|
|
|
|
kgpg->setMode(KGPG::VerifyMode);
|
2022-03-14 17:19:33 +02:00
|
|
|
kgpg->setSource(args->getOption("verify"));
|
2022-03-14 15:11:34 +02:00
|
|
|
} else {
|
|
|
|
kgpg->setMode(KGPG::EncryptMode);
|
|
|
|
}
|
2022-03-14 13:42:33 +02:00
|
|
|
kgpg->show();
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_kgpg.cpp"
|