Skip to content

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

Closed

Description

@wech71

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> *-+";

Activity

  1. Rob-Hague commented on Nov 13, 2024

    @Rob-Hague
    Collaborator

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

  2. wech71 commented on Nov 13, 2024

    @wech71
    Author

    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

  3. Rob-Hague commented on Nov 19, 2024

    @Rob-Hague
    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

  4. wech71 commented on Nov 19, 2024

    @wech71
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions