@@ -303,24 +303,7 @@ extension Certificate.PrivateKey {
303
303
self = try . init( ecdsaAlgorithm: sec1. algorithm, rawEncodedPrivateKey: sec1. privateKey. bytes)
304
304
305
305
case Self . pemDiscriminatorForPKCS8:
306
- let pkcs8 = try PKCS8PrivateKey ( derEncoded: pemDocument. derBytes)
307
- switch pkcs8. algorithm {
308
- case . ecdsaP256, . ecdsaP384, . ecdsaP521:
309
- let sec1 = try SEC1PrivateKey ( derEncoded: pkcs8. privateKey. bytes)
310
- if let innerAlgorithm = sec1. algorithm, innerAlgorithm != pkcs8. algorithm {
311
- throw ASN1Error . invalidASN1Object (
312
- reason: " algorithm mismatch. PKCS#8 is \( pkcs8. algorithm) but inner SEC1 is \( innerAlgorithm) "
313
- )
314
- }
315
- self = try . init( ecdsaAlgorithm: pkcs8. algorithm, rawEncodedPrivateKey: sec1. privateKey. bytes)
316
-
317
- case . rsaKey:
318
- self = try . init( _CryptoExtras. _RSA. Signing. PrivateKey ( derRepresentation: pkcs8. privateKey. bytes) )
319
- case . ed25519:
320
- self = try . init( Curve25519 . Signing. PrivateKey ( pkcs8Key: pkcs8) )
321
- default :
322
- throw CertificateError . unsupportedPrivateKey ( reason: " unknown algorithm \( pkcs8. algorithm) " )
323
- }
306
+ self = try . init( derBytes: pemDocument. derBytes)
324
307
325
308
default :
326
309
throw ASN1Error . invalidPEMDocument (
@@ -364,3 +347,28 @@ extension Certificate.PrivateKey {
364
347
}
365
348
}
366
349
}
350
+
351
+ @available ( macOS 11 . 0 , iOS 14 , tvOS 14 , watchOS 7 , macCatalyst 14 , visionOS 1 . 0 , * )
352
+ extension Certificate . PrivateKey {
353
+ /// Initialize a new certificate private key from PKCS8-format DER bytes.
354
+ public init ( derBytes: [ UInt8 ] ) throws {
355
+ let pkcs8 = try PKCS8PrivateKey ( derEncoded: derBytes)
356
+ switch pkcs8. algorithm {
357
+ case . ecdsaP256, . ecdsaP384, . ecdsaP521:
358
+ let sec1 = try SEC1PrivateKey ( derEncoded: pkcs8. privateKey. bytes)
359
+ if let innerAlgorithm = sec1. algorithm, innerAlgorithm != pkcs8. algorithm {
360
+ throw ASN1Error . invalidASN1Object (
361
+ reason: " algorithm mismatch. PKCS#8 is \( pkcs8. algorithm) but inner SEC1 is \( innerAlgorithm) "
362
+ )
363
+ }
364
+ self = try . init( ecdsaAlgorithm: pkcs8. algorithm, rawEncodedPrivateKey: sec1. privateKey. bytes)
365
+
366
+ case . rsaKey:
367
+ self = try . init( _CryptoExtras. _RSA. Signing. PrivateKey ( derRepresentation: pkcs8. privateKey. bytes) )
368
+ case . ed25519:
369
+ self = try . init( Curve25519 . Signing. PrivateKey ( pkcs8Key: pkcs8) )
370
+ default :
371
+ throw CertificateError . unsupportedPrivateKey ( reason: " unknown algorithm \( pkcs8. algorithm) " )
372
+ }
373
+ }
374
+ }
0 commit comments