apple / swift-crypto

Open-source implementation of a substantial portion of the API of Apple CryptoKit suitable for use on Linux platforms.
https://apple.github.io/swift-crypto
Apache License 2.0
1.43k stars 151 forks source link

extras: Provide initializers for RSA keys from RSA parameters #247

Open simonjbeaumont opened 3 days ago

simonjbeaumont commented 3 days ago

Motivation

We currently offer APIs to construct RSA keys from PEM and DER representations but we have no way of constructing they key from its constituent RSA parameters: n and e for public keys, and n, e, d, p, and q for private keys. Sometimes these are what you have to hand, e.g. in a JSON Web Key.

Modifications

Provide initializers for RSA keys from RSA parameters for all the RSA key types.

To implement this, we leverage the fact that we are making use of BoringSSL in _CryptoExtras on all platforms. We need this because there are no APIs to construct the underlying key type on Darwin platforms from these parameters. So we do this by first creating a BoringSSL key, serializing it to PEM format, and then constructing a platform-specific key from the PEM representation.

Result

New APIs to construct RSA keys from RSA parameters.