diff --git a/src/bbs2gh/RsaWithSha256SignatureKey.cs b/src/bbs2gh/RsaWithSha256SignatureKey.cs deleted file mode 100644 index 7589efbd1..000000000 --- a/src/bbs2gh/RsaWithSha256SignatureKey.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.Security.Cryptography; -using Renci.SshNet.Common; -using Renci.SshNet.Security; -using Renci.SshNet.Security.Cryptography; -using Renci.SshNet.Security.Cryptography.Ciphers; - -namespace OctoshiftCLI.BbsToGithub; - -// workaround for RSA keys on Ubuntu 22.04 -// https://github.com/sshnet/SSH.NET/issues/825#issuecomment-1139440419 - -public class RsaWithSha256SignatureKey : RsaKey -{ - public RsaWithSha256SignatureKey(BigInteger modulus, BigInteger exponent, BigInteger d, BigInteger p, BigInteger q, - BigInteger inverseQ) : base(modulus, exponent, d, p, q, inverseQ) - { - } - - private RsaSha256DigitalSignature _digitalSignature; - - protected override DigitalSignature DigitalSignature - { - get - { - _digitalSignature ??= new RsaSha256DigitalSignature(this); - - return _digitalSignature; - } - } - - public override string ToString() => "rsa-sha2-256"; -} - -public class RsaSha256DigitalSignature : CipherDigitalSignature, IDisposable -{ - private HashAlgorithm _hash; - - public RsaSha256DigitalSignature(RsaWithSha256SignatureKey rsaKey) - // custom OID - : base(new ObjectIdentifier(2, 16, 840, 1, 101, 3, 4, 2, 1), new RsaCipher(rsaKey)) - { - // custom - _hash = SHA256.Create(); - } - - protected override byte[] Hash(byte[] input) => _hash.ComputeHash(input); - - private bool _isDisposed; - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (_isDisposed) - { - return; - } - - if (disposing) - { - var hash = _hash; - if (hash != null) - { - hash.Dispose(); - _hash = null; - } - - _isDisposed = true; - } - } -} diff --git a/src/bbs2gh/Services/BbsSshArchiveDownloader.cs b/src/bbs2gh/Services/BbsSshArchiveDownloader.cs index ba71b24c0..d9901c4b4 100644 --- a/src/bbs2gh/Services/BbsSshArchiveDownloader.cs +++ b/src/bbs2gh/Services/BbsSshArchiveDownloader.cs @@ -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; @@ -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(); @@ -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) @@ -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); } @@ -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(); } } diff --git a/src/bbs2gh/bbs2gh.csproj b/src/bbs2gh/bbs2gh.csproj index 6a46031e8..1c25ff12a 100644 --- a/src/bbs2gh/bbs2gh.csproj +++ b/src/bbs2gh/bbs2gh.csproj @@ -13,7 +13,7 @@ - +