@@ -55,6 +55,18 @@ public enum SignatureProvider {
5555 observabilityScope: observabilityScope
5656 )
5757 }
58+
59+ public static func extractSigningEntity(
60+ signature: [ UInt8 ] ,
61+ format: SignatureFormat ,
62+ verifierConfiguration: VerifierConfiguration
63+ ) async throws -> SigningEntity {
64+ let provider = format. provider
65+ return try await provider. extractSigningEntity (
66+ signature: signature,
67+ verifierConfiguration: verifierConfiguration
68+ )
69+ }
5870}
5971
6072public struct VerifierConfiguration {
@@ -162,6 +174,11 @@ protocol SignatureProviderProtocol {
162174 verifierConfiguration: VerifierConfiguration ,
163175 observabilityScope: ObservabilityScope
164176 ) async throws -> SignatureStatus
177+
178+ func extractSigningEntity(
179+ signature: [ UInt8 ] ,
180+ verifierConfiguration: VerifierConfiguration
181+ ) async throws -> SigningEntity
165182}
166183
167184// MARK: - CMS signature provider
@@ -232,34 +249,66 @@ struct CMSSignatureProvider: SignatureProviderProtocol {
232249 }
233250 }
234251
252+ private func isValidSignature(
253+ signature: [ UInt8 ] ,
254+ content: [ UInt8 ] ,
255+ verifierConfiguration: VerifierConfiguration
256+ ) async throws -> CMS . SignatureVerificationResult {
257+ var trustRoots : [ Certificate ] = [ ]
258+ if verifierConfiguration. includeDefaultTrustStore {
259+ trustRoots. append ( contentsOf: CertificateStores . defaultTrustRoots)
260+ }
261+ trustRoots. append ( contentsOf: try verifierConfiguration. trustedRoots. map { try Certificate ( $0) } )
262+
263+ return await CMS . isValidSignature (
264+ dataBytes: content,
265+ signatureBytes: signature,
266+ // The intermediates supplied here will be combined with those
267+ // included in the signature to build cert chain for validation.
268+ //
269+ // Those who use ADP certs for signing are not required to provide
270+ // the entire cert chain, thus we must supply WWDR intermediates
271+ // here so that the chain can be constructed during validation.
272+ // Whether the signing cert is trusted still depends on whether
273+ // the WWDR roots are in the trust store or not, which by default
274+ // they are but user may disable that through configuration.
275+ additionalIntermediateCertificates: Certificates . wwdrIntermediates,
276+ trustRoots: CertificateStore ( trustRoots) ,
277+ policy: self . buildPolicySet ( configuration: verifierConfiguration, httpClient: self . httpClient)
278+ )
279+ }
280+
281+ func extractSigningEntity(
282+ signature: [ UInt8 ] ,
283+ verifierConfiguration: VerifierConfiguration
284+ ) async throws -> SigningEntity {
285+ let result = try await isValidSignature (
286+ signature: signature,
287+ content: [ ] ,
288+ verifierConfiguration: verifierConfiguration
289+ )
290+
291+ switch result {
292+ case . success( let valid) :
293+ return SigningEntity . from ( certificate: valid. signer)
294+ case . failure( CMS . VerificationError . unableToValidateSigner( let invalid) ) :
295+ return SigningEntity . from ( certificate: invalid. signer)
296+ case . failure( let error) :
297+ throw error
298+ }
299+ }
300+
235301 func status(
236302 signature: [ UInt8 ] ,
237303 content: [ UInt8 ] ,
238304 verifierConfiguration: VerifierConfiguration ,
239305 observabilityScope: ObservabilityScope
240306 ) async throws -> SignatureStatus {
241307 do {
242- var trustRoots : [ Certificate ] = [ ]
243- if verifierConfiguration. includeDefaultTrustStore {
244- trustRoots. append ( contentsOf: CertificateStores . defaultTrustRoots)
245- }
246- trustRoots. append ( contentsOf: try verifierConfiguration. trustedRoots. map { try Certificate ( $0) } )
247-
248- let result = await CMS . isValidSignature (
249- dataBytes: content,
250- signatureBytes: signature,
251- // The intermediates supplied here will be combined with those
252- // included in the signature to build cert chain for validation.
253- //
254- // Those who use ADP certs for signing are not required to provide
255- // the entire cert chain, thus we must supply WWDR intermediates
256- // here so that the chain can be constructed during validation.
257- // Whether the signing cert is trusted still depends on whether
258- // the WWDR roots are in the trust store or not, which by default
259- // they are but user may disable that through configuration.
260- additionalIntermediateCertificates: Certificates . wwdrIntermediates,
261- trustRoots: CertificateStore ( trustRoots) ,
262- policy: self . buildPolicySet ( configuration: verifierConfiguration, httpClient: self . httpClient)
308+ let result = try await isValidSignature (
309+ signature: signature,
310+ content: content,
311+ verifierConfiguration: verifierConfiguration
263312 )
264313
265314 switch result {
0 commit comments