-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathsecurity.rs
More file actions
268 lines (243 loc) · 9.64 KB
/
security.rs
File metadata and controls
268 lines (243 loc) · 9.64 KB
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
use agama_l10n::helpers::gettext_noop;
use agama_utils::{actor::Handler, api::question::QuestionSpec, question};
use i18n_format::i18n_format;
use zypp_agama::callbacks::security;
use crate::{callbacks::ask_software_question, state::RepoKey};
#[derive(Clone)]
pub struct Security {
questions: Handler<question::Service>,
trusted_gpg_keys: Vec<RepoKey>,
unsigned_repos: Vec<String>,
}
impl Security {
pub fn new(questions: Handler<question::Service>) -> Self {
Self {
questions,
trusted_gpg_keys: vec![],
unsigned_repos: vec![],
}
}
pub fn set_trusted_gpg_keys(&mut self, trusted_gpg_keys: Vec<RepoKey>) {
tracing::info!("Configured trusted GPG keys: {:?}", trusted_gpg_keys);
self.trusted_gpg_keys = trusted_gpg_keys;
}
pub fn set_unsigned_repos(&mut self, unsigned_repos: Vec<String>) {
tracing::info!("Allowed unsigned repositories: {:?}", unsigned_repos);
self.unsigned_repos = unsigned_repos;
}
}
impl security::Callback for Security {
fn unsigned_file(&self, file: String, repository_alias: String) -> bool {
tracing::info!(
"unsigned_file callback: file='{}', repository_alias='{}'",
file,
repository_alias
);
if self.unsigned_repos.contains(&repository_alias) {
return true;
}
// TODO: localization for text when parameters in gextext will be solved
let text = if repository_alias.is_empty() {
format!(
"The file {file} is not digitally signed. The origin \
and integrity of the file cannot be verified. Use it anyway?"
)
} else {
format!(
"The file {file} from {repository_alias} is not digitally signed. The origin \
and integrity of the file cannot be verified. Use it anyway?"
)
};
let question = QuestionSpec::new(&text, "software.unsigned_file")
.with_yes_no_actions()
.with_data(&[("filename", file.as_str())]);
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return false;
};
answer.action == "Yes"
}
fn accept_key(
&self,
key_id: String,
key_name: String,
key_fingerprint: String,
repository_alias: String,
) -> security::GpgKeyTrust {
tracing::info!(
"accept_key callback: key_id='{}', key_name='{}', key_fingerprint='{}'",
key_id,
key_name,
key_fingerprint,
);
let predefined = self
.trusted_gpg_keys
.iter()
.any(|key| key.alias == repository_alias && key.fingerprint == key_fingerprint);
if predefined {
tracing::info!("GPG key trusted as specified in profile");
return security::GpgKeyTrust::Import;
}
let human_fingerprint = key_fingerprint
.chars()
.collect::<Vec<char>>()
.chunks(4)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<String>>()
.join(" ");
let text = i18n_format!(
// TRANSLATORS: substituting: key ID, (key name), fingerprint
"The key {0} ({1}) with fingerprint {2} is unknown. \
Do you want to trust this key?",
&key_id,
&key_name,
&human_fingerprint
);
let question = QuestionSpec::new(&text, "software.import_gpg")
.with_action_ids(&[gettext_noop("Trust"), gettext_noop("Skip")])
.with_data(&[
("id", key_id.as_str()),
("name", key_name.as_str()),
("fingerprint", human_fingerprint.as_str()),
])
.with_default_action("Skip");
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return security::GpgKeyTrust::Reject;
};
tracing::info!("Received answer: {:?}", answer);
answer
.action
.as_str()
.parse::<security::GpgKeyTrust>()
.unwrap_or(security::GpgKeyTrust::Reject)
}
fn unknown_key(&self, file: String, key_id: String, repository_alias: String) -> bool {
tracing::info!(
"unknown_key callback: file='{}', key_id='{}', repository_alias='{}'",
file,
key_id,
repository_alias
);
// TODO: localization for text when parameters in gextext will be solved
let text = if repository_alias.is_empty() {
format!(
"The file {file} is digitally signed with \
the following unknown GnuPG key: {key_id}. Use it anyway?"
)
} else {
format!(
"The file {file} from {repository_alias} is digitally signed with \
the following unknown GnuPG key: {key_id}. Use it anyway?"
)
};
let question = QuestionSpec::new(&text, "software.unknown_gpg")
.with_yes_no_actions()
.with_data(&[("filename", file.as_str()), ("id", key_id.as_str())]);
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return false;
};
answer.action == "Yes"
}
fn verification_failed(
&self,
file: String,
key_id: String,
key_name: String,
_key_fingerprint: String,
repository_alias: String,
) -> bool {
tracing::info!(
"verification_failed callback: file='{}', key_id='{}', key_name='{}', repository_alias='{}'",
file,
key_id,
key_name,
repository_alias
);
// TODO: localization for text when parameters in gextext will be solved
let text = if repository_alias.is_empty() {
format!(
"The file {file} is digitally signed with the \
following GnuPG key, but the integrity check failed: {key_id} ({key_name}). \
Use it anyway?"
)
} else {
// TODO: Originally it uses repository url and not alias. Does it matter?
format!(
"The file {file} from {repository_alias} is digitally signed with the \
following GnuPG key, but the integrity check failed: {key_id} ({key_name}). \
Use it anyway?"
)
};
let question = QuestionSpec::new(&text, "software.verification_failed")
.with_yes_no_actions()
.with_data(&[("filename", file.as_str())]);
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return false;
};
answer.action == "Yes"
}
fn checksum_missing(&self, file: String) -> bool {
tracing::info!("checksum_missing callback: file='{}'", file);
// TODO: localization for text when parameters in gextext will be solved
let text = format!(
"No checksum for the file {file} was found in the repository. This means that \
although the file is part of the signed repository, the list of checksums \
does not mention this file. Use it anyway?"
);
let question = QuestionSpec::new(&text, "software.digest.no_digest").with_yes_no_actions();
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return false;
};
answer.action == "Yes"
}
fn checksum_unknown(&self, file: String, checksum: String) -> bool {
tracing::info!(
"checksum_unknown callback: file='{}', checksum='{}'",
file,
checksum
);
let text = format!(
"The checksum of the file {file} is \"{checksum}\" but the expected checksum is \
unknown. This means that the origin and integrity of the file cannot be verified. \
Use it anyway?"
);
let question =
QuestionSpec::new(&text, "software.digest.unknown_digest").with_yes_no_actions();
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return false;
};
answer.action == "Yes"
}
fn checksum_wrong(&self, file: String, expected: String, actual: String) -> bool {
tracing::info!(
"checksum_wrong callback: file='{}', expected='{}', actual='{}'",
file,
expected,
actual
);
let text = format!(
"The expected checksum of file %{file} is \"%{actual}\" but it was expected to be \
\"%{expected}\". The file has changed by accident or by an attacker since the \
creater signed it. Use it anyway?"
);
let question =
QuestionSpec::new(&text, "software.digest.unknown_digest").with_yes_no_actions();
let result = ask_software_question(&self.questions, question);
let Ok(answer) = result else {
tracing::warn!("Failed to ask question {:?}", result);
return false;
};
answer.action == "Yes"
}
}