o
    àý°j1õ  ã                   @  sB  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rCd dlm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) g d¢Z*eej+ej,ej-ej.ej/f Z0eej1ej2ej3ej4ej5f Z6ee0e6f Z7ee8ede8f f Z9e%j:Z;de<d< e%j=Z>de<d< dZ?e%j@ZAde<d< e%jBZCde<d< e%jDZEde<d< e%jFZGde<d< G dd„ deHƒZIee!eIƒZJe'eIƒZKdfdgd"d#„ZLdhd&d'„ZMdid+d,„ZNdjd-d.„ZOdkd0d1„ZPG d2d3„ d3ƒZQG d4d5„ d5ƒZRG d6d7„ d7ƒZSed8ƒdld:d;„ƒZTed<ƒdmd?d@„ƒZUedAƒejVG dBdC„ dCƒƒƒZWG dDdE„ dEƒZXG dFdG„ dGƒZYG dHdI„ dIƒZZG dJdK„ dKeHƒZ[G dLdM„ dMƒZ\dndOdP„Z]dodRdS„Z^dpdUdV„Z_		dqdrd[d\„Z`e`Zaeje`ebd]ecd\d^� G d_d`„ d`ƒZddsdbdc„Ze	dfdtddde„ZfdS )ué    )ÚannotationsN)Ú	b16encode)ÚSequence)Úpartial)ÚAnyÚCallableÚUnion)é   é   )Ú
deprecated)Ú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Ú	X509StoreÚX509StoreContextÚX509StoreContextErrorÚX509StoreFlagsÚdump_certificateÚdump_privatekeyÚdump_publickeyÚget_elliptic_curveÚget_elliptic_curvesÚload_certificateÚload_privatekeyÚload_publickey.Úintr   r   iÿÿ  r   r   Ú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__© r6   r6   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/OpenSSL/crypto.pyr    j   s    r    Úbufferúbytes | NoneÚreturnr   c                 C  sf   | du rt  t  ¡ ¡}t j}nt d| ¡}t  |t| ƒ¡}|f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[]Úbior   Úrefr:   c                 S  s
   t  | ¡S ©N)Ú_libÚBIO_free)r<   r=   r6   r6   r7   Úfree…   ó   
z_new_mem_buf.<locals>.free)r<   r   r=   r   r:   r   )r?   ÚBIO_newÚ	BIO_s_memr@   Ú_ffiÚnewÚBIO_new_mem_bufÚlenÚ_openssl_assertÚNULLÚgc)r8   r<   rA   Údatar6   r6   r7   Ú_new_mem_buft   s   	rM   r<   Úbytesc                 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)rE   rF   r?   ÚBIO_get_mem_datar8   )r<   Úresult_bufferÚbuffer_lengthr6   r6   r7   Ú_bio_to_stringŽ   s   
rR   ÚboundaryÚwhenÚNonec                 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)	Ú
isinstancerN   Ú	TypeErrorrI   rE   rJ   r?   ÚASN1_TIME_set_stringÚ
ValueError)rS   rT   Ú
set_resultr6   r6   r7   Ú_set_asn1_time—   s   
ÿ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.
    )r?   ÚASN1_TIME_newrI   rE   rJ   rK   ÚASN1_TIME_freer[   )rT   Úretr6   r6   r7   Ú_new_asn1_time°   s
   
r_   Ú	timestampc                 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 )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**)rE   Úcastr?   ÚASN1_STRING_lengthÚASN1_STRING_typeÚV_ASN1_GENERALIZEDTIMEÚstringÚASN1_STRING_get0_datarF   ÚASN1_TIME_to_generalizedtimerI   rJ   ÚASN1_GENERALIZEDTIME_free)r`   Ústring_timestampÚgeneralized_timestampÚstring_dataÚstring_resultr6   r6   r7   Ú_get_asn1_timeÃ   s   



rm   c                   @  s*   e Zd Zddd„Zddd„Zdd	d
„ZdS )Ú_X509NameInvalidatorr:   rU   c                 C  s
   g | _ d S r>   )Ú_names©Úselfr6   r6   r7   Ú__init__á   rB   z_X509NameInvalidator.__init__Únamer"   c                 C  s   | j  |¡ d S r>   )ro   Úappend©rq   rs   r6   r6   r7   Úaddä   s   z_X509NameInvalidator.addc                 C  s   | j D ]}|`qd S r>   )ro   Ú_nameru   r6   r6   r7   Úclearç   s   
þz_X509NameInvalidator.clearN©r:   rU   ©rs   r"   r:   rU   )r2   r3   r4   rr   rv   rx   r6   r6   r6   r7   rn   à   s    

rn   c                   @  sr   e Zd ZdZdZdZddd„Zdd	d
„Zeddd„ƒZ	e
dƒd dd„ƒZe
dƒd!dd„ƒZd"dd„Zd"dd„ZdS )#r!   zD
    A class representing an DSA or RSA public key or key pair.
    FTr:   rU   c                 C  s"   t  ¡ }t |t j¡| _d| _d S )NF)r?   ÚEVP_PKEY_newrE   rK   ÚEVP_PKEY_freeÚ_pkeyÚ_initialized©rq   Úpkeyr6   r6   r7   rr   õ   s   
zPKey.__init__Ú_Keyc                 C  sN   ddl m}m} | jrtt| ƒ}t t||ƒ¡S t	t| ƒ}t t||d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_publicr)   r   Útypingra   r�   Ú_dump_privatekey_internal)rq   r‚   rƒ   Úderr6   r6   r7   Úto_cryptography_keyú   s   

zPKey.to_cryptography_keyÚ
crypto_keyc                 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ƒrCtt| |j|j¡ƒS | |j|j|ƒ ¡}tt|ƒ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ÚPublicFormat)rV   r   ÚDSAPrivateKeyÚDSAPublicKeyr   ÚEllipticCurvePrivateKeyÚEllipticCurvePublicKeyr   ÚEd25519PrivateKeyÚEd25519PublicKeyr   ÚEd448PrivateKeyÚEd448PublicKeyr   ÚRSAPrivateKeyÚRSAPublicKeyrW   r…   rŒ   r�   rŽ   r�   r.   r   Úpublic_bytesÚDERÚSubjectPublicKeyInfoÚprivate_bytesÚPKCS8r-   )Úclsr‹   rŒ   r�   rŽ   r�   r‰   r6   r6   r7   Úfrom_cryptography_key  sF   öþûþ
ÿþÿ
zPKey.from_cryptography_keyz`PKey.generate_key is deprecated. You should use the key generation APIs in cryptography instead.Útyper/   Úbitsc              	   C  s4  t |tƒs	tdƒ‚t |tƒstdƒ‚|tkrQ|dkrtdƒ‚t ¡ }t |tj	¡}t 
|tj¡ t ¡ }t |||tj¡}t|dkƒ t | j|¡}t|dkƒ nD|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)rV   r/   rW   r   rY   r?   ÚBN_newrE   rK   ÚBN_freeÚBN_set_wordÚRSA_F4ÚRSA_newÚRSA_generate_key_exrJ   rI   ÚEVP_PKEY_assign_RSAr}   r   ÚDSA_newÚDSA_freeÚDSA_generate_parameters_exÚDSA_generate_keyÚEVP_PKEY_set1_DSAr    r~   )rq   r¡   r¢   Úexponentr   Úresultr   Úresr6   r6   r7   Úgenerate_keyK  s6   

ÿ
zPKey.generate_keyzJPKey.check is deprecated. You should use the APIs in cryptography instead.Úboolc                 C  sd   | j rtdƒ‚t |  ¡ ¡tjkrtdƒ‚t | j¡}t 	|tj
¡}t |¡}|dkr-d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†   rW   r?   ÚEVP_PKEY_typer¡   ÚEVP_PKEY_RSAÚEVP_PKEY_get1_RSAr}   rE   rK   ÚRSA_freeÚRSA_check_keyÚ_raise_current_error)rq   r   r±   r6   r6   r7   Úcheck‡  s   

z
PKey.checkc                 C  ó   t  | j¡S )zT
        Returns the type of the key

        :return: The type of the key.
        )r?   ÚEVP_PKEY_idr}   rp   r6   r6   r7   r¡   ¥  ó   z	PKey.typec                 C  r¼   )zh
        Returns the number of bits of the key

        :return: The number of bits of the key.
        )r?   ÚEVP_PKEY_bitsr}   rp   r6   r6   r7   r¢   ­  r¾   z	PKey.bitsNry   )r:   r�   )r‹   r�   r:   r!   )r¡   r/   r¢   r/   r:   rU   ©r:   r´   ©r:   r/   )r2   r3   r4   r5   r†   r~   rr   rŠ   Úclassmethodr    r   r³   r»   r¡   r¢   r6   r6   r6   r7   r!   í   s$    

9ÿ8ÿ
r!   c                      sn   e Zd ZdZdZd‡ fdd„Zeddd„ƒZeddd„ƒZeddd„ƒZ	ddd„Z
d dd„Z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`
    NÚotherr   r:   r´   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.
        )rV   rÃ   ÚsuperÚ__ne__ÚNotImplemented©rq   rÄ   ©Ú	__class__r6   r7   rÆ   Â  s   
z_EllipticCurve.__ne__r   úset[_EllipticCurve]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   r6   r7   Ú	<genexpr>Ý  s   € z7_EllipticCurve._load_elliptic_curves.<locals>.<genexpr>)ÚEC_get_builtin_curvesrE   rJ   rF   Úset)rŸ   r   Ú
num_curvesÚbuiltin_curvesr6   rÐ   r7   Ú_load_elliptic_curvesÍ  s   
z$_EllipticCurve._load_elliptic_curvesc                 C  s   | j du 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Ð   r6   r6   r7   Ú_get_elliptic_curvesß  s   

z#_EllipticCurve._get_elliptic_curvesrÍ   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)rE   re   Ú
OBJ_nid2snÚdecode)rŸ   r   rÍ   r6   r6   r7   rÌ   í  s   z_EllipticCurve.from_nidrs   ÚstrrU   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)r?   Ú_nidrs   )rq   r   rÍ   rs   r6   r6   r7   rr   þ  s   
z_EllipticCurve.__init__c                 C  s   d| j ›d�S )Nz<Curve ú>©rs   rp   r6   r6   r7   Ú__repr__  ó   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.
        )r?   ÚEC_KEY_new_by_curve_namerÝ   rE   rK   ÚEC_KEY_free)rq   Úkeyr6   r6   r7   Ú
_to_EC_KEY  s   z_EllipticCurve._to_EC_KEY©rÄ   r   r:   r´   )r   r   r:   rË   )r   r   rÍ   r/   r:   rÃ   )r   r   rÍ   r/   rs   rÜ   r:   rU   ©r:   rÜ   ©r:   r   )r2   r3   r4   r5   r×   rÆ   rÂ   rÖ   rØ   rÌ   rr   rà   rå   Ú__classcell__r6   r6   rÉ   r7   rÃ   ¶  s    	

