puressh 0.2.4

A pure-Rust SSH (Secure Shell) protocol library, in the spirit of libssh, built on purecrypto.
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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Crypto-algorithm selection for `ssh_config` / `sshd_config` keywords.
//!
//! Implements the OpenSSH keywords that override the built-in algorithm
//! preference lists — `Ciphers`, `MACs`, `KexAlgorithms`,
//! `HostKeyAlgorithms`, and (client-only) `PubkeyAcceptedAlgorithms`.
//!
//! Every algorithm name a directive resolves to is one this build actually
//! implements. Names OpenSSH knows but puressh lacks (`aes128-cbc`,
//! `ssh-dss`, ...) are dropped, so configs written for OpenSSH still load;
//! any other unknown name fails the parse with [`ConfigError::BadValue`]
//! naming the offending token and the source line. A directive that
//! resolves to the empty set is likewise a `BadValue` — an SSH peer
//! advertising an empty name-list cannot negotiate.
//!
//! ## List-modifier grammar (OpenSSH `ssh_config(5)`)
//!
//! The argument is a comma-separated list. A leading modifier character on
//! the *whole* list selects how it combines with the built-in defaults:
//!
//! | Form        | Effect                                                  |
//! |-------------|---------------------------------------------------------|
//! | `a,b,c`     | **replace** the defaults with exactly this list         |
//! | `+a,b`      | **append** these to the end of the defaults             |
//! | `-a,b*`     | **remove** entries matching these globs from defaults   |
//! | `^a,b`      | **prepend** these to the front of the defaults          |
//!
//! For `+`, `^`, and bare-replace the named tokens are validated against the
//! catalogue. For `-` the tokens are globs (`aes*`) matched against the
//! current list; an unmatched glob is *not* an error (OpenSSH tolerates it),
//! but the resulting set must still be non-empty.
//!
//! The KEX strict-kex marker names (`kex-strict-{c,s}-v00@openssh.com`) are
//! signalling tokens, never user-selectable algorithms: they are excluded
//! from the kex default list exposed here and are re-appended by the KEXINIT
//! builders after override resolution, so a user `-`/replace can never strip
//! the Terrapin (CVE-2023-48795) mitigation.

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use super::ConfigError;
use super::glob::glob_match;
use crate::transport::kex::{defaults, is_strict_kex_marker};

/// Which algorithm family a directive selects, used to pick the catalogue of
/// known names and the default preference list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AlgoCategory {
    /// `Ciphers` — symmetric encryption suites.
    Cipher,
    /// `MACs` — message authentication codes.
    Mac,
    /// `KexAlgorithms` — key-exchange methods (excluding strict-kex markers).
    Kex,
    /// `HostKeyAlgorithms` — server host-key / signature algorithms.
    HostKey,
    /// `PubkeyAcceptedAlgorithms` — client publickey-auth signature algorithms.
    PubkeyAccepted,
    /// `CASignatureAlgorithms` — signature algorithms accepted from a
    /// certificate authority when verifying an OpenSSH certificate.
    CaSignature,
}

/// The default `CASignatureAlgorithms` set (OpenSSH default, minus plain
/// SHA-1 `ssh-rsa`). These are the algorithms a CA's signature over a
/// certificate may use.
pub const CA_SIGNATURE_DEFAULTS: &[&str] = &[
    "ssh-ed25519",
    "ecdsa-sha2-nistp256",
    "ecdsa-sha2-nistp384",
    "ecdsa-sha2-nistp521",
    "rsa-sha2-512",
    "rsa-sha2-256",
];

/// Real (non-marker) KEX algorithm names, in default preference order.
///
/// `defaults::KEX` carries the two strict-kex signalling markers as its
/// trailing entries; this filters them out so they are never user-visible
/// or user-removable.
pub fn kex_names() -> Vec<&'static str> {
    defaults::KEX
        .iter()
        .copied()
        .filter(|n| !is_strict_kex_marker(n))
        .collect()
}

/// Key exchanges implemented for old peers but, as in OpenSSH, only used
/// when configured (`KexAlgorithms +diffie-hellman-group14-sha1`).
const LEGACY_KEX: &[&str] = &[
    "diffie-hellman-group14-sha1",
    "diffie-hellman-group-exchange-sha1",
    "diffie-hellman-group1-sha1",
];

/// The set of valid algorithm names for `cat`, derived from the live
/// catalogues (so it can never drift from what the crypto layer implements).
///
/// `HostKeyAlgorithms` accepts one extra name over the strict default set:
/// legacy bare `ssh-rsa` (SHA-1). It is *namable* but never a default (see
/// [`default_list`]), so it can only enter the list when a config explicitly
/// requests it (e.g. `HostKeyAlgorithms +ssh-rsa`). Selecting it flips the
/// per-process SHA-1 opt-in — see `crate::hostkey::set_allow_rsa_sha1` and the
/// wiring in the client config resolver. `PubkeyAcceptedAlgorithms` keeps the
/// strict set (no SHA-1 pubkey auth).
pub fn known_names(cat: AlgoCategory) -> Vec<&'static str> {
    match cat {
        AlgoCategory::Cipher => crate::cipher::ALL.iter().map(|c| c.name).collect(),
        AlgoCategory::Mac => crate::mac::ALL.iter().map(|m| m.name).collect(),
        AlgoCategory::Kex => {
            let mut v = kex_names();
            v.extend_from_slice(LEGACY_KEX);
            v
        }
        // The defaults (certificate types included), plus legacy `ssh-rsa`
        // (SHA-1) and the ML-DSA composite: namable, never a default, as in
        // OpenSSH (`HostKeyAlgorithms +ssh-rsa`, `PubkeyAcceptedAlgorithms
        // +ssh-rsa` for old peers).
        AlgoCategory::HostKey | AlgoCategory::PubkeyAccepted => {
            let mut names = default_list(cat);
            names.push("ssh-mldsa44-ed25519-cert-v01@openssh.com");
            names.push("ssh-mldsa44-ed25519@openssh.com");
            names.push("ssh-rsa-cert-v01@openssh.com");
            names.push("ssh-rsa");
            names
        }
        AlgoCategory::CaSignature => {
            let mut names = CA_SIGNATURE_DEFAULTS.to_vec();
            names.push("ssh-mldsa44-ed25519@openssh.com");
            names
        }
    }
}

