|
| 1 | +// Copyright 2023 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ssh |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestClientAuthRestrictedPublicKeyAlgos(t *testing.T) { |
| 12 | + for _, tt := range []struct { |
| 13 | + name string |
| 14 | + key Signer |
| 15 | + wantError bool |
| 16 | + }{ |
| 17 | + {"rsa", testSigners["rsa"], false}, |
| 18 | + {"dsa", testSigners["dsa"], true}, |
| 19 | + {"ed25519", testSigners["ed25519"], true}, |
| 20 | + } { |
| 21 | + c1, c2, err := netPipe() |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("netPipe: %v", err) |
| 24 | + } |
| 25 | + defer c1.Close() |
| 26 | + defer c2.Close() |
| 27 | + serverConf := &ServerConfig{ |
| 28 | + PublicKeyAuthAlgorithms: []string{KeyAlgoRSASHA256, KeyAlgoRSASHA512}, |
| 29 | + PublicKeyCallback: func(conn ConnMetadata, key PublicKey) (*Permissions, error) { |
| 30 | + return nil, nil |
| 31 | + }, |
| 32 | + } |
| 33 | + serverConf.AddHostKey(testSigners["ecdsap256"]) |
| 34 | + |
| 35 | + done := make(chan struct{}) |
| 36 | + go func() { |
| 37 | + defer close(done) |
| 38 | + NewServerConn(c1, serverConf) |
| 39 | + }() |
| 40 | + |
| 41 | + clientConf := ClientConfig{ |
| 42 | + User: "user", |
| 43 | + Auth: []AuthMethod{ |
| 44 | + PublicKeys(tt.key), |
| 45 | + }, |
| 46 | + HostKeyCallback: InsecureIgnoreHostKey(), |
| 47 | + } |
| 48 | + |
| 49 | + _, _, _, err = NewClientConn(c2, "", &clientConf) |
| 50 | + if err != nil { |
| 51 | + if !tt.wantError { |
| 52 | + t.Errorf("%s: got unexpected error %q", tt.name, err.Error()) |
| 53 | + } |
| 54 | + } else if tt.wantError { |
| 55 | + t.Errorf("%s: succeeded, but want error", tt.name) |
| 56 | + } |
| 57 | + <-done |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestNewServerConnValidationErrors(t *testing.T) { |
| 62 | + c1, c2, err := netPipe() |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("netPipe: %v", err) |
| 65 | + } |
| 66 | + defer c1.Close() |
| 67 | + defer c2.Close() |
| 68 | + |
| 69 | + serverConf := &ServerConfig{ |
| 70 | + PublicKeyAuthAlgorithms: []string{CertAlgoRSAv01}, |
| 71 | + } |
| 72 | + _, _, _, err = NewServerConn(c1, serverConf) |
| 73 | + if err == nil { |
| 74 | + t.Fatal("NewServerConn with invalid public key auth algorithms succeeded") |
| 75 | + } |
| 76 | + serverConf = &ServerConfig{ |
| 77 | + Config: Config{ |
| 78 | + KeyExchanges: []string{kexAlgoDHGEXSHA256}, |
| 79 | + }, |
| 80 | + } |
| 81 | + _, _, _, err = NewServerConn(c1, serverConf) |
| 82 | + if err == nil { |
| 83 | + t.Fatal("NewServerConn with unsupported key exchange succeeded") |
| 84 | + } |
| 85 | +} |
0 commit comments