Skip to content

Conversation

@josephnoir
Copy link
Contributor

Motivation

PKCS#8 is pretty widely used. Currently getting a key in PKCS#8 DER representations requires going through a PKCS8 PEM document and then get its DER bytes.

Modifications

Add a computed property to RSA private keys that calls into BoringSSL or Security.framework to get the PKCS8 DER representation of the key.

ECDH keys use the existing derRepresentation property to provide a property of the same name.

A small ASN1 encoder adds the functionality to ed25519/x25519 keys.

Result

The representation can be accessed directly.

The identifiers for MLKEM are still a draft. As such MLKEM is not included in the PR.

Motivation:

PKCS#8 is pretty widely used. Currently getting a key in PKCS#8 DER
representations requires goining through a PKCS8 PEM document and
then get its DER bytes.

Modifications:

Add a computed property to RSA private keys that calls into BoringSSL
or Security.framework to get the PKCS8 DER representation of the key.

ECDH keys use the existing derRepresentation property to provide a
property of the same name.

A small ASN1 encoder adds the functionality to ed25519/x25519 keys.

Result:

The representation can be accessed directly.

@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
extension P521.Signing.PrivateKey {
public var pkcs8DERRepresentation: Data {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding doc comments to all of these?

// Identifies the key agreement algorithm X25519.
//
// This identifier is defined in RFC 8410
public static let idX25519: ASN1ObjectIdentifier = [1, 3, 101, 110]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not make these public, we don't need to.

self.backing.pemRepresentation
}

public var pkcs8DERRepresentation: Data {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add documentation to this as well.

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks @josephnoir! ✨

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Jul 2, 2025
@Lukasa Lukasa enabled auto-merge (squash) July 2, 2025 01:00
@Lukasa Lukasa merged commit 4ce1b9b into apple:main Jul 2, 2025
31 of 32 checks passed
@josephnoir josephnoir deleted the pkcs8-der-representation branch July 2, 2025 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants