o
    zqfE                  	   @   s  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mZmZmZm	Z	m
Z
mZmZmZ d dlmZ ddlmZ e d  ZeeeedZedk rXeded   ded  d	ed
edefddZeddZeddZeZe
ZeZ eZ!eZ"eZ#eZ$eZ%eZ&eZ'ee&Z(eZ)e	Z*ee%Z+ee!Z,ee"Z-ee#Z.ee$Z/eZ0eZ1eeZ2eeZ3eeZ4eZ5eZ6z*e)e,gej7_8e2ej7_9e2gej:_8e,ej:_9e*egej;_8e-ej;_9e4e.gej<_8e*ej<_9e4egej=_8e*ej=_9e4ee5gej>_8e*ej>_9e1gej?_8e3ej?_9ee-gej@_8e3ej@_9e(e(ee4gejA_8e*ejA_9e4ee5gejB_8e*ejB_9e4e_4e5e_5e*e_*dZCdZDe(gejE_8dejE_9e(gejF_8e'ejF_9e)ee gejG_8e-ejG_9e-e gejH_8eejH_9e-eee gejI_8eejI_9e)eegejJ_8e,ejJ_9e,gejK_8eejK_9e,gejL_8eejL_9e)ee(ee0gejM_8e.ejM_9e)ee0gejN_8e/ejN_9e/egejO_8dejO_9e.gejP_8eejP_9e.egejQ_8eejQ_9e+gejR_8eejR_9e+gejS_8e-ejS_9e)Tede_UeTede_Ve(e_(e.e_.e-e_-e+e_+W n eWy   eddw de*dejXdejXdejXfddZYeYejA_ZeYej<_ZeYejB_ZG dd dZ[de\de,fdd Z]de\de"fd!d"Z^d#e-dedB fd$d%Z_d&e`e\ de/fd'd(Zae jbd)ejcdejdd fd*d+Ze	d1d,ejcd-e`e\ d.edB ddfd/d0ZfdS )2    N)	CDLLPOINTERc_boolc_char_pc_int32c_longc_uint32c_ulongc_void_p)find_library   )_set_ssl_context_verify_mode.)
      z,Only OS X 10.8 and newer are supported, not namemacos10_16_pathreturnc                 C   sN   zt dkr|}nt| }|stt|ddW S  ty&   td|  ddw )z:Loads a CDLL by name, falling back to known path on 10.16+)r      T)	use_errnozThe library z failed to loadN)_mac_version_infor   OSErrorr   ImportError)r   r   path r   \/root/parts/websockify/install/lib/python3.10/site-packages/pip/_vendor/truststore/_macos.py
_load_cdll   s   r   Securityz6/System/Library/Frameworks/Security.framework/SecurityCoreFoundationzB/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation   r   kCFAllocatorDefaultkCFTypeArrayCallBackszError initializing ctypesresult_argsc              	   C   s   t | dkr|S d}z=t| d}t|ttj}t|t	j
}|du r=td}t||dt	j
} | s:td|j}W |durGt| n|durRt| w w |du s[|dkr`d|  }t|)z<
    Raises an error if the OSStatus value is non-zero.
    r   N   'Error copying C string from CFStringRef z8SecureTransport operation returned a non-zero OSStatus: )intr   SecCopyErrorMessageStringctypescastr   r
   r   CFStringGetCStringPtrCFConstkCFStringEncodingUTF8create_string_bufferCFStringGetCStringr   value	CFReleasesslSSLError)r"   r#   r$   error_message_cfstringerror_message_cfstring_c_void_pmessagebufferr   r   r   _handle_osstatus   s@   



r9   c                   @   s(   e Zd ZdZedZdZdZdZdZ	dS )r-   zCoreFoundation constantsi  iiiiN)
__name__
__module____qualname____doc__CFStringEncodingr.   #errSecIncompleteCertRevocationCheckerrSecHostNameMismatcherrSecCertificateExpirederrSecNotTrustedr   r   r   r   r-   	  s    r-   r1   c                 C   s   t t j| t| S N)r   CFDataCreater    len)r1   r   r   r   _bytes_to_cf_data_ref  s   rF   c                 C   s    t | }ttj|tj}|S )zi
    Given a Python binary data, create a CFString.
    The string must be CFReleased by the caller.
    )r*   r   r   CFStringCreateWithCStringr    r-   r.   )r1   c_strcf_strr   r   r   _bytes_to_cf_string  s   
rJ   cf_string_refc                 C   sZ   t | tj}|du r"td}t | |dtj}|std|j}|dur+|	d}|S )z
    Creates a Unicode string from a CFString object. Used entirely for error
    reporting.
    Yes, it annoys me quite a lot that this function is this complex.
    Nr%   r&   zutf-8)
r   r,   r-   r.   r*   r/   r0   r   r1   decode)rK   stringr8   r"   r   r   r   _cf_string_ref_to_str(  s   


rN   certsc              
   C   s   t t jdtt j}|std| D ]7}d}d}z!t|}t	t j|}t 
|| W |r4t | |r;t | q|rCt | |rKt | w w |S )zBuilds a CFArray of SecCertificateRefs from a list of DER-encoded certificates.
    Responsibility of the caller to call CoreFoundation.CFRelease on the CFArray.
    r   zUnable to allocate memory!N)r   CFArrayCreateMutabler    r*   byrefr!   MemoryErrorrF   r   SecCertificateCreateWithDataCFArrayAppendValuer2   )rO   cf_array	cert_datacf_datasec_cert_refr   r   r   _der_certs_to_cf_cert_array?  s6   



rY   ctxc              	   c   sP    | j }| j}d| _ t| tj zd V  W || _ t| | d S || _ t| | w )NF)check_hostnameverify_moder   r3   	CERT_NONE)rZ   r[   r\   r   r   r   _configure_context]  s   r^   ssl_context
cert_chainserver_hostnamec              	   C   s  d }d }d }d }zV|d ur0d }zt |d}td|}W |r&t| n|r/t| w w tdd }|}| jtj@ rht	tj
dttj}t|| t| tttB }	t||	 t|	 n
| jtj@ rrtdd }zt|}t }t||t| W |rt| n	|rt| w w | jdd}
|
rd }zt|}t|| W |rt| n|rt| w w t|d  t }t|t|}|dkrd}n|dkrd}ntd|d}|st|}| jtjkr	|tj ks|tj!kr	d}n| j"s|tj#krd}|sKd }z&t$|}t%|p&d	}t& }t'|t| t(|}||_)||_*||rJt| w w W |rTt| |r^t| d S d S |rht| |rqt| w w )
NasciiTr   z/VERIFY_CRL_CHECK_LEAF not implemented for macOS)binary_formr   Fz8Unknown result from Security.SecTrustEvaluateWithError: zCertificate verification failed)+rJ   encoder   SecPolicyCreateSSLr   r2   verify_flagsr3   VERIFY_CRL_CHECK_CHAINrP   r    r*   rQ   r!   rT   SecPolicyCreateRevocation#kSecRevocationUseAnyAvailableMethod%kSecRevocationRequirePositiveResponseVERIFY_CRL_CHECK_LEAFNotImplementedErrorrY   SecTrustRefSecTrustCreateWithCertificatesget_ca_certsSecTrustSetAnchorCertificates
CFErrorRefSecTrustEvaluateWithErrorr4   CFErrorGetCoder\   CERT_REQUIREDr-   rB   rA   r[   r@   CFErrorCopyDescriptionrN   SecTrustResultTypeSecTrustGetTrustResultSSLCertVerificationErrorverify_messageverify_code)r_   r`   ra   rO   policiestrustcf_errorcf_str_hostname
ssl_policyrevocation_policyctx_ca_certs_derctx_ca_certssec_trust_eval_result
is_trustedcf_error_codecf_error_string_refcf_error_messagesec_trust_result_typeerrr   r   r   _verify_peercerts_implj  s   












r   rC   )g
contextlibr*   platformr3   typingr   r   r   r   r   r   r   r	   r
   ctypes.utilr   _ssl_constantsr   mac_ver_mac_versiontuplemapr(   splitr   r   strr   r   r   BooleanCFIndexr>   CFDataCFStringCFArrayCFMutableArrayCFErrorCFTypeCFTypeID	CFTypeRefCFAllocatorRefOSStatusrq   	CFDataRefCFStringRef
CFArrayRefCFMutableArrayRefCFArrayCallBacksCFOptionFlagsSecCertificateRefSecPolicyRefrm   rv   SecTrustOptionFlagsrS   argtypesrestypeSecCertificateCopyDatar)   rp   !SecTrustSetAnchorCertificatesOnlySecTrustEvaluaterh   re   rn   rw   ri   rj   r2   CFGetTypeIDrG   r,   r0   rD   CFDataGetLengthCFDataGetBytePtrCFArrayCreaterP   rT   CFArrayGetCountCFArrayGetValueAtIndexrs   ru   in_dllr    r!   AttributeErrorAnyr9   errcheckr-   bytesrF   rJ   rN   listrY   contextmanager
SSLContextIteratorr^   r   r   r   r   r   <module>   s8   ,










 5