Skip to content

Make PrivateKeyFile an implementation of IPrivateKeySource to make room for other key sources #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@
<Compile Include="..\Renci.SshNet\IForwardedPort.cs">
<Link>IForwardedPort.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IPrivateKeySource.cs">
<Link>IPrivateKeySource.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IRemotePathTransformation.cs">
<Link>IRemotePathTransformation.cs</Link>
</Compile>
Expand Down Expand Up @@ -971,7 +974,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" />
<UserProperties ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@
<Compile Include="..\Renci.SshNet\IForwardedPort.cs">
<Link>IForwardedPort.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IPrivateKeySource.cs">
<Link>IPrivateKeySource.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IRemotePathTransformation.cs">
<Link>IRemotePathTransformation.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@
<Compile Include="..\Renci.SshNet\IForwardedPort.cs">
<Link>IForwardedPort.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IPrivateKeySource.cs">
<Link>IPrivateKeySource.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IRemotePathTransformation.cs">
<Link>IRemotePathTransformation.cs</Link>
</Compile>
Expand Down
16 changes: 8 additions & 8 deletions src/Renci.SshNet.Tests/Classes/ScpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public void Ctor_HostAndPortAndUsernameAndPrivateKeys()
var privateKeyAuthentication = privateKeyConnectionInfo.AuthenticationMethods[0] as PrivateKeyAuthenticationMethod;
Assert.IsNotNull(privateKeyAuthentication);
Assert.AreEqual(userName, privateKeyAuthentication.Username);
Assert.IsNotNull(privateKeyAuthentication.KeyFiles);
Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeyFiles.Count);
Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[0]));
Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[1]));
Assert.IsNotNull(privateKeyAuthentication.KeySources);
Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeySources.Count);
Assert.IsTrue(privateKeyAuthentication.KeySources.Contains(privateKeys[0]));
Assert.IsTrue(privateKeyAuthentication.KeySources.Contains(privateKeys[1]));
}

[TestMethod]
Expand Down Expand Up @@ -178,10 +178,10 @@ public void Ctor_HostAndUsernameAndPrivateKeys()
var privateKeyAuthentication = privateKeyConnectionInfo.AuthenticationMethods[0] as PrivateKeyAuthenticationMethod;
Assert.IsNotNull(privateKeyAuthentication);
Assert.AreEqual(userName, privateKeyAuthentication.Username);
Assert.IsNotNull(privateKeyAuthentication.KeyFiles);
Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeyFiles.Count);
Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[0]));
Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[1]));
Assert.IsNotNull(privateKeyAuthentication.KeySources);
Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeySources.Count);
Assert.IsTrue(privateKeyAuthentication.KeySources.Contains(privateKeys[0]));
Assert.IsTrue(privateKeyAuthentication.KeySources.Contains(privateKeys[1]));
}

[TestMethod]
Expand Down
3 changes: 3 additions & 0 deletions src/Renci.SshNet.UAP10/Renci.SshNet.UAP10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@
<Compile Include="..\Renci.SshNet\IForwardedPort.cs">
<Link>IForwardedPort.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IPrivateKeySource.cs">
<Link>IPrivateKeySource.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IRemotePathTransformation.cs">
<Link>IRemotePathTransformation.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@
<Compile Include="..\Renci.SshNet\IForwardedPort.cs">
<Link>IForwardedPort.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IPrivateKeySource.cs">
<Link>IPrivateKeySource.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IRemotePathTransformation.cs">
<Link>IRemotePathTransformation.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@
<Compile Include="..\Renci.SshNet\IForwardedPort.cs">
<Link>IForwardedPort.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IPrivateKeySource.cs">
<Link>IPrivateKeySource.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\IRemotePathTransformation.cs">
<Link>IRemotePathTransformation.cs</Link>
</Compile>
Expand Down
15 changes: 15 additions & 0 deletions src/Renci.SshNet/IPrivateKeySource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Renci.SshNet.Security;

