
    &`eE              	       <
   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j                    d          Z e eee                    d                              Zedk     r eded           ded                    d	ed
edef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Z1 ee          Z2 ee          Z3 ee          Z4eZ5eZ6	 e)e,gej7        _8        e2ej7        _9        e2gej:        _8        e,ej:        _9        e*egej;        _8        e-ej;        _9        e4e.gej<        _8        e*ej<        _9        e4egej=        _8        e*ej=        _9        e4 ee5          gej>        _8        e*ej>        _9        e1gej?        _8        e3ej?        _9        ee-gej@        _8        e3ej@        _9        e(e( ee4          gejA        _8        e*ejA        _9        e4 ee5          gejB        _8        e*ejB        _9        e4e_4        e5e_5        e*e_*        dZCdZDe(gejE        _8        dejE        _9        e(gejF        _8        e'ejF        _9        e)ee gejG        _8        e-ejG        _9        e-e gejH        _8        eejH        _9        e-eee gejI        _8        eejI        _9        e)eegejJ        _8        e,ejJ        _9        e,gejK        _8        eejK        _9        e,gejL        _8        eejL        _9        e) ee(          ee0gejM        _8        e.ejM        _9        e)ee0gejN        _8        e/ejN        _9        e/egejO        _8        dejO        _9        e.gejP        _8        eejP        _9        e.egejQ        _8        eejQ        _9        e+gejR        _8        eejR        _9        e+gejS        _8        e-ejS        _9        e)T                    ed          e_U         ejT        ed          e_V        e(e_(        e.e_.        e-e_-        e+e_+        n# eW$ r  ed          dw xY wde*dejX        dejX        dejX        fdZYeYejA        _Z        eYej<        _Z        eYejB        _Z         G d d          Z[de\de,fdZ]de\de"fdZ^de-dedz  fd Z_d!e`e\         de/fd"Zae jb        d#ejc        dejd        d         fd$            Ze	 d)d%ejc        d&e`e\         d'edz  ddfd(ZfdS )*    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                     	 t           dk    r|}nt          |           }|st          t          |d          S # t          $ r t	          d|  d          dw xY 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   paths      1lib/python3.11/site-packages/truststore/_macos.py
_load_cdllr      s    J (("DD%%D 	MDD)))) J J J>>>>??TIJs	   69 ASecurityz6/System/Library/Frameworks/Security.framework/SecurityCoreFoundationzB/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation   r   kCFAllocatorDefaultkCFTypeArrayCallBackszError initializing ctypesresult_argsc                 ~   t          |           dk    r|S d}	 t                              | d          }t          j        |t          j        t          j                            }t                              |t          j
                  }|St          j        d          }t                              ||dt          j
                  } | st          d          |j        }|t                              |           n"# |t                              |           w w xY w||dk    rd|  }t!          j        |          )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buffers          r   _handle_osstatusr;      sP    6{{a "!=!)!C!CFD!Q!Q +1+"FN6?$C$C+
 +
' !66+W-J
 
 ?066F#66/-	 F  IGHHHlG "-$$%;<<< "-$$%;<<<< .
 'R--UVUU
,w

s   CC< <Dc                   8    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   #errSecIncompleteCertRevocationCheckerrSecHostNameMismatcherrSecCertificateExpirederrSecNotTrusted     r   r/   r/   	  s?        "",,Z88*0'#%rG   r/   r3   c                 h    t                               t           j        | t          |                     S N)r   CFDataCreater!   len)r3   s    r   _bytes_to_cf_data_refrL     s*    &&*E3u::  rG   c                     t          j        |           }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   c_strcf_strs      r   _bytes_to_cf_stringrQ     s>    
 OE""E55*% F
 MrG   cf_string_refc                 (   t                               | t          j                  }|St	          j        d          }t                               | |dt          j                  }|st          d          |j        }||	                    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)rR   stringr:   r#   s       r   _cf_string_ref_to_strrV   (  s     11w4 F ~,T222264)F
 
  	ECDDDw''MrG   certsc                 J   t                               t           j        dt          j        t           j                            }|st          d          | D ]}d}d}	 t          |          }t          	                    t           j        |          }t           
                    ||           |rt                               |           |rt                               |           # |rt                               |           |rt                               |           w w xY 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"   MemoryErrorrL   r   SecCertificateCreateWithDataCFArrayAppendValuer4   )rW   cf_array	cert_datacf_datasec_cert_refs        r   _der_certs_to_cf_cert_arrayrb   ?  s1    22*	^9:: H
  86777 7 7	
	7+I66G#@@2G L --hEEE 2((111 7((666  2((111 7((66667 Os   AC%%;D ctxc              #      K   | j         }| j        }d| _         t          | t          j                   	 d V  || _         t          | |           d S # || _         t          | |           w xY w)NF)check_hostnameverify_moder   r5   	CERT_NONE)rc   re   rf   s      r   _configure_contextrh   ]  s|      'N/KC cm4447+$S+66666 ,$S+6666s   A A)ssl_context
cert_chainserver_hostnamec                 	   d }d }d }d }	 |d }	 t          |                    d                    }t                              d|          }|rt                              |           n=# |rt                              |           w w xY wt                              dd           }|}| j        t          j        z  rt          	                    t          j
        dt          j        t          j                            }t                              ||           t                              |           t                              t           t"          z            }	t                              ||	           t                              |	           n#| j        t          j        z  rt'          d          d }	 t)          |          }t                                          }t                              ||t          j        |                     |rt                              |           n"# |rt                              |           w w xY w|                     d          }
