ostrya 0.9.0

Pure-Rust async reimplementation of the ostree repository library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! The signing keys a receiving server holds.

use std::sync::Arc;

use ostrya_core::Value;

use crate::error::{Error, Result};
use crate::sign::{Ed25519Signer, Ed25519Verifier, Signer, Verifier, signatures_for};
use crate::verify::Payload;

/// The length of an ed25519 secret key: the 32-byte seed, then the 32-byte
/// public key.
const ED25519_SECRET_LEN: usize = 64;

/// A signing key of the server, with a verifier that trusts that key alone.
///
/// The server signs each commit that becomes the new value of a ref with the
/// keys in [`ReceivePolicy::signers`](super::ReceivePolicy::signers). Before it
/// signs, the server looks for a signature of this key in the detached
/// metadata of the commit, stored or incoming. If it finds one, the server
/// does not add a second signature.
///
/// # Existing signatures
///
/// The server verifies each signature with the verifier and makes no byte
/// comparison. A GPG signature holds the time of signing, so two GPG
/// signatures from one key over one commit have different bytes.
///
/// The server reads the signatures under the detached-metadata key of the
/// engine, in the stored order. It verifies each signature on its own, with
/// this key alone, and stops at the first signature that verifies. These
/// signatures count as not made by this key:
///
/// - a signature from another key
/// - a signature over other bytes
/// - a signature that the verifier refuses, for example a blob that does not
///   parse, a blob over 1 MiB, or a blob with more than 64 GPG signature
///   packets (the limits of `GpgVerifier`).
///
/// A refused signature does not stop the search, so a signature that a
/// client adds cannot hide the signature of the server.
pub struct ServerSigner {
    /// The key that the server signs with.
    signer: Box<dyn Signer>,
    /// A verifier that trusts the public half of the key of `signer` and no
    /// other key.
    verifier: Arc<dyn Verifier>,
}

impl ServerSigner {
    /// Creates a server key from a signer and a verifier of its public key.
    ///
    /// `verifier` must trust the public half of the key of `signer` and no
    /// other key. The caller is responsible for this pairing, and `new` does
    /// not check it:
    ///
    /// - If `verifier` trusts another key, the server signs a commit that its
    ///   own key already signed.
    /// - If `verifier` trusts more keys, the server does not sign a commit
    ///   that another key signed.
    ///
    /// The server verifies the [existing signatures](ServerSigner#existing-signatures)
    /// with this pair on the blocking pool.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidFormat`] if `signer` and `verifier` use different
    ///   detached-metadata keys. The message is
    ///   `the signer writes '<key>' and the verifier reads '<key>'`.
    pub fn new(signer: Box<dyn Signer>, verifier: Box<dyn Verifier>) -> Result<ServerSigner> {
        if signer.metadata_key() != verifier.metadata_key() {
            return Err(Error::InvalidFormat(format!(
                "the signer writes '{}' and the verifier reads '{}'",
                signer.metadata_key(),
                verifier.metadata_key()
            )));
        }
        Ok(ServerSigner {
            signer,
            verifier: Arc::from(verifier),
        })
    }

    /// Creates a server key from a 64-byte ed25519 secret key.
    ///
    /// The secret is the 32-byte seed, then the 32-byte public key. The
    /// verifier trusts that public key alone.
    ///
    /// # Errors
    ///
    /// - [`Error::Signature`] if `secret` is not 64 bytes long. The message is
    ///   `ed25519 secret key must be 64 bytes, got <n>`.
    /// - [`Error::Signature`] if the public half of `secret` does not match
    ///   its seed. The message starts with `ed25519 secret key:`.
    pub fn ed25519(secret: &[u8]) -> Result<ServerSigner> {
        let signer = Ed25519Signer::from_secret_key(secret)?;
        let public = &secret[ED25519_SECRET_LEN / 2..ED25519_SECRET_LEN];
        let verifier = Ed25519Verifier::new([public], Vec::<Vec<u8>>::new())?;
        Ok(ServerSigner {
            signer: Box::new(signer),
            verifier: Arc::new(verifier),
        })
    }

    /// Creates a server key from an spki signer.
    ///
    /// The verifier is a [`SpkiVerifier`](crate::spki::SpkiVerifier) that
    /// trusts the public key of `signer` alone.
    ///
    /// The key signs on the blocking pool, because the spki signer does its
    /// work in the call to [`Signer::sign`]. An RSA signature with a
    /// 16384-bit key takes about 0.5 seconds of CPU time. The signer that
    /// [`signer`](Self::signer) returns also signs on the blocking pool.
    ///
    /// # Errors
    ///
    /// - [`Error::Signature`] if the verifier does not accept the DER public
    ///   key of `signer`. The message is
    ///   `spki public key: expected SubjectPublicKeyInfo DER`.
    #[cfg(feature = "sign-spki")]
    pub fn spki(signer: crate::spki::SpkiSigner) -> Result<ServerSigner> {
        let verifier =
            crate::spki::SpkiVerifier::new([signer.public_key_der()], Vec::<Vec<u8>>::new())?;
        Ok(ServerSigner {
            signer: Box::new(Unblocked {
                inner: Arc::new(signer),
            }),
            verifier: Arc::new(verifier),
        })
    }

    /// Creates a server key from the one secret key that a GPG signer selects.
    ///
    /// The selector of `signer` must name exactly one secret key in the GnuPG
    /// home of `signer`. The function refuses a user id that matches two keys.
    /// The server key signs with the selected key by its fingerprint.
    ///
    /// [`GpgSigner::export_public`](crate::gpg::GpgSigner::export_public) gives
    /// the minimal public certificate of the key from the same home. The
    /// certificate holds the primary key, the primary user id with its
    /// self-signature, and each signing subkey with its binding signature. A
    /// revocation of the primary key or of a signing subkey stays in the
    /// certificate. The verifier trusts that certificate alone.
    ///
    /// # Errors
    ///
    /// - [`Error::Signature`] if `gpg` cannot start. If `gpg` is not in
    ///   `PATH`, the message is `gpg: program not found in PATH`. For another
    ///   failure, the message is `gpg: <io error>`.
    /// - [`Error::Signature`] with the message
    ///   `the key selector names no secret key` if the selector names no
    ///   secret key. The same error occurs if the `gpg` listing fails, for
    ///   example if the GnuPG home does not exist.
    /// - [`Error::Signature`] with the message
    ///   `the key selector names more than one secret key; use the fingerprint`
    ///   if the selector names two or more secret keys.
    /// - [`Error::Signature`] for each error of
    ///   [`GpgSigner::export_public`](crate::gpg::GpgSigner::export_public).
    /// - The errors of
    ///   [`GpgVerifier::from_keyring_bytes`](crate::gpg::GpgVerifier::from_keyring_bytes)
    ///   for the exported certificate.
    #[cfg(feature = "sign-gpg")]
    pub async fn gpg(signer: crate::gpg::GpgSigner) -> Result<ServerSigner> {
        let fingerprints = signer.secret_key_fingerprints().await?;
        let fingerprint = match fingerprints.as_slice() {
            [one] => one,
            [] => {
                return Err(Error::Signature(
                    "the key selector names no secret key".into(),
                ));
            }
            _ => {
                return Err(Error::Signature(
                    "the key selector names more than one secret key; use the fingerprint".into(),
                ));
            }
        };
        let mut bound = crate::gpg::GpgSigner::new(fingerprint.as_str());
        if let Some(dir) = signer.homedir() {
            bound = bound.with_homedir(dir);
        }
        let certificate = bound.export_public(fingerprint).await?;
        let verifier = crate::gpg::GpgVerifier::from_keyring_bytes([certificate])?;
        Ok(ServerSigner {
            signer: Box::new(bound),
            verifier: Arc::new(verifier),
        })
    }

    /// Returns the key that the server signs with.
    pub fn signer(&self) -> &dyn Signer {
        self.signer.as_ref()
    }

    /// Returns `true` if `dict` holds a signature over `payload` that this key
    /// made.
    ///
    /// `dict` is the detached metadata of a commit, or `None` for a commit
    /// with no detached metadata. [`ServerSigner`](ServerSigner#existing-signatures)
    /// states the rules of the search.
    ///
    /// The search runs on the blocking pool, because a verifier does its work
    /// in the call. The pool gets a [`Payload::Shared`] payload with no copy,
    /// and a copy of a [`Payload::Borrowed`] payload.
    pub(crate) async fn has_signed<'p>(
        &self,
        payload: impl Into<Payload<'p>>,
        dict: Option<&Value>,
    ) -> bool {
        let Some(dict) = dict else {
            return false;
        };
        let blobs = signatures_for(dict, self.verifier.metadata_key());
        if blobs.is_empty() {
            return false;
        }
        let verifier = Arc::clone(&self.verifier);
        let payload = payload.into().to_shared();
        ostrya_rt::unblock(move || any_verifies(verifier.as_ref(), &payload, &blobs)).await
    }
}

/// A signer that signs on the blocking pool.
///
/// The signer copies the data of each call and gives the copy to the pool.
/// The data is a commit object or a summary, whose size the format caps.
#[cfg(feature = "sign-spki")]
struct Unblocked {
    /// The signer that does the work.
    inner: Arc<crate::spki::SpkiSigner>,
}

#[cfg(feature = "sign-spki")]
impl Signer for Unblocked {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn metadata_key(&self) -> &str {
        self.inner.metadata_key()
    }

    fn sign<'a>(&'a self, data: &'a [u8]) -> crate::sign::SignFuture<'a> {
        let inner = Arc::clone(&self.inner);
        let data = data.to_vec();
        Box::pin(ostrya_rt::unblock(move || {
            futures_lite::future::block_on(inner.sign(&data))
        }))
    }
}

/// Returns `true` if one of `blobs` verifies over `payload` under `verifier`.
///
/// The function verifies each blob on its own and stops at the first blob that
/// verifies. A blob that the verifier refuses counts as not valid.
fn any_verifies(verifier: &dyn Verifier, payload: &[u8], blobs: &[Vec<u8>]) -> bool {
    for blob in blobs {
        if let Ok(outcome) = verifier.verify(payload, std::slice::from_ref(blob))
            && outcome.valid
        {
            return true;
        }
    }
    false
}

