Skip to content

Commit 876cd01

Browse files
committed
replace more protobuf models
1 parent 0cc561e commit 876cd01

File tree

7 files changed

+76
-156
lines changed

7 files changed

+76
-156
lines changed

sigstore/_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from rich.console import Console
3131
from rich.logging import RichHandler
3232
from sigstore_models.bundle.v1 import Bundle as RawBundle
33-
from sigstore_protobuf_specs.dev.sigstore.common.v1 import HashAlgorithm
33+
from sigstore_models.common.v1 import HashAlgorithm
3434
from typing_extensions import TypeAlias
3535

3636
from sigstore import __version__, dsse

sigstore/_internal/key_details.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313
# limitations under the License.
1414

1515
"""
16-
Utilities for getting the sigstore_protobuf_specs.dev.sigstore.common.v1.PublicKeyDetails.
16+
Utilities for getting PublicKeyDetails.
1717
"""
1818

19-
from typing import cast
20-
2119
from cryptography.hazmat.primitives.asymmetric import ec, ed25519, padding, rsa
2220
from cryptography.x509 import Certificate
23-
from sigstore_protobuf_specs.dev.sigstore.common import v1
21+
from sigstore_models.common.v1 import PublicKeyDetails
2422

2523

26-
def _get_key_details(certificate: Certificate) -> v1.PublicKeyDetails:
24+
def _get_key_details(certificate: Certificate) -> PublicKeyDetails:
2725
"""
2826
Determine PublicKeyDetails from the Certificate.
2927
We disclude the unrecommended types.
@@ -35,38 +33,38 @@ def _get_key_details(certificate: Certificate) -> v1.PublicKeyDetails:
3533
params = certificate.signature_algorithm_parameters
3634
if isinstance(public_key, ec.EllipticCurvePublicKey):
3735
if isinstance(public_key.curve, ec.SECP256R1):
38-
key_details = v1.PublicKeyDetails.PKIX_ECDSA_P256_SHA_256
36+
key_details = PublicKeyDetails.PKIX_ECDSA_P256_SHA_256
3937
elif isinstance(public_key.curve, ec.SECP384R1):
40-
key_details = v1.PublicKeyDetails.PKIX_ECDSA_P384_SHA_384
38+
key_details = PublicKeyDetails.PKIX_ECDSA_P384_SHA_384
4139
elif isinstance(public_key.curve, ec.SECP521R1):
42-
key_details = v1.PublicKeyDetails.PKIX_ECDSA_P521_SHA_512
40+
key_details = PublicKeyDetails.PKIX_ECDSA_P521_SHA_512
4341
else:
4442
raise ValueError(f"Unsupported EC curve: {public_key.curve.name}")
4543
elif isinstance(public_key, rsa.RSAPublicKey):
4644
if public_key.key_size == 3072:
4745
if isinstance(params, padding.PKCS1v15):
48-
key_details = v1.PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256
46+
key_details = PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256
4947
elif isinstance(params, padding.PSS):
50-
key_details = v1.PublicKeyDetails.PKIX_RSA_PSS_3072_SHA256
48+
key_details = PublicKeyDetails.PKIX_RSA_PSS_3072_SHA256
5149
else:
5250
raise ValueError(
5351
f"Unsupported public key type, size, and padding: {type(public_key)}, {public_key.key_size}, {params}"
5452
)
5553
elif public_key.key_size == 4096:
5654
if isinstance(params, padding.PKCS1v15):
57-
key_details = v1.PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256
55+
key_details = PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256
5856
elif isinstance(params, padding.PSS):
59-
key_details = v1.PublicKeyDetails.PKIX_RSA_PSS_3072_SHA256
57+
key_details = PublicKeyDetails.PKIX_RSA_PSS_3072_SHA256
6058
else:
6159
raise ValueError(
6260
f"Unsupported public key type, size, and padding: {type(public_key)}, {public_key.key_size}, {params}"
6361
)
6462
else:
6563
raise ValueError(f"Unsupported RSA key size: {public_key.key_size}")
6664
elif isinstance(public_key, ed25519.Ed25519PublicKey):
67-
key_details = v1.PublicKeyDetails.PKIX_ED25519
65+
key_details = PublicKeyDetails.PKIX_ED25519
6866
# There is likely no need to explicitly detect PKIX_ED25519_PH, especially since the cryptography
6967
# library does not yet support Ed25519ph.
7068
else:
7169
raise ValueError(f"Unsupported public key type: {type(public_key)}")
72-
return cast(v1.PublicKeyDetails, key_details)
70+
return key_details

sigstore/_internal/merkle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,5 @@ def verify_merkle_inclusion(entry: TransparencyLogEntry) -> None:
122122
if calc_hash != inclusion_proof.root_hash:
123123
raise VerificationError(
124124
f"inclusion proof contains invalid root hash: expected {inclusion_proof}, calculated "
125-
f"{calc_hash}"
125+
f"{calc_hash.hex()}"
126126
)

sigstore/_internal/rekor/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from sigstore.hashes import Hashed
3232

3333
if typing.TYPE_CHECKING:
34-
from sigstore.models import LogEntry
34+
from sigstore.models import TransparencyLogEntry
3535

3636
__all__ = [
3737
"_hashedrekord_from_parts",
@@ -72,7 +72,7 @@ class RekorLogSubmitter(ABC):
7272
def create_entry(
7373
self,
7474
request: EntryRequestBody,
75-
) -> LogEntry:
75+
) -> TransparencyLogEntry:
7676
"""
7777
Submit the request to Rekor.
7878
"""

sigstore/_internal/rekor/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def log(self) -> RekorLog:
250250
"""
251251
return RekorLog(f"{self.url}/log", session=self.session)
252252