|
rld }	 t)          |          }t                              ||           |rt                              |           n=# |rt                              |           w w xY wt                              |d            t                                          }t                              |t          j        |                    }|dk    rd}n |dk    rd}nt          j        d|          d}|skt                              |          }| j        t          j        k    r#|t>          j         k    s|t>          j!        k    rd}n| j"        s|t>          j#        k    rd}|sd }	 t          $                    |          }tK          |          pd	}t          &                                }t          '                    |t          j        |                     t          j(        |          }||_)        ||_*        |# |rt                              |           w w xY w	 |rt                              |           |rt                              |           d S d S # |rt                              |           |rt                              |           w w xY w)
NasciiTr   z/VERIFY_CRL_CHECK_LEAF not implemented for macOS)binary_formr   Fz8Unknown result from Security.SecTrustEvaluateWithError: zCertificate verification failed)+rQ   encoder   SecPolicyCreateSSLr   r4   verify_flagsr5   VERIFY_CRL_CHECK_CHAINrY   r!   r,   rZ   r"   r]   SecPolicyCreateRevocation#kSecRevocationUseAnyAvailableMethod%kSecRevocationRequirePositiveResponseVERIFY_CRL_CHECK_LEAFNotImplementedErrorrb   SecTrustRefSecTrustCreateWithCertificatesget_ca_certsSecTrustSetAnchorCertificates
CFErrorRefSecTrustEvaluateWithErrorr6   CFErrorGetCoderf   CERT_REQUIREDr/   rE   rD   re   rC   CFErrorCopyDescriptionrV   SecTrustResultTypeSecTrustGetTrustResultSSLCertVerificationErrorverify_messageverify_code)ri   rj   rk   rW   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errs                      r   _verify_peercerts_implr   j  sT   
 EHEHB,&"O>"5o6L6LW6U6U"V"V%88OO
" >",,_=== # >",,_====> "44T4@@J#c&@@ 	Y%::2^ABB H
 --h
CCC$$Z000 ( B B378! ! --h8IJJJ$$%67777%(AA 	Y%&WXXX	0/
;;E ((**E33xe!4!4    0((///  0((////0 0;/G/G 0H 0
 0
  		@L;::FF66ulKKK ;",,\:::   ;",,\::::; 225$???!,,.. ( B B6<))!
 !

 !A%%JJ"a''JJ,dK`dd    	"*99(CCM
 &#*;;;!999 G$DDD!

."!W%CCC!
  	B"&B&4&K&KH&U&U# **=>> 98 ! )1(C(C(E(E%//6<(=>>   23CDD%5""/	& B",,-@AAAAB/	B6  	/$$X... 	,$$U+++++	, 	,  	/$$X... 	,$$U++++	,sh   S =A) S )BD-S 6AH) S )IS &*J- S -KD S BQ" "RS ;S<rI   )g
contextlibr,   platformr5   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CFIndexrA   CFDataCFStringCFArrayCFMutableArrayCFErrorCFTypeCFTypeID	CFTypeRefCFAllocatorRefOSStatusr|   	CFDataRefCFStringRef
CFArrayRefCFMutableArrayRefCFArrayCallBacksCFOptionFlagsSecCertificateRefSecPolicyRefrx   r   SecTrustOptionFlagsr\   argtypesrestypeSecCertificateCopyDatar+   r{   !SecTrustSetAnchorCertificatesOnlySecTrustEvaluaters   rp   ry   r   rt   ru   r4   CFGetTypeIDrN   r.   r2   rJ   CFDataGetLengthCFDataGetBytePtrCFArrayCreaterY   r]   CFArrayGetCountCFArrayGetValueAtIndexr~   r   in_dllr!   r"   AttributeErrorAnyr;   errcheckr/   bytesrL   rQ   rV   listrb   contextmanager
SSLContextIteratorrh   r   rF   rG   r   <module>r      s         



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 % $ $ $ $ $ 8 8 8 8 8 8x!!!$E##c<#5#5c#:#:;;<< w
+d7H7KddN_`aNbdd  
JS J3 J4 J J J J" :H  H 
 
 	

	GFOO	WW
GFOO	ghWW
GN++  GH%% wx  gh  y=6Di5PH)24EH)10A/BH#,.7H#+3;X2FH&/1<H&.7BJ6OH*35=H*2;F:PH.79AH.6*5ww?Q7R7R)SH&(0H%3@/H&/1=H&.,3[+AH(*6H' 	8H+4
 7?H+3 	"##0H#, /7H#+&H"4H H*+',-))2N%'+N$+4+N')1N& 	9N,5
 8CN,45@BR4SN(13;N(0 		2N%. 17N%-,:Hg+NN(*3N'/8kN"+-4N"*09{N#,.6N#+ 			-N ) ,6N ( 	4N'0
 3DN'/2CX1NN%.04N%-/9lN"+-4N"*6@'5JN)24<N)1.8\N!*,3N!)6@\N)24?N)1)7)>)>-* *N& ,;8?/, ,N(  )N *N!,N *N = = =
+1
2
2<=2 X 2 &* 2 FJ 2 6: 2  2  2  2 j 4D ' 02B & /+;  (        9    u      t    .tE{ 7H    < 	7CN 	7vt/D 	7 	7 	7 	7 #'K, K,K,UK, 4ZK, 
	K, K, K, K, K, K,s   =LQ Q#