o
    ïT·jP  ã                
   @   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 › �ƒ‚ed	kZ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%ƒZ0eZ1eZ2eeƒZ3eeƒZ4eeƒZ5eZ6eZ7�z*e*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 n eX�yõ ZY zedeY› �ƒd‚dZY[Yww e�rze5ee,ƒgejZ_9eejZ_:W n eX�y ZY zedeY› �ƒd‚dZY[Yww de+dej[dej[dej[fdd„Z\e\ejA_]e\ej=_]e\ej>_]e\ejB_]e\ejC_]G dd„ dƒZ^de_de-fd d!„Z`de_de#fd"d#„Zad$e.dedB fd%d&„Zbd'ece_ de0fd(d)„Zde jed*ejfdejgd fd+d,„ƒZh	d7d-ejfd.ece_ d/edB ddfd0d1„Zid-ejfd2ej[ddfd3d4„Zjd-ejfd2ej[ddfd5d6„ZkdS )8é    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   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   úT/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/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@   ÿÿ
ü€
€ÿÿ

r:   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  iÍ÷þÿiî÷þÿi÷þÿiýöþÿN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚCFStringEncodingr/   Ú#errSecIncompleteCertRevocationCheckÚerrSecHostNameMismatchÚerrSecCertificateExpiredÚerrSecNotTrustedr   r   r   r   r.     s    r.   r2   c                 C   s   t  t j| t| ƒ¡S ©N)r   ÚCFDataCreater!   Úlen)r2   r   r   r   Ú_bytes_to_cf_data_ref&  s   ÿrG   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/   )r2   Úc_strÚcf_strr   r   r   Ú_bytes_to_cf_string,  s   
ýrK   Ú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+   r0   r1   r   r2   Údecode)rL   Ústringr9   r#   r   r   r   Ú_cf_string_ref_to_str:  s   ÿ

ÿ
rO   Ú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 )z¦Builds 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"   ÚMemoryErrorrG   r   ÚSecCertificateCreateWithDataÚCFArrayAppendValuer3   )rP   Úcf_arrayÚ	cert_dataÚcf_dataÚsec_cert_refr   r   r   Ú_der_certs_to_cf_cert_arrayQ  s6   
ýÿ

€ý
ÿrZ   Úctxc              	   c   sP   � | j }| j}d| _ t| tjƒ zd V  W || _ t| |ƒ d S || _ t| |ƒ w )NF)Úcheck_hostnameÚverify_moder   r4   Ú	CERT_NONE)r[   r\   r]   r   r   r   Ú_configure_contexto  s   €ÿr_   Ússl_contextÚ
cert_chainÚserver_hostnamec              	   C   sì  d }d }d }zß|d ur0| j 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 |r�t |¡ n	|r˜t |¡ w w | jdd�}	|	rÀd }
zt|	ƒ}
t ||
¡ W |
r¶t |
¡ n	|
r¿t |
¡ w w t |d¡ trÎt| |ƒ nt| |ƒ W |rÛt |¡ |rät |¡ d S d S |rít |¡ |rõt |¡ w w )NÚasciiTr   z/VERIFY_CRL_CHECK_LEAF not implemented for macOS)Úbinary_formF)r\   rK   Úencoder   ÚSecPolicyCreateSSLr   r3   Úverify_flagsr4   ÚVERIFY_CRL_CHECK_CHAINrQ   r!   r+   rR   r"   rU   ÚSecPolicyCreateRevocationÚ#kSecRevocationUseAnyAvailableMethodÚ%kSecRevocationRequirePositiveResponseÚVERIFY_CRL_CHECK_LEAFÚNotImplementedErrorrZ   Ú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)r`   ra   rb   rP   ÚpoliciesÚtrustÚcf_str_hostnameÚ
ssl_policyÚrevocation_policyÚctx_ca_certs_derÚctx_ca_certsr   r   r   Ú_verify_peercerts_impl|  s˜   
€ÿÿ
ý
ÿÿÿ
€ÿÿÿ
€ÿÿ
€
ÿþ
ÿr}   Úsec_trust_refc              	   C   sž   t  ¡ }t  |t |¡¡ zt|jƒ}W n ttfy!   d}Y nw | j	t
jkrK|dvrMddddddd	œ}| |d
|› �¡}t
 |¡}||_||_|‚dS dS )z€Verify 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+   rR   r)   r2   Ú
ValueErrorÚ	TypeErrorr]   r4   ÚCERT_REQUIREDÚgetÚSSLCertVerificationErrorÚverify_messageÚverify_code)r`   r~   Úsec_trust_result_typeÚsec_trust_result_type_as_intÚ sec_trust_result_type_to_messageÚerror_messageÚerrr   r   r   ru   Õ  s4   ÿø
þ
èru   c           
   	   C   sæ   t  ¡ }t |t |¡¡}|dkrd}n|dkrd}nt d|›�¡‚d}|s>t  |¡}| j	tj
kr>|tjks<|tjkr>d}|sqd}z%t  |¡}t|ƒpMd}t ¡ }t |t |¡¡ t |¡}	||	_||	_|	‚|rpt  |¡ w w 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+   rR   r4   r5   ÚCFErrorGetCoder]   r‰   r.   rC   rB   ÚCFErrorCopyDescriptionrO   r…   ÚSecTrustGetTrustResultr‹   rŒ   r�   r3   )
r`   r~   Úcf_errorÚsec_trust_eval_resultÚ
is_trustedÚcf_error_codeÚcf_error_string_refÚcf_error_messagerŽ   r’   r   r   r   rt     sJ   
ÿÿ



þ
ÿ
ÿért   rD   )lÚ
contextlibr+   Úplatformr4   Útypingr   r   r   r   r   r   r   r	   r
   Úctypes.utilr   Ú_ssl_constantsr   Úmac_verÚ_mac_versionÚtupleÚmapr)   Úsplitr   r   rs   Ústrr   r   r   ÚBooleanÚCFIndexr?   ÚCFDataÚCFStringÚCFArrayÚCFMutableArrayÚCFErrorÚCFTypeÚCFTypeIDÚ	CFTypeRefÚCFAllocatorRefÚOSStatusr“   Ú	CFDataRefÚCFStringRefÚ
CFArrayRefÚCFMutableArrayRefÚCFArrayCallBacksÚCFOptionFlagsÚSecCertificateRefÚSecPolicyRefrn   r…   ÚSecTrustOptionFlagsrT   ÚargtypesÚrestypeÚSecCertificateCopyDatar*   rq   rr   ri   rf   ro   r—   r†   rj   rk   r3   ÚCFGetTypeIDrH   r-   r1   rE   ÚCFDataGetLengthÚCFDataGetBytePtrÚCFArrayCreaterQ   rU   ÚCFArrayGetCountÚCFArrayGetValueAtIndexr•   r–   Úin_dllr!   r"   ÚAttributeErrorÚer”   ÚAnyr:   Úerrcheckr.   ÚbytesrG   rK   rO   ÚlistrZ   ÚcontextmanagerÚ
SSLContextÚIteratorr_   r}   ru   rt   r   r   r   r   Ú<module>   st   ,ÿÿþ

ýþþ

ýü

üý


ÿÿ
€ÿþ€ÿ 5ýÿþý
üYÿÿ
þ,ÿÿþ