253-
def create_entry(self, request: EntryRequestBody) -> LogEntry:
253+
def create_entry(self, request: EntryRequestBody) -> TransparencyLogEntry:
254254
"""
255255
Submit the request to Rekor.
256256
"""

sigstore/_internal/trust.py

Lines changed: 47 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,8 @@
3535
Certificate,
3636
load_der_x509_certificate,
3737
)
38-
from sigstore_protobuf_specs.dev.sigstore.common.v1 import PublicKey as _PublicKey
39-
from sigstore_protobuf_specs.dev.sigstore.common.v1 import (
40-
PublicKeyDetails as _PublicKeyDetails,
41-
)
42-
from sigstore_protobuf_specs.dev.sigstore.common.v1 import TimeRange
43-
from sigstore_protobuf_specs.dev.sigstore.trustroot.v1 import (
44-
CertificateAuthority as _CertificateAuthority,
45-
)
46-
from sigstore_protobuf_specs.dev.sigstore.trustroot.v1 import (
47-
ClientTrustConfig as _ClientTrustConfig,
48-
)
49-
from sigstore_protobuf_specs.dev.sigstore.trustroot.v1 import (
50-
Service,
51-
ServiceConfiguration,
52-
ServiceSelector,
53-
TransparencyLogInstance,
54-
)
55-
from sigstore_protobuf_specs.dev.sigstore.trustroot.v1 import (
56-
SigningConfig as _SigningConfig,
57-
)
58-
from sigstore_protobuf_specs.dev.sigstore.trustroot.v1 import (
59-
TrustedRoot as _TrustedRoot,
60-
)
38+
from sigstore_models.common import v1 as common_v1
39+
from sigstore_models.trustroot import v1 as trustroot_v1
6140

6241
from sigstore._internal.fulcio.client import FulcioClient
6342
from sigstore._internal.rekor import RekorLogSubmitter
@@ -83,7 +62,9 @@
8362
_logger = logging.getLogger(__name__)
8463

8564

