mirror of
https://github.com/apple/swift-certificates
synced 2026-11-28 04:54:21 +00:00
Motivation We omitted RSA signing in the initial implementation. We should support it. As part of this, we need to decouple the idea that we know what the digest algorithm is based solely on the key type. There's no particular reason not to support ECDSA with SHA256 when using a P521 key, so we need to tolerate that as well. Modifications - Explicitly pass the signature algorithm to our various cryptographic operations. - Add a Digest type to handle the various digest types we create. - Add tests for the various cross products. Results RSA signing works, as do weirder flavours of ECDSA.
210 lines
7.5 KiB
Swift
210 lines
7.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCertificates open source project
|
|
//
|
|
// Copyright (c) 2023 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
import Crypto
|
|
import _CryptoExtras
|
|
|
|
@usableFromInline
|
|
enum Digest {
|
|
case insecureSHA1(Insecure.SHA1Digest)
|
|
case sha256(SHA256Digest)
|
|
case sha384(SHA384Digest)
|
|
case sha512(SHA512Digest)
|
|
|
|
@inlinable
|
|
static func computeDigest<Bytes: DataProtocol>(for bytes: Bytes, using digestIdentifier: AlgorithmIdentifier) throws -> Digest {
|
|
switch digestIdentifier {
|
|
case .sha1, .sha1UsingNil:
|
|
return .insecureSHA1(Insecure.SHA1.hash(data: bytes))
|
|
case .sha256, .sha256UsingNil:
|
|
return .sha256(SHA256.hash(data: bytes))
|
|
case .sha384, .sha384UsingNil:
|
|
return .sha384(SHA384.hash(data: bytes))
|
|
case .sha512, .sha512UsingNil:
|
|
return .sha512(SHA512.hash(data: bytes))
|
|
default:
|
|
throw CertificateError.unsupportedDigestAlgorithm(reason: "Unknown digest algorithm: \(digestIdentifier)")
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Public key operations
|
|
|
|
extension P256.Signing.PublicKey {
|
|
@inlinable
|
|
func isValidSignature(_ signature: Certificate.Signature, for digest: Digest) -> Bool {
|
|
guard case .p256(let innerSignature) = signature.backing else {
|
|
// Signature mismatch
|
|
return false
|
|
}
|
|
|
|
switch digest {
|
|
case .insecureSHA1(let sha1):
|
|
return self.isValidSignature(innerSignature, for: sha1)
|
|
case .sha256(let sha256):
|
|
return self.isValidSignature(innerSignature, for: sha256)
|
|
case .sha384(let sha384):
|
|
return self.isValidSignature(innerSignature, for: sha384)
|
|
case .sha512(let sha512):
|
|
return self.isValidSignature(innerSignature, for: sha512)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension P384.Signing.PublicKey {
|
|
@inlinable
|
|
func isValidSignature(_ signature: Certificate.Signature, for digest: Digest) -> Bool {
|
|
guard case .p384(let innerSignature) = signature.backing else {
|
|
// Signature mismatch
|
|
return false
|
|
}
|
|
|
|
switch digest {
|
|
case .insecureSHA1(let sha1):
|
|
return self.isValidSignature(innerSignature, for: sha1)
|
|
case .sha256(let sha256):
|
|
return self.isValidSignature(innerSignature, for: sha256)
|
|
case .sha384(let sha384):
|
|
return self.isValidSignature(innerSignature, for: sha384)
|
|
case .sha512(let sha512):
|
|
return self.isValidSignature(innerSignature, for: sha512)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension P521.Signing.PublicKey {
|
|
@inlinable
|
|
func isValidSignature(_ signature: Certificate.Signature, for digest: Digest) -> Bool {
|
|
guard case .p521(let innerSignature) = signature.backing else {
|
|
// Signature mismatch
|
|
return false
|
|
}
|
|
|
|
switch digest {
|
|
case .insecureSHA1(let sha1):
|
|
return self.isValidSignature(innerSignature, for: sha1)
|
|
case .sha256(let sha256):
|
|
return self.isValidSignature(innerSignature, for: sha256)
|
|
case .sha384(let sha384):
|
|
return self.isValidSignature(innerSignature, for: sha384)
|
|
case .sha512(let sha512):
|
|
return self.isValidSignature(innerSignature, for: sha512)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension _RSA.Signing.PublicKey {
|
|
@inlinable
|
|
func isValidSignature(_ signature: Certificate.Signature, for digest: Digest, padding: _RSA.Signing.Padding) -> Bool {
|
|
guard case .rsa(let innerSignature) = signature.backing else {
|
|
// Signature mismatch
|
|
return false
|
|
}
|
|
|
|
switch digest {
|
|
case .insecureSHA1(let sha1):
|
|
return self.isValidSignature(innerSignature, for: sha1, padding: padding)
|
|
case .sha256(let sha256):
|
|
return self.isValidSignature(innerSignature, for: sha256, padding: padding)
|
|
case .sha384(let sha384):
|
|
return self.isValidSignature(innerSignature, for: sha384, padding: padding)
|
|
case .sha512(let sha512):
|
|
return self.isValidSignature(innerSignature, for: sha512, padding: padding)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Private key operations
|
|
|
|
extension P256.Signing.PrivateKey {
|
|
@inlinable
|
|
func signature<Bytes: DataProtocol>(for bytes: Bytes, digestAlgorithm: AlgorithmIdentifier) throws -> Certificate.Signature {
|
|
let signature: P256.Signing.ECDSASignature
|
|
|
|
switch try Digest.computeDigest(for: bytes, using: digestAlgorithm) {
|
|
case .insecureSHA1(let sha1):
|
|
signature = try self.signature(for: sha1)
|
|
case .sha256(let sha256):
|
|
signature = try self.signature(for: sha256)
|
|
case .sha384(let sha384):
|
|
signature = try self.signature(for: sha384)
|
|
case .sha512(let sha512):
|
|
signature = try self.signature(for: sha512)
|
|
}
|
|
|
|
return Certificate.Signature(backing: .p256(signature))
|
|
}
|
|
}
|
|
|
|
extension P384.Signing.PrivateKey {
|
|
@inlinable
|
|
func signature<Bytes: DataProtocol>(for bytes: Bytes, digestAlgorithm: AlgorithmIdentifier) throws -> Certificate.Signature {
|
|
let signature: P384.Signing.ECDSASignature
|
|
|
|
switch try Digest.computeDigest(for: bytes, using: digestAlgorithm) {
|
|
case .insecureSHA1(let sha1):
|
|
signature = try self.signature(for: sha1)
|
|
case .sha256(let sha256):
|
|
signature = try self.signature(for: sha256)
|
|
case .sha384(let sha384):
|
|
signature = try self.signature(for: sha384)
|
|
case .sha512(let sha512):
|
|
signature = try self.signature(for: sha512)
|
|
}
|
|
|
|
return Certificate.Signature(backing: .p384(signature))
|
|
}
|
|
}
|
|
|
|
extension P521.Signing.PrivateKey {
|
|
@inlinable
|
|
func signature<Bytes: DataProtocol>(for bytes: Bytes, digestAlgorithm: AlgorithmIdentifier) throws -> Certificate.Signature {
|
|
let signature: P521.Signing.ECDSASignature
|
|
|
|
switch try Digest.computeDigest(for: bytes, using: digestAlgorithm) {
|
|
case .insecureSHA1(let sha1):
|
|
signature = try self.signature(for: sha1)
|
|
case .sha256(let sha256):
|
|
signature = try self.signature(for: sha256)
|
|
case .sha384(let sha384):
|
|
signature = try self.signature(for: sha384)
|
|
case .sha512(let sha512):
|
|
signature = try self.signature(for: sha512)
|
|
}
|
|
|
|
return Certificate.Signature(backing: .p521(signature))
|
|
}
|
|
}
|
|
|
|
extension _RSA.Signing.PrivateKey {
|
|
@inlinable
|
|
func signature<Bytes: DataProtocol>(for bytes: Bytes, digestAlgorithm: AlgorithmIdentifier, padding: _RSA.Signing.Padding) throws -> Certificate.Signature {
|
|
let signature: _RSA.Signing.RSASignature
|
|
|
|
switch try Digest.computeDigest(for: bytes, using: digestAlgorithm) {
|
|
case .insecureSHA1(let sha1):
|
|
signature = try self.signature(for: sha1, padding: padding)
|
|
case .sha256(let sha256):
|
|
signature = try self.signature(for: sha256, padding: padding)
|
|
case .sha384(let sha384):
|
|
signature = try self.signature(for: sha384, padding: padding)
|
|
case .sha512(let sha512):
|
|
signature = try self.signature(for: sha512, padding: padding)
|
|
}
|
|
|
|
return Certificate.Signature(backing: .rsa(signature))
|
|
}
|
|
}
|