diff --git a/.gitignore b/.gitignore index a6b9b5a..23f7f16 100644 --- a/.gitignore +++ b/.gitignore @@ -1,48 +1,50 @@ *.lo *.o .deps/ .libs/ /aclocal.m4 /autom4te.cache /config.h.in /config.h /config.log /config.status /configure /Makefile.in /VERSION autom4te.cache/ assuan/Makefile.in assuan/Makefile curses/Makefile.in curses/Makefile doc/Makefile.in doc/Makefile doc/pinentry.info doc/stamp-vti doc/version.texi efl/Makefile.in efl/Makefile fltk/Makefile.in fltk/Makefile gtk+-2/Makefile.in gtk+-2/Makefile gnome3/Makefile.in gnome3/Makefile pinentry/Makefile.in pinentry/Makefile qt/Makefile.in qt/Makefile +qt/icons/Makefile.in +qt/icons/Makefile tqt/Makefile.in tqt/Makefile secmem/Makefile.in secmem/Makefile w32/Makefile.in w32/Makefile tty/Makefile.in tty/Makefile /qt/pinentryconfirm.moc /qt/pinentrydialog.moc /qt/qsecurelineedit.moc /m4/Makefile.in /emacs/Makefile.in diff --git a/qt/icons/Makefile.am b/qt/icons/Makefile.am index c9e610d..49cef36 100644 --- a/qt/icons/Makefile.am +++ b/qt/icons/Makefile.am @@ -1,23 +1,26 @@ # Makefile.am # Copyright (C) 2022 g10 Code GmbH # # This file is part of PINENTRY. # # PINENTRY is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # PINENTRY 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . # SPDX-License-Identifier: GPL-2.0+ ## Process this file with automake to produce Makefile.in -EXTRA_DIST = document-encrypt.png hint.svg password-generate.svg \ +EXTRA_DIST = data-error.svg \ + document-encrypt.png \ + hint.svg \ + password-generate.svg \ visibility.svg diff --git a/qt/icons/data-error.svg b/qt/icons/data-error.svg new file mode 100644 index 0000000..6fc3137 --- /dev/null +++ b/qt/icons/data-error.svg @@ -0,0 +1,9 @@ + + + + + diff --git a/qt/main.cpp b/qt/main.cpp index 81b48a3..8236ac6 100644 --- a/qt/main.cpp +++ b/qt/main.cpp @@ -1,426 +1,426 @@ /* main.cpp - A Qt dialog for PIN entry. * Copyright (C) 2002, 2008 Klarälvdalens Datakonsult AB (KDAB) * Copyright (C) 2003, 2021 g10 Code GmbH * Copyright 2007 Ingo Klöcker * * Written by Steffen Hansen . * Modified by Marcus Brinkmann . * Modified by Marc Mutz * Software engineering by Ingo Klöcker * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . * SPDX-License-Identifier: GPL-2.0+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "accessibility.h" #include "pinentryconfirm.h" #include "pinentrydialog.h" #include "pinentry.h" #include "util.h" #include #include #include #include #include #include #include #if QT_VERSION >= 0x050000 #include #endif #include #include #include #include #ifdef FALLBACK_CURSES #include #endif #if QT_VERSION >= 0x050000 && defined(QT_STATIC) #include #ifdef Q_OS_WIN #include #include Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) #elif defined(Q_OS_MAC) Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin) #else Q_IMPORT_PLUGIN(QXcbIntegrationPlugin) #endif #endif #ifdef Q_OS_WIN #include #endif #include "pinentry_debug.h" static QString escape_accel(const QString &s) { QString result; result.reserve(s.size()); bool afterUnderscore = false; for (unsigned int i = 0, end = s.size() ; i != end ; ++i) { const QChar ch = s[i]; if (ch == QLatin1Char('_')) { if (afterUnderscore) { // escaped _ result += QLatin1Char('_'); afterUnderscore = false; } else { // accel afterUnderscore = true; } } else { if (afterUnderscore || // accel ch == QLatin1Char('&')) { // escape & from being interpreted by Qt result += QLatin1Char('&'); } result += ch; afterUnderscore = false; } } if (afterUnderscore) // trailing single underscore: shouldn't happen, but deal with it robustly: { result += QLatin1Char('_'); } return result; } namespace { class InvalidUtf8 : public std::invalid_argument { public: InvalidUtf8() : std::invalid_argument("invalid utf8") {} ~InvalidUtf8() throw() {} }; } static const bool GPG_AGENT_IS_PORTED_TO_ONLY_SEND_UTF8 = false; static QString from_utf8(const char *s) { const QString result = QString::fromUtf8(s); if (result.contains(QChar::ReplacementCharacter)) { if (GPG_AGENT_IS_PORTED_TO_ONLY_SEND_UTF8) { throw InvalidUtf8(); } else { return QString::fromLocal8Bit(s); } } return result; } static void setup_foreground_window(QWidget *widget, WId parentWid) { #if QT_VERSION >= 0x050000 /* For windows set the desktop window as the transient parent */ QWindow *parentWindow = nullptr; if (parentWid) { parentWindow = QWindow::fromWinId(parentWid); } #ifdef Q_OS_WIN if (!parentWindow) { HWND desktop = GetDesktopWindow(); if (desktop) { parentWindow = QWindow::fromWinId((WId) desktop); } } #endif if (parentWindow) { // Ensure that we have a native wid widget->winId(); QWindow *wndHandle = widget->windowHandle(); if (wndHandle) { wndHandle->setTransientParent(parentWindow); } } #endif widget->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint | Qt::WindowMinimizeButtonHint); } static int qt_cmd_handler(pinentry_t pe) { int want_pass = !!pe->pin; const QString ok = pe->ok ? escape_accel(from_utf8(pe->ok)) : pe->default_ok ? escape_accel(from_utf8(pe->default_ok)) : /* else */ QLatin1String("&OK") ; const QString cancel = pe->cancel ? escape_accel(from_utf8(pe->cancel)) : pe->default_cancel ? escape_accel(from_utf8(pe->default_cancel)) : /* else */ QLatin1String("&Cancel") ; unique_malloced_ptr str{pinentry_get_title(pe)}; const QString title = str ? from_utf8(str.get()) : /* else */ QLatin1String("pinentry-qt") ; const QString repeatError = pe->repeat_error_string ? from_utf8(pe->repeat_error_string) : QLatin1String("Passphrases do not match"); const QString repeatString = pe->repeat_passphrase ? from_utf8(pe->repeat_passphrase) : QString(); const QString visibilityTT = pe->default_tt_visi ? from_utf8(pe->default_tt_visi) : QLatin1String("Show passphrase"); const QString hideTT = pe->default_tt_hide ? from_utf8(pe->default_tt_hide) : QLatin1String("Hide passphrase"); const QString capsLockHint = pe->default_capshint ? from_utf8(pe->default_capshint) : QLatin1String("Caps Lock is on"); const QString generateLbl = pe->genpin_label ? from_utf8(pe->genpin_label) : QString(); const QString generateTT = pe->genpin_tt ? from_utf8(pe->genpin_tt) : QString(); if (want_pass) { PinEntryDialog pinentry(nullptr, 0, pe->timeout, true, !!pe->quality_bar, repeatString, visibilityTT, hideTT); setup_foreground_window(&pinentry, pe->parent_wid); pinentry.setPinentryInfo(pe); pinentry.setPrompt(escape_accel(from_utf8(pe->prompt))); pinentry.setDescription(from_utf8(pe->description)); pinentry.setRepeatErrorText(repeatError); pinentry.setGenpinLabel(generateLbl); pinentry.setGenpinTT(generateTT); pinentry.setCapsLockHint(capsLockHint); pinentry.setFormattedPassphrase({ bool(pe->formatted_passphrase), from_utf8(pe->formatted_passphrase_hint)}); pinentry.setConstraintsOptions({ bool(pe->constraints_enforce), from_utf8(pe->constraints_hint_short), from_utf8(pe->constraints_hint_long), from_utf8(pe->constraints_error_title) }); if (!title.isEmpty()) { pinentry.setWindowTitle(title); } /* If we reuse the same dialog window. */ pinentry.setPin(QString()); pinentry.setOkText(ok); pinentry.setCancelText(cancel); if (pe->error) { pinentry.setError(from_utf8(pe->error)); } if (pe->quality_bar) { pinentry.setQualityBar(from_utf8(pe->quality_bar)); } if (pe->quality_bar_tt) { pinentry.setQualityBarTT(from_utf8(pe->quality_bar_tt)); } bool ret = pinentry.exec(); if (!ret) { if (pinentry.timedOut()) pe->specific_err = gpg_error (GPG_ERR_TIMEOUT); return -1; } const QString pinStr = pinentry.pin(); QByteArray pin = pinStr.toUtf8(); if (!!pe->repeat_passphrase) { /* Should not have been possible to accept the dialog in that case but we do a safety check here */ pe->repeat_okay = (pinStr == pinentry.repeatedPin()); } int len = strlen(pin.constData()); if (len >= 0) { pinentry_setbufferlen(pe, len + 1); if (pe->pin) { strcpy(pe->pin, pin.constData()); return len; } } return -1; } else { const QString desc = pe->description ? from_utf8(pe->description) : QString(); const QString notok = pe->notok ? escape_accel(from_utf8(pe->notok)) : QString(); const QMessageBox::StandardButtons buttons = pe->one_button ? QMessageBox::Ok : pe->notok ? QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel : /* else */ QMessageBox::Ok | QMessageBox::Cancel ; PinentryConfirm box(QMessageBox::Information, pe->timeout, title, desc, buttons, nullptr); setup_foreground_window(&box, pe->parent_wid); const struct { QMessageBox::StandardButton button; QString label; } buttonLabels[] = { { QMessageBox::Ok, ok }, { QMessageBox::Yes, ok }, { QMessageBox::No, notok }, { QMessageBox::Cancel, cancel }, }; for (size_t i = 0 ; i < sizeof buttonLabels / sizeof * buttonLabels ; ++i) if ((buttons & buttonLabels[i].button) && !buttonLabels[i].label.isEmpty()) { box.button(buttonLabels[i].button)->setText(buttonLabels[i].label); Accessibility::setDescription(box.button(buttonLabels[i].button), buttonLabels[i].label); } - box.setIconPixmap(icon()); + box.setIconPixmap(applicationIconPixmap()); if (!pe->one_button) { box.setDefaultButton(QMessageBox::Cancel); } box.show(); raiseWindow(&box); const int rc = box.exec(); if (rc == QMessageBox::Cancel) { pe->canceled = true; } if (box.timedOut()) { pe->specific_err = gpg_error (GPG_ERR_TIMEOUT); } return rc == QMessageBox::Ok || rc == QMessageBox::Yes ; } } static int qt_cmd_handler_ex(pinentry_t pe) { try { return qt_cmd_handler(pe); } catch (const InvalidUtf8 &) { pe->locale_err = true; return pe->pin ? -1 : false ; } catch (...) { pe->canceled = true; return pe->pin ? -1 : false ; } } pinentry_cmd_handler_t pinentry_cmd_handler = qt_cmd_handler_ex; int main(int argc, char *argv[]) { pinentry_init("pinentry-qt"); QApplication *app = NULL; int new_argc = 0; #ifdef FALLBACK_CURSES #if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) // check a few environment variables that are usually set on X11 or Wayland sessions const bool hasWaylandDisplay = qEnvironmentVariableIsSet("WAYLAND_DISPLAY"); const bool isWaylandSessionType = qgetenv("XDG_SESSION_TYPE") == "wayland"; const bool hasX11Display = pinentry_have_display(argc, argv); const bool isX11SessionType = qgetenv("XDG_SESSION_TYPE") == "x11"; const bool isGUISession = hasWaylandDisplay || isWaylandSessionType || hasX11Display || isX11SessionType; qCDebug(PINENTRY_LOG) << "hasWaylandDisplay:" << hasWaylandDisplay; qCDebug(PINENTRY_LOG) << "isWaylandSessionType:" << isWaylandSessionType; qCDebug(PINENTRY_LOG) << "hasX11Display:" << hasX11Display; qCDebug(PINENTRY_LOG) << "isX11SessionType:" << isX11SessionType; qCDebug(PINENTRY_LOG) << "isGUISession:" << isGUISession; #else const bool isGUISession = pinentry_have_display(argc, argv); #endif if (!isGUISession) { pinentry_cmd_handler = curses_cmd_handler; pinentry_set_flavor_flag ("curses"); } else #endif { /* Qt does only understand -display but not --display; thus we are fixing that here. The code is pretty simply and may get confused if an argument is called "--display". */ char **new_argv, *p; size_t n; int i, done; for (n = 0, i = 0; i < argc; i++) { n += strlen(argv[i]) + 1; } n++; new_argv = (char **)calloc(argc + 1, sizeof * new_argv); if (new_argv) { *new_argv = (char *)malloc(n); } if (!new_argv || !*new_argv) { fprintf(stderr, "pinentry-qt: can't fixup argument list: %s\n", strerror(errno)); exit(EXIT_FAILURE); } for (done = 0, p = *new_argv, i = 0; i < argc; i++) if (!done && !strcmp(argv[i], "--display")) { new_argv[i] = strcpy(p, argv[i] + 1); p += strlen(argv[i] + 1) + 1; done = 1; } else { new_argv[i] = strcpy(p, argv[i]); p += strlen(argv[i]) + 1; } /* Note: QApplication uses int &argc so argc has to be valid * for the full lifetime of the application. * * As Qt might modify argc / argv we use copies here so that * we do not loose options that are handled in both. e.g. display. */ new_argc = argc; Q_ASSERT (new_argc); app = new QApplication(new_argc, new_argv); app->setWindowIcon(QIcon(QLatin1String(":/icons/document-encrypt.png"))); } pinentry_parse_opts(argc, argv); int rc = pinentry_loop(); delete app; return rc ? EXIT_FAILURE : EXIT_SUCCESS ; } diff --git a/qt/pinentrydialog.cpp b/qt/pinentrydialog.cpp index c5874c0..94d7541 100644 --- a/qt/pinentrydialog.cpp +++ b/qt/pinentrydialog.cpp @@ -1,806 +1,805 @@ /* pinentrydialog.cpp - A (not yet) secure Qt 4 dialog for PIN entry. * Copyright (C) 2002, 2008 Klarälvdalens Datakonsult AB (KDAB) * Copyright 2007 Ingo Klöcker * Copyright 2016 Intevation GmbH * Copyright (C) 2021, 2022 g10 Code GmbH * * Written by Steffen Hansen . * Modified by Andre Heinecke * Software engineering by Ingo Klöcker * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . * SPDX-License-Identifier: GPL-2.0+ */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include "pinentrydialog.h" #include "accessibility.h" #include "capslock.h" #include "pinlineedit.h" #include "util.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #if QT_VERSION >= 0x050700 #include #endif #endif void raiseWindow(QWidget *w) { #ifdef Q_OS_WIN #if QT_VERSION >= 0x050700 QWindowsWindowFunctions::setWindowActivationBehavior( QWindowsWindowFunctions::AlwaysActivateWindow); #endif #endif w->setWindowState((w->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive); w->activateWindow(); w->raise(); } -QPixmap icon(QStyle::StandardPixmap which) +QPixmap applicationIconPixmap(const QIcon &overlayIcon) { QPixmap pm = qApp->windowIcon().pixmap(48, 48); - if (which != QStyle::SP_CustomBase) { - const QIcon ic = qApp->style()->standardIcon(which); + if (!overlayIcon.isNull()) { QPainter painter(&pm); const int emblemSize = 22; painter.drawPixmap(pm.width() - emblemSize, 0, - ic.pixmap(emblemSize, emblemSize)); + overlayIcon.pixmap(emblemSize, emblemSize)); } return pm; } namespace { class TextLabel : public QLabel { public: using QLabel::QLabel; protected: void focusInEvent(QFocusEvent *ev) override; }; void TextLabel::focusInEvent(QFocusEvent *ev) { QLabel::focusInEvent(ev); // if the text label gets focus, then select its text; this is a workaround // for missing focus indicators for labels in many Qt styles const Qt::FocusReason reason = ev->reason(); const auto isKeyboardFocusEvent = reason == Qt::TabFocusReason || reason == Qt::BacktabFocusReason || reason == Qt::ShortcutFocusReason; if (!text().isEmpty() && isKeyboardFocusEvent) { if (textFormat() == Qt::PlainText) { setSelection(0, text().size()); } else if (textFormat() == Qt::RichText) { // unfortunately, there is no selectAll(); therefore, we need // to determine the "visual" length of the text by stripping // the label's text of all formatting information QTextDocument temp; temp.setHtml(text()); setSelection(0, temp.toRawText().size()); } else { qDebug() << "Label with unsupported text format" << textFormat() << "got focus"; } } } } void PinEntryDialog::slotTimeout() { _timed_out = true; reject(); } PinEntryDialog::PinEntryDialog(QWidget *parent, const char *name, int timeout, bool modal, bool enable_quality_bar, const QString &repeatString, const QString &visibilityTT, const QString &hideTT) : QDialog{parent} , _have_quality_bar{enable_quality_bar} , mVisibilityTT{visibilityTT} , mHideTT{hideTT} { Q_UNUSED(name) if (modal) { setWindowModality(Qt::ApplicationModal); } QPalette redTextPalette; redTextPalette.setColor(QPalette::WindowText, Qt::red); auto *const mainLayout = new QVBoxLayout{this}; auto *const hbox = new QHBoxLayout; _icon = new QLabel(this); - _icon->setPixmap(icon()); + _icon->setPixmap(applicationIconPixmap()); hbox->addWidget(_icon, 0, Qt::AlignVCenter | Qt::AlignLeft); auto *const grid = new QGridLayout; int row = 1; _error = new TextLabel{this}; _error->setTextFormat(Qt::PlainText); _error->setTextInteractionFlags(Qt::TextSelectableByMouse); _error->setPalette(redTextPalette); _error->hide(); grid->addWidget(_error, row, 1, 1, 2); row++; _desc = new TextLabel{this}; _desc->setTextFormat(Qt::PlainText); _desc->setTextInteractionFlags(Qt::TextSelectableByMouse); _desc->hide(); grid->addWidget(_desc, row, 1, 1, 2); row++; mCapsLockHint = new TextLabel{this}; mCapsLockHint->setTextFormat(Qt::PlainText); mCapsLockHint->setTextInteractionFlags(Qt::TextSelectableByMouse); mCapsLockHint->setPalette(redTextPalette); mCapsLockHint->setAlignment(Qt::AlignCenter); mCapsLockHint->setVisible(false); grid->addWidget(mCapsLockHint, row, 1, 1, 2); row++; { _prompt = new QLabel(this); _prompt->setTextFormat(Qt::PlainText); _prompt->setTextInteractionFlags(Qt::TextSelectableByMouse); _prompt->hide(); grid->addWidget(_prompt, row, 1); const auto l = new QHBoxLayout; _edit = new PinLineEdit(this); _edit->setMaxLength(256); _edit->setMinimumWidth(_edit->fontMetrics().averageCharWidth()*20 + 48); _edit->setEchoMode(QLineEdit::Password); _prompt->setBuddy(_edit); l->addWidget(_edit, 1); if (!repeatString.isNull()) { mGenerateButton = new QPushButton{this}; mGenerateButton->setIcon(QIcon(QLatin1String(":/icons/password-generate"))); mGenerateButton->setVisible(false); l->addWidget(mGenerateButton); } grid->addLayout(l, row, 2); } /* Set up the show password action */ const QIcon visibilityIcon = QIcon(QLatin1String(":/icons/visibility.svg")); const QIcon hideIcon = QIcon(QLatin1String(":/icons/hint.svg")); #if QT_VERSION >= 0x050200 if (!visibilityIcon.isNull() && !hideIcon.isNull()) { mVisiActionEdit = _edit->addAction(visibilityIcon, QLineEdit::TrailingPosition); mVisiActionEdit->setVisible(false); mVisiActionEdit->setToolTip(mVisibilityTT); } else #endif { if (!mVisibilityTT.isNull()) { row++; mVisiCB = new QCheckBox{mVisibilityTT, this}; grid->addWidget(mVisiCB, row, 1, 1, 2, Qt::AlignLeft); } } row++; mConstraintsHint = new TextLabel{this}; mConstraintsHint->setTextFormat(Qt::PlainText); mConstraintsHint->setTextInteractionFlags(Qt::TextSelectableByMouse); mConstraintsHint->setVisible(false); grid->addWidget(mConstraintsHint, row, 2); row++; mFormattedPassphraseHintSpacer = new QLabel{this}; mFormattedPassphraseHintSpacer->setVisible(false); mFormattedPassphraseHint = new TextLabel{this}; mFormattedPassphraseHint->setTextFormat(Qt::PlainText); mFormattedPassphraseHint->setTextInteractionFlags(Qt::TextSelectableByMouse); mFormattedPassphraseHint->setVisible(false); grid->addWidget(mFormattedPassphraseHintSpacer, row, 1); grid->addWidget(mFormattedPassphraseHint, row, 2); if (!repeatString.isNull()) { row++; auto repeatLabel = new QLabel{this}; repeatLabel->setTextFormat(Qt::PlainText); repeatLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); repeatLabel->setText(repeatString); grid->addWidget(repeatLabel, row, 1); mRepeat = new PinLineEdit(this); mRepeat->setMaxLength(256); mRepeat->setEchoMode(QLineEdit::Password); repeatLabel->setBuddy(mRepeat); grid->addWidget(mRepeat, row, 2); row++; mRepeatError = new TextLabel{this}; mRepeatError->setTextFormat(Qt::PlainText); mRepeatError->setTextInteractionFlags(Qt::TextSelectableByMouse); mRepeatError->setPalette(redTextPalette); mRepeatError->hide(); grid->addWidget(mRepeatError, row, 2); } if (enable_quality_bar) { row++; _quality_bar_label = new QLabel(this); _quality_bar_label->setTextFormat(Qt::PlainText); _quality_bar_label->setTextInteractionFlags(Qt::TextSelectableByMouse); _quality_bar_label->setAlignment(Qt::AlignVCenter); grid->addWidget(_quality_bar_label, row, 1); _quality_bar = new QProgressBar(this); _quality_bar->setAlignment(Qt::AlignCenter); _quality_bar_label->setBuddy(_quality_bar); grid->addWidget(_quality_bar, row, 2); } hbox->addLayout(grid, 1); mainLayout->addLayout(hbox); QDialogButtonBox *const buttons = new QDialogButtonBox(this); buttons->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); _ok = buttons->button(QDialogButtonBox::Ok); _cancel = buttons->button(QDialogButtonBox::Cancel); if (style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons)) { _ok->setIcon(style()->standardIcon(QStyle::SP_DialogOkButton)); _cancel->setIcon(style()->standardIcon(QStyle::SP_DialogCancelButton)); } mainLayout->addStretch(1); mainLayout->addWidget(buttons); mainLayout->setSizeConstraint(QLayout::SetFixedSize); if (timeout > 0) { _timer = new QTimer(this); connect(_timer, &QTimer::timeout, this, &PinEntryDialog::slotTimeout); _timer->start(timeout * 1000); } connect(buttons, &QDialogButtonBox::accepted, this, &PinEntryDialog::onAccept); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(_edit, &QLineEdit::textChanged, this, &PinEntryDialog::updateQuality); connect(_edit, &QLineEdit::textChanged, this, &PinEntryDialog::textChanged); connect(_edit, &PinLineEdit::backspacePressed, this, &PinEntryDialog::onBackspace); if (mGenerateButton) { connect(mGenerateButton, &QPushButton::clicked, this, &PinEntryDialog::generatePin); } if (mVisiActionEdit) { connect(mVisiActionEdit, &QAction::triggered, this, &PinEntryDialog::toggleVisibility); } if (mVisiCB) { connect(mVisiCB, &QCheckBox::toggled, this, &PinEntryDialog::toggleVisibility); } if (mRepeat) { connect(mRepeat, &QLineEdit::textChanged, this, &PinEntryDialog::textChanged); } auto capsLockWatcher = new CapsLockWatcher{this}; connect(capsLockWatcher, &CapsLockWatcher::stateChanged, this, [this] (bool locked) { mCapsLockHint->setVisible(locked); }); connect(qApp, &QApplication::focusChanged, this, &PinEntryDialog::focusChanged); connect(qApp, &QApplication::applicationStateChanged, this, &PinEntryDialog::checkCapsLock); checkCapsLock(); #ifndef QT_NO_ACCESSIBILITY QAccessible::installActivationObserver(this); accessibilityActiveChanged(QAccessible::isActive()); #endif #if QT_VERSION >= 0x050000 /* This is mostly an issue on Windows where this results in the pinentry popping up nicely with an animation and comes to front. It is not ifdefed for Windows only since window managers on Linux like KWin can also have this result in an animation when the pinentry is shown and not just popping it up. */ if (qApp->platformName() != QLatin1String("wayland")) { setWindowState(Qt::WindowMinimized); QTimer::singleShot(0, this, [this] () { raiseWindow(this); }); } #else activateWindow(); raise(); #endif } PinEntryDialog::~PinEntryDialog() { #ifndef QT_NO_ACCESSIBILITY QAccessible::removeActivationObserver(this); #endif } void PinEntryDialog::keyPressEvent(QKeyEvent *e) { const auto returnPressed = (!e->modifiers() && (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)) || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter); if (returnPressed && _edit->hasFocus() && mRepeat) { // if the user pressed Return in the first input field, then move the // focus to the repeat input field and prevent further event processing // by QDialog (which would trigger the default button) mRepeat->setFocus(); e->ignore(); return; } QDialog::keyPressEvent(e); } void PinEntryDialog::keyReleaseEvent(QKeyEvent *event) { QDialog::keyReleaseEvent(event); checkCapsLock(); } void PinEntryDialog::showEvent(QShowEvent *event) { QDialog::showEvent(event); _edit->setFocus(); } void PinEntryDialog::setDescription(const QString &txt) { _desc->setVisible(!txt.isEmpty()); _desc->setText(txt); Accessibility::setDescription(_desc, txt); - _icon->setPixmap(icon()); + _icon->setPixmap(applicationIconPixmap()); setError(QString()); } QString PinEntryDialog::description() const { return _desc->text(); } void PinEntryDialog::setError(const QString &txt) { if (!txt.isNull()) { - _icon->setPixmap(icon(QStyle::SP_MessageBoxCritical)); + _icon->setPixmap(applicationIconPixmap(QIcon{QStringLiteral(":/icons/data-error.svg")})); } _error->setText(txt); Accessibility::setDescription(_error, txt); _error->setVisible(!txt.isEmpty()); } QString PinEntryDialog::error() const { return _error->text(); } void PinEntryDialog::setPin(const QString &txt) { _edit->setPin(txt); } QString PinEntryDialog::pin() const { return _edit->pin(); } void PinEntryDialog::setPrompt(const QString &txt) { _prompt->setText(txt); _prompt->setVisible(!txt.isEmpty()); if (txt.contains("PIN")) _disable_echo_allowed = false; } QString PinEntryDialog::prompt() const { return _prompt->text(); } void PinEntryDialog::setOkText(const QString &txt) { _ok->setText(txt); Accessibility::setDescription(_ok, txt); _ok->setVisible(!txt.isEmpty()); } void PinEntryDialog::setCancelText(const QString &txt) { _cancel->setText(txt); Accessibility::setDescription(_cancel, txt); _cancel->setVisible(!txt.isEmpty()); } void PinEntryDialog::setQualityBar(const QString &txt) { if (_have_quality_bar) { _quality_bar_label->setText(txt); Accessibility::setDescription(_quality_bar_label, txt); } } void PinEntryDialog::setQualityBarTT(const QString &txt) { if (_have_quality_bar) { _quality_bar->setToolTip(txt); } } void PinEntryDialog::setGenpinLabel(const QString &txt) { if (!mGenerateButton) { return; } mGenerateButton->setVisible(!txt.isEmpty()); if (!txt.isEmpty()) { Accessibility::setName(mGenerateButton, txt); } } void PinEntryDialog::setGenpinTT(const QString &txt) { if (mGenerateButton) { mGenerateButton->setToolTip(txt); } } void PinEntryDialog::setCapsLockHint(const QString &txt) { mCapsLockHint->setText(txt); } void PinEntryDialog::setFormattedPassphrase(const PinEntryDialog::FormattedPassphraseOptions &options) { mFormatPassphrase = options.formatPassphrase; mFormattedPassphraseHint->setTextFormat(Qt::RichText); mFormattedPassphraseHint->setText(QLatin1String("") + options.hint.toHtmlEscaped() + QLatin1String("")); Accessibility::setName(mFormattedPassphraseHint, options.hint); toggleFormattedPassphrase(); } void PinEntryDialog::setConstraintsOptions(const ConstraintsOptions &options) { mEnforceConstraints = options.enforce; mConstraintsHint->setText(options.shortHint); if (!options.longHint.isEmpty()) { mConstraintsHint->setToolTip(QLatin1String("") + options.longHint.toHtmlEscaped().replace(QLatin1String("\n\n"), QLatin1String("
")) + QLatin1String("")); Accessibility::setDescription(mConstraintsHint, options.longHint); } mConstraintsErrorTitle = options.errorTitle; mConstraintsHint->setVisible(mEnforceConstraints && !options.shortHint.isEmpty()); } void PinEntryDialog::toggleFormattedPassphrase() { const bool enableFormatting = mFormatPassphrase && _edit->echoMode() == QLineEdit::Normal; _edit->setFormattedPassphrase(enableFormatting); if (mRepeat) { mRepeat->setFormattedPassphrase(enableFormatting); const bool hintAboutToBeHidden = mFormattedPassphraseHint->isVisible() && !enableFormatting; if (hintAboutToBeHidden) { // set hint spacer to current height of hint label before hiding the hint mFormattedPassphraseHintSpacer->setMinimumHeight(mFormattedPassphraseHint->height()); mFormattedPassphraseHintSpacer->setVisible(true); } else if (enableFormatting) { mFormattedPassphraseHintSpacer->setVisible(false); } mFormattedPassphraseHint->setVisible(enableFormatting); } } void PinEntryDialog::onBackspace() { cancelTimeout(); if (_disable_echo_allowed) { _edit->setEchoMode(QLineEdit::NoEcho); if (mRepeat) { mRepeat->setEchoMode(QLineEdit::NoEcho); } } } void PinEntryDialog::updateQuality(const QString &txt) { int length; int percent; QPalette pal; _disable_echo_allowed = false; if (!_have_quality_bar || !_pinentry_info) { return; } const QByteArray utf8_pin = txt.toUtf8(); const char *pin = utf8_pin.constData(); length = strlen(pin); percent = length ? pinentry_inq_quality(_pinentry_info, pin, length) : 0; if (!length) { _quality_bar->reset(); } else { pal = _quality_bar->palette(); if (percent < 0) { pal.setColor(QPalette::Highlight, QColor("red")); percent = -percent; } else { pal.setColor(QPalette::Highlight, QColor("green")); } _quality_bar->setPalette(pal); _quality_bar->setValue(percent); } } void PinEntryDialog::setPinentryInfo(pinentry_t peinfo) { _pinentry_info = peinfo; } void PinEntryDialog::focusChanged(QWidget *old, QWidget *now) { // Grab keyboard. It might be a little weird to do it here, but it works! // Previously this code was in showEvent, but that did not work in Qt4. if (!_pinentry_info || _pinentry_info->grab) { if (_grabbed && old && (old == _edit || old == mRepeat)) { old->releaseKeyboard(); _grabbed = false; } if (!_grabbed && now && (now == _edit || now == mRepeat)) { now->grabKeyboard(); _grabbed = true; } } } void PinEntryDialog::textChanged(const QString &text) { Q_UNUSED(text); cancelTimeout(); if (mVisiActionEdit && sender() == _edit) { mVisiActionEdit->setVisible(!_edit->pin().isEmpty()); } if (mGenerateButton) { mGenerateButton->setVisible( _edit->pin().isEmpty() #ifndef QT_NO_ACCESSIBILITY && !mGenerateButton->accessibleName().isEmpty() #endif ); } } void PinEntryDialog::generatePin() { unique_malloced_ptr pin{pinentry_inq_genpin(_pinentry_info)}; if (pin) { if (_edit->echoMode() == QLineEdit::Password) { if (mVisiActionEdit) { mVisiActionEdit->trigger(); } if (mVisiCB) { mVisiCB->setChecked(true); } } const auto pinStr = QString::fromUtf8(pin.get()); _edit->setPin(pinStr); mRepeat->setPin(pinStr); // explicitly focus the first input field and select the generated password _edit->setFocus(); _edit->selectAll(); } } void PinEntryDialog::toggleVisibility() { if (sender() != mVisiCB) { if (_edit->echoMode() == QLineEdit::Password) { if (mVisiActionEdit) { mVisiActionEdit->setIcon(QIcon(QLatin1String(":/icons/hint.svg"))); mVisiActionEdit->setToolTip(mHideTT); } _edit->setEchoMode(QLineEdit::Normal); if (mRepeat) { mRepeat->setEchoMode(QLineEdit::Normal); } } else { if (mVisiActionEdit) { mVisiActionEdit->setIcon(QIcon(QLatin1String(":/icons/visibility.svg"))); mVisiActionEdit->setToolTip(mVisibilityTT); } _edit->setEchoMode(QLineEdit::Password); if (mRepeat) { mRepeat->setEchoMode(QLineEdit::Password); } } } else { if (mVisiCB->isChecked()) { if (mRepeat) { mRepeat->setEchoMode(QLineEdit::Normal); } _edit->setEchoMode(QLineEdit::Normal); } else { if (mRepeat) { mRepeat->setEchoMode(QLineEdit::Password); } _edit->setEchoMode(QLineEdit::Password); } } toggleFormattedPassphrase(); } QString PinEntryDialog::repeatedPin() const { if (mRepeat) { return mRepeat->pin(); } return QString(); } bool PinEntryDialog::timedOut() const { return _timed_out; } void PinEntryDialog::setRepeatErrorText(const QString &err) { if (mRepeatError) { mRepeatError->setText(err); } } void PinEntryDialog::cancelTimeout() { if (_timer) { _timer->stop(); } } void PinEntryDialog::checkCapsLock() { const auto state = capsLockState(); if (state != LockState::Unknown) { mCapsLockHint->setVisible(state == LockState::On); } } void PinEntryDialog::onAccept() { cancelTimeout(); if (mRepeat && mRepeat->pin() != _edit->pin()) { #ifndef QT_NO_ACCESSIBILITY if (QAccessible::isActive()) { QMessageBox::information(this, mRepeatError->text(), mRepeatError->text()); } else #endif { mRepeatError->setVisible(true); } return; } const auto result = checkConstraints(); if (result != PassphraseNotOk) { accept(); } } #ifndef QT_NO_ACCESSIBILITY void PinEntryDialog::accessibilityActiveChanged(bool active) { // Allow text labels to get focus if accessibility is active const auto focusPolicy = active ? Qt::StrongFocus : Qt::ClickFocus; _error->setFocusPolicy(focusPolicy); _desc->setFocusPolicy(focusPolicy); mCapsLockHint->setFocusPolicy(focusPolicy); mConstraintsHint->setFocusPolicy(focusPolicy); mFormattedPassphraseHint->setFocusPolicy(focusPolicy); if (mRepeatError) { mRepeatError->setFocusPolicy(focusPolicy); } } #endif PinEntryDialog::PassphraseCheckResult PinEntryDialog::checkConstraints() { if (!mEnforceConstraints) { return PassphraseNotChecked; } const auto passphrase = _edit->pin().toUtf8(); unique_malloced_ptr error{pinentry_inq_checkpin( _pinentry_info, passphrase.constData(), passphrase.size())}; if (!error) { return PassphraseOk; } const auto messageLines = QString::fromUtf8(QByteArray::fromPercentEncoding(error.get())).split(QChar{'\n'}); if (messageLines.isEmpty()) { // shouldn't happen because pinentry_inq_checkpin() either returns NULL or a non-empty string return PassphraseOk; } const auto firstLine = messageLines.first(); const auto indexOfFirstNonEmptyAdditionalLine = messageLines.indexOf(QRegularExpression{QStringLiteral(".*\\S.*")}, 1); const auto additionalLines = indexOfFirstNonEmptyAdditionalLine > 0 ? messageLines.mid(indexOfFirstNonEmptyAdditionalLine).join(QChar{'\n'}) : QString{}; QMessageBox messageBox{this}; messageBox.setIcon(QMessageBox::Information); messageBox.setWindowTitle(mConstraintsErrorTitle); messageBox.setText(firstLine); messageBox.setInformativeText(additionalLines); messageBox.setStandardButtons(QMessageBox::Ok); messageBox.exec(); return PassphraseNotOk; } #include "pinentrydialog.moc" diff --git a/qt/pinentrydialog.h b/qt/pinentrydialog.h index 75f62ec..60161c5 100644 --- a/qt/pinentrydialog.h +++ b/qt/pinentrydialog.h @@ -1,180 +1,181 @@ /* pinentrydialog.h - A (not yet) secure Qt 4 dialog for PIN entry. * Copyright (C) 2002, 2008 Klarälvdalens Datakonsult AB (KDAB) * Copyright 2007 Ingo Klöcker * Copyright 2016 Intevation GmbH * Copyright (C) 2021, 2022 g10 Code GmbH * * Written by Steffen Hansen . * Modified by Andre Heinecke * Software engineering by Ingo Klöcker * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . * SPDX-License-Identifier: GPL-2.0+ */ #ifndef __PINENTRYDIALOG_H__ #define __PINENTRYDIALOG_H__ #include #include #include #include #include "pinentry.h" +class QIcon; class QLabel; class QPushButton; class QLineEdit; class PinLineEdit; class QString; class QProgressBar; class QCheckBox; class QAction; -QPixmap icon(QStyle::StandardPixmap which = QStyle::SP_CustomBase); +QPixmap applicationIconPixmap(const QIcon &overlayIcon = {}); void raiseWindow(QWidget *w); class PinEntryDialog : public QDialog #ifndef QT_NO_ACCESSIBILITY , public QAccessible::ActivationObserver #endif { Q_OBJECT Q_PROPERTY(QString description READ description WRITE setDescription) Q_PROPERTY(QString error READ error WRITE setError) Q_PROPERTY(QString pin READ pin WRITE setPin) Q_PROPERTY(QString prompt READ prompt WRITE setPrompt) public: struct FormattedPassphraseOptions { bool formatPassphrase; QString hint; }; struct ConstraintsOptions { bool enforce; QString shortHint; QString longHint; QString errorTitle; }; explicit PinEntryDialog(QWidget *parent = 0, const char *name = 0, int timeout = 0, bool modal = false, bool enable_quality_bar = false, const QString &repeatString = QString(), const QString &visibiltyTT = QString(), const QString &hideTT = QString()); ~PinEntryDialog() override; void setDescription(const QString &); QString description() const; void setError(const QString &); QString error() const; void setPin(const QString &); QString pin() const; QString repeatedPin() const; void setRepeatErrorText(const QString &); void setPrompt(const QString &); QString prompt() const; void setOkText(const QString &); void setCancelText(const QString &); void setQualityBar(const QString &); void setQualityBarTT(const QString &); void setGenpinLabel(const QString &); void setGenpinTT(const QString &); void setCapsLockHint(const QString &); void setFormattedPassphrase(const FormattedPassphraseOptions &options); void setConstraintsOptions(const ConstraintsOptions &options); void setPinentryInfo(pinentry_t); bool timedOut() const; protected Q_SLOTS: void updateQuality(const QString &); void slotTimeout(); void textChanged(const QString &); void focusChanged(QWidget *old, QWidget *now); void toggleVisibility(); void onBackspace(); void generatePin(); void toggleFormattedPassphrase(); protected: void keyPressEvent(QKeyEvent *event) override; void keyReleaseEvent(QKeyEvent *event) override; void showEvent(QShowEvent *event) override; private Q_SLOTS: void cancelTimeout(); void checkCapsLock(); void onAccept(); private: #ifndef QT_NO_ACCESSIBILITY void accessibilityActiveChanged(bool active) override; #endif enum PassphraseCheckResult { PassphraseNotChecked = -1, PassphraseNotOk = 0, PassphraseOk }; PassphraseCheckResult checkConstraints(); private: QLabel *_icon = nullptr; QLabel *_desc = nullptr; QLabel *_error = nullptr; QLabel *_prompt = nullptr; QLabel *_quality_bar_label = nullptr; QProgressBar *_quality_bar = nullptr; PinLineEdit *_edit = nullptr; PinLineEdit *mRepeat = nullptr; QLabel *mRepeatError = nullptr; QPushButton *_ok = nullptr; QPushButton *_cancel = nullptr; bool _grabbed = false; bool _have_quality_bar = false; bool _timed_out = false; bool _disable_echo_allowed = true; bool mEnforceConstraints = false; bool mFormatPassphrase = false; pinentry_t _pinentry_info = nullptr; QTimer *_timer = nullptr; QString mVisibilityTT; QString mHideTT; QAction *mVisiActionEdit = nullptr; QPushButton *mGenerateButton = nullptr; QCheckBox *mVisiCB = nullptr; QLabel *mFormattedPassphraseHint = nullptr; QLabel *mFormattedPassphraseHintSpacer = nullptr; QLabel *mCapsLockHint = nullptr; QLabel *mConstraintsHint = nullptr; QString mConstraintsErrorTitle; }; #endif // __PINENTRYDIALOG_H__ diff --git a/qt/pinentryrc.qrc b/qt/pinentryrc.qrc index 6ffab99..2b198de 100644 --- a/qt/pinentryrc.qrc +++ b/qt/pinentryrc.qrc @@ -1,9 +1,10 @@ + icons/data-error.svg icons/document-encrypt.png icons/hint.svg icons/password-generate.svg icons/visibility.svg