o
    àý°j·Õ ã                   @  s¤  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dl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 d dlmZmZmZmZ d d	lmZ ejd
kr[d dlmZ n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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l0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 g d¢Z9e'j:Z:de;d< e'j<Z<de;d< e'j=Z=de;d< e'j>Z>de;d< e'j?Z?de;d< e'j@Z@de;d< e<ZAe=ZBe>ZCe?ZDe@ZEe'jFZGe'jHZIdZJd ZKd!ZLd"ZMd#ZNd$ZOd%ZPd&ZQd'ZRd(ZSe'jTZTde;d)< e'jUZUde;d*< e'jVZVde;d+< e'jWZWde;d,< e'jXZXde;d-< e'jYZZde;d.< e'j[Z\de;d/< e'j]Z^de;d0< e'j_Z`de;d1< e'jaZbde;d2< e'jcZdde;d3< e'jeZfde;d4< e'jgZhde;d5< e'jiZjde;d6< e'jkZlde;d7< e'jmZnde;d8< e'joZpde;d9< e'jqZrde;d:< e'jsZtde;d;< e'juZvde;d<< e'jwZxde;d=< e'jyZzde;d>< e'j{Z|de;d?< e'j}Z~de;d@< e'jZ€de;dA< e'j�Z‚de;dB< e'jƒZ„de;dC< e'j…Z†e'j‡Zˆde;dD< e'j‰ZŠde;dE< e'j‹ZŒde;dF< e'j�ZŽde;dG< e'j�Z�de;dH< ze'j‘Z’dIe;dJ< e9 “dJ¡ W n e”�y    dZ’Y nw e'j•Z–de;dK< ze'j—Z˜de;dL< e9 “dL¡ W n
 e”�y    Y nw ze'j™Zšde;dM< e9 “dM¡ W n
 e”�y9   Y nw ze'j›Zœde;dN< e9 “dN¡ W n
 e”�yR   Y nw e'j�Zžde;dO< e'jŸZ de;dP< e'j¡Z¢de;dQ< e'j£Z¤de;dR< e'j¥Z¦de;dS< e'j§Z¨de;dT< e'j©Zªde;dU< e'j«Z¬de;dV< e'j­Z®de;dW< e'j¯Z°de;dX< e'j±Z²de;dY< e'j³Z´de;dZ< e'jµZ¶de;d[< e'j·Z·de;d\< e'j¸Z¸de;d]< e'j¹Z¹de;d^< e'jºZºde;d_< e'j»Z»de;d`< e'j¼Z¼de;da< e'j½Z½de;db< e'j¾Z¾de;dc< e'j¿Z¿de;dd< e'jÀZÀde;de< e'jÁZÁde;df< e'jÂZÂde;dg< e'jÃZÃde;dh< e'jÄZÄde;di< e'jÅZÅde;dj< e'jÆZÆde;dk< ejÇeÈeÉeÊf ZËedlƒZÌG dmdn„ dnƒZÍeÍƒ ZÎedoejÏeÈ gejÇeÈeÍf f ZÐedogeÈf ZÑedoeÈgeÒf ZÓedoeÈeeÌ geÒf ZÔedoeeÌ geÈf ZÕeeÖeÒeeÌ geÈf Z×edoe2eÖeÖeÖgeÒf ZØG dpdq„ dqƒZÙg dr¢ZÚdsgZÛdtZÜduZÝG dvdw„ dweÞƒZßee#eßƒZàe)eßƒZáG dxdy„ dyeßƒZâG dzd{„ d{eßƒZãG d|d}„ d}eßƒZäG d~d„ deßƒZåG d€d�„ d�eßƒZæG d‚dƒ„ dƒƒZçG d„d…„ d…eçƒZèG d†d‡„ d‡eçƒZéG dˆd‰„ d‰eçƒZêG dŠd‹„ d‹eçƒZëG dŒd�„ d�eçƒZìG dŽd�„ d�eçƒZíd°d“d”„Zîd±d—d˜„ZïeïZðd²d�dž„Zñeñeòe'dŸd ƒd ƒZóeñeòe'd¡d ƒd¢ƒZôeñeòe'd£d ƒd¤ƒZõG d¥d¦„ d¦ƒZöed§ed¨ef d©�Z÷d³d«d¬„ZøG d­d®„ d®ƒZùG d¯do„ doƒZúdS )´é    )ÚannotationsN)ÚSequence)Ú	errorcode)ÚpartialÚwraps)ÚchainÚcount)Úplatform)ÚAnyÚCallableÚOptionalÚTypeVar)ÚWeakValueDictionary)é   é   )Ú
deprecated)Úx509)Úec)ÚStrOrBytesPath)Úexception_from_error_queue)Úffi)Úlib)Úmake_assert)Úno_zero_allocator)Ú
path_bytes)Útext_to_bytes_and_warn)ÚFILETYPE_PEMÚX509ÚPKeyÚX509NameÚ	X509StoreÚ_EllipticCurveÚ_PassphraseHelperÚ_PrivateKey)bÚDTLS_CLIENT_METHODÚDTLS_METHODÚDTLS_SERVER_METHODÚMODE_RELEASE_BUFFERSÚNO_OVERLAPPING_PROTOCOLSÚOPENSSL_BUILT_ONÚOPENSSL_CFLAGSÚOPENSSL_DIRÚOPENSSL_PLATFORMÚOPENSSL_VERSIONÚOPENSSL_VERSION_NUMBERÚOP_ALLÚOP_CIPHER_SERVER_PREFERENCEÚOP_DONT_INSERT_EMPTY_FRAGMENTSÚOP_EPHEMERAL_RSAÚOP_MICROSOFT_BIG_SSLV3_BUFFERÚOP_MICROSOFT_SESS_ID_BUGÚOP_MSIE_SSLV2_RSA_PADDINGÚOP_NETSCAPE_CA_DN_BUGÚOP_NETSCAPE_CHALLENGE_BUGÚ"OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUGÚ#OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUGÚOP_NO_COMPRESSIONÚOP_NO_QUERY_MTUÚOP_NO_TICKETÚOP_PKCS1_CHECK_1ÚOP_PKCS1_CHECK_2ÚOP_SINGLE_DH_USEÚOP_SINGLE_ECDH_USEÚOP_SSLEAY_080_CLIENT_DH_BUGÚOP_SSLREF2_REUSE_CERT_TYPE_BUGÚOP_TLS_BLOCK_PADDING_BUGÚOP_TLS_D5_BUGÚOP_TLS_ROLLBACK_BUGÚRECEIVED_SHUTDOWNÚSENT_SHUTDOWNÚSESS_CACHE_BOTHÚSESS_CACHE_CLIENTÚSESS_CACHE_NO_AUTO_CLEARÚSESS_CACHE_NO_INTERNALÚSESS_CACHE_NO_INTERNAL_LOOKUPÚSESS_CACHE_NO_INTERNAL_STOREÚSESS_CACHE_OFFÚSESS_CACHE_SERVERÚSSL3_VERSIONÚSSLEAY_BUILT_ONÚSSLEAY_CFLAGSÚ
SSLEAY_DIRÚSSLEAY_PLATFORMÚSSLEAY_VERSIONÚSSL_CB_ACCEPT_EXITÚSSL_CB_ACCEPT_LOOPÚSSL_CB_ALERTÚSSL_CB_CONNECT_EXITÚSSL_CB_CONNECT_LOOPÚSSL_CB_EXITÚSSL_CB_HANDSHAKE_DONEÚSSL_CB_HANDSHAKE_STARTÚSSL_CB_LOOPÚSSL_CB_READÚSSL_CB_READ_ALERTÚSSL_CB_WRITEÚSSL_CB_WRITE_ALERTÚSSL_ST_ACCEPTÚSSL_ST_CONNECTÚSSL_ST_MASKÚTLS1_1_VERSIONÚTLS1_2_VERSIONÚTLS1_3_VERSIONÚTLS1_VERSIONÚTLS_CLIENT_METHODÚ
TLS_METHODÚTLS_SERVER_METHODÚVERIFY_CLIENT_ONCEÚVERIFY_FAIL_IF_NO_PEER_CERTÚVERIFY_NONEÚVERIFY_PEERÚ
ConnectionÚContextÚErrorÚOP_NO_SSLv2ÚOP_NO_SSLv3ÚOP_NO_TLSv1ÚOP_NO_TLSv1_1ÚOP_NO_TLSv1_2ÚOP_NO_TLSv1_3ÚSSLeay_versionÚSSLv23_METHODÚSessionÚSysCallErrorÚTLSv1_1_METHODÚTLSv1_2_METHODÚTLSv1_METHODÚWantReadErrorÚWantWriteErrorÚWantX509LookupErrorÚX509VerificationCodesÚZeroReturnErrorÚintr.   r-   r*   r,   r+   r)   r   é   é   é   é   é   é	   é
   é   é   rP   ri   rf   rg   rh   rt   ru   rv   rw   rx   ry   r'   r?   r@   r2   r4   r7   r9   rB   r3   r5   rA   rD   rC   r1   r0   rE   r>   r6   r8   r:   r;   ú
int | NoneÚOP_COOKIE_EXCHANGEr<   ÚOP_NO_RENEGOTIATIONÚOP_IGNORE_UNEXPECTED_EOFÚOP_LEGACY_SERVER_CONNECTr/   rp   rn   rm   ro   rN   rI   rO   rH   rJ   rL   rM   rK   rd   rc   re   r^   r[   r_   ra   rX   r`   rb   rW   rV   rZ   rY   r]   r\   Ú_Tc                   @  ó   e Zd ZdS )Ú_NoOverlappingProtocolsN©Ú__name__Ú
__module__Ú__qualname__© rœ   rœ   ú~/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/OpenSSL/SSL.pyr—   $  ó    r—   rq   c                   @  sf  e Zd ZdZejZejZej	Z
ejZejZejZejZejZejZejZejZejZejZejZ ej!Z"ej#Z$ej%Z&ej'Z(ej)Z*ej+Z,ej-Z.ej/Z0ej1Z2ej3Z4ej5Z6ej7Z8ej9Z:ej;Z<ej=Z>ej?Z@ejAZBejCZDejEZFejGZHejIZJejKZLejMZNejOZPejQZRejSZTejUZVejWZXejYZZej[Z\ej]Z^ej_Z`ejaZbejcZdejeZfejgZhejiZjejkZlejmZnejoZpejqZrejsZtejuZvdS )r„   a\  
    Success and error codes for X509 verification, as returned by the
    underlying ``X509_STORE_CTX_get_error()`` function and passed by pyOpenSSL
    to verification callback functions.

    See `OpenSSL Verification Errors
    <https://www.openssl.org/docs/manmaster/man3/X509_verify_cert_error_string.html#ERROR-CODES>`_
    for details.
    N)wr™   rš   r›   Ú__doc__Ú_libÚ	X509_V_OKÚOKÚ$X509_V_ERR_UNABLE_TO_GET_ISSUER_CERTÚERR_UNABLE_TO_GET_ISSUER_CERTÚX509_V_ERR_UNABLE_TO_GET_CRLÚERR_UNABLE_TO_GET_CRLÚ+X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATUREÚ$ERR_UNABLE_TO_DECRYPT_CERT_SIGNATUREÚ*X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATUREÚ#ERR_UNABLE_TO_DECRYPT_CRL_SIGNATUREÚ-X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEYÚ&ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEYÚ!X509_V_ERR_CERT_SIGNATURE_FAILUREÚERR_CERT_SIGNATURE_FAILUREÚ X509_V_ERR_CRL_SIGNATURE_FAILUREÚERR_CRL_SIGNATURE_FAILUREÚX509_V_ERR_CERT_NOT_YET_VALIDÚERR_CERT_NOT_YET_VALIDÚX509_V_ERR_CERT_HAS_EXPIREDÚERR_CERT_HAS_EXPIREDÚX509_V_ERR_CRL_NOT_YET_VALIDÚERR_CRL_NOT_YET_VALIDÚX509_V_ERR_CRL_HAS_EXPIREDÚERR_CRL_HAS_EXPIREDÚ)X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELDÚ"ERR_ERROR_IN_CERT_NOT_BEFORE_FIELDÚ(X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELDÚ!ERR_ERROR_IN_CERT_NOT_AFTER_FIELDÚ)X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELDÚ"ERR_ERROR_IN_CRL_LAST_UPDATE_FIELDÚ)X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELDÚ"ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELDÚX509_V_ERR_OUT_OF_MEMÚERR_OUT_OF_MEMÚ&X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERTÚERR_DEPTH_ZERO_SELF_SIGNED_CERTÚ$X509_V_ERR_SELF_SIGNED_CERT_IN_CHAINÚERR_SELF_SIGNED_CERT_IN_CHAINÚ,X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLYÚ%ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLYÚ*X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATUREÚ#ERR_UNABLE_TO_VERIFY_LEAF_SIGNATUREÚX509_V_ERR_CERT_CHAIN_TOO_LONGÚERR_CERT_CHAIN_TOO_LONGÚX509_V_ERR_CERT_REVOKEDÚERR_CERT_REVOKEDÚX509_V_ERR_INVALID_CAÚERR_INVALID_CAÚX509_V_ERR_PATH_LENGTH_EXCEEDEDÚERR_PATH_LENGTH_EXCEEDEDÚX509_V_ERR_INVALID_PURPOSEÚERR_INVALID_PURPOSEÚX509_V_ERR_CERT_UNTRUSTEDÚERR_CERT_UNTRUSTEDÚX509_V_ERR_CERT_REJECTEDÚERR_CERT_REJECTEDÚ"X509_V_ERR_SUBJECT_ISSUER_MISMATCHÚERR_SUBJECT_ISSUER_MISMATCHÚX509_V_ERR_AKID_SKID_MISMATCHÚERR_AKID_SKID_MISMATCHÚ&X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCHÚERR_AKID_ISSUER_SERIAL_MISMATCHÚX509_V_ERR_KEYUSAGE_NO_CERTSIGNÚERR_KEYUSAGE_NO_CERTSIGNÚ#X509_V_ERR_UNABLE_TO_GET_CRL_ISSUERÚERR_UNABLE_TO_GET_CRL_ISSUERÚ'X509_V_ERR_UNHANDLED_CRITICAL_EXTENSIONÚ ERR_UNHANDLED_CRITICAL_EXTENSIONÚX509_V_ERR_KEYUSAGE_NO_CRL_SIGNÚERR_KEYUSAGE_NO_CRL_SIGNÚ+X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSIONÚ$ERR_UNHANDLED_CRITICAL_CRL_EXTENSIONÚX509_V_ERR_INVALID_NON_CAÚERR_INVALID_NON_CAÚ%X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDEDÚERR_PROXY_PATH_LENGTH_EXCEEDEDÚ(X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATUREÚ!ERR_KEYUSAGE_NO_DIGITAL_SIGNATUREÚ)X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWEDÚ"ERR_PROXY_CERTIFICATES_NOT_ALLOWEDÚX509_V_ERR_INVALID_EXTENSIONÚERR_INVALID_EXTENSIONÚ#X509_V_ERR_INVALID_POLICY_EXTENSIONÚERR_INVALID_POLICY_EXTENSIONÚX509_V_ERR_NO_EXPLICIT_POLICYÚERR_NO_EXPLICIT_POLICYÚX509_V_ERR_DIFFERENT_CRL_SCOPEÚERR_DIFFERENT_CRL_SCOPEÚ(X509_V_ERR_UNSUPPORTED_EXTENSION_FEATUREÚ!ERR_UNSUPPORTED_EXTENSION_FEATUREÚX509_V_ERR_UNNESTED_RESOURCEÚERR_UNNESTED_RESOURCEÚX509_V_ERR_PERMITTED_VIOLATIONÚERR_PERMITTED_VIOLATIONÚX509_V_ERR_EXCLUDED_VIOLATIONÚERR_EXCLUDED_VIOLATIONÚX509_V_ERR_SUBTREE_MINMAXÚERR_SUBTREE_MINMAXÚ&X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPEÚERR_UNSUPPORTED_CONSTRAINT_TYPEÚ(X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAXÚ!ERR_UNSUPPORTED_CONSTRAINT_SYNTAXÚ"X509_V_ERR_UNSUPPORTED_NAME_SYNTAXÚERR_UNSUPPORTED_NAME_SYNTAXÚ$X509_V_ERR_CRL_PATH_VALIDATION_ERRORÚERR_CRL_PATH_VALIDATION_ERRORÚX509_V_ERR_HOSTNAME_MISMATCHÚERR_HOSTNAME_MISMATCHÚX509_V_ERR_EMAIL_MISMATCHÚERR_EMAIL_MISMATCHÚX509_V_ERR_IP_ADDRESS_MISMATCHÚERR_IP_ADDRESS_MISMATCHÚ#X509_V_ERR_APPLICATION_VERIFICATIONÚERR_APPLICATION_VERIFICATIONrœ   rœ   rœ   r�   r„   :  sš    
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
r„   )z"/etc/ssl/certs/ca-certificates.crtz /etc/pki/tls/certs/ca-bundle.crtz/etc/ssl/ca-bundle.pemz/etc/pki/tls/cacert.pemz1/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pemz/etc/ssl/certss$   /opt/pyca/cryptography/openssl/certss'   /opt/pyca/cryptography/openssl/cert.pemc                   @  s   e Zd ZdZdS )rs   z4
    An error occurred in an `OpenSSL.SSL` API.
    N)r™   rš   r›   rŸ   rœ   rœ   rœ   r�   rs   ·  s    rs   c                   @  r–   )r�   Nr˜   rœ   rœ   rœ   r�   r�   Á  rž   r�   c                   @  r–   )r‚   Nr˜   rœ   rœ   rœ   r�   r‚   Å  rž   r‚   c                   @  r–   )rƒ   Nr˜   rœ   rœ   rœ   r�   rƒ   É  rž   rƒ   c                   @  r–   )r…   Nr˜   rœ   rœ   rœ   r�   r…   Í  rž   r…   c                   @  r–   )r}   Nr˜   rœ   rœ   rœ   r�   r}   Ñ  rž   r}   c                   @  s$   e Zd ZdZd	dd„Zd	dd„ZdS )
