From 8a2a1ab468f3f95be53566c2de12fcf9920e1845 Mon Sep 17 00:00:00 2001 From: olegkap_cp Date: Wed, 18 Aug 2010 15:10:52 +0000 Subject: [PATCH] Fix DSS key authentication, Rename KeyFile to PrivateKeyFile --- .../Renci.SshClient/ConnectionInfo.cs | 2 +- .../{KeyFile.cs => PrivateKeyFile.cs} | 33 +++++++++++-------- .../Renci.SshClient/Renci.SshClient.csproj | 3 +- .../Security/CryptoPrivateKeyDss.cs | 8 ++--- .../Security/CryptoPublicKeyDss.cs | 13 ++++---- .../Security/UserAuthenticationPublicKey.cs | 8 ++--- 6 files changed, 33 insertions(+), 34 deletions(-) rename Renci.SshClient/Renci.SshClient/{KeyFile.cs => PrivateKeyFile.cs} (88%) diff --git a/Renci.SshClient/Renci.SshClient/ConnectionInfo.cs b/Renci.SshClient/Renci.SshClient/ConnectionInfo.cs index cc5720b1..0006a3d7 100644 --- a/Renci.SshClient/Renci.SshClient/ConnectionInfo.cs +++ b/Renci.SshClient/Renci.SshClient/ConnectionInfo.cs @@ -10,7 +10,7 @@ public string Password { get; set; } - public KeyFile KeyFile { get; set; } + public PrivateKeyFile KeyFile { get; set; } public ConnectionInfo() { diff --git a/Renci.SshClient/Renci.SshClient/KeyFile.cs b/Renci.SshClient/Renci.SshClient/PrivateKeyFile.cs similarity index 88% rename from Renci.SshClient/Renci.SshClient/KeyFile.cs rename to Renci.SshClient/Renci.SshClient/PrivateKeyFile.cs index f1049764..0880a342 100644 --- a/Renci.SshClient/Renci.SshClient/KeyFile.cs +++ b/Renci.SshClient/Renci.SshClient/PrivateKeyFile.cs @@ -7,7 +7,7 @@ using Renci.SshClient.Security; namespace Renci.SshClient { - public class KeyFile + public class PrivateKeyFile { private Regex _beginKeyLine = new Regex(@"----[ ]*BEGIN (?.+) PRIVATE KEY[ ]*----"); private Regex _headerLine = new Regex(@"(?[^:]{1,64}):[ ](?[^:]+(?\\)?)"); @@ -37,12 +37,27 @@ namespace Renci.SshClient return this._key.GetSignature(sessionId); } - public KeyFile() + public PrivateKeyFile(Stream privateKey) { - + this.Open(privateKey, null); } - public void Open(Stream privateKey, string passPhrase) + public PrivateKeyFile(string fileName) + { + this.Open(File.Open(fileName, FileMode.Open), null); + } + + public PrivateKeyFile(string fileName, string passPhrase) + { + this.Open(File.Open(fileName, FileMode.Open), passPhrase); + } + + public PrivateKeyFile(Stream privateKey, string passPhrase) + { + this.Open(privateKey, passPhrase); + } + + private void Open(Stream privateKey, string passPhrase) { var headerTag = string.Empty; var headerValue = string.Empty; @@ -134,15 +149,5 @@ namespace Renci.SshClient this._key.Load(System.Convert.FromBase64String(data.ToString()), passPhrase); } - public void Open(string fileName) - { - this.Open(File.Open(fileName, FileMode.Open), null); - } - - public void Open(string fileName, string passPhrase) - { - this.Open(File.Open(fileName, FileMode.Open), passPhrase); - } - } } \ No newline at end of file diff --git a/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj b/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj index ffc5b634..d1f4c1a1 100644 --- a/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj +++ b/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj @@ -118,7 +118,7 @@ - + @@ -199,7 +199,6 @@ - diff --git a/Renci.SshClient/Renci.SshClient/Security/CryptoPrivateKeyDss.cs b/Renci.SshClient/Renci.SshClient/Security/CryptoPrivateKeyDss.cs index 03f3c577..63eff65e 100644 --- a/Renci.SshClient/Renci.SshClient/Security/CryptoPrivateKeyDss.cs +++ b/Renci.SshClient/Renci.SshClient/Security/CryptoPrivateKeyDss.cs @@ -11,8 +11,8 @@ namespace Renci.SshClient.Security private byte[] _p; private byte[] _q; private byte[] _g; - private byte[] _x; private byte[] _publicKey; + private byte[] _privateKey; public override string Name { @@ -62,13 +62,13 @@ namespace Renci.SshClient.Security this._publicKey = binr.ReadBytes(elems); elems = CryptoPrivateKeyDss.GetIntegerSize(binr); - this._x = binr.ReadBytes(elems); + this._privateKey = binr.ReadBytes(elems); } } public override CryptoPublicKey GetPublicKey() { - return new CryptoPublicKeyDss(this._p, this._q, this._g, this._x); + return new CryptoPublicKeyDss(this._p, this._q, this._g, this._publicKey); } public override IEnumerable GetSignature(IEnumerable key) @@ -79,7 +79,7 @@ namespace Renci.SshClient.Security { DSAParameters DSAKeyInfo = new DSAParameters(); - DSAKeyInfo.X = this._x.TrimLeadinZero().ToArray(); + DSAKeyInfo.X = this._privateKey.TrimLeadinZero().ToArray(); DSAKeyInfo.P = this._p.TrimLeadinZero().ToArray(); DSAKeyInfo.Q = this._q.TrimLeadinZero().ToArray(); DSAKeyInfo.G = this._g.TrimLeadinZero().ToArray(); diff --git a/Renci.SshClient/Renci.SshClient/Security/CryptoPublicKeyDss.cs b/Renci.SshClient/Renci.SshClient/Security/CryptoPublicKeyDss.cs index 925318a2..bc9e33d7 100644 --- a/Renci.SshClient/Renci.SshClient/Security/CryptoPublicKeyDss.cs +++ b/Renci.SshClient/Renci.SshClient/Security/CryptoPublicKeyDss.cs @@ -12,7 +12,7 @@ namespace Renci.SshClient.Security private IEnumerable _p; private IEnumerable _q; private IEnumerable _g; - private IEnumerable _x; + private IEnumerable _publicKey; public override string Name { @@ -24,12 +24,12 @@ namespace Renci.SshClient.Security } - public CryptoPublicKeyDss(IEnumerable p, IEnumerable q, IEnumerable g, IEnumerable x) + public CryptoPublicKeyDss(IEnumerable p, IEnumerable q, IEnumerable g, IEnumerable publicKey) { this._p = p; this._q = q; this._g = g; - this._x = x; + this._publicKey = publicKey; } public override void Load(IEnumerable data) @@ -52,7 +52,7 @@ namespace Renci.SshClient.Security var xl = BitConverter.ToUInt32(br.ReadBytes(4).Reverse().ToArray(), 0); - _x = br.ReadBytes((int)xl); + _publicKey = br.ReadBytes((int)xl); } } @@ -71,7 +71,7 @@ namespace Renci.SshClient.Security { dsa.ImportParameters(new DSAParameters { - X = _x.TrimLeadinZero().ToArray(), + X = _publicKey.TrimLeadinZero().ToArray(), P = _p.TrimLeadinZero().ToArray(), Q = _q.TrimLeadinZero().ToArray(), G = _g.TrimLeadinZero().ToArray(), @@ -117,7 +117,7 @@ namespace Renci.SshClient.Security P = this._p, Q = this._q, G = this._g, - Public = this._x, + Public = this._publicKey, }.GetBytes(); } @@ -144,6 +144,5 @@ namespace Renci.SshClient.Security this.Write(this.Public.GetSshString()); } } - } } diff --git a/Renci.SshClient/Renci.SshClient/Security/UserAuthenticationPublicKey.cs b/Renci.SshClient/Renci.SshClient/Security/UserAuthenticationPublicKey.cs index 19db474a..e0660c06 100644 --- a/Renci.SshClient/Renci.SshClient/Security/UserAuthenticationPublicKey.cs +++ b/Renci.SshClient/Renci.SshClient/Security/UserAuthenticationPublicKey.cs @@ -30,7 +30,6 @@ namespace Renci.SshClient.Security Message.RegisterMessageType(MessageTypes.UserAuthenticationInformationRequest); - // TODO: Complete full public key implemention which includes other messages var message = new PublicKeyRequestMessage { @@ -38,15 +37,12 @@ namespace Renci.SshClient.Security Username = this.Session.ConnectionInfo.Username, PublicKeyAlgorithmName = this.Session.ConnectionInfo.KeyFile.AlgorithmName, PublicKeyData = this.Session.ConnectionInfo.KeyFile.PublicKey, - Signature = new byte[] { }, - //Signature = this.Session.ConnectionInfo.KeyFile.GetSignature(this.Session.SessionId), + //Signature = new byte[] { }, }; var signatureData = new SignatureData(message, this.Session.SessionId.GetSshString()).GetBytes(); - var signature = this.Session.ConnectionInfo.KeyFile.GetSignature(signatureData); - - message.Signature = signature; + message.Signature = this.Session.ConnectionInfo.KeyFile.GetSignature(signatureData); this.Session.SendMessage(message);