
    thP              	       D   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j/                  d                  Zedk  r eded     ded          ed	k\  Z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%      Z0eZ1eZ2 ee      Z3 ee      Z4 ee      Z5eZ6eZ7	 e*e-gejp                  _9        e3ejp                  _:        e3gejv                  _9        e-ejv                  _:        e+egejx                  _9        e.ejx                  _:        e5e/gejz                  _9        e+ejz                  _:        e5egej|                  _9        e+ej|                  _:        e2gej~                  _9        e4ej~                  _:        ee.gej                  _9        e4ej                  _:        e)e) ee5      gej                  _9        e+ej                  _:        e5 ee6      gej                  _9        e+ej                  _:        e5 ee6      gej                  _9        e+ej                  _:        e5e_5        e6e_6        e+e_+        dZDdZEe)gej                  _9        dej                  _:        e)gej                  _9        e(ej                  _:        e*ee!gej                  _9        e.ej                  _:        e.e!gej                  _9        eej                  _:        e.ee e!gej                  _9        eej                  _:        e*ee gej                  _9        e-ej                  _:        e-gej                  _9        e ej                  _:        e-gej                  _9        eej                  _:        e* ee)      e e1gej                  _9        e/ej                  _:        e*e e1gej                  _9        e0ej                  _:        e0egej                  _9        dej                  _:        e/gej                  _9        e ej                  _:        e/e gej                  _9        eej                  _:        e,gej                  _9        e ej                  _:        e,gej                  _9        e.ej                  _:        e*j                  ed      e_V         ej                  ed      e_W        e)e_)        e/e_/        e.e_.        e,e_,        er+	 e5 ee,      gej                  _9        eej                  _:        de+dej                  dej                  dej                  fdZ\e\ej                  _]        e\ejz                  _]        e\ej|                  _]        e\ej                  _]        e\ej                  _]         G d d      Z^de_de-fdZ`de_de#fdZad e.dedz  fd!Zbd"ece_   de0fd#Zde j                  d$ej                  dej                  d   fd%       Zh	 d-d&ej                  d'ece_   d(edz  ddfd)Zid&ej                  d*ej                  ddfd+Zjd&ej                  d*ej                  ddfd,Zky# eX$ rZY edeY       ddZY[Yww xY w# eX$ rZY edeY       ddZY[Yww xY w).    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                     	 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      BD:\jyotish\venv\Lib\site-packages\pip/_vendor/truststore/_macos.py
_load_cdllr      s]    J ("D%DMDD)) JLo>?TIJs	   +. ASecurityz6/System/Library/Frameworks/Security.framework/SecurityCoreFoundationzB/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation   r   kCFAllocatorDefaultkCFTypeArrayCallBackszError initializing ctypes: result_argsc                 b   t        |       dk(  r|S d}	 t        j                  | d      }t        j                  |t        j
                  t        j                              }t        j                  |t        j                        }|Tt        j                  d      }t        j                  ||dt        j                        } | st        d      |j                  }|t        j                  |       	 ||dk(  rd|  }t!        j"                  |      # |t        j                  |       w w xY w)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<      s%    6{a "!=!)!C!CFD!Q +1++"FNN6??$C+
' !66+W-J-J
 ?006F#66/--	F GHHllG "-$$%;< 'R-LVHU
,,w
 "-$$%;< .s   CD D.c                   0    e Zd ZdZ ed      ZdZdZdZdZ	y)r0   zCoreFoundation constantsi  iiiiN)
__name__
__module____qualname____doc__CFStringEncodingr1   #errSecIncompleteCertRevocationCheckerrSecHostNameMismatcherrSecCertificateExpirederrSecNotTrusted     r   r0   r0     s(    ",Z8*0'#%rH   r0   r4   c                 ^    t         j                  t         j                  | t        |             S N)r    CFDataCreater"   len)r4   s    r   _bytes_to_cf_data_refrM   &  s&    &&**E3u: rH   c                     t        j                  |       }t        j                  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"   r0   r1   )r4   c_strcf_strs      r   _bytes_to_cf_stringrR   ,  s>    
 OOE"E55**%%F
 MrH   cf_string_refc                     t         j                  | t        j                        }|Tt	        j
                  d      }t         j                  | |dt        j                        }|st        d      |j                  }||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.
    r(   r)   zutf-8)
r    r/   r0   r1   r-   r2   r3   r   r4   decode)rS   stringr;   r$   s       r   _cf_string_ref_to_strrW   :  s     11w44F ~,,T22264)F)F
 CDDw'MrH   certsc                    t         j                  t         j                  dt        j                  t         j
                              }|st        d      | D ]{  }d}d}	 t        |      }t        j                  t         j                  |      }t         j                  ||       |rt         j                  |       |sgt         j                  |       } |S # |rt         j                  |       |rt         j                  |       w w xY w)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#   MemoryErrorrM   r   SecCertificateCreateWithDataCFArrayAppendValuer5   )rX   cf_array	cert_datacf_datasec_cert_refs        r   _der_certs_to_cf_cert_arrayrc   Q  s     22**	^99:H
 677	
	7+I6G#@@22GL --hE((1((6  O ((1((6 s   AC1Dctxc              #      K   | j                   }| j                  }d| _         t        | t        j                         	 d  || _         t        | |       y # || _         t        | |       w xY ww)NF)check_hostnameverify_moder   r6   	CERT_NONE)rd   rf   rg   s      r   _configure_contextri   o  s_     ''N//KC cmm47+$S+6 ,$S+6s   :A-A A-A**A-ssl_context
cert_chainserver_hostnamec                 >   d }d }d }	 |W| j                   rKd }	 t        |j                  d            }t        j	                  d|      }|r,t
        j                  |       nt        j	                  dd       }|}| j                  t        j                  z  rt
        j                  t
        j                  dt        j                  t
        j                              }t
        j                  ||       t
        j                  |       t        j!                  t"        t$        z        }t
        j                  ||       t
        j                  |       n(| j                  t        j&                  z  rt)        d      d }	 t+        |      }t        j-                         }t        j/                  ||t        j                  |             |rt
        j                  |       	 | j1                  d      }	|	r<d }
	 t+        |	      }
t        j3                  ||
       |
rt
        j                  |
       	 t        j5                  |d       t6        rt9        | |       nt;        | |       |rt
        j                  |       |rt
        j                  |       y y # |rt
        j                  |       w w xY w# |rt
        j                  |       w w xY w# |
rt
        j                  |
       w w xY w# |rt
        j                  |       |rt
        j                  |       w w xY w)NasciiTr   z/VERIFY_CRL_CHECK_LEAF not implemented for macOS)binary_formF)rf   rR   encoder   SecPolicyCreateSSLr    r5   verify_flagsr6   VERIFY_CRL_CHECK_CHAINrZ   r"   r-   r[   r#   r^   SecPolicyCreateRevocation#kSecRevocationUseAnyAvailableMethod%kSecRevocationRequirePositiveResponseVERIFY_CRL_CHECK_LEAFNotImplementedErrorrc   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)rj   rk   rl   rX   policiestrustcf_str_hostname
ssl_policyrevocation_policyctx_ca_certs_derctx_ca_certss              r   _verify_peercerts_implr   |  s   
 EHEN, &;+E+E"O>"5o6L6LW6U"V%88O
"",,_=!44T4@J##c&@&@@%::22^AABH
 --h
C$$Z0 ( B B378! --h8IJ$$%67%%(A(AA%&WXX	0/
;E ((*E33xe!4 ((/ 0;/G/G 0H 0
 L;:;KL66ulK",,\: 	225%@
 ,.{EB.{EB$$X.$$U+ I #",,_= #L ((/   ",,\:   $$X.$$U+ sU   K+ 0J 	D4K+ >A	J1 .K+ 6!K AK+ J..K+ 1KK+ K((K+ +1Lsec_trust_refc                    t         j                         }t         j                  |t        j                  |             	 t        |j                        }| j                  t        j                  k(  rH|dvrCddddddd	}|j                  |d
|       }t        j                  |      }||_        ||_        |yy# t        t        f$ r d}Y yw xY w)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-   r[   r+   r4   
ValueError	TypeErrorrg   r6   CERT_REQUIREDgetSSLCertVerificationErrorverify_messageverify_code)rj   r   sec_trust_result_typesec_trust_result_type_as_int sec_trust_result_type_to_messageerror_messageerrs          r   r   r     s     %779mV\\:O-PQ*'*+@+F+F'G$ 	3#4#44(6 ++?3-A	,
( 9<<($%A$BC

 **=9*6	/ 7 	5 	" *')$*s   B: :CCc                    t         j                         }t        j                  |t	        j
                  |            }|dk(  rd}n |dk(  rd}nt        j                  d|      d}|sZt         j                  |      }| j                  t        j                  k7  r(|t        j                  k(  s|t        j                  k(  rd}|sd}	 t         j                  |      }t        |      xs d}t        j!                         }t        j#                  |t	        j
                  |             t        j$                  |      }	||	_        ||	_        |	y# |rt         j+                  |       w w xY w)z>Verify using 'SecTrustEvaluateWithError' API for macOS 10.14+.r   Tr   Fz8Unknown result from Security.SecTrustEvaluateWithError: NzCertificate verification failed)r    
CFErrorRefr   SecTrustEvaluateWithErrorr-   r[   r6   r7   CFErrorGetCoderg   r   r0   rF   rE   CFErrorCopyDescriptionrW   r   SecTrustGetTrustResultr   r   r   r5   )
rj   r   cf_errorsec_trust_eval_result
is_trustedcf_error_codecf_error_string_refcf_error_messager   r   s
             r   r   r     si    ((*H$>>v||H-
 !
	!	#
llFG\F_`
 	
 M&55h?
 ""c&7&77W555 @ @@J "	>"0"G"G"Q &&9: 54  %-$?$?$A!++v||,AB ../?@C!1C+COI+ . #(()<= #s   	BE E*rJ   )l
