@@ -67,11 +67,11 @@ def _b64(data: bytes) -> str:
6767 return base64 .b64encode (data ).decode ()
6868
6969
70- def _make_attestation (statement_bytes , signature_bytes ):
70+ def _make_attestation (statement_bytes , signature_bytes , verification_material = None ):
7171 """Return a single PEP-740 Attestation dict (SLSA flavour)."""
7272 return {
7373 "version" : 1 ,
74- "verification_material" : None ,
74+ "verification_material" : verification_material ,
7575 "envelope" : {
7676 "statement" : _b64 (statement_bytes ),
7777 "signature" : _b64 (signature_bytes ),
@@ -92,6 +92,21 @@ def _make_provenance(attestation):
9292 }
9393
9494
95+ def _read_public_key_pem ():
96+ """Read the test public key as a PEM string."""
97+ with open (TEST_PUBLIC_KEY_PATH ) as f :
98+ return f .read ()
99+
100+
101+ def _make_embedded_key_material (public_key_pem ):
102+ """Build a verification_material dict with an embedded public key."""
103+ return {
104+ "certificate" : None ,
105+ "public_key" : public_key_pem ,
106+ "transparency_entries" : [{"log_index" : 0 }],
107+ }
108+
109+
95110# ---------------------------------------------------------------------------
96111# Fixtures
97112# ---------------------------------------------------------------------------
@@ -245,3 +260,97 @@ def test_slsa_attestation_via_content_upload(
245260 assert publisher ["builder_id" ] == "https://konflux-ci.dev/calunga"
246261 assert publisher ["build_type" ] == "https://konflux-ci.dev/PythonWheelBuild@v1"
247262 assert publisher ["kind" ] == "konflux-ci.dev"
263+
264+
265+ def test_slsa_embedded_key_accepted (
266+ python_bindings , python_content_factory , monitor_task , test_private_key , _provenance_file
267+ ):
268+ """An attestation with an embedded public key matching the server key is accepted."""
269+ content = python_content_factory ()
270+
271+ stmt = _build_statement (content .filename , content .sha256 )
272+ sig = _sign (stmt , test_private_key )
273+ vm = _make_embedded_key_material (_read_public_key_pem ())
274+ att = _make_attestation (stmt , sig , verification_material = vm )
275+ prov = _make_provenance (att )
276+
277+ task = python_bindings .ContentProvenanceApi .create (
278+ package = content .pulp_href ,
279+ file = _provenance_file (prov ),
280+ verify = True ,
281+ ).task
282+ result = monitor_task (task )
283+
284+ prov_obj = python_bindings .ContentProvenanceApi .read (result .created_resources [- 1 ])
285+ assert prov_obj .package == content .pulp_href
286+ stored_vm = prov_obj .provenance ["attestation_bundles" ][0 ]["attestations" ][0 ][
287+ "verification_material"
288+ ]
289+ assert stored_vm ["public_key" ] == _read_public_key_pem ()
290+
291+
292+ def test_slsa_embedded_key_mismatch_rejected (
293+ python_bindings , python_content_factory , monitor_task , test_private_key , _provenance_file
294+ ):
295+ """An attestation with an embedded key that does not match the server key is rejected."""
296+ content = python_content_factory ()
297+
298+ stmt = _build_statement (content .filename , content .sha256 )
299+ sig = _sign (stmt , test_private_key )
300+ wrong_key = (
301+ "-----BEGIN PUBLIC KEY-----\n "
302+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArvxtuKOCCHGNd6agbC4b\n "
303+ "zX9S7qMnMRdz9bxxzkPf9a4T75St3EcrNQYxLqzcDBfeiiLa1wXQoraqFSGkvqX+\n "
304+ "YDFmtYRgnXEujUwYqHQBYrKtrQHxrQ2J3V1e+PqhJ4HCQq/uRZdx6KJ5DOlpYuJM\n "
305+ "aL2m7IozNJJkgjQH1frmDfM5/GuPVbgwN0QDnC0oSzNJFKtdVMD3SPnQGMPQ9qxf\n "
306+ "DiTZOk1tO3VD/xXoNJ6Zgf0D5RvGyLAfLflgojKImHezgpdE6iQyZAVU2ShKMq3M\n "
307+ "K+XtX39/ajXHJ1FCLXlRFoNoHltWioyvkmXHbhb9OxGncB+x6idSjgp7AR5y15HN\n "
308+ "DwIDAQAB\n "
309+ "-----END PUBLIC KEY-----\n "
310+ )
311+ vm = _make_embedded_key_material (wrong_key )
312+ att = _make_attestation (stmt , sig , verification_material = vm )
313+ prov = _make_provenance (att )
314+
315+ task = python_bindings .ContentProvenanceApi .create (
316+ package = content .pulp_href ,
317+ file = _provenance_file (prov ),
318+ verify = True ,
319+ ).task
320+ with pytest .raises (PulpTaskError ) as exc_info :
321+ monitor_task (task )
322+ err_msg = "embedded public key does not match server-configured ATTESTATION_VERIFICATION_KEY"
323+ assert err_msg in exc_info .value .task .error ["description" ]
324+
325+
326+ def test_slsa_certificate_and_public_key_rejected (
327+ python_bindings , python_content_factory , test_private_key
328+ ):
329+ """An attestation with both certificate and public_key is rejected."""
330+ content = python_content_factory ()
331+
332+ stmt = _build_statement (content .filename , content .sha256 )
333+ sig = _sign (stmt , test_private_key )
334+ att = {
335+ "version" : 1 ,
336+ "verification_material" : {
337+ "certificate" : _b64 (b"fake-cert" ),
338+ "public_key" : _read_public_key_pem (),
339+ "transparency_entries" : [{"log_index" : 0 }],
340+ },
341+ "envelope" : {
342+ "statement" : _b64 (stmt ),
343+ "signature" : _b64 (sig ),
344+ },
345+ }
346+ body = {
347+ "artifact" : content .artifact ,
348+ "relative_path" : content .filename ,
349+ "sha256" : content .sha256 ,
350+ "attestations" : json .dumps ([att ]),
351+ }
352+ with pytest .raises (python_bindings .ApiException ) as exc_info :
353+ python_bindings .ContentPackagesApi .create (** body )
354+ err_msg = "verification_material cannot contain both certificate and public_key"
355+ assert err_msg in exc_info .value .body
356+ assert exc_info .value .status == 400
0 commit comments