Your IP : 18.189.192.214


Current Path : /usr/lib/python3.6/site-packages/OpenSSL/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/OpenSSL/__pycache__/crypto.cpython-36.pyc

3

�F\���$@s�ddlZddlmZddlmZddlmZmZmZm	Z	m
Z
mZddlm
ZmZmZddlmZddlmZmZddlmZdd	lmZmZmZ m!Z"m#Z$m%Z&m'Z(m)Z*d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-g$Z+ej,Z-ej.Z/dnZ0ej1Z2ej3Z4ej5Z6ej7Z8Gd1d�de9�Z:ee e:�Z;e*e:�Z<d2d3�Z=d4d5�Z>dod6d7�Z?d8d9�Z@d:d;�ZAd<d=�ZBGd>d?�d?eC�ZDGd@d�deC�ZEeeEeFdAeG�ZHGdBdC�dCeC�ZIdDd�ZJdEd�ZKGdFd�deC�ZLeeLeFdGeG�ZMGdHd�deC�ZNeeNeFdIeG�ZOGdJd�deC�ZPeePeFdKeG�ZQGdLd�deC�ZReeReFdMeG�ZSGdNd�deC�ZTGdOd�deC�ZUeeUeFdPeG�ZVGdQd�de9�ZWGdRd�deC�ZXdSd�ZYdTd�ZZdUd�Z[dpdVd�Z\GdWd�deC�Z]GdXd �d eC�Z^ee^eFdYeG�Z_GdZd!�d!eC�Z`ee`eFd[eG�ZaGd\d"�d"eC�ZbeebeFd]eG�ZcGd^d#�d#eC�ZdeedeFd_eG�ZeGd`da�daeC�Zfdbd$�Zgdqdcd%�Zhddd&�Zided'�Zjdfd(�Zkdgd)�Zldhd*�Zmdid+�Zndjd,�Zodrdkd-�Zpejq�ejr�ejsdl�dS)s�N)�	b16encode)�partial)�__eq__�__ne__�__lt__�__le__�__gt__�__ge__)�
integer_types�	text_type�PY3)�x509)�dsa�rsa)�
deprecated)�ffi�lib�exception_from_error_queue�byte_string�native�UNSPECIFIED�text_to_bytes_and_warn�make_assert�FILETYPE_PEM�
FILETYPE_ASN1�
FILETYPE_TEXT�TYPE_RSA�TYPE_DSA�Error�PKey�get_elliptic_curves�get_elliptic_curve�X509Name�
X509Extension�X509Req�X509�X509StoreFlags�	X509Store�X509StoreContextError�X509StoreContext�load_certificate�dump_certificate�dump_publickey�dump_privatekey�Revoked�CRL�PKCS7�PKCS12�NetscapeSPKI�load_publickey�load_privatekey�dump_certificate_request�load_certificate_request�sign�verify�dump_crl�load_crl�load_pkcs7_data�load_pkcs12���c@seZdZdZdS)rz7
    An error occurred in an `OpenSSL.crypto` API.
    N)�__name__�
__module__�__qualname__�__doc__�rDrD�/usr/lib/python3.6/crypto.pyrNscCsddlm}|S)ap
    Importing the backend from cryptography has the side effect of activating
    the osrandom engine. This mutates the global state of OpenSSL in the
    process and causes issues for various programs that use subinterpreters or
    embed Python. By putting the import in this function we can avoid
    triggering this side effect unless _get_backend is called.
    r)�backend)Z,cryptography.hazmat.backends.openssl.backendrF)rFrDrDrE�_get_backendXsrGcCstd|f��dS)z�
    An OpenSSL API failed somehow.  Additionally, the failure which was
    encountered isn't one that's exercised by the test suite so future behavior
    of pyOpenSSL is now somewhat less predictable.
    zUnknown %s failureN)�RuntimeError)�whererDrDrE�_untested_errordsrJcCsd|dkrtjtj��}tj}n(tjd|�}tj|t|��}|fdd�}t|tj	k�tj
||�}|S)z�
    Allocate a new OpenSSL memory BIO.

    Arrange for the garbage collector to clean it up automatically.

    :param buffer: None or some bytes to use to put into the BIO so that they
        can be read out.
    Nzchar[]cSs
tj|�S)N)�_lib�BIO_free)�bio�refrDrDrE�free~sz_new_mem_buf.<locals>.free)rK�BIO_new�	BIO_s_memrL�_ffi�newZBIO_new_mem_buf�len�_openssl_assert�NULL�gc)�bufferrMrO�datarDrDrE�_new_mem_bufms	rZcCs.tjd�}tj||�}tj|d|�dd�S)zO
    Copy the contents of an OpenSSL BIO object into a Python byte string.
    zchar**rN)rRrSrKZBIO_get_mem_datarX)rM�
result_bufferZ
buffer_lengthrDrDrE�_bio_to_string�s
r\cCs2t|t�std��tj||�}|dkr.td��dS)a�
    The the time value of an ASN1 time object.

    @param boundary: An ASN1_TIME pointer (or an object safely
        castable to that type) which will have its value set.
    @param when: A string representation of the desired time value.

    @raise TypeError: If C{when} is not a L{bytes} string.
    @raise ValueError: If C{when} does not represent a time in the required
        format.
    @raise RuntimeError: If the time value cannot be set for some other
        (unspecified) reason.
    zwhen must be a byte stringrzInvalid stringN)�
isinstance�bytes�	TypeErrorrKZASN1_TIME_set_string�
ValueError)�boundary�when�
set_resultrDrDrE�_set_asn1_time�s

rdcCs�tjd|�}tj|�dkrdStj|�tjkr>tjtj|��Stjd�}tj	||�|dtj
krltd�n6tjd|d�}tj|�}tj|�}tj|d�|SdS)a]
    Retrieve the time value of an ASN1 time object.

    @param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to
        that type) from which the time value will be retrieved.

    @return: The time value from C{timestamp} as a L{bytes} string in a certain
        format.  Or C{None} if the object contains no time value.
    zASN1_STRING*rNzASN1_GENERALIZEDTIME**�ASN1_TIME_to_generalizedtime)
rR�castrK�ASN1_STRING_lengthZASN1_STRING_typeZV_ASN1_GENERALIZEDTIME�string�ASN1_STRING_datarSrerVrJZASN1_GENERALIZEDTIME_free)Z	timestampZstring_timestampZgeneralized_timestampZstring_data�
string_resultrDrDrE�_get_asn1_time�s





rkc@s$eZdZdd�Zdd�Zdd�ZdS)�_X509NameInvalidatorcCs
g|_dS)N)�_names)�selfrDrDrE�__init__�sz_X509NameInvalidator.__init__cCs|jj|�dS)N)rm�append)rn�namerDrDrE�add�sz_X509NameInvalidator.addcCsx|jD]}|`qWdS)N)rm�_name)rnrqrDrDrE�clear�sz_X509NameInvalidator.clearN)r@rArBrorrrtrDrDrDrErl�srlc@sTeZdZdZdZdZdd�Zdd�Zedd	��Z	d
d�Z
dd
�Zdd�Zdd�Z
dS)rzD
    A class representing an DSA or RSA public key or key pair.
    FTcCs"tj�}tj|tj�|_d|_dS)NF)rKZEVP_PKEY_newrRrW�
EVP_PKEY_free�_pkey�_initialized)rn�pkeyrDrDrEro�sz
PKey.__init__cCs(t�}|jr|j|j�S|j|j�SdS)a
        Export as a ``cryptography`` key.

        :rtype: One of ``cryptography``'s `key interfaces`_.

        .. _key interfaces: https://cryptography.io/en/latest/hazmat/            primitives/asymmetric/rsa/#key-interfaces

        .. versionadded:: 16.1.0
        N)rG�_only_publicZ_evp_pkey_to_public_keyrvZ_evp_pkey_to_private_key)rnrFrDrDrE�to_cryptography_key�szPKey.to_cryptography_keycCsR|�}t|tjtjtjtjf�s(td��|j|_	t|tjtjf�rHd|_
d|_|S)z�
        Construct based on a ``cryptography`` *crypto_key*.

        :param crypto_key: A ``cryptography`` key.
        :type crypto_key: One of ``cryptography``'s `key interfaces`_.

        :rtype: PKey

        .. versionadded:: 16.1.0
        zUnsupported key typeT)r]rZRSAPublicKeyZ
RSAPrivateKeyrZDSAPublicKeyZ
DSAPrivateKeyr_Z	_evp_pkeyrvryrw)�clsZ
crypto_keyrxrDrDrE�from_cryptography_key�szPKey.from_cryptography_keycCs6t|t�std��t|t�s$td��|tkr�|dkr<td��tj�}tj|tj	�}tj
|tj�tj�}tj
|||tj�}t|dk�tj|j|�}t|dk�n�|tk�r$tj�}t|tjk�tj|tj�}tj||tjdtjtjtj�}t|dk�ttj|�dk�ttj|j|�dk�ntd��d|_dS)	a3
        Generate a key pair of the given type, with the given number of bits.

        This generates a key "into" the this object.

        :param type: The key type.
        :type type: :py:data:`TYPE_RSA` or :py:data:`TYPE_DSA`
        :param bits: The number of bits.
        :type bits: :py:data:`int` ``>= 0``
        :raises TypeError: If :py:data:`type` or :py:data:`bits` isn't
            of the appropriate type.
        :raises ValueError: If the number of bits isn't an integer of
            the appropriate size.
        :return: ``None``
        ztype must be an integerzbits must be an integerrzInvalid number of bitsr?zNo such key typeTN)r]�intr_rr`rK�BN_newrRrW�BN_freeZBN_set_wordZRSA_F4ZRSA_newZRSA_generate_key_exrVrUZEVP_PKEY_assign_RSArvrZDSA_newZDSA_freeZDSA_generate_parameters_exZDSA_generate_keyZEVP_PKEY_set1_DSArrw)rn�type�bitsZexponentr�resultr�resrDrDrE�generate_keys4