86-
def _is_timerange_valid(period: TimeRange | None, *, allow_expired: bool) -> bool:
65+
def _is_timerange_valid(
66+
period: common_v1.TimeRange | None, *, allow_expired: bool
67+
) -> bool:
8768
"""
8869
Given a `period`, checks that the the current time is not before `start`. If
8970
`allow_expired` is `False`, also checks that the current time is not after
@@ -116,19 +97,19 @@ class Key:
11697
key_id: KeyID
11798

11899
_RSA_SHA_256_DETAILS: ClassVar = {
119-
_PublicKeyDetails.PKCS1_RSA_PKCS1V5,
120-
_PublicKeyDetails.PKIX_RSA_PKCS1V15_2048_SHA256,
121-
_PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256,
122-
_PublicKeyDetails.PKIX_RSA_PKCS1V15_4096_SHA256,
100+
common_v1.PublicKeyDetails.PKCS1_RSA_PKCS1V5,
101+
common_v1.PublicKeyDetails.PKIX_RSA_PKCS1V15_2048_SHA256,
102+
common_v1.PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256,
103+
common_v1.PublicKeyDetails.PKIX_RSA_PKCS1V15_4096_SHA256,
123104
}
124105

125106
_EC_DETAILS_TO_HASH: ClassVar = {
126-
_PublicKeyDetails.PKIX_ECDSA_P256_SHA_256: hashes.SHA256(),
127-
_PublicKeyDetails.PKIX_ECDSA_P384_SHA_384: hashes.SHA384(),
128-
_PublicKeyDetails.PKIX_ECDSA_P521_SHA_512: hashes.SHA512(),
107+
common_v1.PublicKeyDetails.PKIX_ECDSA_P256_SHA_256: hashes.SHA256(),
108+
common_v1.PublicKeyDetails.PKIX_ECDSA_P384_SHA_384: hashes.SHA384(),
109+
common_v1.PublicKeyDetails.PKIX_ECDSA_P521_SHA_512: hashes.SHA512(),
129110
}
130111

131-
def __init__(self, public_key: _PublicKey) -> None:
112+
def __init__(self, public_key: common_v1.PublicKey) -> None:
132113
"""
133114
Construct a key from the given Sigstore PublicKey message.
134115
"""
@@ -147,7 +128,7 @@ def __init__(self, public_key: _PublicKey) -> None:
147128
key = load_der_public_key(
148129
public_key.raw_bytes, types=(ec.EllipticCurvePublicKey,)
149130
)
150-
elif public_key.key_details == _PublicKeyDetails.PKIX_ED25519:
131+
elif public_key.key_details == common_v1.PublicKeyDetails.PKIX_ED25519:
151132
hash_algorithm = None
152133
key = load_der_public_key(
153134
public_key.raw_bytes, types=(ed25519.Ed25519PublicKey,)
@@ -198,7 +179,7 @@ class Keyring:
198179
Represents a set of keys, each of which is a potentially valid verifier.
199180
"""
200181

201-
def __init__(self, public_keys: list[_PublicKey] = []):
182+
def __init__(self, public_keys: list[common_v1.PublicKey] = []):
202183
"""
203184
Create a new `Keyring`, with `keys` as the initial set of verifying keys.
204185
"""
@@ -263,7 +244,7 @@ class CertificateAuthority:
263244
Certificate Authority used in a Trusted Root configuration.
264245
"""
265246

266-
def __init__(self, inner: _CertificateAuthority):
247+
def __init__(self, inner: trustroot_v1.CertificateAuthority):
267248
"""
268249
Construct a new `CertificateAuthority`.
269250
@@ -278,7 +259,7 @@ def from_json(cls, path: str) -> CertificateAuthority:
278259
"""
279260
Create a CertificateAuthority directly from JSON.
280261
"""
281-
inner = _CertificateAuthority().from_json(Path(path).read_bytes())
262+
inner = trustroot_v1.CertificateAuthority.from_json(Path(path).read_bytes())
282263
return cls(inner)
283264

