Skip to content

Support for rsa-sha2-256 and rsa-sha2-512 #825

Closed

Description

@jeson1981

Do you have plan to support for rsa-sha2-256 and rsa-sha2-512?

Activity

  1. ml054 commented on May 27, 2022

    @ml054

    If you are looking for temporary solution it might help:

    Note: it doesn't need to touch library code and uses latest version 2020.0.1 - so we need reflection to set props on PrivateKeyFile

     /// <summary>
        /// Based on https://github.com/sshnet/SSH.NET/blob/1d5d58e17c68a2f319c51e7f938ce6e964498bcc/src/Renci.SshNet/Security/Cryptography/RsaDigitalSignature.cs#L12
        ///
        /// With following changes:
        ///
        /// - OID changed to sha2-256
        /// - hash changed from sha1 to sha2-256
        /// </summary>
        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)
            {
                return _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;
                }
            }
    
            ~RsaSha256DigitalSignature()
            {
                Dispose(false);
            }
        }
    
     /// <summary>
        /// Utility class which allows ssh.net to connect to servers using ras-sha2-256
        /// </summary>
        public static class RsaSha256Util
        {
            public static void SetupConnection(ConnectionInfo connection)
            {
                connection.HostKeyAlgorithms["rsa-sha2-256"] = data => new KeyHostAlgorithm("rsa-sha2-256", new RsaKey(), data);
            }
    
            /// <summary>
            /// Converts key file to rsa key with sha2-256 signature
            /// Due to lack of constructor: https://github.com/sshnet/SSH.NET/blob/bc99ada7da3f05f50d9379f2644941d91d5bf05a/src/Renci.SshNet/PrivateKeyFile.cs#L86
            /// We do that in place
            /// </summary>
            /// <param name="keyFile"></param>
            /// <exception cref="ArgumentNullException"></exception>
            public static void ConvertToKeyWithSha256Signature(PrivateKeyFile keyFile)
            {
                var oldKeyHostAlgorithm = keyFile.HostKey as KeyHostAlgorithm;
                if (oldKeyHostAlgorithm == null)
                {
                    throw new ArgumentNullException(nameof(oldKeyHostAlgorithm));
                }
                var oldRsaKey = oldKeyHostAlgorithm.Key as RsaKey;
                if (oldRsaKey == null)
                {
                    throw new ArgumentNullException(nameof(oldRsaKey));
                }
                
                var newRsaKey = new RsaWithSha256SignatureKey(oldRsaKey.Modulus, oldRsaKey.Exponent, oldRsaKey.D, oldRsaKey.P, oldRsaKey.Q,
                    oldRsaKey.InverseQ);
    
                UpdatePrivateKeyFile(keyFile, newRsaKey);
            }
    
            private static void UpdatePrivateKeyFile(PrivateKeyFile keyFile, RsaWithSha256SignatureKey key)
            {
                var keyHostAlgorithm = new KeyHostAlgorithm(key.ToString(), key);
                
                var hostKeyProperty = typeof(PrivateKeyFile).GetProperty(nameof(PrivateKeyFile.HostKey));
                hostKeyProperty.SetValue(keyFile, keyHostAlgorithm);
    
                var keyField = typeof(PrivateKeyFile).GetField("_key", BindingFlags.NonPublic | BindingFlags.Instance);
                keyField.SetValue(keyFile, key);
            }
        }
    
    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
                {
                    if (_digitalSignature == null)
                    {
                        _digitalSignature = new RsaSha256DigitalSignature(this);
                    }
    
                    return _digitalSignature;
                }
            }
    
            public override string ToString()
            {
                return "rsa-sha2-256";
            }
        }
    

    Usage:

     [Fact]
            public void CanConnectUsingRsaKeyAndSha256Signature()
            {
                var user = "ml054";
                var host = "192.168.2.154";
    
                var regularKey = File.ReadAllBytes("c:\\temp\\my_rsa_key");
                var pkRsa = new PrivateKeyFile(new MemoryStream(regularKey));
    
                RsaSha256Util.ConvertToKeyWithSha256Signature(pkRsa);
    
                using (var authenticationMethodRsa = new PrivateKeyAuthenticationMethod(user, pkRsa))
                {
                    var conn = new ConnectionInfo(host, user, authenticationMethodRsa);
                    RsaSha256Util.SetupConnection(conn); // adds rsa-sha2-256
    
                    using (var sshClient = new SshClient(conn))
                    {
                        sshClient.Connect();
                        using (var command = sshClient.RunCommand("ls"))
                        {
                            Console.WriteLine(command.Result);
                        }
                    }
                }
            }
    
  2. ml054 commented on Jun 1, 2022

    @ml054
  3. nicholi commented on Jun 22, 2022

    @nicholi

    Thanks for this workaround, works great.

  4. superlazycoder commented on Jul 20, 2022

    @superlazycoder

    Thank you so much for this. I was pulling my hair out.

    Score 1 for open source.

  5. added a commit that references this issue on Nov 1, 2022
  6. lcheng526 commented on Jan 30, 2023

    @lcheng526

    I have implemented the workaround solution which is provided in this

    Thanks for this workaround, works great.

    The workaround solution does not work for me. I am getting "Key Exchange negotiation failed" exception for rsa-sha2-256.

    please help me out?

  7. lcheng526 commented on Jan 30, 2023

    @lcheng526

    The workaround solution does not work for me. I am getting "Key Exchange negotiation failed" exception for rsa-sha2-256.

    please help me out?

  8. ml054 commented on Jan 30, 2023

    @ml054

    @lcheng526 How about this: #971 ? Did you give a try? If it doesn't work. You can always play with debugger and try to fix by yourself. Believe me it is great adventure to play with opensource library and fix such issues. Good luck. :)

  9. lcheng526 commented on Jan 31, 2023

    @lcheng526

    @lcheng526 How about this: #971 ? Did you give a try? If it doesn't work. You can always play with debugger and try to fix by yourself. Believe me it is great adventure to play with opensource library and fix such issues. Good luck. :)

    I thought the workaround solution which is provided #825 (comment) should just work.

  10. lcheng526 commented on Jan 31, 2023

    @lcheng526

    @lcheng526 How about this: #971 ? Did you give a try? If it doesn't work. You can always play with debugger and try to fix by yourself. Believe me it is great adventure to play with opensource library and fix such issues. Good luck. :)

    In the workaround solution, it constructs PrivateKeyFile() based on a key file, is this a rsa-sha2-256 openssh private key file or it is a normal rsa key?

    var regularKey = File.ReadAllBytes("c:\temp\my_rsa_key");
    var pkRsa = new PrivateKeyFile(new MemoryStream(regularKey));

  11. lcheng526 commented on Jan 31, 2023

    @lcheng526

    I am getting "Key Exchange negotiation failed" exception

    do you need to make any changes to the workaround solution? I am getting "Key Exchange negotiation failed" exception with the workaround exception. any ideas?

  12. lcheng526 commented on Feb 2, 2023

    @lcheng526

    Didn't check that - however if you manage to find out - please share with others on github.
    …
    On Wed, Jun 1, 2022 at 2:08 PM jhonson27 @.> wrote: If you are looking for temporary solution it might help: Note: it doesn't need to touch library code and uses latest version 2020.0.1 - so we need reflection to set props on PrivateKeyFile ///

    /// Based on
    public class RsaDigitalSignature : CipherDigitalSignature, IDisposable
    /// /// With following changes: /// /// - OID changed to sha2-256 /// - hash changed from sha1 to 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) { return _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; } } ~RsaSha256DigitalSignature() { Dispose(false); } } /// /// Utility class which allows ssh.net to connect to servers using ras-sha2-256 /// public static class RsaSha256Util { public static void SetupConnection(ConnectionInfo connection) { connection.HostKeyAlgorithms["rsa-sha2-256"] = data => new KeyHostAlgorithm("rsa-sha2-256", new RsaKey(), data); } /// /// Converts key file to rsa key with sha2-256 signature /// Due to lack of constructor:
    public PrivateKeyFile(Key key)
    /// We do that in place ///
    /// /// public static void ConvertToKeyWithSha256Signature(PrivateKeyFile keyFile) { var oldKeyHostAlgorithm = keyFile.HostKey as KeyHostAlgorithm; if (oldKeyHostAlgorithm == null) { throw new ArgumentNullException(nameof(oldKeyHostAlgorithm)); } var oldRsaKey = oldKeyHostAlgorithm.Key as RsaKey; if (oldRsaKey == null) { throw new ArgumentNullException(nameof(oldRsaKey)); } var newRsaKey = new RsaWithSha256SignatureKey(oldRsaKey.Modulus, oldRsaKey.Exponent, oldRsaKey.D, oldRsaKey.P, oldRsaKey.Q, oldRsaKey.InverseQ); UpdatePrivateKeyFile(keyFile, newRsaKey); } private static void UpdatePrivateKeyFile(PrivateKeyFile keyFile, RsaWithSha256SignatureKey key) { var keyHostAlgorithm = new KeyHostAlgorithm(key.ToString(), key); var hostKeyProperty = typeof(PrivateKeyFile).GetProperty(nameof(PrivateKeyFile.HostKey)); hostKeyProperty.SetValue(keyFile, keyHostAlgorithm); var keyField = typeof(PrivateKeyFile).GetField("_key", BindingFlags.NonPublic | BindingFlags.Instance); keyField.SetValue(keyFile, key); } } 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 { if (_digitalSignature == null) { _digitalSignature = new RsaSha256DigitalSignature(this); } return _digitalSignature; } } public override string ToString() { return "rsa-sha2-256"; } } Usage: [Fact] public void CanConnectUsingRsaKeyAndSha256Signature() { var user = "ml054"; var host = "192.168.2.154"; var regularKey = File.ReadAllBytes("c:\temp\my_rsa_key"); var pkRsa = new PrivateKeyFile(new MemoryStream(regularKey)); RsaSha256Util.ConvertToKeyWithSha256Signature(pkRsa); using (var authenticationMethodRsa = new PrivateKeyAuthenticationMethod(user, pkRsa)) { var conn = new ConnectionInfo(host, user, authenticationMethodRsa); RsaSha256Util.SetupConnection(conn); // adds rsa-sha2-256 using (var sshClient = new SshClient(conn)) { sshClient.Connect(); using (var command = sshClient.RunCommand("ls")) { Console.WriteLine(command.Result); } } } } @ml054 https://github.com/ml054 can something similar be used in other authentication methods ? I am having issues with host key algorithm not supported when connecting with user name and password using KeyboardInteractiveAuthenticationMethod host supports rsa-sha2-512,rsa-sha2-256 Thanks — Reply to this email directly, view it on GitHub <#825 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJRI75K7W5P6ASR23LK3ITVM5HE7ANCNFSM45BTP6LA . You are receiving this because you were mentioned.Message ID: <sshnet/SSH. @.
    >
    -- https://ravendb.net/ Marcin Lewandowski Team Leader / Hibernating Rhinos LTD https://hibernatingrhinos.com/ Sales: @.*** Skype: ml054 Support: @.*** https://www.facebook.com/pages/RavenDB/265907650186374 https://twitter.com/ravendb https://www.linkedin.com/groups/RavenDB-4499263

    Thank you so much for this. I was pulling my hair out.

    Score 1 for open source.

    Should I generate a sha1 key to test out this workaround solution? thanks

  13. lcheng526 commented on Feb 2, 2023

    @lcheng526

    Should I generate a sha1 key to test out this workaround solution? thanks

    Did you generate a sha1 key to test out the workaround solution? thanks

  14. vvda1984 commented on Mar 2, 2023

    @vvda1984

    Following code work

    var sshKeyContent = File.ReadAllBytes(sshKeyFile);
    var pkRsa = new PrivateKeyFile(new MemoryStream(sshKeyContent), sskKeyPassword);

    var authenticationMethodRsa = new PrivateKeyAuthenticationMethod(user, pkRsa);
    methods.Add(authenticationMethodRsa);

    var conn = new ConnectionInfo(host, port, user, methods.ToArray());
    conn.HostKeyAlgorithms.Add("rsa-sha2-256", data =>
    {
    return new KeyHostAlgorithm("rsa-sha2-256", new RsaWithSha256SignatureKey(), data);
    });
    conn.HostKeyAlgorithms["ssh-rsa"] = data =>
    {
    return new KeyHostAlgorithm("ssh-rsa", new RsaWithSha256SignatureKey(), data);
    };
    var client = new SftpClient(conn);
    client.Connect();

  15. masherak commented on Mar 29, 2023

    @masherak

    Workaround works, is there plan to update library?

  16. 11 remaining items

  17. tate98 commented on Aug 14, 2023

    @tate98

    @lcheng526
    I assume that's what you are looking for: #825 (comment)

    I am getting "Permission denied (public key)" exception. any ideas?

    @lcheng526 did you fix this exception? I am getting the same one.

  18. added this to the 2023.0.0 milestone on Sep 13, 2023
  19. igavva commented on Sep 19, 2023

    @igavva

    A simple solution.

    Add this line in /etc/ssh/sshd_config.

    PubkeyAcceptedAlgorithms +ssh-rsa
    Afterwards, restart the sshd service to make the new settings come into effect.

    $ sudo systemctl restart sshd

  20. WojciechNagorski commented on Oct 10, 2023

    @WojciechNagorski
    Collaborator
  21. timrogers commented on Oct 24, 2023

    @timrogers

    I've tried updating to the new version and removing the workaround code above, and it doesn't seem to work with these keys. Anyone else with the same issue?

  22. masherak commented on Oct 24, 2023

    @masherak

    I've tried updating to the new version and removing the workaround code above, and it doesn't seem to work with these keys. Anyone else with the same issue?

    I am fine after update and remove workaround above...

  23. Rob-Hague commented on Oct 24, 2023

    @Rob-Hague
    Collaborator

    @timrogers are you saying it worked with the workaround on the old version 2020.0.2, but does not work without the workaround on the new version 2023.0.0? That would be very unexpected. Any more details or code samples you can provide?

  24. timrogers commented on Oct 26, 2023

    @timrogers

    @timrogers are you saying it worked with the workaround on the old version 2020.0.2, but does not work without the workaround on the new version 2023.0.0? That would be very unexpected. Any more details or code samples you can provide?

    That is what I'm saying - but it's definitely possible that I did something wrong, and I don't have a minimal reproduction of the issue. I will look into this in more detail.

  25. vsvsav commented on Feb 27, 2024

    @vsvsav

    v2024.0.0 - the issue remains for partnerupload.google.com...

    PS
    It works! But key must be converted to OpenSSH key, annoying issue

  26. DamianSuess commented on Mar 7, 2024

    @DamianSuess

    After upgrading to 2023.0 and 2024.0, the implementation of the workaround has been throwing errors on our Visual Studio extension.

    From reading the comments by @timrogers and @masherak, does this mean I should remove this RsaSha256Util.ConvertToKeyWithSha256Signature(keyFile); ... and RsaSha256Util.SetupConnection(conn) altogether? and use the ...

    public static void SetupConnection(ConnectionInfo connection)
      connection.HostKeyAlgorithms.Clear();
      connection.HostKeyAlgorithms.Add("rsa-sha2-256", (data) => { return new KeyHostAlgorithm("rsa-sha2-256", new RsaWithSha256SignatureKey(), data); });

    The one issue is that the workaround post doesn't have a RsaWithSha256SignatureKey class with an empty constructor. Just want to make sure what I should correctly pass in.

  27. Rob-Hague commented on Mar 7, 2024

    @Rob-Hague
    Collaborator

    Yes you should delete the workaround completely and use 2023.0.1 or higher

    edit: There is no need to modify the HostKeyAlgorithms

  28. DamianSuess commented on Mar 7, 2024

    @DamianSuess

    Thank you, @Rob-Hague for the timely response. I'll give it a shot.

    Ever since the upgrade to Visual Studio v17.9 our extension has been throwing errors, and didn't see anything reported on "breaking changes". Gotta love it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions