3 y�_ � @ s d dl mZmZmZ d dlZd dlZd dlZd dlZd dlZd dl Z d dl m Z d dlmZ d dl mZmZ d dlmZ ejddd d ddg�ZG d d� de�Zdd� Zdd� Zdd� Zddd�Zdd� ZG dd� de�Zdd� Zdd� Zee j� ej� eej� dS ) � )�absolute_import�division�print_functionN)�utils)� InternalError)�ffi�lib)�CONDITIONAL_NAMES�_OpenSSLErrorWithText�coder �func�reasonZreason_textc @ sD e Zd Zdd� Zdd� Zejd�Zejd�Zejd�Z ejd�Z d S ) � _OpenSSLErrorc C s || _ || _|| _|| _d S )N)�_code�_lib�_func�_reason)�selfr r r r � r �/usr/lib64/python3.6/binding.py�__init__ s z_OpenSSLError.__init__c C s || j ko|| jkS )N)r r )r r r r r r �_lib_reason_match s z_OpenSSLError._lib_reason_matchr r r r N)�__name__� __module__�__qualname__r r r Zread_only_propertyr r r r r r r r r s r c C sR g }xH| j � }|dkrP | j|�}| j|�}| j|�}|jt||||�� qW |S )Nr )Z ERR_get_errorZERR_GET_LIBZERR_GET_FUNCZERR_GET_REASON�appendr )r �errorsr Zerr_libZerr_funcZ err_reasonr r r �_consume_errors( s r c C s^ g }xT| D ]L}t jdd�}tj|j|t|�� t j|�}|jt|j|j|j |j |�� q W |S )Nzchar[]� )r �newr ZERR_error_string_nr �len�stringr r r r )r �errors_with_text�errZbufZerr_text_reasonr r r �_errors_with_text8 s r$ c C s t t| ��S )N)r$ r )r r r r �_consume_errors_with_textH s r% c C s0 |s,|d krt | �}t|�}tdj|�|��d S )Nai Unknown OpenSSL error. This error is commonly encountered when another library is not cleaning up the OpenSSL error stack. If you are using cryptography with another library that uses OpenSSL try disabling it before reporting a bug. Otherwise please file an issue at https://github.com/pyca/cryptography/issues with information on how to reproduce this. ({0!r}))r r$ r �format)r �okr r" r r r �_openssl_assertL s r( c C sr t jd�}| |_t� }x*|j� D ]\}}t| |�s |j|� � q W x*t| �D ]}||krLt||t| |�� qLW |S )Nr ) �types� ModuleTypeZ _original_lib�set�items�getattr�update�dir�setattr)r Zconditional_namesZconditional_libZexcluded_namesZ conditionZnames_cb�attrr r r �build_conditional_library^ s r2 c @ sX e Zd ZdZdZeZdZej� Z ej� Z dd� Zedd� �Z edd � �Zed d� �ZdS )�Bindingz OpenSSL API wrapper. NFc C s | j � d S )N)�_ensure_ffi_initialized)r r r r r x s zBinding.__init__c C s0 | j j� | j jr,| j j� }t| j |dk� d S )N� � )r5 r6 )r ZERR_clear_errorZ"CRYPTOGRAPHY_NEEDS_OSRANDOM_ENGINEZ Cryptography_add_osrandom_enginer( )�cls�resultr r r �_register_osrandom_engine{ s z!Binding._register_osrandom_enginec C sT | j �D | jsFttt�| _d| _| jj� | jj� | jj� | j� W d Q R X d S )NT) � _init_lock�_lib_loadedr2 r r ZSSL_library_initZOpenSSL_add_all_algorithmsZSSL_load_error_stringsr9 )r7 r r r r4 � s zBinding._ensure_ffi_initializedc C s^ | j �N | j� td� | jj s4| jj� | jjkr8d S tj� }t | j|dk� W d Q R X d S )NZ_sslr5 ) �_lock_init_lockr4 � __import__r Z"Cryptography_HAS_LOCKING_CALLBACKSZCRYPTO_get_locking_callbackr ZNULLZCryptography_setup_ssl_threadsr( )r7 �resr r r �init_static_locks� s zBinding.init_static_locks)r r r �__doc__r r r; � threadingZLockr: r<