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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Copyright 2021 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.

//! Wireless network security descriptors and authenticators.
//!
//! This module describes wireless network security protocols as well as credentials used to
//! authenticate using those protocols. Types in this module provide two important features:
//! conversions from and into FIDL types used within the WLAN stack and consistency. Here,
//! consistency means that types have no invalid values; given an instance of a type, it is always
//! valid. For example, a `SecurityAuthenticator` always represents a valid protocol-credential
//! pair.
//!
//! The `SecurityDescriptor` and `SecurityAuthenticator` types form the primary API of this module.
//! A _descriptor_ merely describes (names) a security protocol such as WPA2 Personal using TKIP.
//! An _authenticator_ describes a security protocol and additionally contains credentials used to
//! authenticate using that protocol such as WPA Personal using TKIP with a PSK credential.
//! Authenticators can be converted into descriptors (which drops any associated credentials).
//!
//! # FIDL
//!
//! Types in this module support conversion into and from datagrams in the
//! `fuchsia.wlan.common.security` package. When interacting with FIDL, most code should prefer
//! conversions between FIDL types and this module's `SecurityDescriptor` and
//! `SecurityAuthenticator` types, though conversions for intermediate types are also provided.
//!
//! The models used by this module and the FIDL package differ, so types do not always have a
//! direct analog, but the table below describes the most straightforward and important mappings.
//!
//! | Rust Type               | FIDL Type        | Rust to FIDL | FIDL to Rust |
//! |-------------------------|------------------|--------------|--------------|
//! | `SecurityDescriptor`    | `Protocol`       | `From`       | `From`       |
//! | `SecurityAuthenticator` | `Authentication` | `From`       | `TryFrom`    |
//!
//! # Cryptography and RSN
//!
//! This module does **not** implement any security protocols nor cryptography and only describes
//! them with limited detail as needed at API boundaries and in code that merely interacts with
//! these protocols. See the `rsn` crate for implementations of the IEEE Std 802.11-2016 Robust
//! Security Network (RSN).

// NOTE: At the time of this writing, the WLAN stack does not support WPA Enterprise. One
//       consequence of this is that conversions between Rust and FIDL types may return errors or
//       panic when they involve WPA Enterprise representations. See https://fxbug.dev/42174395 and the TODOs
//       in this module.

pub mod wep;
pub mod wpa;

use fidl_fuchsia_wlan_common_security as fidl_security;
use thiserror::Error;

use crate::security::{
    wep::WepKey,
    wpa::credential::{Passphrase, PassphraseError, Psk, PskError},
};

/// Extension methods for the `Credentials` FIDL datagram.
pub trait CredentialsExt {
    fn into_wep(self) -> Option<fidl_security::WepCredentials>;
    fn into_wpa(self) -> Option<fidl_security::WpaCredentials>;
}

impl CredentialsExt for fidl_security::Credentials {
    fn into_wep(self) -> Option<fidl_security::WepCredentials> {
        if let fidl_security::Credentials::Wep(credentials) = self {
            Some(credentials)
        } else {
            None
        }
    }