zPKey.generate_keycCs`|jrtd��tj|j��tjkr*td��tj|j�}tj	|tj
�}tj|�}|rVdSt�dS)ax
        Check the consistency of an RSA private key.

        This is the Python equivalent of OpenSSL's ``RSA_check_key``.

        :return: ``True`` if key is consistent.

        :raise OpenSSL.crypto.Error: if the key is inconsistent.

        :raise TypeError: if the key is of a type which cannot be checked.
            Only RSA keys can currently be checked.
        zpublic key onlyzkey type unsupportedTN)
ryr_rKZ
EVP_PKEY_typer��EVP_PKEY_RSA�EVP_PKEY_get1_RSArvrRrW�RSA_freeZ
RSA_check_key�_raise_current_error)rnrr�rDrDrE�checkGs

z
PKey.checkcCstj|j�S)zT
        Returns the type of the key

        :return: The type of the key.
        )rK�EVP_PKEY_idrv)rnrDrDrEr�asz	PKey.typecCstj|j�S)zh
        Returns the number of bits of the key

        :return: The number of bits of the key.
        )rKZ
EVP_PKEY_bitsrv)rnrDrDrEr�isz	PKey.bitsN)r@rArBrCryrwrorz�classmethodr|r�r�r�r�rDrDrDrEr�s8z.PKeyType has been deprecated, use PKey insteadcsdeZdZdZdZer �fdd�Zedd��Zedd��Z	ed	d
��Z
dd�Zd
d�Zdd�Z
�ZS)�_EllipticCurveaZ
    A representation of a supported elliptic curve.

    @cvar _curves: :py:obj:`None` until an attempt is made to load the curves.
        Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve`
        instances each of which represents one curve supported by the system.
    @type _curves: :py:type:`NoneType` or :py:type:`set`
    Ncst|t�rtt|�j|�StS)z�
            Implement cooperation with the right-hand side argument of ``!=``.

            Python 3 seems to have dropped this cooperation in this very narrow
            circumstance.
            )r]r��superr�NotImplemented)rn�other)�	__class__rDrEr�s
z_EllipticCurve.__ne__cs>�jtjd�}tjd|�}�j||�t��fdd�|D��S)z�
        Get the curves supported by OpenSSL.

        :param lib: The OpenSSL library binding object.

        :return: A :py:type:`set` of ``cls`` instances giving the names of the
            elliptic curves the underlying library supports.
        rzEC_builtin_curve[]c3s|]}�j�|j�VqdS)N)�from_nid�nid)�.0�c)r{rrDrE�	<genexpr>�sz7_EllipticCurve._load_elliptic_curves.<locals>.<genexpr>)ZEC_get_builtin_curvesrRrVrS�set)r{rZ
num_curvesZbuiltin_curvesrD)r{rrE�_load_elliptic_curves�s
z$_EllipticCurve._load_elliptic_curvescCs|jdkr|j|�|_|jS)a
        Get, cache, and return the curves supported by OpenSSL.

        :param lib: The OpenSSL library binding object.

        :return: A :py:type:`set` of ``cls`` instances giving the names of the
            elliptic curves the underlying library supports.
        N)�_curvesr�)r{rrDrDrE�_get_elliptic_curves�s

z#_EllipticCurve._get_elliptic_curvescCs|||tj|j|��jd��S)a�
        Instantiate a new :py:class:`_EllipticCurve` associated with the given
        OpenSSL NID.

        :param lib: The OpenSSL library binding object.

        :param nid: The OpenSSL NID the resulting curve object will represent.
            This must be a curve NID (and not, for example, a hash NID) or
            subsequent operations will fail in unpredictable ways.
        :type nid: :py:class:`int`

        :return: The curve object.
        �ascii)rRrh�
OBJ_nid2sn�decode)r{rr�rDrDrEr��sz_EllipticCurve.from_nidcCs||_||_||_dS)a�
        :param _lib: The :py:mod:`cryptography` binding instance used to
            interface with OpenSSL.

        :param _nid: The OpenSSL NID identifying the curve this object
            represents.
        :type _nid: :py:class:`int`

        :param name: The OpenSSL short name identifying the curve this object
            represents.
        :type name: :py:class:`unicode`
        N)rK�_nidrq)rnrr�rqrDrDrEro�s
z_EllipticCurve.__init__cCsd|jfS)Nz
<Curve %r>)rq)rnrDrDrE�__repr__�sz_EllipticCurve.__repr__cCs|jj|j�}tj|tj�S)z�
        Create a new OpenSSL EC_KEY structure initialized to use this curve.

        The structure is automatically garbage collected when the Python object
        is garbage collected.
        )rKZEC_KEY_new_by_curve_namer�rRrWZEC_KEY_free)rn�keyrDrDrE�
_to_EC_KEY�sz_EllipticCurve._to_EC_KEY)r@rArBrCr��_PY3rr�r�r�r�ror�r��
__classcell__rDrD)r�rEr�ysr�cCs
tjt�S)a�
    Return a set of objects representing the elliptic curves supported in the
    OpenSSL build in use.

    The curve objects have a :py:class:`unicode` ``name`` attribute by which
    they identify themselves.

    The curve objects are useful as values for the argument accepted by
    :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
    used for ECDHE key exchange.
    )r�r�rKrDrDrDrEr �scCs,xt�D]}|j|kr|SqWtd|��dS)aT
    Return a single curve object selected by name.

    See :py:func:`get_elliptic_curves` for information about curve objects.

    :param name: The OpenSSL short name identifying the curve object to
        retrieve.
    :type name: :py:class:`unicode`

    If the named curve is not supported then :py:class:`ValueError` is raised.
    zunknown curve nameN)r rqr`)rqZcurverDrDrEr!�s
cs�eZdZdZdd�Z�fdd�Z�fdd�Zdd	�Zee�Zee	�Z	ee
�Z
ee�Zee�Zee
�Z
d
d�Zdd
�Zdd�Zdd�Z�ZS)r"a
    An X.509 Distinguished Name.

    :ivar countryName: The country of the entity.
    :ivar C: Alias for  :py:attr:`countryName`.

    :ivar stateOrProvinceName: The state or province of the entity.
    :ivar ST: Alias for :py:attr:`stateOrProvinceName`.

    :ivar localityName: The locality of the entity.
    :ivar L: Alias for :py:attr:`localityName`.

    :ivar organizationName: The organization name of the entity.
    :ivar O: Alias for :py:attr:`organizationName`.

    :ivar organizationalUnitName: The organizational unit of the entity.
    :ivar OU: Alias for :py:attr:`organizationalUnitName`

    :ivar commonName: The common name of the entity.
    :ivar CN: Alias for :py:attr:`commonName`.

    :ivar emailAddress: The e-mail address of the entity.
    cCs tj|j�}tj|tj�|_dS)z�
        Create a new X509Name, copying the given X509Name instance.

        :param name: The name to copy.
        :type name: :py:class:`X509Name`
        N)rK�
X509_NAME_duprsrRrW�X509_NAME_free)rnrqrDrDrEroszX509Name.__init__c	s|jd�rtt|�j||�St|�tk	r<tdt|�jf��tj	t
|��}|tjkr|y
t�Wnt
k
rrYnXtd��x\ttj|j��D]H}tj|j|�}tj|�}tj|�}||kr�tj|j|�}tj|�Pq�Wt|t�r�|jd�}tj|j|tj|ddd�}|�st�dS)	N�_z+attribute name must be string, not '%.200s'zNo such attributezutf-8r?r���r�)�
startswithr�r"�__setattr__r��strr_r@rK�OBJ_txt2nid�_byte_string�	NID_undefr�r�AttributeError�range�X509_NAME_entry_countrs�X509_NAME_get_entry�X509_NAME_ENTRY_get_object�OBJ_obj2nidZX509_NAME_delete_entryZX509_NAME_ENTRY_freer]�
_text_type�encodeZX509_NAME_add_entry_by_NIDZ
MBSTRING_UTF8)	rnrq�valuer��i�entZent_objZent_nid�
add_result)r�rDrEr�'s4







zX509Name.__setattr__c	s�tjt|��}|tjkrHy
t�Wntk
r6YnXtt|�j|�Stj	|j
|d�}|dkrddStj|j
|�}tj|�}t
jd�}tj||�}t|dk�z"t
j|d|�dd�jd�}Wdtj|d�X|S)a

        Find attribute. An X509Name object has the following attributes:
        countryName (alias C), stateOrProvince (alias ST), locality (alias L),
        organization (alias O), organizationalUnit (alias OU), commonName
        (alias CN) and more...
        r?Nzunsigned char**rzutf-8r�r�)rKr�r�r�r�rr�r"�__getattr__ZX509_NAME_get_index_by_NIDrsr��X509_NAME_ENTRY_get_datarRrSZASN1_STRING_to_UTF8rUrXr��OPENSSL_free)	rnrqr�Zentry_index�entryrYr[Zdata_lengthr�)r�rDrEr�Ks*




zX509Name.__getattr__cs�fdd�}|S)Ncs(t|t�stStj|j|j�}�|d�S)Nr)r]r"r�rKZ
X509_NAME_cmprs)rnr�r�)�oprDrE�fts
zX509Name._cmp.<locals>.frD)r�r�rD)r�rE�_cmpssz
X509Name._cmpcCsBtjdd�}tj|j|t|��}t|tjk�dttj	|��fS)z6
        String representation of an X509Name
        zchar[]iz<X509Name object '%s'>)
rRrSrKZX509_NAME_onelinersrTrUrV�_nativerh)rnr[Z
format_resultrDrDrEr��szX509Name.__repr__cCstj|j�S)a&
        Return an integer representation of the first four bytes of the
        MD5 digest of the DER representation of the name.

        This is the Python equivalent of OpenSSL's ``X509_NAME_hash``.

        :return: The (integer) hash of this name.
        :rtype: :py:class:`int`
        )rKZX509_NAME_hashrs)rnrDrDrE�hash�s
z
X509Name.hashcCsNtjd�}tj|j|�}t|dk�tj|d|�dd�}tj|d�|S)z�
        Return the DER encoding of this name.

        :return: The DER encoded form of this name.
        :rtype: :py:class:`bytes`
        zunsigned char**rN)rRrSrKZ
i2d_X509_NAMErsrUrXr�)rnr[Z
encode_resultrjrDrDrE�der�s
zX509Name.derc	Cs�g}x�ttj|j��D]n}tj|j|�}tj|�}tj|�}tj|�}tj|�}t	j
tj|�tj|��dd�}|j
t	j|�|f�qW|S)z�
        Returns the components of this name, as a sequence of 2-tuples.

        :return: The components of this name.
        :rtype: :py:class:`list` of ``name, value`` tuples.
        N)r�rKr�rsr�r�r�r�r�rRrXrirgrprh)	rnr�r�r�ZfnameZfvalr�rqr�rDrDrE�get_components�s



zX509Name.get_components)r@rArBrCror�r�r�rrrrrr	r�r�r�r�r�rDrD)r�rEr"s
$(z6X509NameType has been deprecated, use X509Name insteadc@sdeZdZdZddd�Zedd��Zejdej	dej
d	iZd
d�Zdd
�Z
dd�Zdd�Zdd�ZdS)r#z,
    An X.509 v3 certificate extension.
    NcCs�tjd�}tj|tjtjtjtjd�tj|�|dk	rRt|t�sJtd��|j	|_
|dk	rtt|t�sltd��|j	|_|r�d|}tjtj|||�}|tjkr�t
�tj|tj�|_dS)a�
        Initializes an X509 extension.

        :param type_name: The name of the type of extension_ to create.
        :type type_name: :py:data:`bytes`

        :param bool critical: A flag indicating whether this is a critical
            extension.

        :param value: The value of the extension.
        :type value: :py:data:`bytes`

        :param subject: Optional X509 certificate to use as subject.
        :type subject: :py:class:`X509`

        :param issuer: Optional X509 certificate to use as issuer.
        :type issuer: :py:class:`X509`

        .. _extension: https://www.openssl.org/docs/manmaster/man5/
            x509v3_config.html#STANDARD-EXTENSIONS
        zX509V3_CTX*rNzissuer must be an X509 instancez subject must be an X509 instances	critical,)rRrSrKZX509V3_set_ctxrVZX509V3_set_ctx_nodbr]r%r_�_x509�issuer_certZsubject_certZX509V3_EXT_nconfr�rW�X509_EXTENSION_free�
_extension)rnZ	type_nameZcriticalr��subject�issuerZctx�	extensionrDrDrEro�s"




zX509Extension.__init__cCstjtj|j��S)N)rKr��X509_EXTENSION_get_objectr�)rnrDrDrEr�szX509Extension._nidZemailZDNSZURIc
Cs�tjdtj|j��}tj|tj�}g}x�ttj|��D]�}tj	||�}y|j
|j}Wn8tk
r�t
�}tj||�|jtt|���Yq6Xttj|jjj|jjj�dd��}|j|d|�q6Wdj|�S)NzGENERAL_NAMES*�:z, )rRrfrKZX509V3_EXT_d2ir�rWZGENERAL_NAMES_freer�Zsk_GENERAL_NAME_numZsk_GENERAL_NAME_value�	_prefixesr��KeyErrorrZZGENERAL_NAME_printrpr�r\rX�dZia5rY�length�join)rn�names�partsr�rqZlabelrMr�rDrDrE�_subjectAltNameStrings "z#X509Extension._subjectAltNameStringcCsDtj|jkr|j�St�}tj||jdd�}t|dk�tt	|��S)zF
        :return: a nice text representation of the extension
        r)
rKZNID_subject_alt_namer�r�rZ�X509V3_EXT_printr�rUr�r\)rnrM�print_resultrDrDrE�__str__/szX509Extension.__str__cCstj|j�S)zk
        Returns the critical field of this X.509 extension.

        :return: The critical field.
        )rKZX509_EXTENSION_get_criticalr�)rnrDrDrE�get_critical<szX509Extension.get_criticalcCs&tj|j�}tj|�}tjtj|��S)z�
        Returns the short type name of this X.509 extension.

        The result is a byte string such as :py:const:`b"basicConstraints"`.

        :return: The short type name.
        :rtype: :py:data:`bytes`

        .. versionadded:: 0.12
        )rKr�r�r�rRrhr�)rn�objr�rDrDrE�get_short_nameDs
zX509Extension.get_short_namecCs@tj|j�}tjd|�}tj|�}tj|�}tj||�dd�S)z�
        Returns the data of the X509 extension, encoded as ASN.1.

        :return: The ASN.1 encoded data of this X509 extension.
        :rtype: :py:data:`bytes`

        .. versionadded:: 0.12
        zASN1_STRING*N)rK�X509_EXTENSION_get_datar�rRrfrirgrX)rnZoctet_resultrjZchar_result�
result_lengthrDrDrE�get_dataSs
	

zX509Extension.get_data)NN)r@rArBrCro�propertyr�rKZ	GEN_EMAILZGEN_DNSZGEN_URIr�r�r�r�r�r�rDrDrDrEr#�s
=

z@X509ExtensionType has been deprecated, use X509Extension insteadc@steZdZdZdd�Zdd�Zedd��Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)r$z0
    An X.509 certificate signing requests.
    cCs&tj�}tj|tj�|_|jd�dS)Nr)rKZX509_REQ_newrRrW�
X509_REQ_free�_req�set_version)rn�reqrDrDrErooszX509Req.__init__cCsddlm}t�}|||j�S)z�
        Export as a ``cryptography`` certificate signing request.

        :rtype: ``cryptography.x509.CertificateSigningRequest``

        .. versionadded:: 17.1.0
        r)�_CertificateSigningRequest)�)cryptography.hazmat.backends.openssl.x509r�rGr�)rnr�rFrDrDrE�to_cryptographyuszX509Req.to_cryptographycCs&t|tj�std��|�}|j|_|S)a
        Construct based on a ``cryptography`` *crypto_req*.

        :param crypto_req: A ``cryptography`` X.509 certificate signing request
        :type crypto_req: ``cryptography.x509.CertificateSigningRequest``

        :rtype: X509Req

        .. versionadded:: 17.1.0
        z%Must be a certificate signing request)r]r
ZCertificateSigningRequestr_Z	_x509_reqr�)r{Z
crypto_reqr�rDrDrE�from_cryptography�s
zX509Req.from_cryptographycCs tj|j|j�}t|dk�dS)z�
        Set the public key of the certificate signing request.

        :param pkey: The public key to use.
        :type pkey: :py:class:`PKey`

        :return: ``None``
        r?N)rKZX509_REQ_set_pubkeyr�rvrU)rnrxrcrDrDrE�
set_pubkey�s	zX509Req.set_pubkeycCsDtjt�}tj|j�|_t|jtjk�tj	|jtj
�|_d|_|S)z�
        Get the public key of the certificate signing request.

        :return: The public key.
        :rtype: :py:class:`PKey`
        T)r�__new__rKZX509_REQ_get_pubkeyr�rvrUrRrVrWrury)rnrxrDrDrE�
get_pubkey�s
zX509Req.get_pubkeycCstj|j|�}t|dk�dS)z�
        Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate
        request.

        :param int version: The version number.
        :return: ``None``
        r?N)rKZX509_REQ_set_versionr�rU)rn�versionrcrDrDrEr��szX509Req.set_versioncCstj|j�S)z�
        Get the version subfield (RFC 2459, section 4.1.2.1) of the certificate
        request.

        :return: The value of the version subfield.
        :rtype: :py:class:`int`
        )rKZX509_REQ_get_versionr�)rnrDrDrE�get_version�szX509Req.get_versioncCs2tjt�}tj|j�|_t|jtjk�||_	|S)a�
        Return the subject of this certificate signing request.

        This creates a new :class:`X509Name` that wraps the underlying subject
        name field on the certificate signing request. Modifying it will modify
        the underlying signing request, and will have the effect of modifying
        any other :class:`X509Name` that refers to this subject.

        :return: The subject of this certificate signing request.
        :rtype: :class:`X509Name`
        )
r"r�rKZX509_REQ_get_subject_namer�rsrUrRrV�_owner)rnrqrDrDrE�get_subject�s

zX509Req.get_subjectcCsptj�}t|tjk�tj|tj�}x,|D]$}t|t�s@t	d��tj
||j�q*Wtj|j
|�}t|dk�dS)z�
        Add extensions to the certificate signing request.

        :param extensions: The X.509 extensions to add.
        :type extensions: iterable of :py:class:`X509Extension`
        :return: ``None``
        z+One of the elements is not an X509Extensionr?N)rKZsk_X509_EXTENSION_new_nullrUrRrVrWZsk_X509_EXTENSION_freer]r#r`Zsk_X509_EXTENSION_pushr�ZX509_REQ_add_extensionsr�)rn�
extensions�stack�extr�rDrDrE�add_extensions�s

zX509Req.add_extensionscCsNg}tj|j�}x8ttj|��D]&}tjt�}tj||�|_|j	|�q W|S)z�
        Get X.509 extensions in the certificate signing request.

        :return: The X.509 extensions in this request.
        :rtype: :py:class:`list` of :py:class:`X509Extension` objects.

        .. versionadded:: 0.15
        )
rKZX509_REQ_get_extensionsr�r�Zsk_X509_EXTENSION_numr#r�Zsk_X509_EXTENSION_valuer�rp)rnZextsZnative_exts_objr�r�rDrDrE�get_extensions�s	
zX509Req.get_extensionscCs^|jrtd��|jstd��tjt|��}|tjkr<td��tj|j	|j
|�}t|dk�dS)ad
        Sign the certificate signing request with this key and digest type.

        :param pkey: The key pair to sign with.
        :type pkey: :py:class:`PKey`
        :param digest: The name of the message digest to use for the signature,
            e.g. :py:data:`b"sha256"`.
        :type digest: :py:class:`bytes`
        :return: ``None``
        zKey has only public partzKey is uninitializedzNo such digest methodrN)ryr`rwrK�EVP_get_digestbynamer�rRrVZ
X509_REQ_signr�rvrU)rnrx�digest�
digest_obj�sign_resultrDrDrEr7s
zX509Req.signcCs4t|t�std��tj|j|j�}|dkr0t�|S)a@
        Verifies the signature on this certificate signing request.

        :param PKey key: A public key.

        :return: ``True`` if the signature is correct.
        :rtype: bool

        :raises OpenSSL.crypto.Error: If the signature is invalid or there is a
            problem verifying the signature.
        zpkey must be a PKey instancer)r]rr_rKZX509_REQ_verifyr�rvr�)rnrxr�rDrDrEr8s
zX509Req.verifyN)r@rArBrCror�r�r�r�r�r�r�r�r�r�r7r8rDrDrDrEr$js
z4X509ReqType has been deprecated, use X509Req insteadc@seZdZdZdd�Zedd��Zdd�Zedd	��Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Z d:d;�Z!d<d=�Z"d>d?�Z#d@dA�Z$dBS)Cr%z
    An X.509 certificate.
    cCs:tj�}t|tjk�tj|tj�|_t�|_	t�|_
dS)N)rKZX509_newrUrRrVrW�	X509_freer�rl�_issuer_invalidator�_subject_invalidator)rnr
rDrDrEro<s
z
X509.__init__cCs.|j|�}tj|tj�|_t�|_t�|_|S)N)	r�rRrWrKr�r�rlr�r�)r{r
�certrDrDrE�_from_raw_x509_ptrDs

zX509._from_raw_x509_ptrcCsddlm}t�}|||j�S)z�
        Export as a ``cryptography`` certificate.

        :rtype: ``cryptography.x509.Certificate``

        .. versionadded:: 17.1.0
        r)�_Certificate)r�r�rGr�)rnr�rFrDrDrEr�LszX509.to_cryptographycCs&t|tj�std��|�}|j|_|S)z�
        Construct based on a ``cryptography`` *crypto_cert*.

        :param crypto_key: A ``cryptography`` X.509 certificate.
        :type crypto_key: ``cryptography.x509.Certificate``

        :rtype: X509

        .. versionadded:: 17.1.0
        zMust be a certificate)r]r
ZCertificater_r�)r{Zcrypto_certr�rDrDrEr�Xs
zX509.from_cryptographycCs$t|t�std��tj|j|�dS)a	
        Set the version number of the certificate. Note that the
        version value is zero-based, eg. a value of 0 is V1.

        :param version: The version number of the certificate.
        :type version: :py:class:`int`

        :return: ``None``
        zversion must be an integerN)r]r}r_rKZX509_set_versionr�)rnr�rDrDrEr�ks

zX509.set_versioncCstj|j�S)z�
        Return the version number of the certificate.

        :return: The version number of the certificate.
        :rtype: :py:class:`int`
        )rKZX509_get_versionr�)rnrDrDrEr�zszX509.get_versioncCsFtjt�}tj|j�|_|jtjkr*t�tj	|jtj
�|_d|_|S)z{
        Get the public key of the certificate.

        :return: The public key.
        :rtype: :py:class:`PKey`
        T)rr�rK�X509_get_pubkeyr�rvrRrVr�rWrury)rnrxrDrDrEr��s
zX509.get_pubkeycCs2t|t�std��tj|j|j�}t|dk�dS)z�
        Set the public key of the certificate.

        :param pkey: The public key.
        :type pkey: :py:class:`PKey`

        :return: :py:data:`None`
        zpkey must be a PKey instancer?N)r]rr_rKZX509_set_pubkeyr�rvrU)rnrxrcrDrDrEr��s	
zX509.set_pubkeycCspt|t�std��|jr td��|js.td��tjt|��}|t	j
krNtd��tj|j|j
|�}t|dk�dS)a
        Sign the certificate with this key and digest type.

        :param pkey: The key to sign with.
        :type pkey: :py:class:`PKey`

        :param digest: The name of the message digest to use.
        :type digest: :py:class:`bytes`

        :return: :py:data:`None`
        zpkey must be a PKey instancezKey only has public partzKey is uninitializedzNo such digest methodrN)r]rr_ryr`rwrKr�r�rRrVZ	X509_signr�rvrU)rnrxr�Zevp_mdr�rDrDrEr7�s

