Skip to content

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

Closed
@wech71

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

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