o
    �õ±j/  ã                   @   s”   d dl Z d dl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mZmZmZmZmZmZmZ G dd	„ d	ƒZG d
d„ dƒZdS )é    N)Úsha256)ÚCertificate)ÚasyauthSecret)Úlogger)ÚSPNEGOClientNative)Ú	NegoDatasÚNegoDataÚ	TSRequestr	   ÚTSPasswordCredsÚTSCredentialsÚTSRemoteGuardCredsÚTSRemoteGuardPackageCredc                   @   s4   e Zd Zdd„ Zdd„ Zddd„Zdefd	d
„ZdS )Ú	SSLTunnelc                 C   sp   t  t j¡| _d| j_t j| j_| j jt jdB dB O  _t  	¡ | _
t  	¡ | _| jj| j
| jdd�| _d| _d S )NFi   i   )Úserver_side)ÚsslÚ
SSLContextÚPROTOCOL_TLS_CLIENTÚssl_ctxÚcheck_hostnameÚ	CERT_NONEÚverify_modeÚoptionsÚOP_NO_COMPRESSIONÚ	MemoryBIOÚtls_in_buffÚtls_out_buffÚwrap_bioÚtls_objÚhandshake_done©Úself© r!   úš/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asyauth/protocols/credssp/client/native.pyÚ__init__   s   



zSSLTunnel.__init__c                 C   s   | j jdd�S )NT)Úbinary_form)r   Úgetpeercertr   r!   r!   r"   Úget_peer_certificate   ó   zSSLTunnel.get_peer_certificateNc                 C   sž   |d ur
| j  |¡ | jdu r6z| j ¡  W n tjy/   	 | j ¡ }|dkr,| Y S 	 Y n   ‚ d| _d}	 z	|| j ¡ 7 }W n tjyM   Y |S w q9)NFTó    )	r   Úwriter   r   Údo_handshaker   ÚSSLWantReadErrorr   Úread)r    ÚdataÚclient_helloÚbuffr!   r!   r"   Údata_in    s.   

þýzSSLTunnel.data_inr-   c                 C   s4   | j  |¡ d}	 | j ¡ }|dkr||7 }q	 |S )Nr(   )r   r)   r   r,   )r    r-   r/   Úrawr!   r!   r"   Údata_out;   s   
zSSLTunnel.data_out©N)Ú__name__Ú
__module__Ú__qualname__r#   r&   r0   Úbytesr2   r!   r!   r!   r"   r      s
    

r   c                   @   s¤   e Zd Zdd„ Ze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d'dd„Zdd„ Zd d!„ Zd"d#„ Zd(d%d&„Zd$S ))ÚCredSSPClientNativec                 C   s@   || _ d | _d | _d| _t d¡| _d| _d| _d | _	d | _
d S )Né   é    Tr   )Ú
credentialÚauth_ctxÚcredÚversionÚosÚurandomÚnonceÚ,_CredSSPClientNative__internal_auth_continueÚseqnoÚ)_CredSSPClientNative__internal_ssl_tunnelÚ_CredSSPClientNative__pubkey)r    r;   r!   r!   r"   r#   I   s   
zCredSSPClientNative.__init__c                 C   sT   t  | ¡}|d d d j}|d dkrt d¡ |dd … S t d| ¡  ¡ |S )NÚtbs_certificateÚsubject_public_key_infoÚ
public_keyr   z!CredSSP - removing pubkey paddingé   zCredSSP - pubkey: %s)r   ÚloadÚcontentsr   ÚdebugÚhex)ÚcertdataÚcertÚpubkeyr!   r!   r"   Úcertificate_to_pubkeyT   s   

z)CredSSPClientNative.certificate_to_pubkeyc                 C   s:   | j d u rd S | j j ¡ }|d u st|ƒdkrd S |d S )Nr   )rD   r   ÚcipherÚlen)r    Úciphersr!   r!   r"   Úget_cipher_name`   s   
z#CredSSPClientNative.get_cipher_namec                 C   ó
   | j  ¡ S r3   )r<   Úget_extra_infor   r!   r!   r"   rW   h   ó   
