ctap-types 0.6.0-rc.2

no_std friendly types for FIDO CTAP
Documentation
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
use core::{fmt::Debug, ops::ControlFlow};

use arbitrary::{Arbitrary, Error, Result, Unstructured};
use cosey::EcdhEsHkdf256PublicKey;
use heapless::{String, Vec};
use heapless_bytes::Bytes;
use serde_bytes::ByteArray;

use crate::{ctap1, ctap2, webauthn};

// cannot be derived because of missing impl for &[T; N]
impl<'a> Arbitrary<'a> for ctap1::authenticate::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let control_byte = Arbitrary::arbitrary(u)?;
        let challenge = u.bytes(32)?.try_into().unwrap();
        let app_id = u.bytes(32)?.try_into().unwrap();
        let key_handle = Arbitrary::arbitrary(u)?;
        Ok(Self {
            control_byte,
            challenge,
            app_id,
            key_handle,
        })
    }
}

// cannot be derived because of missing impl for &[T; N]
impl<'a> Arbitrary<'a> for ctap1::register::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let challenge = u.bytes(32)?.try_into().unwrap();
        let app_id = u.bytes(32)?.try_into().unwrap();
        Ok(Self { challenge, app_id })
    }
}

// cannot be derived because of missing impl for Vec<_>
impl<'a> Arbitrary<'a> for ctap2::AttestationFormatsPreference {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let known_formats = arbitrary_vec(u)?;
        let unknown = u.arbitrary()?;
        Ok(Self {
            known_formats,
            unknown,
        })
    }
}

// cannot be derived because of missing impl for serde_bytes::Bytes, EcdhEsHkdf256PublicKey
impl<'a> Arbitrary<'a> for ctap2::client_pin::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let pin_protocol = u.arbitrary()?;
        let sub_command = u.arbitrary()?;
        let key_agreement = arbitrary_option(u, arbitrary_key)?;
        let pin_auth = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let new_pin_enc = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let pin_hash_enc = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let _placeholder07 = u.arbitrary()?;
        let _placeholder08 = u.arbitrary()?;
        let permissions = u.arbitrary()?;
        let rp_id = u.arbitrary()?;
        Ok(Self {
            pin_protocol,
            sub_command,
            key_agreement,
            pin_auth,
            new_pin_enc,
            pin_hash_enc,
            _placeholder07,
            _placeholder08,
            permissions,
            rp_id,
        })
    }
}

// cannot be derived because of missing impl for serde_bytes::Bytes
impl<'a> Arbitrary<'a> for ctap2::credential_management::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let sub_command = u.arbitrary()?;
        let sub_command_params = u.arbitrary()?;
        let pin_protocol = u.arbitrary()?;
        let pin_auth = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        Ok(Self {
            sub_command,
            sub_command_params,
            pin_protocol,
            pin_auth,
        })
    }
}

// cannot be derived because of missing impl for serde_bytes::ByteArray
impl<'a> Arbitrary<'a> for ctap2::credential_management::SubcommandParameters<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let rp_id_hash = arbitrary_option(u, arbitrary_byte_array)?;
        let credential_id = u.arbitrary()?;
        let user = u.arbitrary()?;
        Ok(Self {
            rp_id_hash,
            credential_id,
            user,
        })
    }
}

// cannot be derived because of missing impl for EcdhEsHkdf256PublicKey, Bytes<_>
impl<'a> Arbitrary<'a> for ctap2::get_assertion::HmacSecretInput {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let key_agreement = arbitrary_key(u)?;
        let salt_enc = arbitrary_bytes(u)?;
        let salt_auth = arbitrary_bytes(u)?;
        let pin_protocol = u.arbitrary()?;
        Ok(Self {
            key_agreement,
            salt_enc,
            salt_auth,
            pin_protocol,
        })
    }
}

