o
    Œõ±jb  ã                   @   s¤   d dl mZmZmZmZ d dlZd dlZddlmZ ddl	m
Z
mZ ddlmZmZ ddlmZmZmZ ejd	k r;eZd
dgZdd„ Zddd
„Zdd„ Zdd„ ZdS )é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )ÚCertificate)ÚnewÚunwrapé   )ÚCoreFoundationÚ	CFHelpers)ÚSecurityÚSecurityConstÚhandle_sec_error)é   Úextract_from_systemÚsystem_pathc                   C   s   d S )N© r   r   r   ú‹/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/oscrypto/_mac/trust_list.pyr      s   Fc                 C   s˜  t tdƒ}t |¡}t|ƒ t|ƒ}i }i }d}tƒ tƒ f}t |¡}	td|	ƒD ]}
t 	||
¡}t
|ƒ\}}|||< q(t |¡ tjtjfD ]Ü}t tdƒ}t ||¡}|tjkrYqFt|ƒ t|ƒ}t |¡}	td|	ƒD ]±}
t 	||
¡}t tdƒ}t |||¡}|tjkr…qk|tjkr t
|ƒ\}}||v rŸt| || dƒ ||= qkt|ƒ t|ƒ}tƒ }tƒ }t |¡}td|ƒD ]2}t 	||¡}t |¡}| di ¡ d|¡}| dd¡}|dkoÜ|d	k}|rå| |¡ q¸| |¡ q¸t
|ƒ\}}||v r||v rt| || d
ƒ ||= n||v rt|gƒ}||f||< t |¡ qkt |¡ qFg }|D ]"}|s4t| || dƒ | ||¡}| || |d |d f¡ q'|S )aÇ  
    Extracts trusted CA certificates from the OS X trusted root keychain.

    :param cert_callback:
        A callback that is called once for each certificate in the trust store.
        It should accept two parameters: an asn1crypto.x509.Certificate object,
        and a reason. The reason will be None if the certificate is being
        exported, otherwise it will be a unicode string of the reason it won't.

    :param callback_only_on_failure:
        A boolean - if the callback should only be called when a certificate is
        not exported.

    :raises:
        OSError - when an error is returned by the OS crypto library

    :return:
        A list of 3-element tuples:
         - 0: a byte string of a DER-encoded certificate
         - 1: a set of unicode strings that are OIDs of purposes to trust the
              certificate for
         - 2: a set of unicode strings that are OIDs of purposes to reject the
              certificate for
    zCFArrayRef *z2.5.29.37.0r   zinvalid trust settingsÚkSecTrustSettingsPolicyÚSecPolicyOidÚkSecTrustSettingsResultr
   r   zexplicitly distrustedN)r   r   r   ÚSecTrustCopyAnchorCertificatesr   r	   ÚsetÚCFArrayGetCountÚrangeÚCFArrayGetValueAtIndexÚ_cert_detailsÚ	CFReleaser   ÚkSecTrustSettingsDomainUserÚkSecTrustSettingsDomainAdminÚ SecTrustSettingsCopyCertificatesÚerrSecNoTrustSettingsÚ!SecTrustSettingsCopyTrustSettingsÚerrSecItemNotFoundÚerrSecInvalidTrustSettingsÚ_cert_callbackr   Úcf_dictionary_to_dictÚgetÚaddÚappend)Úcert_callbackÚcallback_only_on_failureÚcerts_pointer_pointerÚresÚcerts_pointerÚcertificatesÚ
trust_infoÚall_purposesÚdefault_trustÚlengthÚindexÚcert_pointerÚder_certÚ	cert_hashÚdomainÚ#cert_trust_settings_pointer_pointerÚcert_trust_settings_pointerÚtrust_settings_pointer_pointerÚtrust_settings_pointerÚ
trust_oidsÚreject_oidsÚsettings_lengthÚsettings_indexÚsettings_dict_entryÚsettings_dictÚ
policy_oidÚtrust_resultÚshould_trustÚoutputÚcert_trust_infor   r   r   r      s”   










ý



ý€

 c                 C   s   | sdS | t  |¡|ƒ dS )aY  
    Constructs an asn1crypto.x509.Certificate object and calls the export
    callback

    :param callback:
        The callback to call

    :param der_cert:
        A byte string of the DER-encoded certificate

    :param reason:
        None if cert is being exported, or a unicode string of the reason it
        is not being exported
    N)r   Úload)Úcallbackr7   Úreasonr   r   r   r&   £   s   r&   c              	   C   s\   d}z t  | ¡}t |¡}t |¡ ¡ }||fW |dur"t |¡ S S |dur-t |¡ w w )a  
    Return the certificate and a hash of it

    :param cert_pointer:
        A SecCertificateRef

    :return:
        A 2-element tuple:
         - [0]: A byte string of the SHA1 hash of the cert
         - [1]: A byte string of the DER-encoded contents of the cert
    N)	r   ÚSecCertificateCopyDatar   Úcf_data_to_bytesÚhashlibÚsha1Údigestr   r   )r6   Údata_pointerr7   r8   r   r   r   r   ¸   s   


ÿÿr   )NF)Ú
__future__r   r   r   r   rN   ÚsysÚ_asn1r   Ú_ffir   r	   Ú_core_foundationr   r   Ú	_securityr   r   r   Úversion_infoÚxranger   Ú__all__r   r   r&   r   r   r   r   r   Ú<module>   s"   
þ
 