impl std::fmt::Debug for ServerSigner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ServerSigner")
            .field("engine", &self.signer.name())
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use ostrya_core::base64;

    use super::*;
    use crate::sign::{DummySigner, DummyVerifier, append_signature};

    /// The base64 of a 64-byte ed25519 secret key (seed, then public key).
    const SECRET_B64: &str =
        "o74ME/dmhvDeYf64dDJQY8kX2piK0M/nyIRWVi30i6DCOzRsHVcvgYToz6zOb5OvK/v8nH6KfLR3dfdsn6ZSyQ==";
    /// Another ed25519 secret key.
    const OTHER_SECRET_B64: &str =
        "5ILWxT+l9G/u3h0BptRpmSi35C9uog7YDdD+Fp1Xk+Hz52p0NlYh6xBA73kJEJKhKbbnjcE0rsWA5XA/K5Sq5Q==";

    const PAYLOAD: &[u8] = b"the commit bytes";

    /// Returns a detached-metadata dict that holds `blob` under `key`.
    fn dict_with(key: &str, blob: Vec<u8>) -> Value {
        let mut dict = Value::Array(Vec::new());
        append_signature(&mut dict, key, blob).unwrap();
        dict
    }

    /// Returns a detached-metadata dict that holds `blobs` under `key`, in order.
    fn dict_of(key: &str, blobs: Vec<Vec<u8>>) -> Value {
        let mut dict = Value::Array(Vec::new());
        for blob in blobs {
            append_signature(&mut dict, key, blob).unwrap();
        }
        dict
    }

    /// Returns a blob one byte over the size limit of a GPG signature blob.
    fn oversize_blob() -> Vec<u8> {
        vec![0x88; 1024 * 1024 + 1]
    }

    /// Returns a signature that `signer` makes over [`PAYLOAD`].
    fn signature(signer: &ServerSigner) -> Vec<u8> {
        ostrya_rt::block_on(signer.signer().sign(PAYLOAD)).unwrap()
    }

    fn has_signed(signer: &ServerSigner, dict: Option<&Value>) -> bool {
        ostrya_rt::block_on(signer.has_signed(PAYLOAD, dict))
    }

    /// The ed25519 search recognizes the signature of the key and no other
    /// signature.
    #[test]
    fn an_ed25519_key_recognizes_its_own_signature() {
        let server = ServerSigner::ed25519(&base64::decode(SECRET_B64).unwrap()).unwrap();
        let other = ServerSigner::ed25519(&base64::decode(OTHER_SECRET_B64).unwrap()).unwrap();
        let key = server.signer().metadata_key().to_owned();

        assert!(has_signed(
            &server,
            Some(&dict_with(&key, signature(&server)))
        ));
        assert!(!has_signed(
            &server,
            Some(&dict_with(&key, signature(&other)))
        ));
        assert!(!has_signed(&server, Some(&dict_with(&key, vec![0u8; 64]))));
        assert!(!has_signed(
            &server,
            Some(&dict_with(&key, b"garbage".to_vec()))
        ));
        assert!(!has_signed(&server, None));
        assert!(!has_signed(&server, Some(&Value::Array(Vec::new()))));
        // The search does not read a signature of this key under the
        // detached-metadata key of another engine.
        assert!(!has_signed(
            &server,
            Some(&dict_with("ostree.sign.dummy", signature(&server)))
        ));
        // A signature over other bytes is not one over the payload.
        let elsewhere = ostrya_rt::block_on(server.signer().sign(b"other bytes")).unwrap();
        assert!(!has_signed(&server, Some(&dict_with(&key, elsewhere))));
    }

    /// A garbage or oversize blob beside the ed25519 signature of the key does
    /// not hide that signature, in either order.
    #[test]
    fn an_ed25519_signature_beside_a_bad_blob_is_recognized() {
        let server = ServerSigner::ed25519(&base64::decode(SECRET_B64).unwrap()).unwrap();
        let key = server.signer().metadata_key().to_owned();
        let own = signature(&server);
        for bad in [b"garbage".to_vec(), oversize_blob()] {
            let before = dict_of(&key, vec![bad.clone(), own.clone()]);
            assert!(has_signed(&server, Some(&before)));
            let after = dict_of(&key, vec![own.clone(), bad.clone()]);
            assert!(has_signed(&server, Some(&after)));
            assert!(!has_signed(&server, Some(&dict_of(&key, vec![bad]))));
        }
    }

    /// A verifier that returns an error for each blob except one.
    struct RefusesOthers {
        good: Vec<u8>,
    }

    impl Verifier for RefusesOthers {
        fn metadata_key(&self) -> &str {
            "ostree.sign.dummy"
        }

        fn verify(
            &self,
            _data: &[u8],
            signatures: &[Vec<u8>],
        ) -> ostrya_sign::Result<ostrya_sign::VerifyOutcome> {
            if signatures.iter().any(|blob| *blob != self.good) {
                return Err(ostrya_sign::Error::Signature("refused".into()));
            }
            let valid = ostrya_sign::SignatureInfo {
                valid: true,
                ..Default::default()
            };
            Ok(ostrya_sign::VerifyOutcome {
                valid: true,
                signatures: vec![valid; signatures.len()],
            })
        }
    }

    /// A blob that the verifier refuses with an error counts as not signed. It
    /// does not hide a blob that verifies.
    #[test]
    fn a_refused_blob_counts_as_not_signed() {
        let server = ServerSigner::new(
            Box::new(DummySigner::new(b"k".to_vec())),
            Box::new(RefusesOthers {
                good: b"good".to_vec(),
            }),
        )
        .unwrap();
        let key = "ostree.sign.dummy";
        let mixed = dict_of(key, vec![b"bad".to_vec(), b"good".to_vec()]);
        assert!(has_signed(&server, Some(&mixed)));
        assert!(!has_signed(&server, Some(&dict_with(key, b"bad".to_vec()))));
    }

    /// The search runs the verifier of a pair from `new` on the blocking pool,
    /// so a verifier that works in the call does not hold the async executor.
    /// The search gives a payload in an [`Arc`] to the verifier with no copy.
    #[test]
    fn the_search_verifies_on_the_blocking_pool() {
        use crate::verify::trust::tests::ThreadRecorder;

        let recorder = ThreadRecorder::new("ostree.sign.dummy");
        let server = ServerSigner::new(
            Box::new(DummySigner::new(b"k".to_vec())),
            Box::new(recorder.clone()),
        )
        .unwrap();
        let payload = Arc::new(PAYLOAD.to_vec());
        let dict = dict_with("ostree.sign.dummy", b"k".to_vec());
        assert!(ostrya_rt::block_on(
            server.has_signed(&payload, Some(&dict))
        ));
        recorder.assert_one_foreign_call();
        recorder.assert_no_copy_of(&payload);
    }

    /// `ServerSigner::ed25519` refuses a secret of another length and a secret
    /// whose public half does not match its seed.
    #[test]
    fn a_malformed_ed25519_secret_is_refused() {
        let secret = base64::decode(SECRET_B64).unwrap();
        assert!(ServerSigner::ed25519(&secret[..63]).is_err());
        let other = base64::decode(OTHER_SECRET_B64).unwrap();
        let mut mixed = secret[..32].to_vec();
        mixed.extend_from_slice(&other[32..]);
        assert!(ServerSigner::ed25519(&mixed).is_err());
    }

    /// The search recognizes a signature of a pair that the caller builds. `new`
    /// refuses a pair whose two halves read different detached-metadata keys.
    #[test]
    fn new_checks_the_metadata_key() {
        let server = ServerSigner::new(
            Box::new(DummySigner::new(b"k".to_vec())),
            Box::new(DummyVerifier::new([b"k".to_vec()])),
        )
        .unwrap();
        assert!(has_signed(
            &server,
            Some(&dict_with("ostree.sign.dummy", signature(&server)))
        ));

        let err = ServerSigner::new(
            Box::new(DummySigner::new(b"k".to_vec())),
            Box::new(Ed25519Verifier::new(Vec::<Vec<u8>>::new(), Vec::<Vec<u8>>::new()).unwrap()),
        )
        .unwrap_err();
        assert!(matches!(err, Error::InvalidFormat(_)), "{err}");
    }

    /// The spki search recognizes the DER signature of the key, the form
    /// that the verifier reads. It does not recognize the fixed-width
    /// `r || s` form of the same signature, or the signature of another key.
    #[cfg(feature = "sign-spki")]
    #[test]
    fn an_spki_key_recognizes_its_own_der_signature_alone() {
        use crate::spki::SpkiSigner;

        // Two P-256 secret keys, base64 of the PKCS#8 DER.
        let server = ServerSigner::spki(
            SpkiSigner::from_base64(concat!(
                "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg2L708EsnnzHER0SYasMNIUcG",
                "v63QapC/3kVsoPerzKGhRANCAATxfzfHKUPeJtyLTGMUoxHhvBS1NT9guWhUQPGiZRLZIcB8Wc3",
                "csdVU1iOiTRmbZGKJTtekOdEAbVRrx5HxIpst",
            ))
            .unwrap(),
        )
        .unwrap();
        let other = ServerSigner::spki(
            SpkiSigner::from_base64(concat!(
                "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgU5tYPz/hTTxfsSTPOxD/H9U+",
                "IkBj5qWV4/H3D1NBfRChRANCAAQPyTr9rh2d6WiYOi3oxNiE3ppTP2g5JP2xX9D7rEKnzixp",
                "MqVuqA9XnzQr5GlCYd71m/D7BnYIqS4MytNawYrR",
            ))
            .unwrap(),
        )
        .unwrap();
        let key = server.signer().metadata_key().to_owned();

        let der = signature(&server);
        assert!(has_signed(&server, Some(&dict_with(&key, der.clone()))));
        assert!(!has_signed(
            &server,
            Some(&dict_with(&key, fixed_width(&der)))
        ));
        assert!(!has_signed(
            &server,
            Some(&dict_with(&key, signature(&other)))
        ));
    }

    /// An spki server key of the RSA key type signs on the blocking pool, and
    /// the search recognizes its own signature alone. It does not recognize
    /// the signature of another key, or a blob of another length.
    #[cfg(feature = "sign-spki")]
    #[test]
    fn an_rsa_spki_key_recognizes_its_own_signature_alone() {
        use crate::spki::{SpkiSigner, spki_secret_keys};

        /// Returns the server key of the key fixture file `name`.
        fn server(name: &str) -> ServerSigner {
            let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../../tests/fixtures/spki/keys")
                .join(name);
            let mut signers = spki_secret_keys(std::fs::read(path).unwrap(), name).unwrap();
            assert_eq!(signers.len(), 1);
            ServerSigner::spki(signers.remove(0)).unwrap()
        }

        let server_key = server("rsa-2048.pem");
        let other = server("rsa-8192.pem");
        let key = server_key.signer().metadata_key().to_owned();
        assert_eq!(server_key.signer().name(), "spki");

        let own = signature(&server_key);
        assert_eq!(own.len(), 256);
        assert!(has_signed(&server_key, Some(&dict_with(&key, own.clone()))));
        assert!(!has_signed(
            &server_key,
            Some(&dict_with(&key, signature(&other)))
        ));
        assert!(!has_signed(
            &server_key,
            Some(&dict_with(&key, own[1..].to_vec()))
        ));

        // The key signs on the blocking pool, and gives the bytes of the
        // signer that it holds.
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../tests/fixtures/spki/keys/rsa-2048.pem");
        let text = std::fs::read_to_string(path).unwrap();
        let direct = SpkiSigner::from_pkcs8_pem(&text).unwrap();
        assert_eq!(ostrya_rt::block_on(direct.sign(PAYLOAD)).unwrap(), own);
    }

    /// Returns the fixed-width `r || s` form of a DER ECDSA signature over
    /// P-256.
    #[cfg(feature = "sign-spki")]
    fn fixed_width(der: &[u8]) -> Vec<u8> {
        // SEQUENCE { INTEGER r, INTEGER s }, each short-form length.
        assert_eq!(der[0], 0x30);
        let mut out = Vec::with_capacity(64);
        let mut at = 2;
        for _ in 0..2 {
            assert_eq!(der[at], 0x02);
            let len = usize::from(der[at + 1]);
            let int = &der[at + 2..at + 2 + len];
            let int = &int[int.len().saturating_sub(32)..];
            out.extend(std::iter::repeat_n(0u8, 32 - int.len()));
            out.extend_from_slice(int);
            at += 2 + len;
        }
        out
    }

    /// Returns `true` if `gpg` answers.
    ///
    /// If `gpg` does not answer, a test that needs it skips. If
    /// `OSTRYA_REQUIRE_GNUPG` is set, the absence of `gpg` fails the test.
    #[cfg(feature = "sign-gpg")]
    fn gpg_or_skip() -> bool {
        if crate::gpg::tests::gpg_available() {
            return true;
        }
        assert!(
            std::env::var_os("OSTRYA_REQUIRE_GNUPG").is_none(),
            "OSTRYA_REQUIRE_GNUPG is set and `gpg` is not available"
        );
        eprintln!("skipping: gpg not available");
        false
    }

    /// The GPG search recognizes a signature that the server key made, stored
    /// or from the merge. It does not recognize a signature from another key.
    #[cfg(feature = "sign-gpg")]
    #[test]
    fn a_gpg_key_recognizes_its_own_signature() {
        use crate::gpg::GpgSigner;
        use crate::gpg::tests::KeyFixture;

        if !gpg_or_skip() {
            return;
        }
        let home = KeyFixture::new("Receive Server <server@example.org>");
        let stranger = KeyFixture::new("Stranger <stranger@example.org>");
        let server = ostrya_rt::block_on(ServerSigner::gpg(
            GpgSigner::new("server@example.org").with_homedir(&home.dir),
        ))
        .unwrap();
        let key = server.signer().metadata_key().to_owned();
        assert_eq!(key, "ostree.gpgsigs");

        let own = signature(&server);
        assert!(has_signed(&server, Some(&dict_with(&key, own.clone()))));
        let foreign = stranger.sign(PAYLOAD);
        assert!(!has_signed(
            &server,
            Some(&dict_with(&key, foreign.clone()))
        ));

        // Through the merge: a stored signature from another key and an
        // incoming signature from the server key.
        let merged = super::super::merge::merge_detached(
            Some(dict_with(&key, foreign)),
            dict_with(&key, own),
            &[],
        )
        .unwrap();
        assert!(has_signed(&server, Some(&merged)));
    }

    /// A blob that the GPG verifier refuses does not hide the signature of the
    /// key beside it. The blobs are one over the blob limit, one with too many
    /// signature packets, and garbage.
    #[cfg(feature = "sign-gpg")]
    #[test]
    fn a_gpg_signature_beside_a_refused_blob_is_recognized() {
        use crate::gpg::GpgSigner;
        use crate::gpg::tests::KeyFixture;

        if !gpg_or_skip() {
            return;
        }
        let home = KeyFixture::new("Receive Server <server@example.org>");
        let server = ostrya_rt::block_on(ServerSigner::gpg(
            GpgSigner::new("server@example.org").with_homedir(&home.dir),
        ))
        .unwrap();
        let key = server.signer().metadata_key().to_owned();
        let own = signature(&server);
        for bad in [oversize_blob(), own.repeat(65), b"garbage".to_vec()] {
            let mixed = dict_of(&key, vec![bad.clone(), own.clone()]);
            assert!(has_signed(&server, Some(&mixed)));
            assert!(!has_signed(&server, Some(&dict_of(&key, vec![bad]))));
        }
    }

    /// `ServerSigner::gpg` refuses a selector that names no secret key and a
    /// selector that names two.
    #[cfg(feature = "sign-gpg")]
    #[test]
    fn a_gpg_selector_names_exactly_one_key() {
        use crate::gpg::GpgSigner;
        use crate::gpg::tests::KeyFixture;

        if !gpg_or_skip() {
            return;
        }
        let home = KeyFixture::new("Server A <a@example.org>");
        home.add_key("Server B <b@example.org>");

        let refused = |selector: &str, part: &str| {
            let err = ostrya_rt::block_on(ServerSigner::gpg(
                GpgSigner::new(selector).with_homedir(&home.dir),
            ))
            .unwrap_err();
            assert!(
                matches!(&err, Error::Signature(m) if m.contains(part)),
                "{err}"
            );
        };
        refused("Server", "more than one secret key");
        refused("nobody@example.org", "no secret key");

        // The fingerprint names one of the two.
        let fingerprint = home.fingerprint();
        ostrya_rt::block_on(ServerSigner::gpg(
            GpgSigner::new(fingerprint).with_homedir(&home.dir),
        ))
        .unwrap();
    }
}