PrivateKeyAuthenticationTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Renci.SshNet.IntegrationTests.Common;
  2. using Renci.SshNet.TestTools.OpenSSH;
  3. namespace Renci.SshNet.IntegrationTests
  4. {
  5. [TestClass]
  6. public class PrivateKeyAuthenticationTests : TestBase
  7. {
  8. private IConnectionInfoFactory _connectionInfoFactory;
  9. private RemoteSshdConfig _remoteSshdConfig;
  10. [TestInitialize]
  11. public void SetUp()
  12. {
  13. _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
  14. _remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
  15. }
  16. [TestCleanup]
  17. public void TearDown()
  18. {
  19. _remoteSshdConfig?.Reset();
  20. }
  21. [TestMethod]
  22. public void SshDss()
  23. {
  24. DoTest(PublicKeyAlgorithm.SshDss, "Data.Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt", "12345");
  25. }
  26. [TestMethod]
  27. public void SshRsa()
  28. {
  29. DoTest(PublicKeyAlgorithm.SshRsa, "Data.Key.RSA.txt");
  30. }
  31. [TestMethod]
  32. public void SshRsaSha256()
  33. {
  34. DoTest(PublicKeyAlgorithm.RsaSha2256, "Data.Key.RSA.txt");
  35. }
  36. [TestMethod]
  37. public void SshRsaSha512()
  38. {
  39. DoTest(PublicKeyAlgorithm.RsaSha2512, "Data.Key.RSA.txt");
  40. }
  41. [TestMethod]
  42. public void Ecdsa256()
  43. {
  44. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp256, "Data.Key.ECDSA.Encrypted.txt", "12345");
  45. }
  46. [TestMethod]
  47. public void Ecdsa384()
  48. {
  49. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp384, "Data.Key.OPENSSH.ECDSA384.Encrypted.txt", "12345");
  50. }
  51. [TestMethod]
  52. public void Ecdsa521()
  53. {
  54. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp521, "Data.Key.OPENSSH.ECDSA521.Encrypted.txt", "12345");
  55. }
  56. [TestMethod]
  57. public void Ed25519()
  58. {
  59. DoTest(PublicKeyAlgorithm.SshEd25519, "Data.Key.OPENSSH.ED25519.Encrypted.txt", "12345");
  60. }
  61. // The private keys used for the certificate tests below should stay out of authorized_keys for a proper test.
  62. [TestMethod]
  63. public void SshRsaCertificate()
  64. {
  65. // ssh-keygen -L -f Key.OPENSSH.RSA.Encrypted.Aes.192.CTR-cert.pub
  66. // Type: ssh-rsa-cert-v01@openssh.com user certificate
  67. // Public key: RSA-CERT SHA256:MMIzDVhQHqU9SAZ8p3x2wo6JpXixCWO/7qf6h0l8DJA
  68. // Signing CA: RSA SHA256:NqLEgdYti0XjUkYjGyQv2Ddy1O5v2NZDZFRtlfESLIA (using rsa-sha2-512)
  69. // And we will authenticate (sign) with ssh-rsa (SHA-1)
  70. DoTest(PublicKeyAlgorithm.SshRsaCertV01OpenSSH, "Data.Key.OPENSSH.RSA.Encrypted.Aes.192.CTR.txt", "12345", "Data.Key.OPENSSH.RSA.Encrypted.Aes.192.CTR-cert.pub");
  71. }
  72. [TestMethod]
  73. public void SshRsaSha256Certificate()
  74. {
  75. // As above, but we will authenticate (sign) with rsa-sha2-256
  76. DoTest(PublicKeyAlgorithm.SshRsaCertV01OpenSSH, "Data.Key.OPENSSH.RSA.Encrypted.Aes.192.CTR.txt", "12345", "Data.Key.OPENSSH.RSA.Encrypted.Aes.192.CTR-cert.pub");
  77. }
  78. [TestMethod]
  79. public void Ecdsa256Certificate()
  80. {
  81. // ssh-keygen -L -f Key.OPENSSH.ECDSA.Encrypted.Aes.128.CTR-cert.pub
  82. // Type: ecdsa-sha2-nistp256-cert-v01@openssh.com user certificate
  83. // Public key: ECDSA-CERT SHA256:ufAaMwjTmKrjvt4CQiLPal1/HrmB2D7oL+H2lh/Om8c
  84. // Signing CA: RSA SHA256:NqLEgdYti0XjUkYjGyQv2Ddy1O5v2NZDZFRtlfESLIA (using rsa-sha2-512)
  85. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp256CertV01OpenSSH, "Data.Key.OPENSSH.ECDSA.Encrypted.Aes.128.CTR.txt", "12345", "Data.Key.OPENSSH.ECDSA.Encrypted.Aes.128.CTR-cert.pub");
  86. }
  87. [TestMethod]
  88. public void Ecdsa384Certificate()
  89. {
  90. // ssh-keygen -L -f Key.OPENSSH.ECDSA384.Encrypted.Aes.256.GCM-cert.pub
  91. // Type: ecdsa-sha2-nistp384-cert-v01@openssh.com user certificate
  92. // Public key: ECDSA-CERT SHA256:wy4X47uddqD8nggcsGHG7Rcs0qcnh4r6NrdBGdh/8us
  93. // Signing CA: RSA SHA256:NqLEgdYti0XjUkYjGyQv2Ddy1O5v2NZDZFRtlfESLIA (using rsa-sha2-256)
  94. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp384CertV01OpenSSH, "Data.Key.OPENSSH.ECDSA384.Encrypted.Aes.256.GCM.txt", "12345", "Data.Key.OPENSSH.ECDSA384.Encrypted.Aes.256.GCM-cert.pub");
  95. }
  96. [TestMethod]
  97. public void Ecdsa521Certificate()
  98. {
  99. // ssh-keygen -L -f Key.OPENSSH.ECDSA521.Encrypted.Aes.192.CBC-cert.pub
  100. // Type: ecdsa-sha2-nistp521-cert-v01@openssh.com user certificate
  101. // Public key: ECDSA-CERT SHA256:U3wBX0sSPYxso31gi1QPz7O+1eMOTb0LoOSOjWRwyYE
  102. // Signing CA: ECDSA SHA256:r/t6I+bZQzN5BhSuntFSHDHlrnNHVM2lAo6gbvynG/4 (using ecdsa-sha2-nistp256)
  103. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp521CertV01OpenSSH, "Data.Key.OPENSSH.ECDSA521.Encrypted.Aes.192.CBC.txt", "12345", "Data.Key.OPENSSH.ECDSA521.Encrypted.Aes.192.CBC-cert.pub");
  104. }
  105. [TestMethod]
  106. public void Ed25519Certificate()
  107. {
  108. // ssh-keygen -L -f Key.OPENSSH.ED25519.Encrypted.ChaCha20.Poly1305-cert.pub
  109. // Type: ssh-ed25519-cert-v01@openssh.com user certificate
  110. // Public key: ED25519-CERT SHA256:gwO3eBcuPqChqg9B/kHsQo1/bYTAjaEZCanA7hqSuEg
  111. // Signing CA: ECDSA SHA256:r/t6I+bZQzN5BhSuntFSHDHlrnNHVM2lAo6gbvynG/4 (using ecdsa-sha2-nistp256)
  112. DoTest(PublicKeyAlgorithm.SshEd25519CertV01OpenSSH, "Data.Key.OPENSSH.ED25519.Encrypted.ChaCha20.Poly1305.txt", "12345", "Data.Key.OPENSSH.ED25519.Encrypted.ChaCha20.Poly1305-cert.pub");
  113. }
  114. private void DoTest(PublicKeyAlgorithm publicKeyAlgorithm, string keyResource, string passPhrase = null, string certificateResource = null)
  115. {
  116. _remoteSshdConfig.ClearPublicKeyAcceptedAlgorithms()
  117. .AddPublicKeyAcceptedAlgorithm(publicKeyAlgorithm)
  118. .Update()
  119. .Restart();
  120. var connectionInfo = _connectionInfoFactory.Create(CreatePrivateKeyAuthenticationMethod(keyResource, passPhrase, certificateResource));
  121. using (var client = new SshClient(connectionInfo))
  122. {
  123. client.Connect();
  124. }
  125. }
  126. private static PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource, string passPhrase, string certificateResource)
  127. {
  128. PrivateKeyFile privateKey;
  129. using (var keyStream = GetData(keyResource))
  130. {
  131. if (certificateResource is not null)
  132. {
  133. using (var certificateStream = GetData(certificateResource))
  134. {
  135. privateKey = new PrivateKeyFile(keyStream, passPhrase, certificateStream);
  136. }
  137. }
  138. else
  139. {
  140. privateKey = new PrivateKeyFile(keyStream, passPhrase);
  141. }
  142. }
  143. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKey);
  144. }
  145. }
  146. }