rÃ   zSget_elliptic_curves is deprecated. You should use the APIs in cryptography instead.rË   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Ø   r?   r6   r6   r6   r7   r+     s   
r+   zRget_elliptic_curve is deprecated. You should use the APIs in cryptography instead.rs   rÜ   c                 C  s(   t ƒ D ]}|j| kr|  S qtd| ƒ‚)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 name)r+   rs   rY   )rs   Úcurver6   r6   r7   r*   0  s
   

ÿ
r*   zUX509Name support in pyOpenSSL is deprecated. You should use the APIs in cryptography.c                      sr   e Zd ZdZd dd„Zd!‡ fd
d„Zd"dd„Zd#dd„Zd#dd„Zd$dd„Z	d%dd„Z
d&dd„Z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.
    rs   r:   rU   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)r?   ÚX509_NAME_duprw   rE   rK   ÚX509_NAME_freeru   r6   r6   r7   rr   d  s   zX509Name.__init__rÜ   Úvaluer   c           	   	     s  |  d¡rtƒ  ||¡S t|ƒturtdt|ƒjd›d�ƒ‚t t	|ƒ¡}|tj
kr?ztƒ  W tdƒ‚ ty>   Y tdƒ‚w tt | j¡ƒD ]%}t | j|¡}t |¡}t |¡}||krlt | j|¡}t |¡  nqGt|tƒrw| d¡}t | j|tj|t|ƒdd¡}|s�tƒ  d S d S )	NÚ_z$attribute name must be string, not 'z.200ú'úNo such attributeúutf-8éÿÿÿÿr   )Ú
startswithrÅ   Ú__setattr__r¡   rÜ   rW   r2   r?   ÚOBJ_txt2nidÚ_byte_stringÚ	NID_undefrº   r    ÚAttributeErrorÚrangeÚX509_NAME_entry_countrw   ÚX509_NAME_get_entryÚX509_NAME_ENTRY_get_objectÚOBJ_obj2nidÚX509_NAME_delete_entryÚX509_NAME_ENTRY_freerV   ÚencodeÚX509_NAME_add_entry_by_NIDÚMBSTRING_UTF8rH   )	rq   rs   rí   rÍ   ÚiÚentÚent_objÚent_nidÚ
add_resultrÉ   r6   r7   rô   n  sD   

ÿÿ
þþ


ý

ÿ
ÿzX509Name.__setattr__ú
str | Nonec           	   
   C  sÜ   t  t|ƒ¡}|t jkr!ztƒ  W tdƒ‚ ty    Y tdƒ‚w t  | j|d¡}|dkr/dS t  	| j|¡}t  
|¡}t d¡}t  ||¡}t|dkƒ zt |d |¡dd…  d¡}W t  |d ¡ |S t  |d ¡ w )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ñ   )r?   rõ   rö   r÷   rº   r    rø   ÚX509_NAME_get_index_by_NIDrw   rû   ÚX509_NAME_ENTRY_get_datarE   rF   ÚASN1_STRING_to_UTF8rI   r8   rÛ   ÚOPENSSL_free)	rq   rs   rÍ   Úentry_indexÚentryrL   rP   Údata_lengthr±   r6   r6   r7   Ú__getattr__•  s0   
þþ

ÿÿzX509Name.__getattr__rÄ   r´   c                 C  s"   t |tƒstS t | j|j¡dkS ©Nr   ©rV   r"   rÇ   r?   ÚX509_NAME_cmprw   rÈ   r6   r6   r7   Ú__eq__½  ó   
zX509Name.__eq__c                 C  s"   t |tƒstS t | j|j¡dk S r  r  rÈ   r6   r6   r7   Ú__lt__Ã  r  zX509Name.__lt__c                 C  sD   t  dd¡}t | j|t|ƒ¡}t|t jkƒ d t  	|¡ 
d¡¡S )z6
        String representation of an X509Name
        r;   i   z<X509Name object '{}'>rñ   )rE   rF   r?   ÚX509_NAME_onelinerw   rH   rI   rJ   Úformatre   rÛ   )rq   rP   Úformat_resultr6   r6   r7   rà   É  s   ÿÿzX509Name.__repr__r/   c                 C  r¼   )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`
        )r?   ÚX509_NAME_hashrw   rp   r6   r6   r7   Úhash×  ó   
zX509Name.hashrN   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)rE   rF   r?   Úi2d_X509_NAMErw   rI   r8   r  )rq   rP   Úencode_resultrl   r6   r6   r7   r‰   ã  s   
zX509Name.derúlist[tuple[bytes, bytes]]c           	      C  sˆ   g }t t | j¡ƒD ]7}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ù   r?   rú   rw   rû   rü   r  rý   rÚ   rE   r8   rf   rb   rt   re   )	rq   r±   r  r  ÚfnameÚfvalrÍ   rs   rí   r6   r6   r7   Úget_componentsò  s   



ÿþzX509Name.get_componentsrz   )rs   rÜ   rí   r   r:   rU   )rs   rÜ   r:   r  ræ   rç   rÁ   ©r:   rN   )r:   r   )r2   r3   r4   r5   rr   rô   r  r  r  rà   r  r‰   r#  ré   r6   r6   rÉ   r7   r"   F  s    


'
(



r"   c                   @  s¤  e Zd ZdZdddd„Zededd	„ƒZdfdd„Zedgdd„ƒZe	dƒdhdd„ƒZ
didd„Zdjdd„Ze	dƒdkdd„ƒZe	dƒdld!d"„ƒZdmd$d%„Zdnd'd(„Zdid)d*„Ze	d+ƒdod-d.„ƒZdid/d0„Ze	d1ƒdpd3d4„ƒZe	d5ƒdpd6d7„ƒZdqd9d:„Zdrd=d>„Zdsd?d@„ZdtdCdD„Ze	dEƒdudFdG„ƒZdsdHdI„Ze	dJƒdudKdL„ƒZdvdNdO„ZdwdQdR„Ze	dSƒdxdTdU„ƒZe	dVƒdydXdY„ƒZ e	dZƒdxd[d\„ƒZ!e	d]ƒdzd_d`„ƒZ"didadb„Z#dcS ){r   z
    An X.509 certificate.
    r:   rU   c                 C  s:   t  ¡ }t|tjkƒ t |t j¡| _tƒ | _	tƒ | _
