Skip to content

Add support for encrypted PEM keys via BoringSSL#428

Merged
Lukasa merged 6 commits intoapple:mainfrom
fpseverino:encrypted-pem-keys
Feb 23, 2026
Merged

Add support for encrypted PEM keys via BoringSSL#428
Lukasa merged 6 commits intoapple:mainfrom
fpseverino:encrypted-pem-keys

Conversation

@fpseverino
Copy link
Contributor

@fpseverino fpseverino commented Jan 25, 2026

Add support for creating RSA private keys from encrypted PEM files.

Checklist

  • I've run tests to see all new and existing tests pass
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

If you've made changes to gyb files

  • I've run ./scripts/generate_boilerplate_files_with_gyb.sh and included updated generated files in a commit of this pull request

Motivation:

Resolves #264

Modifications:

Adds an initializer to _RSA.Signing.PrivateKey that takes the encrypted PEM representation and the encryption password and calls the high-level BoringSSL function to parse the encrypted key format.

Tests were copied from #318 by @ptoffy.

Result:

RSA private keys can be created from encrypted PEM files.

/// - encryptionPassword: The password used to decrypt the PEM representation.
///
/// - Throws: An error if the key could not be initialized.
public init(encryptedPEMRepresentation: String, encryptionPassword: String) throws {
Copy link
Contributor

Choose a reason for hiding this comment

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

Historically we've been nervous about accepting passphrases as Strings: it's not really an ideal way to handle the passphrase. It may be worth looking at what swift-nio-ssl does, where we accept a callback into which the user can write the passphrase. Many users will still use Strings, of course, but it's nicer to offer something that at least can be used safely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @Lukasa, I implemented the passphrase callback by copying the NIOSSL implementation.
Regarding the license and attribution for the copied code, NOTICE.txt already contains a link to Swift NIO, but let me know if there's anything else I need to add regarding the license.

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.

Shiny, this looks really nice. Thanks so much! ✨

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Feb 12, 2026
@fpseverino
Copy link
Contributor Author

Hi @Lukasa, I formatted the code; it was the only CI check that failed. Is it possible to merge and release this code now?

@Lukasa Lukasa enabled auto-merge (squash) February 23, 2026 13:22
@Lukasa Lukasa merged commit a5a71cf into apple:main Feb 23, 2026
49 checks passed
@fpseverino fpseverino deleted the encrypted-pem-keys branch February 23, 2026 13:31
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.

Encrypted PEM keys

2 participants