// cannot be derived because of missing impl for serde_bytes::Bytes, Vec<_>
impl<'a> Arbitrary<'a> for ctap2::get_assertion::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let rp_id = u.arbitrary()?;
        let client_data_hash = serde_bytes::Bytes::new(u.arbitrary()?);
        let allow_list = arbitrary_option(u, arbitrary_vec)?;
        let extensions = u.arbitrary()?;
        let options = u.arbitrary()?;
        let pin_auth = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let pin_protocol = u.arbitrary()?;
        let enterprise_attestation = u.arbitrary()?;
        let attestation_formats_preference = u.arbitrary()?;
        Ok(Self {
            rp_id,
            client_data_hash,
            allow_list,
            extensions,
            options,
            pin_auth,
            pin_protocol,
            enterprise_attestation,
            attestation_formats_preference,
        })
    }
}

// cannot be derived because of missing impl for serde_bytes::Bytes
impl<'a> Arbitrary<'a> for ctap2::large_blobs::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let get = u.arbitrary()?;
        let set = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let offset = u.arbitrary()?;
        let length = u.arbitrary()?;
        let pin_uv_auth_param = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let pin_uv_auth_protocol = u.arbitrary()?;
        Ok(Self {
            get,
            set,
            offset,
            length,
            pin_uv_auth_param,
            pin_uv_auth_protocol,
        })
    }
}

// cannot be derived because of missing impl for serde_bytes::Bytes
impl<'a> Arbitrary<'a> for ctap2::make_credential::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let client_data_hash = serde_bytes::Bytes::new(u.arbitrary()?);
        let rp = u.arbitrary()?;
        let user = u.arbitrary()?;
        let pub_key_cred_params = u.arbitrary()?;
        let exclude_list = arbitrary_option(u, arbitrary_vec)?;
        let extensions = u.arbitrary()?;
        let options = u.arbitrary()?;
        let pin_auth = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let pin_protocol = u.arbitrary()?;
        let enterprise_attestation = u.arbitrary()?;
        let attestation_formats_preference = u.arbitrary()?;
        Ok(Self {
            client_data_hash,
            rp,
            user,
            pub_key_cred_params,
            exclude_list,
            extensions,
            options,
            pin_auth,
            pin_protocol,
            enterprise_attestation,
            attestation_formats_preference,
        })
    }
}

// cannot be derived because of missing impl for Vec<_>
impl<'a> Arbitrary<'a> for webauthn::FilteredPublicKeyCredentialParameters {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let parameters = arbitrary_vec(u)?;
        Ok(Self(parameters))
    }
}

// cannot be derived because we want to make sure that we have valid values
impl<'a> Arbitrary<'a> for webauthn::KnownPublicKeyCredentialParameters {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let alg = *u.choose(&webauthn::KNOWN_ALGS)?;
        Ok(Self { alg })
    }
}

// cannot be derived because of missing impl for serde_bytes::Bytes
impl<'a> Arbitrary<'a> for webauthn::PublicKeyCredentialDescriptorRef<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let id = serde_bytes::Bytes::new(u.arbitrary()?);
        let key_type = u.arbitrary()?;
        Ok(Self { id, key_type })
    }
}

// cannot be derived because of missing impl for String<_>
impl<'a> Arbitrary<'a> for webauthn::PublicKeyCredentialRpEntity {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let id = arbitrary_str(u)?;
        let name = if bool::arbitrary(u)? {
            Some(arbitrary_str(u)?)
        } else {
            None
        };
        let icon = Arbitrary::arbitrary(u)?;
        Ok(Self { id, name, icon })
    }
}

// cannot be derived because of missing impl for Bytes<_> and String<_>
impl<'a> Arbitrary<'a> for webauthn::PublicKeyCredentialUserEntity {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let id = arbitrary_bytes(u)?;
        let icon = if bool::arbitrary(u)? {
            Some(arbitrary_str(u)?)
        } else {
            None
        };
        let name = if bool::arbitrary(u)? {
            Some(arbitrary_str(u)?)
        } else {
            None
        };
        let display_name = if bool::arbitrary(u)? {
            Some(arbitrary_str(u)?)
        } else {
            None
        };
        Ok(Self {
            id,
            icon,
            name,
            display_name,
        })
    }
}

