Skip to content

Add X509SubjectAlternativeName as a rich type #22699

Closed

Description

@bartonjs

SubjectAltName is an extension from RFC 3280 that's fairly important to the people who need it. Currently applications make programmatic decisions for it via ToString() which has a couple of problems:

  • It's localized on Windows
  • It's not the same on Windows and !Windows.

Rather than exposing the whole GeneralName concept (and dealing with X400Address) the type would

  • Exist
  • Allow enumerating the DNSName entries
  • Maybe allow enumerating the IPAddress entries
  • Probably have a MatchesHostname(string) method
  • Maybe have a MatchesIPAddress(IPAddress) method.

It might make sense to put MatchesHostname on X509Certificate2, just to make things easier.


API Proposal

namespace System.Security.Cryptography.X509Certificates
{
    public partial class X509Certificate2
    {
        // Performs RFC 6125 matching of a hostname against a certificate and the
        // Subject Alternative Names.
        //
        // Uses IPAddress.TryParse to determine DNS match vs IP match
        public bool MatchesHostname(string hostname) { throw null; }
        public bool MatchesHostname(string hostname, bool allowWildcards = true, bool allowCommonName = true) { throw null; }
    }

    public sealed class X509SubjectAlternativeNameExtension : X509Extension
    {
        public X509SubjectAlternativeNameExtension() { }
        public X509SubjectAlternativeNameExtension(byte[] rawData, bool critical = false) { }
        public X509SubjectAlternativeNameExtension(ReadOnlySpan<byte> rawData, bool critical = false) { }

        public IEnumerable<string> EnumerateDnsNames() { throw null; }
        public IEnumerable<IPAddress> EnumerateIPAddresses() { throw null; }
    }
}

Activity

  1. morganbr commented on Jul 11, 2017

    @morganbr
    Contributor

    What should they do if the SubjectAltName contains a URI, email address, or other annoying kind of name?

  2. bartonjs commented on Jul 11, 2017

    @bartonjs
    MemberAuthor

    A EnumerateDnsNames() method would skip them, as would EnumerateIPAddresses().

    If someone wanted to read any kind of extra name they'd need to use #22610.

  3. danmoseley commented on Dec 1, 2017

    @danmoseley
    Contributor

    @bartonjs estimates 1 week remaining

  4. transferred this issue fromdotnet/corefxon Jan 31, 2020
  5. added this to the 5.0 milestone on Jan 31, 2020
  6. added
    untriaged
    on Feb 23, 2020
  7. bartonjs commented on Jul 7, 2020

    @bartonjs
    MemberAuthor

    Related to #28771. (This issue focuses more on "make the extension type for the cert.Extensions collection", that issue focuses more on "be able to answer the question from a certificate").

  8. modified the milestones: 5.0.0, Future on Jul 7, 2020
  9. removed
    untriaged
    on Jul 7, 2020
  10. added
    api-suggestion
    and removed
    api-needs-work
    on Jan 27, 2022
  11. stephenjust commented on Jan 27, 2022

    @stephenjust

    Would it make sense to support enumerating each of the names that SubjectAlternativeNameBuilder allows you to specify? I do think DnsName and IP Address cover the most common use cases, but from a consistency point of view it seems like we should at least support easily reading the values that we can write.

  12. bartonjs commented on Jan 27, 2022

    @bartonjs
    MemberAuthor

    I can understand it from a consistency perspective, but what's the scenario? Assuming we go the typed enumerators route (EnumerateDnsNames(), EnumerateIPAddresses(), etc) there's always going to be data that you need to drop to AsnReader to recover (e.g. I have both dnsName and iPAddress values, but how were they interleaved?). The nice thing about accepting that is that we get to focus on what's actually useful (and making it easy to use) instead of what's just completionist.

    For UPN, the only thing I know of that uses/used it is Windows Active Directory Certificate Services, issuing a smartcard-style cert to a user... and it only supports one UPN value. So if AD logon felt like using .NET to achieve their results, they'd be served enough with cert.GetNameInfo; thus there's not really a scenario for UPN.

    If there's a scenario for Uri/Email/etc, we could add them.

  13. stephenjust commented on Jan 27, 2022

    @stephenjust

    Can we specify that when enumerating DNS names and IP addresses, sort order will be maintained? i.e. the records may be interleaved in the raw data, but the first returned DNS name will be the first record if that type specified in raw data?

    We definitely have some scenarios where services only look at the first DNS Name specified in the alternative names, I think to match the behavior of win32. It's a niche use case, but it seems like a desirable property in general for ordering to be predictable.

    In contrast, X509Certificate2.GetNameInfo does not define which of multiple names will be returned, and the ambiguity can lead to incorrect assumptions.

  14. 1 remaining item

  15. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jun 13, 2022
  16. modified the milestones: Future, 7.0.0 on Jun 13, 2022
  17. deeprobin commented on Jun 14, 2022

    @deeprobin
    Contributor

    Is there any reason not to seal this type?

  18. bartonjs commented on Jun 14, 2022

    @bartonjs
    MemberAuthor

    @deeprobin Just a transcription error, thanks for noticing.

  19. added
    blocking
    on Jul 13, 2022
  20. bartonjs commented on Jul 14, 2022

    @bartonjs
    MemberAuthor

    Video

    Looks good as proposed

    namespace System.Security.Cryptography.X509Certificates
    {
        public partial class X509Certificate2
        {
            // Performs RFC 6125 matching of a hostname against a certificate and the
            // Subject Alternative Names.
            //
            // Uses IPAddress.TryParse to determine DNS match vs IP match
            public bool MatchesHostname(string hostname) { throw null; }
            public bool MatchesHostname(string hostname, bool allowWildcards = true, bool allowCommonName = true) { throw null; }
        }
    
        public sealed class X509SubjectAlternativeNameExtension : X509Extension
        {
            public X509SubjectAlternativeNameExtension() { }
            public X509SubjectAlternativeNameExtension(byte[] rawData, bool critical = false) { }
            public X509SubjectAlternativeNameExtension(ReadOnlySpan<byte> rawData, bool critical = false) { }
    
            public IEnumerable<string> EnumerateDnsNames() { throw null; }
            public IEnumerable<IPAddress> EnumerateIPAddresses() { throw null; }
        }
    }
  21. added
    api-approved
    and removed
    blocking
    api-ready-for-review
    on Jul 14, 2022
  22. ghost added
    in-pr
    on Jul 16, 2022
  23. ghost removed
    in-pr
    on Jul 20, 2022
  24. ghost locked as resolved and limited conversation to collaborators on Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Security

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions