Skip to content

Commit 2b53e46

Browse files
authored
Authenticate with ssh-rsa by default (#1283)
1 parent e7a64dd commit 2b53e46

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/Renci.SshNet/PrivateKeyFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ private void Open(Stream privateKey, string passPhrase)
250250
case "RSA":
251251
var rsaKey = new RsaKey(decryptedData);
252252
_key = rsaKey;
253+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
253254
#pragma warning disable CA2000 // Dispose objects before losing scope
254255
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA512)));
255256
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA256)));
256257
#pragma warning restore CA2000 // Dispose objects before losing scope
257-
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
258258
break;
259259
case "DSA":
260260
_key = new DsaKey(decryptedData);
@@ -268,11 +268,11 @@ private void Open(Stream privateKey, string passPhrase)
268268
_key = ParseOpenSshV1Key(decryptedData, passPhrase);
269269
if (_key is RsaKey parsedRsaKey)
270270
{
271+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
271272
#pragma warning disable CA2000 // Dispose objects before losing scope
272273
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(parsedRsaKey, HashAlgorithmName.SHA512)));
273274
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(parsedRsaKey, HashAlgorithmName.SHA256)));
274275
#pragma warning restore CA2000 // Dispose objects before losing scope
275-
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
276276
}
277277
else
278278
{
@@ -337,11 +337,11 @@ private void Open(Stream privateKey, string passPhrase)
337337
var p = reader.ReadBigIntWithBits(); // q
338338
var decryptedRsaKey = new RsaKey(modulus, exponent, d, p, q, inverseQ);
339339
_key = decryptedRsaKey;
340+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
340341
#pragma warning disable CA2000 // Dispose objects before losing scope
341342
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(decryptedRsaKey, HashAlgorithmName.SHA512)));
342343
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(decryptedRsaKey, HashAlgorithmName.SHA256)));
343344
#pragma warning restore CA2000 // Dispose objects before losing scope
344-
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
345345
}
346346
else if (keyType == "dl-modp{sign{dsa-nist-sha1},dh{plain}}")
347347
{

test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
8888
using (var s = GetData("Key.RSA.txt"))
8989
{
9090
var privateKey = new PrivateKeyFile(s);
91-
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
91+
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.Single(x => x.Name == "rsa-sha2-512");
9292
}
9393
}
9494

test/Renci.SshNet.Tests/Classes/PrivateKeyFileTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,11 @@ private static void TestRsaKeyFile(PrivateKeyFile rsaPrivateKeyFile)
687687

688688
var algorithms = rsaPrivateKeyFile.HostKeyAlgorithms.ToList();
689689

690-
Assert.AreEqual("rsa-sha2-512", algorithms[0].Name);
691-
Assert.AreEqual("rsa-sha2-256", algorithms[1].Name);
692-
Assert.AreEqual("ssh-rsa", algorithms[2].Name);
690+
// ssh-rsa should be attempted first during authentication by default.
691+
// See https://github.com/sshnet/SSH.NET/issues/1233#issuecomment-1871196405
692+
Assert.AreEqual("ssh-rsa", algorithms[0].Name);
693+
Assert.AreEqual("rsa-sha2-512", algorithms[1].Name);
694+
Assert.AreEqual("rsa-sha2-256", algorithms[2].Name);
693695
}
694696
}
695697
}

0 commit comments

Comments
 (0)