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
use serde::{Deserialize, Serialize};
use serde_cbor::Value;
use self::CBORCommand;
use super::*;
#[derive(Serialize, Debug, Clone)]
pub struct GetInfoRequest {}
impl CBORCommand for GetInfoRequest {
const CMD: u8 = 0x04;
const HAS_PAYLOAD: bool = false;
type Response = GetInfoResponse;
}
#[derive(Serialize, Deserialize, Debug)]
struct GetInfoResponseDict {
#[serde(flatten)]
pub keys: BTreeMap<u32, Value>,
}
#[derive(Deserialize, Debug)]
#[serde(try_from = "GetInfoResponseDict")]
pub struct GetInfoResponse {
pub versions: BTreeSet<String>,
pub extensions: Option<Vec<String>>,
pub aaguid: Vec<u8>,
pub options: Option<BTreeMap<String, bool>>,
pub max_msg_size: Option<u32>,
pub pin_protocols: Option<Vec<u32>>,
pub max_cred_count_in_list: Option<u32>,
pub max_cred_id_len: Option<u32>,
pub transports: Option<Vec<String>>,
pub algorithms: Option<Value>,
}
impl TryFrom<GetInfoResponseDict> for GetInfoResponse {
type Error = &'static str;
fn try_from(mut raw: GetInfoResponseDict) -> Result<Self, Self::Error> {
let versions = raw
.keys
.remove(&0x01)
.and_then(|v| value_to_set_string(v, "0x01"))
.ok_or("0x01")?;
let extensions = raw
.keys
.remove(&0x02)
.and_then(|v| value_to_vec_string(v, "0x02"));
let aaguid = raw
.keys
.remove(&0x03)
.and_then(|v| match v {
Value::Bytes(x) => Some(x),
_ => {
error!("Invalid type for 0x03: {:?}", v);
None
}
})
.ok_or("0x03")?;
let options = raw.keys.remove(&0x04).and_then(|v| {
if let Value::Map(v) = v {
let mut x = BTreeMap::new();
for (ka, va) in v.into_iter() {
match (ka, va) {
(Value::Text(s), Value::Bool(b)) => {
x.insert(s, b);
}
_ => error!("Invalid value inside 0x04"),
}
}
Some(x)
} else {
error!("Invalid type for 0x04: {:?}", v);
None
}
});
let max_msg_size = raw
.keys
.remove(&0x05)
.and_then(|v| value_to_u32(&v, "0x05"));
let pin_protocols = raw
.keys
.remove(&0x06)
.and_then(|v| value_to_vec_u32(v, "0x06"));
let max_cred_count_in_list = raw
.keys
.remove(&0x07)
.and_then(|v| value_to_u32(&v, "0x07"));
let max_cred_id_len = raw
.keys
.remove(&0x08)
.and_then(|v| value_to_u32(&v, "0x08"));
let transports = raw
.keys
.remove(&0x09)
.and_then(|v| value_to_vec_string(v, "0x09"));
let algorithms = raw.keys.remove(&0x0A);
Ok(GetInfoResponse {
versions,
extensions,
aaguid,
options,
max_msg_size,
pin_protocols,
max_cred_count_in_list,
max_cred_id_len,
transports,
algorithms,
})
}
}
crate::deserialize_cbor!(GetInfoResponse);
#[cfg(test)]
mod tests {
use super::*;
use crate::transport::iso7816::ISO7816LengthForm;
#[test]
fn get_info_response_nfc_usb() {
let _ = tracing_subscriber::fmt().try_init();
let raw_apdu: Vec<u8> = vec![
170, 1, 131, 102, 85, 50, 70, 95, 86, 50, 104, 70, 73, 68, 79, 95, 50, 95, 48, 108, 70,
73, 68, 79, 95, 50, 95, 49, 95, 80, 82, 69, 2, 130, 107, 99, 114, 101, 100, 80, 114,
111, 116, 101, 99, 116, 107, 104, 109, 97, 99, 45, 115, 101, 99, 114, 101, 116, 3, 80,
47, 192, 87, 159, 129, 19, 71, 234, 177, 22, 187, 90, 141, 185, 32, 42, 4, 165, 98,
114, 107, 245, 98, 117, 112, 245, 100, 112, 108, 97, 116, 244, 105, 99, 108, 105, 101,
110, 116, 80, 105, 110, 245, 117, 99, 114, 101, 100, 101, 110, 116, 105, 97, 108, 77,
103, 109, 116, 80, 114, 101, 118, 105, 101, 119, 245, 5, 25, 4, 176, 6, 129, 1, 7, 8,
8, 24, 128, 9, 130, 99, 110, 102, 99, 99, 117, 115, 98, 10, 130, 162, 99, 97, 108, 103,
38, 100, 116, 121, 112, 101, 106, 112, 117, 98, 108, 105, 99, 45, 107, 101, 121, 162,
99, 97, 108, 103, 39, 100, 116, 121, 112, 101, 106, 112, 117, 98, 108, 105, 99, 45,
107, 101, 121,
];
let a = <GetInfoResponse as CBORResponse>::try_from(raw_apdu.as_slice())
.expect("Falied to decode apdu");
assert!(a.versions.len() == 3);
assert!(a.versions.contains("U2F_V2"));
assert!(a.versions.contains("FIDO_2_0"));
assert!(a.versions.contains("FIDO_2_1_PRE"));
assert!(a.extensions == Some(vec!["credProtect".to_string(), "hmac-secret".to_string()]));
assert!(
a.aaguid
== vec![47, 192, 87, 159, 129, 19, 71, 234, 177, 22, 187, 90, 141, 185, 32, 42]
);
let m = a.options.as_ref().unwrap();
assert!(m.len() == 5);
assert!(m.get("clientPin") == Some(&true));
assert!(m.get("credentialMgmtPreview") == Some(&true));
assert!(m.get("plat") == Some(&false));
assert!(m.get("rk") == Some(&true));
assert!(m.get("up") == Some(&true));
assert!(a.max_msg_size == Some(1200));
assert!(a.max_cred_count_in_list == Some(8));
assert!(a.max_cred_id_len == Some(128));
assert!(a.transports == Some(vec!["nfc".to_string(), "usb".to_string()]));
}
#[test]
fn get_info_request() {
let req = GetInfoRequest {};
let short = vec![0x80, 0x10, 0, 0, 1, 0x4, 0];
let ext = vec![0x80, 0x10, 0, 0, 0, 0, 1, 0x4, 0xff, 0xff];
let a = req.to_short_apdus().unwrap();
assert_eq!(1, a.len());
assert_eq!(short, a[0].to_bytes(&ISO7816LengthForm::ShortOnly).unwrap());
assert_eq!(short, a[0].to_bytes(&ISO7816LengthForm::Extended).unwrap());
assert_eq!(
ext,
req.to_extended_apdu()
.unwrap()
.to_bytes(&ISO7816LengthForm::Extended)
.unwrap()
);
assert_eq!(
ext,
req.to_extended_apdu()
.unwrap()
.to_bytes(&ISO7816LengthForm::ExtendedOnly)
.unwrap()
);
assert!(req
.to_extended_apdu()
.unwrap()
.to_bytes(&ISO7816LengthForm::ShortOnly)
.is_err());
}
}