ostrya 0.19.1

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
//! The signature verification of a pull.
//!
//! A pull holds two independent policies: one for the commits that it verifies
//! and one for the summary of the remote. The pull builds them once, from the
//! [`PullTrust`] of the pull.
//!
//! The crate module `verify` holds the policy, its two axes, the trusted keys,
//! and the resolution of the trust from the config of a remote. The receive
//! path shares that module. [`PullVerify`](crate::PullVerify) states the rules
//! that a caller sees.

use std::sync::{Arc, OnceLock};

use ostrya_core::{Checksum, Value};

use crate::error::{Error, Result};
use crate::repo::Repo;
use crate::verify::{CrlScope, Found, Payload, Policy, PullTrust, examine, with_details};

/// The signature verification of one pull.
pub(crate) struct Verification {
    /// The policy for each commit that the pull carries, except an attachment
    /// in no other role.
    commit: Policy,
    /// The policy for the summary of the remote.
    summary: Policy,
    /// The X.509 CRL cache that the checks read, and write if the pull
    /// brings objects into the repository of the cache.
    crls: CrlScope,
    /// The dict of `summary.sig`, after a summary check that passed. Each
    /// commit check and each delta check gets it as a context dict.
    summary_dict: OnceLock<Arc<Value>>,
}

impl Verification {
    /// Creates the verification of a pull from its trust.
    ///
    /// The trust holds its verifiers, so this function reads no key source.
    /// The verification reads no X.509 CRL cache.
    pub(crate) fn from_trust(trust: PullTrust) -> Verification {
        let (commit, summary) = trust.into_policies();
        Verification {
            commit,
            summary,
            crls: CrlScope::default(),
            summary_dict: OnceLock::new(),
        }
    }

    /// Returns this verification with the X.509 CRL cache of `repo`, the
    /// destination of the pull. Each check reads the cache, and writes to it
    /// the CRLs that the X.509 check selects.
    pub(crate) fn caching_crls(self, repo: &Repo) -> Verification {
        Verification {
            crls: CrlScope::caching(repo),
            ..self
        }
    }

    /// Returns this verification with the X.509 CRL cache of `repo`, which
    /// each check reads and does not write: a check that brings nothing into
    /// `repo`.
    pub(crate) fn reading_crls(self, repo: &Repo) -> Verification {
        Verification {
            crls: CrlScope::reading(repo),
            ..self
        }
    }

    /// Returns the CRL scope of a commit check or a delta check: the cache,
    /// then the dict of `summary.sig` if a summary check passed.
    fn commit_crls(&self) -> CrlScope {
        match self.summary_dict.get() {
            Some(dict) => self.crls.clone().with_context(Arc::clone(dict)),
            None => self.crls.clone(),
        }
    }

    /// Returns the dict of `summary.sig` of a summary check of this pull that
    /// passed, if the pull keeps one.
    pub(crate) fn summary_dict(&self) -> Option<Arc<Value>> {
        self.summary_dict.get().cloned()
    }

    /// Keeps `dict` as the dict of `summary.sig` of this pull: the dict of a
    /// summary check that passed under the trust of this pull in another
    /// verification, for example in the probe of a collection pull. Each
    /// later commit check and delta check gets it as a context dict. A dict
    /// that the pull keeps already stays.
    pub(crate) fn keep_summary_dict(&self, dict: Arc<Value>) {
        let _ = self.summary_dict.set(dict);
    }

    /// Returns `true` if this pull verifies its commits. An attachment in no
    /// other role gets no check.
    pub(crate) fn checks_commits(&self) -> bool {
        self.commit.applies()
    }

    /// Returns `true` if this pull verifies the summary of the remote.
    pub(crate) fn checks_summary(&self) -> bool {
        self.summary.applies()
    }

