Skip to content

Commit 173c91e

Browse files
committed
feat: sign header x5c usage and validation
1 parent f33ddf1 commit 173c91e

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

pyeudiw/jwt/jws_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _validate_key_with_header_kid(key: dict, header: dict) -> None:
349349
raise Exception(
350350
f"token header contains a kid {header_kid} that does not match the signing key kid {key_kid}"
351351
)
352-
return None
352+
return
353353

354354

355355
def _validate_key_with_header_x5c(key: dict, header: dict) -> None:
@@ -363,7 +363,7 @@ def _validate_key_with_header_x5c(key: dict, header: dict) -> None:
363363
:raises Exception: if the key is not compatible with the header content x5c (if any)
364364
"""
365365
x5c: list[str] | None = header.get("x5c")
366-
if x5c is None:
366+
if not x5c:
367367
return
368368
leaf_cert: str = x5c[0]
369369

@@ -374,13 +374,13 @@ def _validate_key_with_header_x5c(key: dict, header: dict) -> None:
374374
raise Exception(
375375
f"token header containes a chain whose leaf certificate {leaf_cert} does not match the signing key leaf certificate {leaf_x5c_cert}"\
376376
)
377-
return None
377+
return
378378
header_key = parse_b64der(leaf_cert)
379379
if header_key.thumbprint != JWK(key).thumbprint:
380380
raise Exception(
381381
f"public material of the key does not matches the key in the leaf certificate {leaf_cert}"
382382
)
383-
return None
383+
return
384384

385385

386386
def _validate_key_with_jws_header(key: dict, protected_jws_header: dict, unprotected_jws_header: dict) -> None:

pyeudiw/openid4vp/presentation_submission/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ def validate(
179179

180180
for descriptor in validated_submission.descriptor_map:
181181
handler = self.handlers.get(descriptor.format)
182-
183182
if not handler:
184183
raise MissingHandler(f"Handler for format '{descriptor.format}' not found.")
185184

pyeudiw/tests/satosa/test_backend.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def _generate_payload(self, issuer_jwk, holder_jwk, nonce, state, aud, x509=Fals
192192

193193
if x509:
194194
additional_headers = {
195-
"x5c": self.chain
195+
"x5c": [PEM_cert_to_B64DER_cert(pem) for pem in self.chain]
196196
}
197197
else:
198198
additional_headers = {
@@ -622,7 +622,6 @@ def test_response_endpoint_x5c_chain(self, context):
622622
}
623623
context.request_method = "POST"
624624
context.http_headers = {"HTTP_CONTENT_TYPE": "application/x-www-form-urlencoded"}
625-
626625
response_endpoint = self.backend.response_endpoint(context)
627626
assert response_endpoint.status == "200"
628627
assert "redirect_uri" in response_endpoint.message

pyeudiw/x509/verify.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def B64DER_cert_to_PEM_cert(cert: str) -> str:
111111
return DER_cert_to_PEM_cert(base64.b64decode(cert))
112112

113113

114+
def B64DER_cert_to_DER_cert(cert: str) -> bytes:
115+
"""
116+
Takes a certificate Base64 encoded DER and returns the
117+
certificate in DER format.
118+
"""
119+
return base64.b64decode(cert)
120+
121+
114122
def to_DER_cert(cert: str | bytes) -> bytes:
115123
"""
116124
This function takes in a certificate with unknown representation
@@ -134,7 +142,7 @@ def to_DER_cert(cert: str | bytes) -> bytes:
134142

135143
cert_s = cert_s.replace('\n\r', '')
136144
if _BASE64_RE.fullmatch(cert_s):
137-
return B64DER_cert_to_PEM_cert(cert_s)
145+
return B64DER_cert_to_DER_cert(cert_s)
138146

139147
raise ValueError("unable to recognize input [cert] as a ccertifficate")
140148

0 commit comments

Comments
 (0)