U
    2ÜŸjÆ ã                   @  s:  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZ ejdkr†d dlmZ n4ejd	k r®e d
¡Zddddœdd„Znd dlmZ d dlmZmZ d dlmZmZmZmZmZ d dlmZ d dlm Z! d dlm"Z# d dlm$Z% d dlm&Z' d dlm(Z) d dlm*Z+ ddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1gZ,eej-ej.ej/ej0ej1f Z2eej3ej4ej5ej6ej7f Z8ee2e8f Z9ee:ed2e:f f Z;e'j<Z=d3e>d< e'j?Z@d3e>d< d4ZAe'jBZCd3e>d< e'jDZEd3e>d< e'jFZGd3e>d5< e'jHZId3e>d6< G d7d „ d eJƒZKee#eKƒZLe)eKƒZMdsd8d9d:œd;d<„ZNd9d=d>œd?d@„ZOd9d=dAdBœdCdD„ZPd=d9dEœdFdG„ZQd9d8dHœdIdJ„ZRG dKdL„ dLƒZSG dMd!„ d!ƒZTG dNdO„ dOƒZUedPƒdQdRœdSd-„ƒZVedTƒddOdUœdVd,„ƒZWejXG dWd"„ d"ƒƒZYedXƒG dYd#„ d#ƒƒZZG dZd„ dƒZ[G d[d'„ d'ƒZ\G d\d$„ d$ƒZ]G d]d&„ d&eJƒZ^G d^d%„ d%ƒZ_d3d=dd_œd`d.„Z`d3dd=daœdbd(„Zad3d!d=dcœddd+„Zbdtd3d!dedfd=dgœdhd*„ZcG didj„ djƒZdd3dkd!d_œdld1„Zedud3dkdfd!dmœdnd0„Zfd3d#d=doœdpd)„ZgegZhejegeidXejd)dq� d3d=d#d_œdrd/„ZkekZlejekeidXejd/dq� dS )vé    )ÚannotationsN)Ú	b16encode)ÚSequence)Úpartial)ÚAnyÚCallableÚUnion)é   é   )Ú
deprecated)r	   é   ÚTÚstrÚobjectzCallable[[_T], _T])ÚmsgÚkwargsÚreturnc                 K  s   dd„ S )Nc                 S  s   | S ©N© )Úfr   r   ú2/tmp/pip-unpacked-wheel-y9ao6mel/OpenSSL/crypto.pyÚ<lambda>   ó    zdeprecated.<locals>.<lambda>r   )r   r   r   r   r   r      s    r   )ÚutilsÚx509)ÚdsaÚecÚed448Úed25519Úrsa)ÚStrOrBytesPath)Úbyte_string)Úexception_from_error_queue)Úffi)Úlib)Úmake_assert)Ú
path_bytesÚFILETYPE_ASN1ÚFILETYPE_PEMÚFILETYPE_TEXTÚTYPE_DSAÚTYPE_RSAÚX509ÚErrorÚPKeyÚX509NameÚX509ReqÚ	X509StoreÚX509StoreContextÚX509StoreContextErrorÚX509StoreFlagsÚdump_certificateÚdump_certificate_requestÚdump_privatekeyÚdump_publickeyÚget_elliptic_curveÚget_elliptic_curvesÚload_certificateÚload_certificate_requestÚload_privatekeyÚload_publickey.Úintiÿÿ  ÚTYPE_DHÚTYPE_ECc                   @  s   e Zd ZdZdS )r-   z7
    An error occurred in an `OpenSSL.crypto` API.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r-   r   s   úbytes | Noner   )Úbufferr   c                 C  sn   | dkrt  t  ¡ ¡}t j}n2t d| ¡}t  |t| ƒ¡}|fddddœdd„}t|tj	kƒ t 
||¡}|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.
    Núchar[]r   )ÚbioÚrefr   c                 S  s
   t  | ¡S r   )Ú_libÚBIO_free)rI   rJ   r   r   r   Úfree�   s    z_new_mem_buf.<locals>.free)rK   ZBIO_newZ	BIO_s_memrL   Ú_ffiÚnewZBIO_new_mem_bufÚlenÚ_openssl_assertÚNULLÚgc)rG   rI   rM   Údatar   r   r   Ú_new_mem_buf|   s    	rU   Úbytes)rI   r   c                 C  s.   t  d¡}t | |¡}t  |d |¡dd… S )zO
    Copy the contents of an OpenSSL BIO object into a Python byte string.
    zchar**r   N)rN   rO   rK   ZBIO_get_mem_datarG   )rI   Úresult_bufferZbuffer_lengthr   r   r   Ú_bio_to_string–   s    
rX   ÚNone)ÚboundaryÚwhenr   c                 C  s@   t |tƒstdƒ‚t| tjkƒ t | |¡}|dkr<tdƒ‚dS )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.
    zwhen must be a byte stringr   zInvalid stringN)	Ú
isinstancerV   Ú	TypeErrorrQ   rN   rR   rK   ZASN1_TIME_set_stringÚ
ValueError)rZ   r[   Ú
set_resultr   r   r   Ú_set_asn1_timeŸ   s    
r`   ©r[   r   c                 C  s2   t  ¡ }t|tjkƒ t |t j¡}t|| ƒ |S )aŒ  
    Behaves like _set_asn1_time but returns a new ASN1_TIME object.

    @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.
    )rK   ZASN1_TIME_newrQ   rN   rR   rS   ZASN1_TIME_freer`   )r[   Úretr   r   r   Ú_new_asn1_time¸   s
    
rc   )Ú	timestampr   c                 C  s    t  d| ¡}t |¡dkrdS t |¡tjkr>t  t |¡¡S t  d¡}t 	| |¡ t
|d t jkƒ t  d|d ¡}t |¡}t  |¡}t |d ¡ |S dS )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*r   NzASN1_GENERALIZEDTIME**)rN   ÚcastrK   ÚASN1_STRING_lengthZASN1_STRING_typeZV_ASN1_GENERALIZEDTIMEÚstringÚASN1_STRING_get0_datarO   ZASN1_TIME_to_generalizedtimerQ   rR   ZASN1_GENERALIZEDTIME_free)rd   Zstring_timestampZgeneralized_timestampZstring_dataÚstring_resultr   r   r   Ú_get_asn1_timeË   s    
ÿ


rj   c                   @  s8   e Zd Zddœdd„Zdddœdd„Zddœd	d
„ZdS )Ú_X509NameInvalidatorrY   ©r   c                 C  s
   g | _ d S r   )Ú_names©Úselfr   r   r   Ú__init__é   s    z_X509NameInvalidator.__init__r/   ©Únamer   c                 C  s   | j  |¡ d S r   )rm   Úappend©ro   rr   r   r   r   Úaddì   s    z_X509NameInvalidator.addc                 C  s   | j D ]}|`qd S r   )rm   Ú_namert   r   r   r   Úclearï   s    
z_X509NameInvalidator.clearN)rB   rC   rD   rp   ru   rw   r   r   r   r   rk   è   s   rk   c                   @  s„   e Zd ZdZdZdZddœdd„Zddœd	d
„Zedd dœdd„ƒZ	ddddœdd„Z
ddœdd„Zddœdd„Zddœdd„ZdS )r.   zD
    A class representing an DSA or RSA public key or key pair.
    FTrY   rl   c                 C  s"   t  ¡ }t |t j¡| _d| _d S )NF)rK   ZEVP_PKEY_newrN   rS   ÚEVP_PKEY_freeÚ_pkeyÚ_initialized©ro   Úpkeyr   r   r   rp   ý   s    zPKey.__init__Ú_Keyc                 C  sR   ddl m}m} | jr0tt| ƒ}t t||ƒ¡S t	t| ƒ}t t||dd�¡S dS )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
        r   )Úload_der_private_keyÚload_der_public_keyN)Úpassword)
Ú,cryptography.hazmat.primitives.serializationr~   r   Ú_only_publicr8   r'   Útypingre   r}   r7   )ro   r~   r   Úderr   r   r   Úto_cryptography_key  s    

zPKey.to_cryptography_key)Ú
crypto_keyr   c                 C  s¨   t |tjtjtjtjtjtj	t
jt
jtjtjf
ƒs:tdƒ‚ddlm}m}m}m} t |tjtjtj	t
jtjfƒr†tt| |j|j¡ƒS | |j|j|ƒ ¡}tt|ƒS 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
        zUnsupported key typer   )ÚEncodingÚNoEncryptionÚPrivateFormatÚPublicFormatN)r\   r   ÚDSAPrivateKeyÚDSAPublicKeyr   ÚEllipticCurvePrivateKeyÚEllipticCurvePublicKeyr   ÚEd25519PrivateKeyÚEd25519PublicKeyr   ÚEd448PrivateKeyÚEd448PublicKeyr   ÚRSAPrivateKeyÚRSAPublicKeyr]   r�   r‡   rˆ   r‰   rŠ   r>   r'   Úpublic_bytesÚDERZSubjectPublicKeyInfoZprivate_bytesZPKCS8r=   )Úclsr†   r‡   rˆ   r‰   rŠ   r„   r   r   r   Úfrom_cryptography_key  sL    öþûþ
 ÿþ  ÿzPKey.from_cryptography_keyr?   )ÚtypeÚbitsr   c              	   C  s6  t |tƒstdƒ‚t |tƒs$tdƒ‚|tkr¢|dkr<tdƒ‚t ¡ }t |tj	¡}t 
|tj¡ t ¡ }t |||tj¡}t|dkƒ t | j|¡}t|dkƒ nŠ|tk�r$t ¡ }t|tjkƒ t |tj¡}t ||tjdtjtjtj¡}t|dkƒ tt |¡dkƒ tt | j|¡dkƒ ntdƒ‚d| _dS )	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``
        ztype must be an integerzbits must be an integerr   zInvalid number of bitsé   zNo such key typeTN)r\   r?   r]   r+   r^   rK   ZBN_newrN   rS   ÚBN_freeZBN_set_wordZRSA_F4ZRSA_newZRSA_generate_key_exrR   rQ   ZEVP_PKEY_assign_RSAry   r*   ZDSA_newZDSA_freeZDSA_generate_parameters_exZDSA_generate_keyZEVP_PKEY_set1_DSAr-   rz   )ro   r™   rš   Úexponentr   Úresultr   Úresr   r   r   Úgenerate_keyS  sB    


      ÿzPKey.generate_keyÚboolc                 C  sd   | j rtdƒ‚t |  ¡ ¡tjkr*tdƒ‚t | j¡}t 	|tj
¡}t |¡}|dkrZdS tƒ  dS )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.
        zpublic key onlyz'Only RSA keys can currently be checked.r›   TN)r‚   r]   rK   ZEVP_PKEY_typer™   ÚEVP_PKEY_RSAÚEVP_PKEY_get1_RSAry   rN   rS   ÚRSA_freeZRSA_check_keyÚ_raise_current_error)ro   r   rž   r   r   r   Úcheck‹  s    
z
PKey.checkc                 C  s   t  | j¡S )zT
        Returns the type of the key

        :return: The type of the key.
        )rK   ÚEVP_PKEY_idry   rn   r   r   r   r™   ¥  s    z	PKey.typec                 C  s   t  | j¡S )zh
        Returns the number of bits of the key

        :return: The number of bits of the key.
        )rK   ZEVP_PKEY_bitsry   rn   r   r   r   rš   ­  s    z	PKey.bitsN)rB   rC   rD   rE   r‚   rz   rp   r…   Úclassmethodr˜   r    r¦   r™   rš   r   r   r   r   r.   õ   s   98c                      sš   e Zd ZdZdZdddœ‡ fdd„Zeddd	œd
d„ƒZeddd	œdd„ƒZeddd dœdd„ƒZ	dddddœdd„Z
ddœdd„Zddœdd„Z‡  ZS )Ú_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`
    Nr   r¡   ©Úotherr   c                   s   t |tƒrtƒ  |¡S tS )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©   ÚsuperÚ__ne__ÚNotImplemented©ro   r«   ©Ú	__class__r   r   r­   Â  s    
z_EllipticCurve.__ne__úset[_EllipticCurve])r$   r   c                   s>   ˆ  tjd¡}t d|¡}ˆ  ||¡ t‡ ‡fdd„|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.
        r   zEC_builtin_curve[]c                 3  s   | ]}ˆ   ˆ|j¡V  qd S r   )Úfrom_nidÚnid)Ú.0Úc©r—   r$   r   r   Ú	<genexpr>Ý  s     z7_EllipticCurve._load_elliptic_curves.<locals>.<genexpr>)ZEC_get_builtin_curvesrN   rR   rO   Úset)r—   r$   Z
num_curvesZbuiltin_curvesr   r·   r   Ú_load_elliptic_curvesÍ  s    
z$_EllipticCurve._load_elliptic_curvesc                 C  s   | j dkr|  |¡| _ | j S )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·   r   r   r   Ú_get_elliptic_curvesß  s    

z#_EllipticCurve._get_elliptic_curvesr?   )r$   r´   r   c                 C  s   | ||t  | |¡¡ d¡ƒ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)rN   rg   Ú
OBJ_nid2snÚdecode)r—   r$   r´   r   r   r   r³   í  s    z_EllipticCurve.from_nidr   rY   )r$   r´   rr   r   c                 C  s   || _ || _|| _dS )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   Ú_nidrr   )ro   r$   r´   rr   r   r   r   rp   þ  s    z_EllipticCurve.__init__rl   c                 C  s   d| j ›d�S )Nz<Curve ú>©rr   rn   r   r   r   Ú__repr__  s    z_EllipticCurve.__repr__c                 C  s   | j  | j¡}t |t j¡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.
        )rK   ZEC_KEY_new_by_curve_namerÀ   rN   rS   ZEC_KEY_free)ro   Úkeyr   r   r   Ú