    /// Verifies one commit against the commit policy.
    ///
    /// `detached` is the detached metadata of the commit, which holds its
    /// signatures. `bytes` is the serialized commit. Each verifier runs on the
    /// blocking pool, as [`Policy::check`] states. The dict of `summary.sig`
    /// of a summary check that passed is a context dict of the check.
    pub(crate) async fn check_commit<'p>(
        &self,
        checksum: &Checksum,
        bytes: impl Into<Payload<'p>>,
        detached: Option<&Value>,
    ) -> Result<()> {
        if !self.commit.applies() {
            return Ok(());
        }
        self.commit
            .check(
                &format!("commit {checksum}"),
                bytes,
                detached,
                &self.commit_crls(),
            )
            .await
    }

    /// Verifies the summary of the remote against the summary policy, and
    /// gives back the `summary.sig` bytes `signature`.
    ///
    /// A policy that applies needs both files. If the source publishes no
    /// summary, or a summary with no `summary.sig`, this function fails with
    /// [`Error::Signature`]. The message names the missing file. The `ostree`
    /// command reports the same two cases. A policy that does not apply reads
    /// nothing.
    ///
    /// The parse of `summary.sig` and each verifier run on the blocking pool,
    /// because each file can be as large as the read cap of a summary. The
    /// verifiers share the `Arc` of `summary`, so the check makes no copy of
    /// the summary.
    ///
    /// After a check that passes, the verification keeps the dict of
    /// `summary.sig`, and gives it to each later commit check and delta check
    /// as a context dict.
    pub(crate) async fn check_summary(
        &self,
        summary: Option<&Arc<Vec<u8>>>,
        signature: Option<Vec<u8>>,
    ) -> Result<Option<Vec<u8>>> {
        if !self.summary.applies() {
            return Ok(signature);
        }
        let Some(summary) = summary else {
            return Err(no_summary());
        };
        let Some(signature) = signature else {
            return Err(no_summary_sig());
        };
        let (dict, signature) = super::parse_detached(signature).await;
        let dict = dict?;
        self.summary
            .check("the summary", summary, dict.as_ref(), &self.crls)
            .await?;
        if let Some(dict) = dict {
            let _ = self.summary_dict.set(Arc::new(dict));
        }
        Ok(Some(signature))
    }

    /// Refuses a summary check that misses a file, before the pull reads a
    /// file.
    ///
    /// `summary` and `signature` tell if the source publishes `summary` and
    /// `summary.sig`. A policy that applies needs both files, and the error
    /// is the error of [`check_summary`](Self::check_summary) for the same
    /// files. A policy that does not apply passes.
    pub(crate) fn check_summary_files(&self, summary: bool, signature: bool) -> Result<()> {
        if !self.summary.applies() {
            return Ok(());
        }
        if !summary {
            return Err(no_summary());
        }
        if !signature {
            return Err(no_summary_sig());
        }
        Ok(())
    }

    /// Verifies a fetched static delta against the sign-api axis of the commit
    /// policy.
    ///
    /// The signatures cover the raw bytes of the superblock. Only the sign api
    /// signs a delta, so the GPG axis has no part.
    ///
    /// A delta with no signature that the axis can read passes. The removal of
    /// a signature does not let a pull accept other bytes:
    ///
    /// - The superblock names what the delta produces.
    /// - The advertisement names the superblock.
    /// - The commit policy applies to the delivered commit: a target or a
    ///   commit of a chain. The pull does not call this function for the
    ///   delta of an attachment.
    ///
    /// If a delta carries signatures and no signature is from a trusted key,
    /// this function fails with [`Error::Signature`]. It also fails with
    /// [`Error::Signature`] before a verifier runs if the delta carries more
    /// than [`MAX_SIGNATURES_PER_ENGINE`](crate::sign::MAX_SIGNATURES_PER_ENGINE)
    /// signatures under the key of one engine of the axis.
    ///
    /// Each verifier runs on the blocking pool, as [`examine`] states. The
    /// dict of `summary.sig` of a summary check that passed is a context dict
    /// of the check.
    pub(crate) async fn check_delta<'p>(
        &self,
        name: &str,
        superblock: impl Into<Payload<'p>>,
        signatures: Option<&Value>,
    ) -> Result<()> {
        let Some(engines) = self.commit.sign_axis() else {
            return Ok(());
        };
        match examine(engines, superblock, signatures, &self.commit_crls()).await? {
            Found::Valid | Found::Nothing => Ok(()),
            Found::Untrusted(details) => Err(Error::Signature(with_details(
                format!("static delta {name}: no signature is from a trusted key"),
                &details,
            ))),
        }
    }
}

/// Returns the error of a summary check with no `summary` to check.
fn no_summary() -> Error {
    Error::Signature("summary verification is enabled, but no summary is published".into())
}

/// Returns the error of a summary check with no `summary.sig` to check.
fn no_summary_sig() -> Error {
    Error::Signature("summary verification is enabled, but no summary.sig is published".into())
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::pull::PullVerify;
    use crate::repo::Repo;
    use crate::sign::{Ed25519Signer, Ed25519Verifier, Signer, Verifier, append_signature};
    use crate::verify::TrustedKeys;
    use crate::{CreateOptions, RepoMode};

    /// The base64 of a 64-byte ed25519 secret key (seed, then public key).
    const SECRET_B64: &str =
        "o74ME/dmhvDeYf64dDJQY8kX2piK0M/nyIRWVi30i6DCOzRsHVcvgYToz6zOb5OvK/v8nH6KfLR3dfdsn6ZSyQ==";
    /// The matching 32-byte ed25519 public key.
    const PUBLIC_B64: &str = "wjs0bB1XL4GE6M+szm+Tryv7/Jx+iny0d3X3bJ+mUsk=";
    /// Another ed25519 secret key.
    const OTHER_SECRET_B64: &str =
        "5ILWxT+l9G/u3h0BptRpmSi35C9uog7YDdD+Fp1Xk+Hz52p0NlYh6xBA73kJEJKhKbbnjcE0rsWA5XA/K5Sq5Q==";
    /// The matching public key of [`OTHER_SECRET_B64`].
    const OTHER_PUBLIC_B64: &str = "8+dqdDZWIesQQO95CRCSoSm2543BNK7FgOVwPyuUquU=";

    /// The bytes that each test signs: a commit, a summary, or a superblock.
    const PAYLOAD: &[u8] = b"the signed bytes";

    /// The name of the commit in a message.
    fn checksum() -> Checksum {
        Checksum::from_bytes([0x5a; 32])
    }

    /// The trusted keys of one ed25519 public key.
    fn keys(public: &str) -> TrustedKeys {
        let verifier = Ed25519Verifier::new(
            [ostrya_core::base64::decode(public).unwrap()],
            Vec::<Vec<u8>>::new(),
        )
        .unwrap();
        TrustedKeys::new(vec![Arc::new(verifier) as Arc<dyn Verifier>]).unwrap()
    }

    /// The ed25519 signature of `secret` over [`PAYLOAD`], in its dict.
    fn signed(secret: &str) -> Value {
        let signer = Ed25519Signer::from_base64(secret).unwrap();
        let blob = ostrya_rt::block_on(signer.sign(PAYLOAD)).unwrap();
        let mut dict = Value::Array(Vec::new());
        append_signature(&mut dict, signer.metadata_key(), blob).unwrap();
        dict
    }

    /// The bytes of a `summary.sig` that holds the signature of `secret` over
    /// [`PAYLOAD`].
    fn summary_sig(secret: &str) -> Vec<u8> {
        crate::summary::serialize_signature_dict(&signed(secret)).unwrap()
    }

    /// Keys from `TrustedKeys::new` are the whole trusted set of the commits.
    ///
    /// The proof is by construction. A verification from a trust takes no
    /// [`Repo`], so it cannot read the keyring of a repository or the config
    /// of a remote. The system key store is under `/etc/ostree` and
    /// `/usr/share/ostree`, and a test cannot write it to add the other key.
    /// The verification reads no key source at all: it holds the verifiers
    /// that the trust holds. So a commit signed with another key fails, on any
    /// host.
    #[test]
    fn explicit_keys_are_the_whole_trusted_set() {
        let verification = Verification::from_trust(PullTrust::commits(keys(PUBLIC_B64)));
        assert!(verification.checks_commits());
        assert!(!verification.checks_summary());
        let (own, other) = (signed(SECRET_B64), signed(OTHER_SECRET_B64));
        let (own, other, unsigned, summary) = ostrya_rt::block_on(async {
            (
                verification
                    .check_commit(&checksum(), PAYLOAD, Some(&own))
                    .await,
                verification
                    .check_commit(&checksum(), PAYLOAD, Some(&other))
                    .await,
                verification.check_commit(&checksum(), PAYLOAD, None).await,
                verification.check_summary(None, None).await.map(drop),
            )
        });
        own.expect("a commit signed with the trusted key passes");
        let err = other.expect_err("a commit signed with another key fails");
        assert!(
            matches!(&err, Error::Signature(m)
                if m.contains("no sign-api signature is from a trusted key")),
            "{err}"
        );
        let err = unsigned.expect_err("an unsigned commit fails");
        assert!(
            matches!(&err, Error::Signature(m) if m.contains("carries no signature")),
            "{err}"
        );
        summary.expect("the summary is not checked");
    }

    /// The delta check runs each verifier on the blocking pool, so a verifier
    /// that works in the call does not hold the async executor. The check
    /// gives a superblock in an [`Arc`] to the verifier with no copy.
    #[test]
    fn the_delta_check_verifies_on_the_blocking_pool() {
        use crate::verify::trust::tests::{ThreadRecorder, dict_with};

        let recorder = ThreadRecorder::new("ostree.sign.ed25519");
        let keys = TrustedKeys::new(vec![Arc::new(recorder.clone()) as Arc<dyn Verifier>]).unwrap();
        let verification = Verification::from_trust(PullTrust::commits(keys));
        let dict = dict_with("ostree.sign.ed25519", vec![7; 64]);
        let superblock = Arc::new(PAYLOAD.to_vec());
        ostrya_rt::block_on(verification.check_delta("delta", &superblock, Some(&dict))).unwrap();
        recorder.assert_one_foreign_call();
        recorder.assert_no_copy_of(&superblock);
    }

    /// The summary check gives a summary in an [`Arc`] to the verifier with
    /// no copy.
    #[test]
    fn the_summary_check_gives_the_summary_with_no_copy() {
        use crate::verify::trust::tests::{ThreadRecorder, dict_with};

        let recorder = ThreadRecorder::new("ostree.sign.ed25519");
        let keys = TrustedKeys::new(vec![Arc::new(recorder.clone()) as Arc<dyn Verifier>]).unwrap();
        let verification = Verification::from_trust(PullTrust::unverified().with_summary(keys));
        let dict = dict_with("ostree.sign.ed25519", vec![7; 64]);
        let signature = crate::summary::serialize_signature_dict(&dict).unwrap();
        let summary = Arc::new(PAYLOAD.to_vec());
        ostrya_rt::block_on(verification.check_summary(Some(&summary), Some(signature))).unwrap();
        recorder.assert_one_foreign_call();
        recorder.assert_no_copy_of(&summary);
    }

    /// A trust with summary keys alone checks the summary, and no commit and
    /// no static delta. `with_summary` replaces the summary keys of the trust.
    #[test]
    fn a_summary_trust_checks_the_summary_alone() {
        let verification = Verification::from_trust(
            PullTrust::unverified()
                .with_summary(keys(OTHER_PUBLIC_B64))
                .with_summary(keys(PUBLIC_B64)),
        );
        assert!(!verification.checks_commits());
        assert!(verification.checks_summary());
        let other = signed(OTHER_SECRET_B64);
        let (unsigned, other, delta) = ostrya_rt::block_on(async {
            (
                verification.check_commit(&checksum(), PAYLOAD, None).await,
                verification
                    .check_commit(&checksum(), PAYLOAD, Some(&other))
                    .await,
                verification
                    .check_delta("delta", PAYLOAD, Some(&other))
                    .await,
            )
        });
        unsigned.expect("an unsigned commit passes");
        other.expect("a commit signed with another key passes");
        delta.expect("a delta signed with another key passes");

        let own_sig = summary_sig(SECRET_B64);
        let other = summary_sig(OTHER_SECRET_B64);
        let summary = Arc::new(PAYLOAD.to_vec());
        let (own, other, missing) = ostrya_rt::block_on(async {
            (
                verification
                    .check_summary(Some(&summary), Some(own_sig.clone()))
                    .await,
                verification
                    .check_summary(Some(&summary), Some(other))
                    .await,
                verification.check_summary(Some(&summary), None).await,
            )
        });
        assert_eq!(
            own.expect("a summary signed with the last summary key passes"),
            Some(own_sig),
            "the check gives back the summary.sig bytes"
        );
        let err = other.expect_err("a summary signed with a replaced key fails");
        assert!(
            matches!(&err, Error::Signature(m)
                if m.contains("no sign-api signature is from a trusted key")),
            "{err}"
        );
        let err = missing.expect_err("a summary with no summary.sig fails");
        assert!(
            matches!(&err, Error::Signature(m)
                if m == "summary verification is enabled, but no summary.sig is published"),
            "{err}"
        );
    }

    /// `PullTrust::from_remote` reads the keys and the switches of the
    /// section of the remote. The fields of `PullVerify` override the
    /// switches.
    #[test]
    fn from_remote_reads_the_remote_section() {
        let dir = std::env::temp_dir().join(format!(
            "ostrya-pull-trust-{}-{}",
            std::process::id(),
            crate::write::unique()
        ));
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let outcome = ostrya_rt::block_on(async {
            Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            let mut config = std::fs::read_to_string(root.join("config")).unwrap();
            config.push_str(&format!(
                "\n[remote \"origin\"]\nurl=http://localhost/\ngpg-verify=false\n\
                 sign-verify=ed25519\nverification-ed25519-key={PUBLIC_B64}\n"
            ));
            std::fs::write(root.join("config"), config).unwrap();
            let repo = Repo::open(&root).await.unwrap();
            let plain = PullTrust::from_remote(&repo, "origin", &PullVerify::default()).await;
            let summary = PullVerify {
                sign_summary: Some(true),
                ..PullVerify::default()
            };
            let summary = PullTrust::from_remote(&repo, "origin", &summary).await;
            (plain, summary)
        });
        std::fs::remove_dir_all(&dir).unwrap();
        let (plain, summary) = outcome;

        let verification = Verification::from_trust(plain.unwrap());
        assert!(verification.checks_commits());
        assert!(!verification.checks_summary());
        let (own, other) = (signed(SECRET_B64), signed(OTHER_SECRET_B64));
        let (own, unsigned, other) = ostrya_rt::block_on(async {
            (
                verification
                    .check_commit(&checksum(), PAYLOAD, Some(&own))
                    .await,
                verification.check_commit(&checksum(), PAYLOAD, None).await,
                verification
                    .check_commit(&checksum(), PAYLOAD, Some(&other))
                    .await,
            )
        });
        own.expect("a commit signed with the key of the section passes");
        unsigned.expect_err("an unsigned commit fails");
        // The system key store adds its keys to the keys of the section. On a
        // host with no store of its own, the other key is not trusted. The
        // test reads the store only if the commit passes.
        if other.is_ok() {
            assert!(
                crate::sign::load_sign_keys("ed25519").is_ok_and(|store| !store.trusted.is_empty()),
                "a commit signed with another key passes, and the system key \
                 store holds no ed25519 key"
            );
        }

        let verification = Verification::from_trust(summary.unwrap());
        assert!(verification.checks_commits());
        assert!(verification.checks_summary());
        let own = summary_sig(SECRET_B64);
        let summary = Arc::new(PAYLOAD.to_vec());
        ostrya_rt::block_on(verification.check_summary(Some(&summary), Some(own)))
            .expect("a summary signed with the key of the section passes");
    }

    /// `PullTrust::from_remote` refuses a name that `valid_remote_name`
    /// refuses, before it reads a key source. The keyring paths hold the
    /// name, so a name with `/` names a keyring outside the repository or
    /// outside the remotes directory.
    #[test]
    fn from_remote_refuses_an_invalid_remote_name() {
        let dir = std::env::temp_dir().join(format!(
            "ostrya-pull-trust-name-{}-{}",
            std::process::id(),
            crate::write::unique()
        ));
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let outcome = ostrya_rt::block_on(async {
            let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            let mut refused = Vec::new();
            for name in ["../../x", ""] {
                let result = PullTrust::from_remote(&repo, name, &PullVerify::default()).await;
                refused.push((name, result));
            }
            refused
        });
        std::fs::remove_dir_all(&dir).unwrap();
        for (name, result) in outcome {
            let err = result.expect_err("an invalid remote name is refused");
            assert!(
                matches!(&err, Error::InvalidInput(m) if m.contains("invalid remote name")),
                "{name:?}: {err}"
            );
        }
    }
}