@@ -93,13 +93,16 @@ public enum CMS {
93
93
signatureBytes: SignatureBytes ,
94
94
additionalIntermediateCertificates: [ Certificate ] = [ ] ,
95
95
trustRoots: CertificateStore ,
96
- policy: PolicySet
97
- ) async -> SignatureVerificationResult {
96
+ @PolicyBuilder policy: ( ) throws -> some VerifierPolicy
97
+ ) async rethrows -> SignatureVerificationResult {
98
+ let signedData : CMSSignedData
99
+ let signingCert : Certificate
98
100
do {
99
101
let parsedSignature = try CMSContentInfo ( derEncoded: ArraySlice ( signatureBytes) )
100
- guard let signedData = try parsedSignature. signedData else {
102
+ guard let _signedData = try parsedSignature. signedData else {
101
103
return . failure( . init( invalidCMSBlockReason: " Unable to parse signed data " ) )
102
104
}
105
+ signedData = _signedData
103
106
104
107
// We have a bunch of very specific requirements here: in particular, we need to have only one signature. We also only want
105
108
// to tolerate v1 signatures and detached signatures.
@@ -134,9 +137,10 @@ public enum CMS {
134
137
135
138
// Ok, now we need to find the signer. We expect to find them in the list of certificates provided
136
139
// in the signature.
137
- guard let signingCert = try signedData. certificates? . certificate ( signerInfo: signer) else {
140
+ guard let _signingCert = try signedData. certificates? . certificate ( signerInfo: signer) else {
138
141
return . failure( . init( invalidCMSBlockReason: " Unable to locate signing certificate " ) )
139
142
}
143
+ signingCert = _signingCert
140
144
141
145
// Ok at this point we've done the cheap stuff and we're fairly confident we have the entity who should have
142
146
// done the signing. Our next step is to confirm that they did in fact sign the data. For that we have to compute
@@ -145,24 +149,24 @@ public enum CMS {
145
149
guard signingCert. publicKey. isValidSignature ( signature, for: dataBytes, signatureAlgorithm: signatureAlgorithm) else {
146
150
return . failure( . init( invalidCMSBlockReason: " Invalid signature from signing certificate: \( signingCert) " ) )
147
151
}
148
-
149
- // Ok, the signature was signed by the private key associated with this cert. Now we need to validate the certificate.
150
- // This force-unwrap is safe: we know there are certificates because we've located at least one certificate from this set!
151
- var untrustedIntermediates = CertificateStore ( signedData. certificates!)
152
- untrustedIntermediates. insert ( contentsOf: additionalIntermediateCertificates)
153
-
154
- var verifier = Verifier ( rootCertificates: trustRoots, policy: policy)
155
- let result = await verifier. validate ( leafCertificate: signingCert, intermediates: untrustedIntermediates)
156
-
157
- switch result {
158
- case . validCertificate:
159
- return . success( . init( signer: signingCert) )
160
- case . couldNotValidate( let validationFailures) :
161
- return . failure( . unableToValidateSigner( . init( validationFailures: validationFailures, signer: signingCert) ) )
162
- }
163
152
} catch {
164
153
return . failure( . invalidCMSBlock( . init( reason: String ( describing: error) ) ) )
165
154
}
155
+
156
+ // Ok, the signature was signed by the private key associated with this cert. Now we need to validate the certificate.
157
+ // This force-unwrap is safe: we know there are certificates because we've located at least one certificate from this set!
158
+ var untrustedIntermediates = CertificateStore ( signedData. certificates!)
159
+ untrustedIntermediates. insert ( contentsOf: additionalIntermediateCertificates)
160
+
161
+ var verifier = try Verifier ( rootCertificates: trustRoots, policy: policy)
162
+ let result = await verifier. validate ( leafCertificate: signingCert, intermediates: untrustedIntermediates)
163
+
164
+ switch result {
165
+ case . validCertificate:
166
+ return . success( . init( signer: signingCert) )
167
+ case . couldNotValidate( let validationFailures) :
168
+ return . failure( . unableToValidateSigner( . init( validationFailures: validationFailures, signer: signingCert) ) )
169
+ }
166
170
}
167
171
168
172
@_spi ( CMS)
0 commit comments