_to_EC_KEY  s    z_EllipticCurve._to_EC_KEY)rB   rC   rD   rE   r»   r­   r¨   rº   r¼   r³   rp   rÃ   rÅ   Ú__classcell__r   r   r°   r   r©   ¶  s   	r©   zSget_elliptic_curves is deprecated. You should use the APIs in cryptography instead.r²   rl   c                   C  s
   t  t¡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¼   rK   r   r   r   r   r:     s    zRget_elliptic_curve is deprecated. You should use the APIs in cryptography instead.rq   c                 C  s,   t ƒ D ]}|j| kr|  S qtd| ƒ‚dS )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.
    zunknown curve nameN)r:   rr   r^   )rr   Zcurver   r   r   r9   0  s    


c                      s¢   e Zd ZdZd ddœdd„Zddddœ‡ fd	d
„Zdddœdd„Zdddœdd„Zdddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Z‡  ZS ) 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.
    rY   rq   c                 C  s    t  |j¡}t |t j¡| _dS )zž
        Create a new X509Name, copying the given X509Name instance.

        :param name: The name to copy.
        :type name: :py:class:`X509Name`
        N)rK   ZX509_NAME_duprv   rN   rS   ZX509_NAME_freert   r   r   r   rp   `  s    zX509Name.__init__r   r   )rr   Úvaluer   c           	   	     s  |  d¡rtƒ  ||¡S t|ƒtk	r<tdt|ƒjd›d�ƒ‚t t	|ƒ¡}|tj
kr|z
tƒ  W n tk
rr   Y nX tdƒ‚tt | j¡ƒD ]J}t | j|¡}t |¡}t |¡}||krŒt | j|¡}t |¡  qØqŒt|tƒrì| d¡}t | j|tj|t|ƒdd¡}|�stƒ  d S )	NÚ_z$attribute name must be string, not 'z.200ú'úNo such attributeúutf-8éÿÿÿÿr   )Ú
startswithr¬   Ú__setattr__r™   r   r]   rB   rK   ÚOBJ_txt2nidÚ_byte_stringÚ	NID_undefr¥   r-   ÚAttributeErrorÚrangeÚX509_NAME_entry_countrv   ÚX509_NAME_get_entryÚX509_NAME_ENTRY_get_objectÚOBJ_obj2nidZX509_NAME_delete_entryZX509_NAME_ENTRY_freer\   ÚencodeZX509_NAME_add_entry_by_NIDZMBSTRING_UTF8rP   )	ro   rr   rÇ   r´   ÚiÚentZent_objZent_nidZ
add_resultr°   r   r   rÎ   j  sD    
ÿ






      ÿzX509Name.__setattr__ú
str | Nonec           	   
   C  sÎ   t  t|ƒ¡}|t jkr@z
tƒ  W n tk
r6   Y nX tdƒ‚t  | j|d¡}|dkr\dS t  	| j|¡}t  
|¡}t d¡}t  ||¡}t|dkƒ z"t |d |¡dd…  d¡}W 5 t  |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Ê   rÌ   Núunsigned char**r   rË   )rK   rÏ   rÐ   rÑ   r¥   r-   rÒ   ZX509_NAME_get_index_by_NIDrv   rÕ   ÚX509_NAME_ENTRY_get_datarN   rO   ZASN1_STRING_to_UTF8rQ   ÚOPENSSL_freerG   r¿   )	ro   rr   r´   Zentry_indexÚentryrT   rW   Zdata_lengthrž   r   r   r   Ú__getattr__‘  s*    



ÿzX509Name.__getattr__r¡   rª   c                 C  s"   t |tƒstS t | j|j¡dkS ©Nr   ©r\   r/   r®   rK   ZX509_NAME_cmprv   r¯   r   r   r   Ú__eq__¹  s    
zX509Name.__eq__c                 C  s"   t |tƒstS t | j|j¡dk S rá   râ   r¯   r   r   r   Ú__lt__¿  s    
zX509Name.__lt__rl   c                 C  sD   t  dd¡}t | j|t|ƒ¡}t|t jkƒ d t  	|¡ 
d¡¡S )z6
        String representation of an X509Name
        rH   i   z<X509Name object '{}'>rË   )rN   rO   rK   ZX509_NAME_onelinerv   rP   rQ   rR   Úformatrg   r¿   )ro   rW   Zformat_resultr   r   r   rÃ   Å  s      ÿÿzX509Name.__repr__r?   c                 C  s   t  | 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`
        )rK   ZX509_NAME_hashrv   rn   r   r   r   ÚhashÓ  s    
zX509Name.hashrV   c                 C  sN   t  d¡}t | j|¡}t|dkƒ t  |d |¡dd… }t |d ¡ |S )zŽ
        Return the DER encoding of this name.

        :return: The DER encoded form of this name.
        :rtype: :py:class:`bytes`
        rÜ   r   N)rN   rO   rK   Zi2d_X509_NAMErv   rQ   rG   rÞ   )ro   rW   Zencode_resultri   r   r   r   r„   ß  s    
zX509Name.derzlist[tuple[bytes, bytes]]c           	      C  sˆ   g }t t | j¡ƒD ]n}t | j|¡}t |¡}t |¡}t |¡}t |¡}t	 
t |¡t |¡¡dd… }| t	 |¡|f¡ q|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Ó   rK   rÔ   rv   rÕ   rÖ   rÝ   r×   r¾   rN   rG   rh   rf   rs   rg   )	ro   rž   rÙ   rÚ   ÚfnameZfvalr´   rr   rÇ   r   r   r   Úget_componentsî  s    



 ÿzX509Name.get_components)rB   rC   rD   rE   rp   rÎ   rà   rã   rä   rÃ   ræ   r„   rè   rÆ   r   r   r°   r   r/   F  s   
'(zPCSR support in pyOpenSSL is deprecated. You should use the APIs in cryptography.c                   @  s¬   e Zd ZdZddœdd„Zddœdd„Zedd d	œd
d„ƒZdddœdd„Zddœdd„Z	dddœdd„Z
ddœdd„Zddœdd„Zddddœdd„Zdddœd d!„Zd"S )#r0   zŽ
    An X.509 certificate signing requests.

    .. deprecated:: 24.2.0
       Use `cryptography.x509.CertificateSigningRequest` instead.
    rY   rl   c                 C  s&   t  ¡ }t |t j¡| _|  d¡ d S rá   )rK   ZX509_REQ_newrN   rS   ÚX509_REQ_freeÚ_reqÚset_version)ro   Úreqr   r   r   rp     s    zX509Req.__init__zx509.CertificateSigningRequestc                 C  s   ddl m} tt| ƒ}||ƒS )z®
        Export as a ``cryptography`` certificate signing request.

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

        .. versionadded:: 17.1.0
        r   )Úload_der_x509_csr)Úcryptography.x509rí   Ú"_dump_certificate_request_internalr'   )ro   rí   r„   r   r   r   Úto_cryptography  s    