d S r>   )r?   ÚX509_newrI   rE   rJ   rK   Ú	X509_freeÚ_x509rn   Ú_issuer_invalidatorÚ_subject_invalidator)rq   r   r6   r6   r7   rr     s
   zX509.__init__r   r   c                 C  s.   |   | ¡}t |tj¡|_tƒ |_tƒ |_|S r>   )	Ú__new__rE   rK   r?   r&  r'  rn   r(  r)  )rŸ   r   Úcertr6   r6   r7   Ú_from_raw_x509_ptr  s
   
zX509._from_raw_x509_ptrú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)Úcryptography.x509r.  r'   r   )rq   r.  r‰   r6   r6   r7   Úto_cryptography"  s   
zX509.to_cryptographyÚcrypto_certc                 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Œ   )
rV   r   ÚCertificaterW   r…   rŒ   rš   r›   r,   r   )rŸ   r1  rŒ   r‰   r6   r6   r7   Úfrom_cryptography/  s
   
zX509.from_cryptographyzYX509.set_version is deprecated. You should use cryptography's CertificateBuilder instead.Úversionr/   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)rV   r/   rW   rI   r?   ÚX509_set_versionr'  )rq   r5  r6   r6   r7   Úset_versionC  s   
zX509.set_versionc                 C  r¼   )z˜
        Return the version number of the certificate.

        :return: The version number of the certificate.
        :rtype: :py:class:`int`
        )r?   ÚX509_get_versionr'  rp   r6   r6   r7   Úget_versionV  ó   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*  r?   ÚX509_get_pubkeyr'  r}   rE   rJ   rº   rK   r|   r†   r   r6   r6   r7   Ú
get_pubkey_  s   
zX509.get_pubkeyzXX509.set_pubkey is deprecated. You should use cryptography's CertificateBuilder instead.r€   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`
        úpkey must be a PKey instancer£   N)rV   r!   rW   r?   ÚX509_set_pubkeyr'  r}   rI   )rq   r€   rZ   r6   r6   r7   Ú
set_pubkeyn  s   
zX509.set_pubkeyzRX509.sign is deprecated. You should use cryptography's CertificateBuilder instead.ÚdigestrÜ   c                 C  sp   t |tƒs	tdƒ‚|jrtdƒ‚|jstdƒ‚t t|ƒ¡}|t	j
kr't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 partzKey is uninitializedúNo such digest methodr   N)rV   r!   rW   r†   rY   r~   r?   ÚEVP_get_digestbynamerö   rE   rJ   Ú	X509_signr'  r}   rI   )rq   r€   r@  Úevp_mdÚsign_resultr6   r6   r7   Úsign�  s   

z	X509.signrN   c                 C  sZ   t  | j¡}t d¡}t  |tjtj|¡ t  |d ¡}|t jkr%t	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)r?   ÚX509_get0_tbs_sigalgr'  rE   rF   ÚX509_ALGOR_get0rJ   rý   r÷   rY   re   Ú
OBJ_nid2ln)rq   Úsig_algÚalgrÍ   r6   r6   r7   Úget_signature_algorithm¡  s   

zX509.get_signature_algorithmÚdigest_namec                 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`
        rA  zunsigned char[]zunsigned int[]r£   r   ó   :c                 S  s   g | ]}t |ƒ ¡ ‘qS r6   )r   Úupper)rÎ   Úchr6   r6   r7   Ú
<listcomp>Í  s    
ÿÿzX509.digest.<locals>.<listcomp>)r?   rB  rö   rE   rJ   rY   rF   ÚEVP_MAX_MD_SIZErH   ÚX509_digestr'  rI   Újoinr8   )rq   rM  r@  rP   Úresult_lengthÚdigest_resultr6   r6   r7   r@  ´  s   

ÿþÿzX509.digestc                 C  r¼   )z�
        Return the hash of the X509 subject.

        :return: The hash of the subject.
        :rtype: :py:class:`int`
        )r?   ÚX509_subject_name_hashr'  rp   r6   r6   r7   Úsubject_name_hashÓ  r:  zX509.subject_name_hashz_X509.set_serial_number is deprecated. You should use cryptography's CertificateBuilder instead.Úserialc                 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£   )rV   r/   rW   Úhexr   rE   rF   r?   Ú	BN_hex2bnrI   rJ   ÚBN_to_ASN1_INTEGERr¥   rK   ÚASN1_INTEGER_freeÚX509_set_serialNumberr'  )rq   rY  Ú
hex_serialÚhex_serial_bytesÚbignum_serialr±   Úasn1_serialrZ   r6   r6   r7   Úset_serial_numberÜ  s   


zX509.set_serial_numberc              	   C  sp   t  | j¡}t  |tj¡}z$t  |¡}zt |¡}t|dƒ}|W t  	|¡ W t  
|¡ S t  	|¡ w t  
|¡ w )zx
        Return the serial number of this certificate.

        :return: The serial number.
        :rtype: int
        é   )r?   ÚX509_get_serialNumberr'  ÚASN1_INTEGER_to_BNrE   rJ   Ú	BN_bn2hexre   r/   r  r¥   )rq   rc  rb  r`  Úhexstring_serialrY  r6   r6   r7   Úget_serial_numberü  s   


