sshnet / sshnet/SSH.NET

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

Open
#1,630 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
4.4k
Forks
993
Avg merge
9d 21h
Merged PRs (30d)
1

Description

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)
   

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Renci.SshNet.Security.RsaKey..ctor and the PrivateKeyFile.Open path shown in the stack trace. Reproduce the failure with the PuTTY-generated private key and compare its encoding with the formats these entry points expect. Done means the supported key format loads successfully, or its unsupported status is clearly established and covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.