
    h               
          d Z ddlZddlmZmZmZmZ ddlmZ ddddd	Z	d
d
dddddddd	Z
dddddddddd	Zd Z G d de          Z G d d e          Z G d! d"e          Z G d# d$e          Z G d% d&e          Z G d' d(e          Z G d) d*e          Z G d+ d,e          Z G d- d.e          Z G d/ d0e          Z G d1 d2e          ZdS )3z
TLS base fields, used for record parsing/building. As several operations depend
upon the TLS version or ciphersuite, the packet has to provide a TLS context.
    N)	ByteFieldShortEnumField
ShortFieldStrField)orbchange_cipher_specalert	handshakeapplication_data)            SSLv2SSLv3zTLS 1.0zTLS 1.1zTLS 1.2zTLS 1.3-d18zTLS 1.3-d19zTLS 1.3)	   i                  r   r   r   r   r   r   r   r   )	sslv2sslv3tls1tls10tls11tls12z	tls13-d18z	tls13-d19tls13c                     | dk     r| S |S )Nr    )versionlegacy_versions     _/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/layers/tls/basefields.py_tls13_version_filterr&   *   s        c                       e Zd ZdZd Zd ZdS )_TLSClientVersionFieldzr
    We use the advertised_tls_version if it has been defined,
    and the legacy 0x0303 for TLS 1.3 packets.
    c                 J    | |j         j        }|rt          |d          S dS |S )Nr    tls_sessionadvertised_tls_versionr&   selfpktxvs       r%   i2hz_TLSClientVersionField.i2h7   s3    96A 8,Q7772r'   c                 J    | |j         j        }|rt          |d          S dS |S )Nr   r'   r,   r/   s       r%   i2mz_TLSClientVersionField.i2m?   s3    96A 8,Q7773r'   N__name__
__module____qualname____doc__r4   r6   r"   r'   r%   r)   r)   1   s<         
      r'   r)   c                       e Zd ZdZd Zd ZdS )_TLSVersionFieldz
    We use the tls_version if it has been defined, else the advertised version.
    Also, the legacy 0x0301 is used for TLS 1.3 packets.
    c                 ~    |:|j         j        }|rt          |d          S |j         j        }t          |d          S |S Nr   r-   tls_versionr&   r.   r0   r1   r2   r3   adv_vs        r%   r4   z_TLSVersionField.i2hN   H    9+A <,Q777>,UF;;;r'   c                 ~    |:|j         j        }|rt          |d          S |j         j        }t          |d          S |S r?   r@   rB   s        r%   r6   z_TLSVersionField.i2mX   rD   r'   Nr7   r"   r'   r%   r=   r=   H   s<         
      r'   r=   c                        e Zd Z fdZ xZS )_TLSLengthFieldc                     t          t          |                               ||          }|j        8|j        }t          t          |                               ||          }|d|z  z  }|S )Nz    [deciphered_len= %s])superrG   i2reprdeciphered_len)r0   r1   r2   sdxds	__class__s         r%   rJ   z_TLSLengthField.i2reprd   sg    /4((//Q77)#B--44S"==B+b00Ar'   )r8   r9   r:   rJ   __classcell__rO   s   @r%   rG   rG   c   s8                r'   rG   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )_TLSIVFielda  
    As stated in Section 6.2.3.2. RFC 4346, TLS 1.1 implements an explicit IV
    mechanism. For that reason, the behavior of the field is dependent on the
    TLS version found in the packet if available or otherwise (on build, if
    not overloaded, it is provided by the session). The size of the IV and
    its value are obviously provided by the session. As a side note, for the
    first packets exchanged by peers, NULL being the default enc alg, it is
    empty (except if forced to a specific value). Also note that the field is
    kept empty (unless forced to a specific value) when the cipher is a stream
    cipher (and NULL is considered a stream cipher).
    c                     |t          |          S d}|j        j        j        j        }|dk    r'|j        j        dk    r|j        j        j        j        }n|dk    r|j        j        j        j        }|S Nr   blockr   aead)lenr-   rcsciphertyperA   