þzX509.get_serial_numberzaX509.gmtime_adj_notAfter is deprecated. You should use cryptography's CertificateBuilder instead.Úamountc                 C  ó.   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)rV   r/   rW   r?   ÚX509_getm_notAfterr'  ÚX509_gmtime_adj)rq   rk  ÚnotAfterr6   r6   r7   Úgmtime_adj_notAfter  s   
zX509.gmtime_adj_notAfterzbX509.gmtime_adj_notBefore is deprecated. You should use cryptography's CertificateBuilder instead.c                 C  rl  )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``
        rm  N)rV   r/   rW   r?   ÚX509_getm_notBeforer'  ro  )rq   rk  Ú	notBeforer6   r6   r7   Úgmtime_adj_notBefore"  s   
zX509.gmtime_adj_notBeforer´   c                 C  sT   |   ¡ }|du 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_notAfterrY   rÛ   ÚdatetimeÚstrptimeÚtimezoneÚutcÚnowÚreplace)rq   Ú
time_bytesÚtime_stringÚ	not_afterÚUTCÚutcnowr6   r6   r7   Úhas_expired3  s   
zX509.has_expiredÚwhichr9   c                 C  s   t || jƒƒS r>   )rm   r'  )rq   rƒ  r6   r6   r7   Ú_get_boundary_timeD  rá   zX509._get_boundary_timec                 C  ó   |   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
        )r„  r?   rr  rp   r6   r6   r7   Úget_notBeforeG  ó   zX509.get_notBeforeúCallable[..., Any]rT   c                 C  s   t || jƒ|ƒS r>   )r[   r'  )rq   rƒ  rT   r6   r6   r7   Ú_set_boundary_timeT  s   zX509._set_boundary_timez[X509.set_notBefore is deprecated. You should use cryptography's CertificateBuilder instead.c                 C  ó   |   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``
        )r‰  r?   rr  ©rq   rT   r6   r6   r7   Úset_notBeforeY  ó   zX509.set_notBeforec                 C  r…  )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
        )r„  r?   rn  rp   r6   r6   r7   rv  j  r‡  zX509.get_notAfterzZX509.set_notAfter is deprecated. You should use cryptography's CertificateBuilder instead.c                 C  rŠ  )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``
        )r‰  r?   rn  r‹  r6   r6   r7   Úset_notAfterw  r�  zX509.set_notAfterr"   c                 C  s0   t  t¡}|| jƒ|_t|jtjkƒ | |_|S r>   )	Úobjectr*  r"   r'  rw   rI   rE   rJ   Ú_owner)rq   rƒ  rs   r6   r6   r7   Ú	_get_nameˆ  s
   
zX509._get_namers   c                 C  s0   t |tƒs	tdƒ‚|| j|jƒ}t|dkƒ d S )Nzname must be an X509Namer£   )rV   r"   rW   r'  rw   rI   )rq   rƒ  rs   rZ   r6   r6   r7   Ú	_set_name•  s   
zX509._set_namezPX509.get_issuer is deprecated. You should use cryptography's X.509 APIs instead.c                 C  ó   |   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‘  r?   ÚX509_get_issuer_namer(  rv   ru   r6   r6   r7   Ú
get_issuer›  ó   zX509.get_issuerzXX509.set_issuer is deprecated. You should use cryptography's CertificateBuilder instead.Úissuerc                 C  ó   |   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’  r?   ÚX509_set_issuer_namer(  rx   )rq   r—  r6   r6   r7   Ú
set_issuer¯  ó   zX509.set_issuerzQX509.get_subject is deprecated. You should use cryptography's X.509 APIs instead.c                 C  r“  )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‘  r?   ÚX509_get_subject_namer)  rv   ru   r6   r6   r7   Úget_subject¿  r–  zX509.get_subjectzYX509.set_subject is deprecated. You should use cryptography's CertificateBuilder instead.Úsubjectc                 C  r˜  )z£
        Set the subject of this certificate.

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

        :return: ``None``
        N)r’  r?   ÚX509_set_subject_namer)  rx   )rq   rž  r6   r6   r7   Úset_subjectÓ  r›  zX509.set_subjectc                 C  r¼   )z¯
        Get the number of extensions on this certificate.

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

        .. versionadded:: 0.12
        )r?   ÚX509_get_ext_countr'  rp   r6   r6   r7   Úget_extension_countã  s   	zX509.get_extension_countNry   )r   r   r:   r   )r:   r-  )r1  r-  r:   r   )r5  r/   r:   rU   rÁ   )r:   r!   )r€   r!   r:   rU   )r€   r!   r@  rÜ   r:   rU   r$  )rM  rÜ   r:   rN   )rY  r/   r:   rU   )rk  r/   r:   rU   rÀ   )rƒ  r   r:   r9   )r:   r9   )rƒ  rˆ  rT   rN   r:   rU   )rT   rN   r:   rU   )rƒ  r   r:   r"   )rƒ  r   rs   r"   r:   rU   )r:   r"   )r—  r"   r:   rU   )rž  r"   r:   rU   )$r2   r3   r4   r5   rr   rÂ   r,  r0  r4  r   r7  r9  r<  r?  rF  rL  r@  rX  rd  rj  rq  rt  r‚  r„  r†  r‰  rŒ  rv  rŽ  r‘  r’  r•  rš  r�  r   r¢  r6   r6   r6   r7   r     sŒ    

