Skip to content

Commit 5dc6593

Browse files
committed
PackageCollectionsSigning: make some of the private API more public
This extends the visibility of some of the private implementation to the package level to allow use for testing without `@testable` imports.
1 parent ae0ec08 commit 5dc6593

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

Sources/PackageCollectionsSigning/CertificatePolicy.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public enum CertificatePolicyKey: Hashable, CustomStringConvertible {
5454

5555
// MARK: - Certificate policies
5656

57-
protocol CertificatePolicy {
57+
package protocol CertificatePolicy {
5858
/// Validates the given certificate chain.
5959
///
6060
/// - Parameters:
@@ -71,11 +71,11 @@ extension CertificatePolicy {
7171
/// - Parameters:
7272
/// - certChain: The certificate being verified must be the first element of the array, with its issuer the next
7373
/// element and so on, and the root CA certificate is last.
74-
func validate(certChain: [Certificate]) async throws {
74+
package func validate(certChain: [Certificate]) async throws {
7575
try await self.validate(certChain: certChain, validationTime: Date())
7676
}
7777

78-
func verify(
78+
package func verify(
7979
certChain: [Certificate],
8080
trustedRoots: [Certificate]?,
8181
@PolicyBuilder policies: () -> some VerifierPolicy,
@@ -114,7 +114,7 @@ extension CertificatePolicy {
114114
}
115115
}
116116

117-
enum CertificatePolicyError: Error, Equatable {
117+
package enum CertificatePolicyError: Error, Equatable {
118118
case noTrustedRootCertsConfigured
119119
case emptyCertChain
120120
case invalidCertChain
@@ -128,7 +128,7 @@ enum CertificatePolicyError: Error, Equatable {
128128
/// - The certificate must use either 256-bit EC (recommended) or 2048-bit RSA key.
129129
/// - The certificate must not be revoked. The certificate authority must support OCSP.
130130
/// - The certificate chain is valid and root certificate must be trusted.
131-
struct DefaultCertificatePolicy: CertificatePolicy {
131+
package struct DefaultCertificatePolicy: CertificatePolicy {
132132
let trustedRoots: [Certificate]
133133
let expectedSubjectUserID: String?
134134
let expectedSubjectOrganizationalUnit: String?
@@ -146,7 +146,7 @@ struct DefaultCertificatePolicy: CertificatePolicy {
146146
/// user configured and dynamic, while this is configured by SwiftPM and static.
147147
/// - expectedSubjectUserID: The subject user ID that must match if specified.
148148
/// - expectedSubjectOrganizationalUnit: The subject organizational unit name that must match if specified.
149-
init(
149+
package init(
150150
trustedRootCertsDir: URL?,
151151
additionalTrustedRootCerts: [Certificate]?,
152152
expectedSubjectUserID: String? = nil,
@@ -168,7 +168,7 @@ struct DefaultCertificatePolicy: CertificatePolicy {
168168
self.observabilityScope = observabilityScope
169169
}
170170

171-
func validate(certChain: [Certificate], validationTime: Date) async throws {
171+
package func validate(certChain: [Certificate], validationTime: Date) async throws {
172172
guard !certChain.isEmpty else {
173173
throw CertificatePolicyError.emptyCertChain
174174
}
@@ -202,7 +202,7 @@ struct DefaultCertificatePolicy: CertificatePolicy {
202202
///
203203
/// This has the same requirements as `DefaultCertificatePolicy` plus additional
204204
/// marker extensions for Swift Package Collection certifiicates.
205-
struct ADPSwiftPackageCollectionCertificatePolicy: CertificatePolicy {
205+
package struct ADPSwiftPackageCollectionCertificatePolicy: CertificatePolicy {
206206
let trustedRoots: [Certificate]
207207
let expectedSubjectUserID: String?
208208
let expectedSubjectOrganizationalUnit: String?
@@ -220,7 +220,7 @@ struct ADPSwiftPackageCollectionCertificatePolicy: CertificatePolicy {
220220
/// user configured and dynamic, while this is configured by SwiftPM and static.
221221
/// - expectedSubjectUserID: The subject user ID that must match if specified.
222222
/// - expectedSubjectOrganizationalUnit: The subject organizational unit name that must match if specified.
223-
init(
223+
package init(
224224
trustedRootCertsDir: URL?,
225225
additionalTrustedRootCerts: [Certificate]?,
226226
expectedSubjectUserID: String? = nil,
@@ -242,7 +242,7 @@ struct ADPSwiftPackageCollectionCertificatePolicy: CertificatePolicy {
242242
self.observabilityScope = observabilityScope
243243
}
244244

245-
func validate(certChain: [Certificate], validationTime: Date) async throws {
245+
package func validate(certChain: [Certificate], validationTime: Date) async throws {
246246
guard !certChain.isEmpty else {
247247
throw CertificatePolicyError.emptyCertChain
248248
}
@@ -353,13 +353,13 @@ struct ADPAppleDistributionCertificatePolicy: CertificatePolicy {
353353
// MARK: - Verifier policies
354354

355355
/// Policy for code signing certificates.
356-
struct _CodeSigningPolicy: VerifierPolicy {
357-
let verifyingCriticalExtensions: [ASN1ObjectIdentifier] = [
356+
package struct _CodeSigningPolicy: VerifierPolicy {
357+
package let verifyingCriticalExtensions: [ASN1ObjectIdentifier] = [
358358
ASN1ObjectIdentifier.X509ExtensionID.keyUsage,
359359
ASN1ObjectIdentifier.X509ExtensionID.extendedKeyUsage,
360360
]
361361

362-
func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) async -> PolicyEvaluationResult {
362+
package func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) async -> PolicyEvaluationResult {
363363
let isCodeSigning = (
364364
try? chain.leaf.extensions.extendedKeyUsage?.contains(ExtendedKeyUsage.Usage.codeSigning)
365365
) ?? false
@@ -368,6 +368,8 @@ struct _CodeSigningPolicy: VerifierPolicy {
368368
}
369369
return .meetsPolicy
370370
}
371+
372+
package init() {}
371373
}
372374

373375
/// Policy for revocation check via OCSP.

Sources/PackageCollectionsSigning/PackageCollectionSigning.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public actor PackageCollectionSigning: PackageCollectionSigner, PackageCollectio
129129
self.observabilityScope = observabilityScope
130130
}
131131

132-
init(certPolicy: CertificatePolicy, observabilityScope: ObservabilityScope) {
132+
package init(certPolicy: CertificatePolicy, observabilityScope: ObservabilityScope) {
133133
// These should be set through the given CertificatePolicy
134134
self.trustedRootCertsDir = nil
135135
self.additionalTrustedRootCerts = nil

Sources/PackageCollectionsSigning/Signature.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@ import X509
3838
// The logic in this source file loosely follows https://www.rfc-editor.org/rfc/rfc7515.html
3939
// for JSON Web Signature (JWS).
4040

41-
struct Signature {
42-
let header: Header
43-
let payload: Data
41+
package struct Signature {
42+
package let header: Header
43+
package let payload: Data
4444
let signature: Data
4545
}
4646

4747
extension Signature {
48-
enum Algorithm: String, Codable {
48+
package enum Algorithm: String, Codable {
4949
case RS256 // RSASSA-PKCS1-v1_5 using SHA-256
5050
case ES256 // ECDSA using P-256 and SHA-256
5151
}
5252

53-
struct Header: Equatable, Codable {
53+
package struct Header: Equatable, Codable {
5454
// https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.1
55-
let algorithm: Algorithm
55+
package let algorithm: Algorithm
5656

5757
/// Base64 encoded certificate chain
58-
let certChain: [String]
58+
package let certChain: [String]
5959

6060
enum CodingKeys: String, CodingKey {
6161
case algorithm = "alg"
@@ -66,9 +66,9 @@ extension Signature {
6666

6767
// Reference: https://github.com/vapor/jwt-kit/blob/master/Sources/JWTKit/JWTSerializer.swift
6868
extension Signature {
69-
static let rsaSigningPadding = _RSA.Signing.Padding.insecurePKCS1v1_5
69+
package static let rsaSigningPadding = _RSA.Signing.Padding.insecurePKCS1v1_5
7070

71-
static func generate(
71+
package static func generate(
7272
payload: some Encodable,
7373
certChainData: [Data],
7474
jsonEncoder: JSONEncoder,
@@ -102,9 +102,9 @@ extension Signature {
102102

103103
// Reference: https://github.com/vapor/jwt-kit/blob/master/Sources/JWTKit/JWTParser.swift
104104
extension Signature {
105-
typealias CertChainValidate = ([Data]) async throws -> [Certificate]
105+
package typealias CertChainValidate = ([Data]) async throws -> [Certificate]
106106

107-
static func parse(
107+
package static func parse(
108108
_ signature: String,
109109
certChainValidate: CertChainValidate,
110110
jsonDecoder: JSONDecoder
@@ -113,7 +113,7 @@ extension Signature {
113113
return try await Self.parse(bytes, certChainValidate: certChainValidate, jsonDecoder: jsonDecoder)
114114
}
115115

116-
static func parse(
116+
package static func parse(
117117
_ signature: some DataProtocol,
118118
certChainValidate: CertChainValidate,
119119
jsonDecoder: JSONDecoder
@@ -180,7 +180,7 @@ extension Signature {
180180
}
181181
}
182182

183-
enum SignatureError: Error {
183+
package enum SignatureError: Error {
184184
case malformedSignature
185185
case invalidSignature
186186
case invalidPublicKey

0 commit comments

Comments
 (0)