Skip to content

Commit

Permalink
Improvements after sshnet#1177 (sshnet#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechNagorski authored Sep 23, 2023
1 parent 8732d3d commit 18e6673
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public RsaCipherBenchmarks()

using (var s = typeof(RsaCipherBenchmarks).Assembly.GetManifestResourceStream("Renci.SshNet.Benchmarks.Data.Key.RSA.txt"))
{
_privateKey = (RsaKey)((KeyHostAlgorithm) new PrivateKeyFile(s).HostKey).Key;

_privateKey = (RsaKey)new PrivateKeyFile(s).Key;

// The implementations of RsaCipher.Encrypt/Decrypt differ based on whether the supplied RsaKey has private key information
// or only public. So we extract out the public key information to a separate variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ED25519DigitalSignatureBenchmarks()

using (var s = typeof(ED25519DigitalSignatureBenchmarks).Assembly.GetManifestResourceStream("Renci.SshNet.Benchmarks.Data.Key.OPENSSH.ED25519.txt"))
{
_key = (ED25519Key) ((KeyHostAlgorithm) new PrivateKeyFile(s).HostKey).Key;
_key = (ED25519Key) new PrivateKeyFile(s).Key;
}
_signature = new ED25519DigitalSignature(_key).Sign(_data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -140,7 +141,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
{
var privateKey = new PrivateKeyFile(s);
return (KeyHostAlgorithm) privateKey.HostKey;
return (KeyHostAlgorithm) privateKey.HostKeyAlgorithms.First();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
{
var privateKey = new PrivateKeyFile(s);
return (KeyHostAlgorithm)privateKey.HostKey;
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;

Expand Down Expand Up @@ -113,7 +114,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
{
var privateKey = new PrivateKeyFile(s);
return (KeyHostAlgorithm)privateKey.HostKey;
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/Renci.SshNet.Tests/Classes/PrivateKeyFileTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Security;
using Renci.SshNet.Tests.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -412,7 +410,7 @@ public void ConstructorWithStreamAndPassphrase()
using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
{
var privateKeyFile = new PrivateKeyFile(stream, "12345");
Assert.IsNotNull(privateKeyFile.HostKey);
TestRsaKeyFile(privateKeyFile);
}
}

Expand All @@ -430,7 +428,7 @@ public void ConstructorWithFileNameAndPassphrase()
using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
Assert.IsNotNull(privateKeyFile.HostKey);
TestRsaKeyFile(privateKeyFile);

fs.Close();
}
Expand Down Expand Up @@ -498,7 +496,7 @@ public void ConstructorWithFileName()
}

var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
Assert.IsNotNull(privateKeyFile.HostKey);
TestRsaKeyFile(privateKeyFile);
}

/// <summary>
Expand All @@ -510,7 +508,7 @@ public void ConstructorWithStream()
using (var stream = GetData("Key.RSA.txt"))
{
var privateKeyFile = new PrivateKeyFile(stream);
Assert.IsNotNull(privateKeyFile.HostKey);
TestRsaKeyFile(privateKeyFile);
}
}

Expand All @@ -526,7 +524,7 @@ public void ConstructorWithFileNameShouldBeAbleToReadFileThatIsSharedForReadAcce
using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var privateKeyFile = new PrivateKeyFile(_temporaryFile);
Assert.IsNotNull(privateKeyFile.HostKey);
TestRsaKeyFile(privateKeyFile);

fs.Close();
}
Expand All @@ -544,7 +542,7 @@ public void ConstructorWithFileNameAndPassPhraseShouldBeAbleToReadFileThatIsShar
using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
Assert.IsNotNull(privateKeyFile.HostKey);
TestRsaKeyFile(privateKeyFile);

fs.Close();
}
Expand Down Expand Up @@ -684,15 +682,14 @@ private string GetTempFileName()

private static void TestRsaKeyFile(PrivateKeyFile rsaPrivateKeyFile)
{
Assert.AreEqual(3, rsaPrivateKeyFile.HostAlgorithms.Count);
Assert.IsNotNull(rsaPrivateKeyFile.HostKeyAlgorithms);
Assert.AreEqual(3, rsaPrivateKeyFile.HostKeyAlgorithms.Count);

List<KeyHostAlgorithm> algorithms = rsaPrivateKeyFile.HostAlgorithms.Cast<KeyHostAlgorithm>().ToList();
var algorithms = rsaPrivateKeyFile.HostKeyAlgorithms.ToList();

Assert.AreEqual("rsa-sha2-512", algorithms[0].Name);
Assert.AreEqual("rsa-sha2-256", algorithms[1].Name);
Assert.AreEqual("ssh-rsa", algorithms[2].Name);

Assert.AreSame(algorithms[0], rsaPrivateKeyFile.HostKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static RsaKey GetRsaKey()
{
using (var stream = GetData("Key.RSA.txt"))
{
return (RsaKey) ((KeyHostAlgorithm) new PrivateKeyFile(stream).HostKey).Key;
return (RsaKey) new PrivateKeyFile(stream).Key;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet.Tests/Classes/Security/KeyAlgorithmTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -176,7 +176,7 @@ private static RsaKey GetRsaKey()
{
using (var stream = GetData("Key.RSA.txt"))
{
return (RsaKey) ((KeyHostAlgorithm) new PrivateKeyFile(stream).HostKey).Key;
return (RsaKey) new PrivateKeyFile(stream).Key;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;

Expand Down Expand Up @@ -122,7 +123,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
{
var privateKey = new PrivateKeyFile(s);
return (KeyHostAlgorithm)privateKey.HostKey;
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
}
}
}
Expand Down
21 changes: 0 additions & 21 deletions src/Renci.SshNet/IHostAlgorithmsProvider.cs

This file was deleted.

21 changes: 8 additions & 13 deletions src/Renci.SshNet/IPrivateKeySource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;
using System.Collections.Generic;

using Renci.SshNet.Security;

Expand All @@ -8,19 +7,15 @@ namespace Renci.SshNet
/// <summary>
/// Represents private key source interface.
/// </summary>
/// <remarks>
/// This interface has been replaced by <see cref="IHostAlgorithmsProvider"/>
/// and is obsolete.
/// </remarks>
[Obsolete($"Use {nameof(IHostAlgorithmsProvider)} instead. " +
$"{nameof(IPrivateKeySource)} may be removed in a future release. " +
$"See https://github.com/sshnet/SSH.NET/issues/1174 for details.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IPrivateKeySource : IHostAlgorithmsProvider
public interface IPrivateKeySource
{
/// <summary>
/// Gets the host key.
/// Gets the host keys algorithms.
/// </summary>
HostAlgorithm HostKey { get; }
/// <remarks>
/// In situations where there is a preferred order of usage of the host algorithms,
/// the collection should be ordered from most preferred to least.
/// </remarks>
IReadOnlyCollection<HostAlgorithm> HostKeyAlgorithms { get; }
}
}
4 changes: 2 additions & 2 deletions src/Renci.SshNet/NetConfClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public NetConfClient(string host, string username, string password)
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
[SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
public NetConfClient(string host, int port, string username, params IHostAlgorithmsProvider[] keyFiles)
public NetConfClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)
: this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true)
{
}
Expand All @@ -120,7 +120,7 @@ public NetConfClient(string host, int port, string username, params IHostAlgorit
/// <param name="keyFiles">Authentication private key file(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
public NetConfClient(string host, string username, params IHostAlgorithmsProvider[] keyFiles)
public NetConfClient(string host, string username, params IPrivateKeySource[] keyFiles)
: this(host, ConnectionInfo.DefaultPort, username, keyFiles)
{
}
Expand Down
8 changes: 4 additions & 4 deletions src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ public override string Name
/// <summary>
/// Gets the key files used for authentication.
/// </summary>
public ICollection<IHostAlgorithmsProvider> KeyFiles { get; private set; }
public ICollection<IPrivateKeySource> KeyFiles { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="PrivateKeyAuthenticationMethod"/> class.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="keyFiles">The key files.</param>
/// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <c>null</c>.</exception>
public PrivateKeyAuthenticationMethod(string username, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyAuthenticationMethod(string username, params IPrivateKeySource[] keyFiles)
: base(username)
{
if (keyFiles is null)
{
throw new ArgumentNullException(nameof(keyFiles));
}

KeyFiles = new Collection<IHostAlgorithmsProvider>(keyFiles);
KeyFiles = new Collection<IPrivateKeySource>(keyFiles);
}

/// <summary>
Expand All @@ -65,7 +65,7 @@ public override AuthenticationResult Authenticate(Session session)

session.RegisterMessage("SSH_MSG_USERAUTH_PK_OK");

var hostAlgorithms = KeyFiles.SelectMany(x => x.HostAlgorithms).ToList();
var hostAlgorithms = KeyFiles.SelectMany(x => x.HostKeyAlgorithms).ToList();

try
{
Expand Down
18 changes: 9 additions & 9 deletions src/Renci.SshNet/PrivateKeyConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PrivateKeyConnectionInfo : ConnectionInfo, IDisposable
/// <summary>
/// Gets the key files used for authentication.
/// </summary>
public ICollection<IHostAlgorithmsProvider> KeyFiles { get; private set; }
public ICollection<IPrivateKeySource> KeyFiles { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="PrivateKeyConnectionInfo"/> class.
Expand All @@ -41,7 +41,7 @@ public PrivateKeyConnectionInfo(string host, string username, params PrivateKeyF
/// <param name="port">Connection port.</param>
/// <param name="username">Connection username.</param>
/// <param name="keyFiles">Connection key files.</param>
public PrivateKeyConnectionInfo(string host, int port, string username, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, int port, string username, params IPrivateKeySource[] keyFiles)
: this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty, keyFiles)
{
}
Expand All @@ -56,7 +56,7 @@ public PrivateKeyConnectionInfo(string host, int port, string username, params I
/// <param name="proxyHost">The proxy host.</param>
/// <param name="proxyPort">The proxy port.</param>
/// <param name="keyFiles">The key files.</param>
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IPrivateKeySource[] keyFiles)
: this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty, keyFiles)
{
}
Expand All @@ -72,7 +72,7 @@ public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTyp
/// <param name="proxyPort">The proxy port.</param>
/// <param name="proxyUsername">The proxy username.</param>
/// <param name="keyFiles">The key files.</param>
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IPrivateKeySource[] keyFiles)
: this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty, keyFiles)
{
}
Expand All @@ -86,7 +86,7 @@ public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTyp
/// <param name="proxyHost">The proxy host.</param>
/// <param name="proxyPort">The proxy port.</param>
/// <param name="keyFiles">The key files.</param>
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IPrivateKeySource[] keyFiles)
: this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty, keyFiles)
{
}
Expand All @@ -101,7 +101,7 @@ public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyTy
/// <param name="proxyPort">The proxy port.</param>
/// <param name="proxyUsername">The proxy username.</param>
/// <param name="keyFiles">The key files.</param>
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IPrivateKeySource[] keyFiles)
: this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty, keyFiles)
{
}
Expand All @@ -117,7 +117,7 @@ public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyTy
/// <param name="proxyUsername">The proxy username.</param>
/// <param name="proxyPassword">The proxy password.</param>
/// <param name="keyFiles">The key files.</param>
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IPrivateKeySource[] keyFiles)
: this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, keyFiles)
{
}
Expand All @@ -134,10 +134,10 @@ public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyTy
/// <param name="proxyUsername">The proxy username.</param>
/// <param name="proxyPassword">The proxy password.</param>
/// <param name="keyFiles">The key files.</param>
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IHostAlgorithmsProvider[] keyFiles)
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IPrivateKeySource[] keyFiles)
: base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new PrivateKeyAuthenticationMethod(username, keyFiles))
{
KeyFiles = new Collection<IHostAlgorithmsProvider>(keyFiles);
KeyFiles = new Collection<IPrivateKeySource>(keyFiles);
}

/// <summary>
Expand Down
Loading

0 comments on commit 18e6673

Please sign in to comment.