mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
Rejects NIST PKCS#8 private keys labelled with the wrong curve. ### Checklist - [x] I've run tests to see all new and existing tests pass - [x] I've followed the code style of the rest of the project - [x] I've read the [Contribution Guidelines](CONTRIBUTING.md) - [x] I've updated the documentation if necessary ### Motivation: NIST `PrivateKey(derRepresentation:)` and `PrivateKey(pemRepresentation:)` never compare the PKCS#8 `privateKeyAlgorithm` to the curve. A PKCS#8 labelled secp384r1 that holds a 32-byte SEC1 key with no `[0]` parameters loads as a P256 key ### Modifications: - Throw `invalidASN1Object` when the PKCS#8 OID doesn't match the curve, for P256, P384 and P521 `Signing` and `KeyAgreement`, DER and PEM - Add a test that loads each curve's key under another curve's OID ### Result: Keys labelled with the wrong curve now throw. No public API change. The new test fails on `main` and passes with this change. The full suite passes on Linux. Fixes #485 Co-authored-by: Si Beaumont <beaumont@apple.com>