Skip to content

Commit 3f48bb8

Browse files
committed
[Collections] Tighten import conditions
Some `Security` framework APIs are available on macOS only
1 parent aa681bd commit 3f48bb8

File tree

8 files changed

+33
-17
lines changed

8 files changed

+33
-17
lines changed

Sources/PackageCollectionsSigning/Certificate/Certificate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010

1111
import struct Foundation.Data
1212

13-
#if canImport(Security)
13+
#if os(macOS)
1414
import Security
1515
#endif
1616

17-
#if canImport(Security)
17+
#if os(macOS)
1818
typealias Certificate = CoreCertificate
1919
#else
2020
typealias Certificate = BoringSSLCertificate
2121
#endif
2222

2323
// MARK: - Certificate implementation using the Security framework
2424

25-
#if canImport(Security)
25+
#if os(macOS)
2626
struct CoreCertificate {
2727
let underlying: SecCertificate
2828

Sources/PackageCollectionsSigning/Certificate/CertificatePolicy.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import struct Foundation.URL
1616

1717
import TSCBasic
1818

19-
#if canImport(Security)
19+
#if os(macOS)
2020
import Security
2121
#endif
2222

@@ -53,7 +53,7 @@ extension CertificatePolicy {
5353
return wrappedCallback(.failure(CertificatePolicyError.emptyCertChain))
5454
}
5555

56-
#if canImport(Security)
56+
#if os(macOS)
5757
let policy = SecPolicyCreateBasicX509()
5858
let revocationPolicy = SecPolicyCreateRevocation(kSecRevocationOCSPMethod)
5959

@@ -99,7 +99,7 @@ extension CertificatePolicy {
9999

100100
extension CertificatePolicy {
101101
func hasExtension(oid: String, in certificate: Certificate) throws -> Bool {
102-
#if canImport(Security)
102+
#if os(macOS)
103103
guard let dict = SecCertificateCopyValues(certificate.underlying, [oid as CFString] as CFArray, nil) as? [CFString: Any] else {
104104
throw CertificatePolicyError.extensionFailure
105105
}
@@ -110,7 +110,7 @@ extension CertificatePolicy {
110110
}
111111

112112
func hasExtendedKeyUsage(_ usage: CertificateExtendedKeyUsage, in certificate: Certificate) throws -> Bool {
113-
#if canImport(Security)
113+
#if os(macOS)
114114
guard let dict = SecCertificateCopyValues(certificate.underlying, [kSecOIDExtendedKeyUsage] as CFArray, nil) as? [CFString: Any] else {
115115
throw CertificatePolicyError.extensionFailure
116116
}
@@ -127,7 +127,7 @@ extension CertificatePolicy {
127127
/// Checks that the certificate supports OCSP. This **must** be done before calling `verify` to ensure
128128
/// the necessary properties are in place to trigger revocation check.
129129
func supportsOCSP(certificate: Certificate) throws -> Bool {
130-
#if canImport(Security)
130+
#if os(macOS)
131131
// Check that certificate has "Certificate Authority Information Access" extension and includes OCSP as access method.
132132
// The actual revocation check will be done by the Security framework in `verify`.
133133
guard let dict = SecCertificateCopyValues(certificate.underlying, [kSecOIDAuthorityInfoAccess] as CFArray, nil) as? [CFString: Any] else { // ignore error
@@ -147,7 +147,7 @@ extension CertificatePolicy {
147147
enum CertificateExtendedKeyUsage {
148148
case codeSigning
149149

150-
#if canImport(Security)
150+
#if os(macOS)
151151
var data: Data {
152152
switch self {
153153
case .codeSigning:

Sources/PackageCollectionsSigning/Key/Key+EC.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct ECPrivateKey: PrivateKey {
2121
init<Data>(pem data: Data) throws where Data: DataProtocol {
2222
let pem = String(decoding: data, as: UTF8.self)
2323
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
24-
if #available(macOS 11, *) {
24+
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
2525
self.underlying = try CryptoECPrivateKey(pemRepresentation: pem)
2626
} else {
2727
let pemDocument = try ASN1.PEMDocument(pemString: pem)
@@ -45,7 +45,7 @@ struct ECPublicKey: PublicKey {
4545
init<Data>(pem data: Data) throws where Data: DataProtocol {
4646
let pem = String(decoding: data, as: UTF8.self)
4747
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
48-
if #available(macOS 11, *) {
48+
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
4949
self.underlying = try CryptoECPublicKey(pemRepresentation: pem)
5050
} else {
5151
let pemDocument = try ASN1.PEMDocument(pemString: pem)

Sources/PackageCollectionsSigning/Key/Key+RSA.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
import Foundation
1212

13-
#if canImport(Security)
13+
#if os(macOS)
1414
import Security
1515
#endif
1616

17-
#if canImport(Security)
17+
#if os(macOS)
1818
typealias RSAPublicKey = CoreRSAPublicKey
1919
typealias RSAPrivateKey = CoreRSAPrivateKey
2020
#else
@@ -24,7 +24,7 @@ typealias RSAPrivateKey = BoringSSLRSAPrivateKey
2424

2525
// MARK: - RSA key implementations using the Security framework
2626

27-
#if canImport(Security)
27+
#if os(macOS)
2828
struct CoreRSAPrivateKey: PrivateKey {
2929
let underlying: SecKey
3030

Sources/PackageCollectionsSigning/Signing/Signing+RSAKey.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
import struct Foundation.Data
1212

13-
#if canImport(Security)
13+
#if os(macOS)
1414
import Security
1515
#endif
1616

1717
// MARK: - MessageSigner and MessageValidator conformance using the Security framework
1818

19-
#if canImport(Security)
19+
#if os(macOS)
2020
extension CoreRSAPrivateKey {
2121
func sign(message: Data) throws -> Data {
2222
var error: Unmanaged<CFError>?

Tests/PackageCollectionsSigningTests/KeyTests+EC.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ class ECKeyTests: XCTestCase {
3131
}
3232

3333
func testPublicKeyFromPEM() throws {
34+
if !isSupportedPlatform {
35+
try XCTSkipIf(true)
36+
}
37+
3438
XCTAssertNoThrow(try ECPublicKey(pem: ecPublicKey.bytes))
3539
}
3640

3741
func testPrivateKeyFromPEM() throws {
42+
if !isSupportedPlatform {
43+
try XCTSkipIf(true)
44+
}
45+
3846
XCTAssertNoThrow(try ECPrivateKey(pem: ecPrivateKey.bytes))
3947
}
4048
}

Tests/PackageCollectionsSigningTests/SigningTests+ECKey.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import XCTest
1515

1616
class ECKeySigningTests: XCTestCase {
1717
func test_signAndValidate_happyCase() throws {
18+
if !isSupportedPlatform {
19+
try XCTSkipIf(true)
20+
}
21+
1822
let privateKey = try ECPrivateKey(pem: ecPrivateKey.bytes)
1923
let publicKey = try ECPublicKey(pem: ecPublicKey.bytes)
2024

@@ -24,6 +28,10 @@ class ECKeySigningTests: XCTestCase {
2428
}
2529

2630
func test_signAndValidate_mismatch() throws {
31+
if !isSupportedPlatform {
32+
try XCTSkipIf(true)
33+
}
34+
2735
let privateKey = try ECPrivateKey(pem: ecPrivateKey.bytes)
2836
let publicKey = try ECPublicKey(pem: ecPublicKey.bytes)
2937

Tests/PackageCollectionsSigningTests/Utilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414
@testable import PackageCollectionsSigning
1515
import TSCBasic
1616

17-
#if canImport(Security)
17+
#if os(macOS)
1818
let isSupportedPlatform = true
1919
#else
2020
let isSupportedPlatform = false

0 commit comments

Comments
 (0)