1- import base64
21import logging
3- from ssl import PEM_cert_to_DER_cert
42from typing import Union
53
64from pyeudiw .trust .handler .interface import TrustHandlerInterface
97from pyeudiw .jwk .parse import parse_pem , parse_x5c_keys , parse_certificate
108from cryptojwt .jwk .jwk import key_from_jwk_dict
119from pyeudiw .x509 .verify import (
10+ PEM_cert_to_B64DER_cert ,
11+ to_DER_cert ,
1212 verify_x509_attestation_chain ,
1313 get_expiry_date_from_x5c ,
1414 der_list_to_pem_list ,
@@ -36,11 +36,13 @@ def __init__(
3636 private_keys : list [dict [str , str ]],
3737 client_id_scheme : str = "x509_san_uri" ,
3838 certificate_authorities : dict [str , str ] = [],
39+ include_issued_jwt_header_param : bool = False ,
3940 ** kwargs
4041 ) -> None :
4142 self .client_id = client_id
4243 self .client_id_scheme = client_id_scheme
4344 self .certificate_authorities = certificate_authorities
45+ self .include_issued_jwt_header_param = include_issued_jwt_header_param
4446
4547 if not relying_party_certificate_chains_by_ca :
4648 raise InvalidTrustHandlerConfiguration ("No x509 certificate chains provided in the configuration" )
@@ -70,7 +72,7 @@ def __init__(
7072 break
7173
7274 if not found_client_id :
73- logger .error (f"Invalid x509 leaf certificate using CA { k } . Unmatching client id ({ client_id } ); searched among { search_set } , the chain will be removed" )
75+ logger .error (f"Invalid x509 leaf certificate using CA { k } . Unmatching client id ({ client_id } ); the chain will be removed" )
7476 continue
7577
7678 pem_type = get_certificate_type (v [0 ])
@@ -130,24 +132,30 @@ def extract_and_update_trust_materials(
130132 return trust_source
131133
132134 def validate_trust_material (
133- self ,
134- x5c : list [str ],
135+ self ,
136+ x5c : list [str ],
135137 trust_source : TrustSourceData ,
136138 ) -> tuple [bool , TrustSourceData ]:
137- chain = [base64 .b64decode (b64der ) for b64der in x5c ]
138-
139- if len (chain ) > 1 and not verify_x509_attestation_chain (chain ):
139+ # TODO: qui c'è del lavoro veramente sporco da fare.
140+ # Bisogna
141+ # (1) normalizzare la rappresentazione della chain a DER; per fare questo bisogna fare inferenza se PEM o Base64+DER
142+ # (2) normalizzare il salvatagggio della chain a PEM
143+ # (3) incrociare le dita che MDOC non si sfasci...
144+ der_chain = [to_DER_cert (cert ) for cert in x5c ]
145+ pem_chain = der_list_to_pem_list (der_chain )
146+
147+ if len (der_chain ) > 1 and not verify_x509_attestation_chain (der_chain ):
140148 logger .error (f"Invalid x509 certificate chain. Chain validation failed" )
141149 return False , trust_source
142150
143- issuer = get_trust_anchor_from_x5c (chain )
151+ issuer = get_trust_anchor_from_x5c (der_chain )
144152
145153 if not issuer :
146- logger .error (f "Invalid x509 certificate chain. Issuer not found" )
154+ logger .error ("Invalid x509 certificate chain. Issuer not found" )
147155 return False , trust_source
148156
149157 if not issuer in self .certificate_authorities :
150- logger .error (f "Invalid x509 certificate chain. Issuer not found in the list of trusted CAs" )
158+ logger .error ("Invalid x509 certificate chain. Issuer not found in the list of trusted CAs" )
151159 return False , trust_source
152160
153161 issuer_pem = self .certificate_authorities [issuer ]
@@ -156,19 +164,19 @@ def validate_trust_material(
156164 issuer_jwk = parse_pem (issuer_pem )
157165 chain_jwks = parse_x5c_keys (x5c )
158166 except Exception as e :
159- logger .error (f "Invalid x509 certificate chain. Parsing failed: { e } " )
167+ logger .error ("Invalid x509 certificate chain. Parsing failed: {e}" )
160168 return False , trust_source
161169
162170 if not issuer_jwk .thumbprint == chain_jwks [- 1 ].thumbprint :
163- logger .error (f "Invalid x509 certificate chain. Issuer thumbprint does not match" )
171+ logger .error ("Invalid x509 certificate chain. Issuer thumbprint does not match" )
164172 return False , trust_source
165173
166174 trust_source .add_trust_param (
167175 "x509" ,
168176 TrustEvaluationType (
169177 attribute_name = self .get_handled_trust_material_name (),
170- x5c = x5c ,
171- expiration_date = get_expiry_date_from_x5c (chain ),
178+ x5c = pem_chain ,
179+ expiration_date = get_expiry_date_from_x5c (der_chain ),
172180 jwks = chain_jwks ,
173181 trust_handler_name = self .name ,
174182 )
@@ -179,7 +187,7 @@ def validate_trust_material(
179187 def extract_jwt_header_trust_parameters (self , trust_source : TrustSourceData ) -> dict :
180188 tp : dict = trust_source .serialize ().get (X509Handler ._TRUST_TYPE , {})
181189 if (x5c_pem := tp .get (X509Handler ._TRUST_PARAMETER_NAME , None )):
182- x5c = [base64 . b64encode ( PEM_cert_to_DER_cert ( pem )). decode ( ) for pem in x5c_pem ]
190+ x5c = [PEM_cert_to_B64DER_cert ( pem ) for pem in x5c_pem ]
183191 return {"x5c" : x5c }
184192 return {}
185193
0 commit comments