    fn into_wpa(self) -> Option<fidl_security::WpaCredentials> {
        if let fidl_security::Credentials::Wpa(credentials) = self {
            Some(credentials)
        } else {
            None
        }
    }
}

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
#[non_exhaustive]
pub enum SecurityError {
    #[error(transparent)]
    Wep(#[from] wep::WepError),
    #[error(transparent)]
    Wpa(#[from] wpa::WpaError),
    /// This error occurs when there is an incompatibility between security protocols, features,
    /// and/or credentials.
    ///
    /// Note that this is distinct from `SecurityError::Unsupported`.
    #[error("incompatible protocol or features")]
    Incompatible,
    /// This error occurs when a specified security protocol, features, and/or credentials are
    /// **not** supported.
    ///
    /// Note that this is distinct from `SecurityError::Incompatible`. Unsupported features may be
    /// compatible and specified in IEEE Std. 802.11-2016.
    #[error("unsupported protocol or features")]
    Unsupported,
}

impl From<PassphraseError> for SecurityError {
    fn from(error: PassphraseError) -> Self {
        SecurityError::Wpa(error.into())
    }
}

impl From<PskError> for SecurityError {
    fn from(error: PskError) -> Self {
        SecurityError::Wpa(error.into())
    }
}

/// General credential data that is not explicitly coupled to a particular security protocol.
///
/// The variants of this enumeration are particular to general protocols (i.e., WEP and WPA), but
/// don't provide any more details or validation. For WPA credential data, this means that the
/// version of the WPA security protocol is entirely unknown.
///
/// This type is meant for code and APIs that accept such bare credentials and must incorporate
/// additional information or apply heuristics to negotiate a specific protocol. For example, this
/// occurs in code that communicates directly with SME without support from the Policy layer to
/// derive this information.
///
/// The FIDL analogue of this type is `fuchsia.wlan.common.security.Credentials`, into and from
/// which this type can be infallibly converted.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BareCredentials {
    /// WEP key.
    WepKey(WepKey),
    /// WPA passphrase.
    ///
    /// Passphrases can be used to authenticate with WPA1, WPA2, and WPA3.
    WpaPassphrase(Passphrase),
    /// WPA PSK.
    ///
    /// PSKs are distinct from passphrases and can be used to authenticate with WPA1 and WPA2. A
    /// PSK cannot be used to authenticate with WPA3.
    WpaPsk(Psk),
}

impl From<BareCredentials> for fidl_security::Credentials {
    fn from(credentials: BareCredentials) -> Self {
        match credentials {
            BareCredentials::WepKey(key) => {
                fidl_security::Credentials::Wep(fidl_security::WepCredentials { key: key.into() })
            }
            BareCredentials::WpaPassphrase(passphrase) => fidl_security::Credentials::Wpa(
                fidl_security::WpaCredentials::Passphrase(passphrase.into()),
            ),
            BareCredentials::WpaPsk(psk) => {
                fidl_security::Credentials::Wpa(fidl_security::WpaCredentials::Psk(psk.into()))
            }
        }
    }
}

impl TryFrom<fidl_security::Credentials> for BareCredentials {
    type Error = SecurityError;

    fn try_from(credentials: fidl_security::Credentials) -> Result<Self, Self::Error> {
        match credentials {
            fidl_security::Credentials::Wep(fidl_security::WepCredentials { key }) => {
                WepKey::try_from_literal_bytes(key.as_slice())
                    .map(|key| BareCredentials::WepKey(key))
                    .map_err(From::from)
            }
            fidl_security::Credentials::Wpa(credentials) => match credentials {
                fidl_security::WpaCredentials::Passphrase(passphrase) => {
                    Passphrase::try_from(passphrase)
                        .map(|passphrase| BareCredentials::WpaPassphrase(passphrase))
                        .map_err(From::from)
                }
                fidl_security::WpaCredentials::Psk(psk) => Psk::try_from(psk.as_slice())
                    .map(|psk| BareCredentials::WpaPsk(psk))
                    .map_err(From::from),
                // Unknown variant.
                _ => Err(SecurityError::Incompatible),
            },
            // Unknown variant.
            _ => Err(SecurityError::Incompatible),
        }
    }
}

/// Conversion from a WPA passphrase into bare credentials.
impl From<Passphrase> for BareCredentials {
    fn from(passphrase: Passphrase) -> Self {
        BareCredentials::WpaPassphrase(passphrase)
    }
}

/// Conversion from a WPA PSK into bare credentials.
impl From<Psk> for BareCredentials {
    fn from(psk: Psk) -> Self {
        BareCredentials::WpaPsk(psk)
    }
}

/// Conversion from a WEP key into bare credentials.
impl From<WepKey> for BareCredentials {
    fn from(key: WepKey) -> Self {
        BareCredentials::WepKey(key)
    }
}

/// Description of a wireless network security protocol.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SecurityDescriptor {
    Open,
    Wep,
    Wpa(wpa::WpaDescriptor),
}