namespace Renci.SshNet
{
/// <summary>
/// Represents private key information from various sources.
/// </summary>
public interface IPrivateKeySource
{
/// <summary>
/// Stores the host key.
/// </summary>
HostAlgorithm HostKey { get; }
}
}
16 changes: 8 additions & 8 deletions src/Renci.SshNet/NetConfClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public NetConfClient(string host, string username, string password)
/// <param name="host">Connection host.</param>
/// <param name="port">Connection port.</param>
/// <param name="username">Authentication username.</param>
/// <param name="keyFiles">Authentication private key file(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
/// <param name="keySources">Authentication private key source(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keySources"/> 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>
/// <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 PrivateKeyFile[] keyFiles)
: this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), true)
public NetConfClient(string host, int port, string username, params IPrivateKeySource[] keySources)
: this(new PrivateKeyConnectionInfo(host, port, username, keySources), true)
{
}

Expand All @@ -113,11 +113,11 @@ public NetConfClient(string host, int port, string username, params PrivateKeyFi
/// </summary>
/// <param name="host">Connection host.</param>
/// <param name="username">Authentication username.</param>
/// <param name="keyFiles">Authentication private key file(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
/// <param name="keySources">Authentication private key source(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keySources"/> 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 PrivateKeyFile[] keyFiles)
: this(host, ConnectionInfo.DefaultPort, username, keyFiles)
public NetConfClient(string host, string username, params IPrivateKeySource[] keySources)
: this(host, ConnectionInfo.DefaultPort, username, keySources)
{
}

Expand Down
32 changes: 16 additions & 16 deletions src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ public override string Name
}

/// <summary>
/// Gets the key files used for authentication.
/// Gets the key sources used for authentication.
/// </summary>
public ICollection<PrivateKeyFile> KeyFiles { get; private set; }
public ICollection<IPrivateKeySource> KeySources { 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>
/// <param name="keySources">The key sources.</param>
/// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <c>null</c>.</exception>
public PrivateKeyAuthenticationMethod(string username, params PrivateKeyFile[] keyFiles)
public PrivateKeyAuthenticationMethod(string username, params IPrivateKeySource[] keySources)
: base(username)
{
if (keyFiles == null)
throw new ArgumentNullException("keyFiles");
if (keySources == null)
throw new ArgumentNullException("keySources");

KeyFiles = new Collection<PrivateKeyFile>(keyFiles);
KeySources = new Collection<IPrivateKeySource>(keySources);
}

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

try
{
foreach (var keyFile in KeyFiles)
foreach (var keySource in KeySources)
{
_authenticationCompleted.Reset();
_isSignatureRequired = false;

var message = new RequestMessagePublicKey(ServiceName.Connection,
Username,
keyFile.HostKey.Name,
keyFile.HostKey.Data);
keySource.HostKey.Name,
keySource.HostKey.Data);

if (KeyFiles.Count < 2)
if (KeySources.Count < 2)
{
// If only one key file provided then send signature for very first request
// If only one key source provided then send signature for very first request
var signatureData = new SignatureData(message, session.SessionId).GetBytes();

message.Signature = keyFile.HostKey.Sign(signatureData);
message.Signature = keySource.HostKey.Sign(signatureData);
}

// Send public key authentication request
Expand All @@ -91,12 +91,12 @@ public override AuthenticationResult Authenticate(Session session)

var signatureMessage = new RequestMessagePublicKey(ServiceName.Connection,
Username,
keyFile.HostKey.Name,
keyFile.HostKey.Data);
keySource.HostKey.Name,
keySource.HostKey.Data);

var signatureData = new SignatureData(message, session.SessionId).GetBytes();

signatureMessage.Signature = keyFile.HostKey.Sign(signatureData);
signatureMessage.Signature = keySource.HostKey.Sign(signatureData);

// Send public key authentication request with signature
session.SendMessage(signatureMessage);
Expand Down
Loading