contextlibr-   platformr6   typingr   r   r   r   r   r   r	   r
   r   ctypes.utilr   _ssl_constantsr   mac_ver_mac_versiontuplemapr+   splitr   r   r~   strr   r   r    BooleanCFIndexrB   CFDataCFStringCFArrayCFMutableArrayCFErrorCFTypeCFTypeID	CFTypeRefCFAllocatorRefOSStatusr   	CFDataRefCFStringRef
CFArrayRefCFMutableArrayRefCFArrayCallBacksCFOptionFlagsSecCertificateRefSecPolicyRefry   r   SecTrustOptionFlagsr]   argtypesrestypeSecCertificateCopyDatar,   r|   r}   rt   rq   rz   r   r   ru   rv   r5   CFGetTypeIDrO   r/   r3   rK   CFDataGetLengthCFDataGetBytePtrCFArrayCreaterZ   r^   CFArrayGetCountCFArrayGetValueAtIndexr   r   in_dllr"   r#   AttributeErrorer   Anyr<   errcheckr0   bytesrM   rR   rW   listrc   contextmanager
SSLContextIteratorri   r   r   r   rG   rH   r   <module>r      s      
 
 
 
 % 8x!!$#c<#5#5c#:;< w

67H7K6LAN_`aNbMcd  $5#@  JS J3 J4 J" H H
 
 	

	FO	W
FO	hW
N+  H% x h  |C6Di5PH))24EH))10A/BH##,.7H##+3;X2FH&&/1<H&&.7BJ6OH**35=H**2;F:PH..79AH..63@/H&&/1=H&&.,3[+AH(*6H' 	8H++4
 7?H++3 	"#0H##, /7H##+ 	"#*H& )1H%&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 $GJ7
**3 6<**2
2 X 2 &** 2 FJJ 2 6:: 2 j 4D ' ' 02B & & /6F * * 3+;   (%5   "  9 u   t .tE{ 7H < 	7CNN 	7vt/D 	7 	7 #'V,V,UV, 4ZV, 
	V,r))06

)	)X:>:>06

:>	:>c	  C
3A37
8dBC  G7s;<$FGs1   O?Y0 *Z	 0Z5ZZ	ZZZ