wlan_mlme/
auth.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
// Copyright 2019 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.

use anyhow::{bail, ensure, Error};
use fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211;
use wlan_common::mac;

pub fn make_open_client_req() -> mac::AuthHdr {
    mac::AuthHdr {
        auth_alg_num: mac::AuthAlgorithmNumber::OPEN,
        auth_txn_seq_num: 1,
        status_code: fidl_ieee80211::StatusCode::Success.into(),
    }
}

#[derive(Debug)]
pub enum ValidFrame {
    Open,
    SaeCommit,
    SaeConfirm,
}

/// Validates whether a given authentication header is a valid response to an authentication
/// request.
pub fn validate_ap_resp(auth: &mac::AuthHdr) -> Result<ValidFrame, Error> {
    ensure!(
        { auth.status_code } == fidl_ieee80211::StatusCode::Success.into(),
        "invalid status_code: {}",
        { auth.status_code }.0
    );
    match auth.auth_alg_num {
        mac::AuthAlgorithmNumber::OPEN => {
            ensure!(auth.auth_txn_seq_num == 2, "invalid auth_txn_seq_num: {}", {
                auth.auth_txn_seq_num
            });
            Ok(ValidFrame::Open)
        }
        mac::AuthAlgorithmNumber::SAE => match auth.auth_txn_seq_num {
            1 => Ok(ValidFrame::SaeCommit),
            2 => Ok(ValidFrame::SaeConfirm),
            _ => bail!("invalid auth_txn_seq_num: {}", { auth.auth_txn_seq_num }),
        },
        _ => bail!("invalid auth_alg_num: {}", { auth.auth_alg_num }.0),
    }
}

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

    fn make_valid_auth_resp(frame_type: ValidFrame) -> mac::AuthHdr {
        mac::AuthHdr {
            auth_alg_num: match frame_type {
                ValidFrame::Open => mac::AuthAlgorithmNumber::OPEN,
                ValidFrame::SaeCommit | ValidFrame::SaeConfirm => mac::AuthAlgorithmNumber::SAE,
            },
            auth_txn_seq_num: match frame_type {
                ValidFrame::SaeCommit => 1,
                ValidFrame::Open | ValidFrame::SaeConfirm => 2,
            },
            status_code: fidl_ieee80211::StatusCode::Success.into(),
        }
    }

    #[test]
    fn valid_auth_resp() {
        assert_variant!(
            validate_ap_resp(&make_valid_auth_resp(ValidFrame::Open)),
            Ok(ValidFrame::Open)
        );
        assert_variant!(
            validate_ap_resp(&make_valid_auth_resp(ValidFrame::SaeCommit)),
            Ok(ValidFrame::SaeCommit)
        );
        assert_variant!(
            validate_ap_resp(&make_valid_auth_resp(ValidFrame::SaeConfirm)),
            Ok(ValidFrame::SaeConfirm)
        );
    }

    #[test]
    fn invalid_auth_resp() {
        let mut auth_hdr = make_valid_auth_resp(ValidFrame::Open);
        auth_hdr.auth_alg_num = mac::AuthAlgorithmNumber::FAST_BSS_TRANSITION;
        assert_variant!(validate_ap_resp(&auth_hdr), Err(_));

        let mut auth_hdr = make_valid_auth_resp(ValidFrame::Open);
        auth_hdr.auth_txn_seq_num = 1;
        assert_variant!(validate_ap_resp(&auth_hdr), Err(_));

        let mut auth_hdr = make_valid_auth_resp(ValidFrame::Open);
        auth_hdr.status_code = fidl_ieee80211::StatusCode::RefusedReasonUnspecified.into();
        assert_variant!(validate_ap_resp(&auth_hdr), Err(_));

        let mut auth_hdr = make_valid_auth_resp(ValidFrame::SaeCommit);
        auth_hdr.auth_txn_seq_num = 4;
        assert_variant!(validate_ap_resp(&auth_hdr), Err(_));

        let mut auth_hdr = make_valid_auth_resp(ValidFrame::SaeCommit);
        auth_hdr.status_code = fidl_ieee80211::StatusCode::RefusedReasonUnspecified.into();
        assert_variant!(validate_ap_resp(&auth_hdr), Err(_));
    }
}