U
    8hP                  
   @   sP  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 reded   ded  ed	kZeeed
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%Z0eZ1eZ2eeZ3eeZ4eeZ5eZ6eZ7zTe*e-gej8_9e3ej8_:e3gej;_9e-ej;_:e+egej<_9e.ej<_:e5e/gej=_9e+ej=_:e5egej>_9e+ej>_:e2gej?_9e4ej?_:ee.gej@_9e4ej@_:e)e)ee5gejA_9e+ejA_:e5ee6gejB_9e+ejB_:e5ee6gejC_9e+ejC_:e5e_5e6e_6e+e_+dZDdZEe)gejF_9dejF_:e)gejG_9e(ejG_:e*ee!gejH_9e.ejH_:e.e!gejI_9eejI_:e.ee e!gejJ_9eejJ_:e*ee gejK_9e-ejK_:e-gejL_9e ejL_:e-gejM_9eejM_:e*ee)e e1gejN_9e/ejN_:e*e e1gejO_9e0ejO_:e0egejP_9dejP_:e/gejQ_9e ejQ_:e/e gejR_9eejR_:e,gejS_9e ejS_:e,gejT_9e.ejT_:e*Uede_VeUede_We)e_)e/e_/e.e_.e,e_,W n4 eXk
r ZY zedeY dW 5 dZY[YX Y nX erJze5ee,gejZ_9eejZ_:W n4 eXk
rH ZY zedeY dW 5 dZY[YX Y nX e+ej[ej[ej[dddZ\e\ejA_]e\ej=_]e\ej>_]e\ejB_]e\ejC_]G dd dZ^e_e-dddZ`e_e#dddZae.edB d d!d"Zbece_ e0d#d$d%Zde jeejfejgd d&d'd(Zhd1ejfece_ edB dd)d*d+Ziejfej[dd,d-d.Zjejfej[dd,d/d0Zkd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 )r      )namemacos10_16_pathreturnc                 C   sX   z,t dkr|}nt| }|s tt|ddW S  tk
rR   td|  ddY nX dS )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   [C:\Users\jeffr\AppData\Local\Temp\pip-install-hfyjq797\pip\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_argsr   c              	   C   s   t | dkr|S d}zht| d}t|ttj}t	|t
j}|dkrztd}t||dt
j} | sttd|j}W 5 |dk	rt| X |dks|dkrd|  }t|dS )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   	CFReleaser   SecCopyErrorMessageStringctypescastr   r
   CFStringGetCStringPtrCFConstkCFStringEncodingUTF8create_string_bufferCFStringGetCStringr   valuesslSSLError)r#   r$   r%   Zerror_message_cfstringZerror_message_cfstring_c_void_pmessagebufferr   r   r   _handle_osstatus   s:     
 


r8   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__CFStringEncodingr0   Z#errSecIncompleteCertRevocationCheckZerrSecHostNameMismatcherrSecCertificateExpirederrSecNotTrustedr   r   r   r   r/     s   r/   )r3   r   c                 C   s   t t j| t| S )N)r   CFDataCreater!   len)r3   r   r   r   _bytes_to_cf_data_ref&  s
      rB   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/   r0   )r3   Zc_strZcf_strr   r   r   _bytes_to_cf_string,  s    
rD   )cf_string_refr   c                 C   sZ   t | tj}|dkrDtd}t | |dtj}|s>td|j}|dk	rV|	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/   r0   r,   r1   r2   r   r3   decode)rE   stringr7   r#   r   r   r   _cf_string_ref_to_str:  s$     
   
rH   )certsr   c              
   C   s   t t jdtt j}|s$td| D ]R}d}d}z&t|}t	
t j|}t || W 5 |rjt | |rxt | X q(|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"   MemoryErrorr*   rB   r   SecCertificateCreateWithDataCFArrayAppendValue)rI   Zcf_arrayZ	cert_dataZcf_dataZsec_cert_refr   r   r   _der_certs_to_cf_cert_arrayQ  s,    
 
rO   )ctxr   c              	   c   s@   | j }| j}d| _ t| tj z
d V  W 5 || _ t| | X d S )NF)check_hostnameverify_moder   r4   	CERT_NONE)rP   rQ   rR   r   r   r   _configure_contexto  s    
rT   )ssl_context
cert_chainserver_hostnamer   c              	   C   s  d }d }d }zv|d k	rT| jrTd }zt|d}td|}W 5 |rPt | X ntdd }|}| jtj	@ rt 
t jdtt j}t || t | tttB }t || t | n| jtj@ rtdd }z(t|}t }t||t| W 5 |rt | X | jdd}	|	rZd }
zt|	}
t||
 W 5 |
rXt |
 X t|d trxt| | n
t| | W 5 |rt | |rt | X d S )NasciiTr   z/VERIFY_CRL_CHECK_LEAF not implemented for macOS)binary_formF)r   r*   rQ   rD   encoder   SecPolicyCreateSSLverify_flagsr4   VERIFY_CRL_CHECK_CHAINrJ   r!   r,   rK   r"   rN   SecPolicyCreateRevocation#kSecRevocationUseAnyAvailableMethod%kSecRevocationRequirePositiveResponseVERIFY_CRL_CHECK_LEAFNotImplementedErrorrO   SecTrustRefSecTrustCreateWithCertificatesget_ca_certsSecTrustSetAnchorCertificates!SecTrustSetAnchorCertificatesOnly _is_macos_version_10_14_or_later"_verify_peercerts_impl_macos_10_14"_verify_peercerts_impl_macos_10_13)rU   rV   rW   rI   ZpoliciestrustZcf_str_hostnameZ
ssl_policyZrevocation_policyZctx_ca_certs_derZctx_ca_certsr   r   r   _verify_peercerts_impl|  sv    

  
rl   )rU   sec_trust_refr   c              	   C   s   t  }t |t| zt|j}W n ttfk
rD   d}Y nX | j	t
jkr|dkrddddddd	}||d
| }t
|}||_||_|dS )zVerify using 'SecTrustEvaluate' API for macOS 10.13 and earlier.
    macOS 10.14 added the 'SecTrustEvaluateWithError' API.
    )r      zInvalid trust result typezUser confirmation requiredz.User specified that certificate is not trustedz"Recoverable trust failure occurredzFatal trust failure occurredz0Other error occurred, certificate may be revoked)r      r             zUnknown trust result: N)r   SecTrustResultTypeSecTrustEvaluater,   rK   r)   r3   
ValueError	TypeErrorrR   r4   CERT_REQUIREDgetSSLCertVerificationErrorverify_messageverify_code)rU   rm   sec_trust_result_typeZsec_trust_result_type_as_intZ sec_trust_result_type_to_messageerror_messageerrr   r   r   rj     s2    



rj   c           
   	   C   s   t  }t|t|}|dkr(d}n|dkr6d}ntd|d}|s|t |}| j	tj
kr||tjksx|tjkr|d}|sd}zNt |}t|pd}t }t|t| t|}	||	_||	_|	W 5 |rt | X dS )z>Verify using 'SecTrustEvaluateWithError' API for macOS 10.14+.r   Tr   Fz8Unknown result from Security.SecTrustEvaluateWithError: NzCertificate verification failed)r   
CFErrorRefr   SecTrustEvaluateWithErrorr,   rK   r4   r5   CFErrorGetCoderR   rx   r/   r?   r>   r*   CFErrorCopyDescriptionrH   rt   SecTrustGetTrustResultrz   r{   r|   )
rU   rm   Zcf_errorZsec_trust_eval_resultZ
is_trustedZcf_error_codeZcf_error_string_refZcf_error_messager}   r   r   r   r   ri     sN     

 
ri   )N)l
contextlibr,   platformr4   typingr   r   r   r   r   r   r   r	   r
   Zctypes.utilr   Z_ssl_constantsr   mac_verZ_mac_versiontuplemapr)   splitr   r   rh   strr   r   r   BooleanZCFIndexr=   ZCFDataZCFStringZCFArrayZCFMutableArrayZCFErrorZCFTypeZCFTypeIDZ	CFTypeRefZCFAllocatorRefZOSStatusr   Z	CFDataRefZCFStringRefZ
CFArrayRefZCFMutableArrayRefZCFArrayCallBacksZCFOptionFlagsZSecCertificateRefZSecPolicyRefrc   rt   ZSecTrustOptionFlagsrM   argtypesrestypeZSecCertificateCopyDatar+   rf   rg   r^   r[   rd   r   ru   r_   r`   r*   ZCFGetTypeIDrC   r.   r2   r@   ZCFDataGetLengthZCFDataGetBytePtrZCFArrayCreaterJ   rN   ZCFArrayGetCountZCFArrayGetValueAtIndexr   r   in_dllr!   r"   AttributeErrorer   ZAnyr8   errcheckr/   bytesrB   rD   rH   listrO   contextmanager
SSLContextIteratorrT   rl   rj   ri   r   r   r   r   <module>   s^  , 








  
""5 Z - 