-
-
Notifications
You must be signed in to change notification settings - Fork 955
[Private Key] Support more ciphers for OpenSSH private key decryption #1487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -386,9 +386,9 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy | |
{ "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "[email protected]", new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(256, (key, iv) => new AesGcmCipher(key, iv), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(512, (key, iv) => new ChaCha20Poly1305Cipher(key), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv, aadLength: 4), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(256, (key, iv) => new AesGcmCipher(key, iv, aadLength: 4), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(512, (key, iv) => new ChaCha20Poly1305Cipher(key, aadLength: 4), isAead: true) }, | ||
{ "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ namespace Renci.SshNet | |
/// </list> | ||
/// </para> | ||
/// <para> | ||
/// The following encryption algorithms are supported: | ||
/// The following encryption algorithms are supported for OpenSSL PEM and ssh.com format: | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description>DES-EDE3-CBC</description> | ||
|
@@ -60,6 +60,39 @@ namespace Renci.SshNet | |
/// <description>AES-256-CBC</description> | ||
/// </item> | ||
/// </list> | ||
/// The following encryption algorithms are supported for OpenSSH format: | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description>3des-cbc</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>aes128-cbc</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>aes192-cbc</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>aes256-cbc</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>aes128-ctr</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>aes192-ctr</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>aes256-ctr</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>[email protected]</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>[email protected]</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>[email protected]</description> | ||
/// </item> | ||
/// </list> | ||
/// </para> | ||
/// </remarks> | ||
public partial class PrivateKeyFile : IPrivateKeySource, IDisposable | ||
|
@@ -450,7 +483,17 @@ private static byte[] DecryptKey(CipherInfo cipherInfo, byte[] cipherData, strin | |
|
||
var cipher = cipherInfo.Cipher(cipherKey.ToArray(), binarySalt); | ||
|
||
return cipher.Decrypt(cipherData); | ||
try | ||
{ | ||
return cipher.Decrypt(cipherData); | ||
} | ||
finally | ||
{ | ||
if (cipher is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -474,7 +517,7 @@ private static Key ParseOpenSshV1Key(byte[] keyFileData, string passPhrase) | |
throw new SshException("This openssh key does not contain the 'openssh-key-v1' format magic header"); | ||
} | ||
|
||
// cipher will be "aes256-cbc" if using a passphrase, "none" otherwise | ||
// cipher will be "aes256-cbc" or other cipher if using a passphrase, "none" otherwise | ||
var cipherName = keyReader.ReadString(Encoding.UTF8); | ||
|
||
// key derivation function (kdf): bcrypt or nothing | ||
|
@@ -503,7 +546,7 @@ private static Key ParseOpenSshV1Key(byte[] keyFileData, string passPhrase) | |
|
||
// possibly encrypted private key | ||
var privateKeyLength = (int)keyReader.ReadUInt32(); | ||
var privateKeyBytes = keyReader.ReadBytes(privateKeyLength); | ||
byte[] privateKeyBytes; | ||
|
||
// decrypt private key if necessary | ||
if (cipherName != "none") | ||
|
@@ -518,38 +561,76 @@ private static Key ParseOpenSshV1Key(byte[] keyFileData, string passPhrase) | |
throw new SshException("kdf " + kdfName + " is not supported for openssh key file"); | ||
} | ||
|
||
// inspired by the SSHj library (https://github.com/hierynomus/sshj) | ||
// apply the kdf to derive a key and iv from the passphrase | ||
var passPhraseBytes = Encoding.UTF8.GetBytes(passPhrase); | ||
var keyiv = new byte[48]; | ||
new BCrypt().Pbkdf(passPhraseBytes, salt, rounds, keyiv); | ||
var key = new byte[32]; | ||
Array.Copy(keyiv, 0, key, 0, 32); | ||
var iv = new byte[16]; | ||
Array.Copy(keyiv, 32, iv, 0, 16); | ||
|
||
AesCipher cipher; | ||
var ivLength = 16; | ||
CipherInfo cipherInfo; | ||
switch (cipherName) | ||
{ | ||
case "3des-cbc": | ||
ivLength = 8; | ||
cipherInfo = new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), padding: null)); | ||
break; | ||
case "aes128-cbc": | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes192-cbc": | ||
cipherInfo = new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes256-cbc": | ||
cipher = new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false); | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes128-ctr": | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)); | ||
break; | ||
case "aes192-ctr": | ||
cipherInfo = new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)); | ||
break; | ||
case "aes256-ctr": | ||
cipher = new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false); | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)); | ||
break; | ||
case "[email protected]": | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv, aadLength: 0), isAead: true); | ||
break; | ||
case "[email protected]": | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesGcmCipher(key, iv, aadLength: 0), isAead: true); | ||
break; | ||
case "[email protected]": | ||
ivLength = 12; | ||
cipherInfo = new CipherInfo(256, (key, iv) => new ChaCha20Poly1305Cipher(key, aadLength: 0), isAead: true); | ||
break; | ||
default: | ||
throw new SshException("Cipher '" + cipherName + "' is not supported for an OpenSSH key."); | ||
} | ||
|
||
var keyLength = cipherInfo.KeySize / 8; | ||
|
||
// inspired by the SSHj library (https://github.com/hierynomus/sshj) | ||
// apply the kdf to derive a key and iv from the passphrase | ||
var passPhraseBytes = Encoding.UTF8.GetBytes(passPhrase); | ||
var keyiv = new byte[keyLength + ivLength]; | ||
new BCrypt().Pbkdf(passPhraseBytes, salt, rounds, keyiv); | ||
|
||
var key = keyiv.Take(keyLength); | ||
var iv = keyiv.Take(keyLength, ivLength); | ||
|
||
var cipher = cipherInfo.Cipher(key, iv); | ||
var cipherData = keyReader.ReadBytes(privateKeyLength + cipher.TagSize); | ||
|
||
try | ||
{ | ||
privateKeyBytes = cipher.Decrypt(privateKeyBytes); | ||
privateKeyBytes = cipher.Decrypt(cipherData, 0, privateKeyLength); | ||
} | ||
finally | ||
{ | ||
cipher.Dispose(); | ||
if (cipher is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
privateKeyBytes = keyReader.ReadBytes(privateKeyLength); | ||
} | ||
|
||
// validate private key length | ||
privateKeyLength = privateKeyBytes.Length; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.