z"CredSSPClientNative.get_extra_infoc                 C   rV   r3   ©r<   Úget_active_credentialr   r!   r!   r"   rZ   k   rX   z)CredSSPClientNative.get_active_credentialc                 C   rV   r3   )r;   Úbuild_contextr   r!   r!   r"   Úget_copyn   rX   zCredSSPClientNative.get_copyc                 C   s   | j S r3   )rC   r   r!   r!   r"   Úget_internal_seqq   s   z$CredSSPClientNative.get_internal_seqc                 C   s   | j  ¡  d S r3   rY   r   r!   r!   r"   rZ   t   r'   c                 C   rV   r3   )r<   Úis_guestr   r!   r!   r"   r^   w   rX   zCredSSPClientNative.is_guestc                 C   rV   r3   )r<   Úsigning_neededr   r!   r!   r"   r_   z   rX   z"CredSSPClientNative.signing_neededc                 C   rV   r3   )r<   Úencryption_neededr   r!   r!   r"   r`   }   rX   z%CredSSPClientNative.encryption_neededc                 C   rV   r3   )r<   Úget_seq_numberr   r!   r!   r"   ra   €   rX   z"CredSSPClientNative.get_seq_numberc                 Ã   ó
   �t dƒ‚)Nz2CredSSP - verify is not supported by the protocol!©Ú	Exception)r    r-   Ú	signaturer!   r!   r"   Úverifyƒ   ó   €zCredSSPClientNative.verifyÚinitFc                 Ã   rb   )Nz0CredSSP - sign is not supported by the protocol!rc   )r    r-   Ú
message_noÚ	directionÚreset_cipherr!   r!   r"   Úsign†   rg   zCredSSPClientNative.signc                 Ï   ó$   �| j d u r
tdƒ‚| j  |¡d fS ©Nz"CredSSP - no SSL tunnel available!)rD   rd   r2   ©r    r-   ri   ÚargsÚkwargsr!   r!   r"   Úencrypt‰   ó   €
zCredSSPClientNative.encryptc                 Ï   rm   rn   )rD   rd   r0   ro   r!   r!   r"   ÚdecryptŽ   rs   zCredSSPClientNative.decryptc                 C   s&   | j d ur| j  |¡||fS |||fS r3   )rD   r2   )r    r-   Úto_continueÚerrr!   r!   r"   Ú__return“   s   

zCredSSPClientNative.__returnNc              
   Ë   sâ  ��zÖ| j d u r!|d urt |¡| _ n| jd u r!t d¡ tƒ | _| jd urY| j |¡}| jjdu rNt d¡ | j d u rEt | j 	¡ ¡| _ |}|dkrMd }nt d¡ |dd fW S t d| j
 ¡ t d| ¡ |d u r§| jj|||d�I d H \}| _
}	|	d ur‚|	‚d	|i}
| jtt|
ƒgƒd
œ}t|ƒ}t d|j ¡ |  | ¡ dd ¡W S | j
du �r¦t |¡}t d|j ¡ |jd | jk rÕt d|jd | jf ¡ |jd | _|jd d u ràtdƒ‚|jd d d	 }t d| ¡ | jj|||d�I d H \}| _
}	|	d u�r|	‚d	|i}
d| ji}|d u�rtt|
ƒgƒ|d< t d| j
 ¡ | j
du �r�| j ¡ | _| jdv �rod}t|| j | j  ƒ ¡ }t d| ¡  ¡ | j || j¡I d H \}}|  jd7  _|| |d< | j|d< n!| jdv �r�| j | j | j¡I d H \}}|  jd7  _|| |d< t|ƒ}t d|j ¡ |  | ¡ dd ¡W S t d¡ t |¡j}|d d u�rÄtdt|d d @ ƒ ƒ‚|d d u �rÏtd!ƒ‚| j |d d¡I d H \}}t d"| ¡  ¡ | jdv �rd#}t|| j | j  ƒ ¡ }t d| ¡  ¡ ||k�rtd$ƒ‚n| jdv �r%|dd … | j dd … k�r%td$ƒ‚|du �r™dddd%œ}| j ¡ }|jt j!k�rX|j"}|j"d u �rGd&}| #d'¡|j$ #d'¡|j% #d'¡d%œ}t&|ƒ}t'd| ¡ d(œƒ}| j | ¡ | j¡I d H \}}|  jd7  _| j|| d)œ}t|ƒ}t d*|j ¡ |  | ¡ dd ¡W S t(d+ƒ d}d, #d'¡|d-œ}t)d.t*|ƒiƒ}| j | ¡ | j¡I d H \}}|  jd7  _| j|| d)œ}|  t|ƒ ¡ dd ¡W S  t�yð } zd d |fW  Y d }~S d }~ww )/NzFCredSSP - creating internal SSL tunnel, as no certificate was providedTz#CredSSP - SSL tunnel handshake doner(   z+CredSSP - SSL tunnel handshake not done yetzCredSSP - auth continue: %szCredSSP - token: %s)ÚflagsÚspnÚ	negoToken)r>   Ú
negoTokensz(CredSSP - sending initial auth token: %sz#CredSSP - got token from server: %sr>   zS[CREDSSP] Server supports version %s which is smaller than our supported version %sr{   z!SSPI auth not supported by serverr   zCredSSP - SSPI token: %sz$CredSSP - internal auth continue: %sF)é   r9   s&   CredSSP Client-To-Server Binding Hash zCredSSP - ClientServerHash: %srI   Ú
pubKeyAuthÚclientNonce)é   é   é   z)CredSSP - sending internal auth token: %sz CredSSP - internal auth finishedÚ	errorCodez(CredSSP - Server sent an error! Code: %sl   ÿÿ zMissing pubKeyAuthzCredSSP - verification data: %ss&   CredSSP Server-To-Client Binding Hash z%CredSSP - Server verification failed!)Ú
domainNameÚuserNameÚpasswordÚ.z	utf-16-le)ÚcredTypeÚcredentials)r>   ÚauthInfoz!CredSSP - sending credentials: %sz-DO NOT USE THIS! THIS IS NOT IMPLEMENTED YET!ÚKERBEROS)ÚpackageNameÚ
credBufferÚ	logonCred)+rE   r8   rQ   rD   r   rL   r   r0   r   r&   rB   r<   Úauthenticater>   r   r   r	   ÚnativeÚ_CredSSPClientNative__returnÚdumprJ   rd   r]   rC   r   rA   ÚdigestrM   rr   rt   rZ   Ústyper   ÚPASSWORDÚdomainÚencodeÚusernameÚsecretr
   r   Úprintr   r   )r    Útokenrx   Úcertificatery   Úremote_credguardrq   ÚssldataÚ
returndatarv   Ú	negotokenÚretokenÚresultÚtdataÚ	sspitokenÚClientServerHashMagicÚClientServerHashÚsealedMessagere   Úverification_dataÚ_Úcredsr=   r•   ÚresrŒ   r-   ÚremcredguardcredsÚer!   r!   r"   rŽ   ˜   s  €





€
 ÿþ
 
ÿÿ


ÿ
ý


ýþþÿþ€ÿz CredSSPClientNative.authenticate)rh   F)NNNF)r4   r5   r6   r#   ÚstaticmethodrQ   rU   rW   rZ   r\   r]   r^   r_   r`   ra   rf   rl   rr   rt   r�   rŽ   r!   r!   r!   r"   r8   H   s(    

r8   )r?   r   Úhashlibr   Úasn1crypto.x509r   Úasyauth.common.constantsr   Úasyauth.protocols.credsspr   Ú&asyauth.protocols.spnego.client.nativer   Ú/asyauth.protocols.credssp.messages.asn1_structsr   r   r	   r
   r   r   r   r   r8   r!   r!   r!   r"   Ú<module>   s   (7