SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

System.Formats.Asn1.AsnContentException: 'The encoded length exceeds the maximum supported by this library (Int32.MaxValue).'

Open lesvaches opened this issue 6 months ago • 3 comments

System.Formats.Asn1.AsnContentException occurs when loading a private key file generated by PuTTY key generator.

PrivateKeyFile[] keyFiles = new[] { new PrivateKeyFile(PrivateKeyFilePath, PrivateKeyFilePassword) };

System.Formats.Asn1.AsnContentException
  HResult=0x80131500
  Message=The encoded length exceeds the maximum supported by this library (Int32.MaxValue).
  Source=System.Formats.Asn1
  StackTrace:
   at System.Formats.Asn1.AsnDecoder.ReadLength(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Int32& bytesConsumed)
   at System.Formats.Asn1.AsnDecoder.ReadTagAndLength(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Nullable`1& contentsLength, Int32& bytesRead)
   at System.Formats.Asn1.AsnDecoder.ReadSequence(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Int32& contentOffset, Int32& contentLength, Int32& bytesConsumed, Nullable`1 expectedTag)
   at System.Formats.Asn1.AsnReader.ReadSequence(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)

Changing the key produces a new error:

System.Formats.Asn1.AsnContentException: 'The provided data is tagged with 'ContextSpecific' class value '14', but it should have been 'Universal' class value '16'.'

   at System.Formats.Asn1.AsnDecoder.CheckExpectedTag(Asn1Tag tag, Asn1Tag expectedTag, UniversalTagNumber tagNumber)
   at System.Formats.Asn1.AsnDecoder.ReadSequence(ReadOnlySpan`1 source, AsnEncodingRules ruleSet, Int32& contentOffset, Int32& contentLength, Int32& bytesConsumed, Nullable`1 expectedTag)
   at System.Formats.Asn1.AsnReader.ReadSequence(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)
   

lesvaches avatar Apr 16 '25 10:04 lesvaches