ÿ

	ÿÿ


	ÿ
ÿÿ



ÿ
ÿ

ÿÿÿÿr   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 )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
    r/   Ú	CRL_CHECKÚCRL_CHECK_ALLÚIGNORE_CRITICALÚX509_STRICTÚALLOW_PROXY_CERTSÚPOLICY_CHECKÚEXPLICIT_POLICYÚINHIBIT_MAPÚCHECK_SS_SIGNATUREÚPARTIAL_CHAINN)r2   r3   r4   r5   r?   ÚX509_V_FLAG_CRL_CHECKr£  Ú__annotations__ÚX509_V_FLAG_CRL_CHECK_ALLr¤  ÚX509_V_FLAG_IGNORE_CRITICALr¥  ÚX509_V_FLAG_X509_STRICTr¦  ÚX509_V_FLAG_ALLOW_PROXY_CERTSr§  ÚX509_V_FLAG_POLICY_CHECKr¨  ÚX509_V_FLAG_EXPLICIT_POLICYr©  ÚX509_V_FLAG_INHIBIT_MAPrª  ÚX509_V_FLAG_CHECK_SS_SIGNATUREr«  ÚX509_V_FLAG_PARTIAL_CHAINr¬  r6   r6   r6   r7   r&   ï  s   
 
r&   c                   @  sP   e Zd ZdZddd„Zddd	„Zddd„Zddd„Zd dd„Z	d!d"dd„Z	d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`.
    r:   rU   c                 C  s   t  ¡ }t |t j¡| _d S r>   )r?   ÚX509_STORE_newrE   rK   ÚX509_STORE_freeÚ_store©rq   Ústorer6   r6   r7   rr     s   zX509Store.__init__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)rV   r   rW   r?   ÚX509_STORE_add_certrº  r'  rI   )rq   r+  r²   r6   r6   r7   Úadd_cert  s   
zX509Store.add_certÚcrlúx509.CertificateRevocationListc                 C  sv   t |tjƒr*d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   r2  z?CRL must be of type cryptography.x509.CertificateRevocationListN)rV   r   ÚCertificateRevocationListr…   rŒ   rM   rš   r›   r?   Úd2i_X509_CRL_biorE   rJ   rI   rK   ÚX509_CRL_freerW   ÚX509_STORE_add_crlrº  )rq   r¿  rŒ   r<   Úopenssl_crlr6   r6   r7   Úadd_crl.  s   ÿzX509Store.add_crlÚ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)rI   r?   ÚX509_STORE_set_flagsrº  )rq   rÇ  r6   r6   r7   Ú	set_flagsL  s   zX509Store.set_flagsÚvfy_timeúdatetime.datetimec                 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)r?   ÚX509_VERIFY_PARAM_newrE   rK   ÚX509_VERIFY_PARAM_freeÚX509_VERIFY_PARAM_set_timeÚcalendarÚtimegmÚ	timetuplerI   ÚX509_STORE_set1_paramrº  )rq   rÊ  Úparamr6   r6   r7   Úset_timed  s   ÿzX509Store.set_timeNÚcafileúStrOrBytesPath | NoneÚcapathc                 C  sR   |du rt j}nt|ƒ}|du rt j}nt|ƒ}t | j||¡}|s'tƒ  dS 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)rE   rJ   Ú_path_bytesr?   ÚX509_STORE_load_locationsrº  rº   )rq   rÕ  r×  Úload_resultr6   r6   r7   Úload_locations|  s   #ÿ
ÿzX509Store.load_locationsry   )r+  r   r:   rU   )r¿  rÀ  r:   rU   )rÇ  r/   r:   rU   )rÊ  rË  r:   rU   r>   )rÕ  rÖ  r×  rÖ  r:   rU   )
r2   r3   r4   r5   rr   r¾  rÆ  rÉ  rÔ  rÛ  r6   r6   r6   r7   r#     s    




ýr#   c                      s"   e Zd ZdZd‡ fd
d„Z‡  ZS )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`
    ÚmessagerÜ   Úerrorsú	list[Any]Úcertificater   r:   rU   c                   s   t ƒ  |¡ || _|| _d S r>   )rÅ   rr   rÝ  rß  )rq   rÜ  rÝ  rß  rÉ   r6   r7   rr   ¹  s   
zX509StoreContextError.__init__)rÜ  rÜ   rÝ  rÞ  rß  r   r:   rU   )r2   r3   r4   r5   rr   ré   r6   r6   rÉ   r7   r%   °  s    r%   c                   @  sb   e Zd ZdZ	dddd„Zed dd„ƒZed!dd„ƒZd"dd„Zd#dd„Z	d$dd„Z
d%dd„ZdS )&r$   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`
    Nr¼  r#   rß  r   ÚchainúSequence[X509] | Noner:   rU   c                 C  s   || _ || _|  |¡| _d S r>   )rº  Ú_certÚ_build_certificate_stackÚ_chain)rq   r¼  rß  rà  r6   r6   r7   rr   Ñ  s   zX509StoreContext.__init__Úcertificatesc                 C  sž   d	dd„}| d u st | ƒdkrtjS t ¡ }t|tjkƒ t ||¡}| D ]'}t|tƒs0t	dƒ‚tt 
