U
    ;g&                     @   s   d dl mZmZmZmZ d dlZd dlZd dlZd dlZddl	m
Z
mZ ddlmZ ddlmZmZ ddd	d
ddddgZe ZddddZdd	 Zdd ZdddZdd Zdd Zdd
 Zdd ZdS )    )unicode_literalsdivisionabsolute_importprint_functionN   )str_cls	type_name)LibraryNotFoundError)__version____version_info__r
   r   backendffi
load_order
use_ctypesuse_openssluse_winlegacy)r   backend_configr   c                
   C   s   t d dk	rt d S t t d dk	r:t d W  5 Q R  S tjdkrht d dk r^dt d< qdt d< ntjdkr|d	t d< nd
t d< t d W  5 Q R  S Q R X dS )zs
    :return:
        A unicode string of the backend being used: "openssl", "mac", "win",
        "winlegacy"
    r   Nwin32r      	winlegacywindarwinmacopenssl)_module_values_backend_locksysplatformgetwindowsversion r   r   5/tmp/pip-unpacked-wheel-x1gypflw/oscrypto/__init__.pyr   "   s    




c                
   C   sn   t  dkri S td dk	r"td S t> td dk	rHtd W  5 Q R  S i td< td W  5 Q R  S Q R X dS )z
    :return:
        A dict of config info for the backend. Only currently used by "openssl",
        it may contains zero or more of the following keys:
         - "libcrypto_path"
         - "libssl_path"
    r   r   N)r   r   r   r   r   r   r    _backend_config>   s    	
r!   c              	   C   s  t | tstdt|  t |ts4tdt| tj| sLtd|  tj|sdtd| |dk	rt |tstdt| tj|std| t	\ | ||d}t
d	 d
krt
d |krW 5 Q R  dS t
d	 dk	rtdd
t
d	< |t
d< W 5 Q R X dS )a  
    Forces using OpenSSL dynamic libraries on OS X (.dylib) or Windows (.dll),
    or using a specific dynamic library on Linux/BSD (.so).

    This can also be used to configure oscrypto to use LibreSSL dynamic
    libraries.

    This method must be called before any oscrypto submodules are imported.

    :param libcrypto_path:
        A unicode string of the file path to the OpenSSL/LibreSSL libcrypto
        dynamic library.

    :param libssl_path:
        A unicode string of the file path to the OpenSSL/LibreSSL libssl
        dynamic library.

    :param trust_list_path:
        An optional unicode string of the path to a file containing
        OpenSSL-compatible CA certificates in PEM format. If this is not
        provided and the platform is OS X or Windows, the system trust roots
        will be exported from the OS and used for all TLS connections.

    :raises:
        ValueError - when one of the paths is not a unicode string
        OSError - when the trust_list_path does not exist on the filesystem
        oscrypto.errors.LibraryNotFoundError - when one of the path does not exist on the filesystem
        RuntimeError - when this function is called after another part of oscrypto has been imported
    z/libcrypto_path must be a unicode string, not %sz,libssl_path must be a unicode string, not %szlibcrypto does not exist at %szlibssl does not exist at %sNz0trust_list_path must be a unicode string, not %sz$trust_list_path does not exist at %s)libcrypto_pathlibssl_pathtrust_list_pathr   r   r   zRAnother part of oscrypto has already been imported, unable to force use of OpenSSL)
isinstancer   
ValueErrorr   ospathexistsr	   OSErrorr   r   RuntimeError)r"   r#   r$   Z
new_configr   r   r    r   U   s0    


c               	   C   sz   t jdkr0t pt j} | dkr$d} td|  t< td dkrPW 5 Q R  dS td dk	rdtddtd< W 5 Q R X dS )	as  
    Forces use of the legacy Windows CryptoAPI. This should only be used on
    Windows XP or for testing. It is less full-featured than the Cryptography
    Next Generation (CNG) API, and as a result the elliptic curve and PSS
    padding features are implemented in pure Python. This isn't ideal, but it
    a shim for end-user client code. No one is going to run a server on Windows
    XP anyway, right?!

    :raises:
        EnvironmentError - when this function is called on an operating system other than Windows
        RuntimeError - when this function is called after another part of oscrypto has been imported
    r   DarwinzOS Xz9The winlegacy backend can only be used on Windows, not %sr   r   NzcAnother part of oscrypto has already been imported, unable to force use of Windows legacy CryptoAPI)r   r   systemEnvironmentErrorr   r   r+   )platr   r   r    r      s    