impl SecurityDescriptor {
    /// Open (no user authentication nor traffic encryption).
    pub const OPEN: Self = SecurityDescriptor::Open;
    /// WEP (trivially insecure; for legacy support only).
    ///
    /// This protocol is not configurable beyond the format of credentials used to authenticate.
    /// WEP provides no protection and is provided for legacy support only.
    pub const WEP: Self = SecurityDescriptor::Wep;
    /// Legacy WPA (WPA1).
    ///
    /// This protocol is not configurable beyond the format of credentials used to authenticate.
    pub const WPA1: Self = SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa1 { credentials: () });
    /// WPA2 Personal.
    ///
    /// Describes the personal variant of the WPA2 protocol. This descriptor does not specify a
    /// pairwise cipher.
    pub const WPA2_PERSONAL: Self = SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa2 {
        cipher: None,
        authentication: wpa::Authentication::Personal(()),
    });
    /// WPA3 Personal.
    ///
    /// Describes the personal variant of the WPA3 protocol. This descriptor does not specify a
    /// pairwise cipher.
    pub const WPA3_PERSONAL: Self = SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa3 {
        cipher: None,
        authentication: wpa::Authentication::Personal(()),
    });

    /// Binds bare credentials to a descriptor to form an authenticator.
    ///
    /// A security descriptor only describes a protocol and bare credentials provide authentication
    /// data without completely describing a protocol. When compatible, a descriptor and
    /// credentials form the components of an authenticator, and this function attempts to form an
    /// authenticator by binding these components together.
    ///
    /// # Errors
    ///
    /// Returns an error if the bare credentials are incompatible with the descriptor.
    pub fn bind(
        self,
        credentials: Option<BareCredentials>,
    ) -> Result<SecurityAuthenticator, SecurityError> {
        match self {
            SecurityDescriptor::Open if credentials.is_none() => Ok(SecurityAuthenticator::Open),
            SecurityDescriptor::Wep => match credentials {
                Some(BareCredentials::WepKey(key)) => {
                    Ok(SecurityAuthenticator::Wep(wep::WepAuthenticator { key }))
                }
                _ => Err(SecurityError::Incompatible),
            },
            SecurityDescriptor::Wpa(wpa) => match credentials {
                Some(credentials) => wpa.bind(credentials).map(SecurityAuthenticator::Wpa),
                _ => Err(SecurityError::Incompatible),
            },
            _ => Err(SecurityError::Incompatible),
        }
    }

    pub fn is_open(&self) -> bool {
        matches!(self, SecurityDescriptor::Open)
    }

    pub fn is_wep(&self) -> bool {
        matches!(self, SecurityDescriptor::Wep)
    }

    pub fn is_wpa(&self) -> bool {
        matches!(self, SecurityDescriptor::Wpa(_))
    }
}

impl From<fidl_security::Protocol> for SecurityDescriptor {
    fn from(protocol: fidl_security::Protocol) -> Self {
        match protocol {
            fidl_security::Protocol::Open => SecurityDescriptor::Open,
            fidl_security::Protocol::Wep => SecurityDescriptor::Wep,
            fidl_security::Protocol::Wpa1 => {
                SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa1 { credentials: () })
            }
            fidl_security::Protocol::Wpa2Personal => {
                SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa2 {
                    authentication: wpa::Authentication::Personal(()),
                    cipher: None,
                })
            }
            fidl_security::Protocol::Wpa2Enterprise => {
                SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa2 {
                    authentication: wpa::Authentication::Enterprise(()),
                    cipher: None,
                })
            }
            fidl_security::Protocol::Wpa3Personal => {
                SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa3 {
                    authentication: wpa::Authentication::Personal(()),
                    cipher: None,
                })
            }
            fidl_security::Protocol::Wpa3Enterprise => {
                SecurityDescriptor::Wpa(wpa::WpaDescriptor::Wpa3 {
                    authentication: wpa::Authentication::Enterprise(()),
                    cipher: None,
                })
            }
            _ => panic!("unknown FIDL security protocol variant"),
        }
    }
}

