o
    ™¨Êh>*  ã                   @   sÀ   d dl mZmZmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ dd„ Zdd	„ Ze ej¡G d
d„ deƒƒZdd„ Zdd„ Ze ej¡G dd„ deƒƒZe ej¡G dd„ deƒƒZdS )é    )Úabsolute_importÚdivisionÚprint_function)Úutils)ÚUnsupportedAlgorithmÚ_Reasons)Úserialization)Údhc                 C   sŒ   |j }|j}| | ¡}| ||jk¡ | ||j¡}|jrD| d¡}| 	| |j||j¡ | 
|d ¡}| ||j||j¡}| |dk¡ |S ©Nú	BIGNUM **r   é   )Ú_libÚ_ffiÚDHparams_dupÚopenssl_assertÚNULLÚgcÚDH_freeÚ"CRYPTOGRAPHY_OPENSSL_LESS_THAN_102ÚnewÚDH_get0_pqgÚBN_dupÚDH_set0_pqg)Údh_cdataÚbackendÚlibÚffiÚparam_cdataÚqÚq_dupÚres© r!   úY/var/www/html/env/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/dh.pyÚ_dh_params_dup   s   

r#   c                 C   s   t | |ƒ}t||ƒS ©N)r#   Ú_DHParameters)r   r   r   r!   r!   r"   Ú_dh_cdata_to_parameters   s   

r&   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
r%   c                 C   s   || _ || _d S r$   )Ú_backendÚ	_dh_cdata)Úselfr   r   r!   r!   r"   Ú__init__&   s   
z_DHParameters.__init__c                 C   sÄ   | j j d¡}| j j d¡}| j j d¡}| j j | j|||¡ | j  |d | j jjk¡ | j  |d | j jjk¡ |d | j jjkrFd }n| j  |d ¡}t	j
| j  |d ¡| j  |d ¡|d�S )Nr   r   ©ÚpÚgr   )r'   r   r   r   r   r(   r   r   Ú
_bn_to_intr	   ÚDHParameterNumbers)r)   r,   r-   r   Úq_valr!   r!   r"   Úparameter_numbers*   s   ýz_DHParameters.parameter_numbersc                 C   s   | j  | ¡S r$   )r'   Úgenerate_dh_private_key©r)   r!   r!   r"   Úgenerate_private_key;   s   z"_DHParameters.generate_private_keyc                 C   s~   |t jjur
tdƒ‚| jjjs6| jj d¡}| jj 	| j
| jjj|| jjj¡ |d | jjjkr6tdtjƒ‚| j ||| j
¡S )Nz%Only PKCS3 serialization is supportedr   r   ú'DH X9.42 serialization is not supported)r   ÚParameterFormatÚPKCS3Ú
ValueErrorr'   r   ÚCryptography_HAS_EVP_PKEY_DHXr   r   r   r(   r   r   r   ÚUNSUPPORTED_SERIALIZATIONÚ_parameter_bytes©r)   ÚencodingÚformatr   r!   r!   r"   Úparameter_bytes>   s*   ÿ
ýþýz_DHParameters.parameter_bytesN)Ú__name__Ú
__module__Ú__qualname__r*   r1   r4   r?   r!   r!   r!   r"   r%   $   s
    r%   c                 C   s(   |j }| | d  |j|j¡¡ tdƒ‚)Nr   z.Public key value is invalid for this exchange.)r   r   Ú_lib_reason_matchÚ
ERR_LIB_DHÚDH_R_INVALID_PUBKEYr8   )Úerrorsr   r   r!   r!   r"   Ú_handle_dh_compute_key_errorU   s   ÿÿrG   c                 C   sL   | j  d¡}| j ||| j j| j j¡ |  |d | j jk¡ | j |d ¡S )Nr   r   )r   r   r   r   r   r   ÚBN_num_bits)r   r   r,   r!   r!   r"   Ú_get_dh_num_bitsa   s   
þrI   c                   @   sH   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S )Ú_DHPrivateKeyc                 C   s&   || _ || _|| _| j j |¡| _d S r$   )r'   r(   Ú	_evp_pkeyr   ÚDH_sizeÚ_key_size_bytes©r)   r   r   Úevp_pkeyr!   r!   r"   r*   l   ó   z_DHPrivateKey.__init__c                 C   ó   t | j| jƒS r$   )rI   r'   r(   r3   r!   r!   r"   Úkey_sizer   s   z_DHPrivateKey.key_sizec                 C   sT  | j j d¡}| j j d¡}| j j d¡}| j j | j|||¡ | j  |d | j jjk¡ | j  |d | j jjk¡ |d | j jjkrFd }n| j  |d ¡}| j j d¡}| j j d¡}| j j 	| j||¡ | j  |d | j jjk¡ | j  |d | j jjk¡ t
jt
jt
j| j  |d ¡| j  |d ¡|d�| j  |d ¡d�| j  |d ¡d�S )Nr   r   r+   ©r1   Úy)Úpublic_numbersÚx)r'   r   r   r   r   r(   r   r   r.   ÚDH_get0_keyr	   ÚDHPrivateNumbersÚDHPublicNumbersr/   )r)   r,   r-   r   r0   Úpub_keyÚpriv_keyr!   r!   r"   Úprivate_numbersv   s2   ýú÷z_DHPrivateKey.private_numbersc                 C   sÖ   | j j d| j¡}| j j d¡}| j j |j|| j jj¡ | j  |d | j jjk¡ | j j 	||d | j¡}|dkrE| j  
¡ }t|| j ƒS | j  |dk¡ | j j |¡d |… }| jt|ƒ }|dkrid| | }|S )Nzunsigned char[]r   r   éÿÿÿÿr   ó    )r'   r   r   rM   r   rW   r(   r   r   ÚDH_compute_keyÚ_consume_errorsrG   ÚbufferÚlen)r)   Úpeer_public_keyÚbufrZ   r    rF   ÚkeyÚpadr!   r!   r"   Úexchange’   s(   ÿý
z_DHPrivateKey.exchangec                 C   sº   t | j| jƒ}| jj d¡}| jj | j|| jjj¡ | j |d | jjjk¡ | jj 	|d ¡}| j || jjjk¡ | jj 
||| jjj¡}| j |dk¡ | j |¡}t| j||ƒS r
   )r#   r(   r'   r   r   r   rW   r   r   r   ÚDH_set0_keyÚ_dh_cdata_to_evp_pkeyÚ_DHPublicKey)r)   r   rZ   Úpub_key_dupr    rO   r!   r!   r"   Ú
public_key­   s   
ÿ
þz_DHPrivateKey.public_keyc                 C   rQ   r$   ©r&   r(   r'   r3   r!   r!   r"   Ú
parameters½   ó   z_DHPrivateKey.parametersc                 C   s„   |t jjur
tdƒ‚| jjjs6| jj d¡}| jj 	| j
| jjj|| jjj¡ |d | jjjkr6tdtjƒ‚| j |||| j| j
¡S )Nz0DH private keys support only PKCS8 serializationr   r   r5   )r   ÚPrivateFormatÚPKCS8r8   r'   r   r9   r   r   r   r(   r   r   r   r:   Ú_private_key_bytesrK   )r)   r=   r>   Úencryption_algorithmr   r!   r!   r"   Úprivate_bytesÀ   s.   ÿ
ýþûz_DHPrivateKey.private_bytesN)r@   rA   rB   r*   ÚpropertyrR   r\   rg   rl   rn   rt   r!   r!   r!   r"   rJ   j   s    
rJ   c                   @   s8   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	d
„ ZdS )rj   c                 C   s&   || _ || _|| _t| j | jƒ| _d S r$   )r'   r(   rK   rI   Ú_key_size_bitsrN   r!   r!   r"   r*   Û   rP   z_DHPublicKey.__init__c                 C   s   | j S r$   )rv   r3   r!   r!   r"   rR   á   s   z_DHPublicKey.key_sizec                 C   s  | j j d¡}| j j d¡}| j j d¡}| j j | j|||¡ | j  |d | j jjk¡ | j  |d | j jjk¡ |d | j jjkrFd }n| j  |d ¡}| j j d¡}| j j 	| j|| j jj¡ | j  |d | j jjk¡ t
jt
j| j  |d ¡| j  |d ¡|d�| j  |d ¡d�S )Nr   r   r+   rS   )r'   r   r   r   r   r(   r   r   r.   rW   r	   rY   r/   )r)   r,   r-   r   r0   rZ   r!   r!   r"   rU   å   s,   
ÿýúz_DHPublicKey.public_numbersc                 C   rQ   r$   rm   r3   r!   r!   r"   rn   ý   ro   z_DHPublicKey.parametersc                 C   s‚   |t jjur
tdƒ‚| jjjs6| jj d¡}| jj 	| j
| jjj|| jjj¡ |d | jjjkr6tdtjƒ‚| j ||| | jd ¡S )Nz>DH public keys support only SubjectPublicKeyInfo serializationr   r   r5   )r   ÚPublicFormatÚSubjectPublicKeyInfor8   r'   r   r9   r   r   r   r(   r   r   r   r:   Ú_public_key_bytesrK   r<   r!   r!   r"   Úpublic_bytes   s.   ÿ
ýþûz_DHPublicKey.public_bytesN)	r@   rA   rB   r*   ru   rR   rU   rn   rz   r!   r!   r!   r"   rj   Ù   s    
rj   N)Ú
__future__r   r   r   Úcryptographyr   Úcryptography.exceptionsr   r   Úcryptography.hazmat.primitivesr   Ú)cryptography.hazmat.primitives.asymmetricr	   r#   r&   Úregister_interfaceÚDHParametersWithSerializationÚobjectr%   rG   rI   ÚDHPrivateKeyWithSerializationrJ   ÚDHPublicKeyWithSerializationrj   r!   r!   r!   r"   Ú<module>   s   
0
	
n