/// Algorithm names OpenSSH knows (some versions of it) that this build does
/// not implement. Configs written for OpenSSH name them — `Ciphers
/// +aes128-cbc` for old network gear, `PubkeyAcceptedKeyTypes +ssh-rsa` —
/// and OpenSSH accepts them; here they are dropped from the list (neither
/// side could use them), so such a file still loads. Names OpenSSH does not
/// know either remain an error.
fn openssh_only(cat: AlgoCategory) -> &'static [&'static str] {
    const CIPHERS: &[&str] = &[
        "3des-cbc",
        "aes128-cbc",
        "aes192-cbc",
        "aes256-cbc",
        "rijndael-cbc@lysator.liu.se",
        "blowfish-cbc",
        "cast128-cbc",
        "arcfour",
        "arcfour128",
        "arcfour256",
        "aes128-ctr",
        "aes192-ctr",
        "aes256-ctr",
        "aes128-gcm@openssh.com",
        "aes256-gcm@openssh.com",
        "chacha20-poly1305@openssh.com",
    ];
    const MACS: &[&str] = &[
        "hmac-sha1",
        "hmac-sha1-96",
        "hmac-sha2-256",
        "hmac-sha2-512",
        "hmac-md5",
        "hmac-md5-96",
        "umac-64@openssh.com",
        "umac-128@openssh.com",
        "hmac-sha1-etm@openssh.com",
        "hmac-sha1-96-etm@openssh.com",
        "hmac-sha2-256-etm@openssh.com",
        "hmac-sha2-512-etm@openssh.com",
        "hmac-md5-etm@openssh.com",
        "hmac-md5-96-etm@openssh.com",
        "umac-64-etm@openssh.com",
        "umac-128-etm@openssh.com",
    ];
    const KEX: &[&str] = &[
        "diffie-hellman-group1-sha1",
        "diffie-hellman-group14-sha1",
        "diffie-hellman-group14-sha256",
        "diffie-hellman-group16-sha512",
        "diffie-hellman-group18-sha512",
        "diffie-hellman-group-exchange-sha1",
        "diffie-hellman-group-exchange-sha256",
        "ecdh-sha2-nistp256",
        "ecdh-sha2-nistp384",
        "ecdh-sha2-nistp521",
        "curve25519-sha256",
        "curve25519-sha256@libssh.org",
        "sntrup761x25519-sha512",
        "sntrup761x25519-sha512@openssh.com",
        "mlkem768x25519-sha256",
    ];
    const KEYS: &[&str] = &[
        "ssh-ed25519",
        "ssh-ed25519-cert-v01@openssh.com",
        "sk-ssh-ed25519@openssh.com",
        "sk-ssh-ed25519-cert-v01@openssh.com",
        "ssh-mldsa44-ed25519@openssh.com",
        "ssh-mldsa44-ed25519-cert-v01@openssh.com",
        "ecdsa-sha2-nistp256",
        "ecdsa-sha2-nistp256-cert-v01@openssh.com",
        "ecdsa-sha2-nistp384",
        "ecdsa-sha2-nistp384-cert-v01@openssh.com",
        "ecdsa-sha2-nistp521",
        "ecdsa-sha2-nistp521-cert-v01@openssh.com",
        "sk-ecdsa-sha2-nistp256@openssh.com",
        "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
        "webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
        "webauthn-sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
        "ssh-dss",
        "ssh-dss-cert-v01@openssh.com",
        "ssh-rsa",
        "ssh-rsa-cert-v01@openssh.com",
        "rsa-sha2-256",
        "rsa-sha2-256-cert-v01@openssh.com",
        "rsa-sha2-512",
        "rsa-sha2-512-cert-v01@openssh.com",
    ];
    match cat {
        AlgoCategory::Cipher => CIPHERS,
        AlgoCategory::Mac => MACS,
        AlgoCategory::Kex => KEX,
        AlgoCategory::HostKey | AlgoCategory::PubkeyAccepted | AlgoCategory::CaSignature => KEYS,
    }
}

/// The built-in default preference list for `cat` (what a bare-replace
/// directive starts from for `+`/`-`/`^`). The KEX list excludes the
/// strict-kex markers.
pub fn default_list(cat: AlgoCategory) -> Vec<&'static str> {
    match cat {
        AlgoCategory::Cipher => defaults::CIPHERS.to_vec(),
        AlgoCategory::Mac => defaults::MACS.to_vec(),
        AlgoCategory::Kex => kex_names(),
        // Certificate types first, then plain keys, as OpenSSH lists them
        // (so `-ssh-rsa` and the like keep certificates working).
        AlgoCategory::HostKey | AlgoCategory::PubkeyAccepted => {
            let mut v = crate::cert::CERT_KEY_NAMES.to_vec();
            v.extend_from_slice(crate::hostkey::HOST_KEY_VERIFY_NAMES);
            v
        }
        AlgoCategory::CaSignature => CA_SIGNATURE_DEFAULTS.to_vec(),
    }
}

