Skip to content

Commit 18e6673

Browse files
Improvements after #1177 (#1180)
1 parent 8732d3d commit 18e6673

18 files changed

+68
-104
lines changed

src/Renci.SshNet.Benchmarks/Security/Cryptography/Ciphers/RsaCipherBenchmarks.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public RsaCipherBenchmarks()
2121

2222
using (var s = typeof(RsaCipherBenchmarks).Assembly.GetManifestResourceStream("Renci.SshNet.Benchmarks.Data.Key.RSA.txt"))
2323
{
24-
_privateKey = (RsaKey)((KeyHostAlgorithm) new PrivateKeyFile(s).HostKey).Key;
24+
25+
_privateKey = (RsaKey)new PrivateKeyFile(s).Key;
2526

2627
// The implementations of RsaCipher.Encrypt/Decrypt differ based on whether the supplied RsaKey has private key information
2728
// or only public. So we extract out the public key information to a separate variable.

src/Renci.SshNet.Benchmarks/Security/Cryptography/ED25519DigitalSignatureBenchmarks.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ED25519DigitalSignatureBenchmarks()
2121

2222
using (var s = typeof(ED25519DigitalSignatureBenchmarks).Assembly.GetManifestResourceStream("Renci.SshNet.Benchmarks.Data.Key.OPENSSH.ED25519.txt"))
2323
{
24-
_key = (ED25519Key) ((KeyHostAlgorithm) new PrivateKeyFile(s).HostKey).Key;
24+
_key = (ED25519Key) new PrivateKeyFile(s).Key;
2525
}
2626
_signature = new ED25519DigitalSignature(_key).Sign(_data);
2727
}

src/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Reflection;
34
using System.Threading;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -140,7 +141,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
140141
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
141142
{
142143
var privateKey = new PrivateKeyFile(s);
143-
return (KeyHostAlgorithm) privateKey.HostKey;
144+
return (KeyHostAlgorithm) privateKey.HostKeyAlgorithms.First();
144145
}
145146
}
146147

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
8989
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
9090
{
9191
var privateKey = new PrivateKeyFile(s);
92-
return (KeyHostAlgorithm)privateKey.HostKey;
92+
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
9393
}
9494
}
9595

src/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Reflection;
34
using System.Threading;
45

@@ -113,7 +114,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
113114
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
114115
{
115116
var privateKey = new PrivateKeyFile(s);
116-
return (KeyHostAlgorithm)privateKey.HostKey;
117+
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
117118
}
118119
}
119120
}

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

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Renci.SshNet.Common;
3-
using Renci.SshNet.Security;
43
using Renci.SshNet.Tests.Common;
54
using System;
6-
using System.Collections.Generic;
75
using System.IO;
86
using System.Linq;
97

@@ -412,7 +410,7 @@ public void ConstructorWithStreamAndPassphrase()
412410
using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
413411
{
414412
var privateKeyFile = new PrivateKeyFile(stream, "12345");
415-
Assert.IsNotNull(privateKeyFile.HostKey);
413+
TestRsaKeyFile(privateKeyFile);
416414
}
417415
}
418416

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

435433
fs.Close();
436434
}
@@ -498,7 +496,7 @@ public void ConstructorWithFileName()
498496
}
499497

500498
var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
501-
Assert.IsNotNull(privateKeyFile.HostKey);
499+
TestRsaKeyFile(privateKeyFile);
502500
}
503501

504502
/// <summary>
@@ -510,7 +508,7 @@ public void ConstructorWithStream()
510508
using (var stream = GetData("Key.RSA.txt"))
511509
{
512510
var privateKeyFile = new PrivateKeyFile(stream);
513-
Assert.IsNotNull(privateKeyFile.HostKey);
511+
TestRsaKeyFile(privateKeyFile);
514512
}
515513
}
516514

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

531529
fs.Close();
532530
}
@@ -544,7 +542,7 @@ public void ConstructorWithFileNameAndPassPhraseShouldBeAbleToReadFileThatIsShar
544542
using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
545543
{
546544
var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
547-
Assert.IsNotNull(privateKeyFile.HostKey);
545+
TestRsaKeyFile(privateKeyFile);
548546

549547
fs.Close();
550548
}
@@ -684,15 +682,14 @@ private string GetTempFileName()
684682

685683
private static void TestRsaKeyFile(PrivateKeyFile rsaPrivateKeyFile)
686684
{
687-
Assert.AreEqual(3, rsaPrivateKeyFile.HostAlgorithms.Count);
685+
Assert.IsNotNull(rsaPrivateKeyFile.HostKeyAlgorithms);
686+
Assert.AreEqual(3, rsaPrivateKeyFile.HostKeyAlgorithms.Count);
688687

689-
List<KeyHostAlgorithm> algorithms = rsaPrivateKeyFile.HostAlgorithms.Cast<KeyHostAlgorithm>().ToList();
688+
var algorithms = rsaPrivateKeyFile.HostKeyAlgorithms.ToList();
690689

691690
Assert.AreEqual("rsa-sha2-512", algorithms[0].Name);
692691
Assert.AreEqual("rsa-sha2-256", algorithms[1].Name);
693692
Assert.AreEqual("ssh-rsa", algorithms[2].Name);
694-
695-
Assert.AreSame(algorithms[0], rsaPrivateKeyFile.HostKey);
696693
}
697694
}
698695
}