// cannot be derived because of missing impl for &'a serde_bytes::Bytes (cred_blob).
// Mirrors the make_credential::Request handling of `pin_auth: Option<&Bytes>`.
impl<'a> Arbitrary<'a> for ctap2::make_credential::ExtensionsInput<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let cred_protect = u.arbitrary()?;
        let hmac_secret = u.arbitrary()?;
        let large_blob_key = u.arbitrary()?;
        let min_pin_length = u.arbitrary()?;
        #[cfg(feature = "third-party-payment")]
        let third_party_payment = u.arbitrary()?;
        let cred_blob = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        let hmac_secret_mc = u.arbitrary()?;
        Ok(Self {
            cred_protect,
            hmac_secret,
            large_blob_key,
            min_pin_length,
            #[cfg(feature = "third-party-payment")]
            third_party_payment,
            cred_blob,
            hmac_secret_mc,
        })
    }
}

// cannot be derived because of missing impl for Vec<&'a str, N> (rp ID list).
impl<'a> Arbitrary<'a> for ctap2::config::SubcommandParameters<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let new_min_pin_length = u.arbitrary()?;
        let min_pin_length_rp_ids = arbitrary_option(u, arbitrary_vec)?;
        let force_change_pin = u.arbitrary()?;
        Ok(Self {
            new_min_pin_length,
            min_pin_length_rp_ids,
            force_change_pin,
        })
    }
}

// cannot be derived because of missing impl for &'a serde_bytes::Bytes (pin_auth)
// + Vec<_> in SubcommandParameters.
impl<'a> Arbitrary<'a> for ctap2::config::Request<'a> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let sub_command = u.arbitrary()?;
        let sub_command_params = u.arbitrary()?;
        let pin_protocol = u.arbitrary()?;
        let pin_auth = if bool::arbitrary(u)? {
            Some(serde_bytes::Bytes::new(u.arbitrary()?))
        } else {
            None
        };
        Ok(Self {
            sub_command,
            sub_command_params,
            pin_protocol,
            pin_auth,
        })
    }
}

fn arbitrary_byte_array<'a, const N: usize>(u: &mut Unstructured<'_>) -> Result<&'a ByteArray<N>> {
    let bytes: &[u8; N] = u.bytes(N)?.try_into().unwrap();
    // TODO: conversion should be provided by serde_bytes
    Ok(unsafe { &*(bytes as *const [u8; N] as *const ByteArray<N>) })
}

fn arbitrary_bytes<const N: usize>(u: &mut Unstructured<'_>) -> Result<Bytes<N>> {
    let n = usize::arbitrary(u)?.min(N);
    Ok(Bytes::try_from(u.bytes(n)?).unwrap())
}

fn arbitrary_vec<'a, T: Arbitrary<'a> + Debug, const N: usize>(
    u: &mut Unstructured<'a>,
) -> Result<Vec<T, N>> {
    let mut vec = Vec::new();
    u.arbitrary_loop(Some(0), Some(N.try_into().unwrap()), |u| {
        vec.push(u.arbitrary()?).unwrap();
        Ok(ControlFlow::Continue(()))
    })?;
    Ok(vec)
}

fn arbitrary_str<const N: usize>(u: &mut Unstructured<'_>) -> Result<String<N>> {
    let n = usize::arbitrary(u)?.min(N);
    match core::str::from_utf8(u.peek_bytes(n).ok_or(Error::NotEnoughData)?) {
        Ok(s) => {
            u.bytes(n)?;
            Ok(s.try_into().unwrap())
        }
        Err(e) => {
            let i = e.valid_up_to();
            let valid = u.bytes(i)?;
            let s = unsafe { core::str::from_utf8_unchecked(valid) };
            Ok(s.try_into().unwrap())
        }
    }
}

fn arbitrary_option<'a, T, F>(u: &mut Unstructured<'a>, f: F) -> Result<Option<T>>
where
    F: FnOnce(&mut Unstructured<'a>) -> Result<T>,
{
    if bool::arbitrary(u)? {
        f(u).map(Some)
    } else {
        Ok(None)
    }
}

fn arbitrary_key(u: &mut Unstructured<'_>) -> Result<EcdhEsHkdf256PublicKey> {
    let x = arbitrary_bytes(u)?;
    let y = arbitrary_bytes(u)?;
    Ok(EcdhEsHkdf256PublicKey { x, y })
}