zX509Req.to_cryptography)Ú
crypto_reqr   c                 C  s6   t |tjƒstdƒ‚ddlm} | |j¡}tt	|ƒ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 requestr   ©r‡   )
r\   r   ZCertificateSigningRequestr]   r�   r‡   r•   r–   Ú"_load_certificate_request_internalr'   )r—   rñ   r‡   r„   r   r   r   Úfrom_cryptography)  s
    zX509Req.from_cryptographyr.   ©r|   r   c                 C  s    t  | j|j¡}t|dkƒ dS )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)rK   ZX509_REQ_set_pubkeyrê   ry   rQ   ©ro   r|   r_   r   r   r   Ú
set_pubkey?  s    	zX509Req.set_pubkeyc                 C  sD   t  t ¡}t | j¡|_t|jtjkƒ t 	|jtj
¡|_d|_|S )z‹
        Get the public key of the certificate signing request.

        :return: The public key.
        :rtype: :py:class:`PKey`
        T)r.   Ú__new__rK   ZX509_REQ_get_pubkeyrê   ry   rQ   rN   rR   rS   rx   r‚   r{   r   r   r   Ú
get_pubkeyK  s    
zX509Req.get_pubkeyr?   ©Úversionr   c                 C  s@   t |tƒstdƒ‚|dkr"tdƒ‚t | j|¡}t|dkƒ dS )z±
        Set the version subfield (RFC 2986, section 4.1) of the certificate
        request.

        :param int version: The version number.
        :return: ``None``
        zversion must be an intr   z9Invalid version. The only valid version for X509Req is 0.r›   N)r\   r?   r]   r^   rK   ZX509_REQ_set_versionrê   rQ   )ro   rû   r_   r   r   r   rë   Y  s    
ÿzX509Req.set_versionc                 C  s   t  | 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`
        )rK   ZX509_REQ_get_versionrê   rn   r   r   r   Úget_versionj  s    zX509Req.get_versionr/   c                 C  s2   t  t ¡}t | j¡|_t|jtjkƒ | |_	|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ø   rK   ZX509_REQ_get_subject_namerê   rv   rQ   rN   rR   Ú_ownerrt   r   r   r   Úget_subjectt  s
    
zX509Req.get_subjectr   ©r|   Údigestr   c                 C  s^   |j rtdƒ‚|jstdƒ‚t t|ƒ¡}|tjkr<tdƒ‚t | j	|j
|¡}t|dkƒ dS )aa  
        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:`"sha256"`.
        :type digest: :py:class:`str`
        :return: ``None``
        zKey has only public partúKey is uninitializedúNo such digest methodr   N)r‚   r^   rz   rK   ÚEVP_get_digestbynamerÐ   rN   rR   ZX509_REQ_signrê   ry   rQ   )ro   r|   r   Z
digest_objÚsign_resultr   r   r   ÚsignŠ  s    
zX509Req.signr¡   c                 C  s4   t |tƒstdƒ‚t | j|j¡}|dkr0tƒ  |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.
        úpkey must be a PKey instancer   )r\   r.   r]   rK   ZX509_REQ_verifyrê   ry   r¥   )ro   r|   rž   r   r   r   Úverify¢  s    
zX509Req.verifyN)rB   rC   rD   rE   rp   rð   r¨   rô   r÷   rù   rë   rü   rþ   r  r  r   r   r   r   r0   	  s   
c                   @  sä  e Zd ZdZddœdd„Zedd dœdd	„ƒZd
dœdd„Zed
d dœdd„ƒZdddœdd„Z	ddœdd„Z
ddœdd„Zdddœdd„Zddddœdd„Zd dœd!d"„Zdd d#œd$d%„Zddœd&d'„Zddd(œd)d*„Zddœd+d,„Zddd-œd.d/„Zddd-œd0d1„Zd2dœd3d4„Zdd5d6œd7d8„Zd5dœd9d:„Zd;d dd<œd=d>„Zd dd?œd@dA„Zd5dœdBdC„Zd dd?œdDdE„ZddFd6œdGdH„ZddFddIœdJdK„ZdFdœdLdM„ZdFddNœdOdP„ZdFdœdQdR„Z dFddSœdTdU„Z!ddœdVdW„Z"dXS )Yr,   z
    An X.509 certificate.
    rY   rl   c                 C  s:   t  ¡ }t|tjkƒ t |t j¡| _tƒ | _	tƒ | _
d S r   )rK   ZX509_newrQ   rN   rR   rS   Ú	X509_freeÚ_x509rk   Ú_issuer_invalidatorÚ_subject_invalidator)ro   r   r   r   r   rp   ½  s
    zX509.__init__r   )r   r   c                 C  s.   |   | ¡}t |tj¡|_tƒ |_tƒ |_|S r   )	rø   rN   rS   rK   r  r	  rk   r
  r  )r—   r   Úcertr   r   r   Ú_from_raw_x509_ptrÅ  s
    
zX509._from_raw_x509_ptrzx509.Certificatec                 C  s   ddl m} tt| ƒ}||ƒS )z�
        Export as a ``cryptography`` certificate.

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

        .. versionadded:: 17.1.0
        r   )Úload_der_x509_certificate)rî   r  r5   r'   )ro   r  r„   r   r   r   rð   Í  s    
zX509.to_cryptography)Úcrypto_certr   c                 C  s6   t |tjƒstdƒ‚ddlm} | |j¡}tt	|ƒ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
        zMust be a certificater   rò   )
r\   r   ZCertificater]   r�   r‡   r•   r–   r;   r'   )r—   r  r‡   r„   r   r   r   rô   Ú  s
    zX509.from_cryptographyr?   rú   c                 C  s,   t |tƒstdƒ‚tt | j|¡dkƒ dS )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``
        zversion must be an integerr›   N)r\   r?   r]   rQ   rK   ZX509_set_versionr	  )ro   rû   r   r   r   rë   î  s    

zX509.set_versionc                 C  s   t  | j¡S )z˜
        Return the version number of the certificate.

        :return: The version number of the certificate.
        :rtype: :py:class:`int`
        )rK   ZX509_get_versionr	  rn   r   r   r   rü   ý  s    zX509.get_versionr.   c                 C  sF   t  t ¡}t | j¡|_|jtjkr*tƒ  t 	|jtj
¡|_d|_|S )z{
        Get the public key of the certificate.

        :return: The public key.
        :rtype: :py:class:`PKey`
        T)r.   rø   rK   ZX509_get_pubkeyr	  ry   rN   rR   r¥   rS   rx   r‚   r{   r   r   r   rù     s    
zX509.get_pubkeyrõ   c                 C  s2   t |tƒstdƒ‚t | j|j¡}t|dkƒ dS )z¥
        Set the public key of the certificate.

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

        :return: :py:data:`None`
        r  r›   N)r\   r.   r]   rK   ZX509_set_pubkeyr	  ry   rQ   rö   r   r   r   r÷     s    	
zX509.set_pubkeyr   rÿ   c                 C  sp   t |tƒstdƒ‚|jr tdƒ‚|js.tdƒ‚t t|ƒ¡}|t	j
krNtdƒ‚t | j|j|¡}t|dkƒ dS )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:`str`

        :return: :py:data:`None`
        r  zKey only has public partr  r  r   N)r\   r.   r]   r‚   r^   rz   rK   r  rÐ   rN   rR   Z	X509_signr	  ry   rQ   )ro   r|   r   Zevp_mdr  r   r   r   r  $  s    

z	X509.signrV   c                 C  sZ   t  | j¡}t d¡}t  |tjtj|¡ t  |d ¡}|t jkrJt	dƒ‚t 
t  |¡¡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
        zASN1_OBJECT **r   zUndefined signature algorithm)rK   ZX509_get0_tbs_sigalgr	  rN   rO   ZX509_ALGOR_get0rR   r×   rÑ   r^   rg   Z
OBJ_nid2ln)ro   Zsig_algZalgr´   r   r   r   Úget_signature_algorithm@  s    

zX509.get_signature_algorithm)Údigest_namer   c                 C  s„   t  t|ƒ¡}|tjkr tdƒ‚t dt j¡}t dd¡}t|ƒ|d< t  	| j
|||¡}t|dkƒ d dd„ t ||d ¡D ƒ¡S )	a5  
        Return the digest of the X509 object.

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

        :return: The digest of the object, formatted as
            :py:const:`b":"`-delimited hex pairs.
        :rtype: :py:class:`bytes`
        r  zunsigned char[]zunsigned int[]r›   r   ó   :c                 S  s   g | ]}t |ƒ ¡ ‘qS r   )r   Úupper)rµ   Úchr   r   r   Ú
<listcomp>l  s   ÿzX509.digest.<locals>.<listcomp>)rK   r  rÐ   rN   rR   r^   rO   ZEVP_MAX_MD_SIZErP   ZX509_digestr	  rQ   ÚjoinrG   )ro   r  r   rW   Zresult_lengthZdigest_resultr   r   r   r   S  s$    
   ÿþÿzX509.digestc                 C  s   t  | j¡S )z�
        Return the hash of the X509 subject.

        :return: The hash of the subject.
        :rtype: :py:class:`int`
        )rK   ZX509_subject_name_hashr	  rn   r   r   r   Úsubject_name_hashr  s    zX509.subject_name_hash)Úserialr   c                 C  sª   t |tƒstdƒ‚t|ƒdd… }| d¡}t d¡}t ||¡}t	|tj
kƒ t |d tj
¡}t |d ¡ t	|tj
kƒ t |tj¡}t | j|¡}t	|dkƒ dS )z±
        Set the serial number of the certificate.

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

        :return: :py:data`None`
        zserial must be an integeré   Nr½   zBIGNUM**r   r›   )r\   r?   r]   ÚhexrØ   rN   rO   rK   Z	BN_hex2bnrQ   rR   ZBN_to_ASN1_INTEGERrœ   rS   ZASN1_INTEGER_freeZX509_set_serialNumberr	  )ro   r  Ú
hex_serialZhex_serial_bytesÚbignum_serialrž   Úasn1_serialr_   r   r   r   Úset_serial_number{  s    	


zX509.set_serial_numberc              	   C  sh   t  | j¡}t  |tj¡}z<t  |¡}z t 	|¡}t
|dƒ}|W ¢W ¢S t  |¡ X W 5 t  |¡ X dS )zx
        Return the serial number of this certificate.

        :return: The serial number.
        :rtype: int
        é   N)rK   ZX509_get_serialNumberr	  ZASN1_INTEGER_to_BNrN   rR   rœ   Z	BN_bn2hexrÞ   rg   r?   )ro   r  r  r  Zhexstring_serialr  r   r   r   Úget_serial_number—  s    


zX509.get_serial_number)Úamountr   c                 C  s.   t |tƒstdƒ‚t | j¡}t ||¡ dS )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``
        úamount must be an integerN)r\   r?   r]   rK   ÚX509_getm_notAfterr	  ÚX509_gmtime_adj)ro   r!  ZnotAfterr   r   r   Úgmtime_adj_notAfter«  s    
zX509.gmtime_adj_notAfterc                 C  s.   t |tƒstdƒ‚t | j¡}t ||¡ dS )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``
        r"  N)r\   r?   r]   rK   ÚX509_getm_notBeforer	  r$  )ro   r!  Z	notBeforer   r   r   Úgmtime_adj_notBefore¹  s    
zX509.gmtime_adj_notBeforer¡   c                 C  sT   |   ¡ }|dkrtdƒ‚| d¡}tj |d¡}tjj}tj |¡jdd�}||k S )z¡
        Check whether the certificate has expired.

        :return: ``True`` if the certificate has expired, ``False`` otherwise.
        :rtype: bool
        NzUnable to determine notAfterrË   z%Y%m%d%H%M%SZ)Útzinfo)	Úget_notAfterr^   r¿   ÚdatetimeÚstrptimeÚtimezoneÚutcÚnowÚreplace)ro   Z
time_bytesZtime_stringZ	not_afterÚUTCÚutcnowr   r   r   Úhas_expiredÆ  s    
zX509.has_expiredrF   )Úwhichr   c                 C  s   t || jƒƒS r   )rj   r	  )ro   r3  r   r   r   Ú_get_boundary_time×  s    zX509._get_boundary_timec                 C  s   |   tj¡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
        )r4  rK   r&  rn   r   r   r   Úget_notBeforeÚ  s    zX509.get_notBeforezCallable[..., Any])r3  r[   r   c                 C  s   t || jƒ|ƒS r   )r`   r	  )ro   r3  r[   r   r   r   Ú_set_boundary_timeç  s    zX509._set_boundary_timera   c                 C  s   |   tj|¡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``
        )r6  rK   r&  ©ro   r[   r   r   r   Úset_notBeforeì  s    zX509.set_notBeforec                 C  s   |   tj¡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
        )r4  rK   r#  rn   r   r   r   r)  ù  s    zX509.get_notAfterc                 C  s   |   tj|¡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``
        )r6  rK   r#  r7  r   r   r   Úset_notAfter  s    zX509.set_notAfterr/   c                 C  s0   t  t ¡}|| jƒ|_t|jtjkƒ | |_|S r   )r/   rø   r	  rv   rQ   rN   rR   rý   )ro   r3  rr   r   r   r   Ú	_get_name  s
    