|j¡dkƒ t ||j¡dkrLt |j¡ tƒ  q%|S )
NÚsr   r:   rU   c                 S  s8   t t | ¡ƒD ]}t | |¡}t |¡ qt | ¡ d S r>   )rù   r?   Úsk_X509_numÚsk_X509_valuer&  Úsk_X509_free)ræ  r  Úxr6   r6   r7   Úcleanupß  s   z:X509StoreContext._build_certificate_stack.<locals>.cleanupr   z+One of the elements is not an X509 instance)ræ  r   r:   rU   )rH   rE   rJ   r?   Úsk_X509_new_nullrI   rK   rV   r   rW   ÚX509_up_refr'  Úsk_X509_pushr&  rº   )rå  rë  Ústackr+  r6   r6   r7   rã  Û  s   

€z)X509StoreContext._build_certificate_stackÚ	store_ctxr   r%   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ñ   )rE   re   r?   ÚX509_verify_cert_error_stringÚX509_STORE_CTX_get_errorrÛ   ÚX509_STORE_CTX_get_error_depthÚX509_STORE_CTX_get_current_certÚX509_dupr   r,  r%   )rð  rÜ  rÝ  r'  râ  Úpycertr6   r6   r7   Ú_exception_from_contextù  s   	ÿÿüý


z(X509StoreContext._exception_from_contextc                 C  sj   t  ¡ }t|tjkƒ t |t j¡}t  || jj| j	j
| j¡}t|dkƒ t  |¡}|dkr3|  |¡‚|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   )r?   ÚX509_STORE_CTX_newrI   rE   rJ   rK   ÚX509_STORE_CTX_freeÚX509_STORE_CTX_initrº  râ  r'  rä  ÚX509_verify_certr÷  )rq   rð  r^   r6   r6   r7   Ú_verify_certificate  s   	ÿ

z$X509StoreContext._verify_certificatec                 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)rº  r»  r6   r6   r7   Ú	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)rü  rp   r6   r6   r7   Úverify_certificate6  r  z#X509StoreContext.verify_certificateú
list[X509]c                 C  st   |   ¡ }t |¡}t|tjkƒ g }tt |¡ƒD ]}t ||¡}t|tjkƒ t	 
|¡}| |¡ qt |¡ |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
        )rü  r?   ÚX509_STORE_CTX_get1_chainrI   rE   rJ   rù   rç  rè  r   r,  rt   ré  )rq   rð  Ú
cert_stackr±   r  r+  rö  r6   r6   r7   Úget_verified_chainB  s   


z#X509StoreContext.get_verified_chainr>   )r¼  r#   rß  r   rà  rá  r:   rU   )rå  rá  r:   rU   )rð  r   r:   r%   rè   )r¼  r#   r:   rU   ry   )r:   rÿ  )r2   r3   r4   r5   rr   Ústaticmethodrã  r÷  rü  rý  rþ  r  r6   r6   r6   r7   r$   Á  s    ü



r$   r¡   c                 C  sv   t |tƒr
| d¡}t|ƒ}| tkrt |tjtjtj¡}n| t	kr*t 
|tj¡}ntdƒ‚|tjkr6tƒ  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)rV   rÜ   r   rM   r   r?   ÚPEM_read_bio_X509rE   rJ   r   Úd2i_X509_biorY   rº   r   r,  )r¡   r8   r<   r   r6   r6   r7   r,   _  s   



r,   r+  c                 C  sn   t ƒ }| tkrt ||j¡}n| tkrt ||j¡}n| tkr)t ||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£   )rM   r   r?   ÚPEM_write_bio_X509r'  r   Úi2d_X509_bior   ÚX509_print_exrY   rI   rR   )r¡   r+  r<   Úresult_coder6   r6   r7   r'   |  s   
ÿr'   r€   c                 C  sP   t ƒ }| tkrtj}n| tkrtj}ntdƒ‚|||jƒ}|dkr$tƒ  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
    r  r£   )
rM   r   r?   ÚPEM_write_bio_PUBKEYr   Úi2d_PUBKEY_biorY   r}   rº   rR   )r¡   r€   r<   Ú	write_bior  r6   r6   r7   r)   ˜  s   
r)   Úcipherr  Ú
passphraseúPassphraseCallableT | Nonec           	   	   C  s  t ƒ }t|tƒstdƒ‚|dur)|du rtdƒ‚t t|ƒ¡}|tjkr(t	dƒ‚ntj}t
| |ƒ}| tkrIt ||j|tjd|j|j¡}| ¡  n4| tkrUt ||j¡}n(| tkryt |j¡tjkrft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

    .. deprecated:: 26.3.0
       Use the serialization APIs on ``cryptography`` private key types
       instead.
    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_TEXTr  )rM   rV   r!   rW   r?   ÚEVP_get_cipherbynamerö   rE   rJ   rY   Ú_PassphraseHelperr   ÚPEM_write_bio_PrivateKeyr}   ÚcallbackÚcallback_argsÚraise_if_problemr   Úi2d_PrivateKey_bior   r½   r¶   rK   r·   r¸   Ú	RSA_printrI   rR   )	r¡   r€   r  r  r<   Ú