Ú_CallbackExceptionHelperaÅ  
    A base class for wrapper classes that allow for intelligent exception
    handling in OpenSSL callbacks.

    :ivar list _problems: Any exceptions that occurred while executing in a
        context where they could not be raised in the normal way.  Typically
        this is because OpenSSL has called into some Python code and requires a
        return value.  The exceptions are saved to be raised later when it is
        possible to do so.
    ÚreturnÚNonec                 C  s
   g | _ d S ©N)Ú	_problems©Úselfrœ   rœ   r�   Ú__init__á  s   
z!_CallbackExceptionHelper.__init__c                 C  s4   | j rztƒ  W n	 ty   Y nw | j  d¡‚dS )z�
        Raise an exception from the OpenSSL error queue or that was previously
        captured whe running a callback.
        r   N)r  Ú_raise_current_errorrs   Úpopr  rœ   rœ   r�   Úraise_if_problemä  s   
ÿûz)_CallbackExceptionHelper.raise_if_problemN©r  r  )r™   rš   r›   rŸ   r  r  rœ   rœ   rœ   r�   r  Õ  s    
r  c                   @  ó   e Zd ZdZd	dd„ZdS )
Ú_VerifyHelperz^
    Wrap a callback such that it can be used as a certificate verification
    callback.
    ÚcallbackÚ_VerifyCallbackr  r  c                   ó2   t  ˆ¡ tˆ ƒ‡ ‡fdd„ƒ}t d|¡ˆ_d S )Nc              
     s²   t  |¡}t  |¡ t |¡}t  |¡}t  |¡}t  ¡ }t  ||¡}t	j
| }z
ˆ ||||| ƒ}	W n tyK }
 zˆj |
¡ W Y d }
~
dS d }
~
ww |	rWt  |t j¡ dS dS )Nr   é   )r    ÚX509_STORE_CTX_get_current_certÚX509_up_refr   Ú_from_raw_x509_ptrÚX509_STORE_CTX_get_errorÚX509_STORE_CTX_get_error_depthÚ"SSL_get_ex_data_X509_STORE_CTX_idxÚX509_STORE_CTX_get_ex_datarq   Ú_reverse_mappingÚ	Exceptionr  ÚappendÚX509_STORE_CTX_set_errorr¡   )ÚokÚ	store_ctxr   ÚcertÚerror_numberÚerror_depthÚindexÚsslÚ
connectionÚresultÚe©r!  r  rœ   r�   Úwrapperú  s*   






ÿ€þz'_VerifyHelper.__init__.<locals>.wrapperzint (*)(int, X509_STORE_CTX *)©r  r  r   Ú_ffir!  ©r  r!  r;  rœ   r:  r�   r  ÷  s   

ÿz_VerifyHelper.__init__N)r!  r"  r  r  ©r™   rš   r›   rŸ   r  rœ   rœ   rœ   r�   r   ñ  s    r   c                   @  r  )
Ú_ALPNSelectHelperzQ
    Wrap a callback such that it can be used as an ALPN selection callback.
    r!  Ú_ALPNSelectCallbackr  r  c                   r#  )Nc              
     s  zpt j|  }t ||¡d d … }g }|r/|d }	|d|	d … }
| |
¡ ||	d d … }|sˆ ||ƒ}d}|tu r?d}d}n	t|tƒsHtdƒ‚t 	dt
|ƒ¡t 	d|¡g|_|jd d |d< |jd |d< |smtjW S tjW S  ty‹ } zˆj |¡ tjW  Y d }~S d }~ww )	Nr   r$  Tó    Fz^ALPN callback must return a bytestring or the special NO_OVERLAPPING_PROTOCOLS sentinel value.zunsigned char *úunsigned char[])rq   r,  r=  Úbufferr.  r(   Ú
isinstanceÚbytesÚ	TypeErrorÚnewÚlenÚ_alpn_select_callback_argsr    ÚSSL_TLSEXT_ERR_NOACKÚSSL_TLSEXT_ERR_OKr-  r  ÚSSL_TLSEXT_ERR_ALERT_FATAL)r6  ÚoutÚoutlenÚin_ÚinlenÚargÚconnÚinstrÚ	protolistÚencoded_lenÚprotoÚoutbytesÚany_acceptedr9  r:  rœ   r�   r;  !  s@   

ü

ÿ	
þ€þz+_ALPNSelectHelper.__init__.<locals>.wrapperz^int (*)(SSL *, unsigned char **, unsigned char *, const unsigned char *, unsigned int, void *)r<  r>  rœ   r:  r�   r    s   
+
ûz_ALPNSelectHelper.__init__N©r!  rA  r  r  r?  rœ   rœ   rœ   r�   r@    s    r@  c                   @  r  )
Ú_OCSPServerCallbackHelperað  
    Wrap a callback such that it can be used as an OCSP callback for the server
    side.

    Annoyingly, OpenSSL defines one OCSP callback but uses it in two different
    ways. For servers, that callback is expected to retrieve some OCSP data and
    hand it to OpenSSL, and may return only SSL_TLSEXT_ERR_OK,
    SSL_TLSEXT_ERR_FATAL, and SSL_TLSEXT_ERR_NOACK. For clients, that callback
    is expected to check the OCSP data, and returns a negative value on error,
    0 if the response is not acceptable, or positive if it is. These are
    mutually exclusive return code behaviours, and they mean that we need two
    helpers so that we always return an appropriate error code if the user's
    code throws an exception.

    Given that we have to have two helpers anyway, these helpers are a bit more
    helpery than most: specifically, they hide a few more of the OpenSSL
    functions so that the user has an easier time writing these callbacks.

    This helper implements the server side.
    r!  ú_OCSPServerCallback[Any]r  r  c                   r#  )Nc              
     s¸   zBt j|  }|tjkrt |¡}nd }ˆ ||ƒ}t|tƒs!tdƒ‚|s&W dS t|ƒ}t	 
|¡}|t ||¡d d …< t	 | ||¡ W dS  ty[ } zˆj |¡ W Y d }~dS d }~ww )Nz'OCSP callback must return a bytestring.r   r   é   )rq   r,  r=  ÚNULLÚfrom_handlerE  rF  rG  rI  r    ÚOPENSSL_mallocrD  ÚSSL_set_tlsext_status_ocsp_respr-  r  r.  )r6  ÚcdatarS  ÚdataÚ	ocsp_dataÚocsp_data_lengthÚdata_ptrr9  r:  rœ   r�   r;  o  s,   




ÿ€þz3_OCSPServerCallbackHelper.__init__.<locals>.wrapperúint (*)(SSL *, void *)r<  r>  rœ   r:  r�   r  l  s   
&z"_OCSPServerCallbackHelper.__init__N)r!  r\  r  r  r?  rœ   rœ   rœ   r�   r[  V  ó    r[  c                   @  r  )
Ú_OCSPClientCallbackHelperað  
    Wrap a callback such that it can be used as an OCSP callback for the client
    side.

    Annoyingly, OpenSSL defines one OCSP callback but uses it in two different
    ways. For servers, that callback is expected to retrieve some OCSP data and
    hand it to OpenSSL, and may return only SSL_TLSEXT_ERR_OK,
    SSL_TLSEXT_ERR_FATAL, and SSL_TLSEXT_ERR_NOACK. For clients, that callback
    is expected to check the OCSP data, and returns a negative value on error,
    0 if the response is not acceptable, or positive if it is. These are
    mutually exclusive return code behaviours, and they mean that we need two
    helpers so that we always return an appropriate error code if the user's
    code throws an exception.

    Given that we have to have two helpers anyway, these helpers are a bit more
    helpery than most: specifically, they hide a few more of the OpenSSL
    functions so that the user has an easier time writing these callbacks.

    This helper implements the client side.
    r!  ú_OCSPClientCallback[Any]r  r  c                   r#  )Nc           	   
     s®   z=t j|  }|tjkrt |¡}nd }t d¡}t | |¡}|dk r%d}nt |d |¡d d … }ˆ |||ƒ}t	t
|ƒƒW S  tyV } zˆj |¡ W Y d }~dS d }~ww )Núunsigned char **r   rB  éÿÿÿÿ)rq   r,  r=  r^  r_  rH  r    ÚSSL_get_tlsext_status_ocsp_resprD  r†   Úboolr-  r  r.  )	r6  rb  rS  rc  Úocsp_ptrÚocsp_lenrd  Úvalidr9  r:  rœ   r�   r;  ²  s"   


€ýz3_OCSPClientCallbackHelper.__init__.<locals>.wrapperrg  r<  r>  rœ   r:  r�   r  ¯  s   
z"_OCSPClientCallbackHelper.__init__N)r!  rj  r  r  r?  rœ   rœ   rœ   r�   ri  ™  rh  ri  c                   @  ó   e Zd Zddd„ZdS )	Ú_CookieGenerateCallbackHelperr!  Ú_CookieGenerateCallbackr  r  c                   s@   t  ˆ¡ ttddƒ‰tˆ ƒ‡ ‡‡fdd„ƒ}t d|¡ˆ_d S )NÚDTLS1_COOKIE_LENGTHéÿ   c              
     sŽ   z-t j|  }ˆ |ƒ}t|ƒˆkrtdt|ƒ› dˆ› d�ƒ‚||dt|ƒ…< t|ƒ|d< W dS  tyF } zˆj |¡ W Y d }~dS d }~ww )NzCookie too long (got z bytes, max ú)r   r$  )rq   r,  rI  Ú
ValueErrorr-  r  r.  )r6  rN  rO  rS  Úcookier9  ©r!  Úmax_cookie_lenr  rœ   r�   r;  Ú  s"   
ÿÿ€ýz7_CookieGenerateCallbackHelper.__init__.<locals>.wrapperz/int (*)(SSL *, unsigned char *, unsigned int *))r  r  Úgetattrr    r   r=  r!  r>  rœ   rz  r�   r  Õ  s   

þz&_CookieGenerateCallbackHelper.__init__N©r!  rt  r  r  ©r™   rš   r›   r  rœ   rœ   rœ   r�   rs  Ô  ó    rs  c                   @  rr  )	Ú_CookieVerifyCallbackHelperr!  Ú_CookieVerifyCallbackr  r  c                   r#  )Nc              
     sV   zt j|  }ˆ |t|d|… ƒƒW S  ty* } zˆj |¡ W Y d }~dS d }~ww ©Nr   )rq   r,  rF  r-  r  r.  )r6  Úc_cookieÚ
cookie_lenrS  r9  r:  rœ   r�   r;  ö  s   
€þz5_CookieVerifyCallbackHelper.__init__.<locals>.wrapperz-int (*)(SSL *, unsigned char *, unsigned int)r<  r>  rœ   r:  r�   r  ó  s   

þz$_CookieVerifyCallbackHelper.__init__N©r!  r�  r  r  r~  rœ   rœ   rœ   r�   r€  ò  r  r€  Úobjr
   r  c                 C  sf   d }t | tƒst| dd ƒ}|d ur|ƒ } t | tƒr| }t |tƒs$tdƒ‚|dk r1td|d›d�ƒ‚|S )NÚfilenoz3argument must be an int, or have a fileno() method.r   z.file descriptor cannot be a negative integer (Úirw  )rE  r†   r|  rG  rx  )r†  ÚfdÚmethrœ   rœ   r�   Ú_asFileDescriptor  s   


ÿr‹  ÚtyperF  c                 C  s   t  t | ¡¡S )z”
    Return a string describing the version of OpenSSL in use.

    :param type: One of the :const:`OPENSSL_` constants defined in this module.
    )r=  Ústringr    ÚOpenSSL_version)rŒ  rœ   rœ   r�   rŽ    s   rŽ  ÚflagÚerrorÚstrúCallable[[_T], _T]c                   s   ‡ ‡fdd„}|S )a”  
    Builds a decorator that ensures that functions that rely on OpenSSL
    functions that are not present in this build raise NotImplementedError,
    rather than AttributeError coming out of cryptography.

    :param flag: A cryptography flag that guards the functions, e.g.
        ``Cryptography_HAS_NEXTPROTONEG``.
    :param error: The string to be used in the exception if the flag is false.
    c                   s    ˆst | ƒ‡ fdd„ƒ}|S | S )Nc                    s   t ˆ ƒ‚r  ©ÚNotImplementedError)ÚargsÚkwargs)r�  rœ   r�   Úexplode3  s   z<_make_requires.<locals>._requires_decorator.<locals>.explode)r   )Úfuncr—  ©r�  r�  rœ   r�   Ú_requires_decorator0  s
   z+_make_requires.<locals>._requires_decoratorrœ   )r�  r�  rš  rœ   r™  r�   Ú_make_requires%  s   r›  ÚCryptography_HAS_KEYLOGzKey logging not availableÚ$Cryptography_HAS_SSL_GET0_GROUP_NAMEzAGetting group name is not supported by the linked OpenSSL versionÚCryptography_HAS_SSL_COOKIEz$DTLS cookie support is not availablec                   @  s"   e Zd ZU dZded< ded< dS )r|   zÉ
    A class representing an SSL session.  A session defines certain connection
    parameters which may be re-used to speed up the setup of subsequent
    connections.

    .. versionadded:: 0.14
    r
   Ú_sessionrr   Ú_contextN)r™   rš   r›   rŸ   Ú__annotations__rœ   rœ   rœ   r�   r|   M  s   
 r|   ÚF.)ÚboundÚfc                   s"   t ˆ ƒd	‡ fdd„ƒ}t t|¡S )
Nr  rr   r•  r
   r–  r  c                   s$   | j rtdƒ‚ˆ | g|¢R i |¤ŽS )NzPContext has already been used to create a Connection, it cannot be mutated again)Ú_usedrx  ©r  r•  r–  ©r¤  rœ   r�   Úinnerb  s
   ÿz _require_not_used.<locals>.inner)r  rr   r•  r
   r–  r
   r  r
   )r   ÚtypingÚcastr¢  )r¤  r¨  rœ   r§  r�   Ú_require_not_useda  s   r«  c                   @  sB  e Zd ZU dZeejdfeejefe	eje
feejefeejdfeejdfeejdfeejdfeejdfeejdfi
Zded< d©d	d
„Zedªdd„ƒZedªdd„ƒZe	d«d¬dd„ƒZd­dd„Ze dƒe	d«d®dd„ƒƒZ!ed¯dd „ƒZ"d°d%d&„Z#d±d*d+„Z$ed²d.d/„ƒZ%ee&fd³d1d2„ƒZ'ed´d5d6„ƒZ(edµd8d9„ƒZ)d¯d:d;„Z*ee&fd¶d=d>„ƒZ+ed·dAdB„ƒZ,d¯dCdD„Z-ed¸dFdG„ƒZ.ed¹dIdJ„ƒZ/edºdLdM„ƒZ0d»dNdO„Z1e	d«d¼dQdR„ƒZ2ed½dTdU„ƒZ3d»dVdW„Z4d»dXdY„Z5ed¾d[d\„ƒZ6ed¿d_d`„ƒZ7edÀdbdc„ƒZ8edÁdedf„ƒZ9e dgƒedÂdjdk„ƒƒZ:edÃdmdn„ƒZ;edÄdpdq„ƒZ<d»drds„Z=edÅdudv„ƒZ>e?edÆdxdy„ƒƒZ@dÇd{d|„ZAedÈd~d„ƒZBdÉd�d‚„ZCedÊd„d…„ƒZDedºd†d‡„ƒZEedËd‰dŠ„ƒZFedÌdŒd�„ƒZGedÍd�d�„ƒZHedÎd“d”„ƒZIedÏd–d—„ƒZJdÐd›dœ„ZKe	d«dÑdždŸ„ƒZLe	d«dÒd¡d¢„ƒZMeeNdÓd¤d¥„ƒƒZOeeNdÔd§d¨„ƒƒZPdS )Õrr   au  
    :class:`OpenSSL.SSL.Context` instances define the parameters for setting
    up new SSL connections.

    :param method: One of TLS_METHOD, TLS_CLIENT_METHOD, TLS_SERVER_METHOD,
                   DTLS_METHOD, DTLS_CLIENT_METHOD, or DTLS_SERVER_METHOD.
                   SSLv23_METHOD, TLSv1_METHOD, etc. are deprecated and should
                   not be used.
    Nz@typing.ClassVar[dict[int, tuple[Callable[[], Any], int | None]]]Ú_methodsÚmethodr†   r  r  c                 C  s  t |tƒs	tdƒ‚z	| j| \}}W n ty   tdƒ‚w |ƒ }t|tjkƒ t	 
|¡}t|tjkƒ t |t	j¡}|| _d| _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _|  t	j t	j!B ¡ |d urŠ|  "|¡ |  #|¡ d S d S )Nzmethod must be an integerzNo such protocolF)$rE  r†   rG  r¬  ÚKeyErrorrx  Ú_openssl_assertr=  r^  r    ÚSSL_CTX_newÚgcÚSSL_CTX_freer   r¥  Ú_passphrase_helperÚ_passphrase_callbackÚ_passphrase_userdataÚ_verify_helperÚ_verify_callbackÚ_info_callbackÚ_keylog_callbackÚ_tlsext_servername_callbackÚ	_app_dataÚ_alpn_select_helperÚ_alpn_select_callbackÚ_ocsp_helperÚ_ocsp_callbackÚ
_ocsp_dataÚ_cookie_generate_helperÚ_cookie_verify_helperÚset_modeÚSSL_MODE_ENABLE_PARTIAL_WRITEÚ#SSL_MODE_ACCEPT_MOVING_WRITE_BUFFERÚset_min_proto_versionÚset_max_proto_version)r  r­  Úmethod_funcÚversionÚ
method_objÚcontextrœ   rœ   r�   r  ˆ  sT   
ÿ
þþÿÿÿ
þzContext.__init__rÉ  c                 C  ó   t t | j|¡dkƒ dS )aD  
        Set the minimum supported protocol version. Setting the minimum
        version to 0 will enable protocol versions down to the lowest version
        supported by the library.

        If the underlying OpenSSL build is missing support for the selected
        version, this method will raise an exception.
        r$  N)r¯  r    ÚSSL_CTX_set_min_proto_versionr   ©r  rÉ  rœ   rœ   r�   rÆ  ¹  ó   
ÿzContext.set_min_proto_versionc                 C  rÌ  )aC  
        Set the maximum supported protocol version. Setting the maximum
        version to 0 will enable protocol versions up to the highest version
        supported by the library.

        If the underlying OpenSSL build is missing support for the selected
        version, this method will raise an exception.
        r$  N)r¯  r    ÚSSL_CTX_set_max_proto_versionr   rÎ  rœ   rœ   r�   rÇ  Ç  rÏ  zContext.set_max_proto_versionÚcafileú_StrOrBytesPath | NoneÚcapathc                 C  sR   |du rt j}nt|ƒ}|du rt j}nt|ƒ}t | j||¡}|s'tƒ  dS dS )aM  
        Let SSL know where we can find trusted certificates for the certificate
        chain.  Note that the certificates have to be in PEM format.

        If capath is passed, it must be a directory prepared using the
        ``c_rehash`` tool included with OpenSSL.  Either, but not both, of
        *pemfile* or *capath* may be :data:`None`.

        :param cafile: In which file we can find the certificates (``bytes`` or
            ``str``).
        :param capath: In which directory we can find the certificates
            (``bytes`` or ``str``).

        :return: None
        N)r=  r^  Ú_path_bytesr    ÚSSL_CTX_load_verify_locationsr   r  )r  rÑ  rÓ  Úload_resultrœ   rœ   r�   Úload_verify_locationsÕ  s   ÿ
ÿzContext.load_verify_locationsr!  ú_PassphraseCallback[_T]r"   c                   s(   t ˆ ƒd‡ ‡fd	d
„ƒ}tt|ddd�S )NÚsizer†   Úverifyrn  Úuserdatar
   r  rF  c                   s   ˆ | |ˆj ƒS r  )rµ  )rÙ  rÚ  rÛ  r:  rœ   r�   r;  ý  s   z'Context._wrap_callback.<locals>.wrapperT)Ú	more_argsÚtruncate)rÙ  r†   rÚ  rn  rÛ  r
   r  rF  )r   r"   r   r>  rœ   r:  r�   Ú_wrap_callbackú  s
   ÿzContext._wrap_callbackz®Context.set_passwd_cb is deprecated. You should decrypt and load your private key yourself, with cryptography's key loading APIs, and then use Context.use_privatekey instead.rÛ  ú	_T | Nonec                 C  s@   t |ƒstdƒ‚|  |¡| _| jj| _t | j| j¡ || _	dS )aò  
        Set the passphrase callback.  This function will be called
        when a private key with a passphrase is loaded.

        :param callback: The Python callback to use.  This must accept three
            positional arguments.  First, an integer giving the maximum length
            of the passphrase it may return.  If the returned passphrase is
            longer than this, it will be truncated.  Second, a boolean value
            which will be true if the user should be prompted for the
            passphrase twice and the callback should verify that the two values
            supplied are equal. Third, the value given as the *userdata*
            parameter to :meth:`set_passwd_cb`.  The *callback* must return
            a byte string. If an error occurs, *callback* should return a false
            value (e.g. an empty string).
        :param userdata: (optional) A Python object which will be given as
                         argument to the callback
        :return: None
        úcallback must be callableN)
ÚcallablerG  rÞ  r³  r!  r´  r    ÚSSL_CTX_set_default_passwd_cbr   rµ  )r  r!  rÛ  rœ   rœ   r�   Úset_passwd_cb  s   
ÿ
zContext.set_passwd_cbc                 C  sl   t  | j¡}t|dkƒ |  dd¡s0t t  ¡ ¡}t t  ¡ ¡}|t	kr2|t
kr4|  tt¡ dS dS dS dS )aÕ  
        Specify that the platform provided CA certificates are to be used for
        verification purposes. This method has some caveats related to the
        binary wheels that cryptography (pyOpenSSL's primary dependency) ships:

        *   macOS will only load certificates using this method if the user has
            the ``openssl@3`` `Homebrew <https://brew.sh>`_ formula installed
            in the default location.
        *   Windows will not work.
        *   manylinux cryptography wheels will work on most common Linux
            distributions in pyOpenSSL 17.1.0 and above.  pyOpenSSL detects the
            manylinux wheel and attempts to load roots via a fallback path.

        :return: None
        r$  ÚSSL_CERT_DIRÚSSL_CERT_FILEN)r    Ú SSL_CTX_set_default_verify_pathsr   r¯  Ú_check_env_vars_setr=  r�  ÚX509_get_default_cert_dirÚX509_get_default_cert_fileÚ_CRYPTOGRAPHY_MANYLINUX_CA_DIRÚ_CRYPTOGRAPHY_MANYLINUX_CA_FILEÚ_fallback_default_verify_pathsÚ_CERTIFICATE_FILE_LOCATIONSÚ_CERTIFICATE_PATH_LOCATIONS)r  Ú
set_resultÚdefault_dirÚdefault_filerœ   rœ   r�   Úset_default_verify_paths,  s   ÿõz Context.set_default_verify_pathsÚdir_env_varr‘  Úfile_env_varrn  c                 C  s    t j |¡dupt j |¡duS )zp
        Check to see if the default cert dir/file environment vars are present.

        :return: bool
        N)ÚosÚenvironÚget)r  ró  rô  rœ   rœ   r�   rç  W  s   þzContext._check_env_vars_setÚ	file_pathú	list[str]Údir_pathc                 C  sP   |D ]}t j |¡r|  |¡  nq|D ]}t j |¡r%|  d|¡  dS qdS )aV  
        Default verify paths are based on the compiled version of OpenSSL.
        However, when pyca/cryptography is compiled as a manylinux wheel
        that compiled location can potentially be wrong. So, like Go, we
        will try a predefined set of paths and attempt to load roots
        from there.

        :return: None
        N)rõ  ÚpathÚisfiler×  Úisdir)r  rø  rú  rÑ  rÓ  rœ   rœ   r�   rì  b  s   
þþÿz&Context._fallback_default_verify_pathsÚcertfileÚ_StrOrBytesPathc                 C  s(   t |ƒ}t | j|¡}|stƒ  dS dS )zÉ
        Load a certificate chain from a file.

        :param certfile: The name of the certificate chain file (``bytes`` or
            ``str``).  Must be PEM encoded.

        :return: None
        N)rÔ  r    Ú"SSL_CTX_use_certificate_chain_filer   r  )r  rþ  r8  rœ   rœ   r�   Úuse_certificate_chain_filex  s   
ÿ
ÿz"Context.use_certificate_chain_fileÚfiletypec                 C  s<   t |ƒ}t|tƒstdƒ‚t | j||¡}|stƒ  dS dS )ad  
        Load a certificate from a file

        :param certfile: The name of the certificate file (``bytes`` or
            ``str``).
        :param filetype: (optional) The encoding of the file, which is either
            :const:`FILETYPE_PEM` or :const:`FILETYPE_ASN1`.  The default is
            :const:`FILETYPE_PEM`.

        :return: None
        úfiletype must be an integerN)rÔ  rE  r†   rG  r    ÚSSL_CTX_use_certificate_filer   r  )r  rþ  r  Ú
use_resultrœ   rœ   r�   Úuse_certificate_fileŠ  s   
ÿ
ÿzContext.use_certificate_filer2  úX509 | x509.Certificatec                 C  óH   t |tƒst |¡}ntjdtdd� t | j|j	¡}|s"t
ƒ  dS dS ©zs
        Load a certificate from a X509 object

        :param cert: The X509 object
        :return: None
        úePassing pyOpenSSL X509 objects is deprecated. You should use a cryptography.x509.Certificate instead.r]  ©Ú
stacklevelN)rE  r   Úfrom_cryptographyÚwarningsÚwarnÚDeprecationWarningr    ÚSSL_CTX_use_certificater   Ú_x509r  ©r  r2  r  rœ   rœ   r�   Úuse_certificate£  s   
	ú	
ÿzContext.use_certificateÚcertobjc                 C  s\   t |tƒst |¡}ntjdtdd� t |j¡}t 	| j
|¡}|s,t |¡ tƒ  dS dS )z‰
        Add certificate to chain

        :param certobj: The X509 certificate object to add to the chain
        :return: None
        r
  r]  r  N)rE  r   r  r  r  r  r    ÚX509_dupr  ÚSSL_CTX_add_extra_chain_certr   Ú	X509_freer  )r  r  ÚcopyÚ
add_resultrœ   rœ   r�   Úadd_extra_chain_cert¼  s   
ú	

ýzContext.add_extra_chain_certc                 C  s    | j d ur| j  t¡ tƒ  d S r  )r³  r  rs   r  r  rœ   rœ   r�   Ú_raise_passphrase_exception×  s   

z#Context._raise_passphrase_exceptionÚkeyfilec                 C  s>   t |ƒ}t|tƒstdƒ‚t | j||¡}|s|  ¡  dS dS )aN  
        Load a private key from a file

        :param keyfile: The name of the key file (``bytes`` or ``str``)
        :param filetype: (optional) The encoding of the file, which is either
            :const:`FILETYPE_PEM` or :const:`FILETYPE_ASN1`.  The default is
            :const:`FILETYPE_PEM`.

        :return: None
        r  N)rÔ  rE  r†   rG  r    ÚSSL_CTX_use_PrivateKey_filer   r  )r  r  r  r  rœ   rœ   r�   Úuse_privatekey_fileÝ  s   
ÿÿzContext.use_privatekey_fileÚpkeyú_PrivateKey | PKeyc                 C  sJ   t |tƒst |¡}ntjdtdd� t | j|j	¡}|s#|  
¡  dS dS ©zs
        Load a private key from a PKey object

        :param pkey: The PKey object
        :return: None
        z`Passing pyOpenSSL PKey objects is deprecated. You should use a cryptography private key instead.r]  r  N)rE  r   Úfrom_cryptography_keyr  r  r  r    ÚSSL_CTX_use_PrivateKeyr   Ú_pkeyr  ©r  r   r  rœ   rœ   r�   Úuse_privatekeyö  s   
	ú	ÿzContext.use_privatekeyc                 C  s   t  | j¡stƒ  dS dS )zß
        Check if the private key (loaded with :meth:`use_privatekey`) matches
        the certificate (loaded with :meth:`use_certificate`)

        :return: :data:`None` (raises :exc:`Error` if something's wrong)
        N)r    ÚSSL_CTX_check_private_keyr   r  r  rœ   rœ   r�   Úcheck_privatekey  s   
ÿzContext.check_privatekeyrF  c                 C  s0   t  td|ƒ¡}t|tjkƒ t  | j|¡ dS )a%  
        Load the trusted certificates that will be sent to the client.  Does
        not actually imply any of the certificates are trusted; that must be
        configured separately.

        :param bytes cafile: The path to a certificates file in PEM format.
        :return: None
        rÑ  N)r    ÚSSL_load_client_CA_fileÚ_text_to_bytes_and_warnr¯  r=  r^  ÚSSL_CTX_set_client_CA_listr   )r  rÑ  Úca_listrœ   rœ   r�   Úload_client_ca  s
   
ÿzContext.load_client_caÚbufc                 C  s*   t d|ƒ}tt | j|t|ƒ¡dkƒ dS )aV  
        Set the session id to *buf* within which a session can be reused for
        this Context object.  This is needed when doing session resumption,
        because there is no way for a stored session to know which Context
        object it is associated with.

        :param bytes buf: The session id.

        :returns: None
        r/  r$  N)r+  r¯  r    ÚSSL_CTX_set_session_id_contextr   rI  )r  r/  rœ   rœ   r�   Úset_session_id)  s   
ÿÿzContext.set_session_idÚmodec                 C  ó    t |tƒs	tdƒ‚t | j|¡S )aŽ  
        Set the behavior of the session cache used by all connections using
        this Context.  The previously set mode is returned.  See
        :const:`SESS_CACHE_*` for details about particular modes.

        :param mode: One or more of the SESS_CACHE_* flags (combine using
            bitwise or)
        :returns: The previously set caching mode.

        .. versionadded:: 0.14
        úmode must be an integer)rE  r†   rG  r    ÚSSL_CTX_set_session_cache_moder   ©r  r2  rœ   rœ   r�   Úset_session_cache_mode;  s   
zContext.set_session_cache_modec                 C  ó   t  | j¡S )z‡
        Get the current session cache mode.

        :returns: The currently used cache mode.

        .. versionadded:: 0.14
        )r    ÚSSL_CTX_get_session_cache_moder   r  rœ   rœ   r�   Úget_session_cache_modeM  s   zContext.get_session_cache_modeú_VerifyCallback | Nonec                 C  óv   t |tƒs	tdƒ‚|du rd| _d| _t | j|tj	¡ dS t
|ƒs&tdƒ‚t|ƒ| _| jj| _t | j|| j¡ dS )aÚ  
        Set the verification flags for this Context object to *mode* and
        specify that *callback* should be used for verification callbacks.

        :param mode: The verify mode, this should be one of
            :const:`VERIFY_NONE` and :const:`VERIFY_PEER`. If
            :const:`VERIFY_PEER` is used, *mode* can be OR:ed with
            :const:`VERIFY_FAIL_IF_NO_PEER_CERT` and
            :const:`VERIFY_CLIENT_ONCE` to further control the behaviour.
        :param callback: The optional Python verification callback to use.
            This should take five arguments: A Connection object, an X509
            object, and three integer variables, which are in turn potential
            error number, error depth and return code. *callback* should
            return True if verification passes and False otherwise.
            If omitted, OpenSSL's default verification is used.
        :return: None

        See SSL_CTX_set_verify(3SSL) for further details.
        r4  Nrà  )rE  r†   rG  r¶  r·  r    ÚSSL_CTX_set_verifyr   r=  r^  rá  r   r!  ©r  r2  r!  rœ   rœ   r�   Ú
set_verifyW  s   


zContext.set_verifyÚdepthc                 C  ó$   t |tƒs	tdƒ‚t | j|¡ dS )zÙ
        Set the maximum depth for the certificate chain verification that shall
        be allowed for this Context object.

        :param depth: An integer specifying the verify depth
        :return: None
        zdepth must be an integerN)rE  r†   rG  r    ÚSSL_CTX_set_verify_depthr   )r  r@  rœ   rœ   r�   Úset_verify_depth}  s   
	zContext.set_verify_depthc                 C  r8  )z„
        Retrieve the Context object's verify mode, as set by
        :meth:`set_verify`.

        :return: The verify mode
        )r    ÚSSL_CTX_get_verify_moder   r  rœ   rœ   r�   Úget_verify_mode‹  ó   zContext.get_verify_modec                 C  r8  )zŒ
        Retrieve the Context object's verify depth, as set by
        :meth:`set_verify_depth`.

        :return: The verify depth
        )r    ÚSSL_CTX_get_verify_depthr   r  rœ   rœ   r�   Úget_verify_depth”  rF  zContext.get_verify_depthÚdhfilec                 C  st   t |ƒ}t |d¡}|tjkrtƒ  t |tj¡}t |tjtjtj¡}t |tj	¡}t 
| j|¡}t|dkƒ dS )z¶
        Load parameters for Ephemeral Diffie-Hellman

        :param dhfile: The file to load EDH parameters from (``bytes`` or
            ``str``).

        :return: None
        ó   rr$  N)rÔ  r    ÚBIO_new_filer=  r^  r  r±  ÚBIO_freeÚPEM_read_bio_DHparamsÚDH_freeÚSSL_CTX_set_tmp_dhr   r¯  )r  rI  ÚbioÚdhÚresrœ   rœ   r�   Úload_tmp_dh�  s   

zContext.load_tmp_dhÚcurveú!_EllipticCurve | ec.EllipticCurvec                 C  s¦   t |tƒrtjdtdd� t | j| ¡ ¡ dS |j	}|dkr"d}n|dkr(d}t 
| ¡ ¡}|tjkr7tƒ  t |¡}t|tjkƒ t |tj¡}t | j|¡ dS )	aš  
        Select a curve to use for ECDHE key exchange.

        :param curve: A curve instance from cryptography
            (:class:`~cryptogragraphy.hazmat.primitives.asymmetric.ec.EllipticCurve`).
            Alternatively (deprecated) a curve object from either
            :meth:`OpenSSL.crypto.get_elliptic_curve` or
            :meth:`OpenSSL.crypto.get_elliptic_curves`.

        :return: None
        z|Passing pyOpenSSL elliptic curves to set_tmp_ecdh is deprecated. You should use cryptography's elliptic curve types instead.r]  r  Ú	secp192r1Ú
prime192v1Ú	secp256r1Ú
prime256v1N)rE  r!   r  r  r  r    ÚSSL_CTX_set_tmp_ecdhr   Ú
_to_EC_KEYÚnameÚOBJ_txt2nidÚencodeÚ	NID_undefr  ÚEC_KEY_new_by_curve_namer¯  r=  r^  r±  ÚEC_KEY_free)r  rT  r\  Únidr   rœ   rœ   r�   Úset_tmp_ecdh³  s&   
ù	

zContext.set_tmp_ecdhÚcipher_listc                 C  s6   t d|ƒ}t|tƒstdƒ‚tt | j|¡dkƒ dS )ak  
        Set the list of ciphers to be used in this context.

        See the OpenSSL manual for more information (e.g.
        :manpage:`ciphers(1)`).

        Note this API does not change the cipher suites used in TLS 1.3
        Use `set_tls13_ciphersuites` for that.

        :param bytes cipher_list: An OpenSSL cipher string.
        :return: None
        rd  z"cipher_list must be a byte string.r$  N)r+  rE  rF  rG  r¯  r    ÚSSL_CTX_set_cipher_listr   )r  rd  rœ   rœ   r�   Úset_cipher_listÛ  s   

ÿzContext.set_cipher_listÚciphersuitesc                 C  ó,   t |tƒs	tdƒ‚tt | j|¡dkƒ dS )a´  
        Set the list of TLS 1.3 ciphers to be used in this context.
        OpenSSL maintains a separate list of TLS 1.3+ ciphers to
        ciphers for TLS 1.2 and lowers.

        See the OpenSSL manual for more information (e.g.
        :manpage:`ciphers(1)`).

        :param bytes ciphersuites: An OpenSSL cipher string containing
            TLS 1.3+ ciphersuites.
        :return: None

        .. versionadded:: 25.2.0
        z#ciphersuites must be a byte string.r$  N)rE  rF  rG  r¯  r    ÚSSL_CTX_set_ciphersuitesr   )r  rg  rœ   rœ   r�   Úset_tls13_ciphersuitesò  s
   
ÿzContext.set_tls13_ciphersuiteszVContext.set_client_ca_list is deprecated. X509Name support in pyOpenSSL is deprecated.Úcertificate_authoritiesúSequence[X509Name]c                 C  s¬   t  ¡ }t|tjkƒ z4|D ]/}t|tƒs tdt|ƒj	› d�ƒ‚t  
|j¡}t|tjkƒ t  ||¡}|s=t  |¡ tƒ  qW n tyL   t  |¡ ‚ w t  | j|¡ dS )a_  
        Set the list of preferred client certificate signers for this server
        context.

        This list of certificate authorities will be sent to the client when
        the server requests a client certificate.

        :param certificate_authorities: a sequence of X509Names.
        :return: None

        .. versionadded:: 0.10
        z)client CAs must be X509Name objects, not z objectsN)r    Úsk_X509_NAME_new_nullr¯  r=  r^  rE  r   rG  rŒ  r™   ÚX509_NAME_dupÚ_nameÚsk_X509_NAME_pushÚX509_NAME_freer  r-  Úsk_X509_NAME_freer,  r   )r  rk  Ú
name_stackÚca_namer  Úpush_resultrœ   rœ   r�   Úset_client_ca_list	  s.   
ÿÿ
€õ
þzContext.set_client_ca_listÚcertificate_authorityc                 C  sF   t |tƒst |¡}ntjdtdd� t | j|j	¡}t
|dkƒ dS )ai  
        Add the CA certificate to the list of preferred signers for this
        context.

        The list of certificate authorities will be sent to the client when the
        server requests a client certificate.

        :param certificate_authority: certificate authority's X509 certificate.
        :return: None

        .. versionadded:: 0.10
        r
  r]  r  r$  N)rE  r   r  r  r  r  r    ÚSSL_CTX_add_client_CAr   r  r¯  )r  rw  r  rœ   rœ   r�   Úadd_client_ca3  s   
ÿú	ÿzContext.add_client_caÚtimeoutc                 C  r3  )aQ  
        Set the timeout for newly created sessions for this Context object to
        *timeout*.  The default value is 300 seconds. See the OpenSSL manual
        for more information (e.g. :manpage:`SSL_CTX_set_timeout(3)`).

        :param timeout: The timeout in (whole) seconds
        :return: The previous session timeout
        ztimeout must be an integer)rE  r†   rG  r    ÚSSL_CTX_set_timeoutr   )r  rz  rœ   rœ   r�   Úset_timeoutV  s   

zContext.set_timeoutc                 C  r8  )z”
        Retrieve session timeout, as set by :meth:`set_timeout`. The default
        is 300 seconds.

        :return: The session timeout
        )r    ÚSSL_CTX_get_timeoutr   r  rœ   rœ   r�   Úget_timeoute  rF  zContext.get_timeoutú&Callable[[Connection, int, int], None]c                   ó6   t ˆ ƒ‡ fdd„ƒ}t d|¡| _t | j| j¡ dS )áð  
        Set the information callback to *callback*. This function will be
        called from time to time during SSL handshakes.

        :param callback: The Python callback to use.  This should take three
            arguments: a Connection object and two integers.  The first integer
            specifies where in the SSL handshake the function was called, and
            the other the return code from a (possibly failed) internal
            function call.
        :return: None
        c                   ó   ˆ t j|  ||ƒ d S r  ©rq   r,  ©r6  ÚwhereÚreturn_code©r!  rœ   r�   r;  ~  ó   z*Context.set_info_callback.<locals>.wrapperúvoid (*)(const SSL *, int, int)N)r   r=  r!  r¸  r    ÚSSL_CTX_set_info_callbackr   r>  rœ   r‡  r�   Úset_info_callbackn  s   ÿzContext.set_info_callbackú#Callable[[Connection, bytes], None]c                   r€  )a  
        Set the TLS key logging callback to *callback*. This function will be
        called whenever TLS key material is generated or received, in order
        to allow applications to store this keying material for debugging
        purposes.

        :param callback: The Python callback to use.  This should take two
            arguments: a Connection object and a bytestring that contains
            the key material in the format used by NSS for its SSLKEYLOGFILE
            debugging output.
        :return: None
        c                   s   t  |¡}ˆ tj|  |ƒ d S r  )r=  r�  rq   r,  )r6  Úliner‡  rœ   r�   r;  ™  s   
z,Context.set_keylog_callback.<locals>.wrapperz#void (*)(const SSL *, const char *)N)r   r=  r!  r¹  r    ÚSSL_CTX_set_keylog_callbackr   r>  rœ   r‡  r�   Úset_keylog_callback‡  s   ÿzContext.set_keylog_callbackr
   c                 C  ó   | j S )zw
        Get the application data (supplied via :meth:`set_app_data()`)

        :return: The application data
        ©r»  r  rœ   rœ   r�   Úget_app_data£  ó   zContext.get_app_datarc  c                 C  ó
   || _ dS )z�
        Set the application data (will be returned from get_app_data())

        :param data: Any Python object
        :return: None
        Nr‘  ©r  rc  rœ   rœ   r�   Úset_app_data«  s   
zContext.set_app_dataúX509Store | Nonec                 C  s.   t  | j¡}|tjkrdS t t¡}||_|S )zú
        Get the certificate store for the context.  This can be used to add
        "trusted" certificates without using the
        :meth:`load_verify_locations` method.

        :return: A X509Store object or None if it does not have one.
        N)r    ÚSSL_CTX_get_cert_storer   r=  r^  r    Ú__new__Ú_store)r  ÚstoreÚpystorerœ   rœ   r�   Úget_cert_storeµ  s   

zContext.get_cert_storeÚoptionsc                 C  r3  ©zÝ
        Add options. Options set before are not cleared!
        This method should be used with the :const:`OP_*` constants.

        :param options: The options to add.
        :return: The new option bitmask.
        zoptions must be an integer)rE  r†   rG  r    ÚSSL_CTX_set_optionsr   ©r  rž  rœ   rœ   r�   Úset_optionsÆ  ó   
	zContext.set_optionsc                 C  r3  )zà
        Add modes via bitmask. Modes set before are not cleared!  This method
        should be used with the :const:`MODE_*` constants.

        :param mode: The mode to add.
        :return: The new mode bitmask.
        r4  )rE  r†   rG  r    ÚSSL_CTX_set_moder   r6  rœ   rœ   r�   rÃ  Ô  r£  zContext.set_modeÚmode_to_clearc                 C  s   t  | j|¡S )z•
        Modes previously set cannot be overwritten without being
        cleared first. This method should be used to clear existing modes.
        )r    ÚSSL_CTX_clear_moder   )r  r¥  rœ   rœ   r�   Ú
clear_modeâ  s   zContext.clear_modeúCallable[[Connection], None]c                   r€  )a  
        Specify a callback function to be called when clients specify a server
        name.

        :param callback: The callback function.  It will be invoked with one
            argument, the Connection instance.

        .. versionadded:: 0.13
        c                   s<   z
ˆ t j|  ƒ W dS  ty   tjt ¡ Ž  tj Y S w r‚  )rq   r,  r-  ÚsysÚ
excepthookÚexc_infor    rM  )r6  ÚalertrR  r‡  rœ   r�   r;  ø  s   ý
þz7Context.set_tlsext_servername_callback.<locals>.wrapperzint (*)(SSL *, int *, void *)N)r   r=  r!  rº  r    Ú&SSL_CTX_set_tlsext_servername_callbackr   r>  rœ   r‡  r�   Úset_tlsext_servername_callbackê  s   ÿÿz&Context.set_tlsext_servername_callbackÚprofilesc                 C  rh  )z÷
        Enable support for negotiating SRTP keying material.

        :param bytes profiles: A colon delimited list of protection profile
            names, like ``b'SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32'``.
        :return: None
        zprofiles must be a byte string.r   N)rE  rF  rG  r¯  r    ÚSSL_CTX_set_tlsext_use_srtpr   )r  r¯  rœ   rœ   r�   Úset_tlsext_use_srtp  s
   
	ÿzContext.set_tlsext_use_srtpÚprotosúlist[bytes]c                 C  óR   |st dƒ‚d t dd„ |D ƒ¡¡}t d|¡}tt | j	|t
|ƒ¡dkƒ dS )a•  
        Specify the protocols that the client is prepared to speak after the
        TLS connection has been negotiated using Application Layer Protocol
        Negotiation.

        :param protos: A list of the protocols to be offered to the server.
            This list should be a Python list of bytestrings representing the
            protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``.
        ú'at least one protocol must be specifiedrB  c                 s  ó"   � | ]}t t|ƒfƒ|fV  qd S r  ©rF  rI  ©Ú.0Úprœ   rœ   r�   Ú	<genexpr>,  ó   €  z*Context.set_alpn_protos.<locals>.<genexpr>rC  r   N)rx  Újoinr   Úfrom_iterabler=  rH  r¯  r    ÚSSL_CTX_set_alpn_protosr   rI  ©r  r²  ÚprotostrÚ	input_strrœ   rœ   r�   Úset_alpn_protos  s   ÿÿýÿzContext.set_alpn_protosrA  c                 C  s,   t |ƒ| _| jj| _t | j| jtj¡ dS )aœ  
        Specify a callback function that will be called on the server when a
        client offers protocols using ALPN.

        :param callback: The callback function.  It will be invoked with two
            arguments: the Connection, and a list of offered protocols as
            bytestrings, e.g ``[b'http/1.1', b'spdy/2']``.  It can return
            one of those bytestrings to indicate the chosen protocol, the
            empty bytestring to terminate the TLS connection, or the
            :py:obj:`NO_OVERLAPPING_PROTOCOLS` to indicate that no offered
            protocol was selected, but that the connection should not be
            aborted.
        N)	r@  r¼  r!  r½  r    ÚSSL_CTX_set_alpn_select_cbr   r=  r^  ©r  r!  rœ   rœ   r�   Úset_alpn_select_callback>  s
   

ÿz Context.set_alpn_select_callbackÚhelperú5_OCSPClientCallbackHelper | _OCSPServerCallbackHelperú
Any | Nonec                 C  sh   || _ |j| _|du rtj| _nt |¡| _t | j	| j¡}t
|dkƒ t | j	| j¡}t
|dkƒ dS )z©
        This internal helper does the common work for
        ``set_ocsp_server_callback`` and ``set_ocsp_client_callback``, which is
        almost all of it.
        Nr$  )r¾  r!  r¿  r=  r^  rÀ  Ú
new_handler    ÚSSL_CTX_set_tlsext_status_cbr   r¯  ÚSSL_CTX_set_tlsext_status_arg)r  rÇ  rc  Úrcrœ   rœ   r�   Ú_set_ocsp_callbackS  s   

ÿzContext._set_ocsp_callbackú_OCSPServerCallback[_T]c                 C  ó   t |ƒ}|  ||¡ dS )aû  
        Set a callback to provide OCSP data to be stapled to the TLS handshake
        on the server side.

        :param callback: The callback function. It will be invoked with two
            arguments: the Connection, and the optional arbitrary data you have
            provided. The callback must return a bytestring that contains the
            OCSP data to staple to the handshake. If no OCSP data is available
            for this connection, return the empty bytestring.
        :param data: Some opaque data that will be passed into the callback
            function when called. This can be used to avoid needing to do
            complex data lookups or to keep track of what context is being
            used. This parameter is optional.
        N)r[  rÎ  ©r  r!  rc  rÇ  rœ   rœ   r�   Úset_ocsp_server_callbackk  s   z Context.set_ocsp_server_callbackú_OCSPClientCallback[_T]c                 C  rÐ  )a�  
        Set a callback to validate OCSP data stapled to the TLS handshake on
        the client side.

        :param callback: The callback function. It will be invoked with three
            arguments: the Connection, a bytestring containing the stapled OCSP
            assertion, and the optional arbitrary data you have provided. The
            callback must return a boolean that indicates the result of
            validating the OCSP data: ``True`` if the OCSP data is valid and
            the certificate can be trusted, or ``False`` if either the OCSP
            data is invalid or the certificate has been revoked.
        :param data: Some opaque data that will be passed into the callback
            function when called. This can be used to avoid needing to do
            complex data lookups or to keep track of what context is being
            used. This parameter is optional.
        N)ri  rÎ  rÑ  rœ   rœ   r�   Úset_ocsp_client_callback‚  s   z Context.set_ocsp_client_callbackrt  c                 C  ó    t |ƒ| _t | j| jj¡ d S r  )rs  rÁ  r    ÚSSL_CTX_set_cookie_generate_cbr   r!  rÅ  rœ   rœ   r�   Úset_cookie_generate_callback›  ó
   
þz$Context.set_cookie_generate_callbackr�  c                 C  rÕ  r  )r€  rÂ  r    ÚSSL_CTX_set_cookie_verify_cbr   r!  rÅ  rœ   rœ   r�   Úset_cookie_verify_callback¦  rØ  z"Context.set_cookie_verify_callback)r­  r†   r  r  )rÉ  r†   r  r  r  )rÑ  rÒ  rÓ  rÒ  r  r  )r!  rØ  r  r"   )r!  rØ  rÛ  rß  r  r  r  )ró  r‘  rô  r‘  r  rn  )rø  rù  rú  rù  r  r  )rþ  rÿ  r  r  )rþ  rÿ  r  r†   r  r  ©r2  r  r  r  )r  r  r  r  )r  rÿ  r  r†   r  r  ©r   r!  r  r  )rÑ  rF  r  r  )r/  rF  r  r  )r2  r†   r  r†   ©r  r†   ©r2  r†   r!  r;  r  r  )r@  r†   r  r  )rI  rÿ  r  r  )rT  rU  r  r  )rd  rF  r  r  )rg  rF  r  r  )rk  rl  r  r  )rw  r  r  r  )rz  r†   r  r  ©r!  r  r  r  )r!  rŒ  r  r  ©r  r
   ©rc  r
   r  r  )r  r—  ©rž  r†   r  r†   )r¥  r†   r  r†   )r!  r¨  r  r  )r¯  rF  r  r  ©r²  r³  r  r  rZ  )rÇ  rÈ  rc  rÉ  r  r  )r!  rÏ  rc  rß  r  r  )r!  rÓ  rc  rß  r  r  r}  r…  )Qr™   rš   r›   rŸ   r{   r    Ú
TLS_methodr€   ri   r~   rf   r   rg   rk   rl   ÚTLS_server_methodrj   ÚTLS_client_methodr%   ÚDTLS_methodr&   ÚDTLS_server_methodr$   ÚDTLS_client_methodr¬  r¡  r  r«  rÆ  rÇ  r×  rÞ  r   rã  rò  rç  rì  r  r   r  r  r  r  r  r'  r)  r.  r1  r7  r:  r?  rC  rE  rH  rS  rc  rf  rj  rv  ry  r|  r~  r‹  Ú_requires_keylogr�  r’  r–  r�  r¢  rÃ  r§  r®  r±  rÃ  rÆ  rÎ  rÒ  rÔ  Ú_requires_ssl_cookier×  rÚ  rœ   rœ   rœ   r�   rr   n  sê   
 









ö
þ
1ý
$ÿý!
*
ÿ
ÿ



ÿ%

		'ÿ%"
	

	%
ýý	rr   c                   @  sL  e Zd ZU eƒ Zded< 	dædçd
d„Zdèdd„Zdédd„Zdêdd„Z	dëdd„Z
dìdd„Zdídd„Z	dædîd#d$„Zdïd%d&„Zdðd)d*„Zdñd-d.„Zdòd0d1„Zdïd2d3„Zdód5d6„Zdïd7d8„Zdôdõd=d>„ZeZdôdõd?d@„ZdædödCdD„ZeZ		d÷dødGdH„ZdùdKdL„ZdúdMdN„ZdûdOdP„ZdüdRdS„ZdýdTdU„ZdüdVdW„Z dïdXdY„Z!dþd[d\„Z"dÿd]d^„Z#�d d`da„Z$dýdbdc„Z%�dddde„Z&düdfdg„Z'dýdhdi„Z(düdjdk„Z)�ddmdn„Z*e+j,�ddrds„ƒZ-e+j,dtduœ�ddxds„ƒZ-dtduœ�dd{ds„Z-�dd~d„Z.�dd€d�„Z/�ddƒd„„Z0dïd…d†„Z1�d	dˆd‰„Z2�d
dŠd‹„Z3dídŒd�„Z4dídŽd�„Z5díd�d‘„Z6	dæ�dd”d•„Z7�dd–d—„Z8e+j,�dd™dš„ƒZ9e+j,dtduœ�ddœdš„ƒZ9dtduœ�ddždš„Z9e+j,�ddŸd „ƒZ:e+j,dtduœ�dd¡d „ƒZ:dtduœ�dd¢d „Z:e;�dd¥d¦„ƒZ<e;�dd¨d©„ƒZ=e+j,�dd«d¬„ƒZ>e+j,dtduœ�dd®d¬„ƒZ>dtduœ�dd°d¬„Z>e+j,�dd±d²„ƒZ?e+j,dtduœ�dd³d²„ƒZ?dtduœ�dd´d²„Z?düdµd¶„Z@düd·d¸„ZAdýd¹dº„ZBdýd»d¼„ZC�dd¾d¿„ZD�ddÂdÃ„ZE�ddÆdÇ„ZFdídÈdÉ„ZGdídÊdË„ZH�ddÍdÎ„ZI�ddÏdÐ„ZJ�ddÑdÒ„ZK�ddÓdÔ„ZLdïdÕdÖ„ZM�ddÙdÚ„ZN�d
dÛdÜ„ZO�d
dÝdÞ„ZPeQ�ddßdà„ƒZRdýdádâ„ZS�ddädå„ZTdS (  rq   z&typing.MutableMapping[Any, Connection]r,  NrË  rr   Úsocketúsocket.socket | Noner  r  c                 C  s  t |tƒs	tdƒ‚d|_t |j¡}t |tj	¡| _
t | j
tj¡ || _d| _d| _|j| _|j| _|j| _|j| _| | j| j
< |du rtd| _t t ¡ ¡| _t| jtjkƒ t t ¡ ¡| _t| jtjkƒ t | j
| j| j¡ dS d| _d| _|| _t | j
t| jƒ¡}t|dkƒ dS )zò
        Create a new Connection object, using the given OpenSSL.SSL.Context
        instance and socket.

        :param context: An SSL Context to use for this connection
        :param socket: The socket to use for transport layer
        ú"context must be a Context instanceTNr$  )rE  rr   rG  r¥  r    ÚSSL_newr   r=  r±  ÚSSL_freeÚ_sslÚSSL_set_modeÚSSL_MODE_AUTO_RETRYr»  rJ  r¶  r·  rÁ  rÂ  r,  Ú_socketÚBIO_newÚ	BIO_s_memÚ	_into_sslr¯  r^  Ú	_from_sslÚSSL_set_bioÚ
SSL_set_fdr‹  )r  rË  rì  r6  rï  rœ   rœ   r�   r  ·  s8   

ÿzConnection.__init__r\  r‘  r
   c                 C  s0   | j du rtd| jj› d|› d�ƒ‚t| j |ƒS )zy
        Look up attributes on the wrapped socket object if they are not found
        on the Connection object.
        Nú'z' object has no attribute ')rô  ÚAttributeErrorÚ	__class__r™   r|  ©r  r\  rœ   rœ   r�   Ú__getattr__õ  s
   
ÿzConnection.__getattr__r6  r8  r†   c                 C  sr  | j jd ur| j j ¡  | j jd ur| j j ¡  | j jd ur$| j j ¡  t ||¡}|tjkr2tƒ ‚|tj	kr:t
ƒ ‚|tjkrBtƒ ‚|tjkrJtƒ ‚|tjkr�tdkrZt ¡ d }ntj}t ¡ dksg|dkr||dk rw|dkrwt|t |¡ƒ‚tddƒ‚tƒ  d S |tjkr­t ¡ dkr­t ¡ }t |¡}tjr¨t|tjkƒ t ¡  tddƒ‚tƒ  d S |tjkr´d S tƒ  d S )NÚwin32r   rl  zUnexpected EOF) r   r¶  r  r¼  r¾  r    ÚSSL_get_errorÚSSL_ERROR_WANT_READr�   ÚSSL_ERROR_WANT_WRITEr‚   ÚSSL_ERROR_ZERO_RETURNr…   ÚSSL_ERROR_WANT_X509_LOOKUPrƒ   ÚSSL_ERROR_SYSCALLr	   r=  ÚgetwinerrorÚerrnoÚERR_peek_errorr}   r   r÷  r  ÚSSL_ERROR_SSLÚERR_GET_REASONÚ-Cryptography_HAS_UNEXPECTED_EOF_WHILE_READINGr¯  Ú"SSL_R_UNEXPECTED_EOF_WHILE_READINGÚERR_clear_errorÚSSL_ERROR_NONE)r  r6  r8  r�  r  Úpeeked_errorÚreasonrœ   rœ   r�   Ú_raise_ssl_error  sJ   







ÿ



zConnection._raise_ssl_errorc                 C  r�  )zh
        Retrieve the :class:`Context` object associated with this
        :class:`Connection`.
        )r   r  rœ   rœ   r�   Úget_context6  s   zConnection.get_contextc                 C  s4   t |tƒs	tdƒ‚t | j|j¡ || _d| j_dS )zª
        Switch this connection to a new session context.

        :param context: A :class:`Context` instance giving the new session
            context to use.
        rî  TN)rE  rr   rG  r    ÚSSL_set_SSL_CTXrñ  r   r¥  )r  rË  rœ   rœ   r�   Úset_context=  s
   
zConnection.set_contextrž  c                 C  r3  rŸ  )rE  r†   rG  r    ÚSSL_set_optionsrñ  r¡  rœ   rœ   r�   r¢  K  s   
zConnection.set_optionsúbytes | Nonec                 C  s(   t  | jt j¡}|tjkrdS t |¡S )zï
        Retrieve the servername extension value if provided in the client hello
        message, or None if there wasn't one.

        :return: A byte string giving the server name or :data:`None`.

        .. versionadded:: 0.13
        N)r    ÚSSL_get_servernamerñ  ÚTLSEXT_NAMETYPE_host_namer=  r^  r�  rþ  rœ   rœ   r�   Úget_servernameX  s   	ÿ

zConnection.get_servernamer2  r!  r;  c                 C  r<  )z—
        Override the Context object's verification flags for this specific
        connection. See :py:meth:`Context.set_verify` for details.
        r4  Nrà  )rE  r†   rG  r¶  r·  r    ÚSSL_set_verifyrñ  r=  r^  rá  r   r!  r>  rœ   rœ   r�   r?  i  s   


zConnection.set_verifyc                 C  r8  )z‡
        Retrieve the Connection object's verify mode, as set by
        :meth:`set_verify`.

        :return: The verify mode
        )r    ÚSSL_get_verify_moderñ  r  rœ   rœ   r�   rE    rF  zConnection.get_verify_moder2  r  c                 C  r  r	  )rE  r   r  r  r  r  r    ÚSSL_use_certificaterñ  r  r  r  rœ   rœ   r�   r  ˆ  s   
ú	
ÿzConnection.use_certificater   r!  c                 C  sL   t |tƒst |¡}ntjdtdd� t | j|j	¡}|s$| j
 ¡  dS dS r"  )rE  r   r#  r  r  r  r    ÚSSL_use_PrivateKeyrñ  r%  r   r  r&  rœ   rœ   r�   r'     s   
ú	ÿzConnection.use_privatekeyÚmtuc                 C  s   t  | j|¡ dS )aK  
        For DTLS, set the maximum UDP payload size (*not* including IP/UDP
        overhead).

        Note that you might have to set :data:`OP_NO_QUERY_MTU` to prevent
        OpenSSL from spontaneously clearing this.

        :param mtu: An integer giving the maximum transmission unit.

        .. versionadded:: 21.1
        N)r    ÚSSL_set_mturñ  )r  r  rœ   rœ   r�   Úset_ciphertext_mtu¸  s   zConnection.set_ciphertext_mtuc                 C  s   t tdƒs	tdƒ‚t | j¡S )a
  
        For DTLS, get the maximum size of unencrypted data you can pass to
        :meth:`write` without exceeding the MTU (as passed to
        :meth:`set_ciphertext_mtu`).

        :return: The effective MTU as an integer.

        .. versionadded:: 21.1
        ÚDTLS_get_data_mtuz requires OpenSSL 1.1.1 or better)Úhasattrr    r”  r"  rñ  r  rœ   rœ   r�   Úget_cleartext_mtuÆ  s   
zConnection.get_cleartext_mturF  c                 C  s4   t |tƒs	tdƒ‚d|v rtdƒ‚t | j|¡ dS )z­
        Set the value of the servername extension to send in the client hello.

        :param name: A byte string giving the name.

        .. versionadded:: 0.13
        zname must be a byte stringó    zname must not contain NUL byteN)rE  rF  rG  r    ÚSSL_set_tlsext_host_namerñ  rþ  rœ   rœ   r�   Úset_tlsext_host_nameÕ  s
   
zConnection.set_tlsext_host_namec                 C  r8  )zÏ
        Get the number of bytes that can be safely read from the SSL buffer
        (**not** the underlying transport buffer).

        :return: The number of bytes available in the receive buffer.
        )r    ÚSSL_pendingrñ  r  rœ   rœ   r�   Úpendingå  rF  zConnection.pendingr   r/  Ú_BufferÚflagsc                 C  sr   t d|ƒ}t |¡�%}t|ƒdkrtdƒ‚t | j|t|ƒ¡}|  | j|¡ |W  d  ƒ S 1 s2w   Y  dS )a³  
        Send data on the connection. NOTE: If you get one of the WantRead,
        WantWrite or WantX509Lookup exceptions on this, you have to call the
        method again with the SAME buffer.

        :param buf: The string, buffer or memoryview to send
        :param flags: (optional) Included for compatibility with the socket
                      API, the value is ignored
        :return: The number of bytes written
        r/  éÿÿÿz,Cannot send more than 2**31-1 bytes at once.N)	r+  r=  Úfrom_bufferrI  rx  r    Ú	SSL_writerñ  r  )r  r/  r+  rc  r8  rœ   rœ   r�   Úsendî  s   
ÿ$özConnection.sendc                 C  sˆ   t d|ƒ}t |¡�0}t|ƒ}d}|r1t | j|| t|dƒ¡}|  | j|¡ ||7 }||8 }|s|W  d  ƒ S 1 s=w   Y  dS )a©  
        Send "all" data on the connection. This calls send() repeatedly until
        all data is sent. If an error occurs, it's impossible to tell how much
        data has been sent.

        :param buf: The string, buffer or memoryview to send
        :param flags: (optional) Included for compatibility with the socket
                      API, the value is ignored
        :return: The number of bytes written
        r/  r   r,  N)	r+  r=  r-  rI  r    r.  rñ  Úminr  )r  r/  r+  rc  Úleft_to_sendÚ
total_sentr8  rœ   rœ   r�   Úsendall
	  s   
ÿø
$òzConnection.sendallÚbufsizr�   c                 C  s`   t d|ƒ}|dur|tj@ rt | j||¡}nt | j||¡}|  | j|¡ t 	||¡dd… S )a  
        Receive data on the connection.

        :param bufsiz: The maximum number of bytes to read
        :param flags: (optional) The only supported flag is ``MSG_PEEK``,
            all other flags are ignored.
        :return: The string read from the Connection
        úchar[]N)
Ú_no_zero_allocatorrì  ÚMSG_PEEKr    ÚSSL_peekrñ  ÚSSL_readr  r=  rD  )r  r4  r+  r/  r8  rœ   rœ   r�   Úrecv'	  s   
	zConnection.recvrD  Únbytesc                 C  sˆ   |du r	t |ƒ}nt|t |ƒƒ}td|ƒ}|dur'|tj@ r't | j||¡}nt | j||¡}|  	| j|¡ t
t ||¡ƒ|d|…< |S )ae  
        Receive data on the connection and copy it directly into the provided
        buffer, rather than creating a new string.

        :param buffer: The buffer to copy into.
        :param nbytes: (optional) The maximum number of bytes to read into the
            buffer. If not present, defaults to the size of the buffer. If
            larger than the size of the buffer, is reduced to the size of the
            buffer.
        :param flags: (optional) The only supported flag is ``MSG_PEEK``,
            all other flags are ignored.
        :return: The number of bytes read into the buffer.
        Nr5  )rI  r0  r6  rì  r7  r    r8  rñ  r9  r  Ú
memoryviewr=  rD  )r  rD  r;  r+  r/  r8  rœ   rœ   r�   Ú	recv_into:	  s   

zConnection.recv_intorP  útyping.NoReturnc                 C  sN   t  |¡r"t  |¡rtƒ ‚t  |¡rtƒ ‚t  |¡rtdƒ‚tdƒ‚tƒ  d S )NÚBIO_should_io_specialzunknown bio failure)	r    ÚBIO_should_retryÚBIO_should_readr�   ÚBIO_should_writer‚   r?  rx  r  )r  rP  r8  rœ   rœ   r�   Ú_handle_bio_errorsd	  s   




zConnection._handle_bio_errorsc                 C  sh   | j du r	tdƒ‚t|tƒstdƒ‚td|ƒ}t | j ||¡}|dkr*|  | j |¡ t 	||¡dd… S )a¹  
        If the Connection was created with a memory BIO, this method can be
        used to read bytes from the write end of that memory BIO.  Many
        Connection methods will add bytes which must be read in this manner or
        the buffer will eventually fill up and the Connection will be able to
        take no further actions.

        :param bufsiz: The maximum number of bytes to read
        :return: The string read.
        NúConnection sock was not Nonezbufsiz must be an integerr5  r   )
rø  rG  rE  r†   r6  r    ÚBIO_readrC  r=  rD  )r  r4  r/  r8  rœ   rœ   r�   Úbio_readv	  s   


zConnection.bio_readc                 C  sx   t d|ƒ}| jdu rtdƒ‚t |¡�}t | j|t|ƒ¡}|dkr)|  | j|¡ |W  d  ƒ S 1 s5w   Y  dS )aj  
        If the Connection was created with a memory BIO, this method can be
        used to add bytes to the read end of that memory BIO.  The Connection
        can then read the bytes (for example, in response to a call to
        :meth:`recv`).

        :param buf: The string to put into the memory BIO.
        :return: The number of bytes written
        r/  NrD  r   )	r+  r÷  rG  r=  r-  r    Ú	BIO_writerI  rC  )r  r/  rc  r8  rœ   rœ   r�   Ú	bio_writeŽ	  s   


$üzConnection.bio_writern  c                 C  s$   |   ¡ stt | j¡dkƒ dS dS )zv
        Renegotiate the session.

        :return: True if the renegotiation can be started, False otherwise
        r$  TF)Úrenegotiate_pendingr¯  r    ÚSSL_renegotiaterñ  r  rœ   rœ   r�   Úrenegotiate£	  s   zConnection.renegotiatec                 C  s   t  | j¡}|  | j|¡ dS )a  
        Perform an SSL handshake (usually called after :meth:`renegotiate` or
        one of :meth:`set_accept_state` or :meth:`set_connect_state`). This can
        raise the same exceptions as :meth:`send` and :meth:`recv`.

        :return: None.
        N)r    ÚSSL_do_handshakerñ  r  ©r  r8  rœ   rœ   r�   Údo_handshake®	  s   zConnection.do_handshakec                 C  s   t  | j¡dkS )z¼
        Check if there's a renegotiation in progress, it will return False once
        a renegotiation is finished.

        :return: Whether there's a renegotiation in progress
        r$  )r    ÚSSL_renegotiate_pendingrñ  r  rœ   rœ   r�   rI  ¹	  ó   zConnection.renegotiate_pendingc                 C  r8  )zn
        Find out the total number of renegotiations.

        :return: The number of renegotiations.
        )r    ÚSSL_total_renegotiationsrñ  r  rœ   rœ   r�   Útotal_renegotiationsÂ	  s   zConnection.total_renegotiationsÚaddrc                 C  s   t  | j¡ | j |¡S )a4  
        Call the :meth:`connect` method of the underlying socket and set up SSL
        on the socket, using the :class:`Context` object supplied to this
        :class:`Connection` object at creation.

        :param addr: A remote address
        :return: What the socket's connect method returns
        )r    ÚSSL_set_connect_staterñ  rô  Úconnect)r  rS  rœ   rœ   r�   rU  Ê	  s   	zConnection.connectc                 C  s   | j j}|  ¡  ||ƒS )a•  
        Call the :meth:`connect_ex` method of the underlying socket and set up
        SSL on the socket, using the Context object supplied to this Connection
        object at creation. Note that if the :meth:`connect_ex` method of the
        socket doesn't return 0, SSL won't be initialized.

        :param addr: A remove address
        :return: What the socket's connect_ex method returns
        )rô  Ú
connect_exÚset_connect_state)r  rS  rV  rœ   rœ   r�   rV  Ö	  s   
zConnection.connect_exútuple[Connection, Any]c                 C  s*   | j  ¡ \}}t| j|ƒ}| ¡  ||fS )a‹  
        Call the :meth:`accept` method of the underlying socket and set up SSL
        on the returned socket, using the Context object supplied to this
        :class:`Connection` object at creation.

        :return: A *(conn, addr)* pair where *conn* is the new
            :class:`Connection` object created, and *address* is as returned by
            the socket's :meth:`accept`.
        )rô  Úacceptrq   r   Úset_accept_state)r  ÚclientrS  rS  rœ   rœ   r�   rY  ä	  s   
zConnection.acceptc              	   C  s†   t  ¡ }zt  | j|¡}W t  |¡ nt  |¡ w | jdur#| j ¡  | jdur-| j ¡  |dkr4tƒ ‚|dk rA|  	| j|¡ dS dS )z•
        Call the OpenSSL function DTLSv1_listen on this connection. See the
        OpenSSL manual for more details.

        :return: None
        Nr   )
r    ÚBIO_ADDR_newÚDTLSv1_listenrñ  ÚBIO_ADDR_freerÁ  r  rÂ  r�   r  )r  Úbio_addrr8  rœ   rœ   r�   r]  ó	  s   



ÿzConnection.DTLSv1_listenc                 C  s<   t  d¡}t  d¡}t | j||¡r|d |d d  S dS )a}  
        Determine when the DTLS SSL object next needs to perform internal
        processing due to the passage of time.

        When the returned number of seconds have passed, the
        :meth:`DTLSv1_handle_timeout` method needs to be called.

        :return: The time left in seconds before the next timeout or `None`
            if no timeout is currently active.
        ztime_t *zlong *r   i@B N)r=  rH  r    ÚCryptography_DTLSv1_get_timeoutrñ  )r  Úptv_secÚptv_usecrœ   rœ   r�   ÚDTLSv1_get_timeout
  s
   

zConnection.DTLSv1_get_timeoutc                 C  s2   t  | j¡}|dk r|  | j|¡ J dƒ‚t|ƒS )z°
        Handles any timeout events which have become pending on a DTLS SSL
        object.

        :return: `True` if there was a pending timeout, `False` otherwise.
        r   FÚunreachable)r    ÚDTLSv1_handle_timeoutrñ  r  rn  rM  rœ   rœ   r�   re  
  s
   z Connection.DTLSv1_handle_timeoutc                 C  s$   | j du r	tdƒ‚t | jd¡ dS )zÕ
        If the Connection was created with a memory BIO, this method can be
        used to indicate that *end of file* has been reached on the read end of
        that memory BIO.

        :return: None
        NrD  r   )rø  rG  r    ÚBIO_set_mem_eof_returnr÷  r  rœ   rœ   r�   Úbio_shutdown-
  s   
zConnection.bio_shutdownc                 C  s:   t  | j¡}|dk r|  | j|¡ J dƒ‚|dkrdS dS )aQ  
        Send the shutdown message to the Connection.

        :return: True if the shutdown completed successfully (i.e. both sides
                 have sent closure alerts), False otherwise (in which case you
                 call :meth:`recv` or :meth:`send` when the connection becomes
                 readable/writeable).
        r   Frd  T)r    ÚSSL_shutdownrñ  r  rM  rœ   rœ   r�   Úshutdown:
  s   	zConnection.shutdownrù  c                 C  sH   g }t ƒ D ]}t | j|¡}|tjkr |S | t |¡ d¡¡ q|S )z€
        Retrieve the list of ciphers used by the Connection object.

        :return: A list of native cipher strings.
        úutf-8)	r   r    ÚSSL_get_cipher_listrñ  r=  r^  r.  r�  Údecode)r  Úciphersrˆ  r8  rœ   rœ   r�   Úget_cipher_listL
  s   

ÿzConnection.get_cipher_listÚas_cryptographyútyping.Literal[True]úlist[x509.Name]c                C  ó   d S r  rœ   ©r  ro  rœ   rœ   r�   Úget_client_ca_listZ
  ó   zConnection.get_client_ca_listF)ro  útyping.Literal[False]úlist[X509Name]c                C  rr  r  rœ   rs  rœ   rœ   r�   rt  `
  ru  ú,typing.Literal[True] | typing.Literal[False]ú list[X509Name] | list[x509.Name]c                C  s  t  | j¡}|tjkrg S |rPg }tt  |¡ƒD ]5}t  ||¡}t d¡}t  	||¡}t
