
    i                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJr  S SK	J
r
Jr  S SKJr  \ R                   " S S5      5       r SS\S	\4S
 jjrS	\R$                  \\\4   4S jrS\S	\4S jrg)    N)x509)default_backend)hashesserialization)rsac                   `    \ rS rSr% Sr\R                  \S'   Sr\	R                  \   \S'   Srg)CredSSPTLSContext   a  A TLS context generated for CredSSP.

This is the SSLContext object used by both an initiator and acceptor of
CredSSP authentication. It allows the caller to finely control the SSL/TLS
options used during the CredSSP authentication phase, e.g. selecting
min/max protocol versions, specific cipher suites, etc.

The public_key attribute is used for acceptor CredSSP contexts as the
DER encoded public key loaded in the SSLContext. Here is an example of
generating and loading a X509 certificate for an acceptor context:

    ctx = spnego.tls.default_tls_context()
    cert_pem, key_Pem, pub_key = spnego.tls.generate_tls_certificate()

    # Cannot use tempfile.NamedTemporaryFile due to sharing violations on
    # Windows. Use a tempdir as a workaround.
    temp_dir = tempfile.mkdtemp()
    try:
        cert_path = os.path.join(tmpe_dir, 'ca.pem')
        with open(cert_path, mode'wb') as fd:
            fd.write(cert_pem)
            fd.write(key_pem)

        ctx.context.load_cert_chain(cert_path)
        ctx.public_key = pub_key

    finally:
        shutil.rmtree(temp_dir)

This context is then passed in through the `credssp_tls_context` kwarg of
:meth:`spnego.client` or :meth:`spnego.server`.

Attributes:
    context (ssl.SSLContext): The TLS context generated for CredSSP.
    public_key (Optional[bytes]): When generating the TLS context for an
        acceptor this is the public key bytes for the generated cert in the
        TLS context.
contextN
public_key )__name__
__module____qualname____firstlineno____doc__ssl
SSLContext__annotations__r   typingOptionalbytes__static_attributes__r       D/home/james-whalen/.local/lib/python3.13/site-packages/spnego/tls.pyr	   r	      s'    %N ^^)-J&-r   r	   usagereturnc                    U S:X  aA  [         R                  " [         R                  5      nSUl        [         R                  Ul        O$[         R                  " [         R                  5      nU=R                  [         R                  S-  S-  -  sl        [        [         SS5      n[        US5      (       a  U(       a  [        USUR                  5        O~U=R                  [         R                  R                  [         R                  R                  -  [         R                  R                   -  [         R                  R"                  -  -  sl        [%        US9$ )	a  CredSSP TLSContext with sane defaults.

Creates the TLS context used to generate the SSL object for CredSSP
authentication. By default the TLS context will set the minimum protocol to
TLSv1.2. Certificate verification is also disabled for both the initiator
and acceptor as per the `MS-CSSP Events and Sequencing Rules`_ in step 1.
This can be used as a base context where the caller applies further changes
based on their requirements such as cert validation and so forth.

This context is then passed in through the `credssp_tls_context` kwarg of
:meth:`spnego.client` or :meth:`spnego.server`.

Args:
    usage: Either `initiate` for a client context or `accept` for a server context.

Returns:
    TLSContext: The TLS context that can be used with CredSSP auth.

.. _MS-CSSP Events and Sequencing Rules:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/385a7489-d46b-464c-b224-f7340e308a5c
initiateFi      
TLSVersionNminimum_version)r   )r   r   PROTOCOL_TLS_CLIENTcheck_hostname	CERT_NONEverify_modePROTOCOL_TLS_SERVERoptionsOP_NO_COMPRESSIONgetattrhasattrsetattrTLSv1_2OptionsOP_NO_SSLv2OP_NO_SSLv3OP_NO_TLSv1OP_NO_TLSv1_1r	   )r   ctxtls_versions      r   default_tls_contextr5   =   s    0 
 nnS445"--nnS445 KK3((:5
BBK
 #|T2Ks%&&;&(;(;< 	KK##ckk&=&==@W@WWZ]ZeZeZsZss	
 S))r   c                     [         R                  " SS[        5       S9n S[        R                  " 5        3SS n[
        R                  " [
        R                  " [
        R                  R                  U5      /5      n[        R                  R                  [        R                  R                  5      n[
        R                  " 5       R                  U5      R!                  U5      R#                  U R#                  5       5      R%                  [
        R&                  " 5       5      R)                  U5      R+                  U[        R,                  " SS9-   5      R/                  U [0        R2                  " 5       [        5       5      nUR5                  [6        R8                  R:                  S	9nU R=                  [6        R8                  R:                  [6        R>                  R@                  [6        RB                  " 5       S
9nUR#                  5       R5                  [6        R8                  RD                  [6        RF                  RH                  5      nXVU4$ )ao  Generates X509 cert and key for CredSSP acceptor.

Generates a TLS X509 certificate and key that can be used by a CredSSP
acceptor for authentication. This certificate is modelled after the one
that the WSMan CredSSP service uses on Windows.

Returns:
    Tuple[bytes, bytes, bytes]: The X509 PEM encoded certificate,
    PEM encoded key, and DER encoded public key.
i  r    )public_exponentkey_sizebackendzCREDSSP-N@   im  )days)encoding)r<   formatencryption_algorithm)%r   generate_private_keyr   platformnoder   NameNameAttributeNameOIDCOMMON_NAMEdatetimenowtimezoneutcCertificateBuildersubject_nameissuer_namer   serial_numberrandom_serial_numbernot_valid_beforenot_valid_after	timedeltasignr   SHA256public_bytesr   EncodingPEMprivate_bytesPrivateFormatTraditionalOpenSSLNoEncryptionDERPublicFormatPKCS1)keycn_namenamerG   certcert_pemkey_pemr   s           r   generate_tls_certificaterd   q   s    
"
"54Q`Qb
cC )*3B/G99d(()A)A7KLMD




 1 1 5 5
6C!	d		T		CNN$	%	t002	3		#		x11s;;	<	c6==?O$5	6 	   -*@*@*D*D EH''++**==*779   G
 "//0F0F0J0JMLfLfLlLlmJj((r   datac                     [         R                  " U [        5       5      nUR                  5       nUR	                  [
        R                  R                  [
        R                  R                  5      $ )ay  Public key bytes of an X.509 Certificate.

Gets the public key bytes used by CredSSP of the provided X.509
certificate. Use this for the `public_key` attribute of
class:`CredSSPTLSContext` for an acceptor context when providing your own
certificate.

Args:
    data: The DER encoded bytes of the X.509 certificate.

Returns:
    bytes: The public key bytes of the certificate.
)
r   load_der_x509_certificater   r   rT   r   rU   r[   r\   r]   )re   ra   r   s      r   get_certificate_public_keyrh      sP      ))$0ABD"J""=#9#9#=#=}?Y?Y?_?_``r   )r   )dataclassesrF   r@   r   r   cryptographyr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   	dataclassr	   strr5   Tupler   rd   rh   r   r   r   <module>rq      s       
   8 @ 9 ). ). ).Z 1*1*1*h%)&,,ueU/B"C %)Pa
a
ar   