impl From<SecurityDescriptor> for fidl_security::Protocol {
    fn from(descriptor: SecurityDescriptor) -> Self {
        match descriptor {
            SecurityDescriptor::Open => fidl_security::Protocol::Open,
            SecurityDescriptor::Wep => fidl_security::Protocol::Wep,
            SecurityDescriptor::Wpa(wpa) => match wpa {
                wpa::WpaDescriptor::Wpa1 { .. } => fidl_security::Protocol::Wpa1,
                wpa::WpaDescriptor::Wpa2 { authentication, .. } => match authentication {
                    wpa::Authentication::Personal(_) => fidl_security::Protocol::Wpa2Personal,
                    wpa::Authentication::Enterprise(_) => fidl_security::Protocol::Wpa2Enterprise,
                },
                wpa::WpaDescriptor::Wpa3 { authentication, .. } => match authentication {
                    wpa::Authentication::Personal(_) => fidl_security::Protocol::Wpa3Personal,
                    wpa::Authentication::Enterprise(_) => fidl_security::Protocol::Wpa3Enterprise,
                },
            },
        }
    }
}

impl From<wpa::WpaDescriptor> for SecurityDescriptor {
    fn from(descriptor: wpa::WpaDescriptor) -> Self {
        SecurityDescriptor::Wpa(descriptor)
    }
}

/// Credentials and configuration for authenticating using a particular wireless network security
/// protocol.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SecurityAuthenticator {
    Open,
    Wep(wep::WepAuthenticator),
    Wpa(wpa::WpaAuthenticator),
}

impl SecurityAuthenticator {
    /// Converts an authenticator into a descriptor with no payload (the unit type `()`). Any
    /// payload (i.e., credentials) are dropped.
    pub fn into_descriptor(self) -> SecurityDescriptor {
        match self {
            SecurityAuthenticator::Open => SecurityDescriptor::Open,
            SecurityAuthenticator::Wep(_) => SecurityDescriptor::Wep,
            SecurityAuthenticator::Wpa(authenticator) => {
                SecurityDescriptor::Wpa(authenticator.into_descriptor())
            }
        }
    }

    pub fn into_wep(self) -> Option<wep::WepAuthenticator> {
        match self {
            SecurityAuthenticator::Wep(authenticator) => Some(authenticator),
            _ => None,
        }
    }

    pub fn into_wpa(self) -> Option<wpa::WpaAuthenticator> {
        match self {
            SecurityAuthenticator::Wpa(authenticator) => Some(authenticator),
            _ => None,
        }
    }

    /// Converts the authenticator to bare credentials, if any.
    ///
    /// Returns `None` if the authenticator is `SecurityAuthenticator::None`, as there are no
    /// corresponding credentials in this case.
    pub fn to_credentials(&self) -> Option<BareCredentials> {
        match self {
            SecurityAuthenticator::Open => None,
            SecurityAuthenticator::Wep(wep::WepAuthenticator { ref key }) => {
                Some(key.clone().into())
            }
            SecurityAuthenticator::Wpa(ref wpa) => Some(wpa.to_credentials().into()),
        }
    }

    pub fn as_wep(&self) -> Option<&wep::WepAuthenticator> {
        match self {
            SecurityAuthenticator::Wep(ref authenticator) => Some(authenticator),
            _ => None,
        }
    }

    pub fn as_wpa(&self) -> Option<&wpa::WpaAuthenticator> {
        match self {
            SecurityAuthenticator::Wpa(ref authenticator) => Some(authenticator),
            _ => None,
        }
    }

    pub fn is_open(&self) -> bool {
        matches!(self, SecurityAuthenticator::Open)
    }

    pub fn is_wep(&self) -> bool {
        matches!(self, SecurityAuthenticator::Wep(_))
    }

    pub fn is_wpa(&self) -> bool {
        matches!(self, SecurityAuthenticator::Wpa(_))
    }
}

impl From<wep::WepAuthenticator> for SecurityAuthenticator {
    fn from(authenticator: wep::WepAuthenticator) -> Self {
        SecurityAuthenticator::Wep(authenticator)
    }
}

impl From<wpa::WpaAuthenticator> for SecurityAuthenticator {
    fn from(authenticator: wpa::WpaAuthenticator) -> Self {
        SecurityAuthenticator::Wpa(authenticator)
    }
}

impl From<SecurityAuthenticator> for fidl_security::Authentication {
    fn from(authenticator: SecurityAuthenticator) -> Self {
        match authenticator {
            SecurityAuthenticator::Open => fidl_security::Authentication {
                protocol: fidl_security::Protocol::Open,
                credentials: None,
            },
            SecurityAuthenticator::Wep(wep) => fidl_security::Authentication {
                protocol: fidl_security::Protocol::Wep,
                credentials: Some(Box::new(fidl_security::Credentials::Wep(wep.into()))),
            },
            SecurityAuthenticator::Wpa(wpa) => {
                use wpa::Authentication::{Enterprise, Personal};
                use wpa::Wpa::{Wpa1, Wpa2, Wpa3};

                let protocol = match (&wpa, wpa.to_credentials()) {
                    (Wpa1 { .. }, _) => fidl_security::Protocol::Wpa1,
                    (Wpa2 { .. }, Personal(_)) => fidl_security::Protocol::Wpa2Personal,
                    (Wpa2 { .. }, Enterprise(_)) => fidl_security::Protocol::Wpa2Enterprise,
                    (Wpa3 { .. }, Personal(_)) => fidl_security::Protocol::Wpa3Personal,
                    (Wpa3 { .. }, Enterprise(_)) => fidl_security::Protocol::Wpa3Enterprise,
                };
                fidl_security::Authentication {
                    protocol,
                    // TODO(https://fxbug.dev/42174395): This panics when encountering WPA Enterprise.
                    credentials: Some(Box::new(fidl_security::Credentials::Wpa(
                        wpa.into_credentials().into(),
                    ))),
                }
            }
        }
    }
}

/// Converts an `Authentication` FIDL datagram into a `SecurityAuthenticator`.
///
/// This conversion should be preferred where possible.
///
/// # Errors
///
/// Returns an error if the `Authentication` datagram is invalid, such as specifying contradictory
/// protocols or encoding incompatible or invalid credentials.
impl TryFrom<fidl_security::Authentication> for SecurityAuthenticator {
    type Error = SecurityError;

    fn try_from(authentication: fidl_security::Authentication) -> Result<Self, Self::Error> {
        let fidl_security::Authentication { protocol, credentials } = authentication;
        match protocol {
            fidl_security::Protocol::Open => match credentials {
                None => Ok(SecurityAuthenticator::Open),
                _ => Err(SecurityError::Incompatible),
            },
            fidl_security::Protocol::Wep => credentials
                .ok_or_else(|| SecurityError::Incompatible)? // No credentials.
                .into_wep()
                .map(wep::WepAuthenticator::try_from)
                .transpose()? // Conversion failure.
                .map(From::from)
                .ok_or_else(|| SecurityError::Incompatible), // Non-WEP credentials.
            fidl_security::Protocol::Wpa1 => credentials
                .ok_or_else(|| SecurityError::Incompatible)? // No credentials.
                .into_wpa()
                .map(wpa::Wpa1Credentials::try_from)
                .transpose()? // Conversion failure.
                .map(|credentials| wpa::WpaAuthenticator::Wpa1 { credentials })
                .map(From::from)
                .ok_or_else(|| SecurityError::Incompatible), // Non-WPA credentials.
            fidl_security::Protocol::Wpa2Personal => credentials
                .ok_or_else(|| SecurityError::Incompatible)? // No credentials.
                .into_wpa()
                .map(wpa::Wpa2PersonalCredentials::try_from)
                .transpose()? // Conversion failure.
                .map(From::from)
                .map(|authentication| wpa::WpaAuthenticator::Wpa2 { cipher: None, authentication })
                .map(From::from)
                .ok_or_else(|| SecurityError::Incompatible), // Non-WPA credentials.
            fidl_security::Protocol::Wpa3Personal => credentials
                .ok_or_else(|| SecurityError::Incompatible)? // No credentials.
                .into_wpa()
                .map(wpa::Wpa3PersonalCredentials::try_from)
                .transpose()? // Conversion failure.
                .map(From::from)
                .map(|authentication| wpa::WpaAuthenticator::Wpa3 { cipher: None, authentication })
                .map(From::from)
                .ok_or_else(|| SecurityError::Incompatible), // Non-WPA credentials.
            // TODO(https://fxbug.dev/42174395): This returns an error when encountering WPA Enterprise
            //                        protocols. Some conversions of composing types panic, but
            //                        this top-level conversion insulates client code from this and
            //                        instead yields an error.
            _ => Err(SecurityError::Incompatible),
        }
    }
}

