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
//! CTAP 2 commands.
use serde::Serialize;
use serde_cbor_2::{ser::to_vec_packed, Value};
use std::borrow::Borrow;
use std::collections::{BTreeMap, BTreeSet};

#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
mod bio_enrollment;
mod client_pin;
#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
mod config;
#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
mod credential_management;
mod get_assertion;
mod get_info;
mod make_credential;
#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
mod reset;
mod selection;

#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
pub use self::bio_enrollment::*;
pub use self::client_pin::*;
#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
pub use self::config::*;
#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
pub use self::credential_management::*;
pub use self::get_assertion::*;
pub use self::get_info::*;
pub use self::make_credential::*;
#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
pub use self::reset::*;
pub use self::selection::*;
use crate::error::WebauthnCError;
use crate::transport::iso7816::ISO7816RequestAPDU;

const FRAG_MAX: usize = 0xF0;

/// Common trait for all CBOR responses.
///
/// Ths handles some of the response deserialization process.
pub trait CBORResponse: Sized + std::fmt::Debug + Send {
    fn try_from(i: &[u8]) -> Result<Self, WebauthnCError>;
}

/// Common trait for all CBOR commands.
///
/// This handles some of the command serialization process.
pub trait CBORCommand: Serialize + Sized + std::fmt::Debug + Send {
    /// CTAP comand byte
    const CMD: u8;

    /// If true (default), then the command has a payload, which will be
    /// serialized into CBOR format.
    ///
    /// If false, then the command has no payload.
    const HAS_PAYLOAD: bool = true;

    /// The response type associated with this command.
    type Response: CBORResponse;

    /// Converts a CTAP v2 command into a binary form.
    fn cbor(&self) -> Result<Vec<u8>, serde_cbor_2::Error> {
        // CTAP v2.1, s8.2.9.1.2 (USB CTAPHID_CBOR), s8.3.5 (NFC framing).
        // Similar form used for caBLE.
        // TODO: BLE is different, it includes a u16 length after the command?
        if !Self::HAS_PAYLOAD {
            return Ok(vec![Self::CMD]);
        }

        trace!("Sending: {:?}", self);
        let mut b = to_vec_packed(self)?;
        trace!(
            "CBOR: cmd={}, cbor={:?}",
            Self::CMD,
            serde_cbor_2::from_slice::<'_, serde_cbor_2::Value>(&b[..])
        );

        b.reserve(1);
        b.insert(0, Self::CMD);
        Ok(b)
    }
}

/// Converts a CTAP v2 command into a form suitable for transmission with
/// short ISO/IEC 7816-4 APDUs (over NFC).
pub fn to_short_apdus(cbor: &[u8]) -> Vec<ISO7816RequestAPDU> {
    let chunks = cbor.chunks(FRAG_MAX).rev();
    let mut o = Vec::with_capacity(chunks.len());
    let mut last = true;

    for chunk in chunks {
        o.insert(
            0,
            ISO7816RequestAPDU {
                cla: if last { 0x80 } else { 0x90 },
                ins: 0x10,
                p1: 0x00,
                p2: 0x00,
                data: chunk.to_vec(),
                ne: if last { 256 } else { 0 },
            },
        );
        last = false;
    }

    o
}

/// Converts a CTAP v2 command into a form suitable for transmission with
/// extended ISO/IEC 7816-4 APDUs (over NFC).
pub fn to_extended_apdu(cbor: Vec<u8>) -> ISO7816RequestAPDU {
    ISO7816RequestAPDU {
        cla: 0x80,
        ins: 0x10,
        p1: 0, // 0x80,  // client supports NFCCTAP_GETRESPONSE
        p2: 0x00,
        data: cbor,
        ne: 65536,
    }
}

fn value_to_vec_string(v: Value, loc: &str) -> Option<Vec<String>> {
    if let Value::Array(v) = v {
        let mut x = Vec::with_capacity(v.len());
        for s in v.into_iter() {
            if let Value::Text(s) = s {
                x.push(s);
            } else {
                error!("Invalid value inside {}: {:?}", loc, s);
            }
        }
        Some(x)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_set_string(v: Value, loc: &str) -> Option<BTreeSet<String>> {
    if let Value::Array(v) = v {
        let mut x = BTreeSet::new();
        for s in v.into_iter() {
            if let Value::Text(s) = s {
                x.insert(s);
            } else {
                error!("Invalid value inside {}: {:?}", loc, s);
            }
        }
        Some(x)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_set_u64(v: Value, loc: &str) -> Option<BTreeSet<u64>> {
    if let Value::Array(v) = v {
        let mut x = BTreeSet::new();
        for i in v.into_iter() {
            if let Value::Integer(i) = i {
                if let Ok(i) = u64::try_from(i) {
                    x.insert(i);
                    continue;
                }
            }
            error!("Invalid value inside {}: {:?}", loc, i);
        }
        Some(x)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_vec(v: Value, loc: &str) -> Option<Vec<Value>> {
    if let Value::Array(v) = v {
        Some(v)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_map(v: Value, loc: &str) -> Option<BTreeMap<Value, Value>> {
    if let Value::Map(v) = v {
        Some(v)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_vec_u32(v: Value, loc: &str) -> Option<Vec<u32>> {
    value_to_vec(v, loc).map(|v| {
        v.into_iter()
            .filter_map(|i| value_to_u32(&i, loc))
            .collect()
    })
}

#[cfg(feature = "ctap2-management")]
pub(crate) fn value_to_u8(v: &Value, loc: &str) -> Option<u8> {
    if let Value::Integer(i) = v {
        u8::try_from(*i)
            .map_err(|_| error!("Invalid value inside {}: {:?}", loc, i))
            .ok()
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

pub(crate) fn value_to_u32(v: &Value, loc: &str) -> Option<u32> {
    if let Value::Integer(i) = v {
        u32::try_from(*i)
            .map_err(|_| error!("Invalid value inside {}: {:?}", loc, i))
            .ok()
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

#[cfg(any(doc, feature = "cable"))]
pub(crate) fn value_to_u64(v: &Value, loc: &str) -> Option<u64> {
    if let Value::Integer(i) = v {
        u64::try_from(*i)
            .map_err(|_| error!("Invalid value inside {}: {:?}", loc, i))
            .ok()
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_i128(v: impl Borrow<Value>, loc: &str) -> Option<i128> {
    let v = v.borrow();
    if let Value::Integer(i) = v {
        Some(*i)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

fn value_to_usize(v: impl Borrow<Value>, loc: &str) -> Option<usize> {
    let v = v.borrow();
    if let Value::Integer(i) = v {
        usize::try_from(*i)
            .map_err(|_| error!("Invalid value inside {}: {:?}", loc, i))
            .ok()
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

/// Converts a [`Value::Bool`] into [`Option<bool>`]. Returns [`Option::None`] for other [`Value`] types.
pub(crate) fn value_to_bool(v: &Value, loc: &str) -> Option<bool> {
    if let Value::Bool(b) = v {
        Some(*b)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

/// Converts a [`Value::Bytes`] into [`Option<Vec<u8>>`]. Returns [`Option::None`] for other [`Value`] types.
pub(crate) fn value_to_vec_u8(v: Value, loc: &str) -> Option<Vec<u8>> {
    if let Value::Bytes(b) = v {
        Some(b)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

pub(crate) fn value_to_string(v: Value, loc: &str) -> Option<String> {
    if let Value::Text(s) = v {
        Some(s)
    } else {
        error!("Invalid type for {}: {:?}", loc, v);
        None
    }
}

/// Type for commands which have no response data.
#[derive(Debug)]
pub struct NoResponse {}

impl CBORResponse for NoResponse {
    fn try_from(_raw: &[u8]) -> Result<Self, WebauthnCError> {
        Ok(Self {})
    }
}

fn map_int_keys(m: BTreeMap<Value, Value>) -> Result<BTreeMap<u32, Value>, WebauthnCError> {
    m.into_iter()
        .map(|(k, v)| {
            let k = value_to_u32(&k, "map_int_keys").ok_or(WebauthnCError::Internal)?;

            Ok((k, v))
        })
        .collect()
}

// TODO: switch to #derive
#[macro_export]
macro_rules! deserialize_cbor {
    ($name:ident) => {
        impl $crate::ctap2::commands::CBORResponse for $name {
            fn try_from(i: &[u8]) -> Result<Self, $crate::error::WebauthnCError> {
                if i.is_empty() {
                    TryFrom::try_from(std::collections::BTreeMap::new()).map_err(|e| {
                        error!("Tried to deserialise empty input, got error: {:?}", e);
                        $crate::error::WebauthnCError::Cbor
                    })
                } else {
                    // Convert to Value (Value::Map)
                    let v =
                        serde_cbor_2::from_slice::<'_, serde_cbor_2::Value>(&i).map_err(|e| {
                            error!("deserialise: {:?}", e);
                            $crate::error::WebauthnCError::Cbor
                        })?;

                    // Extract the BTreeMap
                    let v = if let serde_cbor_2::Value::Map(v) = v {
                        Ok(v)
                    } else {
                        error!("deserialise: unexpected CBOR type {:?}", v);
                        Err($crate::error::WebauthnCError::Cbor)
                    }?;

                    // Convert BTreeMap<Value, Value> into BTreeMap<u32, Value>
                    let v = $crate::ctap2::commands::map_int_keys(v)?;

                    TryFrom::try_from(v).map_err(|_| {
                        error!("deserialising structure");
                        $crate::error::WebauthnCError::Cbor
                    })
                }
            }
        }
    };
}