wlan_rsn/key_data/
mod.rs

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
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

pub mod kde;

use crate::Error;
use nom::bytes::streaming::take;
use nom::combinator::complete;
use nom::error::Error as NomError;
use nom::multi::many0;
use nom::{IResult, Needed};
use std::num::NonZero;
use wlan_common::ie::rsn::rsne;
use wlan_common::ie::{wpa, Id};

#[derive(Debug, PartialEq)]
pub enum Element {
    Gtk(kde::Header, kde::Gtk),
    Igtk(kde::Header, kde::Igtk),
    Rsne(rsne::Rsne),
    LegacyWpa1(wpa::WpaIe),
    Padding,
    UnsupportedKde(kde::Header),
    UnsupportedIe(u8, u8),
}

fn peek_u8_at(index: usize) -> impl FnMut(&[u8]) -> IResult<&[u8], u8> {
    move |input: &[u8]| {
        if input.len() <= index {
            Err(nom::Err::Incomplete(Needed::Size(NonZero::new(index + 1).unwrap())))
        } else {
            Ok((input, input[index]))
        }
    }
}

fn parse_ie(i0: &[u8]) -> IResult<&[u8], Element> {
    let (i1, id) = peek_u8_at(0)(i0)?;
    let (i2, len) = peek_u8_at(1)(i1)?;
    let (out, bytes) = take(2 + (len as usize))(i2)?;
    match Id(id) {
        Id::RSNE => {
            let (_, rsne) = rsne::from_bytes(bytes)?;
            Ok((out, Element::Rsne(rsne)))
        }
        _ => Ok((out, Element::UnsupportedIe(id, len))),
    }
}

fn parse_element(input: &[u8]) -> IResult<&[u8], Element> {
    let (_, type_) = peek_u8_at(0)(input)?;
    match type_ {
        kde::TYPE => kde::parse(input),
        _ => parse_ie(input),
    }
}

fn parse_elements(input: &[u8]) -> IResult<&[u8], Vec<Element>> {
    many0(complete(parse_element))(input)
}