block_sizenonce_explicit_len)r0   r1   itmp_lencipher_types        r%   i2lenz_TLSIVField.i2lenz   ss    =q66Mo)05'!!*f44/-4?F""o)0CGr'   c                 
    |pdS Nr'   r"   r0   r1   r2   s      r%   r6   z_TLSIVField.i2m   s    xCr'   c                 4    ||                      ||          z   S N)r6   r0   r1   rL   vals       r%   addfieldz_TLSIVField.addfield   s    488C%%%%r'   c                    d}|j         j        j        j        }|dk    r'|j         j        dk    r|j         j        j        j        }n|dk    r|j         j        j        j        }||d          |                     ||d |                   fS rU   )r-   rY   rZ   r[   rA   r\   r]   m2i)r0   r1   rL   r_   r`   s        r%   getfieldz_TLSIVField.getfield   s    o)05'!!*f44/-4?F""o)0CG{DHHS!HWH+6666r'   c                 H    t          |                     ||                    S rf   reprr6   rd   s      r%   rJ   z_TLSIVField.i2repr   s    DHHS!$$%%%r'   N)	r8   r9   r:   r;   ra   r6   ri   rl   rJ   r"   r'   r%   rS   rS   m   si        
 

 
 
  & & &7 7 7& & & & &r'   rS   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )_TLSMACFieldc                 F    |t          |          S |j        j        j        S rf   )rX   r-   wcsmac_lenr0   r1   r^   s      r%   ra   z_TLSMACField.i2len   s     =q66M"**r'   c                     |dS |S rc   r"   rd   s      r%   r6   z_TLSMACField.i2m       93r'   c                     |S rf   r"   rg   s       r%   ri   z_TLSMACField.addfield       r'   c                    |j         j        j        j        dk    r.d|j         j        j        j                                        v r|dfS |j         j        j        }||d          |                     ||d |                   fS )NrW   Fr'   )r-   rY   rZ   r[   readyvaluesrt   rk   r0   r1   rL   r_   s       r%   rl   z_TLSMACField.getfield   s{    O&+v55S_(/5<<>>>> c6M/%-{DHHS!HWH+6666r'   c                 H    t          |                     ||                    S rf   rn   rd   s      r%   rJ   z_TLSMACField.i2repr       DHHS!$$%%%r'   Nr8   r9   r:   ra   r6   ri   rl   rJ   r"   r'   r%   rq   rq      s_        + + +
  
  7 7 7& & & & &r'   rq   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )_TLSPadFieldc                 (    |t          |          S dS )Nr   )rX   ru   s      r%   ra   z_TLSPadField.i2len   s    =q66Mqr'   c                     |dS |S rc   r"   rd   s      r%   r6   z_TLSPadField.i2m   rw   r'   c                     |S rf   r"   rg   s       r%   ri   z_TLSPadField.addfield   ry   r'   c                     |j                                         rEt          ||j        dz
                     }||d          |                     ||d |                   fS |d fS )N   )r-   consider_read_paddingr   padlenrk   r}   s       r%   rl   z_TLSPadField.getfield   sd    ?0022 	; !CJN+,,GWXX;ak : :::$wr'   c                 H    t          |                     ||                    S rf   rn   rd   s      r%   rJ   z_TLSPadField.i2repr   r   r'   Nr   r"   r'   r%   r   r      s_          
  
  	 	 	& & & & &r'   r   c                       e Zd Zd Zd ZdS )_TLSPadLenFieldc                     |S rf   r"   rg   s       r%   ri   z_TLSPadLenField.addfield   ry   r'   c                 h    |j                                         rt          j        | ||          S |d fS rf   )r-   r   r   rl   r0   r1   rL   s      r%   rl   z_TLSPadLenField.getfield   s6    ?0022 	4%dC333$wr'   N)r8   r9   r:   ri   rl   r"   r'   r%   r   r      s2              r'   r   c                   $     e Zd Z fdZd Z xZS )_SSLv2LengthFieldc                     t          t          |                               ||          }|j        r|dz  }|dt	          |          z  z  }|S )N   z    [with padding: %s])rI   r   rJ   with_paddinghex)r0   r1   r2   rL   rO   s       r%   rJ   z_SSLv2LengthField.i2repr   sT    #T**11#q99 	3KA )CFF22Ar'   c                     t          j        d|d d                   d         }|dz  dk    |_        |j        r|dz  }n|dz  }|dd          |fS )Nz!Hr   r   r   i?  i  )structunpackr   )r0   r1   rL   msglenmsglen_cleans        r%   rl   z_SSLv2LengthField.getfield   sc    tQrrU++A."VO1 	+!F?LL!F?Lul""r'   )r8   r9   r:   rJ   rl   rP   rQ   s   @r%   r   r      sG            # # # # # # #r'   r   c                       e Zd ZdS )_SSLv2MACFieldN)r8   r9   r:   r"   r'   r%   r   r      s        Dr'   r   c                       e Zd Zd ZdS )_SSLv2PadFieldc                 v    |j         /|j         }||d          |                     ||d |                   fS |d fS rf   )r   rk   r}   s       r%   rl   z_SSLv2PadField.getfield   sC    :!jGWXX;ak : :::$wr'   Nr8   r9   r:   rl   r"   r'   r%   r   r      s#            r'   r   c                       e Zd Zd ZdS )_SSLv2PadLenFieldc                 D    |j         rt          j        | ||          S |d fS rf   )r   r   rl   r   s      r%   rl   z_SSLv2PadLenField.getfield  s+     	4%dC333$wr'   Nr   r"   r'   r%   r   r     s#            r'   r   )r;   r   scapy.fieldsr   r   r   r   scapy.compatr   	_tls_type_tls_version_tls_version_optionsr&   r)   r=   rG   rS   rq   r   r   r   r   r   r   r"   r'   r%   <module>r      s     H H H H H H H H H H H H      %#% %	
  !!!%%!# # "(!' &!'!'!'%+%+!') )       ^   .    ~   6    j   *& *& *& *& *&( *& *& *&Z& & & & &8 & & &<& & & & &8 & & &>    i   # # # # # # # #(	 	 	 	 	\ 	 	 	    \            r'   