Skip to content

Invalid private key file for BouncyCastle generated EC private key file #1536

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
wech71 opened this issue Nov 13, 2024 · 4 comments · Fixed by #179
Closed

Invalid private key file for BouncyCastle generated EC private key file #1536

wech71 opened this issue Nov 13, 2024 · 4 comments · Fixed by #179

Comments

@wech71
Copy link

wech71 commented Nov 13, 2024

BouncyCastle create PEM files for EC-keys with DEK-Info salt in lowercase, which prevents the PrivateKeyPattern to match the key.

This results in a SshException("Invalid private key file.") when trying to load the private key file.

https://github.com/sshnet/SSH.NET/blob/6b4524efbd84e3bf5f8f670a81cf1793484e9d24/src/Renci.SshNet/PrivateKeyFile.cs#L122C1-L123C1

Example:

-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,d87771503957057a

This could be fixed by changing
DEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)
to
DEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[a-fA-F0-9]+)

Fixed line would be

private const string PrivateKeyPattern = @"^-+ *BEGIN (?<keyName>\w+( \w+)*) *-+\r?\n((Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[a-zA-F0-9]+)\r?\n\r?\n)|(Comment: ""?[^\r\n]*""?\r?\n))?(?<data>([a-zA-Z0-9/+=]{1,80}\r?\n)+)(\r?\n)?-+ *END \k<keyName> *-+";
@Rob-Hague
Copy link
Collaborator

Thanks, there is actually a very old PR for this: #179 I will get it merged when I next find time

@wech71
Copy link
Author

wech71 commented Nov 13, 2024

Thanks.

For the moment I have a workaround - I just regex-replace the DEK-Info-Line and make the salt uppercase before I pass it to the PrivateKey constructor :-D

Unfortunately the EC private key makes problem while parsing the ANS1 data. I will have to investigate the problem and open another ticket when I find out what's wrong.
[edit] deleted, because, the problem was in my code

@Rob-Hague Rob-Hague linked a pull request Nov 18, 2024 that will close this issue
@Rob-Hague
Copy link
Collaborator

I merged that PR but I did note that

a) BouncyCastle outputs the salt in uppercase these days (https://github.com/bcgit/bc-csharp/blob/63b2e6e5bd596acea200f70c0db8e376f958806b/crypto/src/openssl/MiscPemGenerator.cs#L252); and
b) you are using a rather old cipher scheme

@wech71
Copy link
Author

wech71 commented Nov 19, 2024

Thank you.

I was (accidentally) using Portable.BouncyCastle 1.9.0, because another NuGet packaged depends on it. There are pretty many projects out there which still use Portable Bouncy Castle because it seems to work better with Unity.

And 1.9.0 created the lowercase salt.

I'm now rename the Portable.BouncyCastle package in my project, so I can use BouncyCastle.Cryptography 2.4.2 which in fact produces the salt in uppercase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants