
    h                        d Z ddl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mZmZmZmZmZmZ ddlmZ ddlmZmZmZ ddlmZmZ dd	lmZ dd
lmZm Z  ddl!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z) ej*        rddl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ej2        rddl-m3Z3 ddl-m4Z4 i dddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2i d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWZ5dX Z6dY Z7 G dZ d[e          Z8 G d\ d]e          Z9 G d^ d_e          Z: G d` dae          Z; G db dce          Z< G dd dee          Z= G df dge          Z> G dh die          Z? G dj dke          Z@dldmdndoZAdpdqdrZB G ds dte          ZC G du dve          ZD G dw dxe          ZEeDeEdrZF G dy dze	          ZG G d{ d|e          ZH G d} d~e          ZI G d de          ZJ G d de          ZKeIeJeKdoZLd ZM G d de          ZN G d de          ZO G d de          ZP G d de          ZQ G d de          ZR G d de          ZS G d de          ZTdS )z
TLS key exchange logic.
    N)confcrypto_validator)warning)
ByteEnumField	ByteField	EnumFieldFieldLenFieldFieldListFieldPacketFieldShortEnumField
ShortFieldStrFixedLenFieldStrLenField)orb)PacketRawPadding)	PubKeyRSA
PrivKeyRSA)_GenericTLSSessionInheritance)_tls_version_TLSClientVersionField)
pkcs_i2osp
pkcs_os2ip)_ffdh_groups_tls_named_curves_tls_named_groups_generate_tls_named_groups_import_tls_named_groups_pubbytes)default_backend)dhec)serialization)x25519)x448z	none+anon   znone+rsa   znone+dsa   z
none+ecdsa   zmd5+anoni  zmd5+rsai  zmd5+dsai  z	md5+ecdsa   z	sha1+anoni  zsha1+rsai  zsha1+dsai  z
sha1+ecdsa   zsha224+anon  z
sha224+rsai  z
sha224+dsa  zsha224+ecdsai   zsha256+anoni  z
sha256+rsai  z
sha256+dsai  zsha256+ecdsai   zsha384+anoni  z
sha384+rsai  z
sha384+dsai  zsha384+ecdsai   zsha512+anoni  z
sha512+rsai  z
sha512+dsai  zsha512+ecdsa  zsha256+rsaepssi  zsha384+rsaepssi  zsha512+rsaepss  ed25519  ed448i	  zsha256+rsapsszsha384+rsapsszsha512+rsapss)i
  i  c                 P    | j         sdS | j         j        sdS | j         j        dk     S )a%  
    We expect this. If tls_version is not set, this means we did not process
    any complete ClientHello, so we're most probably reading/building a
    signature_algorithms extension, hence we cannot be in phantom_mode.
    However, if the tls_version has been set, we test for TLS 1.2.
    Fr-   )tls_sessiontls_versionpkts    `/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/layers/tls/keyexchange.pyphantom_moder9   D   s5     ? u?& u?&//    c                       fd}|S )z
    Decorator for version-dependent fields.
    If get_or_add is True (means get), we return s, self.phantom_value.
    If it is False (means add), we return s.
    c                  b    | d d         \  }}}t          |          rr	||j        fS |S  |  S )Nr(   )r9   phantom_value)argsselfr7   sf
get_or_adds       r8   wrapperz!phantom_decorate.<locals>.wrapperX   sL    BQBxc1 	 -$,,,Hq$xr:    )rA   rB   rC   s   `` r8   phantom_decoraterE   R   s)          Nr:   c                   Z    e Zd ZdZdZ eej        d          Z eej        d          ZdS )SigAndHashAlgFieldzUsed in _TLSSignature.NTF)	__name__
__module____qualname____doc__r=   rE   r   getfieldaddfieldrD   r:   r8   rG   rG   b   sC          M	 2D99H	 2E::HHHr:   rG   c                   Z    e Zd ZdZdZ eej        d          Z eej        d          ZdS )SigAndHashAlgsLenField?Used in TLS_Ext_SignatureAlgorithms and TLSCertificateResquest.r   TFN)	rH   rI   rJ   rK   r=   rE   r	   rL   rM   rD   r:   r8   rO   rO   i   sC        IIM 6==H 6>>HHHr:   rO   c                   Z    e Zd ZdZg Z eej        d          Z eej        d          ZdS )SigAndHashAlgsFieldrP   TFN)	rH   rI   rJ   rK   r=   rE   r
   rL   rM   rD   r:   r8   rR   rR   p   sC        IIM 7>>H 7??HHHr:   rR   c                   ,     e Zd ZdZ fdZ fdZ xZS )SigLenField:There is a trick for SSLv2, which uses implicit lengths...c                     |j         j        }|r
|dk     r|d fS t          t          |                               ||          S )Nr+   )r4   r5   superrT   rL   )r?   r7   r@   v	__class__s       r8   rL   zSigLenField.getfieldz   sF    O' 	Vd7N[$''00a888r:   c                     |j         j        }|r|dk     r|S t          t          |                               |||          S )z3With SSLv2 you will never be able to add a sig_len.r+   )r4   r5   rW   rT   rM   )r?   r7   r@   valrX   rY   s        r8   rM   zSigLenField.addfield   sD    O' 	VH[$''00a===r:   )rH   rI   rJ   rK   rL   rM   __classcell__rY   s   @r8   rT   rT   w   sW        DD9 9 9 9 9> > > > > > > > >r:   rT   c                   "     e Zd ZdZ fdZ xZS )SigValFieldrU   c                 j   |j         }|j        r||j        dk     rqt          |j                  dk    r |j        d         j        j        j        dz  }nt          d           d}||d          |                     ||d |                   fS t          t          |                               ||          S )Nr+   r      zSNo client certificate provided. We're making a wild guess about the signature size.r)   )r4   r5   lenclient_certspubKeypubkeykey_sizer   m2irW   r_   rL   )r?   r7   mr@   sig_lenrY   s        r8   rL   zSigValField.getfield   s    O= 	;Q]V331>""Q&&.+29BaG N O O OWXX;ak : :::[$''00a888r:   )rH   rI   rJ   rK   rL   r\   r]   s   @r8   r_   r_      s>        DD
9 
9 
9 
9 
9 
9 
9 
9 
9r:   r_   c                        e Zd ZdZdZ edde           edddd           eddd	 
          gZ	 fdZ
d Zd Zd Zd Z xZS )_TLSSignaturea  
    Prior to TLS 1.2, digitally-signed structure implicitly used the
    concatenation of a MD5 hash and a SHA-1 hash.
    Then TLS 1.2 introduced explicit SignatureAndHashAlgorithms,
    i.e. couples of (hash_alg, sig_alg). See RFC 5246, section 7.4.1.4.1.

    By default, the _TLSSignature implements the TLS 1.2 scheme,
    but if it is provided a TLS context with a tls_version < 0x0303
    at initialization, it will fall back to the implicit signature.
    Even more, the 'sig_len' field won't be used with SSLv2.
    zTLS Digital Signaturesig_algNri   !Hsig_val)fmt	length_ofc                     | j         S N)ri   r6   s    r8   <lambda>z_TLSSignature.<lambda>       s{ r:   length_fromc                     t          t          |           j        |i | d|vr^d| _        | j        rR| j        j        rH| j        }|j        r|j        | _        d S |j        dk     r	d | _        d S |j        dk    rd| _        d S d S d S d S d S )Nrl   r.   r-   i  )rW   rk   __init__rl   r4   r5   selected_sig_alg)r?   r>   kargsr@   rY   s       r8   rx   z_TLSSignature.__init__   s    +mT""+T;U;;;E!!!DL 	*D$4$@ 	*$% *#$#5DLLL]V++#'DLLL]f,, $*DLLL "!	* 	* 	* 	* -,r:   c                 (    | j         | j        _        d S rr   )rl   r4   ry   r?   rs     r8   post_dissectionz_TLSSignature.post_dissection   s    ,0L)))r:   c                    | j         N| j        j        dk    r|                    |dd          | _        dS |                    |dd          | _        dS | j         dv rt
          | j                  d}}nBt
          | j                                      d          \  }}|                    d	          rd	}nd}|                    |||          | _        dS )
z
        Sign 'm' with the PrivKey 'key' and update our own 'sig_val'.
        Note that, even when 'sig_alg' is not None, we use the signature scheme
        of the PrivKey (neither do we care to compare the both of them).
        Nr+   pkcsmd5-sha1thmd5r/   r1   +pss)rl   r4   r5   signrn   _tls_hash_sigsplitendswith)r?   rh   keyr   r   sigs         r8   _update_sigz_TLSSignature._update_sig   s     <+v55"xxVzxBB"xxVux==|///$T\2D1&t|4::3??3<<&& AAA88Aa800DLLLr:   c                    | j         r| j        r~| j        dv rt          | j                 d}}nBt          | j                                     d          \  }}|                    d          rd}nd}|                    || j         ||          S | j        j        dk    r|                    || j         dd          S |                    || j         dd	          S d
S )z~
        Verify that our own 'sig_val' carries the signature of 'm' by the
        key associated to the Cert 'cert'.
        r   Nr   r   r   r   r+   r   r   F)rn   rl   r   r   r   verifyr4   r5   )r?   rh   certr   r   r   s         r8   _verify_sigz_TLSSignature._verify_sig   s    
 < 	K| K<#333(6qAA*4<8>>sCCFAs||E** #!"{{1dla1{===#/699;;q$,&J;OOO;;q$,&E;JJJur:   c                     t           S rr   r   r?   ps     r8   guess_payload_classz!_TLSSignature.guess_payload_class       r:   )rH   rI   rJ   rK   namerG   r   rT   r_   fields_descrx   r~   r   r   r   r\   r]   s   @r8   rk   rk      s        
 
 #D%%i}EE;y$D)24 4 4;y$+B+BD D DEK* * * * * 9 9 91 1 1,  .      r:   rk   c                   ,    e Zd ZdZdgZddZd Zd ZdS )_TLSSignatureFieldz
    Used for 'digitally-signed struct' in several ServerKeyExchange,
    and also in CertificateVerify. We can handle the anonymous case.
    rv   Nc                 L    || _         t          j        | ||t                     d S rr   )rv   r   rx   rk   r?   r   defaultrv   s       r8   rx   z_TLSSignatureField.__init__   s'    &T4-@@@@@r:   c                 h    |                      |          }|dk    rd S t          ||j                  S )Nr   r4   )rv   rk   r4   )r?   r7   rh   tmp_lens       r8   rg   z_TLSSignatureField.m2i   s8    ""3''a<<4QCO<<<<r:   c                     |                      ||          }||d fS d}t          j        |v r |t          j                 }|j        `|j        }||fS )Nr:   )rg   r   padding_layer