c                	   C   sJ   t < td dkr W 5 Q R  dS td dk	r4tddtd< W 5 Q R X dS )z
    Forces use of ctypes instead of cffi for the FFI layer

    :raises:
        RuntimeError - when this function is called after another part of oscrypto has been imported
    r   ctypesNr   zQAnother part of oscrypto has already been imported, unable to force use of ctypes)r   r   r+   r   r   r   r    r      s    c               
   C   sj   t d dk	rt d S tH zddl} dt d< W n tk
rJ   dt d< Y nX t d W  5 Q R  S Q R X dS )zh
    Returns the FFI module being used

    :return:
        A unicode string of "cffi" or "ctypes"
    r   Nr   cffir0   )r   r   r1   ImportError)r1   r   r   r    r      s    c                E   C   s   ddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEgES )Fa)  
    Returns a list of the module and sub-module names for oscrypto in
    dependency load order, for the sake of live reloading code

    :return:
        A list of unicode strings of module names, as they would appear in
        sys.modules, ordered by which module should be reloaded first
    zoscrypto._asn1zoscrypto._cipher_suiteszoscrypto._errorszoscrypto._intzoscrypto._typeszoscrypto.errorszoscrypto.versionZoscryptozoscrypto._ffizoscrypto._pkcs12zoscrypto._pkcs5zoscrypto._randzoscrypto._tlszoscrypto._linux_bsd.trust_listz!oscrypto._mac._common_crypto_cffiz#oscrypto._mac._common_crypto_ctypeszoscrypto._mac._common_cryptoz#oscrypto._mac._core_foundation_cffiz%oscrypto._mac._core_foundation_ctypeszoscrypto._mac._core_foundationzoscrypto._mac._security_cffizoscrypto._mac._security_ctypeszoscrypto._mac._securityzoscrypto._mac.trust_listzoscrypto._mac.utilz!oscrypto._openssl._libcrypto_cffiz#oscrypto._openssl._libcrypto_ctypeszoscrypto._openssl._libcryptozoscrypto._openssl._libssl_cffiz oscrypto._openssl._libssl_ctypeszoscrypto._openssl._libsslzoscrypto._openssl.utilzoscrypto._win._cng_cffizoscrypto._win._cng_ctypeszoscrypto._win._cngzoscrypto._win._decodezoscrypto._win._advapi32_cffizoscrypto._win._advapi32_ctypeszoscrypto._win._advapi32zoscrypto._win._kernel32_cffizoscrypto._win._kernel32_ctypeszoscrypto._win._kernel32zoscrypto._win._secur32_cffizoscrypto._win._secur32_ctypeszoscrypto._win._secur32zoscrypto._win._crypt32_cffizoscrypto._win._crypt32_ctypeszoscrypto._win._crypt32zoscrypto._win.trust_listzoscrypto._win.utilzoscrypto.trust_listzoscrypto.utilzoscrypto.kdfzoscrypto._mac.symmetriczoscrypto._openssl.symmetriczoscrypto._win.symmetriczoscrypto.symmetriczoscrypto._asymmetriczoscrypto._ecdsazoscrypto._pkcs1zoscrypto._mac.asymmetriczoscrypto._openssl.asymmetriczoscrypto._win.asymmetriczoscrypto.asymmetriczoscrypto.keyszoscrypto._mac.tlszoscrypto._openssl.tlszoscrypto._win.tlszoscrypto.tlsr   r   r   r   r    r      s    )N)
__future__r   r   r   r   r'   r   r   	threading_typesr   r   errorsr	   versionr
   r   __all__Lockr   r   r   r!   r   r   r   r   r   r   r   r   r    <module>   s8   
B 