
    h                         d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZmZ  G d d	e          Z G d
 de          Z G d de          Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )z2
TLS helpers, provided as out-of-context methods.
    N)orbchb)warning)ByteEnumFieldShortEnumFieldFieldLenFieldStrLenField)Packet)	_tls_type_tls_versionc                   ~    e Zd ZdZ edde           edde           edddd           e	dd	d
           gZ
dS )TLSPlaintextzTLS PlaintexttypeNversionlendataz!H)	length_offmt c                     | j         S )N)r   )pkts    Z/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/layers/tls/tools.py<lambda>zTLSPlaintext.<lambda>   s    sw     )length_from)__name__
__module____qualname__namer   r   r   r   r   r	   fields_desc r   r   r   r      st        D =y99!>)T<@@ =DIII;vr+>+>@ @ @AKKKr   r   c                       e Zd ZdZdS )TLSCompressedzTLS CompressedNr   r   r   r   r!   r   r   r#   r#              DDDr   r#   c                       e Zd ZdZdS )TLSCiphertextzTLS CiphertextNr$   r!   r   r   r'   r'   #   r%   r   r'   c                     t                      }|j        |_        |j        |_        |                     |j                  |_        t          |j                  |_        |S )z
    Compress p (a TLSPlaintext instance) using compression algorithm instance
    alg and return a TLSCompressed instance.
    )r#   r   r   compressr   r   algpcs      r   _tls_compressr.   '   sH    
 	AVAF	AI\\!&!!AFKKAEHr   c                     t                      }|j        |_        |j        |_        |                     |j                  |_        t          |j                  |_        |S )z
    Decompress c (a TLSCompressed instance) using compression algorithm
    instance alg and return a TLSPlaintext instance.
    )r   r   r   
decompressr   r   r+   r-   r,   s      r   _tls_decompressr2   4   sH    
 	AVAF	AI^^AF##AFKKAEHr   c                     t          j        d|          }|                     |t          |          z             }|xj        |z  c_        |xj        | j        z  c_        dS )a^  
    Compute the MAC using provided MAC alg instance over TLSCiphertext c using
    current write sequence number write_seq_num. Computed MAC is then appended
    to c.data and c.len is updated to reflect that change. It is the
    caller responsibility to increment the sequence number after the operation.
    The function has no return value.
    !QN)structpackdigestbytesr   r   hash_len)r+   r-   write_seq_numhs       r   _tls_mac_addr<   A   sV     Km44M

=588+,,AFFaKFFEES\EEEEr   c                    | j         }|j        |k     rdS |j        | d         }|xj        |z  c_        |j        d|          |_        t          j        d|          }|                     |t          |          z             }||k    S )a9  
    Verify if the MAC in provided message (message resulting from decryption
    and padding removal) is valid. Current read sequence number is used in
    the verification process.

    If the MAC is valid:
     - The function returns True
     - The packet p is updated in the following way: trailing MAC value is
       removed from p.data and length is updated accordingly.

    In case of error, False is returned, and p may have been modified.

    Also note that it is the caller's responsibility to update the read
    sequence number after the operation.
    FNr4   )r9   r   r   r5   r6   r7   r8   )r+   r,   read_seq_numh_size
received_hr;   s         r   _tls_mac_verifyrA   O   s      \Fuv~~u!JEEVOEEVHfWHAF;t\22L

<%((*++A
?r   c                     | j          |z  }t          |          |dz   z  }| xj         t          |          z  c_         | xj        |z  c_        dS )a6  
    Provided with cipher block size parameter and current TLSCompressed packet
    p (after MAC addition), the function adds required, deterministic padding
    to p.data before encryption step, as it is defined for TLS (i.e. not
    SSL and its allowed random padding). The function has no return value.
       N)r   r   r   )r,   
block_sizepadlenpaddings       r   _tls_add_padrG   k   sP     eVj F&kkVaZ(GEES\\EEFFgFFFFr   c                    | j         dk     rt          d           dS t          | j        d                   }|dz   }| j         |k     rt          d           dS | j        | d         t	          |          |z  k    r-t          dt          | j        | d                              dS | j        d|          | _        | xj         |z  c_         dS )	ak  
    Provided with a just decrypted TLSCiphertext (now a TLSPlaintext instance)
    p, the function removes the trailing padding found in p.data. It also
    performs some sanity checks on the padding (length, content, ...). False
    is returned if one of the check fails. Otherwise, True is returned,
    indicating that p.data and p.len have been updated.
    rC   z#Message format is invalid (padding)FzInvalid padding lengthNzPadding content is invalid %sT)r   r   r   r   r   repr)r,   rE   padsizes      r   _tls_del_padrL   x   s     	uqyy5666u__FqjGuw()))uvwhiiCKK'111/afgXYY6G1H1HIIIuVIgXIAFEEWEE4r   c                     t                      }|j        |_        |j        |_        |                     |j                  |_        t          |j                  |_        |S )z
    Provided with an already MACed TLSCompressed packet, and a stream or block
    cipher alg, the function converts it into a TLSCiphertext (i.e. encrypts it
    and updates length). The function returns a newly created TLSCiphertext
    instance.
    )r'   r   r   encryptr   r   r*   s      r   _tls_encryptrO      sH     	AVAF	AI[[  AFKKAEHr   c                     t                      }|j        |_        |j        |_        |                     |j                  |_        t          |j                  |_        |S )z
    Provided with a TLSCiphertext instance c, and a stream or block cipher alg,
    the function decrypts c.data and returns a newly created TLSPlaintext.
    )r   r   r   decryptr   r   r1   s      r   _tls_decryptrR      sH    
 	AVAF	AI[[  AFKKAEHr   c                    t          |          }t          j        d|          }||dd         z   }t                      }|j        |_        |j        |_        |                     |||          |_        t          |j                  |_        |S )a  
    Provided with a TLSCompressed instance p, the function applies AEAD
    cipher alg to p.data and builds a new TLSCiphertext instance. Unlike
    for block and stream ciphers, for which the authentication step is done
    separately, AEAD alg does it simultaneously: this is the reason why
    write_seq_num is passed to the function, to be incorporated in
    authenticated data. Note that it is the caller's responsibility to increment  # noqa: E501
    write_seq_num afterwards.
    r4   N   )	r8   r5   r6   r'   r   r   auth_encryptr   r   )r+   r,   r:   PAr-   s         r   _tls_aead_auth_encryptrX      sz     	aAKm44M"1"AAVAF	AIaM22AFKKAEHr   c                 h   |j         t          | dd          z
  | j        z
  }t          j        d|          }|t          j        d|j        |j        |          z   }t                      }|j        |_        |j        |_        ||_         |                     ||j	        |          |_	        |j	        dS |S )a  
    Provided with a TLSCiphertext instance c, the function applies AEAD
    cipher alg auth_decrypt function to c.data (and additional data)
    in order to authenticate the data and decrypt c.data. When those
    steps succeed, the result is a newly created TLSCompressed instance.
    On error, None is returned. Note that it is the caller's responsibility to
    increment read_seq_num afterwards.
    nonce_explicit_lenr   r4   z!BHHN)
r   getattrtag_lenr5   r6   r   r   r#   auth_decryptr   )r+   r-   r>   plenrW   r,   s         r   _tls_aead_auth_decryptr_      s     573 4a8883;FD;t\22Lv{61619dCCCAAVAF	AIAEa66AFv~tHr   )__doc__r5   scapy.compatr   r   scapy.errorr   scapy.fieldsr   r   r   r	   scapy.packetr
   scapy.layers.tls.basefieldsr   r   r   r#   r'   r.   r2   r<   rA   rG   rL   rO   rR   rX   r_   r!   r   r   <module>rf      s     ! ! ! ! ! ! ! !      6 6 6 6 6 6 6 6 6 6 6 6       ? ? ? ? ? ? ? ?A A A A A6 A A A    L       L   
 
 

 
 
    8
 
 
  <  
 
 
  ,    r   