-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPackageCollectionSigning.swift
334 lines (297 loc) · 13.2 KB
/
PackageCollectionSigning.swift
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021-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 Basics
import Dispatch
import Foundation
import PackageCollectionsModel
#if USE_IMPL_ONLY_IMPORTS
internal import _CryptoExtras
internal import Crypto
internal import X509
#else
import _CryptoExtras
import Crypto
import X509
#endif
public protocol PackageCollectionSigner {
/// Signs package collection using the given certificate and key.
///
/// - Parameters:
/// - collection: The package collection to be signed
/// - certChainPaths: Paths to all DER-encoded certificates in the chain. The certificate used for signing
/// must be the first in the array.
/// - privateKeyPEM: Data of the private key (*.pem) of the certificate
/// - certPolicyKey: The key of the `CertificatePolicy` to use for validating certificates
func sign(
collection: PackageCollectionModel.V1.Collection,
certChainPaths: [URL],
privateKeyPEM: Data,
certPolicyKey: CertificatePolicyKey
) async throws -> PackageCollectionModel.V1.SignedCollection
}
extension PackageCollectionSigner {
/// Signs package collection using the given certificate and key.
///
/// - Parameters:
/// - collection: The package collection to be signed
/// - certChainPaths: Paths to all DER-encoded certificates in the chain. The certificate used for signing
/// must be the first in the array.
/// - certPrivateKeyPath: Path to the private key (*.pem) of the certificate
/// - certPolicyKey: The key of the `CertificatePolicy` to use for validating certificates
public func sign(
collection: PackageCollectionModel.V1.Collection,
certChainPaths: [URL],
certPrivateKeyPath: URL,
certPolicyKey: CertificatePolicyKey = .default
) async throws -> PackageCollectionModel.V1.SignedCollection {
let privateKey = try Data(contentsOf: certPrivateKeyPath)
return try await self.sign(
collection: collection,
certChainPaths: certChainPaths,
privateKeyPEM: privateKey,
certPolicyKey: certPolicyKey
)
}
}
public protocol PackageCollectionSignatureValidator {
/// Validates a signed package collection.
///
/// - Parameters:
/// - signedCollection: The signed package collection
/// - certPolicyKey: The key of the `CertificatePolicy` to use for validating certificates
func validate(
signedCollection: PackageCollectionModel.V1.SignedCollection,
certPolicyKey: CertificatePolicyKey
) async throws
}
// MARK: - Implementation
public actor PackageCollectionSigning: PackageCollectionSigner, PackageCollectionSignatureValidator {
public typealias Model = PackageCollectionModel.V1
private static let minimumRSAKeySizeInBits = 2048
/// URL of the optional directory containing root certificates to be trusted.
private let trustedRootCertsDir: URL?
/// Root certificates to be trusted in additional to those found in `trustedRootCertsDir`
private let additionalTrustedRootCerts: [Certificate]?
/// Internal cache/storage of `CertificatePolicy`s
private let certPolicies: [CertificatePolicyKey: CertificatePolicy]
private let encoder: JSONEncoder
private let decoder: JSONDecoder
private let observabilityScope: ObservabilityScope
public init(
trustedRootCertsDir: URL? = nil,
additionalTrustedRootCerts: [String]? = nil,
observabilityScope: ObservabilityScope
) {
self.trustedRootCertsDir = trustedRootCertsDir
self.additionalTrustedRootCerts = additionalTrustedRootCerts.map { $0.compactMap {
guard let data = Data(base64Encoded: $0) else {
observabilityScope.emit(error: "The certificate \($0) is not in valid base64 encoding")
return nil
}
do {
return try Certificate(derEncoded: Array(data))
} catch {
observabilityScope.emit(
error: "The certificate \($0) is not in valid DER format",
underlyingError: error
)
return nil
}
} }
self.certPolicies = [:]
self.encoder = JSONEncoder.makeWithDefaults()
self.decoder = JSONDecoder.makeWithDefaults()
self.observabilityScope = observabilityScope
}
init(certPolicy: CertificatePolicy, observabilityScope: ObservabilityScope) {
// These should be set through the given CertificatePolicy
self.trustedRootCertsDir = nil
self.additionalTrustedRootCerts = nil
self.certPolicies = [CertificatePolicyKey.custom: certPolicy]
self.encoder = JSONEncoder.makeWithDefaults()
self.decoder = JSONDecoder.makeWithDefaults()
self.observabilityScope = observabilityScope
}
private func getCertificatePolicy(key: CertificatePolicyKey) throws -> CertificatePolicy {
switch key {
case .default(let subjectUserID, let subjectOrganizationalUnit):
// Create new instance each time since contents of trustedRootCertsDir might change
return DefaultCertificatePolicy(
trustedRootCertsDir: self.trustedRootCertsDir,
additionalTrustedRootCerts: self.additionalTrustedRootCerts,
expectedSubjectUserID: subjectUserID,
expectedSubjectOrganizationalUnit: subjectOrganizationalUnit,
observabilityScope: self.observabilityScope
)
case .appleSwiftPackageCollection(let subjectUserID, let subjectOrganizationalUnit):
// Create new instance each time since contents of trustedRootCertsDir might change
return ADPSwiftPackageCollectionCertificatePolicy(
trustedRootCertsDir: self.trustedRootCertsDir,
additionalTrustedRootCerts: self.additionalTrustedRootCerts,
expectedSubjectUserID: subjectUserID,
expectedSubjectOrganizationalUnit: subjectOrganizationalUnit,
observabilityScope: self.observabilityScope
)
case .appleDistribution(let subjectUserID, let subjectOrganizationalUnit):
// Create new instance each time since contents of trustedRootCertsDir might change
return ADPAppleDistributionCertificatePolicy(
trustedRootCertsDir: self.trustedRootCertsDir,
additionalTrustedRootCerts: self.additionalTrustedRootCerts,
expectedSubjectUserID: subjectUserID,
expectedSubjectOrganizationalUnit: subjectOrganizationalUnit,
observabilityScope: self.observabilityScope
)
case .custom:
// Custom `CertificatePolicy` can be set using the internal initializer only
guard let certPolicy = self.certPolicies[key] else {
throw PackageCollectionSigningError.certPolicyNotFound
}
return certPolicy
}
}
public func sign(
collection: Model.Collection,
certChainPaths: [URL],
privateKeyPEM: Data,
certPolicyKey: CertificatePolicyKey = .default
) async throws -> Model.SignedCollection {
let certChainData = try certChainPaths.map { try Data(contentsOf: $0) }
// Check that the certificate is valid
let certChain = try await self.validateCertChain(certChainData, certPolicyKey: certPolicyKey)
let privateKeyPEMString = String(decoding: privateKeyPEM, as: UTF8.self)
let signatureAlgorithm: Signature.Algorithm
let signatureProvider: (Data) throws -> Data
// Determine key type
do {
let privateKey = try P256.Signing.PrivateKey(pemRepresentation: privateKeyPEMString)
signatureAlgorithm = .ES256
signatureProvider = {
try privateKey.signature(for: SHA256.hash(data: $0)).rawRepresentation
}
} catch {
do {
let privateKey = try _RSA.Signing.PrivateKey(pemRepresentation: privateKeyPEMString)
guard privateKey.keySizeInBits >= Self.minimumRSAKeySizeInBits else {
throw PackageCollectionSigningError
.invalidKeySize(minimumBits: Self.minimumRSAKeySizeInBits)
}
signatureAlgorithm = .RS256
signatureProvider = {
try privateKey.signature(for: SHA256.hash(data: $0), padding: Signature.rsaSigningPadding)
.rawRepresentation
}
} catch let error as PackageCollectionSigningError {
throw error
} catch {
throw PackageCollectionSigningError.unsupportedKeyType
}
}
// Generate signature
let signatureData = try Signature.generate(
payload: collection,
certChainData: certChainData,
jsonEncoder: self.encoder,
signatureAlgorithm: signatureAlgorithm,
signatureProvider: signatureProvider
)
guard let signature = String(bytes: signatureData, encoding: .utf8) else {
throw PackageCollectionSigningError.invalidSignature
}
let certificate = certChain.first! // !-safe because certChain cannot be empty at this point
let collectionSignature = Model.Signature(
signature: signature,
certificate: Model.Signature.Certificate(
subject: Model.Signature.Certificate.Name(from: certificate.subject),
issuer: Model.Signature.Certificate.Name(from: certificate.issuer)
)
)
return Model.SignedCollection(collection: collection, signature: collectionSignature)
}
public func validate(
signedCollection: Model.SignedCollection,
certPolicyKey: CertificatePolicyKey = .default
) async throws {
let signatureBytes = Data(signedCollection.signature.signature.utf8).copyBytes()
// Parse the signature
let certChainValidate = { certChainData in
try await self.validateCertChain(certChainData, certPolicyKey: certPolicyKey)
}
let signature = try await Signature.parse(
signatureBytes,
certChainValidate: certChainValidate,
jsonDecoder: self.decoder
)
// Verify the collection embedded in the signature is the same as received
// i.e., the signature is associated with the given collection and not another
guard let collectionFromSignature = try? self.decoder.decode(
Model.Collection.self,
from: signature.payload
),
signedCollection.collection == collectionFromSignature
else {
throw PackageCollectionSigningError.invalidSignature
}
}
private func validateCertChain(
_ certChainData: [Data],
certPolicyKey: CertificatePolicyKey
) async throws -> [Certificate] {
guard !certChainData.isEmpty else {
throw PackageCollectionSigningError.emptyCertChain
}
do {
let certChain = try certChainData.map { try Certificate(derEncoded: Array($0)) }
let certPolicy = try self.getCertificatePolicy(key: certPolicyKey)
do {
try await certPolicy.validate(certChain: certChain)
return certChain
} catch {
self.observabilityScope.emit(
error: "\(certPolicyKey): The certificate chain is invalid",
underlyingError: error
)
if CertificatePolicyError.noTrustedRootCertsConfigured == error as? CertificatePolicyError {
throw PackageCollectionSigningError.noTrustedRootCertsConfigured
} else {
throw PackageCollectionSigningError.invalidCertChain
}
}
} catch let error as PackageCollectionSigningError {
throw error
} catch {
self.observabilityScope.emit(
error: "An error has occurred while validating certificate chain",
underlyingError: error
)
throw PackageCollectionSigningError.invalidCertChain
}
}
}
public enum PackageCollectionSigningError: Error, Equatable {
case certPolicyNotFound
case emptyCertChain
case noTrustedRootCertsConfigured
case invalidCertChain
case invalidSignature
case unsupportedKeyType
case invalidKeySize(minimumBits: Int)
}
extension PackageCollectionModel.V1.Signature.Certificate.Name {
fileprivate init(from name: DistinguishedName) {
self.init(
userID: name.userID,
commonName: name.commonName,
organizationalUnit: name.organizationalUnitName,
organization: name.organizationName
)
}
}