#[allow(clippy::result_large_err, reason = "mass allow for https://fxbug.dev/381896734")]
pub fn extract_elements(key_data: &[u8]) -> Result<Vec<Element>, Error> {
    match parse_elements(&key_data[..]) {
        Ok((_, elements)) => Ok(elements),
        Err(nom::Err::Error(NomError { code, .. })) => Err(Error::InvalidKeyData(code).into()),
        Err(nom::Err::Failure(NomError { code, .. })) => Err(Error::InvalidKeyData(code).into()),
        Err(nom::Err::Incomplete(_)) => Ok(vec![]),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wlan_common::assert_variant;
    use wlan_common::ie::rsn::{akm, cipher};
    use wlan_common::organization::Oui;

    #[test]
    fn test_complex_key_data() {
        #[rustfmt::skip]
        let buf = [
            // GTK KDE
            0xDD,
            14, // Length
            0x00, 0x0F, 0xAC, // OUI
            1, // Data Type
            5, // GTK Info
            0, // Reserved
            1, 2, 3, 4, 5, 6, 7, 8, // GTK (8 bytes)
            // Unsupported IE
            99, 6, 1, 2, 3, 4, 5, 6,
            // 1st RSN Element
            48, 6, // IE Header
            1, 1, // Version
            1, 2, 3, 4, // Group Data Cipher
            // Unsupported KDE (wrong OUI)
            0xDD, 14, 0x01, 0x0F, 0xAC,
            1, // Data Type
            5, 0, 1, 2, 3, 4, 5, 6, 7, 8,
            // 2nd RSN Element
            48, 6, // IE Header
            9, 0, // Version
            0x00, 0x0F, 0xAC, 1, // Group Data Cipher
            // Unsupported IE
            200, 2, 1, 3,
            // 4 bytes padding
            0xDD, 0, 0, 0,
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 7);

        let mut pos = 0;
        for e in elements {
            match e {
                Element::Gtk(hdr, kde) => {
                    assert_eq!(pos, 0);
                    assert_eq!(hdr.type_, 0xDD);
                    assert_eq!(hdr.len, 14);
                    assert_eq!(hdr.oui, Oui::DOT11);
                    assert_eq!(hdr.data_type, 1);
                    assert_eq!(kde.info.value(), 5);
                    assert_eq!(&kde.gtk[..], &[1, 2, 3, 4, 5, 6, 7, 8][..]);
                }
                Element::UnsupportedIe(id, len) => match pos {
                    1 => {
                        assert_eq!(id, 99);
                        assert_eq!(len, 6);
                    }
                    5 => {
                        assert_eq!(id, 200);
                        assert_eq!(len, 2);
                    }
                    other => panic!("unexpected IE position: {}", other),
                },
                Element::Rsne(rsne) => match pos {
                    2 => {
                        assert_eq!(rsne.len(), 8);
                        assert_eq!(rsne.version, 257);
                        assert!(rsne.group_data_cipher_suite.is_some());
                        let cipher = rsne.group_data_cipher_suite.unwrap();
                        assert_eq!(cipher.suite_type, 4);
                        let oui = Oui::new([1, 2, 3]);
                        assert_eq!(cipher.oui, oui);
                    }
                    4 => {
                        assert_eq!(rsne.len(), 8);
                        assert_eq!(rsne.version, 9);
                        assert!(rsne.group_data_cipher_suite.is_some());
                        let cipher = rsne.group_data_cipher_suite.unwrap();
                        assert_eq!(cipher.suite_type, 1);
                        assert_eq!(cipher.oui, Oui::DOT11);
                    }
                    other => panic!("unexpected IE position: {}", other),
                },
                Element::UnsupportedKde(hdr) => {
                    assert_eq!(pos, 3);
                    assert_eq!(hdr.type_, 0xDD);
                    assert_eq!(hdr.len, 14);
                    let oui = Oui::new([0x01, 0x0F, 0xAC]);
                    assert_eq!(hdr.oui, oui);
                    assert_eq!(hdr.data_type, 1);
                }
                Element::Padding => assert_eq!(pos, 6),
                _ => panic!("Unexpected element in key data"),
            }
            pos += 1;
        }
    }

    #[test]
    fn test_no_padding() {
        #[rustfmt::skip]
        let buf = [
            10, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // Unsupported IE
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 1);
        assert_eq!(elements.into_iter().next(), Some(Element::UnsupportedIe(10, 14)));
    }

    #[test]
    fn test_single_padding_byte() {
        #[rustfmt::skip]
        let buf = [
            10, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Unsupported IE
            0xDD, // 1 byte padding
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 2);

        for e in elements {
            assert_variant!(e, Element::UnsupportedIe(10, 13) | Element::Padding);
        }
    }

    #[test]
    fn test_long_padding() {
        #[rustfmt::skip]
        let buf = [
            20, 6, 1, 2, 3, 4, 5, 6, // Unsupported IE
            0xdd, 0, 0, 0, 0, 0, 0, 0, // 8 bytes padding
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 2);

        for e in elements {
            assert_variant!(e, Element::UnsupportedIe(20, 6) | Element::Padding);
        }
    }

    #[test]
    fn test_gtk() {
        #[rustfmt::skip]
        let buf = [
            // GTK KDE
            0xDD,
            14, // Length
            0x00, 0x0F, 0xAC, // OUI
            1, // Data Type
            5, // GTK Info
            0, // Reserved
            1, 2, 3, 4, 5, 6, 7, 8, // GTK (8 bytes)
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 1);

        for e in elements {
            assert_variant!(e, Element::Gtk(hdr, kde) => {
                assert_eq!(
                    hdr,
                    kde::Header { type_: 0xDD, len: 14, oui: Oui::DOT11, data_type: 1 }
                );
                assert_eq!(kde.info.value(), 5);
                assert_eq!(&kde.gtk[..], &[1, 2, 3, 4, 5, 6, 7, 8][..]);
            });
        }
    }

    #[test]
    fn test_long_gtk() {
        #[rustfmt::skip]
        let buf = [
            // GTK KDE
            0xDD,
            22, // Length
            0x00, 0x0F, 0xAC, // OUI
            1, // Data Type
            200, // GTK Info
            0, // Reserved
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, // GTK (16 bytes)
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 1);

        for e in elements {
            assert_variant!(e, Element::Gtk(hdr, kde) => {
                assert_eq!(
                    hdr,
                    kde::Header { type_: 0xDD, len: 22, oui: Oui::DOT11, data_type: 1 }
                );
                assert_eq!(kde.info.value(), 200);
                assert_eq!(
                    &kde.gtk[..],
                    &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16][..]
                );
            });
        }
    }

    #[test]
    fn test_parse_legacy_wpa() {
        #[rustfmt::skip]
        let buf = [
            // MSFT Vendor IE
            0xdd, 0x16, 0x00, 0x50, 0xf2,
            // WPA header
            0x01, 0x01, 0x00,
            // Multicast cipher
            0x00, 0x50, 0xf2, 0x02,
            // Unicast cipher list
            0x01, 0x00, 0x00, 0x50, 0xf2, 0x02,
            // AKM list
            0x01, 0x00, 0x00, 0x50, 0xf2, 0x02,
        ];
        let result = extract_elements(&buf[..]);
        assert!(result.is_ok(), "Error: {:?}", result);

        let elements = result.unwrap();
        assert_eq!(elements.len(), 1);

        for e in elements {
            assert_variant!(e, Element::LegacyWpa1(wpa_ie) => {
                assert_eq!(
                    wpa_ie.multicast_cipher,
                    cipher::Cipher { oui: Oui::MSFT, suite_type: cipher::TKIP }
                );
                assert_eq!(
                    wpa_ie.unicast_cipher_list,
                    vec![cipher::Cipher { oui: Oui::MSFT, suite_type: cipher::TKIP }]
                );
                assert_eq!(
                    wpa_ie.akm_list,
                    vec![akm::Akm { oui: Oui::MSFT, suite_type: akm::PSK }]
                );
            });
        }
    }
}