|
| 1 | +package signer |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/dsa" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/ed25519" |
| 7 | + "crypto/elliptic" |
| 8 | + "crypto/rand" |
| 9 | + "crypto/rsa" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "golang.org/x/crypto/ssh" |
| 13 | +) |
| 14 | + |
| 15 | +func TestValidatePublicKey(t *testing.T) { |
| 16 | + // Strong RSA (2048-bit) |
| 17 | + strongRSAKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("Failed to generate strong RSA key: %v", err) |
| 20 | + } |
| 21 | + strongRSAPub, err := ssh.NewPublicKey(&strongRSAKey.PublicKey) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("Failed to create SSH public key from strong RSA key: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + // Weak RSA (1024-bit) |
| 27 | + weakRSAKey, err := rsa.GenerateKey(rand.Reader, 1024) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Failed to generate weak RSA key: %v", err) |
| 30 | + } |
| 31 | + weak1024RSAPub, err := ssh.NewPublicKey(&weakRSAKey.PublicKey) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Failed to create SSH public key from weak RSA key: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Strong curve ECDSA (P-256) |
| 37 | + strongECDSAKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Failed to generate strong ECDSA key: %v", err) |
| 40 | + } |
| 41 | + strongECDSAPub, err := ssh.NewPublicKey(&strongECDSAKey.PublicKey) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Failed to create SSH public key from strong ECDSA key: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Strong Ed25519 |
| 47 | + ed25519Pub, ed25519Priv, err := ed25519.GenerateKey(rand.Reader) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Failed to generate Ed25519 key: %v", err) |
| 50 | + } |
| 51 | + ed25519SSHPub, err := ssh.NewPublicKey(ed25519Pub) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to create SSH public key from Ed25519 key: %v", err) |
| 54 | + } |
| 55 | + _ = ed25519Priv // Suppress unused variable warning |
| 56 | + |
| 57 | + // Weak DSA |
| 58 | + params := new(dsa.Parameters) |
| 59 | + if err := dsa.GenerateParameters(params, rand.Reader, dsa.L1024N160); err != nil { |
| 60 | + t.Fatalf("Failed to generate DSA params: %v", err) |
| 61 | + } |
| 62 | + dsaKey := new(dsa.PrivateKey) |
| 63 | + dsaKey.Parameters = *params |
| 64 | + if err := dsa.GenerateKey(dsaKey, rand.Reader); err != nil { |
| 65 | + t.Fatalf("Failed to generate DSA key: %v", err) |
| 66 | + } |
| 67 | + dsaPub, err := ssh.NewPublicKey(&dsaKey.PublicKey) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("Failed to create SSH public key from DSA key: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + testCases := []struct { |
| 73 | + name string |
| 74 | + key ssh.PublicKey |
| 75 | + wantErr bool |
| 76 | + }{ |
| 77 | + {"Strong RSA", strongRSAPub, false}, |
| 78 | + {"Weak RSA", weak1024RSAPub, true}, |
| 79 | + {"Strong ECDSA", strongECDSAPub, false}, |
| 80 | + {"Ed25519", ed25519SSHPub, false}, |
| 81 | + {"DSA", dsaPub, true}, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range testCases { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + err := ValidatePublicKey(tc.key) |
| 87 | + if (err != nil) != tc.wantErr { |
| 88 | + t.Errorf("ValidatePublicKey() error = %v, wantErr %v", err, tc.wantErr) |
| 89 | + return |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
0 commit comments