Skip to content

[API Proposal]: PemEncoding for UTF8 #99614

@vcsjones

Description

@vcsjones

Background and motivation

Today, PemEncoding works on inputs of char, either arrays or spans, which implies UTF-16 encoded data. There has been the occasional need to work with UTF-8 encoded PEM structures, and will be needed in #91763.

This proposal is to extend PemEncoding to work with UTF-8 inputs and outputs.

Out of scope: new overloads of the many PEM APIs that have already been added throughout various cryptographic primitives will not gain UTF-8 overloads. Just the PemEncoding class.

API Proposal

namespace System.Security.Cryptography;

public static partial class PemEncoding {
+   public static PemFields FindUtf8(ReadOnlySpan<byte> pemData);
+   public static bool TryFindUtf8(ReadOnlySpan<byte> pemData, out PemFields fields);

+   public static bool TryWriteUtf8(ReadOnlySpan<byte> label, ReadOnlySpan<byte> data, Span<byte> destination, out int bytesWritten);
+   public static byte[] WriteUtf8(ReadOnlySpan<byte> label, System.ReadOnlySpan<byte> data);
}

Some remarks.

  1. PemFields is entirely based on Range, which worked out well in this case so we don't need a new return type.
  2. label is a ReadOnlySpan<byte>. It is expected that literals are almost always going to be used here though, like "CERTIFICATE"u8. We could continue to accept a char here instead, but would force use to do a UTF-16 to UTF-8 conversion. Since the compiler can do the conversion for us in the case of literals, it made sense to accept a UTF-8 ROS<byte>.

API Usage

Same as the existing UTF-16 inputs, except for UTF-8.

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Securityin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions