mirror of
https://github.com/apple/swift-certificates
synced 2026-11-24 04:27:49 +00:00
## Motivation: In some cases a developer may want to sign a certificate using a method other than a private key. For example: if a private key is protected by hardware which signs asynchronously. ## Modifications: - Create `CustomPrivateKey` protocol - Create `async` initializers for `Certificate` and `CertificateSigningRequest`. - `CustomPrivateKey` can now back a`Certificate.PrivateKey` - Make `Certificate.Signature` initializer public ## Result: Developers can now sign a certificate with greater flexibility. ## Alternatives Considered: ### Implementations #### Pass the `CustomPrivateKey` into `Certificate` and `CertificateSigningRequest` initializers directly. There is concern this could add too much duplication of api. ### Various names for the protocol: - `Certificate.PrivateKeyProtocol` and `Certificate.AsyncPrivateKeyProtocol` - `Certificate.Signer`/`Certificate.AsyncSigner` - `Certificate.SignatureProvider`/`Certificate.AsyncSignatureProvider` --------- Co-authored-by: Cory Benfield <cbenfield@apple.com>
78 lines
3.0 KiB
Swift
78 lines
3.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCertificates open source project
|
|
//
|
|
// Copyright (c) 2025 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(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
import SwiftASN1
|
|
|
|
/// Implement ``CustomPrivateKey`` if you need custom signing logic.
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
public protocol CustomPrivateKey: Sendable, Hashable, PEMSerializable {
|
|
|
|
/// Obtain the ``Certificate/PublicKey-swift.struct`` corresponding to
|
|
/// this private key.
|
|
var publicKey: Certificate.PublicKey { get }
|
|
|
|
/// The default signature algorithm to use for signing.
|
|
var defaultSignatureAlgorithm: Certificate.SignatureAlgorithm { get }
|
|
|
|
/// Return a list of all supported signature types for this private key. The ordering is not a comment on the
|
|
/// preference or security of the contained algorithms.
|
|
var supportedSignatureAlgorithms: [Certificate.SignatureAlgorithm] { get }
|
|
|
|
/// Use the private key to sign the provided bytes with a given signature algorithm.
|
|
///
|
|
/// - Parameters:
|
|
/// - bytes: The data to create the signature for.
|
|
/// - signatureAlgorithm: The signature algorithm to use.
|
|
/// - Returns: The signature.
|
|
/// - Throws: If signing fails or synchronous signing is unsupported.
|
|
func signSynchronously(
|
|
bytes: some DataProtocol,
|
|
signatureAlgorithm: Certificate.SignatureAlgorithm
|
|
) throws -> Certificate.Signature
|
|
|
|
/// Use the private key to sign the provided bytes asynchronously with a given signature algorithm.
|
|
///
|
|
/// The default implementation calls ``signSynchronously(bytes:signatureAlgorithm:)`` and returns the result.
|
|
/// Conforming types may override this method to provide a specialized asynchronous implementation.
|
|
///
|
|
/// - Parameters:
|
|
/// - bytes: The data to create the signature for.
|
|
/// - signatureAlgorithm: The signature algorithm to use.
|
|
/// - Returns: The signature.
|
|
/// - Throws: If signing fails or asynchronous signing is unsupported.
|
|
func signAsynchronously(
|
|
bytes: some DataProtocol & Sendable,
|
|
signatureAlgorithm: Certificate.SignatureAlgorithm
|
|
) async throws -> Certificate.Signature
|
|
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
extension CustomPrivateKey {
|
|
|
|
@inlinable
|
|
public func signAsynchronously(
|
|
bytes: some DataProtocol & Sendable,
|
|
signatureAlgorithm: Certificate.SignatureAlgorithm
|
|
) async throws -> Certificate.Signature {
|
|
try self.signSynchronously(bytes: bytes, signatureAlgorithm: signatureAlgorithm)
|
|
}
|
|
|
|
}
|