Skip to content

Commit 43329ee

Browse files
authored
Support SHA256 fingerprints for host key validation (#1098)
* Add tests for HostKeyEventArgs * Add SHA256 fingerprint support
1 parent dcc596a commit 43329ee

File tree

3 files changed

+81
-28
lines changed

3 files changed

+81
-28
lines changed

README.md

+2-19
Original file line numberDiff line numberDiff line change
@@ -143,30 +143,13 @@ using (var client = new SftpClient(connectionInfo))
143143
Establish a SSH connection using user name and password, and reject the connection if the fingerprint of the server does not match the expected fingerprint:
144144

145145
```cs
146-
byte[] expectedFingerPrint = new byte[] {
147-
0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
148-
0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
149-
};
146+
string expectedFingerPrint = "LKOy5LvmtEe17S4lyxVXqvs7uPMy+yF79MQpHeCs/Qo";
150147

151148
using (var client = new SshClient("sftp.foo.com", "guest", "pwd"))
152149
{
153150
client.HostKeyReceived += (sender, e) =>
154151
{
155-
if (expectedFingerPrint.Length == e.FingerPrint.Length)
156-
{
157-
for (var i = 0; i < expectedFingerPrint.Length; i++)
158-
{
159-
if (expectedFingerPrint[i] != e.FingerPrint[i])
160-
{
161-
e.CanTrust = false;
162-
break;
163-
}
164-
}
165-
}
166-
else
167-
{
168-
e.CanTrust = false;
169-
}
152+
e.CanTrust = expectedFingerPrint.Equals(e.FingerPrintSHA256);
170153
};
171154
client.Connect();
172155
}

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

+62-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Renci.SshNet.Common;
33
using Renci.SshNet.Security;
44
using Renci.SshNet.Tests.Common;
5+
using System.Linq;
6+
using System.Reflection;
57

68
namespace Renci.SshNet.Tests.Classes.Common
79
{
@@ -10,7 +12,6 @@ namespace Renci.SshNet.Tests.Classes.Common
1012
///to contain all HostKeyEventArgsTest Unit Tests
1113
///</summary>
1214
[TestClass]
13-
[Ignore] // placeholder for actual test
1415
public class HostKeyEventArgsTest : TestBase
1516
{
1617
/// <summary>
@@ -19,9 +20,52 @@ public class HostKeyEventArgsTest : TestBase
1920
[TestMethod]
2021
public void HostKeyEventArgsConstructorTest()
2122
{
22-
KeyHostAlgorithm host = null; // TODO: Initialize to an appropriate value
23-
HostKeyEventArgs target = new HostKeyEventArgs(host);
24-
Assert.Inconclusive("TODO: Implement code to verify target");
23+
HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
24+
Assert.IsTrue(target.CanTrust);
25+
Assert.IsTrue(new byte[] {
26+
0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, 0x00, 0x00, 0x00, 0x01, 0x23,
27+
0x00, 0x00, 0x01, 0x01, 0x00, 0xb9, 0x3b, 0x57, 0x9f, 0xe0, 0x5a, 0xb5, 0x7d, 0x68, 0x26, 0xeb,
28+
0xe1, 0xa9, 0xf2, 0x59, 0xc3, 0x98, 0xdc, 0xfe, 0x97, 0x08, 0xc4, 0x95, 0x0f, 0x9a, 0xea, 0x05,
29+
0x08, 0x7d, 0xfe, 0x6d, 0x77, 0xca, 0x04, 0x9f, 0xfd, 0xe2, 0x2c, 0x4d, 0x11, 0x3c, 0xd9, 0x05,
30+
0xab, 0x32, 0xbd, 0x3f, 0xe8, 0xcd, 0xba, 0x00, 0x6c, 0x21, 0xb7, 0xa9, 0xc2, 0x4e, 0x63, 0x17,
31+
0xf6, 0x04, 0x47, 0x93, 0x00, 0x85, 0xde, 0xd6, 0x32, 0xc0, 0xa1, 0x37, 0x75, 0x18, 0xa0, 0xb0,
32+
0x32, 0xf6, 0x4e, 0xca, 0x39, 0xec, 0x3c, 0xdf, 0x79, 0xfe, 0x50, 0xa1, 0xc1, 0xf7, 0x67, 0x05,
33+
0xb3, 0x33, 0xa5, 0x96, 0x13, 0x19, 0xfa, 0x14, 0xca, 0x55, 0xe6, 0x7b, 0xf9, 0xb3, 0x8e, 0x32,
34+
0xee, 0xfc, 0x9d, 0x2a, 0x5e, 0x04, 0x79, 0x97, 0x29, 0x3d, 0x1c, 0x54, 0xfe, 0xc7, 0x96, 0x04,
35+
0xb5, 0x19, 0x7c, 0x55, 0x21, 0xe2, 0x0e, 0x42, 0xca, 0x4d, 0x9d, 0xfb, 0x77, 0x08, 0x6c, 0xaa,
36+
0x07, 0x2c, 0xf8, 0xf9, 0x1f, 0xbd, 0x83, 0x14, 0x2b, 0xe0, 0xbc, 0x7a, 0xf9, 0xdf, 0x13, 0x4b,
37+
0x60, 0x5a, 0x02, 0x99, 0x93, 0x41, 0x1a, 0xb6, 0x5f, 0x3b, 0x9c, 0xb5, 0xb2, 0x55, 0x70, 0x78,
38+
0x2f, 0x38, 0x52, 0x0e, 0xd1, 0x8a, 0x2c, 0x23, 0xc0, 0x3a, 0x0a, 0xd7, 0xed, 0xf6, 0x1f, 0xa6,
39+
0x50, 0xf0, 0x27, 0x65, 0x8a, 0xd4, 0xde, 0xa7, 0x1b, 0x41, 0x67, 0xc5, 0x6d, 0x47, 0x84, 0x37,
40+
0x92, 0x2b, 0xb7, 0xb6, 0x4d, 0xb0, 0x1a, 0xda, 0xf6, 0x50, 0x82, 0xf1, 0x57, 0x31, 0x69, 0xce,
41+
0xe0, 0xef, 0xcd, 0x64, 0xaa, 0x78, 0x08, 0xea, 0x4e, 0x45, 0xec, 0xa5, 0x89, 0x68, 0x5d, 0xb4,
42+
0xa0, 0x23, 0xaf, 0xff, 0x9c, 0x0f, 0x8c, 0x83, 0x7c, 0xf8, 0xe1, 0x8e, 0x32, 0x8e, 0x61, 0xfc,
43+
0x5b, 0xbd, 0xd4, 0x46, 0xe1
44+
}.SequenceEqual(target.HostKey));
45+
Assert.AreEqual("ssh-rsa", target.HostKeyName);
46+
Assert.AreEqual(2048, target.KeyLength);
47+
}
48+
49+
/// <summary>
50+
///A test for MD5 calculation in HostKeyEventArgs Constructor
51+
///</summary>
52+
[TestMethod]
53+
public void HostKeyEventArgsConstructorTest_VerifyMD5()
54+
{
55+
HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
56+
Assert.IsTrue(new byte[] {
57+
0x92, 0xea, 0x54, 0xa1, 0x01, 0xf9, 0x95, 0x9c, 0x71, 0xd9, 0xbb, 0x51, 0xb2, 0x55, 0xf8, 0xd9
58+
}.SequenceEqual(target.FingerPrint));
59+
}
60+
61+
/// <summary>
62+
///A test for SHA256 calculation in HostKeyEventArgs Constructor
63+
///</summary>
64+
[TestMethod]
65+
public void HostKeyEventArgsConstructorTest_VerifySHA256()
66+
{
67+
HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
68+
Assert.AreEqual("93LkmoWksp9ytNVZIPXi9KJU1uvlC9clZ/CkUHf6uEE", target.FingerPrintSHA256);
2569
}
2670

2771
/// <summary>
@@ -30,14 +74,24 @@ public void HostKeyEventArgsConstructorTest()
3074
[TestMethod]
3175
public void CanTrustTest()
3276
{
33-
KeyHostAlgorithm host = null; // TODO: Initialize to an appropriate value
34-
HostKeyEventArgs target = new HostKeyEventArgs(host); // TODO: Initialize to an appropriate value
35-
bool expected = false; // TODO: Initialize to an appropriate value
77+
HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
78+
bool expected = false;
3679
bool actual;
3780
target.CanTrust = expected;
3881
actual = target.CanTrust;
3982
Assert.AreEqual(expected, actual);
40-
Assert.Inconclusive("Verify the correctness of this test method.");
4183
}
84+
85+
private static KeyHostAlgorithm GetKeyHostAlgorithm()
86+
{
87+
var executingAssembly = Assembly.GetExecutingAssembly();
88+
89+
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
90+
{
91+
var privateKey = new PrivateKeyFile(s);
92+
return (KeyHostAlgorithm)privateKey.HostKey;
93+
}
94+
}
95+
4296
}
4397
}

src/Renci.SshNet/Common/HostKeyEventArgs.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,21 @@ public class HostKeyEventArgs : EventArgs
2828
public string HostKeyName{ get; private set; }
2929

3030
/// <summary>
31-
/// Gets the finger print.
31+
/// Gets the MD5 fingerprint.
3232
/// </summary>
33+
/// <value>
34+
/// MD5 fingerprint as byte array.
35+
/// </value>
3336
public byte[] FingerPrint { get; private set; }
3437

38+
/// <summary>
39+
/// Gets the SHA256 fingerprint.
40+
/// </summary>
41+
/// <value>
42+
/// Base64 encoded SHA256 fingerprint with padding (equals sign) removed.
43+
/// </value>
44+
public string FingerPrintSHA256 { get; private set; }
45+
3546
/// <summary>
3647
/// Gets the length of the key in bits.
3748
/// </summary>
@@ -55,6 +66,11 @@ public HostKeyEventArgs(KeyHostAlgorithm host)
5566
{
5667
FingerPrint = md5.ComputeHash(host.Data);
5768
}
69+
70+
using (var sha256 = CryptoAbstraction.CreateSHA256())
71+
{
72+
FingerPrintSHA256 = Convert.ToBase64String(sha256.ComputeHash(host.Data)).Replace("=", "");
73+
}
5874
}
5975
}
6076
}

0 commit comments

Comments
 (0)