Skip to content

Add new Certificate.PrivateKey init from PKCS8 DER bytes #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025

Conversation

gjcairo
Copy link
Contributor

@gjcairo gjcairo commented Apr 16, 2025

This PR adds a new init(derBytes:) to initialise a private key in PKCS8 format from an array of DER bytes.

@gjcairo gjcairo added the 🆕 semver/minor Adds new public API. label Apr 16, 2025
@available(macOS 11.0, iOS 14, tvOS 14, watchOS 7, macCatalyst 14, visionOS 1.0, *)
extension Certificate.PrivateKey {
/// Initialize a new certificate private key from PKCS8-format DER bytes.
public init(pkcs8DERBytes: [UInt8]) throws {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sort-of thinking that this should be public init(derBytes:). We'd be willing to detect what the right answer was if we had to, but I think we mostly don't have to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I flip-flopped a bit on this since being unspecific about the format lets us evolve the API better. However it is a reality that right now we only understand PKCS8 in DER. Should I still mention this in the docs but change the argument label?

Copy link
Member

Choose a reason for hiding this comment

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

Can we detect different representation just based on the DER representation reliably?
I don't recall the details but I thought there was a good reason why we switch over the pem discriminator instead of just looking at the bytes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we can't detect it reliably, but that's ok: it's what PKCS#8 is for. I suspect we'll only ever support the PKCS#8 format here, and that's fine.

@available(macOS 11.0, iOS 14, tvOS 14, watchOS 7, macCatalyst 14, visionOS 1.0, *)
extension Certificate.PrivateKey {
/// Initialize a new certificate private key from PKCS8-format DER bytes.
public init(pkcs8DERBytes: [UInt8]) throws {
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 also add at least one unit test that uses this constructor directly, ideally one per algorithm.

@gjcairo gjcairo requested a review from Lukasa April 25, 2025 15:52
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.

Lovely, thanks @gjcairo!

@Lukasa Lukasa merged commit 999fd70 into apple:main Apr 25, 2025
38 checks passed
@gjcairo gjcairo deleted the der-private-key branch April 27, 2025 00:07
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.

3 participants