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
//! Error types and converters.
use std::io;
use thiserror::Error;
use crate::metadata::{MetadataPath, MetadataVersion, TargetPath};
/// Alias for `Result<T, Error>`.
pub type Result<T> = std::result::Result<T, Error>;
/// Error type for all TUF related errors.
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum Error {
/// The metadata had a bad signature.
#[error("metadata {0} has a bad signature")]
BadSignature(MetadataPath),
/// There was a problem encoding or decoding.
#[error("encoding: {0}")]
Encoding(String),
/// Metadata was expired.
#[error("expired {0} metadata")]
ExpiredMetadata(MetadataPath),
/// An illegal argument was passed into a function.
#[error("illegal argument: {0}")]
IllegalArgument(String),
/// Generic error for HTTP connections.
#[error("http error for {uri}")]
Http {
/// URI Resource that resulted in the error.
uri: String,
/// The error.
#[source]
err: http::Error,
},
/// Errors that can occur parsing HTTP streams.
#[cfg(feature = "hyper")]
#[error("hyper error for {uri}")]
Hyper {
/// URI Resource that resulted in the error.
uri: String,
/// The error.
#[source]
err: hyper::Error,
},
/// Unexpected HTTP response status.
#[error("error getting {uri}: request failed with status code {code}")]
BadHttpStatus {
/// URI Resource that resulted in the error.
uri: String,
/// HTTP status code.
code: http::StatusCode,
},
/// An IO error occurred.
#[error(transparent)]
Io(#[from] io::Error),
/// An IO error occurred for a path.
#[error("IO error on path {path}")]
IoPath {
/// Path where the error occurred.
path: std::path::PathBuf,
/// The IO error.
#[source]
err: io::Error,
},
/// A json serialization error occurred.
#[error(transparent)]
Json(#[from] serde_json::error::Error),
/// There were no available hash algorithms.
#[error("no supported hash algorithm")]
NoSupportedHashAlgorithm,
/// The metadata was not found.
#[error("metadata {path} at version {version} not found")]
MetadataNotFound {
/// The metadata path.
path: MetadataPath,
/// The metadata version.
version: MetadataVersion,
},
/// The target was not found.
#[error("target {0} not found")]
TargetNotFound(TargetPath),
/// Opaque error type, to be interpreted similar to HTTP 500. Something went wrong, and you may
/// or may not be able to do anything about it.
#[error("opaque: {0}")]
Opaque(String),
/// There is no known or available key type.
#[error("unknown key type: {0}")]
UnknownKeyType(String),
/// There is no known or available signature scheme.
#[error("unknown signature scheme: {0}")]
UnknownSignatureScheme(String),
/// The metadata threshold cannot equal 0.
#[error("metadata {0} threshold must be greater than zero")]
MetadataThresholdMustBeGreaterThanZero(MetadataPath),
/// The metadata's version must be less than `u32::MAX`.
#[error("metadata {0} version should be less than max u32")]
MetadataVersionMustBeSmallerThanMaxU32(MetadataPath),
/// The metadata was not signed with enough valid signatures.
#[error(
"metadata {role} signature threshold not met: {number_of_valid_signatures}/{threshold}"
)]
MetadataMissingSignatures {
/// The signed metadata.
role: MetadataPath,
/// The number of signatures which are valid.
number_of_valid_signatures: u32,
/// The minimum number of valid signatures.
threshold: u32,
},
/// Attempted to update metadata with an older version.
#[error(
"attempted to roll back metadata {role} from version {trusted_version} to {new_version}"
)]
AttemptedMetadataRollBack {
/// The metadata.
role: MetadataPath,
/// The trusted metadata's version.
trusted_version: u32,
/// The new metadata's version.
new_version: u32,
},
/// The parent metadata expected the child metadata to be at one version, but was found to be at
/// another version.
#[error("metadata {parent_role} expected metadata {child_role} version {expected_version}, but found {new_version}")]
WrongMetadataVersion {
/// The parent metadata that contains the child metadata's version.
parent_role: MetadataPath,
/// The child metadata that has an unexpected version.
child_role: MetadataPath,
/// The expected version of the child metadata.
expected_version: u32,
/// The actual version of the child metadata.
new_version: u32,
},
/// The parent metadata does not contain a description of the child metadata.
#[error("metadata {parent_role} missing description of {child_role}")]
MissingMetadataDescription {
/// The parent metadata that contains the child metadata's description.
parent_role: MetadataPath,
/// The child metadata that should have been contained in the parent.
child_role: MetadataPath,
},
/// The parent metadata did not delegate to the child role.
#[error("{parent_role} delegation to {child_role} is not authorized")]
UnauthorizedDelegation {
/// The parent metadata that did not delegate to the child.
parent_role: MetadataPath,
/// That child metadata that was not delegated to by the parent.
child_role: MetadataPath,
},
}
pub(crate) fn derp_error_to_error(err: derp::Error) -> Error {
Error::Encoding(format!("DER: {:?}", err))
}