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
#![deny(warnings)]
#![warn(unused_extern_crates)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::unreachable)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::trivially_copy_pass_by_ref)]
use clap::Parser;
use clap::{Args, Subcommand};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
use std::str::FromStr;
use tracing::{debug, error, info, trace, warn};
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use url::Url;
use fido_mds::query::Query;
use fido_mds::FidoMds;
use fido_mds::{FIDO2, FIDO_MDS_URL};
#[derive(Debug, Args)]
pub struct CommonOpt {
#[clap(short, long)]
pub debug: bool,
/// Path to the MDS file
#[clap(short, long, default_value = "/tmp/mds.blob.jwt")]
pub path: PathBuf,
}
#[derive(Debug, Args)]
pub struct QueryOpt {
/// A query over the MDS. This query is "scim" like and supports logical conditions. Examples are
///
/// * "desc cnt yubikey"
///
/// * "aaguid eq X or aaguid ne Y"
///
/// * "status gte l1 and not (aaguid eq Z)"
///
/// Supported query types and operators are:
///
/// * aaguid eq \<uuid\>
///
/// * desc eq \<string\>
///
/// * desc cnt \<string\>
///
/// * status gte [valid|l1|l1+|l2|l2+|l3|l3+]
///
/// * states eq [valid|l1|l1+|l2|l2+|l3|l3+]
///
/// * transport eq [usb|nfc|lightning|ble|internal]
///
/// * uvm cnt [presence|pin_internal|pin_external|fingerprint_internal|handprint_internal|eyeprint_internal|voiceprint_internal|faceprint_internal|faceprint_internal|pattern_internal]
///
pub query: String,
#[clap(short, long)]
pub output_cert_roots: bool,
#[clap(short = 'x', long = "extra")]
extra_details: bool,
#[clap(long, hide(true))]
pub show_insecure_devices: bool,
#[clap(flatten)]
pub common: CommonOpt,
}
#[derive(Debug, Subcommand)]
#[clap(about = "Fido Metadata Service parsing tool")]
pub enum Opt {
/// Fetch the latest copy of the MDS and store it into the provided path.
Fetch(CommonOpt),
/// Parse and display the list of U2F devices from an MDS file.
ListU2f(CommonOpt),
/// Parse and display the list of Fido2 devices from an MDS file.
ListFido2 {
#[clap(flatten)]
common: CommonOpt,
/// Show extra details about devices.
#[clap(short = 'x', long = "extra")]
extra_details: bool,
},
/// Query and display metadata for FIDO2 devices based on a query expression.
Query(QueryOpt),
}
impl Opt {
fn debug(&self) -> bool {
match self {
Opt::Fetch(CommonOpt { debug, .. })
| Opt::ListU2f(CommonOpt { debug, .. })
| Opt::ListFido2 {
common: CommonOpt { debug, .. },
..
} => *debug,
Opt::Query(QueryOpt {
common: CommonOpt { debug, .. },
..
}) => *debug,
}
}
}
#[derive(Debug, clap::Parser)]
#[clap(about = "Fido Metadata Service parsing tool")]
pub struct CliParser {
#[clap(subcommand)]
pub commands: Opt,
}
fn main() {
let opt = CliParser::parse();
let fmt_layer = fmt::layer().with_writer(std::io::stderr);
let filter_result = EnvFilter::try_from_default_env().or_else(|_| {
if opt.commands.debug() {
EnvFilter::try_new("fido_mds=debug,fido_mds_tool=debug")
} else {
EnvFilter::try_new("fido_mds=info,fido_mds_tool=info")
}
});
let filter_layer = match filter_result {
Ok(fr) => fr,
Err(e) => {
eprintln!("Failed to setup tracing filter layer {:?}", e);
return;
}
};
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();
match opt.commands {
Opt::Fetch(CommonOpt { debug: _, path }) => {
let mds_url = match Url::parse(FIDO_MDS_URL) {
Ok(mdsurl) => mdsurl,
Err(e) => {
error!(err = ?e, "Error - invalid MDS URL");
return;
}
};
info!("Fetching from {} to {:?}", mds_url, path);
let mut f = match File::create(path) {
Ok(f) => f,
Err(e) => {
error!("Failed to open file for MDS - {:?}", e);
return;
}
};
let data = match reqwest::blocking::get(mds_url).and_then(|req| req.text()) {
Ok(data) => data,
Err(e) => {
error!("Failed to fetch MDS - {:?}", e);
return;
}
};
if let Err(e) = f.write_all(data.as_bytes()) {
error!("Failed to write file for MDS - {:?}", e);
} else {
info!("Ok!");
}
}
Opt::ListU2f(CommonOpt { debug: _, path }) => {
trace!("{:?}", path);
let s = match fs::read_to_string(path) {
Ok(s) => s,
Err(err) => {
tracing::error!(?err, "read_to_string");
return;
}
};
match FidoMds::from_str(&s) {
Ok(mds) => {
debug!("{} fido metadata avaliable", mds.u2f.len());
for fd in mds.u2f.iter() {
eprintln!("{fd}");
}
}
Err(err) => {
tracing::error!(?err, "mds from str");
}
}
}
Opt::ListFido2 {
common: CommonOpt { debug: _, path },
extra_details,
} => {
trace!("{:?}", path);
let s = match fs::read_to_string(path) {
Ok(s) => s,
Err(err) => {
tracing::error!(?err, "read_to_string");
return;
}
};
let mds = match FidoMds::from_str(&s) {
Ok(mds) => {
debug!("{} fido metadata avaliable", mds.fido2.len());
mds
}
Err(err) => {
tracing::error!(?err, "mds from str");
return;
}
};
let query = Query::exclude_compromised_devices();
match mds.fido2_query(&query) {
Some(fds) => display_query_results(&fds, extra_details),
None => {
error!("An internal error has occured, please report a bug!");
}
}
}
Opt::Query(QueryOpt {
query,
output_cert_roots,
show_insecure_devices,
extra_details,
common: CommonOpt { debug: _, path },
}) => {
trace!("{:?}", path);
let s = match fs::read_to_string(path) {
Ok(s) => s,
Err(err) => {
tracing::error!(?err, "read_to_string");
return;
}
};
let query = match Query::from_str(&query) {
Ok(q) => q,
Err(e) => {
tracing::error!(?e, "Failed to parse query");
return;
}
};
// For safety, we wrap this in a "gte" for valid authenticators ONLY.
let query = if show_insecure_devices {
query
} else {
Query::And(
Box::new(Query::exclude_compromised_devices()),
Box::new(query),
)
};
match FidoMds::from_str(&s) {
Ok(mds) => {
debug!("{} fido metadata avaliable", mds.fido2.len());
match mds.fido2_query(&query) {
Some(fds) => {
if output_cert_roots {
display_cert_roots(&fds)
} else {
display_query_results(&fds, extra_details)
}
}
None => warn!("No metadata matched query"),
}
}
Err(err) => {
tracing::error!(?err, "mds from str");
}
}
}
}
}
fn display_cert_roots(fds: &[Rc<FIDO2>]) {
match FidoMds::fido2_to_attestation_ca_list(fds) {
Some(att_ca_list) => match serde_json::to_string(&att_ca_list) {
Ok(list) => println!("{}", list),
Err(e) => {
eprintln!("Failed to serialise CA list - {:?}", e);
}
},
None => {
eprintln!("Invalid MDS data - check errors for more details.")
}
}
}
fn display_query_results(fds: &[Rc<FIDO2>], extra_details: bool) {
for fd in fds {
if extra_details {
println!("description: {}", fd.description);
println!(" aaguid: {}", fd.aaguid);
println!(" last update: {}", fd.time_of_last_status_change);
println!(" authenticator_version: {}", fd.authenticator_version);
println!(" authentication_algorithms:");
for alg in fd.authentication_algorithms.iter() {
println!(" - {alg}");
}
/*
println!(" public_key_alg_and_encodings: ");
for alg in fd.public_key_alg_and_encodings.iter() {
println!(" * {alg:?}");
}
*/
println!(" user_verification_details:");
for uvm_or in fd.user_verification_details.iter() {
let mut first = true;
print!(" -");
for uvm_and in uvm_or.iter() {
if !first {
print!(" AND");
}
first = false;
print!(" {uvm_and}");
}
println!();
}
println!(" key_protection:");
for kp in fd.key_protection.iter() {
println!(" - {kp:?}");
}
println!(" is_key_restricted: {}", fd.is_key_restricted);
println!(
" is_fresh_user_verification_required: {}",
fd.is_fresh_user_verification_required
);
if let Some(authenticator_info) = &fd.authenticator_get_info {
println!(" authenticator_get_info:");
println!(" versions:");
for ver in &authenticator_info.versions {
println!(" - {}", ver);
}
println!(" extensions:");
for extn in &authenticator_info.extensions {
println!(" - {}", extn);
}
// options?
println!(" transports:");
for tran in &authenticator_info.transports {
println!(" - {}", tran);
}
if let Some(mpl) = authenticator_info.min_pin_length {
println!(" minimum pin length: {}", mpl);
}
if !authenticator_info.certifications.is_empty() {
println!(" certifications:");
for (cert, cert_ver) in &authenticator_info.certifications {
println!(" - {} - {}", cert, cert_ver);
}
}
if let Some(mrk) = authenticator_info.remaining_discoverable_credentials {
println!(" resident key slots: {}", mrk);
}
} else {
println!(" authenticator_get_info: not present")
}
println!(" status_reports:");
for sr in fd.status_reports.iter() {
if let Some(e_date) = sr.effective_date() {
println!(" - {} - {}", e_date, sr.as_str());
} else {
println!(" - current - {}", sr.as_str());
}
}
println!();
} else {
println!("{fd}");
/*
println!(" authentication_algorithms:");
for alg in fd.authentication_algorithms.iter() {
println!(" * {alg}");
}
println!(" user_verification_details:");
for uvm_or in fd.user_verification_details.iter() {
let mut first = true;
print!(" *");
for uvm_and in uvm_or.iter() {
if !first {
print!(" AND");
}
first = false;
print!(" {uvm_and}");
}
println!();
}
println!("");
*/
}
}
// End fds
}