#[cfg(test)]
mod tests {
    use fidl_fuchsia_wlan_common_security as fidl_security;

    use test_case::test_case;

    use crate::security::{
        wpa::{self, Authentication, Wpa2PersonalCredentials},
        SecurityAuthenticator, SecurityError,
    };

    pub trait AuthenticationTestCase: Sized {
        fn wpa2_personal_psk() -> Self;
        fn wpa3_personal_psk() -> Self;
        fn wpa3_personal_wep_key() -> Self;
        fn wpa3_personal_no_credentials() -> Self;
    }

    impl AuthenticationTestCase for fidl_security::Authentication {
        fn wpa2_personal_psk() -> Self {
            fidl_security::Authentication {
                protocol: fidl_security::Protocol::Wpa2Personal,
                credentials: Some(Box::new(fidl_security::Credentials::Wpa(
                    fidl_security::WpaCredentials::Psk([0u8; 32]),
                ))),
            }
        }

        // Invalid: WPA3 with PSK.
        fn wpa3_personal_psk() -> Self {
            fidl_security::Authentication {
                protocol: fidl_security::Protocol::Wpa3Personal,
                credentials: Some(Box::new(fidl_security::Credentials::Wpa(
                    fidl_security::WpaCredentials::Psk([0u8; 32]),
                ))),
            }
        }

        // Invalid: WPA3 with WEP key.
        fn wpa3_personal_wep_key() -> Self {
            fidl_security::Authentication {
                protocol: fidl_security::Protocol::Wpa3Personal,
                credentials: Some(Box::new(fidl_security::Credentials::Wep(
                    fidl_security::WepCredentials { key: vec![0u8; 13] },
                ))),
            }
        }

        // Invalid: WPA3 with no credentials.
        fn wpa3_personal_no_credentials() -> Self {
            fidl_security::Authentication {
                protocol: fidl_security::Protocol::Wpa3Personal,
                credentials: None,
            }
        }
    }

    // TODO(seanolson): Move this assertion into a `SecurityAuthenticatorAssertion` trait (a la
    //                  `AuthenticationTestCase`) and test via the `using` pattern in the
    //                  `test-case` 2.0.0 series.
    #[test_case(AuthenticationTestCase::wpa2_personal_psk() => matches
        Ok(SecurityAuthenticator::Wpa(wpa::Wpa::Wpa2 {
            authentication: Authentication::Personal(Wpa2PersonalCredentials::Psk(_)),
            ..
        }))
    )]
    #[test_case(AuthenticationTestCase::wpa3_personal_psk() => Err(SecurityError::Incompatible))]
    #[test_case(AuthenticationTestCase::wpa3_personal_wep_key() => Err(SecurityError::Incompatible))]
    #[test_case(AuthenticationTestCase::wpa3_personal_no_credentials() => Err(SecurityError::Incompatible))]
    fn security_authenticator_from_authentication_fidl(
        authentication: fidl_security::Authentication,
    ) -> Result<SecurityAuthenticator, SecurityError> {
        SecurityAuthenticator::try_from(authentication)
    }

    #[test]
    fn authentication_fidl_from_security_authenticator() {
        let authenticator = SecurityAuthenticator::Wpa(wpa::WpaAuthenticator::Wpa3 {
            authentication: wpa::Authentication::Personal(
                wpa::Wpa3PersonalCredentials::Passphrase("roflcopter".try_into().unwrap()),
            ),
            cipher: None,
        });
        let authentication = fidl_security::Authentication::from(authenticator);
        assert_eq!(
            authentication,
            fidl_security::Authentication {
                protocol: fidl_security::Protocol::Wpa3Personal,
                credentials: Some(Box::new(fidl_security::Credentials::Wpa(
                    fidl_security::WpaCredentials::Passphrase(b"roflcopter".as_slice().into()),
                ))),
            }
        );
    }
}