Skip to content

X509Certificate2 class unable to read Subject data from some certificates on WSL/Docker containers/Linux #88037

Closed

Description

@afdsa213

Description

After upgrading our company projects from .net core 3.1 to .net 6, X509Certificate2 class is unable to read data from certificates issued by two different polish electronic signature providers (KIR and Cencert). On windows everything works fine but on linux-based environments (so far tested on docker containers and WSL), Subject property always contains empty string.

Reproduction Steps

  1. Clone repository from https://github.com/afdsa213/certs-test into linux/wsl environment with .net sdk 6 installed,
  2. run xUnit tests inside X509Certificate2Tests class,
  3. both certificates fail on Assert.NotEmpty(subject)

Repository contains two certificates in DER format.
Screenshot 2023-06-26 114704

Expected behavior

Subject property of certificate object contains string with fields like CN, O, G, SN, C, etc.

Actual behavior

Subject property contains empty string.

Regression?

Worked fine in .net core 3.1 docker containers.

Known Workarounds

Writing custom parser, using AsnReader class (?) - but that seems like a lot of work.

Configuration

Tested on:

  1. WSL2 running Ubuntu 22.04.2 LTS with .net sdk 6.0.119,
  2. alpine linux docker containers with installed .net 6 runtime

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jun 26, 2023
  2. vcsjones commented on Jun 26, 2023

    @vcsjones
    Member

    I can reproduce it on .NET 8 nightly, and nothing jumps out at me as "invalid text according to the ASN.1 tag"

    I'll dig in.

  3. self-assigned this
    on Jun 26, 2023
  4. vcsjones commented on Jun 26, 2023

    @vcsjones
    Member

    The issue is due to the postalAddress in the distinguished name as seen here:

    Sequence: Length: 38 Offset: 0
        ObjectIdentifier: 2.5.4.16, Length: 5 Offset: 2
        Sequence: Length: 31 Offset: 7
            UTF8String: Kolska 12, Length: 11 Offset: 9
            UTF8String: 01-045, Length: 8 Offset: 20
            UTF8String: Warszawa, Length: 10 Offset: 28
    

    Our managed decoder more or less requires the AttributeValue of a certificate to be a DirectoryString:

    https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X500NameEncoder.ManagedDecode.cs#L94-L95

    Now, X.520 defines postalAddress as

    postalAddress ATTRIBUTE ::= {
      WITH SYNTAX              PostalAddress
      EQUALITY MATCHING RULE   caseIgnoreListMatch
      SUBSTRINGS MATCHING RULE caseIgnoreListSubstringsMatch
      LDAP-SYNTAX              postalAddr.&id
      LDAP-NAME                {"postalAddress"}
      ID                       id-at-postalAddress }
    
    PostalAddress ::= SEQUENCE SIZE (1..MAX) OF UnboundedDirectoryString
    

    So, this is "correct". The certificate is not malformed, it just uses a directory type that the managed decoder doesn't handle at all.

    That we haven't run in to this yet so far indicates that most applications expect the AttributeValue to be a directory string. MacOS, for example, is completely unwilling to parse this certificate.

    image

    Windows on the other hand fairs a bit better, it just shows it as hex-encoded.

    image

    I think it's sensible to do what Windows is doing here and display it as hex when we encounter a non-string AttributeValue.

    The next logical question is: "Why did this used to work in .NET Core 3.1?"

    In .NET 5, the distinguished name decoder was changed from using OpenSSL's APIs to a managed implementation.

  5. ghost added
    in-pr
    on Jun 28, 2023
  6. ghost removed
    in-pr
    on Jun 29, 2023
  7. ghost removed
    untriaged
    on Jun 29, 2023
  8. ghost locked as resolved and limited conversation to collaborators on Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions