a
    kÄ#d„2  ã                   @   sÀ   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG d d!„ d!eƒZd"S )#z
TLS Pseudorandom Function.
é    )Úabsolute_import)Úwarning)Ústrxor)Ú_tls_hash_algs)Ú_tls_hmac_algs)Úbytes_encodec                 C   st   |j j}|| d | }t|ƒ}d}|| ƒ |¡}|dkrh||| ƒ || ¡7 }|| ƒ |¡}|d8 }q2|d|… S )aß  
    Provides the implementation of P_hash function defined in
    section 5 of RFC 4346 (and section 5 of RFC 5246). Two
    parameters have been added (hm and req_len):

    - secret : the key to be used. If RFC 4868 is to be believed,
               the length must match hm.key_len. Actually,
               python hmac takes care of formatting every key.
    - seed : the seed to be used.
    - req_len : the length of data to be generated by iterating
               the specific HMAC function (hm). This prevents
               multiple calls to the function.
    - hm : the hmac function class to use for iteration (either
           Hmac_MD5 or Hmac_SHA1 in TLS <= 1.1 or
           Hmac_SHA256 or Hmac_SHA384 in TLS 1.2)
    é   ó    r   N)Zhash_algÚhash_lenr   Údigest)ÚsecretÚseedÚreq_lenZhmr
   ÚnÚresÚa© r   úR/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/layers/tls/crypto/prf.pyÚ_tls_P_hash   s    
r   c                 C   s   t | ||td ƒS )NzHMAC-MD5©r   r   ©r   r   r   r   r   r   Ú
_tls_P_MD56   s    r   c                 C   s   t | ||td ƒS )NzHMAC-SHAr   r   r   r   r   Ú_tls_P_SHA1:   s    r   c                 C   s   t | ||td ƒS )NzHMAC-SHA256r   r   r   r   r   Ú_tls_P_SHA256>   s    r   c                 C   s   t | ||td ƒS )NzHMAC-SHA384r   r   r   r   r   Ú_tls_P_SHA384B   s    r   c                 C   s   t | ||td ƒS )NzHMAC-SHA512r   r   r   r   r   Ú_tls_P_SHA512F   s    r   c                 C   s„   t d ƒ }||j d |j }d}|dkr>|| | | ¡7 }n:d}||k rxt|ƒ d¡}|| | | | ¡7 }|d7 }qB|d |… S )NÚMD5r   r	   r   Úutf8)r   r
   r   ÚstrÚencode)r   r   r   Úhash_md5Úroundsr   ÚrÚlabelr   r   r   Ú
_sslv2_PRFL   s    

r$   c                 C   s–   |dkrt dƒ dS g d¢}d}td ƒ }td ƒ }||j d |j }t|ƒD ]8}|| |d  }	| |	|  | ¡}
|| | |
 ¡7 }qP|d	|… S )
a=  
    Provides the implementation of SSLv3 PRF function:

     SSLv3-PRF(secret, seed) =
        MD5(secret || SHA-1("A" || secret || seed)) ||
        MD5(secret || SHA-1("BB" || secret || seed)) ||
        MD5(secret || SHA-1("CCC" || secret || seed)) || ...

    req_len should not be more than  26 x 16 = 416.
    i   z9_ssl_PRF() is not expected to provide more than 416 bytesÚ )ó   Aó   Bó   Có   Dó   Eó   Fó   Gó   Hó   Ió   Jó   Kó   Ló   Mó   Nó   Oó   Pó   Qó   Ró   Só   Tó   Uó   Vó   Wó   Xó   Yó   Zr	   ÚSHAr   r   N)r   r   r
   Úranger   )r   r   r   Údr   Z	hash_sha1r    r!   Úir#   Útmpr   r   r   Ú_ssl_PRF]   s    

rE   c           	      C   sT   t | ƒd d }| d|… }| | d… }t||| |ƒ}t||| |ƒ}t||ƒS )a]  
    Provides the implementation of TLS PRF function as defined in
    section 5 of RFC 4346:

    PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
                               P_SHA-1(S2, label + seed)

    Parameters are:

    - secret: the secret used by the HMAC in the 2 expansion
              functions (S1 and S2 are the halves of this secret).
    - label: specific label as defined in various sections of the RFC
             depending on the use of the generated PRF keystream
    - seed: the seed used by the expansion functions.
    - req_len: amount of keystream to be generated
    r   é   N)Úlenr   r   r   )	r   r#   r   r   Ztmp_lenZS1ZS2Za1Za2r   r   r   Ú_tls_PRF|   s    rH   c                 C   s   t | || |ƒS )a+  
    Provides the implementation of TLS 1.2 PRF function as
    defined in section 5 of RFC 5246:

    PRF(secret, label, seed) = P_SHA256(secret, label + seed)

    Parameters are:

    - secret: the secret used by the HMAC in the 2 expansion
              functions (S1 and S2 are the halves of this secret).
    - label: specific label as defined in various sections of the RFC
             depending on the use of the generated PRF keystream
    - seed: the seed used by the expansion functions.
    - req_len: amount of keystream to be generated
    )r   ©r   r#   r   r   r   r   r   Ú_tls12_SHA256PRF—   s    rJ   c                 C   s   t | || |ƒS ©N)r   rI   r   r   r   Ú_tls12_SHA384PRFª   s    rL   c                 C   s   t | || |ƒS rK   )r   rI   r   r   r   Ú_tls12_SHA512PRF®   s    rM   c                   @   sD   e Zd ZdZddd„Zddd	„Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )ÚPRFa!  
    The PRF used by SSL/TLS varies based on the version of the protocol and
    (for TLS 1.2) possibly the Hash algorithm of the negotiated cipher suite.
    The various uses of the PRF (key derivation, computation of verify_data,
    computation of pre_master_secret values) for the different versions of the
    protocol also changes. In order to abstract those elements, the common
    _tls_PRF() object is provided. It is expected to be initialised in the
    context of the connection state using the tls_version and the cipher suite.
    ÚSHA256é  c                 C   sŽ   || _ || _|dk rt| _nn|dkr,t| _n^|dks<|dkrDt| _nF|dkr‚|dkr\t| _qŠ|dkrlt| _qŠ|dv rzd| _t| _nt	d	ƒ d S )