284265
def _verify(self) -> None:
@@ -335,7 +316,7 @@ def __str__(self) -> str:
335316
"""Returns the variant's string value."""
336317
return self.value
337318

338-
def __init__(self, inner: _SigningConfig):
319+
def __init__(self, inner: trustroot_v1.SigningConfig):
339320
"""
340321
Construct a new `SigningConfig`.
341322
@@ -377,19 +358,19 @@ def from_file(
377358
path: str,
378359
) -> SigningConfig:
379360
"""Create a new signing config from file"""
380-
inner = _SigningConfig().from_json(Path(path).read_bytes())
361+
inner = trustroot_v1.SigningConfig.from_json(Path(path).read_bytes())
381362
return cls(inner)
382363

383364
@staticmethod
384365
def _get_valid_services(
385-
services: list[Service],
366+
services: list[trustroot_v1.Service],
386367
supported_versions: list[int],
387-
config: ServiceConfiguration | None,
388-
) -> list[Service]:
368+
config: trustroot_v1.ServiceConfiguration | None,
369+
) -> list[trustroot_v1.Service]:
389370
"""Return supported services, taking SigningConfig restrictions into account"""
390371

391372
# split services by operator, only include valid services
392-
services_by_operator: dict[str, list[Service]] = defaultdict(list)
373+
services_by_operator: dict[str, list[trustroot_v1.Service]] = defaultdict(list)
393374
for service in services:
394375
if service.major_api_version not in supported_versions:
395376
continue
@@ -401,20 +382,19 @@ def _get_valid_services(
401382

402383
# build a list of services but make sure we only include one service per operator
403384
# and use the highest version available for that operator
404-
result: list[Service] = []
385+
result: list[trustroot_v1.Service] = []
405386
for op_services in services_by_operator.values():
406387
op_services.sort(key=lambda s: s.major_api_version)
407388
result.append(op_services[-1])
408389

409390
# Depending on ServiceSelector, prune the result list
410-
if not config or config.selector == ServiceSelector.ALL:
391+
if not config or config.selector == trustroot_v1.ServiceSelector.ALL:
411392
return result
412393

413-
if config.selector == ServiceSelector.UNDEFINED:
414-
raise ValueError("Undefined is not a valid signing config ServiceSelector")
415-
416394
# handle EXACT and ANY selectors
417-
count = config.count if config.selector == ServiceSelector.EXACT else 1
395+
count = (
396+
config.count if config.selector == trustroot_v1.ServiceSelector.EXACT else 1
397+
)
418398
if len(result) < count:
419399
raise ValueError(
420400
f"Expected {count} services in signing config, found {len(result)}"
@@ -474,7 +454,7 @@ def __str__(self) -> str:
474454
"""Returns the variant's string value."""
475455
return self.value
476456

