Skip to content

RawData setter not accounted for in some X.509 / Pkcs extensions' memoization #81844

Closed as not planned

Description

@vcsjones

Consider:

using System;
using System.Security.Cryptography.X509Certificates;

X500DistinguishedName dn = new("CN=Wasabi");

foreach (X500RelativeDistinguishedName rdn in dn.EnumerateRelativeDistinguishedNames())
{
    Console.WriteLine(rdn.GetSingleElementValue());
}

dn.RawData = new byte[] { 0x30, 0x00 }; // Empty SEQUENCE

foreach (X500RelativeDistinguishedName rdn in dn.EnumerateRelativeDistinguishedNames())
{
    Console.WriteLine(rdn.GetSingleElementValue());
}

This will, incorrectly, print "Wasabi" to the console twice. This is because the first call to EnumerateRelativeDistinguishedNames memoizes the RDNs and it isn't aware that the RawData changed out from underneath of it.

A number of X.509 extensions I think have this issue, including X509SubjectAlternativeNameExtension, X500DistinguishedName, X509SubjectKeyIdentifierExtension, etc. In addition, some AsnEncodedData derived types in S.S.Cryptography.Pkcs memoize their results like Pkcs9LocalKeyId

There are a few ways I think we can fix this.

  1. Stop memoizing decoded data. This will incur a performance hit.

  2. Add a hook for ourselves when the RawData property is set:

    public byte[] RawData
    {
        // Get omitted
        set
        {
            ArgumentNullException.ThrowIfNull(value);
            _rawData = value.CloneByteArray();
            RawDataChanged();
        }
    }
    
    private protected virtual void RawDataChanged()
    {
        // Override this in types that need to clear out computed values.
    }

    This works for S.S.Cryptography, but it would not work for S.S.Cryptography.Pkcs without either making an API proposal or reflection (and it isn't a very good API, either). I considered suggesting making RawData virtual, but it turns out that's a breaking change.

  3. Do nothing, perhaps document it.

Activity

  1. ghost added
    untriaged
    on Feb 8, 2023
  2. vcsjones commented on Feb 8, 2023

    @vcsjones
    MemberAuthor

    Hm, one other option: since RawData doesn't return defensive copies, we could hold on to the reference to RawData that was used during memoization. When we need look up a memoized value, like in EnumerateRelativeDistinguishedNames, we check to see if the reference from RawData changed from when we cached it:

    byte[]? _rawDataMemoizedReference;
    
    public IEnumerable<X500RelativeDistinguishedName> EnumerateRelativeDistinguishedNames(bool reversed = true)
    {
        if (_rawDataMemoizedReference is null || !ReferenceEquals(RawData, _rawDataMemoizedReference))
        {
            _parsedAttributes ??= ParseAttributes(RawData);
            _rawDataMemoizedReference = RawData;
        }
    
        return EnumerateRelativeDistinguishedNames(_parsedAttributes, reversed);
    }

    That feels fragile though.

  3. bartonjs commented on Feb 9, 2023

    @bartonjs
    Member

    Don't /all/ of the types that interpret RawData have this behavior (so all of the Pkcs9 and X.509 extension derived types)?

    This feels like one of those things that "I've always known, and rolled my eyes at". Assuming we're already fairly consistent then docs is probably the better answer.

  4. vcsjones commented on Feb 9, 2023

    @vcsjones
    MemberAuthor

    @bartonjs

    Don't /all/ of the types that interpret RawData have this behavior (so all of the Pkcs9 and X.509 extension derived types)?

    Probably. I've probably never noticed until recently because most of the extensions weren't that useful for reading data.

    This is more noticeable when the memoization occurs in the constructor:

    using System;
    using System.Security.Cryptography.X509Certificates;
    
    X509BasicConstraintsExtension extension = new()
    {
        RawData = new byte[] { 0x30, 0x03, 0x01, 0x01, 0xFF }
    };
    Console.WriteLine(extension.CertificateAuthority);

    This prints false. It should print true.

    That just seems broken to me.

  5. bartonjs commented on Feb 9, 2023

    @bartonjs
    Member

    Huh. Most of the extensions (I think/thought) don't process any of their properties until first read, which allows for RawData to get assigned late.

    If basic constraints is the odd man out, we should just consider fixing'm.

  6. self-assigned this
    on Feb 9, 2023
  7. vcsjones commented on Feb 9, 2023

    @vcsjones
    MemberAuthor

    If basic constraints is the odd man out

    It's not. Every X.509 extension has this problem. The default constructor for all of them do some method of "hey I already decoded everything and there is nothing to decode".

    I guess we can fix all of them.

    But more to the original point, the setter just doesn't work as soon as you do something that causes memoization. Why have it then, except for object initialization syntax?

    How breaking would it be if we did something like we did with Oid "Set once, and after that it throws"?

  8. vcsjones commented on Feb 9, 2023

    @vcsjones
    MemberAuthor

    How breaking would it be if we did something like we did with Oid "Set once, and after that it throws"?

    Meh that gets weird with CopyFrom. I guess we can just make the object initialization syntax work.

  9. added this to the Future milestone on Jun 6, 2023
  10. removed
    untriaged
    on Jun 6, 2023
  11. vcsjones commented on Jun 6, 2023

    @vcsjones
    MemberAuthor

    Will pick this up soon, but it's existing behavior, no one else cares but me, so marking as Future.

  12. removed their assignment
    on Jun 16, 2023
  13. vcsjones commented on Jun 25, 2026

    @vcsjones
    MemberAuthor

    Closed in favor of #129544.

  14. locked and limited conversation to collaborators on Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions