§
    8?Äj^  ã            	       óÔ   — d Z ddlZddlZddlZddlZddlmZ ddlZddl	m
Z
mZ ddlmZ  ej        e¦  «        Z	 ddededee         d	ej        fd
„Zd„ Zd„ Z	 dd„Zdd„Z	 dd„ZdS )z<
Helper functions for mTLS in async for discovery of certs.
é    N)ÚOptional)Ú_agent_identity_utilsÚ
exceptions)Úsecure_cert_key_pathsÚ
cert_bytesÚ	key_bytesÚ
passphraseÚreturnc                 ó|  — 	 t          | ||¬¦  «        5 \  }}}t          j        t          j        j        ¦  «        }|r|}|                     |||¬¦  «         |cddd¦  «         S # 1 swxY w Y   dS # t          j        t          t          t          t          t          f$ r}t          j        d¦  «        |‚d}~ww xY w)a  Creates an SSLContext with the given client certificate and key.
    This function writes the certificate and key to temporary files so that
    ssl.create_default_context can load them, as the ssl module requires
    file paths for client certificates. These temporary files are deleted
    immediately after the SSL context is created.
    Args:
        cert_bytes (bytes): The client certificate content in PEM format.
        key_bytes (bytes): The client private key content in PEM format.
        passphrase (Optional[bytes]): The passphrase for the private key, if any.
    Returns:
        ssl.SSLContext: The configured SSL context with client certificate.

    Raises:
        google.auth.exceptions.TransportError: If there is an error loading the certificate.
    )r	   )ÚcertfileÚkeyfileÚpasswordNz3Failed to load client certificate and key for mTLS.)r   ÚsslÚcreate_default_contextÚPurposeÚSERVER_AUTHÚload_cert_chainÚSSLErrorÚOSErrorÚIOErrorÚ
ValueErrorÚRuntimeErrorÚ	TypeErrorr   ÚTransportError)	r   r   r	   Ú	cert_pathÚkey_pathÚpassphrase_valÚcontextr   Úexcs	            úOC:\Projects\Historical\venv\Lib\site-packages\google/auth/aio/transport/mtls.pyÚmake_client_cert_ssl_contextr!       s"  € ð$Ý" :¨yÀZÐPÑPÔPð 	ñ U
ØØØåÔ0µ´Ô1HÑIÔIˆGØð Ø)�Ø×'Ò'Ø&Ø$Ø%ð (ñ ô ð ð
 ð	ð 	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	øõ ŒL�'¥7­J½ÅiÐPð ð ð ÝÔ'ØAñ
ô 
àð	øøøøðøøøs;   ‚A3 ”AA&ÁA3 Á&A*Á*A3 Á-A*Á.A3 Á3.B;Â!B6Â6B;c              ‡   ó¨   K  — 	 t          j        | g|¢R Ž ƒ d{V —†S # t          $ r+ t          j        ¦   «         } |j        d| g|¢R Ž ƒ d{V —†cY S w xY w)zŸRun a blocking function in an executor to avoid blocking the event loop.

    This implements the non-blocking execution strategy for disk I/O operations.
    N)ÚasyncioÚ	to_threadÚAttributeErrorÚget_running_loopÚrun_in_executor)ÚfuncÚargsÚloops      r    Ú_run_in_executorr+   G   s”   è è € ð
=åÔ& tÐ3¨dÐ3Ð3Ð3Ð3Ð3Ð3Ð3Ð3Ð3Ð3øÝð =ð =ð =åÔ'Ñ)Ô)ˆØ)�TÔ)¨$°Ð<°tÐ<Ð<Ð<Ð<Ð<Ð<Ð<Ð<Ð<Ð<Ð<Ð<ð=øøøs   „ œ2AÁAc                  óˆ   — t           j        j        j                             d¬¦  «        st          j        d¦  «        ‚d„ } | S )a˜  Get a callback which returns the default client SSL credentials.

    Returns:
        Awaitable[Callable[[], Tuple[bytes, bytes]]]: A callback which returns the default
            client certificate bytes and private key bytes, both in PEM format.

    Raises:
        google.auth.exceptions.DefaultClientCertSourceError: If the default
            client SSL credentials don't exist or are malformed.
    F)Úinclude_context_awarez(Default client cert source doesn't existc               “   ó®   K  — 	 t          ¦   «         ƒ d {V —†\  } }}n6# t          t          t          f$ r}t	          j        |¦  «        }||‚d }~ww xY w||fS ©N)Úget_client_cert_and_keyr   r   r   r   ÚMutualTLSChannelError)Ú_r   r   Ú
caught_excÚnew_excs        r    Úcallbackz,default_client_cert_source.<locals>.callbackg   s|   è è € ð	*Ý-DÑ-FÔ-FÐ'FÐ'FÐ'FÐ'FÐ'FÐ'FÑ$ˆAˆz˜9˜9øÝ�¥zÐ2ð 	*ð 	*ð 	*Ý Ô6°zÑBÔBˆGØ˜zÐ)øøøøð	*øøøð ˜9Ð$Ð$s   „ �A´AÁA)ÚgoogleÚauthÚ	transportÚmtlsÚhas_default_client_cert_sourcer   r1   )r5   s    r    Údefault_client_cert_sourcer;   U   s]   € õ Œ;Ô Ô%×DÒDØ#ð Eñ ô ð 
õ Ô.Ø6ñ
ô 
ð 	
ð%ð %ð %ð €Oó    c              ƒ   ó„   K  — t          t          j        j        j        j        | d¦  «        ƒ d{V —†\  }}|r|rd||dfS dS )a×  Returns the client side certificate, private key and passphrase.

    We look for certificates and keys with the following order of priority:
        1. Certificate and key specified by certificate_config.json.
               Currently, only X.509 workload certificates are supported.

    Args:
        certificate_config_path (str): The certificate_config.json file path.

    Returns:
        Tuple[bool, bytes, bytes, bytes]:
            A boolean indicating if cert, key and passphrase are obtained, the
            cert bytes and key bytes both in PEM format, and passphrase bytes.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when getting
            the cert, key and passphrase.
    FNT)FNNN)r+   r6   r7   r8   Ú_mtls_helperÚ_get_workload_cert_and_key)Úcertificate_config_pathÚcertÚkeys      r    Úget_client_ssl_credentialsrC   s   ss   è è € õ. 'ÝŒÔÔ*ÔEØØñô ð ð ð ð ð ð �I€Dˆ#ð ð %�ð %Ø�T˜3 Ð$Ð$à"Ð"r<   c              ƒ   ó®   K  — | r4 | ¦   «         }t          j        |¦  «        r|ƒ d{V —†\  }}n|\  }}d||fS t          ¦   «         ƒ d{V —†\  }}}}|||fS )a  Returns the client side certificate and private key. The function first
    tries to get certificate and key from client_cert_callback; if the callback
    is None or doesn't provide certificate and key, the function tries application
    default SSL credentials.

    Args:
        client_cert_callback (Optional[Callable[[], (bytes, bytes)]]): An
            optional callback which returns client certificate bytes and private
            key bytes both in PEM format.

    Returns:
        Tuple[bool, bytes, bytes]:
            A boolean indicating if cert and key are obtained, the cert bytes
            and key bytes both in PEM format.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when getting
            the cert and key.
    NT)ÚinspectÚisawaitablerC   )Úclient_cert_callbackÚresultrA   rB   Úhas_certr2   s         r    r0   r0   –   s�   è è € ð( ð Ø%Ð%Ñ'Ô'ˆÝÔ˜vÑ&Ô&ð 	Ø$˜˜˜˜˜˜‰IˆD�#�#à‰IˆD�#Ø�T˜3ˆÐå#=Ñ#?Ô#?Ð?Ð?Ð?Ð?Ð?Ð?Ñ€Hˆd�C˜Ø�T˜3ÐÐr<   c              ƒ   ó�   ‡ ‡K  — t          |¦  «        ƒ d{V —†\  }Š}|r‰sdS ˆ ˆfd„}t          |¦  «        ƒ d{V —†\  }}‰|||fS )au  Async helper to retrieve certs and compute fingerprints for mTLS rotation.

    Args:
        cached_cert (bytes): The cached client certificate.
        client_cert_callback (Optional[Callable[[], (bytes, bytes)]]): An
            optional callback which returns client certificate bytes and private
            key bytes both in PEM format.

    Returns:
        Tuple[Optional[bytes], Optional[bytes], Optional[str], Optional[str]]:
            call_cert_bytes, call_key_bytes, cached_fingerprint, current_cert_fingerprint.
        Returns (None, None, None, None) if mTLS is disabled or no client certificate is present.
    N)NNNNc                  óŽ   •— t          j        ‰¦  «        } t          j        | ¦  «        }‰rt          j        ‰¦  «        }nd }||fS r/   )r   Úparse_certificateÚ!calculate_certificate_fingerprintÚget_cached_cert_fingerprint)Úcert_objÚcurrent_fingerprintÚcached_fingerprintÚcached_certÚcall_cert_bytess      €€r    Ú_fetch_fingerprintszGcheck_parameters_for_unauthorized_response.<locals>._fetch_fingerprintsÌ   s`   ø€ Ý(Ô:¸?ÑKÔKˆÝ3ÔUØñ
ô 
Ðð ð 	&Ý!6Ô!RØñ"ô "ÐÐð "&ÐØ!Ð#6Ð6Ð6r<   )r0   r+   )rR   rG   Úis_mtlsÚcall_key_bytesrT   rQ   Úcurrent_cert_fingerprintrS   s   `      @r    Ú*check_parameters_for_unauthorized_responserX   ¶   sÄ   øøè è € õ  6MØñ6ô 6ð 0ð 0ð 0ð 0ð 0ð 0Ñ,€Gˆ_˜nð ð &˜/ð &Ø%Ð%ð7ð 7ð 7ð 7ð 7ð 7õ :JØñ:ô :ð 4ð 4ð 4ð 4ð 4ð 4Ñ0ÐÐ0ð ˜NÐ,>Ð@XÐXÐXr<   r/   )Ú__doc__r#   rE   Úloggingr   Útypingr   Úgoogle.auth.transport.mtlsr6   Úgoogle.authr   r   Ú"google.auth.transport._mtls_helperr   Ú	getLoggerÚ__name__Ú_LOGGERÚbytesÚ
SSLContextr!   r+   r;   rC   r0   rX   © r<   r    ú<module>re      sL  ððð ð €€€Ø €€€Ø €€€Ø 
€
€
€
Ø Ð Ð Ð Ð Ð à !Ð !Ð !Ð !Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø DÐ DÐ DÐ DÐ DÐ Dà
ˆ'Ô
˜HÑ
%Ô
%€ð HLð$ð $Øð$Ø"'ð$Ø5=¸e´_ð$à„^ð$ð $ð $ð $ðN=ð =ð =ðð ð ð> !ð #ð  #ð  #ð  #ðFð ð ð ðB '+ð&Yð &Yð &Yð &Yð &Yð &Yr<   