zX509._get_name)r3  rr   r   c                 C  s0   t |tƒstdƒ‚|| j|jƒ}t|dkƒ d S )Nzname must be an X509Namer›   )r\   r/   r]   r	  rv   rQ   )ro   r3  rr   r_   r   r   r   Ú	_set_name  s    
zX509._set_namec                 C  s   |   tj¡}| j |¡ |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`
        )r:  rK   ZX509_get_issuer_namer
  ru   rt   r   r   r   Ú
get_issuer$  s    zX509.get_issuer)Úissuerr   c                 C  s   |   tj|¡ | j ¡  dS )zŸ
        Set the issuer of this certificate.

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

        :return: ``None``
        N)r;  rK   ZX509_set_issuer_namer
  rw   )ro   r=  r   r   r   Ú
set_issuer4  s    	zX509.set_issuerc                 C  s   |   tj¡}| j |¡ |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`
        )r:  rK   ZX509_get_subject_namer  ru   rt   r   r   r   rþ   @  s    zX509.get_subject)Úsubjectr   c                 C  s   |   tj|¡ | j ¡  dS )z£
        Set the subject of this certificate.

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

        :return: ``None``
        N)r;  rK   ZX509_set_subject_namer  rw   )ro   r?  r   r   r   Úset_subjectP  s    	zX509.set_subjectc                 C  s   t  | j¡S )z¯
        Get the number of extensions on this certificate.

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

        .. versionadded:: 0.12
        )rK   ZX509_get_ext_countr	  rn   r   r   r   Úget_extension_count\  s    	zX509.get_extension_countN)#rB   rC   rD   rE   rp   r¨   r  rð   rô   rë   rü   rù   r÷   r  r  r   r  r  r   r%  r'  r2  r4  r5  r6  r8  r)  r9  r:  r;  r<  r>  rþ   r@  rA  r   r   r   r   r,   ¸  sB   		c                   @  sž   e Zd ZU dZejZded< ejZ	ded< ej
Zded< ejZded< ejZded< ejZded< ejZded	< ejZded
< ejZded< ejZded< dS )r4   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
    r?   Ú	CRL_CHECKÚCRL_CHECK_ALLÚIGNORE_CRITICALÚX509_STRICTÚALLOW_PROXY_CERTSÚPOLICY_CHECKÚEXPLICIT_POLICYÚINHIBIT_MAPÚCHECK_SS_SIGNATUREÚPARTIAL_CHAINN)rB   rC   rD   rE   rK   ZX509_V_FLAG_CRL_CHECKrB  Ú__annotations__ZX509_V_FLAG_CRL_CHECK_ALLrC  ZX509_V_FLAG_IGNORE_CRITICALrD  ZX509_V_FLAG_X509_STRICTrE  ZX509_V_FLAG_ALLOW_PROXY_CERTSrF  ZX509_V_FLAG_POLICY_CHECKrG  ZX509_V_FLAG_EXPLICIT_POLICYrH  ZX509_V_FLAG_INHIBIT_MAPrI  ZX509_V_FLAG_CHECK_SS_SIGNATURErJ  ZX509_V_FLAG_PARTIAL_CHAINrK  r   r   r   r   r4   h  s   

c                   @  sr   e Zd ZdZddœdd„Zdddœdd	„Zd
ddœdd„Zdddœdd„Zdddœdd„Zdddddœdd„Z	dS )r1   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`.
    rY   rl   c                 C  s   t  ¡ }t |t j¡| _d S r   )rK   ZX509_STORE_newrN   rS   ZX509_STORE_freeÚ_store©ro   Ústorer   r   r   rp   �  s    zX509Store.__init__r,   )r  r   c                 C  s0   t |tƒstƒ ‚t | j|j¡}t|dkƒ dS )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.
        r›   N)r\   r,   r]   rK   ZX509_STORE_add_certrM  r	  rQ   )ro   r  rŸ   r   r   r   Úadd_cert‘  s    
zX509Store.add_certzx509.CertificateRevocationList)Úcrlr   c                 C  sv   t |tjƒrTddlm} t| |j¡ƒ}t 	|t
j¡}t|t
jkƒ t
 |tj¡}ntdƒ‚tt | j|¡dkƒ dS )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: The certificate revocation list to add to this store.
        :type crl: ``cryptography.x509.CertificateRevocationList``
        :return: ``None`` if the certificate revocation list was added
            successfully.
        r   rò   z?CRL must be of type cryptography.x509.CertificateRevocationListN)r\   r   ZCertificateRevocationListr�   r‡   rU   r•   r–   rK   Zd2i_X509_CRL_biorN   rR   rQ   rS   ZX509_CRL_freer]   ZX509_STORE_add_crlrM  )ro   rQ  r‡   rI   Zopenssl_crlr   r   r   Úadd_crl§  s    ÿzX509Store.add_crlr?   )Úflagsr   c                 C  s   t t | j|¡dkƒ dS )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.
        r   N)rQ   rK   ZX509_STORE_set_flagsrM  )ro   rS  r   r   r   Ú	set_flagsÅ  s    zX509Store.set_flagszdatetime.datetime)Úvfy_timer   c                 C  sF   t  ¡ }t |t j¡}t  |t | ¡ ¡¡ t	t  
| j|¡dkƒ dS )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.
        r   N)rK   ZX509_VERIFY_PARAM_newrN   rS   ZX509_VERIFY_PARAM_freeZX509_VERIFY_PARAM_set_timeÚcalendarÚtimegmÚ	timetuplerQ   ZX509_STORE_set1_paramrM  )ro   rU  Úparamr   r   r   Úset_timeÝ  s     ÿzX509Store.set_timeNzStrOrBytesPath | None)ÚcafileÚcapathr   c                 C  sN   |dkrt j}nt|ƒ}|dkr(t j}nt|ƒ}t | j||¡}|sJtƒ  dS )aˆ  
        Let X509Store know where we can find trusted certificates for the
        certificate chain.  Note that the certificates have to be in PEM
        format.

        If *capath* is passed, it must be a directory prepared using the
        ``c_rehash`` tool included with OpenSSL.  Either, but not both, of
        *cafile* or *capath* may be ``None``.

        .. note::

          Both *cafile* and *capath* may be set simultaneously.

          Call this method multiple times to add more than one location.
          For example, CA certificates, and certificate revocation list bundles
          may be passed in *cafile* in subsequent calls to this method.

        .. versionadded:: 20.0

        :param cafile: In which file we can find the certificates (``bytes`` or
                       ``unicode``).
        :param capath: In which directory we can find the certificates
                       (``bytes`` or ``unicode``).

        :return: ``None`` if the locations were set successfully.

        :raises OpenSSL.crypto.Error: If both *cafile* and *capath* is ``None``
            or the locations could not be set for any reason.

        N)rN   rR   Ú_path_bytesrK   ZX509_STORE_load_locationsrM  r¥   )ro   r[  r\  Zload_resultr   r   r   Úload_locationsõ  s    #  ÿzX509Store.load_locations)N)
rB   rC   rD   rE   rp   rP  rR  rT  rZ  r^  r   r   r   r   r1     s    ýc                      s,   e Zd ZdZdddddœ‡ fdd„Z‡  ZS )	r3   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`
    r   z	list[Any]r,   rY   )ÚmessageÚerrorsÚcertificater   c                   s   t ƒ  |¡ || _|| _d S r   )r¬   rp   r`  ra  )ro   r_  r`  ra  r°   r   r   rp   2  s    zX509StoreContextError.__init__)rB   rC   rD   rE   rp   rÆ   r   r   r°   r   r3   )  s   c                   @  sˆ   e Zd ZdZddddddœdd	„Zeddd
œdd„ƒZedddœdd„ƒZddœdd„Zdddœdd„Z	ddœdd„Z
ddœdd„ZdS )r2   a9  
    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`.

    :param X509Store store: The certificates which will be trusted for the
        purposes of any verifications.
    :param X509 certificate: The certificate to be verified.
    :param chain: List of untrusted certificates that may be used for building
        the certificate chain. May be ``None``.
    :type chain: :class:`list` of :class:`X509`
    Nr1   r,   zSequence[X509] | NonerY   )rO  ra  Úchainr   c                 C  s   || _ || _|  |¡| _d S r   )rM  Ú_certÚ_build_certificate_stackÚ_chain)ro   rO  ra  rb  r   r   r   rp   J  s    zX509StoreContext.__init__)Úcertificatesr   c                 C  s¤   dddœdd„}| d ks$t | ƒdkr*tjS t ¡ }t|tjkƒ t ||¡}| D ]N}t|tƒsft	dƒ‚tt 
|j¡dkƒ t ||j¡dkrPt |j¡ tƒ  qP|S )Nr   rY   )Úsr   c                 S  s8   t t | ¡ƒD ]}t | |¡}t |¡ qt | ¡ d S r   )rÓ   rK   Úsk_X509_numÚsk_X509_valuer  Úsk_X509_free)rg  rÙ   Úxr   r   r   ÚcleanupX  s    z:X509StoreContext._build_certificate_stack.<locals>.cleanupr   z+One of the elements is not an X509 instance)rP   rN   rR   rK   Zsk_X509_new_nullrQ   rS   r\   r,   r]   ZX509_up_refr	  Zsk_X509_pushr  r¥   )rf  rl  Ústackr  r   r   r   rd  T  s    
z)X509StoreContext._build_certificate_stackr   r3   )Ú	store_ctxr   c                 C  s\   t  t t | ¡¡¡ d¡}t | ¡t | ¡|g}t | ¡}t |¡}t	 
|¡}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.
        rË   )rN   rg   rK   ZX509_verify_cert_error_stringZX509_STORE_CTX_get_errorr¿   ZX509_STORE_CTX_get_error_depthZX509_STORE_CTX_get_current_certZX509_dupr,   r  r3   )rn  r_  r`  r	  rc  Úpycertr   r   r   Ú_exception_from_contextr  s    	ÿÿüý


z(X509StoreContext._exception_from_contextrl   c                 C  sj   t  ¡ }t|tjkƒ t |t j¡}t  || jj| j	j
| j¡}t|dkƒ t  |¡}|dkrf|  |¡‚|S )a3  
        Verifies the certificate and runs an X509_STORE_CTX containing the
        results.

        :raises X509StoreContextError: If an error occurred when validating a
          certificate in the context. Sets ``certificate`` attribute to
          indicate which certificate caused the error.
        r›   r   )rK   ZX509_STORE_CTX_newrQ   rN   rR   rS   ZX509_STORE_CTX_freeZX509_STORE_CTX_initrM  rc  r	  re  ZX509_verify_certrp  )ro   rn  rb   r   r   r   Ú_verify_certificateŒ  s    	   ÿ

z$X509StoreContext._verify_certificate)rO  r   c                 C  s
   || _ dS )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)rM  rN  r   r   r   Ú	set_store¤  s    	zX509StoreContext.set_storec                 C  s   |   ¡  dS )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.
        N)rq  rn   r   r   r   Úverify_certificate¯  s    
z#X509StoreContext.verify_certificatez
list[X509]c                 C  st   |   ¡ }t |¡}t|tjkƒ g }tt |¡ƒD ]2}t ||¡}t|tjkƒ t	 
|¡}| |¡ q2t |¡ |S )aR  
        Verify a certificate in a context and return the complete validated
        chain.

        :raises X509StoreContextError: If an error occurred when validating a
          certificate in the context. Sets ``certificate`` attribute to
          indicate which certificate caused the error.

        .. versionadded:: 20.0
        )rq  rK   ZX509_STORE_CTX_get1_chainrQ   rN   rR   rÓ   rh  ri  r,   r  rs   rj  )ro   rn  Z
cert_stackrž   rÙ   r  ro  r   r   r   Úget_verified_chain»  s    


z#X509StoreContext.get_verified_chain)N)rB   rC   rD   rE   rp   Ústaticmethodrd  rp  rq  rr  rs  rt  r   r   r   r   r2   :  s    ü
)r™   rG   r   c                 C  sv   t |tƒr| d¡}t|ƒ}| tkr<t |tjtjtj¡}n | t	krTt 
|tj¡}ntdƒ‚|tjkrltƒ  t |¡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½   ú3type argument must be FILETYPE_PEM or FILETYPE_ASN1)r\   r   rØ   rU   r(   rK   ZPEM_read_bio_X509rN   rR   r'   Zd2i_X509_bior^   r¥   r,   r  )r™   rG   rI   r   r   r   r   r;   Ø  s    


)r™   r  r   c                 C  sn   t ƒ }| tkrt ||j¡}n<| tkr6t ||j¡}n$| tkrRt ||jdd¡}nt	dƒ‚t
|dkƒ 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
    r   úCtype argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXTr›   )rU   r(   rK   ZPEM_write_bio_X509r	  r'   Zi2d_X509_bior)   ZX509_print_exr^   rQ   rX   )r™   r  rI   Úresult_coder   r   r   r5   õ  s    
ÿ)r™   r|   r   c                 C  sP   t ƒ }| tkrtj}n| tkr&tj}ntdƒ‚|||jƒ}|dkrHtƒ  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
    rv  r›   )