Né   i  é  rP   ÚSHA384ÚSHA512)r   r@   rO   zUnknown TLS version)
Útls_versionÚ	hash_namer$   ÚprfrE   rH   rL   rM   rJ   r   )ÚselfrV   rU   r   r   r   Ú__init__½   s&    ÿzPRF.__init__FNc                 C   s^   || }d}|du r$|dur$|}d}| j dk r2dS | j dkrJ|  ||d¡S |  |||d¡S dS )zÙ
        Return the 48-byte master_secret, computed from pre_master_secret,
        client_random and server_random. See RFC 5246, section 6.3.
        Supports Extended Master Secret Derivation, see RFC 7627
        s   master secretTNs   extended master secretrQ   é0   ©rU   rW   )rX   Zpre_master_secretÚclient_randomÚserver_randomZextmsZhandshake_hashr   r#   r   r   r   Úcompute_master_secretÔ   s    

zPRF.compute_master_secretc                 C   s4   || }| j dkr |  |||¡S |  |d||¡S dS )zŠ
        Perform the derivation of master_secret into a key_block of req_len
        requested length. See RFC 5246, section 6.3.
        rQ   s   key expansionNr[   )rX   Úmaster_secretr]   r\   r   r   r   r   r   Úderive_key_blocké   s    
zPRF.derive_key_blockc                 C   sN  | j dk rdS | j dkr´|dkr,dddœ}n
dddœ}|| }d}d}d	}	d
}
td ƒ }td ƒ }| || | || | | ¡ ¡}| ||
 | || | |	 ¡ ¡}|| }n–|dkrÈdddœ}n
dddœ}d||   ¡ }| j dk�r$td ƒ  |¡}td ƒ  |¡}|  |||| d¡}n&t| j ƒ }| |¡}|  |||d¡}|S )a  
        Return verify_data based on handshake messages, connection end,
        master secret, and read_or_write position. See RFC 5246, section 7.4.9.

        Every TLS 1.2 cipher suite has a verify_data of length 12. Note also::

            "This PRF with the SHA-256 hash function is used for all cipher
            suites defined in this document and in TLS documents published
            prior to this document when TLS 1.2 is negotiated."

        Cipher suites using SHA-384 were defined later on.
        rQ   NÚwrites   CLNTs   SRVR)ÚclientÚservers0   666666666666666666666666666666666666666666666666s0   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\s(   6666666666666666666666666666666666666666s(   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\r   r@   rb   rc   z%s finishedrR   é   )rU   r   r   r   rW   rV   )rX   Úcon_endÚread_or_writeZhandshake_msgr_   rB   r#   Zsslv3_md5_pad1Zsslv3_md5_pad2Zsslv3_sha1_pad1Zsslv3_sha1_pad2Úmd5Úsha1Zmd5_hashZ	sha1_hashZverify_dataÚs1Ús2ÚhÚsr   r   r   Úcompute_verify_dataõ   sP    






ÿÿÿ

ÿÿÿ


zPRF.compute_verify_datac                 C   sŽ   || }|dkp|dk}| j dk r&dS | j dkrh|rB|| | }n|| | }td ƒ  |¡d|… }	n"|rrd}
nd}
|  ||
|| |¡}	|	S )zÉ
        Postprocess cipher key for EXPORT ciphersuite, i.e. weakens it.
        An export key generation example is given in section 6.3.1 of RFC 2246.
        See also page 86 of EKR's book.
        ÚclientwriteÚ
serverreadrQ   Nr   s   client write keys   server write key©rU   r   r   rW   )rX   Úkeyr\   r]   re   rf   r   rl   ÚtbhZ
export_keyÚtagr   r   r   Úpostprocess_key_for_export0  s$    

ýzPRF.postprocess_key_for_exportc           
      C   sš   || }|dkp|dk}| j dk r&dS | j dkr`|r>|| }n|| }td ƒ  |¡d|… }n6|  dd|| d| ¡}	|rŠ|	d|… }n|	|d… }|S )	z½
        Generate IV for EXPORT ciphersuite, i.e. weakens it.
        An export IV generation example is given in section 6.3.1 of RFC 2246.
        See also page 86 of EKR's book.
        rn   ro   rQ   Nr   r%   s   IV blockrF   rp   )
rX   r\   r]   re   rf   r   rl   rr   ZivZiv_blockr   r   r   Úgenerate_iv_for_exportM  s$    


ýzPRF.generate_iv_for_export)rO   rP   )FN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__rY   r^   r`   rm   rt   ru   r   r   r   r   rN   ²   s   

 ÿ
;rN   N)ry   Ú
__future__r   Zscapy.errorr   Zscapy.utilsr   Zscapy.layers.tls.crypto.hashr   Zscapy.layers.tls.crypto.h_macr   Zscapy.compatr   r   r   r   r   r   r   r$   rE   rH   rJ   rL   rM   ÚobjectrN   r   r   r   r   Ú<module>   s&    