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 Rollback detection for signed `.gpg-id` files: the generation counter
6 : * and the record of what was last seen.
7 : */
8 : #ifndef SRC_GPGIDGENERATION_H_
9 : #define SRC_GPGIDGENERATION_H_
10 :
11 : #include <QByteArray>
12 : #include <QString>
13 : #include <optional>
14 :
15 : /**
16 : * @ingroup keys
17 : * @brief Rollback detection for signed `.gpg-id` files.
18 : *
19 : * A signature proves that a recipient list is authentic and unmodified; it
20 : * does not prove that it is the current one, nor that it was written for
21 : * the folder it sits in. Whoever can write to a shared store can put back an
22 : * older, genuinely signed list that still names a member since removed, or
23 : * copy such a pair into a folder that never had a list of its own, and every
24 : * later encryption there would include them again.
25 : *
26 : * So a `.gpg-id` QtPass writes for a store with a signing key starts with
27 : * three comment lines, `# QtPass-GpgId-Generation: N`,
28 : * `# QtPass-GpgId-Folder: <store-relative folder>` and
29 : * `# QtPass-GpgId-Store: <store id>`, which `pass` (1.7.4 and later),
30 : * QtPass (1.8 and later) and QtPass's own recipient parser ignore and the
31 : * signature covers. The store id binds the list to its store, as the folder
32 : * line binds it to its folder: one signing key may sign several stores, and
33 : * a list of one copied into another at the same folder would verify there
34 : * too (#2050). Each device remembers the id it first saw per store and
35 : * refuses a list of another store, or a new list without the line. QtPass
36 : * remembers, per `.gpg-id` file, the highest generation it has accepted or
37 : * written, and refuses a signed list whose generation is lower or whose folder
38 : * is not the one it sits in. Stores without a signing key get plain lists, as
39 : * before: nothing checks their freshness, and older clients (pass up to 1.7.3,
40 : * Android Password Store) take a comment for a recipient.
41 : *
42 : * What this is and is not: rollback detection on a device that has seen the
43 : * newer list. A device seeing a store for the first time, or one that was
44 : * offline, has nothing to compare against. The generation is monotonic per
45 : * observer, not a distributed sequence number: two devices can both produce
46 : * generation 19 from 18, and Git is what sorts that out; a device that
47 : * accepted one of the two then meets the other as the same generation with
48 : * different bytes, which is a conflict, not a rollback, and is refused until
49 : * a holder of the signing key saves the recipients once more. For that the
50 : * record keeps, next to the generation, a digest of the exact bytes it
51 : * accepted or wrote. A list without the lines (written by `pass`, or by
52 : * QtPass before 2.0) is generation 0 and is refused once anything higher
53 : * was accepted; a holder of the signing key saving the recipients from
54 : * QtPass writes the next generation. Without a readable and writable record
55 : * nothing is accepted or written: unknown state is not "never seen".
56 : */
57 : class GpgIdGeneration {
58 : public:
59 : /// The generation line. The grammar is strict: this prefix, one ASCII
60 : /// decimal number of at most 18 digits, nothing else on the line.
61 : static const QByteArray kGenerationPrefix;
62 : /// The folder line: this prefix and the store-relative folder the list was
63 : /// written for, `.` for the store root, `/`-separated.
64 : static const QByteArray kFolderPrefix;
65 : /// The store line: this prefix and the store's id, 32 lowercase hex
66 : /// digits (a random 128-bit number given when the store's first signed
67 : /// list is written).
68 : static const QByteArray kStorePrefix;
69 : /// The largest generation the grammar admits (18 nines). reserveNext()
70 : /// never goes above it: a header QtPass writes must be one it reads back.
71 : static const qint64 kMaxGeneration;
72 :
73 : /// What a list's header says.
74 210 : struct Header {
75 : /// 0 when the list carries no generation line.
76 : qint64 generation = 0;
77 : /// Nothing when the list carries no folder line.
78 : std::optional<QString> folder;
79 : /// Nothing when the list carries no store line.
80 : std::optional<QString> store;
81 : };
82 :
83 : /**
84 : * @brief The header a `.gpg-id`'s bytes declare.
85 : * @param contents The file, as verified.
86 : * @param error Receives why the metadata is unusable, if not null.
87 : * @return The header (generation 0 and no folder for a list written
88 : * before generations existed, or by `pass`); nothing when a line
89 : * is malformed, appears twice, a generation line comes without a
90 : * folder line or a store line without a generation line, which is
91 : * not a list to trust.
92 : */
93 : static auto parse(const QByteArray &contents, QString *error = nullptr)
94 : -> std::optional<Header>;
95 :
96 : /**
97 : * @brief The header lines for a list.
98 : * @param generation The generation to declare.
99 : * @param folder The store-relative folder the list is written for.
100 : * @param store The store's id (storeIdFor()); empty writes no store line.
101 : * @return The lines, newlines included.
102 : */
103 : static auto header(qint64 generation, const QString &folder,
104 : const QString &store = {}) -> QByteArray;
105 :
106 : /**
107 : * @brief A complete list: the header in front of the recipients.
108 : * @param generation The generation to declare.
109 : * @param folder The store-relative folder the list is written for.
110 : * @param recipients The recipient lines, one per line, no header.
111 : * @param store The store's id (storeIdFor()); empty writes no store line.
112 : * @return The bytes to write.
113 : */
114 : static auto withHeader(qint64 generation, const QString &folder,
115 : const QByteArray &recipients,
116 : const QString &store = {}) -> QByteArray;
117 :
118 : /**
119 : * @brief The store id to write into a list of @p storeRoot: the one this
120 : * device knows the store by; else @p onDisk, the store line of the
121 : * verified list being replaced, so a device joining a store takes its id
122 : * over; else a new random one. Remembered before it is returned.
123 : * @param storeRoot The configured store.
124 : * @param onDisk The store line of the verified list on disk, if any.
125 : * @param error Receives why the record could not be read or written.
126 : * @return The id, or nothing when the record is unavailable; the caller
127 : * must not write the list then.
128 : */
129 : static auto storeIdFor(const QString &storeRoot,
130 : const std::optional<QString> &onDisk,
131 : QString *error = nullptr) -> std::optional<QString>;
132 :
133 : /**
134 : * @brief The store-relative folder of @p gpgIdFile, as written in and
135 : * compared against the folder line: `.` for the store root, `/`-separated,
136 : * from canonical paths, lower-cased on Windows.
137 : * @param gpgIdFile The `.gpg-id`.
138 : * @param storeRoot The configured store.
139 : * @return The folder, or nothing when @p gpgIdFile is not under the store
140 : * or a folder name on the way holds a control character, a line
141 : * break among them (the folder line could not record it).
142 : */
143 : static auto folderOf(const QString &gpgIdFile, const QString &storeRoot)
144 : -> std::optional<QString>;
145 :
146 : /**
147 : * @brief The highest generation this device has accepted or written for
148 : * the `.gpg-id` at @p gpgIdFile. Unreadable is not "never seen": a signed
149 : * list must then be refused, not compared against 0.
150 : * @param gpgIdFile The `.gpg-id`.
151 : * @param error Receives why the record could not be read, if not null.
152 : * @return 0 when this device has never seen the list, the generation
153 : * otherwise; nothing when the record cannot be read.
154 : */
155 : static auto remembered(const QString &gpgIdFile, QString *error = nullptr)
156 : -> std::optional<qint64>;
157 :
158 : /**
159 : * @brief Whether this device accepted or wrote a list at @p gpgIdFile
160 : * before, of any generation: a headerless list (generation 0) counts too,
161 : * so its removal is told apart from a folder that never had one.
162 : * @param gpgIdFile The `.gpg-id`.
163 : * @param error Receives why the record could not be read, if not null.
164 : * @return Whether it was seen; nothing when the record cannot be read.
165 : */
166 : static auto seen(const QString &gpgIdFile, QString *error = nullptr)
167 : -> std::optional<bool>;
168 :
169 : /**
170 : * @brief The outcome of accept(). Only Accepted means the list may be
171 : * used; the others say why not, and Rollback alone is an authentic list
172 : * of this folder.
173 : */
174 : enum class Verdict {
175 : /** @brief Folder, generation and record all agree; remembered. */
176 : Accepted,
177 : /**
178 : * @brief Authentic and bound to this folder, but older than a
179 : * generation this device accepted.
180 : */
181 : Rollback,
182 : /**
183 : * @brief Older than a generation this device accepted and without a
184 : * folder line: authentic, but written for who knows which folder
185 : * (`pass`, or QtPass before 2.0, wrote it), so not this folder's to
186 : * recover.
187 : */
188 : Unbound,
189 : /**
190 : * @brief The generation this device accepted last, but not the bytes:
191 : * two devices saved at once (Git shows that as a conflict), or an
192 : * authentic list of that generation was swapped in.
193 : */
194 : Conflict,
195 : /** @brief Written for another folder of the store. */
196 : WrongFolder,
197 : /**
198 : * @brief Written for another store (its store line names another id
199 : * than this device knows the store by), or new to this device and
200 : * without a store line while the store has an id.
201 : */
202 : WrongStore,
203 : /** @brief A header line malformed or duplicated. */
204 : Malformed,
205 : /** @brief The record could not be read or written. */
206 : RecordUnavailable,
207 : };
208 :
209 : /**
210 : * @brief Decide about a verified list: its folder against where it sits,
211 : * its generation against the remembered one, its bytes against the
212 : * remembered digest when the generations are equal, and remember it when
213 : * it passes. One transaction under the lock: read, compare, write through.
214 : * @param gpgIdFile The `.gpg-id` the bytes came from.
215 : * @param contents The verified bytes.
216 : * @param storeRoot The configured store, for the folder check.
217 : * @param error Receives the reason for a refusal, if not null. Where the
218 : * way through is saving the list again, the text says so, and that
219 : * the recipients the dialog then preselects are this list's.
220 : * @return Accepted when the list may be used; otherwise why not. Without
221 : * established freshness state nothing is accepted.
222 : */
223 : static auto accept(const QString &gpgIdFile, const QByteArray &contents,
224 : const QString &storeRoot, QString *error = nullptr)
225 : -> Verdict;
226 :
227 : /**
228 : * @brief Reserve the generation to write next for @p gpgIdFile: one above
229 : * both what this device remembers and @p verifiedOnDisk, recorded as
230 : * remembered before it is returned. One transaction under the lock, so two
231 : * writers in this process cannot get the same number.
232 : *
233 : * Reserving before the file is written is what makes a failed write
234 : * harmless (the record is ahead, the old list stays refused until a save
235 : * succeeds) and a failed record fatal for the write: a list written but
236 : * not recorded would let the previous one back in.
237 : * @param gpgIdFile The `.gpg-id` about to be written.
238 : * @param verifiedOnDisk The generation of the list currently on disk, if
239 : * its signature verified; nothing otherwise. An unverified number
240 : * is not taken: planted at the top of the grammar it would exhaust
241 : * the counter, planted high it would make every other device see a
242 : * jump.
243 : * @param error Receives why nothing could be reserved, if not null.
244 : * @return The generation to write, or nothing when the record could not
245 : * be read or written or the counter is exhausted; the caller must
246 : * not write the list then.
247 : */
248 : static auto reserveNext(const QString &gpgIdFile,
249 : std::optional<qint64> verifiedOnDisk,
250 : QString *error = nullptr) -> std::optional<qint64>;
251 :
252 : /**
253 : * @brief Record the bytes just written for @p gpgIdFile, so that this
254 : * device recognises its own list afterwards and refuses another list of
255 : * the same generation. Called after the file is on disk; the generation
256 : * in @p contents must be the one reserveNext() gave.
257 : * @param gpgIdFile The `.gpg-id` just written.
258 : * @param contents The exact bytes written.
259 : * @param error Receives why the record could not be written, if not null.
260 : * @return Whether the digest was recorded. A list written but not
261 : * digested is still recognised by its generation; only a second
262 : * list of that generation would pass until it is read once.
263 : */
264 : static auto recordWritten(const QString &gpgIdFile,
265 : const QByteArray &contents,
266 : QString *error = nullptr) -> bool;
267 :
268 : /**
269 : * @brief The digest of a list as the record keeps it: SHA-256 of the
270 : * exact bytes, hexadecimal, so that two authentic lists of one generation
271 : * can be told apart.
272 : * @param contents The bytes.
273 : * @return 64 hexadecimal characters.
274 : */
275 : static auto digest(const QByteArray &contents) -> QString;
276 :
277 : /**
278 : * @brief The settings key for a `.gpg-id`: a hash of its canonical path,
279 : * so path spelling (case, separators, links on the way) and QSettings'
280 : * treatment of `/` do not matter.
281 : * @param gpgIdFile The `.gpg-id`, existing or about to be written.
282 : * @return 64 hexadecimal characters.
283 : */
284 : static auto key(const QString &gpgIdFile) -> QString;
285 :
286 : /**
287 : * @brief The file the accepted generations are recorded in, so a refusal
288 : * can name it: removing it forgets what was accepted, which is the manual
289 : * way out for a device that cannot save the list itself.
290 : * @return The record's path.
291 : */
292 : static auto recordFile() -> QString;
293 :
294 : /**
295 : * @brief The file the store IDs are kept in, next to recordFile() and
296 : * under its lock, so a QtPass from before store IDs, rewriting the record,
297 : * cannot drop them. Removing it forgets the IDs.
298 : * @return The file's path.
299 : */
300 : static auto storeRecordFile() -> QString;
301 :
302 : private:
303 : /**
304 : * @brief Whether @p header's folder line, if it has one, names the folder
305 : * the list sits in: a pair copied elsewhere is that folder's first list,
306 : * a rollback in disguise.
307 : * @param gpgIdFile The `.gpg-id` the bytes came from.
308 : * @param header What its header says.
309 : * @param storeRoot The configured store.
310 : * @param error Receives why not, if not null.
311 : * @return Whether the list belongs where it is.
312 : */
313 : static auto boundToItsFolder(const QString &gpgIdFile, const Header &header,
314 : const QString &storeRoot, QString *error)
315 : -> bool;
316 :
317 : /**
318 : * @brief Whether @p header fits the store this device knows by @p pinned:
319 : * its store line names that id, or it has none and is either generation 0
320 : * (`pass`) or a list this device accepted before it knew the id.
321 : * @param gpgIdFile The `.gpg-id` the bytes came from.
322 : * @param header What its header says.
323 : * @param pinned The store id this device knows the store by.
324 : * @param alreadyAccepted Whether these exact bytes are the generation
325 : * this device accepted last for the file.
326 : * @param recordPath The store IDs' file, which the refusal names.
327 : * @param error Receives why not, if not null.
328 : * @return Whether the list is this store's.
329 : */
330 : static auto ofThisStore(const QString &gpgIdFile, const Header &header,
331 : const QString &pinned, bool alreadyAccepted,
332 : const QString &recordPath, QString *error) -> bool;
333 :
334 : /**
335 : * @brief How a holder of the signing key gets through a refusal, in words:
336 : * saving the recipients writes the next generation, unless the record is
337 : * already at the ceiling of the grammar, where it has to be removed first.
338 : * @param last The generation this device accepted before.
339 : * @param recordPath The record file, which the text names.
340 : * @param saving What the user does in the Users dialog, in this refusal's
341 : * terms ("checking the recipients and saving").
342 : * @return The sentence to append to a refusal.
343 : */
344 : static auto wayThrough(qint64 last, const QString &recordPath,
345 : const QString &saving) -> QString;
346 :
347 : /**
348 : * @brief The refusal for a list older than the one accepted here: a
349 : * rollback, or an unbound list that could be any folder's history.
350 : * @param gpgIdFile The `.gpg-id` the bytes came from.
351 : * @param header What its header says.
352 : * @param last The generation this device accepted before.
353 : * @param recordPath The record file, which the text names.
354 : * @param error Receives the reason, if not null.
355 : * @return Rollback for a list that says which folder it is for, Unbound
356 : * for one that does not.
357 : */
358 : static auto staleList(const QString &gpgIdFile, const Header &header,
359 : qint64 last, const QString &recordPath, QString *error)
360 : -> Verdict;
361 :
362 : /**
363 : * @brief The second half of accept(), for a list whose header parsed and
364 : * fits its folder: check it against what this device accepted before (the
365 : * store's id, the generation and its bytes) and record it when it passes.
366 : * @param gpgIdFile The `.gpg-id` the bytes came from.
367 : * @param header What its header says.
368 : * @param contents The signed list's bytes.
369 : * @param storeRoot The configured store.
370 : * @param error Receives the reason for any verdict but Accepted, if not
371 : * null.
372 : * @return The verdict, as accept() returns it.
373 : */
374 : static auto acceptAgainstRecord(const QString &gpgIdFile,
375 : const Header &header,
376 : const QByteArray &contents,
377 : const QString &storeRoot, QString *error)
378 : -> Verdict;
379 :
380 : /**
381 : * @brief The refusal for a list of the generation accepted here whose
382 : * bytes differ: two devices saved at once, or a swap.
383 : * @param gpgIdFile The `.gpg-id` the bytes came from.
384 : * @param generation Its generation.
385 : * @param last The generation this device accepted before (the same).
386 : * @param recordPath The record file, which the text names.
387 : * @param error Receives the reason, if not null.
388 : * @return Conflict.
389 : */
390 : static auto conflictingList(const QString &gpgIdFile, qint64 generation,
391 : qint64 last, const QString &recordPath,
392 : QString *error) -> Verdict;
393 : };
394 :
395 : #endif // SRC_GPGIDGENERATION_H_
|