Files
swift-certificates/Sources/X509/SecKeyWrapper.swift
T
d8799da503 Support SecKey Certificate private keys (#195)
Motivation:

I would like to create Certificates using a SecKey so that I can create certificates signed by hardware-backed keys. Currently, Swift Certificates requires the private key be provided in an exported representation, but this isn't possible when a SecKey is marked as non-exportable.

Modifications:

Adds a new SecKeyWrapper backing to Certificate.PrivateKey to support initalisation with SecKey. This wrapper struct handles key validation and signing operations by calling the appropriate SecKey interfaces. Adds additional tests to support new backing type.

Result:

Certifcate.PrivateKey can be initialised with a SecKey, allowing existing hardware-backed SecKey instances to be used as the issuer private key for new Certificate instances.

Co-authored-by: Rory Smith <rory.s@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-10-02 16:40:39 +01:00

330 lines
13 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCertificates open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftCertificates project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import SwiftASN1
import Foundation
@preconcurrency import Crypto
@preconcurrency import _CryptoExtras
@preconcurrency import Security
extension Certificate.PrivateKey {
/// A wrapper around ``Security.SecKey`` to allow the use of `SecKey` with certificates.
@usableFromInline
struct SecKeyWrapper: Sendable {
@usableFromInline
let privateKey: SecKey
@usableFromInline
let publicKey: Certificate.PublicKey
@usableFromInline
let type: KeyType
@usableFromInline
let attributes: [String: any Sendable]
@usableFromInline
enum ECKeySize: Sendable {
case P256
case P384
case P521
}
@usableFromInline
enum KeyType: Sendable {
case RSA
case ECDSA(ECKeySize)
}
/// Init with an existing `SecKey`.
///
/// This parses the given `SecKey` and attempts to pre-emptively extract
/// data that will be needed at later points. Importantly, some of these operations
/// can throw, so these are performs during initialisation rather than at later
/// stages where throwing is unacceptable.
@inlinable
init(key: SecKey) throws {
self.privateKey = key
self.attributes = try Self.keyAttributes(key: key)
try Self.validateSecKey(attributes: self.attributes)
self.type = try Self.keyType(attributes: self.attributes)
self.publicKey = try Self.publicKey(privateKey: key, type: self.type)
}
@usableFromInline
static func keyAttributes(key: SecKey) throws -> [String: any Sendable] {
guard let attributes = SecKeyCopyAttributes(key) as? [CFString: Any] else {
throw CertificateError.unsupportedPrivateKey(
reason: "cannot copy SecKey attributes"
)
}
return attributes as [String: any Sendable]
}
@usableFromInline
static func validateSecKey(attributes: [String: any Sendable]) throws {
guard let keyClassType = attributes[kSecAttrKeyClass as String] as? String else {
throw CertificateError.unsupportedPrivateKey(
reason: "cannot determine class of SecKey"
)
}
let privateKeyClassType = kSecAttrKeyClassPrivate as String
if keyClassType != privateKeyClassType {
throw CertificateError.unsupportedPrivateKey(
reason: "SecKey class must be \(privateKeyClassType), not \(keyClassType)"
)
}
}
@usableFromInline
static func publicKeyData(privateKey: SecKey) throws -> Data {
var error: Unmanaged<CFError>? = nil
guard let publicSecKey = SecKeyCopyPublicKey(privateKey),
let publicKeyData = SecKeyCopyExternalRepresentation(publicSecKey, &error) as Data?
else {
if let error = error?.takeRetainedValue() {
throw CertificateError.unsupportedPrivateKey(
reason: "cannot get public key from SecKey instance: \(error)"
)
}
throw CertificateError.unsupportedPrivateKey(
reason: "SecKeyCopyExternalRepresentation returned empty data"
)
}
return publicKeyData
}
@usableFromInline
static func keyType(attributes: [String: any Sendable]) throws -> KeyType {
guard let privateKeyType = attributes[kSecAttrKeyType as String] as? String else {
throw CertificateError.unsupportedPrivateKey(
reason: "cannot get SecKey type"
)
}
if privateKeyType == (kSecAttrKeyTypeECDSA as String) {
let keySize = attributes[kSecAttrKeySizeInBits as String] as? Int ?? -1
if keySize == 256 {
return .ECDSA(.P256)
} else if keySize == 384 {
return .ECDSA(.P384)
} else if keySize == 521 {
return .ECDSA(.P521)
} else {
throw CertificateError.unsupportedPrivateKey(
reason: "unsupported SecKey key size: \(keySize)"
)
}
} else if privateKeyType == (kSecAttrKeyTypeRSA as String) {
return .RSA
} else {
throw CertificateError.unsupportedPrivateKey(
reason: "unsupported SecKey key type: \(privateKeyType)"
)
}
}
@usableFromInline
static func publicKey(privateKey: SecKey, type: KeyType) throws -> Certificate.PublicKey {
let publicKeyData = try Self.publicKeyData(privateKey: privateKey)
do {
switch type {
case .ECDSA(let keySize):
if keySize == .P256 {
return Certificate.PublicKey(try P256.Signing.PublicKey(x963Representation: publicKeyData))
} else if keySize == .P384 {
return Certificate.PublicKey(try P384.Signing.PublicKey(x963Representation: publicKeyData))
} else if keySize == .P521 {
return Certificate.PublicKey(try P521.Signing.PublicKey(x963Representation: publicKeyData))
} else {
throw CertificateError.unsupportedPrivateKey(
reason: "unsupported SecKey ECDSA key size: \(keySize)"
)
}
case .RSA:
return Certificate.PublicKey(try _RSA.Signing.PublicKey(derRepresentation: publicKeyData))
}
} catch {
throw CertificateError.unsupportedPrivateKey(
reason: "cannot get public key from SecKey instance \(error)"
)
}
}
static func signatureData<Bytes: DataProtocol>(
key: SecKey,
type: KeyType,
digestAlgorithm: AlgorithmIdentifier,
bytes: Bytes
) throws -> Data {
let signatureAlgorithm = try Self.signatureAlgorithm(digestAlgorithm: digestAlgorithm, type: type)
var error: Unmanaged<CFError>?
guard
let signatureData = SecKeyCreateSignature(
key,
signatureAlgorithm,
Data(bytes) as CFData,
&error
) as Data?
else {
if let error = error?.takeRetainedValue() {
throw CertificateError.unsupportedPrivateKey(
reason: "could not create signature with SecKey: \(error)"
)
}
throw CertificateError.unsupportedPrivateKey(reason: "SecKeyCreateSignature returned empty data")
}
return signatureData
}
static func signatureAlgorithm(digestAlgorithm: AlgorithmIdentifier, type: KeyType) throws -> SecKeyAlgorithm {
let algorithm: SecKeyAlgorithm
switch type {
case .RSA:
switch digestAlgorithm {
case .sha1, .sha1UsingNil:
algorithm = .rsaSignatureMessagePKCS1v15SHA1
case .sha256, .sha256UsingNil:
algorithm = .rsaSignatureMessagePKCS1v15SHA256
case .sha384, .sha384UsingNil:
algorithm = .rsaSignatureMessagePKCS1v15SHA384
case .sha512, .sha512UsingNil:
algorithm = .rsaSignatureMessagePKCS1v15SHA512
default:
throw CertificateError.unsupportedPrivateKey(
reason: "unsupported SecKey RSA digest algorithm: \(digestAlgorithm)"
)
}
case .ECDSA:
switch digestAlgorithm {
case .sha1, .sha1UsingNil:
algorithm = .ecdsaSignatureMessageX962SHA1
case .sha256, .sha256UsingNil:
algorithm = .ecdsaSignatureMessageX962SHA256
case .sha384, .sha384UsingNil:
algorithm = .ecdsaSignatureMessageX962SHA384
case .sha512, .sha512UsingNil:
algorithm = .ecdsaSignatureMessageX962SHA512
default:
throw CertificateError.unsupportedPrivateKey(
reason: "unsupported SecKey ECDSA digest algorithm: \(digestAlgorithm)"
)
}
}
return algorithm
}
@usableFromInline
func signature<Bytes: DataProtocol>(
for bytes: Bytes,
digestAlgorithm: AlgorithmIdentifier
) throws -> Certificate.Signature {
let signatureData = try Self.signatureData(
key: self.privateKey,
type: self.type,
digestAlgorithm: digestAlgorithm,
bytes: bytes
)
switch self.type {
case .RSA:
let signature = _RSA.Signing.RSASignature(rawRepresentation: signatureData)
return Certificate.Signature(backing: .rsa(signature))
case .ECDSA(let keySize):
switch keySize {
case .P256:
let signature = try P256.Signing.ECDSASignature(derRepresentation: signatureData)
return Certificate.Signature(backing: .ecdsa(.init(signature)))
case .P384:
let signature = try P384.Signing.ECDSASignature(derRepresentation: signatureData)
return Certificate.Signature(backing: .ecdsa(.init(signature)))
case .P521:
let signature = try P521.Signing.ECDSASignature(derRepresentation: signatureData)
return Certificate.Signature(backing: .ecdsa(.init(signature)))
}
}
}
@usableFromInline
var isSerializable: Bool {
if let extractable = self.attributes[kSecAttrIsExtractable as String] as? Bool {
return extractable
}
return false
}
@available(macOS 11.0, iOS 14, tvOS 14, watchOS 7, *)
@inlinable
func pemDocument() throws -> PEMDocument {
if !self.isSerializable {
throw CertificateError.unsupportedPrivateKey(
reason: "SecKey private key PEM cannot be extracted"
)
}
var error: Unmanaged<CFError>?
guard let keyData = SecKeyCopyExternalRepresentation(self.privateKey, &error) as Data? else {
if let error = error?.takeRetainedValue() {
throw CertificateError.unsupportedPrivateKey(
reason: "cannot get external representation of SecKey: \(error)"
)
}
throw CertificateError.unsupportedPrivateKey(
reason: "SecKeyCopyExternalRepresentation returned empty data"
)
}
let derData: Data
let type: String
switch self.type {
case .RSA:
type = "RSA PRIVATE KEY"
// keyData is DER-encoded private key
derData = keyData
case .ECDSA(let keySize):
type = "EC PRIVATE KEY"
switch keySize {
case .P256:
derData = try P256.Signing.PrivateKey(x963Representation: keyData).derRepresentation
case .P384:
derData = try P384.Signing.PrivateKey(x963Representation: keyData).derRepresentation
case .P521:
derData = try P521.Signing.PrivateKey(x963Representation: keyData).derRepresentation
}
}
let array = [UInt8](derData)
return PEMDocument(type: type, derBytes: array)
}
}
}
#endif