z	X509.signcCs:tj|j�}tj|j�}|tjkr*td��tjtj	|��S)z�
        Return the signature algorithm used in the certificate.

        :return: The name of the algorithm.
        :rtype: :py:class:`bytes`

        :raises ValueError: If the signature algorithm is undefined.

        .. versionadded:: 0.13
        zUndefined signature algorithm)
rKZX509_get0_tbs_sigalgr�r��	algorithmr�r`rRrhZ
OBJ_nid2ln)rnZalgorr�rDrDrE�get_signature_algorithm�s

zX509.get_signature_algorithmcCs�tjt|��}|tjkr td��tjdtj�}tjdd�}t|�|d<tj	|j
|||�}t|dk�djdd�tj
||d�D��S)	a7
        Return the digest of the X509 object.

        :param digest_name: The name of the digest algorithm to use.
        :type digest_name: :py:class:`bytes`

        :return: The digest of the object, formatted as
            :py:const:`b":"`-delimited hex pairs.
        :rtype: :py:class:`bytes`
        zNo such digest methodzunsigned char[]zunsigned int[]r?r�:cSsg|]}t|�j��qSrD)r�upper)r�ZchrDrDrE�
<listcomp>�szX509.digest.<locals>.<listcomp>)rKr�r�rRrVr`rSZEVP_MAX_MD_SIZErTZX509_digestr�rUr�rX)rnZdigest_namer�r[r�Z
digest_resultrDrDrEr��s
zX509.digestcCstj|j�S)z�
        Return the hash of the X509 subject.

        :return: The hash of the subject.
        :rtype: :py:class:`bytes`
        )rKZX509_subject_name_hashr�)rnrDrDrE�subject_name_hash�szX509.subject_name_hashcCs�t|t�std��t|�dd�}t|t�s6|jd�}tjd�}tj	||�}|dtj
krztjtj|j
�|�}|r�t�nXtj|dtj
�}tj|d�|tj
kr�t�tj|tj�}tj|j
|�}t|dk�dS)z�
        Set the serial number of the certificate.

        :param serial: The new serial number.
        :type serial: :py:class:`int`

        :return: :py:data`None`
        zserial must be an integerr=Nr�zBIGNUM**rr?)r]�_integer_typesr_�hexr^r�rRrSrK�	BN_hex2bnrVZASN1_INTEGER_set�X509_get_serialNumberr�r��BN_to_ASN1_INTEGERrrW�ASN1_INTEGER_freeZX509_set_serialNumberrU)rn�serial�
hex_serial�
bignum_serialZsmall_serialrc�asn1_serialrDrDrE�set_serial_number�s&	




zX509.set_serial_numbercCs`tj|j�}tj|tj�}z4tj|�}ztj|�}t|d�}|Stj	|�XWdtj
|�XdS)zx
        Return the serial number of this certificate.

        :return: The serial number.
        :rtype: int
        r>N)rKrr�ZASN1_INTEGER_to_BNrRrVZ	BN_bn2hexrhr}r�r)rnr
rrZhexstring_serialr
rDrDrE�get_serial_numbers


zX509.get_serial_numbercCs.t|t�std��tj|j�}tj||�dS)z�
        Adjust the time stamp on which the certificate stops being valid.

        :param int amount: The number of seconds by which to adjust the
            timestamp.
        :return: ``None``
        zamount must be an integerN)r]r}r_rK�X509_get_notAfterr��X509_gmtime_adj)rn�amountZnotAfterrDrDrE�gmtime_adj_notAfter-s
zX509.gmtime_adj_notAftercCs.t|t�std��tj|j�}tj||�dS)z�
        Adjust the timestamp on which the certificate starts being valid.

        :param amount: The number of seconds by which to adjust the timestamp.
        :return: ``None``
        zamount must be an integerN)r]r}r_rK�X509_get_notBeforer�r)rnrZ	notBeforerDrDrE�gmtime_adj_notBefore;s
zX509.gmtime_adj_notBeforecCs(t|j��}tjj|d�}|tjj�kS)z�
        Check whether the certificate has expired.

        :return: ``True`` if the certificate has expired, ``False`` otherwise.
        :rtype: bool
        z
%Y%m%d%H%M%SZ)r��get_notAfter�datetimeZstrptimeZutcnow)rnZtime_stringZ	not_afterrDrDrE�has_expiredHszX509.has_expiredcCst||j��S)N)rkr�)rn�whichrDrDrE�_get_boundary_timeTszX509._get_boundary_timecCs|jtj�S)a

        Get the timestamp at which the certificate starts being valid.

        The timestamp is formatted as an ASN.1 TIME::

            YYYYMMDDhhmmssZ

        :return: A timestamp string, or ``None`` if there is none.
        :rtype: bytes or NoneType
        )rrKr)rnrDrDrE�
get_notBeforeWszX509.get_notBeforecCst||j�|�S)N)rdr�)rnrrbrDrDrE�_set_boundary_timedszX509._set_boundary_timecCs|jtj|�S)z�
        Set the timestamp at which the certificate starts being valid.

        The timestamp is formatted as an ASN.1 TIME::

            YYYYMMDDhhmmssZ

        :param bytes when: A timestamp string.
        :return: ``None``
        )rrKr)rnrbrDrDrE�
set_notBeforegszX509.set_notBeforecCs|jtj�S)a	
        Get the timestamp at which the certificate stops being valid.

        The timestamp is formatted as an ASN.1 TIME::

            YYYYMMDDhhmmssZ

        :return: A timestamp string, or ``None`` if there is none.
        :rtype: bytes or NoneType
        )rrKr)rnrDrDrErtszX509.get_notAftercCs|jtj|�S)z�
        Set the timestamp at which the certificate stops being valid.

        The timestamp is formatted as an ASN.1 TIME::

            YYYYMMDDhhmmssZ

        :param bytes when: A timestamp string.
        :return: ``None``
        )rrKr)rnrbrDrDrE�set_notAfter�szX509.set_notAftercCs0tjt�}||j�|_t|jtjk�||_|S)N)r"r�r�rsrUrRrVr�)rnrrqrDrDrE�	_get_name�s

zX509._get_namecCs0t|t�std��||j|j�}t|dk�dS)Nzname must be an X509Namer?)r]r"r_r�rsrU)rnrrqrcrDrDrE�	_set_name�s
zX509._set_namecCs|jtj�}|jj|�|S)a�
        Return the issuer of this certificate.

        This creates a new :class:`X509Name` that wraps the underlying issuer
        name field on the certificate. Modifying it will modify the underlying
        certificate, and will have the effect of modifying any other
        :class:`X509Name` that refers to this issuer.

        :return: The issuer of this certificate.
        :rtype: :class:`X509Name`
        )rrKZX509_get_issuer_namer�rr)rnrqrDrDrE�
get_issuer�szX509.get_issuercCs|jtj|�|jj�dS)z�
        Set the issuer of this certificate.

        :param issuer: The issuer.
        :type issuer: :py:class:`X509Name`

        :return: ``None``
        N)r rKZX509_set_issuer_namer�rt)rnr�rDrDrE�
set_issuer�s	zX509.set_issuercCs|jtj�}|jj|�|S)a�
        Return the subject of this certificate.

        This creates a new :class:`X509Name` that wraps the underlying subject
        name field on the certificate. Modifying it will modify the underlying
        certificate, and will have the effect of modifying any other
        :class:`X509Name` that refers to this subject.

        :return: The subject of this certificate.
        :rtype: :class:`X509Name`
        )rrK�X509_get_subject_namer�rr)rnrqrDrDrEr��szX509.get_subjectcCs|jtj|�|jj�dS)z�
        Set the subject of this certificate.

        :param subject: The subject.
        :type subject: :py:class:`X509Name`

        :return: ``None``
        N)r rKZX509_set_subject_namer�rt)rnr�rDrDrE�set_subject�s	zX509.set_subjectcCstj|j�S)z�
        Get the number of extensions on this certificate.

        :return: The number of extensions.
        :rtype: :py:class:`int`

        .. versionadded:: 0.12
        )rKZX509_get_ext_countr�)rnrDrDrE�get_extension_count�s	zX509.get_extension_countcCs@x:|D]2}t|t�std��tj|j|jd�}|st�qWdS)z�
        Add extensions to the certificate.

        :param extensions: The extensions to add.
        :type extensions: An iterable of :py:class:`X509Extension` objects.
        :return: ``None``
        z+One of the elements is not an X509Extensionr?Nr�)r]r#r`rKZX509_add_extr�r�r�)rnr�r�r�rDrDrEr��s

zX509.add_extensionscCsNtjt�}tj|j|�|_|jtjkr.td��tj	|j�}tj
|tj�|_|S)a�
        Get a specific extension of the certificate by index.

        Extensions on a certificate are kept in order. The index
        parameter selects which extension will be returned.

        :param int index: The index of the extension to retrieve.
        :return: The extension at the specified index.
        :rtype: :py:class:`X509Extension`
        :raises IndexError: If the extension index was out of bounds.

        .. versionadded:: 0.12
        zextension index out of bounds)r#r�rKZX509_get_extr�r�rRrV�
IndexErrorZX509_EXTENSION_duprWr�)rn�indexr�r�rDrDrE�
get_extension�s
zX509.get_extensionN)%r@rArBrCror�r�r�r�r�r�r�r�r7r�r�rrrrrrrrrrrrrr r!r"r�r$r%r�r(rDrDrDrEr%8sB		'




z.X509Type has been deprecated, use X509 insteadc@sReZdZdZejZejZej	Z
ejZej
ZejZejZejZejZejZejZdS)r&a
    Flags for X509 verification, used to change the behavior of
    :class:`X509Store`.

    See `OpenSSL Verification Flags`_ for details.

    .. _OpenSSL Verification Flags:
        https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_flags.html
    N)r@rArBrCrKZX509_V_FLAG_CRL_CHECKZ	CRL_CHECKZX509_V_FLAG_CRL_CHECK_ALLZ
CRL_CHECK_ALLZX509_V_FLAG_IGNORE_CRITICALZIGNORE_CRITICALZX509_V_FLAG_X509_STRICTZX509_STRICTZX509_V_FLAG_ALLOW_PROXY_CERTSZALLOW_PROXY_CERTSZX509_V_FLAG_POLICY_CHECKZPOLICY_CHECKZX509_V_FLAG_EXPLICIT_POLICYZEXPLICIT_POLICYZX509_V_FLAG_INHIBIT_MAPZINHIBIT_MAPZX509_V_FLAG_NOTIFY_POLICYZ
NOTIFY_POLICYZX509_V_FLAG_CHECK_SS_SIGNATUREZCHECK_SS_SIGNATUREZX509_V_FLAG_CB_ISSUER_CHECKZCB_ISSUER_CHECKrDrDrDrEr&s	c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
r'a�
    An X.509 store.

    An X.509 store is used to describe a context in which to verify a
    certificate. A description of a context may include a set of certificates
    to trust, a set of certificate revocation lists, verification flags and
    more.

    An X.509 store, being only a description, cannot be used by itself to
    verify a certificate. To carry out the actual verification process, see
    :class:`X509StoreContext`.
    cCstj�}tj|tj�|_dS)N)rKZX509_STORE_newrRrWZX509_STORE_free�_store)rn�storerDrDrEro6szX509Store.__init__cCsPt|t�st��tj|j|j�dkrLtj�}tj|�}t	|tj
k�tj�dS)a�
        Adds a trusted certificate to this store.

        Adding a certificate with this method adds this certificate as a
        *trusted* certificate.

        :param X509 cert: The certificate to add to this store.

        :raises TypeError: If the certificate is not an :class:`X509`.

        :raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your
            certificate.

        :return: ``None`` if the certificate was added successfully.
        rN)r]r%r_rKZX509_STORE_add_certr)r�ZERR_peek_errorZERR_GET_REASONrUZ!X509_R_CERT_ALREADY_IN_HASH_TABLEZERR_clear_error)rnr��codeZ
err_reasonrDrDrE�add_cert:s

zX509Store.add_certcCsttj|j|j�dk�dS)a�
        Add a certificate revocation list to this store.

        The certificate revocation lists added to a store will only be used if
        the associated flags are configured to check certificate revocation
        lists.

        .. versionadded:: 16.1.0

        :param CRL crl: The certificate revocation list to add to this store.
        :return: ``None`` if the certificate revocation list was added
            successfully.
        rN)rUrKZX509_STORE_add_crlr)�_crl)rn�crlrDrDrE�add_crlXszX509Store.add_crlcCsttj|j|�dk�dS)a�
        Set verification flags to this store.

        Verification flags can be combined by oring them together.

        .. note::

          Setting a verification flag sometimes requires clients to add
          additional information to the store, otherwise a suitable error will
          be raised.

          For example, in setting flags to enable CRL checking a
          suitable CRL must be added to the store otherwise an error will be
          raised.

        .. versionadded:: 16.1.0

        :param int flags: The verification flags to set on this store.
            See :class:`X509StoreFlags` for available constants.
        :return: ``None`` if the verification flags were successfully set.
        rN)rUrKZX509_STORE_set_flagsr))rn�flagsrDrDrE�	set_flagshszX509Store.set_flagscCsFtj�}tj|tj�}tj|t|jd���ttj	|j
|�dk�dS)a�
        Set the time against which the certificates are verified.

        Normally the current time is used.

        .. note::

          For example, you can determine if a certificate was valid at a given
          time.

        .. versionadded:: 17.0.0

        :param datetime vfy_time: The verification time to set on this store.
        :return: ``None`` if the verification time was successfully set.
        z%srN)rKZX509_VERIFY_PARAM_newrRrWZX509_VERIFY_PARAM_freeZX509_VERIFY_PARAM_set_timer}ZstrftimerUZX509_STORE_set1_paramr))rnZvfy_timeZparamrDrDrE�set_time�szX509Store.set_timeN)	r@rArBrCror,r/r1r2rDrDrDrEr'(sz8X509StoreType has been deprecated, use X509Store insteadcs eZdZdZ�fdd�Z�ZS)r(z�
    An exception raised when an error occurred while verifying a certificate
    using `OpenSSL.X509StoreContext.verify_certificate`.

    :ivar certificate: The certificate which caused verificate failure.
    :type certificate: :class:`X509`
    cstt|�j|�||_dS)N)r�r(ro�certificate)rn�messager3)r�rDrEro�szX509StoreContextError.__init__)r@rArBrCror�rDrD)r�rEr(�sc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)r)a�
    An X.509 store context.

    An X.509 store context is used to carry out the actual verification process
    of a certificate in a described context. For describing such a context, see
    :class:`X509Store`.

    :ivar _store_ctx: The underlying X509_STORE_CTX structure used by this
        instance.  It is dynamically allocated and automatically garbage
        collected.
    :ivar _store: See the ``store`` ``__init__`` parameter.
    :ivar _cert: See the ``certificate`` ``__init__`` parameter.
    :param X509Store store: The certificates which will be trusted for the
        purposes of any verifications.
    :param X509 certificate: The certificate to be verified.
    cCs0tj�}tj|tj�|_||_||_|j�dS)N)	rKZX509_STORE_CTX_newrRrWZX509_STORE_CTX_free�
_store_ctxr)�_cert�_init)rnr*r3Z	store_ctxrDrDrEro�s
zX509StoreContext.__init__cCs.tj|j|jj|jjtj�}|dkr*t�dS)z�
        Set up the store context for a subsequent verification operation.

        Calling this method more than once without first calling
        :meth:`_cleanup` will leak memory.
        rN)	rKZX509_STORE_CTX_initr5r)r6r�rRrVr�)rn�retrDrDrEr7�szX509StoreContext._initcCstj|j�dS)z�
        Internally cleans up the store context.

        The store context can then be reused with a new call to :meth:`_init`.
        N)rKZX509_STORE_CTX_cleanupr5)rnrDrDrE�_cleanup�szX509StoreContext._cleanupcCs\tj|j�tj|j�ttjtjtj|j����g}tj|j�}tj	|�}t
j|�}t||�S)z�
        Convert an OpenSSL native context error failure into a Python
        exception.

        When a call to native OpenSSL X509_verify_cert fails, additional
        information about the failure can be obtained from the store context.
        )
rKZX509_STORE_CTX_get_errorr5ZX509_STORE_CTX_get_error_depthr�rRrhZX509_verify_cert_error_stringZX509_STORE_CTX_get_current_certZX509_dupr%r�r()rn�errorsr�r6�pycertrDrDrE�_exception_from_context�s	




z(X509StoreContext._exception_from_contextcCs
||_dS)z�
        Set the context's X.509 store.

        .. versionadded:: 0.15

        :param X509Store store: The store description which will be used for
            the purposes of any *future* verifications.
        N)r))rnr*rDrDrE�	set_store�s	zX509StoreContext.set_storecCs8|j�|j�tj|j�}|j�|dkr4|j��dS)a"
        Verify a certificate in a context.

        .. versionadded:: 0.15

        :raises X509StoreContextError: If an error occurred when validating a
          certificate in the context. Sets ``certificate`` attribute to
          indicate which certificate caused the error.
        rN)r9r7rKZX509_verify_certr5r<)rnr8rDrDrE�verify_certificate�sz#X509StoreContext.verify_certificateN)
r@rArBrCror7r9r<r=r>rDrDrDrEr)�s

cCsvt|t�r|jd�}t|�}|tkr<tj|tjtjtj�}n |t	krTtj
|tj�}ntd��|tjkrlt�t
j|�S)a
    Load a certificate (X509) from the string *buffer* encoded with the
    type *type*.

    :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)

    :param bytes buffer: The buffer the certificate is stored in

    :return: The X509 object
    r�z3type argument must be FILETYPE_PEM or FILETYPE_ASN1)r]r�r�rZrrKZPEM_read_bio_X509rRrVrZd2i_X509_bior`r�r%r�)r�rXrMr
rDrDrEr*s


cCsnt�}|tkrtj||j�}n<|tkr6tj||j�}n$|tkrRtj||jdd�}nt	d��|dksft
�t|�S)a
    Dump the certificate *cert* into a buffer string encoded with the type
    *type*.

    :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1, or
        FILETYPE_TEXT)
    :param cert: The certificate to dump
    :return: The buffer with the dumped certificate in
    rzCtype argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXTr?)rZrrKZPEM_write_bio_X509r�rZi2d_X509_biorZ
X509_print_exr`�AssertionErrorr\)r�r�rM�result_coderDrDrEr+2s
cCsPt�}|tkrtj}n|tkr&tj}ntd��|||j�}|dkrHt�t	|�S)z�
    Dump a public key to a buffer.

    :param type: The file type (one of :data:`FILETYPE_PEM` or
        :data:`FILETYPE_ASN1`).
    :param PKey pkey: The public key to dump
    :return: The buffer with the dumped key in it.
    :rtype: bytes
    z3type argument must be FILETYPE_PEM or FILETYPE_ASN1r?)
rZrrKZPEM_write_bio_PUBKEYrZi2d_PUBKEY_bior`rvr�r\)r�rxrMZ	write_bior@rDrDrEr,Ms
c	Cst�}t|t�std��|dk	rR|dkr0td��tjt|��}|tjkrXt	d��ntj}t
||�}|tkr�tj||j
|tjd|j|j�}|j�nh|tkr�tj||j
�}nP|tkr�tj|j
�tjkr�td��tjtj|j
�tj�}tj||d�}nt	d��t|dk�t|�S)a�
    Dump the private key *pkey* into a buffer string encoded with the type
    *type*.  Optionally (if *type* is :const:`FILETYPE_PEM`) encrypting it
    using *cipher* and *passphrase*.

    :param type: The file type (one of :const:`FILETYPE_PEM`,
        :const:`FILETYPE_ASN1`, or :const:`FILETYPE_TEXT`)
    :param PKey pkey: The PKey to dump
    :param cipher: (optional) if encrypted PEM format, the cipher to use
    :param passphrase: (optional) if encrypted PEM format, this can be either
        the passphrase to use, or a callback for providing the passphrase.

    :return: The buffer with the dumped key in
    :rtype: bytes
    zpkey must be a PKeyNzDif a value is given for cipher one must also be given for passphrasezInvalid cipher namerz-Only RSA keys are supported for FILETYPE_TEXTzCtype argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT)rZr]rr_rKZEVP_get_cipherbynamer�rRrVr`�_PassphraseHelperrZPEM_write_bio_PrivateKeyrv�callback�
callback_args�raise_if_problemrZi2d_PrivateKey_biorr�r�rWr�r�Z	RSA_printrUr\)	r�rxZcipher�
passphraserMZ
cipher_obj�helperr@rrDrDrEr-fs<





c@sjeZdZdZdddddddgZd	d
�Zdd�Zd
d�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dS)r.z#
    A certificate revocation.
    sunspecifieds
keyCompromisesCACompromisesaffiliationChangeds
supersededscessationOfOperationscertificateHoldcCstj�}tj|tj�|_dS)N)rKZX509_REVOKED_newrRrW�X509_REVOKED_free�_revoked)rn�revokedrDrDrEro�szRevoked.__init__cCsftjtj�tj�}tjd�}||d<tj||�}|s<td��tjtj|tj	�tj
�}tj|j|�dS)z�
        Set the serial number.

        The serial number is formatted as a hexadecimal number encoded in
        ASCII.

        :param bytes hex_str: The new serial number.

        :return: ``None``
        zBIGNUM**rzbad hex stringN)
rRrWrKr~rrSrr`rrVr	ZX509_REVOKED_set_serialNumberrH)rnZhex_strrZ
bignum_ptrZ	bn_resultr
rDrDrE�
set_serial�s
zRevoked.set_serialcCs@t�}tj|j�}t|tjk�tj||�}t|dk�t|�S)z�
        Get the serial number.

        The serial number is formatted as a hexadecimal number encoded in
        ASCII.

        :return: The serial number.
        :rtype: bytes
        r)	rZrKZX509_REVOKED_get0_serialNumberrHrUrRrVZi2a_ASN1_INTEGERr\)rnrMZasn1_intr�rDrDrE�
get_serial�s
zRevoked.get_serialcCs`xZttj|j��D]F}tj|j|�}tj|�}tj|�tjkrtj|�tj	|j|�PqWdS)N)
r�rK�X509_REVOKED_get_ext_countrH�X509_REVOKED_get_extr�r��NID_crl_reasonr�ZX509_REVOKED_delete_ext)rnr�r�r�rDrDrE�_delete_reason�s

zRevoked._delete_reasoncCs�|dkr|j�n�t|t�s&td��n�|j�jdd�}dd�|jD�j|�}tj	�}t
|tjk�tj
|tj�}tj||�}t
|tjk�|j�tj|jtj|dd�}t
|dk�dS)	a�
        Set the reason of this revocation.

        If :data:`reason` is ``None``, delete the reason instead.

        :param reason: The reason string.
        :type reason: :class:`bytes` or :class:`NoneType`

        :return: ``None``

        .. seealso::

            :meth:`all_reasons`, which gives you a list of all supported
            reasons which you might pass to this method.
        Nz$reason must be None or a byte string� �cSsg|]}|j��qSrD)�lower)r��rrDrDrErsz&Revoked.set_reason.<locals>.<listcomp>rr?)rOr]r^r_rR�replace�_crl_reasonsr'rKZASN1_ENUMERATED_newrUrRrVrWZASN1_ENUMERATED_freeZASN1_ENUMERATED_setZX509_REVOKED_add1_ext_i2drHrN)rn�reasonZreason_codeZnew_reason_extrcr�rDrDrE�
set_reason�s


zRevoked.set_reasoncCs�x�ttj|j��D]l}tj|j|�}tj|�}tj|�tjkrt�}tj	||dd�}|svtj
|tj|��}t|dk�t
|�SqWdS)a 
        Get the reason of this revocation.

        :return: The reason, or ``None`` if there is none.
        :rtype: bytes or NoneType

        .. seealso::

            :meth:`all_reasons`, which gives you a list of all supported
            reasons this method might return.
        rN)r�rKrLrHrMr�r�rNrZr�ZM_ASN1_OCTET_STRING_printr�rUr\)rnr�r�r�rMr�rDrDrE�
get_reasons
zRevoked.get_reasoncCs|jdd�S)a
        Return a list of all the supported reason strings.

        This list is a copy; modifying it does not change the supported reason
        strings.

        :return: A list of reason strings.
        :rtype: :class:`list` of :class:`bytes`
        N)rU)rnrDrDrE�all_reasons)s
zRevoked.all_reasonscCstj|j�}t||�S)z�
        Set the revocation timestamp.

        :param bytes when: The timestamp of the revocation,
            as ASN.1 TIME.
        :return: ``None``
        )rK� X509_REVOKED_get0_revocationDaterHrd)rnrb�dtrDrDrE�set_rev_date5szRevoked.set_rev_datecCstj|j�}t|�S)z�
        Get the revocation timestamp.

        :return: The timestamp of the revocation, as ASN.1 TIME.
        :rtype: bytes
        )rKrZrHrk)rnr[rDrDrE�get_rev_date@szRevoked.get_rev_dateN)r@rArBrCrUrorJrKrOrWrXrYr\r]rDrDrDrEr.�s"	$c@s|eZdZdZdd�Zdd�Zedd��Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zedefdd�ZdS)r/z(
    A certificate revocation list.
    cCstj�}tj|tj�|_dS)N)rKZX509_CRL_newrRrW�
X509_CRL_freer-)rnr.rDrDrEroPszCRL.__init__cCsddlm}t�}|||j�S)z�
        Export as a ``cryptography`` CRL.

        :rtype: ``cryptography.x509.CertificateRevocationList``

        .. versionadded:: 17.1.0
        r)�_CertificateRevocationList)r�r_rGr-)rnr_rFrDrDrEr�TszCRL.to_cryptographycCs&t|tj�std��|�}|j|_|S)a
        Construct based on a ``cryptography`` *crypto_crl*.

        :param crypto_crl: A ``cryptography`` certificate revocation list
        :type crypto_crl: ``cryptography.x509.CertificateRevocationList``

        :rtype: CRL

        .. versionadded:: 17.1.0
        z%Must be a certificate revocation list)r]r
ZCertificateRevocationListr_Z	_x509_crlr-)r{Z
crypto_crlr.rDrDrEr�bs
zCRL.from_cryptographycCsrg}tj|j�}xPttj|��D]>}tj||�}tj|�}tjt�}t	j
|tj�|_|j
|�q W|rnt|�SdS)aA
        Return the revocations in this certificate revocation list.

        These revocations will be provided by value, not by reference.
        That means it's okay to mutate them: it won't affect this CRL.

        :return: The revocations in this CRL.
        :rtype: :class:`tuple` of :class:`Revocation`
        N)rKZX509_CRL_get_REVOKEDr-r�Zsk_X509_REVOKED_numZsk_X509_REVOKED_value�Cryptography_X509_REVOKED_dupr.r�rRrWrGrHrp�tuple)rn�resultsZ
revoked_stackr�rIZrevoked_copyZpyrevrDrDrE�get_revokedus


zCRL.get_revokedcCs8tj|j�}t|tjk�tj|j|�}t|dk�dS)a9
        Add a revoked (by value not reference) to the CRL structure

        This revocation will be added by value, not by reference. That
        means it's okay to mutate it after adding: it won't affect
        this CRL.

        :param Revoked revoked: The new revocation.
        :return: ``None``
        rN)rKr`rHrUrRrVZX509_CRL_add0_revokedr-)rnrI�copyr�rDrDrE�add_revoked�szCRL.add_revokedcCsBtjtj|j��}t|tjk�tj|tj�}t	j
t	�}||_|S)zc
        Get the CRL's issuer.

        .. versionadded:: 16.1.0

        :rtype: X509Name
        )rKr�ZX509_CRL_get_issuerr-rUrRrVrWr�r"r�rs)rnZ_issuerr�rDrDrEr!�s
zCRL.get_issuercCsttj|j|�dk�dS)z�
        Set the CRL version.

        .. versionadded:: 16.1.0

        :param int version: The version of the CRL.
        :return: ``None``
        rN)rUrKZX509_CRL_set_versionr-)rnr�rDrDrEr��s	zCRL.set_versioncCst||j�|�S)N)rdr-)rnrrbrDrDrEr�szCRL._set_boundary_timecCs|jtj|�S)z�
        Set when the CRL was last updated.

        The timestamp is formatted as an ASN.1 TIME::

            YYYYMMDDhhmmssZ

        .. versionadded:: 16.1.0

        :param bytes when: A timestamp string.
        :return: ``None``
        )rrKZX509_CRL_get_lastUpdate)rnrbrDrDrE�set_lastUpdate�s
zCRL.set_lastUpdatecCs|jtj|�S)z�
        Set when the CRL will next be udpated.

        The timestamp is formatted as an ASN.1 TIME::

            YYYYMMDDhhmmssZ

        .. versionadded:: 16.1.0

        :param bytes when: A timestamp string.
        :return: ``None``
        )rrKZX509_CRL_get_nextUpdate)rnrbrDrDrE�set_nextUpdate�s
zCRL.set_nextUpdatecCs\tj|�}t|tjk�tj|jtj|j��tj	|j�tj
|j|j|�}t|dk�dS)a;
        Sign the CRL.

        Signing a CRL enables clients to associate the CRL itself with an
        issuer. Before a CRL is meaningful to other OpenSSL functions, it must
        be signed by an issuer.

        This method implicitly sets the issuer's name based on the issuer
        certificate and private key used to sign the CRL.

        .. versionadded:: 16.1.0

        :param X509 issuer_cert: The issuer's certificate.
        :param PKey issuer_key: The issuer's private key.
        :param bytes digest: The digest method to sign the CRL with.
        rN)rKr�rUrRrV�X509_CRL_set_issuer_namer-r#r�Z
X509_CRL_sort�
X509_CRL_signrv)rnr�Z
issuer_keyr�r�r�rDrDrEr7�s
zCRL.sign�dc
Cst|t�std��t|t�s$td��t|t�s6td��|tkrFtd��tj|�}|tj	krbt
d��tjtj��}t
|tj	k�tj�}t
|tj	k�tj|d�tj|j|�tj||ddd�tj|j|�tj|jtj|j��tj|j|j|�}	|	�st�t||�S)	a�
        Export the CRL as a string.

        :param X509 cert: The certificate used to sign the CRL.
        :param PKey key: The key used to sign the CRL.
        :param int type: The export format, either :data:`FILETYPE_PEM`,
            :data:`FILETYPE_ASN1`, or :data:`FILETYPE_TEXT`.
        :param int days: The number of days until the next update of this CRL.
        :param bytes digest: The name of the message digest to use (eg
            ``b"sha2566"``).
        :rtype: bytes
        zcert must be an X509 instancezkey must be a PKey instanceztype must be an integerzdigest must be providedzNo such digest methodr��<)r]r%r_rr}�_UNSPECIFIEDrKr�rRrVr`rPrQrUZ
ASN1_TIME_newrZX509_CRL_set_lastUpdater-ZX509_CRL_set_nextUpdaterhr#r�rirvr�r9)
rnr�r�r�Zdaysr�r�rMZsometimer�rDrDrE�export�s2




z
CRL.exportN)r@rArBrCror�r�r�rcrer!r�rrfrgr7rrmrnrDrDrDrEr/Ksz,CRLType has been deprecated, use CRL insteadc@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)r0cCsttj|j��S)zm
        Check if this NID_pkcs7_signed object

        :return: True if the PKCS7 is of type signed
        )�boolrKZPKCS7_type_is_signed�_pkcs7)rnrDrDrE�type_is_signed-	szPKCS7.type_is_signedcCsttj|j��S)zt
        Check if this NID_pkcs7_enveloped object

        :returns: True if the PKCS7 is of type enveloped
        )rorKZPKCS7_type_is_envelopedrp)rnrDrDrE�type_is_enveloped5	szPKCS7.type_is_envelopedcCsttj|j��S)z�
        Check if this NID_pkcs7_signedAndEnveloped object

        :returns: True if the PKCS7 is of type signedAndEnveloped
        )rorKZ PKCS7_type_is_signedAndEnvelopedrp)rnrDrDrE�type_is_signedAndEnveloped=	sz PKCS7.type_is_signedAndEnvelopedcCsttj|j��S)zi
        Check if this NID_pkcs7_data object

        :return: True if the PKCS7 is of type data
        )rorKZPKCS7_type_is_datarp)rnrDrDrE�type_is_dataE	szPKCS7.type_is_datacCs"tj|jj�}tj|�}tj|�S)zk
        Returns the type name of the PKCS7 structure

        :return: A string with the typename
        )rKr�rpr�r�rRrh)rnr�Zstring_typerDrDrE�
get_type_nameM	s
zPKCS7.get_type_nameN)r@rArBrqrrrsrtrurDrDrDrEr0,	s
z0PKCS7Type has been deprecated, use PKCS7 insteadc@sbeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zddd�Z
dS)r1z
    A PKCS #12 archive.
    cCsd|_d|_d|_d|_dS)N)rvr6�_cacerts�
_friendlyname)rnrDrDrErod	szPKCS12.__init__cCs|jS)z�
        Get the certificate in the PKCS #12 structure.

        :return: The certificate, or :py:const:`None` if there is none.
        :rtype: :py:class:`X509` or :py:const:`None`
        )r6)rnrDrDrE�get_certificatej	szPKCS12.get_certificatecCst|t�std��||_dS)z�
        Set the certificate in the PKCS #12 structure.

        :param cert: The new certificate, or :py:const:`None` to unset it.
        :type cert: :py:class:`X509` or :py:const:`None`

        :return: ``None``
        zcert must be an X509 instanceN)r]r%r_r6)rnr�rDrDrE�set_certificates	s	
zPKCS12.set_certificatecCs|jS)z�
        Get the private key in the PKCS #12 structure.

        :return: The private key, or :py:const:`None` if there is none.
        :rtype: :py:class:`PKey`
        )rv)rnrDrDrE�get_privatekey�	szPKCS12.get_privatekeycCst|t�std��||_dS)z�
        Set the certificate portion of the PKCS #12 structure.

        :param pkey: The new private key, or :py:const:`None` to unset it.
        :type pkey: :py:class:`PKey` or :py:const:`None`

        :return: ``None``
        zpkey must be a PKey instanceN)r]rr_rv)rnrxrDrDrE�set_privatekey�	s	
zPKCS12.set_privatekeycCs|jdk	rt|j�SdS)a
        Get the CA certificates in the PKCS #12 structure.

        :return: A tuple with the CA certificates in the chain, or
            :py:const:`None` if there are none.
        :rtype: :py:class:`tuple` of :py:class:`X509` or :py:const:`None`
        N)rvra)rnrDrDrE�get_ca_certificates�	s
zPKCS12.get_ca_certificatescCsB|dkrd|_n.t|�}x|D]}t|t�std��qW||_dS)a
        Replace or set the CA certificates within the PKCS12 object.

        :param cacerts: The new CA certificates, or :py:const:`None` to unset
            them.
        :type cacerts: An iterable of :py:class:`X509` or :py:const:`None`

        :return: ``None``
        Nz)iterable must only contain X509 instances)rv�listr]r%r_)rn�cacertsr�rDrDrE�set_ca_certificates�	s



zPKCS12.set_ca_certificatescCs2|dkrd|_nt|t�s(td|f��||_dS)z�
        Set the friendly name in the PKCS #12 structure.

        :param name: The new friendly name, or :py:const:`None` to unset.
        :type name: :py:class:`bytes` or :py:const:`None`

        :return: ``None``
        Nz+name must be a byte string or None (not %r))rwr]r^r_)rnrqrDrDrE�set_friendlyname�	s	
zPKCS12.set_friendlynamecCs|jS)z�
        Get the friendly name in the PKCS# 12 structure.

        :returns: The friendly name,  or :py:const:`None` if there is none.
        :rtype: :py:class:`bytes` or :py:const:`None`
        )rw)rnrDrDrE�get_friendlyname�	szPKCS12.get_friendlynameN�r?c
Cs�td|�}|jdkrtj}n4tj�}tj|tj�}x|jD]}tj||j	�q:W|dkr^tj}|j
}|dkrrtj}|jdkr�tj}n|jj}|jdkr�tj}n|jj	}tj
|||||tjtj||d�
}|tjkr�t�tj|tj�}t�}	tj|	|�t|	�S)a�
        Dump a PKCS12 object as a string.

        For more information, see the :c:func:`PKCS12_create` man page.

        :param passphrase: The passphrase used to encrypt the structure. Unlike
            some other passphrase arguments, this *must* be a string, not a
            callback.
        :type passphrase: :py:data:`bytes`

        :param iter: Number of times to repeat the encryption step.
        :type iter: :py:data:`int`

        :param maciter: Number of times to repeat the MAC step.
        :type maciter: :py:data:`int`

        :return: The string representation of the PKCS #12 structure.
        :rtype:
        rENr)�_text_to_bytes_and_warnrvrRrVrKZsk_X509_new_nullrW�sk_X509_freeZsk_X509_pushr�rwrvr6Z
PKCS12_createZ&NID_pbe_WithSHA1And3_Key_TripleDES_CBCr��PKCS12_freerZZi2d_PKCS12_bior\)
rnrE�iterZmaciterr~r��friendlynamerx�pkcs12rMrDrDrErn�	s:






z
PKCS12.export)Nr�r?)r@rArBrCrorxryrzr{r|rr�r�rnrDrDrDrEr1_	s	
	
	z2PKCS12Type has been deprecated, use PKCS12 insteadc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)r2z!
    A Netscape SPKI object.
    cCstj�}tj|tj�|_dS)N)rKZNETSCAPE_SPKI_newrRrWZNETSCAPE_SPKI_free�_spki)rnZspkirDrDrEro
szNetscapeSPKI.__init__cCs^|jrtd��|jstd��tjt|��}|tjkr<td��tj|j	|j
|�}t|dk�dS)a
        Sign the certificate request with this key and digest type.

        :param pkey: The private key to sign with.
        :type pkey: :py:class:`PKey`

        :param digest: The message digest to use.
        :type digest: :py:class:`bytes`

        :return: ``None``
        zKey has only public partzKey is uninitializedzNo such digest methodrN)ryr`rwrKr�r�rRrVZNETSCAPE_SPKI_signr�rvrU)rnrxr�r�r�rDrDrEr7
s
zNetscapeSPKI.signcCs"tj|j|j�}|dkrt�dS)aY
        Verifies a signature on a certificate request.

        :param PKey key: The public key that signature is supposedly from.

        :return: ``True`` if the signature is correct.
        :rtype: bool

        :raises OpenSSL.crypto.Error: If the signature is invalid, or there was
            a problem verifying the signature.
        rT)rKZNETSCAPE_SPKI_verifyr�rvr�)rnr�ZanswerrDrDrEr88
szNetscapeSPKI.verifycCs$tj|j�}tj|�}tj|�|S)z�
        Generate a base64 encoded representation of this SPKI object.

        :return: The base64 encoded string.
        :rtype: :py:class:`bytes`
        )rKZNETSCAPE_SPKI_b64_encoder�rRrhr�)rnZencodedr�rDrDrE�
b64_encodeI
s

zNetscapeSPKI.b64_encodecCsDtjt�}tj|j�|_t|jtjk�tj	|jtj
�|_d|_|S)z|
        Get the public key of this certificate.

        :return: The public key.
        :rtype: :py:class:`PKey`
        T)rr�rKZNETSCAPE_SPKI_get_pubkeyr�rvrUrRrVrWrury)rnrxrDrDrEr�U
s
zNetscapeSPKI.get_pubkeycCs tj|j|j�}t|dk�dS)zv
        Set the public key of the certificate

        :param pkey: The public key
        :return: ``None``
        r?N)rKZNETSCAPE_SPKI_set_pubkeyr�rvrU)rnrxrcrDrDrEr�c
szNetscapeSPKI.set_pubkeyN)
r@rArBrCror7r8r�r�r�rDrDrDrEr2
sz>NetscapeSPKIType has been deprecated, use NetscapeSPKI insteadc@sBeZdZd
dd�Zedd��Zedd��Zefdd	�Zd
d�Z	dS)rAFcCs4|tkr|dk	rtd��||_||_||_g|_dS)Nz0only FILETYPE_PEM key format supports encryption)rr`�_passphrase�
_more_args�	_truncate�	_problems)rnr�rEZ	more_args�truncaterDrDrErov
sz_PassphraseHelper.__init__cCsF|jdkrtjSt|jt�r"tjSt|j�r:tjd|j�Std��dS)NZpem_password_cbz2Last argument must be a byte string or a callable.)	r�rRrVr]r^�callablerB�_read_passphraser_)rnrDrDrErB�
s

