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

CryptographicException in BaseClient.Connect() #805

Open
IgorMilavec opened this issue Apr 14, 2021 · 2 comments
Open

CryptographicException in BaseClient.Connect() #805

IgorMilavec opened this issue Apr 14, 2021 · 2 comments
Labels

Comments

@IgorMilavec
Copy link
Collaborator

Using SSH.NET 2020.0.1, occasionally CryptographicException is thrown in call to BaseClient.Connect():

System.Security.Cryptography.CryptographicException: Hash not valid for use in specified state.
   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 cbData, Int32 ibStart, Int32 cbSize)
   at System.Security.Cryptography.SHA1CryptoServiceProvider.HashCore(Byte[] rgb, Int32 ibStart, Int32 cbSize)
   at System.Security.Cryptography.HashAlgorithm.ComputeHash(Byte[] buffer)
   at Renci.SshNet.Security.Cryptography.CipherDigitalSignature.Sign(Byte[] input)
   at Renci.SshNet.Security.KeyHostAlgorithm.Sign(Byte[] data)
   at Renci.SshNet.PrivateKeyAuthenticationMethod.Authenticate(Session session)
   at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException)
   at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session)
   at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.CreateAndConnectSession()
   at Renci.SshNet.BaseClient.Connect()

Will try to gather more info and report back.

@IgorMilavec
Copy link
Collaborator Author

When simultaneously trying to call SftpClient.Connect on multiple instances of SftpClient, there seems to be a race condition causing either the above exception or this exception to be thrown:

System.ArgumentException: data
   at Renci.SshNet.Security.Cryptography.BlockCipher.Encrypt(Byte[] data, Int32 offset, Int32 length)
   at Renci.SshNet.Session.SendMessage(Message message)
   at Renci.SshNet.PasswordAuthenticationMethod.Authenticate(Session session)
   at Renci.SshNet.AuthenticationMethod.Renci.SshNet.IAuthenticationMethod.Authenticate(ISession session)
   at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException)
   at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session)
   at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.CreateAndConnectSession()
   at Renci.SshNet.BaseClient.Connect()

This last exception is actually wrong and I have created #818 to fix it.

Finding the root cause for this race is a work in progress.

@gregor-tusar-sowa
Copy link

If it helps:
My solution is running a Hangfire server with multiple jobs that try to do something with the files on SFTP.
When jobs run at the same time, the Connect() seems to be called simultaneously so this exception is thrown. The SftpClient is defined as a Singleton service in a DI container.

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

2 participants