1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Dispatch
import struct Foundation.Data
import struct Foundation.Date
import struct Foundation.URL
import Basics
#if USE_IMPL_ONLY_IMPORTS
@_implementationOnly import SwiftASN1
@_implementationOnly @_spi(DisableValidityCheck) import X509
#else
import SwiftASN1
@_spi(DisableValidityCheck) import X509
#endif
extension SignatureProviderProtocol {
@PolicyBuilder
func buildPolicySet(configuration: VerifierConfiguration, httpClient: HTTPClient) -> some VerifierPolicy {
_CodeSigningPolicy()
_ADPCertificatePolicy()
let now = Date()
switch (configuration.certificateExpiration, configuration.certificateRevocation) {
case (.enabled(let expiryValidationTime), .strict(let revocationValidationTime)):
RFC5280Policy(validationTime: expiryValidationTime ?? now)
_OCSPVerifierPolicy(
failureMode: .hard,
httpClient: httpClient,
validationTime: revocationValidationTime ?? now
)
case (.enabled(let expiryValidationTime), .allowSoftFail(let revocationValidationTime)):
RFC5280Policy(validationTime: expiryValidationTime ?? now)
_OCSPVerifierPolicy(
failureMode: .soft,
httpClient: httpClient,
validationTime: revocationValidationTime ?? now
)
case (.enabled(let expiryValidationTime), .disabled):
RFC5280Policy(validationTime: expiryValidationTime ?? now)
case (.disabled, .strict(let revocationValidationTime)):
// Always do expiry check (and before) if revocation check is enabled
RFC5280Policy(validationTime: revocationValidationTime ?? now)
_OCSPVerifierPolicy(
failureMode: .hard,
httpClient: httpClient,
validationTime: revocationValidationTime ?? now
)
case (.disabled, .allowSoftFail(let revocationValidationTime)):
// Always do expiry check (and before) if revocation check is enabled
RFC5280Policy(validationTime: revocationValidationTime ?? now)
_OCSPVerifierPolicy(
failureMode: .soft,
httpClient: httpClient,
validationTime: revocationValidationTime ?? now
)
case (.disabled, .disabled):
// We should still do basic certificate validations even if expiry check is disabled
RFC5280Policy.withValidityCheckDisabled()
}
}
}
/// Policy for code signing certificates.
struct _CodeSigningPolicy: VerifierPolicy {
let verifyingCriticalExtensions: [ASN1ObjectIdentifier] = [
ASN1ObjectIdentifier.X509ExtensionID.keyUsage,
ASN1ObjectIdentifier.X509ExtensionID.extendedKeyUsage,
]
func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) async -> PolicyEvaluationResult {
let isCodeSigning = (
try? chain.leaf.extensions.extendedKeyUsage?.contains(ExtendedKeyUsage.Usage.codeSigning)
) ??
false
guard isCodeSigning else {
return .failsToMeetPolicy(reason: "Certificate \(chain.leaf) does not have code signing extended key usage")
}
return .meetsPolicy
}
}
/// Policy for ADP certificates.
struct _ADPCertificatePolicy: VerifierPolicy {
/// Include custom marker extensions (which can be critical) so they would not
/// be considered unhandled and cause certificate chain validation to fail.
let verifyingCriticalExtensions: [ASN1ObjectIdentifier] = Self.swiftPackageMarkers
+ Self.developmentMarkers
// Marker extensions for Swift Package certificate
private static let swiftPackageMarkers: [ASN1ObjectIdentifier] = [
// This is not a critical extension but including it just in case
ASN1ObjectIdentifier.NameAttributes.adpSwiftPackageMarker,
]
// Marker extensions for Development certificate (included for testing)
private static let developmentMarkers: [ASN1ObjectIdentifier] = [
[1, 2, 840, 113_635, 100, 6, 1, 2],
[1, 2, 840, 113_635, 100, 6, 1, 12],
]
func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) async -> PolicyEvaluationResult {
// Not policing anything here. This policy is mainly for
// listing marker extensions to prevent chain validation
// from failing prematurely.
.meetsPolicy
}
}
struct _OCSPVerifierPolicy: VerifierPolicy {
private static let cacheTTL: DispatchTimeInterval = .seconds(5 * 60)
private let cache = ThreadSafeKeyValueStore<
UnverifiedCertificateChain,
(result: PolicyEvaluationResult, expires: DispatchTime)
>()
private var underlying: OCSPVerifierPolicy<_OCSPRequester>
let verifyingCriticalExtensions: [ASN1ObjectIdentifier] = []
/// Initializes an `_OCSPVerifierPolicy` that caches its results.
///
/// - Parameters:
/// - failureMode: `OCSPFailureMode` that defines policy failure in event of failure.
/// Possible values are `hard` (OCSP request failure and unknown status
/// not allowed) or `soft` (OCSP request failure and unknown status allowed).
/// - httpClient: `HTTPClient` that backs`_OCSPRequester` for making OCSP requests.
/// - validationTime: The time used to decide if the OCSP request is relatively recent. It is
/// considered a failure if the request is too old.
init(failureMode: OCSPFailureMode, httpClient: HTTPClient, validationTime: Date) {
self.underlying = OCSPVerifierPolicy(
failureMode: failureMode,
requester: _OCSPRequester(httpClient: httpClient),
validationTime: validationTime
)
}
mutating func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) async -> PolicyEvaluationResult {
// Look for cached result
if let cached = self.cache[chain], cached.expires < .now() {
return cached.result
}
// This makes HTTP requests
let result = await self.underlying.chainMeetsPolicyRequirements(chain: chain)
// Save result to cache
self.cache[chain] = (result: result, expires: .now() + Self.cacheTTL)
return result
}
}
private struct _OCSPRequester: OCSPRequester {
let httpClient: HTTPClient
func query(request: [UInt8], uri: String) async -> OCSPRequesterQueryResult {
guard let url = URL(string: uri), let host = url.host else {
return .terminalError(SwiftOCSPRequesterError.invalidURL(uri))
}
do {
let response = try await self.httpClient.post(
url,
body: Data(request),
headers: [
"Content-Type": "application/ocsp-request",
"Host": host,
]
)
guard response.statusCode == 200 else {
throw SwiftOCSPRequesterError.invalidResponse(statusCode: response.statusCode)
}
guard let responseBody = response.body else {
throw SwiftOCSPRequesterError.emptyResponse
}
return .response(Array(responseBody))
} catch {
return .nonTerminalError(error)
}
}
}
enum SwiftOCSPRequesterError: Error {
case invalidURL(String)
case emptyResponse
case invalidResponse(statusCode: Int)
}
|