/// Split the raw whitespace-tokenised args back into a single comma-separated
/// list and normalise it: join on a single space (OpenSSH tolerates spaces
/// around commas), split on commas, trim each, drop empties.
///
/// Returns the optional leading modifier char (`+`, `-`, `^`) stripped from
/// the *first* element, plus the cleaned token vector.
fn tokenize(args: &[String]) -> (Option<char>, Vec<String>) {
    let joined = args.join(" ");
    let modifier = match joined.chars().find(|c| !c.is_whitespace()) {
        Some(c @ ('+' | '-' | '^')) => Some(c),
        _ => None,
    };
    // Strip the modifier (and any leading whitespace before it) from the
    // working string before splitting on commas.
    let body = if let Some(m) = modifier {
        let trimmed = joined.trim_start();
        trimmed
            .strip_prefix(m)
            .map(|s| s.to_string())
            .unwrap_or_else(|| trimmed.to_string())
    } else {
        joined
    };
    let tokens = body
        .split(',')
        .map(|t| t.trim().to_string())
        .filter(|t| !t.is_empty())
        .collect();
    (modifier, tokens)
}

fn bad_value(line_no: usize, keyword: &str, msg: String) -> ConfigError {
    ConfigError::BadValue {
        line: line_no,
        keyword: keyword.to_string(),
        msg,
    }
}

