Skip to content

Commit 5062080

Browse files
Allow signing from key provided as Data in addition to URL (#3831)
Allow signing from key Data in addition to URL
1 parent 7fb7c6d commit 5062080

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

Sources/PackageCollectionsSigning/PackageCollectionSigning.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,44 @@ public protocol PackageCollectionSigner {
2222
/// - collection: The package collection to be signed
2323
/// - certChainPaths: Paths to all DER-encoded certificates in the chain. The certificate used for signing
2424
/// must be the first in the array.
25-
/// - certPrivateKeyPath: Path to the private key (*.pem) of the certificate
25+
/// - privateKeyPEM: Data of the private key (*.pem) of the certificate
2626
/// - certPolicyKey: The key of the `CertificatePolicy` to use for validating certificates
2727
/// - callback: The callback to invoke when the signed collection is available.
2828
func sign(collection: PackageCollectionModel.V1.Collection,
2929
certChainPaths: [URL],
30-
certPrivateKeyPath: URL,
30+
privateKeyPEM: Data,
3131
certPolicyKey: CertificatePolicyKey,
3232
callback: @escaping (Result<PackageCollectionModel.V1.SignedCollection, Error>) -> Void)
3333
}
3434

35+
extension PackageCollectionSigner {
36+
/// Signs package collection using the given certificate and key.
37+
///
38+
/// - Parameters:
39+
/// - collection: The package collection to be signed
40+
/// - certChainPaths: Paths to all DER-encoded certificates in the chain. The certificate used for signing
41+
/// must be the first in the array.
42+
/// - certPrivateKeyPath: Path to the private key (*.pem) of the certificate
43+
/// - certPolicyKey: The key of the `CertificatePolicy` to use for validating certificates
44+
/// - callback: The callback to invoke when the signed collection is available.
45+
public func sign(collection: PackageCollectionModel.V1.Collection,
46+
certChainPaths: [URL],
47+
certPrivateKeyPath: URL,
48+
certPolicyKey: CertificatePolicyKey = .default,
49+
callback: @escaping (Result<PackageCollectionModel.V1.SignedCollection, Error>) -> Void) {
50+
do {
51+
let privateKey = try Data(contentsOf: certPrivateKeyPath)
52+
self.sign(collection: collection,
53+
certChainPaths: certChainPaths,
54+
privateKeyPEM: privateKey,
55+
certPolicyKey: certPolicyKey,
56+
callback: callback)
57+
} catch {
58+
callback(.failure(error))
59+
}
60+
}
61+
}
62+
3563
public protocol PackageCollectionSignatureValidator {
3664
/// Validates a signed package collection.
3765
///
@@ -126,7 +154,7 @@ public struct PackageCollectionSigning: PackageCollectionSigner, PackageCollecti
126154

127155
public func sign(collection: Model.Collection,
128156
certChainPaths: [URL],
129-
certPrivateKeyPath: URL,
157+
privateKeyPEM: Data,
130158
certPolicyKey: CertificatePolicyKey = .default,
131159
callback: @escaping (Result<Model.SignedCollection, Error>) -> Void) {
132160
do {
@@ -148,9 +176,6 @@ public struct PackageCollectionSigning: PackageCollectionSigner, PackageCollecti
148176
certChain: certChainData.map { $0.base64EncodedString() }
149177
)
150178

151-
// Key for signing
152-
let privateKeyPEM = try Data(contentsOf: certPrivateKeyPath)
153-
154179
let privateKey: PrivateKey
155180
switch keyType {
156181
case .RSA:

0 commit comments

Comments
 (0)