Line data Source code
1 : // SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Dialog that collects name, e-mail and passphrase for a new GPG
6 : * keypair and drives `gpg --gen-key --batch` through the Pass backend.
7 : */
8 : #ifndef SRC_KEYGENDIALOG_H_
9 : #define SRC_KEYGENDIALOG_H_
10 :
11 : #include <QDialog>
12 : #include <QScopedPointer>
13 : #include <memory>
14 :
15 : #include "qprogressindicator.h"
16 :
17 : namespace Ui {
18 : class KeygenDialog;
19 : }
20 :
21 : class Pass;
22 :
23 : /**
24 : * @class KeygenDialog
25 : * @ingroup keys
26 : * @brief Handles GPG keypair generation.
27 : *
28 : * The form asks for a name, an e-mail address and a passphrase typed twice
29 : * (or an explicit "no passphrase"); an Expert checkbox reveals the editable
30 : * GPG batch template that the name and e-mail fields are mirrored into. The
31 : * passphrase is never written into that template: on OK, done() validates
32 : * the fields, splices the passphrase in with applyPassphrase() and hands the
33 : * batch to Pass::GenerateGPGKeys(), replacing the form with a progress
34 : * indicator. The dialog owns nothing of the backend; it connects to
35 : * Pass::finishedGenerateGPGKeys (accept) and Pass::generateGPGKeysFailed
36 : * (show the error and give the form back) for the duration of one attempt
37 : * only, so an `exec()` caller such as the first-run wizard sees `Accepted`
38 : * exactly when a key exists.
39 : */
40 : class KeygenDialog : public QDialog {
41 14 : Q_OBJECT
42 :
43 : public:
44 : /**
45 : * @brief Construct a KeygenDialog.
46 : * @param gpgExe Path to the gpg executable (for the default template).
47 : * @param pass Backend that runs `gpg --gen-key`; the dialog accepts on its
48 : * finishedGenerateGPGKeys() and shows generateGPGKeysFailed().
49 : * @param parent Parent widget, or nullptr.
50 : */
51 : explicit KeygenDialog(const QString &gpgExe, Pass *pass,
52 : QWidget *parent = nullptr);
53 : /** @brief Destroys the dialog and its generated UI. */
54 : ~KeygenDialog() override;
55 :
56 : /**
57 : * @brief Splice a passphrase into a GPG batch template.
58 : *
59 : * Replaces %no-protection (or an existing Passphrase: line) with
60 : * "Passphrase: <passphrase>"; an empty passphrase yields %no-protection.
61 : * @param batch GPG batch template text.
62 : * @param passphrase Key passphrase, empty for an unprotected key.
63 : * @return Batch text ready for gpg --gen-key --batch.
64 : */
65 : static QString applyPassphrase(const QString &batch,
66 : const QString &passphrase);
67 :
68 : /**
69 : * @brief Whether @p value can go into a GPG batch line as one value.
70 : *
71 : * The batch is line-oriented, and gpg reads a line as a C string: a line
72 : * break or NUL in a name or passphrase would start a directive of its own
73 : * (%no-protection, a Passphrase: of the paster's choosing).
74 : * Text that is not valid UTF-16 (a lone surrogate) is refused too: gpg
75 : * gets no such text, only a replacement for it.
76 : * @param value A field's text.
77 : * @return False when hasBatchBreak() finds a break, or it is not valid
78 : * UTF-16.
79 : */
80 : [[nodiscard]] static auto isBatchValue(const QString &value) -> bool;
81 : /**
82 : * @brief Whether a field's text holds a line break or NUL, which would end
83 : * its batch line early.
84 : *
85 : * The template box reads U+2028, U+2029, U+FDD0 and U+FDD1 back as LF, so
86 : * those count as line breaks too.
87 : * @param value A field's text.
88 : * @return True when it holds a CR, LF, NUL, U+2028, U+2029, U+FDD0 or
89 : * U+FDD1.
90 : */
91 : [[nodiscard]] static auto hasBatchBreak(const QString &value) -> bool;
92 :
93 : /**
94 : * @brief Whether gpg reads @p passphrase back as typed from a batch
95 : * `Passphrase:` line.
96 : *
97 : * gpg skips white space after the colon and cuts spaces and tabs off the
98 : * end of a value, so a passphrase with a blank at either end would protect
99 : * the key without it, and the passphrase typed twice is then refused. Only
100 : * ASCII white space counts: gpg keeps a no-break space.
101 : * @param passphrase The passphrase typed; empty means none.
102 : * @return False when it starts or ends with ASCII white space.
103 : */
104 : [[nodiscard]] static auto isBatchPassphrase(const QString &passphrase)
105 : -> bool;
106 :
107 : protected:
108 : private slots:
109 : /**
110 : * @brief First passphrase field edited: re-evaluates whether OK may be
111 : * enabled. The text itself stays out of the template box.
112 : * @param arg1 The new field text (unused).
113 : */
114 : void on_passphrase1_textChanged(const QString &arg1);
115 : /**
116 : * @brief Confirmation field edited: same as on_passphrase1_textChanged().
117 : * @param arg1 The new field text (unused).
118 : */
119 : void on_passphrase2_textChanged(const QString &arg1);
120 : /**
121 : * @brief Ticking "no passphrase" clears and disables the passphrase
122 : * fields and makes OK available; unticking it asks for one again.
123 : * @param checked The checkbox state.
124 : */
125 : void setNoPassphrase(bool checked);
126 : /**
127 : * @brief Enable or disable expert mode (the editable batch template).
128 : *
129 : * Connected explicitly to QCheckBox::toggled in the constructor; the box
130 : * is two-state, so the deprecated stateChanged(int) is not needed.
131 : * @param checked true when the Expert box is checked.
132 : */
133 : void setExpertMode(bool checked);
134 : /**
135 : * @brief Mirrors the e-mail field into the template's `Name-Email:` line.
136 : * @param arg1 The new field text.
137 : */
138 : void on_email_textChanged(const QString &arg1);
139 : /**
140 : * @brief Mirrors the name field into the template's `Name-Real:` line.
141 : * @param arg1 The new field text.
142 : */
143 : void on_name_textChanged(const QString &arg1);
144 :
145 : private:
146 : /**
147 : * @brief OK is available when a passphrase is typed twice the same, or
148 : * when no passphrase was asked for explicitly. Empty fields alone are not
149 : * a choice: the key would be stored unprotected without anyone saying so.
150 : */
151 : void updateOkState();
152 : /**
153 : * @brief Whether the passphrase question has an answer: typed twice the
154 : * same, or waived with the checkbox. Empty fields alone are neither.
155 : * @return true when generation may proceed as far as the passphrase goes.
156 : */
157 : auto passphraseChosen() const -> bool;
158 : /** @brief Generated form (`keygendialog.ui`); owned. */
159 : QScopedPointer<Ui::KeygenDialog> ui;
160 : /**
161 : * @brief Rewrites every `<key>: ...` line of the batch template to carry
162 : * @p value. Values that fail isBatchValue() are ignored so a pasted line
163 : * break can never become a directive of its own.
164 : * @param key Batch parameter name, e.g. `Name-Real`.
165 : * @param value New value for that parameter.
166 : */
167 : void replace(const QString &key, const QString &value);
168 : /**
169 : * @brief On `Accepted`, validates the form (passphrase chosen, no line
170 : * breaks, name of 5+ characters, plausible e-mail), swaps the form for the
171 : * progress indicator and calls startGeneration(); the dialog only closes
172 : * once the backend reports success. Any other result disconnects the
173 : * backend signals and closes normally.
174 : * @param r The QDialog result code.
175 : */
176 : void done(int r) override;
177 : /**
178 : * @brief Connects to the backend's finished/failed signals for this one
179 : * attempt and calls Pass::GenerateGPGKeys(). With no backend the attempt
180 : * fails at once.
181 : * @param batch Complete batch text, passphrase already spliced in.
182 : */
183 : void startGeneration(const QString &batch);
184 : /**
185 : * @brief Stops and hides the progress indicator, re-enables the form under
186 : * the usual OK rule and shows @p error in the status label, so the user can
187 : * correct the input and try again.
188 : * @param error Backend error text; HTML-escaped before display.
189 : */
190 : void generationFailed(const QString &error);
191 : /** @brief Backend that runs gpg; not owned, may be nullptr. */
192 : Pass *m_pass;
193 : /** @brief Connection to Pass::finishedGenerateGPGKeys, live only during an
194 : * attempt. */
195 : QMetaObject::Connection m_finished;
196 : /** @brief Connection to Pass::generateGPGKeysFailed, live only during an
197 : * attempt. */
198 : QMetaObject::Connection m_failed;
199 : /** @brief Spinner shown while gpg works; created lazily on the first
200 : * attempt, reused (hidden) across retries. */
201 : std::unique_ptr<QProgressIndicator> m_progressIndicator;
202 : };
203 :
204 : #endif // SRC_KEYGENDIALOG_H_
|