Skip to content

Commit

Permalink
Update SSH.NET
Browse files Browse the repository at this point in the history
  • Loading branch information
darinkes committed Mar 8, 2024
1 parent 32e8e27 commit 89522f6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions SshNet.PuttyKeyFile.Tests/PuttyKeyFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,17 +15,17 @@ public void Setup()
{
}

private void TestKey<TKey>(string keyName, string versionSuffix, string comment, int keyLength = 0, string? pass = null) where TKey : Key, new()
private void TestKey<TKey>(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)
throw new NullReferenceException(nameof(keyStream));

var keyFile = new PuttyKeyFile(keyStream, pass);

Assert.IsInstanceOf<TKey>(((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<TKey>(((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key);
ClassicAssert.AreEqual(keyLength, ((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key.KeyLength);
ClassicAssert.AreEqual(comment, ((KeyHostAlgorithm) keyFile.HostKeyAlgorithms.First()).Key.Comment);
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions SshNet.PuttyKeyFile.Tests/SshNet.PuttyKeyFile.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 16 additions & 14 deletions SshNet.PuttyKeyFile/PuttyKeyFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}


Expand Down
6 changes: 3 additions & 3 deletions SshNet.PuttyKeyFile/SshNet.PuttyKeyFile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<PackageId>SshNet.PuttyKeyFile</PackageId>
<Version>0.2.0-beta</Version>
<Version>2024.0.0-beta</Version>
<PackageVersion>$(Version)</PackageVersion>
<PackageTags>ssh;scp;sftp</PackageTags>
<Description>Extension to read and use Authentication Keys in PuTTY-Format</Description>
<PackageReleaseNotes>https://github.com/darinkes/SshNet.PuttyKeyFile/releases/tag/$(PackageVersion)</PackageReleaseNotes>
<Copyright>Copyright (c) 2021 - 2023 Stefan Rinkes</Copyright>
<Copyright>Copyright (c) 2021 - 2024 Stefan Rinkes</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/darinkes/SshNet.PuttyKeyFile/</PackageProjectUrl>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
Expand All @@ -20,7 +20,7 @@

<ItemGroup>
<PackageReference Include="SshNet.Security.Cryptography" Version="[1.3.0]" />
<PackageReference Include="SSH.NET" Version="2023.0.0" />
<PackageReference Include="SSH.NET" Version="2024.0.0" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0"/>
</ItemGroup>
</Project>

0 comments on commit 89522f6

Please sign in to comment.