11"""Signer implementation for project sigstore.
2-
3- NOTE: SigstoreSigner and -Key are disabled temporarily around
4- the Securesystemslib 1.0 release as the cyclic dependency
5- (securesystemslib -> sigstore-python -> tuf -> securesystemslib)
6- is problematic during API deprecations.
7- See issue #781.
82"""
93
10- import io
4+ import json
115import logging
126from typing import Any , Dict , Optional , Tuple
137from urllib import parse
@@ -66,37 +60,38 @@ def to_dict(self) -> Dict:
6660
6761 def verify_signature (self , signature : Signature , data : bytes ) -> None :
6862 # pylint: disable=import-outside-toplevel,import-error
69- result = None
7063 try :
71- from sigstore .verify import VerificationMaterials , Verifier
64+ from sigstore .errors import VerificationError as SigstoreVerifyError
65+ from sigstore .models import Bundle
66+ from sigstore .verify import Verifier
7267 from sigstore .verify .policy import Identity
73- from sigstore_protobuf_specs .dev .sigstore .bundle .v1 import Bundle
68+ except ImportError as e :
69+ raise VerificationError (IMPORT_ERROR ) from e
7470
71+ try :
7572 verifier = Verifier .production ()
7673 identity = Identity (
7774 identity = self .keyval ["identity" ], issuer = self .keyval ["issuer" ]
7875 )
79- bundle = Bundle ().from_dict (signature .unrecognized_fields ["bundle" ])
80- materials = VerificationMaterials .from_bundle (
81- input_ = io .BytesIO (data ), bundle = bundle , offline = True
82- )
83- result = verifier .verify (materials , identity )
76+ bundle_data = signature .unrecognized_fields ["bundle" ]
77+ bundle = Bundle .from_json (json .dumps (bundle_data ))
8478
85- except Exception as e :
86- logger .info ("Key %s failed to verify sig: %s" , self .keyid , str (e ))
87- raise VerificationError (
88- f"Unknown failure to verify signature by { self .keyid } "
89- ) from e
79+ verifier .verify_artifact (data , bundle , identity )
9080
91- if not result :
81+ except SigstoreVerifyError as e :
9282 logger .info (
9383 "Key %s failed to verify sig: %s" ,
9484 self .keyid ,
95- getattr ( result , "reason" , "" ) ,
85+ e ,
9686 )
9787 raise UnverifiedSignatureError (
9888 f"Failed to verify signature by { self .keyid } "
99- )
89+ ) from e
90+ except Exception as e :
91+ logger .info ("Key %s failed to verify sig: %s" , self .keyid , str (e ))
92+ raise VerificationError (
93+ f"Unknown failure to verify signature by { self .keyid } "
94+ ) from e
10095
10196
10297class SigstoreSigner (Signer ):
@@ -189,9 +184,9 @@ def from_priv_key_uri(
189184
190185 key_identity = public_key .keyval ["identity" ]
191186 key_issuer = public_key .keyval ["issuer" ]
192- if key_issuer != token .expected_certificate_subject :
187+ if key_issuer != token .federated_issuer :
193188 raise ValueError (
194- f"Signer identity issuer { token .expected_certificate_subject } "
189+ f"Signer identity issuer { token .federated_issuer } "
195190 f"did not match key: { key_issuer } "
196191 )
197192 # TODO: should check ambient identity too: unfortunately IdentityToken does
@@ -246,9 +241,7 @@ def import_via_auth(cls) -> Tuple[str, SigstoreKey]:
246241
247242 # authenticate to get the identity and issuer
248243 token = Issuer .production ().identity_token ()
249- return cls .import_ (
250- token .identity , token .expected_certificate_subject , False
251- )
244+ return cls .import_ (token .identity , token .federated_issuer , False )
252245
253246 def sign (self , payload : bytes ) -> Signature :
254247 """Signs payload using the OIDC token on the signer instance.
@@ -273,12 +266,12 @@ def sign(self, payload: bytes) -> Signature:
273266
274267 context = SigningContext .production ()
275268 with context .signer (self ._token ) as sigstore_signer :
276- result = sigstore_signer .sign ( io . BytesIO ( payload ) )
277-
278- bundle = result . to_bundle ()
279-
269+ bundle = sigstore_signer .sign_artifact ( payload )
270+ # We want to access the actual signature, see
271+ # https://github.com/sigstore/protobuf-specs/blob/main/protos/sigstore_bundle.proto
272+ bundle_json = json . loads ( bundle . to_json ())
280273 return Signature (
281274 self .public_key .keyid ,
282- bundle . message_signature . signature . hex () ,
283- {"bundle" : bundle . to_dict () },
275+ bundle_json [ "messageSignature" ][ " signature" ] ,
276+ {"bundle" : bundle_json },
284277 )
0 commit comments