32
32
from cryptography .x509 .oid import ExtendedKeyUsageOID
33
33
34
34
from sigstore ._internal .ctfe import CTKeyring
35
- from sigstore ._internal .keyring import KeyringError , KeyringLookupError
35
+ from sigstore ._internal .keyring import (
36
+ KeyringError ,
37
+ KeyringLookupError ,
38
+ KeyringSignatureError ,
39
+ )
36
40
from sigstore ._utils import DERCert , KeyID , key_id
37
41
from sigstore .errors import Error
38
42
@@ -142,13 +146,34 @@ class InvalidSCTError(Error):
142
146
143
147
def diagnostics (self ) -> str :
144
148
"""Returns diagnostics for the error."""
145
- # We specialize this error case, since it usually indicates one of
146
- # two conditions: either the current sigstore client is out-of-date,
147
- # or that the SCT is well-formed but invalid for the current configuration
148
- # (indicating that the user has asked for the wrong instance).
149
- if isinstance (self .__cause__ , KeyringLookupError ):
150
- return dedent (
151
- f"""
149
+
150
+ ctx = f"\n Context: { self .__context__ } " if self .__context__ else ""
151
+ return dedent (
152
+ f"""
153
+ SCT verification failed.
154
+
155
+ Additional context:
156
+
157
+ Message: { str (self )}
158
+ """
159
+ + ctx
160
+ )
161
+
162
+
163
+ class InvalidSCTKeyError (InvalidSCTError ):
164
+ """
165
+ Raised during SCT verification if the SCT can't be validated against the given keyring.
166
+
167
+ We specialize this error case, since it usually indicates one of
168
+ two conditions: either the current sigstore client is out-of-date,
169
+ or that the SCT is well-formed but invalid for the current configuration
170
+ (indicating that the user has asked for the wrong instance).
171
+ """
172
+
173
+ def diagnostics (self ) -> str :
174
+ """Returns diagnostics for the error."""
175
+ return dedent (
176
+ f"""
152
177
Invalid key ID in SCT: not found in current keyring.
153
178
154
179
This may be a result of an outdated `sigstore` installation.
@@ -161,9 +186,27 @@ def diagnostics(self) -> str:
161
186
162
187
{ self .__cause__ }
163
188
"""
164
- )
189
+ )
165
190
166
- return str (self )
191
+
192
+ class SCTSignatureError (InvalidSCTError ):
193
+ """
194
+ Raised during SCT verification if the signature of the SCT is invalid.
195
+ """
196
+
197
+ def diagnostics (self ) -> str :
198
+ """Returns diagnostics for the error."""
199
+ return dedent (
200
+ f"""
201
+ Invalid signature on SCT.
202
+
203
+ If validating a certificate, the certificate associated with this SCT should not be trusted.
204
+
205
+ Additional context:
206
+
207
+ { self .__cause__ }
208
+ """
209
+ )
167
210
168
211
169
212
def verify_sct (
@@ -214,8 +257,8 @@ def verify_sct(
214
257
key_id = KeyID (sct .log_id ), signature = sct .signature , data = digitally_signed
215
258
)
216
259
except KeyringLookupError as exc :
217
- raise InvalidSCTError (
218
- "Invalid key ID in SCT: not found in current keyring"
219
- ) from exc
260
+ raise InvalidSCTKeyError from exc
261
+ except KeyringSignatureError as exc :
262
+ raise SCTSignatureError from exc
220
263
except KeyringError as exc :
221
264
raise InvalidSCTError from exc
0 commit comments