Compare commits

...

3 Commits

6 changed files with 38 additions and 45 deletions

View File

@ -57,5 +57,6 @@ const QString kGnuPGInfoGatheringModuleID =
"com.bktus.gpgfrontend.module.gnupg_info_gathering"; "com.bktus.gpgfrontend.module.gnupg_info_gathering";
const QString kVersionCheckingModuleID = const QString kVersionCheckingModuleID =
"com.bktus.gpgfrontend.module.version_checking"; "com.bktus.gpgfrontend.module.version_checking";
const QString kPinentryModuleID = "com.bktus.gpgfrontend.module.pinentry";
} // namespace GpgFrontend } // namespace GpgFrontend

View File

@ -93,31 +93,24 @@ class GpgContext::Impl {
static auto TestPassphraseCb(void *opaque, const char *uid_hint, static auto TestPassphraseCb(void *opaque, const char *uid_hint,
const char *passphrase_info, int last_was_bad, const char *passphrase_info, int last_was_bad,
int fd) -> gpgme_error_t { int fd) -> gpgme_error_t {
size_t res; QString passphrase = "abcdefg";
#ifdef QT5_BUILD auto pass_bytes = passphrase.toLatin1();
QString pass_qstr = "abcdefg\n"; auto pass_size = pass_bytes.size();
QByteArray pass = pass_qstr.toUtf8(); const auto *p_pass_bytes = pass_bytes.constData();
#else
QString pass = "abcdefg\n";
#endif
auto passphrase_size = pass.size(); ssize_t res = 0;
size_t off = 0; if (pass_size > 0) {
ssize_t off = 0;
do { ssize_t ret = 0;
#ifdef QT5_BUILD do {
const char *p_pass = pass.data(); ret = gpgme_io_write(fd, &p_pass_bytes[off], pass_size - off);
res = gpgme_io_write(fd, &p_pass[off], passpahrase_size - off); if (ret > 0) off += ret;
#else } while (ret > 0 && off != pass_size);
res = gpgme_io_write(fd, &pass[off], passphrase_size - off); res = off;
#endif }
if (res > 0) off += res;
} while (res > 0 && static_cast<long long>(off) != passphrase_size);
res += gpgme_io_write(fd, "\n", 1); res += gpgme_io_write(fd, "\n", 1);
return static_cast<long long>(res) == (passphrase_size + 1) return res == pass_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED);
? 0
: gpgme_error_from_errno(GPG_ERR_CANCELED);
} }
static auto CustomPassphraseCb(void *hook, const char *uid_hint, static auto CustomPassphraseCb(void *hook, const char *uid_hint,
@ -145,12 +138,11 @@ class GpgContext::Impl {
"REQUEST_PIN_ENTRY", "REQUEST_PIN_ENTRY",
{{"uid_hint", uid_hint != nullptr ? uid_hint : ""}, {{"uid_hint", uid_hint != nullptr ? uid_hint : ""},
{"passphrase_info", passphrase_info != nullptr ? passphrase_info : ""}, {"passphrase_info", passphrase_info != nullptr ? passphrase_info : ""},
{"prev_was_bad", prev_was_bad ? "1" : "0"}, {"prev_was_bad", (prev_was_bad != 0) ? "1" : "0"},
{"ask_for_new", ask_for_new ? "1" : "0"}}, {"ask_for_new", ask_for_new ? "1" : "0"}},
[&passphrase, &looper](Module::EventIdentifier i, [&passphrase, &looper](Module::EventIdentifier i,
Module::Event::ListenerIdentifier ei, Module::Event::ListenerIdentifier ei,
Module::Event::Params p) { Module::Event::Params p) {
qCWarning(core) << "REQUEST_PIN_ENTRY callback: " << i << ei << p;
passphrase = p["passphrase"]; passphrase = p["passphrase"];
looper.quit(); looper.quit();
}); });
@ -158,24 +150,23 @@ class GpgContext::Impl {
looper.exec(); looper.exec();
ResetCacheValue("PinentryContext"); ResetCacheValue("PinentryContext");
auto passphrase_size = passphrase.size(); auto pass_bytes = passphrase.toLatin1();
qCWarning(core, "get passphrase from pinentry size: %lld", passphrase_size); auto pass_size = pass_bytes.size();
const auto *p_pass_bytes = pass_bytes.constData();
size_t res = 0; ssize_t res = 0;
if (passphrase_size > 0) { if (pass_size > 0) {
size_t off = 0; ssize_t off = 0;
ssize_t ret = 0;
do { do {
res = gpgme_io_write(fd, &passphrase[off], passphrase_size - off); ret = gpgme_io_write(fd, &p_pass_bytes[off], pass_size - off);
if (res > 0) off += res; if (ret > 0) off += ret;
} while (res > 0 && off != passphrase_size); } while (ret > 0 && off != pass_size);
res = off;
} }
res += gpgme_io_write(fd, "\n", 1); res += gpgme_io_write(fd, "\n", 1);
return res == pass_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED);
qCDebug(core, "custom passphrase cd is about to return, res: %ld", res);
return res == passphrase_size + 1
? 0
: gpgme_error_from_errno(GPG_ERR_CANCELED);
} }
static auto TestStatusCb(void *hook, const char *keyword, static auto TestStatusCb(void *hook, const char *keyword,
@ -280,7 +271,8 @@ class GpgContext::Impl {
qCWarning(core, "set passphrase cb failed, test"); qCWarning(core, "set passphrase cb failed, test");
return false; return false;
}; };
} else if (!args_.use_pinentry) { } else if (!args_.use_pinentry &&
Module::IsModuleActivate(kPinentryModuleID)) {
if (!SetPassphraseCb(ctx, CustomPassphraseCb)) { if (!SetPassphraseCb(ctx, CustomPassphraseCb)) {
qCDebug(core, "set passphrase cb failed, custom"); qCDebug(core, "set passphrase cb failed, custom");
return false; return false;

View File

@ -49,11 +49,11 @@ auto GpgKeyImportExporter::ImportKey(const GFBuffer& in_buffer)
if (in_buffer.Empty()) return {}; if (in_buffer.Empty()) return {};
GpgData data_in(in_buffer); GpgData data_in(in_buffer);
auto err = CheckGpgError(gpgme_op_import(ctx_.DefaultContext(), data_in)); auto err = CheckGpgError(gpgme_op_import(ctx_.BinaryContext(), data_in));
if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) return {}; if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) return {};
gpgme_import_result_t result; gpgme_import_result_t result;
result = gpgme_op_import_result(ctx_.DefaultContext()); result = gpgme_op_import_result(ctx_.BinaryContext());
gpgme_import_status_t status = result->imports; gpgme_import_status_t status = result->imports;
auto import_info = SecureCreateSharedObject<GpgImportInformation>(result); auto import_info = SecureCreateSharedObject<GpgImportInformation>(result);
while (status != nullptr) { while (status != nullptr) {

View File

@ -264,7 +264,7 @@ void CommonUtils::RaiseFailureMessageBox(QWidget *parent, GpgError err) {
.arg(desc.second)); .arg(desc.second));
} }
void CommonUtils::SlotImportKeys(QWidget *parent, const QString &in_buffer) { void CommonUtils::SlotImportKeys(QWidget *parent, const QByteArray &in_buffer) {
auto info = auto info =
GpgKeyImportExporter::GetInstance().ImportKey(GFBuffer(in_buffer)); GpgKeyImportExporter::GetInstance().ImportKey(GFBuffer(in_buffer));
emit SignalKeyStatusUpdated(); emit SignalKeyStatusUpdated();
@ -310,7 +310,7 @@ void CommonUtils::SlotImportKeyFromKeyServer(QWidget *parent) {
void CommonUtils::SlotImportKeyFromClipboard(QWidget *parent) { void CommonUtils::SlotImportKeyFromClipboard(QWidget *parent) {
QClipboard *cb = QApplication::clipboard(); QClipboard *cb = QApplication::clipboard();
SlotImportKeys(parent, cb->text(QClipboard::Clipboard)); SlotImportKeys(parent, cb->text(QClipboard::Clipboard).toLatin1());
} }
void CommonUtils::SlotExecuteCommand( void CommonUtils::SlotExecuteCommand(

View File

@ -215,7 +215,7 @@ class CommonUtils : public QWidget {
void SignalRestartApplication(int); void SignalRestartApplication(int);
/** /**
* @brief * @brief
* *
*/ */
void SignalFavoritesChanged(); void SignalFavoritesChanged();
@ -228,7 +228,7 @@ class CommonUtils : public QWidget {
* @param parent * @param parent
* @param in_buffer * @param in_buffer
*/ */
void SlotImportKeys(QWidget* parent, const QString& in_buffer); void SlotImportKeys(QWidget* parent, const QByteArray& in_buffer);
/** /**
* @brief * @brief

View File

@ -52,7 +52,7 @@ void MainWindow::slot_start_wizard() {
void MainWindow::slot_import_key_from_edit() { void MainWindow::slot_import_key_from_edit() {
if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return; if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return;
CommonUtils::GetInstance()->SlotImportKeys( CommonUtils::GetInstance()->SlotImportKeys(
this, edit_->CurTextPage()->GetTextPage()->toPlainText()); this, edit_->CurTextPage()->GetTextPage()->toPlainText().toLatin1());
} }
void MainWindow::slot_open_key_management() { void MainWindow::slot_open_key_management() {