1
1
"""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.
8
2
"""
9
3
10
- import io
4
+ import json
11
5
import logging
12
6
from typing import Any , Dict , Optional , Tuple
13
7
from urllib import parse
@@ -66,37 +60,38 @@ def to_dict(self) -> Dict:
66
60
67
61
def verify_signature (self , signature : Signature , data : bytes ) -> None :
68
62
# pylint: disable=import-outside-toplevel,import-error
69
- result = None
70
63
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
72
67
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
74
70
71
+ try :
75
72
verifier = Verifier .production ()
76
73
identity = Identity (
77
74
identity = self .keyval ["identity" ], issuer = self .keyval ["issuer" ]
78
75
)
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 ))
84
78
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 )
90
80
91
- if not result :
81
+ except SigstoreVerifyError as e :
92
82
logger .info (
93
83
"Key %s failed to verify sig: %s" ,
94
84
self .keyid ,
95
- getattr ( result , "reason" , "" ) ,
85
+ e ,
96
86
)
97
87
raise UnverifiedSignatureError (
98
88
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
100
95
101
96
102
97
class SigstoreSigner (Signer ):
@@ -189,9 +184,9 @@ def from_priv_key_uri(
189
184
190
185
key_identity = public_key .keyval ["identity" ]
191
186
key_issuer = public_key .keyval ["issuer" ]
192
- if key_issuer != token .expected_certificate_subject :
187
+ if key_issuer != token .federated_issuer :
193
188
raise ValueError (
194
- f"Signer identity issuer { token .expected_certificate_subject } "
189
+ f"Signer identity issuer { token .federated_issuer } "
195
190
f"did not match key: { key_issuer } "
196
191
)
197
192
# TODO: should check ambient identity too: unfortunately IdentityToken does
@@ -246,9 +241,7 @@ def import_via_auth(cls) -> Tuple[str, SigstoreKey]:
246
241
247
242
# authenticate to get the identity and issuer
248
243
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 )
252
245
253
246
def sign (self , payload : bytes ) -> Signature :
254
247
"""Signs payload using the OIDC token on the signer instance.
@@ -273,12 +266,12 @@ def sign(self, payload: bytes) -> Signature:
273
266
274
267
context = SigningContext .production ()
275
268
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 ())
280
273
return Signature (
281
274
self .public_key .keyid ,
282
- bundle . message_signature . signature . hex () ,
283
- {"bundle" : bundle . to_dict () },
275
+ bundle_json [ "messageSignature" ][ " signature" ] ,
276
+ {"bundle" : bundle_json },
284
277
)
0 commit comments