underlayerpayloadload)r?   r7   r@   iremainr}   s         r8   rL   z_TLSSignatureField.getfield  s\    HHS!9d7N""$$%A$VFqyr:   rr   )rH   rI   rJ   rK   	__slots__rx   rg   rL   rD   r:   r8   r   r      s]          IA A A A= = =	 	 	 	 	r:   r   c                   &    e Zd ZdZdgZddZd ZdS )_TLSServerParamsFielda  
    This is a dispatcher for the Server*DHParams below, used in
    TLSServerKeyExchange and based on the key_exchange.server_kx_msg_cls.
    When this cls is None, it means that we should not see a ServerKeyExchange,
    so we grab everything within length_from and make it available using Raw.

    When the context has not been set (e.g. when no ServerHello was parsed or
    dissected beforehand), we (kinda) clumsily set the cls by trial and error.
    XXX We could use Serv*DHParams.check_params() once it has been implemented.
    rv   Nc                 B    || _         t          j        | ||d            d S rr   )rv   r   rx   r   s       r8   rx   z_TLSServerParamsField.__init__  s'    &T4$77777r:   c                    |j         }|                     |          }|j        r]|j        j                            |          }|/t          |d |                   t          ||d                    z  S  |||          S 	 t          ||          }t          |j	        d d                   t          vrt          |S # t          $ ru t          |          } |||          }t          |j	        d d                   t          vr1t          |d |                   t          ||d                    z  cY S |cY S w xY w)Nr   r'   )r4   rv   prcskey_exchangeserver_kx_msg_clsr   r   ServerDHParamsr   r   r   	Exception_tls_server_ecdh_cls_guess)r?   r7   rh   r@   r   clsr   s          r8   rg   z_TLSServerParamsField.m2i  sT   O""3''6 	&%77::C{1XgX;'''!GHH+*>*>>>3qa((((
"1!444afRaRj))>>#O   033Cq)))afRaRj))>>q'{++gak.B.BBBBBs   <B? ?A8D>9D>=D>rr   )rH   rI   rJ   rK   r   rx   rg   rD   r:   r8   r   r     sJ        	 	 I8 8 8 8    r:   r   c            
           e Zd ZdZdZ eddd           eddd 	           ed
dd           eddd 	           eddd           eddd 	          gZed             Z	ed             Z
d Zd ZdS )r   a  
    ServerDHParams for FFDH-based key exchanges, as defined in RFC 5246/7.4.3.

    Either with .fill_missing() or .post_dissection(), the server_kx_privkey or
    server_kx_pubkey of the TLS context are updated according to the
    parsed/assembled values. It is the user's responsibility to store and
    restore the original values if he wants to keep them. For instance, this
    could be done between the writing of a ServerKeyExchange and the receiving
    of a ClientKeyExchange (which includes secret generation).
    zServer FFDH parametersdh_plenNdh_prp    c                     | j         S rr   )r   r6   s    r8   rs   zServerDHParams.<lambda>H  rt   r:   ru   dh_glendh_gc                     | j         S rr   )r   r6   s    r8   rs   zServerDHParams.<lambda>K  rt   r:   dh_Yslendh_Ysc                     | j         S rr   )r   r6   s    r8   rs   zServerDHParams.<lambda>N      s| r:   c                    | j         }t          d         d                                         }t          d         d         }| j        st	          |j        |dz            | _        | j        t          | j                  | _        d| j        dz  z  |_        | j	        st	          |j
        d          | _	        | j        d| _        t          | j                  }t          | j	                  }t          j        ||                              t!                                }| j        sh|                                |_        |j                                        }|                                j        }t	          ||j        dz            | _        | j        t          | j                  | _        |j        s	||_        dS dS )a#  
        We do not want TLSServerKeyExchange.build() to overload and recompute
        things every time it is called. This method can be called specifically
        to have things filled in a smart fashion.

        Note that we do not expect default_params.g to be more than 0xff.
        modp2048r   r&   ra   Nffdhe%s)r4   r   parameter_numbersr   r   r   r   rb   kx_groupr   gr   r   r!   DHParameterNumbers
parametersr    r   generate_private_keyserver_kx_privkey
public_keypublic_numbersyrf   r   client_kx_ffdh_params)	r?   r@   default_paramsdefault_mLenr   r   real_paramsre   r   s	            r8   fill_missingzServerDHParams.fill_missingP  s    %j1!4FFHH#J/2y 	H">#3\Q5FGGDI<ty>>DL$,"23
y 	8">#3Q77DI<DLty!!ty!!+Aq11<<_=N=NOOz 	="-"B"B"D"DA(3355F%%'')A#Av!';<<DJ= 
OODM& 	2&1A###	2 	2r:   c                    t          | j                  }t          | j                  }t          j        ||          }t          | j                  }t          j        ||          }| j        }|                    t                                |_
        d| j        dz  z  |_        |j        s(|                    t                                |_        dS dS )zE
        XXX Check that the pubkey received is in the group.
        r   ra   N)r   r   r   r!   r   r   DHPublicNumbersr4   r   r    server_kx_pubkeyr   r   r   r   )r?   r   r   pnr   r   r@   s          r8   register_pubkeyzServerDHParams.register_pubkeyy  s    
 ty!!ty!!"1a((tz""+Ar22+667H7HII$,"23
& 	G&(mmO4E4E&F&FA###	G 	Gr:   c                 R    	 |                                   d S # t          $ r Y d S w xY wrr   r   ImportErrorr|   s     r8   r~   zServerDHParams.post_dissection  A    	  """"" 	 	 	DD	    
&&c                     t           S )z
        The signature after the params gets saved as Padding.
        This way, the .getfield() which _TLSServerParamsField inherits
        from PacketField will return the signature remain as expected.
        r   r   s     r8   r   z"ServerDHParams.guess_payload_class  s	     r:   rH   rI   rJ   rK   r   r	   r   r   r   r   r   r~   r   rD   r:   r8   r   r   :  s&       	 	 $D =DFCCC;vr+B+BD D D =DFCCC;vr+B+BD D D =TWEEE;w+C+CE E EFK &2 &2 &2P G G G$      r:   r   explicit_primeexplicit_char2named_curve)r&   r'   r(   ec_basis_trinomialec_basis_pentanomial)r   r&   c                       e Zd ZdZ edddd           eddd 	           ed
ddd           eddd 	          gZdS )
ECCurvePktzElliptic CurvealenNaBrp   ro   r   c                     | j         S rr   )r   r6   s    r8   rs   zECCurvePkt.<lambda>       r:   ru   blenbc                     | j         S rr   )r   r6   s    r8   rs   zECCurvePkt.<lambda>  r   r:   )rH   rI   rJ   r   r	   r   r   rD   r:   r8   r   r     sw        D =#FFF;sB4H4HIII =#FFF;sB4H4HIIIKKKKr:   r   c                   X    e Zd ZdZdZ edddd           eddd	 
          gZd ZdS )ECTrinomialBasiszEC Trinomial Basisr   klenNkr   r   r   c                     | j         S rr   )r   r6   s    r8   rs   zECTrinomialBasis.<lambda>  r   r:   ru   c                     t           S rr   r   r   s     r8   r   z$ECTrinomialBasis.guess_payload_class  r   r:   	rH   rI   rJ   r   r[   r	   r   r   r   rD   r:   r8   r   r     sc        D
C =#FFF;sB4H4HIIIKK    r:   r   c            
           e Zd ZdZdZ edddd           eddd	 
           edddd           eddd 
           edddd           eddd 
          gZd ZdS )ECPentanomialBasiszEC Pentanomial Basisr&   k1lenNk1r   r   r   c                     | j         S rr   )r   r6   s    r8   rs   zECPentanomialBasis.<lambda>       r:   ru   k2lenk2c                     | j         S rr   )r   r6   s    r8   rs   zECPentanomialBasis.<lambda>  r   r:   k3lenk3c                     | j         S rr   )r  r6   s    r8   rs   zECPentanomialBasis.<lambda>  r   r:   c                     t           S rr   r   r   s     r8   r   z&ECPentanomialBasis.guess_payload_class  r   r:   r   rD   r:   r8   r   r     s        !D
C =$$CHHH;tR5J5JKKK =$$CHHH;tR5J5JKKK =$$CHHH;tR5J5JKKKMK    r:   r   c                   "    e Zd ZdgZddZd ZdS )_ECBasisTypeFieldbasis_type_ofr   c                 D    || _         t          j        | |||d           d S )Nr   )r  r   rx   )r?   r   r   enumr  r   s         r8   rx   z_ECBasisTypeField.__init__  s)    *4wc:::::r:   c                 p    |3|                     | j                  \  }}|                    ||          }|S rr   )getfield_and_valr  i2basis_type)r?   r7   xfldfvals        r8   i2mz_ECBasisTypeField.i2m  s;    9,,T-?@@IC  d++Ar:   N)r   )rH   rI   rJ   r   rx   r  rD   r:   r8   r  r    s?         !I; ; ; ;    r:   r  c                   (    e Zd ZddgZd Zd Zd ZdS )_ECBasisFieldclsdictbasis_type_fromc                 P    || _         || _        t          j        | ||d            d S rr   )r  r  r   rx   )r?   r   r   r  r  s        r8   rx   z_ECBasisField.__init__  s.    .T4$77777r:   c                 \    |                      |          }| j        |         } ||          S rr   )r  r  )r?   r7   rh   basisr   s        r8   rg   z_ECBasisField.m2i  s.    $$S))l5!s1vvr:   c                 <    d}	 |j         }n# t          $ r Y nw xY w|S Nr   )r[   r   )r?   r7   r  r[   s       r8   r  z_ECBasisField.i2basis_type  s:    	%CC 	 	 	D	
s    
N)rH   rI   rJ   r   rx   rg   r  rD   r:   r8   r  r    sL        -.I8 8 8
  
    r:   r  c                   n   e Zd ZdZdZ edde           edddd	           edd
d            e	dde
           edddd	           edd
d            edddd	           edd
d            edddd	           edd
d            edddd	           edd
d           gZd Zd ZdS )ServerECDHExplicitPrimeParamsz
    We provide parsing abilities for ExplicitPrimeParams, but there is no
    support from the cryptography library, hence no context operations.
    z'Server ECDH parameters - Explicit Prime
curve_typer&   plenNr   r   r   r   c                     | j         S rr   )r  r6   s    r8   rs   z&ServerECDHExplicitPrimeParams.<lambda>  r   r:   ru   curvebaselenbasec                     | j         S rr   r!  r6   s    r8   rs   z&ServerECDHExplicitPrimeParams.<lambda>  rt   r:   orderlenorderc                     | j         S rr   )r%  r6   s    r8   rs   z&ServerECDHExplicitPrimeParams.<lambda>  r   r:   cofactorlencofactorc                     | j         S rr   )r(  r6   s    r8   rs   z&ServerECDHExplicitPrimeParams.<lambda>	  s    s r:   pointlenpointc                     | j         S rr   r+  r6   s    r8   rs   z&ServerECDHExplicitPrimeParams.<lambda>  r   r:   c                 <    | j         t          d         | _         dS dS )z
        Note that if it is not set by the user, the cofactor will always
        be 1. It is true for most, but not all, TLS elliptic curves.
        Nr   r  _tls_ec_curve_typesr?   s    r8   r   z*ServerECDHExplicitPrimeParams.fill_missing  s%    
 ?"12BCDOOO #"r:   c                     t           S rr   r   r   s     r8   r   z1ServerECDHExplicitPrimeParams.guess_payload_class  r   r:   )rH   rI   rJ   rK   r   r   r1  r	   r   r   r   r   r   r   rD   r:   r8   r  r    s         5D =q2EFF =#FFF;sB4H4HIII;wj99 =DFLLL;vr+B+BD D D =T+2= = =;w+C+CE E E =+53@ @ @;z2+F+FH H H =T+2= = =;w+C+CE E E#FK(D D D    r:   r  c                   ^   e Zd ZdZdZ edde           edd           edde	d           e
d e            d	 e           ed
 e            e           edddd           eddd            edd           edd           edddd           eddd           gZd Zd ZdS )ServerECDHExplicitChar2Paramsz
    We provide parsing abilities for Char2Params, but there is no
    support from the cryptography library, hence no context operations.
    z'Server ECDH parameters - Explicit Char2r  r'   rh   N
basis_typer  c                     | j         S rr   )r6  r6   s    r8   rs   z&ServerECDHExplicitChar2Params.<lambda>&  s    S^ r:   r   r!  r"  r   r   r   c                     | j         S rr   r$  r6   s    r8   rs   z&ServerECDHExplicitChar2Params.<lambda>+  rt   r:   ru   r&  r)  r+  r,  c                     | j         S rr   r.  r6   s    r8   rs   z&ServerECDHExplicitChar2Params.<lambda>1  r   r:   c                 <    | j         t          d         | _         d S d S )Nr   r0  r2  s    r8   r   z*ServerECDHExplicitChar2Params.fill_missing3  s#    ?"12BCDOOO #"r:   c                     t           S rr   r   r   s     r8   r   z1ServerECDHExplicitChar2Params.guess_payload_class7  r   r:   )rH   rI   rJ   rK   r   r   r1  r   r  _tls_ec_basis_typesr  r   _tls_ec_basis_clsr   r   r	   r   r   r   r   r   rD   r:   r8   r5  r5    s\         5D =q2EFF:c4(($$\4%8'C C =*:*:*<*<!;!;!24 4 ;w

jAA =DFLLL;vr+B+BD D D9Wd++9Z.. =T+2= = =;w+C+CE E EFK$D D D    r:   r5  c                       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
ed             Zed             Zd Zd ZdS )ServerECDHNamedCurveParamsz$Server ECDH parameters - Named Curver  r(   r   Nr+  r,  r   r   c                     | j         S rr   r.  r6   s    r8   rs   z#ServerECDHNamedCurveParams.<lambda>B  r   r:   ru   c                    | j         }| j        t          d         | _        | j        d| _        | j        }|t          vrd}t          |          |_        t	          j        |t          |                    |_	        | j
        t          |j                  | _
        | j        t          | j
                  | _        |j        s	||_        dS dS )a&  
        We do not want TLSServerKeyExchange.build() to overload and recompute
        things every time it is called. This method can be called specifically
        to have things filled in a smart fashion.

        XXX We should account for the point_format (before 'point' filling).
        Nr      )r4   r  r1  r   r   r   r   getstrr   r,  r   r+  rb   client_kx_ecdh_params)r?   r@   curve_groups      r8   r   z'ServerECDHNamedCurveParams.fill_missingD  s     ?"1-@DO#!D&///K8EE&*;K8H8HII
:3# DJ
 = 
OODM& 	2&1A###	2 	2r:   c                     | j         }t          | j        | j                  |_        t          j        | j        t          | j                            |_        |j	        s| j        |_	        dS dS )zr
        XXX Support compressed point format.
        XXX Check that the pubkey received is on the curve.
        N)
r4   r   r   r,  r   r   rC  rD  r   rE  )r?   r@   s     r8   r   z*ServerECDHNamedCurveParams.register_pubkeyh  ss     5J
 
 '*4+;SAQ=R=RSS
& 	7&*&6A###	7 	7r:   c                 R    	 |                                   d S # t          $ r Y d S w xY wrr   r   r|   s     r8   r~   z*ServerECDHNamedCurveParams.post_dissection|  r   r   c                     t           S rr   r   r   s     r8   r   z.ServerECDHNamedCurveParams.guess_payload_class  r   r:   )rH   rI   rJ   r   r   r1  r   r   r	   r   r   r   r   r   r~   r   rD   r:   r8   r?  r?  ;  s        1D =q2EFF!>-7HII =T+2= = =;w+C+CE E E	FK !2 !2 !2F 7 7 7&      r:   r?  c                 j    | sd S t          | d                   }t                              |d           S r  )r   _tls_server_ecdh_clsrC  )rh   r  s     r8   r   r     s4     tQqTJ##J555r:   c                       e Zd ZdZdZ eddd           eddd 	           ed
dd           eddd 	          gZed             Z	ed             Z
d Zd ZdS )ServerRSAParamsa  
    Defined for RSA_EXPORT kx : it enables servers to share RSA keys shorter
    than their principal {>512}-bit key, when it is not allowed for kx.

    This should not appear in standard RSA kx negotiation, as the key
    has already been advertised in the Certificate message.
    zServer RSA_EXPORT parameters	rsamodlenNrsamodr   r   c                     | j         S rr   )rN  r6   s    r8   rs   zServerRSAParams.<lambda>      s} r:   ru   	rsaexplenrsaexpc                     | j         S rr   )rR  r6   s    r8   rs   zServerRSAParams.<lambda>  rQ  r:   c                 h   t                      }|                    d           || j        _        |j                                        }| j        s't          |j        |j        j	        dz            | _        | j
        t          | j                  | _
        d| j
        z  | j        _        t          j        t          j        |j                  t          j        d          z  dz            }| j        st          |j        |          | _        | j        t          | j                  | _        d S d S )Nr*   )
modulusLenra   rsa%sr'   g       @)r   fill_and_storer4   server_tmp_rsa_keyre   r   rO  r   nrf   rN  rb   r   mathceillogerS  rR  )r?   r   pubNumrR  s       r8   r   zServerRSAParams.fill_missing  s   LL	C(((./+((**{ 	G$VXqx/@A/EFFDK>! --DN$+dn$<!Idhvx0048A;;>CDD	{ 	:$VXy99DK>! --DNNN "!r:   c                     | j         }| j        }| j        }t          |||f          | j        _        d|z  | j        _        d S )NrW  )rN  rO  rS  r   r4   rY  r   )r?   mLenrh   r^  s       r8   r   zServerRSAParams.register_pubkey  sE    ~KK.7At.E.E+$+dN!!!r:   c                 R    	 |                                   d S # t          $ r Y d S w xY wrr   r   r?   r7   s     r8   r~   zServerRSAParams.post_dissection  r   r   c                     t           S rr   r   r   s     r8   r   z#ServerRSAParams.guess_payload_class  r   r:   r   rD   r:   r8   rM  rM    s          *D =dhGGG;x+D+DF F F =dhGGG;x+D+DF F F	GK . . .& 3 3 3      r:   rM  c                   d    e Zd ZdZdZ edddd           eddd	 
          gZd Zd Z	d Z
dS )ServerPSKParamsa  
    XXX We provide some parsing abilities for ServerPSKParams, but the
    context operations have not been implemented yet. See RFC 4279.
    Note that we do not cover the (EC)DHE_PSK key exchange,
    which should contain a Server*DHParams after 'psk_identity_hint'.
    Server PSK parameterspsk_identity_hint_lenNpsk_identity_hintrm   r   r   c                     | j         S rr   )rh  r6   s    r8   rs   zServerPSKParams.<lambda>  s	    s7P r:   ru   c                     d S rr   rD   r2  s    r8   r   zServerPSKParams.fill_missing      r:   c                     d S rr   rD   rc  s     r8   r~   zServerPSKParams.post_dissection  rl  r:   c                     t           S rr   r   r   s     r8   r   z#ServerPSKParams.guess_payload_class  r   r:   )rH   rI   rJ   rK   r   r	   r   r   r   r~   r   rD   r:   r8   rf  rf    s          #D =!8$+>DJ J J;2B+P+PR R RSK
        r:   rf  c                   x    e Zd ZdZdZ eddd           eddd 	          gZed
             Z	d Z
d Zd ZdS )ClientDiffieHellmanPublica  
    If the user provides a value for dh_Yc attribute, we assume he will set
    the pms and ms accordingly and trigger the key derivation on his own.

    XXX As specified in 7.4.7.2. of RFC 4346, we should distinguish the needs
    for implicit or explicit value depending on availability of DH parameters
    in *client* certificate. For now we can only do ephemeral/explicit DH.
    zClient DH Public Valuedh_YclenNdh_Ycr   r   c                     | j         S rr   )rq  r6   s    r8   rs   z"ClientDiffieHellmanPublic.<lambda>  r   r:   ru   c                    | j         }|j                                        |_        |j                                        }|                                j        }t          ||j        dz            | _	        |j        r]|j
        rX|j                            |j
                  }|                    d          |_        |j        s|                                 d S d S d S d S )Nra       )r4   r   r   client_kx_privkeyr   r   r   r   rf   rr  r   exchangelstrippre_master_secretextmscompute_ms_and_derive_keys)r?   r@   re   r   pmss        r8   r   z&ClientDiffieHellmanPublic.fill_missing  s    5JJLL$//11!!##%6?a#788
 	/1#5 	/%..q/ABBC"%**W"5"5A7 / ,,.....	/ 	/ 	/ 	// /r:   c                     | j         s&	 |                                  n# t          $ r Y nw xY w| j        t	          | j                   | _        t          | j        d          | j         z   |z   S )Nr'   )rr  r   r   rq  rb   r   r?   r7   pays      r8   
post_buildz$ClientDiffieHellmanPublic.post_build  s{    z 	!!####   = 
OODM$-++dj83>>    
++c                    | j         }|j        rht          | j                  }|j                                        }t          j        ||          }|                    t                                |_	        |j
        r]|j	        rX|j
                            |j	                  }|                    d          |_        |j        s|                                 dS dS dS dS )z
        First we update the client DHParams. Then, we try to update the server
        DHParams generated during Server*DHParams building, with the shared
        secret. Finally, we derive the session keys and update the context.
        ru  N)r4   r   r   rr  r   r!   r   r   r    client_kx_pubkeyr   rw  rx  ry  rz  r{  )r?   rh   r@   r   param_numbersr   ZZs          r8   r~   z)ClientDiffieHellmanPublic.post_dissection  s      " 	N4:&&A3EEGGM/=AAN!/!:!:?;L;L!M!MA 	/1#5 	/$--a.@AAB"$))G"4"4A7 /,,.....		/ 	/ 	/ 	// /r:   c                     t           S rr   r   r   s     r8   r   z-ClientDiffieHellmanPublic.guess_payload_class'  r   r:   )rH   rI   rJ   rK   r   r	   r   r   r   r   r  r~   r   rD   r:   r8   rp  rp    s          $D =TWEEE;w+C+CE E EFK / / / ? ? ?/ / /*    r:   rp  c                   t    e Zd ZdZdZ edddd           eddd	 
          gZed             Z	d Z
d ZdS )ClientECDiffieHellmanPubliczR
    Note that the 'len' field is 1 byte longer than with the previous class.
    zClient ECDH Public Value
ecdh_YclenNecdh_Ycr   r   r   c                     | j         S rr   )r  r6   s    r8   rs   z$ClientECDiffieHellmanPublic.<lambda>3  s    s~ r:   ru   c                 R   | j         }t          |j                  |_        |j                                        }t          |t          j        t          j	        f          rg|
                    t          j        j        t          j        j                  | _        |j        r&|j        r|j                            |j                  }n|                                j        }|                                j        }dt+          ||j        dz            z   t+          ||j        dz            z   | _        |j        r8|j        r1|j                            t/          j                    |j                  }|j        r+|j        r&||_        |j        s|                                 d S d S d S d S )N   ra   )r4   r   rE  rv  r   
isinstancer$   X25519PublicKeyr%   X448PublicKeypublic_bytesr#   Encodingr   PublicFormatr  r   rw  r   r  r   r   rf   r"   ECDHry  rz  r{  )r?   r@   re   r|  r  r   s         r8   r   z(ClientECDiffieHellmanPublic.fill_missing5  s   8#
 

 $//11fv5#13 4 4 	G!..&**. DL " Gq'9 G)2213EFF %%'')A%%'')A#&q&/Q*>??@&q&/Q*>??@DL " Gq'9 G)222799343EG G  	/1#5 	/"%A7 /,,.....	/ 	/ 	/ 	// /r:   c                     | j         s&	 |                                  n# t          $ r Y nw xY w| j        t	          | j                   | _        t          | j        d          | j         z   |z   S )Nr&   )r  r   r   r  rb   r   r~  s      r8   r  z&ClientECDiffieHellmanPublic.post_buildV  s}    | 	!!####   ?"!$,//DO$/1--<sBBr  c                 .   | j         }|j        rt          |j        | j                  |_        |j        r\|j        rW|j                            t          j                    |j                  }||_	        |j
        s|                                 d S d S d S d S rr   )r4   rE  r   r  r  r   rw  r"   r  ry  rz  r{  )r?   rh   r@   r  s       r8   r~   z+ClientECDiffieHellmanPublic.post_dissection`  s     " 	!9'" "A
  	/1#5 	/$--bgii9KLLB"$A7 /,,.....		/ 	/ 	/ 	// /r:   )rH   rI   rJ   rK   r   r	   r   r   r   r   r  r~   rD   r:   r8   r  r  +  s          &D =t+4#? ? ?;y"+E+EG G GHK
 / / /@C C C/ / / / /r:   r  c                   &     e Zd ZdZdZ fdZ xZS )_UnEncryptedPreMasterSecretz
    When the content of an EncryptedPreMasterSecret could not be deciphered,
    we use this class to represent the encrypted data.
    z*RSA Encrypted PreMaster Secret (protected)c                 p    |                     dd             t          t          |           j        |i |S Nr4   )poprW   r  rx   )r?   r>   rz   rY   s      r8   rx   z$_UnEncryptedPreMasterSecret.__init__z  s:    		-&&&@u0$77@$P%PPPr:   )rH   rI   rJ   rK   r   rx   r\   r]   s   @r8   r  r  s  sR          8DQ Q Q Q Q Q Q Q Qr:   r  c                   t    e Zd ZdZdZ edde           eddd          gZe	dd            Z
d Zd	 Zd
 ZdS )EncryptedPreMasterSecretzO
    Pay attention to implementation notes in section 7.4.7.1 of RFC 5246.
    zRSA Encrypted PreMaster Secretclient_versionNrandom.   c                 V    |r!d|v r|d         }|j         |j        t          S t          S r  )rY  server_rsa_keyr  r  )r   _pktr>   rz   r@   s        r8   dispatch_hookz&EncryptedPreMasterSecret.dispatch_hook  s;     	3MU**m$A#+0@0H22''r:   c                 2   | j         }|}|j        }||j        }|dk    rjt          |          dk     r|S t	          j        d|d d                   d         }t          |          |dz   k    rd}t          |           n
|dd          }|j        %|j                            |          }|dd          }n?|j	        %|j	                            |          }|dd          }nd}d}t          |           ||_
        |j        s|                                 |S )	Nr,   r'   rm   r   z7TLS 1.0+, but RSA Encrypted PMS with no explicit lengthis0                                                   z9No server RSA key to decrypt Pre Master Secret. Skipping.)r4   r5   advertised_tls_versionrb   structunpackr   rY  decryptr  ry  rz  r{  )	r?   rh   r@   tbdr5   r   err	decryptedr|  s	            r8   pre_dissectz$EncryptedPreMasterSecret.pre_dissect  s1   m2K&  1vvzzmD!BQB%003G1vv1$$Oe+,44S99ICDD/CC)(0055ICDD/CC CMCCLLL!w 	+((***
r:   c                    |}| j         }||_        |j        s|                                 |j        |j                            |d          }nQ|j        ;t          |j                  dk    r#|j        d                             |d          }nt          d           d}|j	        }||j
        }|dk    r"t          j        dt          |                    }||z   |z   S )	a>  
        We encrypt the premaster secret (the 48 bytes) with either the server
        certificate or the temporary RSA key provided in a server key exchange
        message. After that step, we add the 2 bytes to provide the length, as
        described in implementation notes at the end of section 7.4.7.1.
        Nr   )r   r   z(No material to encrypt Pre Master Secretr:   r,   rm   )r4   ry  rz  r{  rY  encryptserver_certsrb   r   r5   r  r  pack)r?   r7   r  encr@   r   r5   s          r8   r  z#EncryptedPreMasterSecret.post_build  s     !w 	+((***+&..sf.==CC^'C,?,?!,C,C.#++C6+::CC>???m2K&  k$C11G}s""r:   c                     t           S rr   r   r   s     r8   r   z,EncryptedPreMasterSecret.guess_payload_class  r   r:   rr   )rH   rI   rJ   rK   r   r   r   r   r   classmethodr  r  r  r   rD   r:   r8   r  r    s          ,D))*:D*68 8##HdB779K ( ( ( [(     D# # #:    r:   r  c                   R    e Zd ZdZdZ edddd           eddd	 
          gZdS )ClientPSKIdentitya'  
    XXX We provide parsing abilities for ServerPSKParams, but the context
    operations have not been implemented yet. See RFC 4279.
    Note that we do not cover the (EC)DHE_PSK nor the RSA_PSK key exchange,
    which should contain either an EncryptedPMS or a ClientDiffieHellmanPublic.
    rg  psk_identity_lenNpsk_identityrm   r   r   c                     | j         S rr   )r  r6   s    r8   rs   zClientPSKIdentity.<lambda>  s	    s7K r:   ru   )rH   rI   rJ   rK   r   r	   r   r   rD   r:   r8   r  r    si          #D =!3T+9tE E E;~r+K+KM M MNKKKr:   r  )UrK   r[  r  scapy.configr   r   scapy.errorr   scapy.fieldsr   r   r   r	   r
   r   r   r   r   r   scapy.compatr   scapy.packetr   r   r   scapy.layers.tls.certr   r   scapy.layers.tls.sessionr   scapy.layers.tls.basefieldsr   r   scapy.layers.tls.crypto.pkcs1r   r   scapy.layers.tls.crypto.groupsr   r   r   r   r   crypto_validcryptography.hazmat.backendsr    )cryptography.hazmat.primitives.asymmetricr!   r"   cryptography.hazmat.primitivesr#   crypto_valid_advancedr$   r%   r   r9   rE   rG   rO   rR   rT   r_   rk   r   r   r   r1  r<  r   r   r   r=  r  r  r  r5  r?  rK  r   rM  rf  rp  r  r  r  r  rD   r:   r8   <module>r     s      / / / / / / / /      " " " " " " " " " " " " " " " " " " " " " " " "       - - - - - - - - - - 7 7 7 7 7 7 7 7 B B B B B B L L L L L L L L @ @ @ @ @ @ @ @               =<<<<<<@@@@@@@@<<<<<< ?@@@@@@>>>>>>C Cfj CC%+\CC%+YC C %+KC 	C '-j	C
 C
 &,\C C )/C C (.~C C C )/C C (.~C C )/C C (.~C C )/C C (.~C )C ,23CC )C ,29C  !C  #)/!C C" )/#C C C(0 0 0   ; ; ; ; ; ; ; ;? ? ? ? ?] ? ? ?@ @ @ @ @. @ @ @> > > > >- > > >"9 9 9 9 9+ 9 9 9 U U U U U1 U U Up       :$ $ $ $ $K $ $ $Z^ ^ ^ ^ ^2 ^ ^ ^F +*') )  /3IJJ K K K K K K K K    v           )-?@@            K   <# # # # #$A # # #L    $A   @H H H H H!> H H HV 9857 7 
6 6 63 3 3 3 33 3 3 3p    f   :? ? ? ? ? = ? ? ?DC/ C/ C/ C/ C/"? C/ C/ C/P	Q 	Q 	Q 	Q 	Q# 	Q 	Q 	QQ Q Q Q Q< Q Q QlN N N N N N N N N Nr:   