src/Renci.SshNet.Tests/Classes/Security/Cryptography/RsaDigitalSignatureTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private static RsaKey GetRsaKey()
164164
{
165165
using (var stream = GetData("Key.RSA.txt"))
166166
{
167-
return (RsaKey) ((KeyHostAlgorithm) new PrivateKeyFile(stream).HostKey).Key;
167+
return (RsaKey) new PrivateKeyFile(stream).Key;
168168
}
169169
}
170170

src/Renci.SshNet.Tests/Classes/Security/KeyAlgorithmTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Security.Cryptography;
1+
using System.Security.Cryptography;
22
using System.Text;
33

44
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -176,7 +176,7 @@ private static RsaKey GetRsaKey()
176176
{
177177
using (var stream = GetData("Key.RSA.txt"))
178178
{
179-
return (RsaKey) ((KeyHostAlgorithm) new PrivateKeyFile(stream).HostKey).Key;
179+
return (RsaKey) new PrivateKeyFile(stream).Key;
180180
}
181181
}
182182

src/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Reflection;
34
using System.Threading;
45

@@ -122,7 +123,7 @@ private static KeyHostAlgorithm GetKeyHostAlgorithm()
122123
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
123124
{
124125
var privateKey = new PrivateKeyFile(s);
125-
return (KeyHostAlgorithm)privateKey.HostKey;
126+
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
126127
}
127128
}
128129
}

src/Renci.SshNet/IHostAlgorithmsProvider.cs

-21
This file was deleted.

src/Renci.SshNet/IPrivateKeySource.cs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.ComponentModel;
1+
using System.Collections.Generic;
32

43
using Renci.SshNet.Security;
54

@@ -8,19 +7,15 @@ namespace Renci.SshNet
87
/// <summary>
98
/// Represents private key source interface.
109
/// </summary>
11-
/// <remarks>
12-
/// This interface has been replaced by <see cref="IHostAlgorithmsProvider"/>
13-
/// and is obsolete.
14-
/// </remarks>
15-
[Obsolete($"Use {nameof(IHostAlgorithmsProvider)} instead. " +
16-
$"{nameof(IPrivateKeySource)} may be removed in a future release. " +
17-
$"See https://github.com/sshnet/SSH.NET/issues/1174 for details.")]
18-
[EditorBrowsable(EditorBrowsableState.Never)]
19-
public interface IPrivateKeySource : IHostAlgorithmsProvider
10+
public interface IPrivateKeySource
2011
{
2112
/// <summary>
22-
/// Gets the host key.
13+
/// Gets the host keys algorithms.
2314
/// </summary>
24-
HostAlgorithm HostKey { get; }
15+
/// <remarks>
16+
/// In situations where there is a preferred order of usage of the host algorithms,
17+
/// the collection should be ordered from most preferred to least.
18+
/// </remarks>
19+
IReadOnlyCollection<HostAlgorithm> HostKeyAlgorithms { get; }
2520
}
2621
}

src/Renci.SshNet/NetConfClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public NetConfClient(string host, string username, string password)
107107
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
108108
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
109109
[SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
110-
public NetConfClient(string host, int port, string username, params IHostAlgorithmsProvider[] keyFiles)
110+
public NetConfClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)
111111
: this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true)
112112
{
113113
}
@@ -120,7 +120,7 @@ public NetConfClient(string host, int port, string username, params IHostAlgorit
120120
/// <param name="keyFiles">Authentication private key file(s) .</param>
121121
/// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
122122
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
123-
public NetConfClient(string host, string username, params IHostAlgorithmsProvider[] keyFiles)
123+
public NetConfClient(string host, string username, params IPrivateKeySource[] keyFiles)
124124
: this(host, ConnectionInfo.DefaultPort, username, keyFiles)
125125
{
126126
}

src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ public override string Name
3131
/// <summary>
3232
/// Gets the key files used for authentication.
3333
/// </summary>
34-
public ICollection<IHostAlgorithmsProvider> KeyFiles { get; private set; }
34+
public ICollection<IPrivateKeySource> KeyFiles { get; private set; }
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="PrivateKeyAuthenticationMethod"/> class.
3838
/// </summary>
3939
/// <param name="username">The username.</param>
4040
/// <param name="keyFiles">The key files.</param>
4141
/// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <c>null</c>.</exception>
42-
public PrivateKeyAuthenticationMethod(string username, params IHostAlgorithmsProvider[] keyFiles)
42+
public PrivateKeyAuthenticationMethod(string username, params IPrivateKeySource[] keyFiles)
4343
: base(username)
4444
{
4545
if (keyFiles is null)
4646
{
4747
throw new ArgumentNullException(nameof(keyFiles));
4848
}
4949

50-
KeyFiles = new Collection<IHostAlgorithmsProvider>(keyFiles);
50+
KeyFiles = new Collection<IPrivateKeySource>(keyFiles);
5151
}
5252

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

6666
session.RegisterMessage("SSH_MSG_USERAUTH_PK_OK");
6767

68-
var hostAlgorithms = KeyFiles.SelectMany(x => x.HostAlgorithms).ToList();
68+
var hostAlgorithms = KeyFiles.SelectMany(x => x.HostKeyAlgorithms).ToList();
6969

7070
try
7171
{

src/Renci.SshNet/PrivateKeyConnectionInfo.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class PrivateKeyConnectionInfo : ConnectionInfo, IDisposable
1717
/// <summary>
1818
/// Gets the key files used for authentication.
1919
/// </summary>
20-
public ICollection<IHostAlgorithmsProvider> KeyFiles { get; private set; }
20+
public ICollection<IPrivateKeySource> KeyFiles { get; private set; }
2121

2222
/// <summary>
2323
/// Initializes a new instance of the <see cref="PrivateKeyConnectionInfo"/> class.
@@ -41,7 +41,7 @@ public PrivateKeyConnectionInfo(string host, string username, params PrivateKeyF
4141
/// <param name="port">Connection port.</param>
4242
/// <param name="username">Connection username.</param>
4343
/// <param name="keyFiles">Connection key files.</param>
44-
public PrivateKeyConnectionInfo(string host, int port, string username, params IHostAlgorithmsProvider[] keyFiles)
44+
public PrivateKeyConnectionInfo(string host, int port, string username, params IPrivateKeySource[] keyFiles)
4545
: this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty, keyFiles)
4646
{
4747
}
@@ -56,7 +56,7 @@ public PrivateKeyConnectionInfo(string host, int port, string username, params I
5656
/// <param name="proxyHost">The proxy host.</param>
5757
/// <param name="proxyPort">The proxy port.</param>
5858
/// <param name="keyFiles">The key files.</param>
59-
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IHostAlgorithmsProvider[] keyFiles)
59+
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IPrivateKeySource[] keyFiles)
6060
: this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty, keyFiles)
6161
{
6262
}
@@ -72,7 +72,7 @@ public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTyp
7272
/// <param name="proxyPort">The proxy port.</param>
7373
/// <param name="proxyUsername">The proxy username.</param>
7474
/// <param name="keyFiles">The key files.</param>
75-
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IHostAlgorithmsProvider[] keyFiles)
75+
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IPrivateKeySource[] keyFiles)
7676
: this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty, keyFiles)
7777
{
7878
}
@@ -86,7 +86,7 @@ public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTyp
8686
/// <param name="proxyHost">The proxy host.</param>
8787
/// <param name="proxyPort">The proxy port.</param>
8888
/// <param name="keyFiles">The key files.</param>
89-
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IHostAlgorithmsProvider[] keyFiles)
89+
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params IPrivateKeySource[] keyFiles)
9090
: this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty, keyFiles)
9191
{
9292
}
@@ -101,7 +101,7 @@ public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyTy
101101
/// <param name="proxyPort">The proxy port.</param>
102102
/// <param name="proxyUsername">The proxy username.</param>
103103
/// <param name="keyFiles">The key files.</param>
104-
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IHostAlgorithmsProvider[] keyFiles)
104+
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params IPrivateKeySource[] keyFiles)
105105
: this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty, keyFiles)
106106
{
107107
}
@@ -117,7 +117,7 @@ public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyTy
117117
/// <param name="proxyUsername">The proxy username.</param>
118118
/// <param name="proxyPassword">The proxy password.</param>
119119
/// <param name="keyFiles">The key files.</param>
120-
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IHostAlgorithmsProvider[] keyFiles)
120+
public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IPrivateKeySource[] keyFiles)
121121
: this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, keyFiles)
122122
{
123123
}
@@ -134,10 +134,10 @@ public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyTy
134134
/// <param name="proxyUsername">The proxy username.</param>
135135
/// <param name="proxyPassword">The proxy password.</param>
136136
/// <param name="keyFiles">The key files.</param>
137-
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IHostAlgorithmsProvider[] keyFiles)
137+
public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params IPrivateKeySource[] keyFiles)
138138
: base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new PrivateKeyAuthenticationMethod(username, keyFiles))
139139
{
140-
KeyFiles = new Collection<IHostAlgorithmsProvider>(keyFiles);
140+
KeyFiles = new Collection<IPrivateKeySource>(keyFiles);
141141
}
142142

143143
/// <summary>

0 commit comments

Comments
 (0)