weirdnet_update/error.rs
1//! Errors. None of them carry key material.
2
3use crate::envelope::Domain;
4
5/// Result alias for this crate.
6pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8/// Everything that can go wrong while building, signing or checking update metadata.
9#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
10pub enum Error {
11 /// CBOR encoding failed.
12 #[error("encoding failed: {0}")]
13 Encode(String),
14 /// CBOR decoding failed or the input was malformed.
15 #[error("decoding failed: {0}")]
16 Decode(String),
17 /// A key, signature or hash could not be parsed, or is weak.
18 #[error("invalid key material: {0}")]
19 InvalidKey(String),
20 /// The OS random generator failed.
21 #[error("randomness unavailable")]
22 Random,
23 /// A record is structurally invalid (empty ids, bad thresholds, bad paths, limits).
24 #[error("invalid {what}: {reason}")]
25 Invalid {
26 /// What was being validated.
27 what: &'static str,
28 /// Why it was refused.
29 reason: String,
30 },
31 /// An envelope was signed for a different purpose.
32 #[error("expected a {expected} record, got {found}")]
33 WrongDomain {
34 /// The domain the caller asked for.
35 expected: Domain,
36 /// The domain in the envelope.
37 found: Domain,
38 },
39 /// Too few distinct trusted keys signed.
40 #[error("{domain}: {found} valid signature(s) from trusted keys, {needed} needed")]
41 Threshold {
42 /// The record kind.
43 domain: Domain,
44 /// Valid signatures from distinct trusted keys.
45 found: usize,
46 /// The threshold.
47 needed: usize,
48 },
49 /// The root's keys do not hash to the fingerprint compiled into the app.
50 #[error("root does not match the pinned fingerprint")]
51 PinMismatch,
52 /// A root update does not follow the trusted root.
53 #[error("root version {found} does not follow trusted version {current}")]
54 RootVersion {
55 /// The trusted root version.
56 current: u32,
57 /// The offered root version.
58 found: u32,
59 },
60 /// A root update names a different org.
61 #[error("root update changes the org")]
62 RootOrg,
63 /// A record has expired (or is not valid yet).
64 #[error("{0} record expired or not yet valid")]
65 Expired(Domain),
66 /// The record is for a different app or channel than the caller asked for.
67 #[error("record is for {found}, expected {expected}")]
68 WrongTarget {
69 /// `app/channel` the caller expects.
70 expected: String,
71 /// `app/channel` in the record.
72 found: String,
73 },
74 /// The sequence number is not above the last one seen (replay or rollback).
75 #[error("seq {found} is not above the last seen {last}")]
76 Replay {
77 /// Last seq seen for this app and channel.
78 last: u64,
79 /// Offered seq.
80 found: u64,
81 },
82 /// Two different manifests claim the same seq.
83 #[error("a different manifest was already seen with seq {0}")]
84 Equivocation(u64),
85 /// The update is listed as revoked by a trusted freshness record.
86 #[error("update {0} is revoked")]
87 Revoked(String),
88 /// The update failed its boot-health check on this device before.
89 #[error("update {0} is marked bad on this device")]
90 KnownBad(String),
91 /// The freshness record names another manifest.
92 #[error("freshness record does not match this manifest")]
93 FreshnessMismatch,
94 /// The root trusts timestamp keys but no valid freshness record came with the manifest.
95 #[error("a freshness record is required by the root")]
96 FreshnessRequired,
97 /// A lower version without a rollback record.
98 #[error("downgrade from {installed} to {offered} without a rollback record")]
99 Downgrade {
100 /// Installed version.
101 installed: String,
102 /// Offered version.
103 offered: String,
104 },
105 /// The installed base release is not one the update applies to.
106 #[error("update does not apply to base release {0}")]
107 BaseMismatch(String),
108 /// The running client version is outside the update's range.
109 #[error("update does not apply to client version {0}")]
110 ClientVersion(String),
111 /// The release key is not delegated for this app or channel, or its delegation expired.
112 #[error("no release key is delegated for {0}")]
113 NotDelegated(String),
114 /// This client does not accept the update kind.
115 #[error("update kind {0} is not supported by this client")]
116 UnsupportedKind(&'static str),
117 /// A downloaded file failed its size or hash check.
118 #[error("file {path}: {reason}")]
119 File {
120 /// Path from the manifest.
121 path: String,
122 /// What failed.
123 reason: String,
124 },
125 /// A targeting rule is invalid.
126 #[error("targeting rule: {0}")]
127 Rule(String),
128}
129
130impl Error {
131 pub(crate) fn invalid(what: &'static str, reason: impl Into<String>) -> Self {
132 Self::Invalid { what, reason: reason.into() }
133 }
134}