Skip to content

Exception in SSH.NET 2024.2.0 when creating PrivateKeyFile array and passPhrase is an empty string #1572

@jverbosky

Description

@jverbosky

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

   at System.Formats.Asn1.AsnDecoder.ReadLength(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Int32& bytesConsumed)
   at System.Formats.Asn1.AsnDecoder.GetPrimitiveContentSpan(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Asn1Tag expectedTag, UniversalTagNumber tagNumber, Int32& bytesConsumed)
   at System.Formats.Asn1.AsnDecoder.GetIntegerContents(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Asn1Tag expectedTag, UniversalTagNumber tagNumber, Int32& bytesConsumed)
   at System.Formats.Asn1.AsnDecoder.ReadIntegerBytes(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Int32& bytesConsumed, Nullable`1 expectedTag)
   at System.Formats.Asn1.AsnReader.ReadInteger(Nullable`1 expectedTag)
   at Renci.SshNet.Security.RsaKey..ctor(Byte[] privateKeyData)
   at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase)
   at Renci.SshNet.PrivateKeyFile..ctor(String fileName, String passPhrase, String certificateFileName)
   at Renci.SshNet.PrivateKeyFile..ctor(String fileName, String passPhrase)
   at webdevHealthCheck_Services.SftpService.GetSftpConnectionInfo(String sftpServerOption) in E:\Reports\webdevHealthCheck\App\services\remote\SftpService.cs:line 73

Screenshot:
image

To Reproduce
Here's part a basic prototype to confirm the behavior - the call to create the PrivateKeyFile array is where the exception occurs:

static void Main(string[] args)
{
    ConnectionInfo connectionInfo = null;
    List<AuthenticationMethod> authenticationMethods = [];
    string fileName = @"C:\Prototypes\dotnet_sftp_ssh-key\keys\wfgSshAuthKey";
    string passPhrase = "";

    try
    {
        // bug in SSH.NET 2024.2.0 causing this to fail when _sshKeyPassphrase is an empty string
        // - tested 2024.0.0 - fine
        // - tested 2024.1.0 - fine
        // - tested 2024.2.0 - exception
        PrivateKeyFile[] keyFiles = [new(fileName, passPhrase)];
        authenticationMethods.Add(new PrivateKeyAuthenticationMethod(_sftpUsername, keyFiles));

        connectionInfo = new(_sftpServer, _sftpPort, _sftpUsername, authenticationMethods.ToArray());
    }
    catch (Exception ex)
    {
        _logService.LogActivity($"Exception constructing ConnectionInfo object...");
        _logService.LogActivity($"Exception: {ex.Message}");
    }

}

Screenshot - reproduce

image

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.

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