z_PassphraseHelper.callbackcCs>|jdkrtjSt|jt�r"|jSt|j�r2tjStd��dS)Nz2Last argument must be a byte string or a callable.)r�rRrVr]r^r�r_)rnrDrDrErC�
s

z_PassphraseHelper.callback_argscCs8|jr4yt|�Wn|k
r&YnX|jjd��dS)Nr)r��_exception_from_error_queue�pop)rnZ
exceptionTyperDrDrErD�
sz"_PassphraseHelper.raise_if_problemcCs�y�|jr|j|||�}n
|j|�}t|t�s4td��t|�|kr\|jrT|d|�}ntd��x(tt|��D]}|||d�||<qjWt|�Stk
r�}z|j	j
|�dSd}~XnXdS)NzString expectedz+passphrase returned by callback is too longr?r)r�r�r]r^r`rTr�r��	Exceptionr�rp)rnZbuf�sizeZrwflagZuserdatar�r��erDrDrEr��
s"

z"_PassphraseHelper._read_passphraseN)FF)
r@rArBror�rBrCrrDr�rDrDrDrErAu
s




rAcCs�t|t�r|jd�}t|�}|tkr<tj|tjtjtj�}n |t	krTtj
|tj�}ntd��|tjkrlt�t
jt
�}tj|tj�|_d|_|S)a<
    Load a public key from a buffer.

    :param type: The file type (one of :data:`FILETYPE_PEM`,
        :data:`FILETYPE_ASN1`).
    :param buffer: The buffer the key is stored in.
    :type buffer: A Python string object, either unicode or bytestring.
    :return: The PKey object.
    :rtype: :class:`PKey`
    r�z3type argument must be FILETYPE_PEM or FILETYPE_ASN1T)r]r�r�rZrrKZPEM_read_bio_PUBKEYrRrVrZd2i_PUBKEY_bior`r�rr�rWrurvry)r�rXrM�evp_pkeyrxrDrDrEr3�
s



cCs�t|t�r|jd�}t|�}t||�}|tkrNtj|tj	|j
|j�}|j�n |t
krftj|tj	�}ntd��|tj	kr~t�tjt�}tj|tj�|_|S)a�
    Load a private key (PKey) from the string *buffer* encoded with the type
    *type*.

    :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
    :param buffer: The buffer the key is stored in
    :param passphrase: (optional) if encrypted PEM format, this can be
                       either the passphrase to use, or a callback for
                       providing the passphrase.

    :return: The PKey object
    r�z3type argument must be FILETYPE_PEM or FILETYPE_ASN1)r]r�r�rZrArrKZPEM_read_bio_PrivateKeyrRrVrBrCrDrZd2i_PrivateKey_bior`r�rr�rWrurv)r�rXrErMrFr�rxrDrDrEr4�
s 





cCsnt�}|tkrtj||j�}n<|tkr6tj||j�}n$|tkrRtj||jdd�}nt	d��t
|dk�t|�S)a
    Dump the certificate request *req* into a buffer string encoded with the
    type *type*.

    :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
    :param req: The certificate request to dump
    :return: The buffer with the dumped certificate request in
    rzCtype argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT)rZrrKZPEM_write_bio_X509_REQr�rZi2d_X509_REQ_biorZX509_REQ_print_exr`rUr\)r�r�rMr@rDrDrEr5s	cCs�t|t�r|jd�}t|�}|tkr<tj|tjtjtj�}n |t	krTtj
|tj�}ntd��t|tjk�t
jt
�}tj|tj�|_|S)a
    Load a certificate request (X509Req) from the string *buffer* encoded with
    the type *type*.

    :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
    :param buffer: The buffer the certificate request is stored in
    :return: The X509Req object
    r�z3type argument must be FILETYPE_PEM or FILETYPE_ASN1)r]r�r�rZrrKZPEM_read_bio_X509_REQrRrVrZd2i_X509_REQ_bior`rUr$r�rWr�r�)r�rXrMr�Zx509reqrDrDrEr6s	


c	Cs�td|�}tjt|��}|tjkr*td��tj�}tj|tj	�}tj
||�tj||t|��tj
|j�}t|dk�tjd|�}tjd�}tj||||j�}t|dk�tj||d�dd�S)z�
    Sign a data string using the given key and message digest.

    :param pkey: PKey to sign with
    :param data: data to be signed
    :param digest: message digest to use
    :return: signature

    .. versionadded:: 0.11
    rYzNo such digest methodrzunsigned char[]zunsigned int *r?N)r�rKr�r�rRrVr`�Cryptography_EVP_MD_CTX_newrW�Cryptography_EVP_MD_CTX_freeZEVP_SignInitZEVP_SignUpdaterTZ
EVP_PKEY_sizervrUrSZ
EVP_SignFinalrX)	rxrYr�r��md_ctxr�Zsignature_bufferZsignature_lengthZfinal_resultrDrDrEr79s 


cCs�td|�}tjt|��}|tjkr*td��tj|j�}t	|tjk�tj
|tj�}tj�}tj
|tj
�}tj||�tj||t|��tj||t|�|�}|dkr�t�dS)a�
    Verify the signature for a data string.

    :param cert: signing certificate (X509 object) corresponding to the
        private key which generated the signature.
    :param signature: signature returned by sign function
    :param data: data to be verified
    :param digest: message digest to use
    :return: ``None`` if the signature is correct, raise exception otherwise.

    .. versionadded:: 0.11
    rYzNo such digest methodr?N)r�rKr�r�rRrVr`r�r�rUrWrur�r�ZEVP_VerifyInitZEVP_VerifyUpdaterTZEVP_VerifyFinalr�)r�Z	signaturerYr�r�rxr�Z
verify_resultrDrDrEr8[s

cCsjt�}|tkrtj||j�}n8|tkr6tj||j�}n |tkrNtj||j�}nt	d��|dksbt
�t|�S)a
    Dump a certificate revocation list to a buffer.

    :param type: The file type (one of ``FILETYPE_PEM``, ``FILETYPE_ASN1``, or
        ``FILETYPE_TEXT``).
    :param CRL crl: The CRL to dump.

    :return: The buffer with the CRL.
    :rtype: bytes
    zCtype argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXTr?)rZrrKZPEM_write_bio_X509_CRLr-rZi2d_X509_CRL_biorZX509_CRL_printr`r?r\)r�r.rMr8rDrDrEr9scCs�t|t�r|jd�}t|�}|tkr<tj|tjtjtj�}n |t	krTtj
|tj�}ntd��|tjkrlt�t
jt
�}tj|tj�|_|S)a
    Load Certificate Revocation List (CRL) data from a string *buffer*.
    *buffer* encoded with the type *type*.

    :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
    :param buffer: The buffer the CRL is stored in

    :return: The PKey object
    r�z3type argument must be FILETYPE_PEM or FILETYPE_ASN1)r]r�r�rZrrKZPEM_read_bio_X509_CRLrRrVrZd2i_X509_CRL_bior`r�r/r�rWr^r-)r�rXrMr.r�rDrDrEr:�s




cCs�t|t�r|jd�}t|�}|tkr<tj|tjtjtj�}n |t	krTtj
|tj�}ntd��|tjkrlt�t
jt
�}tj|tj�|_|S)z�
    Load pkcs7 data from the string *buffer* encoded with the type
    *type*.

    :param type: The file type (one of FILETYPE_PEM or FILETYPE_ASN1)
    :param buffer: The buffer with the pkcs7 data.
    :return: The PKCS7 object
    r�z3type argument must be FILETYPE_PEM or FILETYPE_ASN1)r]r�r�rZrrKZPEM_read_bio_PKCS7rRrVrZ
d2i_PKCS7_bior`r�r0r�rWZ
PKCS7_freerp)r�rXrMZpkcs7Zpypkcs7rDrDrEr;�s	



cCs�td|�}t|t�r|jd�}t|�}|s0tj}tj|tj�}|tjkrNt	�tj
|tj�}tjd�}tjd�}tjd�}tj
|||||�}|s�t	�tj
|dtj�}y
t	�Wntk
r�YnX|dtjkr�d}ntjt�}tj
|dtj�|_|dtjk�rd}	d}
nPtj|d�}	tjd�}tj|d|�}tj||d�dd�}
|tjk�rdd}
g}
x8ttj|��D]&}tj||�}tj|�}|
j|��qxW|
�s�d}
tjt�}||_|	|_|
|_|
|_|S)	a�
    Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
    encrypted, a *passphrase* must be included.  The MAC is always
    checked and thus required.

    See also the man page for the C function :py:func:`PKCS12_parse`.

    :param buffer: The buffer the certificate is stored in
    :param passphrase: (Optional) The password to decrypt the PKCS12 lump
    :returns: The PKCS12 object
    rEr�z
EVP_PKEY**zX509**zCryptography_STACK_OF_X509**rNzint*) r�r]r�r�rZrRrVrKZd2i_PKCS12_bior�rWr�rSZPKCS12_parser�rrr�rurvr%r�ZX509_alias_get0rXr�Zsk_X509_numZ
sk_X509_valuerpr1r6rvrw)rXrErMZp12rxr�r~Zparse_resultZpykeyr;r�Zfriendlyname_lengthZfriendlyname_bufferZ	pycacertsr�r
Zpycacertr�rDrDrEr<�sb











sutf8onlyii��)N)NN)N)N)tr�base64r�	functoolsr�operatorrrrrrr	Zsixr
rrr�rr�Zcryptographyr
Z)cryptography.hazmat.primitives.asymmetricrrZcryptography.utilsrZ
OpenSSL._utilrrRrrKrr�rr�rr�rrmrr�rZ_make_assert�__all__ZSSL_FILETYPE_PEMrZSSL_FILETYPE_ASN1rrr�rZEVP_PKEY_DSArZEVP_PKEY_DHZTYPE_DHZEVP_PKEY_ECZTYPE_ECr�rr�rUrGrJrZr\rdrk�objectrlrr@�DeprecationWarningZPKeyTyper�r r!r"ZX509NameTyper#ZX509ExtensionTyper$ZX509ReqTyper%ZX509Typer&r'Z
X509StoreTyper(r)r*r+r,r-r.r/ZCRLTyper0Z	PKCS7Typer1Z
PKCS12Typer2ZNetscapeSPKITyperAr3r4r5r6r7r8r9r:r;r<ZOpenSSL_add_all_algorithmsZSSL_load_error_stringsZ ASN1_STRING_set_default_mask_ascrDrDrDrE�<module>s$ (
	
	(
jBHUoh
;+[,/ZG!
$"$
`

?>