cipher_objÚhelperr  r   r6   r6   r7   r(   ±  sJ   
ÿ
ÿ
ù
	ÿr(   zGdump_privatekey is deprecated. You should use the APIs in cryptography.rß   c                   @  sP   e Zd Z		dddd„Zeddd„ƒZeddd„ƒZefd dd„Zd!dd„Z	dS )"r  Fr¡   r/   r  r  Ú	more_argsr´   Útruncater:   rU   c                 C  s4   |t kr|d urtdƒ‚|| _|| _|| _g | _d S )Nz0only FILETYPE_PEM key format supports encryption)r   rY   Ú_passphraseÚ
_more_argsÚ	_truncateÚ	_problems)rq   r¡   r  r  r  r6   r6   r7   rr   	  s   ÿ
z_PassphraseHelper.__init__r   c                 C  s<   | j d u rtjS t| j tƒst| j ƒrt d| j¡S tdƒ‚)NÚpem_password_cbú2Last argument must be a byte string or a callable.)	r  rE   rJ   rV   rN   Úcallabler  Ú_read_passphraserW   rp   r6   r6   r7   r    s   
ÿz_PassphraseHelper.callbackc                 C  s4   | j d u rtjS t| j tƒst| j ƒrtjS tdƒ‚)Nr#  )r  rE   rJ   rV   rN   r$  rW   rp   r6   r6   r7   r  $  s   
ÿz_PassphraseHelper.callback_argsÚexceptionTypeútype[Exception]c                 C  s6   | j rzt|ƒ W n	 |y   Y nw | j  d¡‚d S r  )r!  Ú_exception_from_error_queueÚpop)rq   r&  r6   r6   r7   r  /  s   ÿùz"_PassphraseHelper.raise_if_problemÚbufÚsizeÚrwflagÚuserdatac              
   C  sÞ   zUt | jƒr| jr|  |||¡}n|  |¡}n
| jd usJ ‚| j}t|tƒs*tdƒ‚t|ƒ|kr>| jr:|d |… }ntdƒ‚tt|ƒƒD ]}|||d … ||< qDt|ƒW S  t	yn } z| j
 |¡ W Y d }~dS d }~ww )NzBytes expectedz+passphrase returned by callback is too longr£   r   )r$  r  r  rV   rN   rY   rH   r   rù   Ú	Exceptionr!  rt   )rq   r*  r+  r,  r-  r±   r  Úer6   r6   r7   r%  9  s.   

ÿ
€þz"_PassphraseHelper._read_passphraseN)FF)
r¡   r/   r  r  r  r´   r  r´   r:   rU   rè   )r&  r'  r:   rU   )
r*  r   r+  r/   r,  r   r-  r   r:   r/   )
r2   r3   r4   rr   Úpropertyr  r  r    r  r%  r6   r6   r6   r7   r    s    û


r  ústr | bytesc                 C  s�   t |tƒr
| d¡}t|ƒ}| tkrt |tjtjtj¡}n| t	kr*t 
|tj¡}ntdƒ‚|tjkr6tƒ  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Ù   r  T)rV   rÜ   r   rM   r   r?   ÚPEM_read_bio_PUBKEYrE   rJ   r   Úd2i_PUBKEY_biorY   rº   r!   r*  rK   r|   r}   r†   )r¡   r8   r<   Úevp_pkeyr€   r6   r6   r7   r.   V  s    

ÿ

r.   c                 C  sœ   t |tƒr
| d¡}t|ƒ}t| |ƒ}| tkr't |tj	|j
|j¡}| ¡  n| tkr3t |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Ù   r  )rV   rÜ   r   rM   r  r   r?   ÚPEM_read_bio_PrivateKeyrE   rJ   r  r  r  r   Úd2i_PrivateKey_biorY   rº   r!   r*  rK   r|   r}   )r¡   r8   r  r<   r  r4  r€   r6   r6   r7   r-   x  s"   


ÿ


r-   r>   )r8   r9   r:   r   )r<   r   r:   rN   )rS   r   rT   rN   r:   rU   )rT   rN   r:   r   )r`   r   r:   r9   )r:   rË   )rs   rÜ   r:   rÃ   )r¡   r/   r8   rN   r:   r   )r¡   r/   r+  r   r:   rN   )r¡   r/   r€   r!   r:   rN   )NN)
r¡   r/   r€   r!   r  r  r  r  r:   rN   )r¡   r/   r8   r1  r:   r!   )r¡   r/   r8   r1  r  r  r:   r!   )gÚ
__future__r   rÏ  rw  Ú	functoolsÚsysr‡   Úbase64r   Úcollections.abcr   r   r   r   r   Úversion_infoÚwarningsr   Útyping_extensionsÚcryptographyr   r   Ú)cryptography.hazmat.primitives.asymmetricr   r   r   r   r   ÚOpenSSL._utilr   r   rö   r   r(  r   rE   r   r?   r   Ú_make_assertr   rØ  Ú__all__r�   r’   r”   r–   r˜   Ú_PrivateKeyr‘   r“   r•   r—   r™   Ú
_PublicKeyr�   rN   ÚPassphraseCallableTÚSSL_FILETYPE_PEMr   r®  ÚSSL_FILETYPE_ASN1r   r   r¶   r   ÚEVP_PKEY_DSAr   ÚEVP_PKEY_DHr0   ÚEVP_PKEY_ECr1   r.  r    rº   rI   rM   rR   r[   r_   rm   rn   r!   rÃ   r+   r*   Útotal_orderingr"   r   r&   r#   r%   r$   r,   r'   r)   r(   rˆ   r2   ÚDeprecationWarningr  r.   r-   r6   r6   r6   r7   Ú<module>   sÌ    
üÿüÿ


	

 Jgÿÿÿ C   e + 


üIø
N%ý