Skip to content

Commit 7df89b1

Browse files
remove error type
We remove the error type as requested in review, and keep the existing string error. In the test now, we check using string contains instead.
1 parent 27623e0 commit 7df89b1

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

ssh/keys.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,15 +1271,6 @@ func (*PassphraseMissingError) Error() string {
12711271
return "ssh: this private key is passphrase protected"
12721272
}
12731273

1274-
type unsupportedCipherError struct {
1275-
BadCipher string
1276-
SupportedCiphers []string
1277-
}
1278-
1279-
func (e *unsupportedCipherError) Error() string {
1280-
return fmt.Sprintf("ssh: unknown cipher %q, only supports one of %q", e.BadCipher, strings.Join(e.SupportedCiphers, ","))
1281-
}
1282-
12831274
// ParseRawPrivateKey returns a private key from a PEM encoded private key. It supports
12841275
// RSA, DSA, ECDSA, and Ed25519 private keys in PKCS#1, PKCS#8, OpenSSL, and OpenSSH
12851276
// formats. If the private key is encrypted, it will return a PassphraseMissingError.
@@ -1438,10 +1429,7 @@ func passphraseProtectedOpenSSHKey(passphrase []byte) openSSHDecryptFunc {
14381429
cbc := cipher.NewCBCDecrypter(c, iv)
14391430
cbc.CryptBlocks(privKeyBlock, privKeyBlock)
14401431
default:
1441-
return nil, &unsupportedCipherError{
1442-
BadCipher: cipherName,
1443-
SupportedCiphers: []string{"aes256-ctr", "aes256-cbc"},
1444-
}
1432+
return nil, fmt.Errorf("ssh: unknown cipher %q, only supports %q or %q", cipherName, "aes256-ctr", "aes256-cbc")
14451433
}
14461434

14471435
return privKeyBlock, nil
@@ -1502,7 +1490,7 @@ type openSSHEncryptedPrivateKey struct {
15021490
NumKeys uint32
15031491
PubKey []byte
15041492
PrivKeyBlock []byte
1505-
Rest []byte `ssh:"rest"`
1493+
Rest []byte `ssh:"rest"`
15061494
}
15071495

15081496
type openSSHPrivateKey struct {

ssh/keys_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,18 @@ func TestParseEncryptedPrivateKeysWithPassphrase(t *testing.T) {
272272
}
273273

274274
func TestParseEncryptedPrivateKeysWithUnsupportedCiphers(t *testing.T) {
275-
for _, tt := range testdata.PEMEncryptedKeysForUnsupportedCiphers {
276-
t.Run(tt.Name, func(t *testing.T) {
277-
_, err := ParsePrivateKeyWithPassphrase(tt.PEMBytes, []byte(tt.EncryptionKey))
278-
var e *unsupportedCipherError
279-
if !errors.As(err, &e) {
280-
t.Errorf("got error %v, want UnsupportedCipherError", err)
281-
}
282-
283-
if e.BadCipher != tt.Cipher {
284-
t.Errorf("got badcipher %q, wanted %q", e.BadCipher, tt.Cipher)
285-
}
286-
})
287-
}
275+
for _, tt := range testdata.UnsupportedCipherData {
276+
t.Run(tt.Name, func(t *testing.T){
277+
_, err := ParsePrivateKeyWithPassphrase(tt.PEMBytes, []byte(tt.EncryptionKey))
278+
if err == nil {
279+
t.Fatalf("expected 'unknown cipher' error for %q, got nil", tt.Name)
280+
// If this cipher is now supported, remove it from testdata.UnsupportedCipherData
281+
}
282+
if !strings.Contains(err.Error(), "unknown cipher") {
283+
t.Errorf("wanted 'unknown cipher' error, got %v", err.Error())
284+
}
285+
})
286+
}
288287
}
289288

290289
func TestParseEncryptedPrivateKeysWithIncorrectPassphrase(t *testing.T) {

ssh/testdata/keys.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,12 @@ var SSHCertificates = map[string][]byte{
216216
`),
217217
}
218218

219-
type PEMEncryptedKey struct {
219+
var PEMEncryptedKeys = []struct {
220220
Name string
221221
EncryptionKey string
222222
IncludesPublicKey bool
223-
Cipher string
224223
PEMBytes []byte
225-
}
226-
227-
var PEMEncryptedKeys = []PEMEncryptedKey{
224+
}{
228225
0: {
229226
Name: "rsa-encrypted",
230227
EncryptionKey: "r54-G0pher_t3st$",
@@ -313,12 +310,14 @@ gbDGyT3bXMQtagvCwoW+/oMTKXiZP5jCJpEO8=
313310
},
314311
}
315312

316-
var PEMEncryptedKeysForUnsupportedCiphers = []PEMEncryptedKey{
313+
var UnsupportedCipherData = []struct {
314+
Name string
315+
EncryptionKey string
316+
PEMBytes []byte
317+
} {
317318
0: {
318319
Name: "ed25519-encrypted-chacha20-poly1305",
319320
EncryptionKey: "password",
320-
IncludesPublicKey: true,
321-
Cipher: "chacha20-poly1305@openssh.com",
322321
PEMBytes: []byte(`-----BEGIN OPENSSH PRIVATE KEY-----
323322
b3BlbnNzaC1rZXktdjEAAAAAHWNoYWNoYTIwLXBvbHkxMzA1QG9wZW5zc2guY29tAAAABm
324323
JjcnlwdAAAABgAAAAQdPyPIjXDRAVHskY0yp9SWwAAAGQAAAABAAAAMwAAAAtzc2gtZWQy
@@ -332,8 +331,6 @@ vYAJZExx2XLgJFEtHCVmJjYzwxx7yC7+s6u/XjrSlZS60RHunOPKyq+C+s48sejXvmX+t5
332331
1: {
333332
Name: "ed25519-encrypted-aes128-gcm",
334333
EncryptionKey: "password",
335-
IncludesPublicKey: true,
336-
Cipher: "aes128-gcm@openssh.com",
337334
PEMBytes: []byte(`-----BEGIN OPENSSH PRIVATE KEY-----
338335
b3BlbnNzaC1rZXktdjEAAAAAFmFlczEyOC1nY21Ab3BlbnNzaC5jb20AAAAGYmNyeXB0AA
339336
AAGAAAABBeMJIOqiyFwNCvDv6f8tQeAAAAZAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAA
@@ -347,8 +344,6 @@ EuVmM0FqS8lbT2ynYSe3va0Qyw13jEO5qbtCuyG+C5GejL7kX4Z64=
347344
2: {
348345
Name: "ed25519-encrypted-aes256-gcm",
349346
EncryptionKey: "password",
350-
IncludesPublicKey: true,
351-
Cipher: "aes256-gcm@openssh.com",
352347
PEMBytes: []byte(`-----BEGIN OPENSSH PRIVATE KEY-----
353348
b3BlbnNzaC1rZXktdjEAAAAAFmFlczI1Ni1nY21Ab3BlbnNzaC5jb20AAAAGYmNyeXB0AA
354349
AAGAAAABBR1p3vH2Wr/HPL+q20L2rjAAAAZAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAA
@@ -361,6 +356,7 @@ kv2ceuJMLT04TrKc2+RUkj4CQYnz7p8EkgZlUozx8wBSxKFGnkP7k=
361356
},
362357
}
363358

359+
364360
// SKData contains a list of PubKeys backed by U2F/FIDO2 Security Keys and their test data.
365361
var SKData = []struct {
366362
Name string

0 commit comments

Comments
 (0)