o
    .jY                     @  sf  U d Z ddlmZ ddlZddlZddlm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 dd	lmZ dd
lmZmZ e
riddl	mZmZmZ ddlmZ edZedddZG dd deeef ZdZded< g Z ded< e Z!dZ"ded< e"a#ded< d8d d!Z$d9d$d%Z%e$d:d;d)d*Ze$d:d<d,d-Z&d:d=d.d/Z'd>d2d3Z(d:d?d5d6Z)d7Z*dS )@z
the Wassima library is a simple library.
It aims to provide a pythonic way to retrieve root CAs from your system without any difficulties or hazmat.
    )annotationsN)wraps)RLock)TYPE_CHECKINGAny   )IS_BSDIS_LINUX)root_der_certificates)VERSION__version__)CallableProtocolTypeVar)	ParamSpec_P_RT)	covariantc                   @  s    e Zd ZdddZdd
dZdS )_CachedFuncargs_P.argskwargs	_P.kwargsreturnr   c                 O     d S N )selfr   r   r   r   E/home/thesage/.local/lib/python3.10/site-packages/wassima/__init__.py__call__!       z_CachedFunc.__call__Nonec                 C  r   r   r   )r   r   r   r   cache_clear"   r    z_CachedFunc.cache_clearN)r   r   r   r   r   r   r   r!   )__name__
__module____qualname__r   r"   r   r   r   r   r       s    
r   zECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305strMOZ_INTERMEDIATE_CIPHERSlist[bytes]_MANUALLY_REGISTERED_CAi  intDEFAULT_CACHE_TTL_SECONDS_CACHE_TTL_SECONDSfuncCallable[_P, _R]r   _CachedFunc[_P, _R]c                   sR   t  i  ddit td fdd}d fd
d}||_|S )zEA minimal, thread-safe memorizing decorator with a per-call-site TTL.
expires_at        r   r   r   r   c                    s   |r| t t| fn| }3 t }|d kr%   |t d<  |}|u r:| i |}| |< |W  d    S 1 sFw   Y  d S )Nr1   )tuplesorteditemstime	monotonicclearr-   get)r   r   keynowresultcacher.   locksentinelstater   r   wrapper?   s   $z_ttl_lru_cache.<locals>.wrapperr!   c                     s:        dd< W d    d S 1 sw   Y  d S )Nr2   r1   )r8   r   )r>   r?   rA   r   r   r"   M   s   
"z#_ttl_lru_cache.<locals>.cache_clearN)r   r   r   r   r   r   r#   )objectr   r   r"   )r.   rB   r"   r   r=   r   _ttl_lru_cache8   s   rD   secondsr!   c                 C  sD   t | tr
t | trtd| dk rtd| at  t  dS )zOverride the default cache TTL (in seconds) used by
    :func:`root_der_certificates` and :func:`root_pem_certificates`.

    A value of ``0`` disables caching entirely (each call recomputes).
    Any pending cached result is dropped immediately.
    z"cache TTL must be an int (seconds)r   zcache TTL cannot be negativeN)	
isinstancer+   bool	TypeError
ValueErrorr-   r
   r"   root_pem_certificates)rE   r   r   r   set_cache_ttlV   s   rK   Fhybrid_storerG   c                 C  s   t  }| }ts	trddlm} | rd}|st }n|r8t|}t|}t D ]}||vr7|| |	| q't
t}|rUt|}|D ]}||vrT|| |	| qD|S )aF  Retrieve a list of root certificates from your operating system trust store,
    DER (binary) encoded.

    When ``hybrid_store`` is ``True``, the embedded CCADB Mozilla bundle is
    forcibly merged in addition to the OS trusted CAs. This is also implicitly
    enabled on Linux/BSD when the system trust store appears to be stale
    (older than 3 years without update).

    The OS-specific backends already guarantee a duplicate-free list; this
    function only re-deduplicates when extra sources (CCADB fallback, hybrid
    bundle, user-registered CAs) are merged on top.
    r   )is_trust_store_staleT)_root_der_certificatesr	   r   
_os._linuxrM   fallback_der_certificatessetlistaddappendr3   r*   )rL   certificatesforce_hybridrM   seencertmanually_registeredr   r   r   r
   g   s2   




r
   	list[str]c                 C  s(   g }t | dD ]
}|t| q|S )z
    Retrieve a list of root certificate from your operating system trust store.
    They will be PEM encoded.

    See :func:`root_der_certificates` for the meaning of ``hybrid_store``.
    rL   )r
   rT   sslDER_cert_to_PEM_cert)rL   	pem_certsbin_certr   r   r   rJ      s   rJ   c                 C  s   d t| dS )z
    Generate an aggregated CA bundle that originate from your system trust store.
    Simply put, concatenated root PEM certificate.

    See :func:`root_der_certificates` for the meaning of ``hybrid_store``.
    z

r[   )joinrJ   r[   r   r   r   generate_ca_bundle   s   ra   pem_or_der_certificatebytes | strc                 C  sp   t , t| trt| } | tvr&t|  t  t	  W d   dS W d   dS 1 s1w   Y  dS )zZ
    You may register your own CA certificate in addition to your system trust store.
    N)
_USER_APPEND_CA_LOCKrF   r'   r\   PEM_cert_to_DER_certr*   rT   rJ   r"   r
   )rb   r   r   r   register_ca   s   



"rf   ssl.SSLContextc                 C  sz   t t j}|jt| dd t jj|_|t	 t j
|_zd|_W n	 ty+   Y nw zd|_W |S  ty<   Y |S w )a  
    Instantiate a native SSLContext (client purposes) that ships with your system root CAs.
    In addition to that, assign it the default OpenSSL ciphers suite and set
    TLS 1.2 as the minimum supported version. Also disable commonName check and enforce
    hostname altName verification. The Mozilla Recommended Cipher Suite is used instead of system default.

    See :func:`root_der_certificates` for the meaning of ``hybrid_store``.
    r[   )cadataFT)r\   
SSLContextPROTOCOL_TLS_CLIENTload_verify_locationsra   
TLSVersionTLSv1_2minimum_versionset_ciphersr(   CERT_REQUIREDverify_modehostname_checks_common_nameAttributeErrorcheck_hostname)rL   ctxr   r   r   create_default_ssl_context   s"   	


rv   )	r
   rJ   ra   rv   rf   rK   r,   r   r   )r.   r/   r   r0   )rE   r+   r   r!   )F)rL   rG   r   r)   )rL   rG   r   rZ   )rL   rG   r   r'   )rb   rc   r   r!   )rL   rG   r   rg   )+__doc__
__future__r   r\   r6   	functoolsr   	threadingr   typingr   r   _osr   r	   r
   rN   
_os._embedrP   _versionr   r   r   r   r   typing_extensionsr   r   r   r   r(   __annotations__r*   rd   r,   r-   rD   rK   rJ   ra   rf   rv   __all__r   r   r   r   <module>   s@    

0