/// Resolve a config algorithm directive into a concrete preference list,
/// applying the OpenSSH list-modifier grammar and validating every resulting
/// name against the catalogue for `cat`.
///
/// `args` is `ParsedLine::args` (already whitespace-split); `line_no` and
/// `keyword` flow into the error so a rejection points at the exact source.
///
/// Returns [`ConfigError::BadValue`] if any named token (for replace / `+` /
/// `^`) is unknown, or if the resolved set is empty.
pub fn resolve_algo_list(
    cat: AlgoCategory,
    args: &[String],
    line_no: usize,
    keyword: &str,
) -> Result<Vec<String>, ConfigError> {
    let (modifier, tokens) = tokenize(args);
    if tokens.is_empty() {
        return Err(bad_value(
            line_no,
            keyword,
            "expected at least one algorithm name".to_string(),
        ));
    }

    let known = known_names(cat);
    let is_known = |name: &str| known.contains(&name);
    // Names only OpenSSH implements are dropped here (see `openssh_only`);
    // `-` takes globs and needs no filtering.
    let named = tokens.len();
    let tokens: Vec<String> = if modifier == Some('-') {
        tokens
    } else {
        tokens
            .into_iter()
            .filter(|t| is_known(t) || !openssh_only(cat).contains(&t.as_str()))
            .collect()
    };
    if named > 0 && tokens.is_empty() && modifier.is_none() {
        return Err(bad_value(
            line_no,
            keyword,
            "none of these algorithms is implemented by puressh".to_string(),
        ));
    }

    // Each name may be a pattern (`*`, `?`): it stands for every supported
    // algorithm it matches, in the catalogue's order, as OpenSSH's
    // kex_assemble_names; a plain name must be a supported one.
    let expand = |t: &str| -> Result<Vec<String>, ConfigError> {
        if t.contains(['*', '?']) {
            return Ok(known
                .iter()
                .filter(|n| glob_match(t, n))
                .map(|n| n.to_string())
                .collect());
        }
        if !is_known(t) {
            return Err(bad_value(
                line_no,
                keyword,
                format!("unknown algorithm {t:?}"),
            ));
        }
        Ok(alloc::vec![t.to_string()])
    };
    let push_unique = |out: &mut Vec<String>, names: Vec<String>| {
        for n in names {
            if !out.contains(&n) {
                out.push(n);
            }
        }
    };

    let result: Vec<String> = match modifier {
        // `-glob,...` — remove matching entries from the defaults. Tokens are
        // globs, not exact names, so they are NOT validated against the
        // catalogue; an unmatched glob is tolerated, as in OpenSSH.
        Some('-') => default_list(cat)
            .into_iter()
            .filter(|name| !tokens.iter().any(|pat| glob_match(pat, name)))
            .map(|s| s.to_string())
            .collect(),

        // `+a,b` — append to the end of the defaults, skipping any already
        // present.
        Some('+') => {
            let mut out: Vec<String> = default_list(cat)
                .into_iter()
                .map(|s| s.to_string())
                .collect();
            for t in &tokens {
                push_unique(&mut out, expand(t)?);
            }
            out
        }

        // `^a,b` — prepend to the front of the defaults, de-duplicating the
        // (now-redundant) later occurrences.
        Some('^') => {
            let mut out: Vec<String> = Vec::new();
            for t in &tokens {
                push_unique(&mut out, expand(t)?);
            }
            push_unique(
                &mut out,
                default_list(cat)
                    .into_iter()
                    .map(|s| s.to_string())
                    .collect(),
            );
            out
        }

        // Bare list — replace the defaults wholesale with exactly these
        // names (preserving the user's order; de-duplicated).
        None => {
            let mut out: Vec<String> = Vec::with_capacity(tokens.len());
            for t in &tokens {
                push_unique(&mut out, expand(t)?);
            }
            out
        }

        Some(_) => unreachable!("tokenize only yields +, -, ^"),
    };

    if result.is_empty() {
        return Err(bad_value(
            line_no,
            keyword,
            "directive resolves to an empty algorithm set".to_string(),
        ));
    }
    Ok(result)
}

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

    fn args(s: &str) -> Vec<String> {
        s.split_whitespace().map(|t| t.to_string()).collect()
    }

    #[test]
    fn bare_list_replaces() {
        let got = resolve_algo_list(
            AlgoCategory::Cipher,
            &args("aes128-ctr,aes256-ctr"),
            1,
            "Ciphers",
        )
        .unwrap();
        assert_eq!(got, vec!["aes128-ctr", "aes256-ctr"]);
    }

    #[test]
    fn append_modifier() {
        let defaults = default_list(AlgoCategory::Mac);
        let got = resolve_algo_list(AlgoCategory::Mac, &args("+hmac-sha2-256"), 1, "MACs").unwrap();
        // The default list is preserved, hmac-sha2-256 was already in it so
        // it is not duplicated.
        assert_eq!(got.len(), defaults.len());
        assert!(got.iter().any(|m| m == "hmac-sha2-256"));
    }

    #[test]
    fn append_modifier_adds_new() {
        // Build a default list without one entry, then re-append it.
        let got =
            resolve_algo_list(AlgoCategory::Cipher, &args("+aes128-ctr"), 1, "Ciphers").unwrap();
        let count = got.iter().filter(|c| *c == "aes128-ctr").count();
        assert_eq!(count, 1, "append must not duplicate an existing entry");
    }

    #[test]
    fn remove_glob() {
        let got = resolve_algo_list(AlgoCategory::Cipher, &args("-aes*"), 1, "Ciphers").unwrap();
        assert!(got.iter().all(|c| !c.starts_with("aes")));
        assert!(got.iter().any(|c| c == "chacha20-poly1305@openssh.com"));
    }

    #[test]
    fn remove_glob_unmatched_is_ok() {
        // A glob that matches nothing is tolerated; the set is unchanged.
        let got = resolve_algo_list(
            AlgoCategory::Cipher,
            &args("-nonexistent-cipher"),
            1,
            "Ciphers",
        )
        .unwrap();
        assert_eq!(got.len(), default_list(AlgoCategory::Cipher).len());
    }

    #[test]
    fn prepend_modifier() {
        let got =
            resolve_algo_list(AlgoCategory::Cipher, &args("^aes128-ctr"), 1, "Ciphers").unwrap();
        assert_eq!(got[0], "aes128-ctr");
        // The rest of the defaults follow, minus the duplicate we moved up.
        assert_eq!(got.len(), default_list(AlgoCategory::Cipher).len());
    }

    #[test]
    fn unknown_name_rejected_with_line() {
        let err =
            resolve_algo_list(AlgoCategory::Cipher, &args("aes999-ctr"), 7, "Ciphers").unwrap_err();
        match err {
            ConfigError::BadValue { line, keyword, msg } => {
                assert_eq!(line, 7);
                assert_eq!(keyword, "Ciphers");
                assert!(
                    msg.contains("aes999-ctr"),
                    "msg should name the token: {msg}"
                );
            }
            other => panic!("expected BadValue, got {other:?}"),
        }
    }

    #[test]
    fn append_unknown_rejected() {
        let err =
            resolve_algo_list(AlgoCategory::Mac, &args("+hmac-bogus"), 3, "MACs").unwrap_err();
        assert!(matches!(err, ConfigError::BadValue { line: 3, .. }));
    }

    #[test]
    fn empty_directive_rejected() {
        let err = resolve_algo_list(AlgoCategory::Cipher, &[], 2, "Ciphers").unwrap_err();
        assert!(matches!(err, ConfigError::BadValue { line: 2, .. }));
    }

    #[test]
    fn remove_everything_is_empty_error() {
        let err = resolve_algo_list(AlgoCategory::Cipher, &args("-*"), 4, "Ciphers").unwrap_err();
        match err {
            ConfigError::BadValue { line, msg, .. } => {
                assert_eq!(line, 4);
                assert!(msg.contains("empty"), "msg: {msg}");
            }
            other => panic!("expected BadValue, got {other:?}"),
        }
    }

    #[test]
    fn comma_and_whitespace_tolerated() {
        // OpenSSH accepts spaces around commas when the whole thing is one
        // logical list; our tokenizer joins args then splits on commas.
        let got = resolve_algo_list(
            AlgoCategory::Cipher,
            &args("aes128-ctr, aes256-ctr , chacha20-poly1305@openssh.com"),
            1,
            "Ciphers",
        )
        .unwrap();
        assert_eq!(
            got,
            vec!["aes128-ctr", "aes256-ctr", "chacha20-poly1305@openssh.com"]
        );
    }

    #[test]
    fn kex_markers_never_user_visible() {
        // The strict-kex markers are not in the known/default kex names, so a
        // user cannot name them and they cannot be removed.
        assert!(
            !known_names(AlgoCategory::Kex)
                .iter()
                .any(|n| is_strict_kex_marker(n))
        );
        assert!(
            !default_list(AlgoCategory::Kex)
                .iter()
                .any(|n| is_strict_kex_marker(n))
        );
        let err = resolve_algo_list(
            AlgoCategory::Kex,
            &args("kex-strict-c-v00@openssh.com"),
            1,
            "KexAlgorithms",
        )
        .unwrap_err();
        assert!(matches!(err, ConfigError::BadValue { .. }));
    }

    #[test]
    fn hostkey_known_names_include_optin_ssh_rsa() {
        // `ssh-rsa` is namable for HostKeyAlgorithms (opt-in)...
        assert!(known_names(AlgoCategory::HostKey).contains(&"ssh-rsa"));
        assert!(known_names(AlgoCategory::HostKey).contains(&"ssh-ed25519"));
        // ...but never a default, so it only enters a resolved list when the
        // config explicitly requests it.
        assert!(!default_list(AlgoCategory::HostKey).contains(&"ssh-rsa"));
        // Likewise for PubkeyAcceptedAlgorithms: namable, never a default.
        assert!(known_names(AlgoCategory::PubkeyAccepted).contains(&"ssh-rsa"));
        assert!(!default_list(AlgoCategory::PubkeyAccepted).contains(&"ssh-rsa"));
    }

    #[test]
    fn hostkey_plus_ssh_rsa_accepted() {
        // `HostKeyAlgorithms +ssh-rsa` now resolves instead of erroring.
        let got = resolve_algo_list(
            AlgoCategory::HostKey,
            &args("+ssh-rsa"),
            1,
            "HostKeyAlgorithms",
        )
        .unwrap();
        assert!(got.iter().any(|n| n == "ssh-rsa"));
        // The defaults are preserved ahead of the appended legacy name.
        assert!(got.iter().any(|n| n == "ssh-ed25519"));
        assert!(
            got.iter().position(|n| n == "ssh-ed25519") < got.iter().position(|n| n == "ssh-rsa")
        );
    }

    #[test]
    fn pubkey_accepted_plus_ssh_rsa_appends_it() {
        // `+ssh-rsa` (common in configs for servers before OpenSSH 7.2)
        // appends the SHA-1 name after the defaults, as with OpenSSH.
        let got = resolve_algo_list(
            AlgoCategory::PubkeyAccepted,
            &args("+ssh-rsa"),
            1,
            "PubkeyAcceptedAlgorithms",
        )
        .unwrap();
        let mut want = default_list(AlgoCategory::PubkeyAccepted);
        want.push("ssh-rsa");
        assert_eq!(got, want);
    }

    #[test]
    fn openssh_only_names_are_dropped_unknown_ones_refused() {
        let got = resolve_algo_list(
            AlgoCategory::Cipher,
            &args("+blowfish-cbc,arcfour"),
            1,
            "Ciphers",
        )
        .unwrap();
        assert_eq!(got, default_list(AlgoCategory::Cipher));
        let got = resolve_algo_list(
            AlgoCategory::Cipher,
            &args("arcfour256,aes256-ctr"),
            1,
            "Ciphers",
        )
        .unwrap();
        assert_eq!(got, vec!["aes256-ctr"]);
        // Implemented legacy names are appended (never defaults).
        let got = resolve_algo_list(
            AlgoCategory::Cipher,
            &args("+aes128-cbc,3des-cbc"),
            1,
            "Ciphers",
        )
        .unwrap();
        assert_eq!(&got[got.len() - 2..], &["aes128-cbc", "3des-cbc"]);
        // Only unimplemented names: nothing left to offer.
        assert!(
            resolve_algo_list(AlgoCategory::Cipher, &args("blowfish-cbc"), 1, "Ciphers").is_err()
        );
        // Not an OpenSSH name either: still a typo to report.
        assert!(
            resolve_algo_list(AlgoCategory::Cipher, &args("+aes128-cbx"), 1, "Ciphers").is_err()
        );
        assert!(
            resolve_algo_list(
                AlgoCategory::Kex,
                &args("+diffie-hellman-group1-sha1"),
                1,
                "KexAlgorithms"
            )
            .is_ok()
        );
    }

    #[test]
    fn pubkey_accepted_uses_hostkey_catalogue() {
        let got = resolve_algo_list(
            AlgoCategory::PubkeyAccepted,
            &args("ssh-ed25519,rsa-sha2-512"),
            1,
            "PubkeyAcceptedAlgorithms",
        )
        .unwrap();
        assert_eq!(got, vec!["ssh-ed25519", "rsa-sha2-512"]);
    }

    #[test]
    fn patterns_stand_for_the_supported_names_they_match() {
        let all = resolve_algo_list(AlgoCategory::PubkeyAccepted, &args("*"), 1, "k").unwrap();
        assert_eq!(all, known_names(AlgoCategory::PubkeyAccepted));
        let ctr = resolve_algo_list(AlgoCategory::Cipher, &args("aes*-ctr"), 1, "k").unwrap();
        assert_eq!(ctr, ["aes256-ctr", "aes192-ctr", "aes128-ctr"]);
        let plus = resolve_algo_list(AlgoCategory::Mac, &args("+hmac-md5*"), 1, "k").unwrap();
        assert!(plus.iter().any(|m| m == "hmac-md5-96"));
        assert!(resolve_algo_list(AlgoCategory::Cipher, &args("nope*"), 1, "k").is_err());
    }
}