Skip to content

Commit af16355

Browse files
committed
Replace sigstore_proto_buf with sigstore_models
Replace sigstore_proto_buf with sigstore_models. In many cases the classes of sigstore_modelscan be called with unchanged parameters, but in some cases explicit base64 encoding needs to be done. Signed-off-by: Stefan Berger <[email protected]>
1 parent 5ac3666 commit af16355

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ dependencies = [
3232
"click",
3333
"cryptography",
3434
"in-toto-attestation",
35-
"sigstore-protobuf-specs == 0.3.2",
3635
"sigstore==3.6.5",
36+
"sigstore-models>=0.0.5",
3737
"typing_extensions",
3838
]
3939
requires-python = ">=3.9"

src/model_signing/_signing/sign_certificate.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Signers and verifiers using certificates."""
1616

17+
import base64
1718
from collections.abc import Iterable
1819
import logging
1920
import pathlib
@@ -26,8 +27,8 @@
2627
from cryptography.hazmat.primitives.asymmetric import ec
2728
from cryptography.x509 import oid
2829
from OpenSSL import crypto
29-
from sigstore_protobuf_specs.dev.sigstore.bundle import v1 as bundle_pb
30-
from sigstore_protobuf_specs.dev.sigstore.common import v1 as common_pb
30+
from sigstore_models.bundle import v1 as bundle_pb
31+
from sigstore_models.common import v1 as common_pb
3132
from typing_extensions import override
3233

3334
from model_signing._signing import sign_ec_key as ec_key
@@ -79,8 +80,10 @@ def __init__(
7980
def _get_verification_material(self) -> bundle_pb.VerificationMaterial:
8081
def _to_protobuf_certificate(certificate):
8182
return common_pb.X509Certificate(
82-
raw_bytes=certificate.public_bytes(
83-
encoding=serialization.Encoding.DER
83+
raw_bytes=base64.b64encode(
84+
certificate.public_bytes(
85+
encoding=serialization.Encoding.DER
86+
)
8487
)
8588
)
8689

@@ -95,7 +98,8 @@ def _to_protobuf_certificate(certificate):
9598
return bundle_pb.VerificationMaterial(
9699
x509_certificate_chain=common_pb.X509CertificateChain(
97100
certificates=chain
98-
)
101+
),
102+
tlog_entries=[],
99103
)
100104

101105

src/model_signing/_signing/sign_ec_key.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Signers and verifiers using elliptic curve keys."""
1616

17+
import base64
1718
import hashlib
1819
import pathlib
1920
from typing import Optional
@@ -23,9 +24,9 @@
2324
from cryptography.hazmat.primitives import serialization
2425
from cryptography.hazmat.primitives.asymmetric import ec
2526
from google.protobuf import json_format
26-
from sigstore_protobuf_specs.dev.sigstore.bundle import v1 as bundle_pb
27-
from sigstore_protobuf_specs.dev.sigstore.common import v1 as common_pb
28-
from sigstore_protobuf_specs.io import intoto as intoto_pb
27+
from sigstore_models import intoto as intoto_pb
28+
from sigstore_models.bundle import v1 as bundle_pb
29+
from sigstore_models.common import v1 as common_pb
2930
from typing_extensions import override
3031

3132
from model_signing._signing import sign_sigstore_pb as sigstore_pb
@@ -102,15 +103,17 @@ def sign(self, payload: signing.Payload) -> signing.Signature:
102103
)
103104

