Skip to content
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

Remove rsa-sha2-256 workaround #1255

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
76 changes: 0 additions & 76 deletions src/bbs2gh/RsaWithSha256SignatureKey.cs

This file was deleted.

51 changes: 4 additions & 47 deletions src/bbs2gh/Services/BbsSshArchiveDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;
using Renci.SshNet;
using Renci.SshNet.Security;

namespace OctoshiftCLI.BbsToGithub.Services;

Expand All @@ -14,9 +12,6 @@ public sealed class BbsSshArchiveDownloader : IBbsArchiveDownloader, IDisposable
private const int DOWNLOAD_PROGRESS_REPORT_INTERVAL_IN_SECONDS = 10;

private readonly ISftpClient _sftpClient;
private readonly RsaKey _rsaKey;
private readonly PrivateKeyFile _privateKey;
private readonly PrivateKeyAuthenticationMethod _authenticationMethodRsa;
private readonly OctoLogger _log;
private readonly FileSystemProvider _fileSystemProvider;
private readonly object _mutex = new();
Expand All @@ -26,43 +21,7 @@ public BbsSshArchiveDownloader(OctoLogger log, FileSystemProvider fileSystemProv
{
_log = log;
_fileSystemProvider = fileSystemProvider;

_privateKey = new PrivateKeyFile(privateKeyFileFullPath);

if (IsRsaKey(_privateKey))
{
_rsaKey = UpdatePrivateKeyFileToRsaSha256(_privateKey);
_authenticationMethodRsa = new PrivateKeyAuthenticationMethod(sshUser, _privateKey);
var connection = new ConnectionInfo(host, sshPort, sshUser, _authenticationMethodRsa);
connection.HostKeyAlgorithms["rsa-sha2-256"] = data => new KeyHostAlgorithm("rsa-sha2-256", _rsaKey, data);
_sftpClient = new SftpClient(connection);
}
else
{
_sftpClient = new SftpClient(host, sshPort, sshUser, _privateKey);
}
}

private bool IsRsaKey(PrivateKeyFile privateKeyFile) => privateKeyFile.HostKey is KeyHostAlgorithm keyHostAlgorithm && keyHostAlgorithm.Key is RsaKey;

private RsaWithSha256SignatureKey UpdatePrivateKeyFileToRsaSha256(PrivateKeyFile privateKeyFile)
{
if ((privateKeyFile.HostKey as KeyHostAlgorithm).Key is not RsaKey oldRsaKey)
{
throw new ArgumentException("The private key file is not an RSA key.", nameof(privateKeyFile));
}

var rsaKey = new RsaWithSha256SignatureKey(oldRsaKey.Modulus, oldRsaKey.Exponent, oldRsaKey.D, oldRsaKey.P, oldRsaKey.Q, oldRsaKey.InverseQ);

var keyHostAlgorithm = new KeyHostAlgorithm(rsaKey.ToString(), rsaKey);

var hostKeyProperty = typeof(PrivateKeyFile).GetProperty(nameof(PrivateKeyFile.HostKey));
hostKeyProperty.SetValue(privateKeyFile, keyHostAlgorithm);

var keyField = typeof(PrivateKeyFile).GetField("_key", BindingFlags.NonPublic | BindingFlags.Instance);
keyField.SetValue(privateKeyFile, rsaKey);

return rsaKey;
_sftpClient = new SftpClient(host, sshPort, sshUser, new PrivateKeyFile(privateKeyFileFullPath));
}

internal BbsSshArchiveDownloader(OctoLogger log, FileSystemProvider fileSystemProvider, ISftpClient sftpClient)
Expand Down Expand Up @@ -125,7 +84,8 @@ private void LogProgress(ulong downloadedBytes, ulong totalBytes)
return;
}

_log.LogInformation($"Archive download in progress, {GetLogFriendlySize(downloadedBytes)} out of {GetLogFriendlySize(totalBytes)} ({GetPercentage(downloadedBytes, totalBytes)}) completed...");
_log.LogInformation(
$"Archive download in progress, {GetLogFriendlySize(downloadedBytes)} out of {GetLogFriendlySize(totalBytes)} ({GetPercentage(downloadedBytes, totalBytes)}) completed...");

_nextProgressReport = _nextProgressReport.AddSeconds(DOWNLOAD_PROGRESS_REPORT_INTERVAL_IN_SECONDS);
}
Expand Down Expand Up @@ -159,9 +119,6 @@ private string GetLogFriendlySize(ulong size)

public void Dispose()
{
(_sftpClient as IDisposable)?.Dispose();
(_rsaKey as IDisposable)?.Dispose();
(_authenticationMethodRsa as IDisposable)?.Dispose();
(_privateKey as IDisposable)?.Dispose();
_sftpClient?.Dispose();
}
}
2 changes: 1 addition & 1 deletion src/bbs2gh/bbs2gh.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="SMBLibrary" Version="1.5.0.1" />
<PackageReference Include="SSH.NET" Version="2020.0.2" />
<PackageReference Include="SSH.NET" Version="2024.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
Expand Down
Loading