2
0

HostKeyAlgorithmTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Renci.SshNet.Common;
  2. using Renci.SshNet.IntegrationTests.Common;
  3. using Renci.SshNet.Security;
  4. using Renci.SshNet.TestTools.OpenSSH;
  5. namespace Renci.SshNet.IntegrationTests
  6. {
  7. [TestClass]
  8. public class HostKeyAlgorithmTests : IntegrationTestBase
  9. {
  10. private IConnectionInfoFactory _connectionInfoFactory;
  11. private RemoteSshdConfig _remoteSshdConfig;
  12. [TestInitialize]
  13. public void SetUp()
  14. {
  15. _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
  16. _remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
  17. }
  18. [TestCleanup]
  19. public void TearDown()
  20. {
  21. _remoteSshdConfig?.Reset();
  22. }
  23. [TestMethod]
  24. public void SshDss()
  25. {
  26. DoTest(HostKeyAlgorithm.SshDss, HostKeyFile.Dsa);
  27. }
  28. [TestMethod]
  29. public void SshRsa()
  30. {
  31. DoTest(HostKeyAlgorithm.SshRsa, HostKeyFile.Rsa);
  32. }
  33. [TestMethod]
  34. public void SshRsaSha256()
  35. {
  36. DoTest(HostKeyAlgorithm.RsaSha2256, HostKeyFile.Rsa);
  37. }
  38. [TestMethod]
  39. public void SshRsaSha512()
  40. {
  41. DoTest(HostKeyAlgorithm.RsaSha2512, HostKeyFile.Rsa);
  42. }
  43. [TestMethod]
  44. public void SshEd25519()
  45. {
  46. DoTest(HostKeyAlgorithm.SshEd25519, HostKeyFile.Ed25519);
  47. }
  48. [TestMethod]
  49. public void Ecdsa256()
  50. {
  51. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp256, HostKeyFile.Ecdsa256);
  52. }
  53. [TestMethod]
  54. public void Ecdsa384()
  55. {
  56. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp384, HostKeyFile.Ecdsa384);
  57. }
  58. [TestMethod]
  59. public void Ecdsa521()
  60. {
  61. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp521, HostKeyFile.Ecdsa521);
  62. }
  63. [TestMethod]
  64. public void SshRsaCertificate()
  65. {
  66. DoTest(HostKeyAlgorithm.SshRsaCertV01OpenSSH, HostCertificateFile.RsaCertRsa);
  67. }
  68. [TestMethod]
  69. public void SshRsaSha256Certificate()
  70. {
  71. DoTest(HostKeyAlgorithm.RsaSha2256CertV01OpenSSH, HostCertificateFile.RsaCertRsa);
  72. }
  73. [TestMethod]
  74. public void Ecdsa256Certificate()
  75. {
  76. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp256CertV01OpenSSH, HostCertificateFile.Ecdsa256CertRsa);
  77. }
  78. [TestMethod]
  79. public void Ecdsa384Certificate()
  80. {
  81. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp384CertV01OpenSSH, HostCertificateFile.Ecdsa384CertEcdsa);
  82. }
  83. [TestMethod]
  84. public void Ecdsa521Certificate()
  85. {
  86. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp521CertV01OpenSSH, HostCertificateFile.Ecdsa521CertEd25519);
  87. }
  88. [TestMethod]
  89. public void Ed25519Certificate()
  90. {
  91. DoTest(HostKeyAlgorithm.SshEd25519CertV01OpenSSH, HostCertificateFile.Ed25519CertEcdsa);
  92. }
  93. private void DoTest(HostKeyAlgorithm hostKeyAlgorithm, HostKeyFile hostKeyFile, HostCertificateFile hostCertificateFile = null)
  94. {
  95. _remoteSshdConfig.ClearHostKeyAlgorithms()
  96. .AddHostKeyAlgorithm(hostKeyAlgorithm)
  97. .ClearHostKeyFiles()
  98. .AddHostKeyFile(hostKeyFile.FilePath)
  99. .WithHostKeyCertificate(hostCertificateFile?.FilePath)
  100. .Update()
  101. .Restart();
  102. HostKeyEventArgs hostKeyEventsArgs = null;
  103. using (var client = new SshClient(_connectionInfoFactory.Create()))
  104. {
  105. client.HostKeyReceived += (sender, e) => hostKeyEventsArgs = e;
  106. client.Connect();
  107. client.Disconnect();
  108. }
  109. Assert.IsNotNull(hostKeyEventsArgs);
  110. Assert.AreEqual(hostKeyAlgorithm.Name, hostKeyEventsArgs.HostKeyName);
  111. Assert.AreEqual(hostKeyFile.KeyLength, hostKeyEventsArgs.KeyLength);
  112. CollectionAssert.AreEqual(hostKeyFile.FingerPrint, hostKeyEventsArgs.FingerPrint);
  113. if (hostCertificateFile is not null)
  114. {
  115. Assert.IsNotNull(hostKeyEventsArgs.Certificate);
  116. Assert.AreEqual(Certificate.CertificateType.Host, hostKeyEventsArgs.Certificate.Type);
  117. Assert.AreEqual(hostCertificateFile.CAFingerPrint, hostKeyEventsArgs.Certificate.CertificateAuthorityKeyFingerPrint);
  118. }
  119. else
  120. {
  121. Assert.IsNull(hostKeyEventsArgs.Certificate);
  122. }
  123. }
  124. private void DoTest(HostKeyAlgorithm hostKeyAlgorithm, HostCertificateFile hostCertificateFile)
  125. {
  126. DoTest(hostKeyAlgorithm, hostCertificateFile.HostKeyFile, hostCertificateFile);
  127. }
  128. }
  129. }