rU   r(   rK   ZPEM_write_bio_PUBKEYr'   Zi2d_PUBKEY_bior^   ry   r¥   rX   )r™   r|   rI   Z	write_biorx  r   r   r   r8     s    
rÛ   úPassphraseCallableT | None)r™   r|   ÚcipherÚ
passphraser   c           	   	   C  s  t ƒ }t|tƒstdƒ‚|dk	rR|dkr0tdƒ‚t t|ƒ¡}|tjkrXt	dƒ‚ntj}t
| |ƒ}| tkr’t ||j|tjd|j|j¡}| ¡  nh| tkrªt ||j¡}nP| tkròt |j¡tjkrÌtdƒ‚t t |j¡tj¡}t ||d¡}nt	dƒ‚t|dkƒ 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
    zpkey must be a PKeyNzDif a value is given for cipher one must also be given for passphrasezInvalid cipher namer   z-Only RSA keys are supported for FILETYPE_TEXTrw  )rU   r\   r.   r]   rK   ZEVP_get_cipherbynamerÐ   rN   rR   r^   Ú_PassphraseHelperr(   ZPEM_write_bio_PrivateKeyry   ÚcallbackÚcallback_argsÚraise_if_problemr'   Zi2d_PrivateKey_bior)   r§   r¢   rS   r£   r¤   Z	RSA_printrQ   rX   )	r™   r|   rz  r{  rI   Z
cipher_objÚhelperrx  r   r   r   r   r7   *  sH    
ÿ


ù	
ÿc                   @  sr   e Zd Zdddddddœdd„Zed	d
œdd„ƒZed	d
œdd„ƒZefdddœdd„Zd	dd	d	ddœdd„Z	dS )r|  Fr?   ry  r¡   rY   )r™   r{  Ú	more_argsÚtruncater   c                 C  s4   |t kr|d k	rtdƒ‚|| _|| _|| _g | _d S )Nz0only FILETYPE_PEM key format supports encryption)r(   r^   Ú_passphraseÚ
_more_argsÚ	_truncateÚ	_problems)ro   r™   r{  r�  r‚  r   r   r   rp   p  s    ÿz_PassphraseHelper.__init__r   rl   c                 C  s@   | j d krtjS t| j tƒs&t| j ƒr4t d| j¡S tdƒ‚d S )NZpem_password_cbú2Last argument must be a byte string or a callable.)	rƒ  rN   rR   r\   rV   Úcallabler}  Ú_read_passphraser]   rn   r   r   r   r}  €  s    
ÿz_PassphraseHelper.callbackc                 C  s8   | j d krtjS t| j tƒs&t| j ƒr,tjS tdƒ‚d S )Nr‡  )rƒ  rN   rR   r\   rV   rˆ  r]   rn   r   r   r   r~  ‹  s    
ÿz_PassphraseHelper.callback_argsztype[Exception])ÚexceptionTyper   c                 C  s8   | j r4zt|ƒ W n |k
r&   Y nX | j  d¡‚d S rá   )r†  Ú_exception_from_error_queueÚpop)ro   rŠ  r   r   r   r  –  s    z"_PassphraseHelper.raise_if_problem)ÚbufÚsizeÚrwflagÚuserdatar   c              
   C  sä   zªt | jƒr.| jr"|  |||¡}qB|  |¡}n| jd k	s<t‚| j}t|tƒsTtdƒ‚t|ƒ|kr|| jrt|d |… }ntdƒ‚t	t|ƒƒD ]}|||d … ||< qˆt|ƒW S  t
k
rÞ } z| j |¡ W Y ¢dS d }~X Y nX d S )NzBytes expectedz+passphrase returned by callback is too longr›   r   )rˆ  rƒ  r„  ÚAssertionErrorr\   rV   r^   rP   r…  rÓ   Ú	Exceptionr†  rs   )ro   r�  rŽ  r�  r�  rž   rÙ   Úer   r   r   r‰     s*    

ÿ
z"_PassphraseHelper._read_passphraseN)FF)
rB   rC   rD   rp   Úpropertyr}  r~  r-   r  r‰  r   r   r   r   r|  o  s     û


r|  zstr | bytesc                 C  s�   t |tƒr| d¡}t|ƒ}| tkr<t |tjtjtj¡}n | t	krTt 
|tj¡}ntdƒ‚|tjkrltƒ  t t¡}t |tj¡|_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½   rv  T)r\   r   rØ   rU   r(   rK   ZPEM_read_bio_PUBKEYrN   rR   r'   Zd2i_PUBKEY_bior^   r¥   r.   rø   rS   rx   ry   r‚   )r™   rG   rI   Úevp_pkeyr|   r   r   r   r>   ½  s&    

   ÿ

)r™   rG   r{  r   c                 C  sœ   t |tƒr| d¡}t|ƒ}t| |ƒ}| tkrNt |tj	|j
|j¡}| ¡  n | tkrft |tj	¡}ntdƒ‚|tj	kr~tƒ  t t¡}t |tj¡|_|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½   rv  )r\   r   rØ   rU   r|  r(   rK   ZPEM_read_bio_PrivateKeyrN   rR   r}  r~  r  r'   Zd2i_PrivateKey_bior^   r¥   r.   rø   rS   rx   ry   )r™   rG   r{  rI   r€  r•  r|   r   r   r   r=   ß  s(    


   ÿ


)r™   rì   r   c                 C  sn   t ƒ }| tkrt ||j¡}n<| tkr6t ||j¡}n$| tkrRt ||jdd¡}nt	dƒ‚t
|dkƒ t|ƒS )av  
    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


    .. deprecated:: 24.2.0
       Use `cryptography.x509.CertificateSigningRequest` instead.
    r   rw  )rU   r(   rK   ZPEM_write_bio_X509_REQrê   r'   Zi2d_X509_REQ_bior)   ZX509_REQ_print_exr^   rQ   rX   )r™   rì   rI   rx  r   r   r   r6     s    ÿrÂ   c                 C  sˆ   t |tƒr| d¡}t|ƒ}| tkr<t |tjtjtj¡}n | t	krTt 
|tj¡}ntdƒ‚t|tjkƒ t t¡}t |tj¡|_|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

    .. deprecated:: 24.2.0
       Use `cryptography.x509.load_der_x509_csr` or
       `cryptography.x509.load_pem_x509_csr` instead.
    r½   rv  )r\   r   rØ   rU   r(   rK   ZPEM_read_bio_X509_REQrN   rR   r'   Zd2i_X509_REQ_bior^   rQ   r0   rø   rS   ré   rê   )r™   rG   rI   rì   Zx509reqr   r   r   r<   6  s    


)N)NN)N)mÚ
__future__r   rV  r*  Ú	functoolsÚsysrƒ   Úbase64r   Úcollections.abcr   r   r   r   r   Úversion_infoÚwarningsr   ÚTypeVarZ_TZtyping_extensionsZcryptographyr   r   Z)cryptography.hazmat.primitives.asymmetricr   r   r   r   r   ZOpenSSL._utilr    r!   rÐ   r"   r‹  r#   rN   r$   rK   r%   Z_make_assertr&   r]  Ú__all__r‹   r�   r�   r‘   r“   Z_PrivateKeyrŒ   rŽ   r�   r’   r”   Z
_PublicKeyr}   rV   ZPassphraseCallableTZSSL_FILETYPE_PEMr(   rL  ZSSL_FILETYPE_ASN1r'   r)   r¢   r+   ZEVP_PKEY_DSAr*   ZEVP_PKEY_DHr@   ZEVP_PKEY_ECrA   r’  r-   r¥   rQ   rU   rX   r`   rc   rj   rk   r.   r©   r:   r9   Útotal_orderingr/   r0   r,   r4   r1   r3   r2   r;   r5   r8   r7   r|  r>   r=   r6   rï   rB   ÚDeprecationWarningr<   ró   r   r   r   r   Ú<module>   s   


èüÿüÿ
	 Bgÿÿ Cÿ ,   3 +   üEN% ý) ø ø