-
-
Notifications
You must be signed in to change notification settings - Fork 940
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
Exception in SSH.NET 2024.2.0 when creating PrivateKeyFile array and passPhrase is an empty string #1572
Comments
Perhaps we should be reading BER @scott-xu ? diff --git a/src/Renci.SshNet/Security/Cryptography/RsaKey.cs b/src/Renci.SshNet/Security/Cryptography/RsaKey.cs
index e1456ea6..a95b25f1 100644
--- a/src/Renci.SshNet/Security/Cryptography/RsaKey.cs
+++ b/src/Renci.SshNet/Security/Cryptography/RsaKey.cs
@@ -161,7 +161,7 @@ public RsaKey(byte[] privateKeyData)
{
ThrowHelper.ThrowIfNull(privateKeyData);
- var der = new AsnReader(privateKeyData, AsnEncodingRules.DER).ReadSequence();
+ var der = new AsnReader(privateKeyData, AsnEncodingRules.BER).ReadSequence();
_ = der.ReadInteger(); // skip version
Modulus = der.ReadInteger(); Is there anywhere that mandates DER rather than just BER for PKCS#1? I don't see anything here: P.S. I updated the top post to wrap blocks in triple ticks ` per https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks |
@jverbosky perhaps you can build this repo with that change above and see if it works for you? Changes were made in this area for 2024.2.0 Do you know how your key was generated i.e. which program? |
@Rob-Hague apologies for my delayed reply, been busy and had to research how to accomplish what you suggested. I used a program called VShell for generating the SSH key: In case it's any help, here's my notes from when I created this key:
FWIW, I'm using other SSH keys (that use passwords) created by this app along with SSH.NET, and am able to connect to the respective SFTP servers fine - issue is just with this 'password-less' SSH key. I was able to build a new DLL from the SSH.NET source code with the 'BER' change and figured out how to include it in my project. But when I run my app and try connecting using the 'password-less' SSH key, I get the following exception:
The only change I made was to swap to 'BER', so not sure if I might be missing something during the build or if there might be a dependency on DER deeper in SSH.NET - any suggestions? Reference Screenshots Thanks for your help! John |
@Rob-Hague well I was re-reading my last post and noticed the 'Message' value in the final screenshot mentioned not being able to load BouncyCastle.Cryptography. So I tried adding that to the project file and tried running the app, and it connected to the SFTP server and listed some test files just fine now! So, the fix (for authentication using an SSH key without a password) appears to be the change that you mentioned earlier (DER ~> BER) plus the BouncyCastle.Cryptography package (never had to include that before, FWIW). Please advise if you need any additional details - otherwise, hoping a new 'option' (DER or BER) can be added to a future SSH.NET release. Thanks again for your help! John |
@jverbosky I can't reproduce the issue. See #1579 |
@scott-xu I spent this morning reviewing my notes and creating & testing new keys (same config as old key), and I'm not seeing the 'encoded length' exception with any of the new keys either. So at this point, I've created a new key pair, verified them with a test SFTP server (connecting fine, no SSH.NET exceptions), and have reached out to our vendor about replacing the key on their server with one of the new ones that I created & tested today. @Rob-Hague My apologies for the trouble, but at this point I think it's going to be easier to side-step the exception that I reported and simply replace the key, considering all of the new keys that I've created are working fine for instantiating the ConnectionInfo object (as scott-xu advised). I reviewed my notes, emails, etc. again and there's no indication anywhere that I needed to 'tweak' the vkeygen command that I shared, and the 'problem key looks fairly identical to the new keys (with regards to header & length), so I'm not sure it's worth additional time to determine how it's different or why it's triggering the exception. Thank you both for your time & help - if nothing else, I've learned how to build SSH.NET from source and reference the DLL in my project (never had to do that before)! John |
No problem. Thanks for the useful feedback and for investigating on your end. |
Description
Started getting an exception in v2024.2.0 when attempting to create a PrivateKeyFile array using a keyfile with an empty password - this configuration works fine in v2024.1.0 & below and has been used in production with a vendor's SFTP server for 2+ years.
Exception Message
The encoded length is not valid under the requested encoding rules, the value may be valid under the BER encoding.
StackTrace
Screenshot:
![image](https://private-user-images.githubusercontent.com/23510038/402654295-e5296aa2-f271-42bb-bba7-30c85cc774b4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxODc1NjgsIm5iZiI6MTczOTE4NzI2OCwicGF0aCI6Ii8yMzUxMDAzOC80MDI2NTQyOTUtZTUyOTZhYTItZjI3MS00MmJiLWJiYTctMzBjODVjYzc3NGI0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEwVDExMzQyOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTMyMzU5YmMyMTliYmI3MWUwODMyNWJjMGZlZmRlZDdhMDFiNWUxMjNiYWQ3YzZiNWYyODdhMzcyNTBjMmVmY2YmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.LSYAgYesFgZ5EwY860TSDBT11BxbkpALBo2nOZZR3Zc)
To Reproduce
Here's part a basic prototype to confirm the behavior - the call to create the PrivateKeyFile array is where the exception occurs:
Screenshot - reproduce
Expected behavior
In v2024.1.0 and below, a PrivateKeyFile array can be created using an empty string for the passPhrase value, and subsequently used to create a new ConnectionInfo object which can be used to create a new SftpClient instance.
Thank you for your help and please advise if you need any more details to reproduce.
The text was updated successfully, but these errors were encountered: