B ©#a<˜ã$@sDddlZddlZddlmZddlmZddlmZmZm Z m Z m Z m Z ddl mZmZmZddlmZmZddlmZmZddlmZmZmZ m!Z"m#Z$m%Z&m'Z(m)Z*m+Z,d d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,g$Z-ej.Z/ej0Z1d-Z2ej3Z4ej5Z6ej7Z8ej9Z:Gd.d„de;ƒZd/d0„Z?d1d2„Z@dbd3d4„ZAd5d6„ZBd7d8„ZCd9d:„ZDGd;d<„dd?„d?eEƒZHd@d„ZIdAd„ZJGdBd„deEƒZKGdCd„deEƒZLGdDd„deEƒZMGdEd„deEƒZNGdFd„deEƒZOGdGd„deEƒZPGdHd„de;ƒZQGdId„deEƒZRdJd„ZSdKd„ZTdLd„ZUdcdMd„ZVGdNd„deEƒZWGdOd„deEƒZXGdPd „d eEƒZYGdQd!„d!eEƒZZGdRd"„d"eEƒZ[GdSdT„dTeEƒZ\dUd#„Z]dddVd$„Z^dWd%„Z_dXd&„Z`dYd'„ZadZd(„Zbd[d)„Zcd\d*„Zdd]d+„Zee feeegd^eh¡Zeded_d,„Zie feiegd`eh¡Zie j¡e k¡e lda¡dS)féN)Ú b16encode)Úpartial)Ú__eq__Ú__ne__Ú__lt__Ú__le__Ú__gt__Ú__ge__)Ú integer_typesÚ text_typeÚPY2)ÚutilsÚx509)ÚdsaÚrsa) ÚffiÚlibÚexception_from_error_queueÚ byte_stringÚnativeÚ path_stringÚ UNSPECIFIEDÚtext_to_bytes_and_warnÚ make_assertÚ FILETYPE_PEMÚ FILETYPE_ASN1Ú FILETYPE_TEXTÚTYPE_RSAÚTYPE_DSAÚErrorÚPKeyÚget_elliptic_curvesÚget_elliptic_curveÚX509NameÚ X509ExtensionÚX509ReqÚX509ÚX509StoreFlagsÚ X509StoreÚX509StoreContextErrorÚX509StoreContextÚload_certificateÚdump_certificateÚdump_publickeyÚdump_privatekeyÚRevokedÚCRLÚPKCS7ÚPKCS12Ú NetscapeSPKIÚload_publickeyÚload_privatekeyÚdump_certificate_requestÚload_certificate_requestÚsignÚverifyÚdump_crlÚload_crlÚload_pkcs7_dataÚ load_pkcs12iÿÿc@seZdZdZdS)rz7 An error occurred in an `OpenSSL.crypto` API. N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©rBrBú5/tmp/pip-target-jgxl_w8r/lib/python/OpenSSL/crypto.pyrPscCsddlm}|S)ap Importing the backend from cryptography has the side effect of activating the osrandom engine. This mutates the global state of OpenSSL in the process and causes issues for various programs that use subinterpreters or embed Python. By putting the import in this function we can avoid triggering this side effect unless _get_backend is called. r)Úbackend)Z,cryptography.hazmat.backends.openssl.backendrD)rDrBrBrCÚ _get_backendZs rEcCstd|fƒ‚dS)zÐ An OpenSSL API failed somehow. Additionally, the failure which was encountered isn't one that's exercised by the test suite so future behavior of pyOpenSSL is now somewhat less predictable. zUnknown %s failureN)Ú RuntimeError)ÚwhererBrBrCÚ_untested_errorgsrHcCsd|dkrt t ¡¡}tj}n(t d|¡}t |t|ƒ¡}|fdd„}t|tj 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. Nzchar[]cSs t |¡S)N)Ú_libÚBIO_free)ÚbioÚrefrBrBrCÚfreesz_new_mem_buf..free) rIÚBIO_newÚ BIO_s_memrJÚ_ffiÚnewZBIO_new_mem_bufÚlenÚ_openssl_assertÚNULLÚgc)ÚbufferrKrMÚdatarBrBrCÚ _new_mem_bufps    rXcCs.t d¡}t ||¡}t |d|¡dd…S)zO Copy the contents of an OpenSSL BIO object into a Python byte string. zchar**rN)rPrQrIZBIO_get_mem_datarV)rKÚ result_bufferZ buffer_lengthrBrBrCÚ_bio_to_stringŠs  rZcCs2t|tƒstdƒ‚t ||¡}|dkr.tdƒ‚dS)aô The the time value of an ASN1 time object. @param boundary: An ASN1_TIME pointer (or an object safely castable to that type) which will have its value set. @param when: A string representation of the desired time value. @raise TypeError: If C{when} is not a L{bytes} string. @raise ValueError: If C{when} does not represent a time in the required format. @raise RuntimeError: If the time value cannot be set for some other (unspecified) reason. zwhen must be a byte stringrzInvalid stringN)Ú isinstanceÚbytesÚ TypeErrorrIZASN1_TIME_set_stringÚ ValueError)ÚboundaryÚwhenÚ set_resultrBrBrCÚ_set_asn1_time“s   rbcCs¦t d|¡}t |¡dkrdSt |¡tjkr>t t |¡¡St d¡}t  ||¡|dtj krlt dƒn6t d|d¡}t |¡}t |¡}t  |d¡|SdS)a] Retrieve the time value of an ASN1 time object. @param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to that type) from which the time value will be retrieved. @return: The time value from C{timestamp} as a L{bytes} string in a certain format. Or C{None} if the object contains no time value. z ASN1_STRING*rNzASN1_GENERALIZEDTIME**ÚASN1_TIME_to_generalizedtime) rPÚcastrIÚASN1_STRING_lengthZASN1_STRING_typeZV_ASN1_GENERALIZEDTIMEÚstringÚASN1_STRING_datarQrcrTrHZASN1_GENERALIZEDTIME_free)Ú timestampZstring_timestampZgeneralized_timestampZ string_dataÚ string_resultrBrBrCÚ_get_asn1_time©s       rjc@s$eZdZdd„Zdd„Zdd„ZdS)Ú_X509NameInvalidatorcCs g|_dS)N)Ú_names)ÚselfrBrBrCÚ__init__Ósz_X509NameInvalidator.__init__cCs|j |¡dS)N)rlÚappend)rmÚnamerBrBrCÚaddÖsz_X509NameInvalidator.addcCsx|jD]}|`qWdS)N)rlÚ_name)rmrprBrBrCÚclearÙs z_X509NameInvalidator.clearN)r>r?r@rnrqrsrBrBrBrCrkÒsrkc@sTeZdZdZdZdZdd„Zdd„Zedd „ƒZ d d „Z d d „Z dd„Z dd„Z dS)r zD A class representing an DSA or RSA public key or key pair. FTcCs"t ¡}t |tj¡|_d|_dS)NF)rIZ EVP_PKEY_newrPrUÚ EVP_PKEY_freeÚ_pkeyÚ _initialized)rmÚpkeyrBrBrCrnçsz PKey.__init__cCs(tƒ}|jr| |j¡S| |j¡SdS)a Export as a ``cryptography`` key. :rtype: One of ``cryptography``'s `key interfaces`_. .. _key interfaces: https://cryptography.io/en/latest/hazmat/ primitives/asymmetric/rsa/#key-interfaces .. versionadded:: 16.1.0 N)rEÚ _only_publicZ_evp_pkey_to_public_keyruZ_evp_pkey_to_private_key)rmrDrBrBrCÚto_cryptography_keyìs  zPKey.to_cryptography_keycCsR|ƒ}t|tjtjtjtjfƒs(tdƒ‚|j|_ t|tjtjfƒrHd|_ d|_ |S)zø Construct based on a ``cryptography`` *crypto_key*. :param crypto_key: A ``cryptography`` key. :type crypto_key: One of ``cryptography``'s `key interfaces`_. :rtype: PKey .. versionadded:: 16.1.0 zUnsupported key typeT) r[rZ RSAPublicKeyZ RSAPrivateKeyrZ DSAPublicKeyZ DSAPrivateKeyr]Z _evp_pkeyrurxrv)ÚclsZ crypto_keyrwrBrBrCÚfrom_cryptography_keyýs  zPKey.from_cryptography_keyc Cs6t|tƒstdƒ‚t|tƒs$tdƒ‚|tkr¢|dkr= 0`` :raises TypeError: If :py:data:`type` or :py:data:`bits` isn't of the appropriate type. :raises ValueError: If the number of bits isn't an integer of the appropriate size. :return: ``None`` ztype must be an integerzbits must be an integerrzInvalid number of bitsézNo such key typeTN)r[Úintr]rr^rIÚBN_newrPrUÚBN_freeZ BN_set_wordZRSA_F4ZRSA_newZRSA_generate_key_exrTrSZEVP_PKEY_assign_RSArurZDSA_newZDSA_freeZDSA_generate_parameters_exZDSA_generate_keyZEVP_PKEY_set1_DSArrv)rmÚtypeÚbitsÚexponentrÚresultrÚresrBrBrCÚ generate_keys4     zPKey.generate_keycCsd|jrtdƒ‚t | ¡¡tjkr*tdƒ‚t |j¡}t  |tj ¡}t  |¡}|dkrZdSt ƒdS)ax Check the consistency of an RSA private key. This is the Python equivalent of OpenSSL's ``RSA_check_key``. :return: ``True`` if key is consistent. :raise OpenSSL.crypto.Error: if the key is inconsistent. :raise TypeError: if the key is of a type which cannot be checked. Only RSA keys can currently be checked. zpublic key onlyzkey type unsupportedr|TN) rxr]rIZ EVP_PKEY_typer€Ú EVP_PKEY_RSAÚEVP_PKEY_get1_RSArurPrUÚRSA_freeZ RSA_check_keyÚ_raise_current_error)rmrrƒrBrBrCÚcheckSs   z PKey.checkcCs t |j¡S)zT Returns the type of the key :return: The type of the key. )rIÚ EVP_PKEY_idru)rmrBrBrCr€msz PKey.typecCs t |j¡S)zh Returns the number of bits of the key :return: The number of bits of the key. )rIZ EVP_PKEY_bitsru)rmrBrBrCrusz PKey.bitsN)r>r?r@rArxrvrnryÚ classmethodr{r…rŠr€rrBrBrBrCr ßs 8csdeZdZdZdZes ‡fdd„Zedd„ƒZedd„ƒZ ed d „ƒZ d d „Z d d„Z dd„Z ‡ZS)Ú_EllipticCurveaZ A representation of a supported elliptic curve. @cvar _curves: :py:obj:`None` until an attempt is made to load the curves. Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve` instances each of which represents one curve supported by the system. @type _curves: :py:type:`NoneType` or :py:type:`set` Ncst|tƒrtt|ƒ |¡StS)zÇ Implement cooperation with the right-hand side argument of ``!=``. Python 3 seems to have dropped this cooperation in this very narrow circumstance. )r[rÚsuperrÚNotImplemented)rmÚother)Ú __class__rBrCrŒs z_EllipticCurve.__ne__cs>ˆ tjd¡}t d|¡}ˆ ||¡t‡‡fdd„|DƒƒS)zü Get the curves supported by OpenSSL. :param lib: The OpenSSL library binding object. :return: A :py:type:`set` of ``cls`` instances giving the names of the elliptic curves the underlying library supports. rzEC_builtin_curve[]c3s|]}ˆ ˆ|j¡VqdS)N)Úfrom_nidÚnid)Ú.0Úc)rzrrBrCú §sz7_EllipticCurve._load_elliptic_curves..)ZEC_get_builtin_curvesrPrTrQÚset)rzrZ num_curvesZbuiltin_curvesrB)rzrrCÚ_load_elliptic_curves—s   z$_EllipticCurve._load_elliptic_curvescCs|jdkr| |¡|_|jS)a Get, cache, and return the curves supported by OpenSSL. :param lib: The OpenSSL library binding object. :return: A :py:type:`set` of ``cls`` instances giving the names of the elliptic curves the underlying library supports. N)Ú_curvesr˜)rzrrBrBrCÚ_get_elliptic_curves©s  z#_EllipticCurve._get_elliptic_curvescCs|||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)rPrfÚ OBJ_nid2snÚdecode)rzrr“rBrBrCr’·sz_EllipticCurve.from_nidcCs||_||_||_dS)a‰ :param _lib: The :py:mod:`cryptography` binding instance used to interface with OpenSSL. :param _nid: The OpenSSL NID identifying the curve this object represents. :type _nid: :py:class:`int` :param name: The OpenSSL short name identifying the curve this object represents. :type name: :py:class:`unicode` N)rIÚ_nidrp)rmrr“rprBrBrCrnÈs z_EllipticCurve.__init__cCs d|jfS)Nz )rp)rmrBrBrCÚ__repr__Ùsz_EllipticCurve.__repr__cCs|j |j¡}t |tj¡S)zÅ Create a new OpenSSL EC_KEY structure initialized to use this curve. The structure is automatically garbage collected when the Python object is garbage collected. )rIZEC_KEY_new_by_curve_nameržrPrUZ EC_KEY_free)rmÚkeyrBrBrCÚ _to_EC_KEYÜsz_EllipticCurve._to_EC_KEY)r>r?r@rAr™Ú_PY2rrŒr˜ršr’rnrŸr¡Ú __classcell__rBrB)r‘rCr~s   rcCs 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. )rršrIrBrBrBrCr!çs cCs,xtƒD]}|j|kr|SqWtd|ƒ‚dS)aT Return a single curve object selected by name. See :py:func:`get_elliptic_curves` for information about curve objects. :param name: The OpenSSL short name identifying the curve object to retrieve. :type name: :py:class:`unicode` If the named curve is not supported then :py:class:`ValueError` is raised. zunknown curve nameN)r!rpr^)rpZcurverBrBrCr"ös  csŒeZdZdZdd„Z‡fdd„Z‡fdd„Zdd „ZeeƒZee ƒZ ee ƒZ ee ƒZ ee ƒZ ee ƒZ d d „Zd d „Zdd„Zdd„Z‡ZS)r#a An X.509 Distinguished Name. :ivar countryName: The country of the entity. :ivar C: Alias for :py:attr:`countryName`. :ivar stateOrProvinceName: The state or province of the entity. :ivar ST: Alias for :py:attr:`stateOrProvinceName`. :ivar localityName: The locality of the entity. :ivar L: Alias for :py:attr:`localityName`. :ivar organizationName: The organization name of the entity. :ivar O: Alias for :py:attr:`organizationName`. :ivar organizationalUnitName: The organizational unit of the entity. :ivar OU: Alias for :py:attr:`organizationalUnitName` :ivar commonName: The common name of the entity. :ivar CN: Alias for :py:attr:`commonName`. :ivar emailAddress: The e-mail address of the entity. cCs t |j¡}t |tj¡|_dS)zž Create a new X509Name, copying the given X509Name instance. :param name: The name to copy. :type name: :py:class:`X509Name` N)rIÚ X509_NAME_duprrrPrUÚX509_NAME_free)rmrprBrBrCrn!s zX509Name.__init__c s| d¡rtt|ƒ ||¡St|ƒtk rrIÚ OBJ_txt2nidÚ _byte_stringÚ NID_undefr‰rÚAttributeErrorÚrangeÚX509_NAME_entry_countrrÚX509_NAME_get_entryÚX509_NAME_ENTRY_get_objectÚ OBJ_obj2nidZX509_NAME_delete_entryZX509_NAME_ENTRY_freer[Ú _text_typeÚencodeZX509_NAME_add_entry_by_NIDZ MBSTRING_UTF8) rmrpÚvaluer“ÚiÚentZent_objZent_nidÚ add_result)r‘rBrCr©+s6         zX509Name.__setattr__c sÖt t|ƒ¡}|tjkrHy tƒWntk r6YnXtt|ƒ |¡St  |j |d¡}|dkrddSt  |j |¡}t  |¡}t  d¡}t ||¡}t|dkƒz"t  |d|¡dd… d¡}Wdt |d¡X|S)a  Find attribute. An X509Name object has the following attributes: countryName (alias C), stateOrProvince (alias ST), locality (alias L), organization (alias O), organizationalUnit (alias OU), commonName (alias CN) and more... r§Nzunsigned char**rzutf-8)rIr«r¬r­r‰rrŽr#Ú __getattr__ZX509_NAME_get_index_by_NIDrrr±ÚX509_NAME_ENTRY_get_datarPrQZASN1_STRING_to_UTF8rSrVrÚ OPENSSL_free) rmrpr“Z entry_indexÚentryrWrYZ data_lengthrƒ)r‘rBrCrºRs(       zX509Name.__getattr__cs‡fdd„}|S)Ncs(t|tƒstSt |j|j¡}ˆ|dƒS)Nr)r[r#rrIZ X509_NAME_cmprr)rmrrƒ)ÚoprBrCÚf{s zX509Name._cmp..frB)r¾r¿rB)r¾rCÚ_cmpzs z X509Name._cmpcCsBt dd¡}t |j|t|ƒ¡}t|tjkƒdtt  |¡ƒfS)z6 String representation of an X509Name zchar[]iz) rPrQrIZX509_NAME_onelinerrrRrSrTÚ_nativerf)rmrYZ format_resultrBrBrCrŸŒs  zX509Name.__repr__cCs 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` )rIZX509_NAME_hashrr)rmrBrBrCÚhashšs z X509Name.hashcCsNt d¡}t |j|¡}t|dkƒt |d|¡dd…}t |d¡|S)zŽ Return the DER encoding of this name. :return: The DER encoded form of this name. :rtype: :py:class:`bytes` zunsigned char**rN)rPrQrIZ i2d_X509_NAMErrrSrVr¼)rmrYZ encode_resultrirBrBrCÚder¦s   z X509Name.derc CsŒg}x‚tt |j¡ƒD]n}t |j|¡}t |¡}t |¡}t |¡}t |¡}t   t  |¡t  |¡¡dd…}|  t  |¡|f¡qW|S)z¼ Returns the components of this name, as a sequence of 2-tuples. :return: The components of this name. :rtype: :py:class:`list` of ``name, value`` tuples. N)r¯rIr°rrr±r²r»r³rœrPrVrgrerorf) rmrƒr·r¸ÚfnameZfvalr“rpr¶rBrBrCÚget_componentsµs    zX509Name.get_components)r>r?r@rArnr©rºrÀrrrrrr rŸrÂrÃrÅr£rBrB)r‘rCr#s ' (  c@sdeZdZdZddd„Zedd„ƒZejdej dej d iZ d d „Z d d „Z dd„Zdd„Zdd„ZdS)r$z, An X.509 v3 certificate extension. NcCs¶t d¡}t |tjtjtjtjd¡t |¡|dk rRt|tƒsJtdƒ‚|j |_ |dk rtt|tƒsltdƒ‚|j |_ |r€d|}t  tj|||¡}|tjkr¢t ƒt |tj¡|_dS)a° Initializes an X509 extension. :param type_name: The name of the type of extension_ to create. :type type_name: :py:data:`bytes` :param bool critical: A flag indicating whether this is a critical extension. :param value: The value of the extension. :type value: :py:data:`bytes` :param subject: Optional X509 certificate to use as subject. :type subject: :py:class:`X509` :param issuer: Optional X509 certificate to use as issuer. :type issuer: :py:class:`X509` .. _extension: https://www.openssl.org/docs/manmaster/man5/ x509v3_config.html#STANDARD-EXTENSIONS z X509V3_CTX*rNzissuer must be an X509 instancez subject must be an X509 instances critical,)rPrQrIZX509V3_set_ctxrTZX509V3_set_ctx_nodbr[r&r]Ú_x509Ú issuer_certZ subject_certZX509V3_EXT_nconfr‰rUÚX509_EXTENSION_freeÚ _extension)rmÚ type_nameÚcriticalr¶ÚsubjectÚissuerÚctxÚ extensionrBrBrCrnÕs"     zX509Extension.__init__cCst t |j¡¡S)N)rIr³ÚX509_EXTENSION_get_objectrÉ)rmrBrBrCržszX509Extension._nidÚemailÚDNSÚURIc CsÔt dt |j¡¡}t |tj¡}g}x¢tt |¡ƒD]}t  ||¡}y|j |j }Wn8t k rŽt ƒ}t ||¡| tt|ƒƒ¡Yq6Xtt |jjj|jjj¡dd…ƒ}| |d|¡q6Wd |¡S)NzGENERAL_NAMES*ú:z, )rPrdrIZX509V3_EXT_d2irÉrUZGENERAL_NAMES_freer¯Zsk_GENERAL_NAME_numZsk_GENERAL_NAME_valueÚ _prefixesr€ÚKeyErrorrXZGENERAL_NAME_printrorÁrZrVÚdZia5rWÚlengthÚjoin)rmÚnamesÚpartsr·rpÚlabelrKr¶rBrBrCÚ_subjectAltNameStrings   "z#X509Extension._subjectAltNameStringcCsDtj|jkr| ¡Stƒ}t ||jdd¡}t|dkƒtt |ƒƒS)zF :return: a nice text representation of the extension r) rIZNID_subject_alt_nameržrÝrXÚX509V3_EXT_printrÉrSrÁrZ)rmrKÚ print_resultrBrBrCÚ__str__4s   zX509Extension.__str__cCs t |j¡S)zk Returns the critical field of this X.509 extension. :return: The critical field. )rIZX509_EXTENSION_get_criticalrÉ)rmrBrBrCÚ get_criticalAszX509Extension.get_criticalcCs&t |j¡}t |¡}t t |¡¡S)zü Returns the short type name of this X.509 extension. The result is a byte string such as :py:const:`b"basicConstraints"`. :return: The short type name. :rtype: :py:data:`bytes` .. versionadded:: 0.12 )rIrÐrÉr³rPrfrœ)rmÚobjr“rBrBrCÚget_short_nameIs  zX509Extension.get_short_namecCs@t |j¡}t d|¡}t |¡}t |¡}t ||¡dd…S)zÍ Returns the data of the X509 extension, encoded as ASN.1. :return: The ASN.1 encoded data of this X509 extension. :rtype: :py:data:`bytes` .. versionadded:: 0.12 z ASN1_STRING*N)rIÚX509_EXTENSION_get_datarÉrPrdrgrerV)rmZ octet_resultriZ char_resultÚ result_lengthrBrBrCÚget_dataXs    zX509Extension.get_data)NN)r>r?r@rArnÚpropertyržrIZ GEN_EMAILZGEN_DNSZGEN_URIrÕrÝràrárãrærBrBrBrCr$Ðs =   c@steZdZdZdd„Zdd„Zedd„ƒZdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„ZdS)r%z0 An X.509 certificate signing requests. cCs&t ¡}t |tj¡|_| d¡dS)Nr)rIZ X509_REQ_newrPrUÚ X509_REQ_freeÚ_reqÚ set_version)rmÚreqrBrBrCrnmszX509Req.__init__cCsddlm}tƒ}|||jƒS)z® Export as a ``cryptography`` certificate signing request. :rtype: ``cryptography.x509.CertificateSigningRequest`` .. versionadded:: 17.1.0 r)Ú_CertificateSigningRequest)Ú)cryptography.hazmat.backends.openssl.x509rìrEré)rmrìrDrBrBrCÚto_cryptographyss zX509Req.to_cryptographycCs&t|tjƒstdƒ‚|ƒ}|j|_|S)a Construct based on a ``cryptography`` *crypto_req*. :param crypto_req: A ``cryptography`` X.509 certificate signing request :type crypto_req: ``cryptography.x509.CertificateSigningRequest`` :rtype: X509Req .. versionadded:: 17.1.0 z%Must be a certificate signing request)r[rZCertificateSigningRequestr]Z _x509_reqré)rzZ crypto_reqrërBrBrCÚfrom_cryptography‚s zX509Req.from_cryptographycCs t |j|j¡}t|dkƒdS)zµ Set the public key of the certificate signing request. :param pkey: The public key to use. :type pkey: :py:class:`PKey` :return: ``None`` r|N)rIZX509_REQ_set_pubkeyrérurS)rmrwrarBrBrCÚ set_pubkey•s zX509Req.set_pubkeycCsDt t¡}t |j¡|_t|jtjkƒt  |jtj ¡|_d|_ |S)z‹ Get the public key of the certificate signing request. :return: The public key. :rtype: :py:class:`PKey` T) r Ú__new__rIZX509_REQ_get_pubkeyrérurSrPrTrUrtrx)rmrwrBrBrCÚ get_pubkey¡s  zX509Req.get_pubkeycCst |j|¡}t|dkƒdS)zµ Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate request. :param int version: The version number. :return: ``None`` r|N)rIZX509_REQ_set_versionrérS)rmÚversionrarBrBrCrê¯szX509Req.set_versioncCs 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` )rIZX509_REQ_get_versionré)rmrBrBrCÚ get_versionºszX509Req.get_versioncCs2t t¡}t |j¡|_t|jtjkƒ||_ |S)aÝ Return the subject of this certificate signing request. This creates a new :class:`X509Name` that wraps the underlying subject name field on the certificate signing request. Modifying it will modify the underlying signing request, and will have the effect of modifying any other :class:`X509Name` that refers to this subject. :return: The subject of this certificate signing request. :rtype: :class:`X509Name` ) r#rñrIZX509_REQ_get_subject_namerérrrSrPrTÚ_owner)rmrprBrBrCÚ get_subjectÄs zX509Req.get_subjectcCspt ¡}t|tjkƒt |tj¡}x,|D]$}t|tƒs@t dƒ‚t  ||j ¡q*Wt  |j |¡}t|dkƒdS)z× Add extensions to the certificate signing request. :param extensions: The X.509 extensions to add. :type extensions: iterable of :py:class:`X509Extension` :return: ``None`` z+One of the elements is not an X509Extensionr|N)rIZsk_X509_EXTENSION_new_nullrSrPrTrUZsk_X509_EXTENSION_freer[r$r^Zsk_X509_EXTENSION_pushrÉZX509_REQ_add_extensionsré)rmÚ extensionsÚstackÚextr¹rBrBrCÚadd_extensionsÚs  zX509Req.add_extensionscCsrg}t |j¡}t |dd„¡}xLtt |¡ƒD]:}t t¡}t  t  ||¡¡}t |tj ¡|_ |  |¡q0W|S)zé Get X.509 extensions in the certificate signing request. :return: The X.509 extensions in this request. :rtype: :py:class:`list` of :py:class:`X509Extension` objects. .. versionadded:: 0.15 cSst |t tjd¡¡S)NrÈ)rIZsk_X509_EXTENSION_pop_freerPÚ addressofZ _original_lib)ÚxrBrBrCÚþsz(X509Req.get_extensions..)rIZX509_REQ_get_extensionsrérPrUr¯Zsk_X509_EXTENSION_numr$rñÚX509_EXTENSION_dupZsk_X509_EXTENSION_valuerÈrÉro)rmÚextsZnative_exts_objr·rùrÏrBrBrCÚget_extensionsñs    zX509Req.get_extensionscCs^|jrtdƒ‚|jstdƒ‚t t|ƒ¡}|tjkrr?r@rArnrîrŒrïrðròrêrôrörúrr8r9rBrBrBrCr%hs    c@seZdZdZdd„Zedd„ƒZdd„Zedd „ƒZd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Z d:d;„Z!dd?„Z#d@dA„Z$dBS)Cr&z An X.509 certificate. cCs:t ¡}t|tjkƒt |tj¡|_tƒ|_ tƒ|_ dS)N) rIZX509_newrSrPrTrUÚ X509_freerÆrkÚ_issuer_invalidatorÚ_subject_invalidator)rmrrBrBrCrn@s z X509.__init__cCs.| |¡}t |tj¡|_tƒ|_tƒ|_|S)N) rñrPrUrIrrÆrkrr)rzrÚcertrBrBrCÚ_from_raw_x509_ptrHs  zX509._from_raw_x509_ptrcCsddlm}tƒ}|||jƒS)z Export as a ``cryptography`` certificate. :rtype: ``cryptography.x509.Certificate`` .. versionadded:: 17.1.0 r)Ú _Certificate)rír rErÆ)rmr rDrBrBrCrîPs zX509.to_cryptographycCs&t|tjƒstdƒ‚|ƒ}|j|_|S)zü Construct based on a ``cryptography`` *crypto_cert*. :param crypto_key: A ``cryptography`` X.509 certificate. :type crypto_key: ``cryptography.x509.Certificate`` :rtype: X509 .. versionadded:: 17.1.0 zMust be a certificate)r[rZ Certificater]rÆ)rzZ crypto_certrrBrBrCrï]s zX509.from_cryptographycCs$t|tƒstdƒ‚t |j|¡dS)a  Set the version number of the certificate. Note that the version value is zero-based, eg. a value of 0 is V1. :param version: The version number of the certificate. :type version: :py:class:`int` :return: ``None`` zversion must be an integerN)r[r}r]rIZX509_set_versionrÆ)rmrórBrBrCrêps zX509.set_versioncCs t |j¡S)z˜ Return the version number of the certificate. :return: The version number of the certificate. :rtype: :py:class:`int` )rIZX509_get_versionrÆ)rmrBrBrCrôszX509.get_versioncCsFt t¡}t |j¡|_|jtjkr*tƒt  |jtj ¡|_d|_ |S)z{ Get the public key of the certificate. :return: The public key. :rtype: :py:class:`PKey` T) r rñrIÚX509_get_pubkeyrÆrurPrTr‰rUrtrx)rmrwrBrBrCròˆs  zX509.get_pubkeycCs2t|tƒstdƒ‚t |j|j¡}t|dkƒdS)z¥ Set the public key of the certificate. :param pkey: The public key. :type pkey: :py:class:`PKey` :return: :py:data:`None` zpkey must be a PKey instancer|N)r[r r]rIZX509_set_pubkeyrÆrurS)rmrwrarBrBrCrð—s zX509.set_pubkeycCspt|tƒstdƒ‚|jr tdƒ‚|js.tdƒ‚t t|ƒ¡}|t j krNtdƒ‚t  |j |j |¡}t|dkƒdS)a Sign the certificate with this key and digest type. :param pkey: The key to sign with. :type pkey: :py:class:`PKey` :param digest: The name of the message digest to use. :type digest: :py:class:`bytes` :return: :py:data:`None` zpkey must be a PKey instancezKey only has public partzKey is uninitializedzNo such digest methodrN)r[r r]rxr^rvrIrr¬rPrTZ X509_signrÆrurS)rmrwrZevp_mdrrBrBrCr8¦s  z X509.signcCs:t |j¡}t |j¡}|tjkr*tdƒ‚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 zUndefined signature algorithm) rIZX509_get0_tbs_sigalgrÆr³Ú algorithmr­r^rPrfZ OBJ_nid2ln)rmZalgorr“rBrBrCÚget_signature_algorithmÂs   zX509.get_signature_algorithmcCs„t t|ƒ¡}|tjkr tdƒ‚t dtj¡}t dd¡}t|ƒ|d<t  |j |||¡}t |dkƒd  dd„t  ||d¡Dƒ¡S) a7 Return the digest of the X509 object. :param digest_name: The name of the digest algorithm to use. :type digest_name: :py:class:`bytes` :return: The digest of the object, formatted as :py:const:`b":"`-delimited hex pairs. :rtype: :py:class:`bytes` zNo such digest methodzunsigned char[]zunsigned int[]r|ró:cSsg|]}t|ƒ ¡‘qSrB)rÚupper)r”ÚchrBrBrCú íszX509.digest..)rIrr¬rPrTr^rQZEVP_MAX_MD_SIZErRZ X509_digestrÆrSrÙrV)rmZ digest_namerrYråZ digest_resultrBrBrCrÓs     z X509.digestcCs t |j¡S)zƒ Return the hash of the X509 subject. :return: The hash of the subject. :rtype: :py:class:`bytes` )rIZX509_subject_name_hashrÆ)rmrBrBrCÚsubject_name_hashòszX509.subject_name_hashcCsÖt|tƒstdƒ‚t|ƒdd…}t|tƒs6| d¡}t d¡}t  ||¡}|dtj krzt  t  |j ¡|¡}|rÒtƒnXt |dtj ¡}t |d¡|tj krªtƒt |tj¡}t |j |¡}t|dkƒdS)z± Set the serial number of the certificate. :param serial: The new serial number. :type serial: :py:class:`int` :return: :py:data`None` zserial must be an integeréNr›zBIGNUM**rr|)r[Ú_integer_typesr]Úhexr\rµrPrQrIÚ BN_hex2bnrTZASN1_INTEGER_setÚX509_get_serialNumberrÆr‰ÚBN_to_ASN1_INTEGERrrUÚASN1_INTEGER_freeZX509_set_serialNumberrS)rmÚserialÚ hex_serialÚ bignum_serialZ small_serialraÚ asn1_serialrBrBrCÚset_serial_numberûs&      zX509.set_serial_numberc Cs`t |j¡}t |tj¡}z4t |¡}zt |¡}t|dƒ}|St  |¡XWdt  |¡XdS)zx Return the serial number of this certificate. :return: The serial number. :rtype: int éN) rIrrÆZASN1_INTEGER_to_BNrPrTZ BN_bn2hexrfr}r¼r)rmrrrZhexstring_serialrrBrBrCÚget_serial_number#s    zX509.get_serial_numbercCs.t|tƒstdƒ‚t |j¡}t ||¡dS)zÍ Adjust the time stamp on which the certificate stops being valid. :param int amount: The number of seconds by which to adjust the timestamp. :return: ``None`` zamount must be an integerN)r[r}r]rIÚX509_getm_notAfterrÆÚX509_gmtime_adj)rmÚamountZnotAfterrBrBrCÚgmtime_adj_notAfter7s  zX509.gmtime_adj_notAftercCs.t|tƒstdƒ‚t |j¡}t ||¡dS)z½ Adjust the timestamp on which the certificate starts being valid. :param amount: The number of seconds by which to adjust the timestamp. :return: ``None`` zamount must be an integerN)r[r}r]rIÚX509_getm_notBeforerÆr")rmr#Z notBeforerBrBrCÚgmtime_adj_notBeforeEs  zX509.gmtime_adj_notBeforecCs(t| ¡ƒ}tj |d¡}|tj ¡kS)z¡ Check whether the certificate has expired. :return: ``True`` if the certificate has expired, ``False`` otherwise. :rtype: bool z %Y%m%d%H%M%SZ)rÁÚ get_notAfterÚdatetimeÚstrptimeÚutcnow)rmZ time_stringZ not_afterrBrBrCÚ has_expiredRs zX509.has_expiredcCst||jƒƒS)N)rjrÆ)rmÚwhichrBrBrCÚ_get_boundary_time^szX509._get_boundary_timecCs | tj¡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-rIr%)rmrBrBrCÚ get_notBeforeas zX509.get_notBeforecCst||jƒ|ƒS)N)rbrÆ)rmr,r`rBrBrCÚ_set_boundary_timenszX509._set_boundary_timecCs| tj|¡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/rIr%)rmr`rBrBrCÚ set_notBeforeqs zX509.set_notBeforecCs | tj¡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 )r-rIr!)rmrBrBrCr'~s zX509.get_notAftercCs| tj|¡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`` )r/rIr!)rmr`rBrBrCÚ set_notAfter‹s zX509.set_notAftercCs0t t¡}||jƒ|_t|jtjkƒ||_|S)N)r#rñrÆrrrSrPrTrõ)rmr,rprBrBrCÚ _get_name˜s   zX509._get_namecCs0t|tƒstdƒ‚||j|jƒ}t|dkƒdS)Nzname must be an X509Namer|)r[r#r]rÆrrrS)rmr,rprarBrBrCÚ _set_name£s zX509._set_namecCs| tj¡}|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` )r2rIZX509_get_issuer_namerrq)rmrprBrBrCÚ get_issuer©s  zX509.get_issuercCs| tj|¡|j ¡dS)zŸ Set the issuer of this certificate. :param issuer: The issuer. :type issuer: :py:class:`X509Name` :return: ``None`` N)r3rIZX509_set_issuer_namerrs)rmrÍrBrBrCÚ set_issuer¹s zX509.set_issuercCs| tj¡}|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` )r2rIÚX509_get_subject_namerrq)rmrprBrBrCröÅs  zX509.get_subjectcCs| tj|¡|j ¡dS)z£ Set the subject of this certificate. :param subject: The subject. :type subject: :py:class:`X509Name` :return: ``None`` N)r3rIZX509_set_subject_namerrs)rmrÌrBrBrCÚ set_subjectÕs zX509.set_subjectcCs t |j¡S)z¯ Get the number of extensions on this certificate. :return: The number of extensions. :rtype: :py:class:`int` .. versionadded:: 0.12 )rIZX509_get_ext_countrÆ)rmrBrBrCÚget_extension_countás zX509.get_extension_countcCs@x:|D]2}t|tƒstdƒ‚t |j|jd¡}|stƒqWdS)zÍ Add extensions to the certificate. :param extensions: The extensions to add. :type extensions: An iterable of :py:class:`X509Extension` objects. :return: ``None`` z+One of the elements is not an X509Extensionr§N)r[r$r^rIZ X509_add_extrÆrÉr‰)rmr÷rùr¹rBrBrCrúìs   zX509.add_extensionscCsNt t¡}t |j|¡|_|jtjkr.tdƒ‚t  |j¡}t  |tj ¡|_|S)aÏ Get a specific extension of the certificate by index. Extensions on a certificate are kept in order. The index parameter selects which extension will be returned. :param int index: The index of the extension to retrieve. :return: The extension at the specified index. :rtype: :py:class:`X509Extension` :raises IndexError: If the extension index was out of bounds. .. versionadded:: 0.12 zextension index out of bounds) r$rñrIZ X509_get_extrÆrÉrPrTÚ IndexErrorrþrUrÈ)rmÚindexrùrÏrBrBrCÚ get_extensionüs   zX509.get_extensionN)%r>r?r@rArnrŒr rîrïrêrôròrðr8r rrrr r$r&r+r-r.r/r0r'r1r2r3r4r5rör7r8rúr;rBrBrBrCr&;sB    (          c@sReZdZdZejZejZej Z ej Z ej ZejZejZejZejZejZejZdS)r'a  Flags for X509 verification, used to change the behavior of :class:`X509Store`. See `OpenSSL Verification Flags`_ for details. .. _OpenSSL Verification Flags: https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_flags.html N)r>r?r@rArIZX509_V_FLAG_CRL_CHECKZ CRL_CHECKZX509_V_FLAG_CRL_CHECK_ALLZ CRL_CHECK_ALLZX509_V_FLAG_IGNORE_CRITICALZIGNORE_CRITICALZX509_V_FLAG_X509_STRICTZ X509_STRICTZX509_V_FLAG_ALLOW_PROXY_CERTSZALLOW_PROXY_CERTSZX509_V_FLAG_POLICY_CHECKZ POLICY_CHECKZX509_V_FLAG_EXPLICIT_POLICYZEXPLICIT_POLICYZX509_V_FLAG_INHIBIT_MAPZ INHIBIT_MAPZX509_V_FLAG_NOTIFY_POLICYZ NOTIFY_POLICYZX509_V_FLAG_CHECK_SS_SIGNATUREZCHECK_SS_SIGNATUREZX509_V_FLAG_CB_ISSUER_CHECKZCB_ISSUER_CHECKrBrBrBrCr's c@sBeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zdd 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`. cCst ¡}t |tj¡|_dS)N)rIZX509_STORE_newrPrUZX509_STORE_freeÚ_store)rmÚstorerBrBrCrn:szX509Store.__init__cCs0t|tƒstƒ‚t |j|j¡}t|dkƒdS)aÔ Adds a trusted certificate to this store. Adding a certificate with this method adds this certificate as a *trusted* certificate. :param X509 cert: The certificate to add to this store. :raises TypeError: If the certificate is not an :class:`X509`. :raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your certificate. :return: ``None`` if the certificate was added successfully. r|N)r[r&r]rIZX509_STORE_add_certr<rÆrS)rmrr„rBrBrCÚadd_cert>s zX509Store.add_certcCstt |j|j¡dkƒdS)a¿ Add a certificate revocation list to this store. The certificate revocation lists added to a store will only be used if the associated flags are configured to check certificate revocation lists. .. versionadded:: 16.1.0 :param CRL crl: The certificate revocation list to add to this store. :return: ``None`` if the certificate revocation list was added successfully. rN)rSrIZX509_STORE_add_crlr<Ú_crl)rmÚcrlrBrBrCÚadd_crlTszX509Store.add_crlcCstt |j|¡dkƒdS)aÒ Set verification flags to this store. Verification flags can be combined by oring them together. .. note:: Setting a verification flag sometimes requires clients to add additional information to the store, otherwise a suitable error will be raised. For example, in setting flags to enable CRL checking a suitable CRL must be added to the store otherwise an error will be raised. .. versionadded:: 16.1.0 :param int flags: The verification flags to set on this store. See :class:`X509StoreFlags` for available constants. :return: ``None`` if the verification flags were successfully set. rN)rSrIZX509_STORE_set_flagsr<)rmÚflagsrBrBrCÚ set_flagsdszX509Store.set_flagscCsFt ¡}t |tj¡}t |t | ¡¡¡t t  |j |¡dkƒdS)a¤ Set the time against which the certificates are verified. Normally the current time is used. .. note:: For example, you can determine if a certificate was valid at a given time. .. versionadded:: 17.0.0 :param datetime vfy_time: The verification time to set on this store. :return: ``None`` if the verification time was successfully set. rN) rIZX509_VERIFY_PARAM_newrPrUZX509_VERIFY_PARAM_freeZX509_VERIFY_PARAM_set_timeÚcalendarÚtimegmÚ timetuplerSZX509_STORE_set1_paramr<)rmZvfy_timeÚparamrBrBrCÚset_time|s zX509Store.set_timeNcCsN|dkrtj}nt|ƒ}|dkr(tj}nt|ƒ}t |j||¡}|sJtƒdS)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)rPrTÚ _path_stringrIZX509_STORE_load_locationsr<r‰)rmÚcafileÚcapathZ load_resultrBrBrCÚload_locations”s zX509Store.load_locations)N) r>r?r@rArnr>rArCrHrLrBrBrBrCr(,s cs eZdZdZ‡fdd„Z‡ZS)r)zù An exception raised when an error occurred while verifying a certificate using `OpenSSL.X509StoreContext.verify_certificate`. :ivar certificate: The certificate which caused verificate failure. :type certificate: :class:`X509` cstt|ƒ |¡||_dS)N)rŽr)rnÚ certificate)rmÚmessagerM)r‘rBrCrnÍszX509StoreContextError.__init__)r>r?r@rArnr£rBrB)r‘rCr)Äsc@sVeZdZdZddd„Zedd„ƒZdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dS)r*a™ An X.509 store context. An X.509 store context is used to carry out the actual verification process of a certificate in a described context. For describing such a context, see :class:`X509Store`. :ivar _store_ctx: The underlying X509_STORE_CTX structure used by this instance. It is dynamically allocated and automatically garbage collected. :ivar _store: See the ``store`` ``__init__`` parameter. :ivar _cert: See the ``certificate`` ``__init__`` parameter. :ivar _chain: See the ``chain`` ``__init__`` parameter. :param X509Store store: The certificates which will be trusted for the purposes of any verifications. :param X509 certificate: The certificate to be verified. :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` NcCs<t ¡}t |tj¡|_||_||_| |¡|_ |  ¡dS)N) rIZX509_STORE_CTX_newrPrUZX509_STORE_CTX_freeÚ _store_ctxr<Ú_certÚ_build_certificate_stackÚ_chainÚ_init)rmr=rMÚchainZ store_ctxrBrBrCrnès  zX509StoreContext.__init__cCs dd„}|dkst|ƒdkr"tjSt ¡}t|tjkƒt ||¡}xV|D]N}t|tƒs`t dƒ‚tt  |j ¡dkƒt  ||j ¡dkrJt  |j ¡tƒqJW|S)NcSs<x,tt |¡ƒD]}t ||¡}t |¡qWt |¡dS)N)r¯rIÚ sk_X509_numÚ sk_X509_valuerÚ sk_X509_free)Úsr·rürBrBrCÚcleanupõs z:X509StoreContext._build_certificate_stack..cleanuprz+One of the elements is not an X509 instance)rRrPrTrIÚsk_X509_new_nullrSrUr[r&r]Z X509_up_refrÆÚ sk_X509_pushrr‰)Z certificatesrYrørrBrBrCrQós     z)X509StoreContext._build_certificate_stackcCs.t |j|jj|jj|j¡}|dkr*tƒdS)zÀ Set up the store context for a subsequent verification operation. Calling this method more than once without first calling :meth:`_cleanup` will leak memory. rN)rIZX509_STORE_CTX_initrOr<rPrÆrRr‰)rmÚretrBrBrCrSszX509StoreContext._initcCst |j¡dS)z‰ Internally cleans up the store context. The store context can then be reused with a new call to :meth:`_init`. N)rIZX509_STORE_CTX_cleanuprO)rmrBrBrCÚ_cleanupszX509StoreContext._cleanupc Cs\t |j¡t |j¡tt t t |j¡¡¡ƒg}t |j¡}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. ) rIZX509_STORE_CTX_get_errorrOZX509_STORE_CTX_get_error_depthrÁrPrfZX509_verify_cert_error_stringZX509_STORE_CTX_get_current_certZX509_dupr&r r))rmÚerrorsrÆrPÚpycertrBrBrCÚ_exception_from_context$s     z(X509StoreContext._exception_from_contextcCs ||_dS)zÖ Set the context's X.509 store. .. versionadded:: 0.15 :param X509Store store: The store description which will be used for the purposes of any *future* verifications. N)r<)rmr=rBrBrCÚ set_store>s zX509StoreContext.set_storecCs8| ¡| ¡t |j¡}| ¡|dkr4| ¡‚dS)a" Verify a certificate in a context. .. versionadded:: 0.15 :raises X509StoreContextError: If an error occurred when validating a certificate in the context. Sets ``certificate`` attribute to indicate which certificate caused the error. rN)r]rSrIÚX509_verify_certrOr`)rmr\rBrBrCÚverify_certificateIs  z#X509StoreContext.verify_certificatecCs®| ¡| ¡t |j¡}|dkr4| ¡| ¡‚t |j¡}t|tj kƒg}xDt t  |¡ƒD]2}t  ||¡}t|tj kƒt  |¡}| |¡qbWt |¡| ¡|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]rSrIrbrOr`ZX509_STORE_CTX_get1_chainrSrPrTr¯rUrVr&r rorW)rmr\Z cert_stackrƒr·rr_rBrBrCÚget_verified_chain_s"     z#X509StoreContext.get_verified_chain)N) r>r?r@rArnÚ staticmethodrQrSr]r`rarcrdrBrBrBrCr*Òs   cCsvt|tƒr| d¡}t|ƒ}|tkr.rr|)rwr[r\r]rzÚreplaceÚ _crl_reasonsr:rIZASN1_ENUMERATED_newrSrPrTrUZASN1_ENUMERATED_freeZASN1_ENUMERATED_setZX509_REVOKED_add1_ext_i2drorv)rmÚreasonZ reason_codeZnew_reason_extrar¹rBrBrCÚ set_reasonds    zRevoked.set_reasoncCs†x€tt |j¡ƒD]l}t |j|¡}t |¡}t |¡tjkrtƒ}t  ||dd¡}|svt  |t  |¡¡}t |dkƒt |ƒSqWdS)a  Get the reason of this revocation. :return: The reason, or ``None`` if there is none. :rtype: bytes or NoneType .. seealso:: :meth:`all_reasons`, which gives you a list of all supported reasons this method might return. rN)r¯rIrtrorurÐr³rvrXrÞZM_ASN1_OCTET_STRING_printrärSrZ)rmr·rùrârKrßrBrBrCÚ get_reason‰s   zRevoked.get_reasoncCs|jdd…S)a Return a list of all the supported reason strings. This list is a copy; modifying it does not change the supported reason strings. :return: A list of reason strings. :rtype: :class:`list` of :class:`bytes` N)r})rmrBrBrCÚ all_reasons¤s zRevoked.all_reasonscCst |j¡}t||ƒS)z¡ Set the revocation timestamp. :param bytes when: The timestamp of the revocation, as ASN.1 TIME. :return: ``None`` )rIÚ X509_REVOKED_get0_revocationDaterorb)rmr`ÚdtrBrBrCÚ set_rev_date°s zRevoked.set_rev_datecCst |j¡}t|ƒS)z‡ Get the revocation timestamp. :return: The timestamp of the revocation, as ASN.1 TIME. :rtype: bytes )rIr‚rorj)rmrƒrBrBrCÚ get_rev_date»s zRevoked.get_rev_dateN)r>r?r@rAr}rnrrrsrwrr€rr„r…rBrBrBrCr/s" %  c@s|eZdZdZdd„Zdd„Zedd„ƒZdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Zedefdd„ZdS)r0z( A certificate revocation list. cCst ¡}t |tj¡|_dS)N)rIZ X509_CRL_newrPrUÚ X509_CRL_freer?)rmr@rBrBrCrnËsz CRL.__init__cCsddlm}tƒ}|||jƒS)z– Export as a ``cryptography`` CRL. :rtype: ``cryptography.x509.CertificateRevocationList`` .. versionadded:: 17.1.0 r)Ú_CertificateRevocationList)rír‡rEr?)rmr‡rDrBrBrCrîÏs zCRL.to_cryptographycCs&t|tjƒstdƒ‚|ƒ}|j|_|S)a Construct based on a ``cryptography`` *crypto_crl*. :param crypto_crl: A ``cryptography`` certificate revocation list :type crypto_crl: ``cryptography.x509.CertificateRevocationList`` :rtype: CRL .. versionadded:: 17.1.0 z%Must be a certificate revocation list)r[rZCertificateRevocationListr]Z _x509_crlr?)rzZ crypto_crlr@rBrBrCrïÞs zCRL.from_cryptographycCsrg}t |j¡}xPtt |¡ƒD]>}t ||¡}t |¡}t t¡}t   |tj ¡|_ |  |¡q W|rnt|ƒSdS)aA Return the revocations in this certificate revocation list. These revocations will be provided by value, not by reference. That means it's okay to mutate them: it won't affect this CRL. :return: The revocations in this CRL. :rtype: :class:`tuple` of :class:`Revocation` N)rIZX509_CRL_get_REVOKEDr?r¯Zsk_X509_REVOKED_numZsk_X509_REVOKED_valueÚCryptography_X509_REVOKED_dupr/rñrPrUrnroroÚtuple)rmÚresultsZ revoked_stackr·rpZ revoked_copyZpyrevrBrBrCÚ get_revokedñs     zCRL.get_revokedcCs8t |j¡}t|tjkƒt |j|¡}t|dkƒdS)a9 Add a revoked (by value not reference) to the CRL structure This revocation will be added by value, not by reference. That means it's okay to mutate it after adding: it won't affect this CRL. :param Revoked revoked: The new revocation. :return: ``None`` rN)rIrˆrorSrPrTZX509_CRL_add0_revokedr?)rmrpÚcopyr¹rBrBrCÚ add_revoked s zCRL.add_revokedcCsBt t |j¡¡}t|tjkƒt |tj¡}t   t ¡}||_ |S)zc Get the CRL's issuer. .. versionadded:: 16.1.0 :rtype: X509Name ) rIr¤ZX509_CRL_get_issuerr?rSrPrTrUr¥r#rñrr)rmZ_issuerrÍrBrBrCr4 s  zCRL.get_issuercCstt |j|¡dkƒdS)z— Set the CRL version. .. versionadded:: 16.1.0 :param int version: The version of the CRL. :return: ``None`` rN)rSrIZX509_CRL_set_versionr?)rmrórBrBrCrê& s zCRL.set_versioncCst||jƒ|ƒS)N)rbr?)rmr,r`rBrBrCr/1 szCRL._set_boundary_timecCs| tj|¡S)zô Set when the CRL was last updated. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ .. versionadded:: 16.1.0 :param bytes when: A timestamp string. :return: ``None`` )r/rIZX509_CRL_get_lastUpdate)rmr`rBrBrCÚset_lastUpdate4 s zCRL.set_lastUpdatecCs| tj|¡S)zø Set when the CRL will next be updated. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ .. versionadded:: 16.1.0 :param bytes when: A timestamp string. :return: ``None`` )r/rIZX509_CRL_get_nextUpdate)rmr`rBrBrCÚset_nextUpdateC s zCRL.set_nextUpdatecCs\t |¡}t|tjkƒt |jt |j¡¡t  |j¡t  |j|j |¡}t|dkƒdS)a; Sign the CRL. Signing a CRL enables clients to associate the CRL itself with an issuer. Before a CRL is meaningful to other OpenSSL functions, it must be signed by an issuer. This method implicitly sets the issuer's name based on the issuer certificate and private key used to sign the CRL. .. versionadded:: 16.1.0 :param X509 issuer_cert: The issuer's certificate. :param PKey issuer_key: The issuer's private key. :param bytes digest: The digest method to sign the CRL with. rN) rIrrSrPrTÚX509_CRL_set_issuer_namer?r6rÆZ X509_CRL_sortÚ X509_CRL_signru)rmrÇZ issuer_keyrrrƒrBrBrCr8R s  zCRL.signédc Cst|tƒstdƒ‚t|tƒs$tdƒ‚t|tƒs6tdƒ‚|tkrFtdƒ‚t |¡}|tj krbt dƒ‚t  t  ¡¡}t |tj kƒt ¡}t |tj kƒt |d¡t |j|¡t ||ddd¡t |j|¡t |jt |j¡¡t |j|j|¡} | stƒt||ƒS) aó Export the CRL as a string. :param X509 cert: The certificate used to sign the CRL. :param PKey key: The key used to sign the CRL. :param int type: The export format, either :data:`FILETYPE_PEM`, :data:`FILETYPE_ASN1`, or :data:`FILETYPE_TEXT`. :param int days: The number of days until the next update of this CRL. :param bytes digest: The name of the message digest to use (eg ``b"sha256"``). :rtype: bytes zcert must be an X509 instancezkey must be a PKey instanceztype must be an integerzdigest must be providedzNo such digest methodréé<)r[r&r]r r}Ú _UNSPECIFIEDrIrrPrTr^rNrOrSZ ASN1_TIME_newr"ZX509_CRL_set_lastUpdater?ZX509_CRL_set_nextUpdaterr6rÆr‘rur‰r:) rmrr r€ÚdaysrrrKZsometimerrBrBrCÚexportl s2      z CRL.exportN)r>r?r@rArnrîrŒrïr‹rr4rêr/rŽrr8rr•r—rBrBrBrCr0Æs  c@s4eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd S) r1cCstt |j¡ƒS)zm Check if this NID_pkcs7_signed object :return: True if the PKCS7 is of type signed )ÚboolrIZPKCS7_type_is_signedÚ_pkcs7)rmrBrBrCÚtype_is_signed¤ szPKCS7.type_is_signedcCstt |j¡ƒS)zt Check if this NID_pkcs7_enveloped object :returns: True if the PKCS7 is of type enveloped )r˜rIZPKCS7_type_is_envelopedr™)rmrBrBrCÚtype_is_enveloped¬ szPKCS7.type_is_envelopedcCstt |j¡ƒS)z† Check if this NID_pkcs7_signedAndEnveloped object :returns: True if the PKCS7 is of type signedAndEnveloped )r˜rIZ PKCS7_type_is_signedAndEnvelopedr™)rmrBrBrCÚtype_is_signedAndEnveloped´ sz PKCS7.type_is_signedAndEnvelopedcCstt |j¡ƒS)zi Check if this NID_pkcs7_data object :return: True if the PKCS7 is of type data )r˜rIZPKCS7_type_is_datar™)rmrBrBrCÚ type_is_data¼ szPKCS7.type_is_datacCs"t |jj¡}t |¡}t |¡S)zk Returns the type name of the PKCS7 structure :return: A string with the typename )rIr³r™r€rœrPrf)rmr“Z string_typerBrBrCÚ get_type_nameÄ s zPKCS7.get_type_nameN)r>r?r@ršr›rœrržrBrBrBrCr1£ s c@sbeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z ddd„Z dS)r2z A PKCS #12 archive. cCsd|_d|_d|_d|_dS)N)rurPÚ_cacertsÚ _friendlyname)rmrBrBrCrnÔ szPKCS12.__init__cCs|jS)z¾ Get the certificate in the PKCS #12 structure. :return: The certificate, or :py:const:`None` if there is none. :rtype: :py:class:`X509` or :py:const:`None` )rP)rmrBrBrCÚget_certificateÚ szPKCS12.get_certificatecCst|tƒstdƒ‚||_dS)zà Set the certificate in the PKCS #12 structure. :param cert: The new certificate, or :py:const:`None` to unset it. :type cert: :py:class:`X509` or :py:const:`None` :return: ``None`` zcert must be an X509 instanceN)r[r&r]rP)rmrrBrBrCÚset_certificateã s zPKCS12.set_certificatecCs|jS)zª Get the private key in the PKCS #12 structure. :return: The private key, or :py:const:`None` if there is none. :rtype: :py:class:`PKey` )ru)rmrBrBrCÚget_privatekeyð szPKCS12.get_privatekeycCst|tƒstdƒ‚||_dS)zè Set the certificate portion of the PKCS #12 structure. :param pkey: The new private key, or :py:const:`None` to unset it. :type pkey: :py:class:`PKey` or :py:const:`None` :return: ``None`` zpkey must be a PKey instanceN)r[r r]ru)rmrwrBrBrCÚset_privatekeyù s zPKCS12.set_privatekeycCs|jdk rt|jƒSdS)a Get the CA certificates in the PKCS #12 structure. :return: A tuple with the CA certificates in the chain, or :py:const:`None` if there are none. :rtype: :py:class:`tuple` of :py:class:`X509` or :py:const:`None` N)rŸr‰)rmrBrBrCÚget_ca_certificates s zPKCS12.get_ca_certificatescCsB|dkrd|_n.t|ƒ}x|D]}t|tƒstdƒ‚qW||_dS)a Replace or set the CA certificates within the PKCS12 object. :param cacerts: The new CA certificates, or :py:const:`None` to unset them. :type cacerts: An iterable of :py:class:`X509` or :py:const:`None` :return: ``None`` Nz)iterable must only contain X509 instances)rŸÚlistr[r&r])rmÚcacertsrrBrBrCÚset_ca_certificates s    zPKCS12.set_ca_certificatescCs2|dkrd|_nt|tƒs(td|fƒ‚||_dS)zâ Set the friendly name in the PKCS #12 structure. :param name: The new friendly name, or :py:const:`None` to unset. :type name: :py:class:`bytes` or :py:const:`None` :return: ``None`` Nz+name must be a byte string or None (not %r))r r[r\r])rmrprBrBrCÚset_friendlyname& s   zPKCS12.set_friendlynamecCs|jS)zÅ Get the friendly name in the PKCS# 12 structure. :returns: The friendly name, or :py:const:`None` if there is none. :rtype: :py:class:`bytes` or :py:const:`None` )r )rmrBrBrCÚget_friendlyname7 szPKCS12.get_friendlynameNér|c Csþtd|ƒ}|jdkrtj}n4t ¡}t |tj¡}x|jD]}t ||j ¡q:W|dkr^tj}|j }|dkrrtj}|j dkr„tj}n|j j }|j dkržtj}n|j j }t  |||||tjtj||d¡ }|tjkrÖtƒt |tj¡}tƒ} t | |¡t| ƒS)a€ Dump a PKCS12 object as a string. For more information, see the :c:func:`PKCS12_create` man page. :param passphrase: The passphrase used to encrypt the structure. Unlike some other passphrase arguments, this *must* be a string, not a callback. :type passphrase: :py:data:`bytes` :param iter: Number of times to repeat the encryption step. :type iter: :py:data:`int` :param maciter: Number of times to repeat the MAC step. :type maciter: :py:data:`int` :return: The string representation of the PKCS #12 structure. :rtype: rlNr)Ú_text_to_bytes_and_warnrŸrPrTrIrZrUrWr[rÆr rurPZ PKCS12_createZ&NID_pbe_WithSHA1And3_Key_TripleDES_CBCr‰Ú PKCS12_freerXZi2d_PKCS12_biorZ) rmrlÚiterZmaciterr§rÚ friendlynamerwÚpkcs12rKrBrBrCr—@ sF       z PKCS12.export)Nr«r|)r>r?r@rArnr¡r¢r£r¤r¥r¨r©rªr—rBrBrBrCr2Ï s      c@s@eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)r3z! A Netscape SPKI object. cCst ¡}t |tj¡|_dS)N)rIZNETSCAPE_SPKI_newrPrUZNETSCAPE_SPKI_freeÚ_spki)rmZspkirBrBrCrn‰ szNetscapeSPKI.__init__cCs^|jrtdƒ‚|jstdƒ‚t t|ƒ¡}|tjkrr?r@rArnr8r9r³ròrðrBrBrBrCr3„ s c@sBeZdZd dd„Zedd„ƒZedd„ƒZefdd „Zd d „Z d S)rgFcCs4|tkr|dk rtdƒ‚||_||_||_g|_dS)Nz0only FILETYPE_PEM key format supports encryption)rr^Ú _passphraseÚ _more_argsÚ _truncateÚ _problems)rmr€rlZ more_argsÚtruncaterBrBrCrnß sz_PassphraseHelper.__init__cCs@|jdkrtjSt|jtƒs&t|jƒr4t d|j¡Stdƒ‚dS)NZpem_password_cbz2Last argument must be a byte string or a callable.) r´rPrTr[r\ÚcallablerhÚ_read_passphraser])rmrBrBrCrhé s  z_PassphraseHelper.callbackcCs8|jdkrtjSt|jtƒs&t|jƒr,tjStdƒ‚dS)Nz2Last argument must be a byte string or a callable.)r´rPrTr[r\r¹r])rmrBrBrCriô s  z_PassphraseHelper.callback_argscCs8|jr4y t|ƒWn|k r&YnX|j d¡‚dS)Nr)r·Ú_exception_from_error_queueÚpop)rmZ exceptionTyperBrBrCrjÿ s  z"_PassphraseHelper.raise_if_problemc CsÒyžt|jƒr.|jr"| |||¡}q4| |¡}n|j}t|tƒsFtdƒ‚t|ƒ|krn|jrf|d|…}ntdƒ‚x(tt|ƒƒD]}|||d…||<q|Wt|ƒSt k rÌ}z|j   |¡dSd}~XYnXdS)NzBytes expectedz+passphrase returned by callback is too longr|r) r¹r´rµr[r\r^rRr¶r¯Ú Exceptionr·ro)rmÚbufÚsizeZrwflagZuserdatarƒr·ÚerBrBrCrº s&     z"_PassphraseHelper._read_passphraseN)FF) r>r?r@rnrçrhrirrjrºrBrBrBrCrgÞ s  rgcCst|tƒr| d¡}t|ƒ}|tkrÚDeprecationWarningr=ZOpenSSL_add_all_algorithmsZSSL_load_error_stringsZ ASN1_STRING_set_default_mask_ascrBrBrBrCÚsì   ,     )  iIT\6 @.^,6ZF" %#$ W