Skip to content

Commit 0ec3e99

Browse files
RKinseyFiloSottile
authored andcommitted
ssh: support aes256-cbc for passphrase-protected OpenSSH keys
The existing code for decrypting OpenSSH-format keys only allows aes256-ctr, the current ssh-keygen default. However, the default encryption scheme was aes256-cbc until relatively recently, and some of these keys are still in use. Support for aes256-cbc has been added. Fixes golang/go#37939 Change-Id: I3730347109c5dd18e4cbe61b48bbca9566ad61d2 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/224817 Reviewed-by: Filippo Valsorda <filippo@golang.org>
1 parent 891825f commit 0ec3e99

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

ssh/keys.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,15 +1246,23 @@ func passphraseProtectedOpenSSHKey(passphrase []byte) openSSHDecryptFunc {
12461246
}
12471247
key, iv := k[:32], k[32:]
12481248

1249-
if cipherName != "aes256-ctr" {
1250-
return nil, fmt.Errorf("ssh: unknown cipher %q, only supports %q", cipherName, "aes256-ctr")
1251-
}
12521249
c, err := aes.NewCipher(key)
12531250
if err != nil {
12541251
return nil, err
12551252
}
1256-
ctr := cipher.NewCTR(c, iv)
1257-
ctr.XORKeyStream(privKeyBlock, privKeyBlock)
1253+
switch cipherName {
1254+
case "aes256-ctr":
1255+
ctr := cipher.NewCTR(c, iv)
1256+
ctr.XORKeyStream(privKeyBlock, privKeyBlock)
1257+
case "aes256-cbc":
1258+
if len(privKeyBlock)%c.BlockSize() != 0 {
1259+
return nil, fmt.Errorf("ssh: invalid encrypted private key length, not a multiple of the block size")
1260+
}
1261+
cbc := cipher.NewCBCDecrypter(c, iv)
1262+
cbc.CryptBlocks(privKeyBlock, privKeyBlock)
1263+
default:
1264+
return nil, fmt.Errorf("ssh: unknown cipher %q, only supports %q or %q", cipherName, "aes256-ctr", "aes256-cbc")
1265+
}
12581266

12591267
return privKeyBlock, nil
12601268
}

ssh/testdata/keys.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,21 @@ ocEWuVhQ94/RjoAAAAEAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIIw1gSurPTDwZidA
269269
xdB6KEw1Ce7Bz8JaDIeagAGd3xtQTH3cuuleVxCZZnk9NspsPxigADKCls/RUiK7F+z3Qf
270270
Lvs9+PH8nIuhFMYZgo3liqZbVS5z4Fqhyzyq4=
271271
-----END OPENSSH PRIVATE KEY-----
272+
`),
273+
},
274+
275+
3: {
276+
Name: "ed25519-encrypted-cbc",
277+
EncryptionKey: "password",
278+
IncludesPublicKey: true,
279+
PEMBytes: []byte(`-----BEGIN OPENSSH PRIVATE KEY-----
280+
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jYmMAAAAGYmNyeXB0AAAAGAAAABDzGKF3uX
281+
G1gXALZKFd6Ir4AAAAEAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIDne4/teO42zTDdj
282+
NwxUMNpbfmp/dxgU4ZNkC3ydgcugAAAAoJ3J/oA7+iqVOz0CIUUk9ufdP1VP4jDf2um+0s
283+
Sgs7x6Gpyjq67Ps7wLRdSmxr/G5b+Z8dRGFYS/wUCQEe3whwuImvLyPwWjXLzkAyMzc01f
284+
ywBGSrHnvP82ppenc2HuTI+E05Xc02i6JVyI1ShiekQL5twoqtR6pEBZnD17UonIx7cRzZ
285+
gbDGyT3bXMQtagvCwoW+/oMTKXiZP5jCJpEO8=
286+
-----END OPENSSH PRIVATE KEY-----
272287
`),
273288
},
274289
}

0 commit comments

Comments
 (0)