477-
def __init__(self, inner: _TrustedRoot):
457+
def __init__(self, inner: trustroot_v1.TrustedRoot):
478458
"""
479459
Construct a new `TrustedRoot`.
480460
@@ -501,12 +481,12 @@ def from_file(
501481
path: str,
502482
) -> TrustedRoot:
503483
"""Create a new trust root from file"""
504-
inner = _TrustedRoot().from_json(Path(path).read_bytes())
484+
inner = trustroot_v1.TrustedRoot.from_json(Path(path).read_bytes())
505485
return cls(inner)
506486

507487
def _get_tlog_keys(
508-
self, tlogs: list[TransparencyLogInstance], purpose: KeyringPurpose
509-
) -> Iterable[_PublicKey]:
488+
self, tlogs: list[trustroot_v1.TransparencyLogInstance], purpose: KeyringPurpose
489+
) -> Iterable[common_v1.PublicKey]:
510490
"""
511491
Yields an iterator of public keys for transparency log instances that
512492
are suitable for `purpose`.
@@ -523,14 +503,18 @@ def _get_tlog_keys(
523503
def rekor_keyring(self, purpose: KeyringPurpose) -> RekorKeyring:
524504
"""Return keyring with keys for Rekor."""
525505

526-
keys: list[_PublicKey] = list(self._get_tlog_keys(self._inner.tlogs, purpose))
506+
keys: list[common_v1.PublicKey] = list(
507+
self._get_tlog_keys(self._inner.tlogs, purpose)
508+
)
527509
if len(keys) == 0:
528510
raise MetadataError("Did not find any Rekor keys in trusted root")
529511
return RekorKeyring(Keyring(keys))
530512

531513
def ct_keyring(self, purpose: KeyringPurpose) -> CTKeyring:
532514
"""Return keyring with key for CTFE."""
533-
ctfes: list[_PublicKey] = list(self._get_tlog_keys(self._inner.ctlogs, purpose))
515+
ctfes: list[common_v1.PublicKey] = list(
516+
self._get_tlog_keys(self._inner.ctlogs, purpose)
517+
)
534518
if not ctfes:
535519
raise MetadataError("CTFE keys not found in trusted root")
536520
return CTKeyring(Keyring(ctfes))
@@ -585,7 +569,7 @@ def from_json(cls, raw: str) -> ClientTrustConfig:
585569
"""
586570
Deserialize the given client trust config.
587571
"""
588-
inner = _ClientTrustConfig().from_json(raw)
572+
inner = trustroot_v1.ClientTrustConfig.from_json(raw)
589573
return cls(inner)
590574

591575
@classmethod
@@ -626,48 +610,33 @@ def from_tuf(
626610
updater = TrustUpdater(url, offline)
627611

628612
tr_path = updater.get_trusted_root_path()
629-
inner_tr = _TrustedRoot().from_json(Path(tr_path).read_bytes())
613+
inner_tr = trustroot_v1.TrustedRoot.from_json(Path(tr_path).read_bytes())
630614

631615
try:
632616
sc_path = updater.get_signing_config_path()
633-
inner_sc = _SigningConfig().from_json(Path(sc_path).read_bytes())
617+
inner_sc = trustroot_v1.SigningConfig.from_json(Path(sc_path).read_bytes())
634618
except TUFError as e:
635619
# TUF repo may not have signing config yet: hard code values for prod:
636620
# https://github.com/sigstore/sigstore-python/issues/1388
637621
if url == DEFAULT_TUF_URL:
638622
embedded = read_embedded("signing_config.v0.2.json", url)
639-
inner_sc = _SigningConfig().from_json(embedded)
623+
inner_sc = trustroot_v1.SigningConfig.from_json(embedded)
640624
else:
641625
raise e
642626

643627
return cls(
644-
_ClientTrustConfig(
645-
ClientTrustConfig.ClientTrustConfigType.CONFIG_0_1,
646-
inner_tr,
647-
inner_sc,
628+
trustroot_v1.ClientTrustConfig(
629+
media_type=ClientTrustConfig.ClientTrustConfigType.CONFIG_0_1.value,
630+
trusted_root=inner_tr,
631+
signing_config=inner_sc,
648632
)
649633
)
650634

651-
def __init__(self, inner: _ClientTrustConfig) -> None:
635+
def __init__(self, inner: trustroot_v1.ClientTrustConfig) -> None:
652636
"""
653637
@api private
654638
"""
655639
self._inner = inner
656-
self._verify()
657-
658-
def _verify(self) -> None:
659-
"""
660-
Performs various feats of heroism to ensure that the client trust config
661-
is well-formed.
662-
"""
663-
664-
# The client trust config must have a recognized media type.
665-
try:
666-
ClientTrustConfig.ClientTrustConfigType(self._inner.media_type)
667-
except ValueError:
668-
raise Error(
669-
f"unsupported client trust config format: {self._inner.media_type}"
670-
)
671640

672641
@property
673642
def trusted_root(self) -> TrustedRoot:

0 commit comments

Comments
 (0)