Skip to content

DOTNETCORE: connecting with private key file >> NotSupportedException at at System.Security.Cryptography.HMAC.HashFinal() #776

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

Closed
mmacagno opened this issue Jan 29, 2021 · 6 comments
Labels
Milestone

Comments

@mmacagno
Copy link

mmacagno commented Jan 29, 2021

DOTNETCORE 3.1, with SSH.NET

I can successfully connect to a remote host with username and private key using Filezilla.
The same credentials and private key used with SSH.NET give me an exception.

The Exception seems to be some internal issue and not sure what to do to address it.

Can anyone help?

Exception:

Accessing a hash algorithm by manipulating the HashName property is not supported on this platform. Instead, you must instantiate one of the supplied subtypes (such as HMACSHA1.)

" at System.Security.Cryptography.HMAC.HashFinal() in //src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HMAC.cs:line 68\r\n at Renci.SshNet.Security.Cryptography.HMACSHA1.HashFinal()\r\n at System.Security.Cryptography.HashAlgorithm.CaptureHashCodeAndReinitialize() in //src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithm.cs:line 115\r\n at Renci.SshNet.Session.SendMessage(Message message)\r\n at Renci.SshNet.Session.Connect()\r\n at Renci.SshNet.BaseClient.CreateAndConnectSession()\r\n at Renci.SshNet.BaseClient.Connect()\r\n at ... (my code)

Exception type:
{Name = "PlatformNotSupportedException" FullName = "System.PlatformNotSupportedException"} Assembly: {System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e} AssemblyQualifiedName: "System.PlatformNotSupportedException, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" Attributes: Public | Serializable | BeforeFieldInit BaseType: {Name = "NotSupportedException" FullName = "System.NotSupportedException"} Cache: {System.RuntimeType.RuntimeTypeCache} CacheIfExists: {System.RuntimeType.RuntimeTypeCache} ContainsGenericParameters: false CustomAttributes: Count = 4 DeclaredConstructors: {System.Reflection.ConstructorInfo[4]} DeclaredEvents: {System.Reflection.EventInfo[0]} DeclaredFields: {System.Reflection.FieldInfo[0]} DeclaredMembers: {System.Reflection.MemberInfo[4]} DeclaredMethods: {System.Reflection.MethodInfo[0]} DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__22} DeclaredProperties: {System.Reflection.PropertyInfo[0]} DeclaringMethod: '((System.RuntimeType)exception.GetType()).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' }

CODE:
` SftpClient client = null;
MemoryStream stream = null;
StreamWriter writer = null;

        try
        {
            stream = new MemoryStream();

            writer = new StreamWriter(stream);
            writer.Write(
                "-----BEGIN RSA PRIVATE KEY-----" + "\n" +
                "MIIEpAIBAAKCAQEAsR7i+1kbPfZj2foKjRzM1gpf0OcjD2cNP/wBmwGmOjdQoW5/" + "\n" +
                ...
                "KcsxONtrmHQGPR8Oh8v94eL2pAC0rZ3rXb9YwVMpWBDyR6WGrw9OeQ==" + "\n" +
                "-----END RSA PRIVATE KEY-----");
            writer.Flush();

            stream.Position = 0;

            var keys = new PrivateKeyFile[] { new PrivateKeyFile(stream) };
            using var client = new SftpClient(this.config.Host, this.config.Port, this.config.Username, this.config.Password);
            
            client.Connect();
            return client.ListDirectory(remoteDirectory);
        }
        catch (Exception exception)
        {
            this.logger.LogError(exception, $"Failed to list directory: {remoteDirectory}");
            return null;
        }
        finally
        {
            stream?.Close();
            writer?.Close();
            client?.Disconnect();
        }`
@mmacagno mmacagno changed the title dotnet core with private key file, getting NotSupportedException at at System.Security.Cryptography.HMAC.HashFinal() DOTNETCORE: connecting with private key file >> NotSupportedException at at System.Security.Cryptography.HMAC.HashFinal() Jan 29, 2021
@sfinkel
Copy link

sfinkel commented Sep 9, 2021

I am encountering a similar issue with password authentication. I think the interesting commonality here is that the call to base.HashFinal() is somehow falling back to the abstract class HMAC instead of the subclassed parent.

In my case, this problem only occurs with .NET Core. If I run the same code under .NET Framework, it works as expected.

@mmacagno
Copy link
Author

Interesting... maybe it's worth to cross post to dotnet core @ microsoft to see what's going on.

@mmacagno
Copy link
Author

I am encountering a similar issue with password authentication. I think the interesting commonality here is that the call to base.HashFinal() is somehow falling back to the abstract class HMAC instead of the subclassed parent.

In my case, this problem only occurs with .NET Core. If I run the same code under .NET Framework, it works as expected.

Did you find a solution yet? I am still stuck with DOTNETCORE and unfortunately SSH.NET seems to be no longer maintained by the author. There are so many pull requests stuck to be approved...

@mmacagno
Copy link
Author

I finally forked the repository and added the explicit support for .NET Core 3.1 and .NET 5.0 and was able to bypass this problem, due to .NET Core runtime issue (see above).

@darinkes
Copy link
Collaborator

darinkes commented Oct 26, 2021

Out of curiosity: I dont understand why should a library get built for netcoreapp?
From my understanding a library uses the lowest possible netstandard it can and other apps can use it. Regardless what .NET Core or .NET 5/6 Version they are using. Thats why the target got called "netcoreapp3.1", its for apps.

I have no issues using SSH.NET with .NET Core or .NET5/6 on Linux and Windows.

@Rob-Hague
Copy link
Collaborator

This is fixed in 2023.0.0 (by virtue of updated target framework support)

@WojciechNagorski WojciechNagorski added this to the 2023.0.0 milestone Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants