dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.46k stars 4.76k forks source link

Consider doing PEM reads with unified code in the new certificate loader #103928

Open bartonjs opened 5 months ago

bartonjs commented 5 months ago

The new certificate loader tests have shown some platform differences in corner cases when it comes to PEM-encoded contents.

Pros:

Cons:

dotnet-policy-service[bot] commented 5 months ago

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones See info in area-owners.md if you want to be subscribed.

vcsjones commented 5 months ago

I think the part that will help guide a decision here is how the behavior matches with GetCertContentType. In .NET, that's easy, we can just make GetCertContentType do whatever we want it to do.

In .NET Framework, we can't change that behavior. It would be weird for GetCertContentType to say "I don't know what that is" but the loader loads it anyway. Or the opposite, where GetCertContentType says "This is a X.509 certificate" and the loader says "I cannot load that".

With that in mind, I would want the answer to be "Do what Windows does". Some experimentation shows that Windows PEM reader just looks for PEM encapsulation boundaries regardless of surrounding data. That said, my proposal would be

  1. Change PemEncoding to not require white space before the PreEB.
  2. Change PemEncoding to not require white space after the PostEB.
    1. Since these are relaxing the reader, I don't think it is a breaking change.
  3. We should add UTF-8 to PemEncoding (#99614) but I do not think it needs to be exposed in M.BCL.Cryptography. It can carry an internal non-public version.
    1. Making this somewhat complicated is the fact that PemEncoding now leans on the Base64 class to do all of the Base64 reading and validation. The version used in M.B.C will need to be an older version of PemEncoding that carries its own Base64 validation.
  4. I don't think we need to consider UTF8 PemEncoding blocking for API review purposes. If we can make it public in time, great. But since the consumer of it is ourselves, and M.B.C will need a copy regardless, it can stay internal until we have it through API review.
  5. Fixup other platforms to agree with Windows.