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
//! Subset of WebAuthn types that crept into CTAP.

use crate::sizes::*;
use crate::{Bytes, String};
use serde::{de::Deserializer, Deserialize, Serialize};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PublicKeyCredentialRpEntity {
    pub id: String<256>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_from_str_and_truncate"
    )]
    pub name: Option<String<64>>,
    /// This field has been removed in Webauthn 2 but CTAP 2.2 requires implementors to accept it.
    ///
    /// The content of this field must not be stored.  Therefore we use the [`Icon`][] helper type.
    ///
    /// See [issue #9][] for more information.
    ///
    /// [issue #9]: https://github.com/solokeys/ctap-types/issues/9
    #[serde(skip_serializing, alias = "url")]
    pub icon: Option<Icon>,
}

/// Helper type for the `icon` field of [`PublicKeyCredentialRpEntity`][].
///
/// This field must be parsed but not used or stored.  Therefore this wrapper type can be
/// deserialized from a string but does not store any data.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Icon;

impl<'de> Deserialize<'de> for Icon {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let _s: &'de str = Deserialize::deserialize(deserializer)?;
        Ok(Self)
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyCredentialUserEntity {
    pub id: Bytes<64>,
    #[serde(
        default,
        deserialize_with = "deserialize_from_str_and_skip_if_too_long"
    )]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<String<128>>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_from_str_and_truncate"
    )]
    pub name: Option<String<64>>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_from_str_and_truncate"
    )]
    pub display_name: Option<String<64>>,
}

fn deserialize_from_str_and_skip_if_too_long<'de, D, const L: usize>(
    deserializer: D,
) -> Result<Option<String<L>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s: &'de str = Deserialize::deserialize(deserializer)?;
    // String::from(s) could panic and is not really infallibe.  It is removed in heapless 0.8.
    #[allow(clippy::unnecessary_fallible_conversions)]
    match String::try_from(s) {
        Ok(string) => Ok(Some(string)),
        Err(_err) => {
            info_now!("skipping field: {:?}", _err);
            Ok(None)
        }
    }
}

fn deserialize_from_str_and_truncate<'de, D, const L: usize>(
    deserializer: D,
) -> Result<Option<String<L>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s: Option<&str> = serde::Deserialize::deserialize(deserializer)?;
    Ok(s.map(truncate))
}

fn truncate<const L: usize>(s: &str) -> String<L> {
    let split = floor_char_boundary(s, L);
    let mut truncated = String::new();
    // floor_char_boundary(s, L) <= L, so this cannot fail
    truncated.push_str(&s[..split]).unwrap();
    truncated
}

// Copy of the nightly str::floor_char_boundary function
fn floor_char_boundary(s: &str, index: usize) -> usize {
    if index >= s.len() {
        s.len()
    } else {
        let lower_bound = index.saturating_sub(3);
        let new_index = s.as_bytes()[lower_bound..=index]
            .iter()
            .rposition(|b| is_utf8_char_boundary(*b));

        // SAFETY: we know that the character boundary will be within four bytes
        unsafe { lower_bound + new_index.unwrap_unchecked() }
    }
}

// Copy of the private u8::is_utf8_char_boundary function
#[inline]
const fn is_utf8_char_boundary(b: u8) -> bool {
    // This is bit magic equivalent to: b < 128 || b >= 192
    (b as i8) >= -0x40
}

impl PublicKeyCredentialUserEntity {
    pub fn from(id: Bytes<64>) -> Self {
        Self {
            id,
            icon: None,
            name: None,
            display_name: None,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KnownPublicKeyCredentialParameters {
    pub alg: i32,
}

impl From<KnownPublicKeyCredentialParameters> for PublicKeyCredentialParameters {
    fn from(value: KnownPublicKeyCredentialParameters) -> Self {
        Self {
            alg: value.alg,
            key_type: String::from("public-key"),
        }
    }
}

pub enum UnknownPKCredentialParam {
    UnknownType,
    UnknownAlg,
}

/// ECDSA w/ SHA-256
pub const ES256: i32 = -7;
/// EdDSA
pub const ED_DSA: i32 = -8;

pub const COUNT_KNOWN_ALGS: usize = 2;
pub const KNOWN_ALGS: [i32; COUNT_KNOWN_ALGS] = [ES256, ED_DSA];

impl TryFrom<PublicKeyCredentialParameters> for KnownPublicKeyCredentialParameters {
    type Error = UnknownPKCredentialParam;

    fn try_from(value: PublicKeyCredentialParameters) -> Result<Self, Self::Error> {
        if value.key_type != "public-key" {
            Err(UnknownPKCredentialParam::UnknownType)
        } else if KNOWN_ALGS.contains(&value.alg) {
            Ok(Self { alg: value.alg })
        } else {
            Err(UnknownPKCredentialParam::UnknownAlg)
        }
    }
}

/// Struct of filtered PublicKeyCredentialParameters, that drops unknown algorithms while parsing
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FilteredPublicKeyCredentialParameters(
    pub heapless::Vec<KnownPublicKeyCredentialParameters, COUNT_KNOWN_ALGS>,
);

impl Serialize for FilteredPublicKeyCredentialParameters {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeSeq;
        let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
        for element in &self.0 {
            let el: PublicKeyCredentialParameters = element.clone().into();
            seq.serialize_element(&el)?
        }
        seq.end()
    }
}

impl<'de> Deserialize<'de> for FilteredPublicKeyCredentialParameters {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct ValueVisitor;
        impl<'de> serde::de::Visitor<'de> for ValueVisitor {
            type Value = FilteredPublicKeyCredentialParameters;

            fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
                formatter.write_str("a sequence")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::SeqAccess<'de>,
            {
                let mut values = FilteredPublicKeyCredentialParameters(Default::default());
                while let Some(value) = seq.next_element::<PublicKeyCredentialParameters>()? {
                    let Ok(el) = value.try_into() else {
                        // Drop unknown algorithms
                        continue;
                    };
                    // We drop too many elements. This shouldn't happen as we have enough space for all known algorithms.
                    // This can only happen in case of duplicates.
                    values.0.push(el).ok();
                }
                Ok(values)
            }
        }

        deserializer.deserialize_seq(ValueVisitor)
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PublicKeyCredentialParameters {
    pub alg: i32,
    #[serde(rename = "type")]
    pub key_type: String<32>,
}

impl PublicKeyCredentialParameters {
    pub fn public_key_with_alg(alg: i32) -> Self {
        Self {
            alg,
            key_type: String::from("public-key"),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyCredentialDescriptor {
    // NB: if this is too small, get a nasty error
    // See serde::error/custom for more info
    pub id: Bytes<MAX_CREDENTIAL_ID_LENGTH>,
    #[serde(rename = "type")]
    pub key_type: String<32>,
    // https://w3c.github.io/webauthn/#enumdef-authenticatortransport
    // transports: ...
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Same as PublicKeyCredentialDescriptor but which deserializes using references
pub struct PublicKeyCredentialDescriptorRef<'a> {
    pub id: &'a serde_bytes::Bytes,
    #[serde(rename = "type")]
    pub key_type: &'a str,
    // https://w3c.github.io/webauthn/#enumdef-authenticatortransport
    // transports: ...
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_truncate() {
        // Example from ยง 6.4.1 String Truncation in the Webauthn spec
        let v = vec![0x61, 0x67, 0xcc, 0x88];
        let s = std::str::from_utf8(&v).unwrap();

        assert_eq!(truncate::<1>(s), "a");
        assert_eq!(truncate::<2>(s), "ag");
        assert_eq!(truncate::<3>(s), "ag");
        assert_eq!(truncate::<4>(s), s);
        assert_eq!(truncate::<5>(s), s);
        assert_eq!(truncate::<64>(s), s);
    }
}