|dkƒ t |d |¡dd… }t  |d ¡ | tj |¡¡ q|S g }	tt  |¡ƒD ]&}t  ||¡}t  |¡}
t
|
tjkƒ t t¡}t |
t j¡|_|	 |¡ qY|	S )a‡  
        Get CAs whose certificates are suggested for client authentication.

        :param bool as_cryptography: Controls whether a list of
            ``cryptography.x509.Name`` or ``OpenSSL.crypto.X509Name``
            objects should be returned.

        :return: If this is a server connection, the list of certificate
            authorities that will be sent or has been sent to the client, as
            controlled by this :class:`Connection`'s :class:`Context`.

            If this is a client connection, the list will be empty until the
            connection with the server is established.

        .. versionadded:: 0.10
        zunsigned char**r   N)r    ÚSSL_get_client_CA_listrñ  r=  r^  ÚrangeÚsk_X509_NAME_numÚsk_X509_NAME_valuerH  Úi2d_X509_NAMEr¯  rD  ÚOPENSSL_freer.  r   ÚNameÚ
from_bytesrn  Úobjectr™  r   r±  rq  ro  )r  ro  Úca_namesÚnamesrˆ  r\  Úresult_bufferÚencode_resultÚderr8  r  Úpynamerœ   rœ   r�   rt  f
  s.   



r•  r–  c                 O  s   t dƒ‚)zœ
        The makefile() method is not implemented, since there is no dup
        semantics for SSL connections

        :raise: NotImplementedError
        z1Cannot make file object of OpenSSL.SSL.Connectionr“  r¦  rœ   rœ   r�   Úmakefileš
  s   ÿzConnection.makefilec                 C  r�  )zr
        Retrieve application data as set by :meth:`set_app_data`.

        :return: The application data
        r‘  r  rœ   rœ   r�   r’  ¥
  r“  zConnection.get_app_datarc  c                 C  r”  )zg
        Set application data

        :param data: The application data
        :return: None
        Nr‘  r•  rœ   rœ   r�   r–  ­
  s   
zConnection.set_app_datac                 C  r8  )zž
        Get the shutdown state of the Connection.

        :return: The shutdown state, a bitvector of SENT_SHUTDOWN,
            RECEIVED_SHUTDOWN.
        )r    ÚSSL_get_shutdownrñ  r  rœ   rœ   r�   Úget_shutdown¶
  rF  zConnection.get_shutdownÚstatec                 C  rA  )z—
        Set the shutdown state of the Connection.

        :param state: bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.
        :return: None
        zstate must be an integerN)rE  r†   rG  r    ÚSSL_set_shutdownrñ  )r  rŒ  rœ   rœ   r�   Úset_shutdown¿
  s   
zConnection.set_shutdownc                 C  s   t  t | j¡¡S )z„
        Retrieve a verbose string detailing the state of the Connection.

        :return: A string representing the state
        )r=  r�  r    ÚSSL_state_string_longrñ  r  rœ   rœ   r�   Úget_state_stringË
  s   zConnection.get_state_stringc                 C  óf   t  | j¡}|tjkrdS t  | jtjd¡}t|dkƒ td|ƒ}t  | j||¡ t ||¡dd… S )z�
        Retrieve the random value used with the server hello message.

        :return: A string representing the state
        Nr   rC  )	r    ÚSSL_get_sessionrñ  r=  r^  ÚSSL_get_server_randomr¯  r6  rD  ©r  ÚsessionÚlengthÚoutprœ   rœ   r�   Úserver_randomÓ
  s   

zConnection.server_randomc                 C  r‘  )z�
        Retrieve the random value used with the client hello message.

        :return: A string representing the state
        Nr   rC  )	r    r’  rñ  r=  r^  ÚSSL_get_client_randomr¯  r6  rD  r”  rœ   rœ   r�   Úclient_randomâ
  s   

zConnection.client_randomc                 C  sb   t  | j¡}|tjkrdS t  |tjd¡}t|dkƒ td|ƒ}t  |||¡ t ||¡dd… S )zz
        Retrieve the value of the master key for this session.

        :return: A string representing the state
        Nr   rC  )	r    r’  rñ  r=  r^  ÚSSL_SESSION_get_master_keyr¯  r6  rD  r”  rœ   rœ   r�   Ú
master_keyò
  s   

zConnection.master_keyÚlabelÚolenc           	   
   C  sn   t d|ƒ}tj}d}d}|dur|}t|ƒ}d}t | j|||t|ƒ|||¡}t|dkƒ t ||¡dd… S )aH  
        Obtain keying material for application use.

        :param: label - a disambiguating label string as described in RFC 5705
        :param: olen - the length of the exported key material in bytes
        :param: context - a per-association context value
        :return: the exported key material bytes or None
        rC  r   Nr$  )	r6  r=  r^  rI  r    ÚSSL_export_keying_materialrñ  r¯  rD  )	r  r�  rž  rË  r—  Úcontext_bufÚcontext_lenÚuse_contextÚsuccessrœ   rœ   r�   Úexport_keying_material  s(   
ø
z!Connection.export_keying_materialc                 O  s   | j j|i |¤ŽS )z®
        Call the :meth:`shutdown` method of the underlying socket.
        See :manpage:`shutdown(2)`.

        :return: What the socket's shutdown() method returns
        )rô  ri  r¦  rœ   rœ   r�   Úsock_shutdown"  s   zConnection.sock_shutdownúx509.Certificate | Nonec                C  rr  r  rœ   rs  rœ   rœ   r�   Úget_certificate+  ru  zConnection.get_certificateúX509 | Nonec                C  rr  r  rœ   rs  rœ   rœ   r�   r§  1  ru  úX509 | x509.Certificate | Nonec                C  s>   t  | j¡}|tjkrt  |¡ t |¡}|r| ¡ S |S dS )a  
        Retrieve the local certificate (if any)

        :param bool as_cryptography: Controls whether a
            ``cryptography.x509.Certificate`` or an ``OpenSSL.crypto.X509``
            object should be returned.

        :return: The local certificate
        N)	r    ÚSSL_get_certificaterñ  r=  r^  r&  r   r'  Úto_cryptography©r  ro  r2  Úpycertrœ   rœ   r�   r§  7  s   


c                C  rr  r  rœ   rs  rœ   rœ   r�   Úget_peer_certificateN  ru  zConnection.get_peer_certificatec                C  rr  r  rœ   rs  rœ   rœ   r�   r®  T  ru  c                C  s4   t  | j¡}|tjkrt |¡}|r| ¡ S |S dS )a  
        Retrieve the other side's certificate (if any)

        :param bool as_cryptography: Controls whether a
            ``cryptography.x509.Certificate`` or an ``OpenSSL.crypto.X509``
            object should be returned.

        :return: The peer's certificate
        N)r    ÚSSL_get_peer_certificaterñ  r=  r^  r   r'  r«  r¬  rœ   rœ   r�   r®  Z  s   

Ú
cert_stackú
list[X509]c                 C  s`   g }t t | ¡ƒD ]$}t | |¡}t|tjkƒ t |¡}t|dkƒ t 	|¡}| 
|¡ q	|S ©zb
        Internal helper to convert a STACK_OF(X509) to a list of X509
        instances.
        r$  )r{  r    Úsk_X509_numÚsk_X509_valuer¯  r=  r^  r&  r   r'  r.  ©r°  r8  rˆ  r2  rR  r­  rœ   rœ   r�   Ú_cert_stack_to_listp  s   

zConnection._cert_stack_to_listúlist[x509.Certificate]c                 C  sd   g }t t | ¡ƒD ]&}t | |¡}t|tjkƒ t |¡}t|dkƒ t 	|¡}| 
| ¡ ¡ q	|S r²  )r{  r    r³  r´  r¯  r=  r^  r&  r   r'  r.  r«  rµ  rœ   rœ   r�   Ú _cert_stack_to_cryptography_list€  s   

z+Connection._cert_stack_to_cryptography_listúlist[x509.Certificate] | Nonec                C  rr  r  rœ   rs  rœ   rœ   r�   Úget_peer_cert_chain’  ru  zConnection.get_peer_cert_chainúlist[X509] | Nonec                C  rr  r  rœ   rs  rœ   rœ   r�   rº  ˜  ru  ú*list[X509] | list[x509.Certificate] | Nonec                C  ó2   t  | j¡}|tjkrdS |r|  |¡S |  |¡S )as  
        Retrieve the other side's certificate (if any)

        :param bool as_cryptography: Controls whether a list of
            ``cryptography.x509.Certificate`` or ``OpenSSL.crypto.X509``
            object should be returned.

        :return: A list of X509 instances giving the peer's certificate chain,
                 or None if it does not have one.
        N)r    ÚSSL_get_peer_cert_chainrñ  r=  r^  r¸  r¶  ©r  ro  r°  rœ   rœ   r�   rº  ž  s   


c                C  rr  r  rœ   rs  rœ   rœ   r�   Úget_verified_chainµ  ru  zConnection.get_verified_chainc                C  rr  r  rœ   rs  rœ   rœ   r�   rÀ  »  ru  c                C  r½  )a‚  
        Retrieve the verified certificate chain of the peer including the
        peer's end entity certificate. It must be called after a session has
        been successfully established. If peer verification was not successful
        the chain may be incomplete, invalid, or None.

        :param bool as_cryptography: Controls whether a list of
            ``cryptography.x509.Certificate`` or ``OpenSSL.crypto.X509``
            object should be returned.

        :return: A list of X509 instances giving the peer's verified
                 certificate chain, or None if it does not have one.

        .. versionadded:: 20.0
        N)r    ÚSSL_get0_verified_chainrñ  r=  r^  r¸  r¶  r¿  rœ   rœ   r�   rÀ  Á  s   


c                 C  r8  )z£
        Checks if more data has to be read from the transport layer to complete
        an operation.

        :return: True iff more data has to be read
        )r    ÚSSL_want_readrñ  r  rœ   rœ   r�   Ú	want_readÞ  rF  zConnection.want_readc                 C  r8  )z�
        Checks if there is data to write to the transport layer to complete an
        operation.

        :return: True iff there is data to write
        )r    ÚSSL_want_writerñ  r  rœ   rœ   r�   Ú
want_writeç  rF  zConnection.want_writec                 C  ó   t  | j¡ dS )z–
        Set the connection to work in server mode. The handshake will be
        handled automatically by read/write.

        :return: None
        N)r    ÚSSL_set_accept_staterñ  r  rœ   rœ   r�   rZ  ð  rP  zConnection.set_accept_statec                 C  rÆ  )z–
        Set the connection to work in client mode. The handshake will be
        handled automatically by read/write.

        :return: None
        N)r    rT  rñ  r  rœ   rœ   r�   rW  ù  rP  zConnection.set_connect_stateúSession | Nonec                 C  s@   t  | j¡}|tjkrdS t t¡}t |t j¡|_	| j
|_
|S )zÄ
        Returns the Session currently used.

        :return: An instance of :class:`OpenSSL.SSL.Session` or
            :obj:`None` if no session exists.

        .. versionadded:: 0.14
        N)r    ÚSSL_get1_sessionrñ  r=  r^  r|   r™  r±  ÚSSL_SESSION_freerŸ  r   )r  r•  Ú	pysessionrœ   rœ   r�   Úget_session  s   	

zConnection.get_sessionr•  r|   c                 C  sF   t |tƒs	tdƒ‚|j| jurtdƒ‚t | j|j¡}t	|dkƒ dS )a
  
        Set the session to be used when the TLS/SSL connection is established.

        The session must have been obtained, via :meth:`get_session`, from a
        :class:`Connection` that was using the same :class:`Context` as this
        one. OpenSSL requires (but does not verify) that sessions only be
        re-used with a compatible ``SSL_CTX``, so this is enforced here.

        :param session: A Session instance representing the session to use.
        :returns: None

        .. versionadded:: 0.14
        z"session must be a Session instancezQsession must have been created by a Connection using the same Context as this oner$  N)
rE  r|   rG  r   rx  r    ÚSSL_set_sessionrñ  rŸ  r¯  )r  r•  r8  rœ   rœ   r�   Úset_session  s   
ÿzConnection.set_sessionÚfunctionúCallable[[Any, Any, int], int]c                 C  sR   t  dd¡}|| j|dƒ}|dkrdS td|ƒ}|| j||ƒ t  ||¡dd… S )aN  
        Helper to implement :meth:`get_finished` and
        :meth:`get_peer_finished`.

        :param function: Either :data:`SSL_get_finished`: or
            :data:`SSL_get_peer_finished`.

        :return: :data:`None` if the desired message has not yet been
            received, otherwise the contents of the message.
        r5  r   N)r=  rH  rñ  r6  rD  )r  rÏ  ÚemptyrÙ  r/  rœ   rœ   r�   Ú_get_finished_message.  s   
z Connection._get_finished_messagec                 C  ó   |   tj¡S )zÛ
        Obtain the latest TLS Finished message that we sent.

        :return: The contents of the message or :obj:`None` if the TLS
            handshake has not yet completed.

        .. versionadded:: 0.15
        )rÒ  r    ÚSSL_get_finishedr  rœ   rœ   r�   Úget_finishedQ  ó   	zConnection.get_finishedc                 C  rÓ  )zí
        Obtain the latest TLS Finished message that we received from the peer.

        :return: The contents of the message or :obj:`None` if the TLS
            handshake has not yet completed.

        .. versionadded:: 0.15
        )rÒ  r    ÚSSL_get_peer_finishedr  rœ   rœ   r�   Úget_peer_finished\  rÖ  zConnection.get_peer_finishedú
str | Nonec                 C  ó4   t  | j¡}|tjkrdS t t  |¡¡}| d¡S )zÚ
        Obtain the name of the currently used cipher.

        :returns: The name of the currently used cipher or :obj:`None`
            if no connection has been established.

        .. versionadded:: 0.15
        Nrj  )r    ÚSSL_get_current_cipherrñ  r=  r^  r�  ÚSSL_CIPHER_get_namerl  )r  Úcipherr\  rœ   rœ   r�   Úget_cipher_nameg  ó
   	

zConnection.get_cipher_namec                 C  s(   t  | j¡}|tjkrdS t  |tj¡S )zü
        Obtain the number of secret bits of the currently used cipher.

        :returns: The number of secret bits of the currently used cipher
            or :obj:`None` if no connection has been established.

        .. versionadded:: 0.15
        N)r    rÛ  rñ  r=  r^  ÚSSL_CIPHER_get_bits)r  rÝ  rœ   rœ   r�   Úget_cipher_bitsw  s   	
zConnection.get_cipher_bitsc                 C  rÚ  )zï
        Obtain the protocol version of the currently used cipher.

        :returns: The protocol name of the currently used cipher
            or :obj:`None` if no connection has been established.

        .. versionadded:: 0.15
        Nrj  )r    rÛ  rñ  r=  r^  r�  ÚSSL_CIPHER_get_versionrl  )r  rÝ  rÉ  rœ   rœ   r�   Úget_cipher_version†  rß  zConnection.get_cipher_versionc                 C  s   t  t | j¡¡}| d¡S )a  
        Retrieve the protocol version of the current connection.

        :returns: The TLS version of the current connection, for example
            the value for TLS 1.2 would be ``TLSv1.2``or ``Unknown``
            for connections that were not successfully established.
        rj  )r=  r�  r    ÚSSL_get_versionrñ  rl  rÎ  rœ   rœ   r�   Úget_protocol_version_name–  s   
z$Connection.get_protocol_version_namec                 C  s   t  | j¡}|S )zï
        Retrieve the SSL or TLS protocol version of the current connection.

        :returns: The TLS version of the current connection.  For example,
            it will return ``0x769`` for connections made over TLS version 1.
        )r    ÚSSL_versionrñ  rÎ  rœ   rœ   r�   Úget_protocol_version¡  s   zConnection.get_protocol_versionr²  r³  c                 C  r´  )ah  
        Specify the client's ALPN protocol list.

        These protocols are offered to the server during protocol negotiation.

        :param protos: A list of the protocols to be offered to the server.
            This list should be a Python list of bytestrings representing the
            protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``.
        rµ  rB  c                 s  r¶  r  r·  r¸  rœ   rœ   r�   r»  ¾  r¼  z-Connection.set_alpn_protos.<locals>.<genexpr>rC  r   N)rx  r½  r   r¾  r=  rH  r¯  r    ÚSSL_set_alpn_protosrñ  rI  rÀ  rœ   rœ   r�   rÃ  «  s   ÿÿzConnection.set_alpn_protosc                 C  sH   t  d¡}t  d¡}t | j||¡ |sdS t  |d |d ¡dd… S )zÇ
        Get the protocol that was negotiated by ALPN.

        :returns: A bytestring of the protocol name.  If no protocol has been
            negotiated yet, returns an empty bytestring.
        rk  zunsigned int *rB  r   N)r=  rH  r    ÚSSL_get0_alpn_selectedrñ  rD  )r  rc  Údata_lenrœ   rœ   r�   Úget_alpn_proto_negotiatedÍ  s   

z$Connection.get_alpn_proto_negotiatedc                 C  s    t  | j¡}|s
dS t |j¡S )zÇ
        Get the SRTP protocol which was negotiated.

        :returns: A bytestring of the SRTP profile name. If no profile has been
            negotiated yet, returns an empty bytestring.
        rB  )r    ÚSSL_get_selected_srtp_profilerñ  r=  r�  r\  )r  Úprofilerœ   rœ   r�   Úget_selected_srtp_profileÞ  s   z$Connection.get_selected_srtp_profilec                 C  sD   t  | j¡}|tjkrdS t  | j¡}|tjkrdS t |¡ d¡S )zŽ
        Get the name of the negotiated group for the key exchange.

        :return: A string giving the group name or :data:`None`.
        Nrj  )r    r’  rñ  r=  r^  ÚSSL_get0_group_namer�  rl  )r  r•  Ú
group_namerœ   rœ   r�   Úget_group_nameë  s   


zConnection.get_group_namec                 C  s    t  | jt j¡}t|dkƒ dS )a  
        Called to request that the server sends stapled OCSP data, if
        available. If this is not called on the client side then the server
        will not send OCSP data. Should be used in conjunction with
        :meth:`Context.set_ocsp_client_callback`.
        r$  N)r    ÚSSL_set_tlsext_status_typerñ  ÚTLSEXT_STATUSTYPE_ocspr¯  )r  rÍ  rœ   rœ   r�   Úrequest_ocspÿ  s   ÿzConnection.request_ocspr  c                   r€  )r�  c                   r‚  r  rƒ  r„  r‡  rœ   r�   r;    rˆ  z-Connection.set_info_callback.<locals>.wrapperr‰  N)r   r=  r!  r¸  r    ÚSSL_set_info_callbackrñ  r>  rœ   r‡  r�   r‹    s   ÿzConnection.set_info_callbackr  )rË  rr   rì  rí  r  r  )r\  r‘  r  r
   )r6  r
   r8  r†   r  r  )r  rr   )rË  rr   r  r  râ  )r  r  rÞ  rÝ  rÛ  rÜ  )r  r†   r  r  )r\  rF  r  r  )r   )r/  r*  r+  r†   r  r†   )r4  r†   r+  r�   r  rF  )NN)rD  r
   r;  r�   r+  r�   r  r†   )rP  r
   r8  r†   r  r>  )r4  r†   r  rF  )r/  r*  r  r†   )r  rn  r  )rS  r
   r  r  )rS  r
   r  r†   )r  rX  )r  r�   )r  rù  )ro  rp  r  rq  )ro  rv  r  rw  )ro  rx  r  ry  )r•  r
   r–  r
   r  r>  rà  rá  )rŒ  r†   r  r  )r  rF  )r�  rF  rž  r†   rË  r  r  rF  )r•  r
   r–  r
   r  r  )ro  rp  r  r¦  )ro  rv  r  r¨  )ro  rx  r  r©  )r°  r
   r  r±  )r°  r
   r  r·  )ro  rp  r  r¹  )ro  rv  r  r»  )ro  rx  r  r¼  )r  rÈ  )r•  r|   r  r  )rÏ  rÐ  r  r  )r  rÙ  )r  r‘  rã  rß  )Ur™   rš   r›   r   r,  r¡  r  rÿ  r  r  r  r¢  r  r?  rE  r  r'  r!  r$  r'  r)  r/  Úwriter3  r:  Úreadr=  rC  rF  rH  rK  rN  rI  rR  rU  rV  rY  r]  rc  re  rg  ri  rn  r©  Úoverloadrt  r‰  r’  r–  r‹  rŽ  r�  r˜  rš  rœ  r¤  r¥  r§  r®  Ústaticmethodr¶  r¸  rº  rÀ  rÃ  rÅ  rZ  rW  rÌ  rÎ  rÒ  rÕ  rØ  rÞ  rá  rã  rå  rç  rÃ  rë  rî  Ú_requires_ssl_get0_group_namerñ  rô  r‹  rœ   rœ   rœ   r�   rq   ²  sè   
 
ÿÿ
>

5


ÿ

	




	ü
*





	





ÿý4
		


ÿ 	ÿýÿýÿýÿý

	
	
		
#


"
)r†  r
   r  r†   )rŒ  r†   r  rF  )r�  r†   r�  r‘  r  r’  )r¤  r¢  r  r¢  )ûÚ
__future__r   rõ  rì  r©  r©  r  Úcollections.abcr   r  r   Ú	functoolsr   r   Ú	itertoolsr   r   r	   r
   r   r   r   Úweakrefr   Úversion_infor   Útyping_extensionsÚcryptographyr   Ú)cryptography.hazmat.primitives.asymmetricr   ÚOpenSSL._utilr   rÿ  r   Ú_exception_from_error_queuer   r=  r   r    r   Ú_make_assertr   r6  r   rÔ  r   r+  ÚOpenSSL.cryptor   r   r   r   r    r!   r"   r#   Ú__all__r.   r¡  r-   r*   r,   r+   r)   rU   rR   rT   rS   rQ   ÚSSL_SENT_SHUTDOWNrG   ÚSSL_RECEIVED_SHUTDOWNrF   r{   r€   r~   r   rk   rl   rj   r%   r&   r$   rP   ri   rf   rg   rh   ÚSSL_OP_NO_SSLv2rt   ÚSSL_OP_NO_SSLv3ru   ÚSSL_OP_NO_TLSv1rv   ÚSSL_OP_NO_TLSv1_1rw   ÚSSL_OP_NO_TLSv1_2rx   ÚSSL_OP_NO_TLSv1_3ry   ÚSSL_MODE_RELEASE_BUFFERSr'   ÚSSL_OP_SINGLE_DH_USEr?   ÚSSL_OP_SINGLE_ECDH_USEr@   ÚSSL_OP_EPHEMERAL_RSAr2   ÚSSL_OP_MICROSOFT_SESS_ID_BUGr4   ÚSSL_OP_NETSCAPE_CHALLENGE_BUGr7   Ú'SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUGr9   Ú"SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUGrB   Ú!SSL_OP_MICROSOFT_BIG_SSLV3_BUFFERr3   ÚSSL_OP_MSIE_SSLV2_RSA_PADDINGr5   ÚSSL_OP_SSLEAY_080_CLIENT_DH_BUGrA   ÚSSL_OP_TLS_D5_BUGrD   ÚSSL_OP_TLS_BLOCK_PADDING_BUGrC   Ú"SSL_OP_DONT_INSERT_EMPTY_FRAGMENTSr1   ÚSSL_OP_CIPHER_SERVER_PREFERENCEr0   ÚSSL_OP_TLS_ROLLBACK_BUGrE   ÚSSL_OP_PKCS1_CHECK_1r=   ÚSSL_OP_PKCS1_CHECK_2r>   ÚSSL_OP_NETSCAPE_CA_DN_BUGr6   Ú&SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUGr8   ÚSSL_OP_NO_COMPRESSIONr:   ÚSSL_OP_NO_QUERY_MTUr;   ÚSSL_OP_COOKIE_EXCHANGEr‘   r.  rü  ÚSSL_OP_NO_TICKETr<   ÚSSL_OP_NO_RENEGOTIATIONr’   ÚSSL_OP_IGNORE_UNEXPECTED_EOFr“   ÚSSL_OP_LEGACY_SERVER_CONNECTr”   Ú
SSL_OP_ALLr/   ÚSSL_VERIFY_PEERrp   ÚSSL_VERIFY_FAIL_IF_NO_PEER_CERTrn   ÚSSL_VERIFY_CLIENT_ONCErm   ÚSSL_VERIFY_NONEro   ÚSSL_SESS_CACHE_OFFrN   ÚSSL_SESS_CACHE_CLIENTrI   ÚSSL_SESS_CACHE_SERVERrO   ÚSSL_SESS_CACHE_BOTHrH   ÚSSL_SESS_CACHE_NO_AUTO_CLEARrJ   Ú!SSL_SESS_CACHE_NO_INTERNAL_LOOKUPrL   Ú SSL_SESS_CACHE_NO_INTERNAL_STORErM   ÚSSL_SESS_CACHE_NO_INTERNALrK   rd   rc   re   r^   r[   r_   ra   rX   r`   rb   rW   rV   rZ   rY   r]   r\   ÚUnionrF  Ú	bytearrayr<  r*  r•   r—   r(   ÚListrA  rt  rn  r�  Ú_OCSPClientCallbackÚ_OCSPServerCallbackr†   Ú_PassphraseCallbackr"  r„   rí  rî  rê  rë  r-  rs   r  r¯  r�   r‚   rƒ   r…   r}   r  r   r@  r[  ri  rs  r€  r‹  rŽ  rz   r›  r|  rê  rú  rë  r|   r¢  r«  rr   rq   rœ   rœ   rœ   r�   Ú<module>   sœ   
(f
ÿ
ÿÿÿÿÿþüÿk	ÿ
(=C;

	
ÿ
þ
þ
        L