104105
raw_signature = intoto_pb.Signature(
105-
sig=self._private_key.sign(
106-
sigstore_pb.pae(raw_payload),
107-
ec.ECDSA(get_ec_key_hash(self._private_key.public_key())),
106+
sig=base64.b64encode(
107+
self._private_key.sign(
108+
sigstore_pb.pae(raw_payload),
109+
ec.ECDSA(get_ec_key_hash(self._private_key.public_key())),
110+
)
108111
),
109112
keyid="",
110113
)
111114

112115
envelope = intoto_pb.Envelope(
113-
payload=raw_payload,
116+
payload=base64.b64encode(raw_payload),
114117
payload_type=signing._IN_TOTO_JSON_PAYLOAD_TYPE,
115118
signatures=[raw_signature],
116119
)
@@ -135,7 +138,8 @@ def _get_verification_material(self) -> bundle_pb.VerificationMaterial:
135138
hash_bytes = hashlib.sha256(raw_bytes).digest().hex()
136139

137140
return bundle_pb.VerificationMaterial(
138-
public_key=common_pb.PublicKeyIdentifier(hint=hash_bytes)
141+
public_key=common_pb.PublicKeyIdentifier(hint=hash_bytes),
142+
tlog_entries=[],
139143
)
140144

141145

src/model_signing/_signing/sign_pkcs11.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import base64
1516
from collections.abc import Iterable
1617
import hashlib
1718
import pathlib
@@ -27,9 +28,9 @@
2728
from cryptography.hazmat.primitives.asymmetric import ec
2829
from google.protobuf import json_format
2930
import PyKCS11
30-
from sigstore_protobuf_specs.dev.sigstore.bundle import v1 as bundle_pb
31-
from sigstore_protobuf_specs.dev.sigstore.common import v1 as common_pb
32-
from sigstore_protobuf_specs.io import intoto as intoto_pb
31+
from sigstore_models import intoto as intoto_pb
32+
from sigstore_models.bundle import v1 as bundle_pb
33+
from sigstore_models.common import v1 as common_pb
3334
from typing_extensions import override
3435

3536
from model_signing._signing import sign_ec_key as ec_key
@@ -175,10 +176,10 @@ def sign(self, payload: signing.Payload) -> signing.Signature:
175176
# Convert plain r & s signature values to ASN.1
176177
sig = DSASignature.from_p1363(rs_sig).dump()
177178

178-
raw_signature = intoto_pb.Signature(sig=sig, keyid="")
179+
raw_signature = intoto_pb.Signature(sig=base64.b64encode(sig), keyid="")
179180

180181
envelope = intoto_pb.Envelope(
181-
payload=raw_payload,
182+
payload=base64.b64encode(raw_payload),
182183
payload_type=signing._IN_TOTO_JSON_PAYLOAD_TYPE,
183184
signatures=[raw_signature],
184185
)
@@ -203,7 +204,8 @@ def _get_verification_material(self) -> bundle_pb.VerificationMaterial:
203204
hash_bytes = hashlib.sha256(raw_bytes).digest().hex()
204205

205206
return bundle_pb.VerificationMaterial(
206-
public_key=common_pb.PublicKeyIdentifier(hint=hash_bytes)
207+
public_key=common_pb.PublicKeyIdentifier(hint=hash_bytes),
208+
tlog_entries=[],
207209
)
208210

209211

src/model_signing/_signing/sign_sigstore_pb.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import sys
2828
from typing import cast
2929

30-
from sigstore_protobuf_specs.dev.sigstore.bundle import v1 as bundle_pb
30+
from sigstore_models.bundle import v1 as bundle_pb
3131
from typing_extensions import override
3232

3333
from model_signing._signing import signing
@@ -112,7 +112,18 @@ def write(self, path: pathlib.Path) -> None:
112112
def read(cls, path: pathlib.Path) -> Self:
113113
content = path.read_text()
114114
parsed_dict = json.loads(content)
115-
return cls(bundle_pb.Bundle().from_dict(parsed_dict))
115+
if "tlogEntries" not in parsed_dict["verificationMaterial"]:
116+
parsed_dict["verificationMaterial"]["tlogEntries"] = []
117+
if "publicKey" in parsed_dict["verificationMaterial"]:
118+
if "hint" not in parsed_dict["verificationMaterial"]["publicKey"]:
119+
parsed_dict["verificationMaterial"]["publicKey"]["hint"] = None
120+
if "rawBytes" in parsed_dict["verificationMaterial"]["publicKey"]:
121+
del parsed_dict["verificationMaterial"]["publicKey"]["rawBytes"]
122+
if "keyDetails" in parsed_dict["verificationMaterial"]["publicKey"]:
123+
del parsed_dict["verificationMaterial"]["publicKey"][
124+
"keyDetails"
125+
]
126+
return cls(bundle_pb.Bundle.from_dict(parsed_dict))
116127

117128

118129
class Signer(signing.Signer):

0 commit comments

Comments
 (0)