Line data Source code
1 : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Dialog that imports an ASCII-armored GPG public key from a file or
6 : * the clipboard by running `gpg --import`.
7 : */
8 : #ifndef SRC_IMPORTKEYDIALOG_H_
9 : #define SRC_IMPORTKEYDIALOG_H_
10 :
11 : #include <QDialog>
12 : #include <QElapsedTimer>
13 : #include <QScopedPointer>
14 : #include <QString>
15 : #include <QStringList>
16 :
17 : namespace Ui {
18 : class ImportKeyDialog;
19 : }
20 :
21 : /**
22 : * @class ImportKeyDialog
23 : * @ingroup keys
24 : * @brief Dialog for importing GPG keys from file or clipboard.
25 : *
26 : * Allows users to import GPG public keys without leaving QtPass.
27 : * Offers two import paths: from file or from clipboard text.
28 : */
29 : class ImportKeyDialog : public QDialog {
30 52 : Q_OBJECT
31 :
32 : public:
33 : /**
34 : * @brief How long gpg may take to import before it is stopped: the import
35 : * runs on the GUI thread, and a key crafted with many user IDs kept gpg
36 : * busy for minutes, the window and its autoclear timers frozen with it.
37 : */
38 : static constexpr int kImportTimeoutMs = 60000;
39 :
40 : /**
41 : * @brief Construct an ImportKeyDialog.
42 : * @param gpgExe Path to the gpg executable (falls back to "gpg" if empty).
43 : * @param parent Optional parent widget.
44 : * @param timeoutMs Most milliseconds gpg may take to import.
45 : */
46 : explicit ImportKeyDialog(QString gpgExe, QWidget *parent = nullptr,
47 : int timeoutMs = kImportTimeoutMs);
48 : /** @brief Destroys the dialog and its generated UI. */
49 : ~ImportKeyDialog() override;
50 :
51 : /**
52 : * @brief Get the imported key id on success.
53 : * @return The imported key id (or fingerprint when GnuPG status output
54 : * provides one), or empty if import failed.
55 : */
56 : auto importedKeyId() const -> QString;
57 :
58 : /**
59 : * @brief Parse GPG import output to extract a key id or fingerprint.
60 : *
61 : * Prefers locale-independent status lines (\c [GNUPG:] IMPORT_OK and
62 : * \c [GNUPG:] IMPORTED) when present, then falls back to the
63 : * human-readable \c "gpg: key ...: imported" line (English locale only).
64 : *
65 : * @param output Combined stdout/stderr from `gpg --import --status-fd 1`.
66 : * @return The imported key id or fingerprint, or empty if not found.
67 : */
68 : static auto parseGpgImportOutput(const QString &output) -> QString;
69 :
70 : /**
71 : * @brief How many keys an armored block holds, by kind.
72 : */
73 : struct BlockKeys {
74 : /** @brief Public keys: `pub` records. */
75 : int publicKeys = 0;
76 : /** @brief Keys with their secret part: `sec` records. */
77 : int secretKeys = 0;
78 : };
79 :
80 : /**
81 : * @brief Count the keys in the listing of
82 : * `gpg --with-colons --import-options show-only --import`.
83 : *
84 : * Only the primary-key records count (`pub` and `sec`); subkeys, user IDs
85 : * and fingerprints belong to the key before them.
86 : *
87 : * @param listing gpg's colon listing of the block.
88 : * @return The public and the secret keys in it.
89 : */
90 : static auto keysInBlock(const QString &listing) -> BlockKeys;
91 :
92 : /** @brief Longest line fitsTheEditor() lets through, in characters. */
93 : static constexpr qsizetype kMaxKeyLine = 1024;
94 : /** @brief Most lines fitsTheEditor() lets through: a 16 MiB key file in
95 : * armor's 64-character lines. */
96 : static constexpr qsizetype kMaxKeyLines = 16 * 1024 * 1024 / 64;
97 :
98 : /**
99 : * @brief Whether @p text is laid out as armor can be, so the input field
100 : * can show it at a bounded cost.
101 : *
102 : * Armor lines are 64 characters (76 at most) and a header line not much
103 : * longer. A line without break opportunity takes Qt time growing with the
104 : * square of its length to wrap, and each line costs the editor some
105 : * 440 bytes, so a file of one long line or of line breaks only froze or
106 : * filled the GUI before gpg ever saw it.
107 : * @param text The text to show.
108 : * A line ends where the editor starts a new one: LF, CR (CRLF once),
109 : * U+2028, U+2029, U+FDD0 and U+FDD1.
110 : * @return False when a line is longer than kMaxKeyLine or there are more
111 : * than kMaxKeyLines lines.
112 : */
113 : [[nodiscard]] static auto fitsTheEditor(QStringView text) -> bool;
114 :
115 : private slots:
116 : /**
117 : * @brief "From file" button: picks a file, reads it as a regular file of at
118 : * most 16 MiB, requires the `-----BEGIN PGP` armor header and loads the
119 : * text into the input field. Binary keyrings are refused with a hint to
120 : * `gpg --armor --export` first, and text that fails fitsTheEditor() with
121 : * the limits it broke.
122 : */
123 : void on_fileButton_clicked();
124 :
125 : /**
126 : * @brief "From clipboard" button: replaces the input field with the
127 : * clipboard text, unless the clipboard is empty or the text fails
128 : * fitsTheEditor().
129 : */
130 : void on_pasteButton_clicked();
131 :
132 : /**
133 : * @brief "Import" button: runs importFromString() on the trimmed input and
134 : * closes the dialog with `accept()` on success.
135 : */
136 : void on_importButton_clicked();
137 :
138 : /**
139 : * @brief Enables the Import button only while the input field holds
140 : * non-blank text.
141 : */
142 : void on_inputTextEdit_textChanged();
143 :
144 : private:
145 : /** @brief Generated form (`importkeydialog.ui`); owned. */
146 : QScopedPointer<Ui::ImportKeyDialog> ui;
147 : /** @brief Key id or fingerprint of the last successful import; empty until
148 : * then. Read back through importedKeyId(). */
149 : QString m_importedKeyId;
150 : /** @brief Path of the gpg binary to run; empty means plain `gpg` on PATH. */
151 : QString m_gpgExe;
152 : /** @brief Most milliseconds gpg may take to import (kImportTimeoutMs). */
153 : int m_timeoutMs;
154 :
155 : /**
156 : * @brief Lists the keys in @p input first, with nothing written, and
157 : * refuses a block that holds a private key or more than one key. Then
158 : * feeds it to `gpg --no-options --status-fd 1 --import --batch --yes` via
159 : * Executor::executeBlocking() and records the imported key id. Neither run
160 : * reads gpg.conf, so options there cannot make the listing and the import
161 : * see different keys.
162 : * @param input Armored key text, trimmed.
163 : * @return true when the block held one public key, gpg exited with 0 and a
164 : * key id could be parsed from its output; false after showError()
165 : * otherwise.
166 : */
167 : auto importFromString(const QString &input) -> bool;
168 : /**
169 : * @brief Runs gpg with @p args on @p input within what is left of
170 : * m_timeoutMs since @p timer started.
171 : * @param args gpg's arguments.
172 : * @param input Text for gpg's stdin.
173 : * @param stdOut Receives gpg's stdout.
174 : * @param stdErr Receives gpg's stderr.
175 : * @param timer Started when the import began.
176 : * @return true when gpg exited with 0; false after showError() otherwise.
177 : */
178 : auto runGpg(const QStringList &args, const QString &input, QString *stdOut,
179 : QString *stdErr, const QElapsedTimer &timer) -> bool;
180 : /**
181 : * @brief Shows @p message in a plain-text warning box (gpg's stderr quotes
182 : * user IDs, which must not be read as markup).
183 : * @param message The error text.
184 : */
185 : void showError(const QString &message);
186 : /**
187 : * @brief Shows the "imported, now verify the fingerprint" information box.
188 : * @param keyId The imported key id or fingerprint.
189 : */
190 : void showSuccess(const QString &keyId);
191 : };
192 : #endif // SRC_IMPORTKEYDIALOG_H_
|