From 89522f6f3f40b78480b29835531bad594fd85c32 Mon Sep 17 00:00:00 2001 From: Stefan Rinkes Date: Thu, 7 Mar 2024 22:11:01 +0100 Subject: [PATCH] Update SSH.NET --- README.md | 1 - SshNet.PuttyKeyFile.Tests/PuttyKeyFileTest.cs | 9 +++--- .../SshNet.PuttyKeyFile.Tests.csproj | 6 ++-- SshNet.PuttyKeyFile/PuttyKeyFile.cs | 30 ++++++++++--------- .../SshNet.PuttyKeyFile.csproj | 6 ++-- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3bdf074..8a17f6d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ SshNet.PuttyKeyFile [![NuGet](https://img.shields.io/nuget/v/SshNet.PuttyKeyFile.svg?style=flat)](https://www.nuget.org/packages/SshNet.PuttyKeyFile) ![Nuget](https://img.shields.io/nuget/dt/SshNet.PuttyKeyFile) -![CodeQL](https://github.com/darinkes/SshNet.PuttyKeyFile/workflows/CodeQL/badge.svg) ![.NET-Ubuntu](https://github.com/darinkes/SshNet.PuttyKeyFile/workflows/.NET-Ubuntu/badge.svg) ![.NET-Windows](https://github.com/darinkes/SshNet.PuttyKeyFile/workflows/.NET-Windows/badge.svg) ![NuGet](https://github.com/darinkes/SshNet.PuttyKeyFile/workflows/NuGet/badge.svg) diff --git a/SshNet.PuttyKeyFile.Tests/PuttyKeyFileTest.cs b/SshNet.PuttyKeyFile.Tests/PuttyKeyFileTest.cs index 35b0a68..eaa7d46 100644 --- a/SshNet.PuttyKeyFile.Tests/PuttyKeyFileTest.cs +++ b/SshNet.PuttyKeyFile.Tests/PuttyKeyFileTest.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using NUnit.Framework; +using NUnit.Framework.Legacy; using Renci.SshNet.Security; namespace SshNet.PuttyKeyFile.Tests @@ -14,7 +15,7 @@ public void Setup() { } - private void TestKey(string keyName, string versionSuffix, string comment, int keyLength = 0, string? pass = null) where TKey : Key, new() + private void TestKey(string keyName, string versionSuffix, string comment, int keyLength = 0, string? pass = null) where TKey : Key { var keyStream = GetKey($"{keyName}-v{versionSuffix}.ppk"); if (keyStream is null) @@ -22,9 +23,9 @@ public void Setup() var keyFile = new PuttyKeyFile(keyStream, pass); - Assert.IsInstanceOf(((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key); - Assert.AreEqual(keyLength, ((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key.KeyLength); - Assert.AreEqual(comment, ((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key.Comment); + ClassicAssert.IsInstanceOf(((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key); + ClassicAssert.AreEqual(keyLength, ((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key.KeyLength); + ClassicAssert.AreEqual(comment, ((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key.Comment); } [Test] diff --git a/SshNet.PuttyKeyFile.Tests/SshNet.PuttyKeyFile.Tests.csproj b/SshNet.PuttyKeyFile.Tests/SshNet.PuttyKeyFile.Tests.csproj index d2009ce..2173fa0 100644 --- a/SshNet.PuttyKeyFile.Tests/SshNet.PuttyKeyFile.Tests.csproj +++ b/SshNet.PuttyKeyFile.Tests/SshNet.PuttyKeyFile.Tests.csproj @@ -8,9 +8,9 @@ - - - + + + diff --git a/SshNet.PuttyKeyFile/PuttyKeyFile.cs b/SshNet.PuttyKeyFile/PuttyKeyFile.cs index 26fd6a0..0a8a51e 100644 --- a/SshNet.PuttyKeyFile/PuttyKeyFile.cs +++ b/SshNet.PuttyKeyFile/PuttyKeyFile.cs @@ -13,6 +13,9 @@ using Renci.SshNet.Security.Cryptography.Ciphers.Paddings; using SshNet.PuttyKeyFile.Extensions; using Konscious.Security.Cryptography; +using Renci.SshNet.Security.Cryptography; +using HMACSHA1 = System.Security.Cryptography.HMACSHA1; +using HMACSHA256 = System.Security.Cryptography.HMACSHA256; namespace SshNet.PuttyKeyFile { @@ -115,8 +118,7 @@ private void Open(Stream privateKey, string? passPhrase) case 2: { var cipherKey = GetCipherKey(passPhrase, 32); - var cipher = new AesCipher(cipherKey, new CbcCipherMode(new byte[cipherKey.Length]), - new PKCS7Padding()); + var cipher = new AesCipher(cipherKey, new byte[cipherKey.Length], AesCipherMode.CBC); var privateKeyData = Convert.FromBase64String(privateLines); if (privateKeyData.Length % cipher.BlockSize != 0) @@ -168,7 +170,7 @@ private void Open(Stream privateKey, string? passPhrase) macKey3.Clear(); macKey3.AddRange(macKey); - var cipher = new AesCipher(cipherKey, new CbcCipherMode(crcIv), new PKCS7Padding()); + var cipher = new AesCipher(cipherKey, crcIv, AesCipherMode.CBC); var privateKeyData = Convert.FromBase64String(privateLines); if (privateKeyData.Length % cipher.BlockSize != 0) @@ -264,24 +266,23 @@ private void Open(Stream privateKey, string? passPhrase) throw new SshException($"PuTTY Public Key Type '{pubKeyType}' and Private Key Type '{keyType}' differ"); } - Key parsedKey; - byte[] publicKey; byte[] unencryptedPrivateKey; switch (keyType) { case "ssh-ed25519": - publicKey = publicKeyReader.ReadBignum2(); unencryptedPrivateKey = privateKeyReader.ReadBignum2(); - parsedKey = new ED25519Key(publicKey.Reverse(), unencryptedPrivateKey); + Key = new ED25519Key(unencryptedPrivateKey); + _hostAlgorithms.Add(new KeyHostAlgorithm(Key.ToString(), Key)); break; case "ecdsa-sha2-nistp256": case "ecdsa-sha2-nistp384": case "ecdsa-sha2-nistp521": var len = (int)publicKeyReader.ReadUInt32(); var curve = Encoding.ASCII.GetString(publicKeyReader.ReadBytes(len)); - publicKey = publicKeyReader.ReadBignum2(); + var publicKey = publicKeyReader.ReadBignum2(); unencryptedPrivateKey = privateKeyReader.ReadBignum2(); - parsedKey = new EcdsaKey(curve, publicKey, unencryptedPrivateKey.TrimLeadingZeros()); + Key = new EcdsaKey(curve, publicKey, unencryptedPrivateKey.TrimLeadingZeros()); + _hostAlgorithms.Add(new KeyHostAlgorithm(Key.ToString(), Key)); break; case "ssh-rsa": var exponent = publicKeyReader.ReadBigIntWithBytes(); @@ -290,16 +291,17 @@ private void Open(Stream privateKey, string? passPhrase) var p = privateKeyReader.ReadBigIntWithBytes(); var q = privateKeyReader.ReadBigIntWithBytes(); var inverseQ = privateKeyReader.ReadBigIntWithBytes(); - parsedKey = new RsaKey(modulus, exponent, d, p, q, inverseQ); + var rsaKey = new RsaKey(modulus, exponent, d, p, q, inverseQ); + Key = rsaKey; + _hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", Key)); + _hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", Key, new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA512))); + _hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", Key, new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA256))); break; default: throw new SshException("PuTTY key type '" + keyType + "' is not supported."); } - parsedKey.Comment = comment; - - Key = parsedKey; - _hostAlgorithms.Add(new KeyHostAlgorithm(parsedKey.ToString(), parsedKey)); + Key.Comment = comment; } diff --git a/SshNet.PuttyKeyFile/SshNet.PuttyKeyFile.csproj b/SshNet.PuttyKeyFile/SshNet.PuttyKeyFile.csproj index 0e1bd04..60dc867 100644 --- a/SshNet.PuttyKeyFile/SshNet.PuttyKeyFile.csproj +++ b/SshNet.PuttyKeyFile/SshNet.PuttyKeyFile.csproj @@ -5,12 +5,12 @@ 9 enable SshNet.PuttyKeyFile - 0.2.0-beta + 2024.0.0-beta $(Version) ssh;scp;sftp Extension to read and use Authentication Keys in PuTTY-Format https://github.com/darinkes/SshNet.PuttyKeyFile/releases/tag/$(PackageVersion) - Copyright (c) 2021 - 2023 Stefan Rinkes + Copyright (c) 2021 - 2024 Stefan Rinkes MIT https://github.com/darinkes/SshNet.PuttyKeyFile/ false @@ -20,7 +20,7 @@ - + \ No newline at end of file