a oe{@s UddlZddlZddlZddlZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZddlmZmZddlmZmZmZmZmZddlmZdd lm Z!dd lm"Z#dd lm$Z%dd lm&Z'dd lm(Z)ddlm*Z+ddlm,Z-gdZ.eej/ej0ej1ej2fZ3ee4e5efZ6ee5e de5ffZ7e'j8Z9e:e;d<e'je'j?Z@e:e;d<e'jAZBe:e;d<e'jCZDe:e;d<e'jEZFe:e;d<GdddeGZHee#eHZIe)eHZJe4e dddZKd{ee5e ddd ZLe e5d!d"d#ZMe e5dd$d%d&ZNe5e d'd(d)ZOe ee5d*d+d,ZPGd-d.d.ZQGd/d0d0ZRGd1d2d2ZSed2d3d4d5ZTe4eSd6d7d8ZUejVGd9d:d:ZWGd;d<d<ZXeXZYejZeXe[d=e\dGd?d@d@Z]GdAdBdBZ^GdCdDdDZ_GdEdFdFZ`GdGdHdHeGZaGdIdJdJZbe:e5e^dKdLdMZce:e^e5dNdOdPZde:eRe5dQdRdSZed|e:eRee4ee7e5dTdUdVZfGdWdXdXZgegZhejZege[dYe\dXd>GdZd[d[ZieiZjejZeie[dYe\d[d>Gd\d]d]ZkejZeke[d^e\d]d>Gd_d`d`ZlejZele[dae\d`d>GdbdcdcZme:ee4e5feRdKdddeZnd}e:ee4e5fee7eRdfdgdhZoe:e]e5didjdkZpe:e5e]dKdldmZqeRee4e5fe4e5dndodpZrejZere[dqe\dpd>e^e5ee4e5fe4ddrdsdtZsejZese[due\dtd>e:eje5dvdwdxZtetZuejZete[dYe\dxd>e:ee4e5fejdKdydzZvevZwejZeve[dYe\dzd>dS)~N) b16encode)partial)PathLike) AnyCallableIterableListNoReturnOptionalSequenceSetTupleTypeUnion)utilsx509)dsaeced448ed25519rsa) UNSPECIFIED) byte_string)exception_from_error_queue)ffi)lib) make_assert) path_bytes)text_to_bytes_and_warn)! FILETYPE_PEM FILETYPE_ASN1 FILETYPE_TEXTTYPE_RSATYPE_DSAErrorPKeyget_elliptic_curvesget_elliptic_curveX509Name X509ExtensionX509ReqX509X509StoreFlags X509StoreX509StoreContextErrorX509StoreContextload_certificatedump_certificatedump_publickeydump_privatekeyRevokedCRLPKCS12 NetscapeSPKIload_publickeyload_privatekeydump_certificate_requestload_certificate_requestsignverifydump_crlload_crl.rr ir"r#TYPE_DHTYPE_ECc@seZdZdZdS)r$z7 An error occurred in an `OpenSSL.crypto` API. N)__name__ __module__ __qualname____doc__rFrF/C:\Program Files\Certbot\pkgs\OpenSSL\crypto.pyr$psr$)wherereturncCstd|ddS)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 z failureN) RuntimeError)rHrFrFrG_untested_errorzsrK)bufferrIcCsn|durtt}tj}n2td|}t|t|}|ftttddd}t |tj kt ||}|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. Nchar[])biorefrIcSs t|SN)_libBIO_free)rNrOrFrFrGfreesz_new_mem_buf..free) rQZBIO_newZ BIO_s_memrR_ffinewZBIO_new_mem_buflenr_openssl_assertNULLgc)rLrNrSdatarFrFrG _new_mem_bufs   r[)rNrIcCs.td}t||}t|d|ddS)zO Copy the contents of an OpenSSL BIO object into a Python byte string. zchar**rN)rTrUrQZBIO_get_mem_datarL)rN result_bufferZ buffer_lengthrFrFrG_bio_to_strings  r])boundarywhenrIcCs@t|tstdt|tjkt||}|dkrtt|Std}t |||dtj krlt dn6td|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. ASN1_STRING*rNzASN1_GENERALIZEDTIME**ASN1_TIME_to_generalizedtime) rTcastrQASN1_STRING_lengthZASN1_STRING_typeZV_ASN1_GENERALIZEDTIMEstringASN1_STRING_get0_datarUrmrXrKZASN1_GENERALIZEDTIME_free)rkZstring_timestampZgeneralized_timestampZ string_data string_resultrFrFrG_get_asn1_times"      rsc@s8eZdZddddZdddddZddd d ZdS) _X509NameInvalidatorNrIcCs g|_dSrP)_namesselfrFrFrG__init__sz_X509NameInvalidator.__init__r(namerIcCs|j|dSrP)rvappendrxr{rFrFrGaddsz_X509NameInvalidator.addcCs|jD]}|`qdSrP)rv_namer}rFrFrGclears z_X509NameInvalidator.clear)rBrCrDryr~rrFrFrFrGrtsrtc@seZdZdZdZdZddddZeddd Ze edd d d Z e e dd ddZ e dddZe dddZe dddZdS)r%zD A class representing an DSA or RSA public key or key pair. FTNrucCs"t}t|tj|_d|_dS)NF)rQZ EVP_PKEY_newrTrY EVP_PKEY_free_pkey _initializedrxpkeyrFrFrGrysz PKey.__init__cCs@ddlm}m}|jr(tt|}||Stt|}||dSdS)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_keyload_der_public_keyN),cryptography.hazmat.primitives.serializationrr _only_publicr2r r3)rxrrderrFrFrGto_cryptography_keys   zPKey.to_cryptography_key) crypto_keyrIc Cst|tjtjtjtjtjt j t j fs.t dddlm}m}m}m}t|tjtjfrntt||j|jS||j|j|}tt|SdS)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 typer)Encoding NoEncryption PrivateFormat PublicFormatN)r`r RSAPublicKey RSAPrivateKeyr DSAPublicKey DSAPrivateKeyrZEllipticCurvePrivateKeyrZEd25519PrivateKeyrZEd448PrivateKeyrbrrrrrr8r public_bytesDERZSubjectPublicKeyInfoZ private_bytesZPKCS8r9)clsrrrrrrrFrFrGfrom_cryptography_key,s0   zPKey.from_cryptography_key)typebitsrIc Cs6t|tstdt|ts$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`` type must be an integerzbits must be an integerrzInvalid number of bitszNo such key typeTN)r`intrbr"rcrQBN_newrTrYBN_freeZ BN_set_wordZRSA_F4ZRSA_newZRSA_generate_key_exrXrWZEVP_PKEY_assign_RSArr#ZDSA_newZDSA_freeZDSA_generate_parameters_exZDSA_generate_keyZEVP_PKEY_set1_DSAr$r)rxrrZexponentrresultrresrFrFrG generate_keyZs6     zPKey.generate_keycCsd|jrtdt|tjkr*tdt|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 onlyz'Only RSA keys can currently be checked.rTN) rrbrQZ EVP_PKEY_typer EVP_PKEY_RSAEVP_PKEY_get1_RSArrTrYRSA_freeZ RSA_check_key_raise_current_error)rxrrrFrFrGchecks   z PKey.checkcCs t|jS)zT Returns the type of the key :return: The type of the key. )rQ EVP_PKEY_idrrwrFrFrGrsz PKey.typecCs t|jS)zh Returns the number of bits of the key :return: The number of bits of the key. )rQZ EVP_PKEY_bitsrrwrFrFrGrsz PKey.bits)rBrCrDrErrry_Keyr classmethodrrrboolrrrrFrFrFrGr%s-8r%cseZdZdZdZeedfdd Zeee ddddZ eee ddd d Z eee dd d d Z ee eddddZedddZedddZZS)_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` NotherrIcst|trt|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`rsuper__ne__NotImplementedrxr __class__rFrGrs  z_EllipticCurve.__ne__)rrIcs>tjd}td|}||tfdd|DS)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|]}|jVqdSrP)from_nidnid).0crrrFrG z7_EllipticCurve._load_elliptic_curves..)ZEC_get_builtin_curvesrTrXrUset)rrZ num_curvesZbuiltin_curvesrFrrG_load_elliptic_curvess   z$_EllipticCurve._load_elliptic_curvescCs|jdur|||_|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)_curvesrrrFrFrG_get_elliptic_curvess  z#_EllipticCurve._get_elliptic_curves)rrrIcCs|||t||dS)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)rTrp OBJ_nid2sndecode)rrrrFrFrGrsz_EllipticCurve.from_nid)rrr{rIcCs||_||_||_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)rQ_nidr{)rxrrr{rFrFrGrys z_EllipticCurve.__init__rucCsd|jdS)Nzr{rwrFrFrG__repr__sz_EllipticCurve.__repr__cCs|j|j}t|tjS)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. )rQZEC_KEY_new_by_curve_namerrTrYZ EC_KEY_free)rxkeyrFrFrG _to_EC_KEYsz_EllipticCurve._to_EC_KEY)rBrCrDrErrrrrr rrrrstrryrr __classcell__rFrFrrGrs   rrucCs ttS)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. )rrrQrFrFrFrGr&$s r&rzcCs,tD]}|j|kr|Sqtd|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&r{rc)r{ZcurverFrFrGr'3s   r'cseZdZdZdddddZeeddfdd Zeeedd d Z ee d d d Z ee d ddZ edddZ edddZedddZeeeefdddZZS)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. NrzcCs 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)rQ X509_NAME_duprrTrYX509_NAME_freer}rFrFrGry_s zX509Name.__init__)r{valuerIc s|drt||St|tur8tdt|jt t |}|tj krvz t Wnt ylYn0tdtt|jD]J}t|j|}t|}t|}||krt|j|}t|qqt|tr|d}t|j|tj|ddd}|s t dS)N_z,attribute name must be string, not '{:.200}'No such attributeutf-8r) startswithr __setattr__rrrbformatrBrQ OBJ_txt2nid _byte_string NID_undefrr$AttributeErrorrangeX509_NAME_entry_countrX509_NAME_get_entryX509_NAME_ENTRY_get_object OBJ_obj2nidZX509_NAME_delete_entryZX509_NAME_ENTRY_freer`encodeZX509_NAME_add_entry_by_NIDZ MBSTRING_UTF8) rxr{rrientZent_objZent_nid add_resultrrFrGris<          zX509Name.__setattr__c Cstt|}|tjkr>z tWnty4Yn0tdt|j|d}|dkrZdSt |j|}t |}t d}t ||}t|dkz0t |d|ddd}Wt|dnt|d0|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... rrNunsigned char**rr)rQrrrrr$rZX509_NAME_get_index_by_NIDrrX509_NAME_ENTRY_get_datarTrUZASN1_STRING_to_UTF8rWrLr OPENSSL_free) rxr{rZ entry_indexentryrZr\Z data_lengthrrFrFrG __getattr__s*        zX509Name.__getattr__rcCs"t|tstSt|j|jdkSNrr`r(rrQZ X509_NAME_cmprrrFrFrG__eq__s zX509Name.__eq__cCs"t|tstSt|j|jdkSrrrrFrFrG__lt__s zX509Name.__lt__rucCsDtdd}t|j|t|}t|tjkdt | dS)z6 String representation of an X509Name rMizr) rTrUrQZX509_NAME_onelinerrVrWrXrrpr)rxr\Z format_resultrFrFrGrs  zX509Name.__repr__cCs t|jS)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` )rQZX509_NAME_hashrrwrFrFrGhashs z X509Name.hashcCsNtd}t|j|}t|dkt|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` rrN)rTrUrQZ i2d_X509_NAMErrWrLr)rxr\Z encode_resultrrrFrFrGrs   z X509Name.derc Csg}tt|jD]n}t|j|}t|}t|}t|}t|}t t |t |dd}| t ||fq|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)rrQrrrrrrrrTrLrqror|rp) rxrrrfnameZfvalrr{rrFrFrGget_componentss    zX509Name.get_components)rBrCrDrEryrrrr rrrrrrrrarrr rrrFrFrrGr(Es (( r(c@seZdZUdZdeeeededddddZee ddd Z e j d e j d e jd iZejejeefed <edddZedddZedddZedddZedddZdS)r)z, An X.509 v3 certificate extension. Nr+) type_namecriticalrsubjectissuerrIcCstd}t|tjtjtjtjdt||durRt|tsJtd|j |_ |durtt|tsltd|j |_ |rd|}t tj|||}|tjkrt 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 OpenSSL textual representation of the extension's value. :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,)rTrUrQZX509V3_set_ctxrXZX509V3_set_ctx_nodbr`r+rb_x509 issuer_certZ subject_certZX509V3_EXT_nconfrrYX509_EXTENSION_free _extension)rxrrrrrctx extensionrFrFrGrys"     zX509Extension.__init__rucCstt|jSrP)rQrX509_EXTENSION_get_objectrrwrFrFrGrSs zX509Extension._nidZemailZDNSZURI _prefixesc Cstdt|j}t|tj}g}tt|D]}t ||}z|j |j }Wn8t yt }t|||t|dYq40t|jjj|jjjddd}||d|q4d|S)NzGENERAL_NAMES*r:z, )rTrnrQZX509V3_EXT_d2irrYZGENERAL_NAMES_freerZsk_GENERAL_NAME_numZsk_GENERAL_NAME_valuerrKeyErrorr[ZGENERAL_NAME_printr|r]rrLdZia5rZlengthjoin)rxnamespartsrr{ZlabelrNrrFrFrG_subjectAltNameString_s(    z#X509Extension._subjectAltNameStringcCsFtj|jkr|St}t||jdd}t|dkt| dS)zF :return: a nice text representation of the extension rr) rQZNID_subject_alt_namerrr[X509V3_EXT_printrrWr]r)rxrN print_resultrFrFrG__str__us   zX509Extension.__str__cCs t|jS)zk Returns the critical field of this X.509 extension. :return: The critical field. )rQZX509_EXTENSION_get_criticalrrwrFrFrG get_criticalszX509Extension.get_criticalcCs<t|j}t|}t|}|tjkr4t|SdSdS)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 sUNDEFN)rQrrrrrTrXrp)rxobjrbufrFrFrGget_short_names     zX509Extension.get_short_namecCs@t|j}td|}t|}t|}t||ddS)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 rlN)rQX509_EXTENSION_get_datarrTrnrqrorL)rxZ octet_resultrrZ char_result result_lengthrFrFrGget_datas    zX509Extension.get_data)NN)rBrCrDrErarr rypropertyrrrQZ GEN_EMAILZGEN_DNSZGEN_URIrtypingClassVarDictrr__annotations__rrrrr rFrFrFrGr) s,   E r)zZX509Extension support in pyOpenSSL is deprecated. You should use the APIs in cryptography.rc@seZdZdZddddZejdddZeejddd d Z e dd d d Z e dddZ e ddddZe dddZedddZeeddddZeedddZe eddddZe ed dd ZdS)!r*z0 An X.509 certificate signing requests. NrucCs&t}t|tj|_|ddSr)rQZ X509_REQ_newrTrY X509_REQ_free_req set_version)rxreqrFrFrGryszX509Req.__init__cCsddlm}tt|}||S)z Export as a ``cryptography`` certificate signing request. :rtype: ``cryptography.x509.CertificateSigningRequest`` .. versionadded:: 17.1.0 r)load_der_x509_csr)cryptography.x509rr:r )rxrrrFrFrGto_cryptographys  zX509Req.to_cryptography) crypto_reqrIcCs6t|tjstdddlm}||j}tt |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 requestrr) r`rCertificateSigningRequestrbrrrrr;r )rrrrrFrFrGfrom_cryptographys    zX509Req.from_cryptographyrrIcCs t|j|j}t|dkdS)z Set the public key of the certificate signing request. :param pkey: The public key to use. :type pkey: :py:class:`PKey` :return: ``None`` rN)rQZX509_REQ_set_pubkeyrrrWrxrrdrFrFrG set_pubkeys zX509Req.set_pubkeycCsDtt}t|j|_t|jtjkt |jtj |_d|_ |S)z Get the public key of the certificate signing request. :return: The public key. :rtype: :py:class:`PKey` T) r%__new__rQZX509_REQ_get_pubkeyrrrWrTrXrYrrrrFrFrG get_pubkeys  zX509Req.get_pubkeyversionrIcCs@t|tstd|dkr"tdt|j|}t|dkdS)z Set the version subfield (RFC 2986, section 4.1) of the certificate request. :param int version: The version number. :return: ``None`` zversion must be an intrz9Invalid version. The only valid version for X509Req is 0.rN)r`rrbrcrQZX509_REQ_set_versionrrW)rxr"rdrFrFrGrs zX509Req.set_versioncCs t|jS)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` )rQZX509_REQ_get_versionrrwrFrFrG get_versionszX509Req.get_versioncCs2tt}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(rrQZX509_REQ_get_subject_namerrrWrTrX_ownerr}rFrFrG get_subject!s zX509Req.get_subject extensionsrIcCslt}t|tjkt|tj}|D]$}t|ts>t dt ||j q(t |j |}t|dkdS)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`` +One of the elements is not an X509ExtensionrN)rQZsk_X509_EXTENSION_new_nullrWrTrXrYZsk_X509_EXTENSION_freer`_X509ExtensionInternalrcZsk_X509_EXTENSION_pushrZX509_REQ_add_extensionsr)rxr'stackextrrFrFrGadd_extensions7s  zX509Req.add_extensionscCsng}t|j}t|dd}tt|D]:}tt}t t ||}t|tj |_ | |q.|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|ttjdS)Nr)rQZsk_X509_EXTENSION_pop_freerTZ addressofZ _original_lib)xrFrFrG]s z(X509Req.get_extensions..)rQZX509_REQ_get_extensionsrrTrYrZsk_X509_EXTENSION_numr)rX509_EXTENSION_dupZsk_X509_EXTENSION_valuerrr|)rxZextsZnative_exts_objrr+rrFrFrGget_extensionsPs     zX509Req.get_extensionsrdigestrIcCs^|jrtd|jstdtt|}|tjkrd?Z'ee&dd@dAdBZ(e&ddCdDZ)e&ddEdFdGZ*e&ddHdIZ+e&ddJdKdLZ,e ddMdNZ-e.e/ddOdPdQZ0e e/dRdSdTZ1dS)Ur+z An X.509 certificate. NrucCs:t}t|tjkt|tj|_t|_ t|_ dSrP) rQZX509_newrWrTrXrY X509_freerrt_issuer_invalidator_subject_invalidator)rxrrFrFrGrys z X509.__init__)rrIcCs.||}t|tj|_t|_t|_|SrP) rrTrYrQr;rrtr<r=)rrcertrFrFrG_from_raw_x509_ptrs  zX509._from_raw_x509_ptrcCsddlm}tt|}||S)z Export as a ``cryptography`` certificate. :rtype: ``cryptography.x509.Certificate`` .. versionadded:: 17.1.0 r)load_der_x509_certificate)rr@r1r )rxr@rrFrFrGrs  zX509.to_cryptography) crypto_certrIcCs6t|tjstdddlm}||j}tt |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 certificaterr) r`r Certificaterbrrrrr0r )rrArrrFrFrGrs   zX509.from_cryptographyr!cCs,t|tstdtt|j|dkdS)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 integerrN)r`rrbrWrQZX509_set_versionrrxr"rFrFrGrs zX509.set_versioncCs t|jS)z Return the version number of the certificate. :return: The version number of the certificate. :rtype: :py:class:`int` )rQZX509_get_versionrrwrFrFrGr#szX509.get_versioncCsFtt}t|j|_|jtjkr*tt |jtj |_d|_ |S)z{ Get the public key of the certificate. :return: The public key. :rtype: :py:class:`PKey` T) r%rrQX509_get_pubkeyrrrTrXrrYrrrrFrFrGr s  zX509.get_pubkeyrcCs2t|tstdt|j|j}t|dkdS)z Set the public key of the certificate. :param pkey: The public key. :type pkey: :py:class:`PKey` :return: :py:data:`None` r:rN)r`r%rbrQZX509_set_pubkeyrrrWrrFrFrGrs zX509.set_pubkeyr1cCspt|tstd|jr td|js.tdtt|}|t j krNtdt |j |j |}t|dkdS)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:zKey only has public partr4r5rN)r`r%rbrrcrrQr6rrTrXZ X509_signrrrW)rxrr2Zevp_mdr9rFrFrGr<s  z X509.signcCsZt|j}td}t|tjtj|t|d}|tjkrJt dt 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 zASN1_OBJECT **rzUndefined signature algorithm) rQZX509_get0_tbs_sigalgrrTrUZX509_ALGOR_get0rXrrrcrpZ OBJ_nid2ln)rxZsig_algZalgrrFrFrGget_signature_algorithm"s   zX509.get_signature_algorithm) digest_namerIcCstt|}|tjkr tdtdtj}tdd}t||d<t |j |||}t |dkd ddt ||dDS) 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` r5unsigned char[]zunsigned int[]rr:cSsg|]}t|qSrF)rupper)rZchrFrFrG NszX509.digest..)rQr6rrTrXrcrUZEVP_MAX_MD_SIZErVZ X509_digestrrWrrL)rxrFr2r\r Z digest_resultrFrFrGr25s      z X509.digestcCs t|jS)z Return the hash of the X509 subject. :return: The hash of the subject. :rtype: :py:class:`bytes` )rQZX509_subject_name_hashrrwrFrFrGsubject_name_hashTszX509.subject_name_hash)serialrIcCst|tstdt|dd}|d}td}t||}|dtj krpt t |j |}|rt nXt|dtj }t|d|tj krt t|tj}t|j |}t|dkdS)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 integerNrBIGNUM**rr)r`rrbhexrrTrUrQ BN_hex2bnrXZASN1_INTEGER_setX509_get_serialNumberrrBN_to_ASN1_INTEGERrrYASN1_INTEGER_freeZX509_set_serialNumberrW)rxrL hex_serialZhex_serial_bytes bignum_serialZ small_serialrd asn1_serialrFrFrGset_serial_number]s&      zX509.set_serial_numberc Cst|j}t|tj}zVt|}z0t|}t|d}|Wt |Wt |St |0Wt |n t |0dS)zx Return the serial number of this certificate. :return: The serial number. :rtype: int N) rQrQrZASN1_INTEGER_to_BNrTrXZ BN_bn2hexrprrr)rxrVrUrTZhexstring_serialrLrFrFrGget_serial_numbers      zX509.get_serial_number)amountrIcCs.t|tstdt|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`` amount must be an integerN)r`rrbrQX509_getm_notAfterrX509_gmtime_adj)rxrZZnotAfterrFrFrGgmtime_adj_notAfters  zX509.gmtime_adj_notAftercCs.t|tstdt|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`` r[N)r`rrbrQX509_getm_notBeforerr])rxrZZ notBeforerFrFrGgmtime_adj_notBefores  zX509.gmtime_adj_notBeforecCsT|}|durtd|d}tj|d}tjj}tj|jdd}||kS)z Check whether the certificate has expired. :return: ``True`` if the certificate has expired, ``False`` otherwise. :rtype: bool NzUnable to determine notAfterrz %Y%m%d%H%M%SZ)Ztzinfo) get_notAfterrcrdatetimestrptimetimezoneZutcZnowreplace)rxZ time_bytesZ time_stringZ not_afterZUTCZutcnowrFrFrG has_expireds zX509.has_expired)whichrIcCst||jSrP)rsr)rxrgrFrFrG_get_boundary_timeszX509._get_boundary_timecCs |tjS)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 )rhrQr_rwrFrFrG get_notBefores zX509.get_notBefore.)rgr_rIcCst||j|SrP)rer)rxrgr_rFrFrG_set_boundary_timeszX509._set_boundary_timerfcCs|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`` )rjrQr_rxr_rFrFrG set_notBefores zX509.set_notBeforecCs |tjS)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 )rhrQr\rwrFrFrGras 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`` )rjrQr\rkrFrFrG set_notAfters zX509.set_notAftercCs0tt}||j|_t|jtjk||_|SrP)r(rrrrWrTrXr$)rxrgr{rFrFrG _get_names   zX509._get_name)rgr{rIcCs0t|tstd||j|j}t|dkdS)Nzname must be an X509Namer)r`r(rbrrrW)rxrgr{rdrFrFrG _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` )rnrQZX509_get_issuer_namer<r~r}rFrFrG get_issuers  zX509.get_issuer)rrIcCs|tj||jdS)z Set the issuer of this certificate. :param issuer: The issuer. :type issuer: :py:class:`X509Name` :return: ``None`` N)rorQZX509_set_issuer_namer<r)rxrrFrFrG 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` )rnrQX509_get_subject_namer=r~r}rFrFrGr%-s  zX509.get_subject)rrIcCs|tj||jdS)z Set the subject of this certificate. :param subject: The subject. :type subject: :py:class:`X509Name` :return: ``None`` N)rorQZX509_set_subject_namer=r)rxrrFrFrG set_subject=s zX509.set_subjectcCs t|jS)z Get the number of extensions on this certificate. :return: The number of extensions. :rtype: :py:class:`int` .. versionadded:: 0.12 )rQZX509_get_ext_countrrwrFrFrGget_extension_countIs zX509.get_extension_countr&cCs<|D]2}t|tstdt|j|jd}|stqdS)z Add extensions to the certificate. :param extensions: The extensions to add. :type extensions: An iterable of :py:class:`X509Extension` objects. :return: ``None`` r(rN)r`r)rcrQZ X509_add_extrrr)rxr'r+rrFrFrGr,Ts  zX509.add_extensions)indexrIcCsNtt}t|j||_|jtjkr.tdt |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)rrQZ X509_get_extrrrTrX IndexErrorr/rYr)rxrur+rrFrFrG get_extensionfs   zX509.get_extension)2rBrCrDrEryrrr?rrBrrrrr#r%r rrr<rarEr2rKrWrYr^r`rrfr rhrirrjrlrarmr(rnrorprqr%rsrtrr)r,rwrFrFrFrGr+sN   '           r+c@seZdZUdZejZeed<ej Z eed<ej Z eed<ej Zeed<ejZeed<ejZeed<ejZeed<ejZeed <ejZeed <ejZeed <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 CRL_CHECK CRL_CHECK_ALLIGNORE_CRITICAL X509_STRICTALLOW_PROXY_CERTS POLICY_CHECKEXPLICIT_POLICY INHIBIT_MAPCHECK_SS_SIGNATURE PARTIAL_CHAINN)rBrCrDrErQZX509_V_FLAG_CRL_CHECKrxrrZX509_V_FLAG_CRL_CHECK_ALLryZX509_V_FLAG_IGNORE_CRITICALrzZX509_V_FLAG_X509_STRICTr{ZX509_V_FLAG_ALLOW_PROXY_CERTSr|ZX509_V_FLAG_POLICY_CHECKr}ZX509_V_FLAG_EXPLICIT_POLICYr~ZX509_V_FLAG_INHIBIT_MAPrZX509_V_FLAG_CHECK_SS_SIGNATURErZX509_V_FLAG_PARTIAL_CHAINrrFrFrFrGr,~s  r,c@seZdZdZddddZeddddZed ej fdd d d Z e dd ddZ e j ddddZdeeeddddZdS)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`. NrucCst}t|tj|_dSrP)rQZX509_STORE_newrTrYZX509_STORE_free_storerxstorerFrFrGryszX509Store.__init__r>rIcCs0t|tstt|j|j}t|dkdS)a Adds a trusted certificate to this store. Adding a certificate with this method adds this certificate as a *trusted* certificate. :param X509 cert: The certificate to add to this store. :raises TypeError: If the certificate is not an :class:`X509`. :raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your certificate. :return: ``None`` if the certificate was added successfully. rN)r`r+rbrQZX509_STORE_add_certrrrW)rxr>rrFrFrGadd_certs zX509Store.add_cert _CRLInternal)crlrIcCst|tjrVddlm}t||j}t |t j }|t j krFt t |tj}nt|trh|j}ntdtt|j|dkdS)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: ``Union[CRL, cryptography.x509.CertificateRevocationList]`` :return: ``None`` if the certificate revocation list was added successfully. rrzUCRL must be of type OpenSSL.crypto.CRL or cryptography.x509.CertificateRevocationListN)r`rCertificateRevocationListrrr[rrrQd2i_X509_CRL_biorTrXrrY X509_CRL_freer_crlrbrWZX509_STORE_add_crlr)rxrrrNZ openssl_crlrFrFrGadd_crls    zX509Store.add_crl)flagsrIcCstt|j|dkdS)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)rWrQZX509_STORE_set_flagsr)rxrrFrFrG set_flagsszX509Store.set_flags)vfy_timerIcCsFt}t|tj}t|t|t t |j |dkdS)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) rQZX509_VERIFY_PARAM_newrTrYZX509_VERIFY_PARAM_freeZX509_VERIFY_PARAM_set_timecalendarZtimegmZ timetuplerWZX509_STORE_set1_paramr)rxrZparamrFrFrGset_times zX509Store.set_time)cafilecapathrIcCsN|durtj}nt|}|dur(tj}nt|}t|j||}|sJtdS)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)rTrX _path_bytesrQZX509_STORE_load_locationsrr)rxrrZ load_resultrFrFrGload_locationss!zX509Store.load_locations)N)rBrCrDrEryr+rrrrrrrrbrStrOrBytesPathr rrFrFrFrGr-s   $r-cs0eZdZdZeeeeddfdd ZZ 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` N)messageerrors certificaterIcst|||_||_dSrP)rryrr)rxrrrrrFrGryLs zX509StoreContextError.__init__) rBrCrDrErrrr+ryrrFrFrrGr.Cs r.c@seZdZdZdeeeeeddddZe eeeddddZ dd d d Z dd d d Z e d ddZeddddZdd ddZeed ddZdS)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` N)rrchainrIcCs<t}t|tj|_||_||_|||_ | dSrP) rQZX509_STORE_CTX_newrTrYZX509_STORE_CTX_free _store_ctxr_cert_build_certificate_stack_chain_init)rxrrrZ store_ctxrFrFrGryjs  zX509StoreContext.__init__) certificatesrIcCstdddd}|dus$t|dkr*tjSt}t|tjkt||}|D]N}t|t sft dtt |j dkt ||j dkrPt|j tqP|S)N)srIcSs8tt|D]}t||}t|qt|dSrP)rrQ sk_X509_num sk_X509_valuer; sk_X509_free)rrr-rFrFrGcleanup~s  z:X509StoreContext._build_certificate_stack..cleanuprz+One of the elements is not an X509 instance)rrVrTrXrQsk_X509_new_nullrWrYr`r+rbZ X509_up_refr sk_X509_pushr;r)rrr*r>rFrFrGrzs   z)X509StoreContext._build_certificate_stackrucCs.t|j|jj|jj|j}|dkr*tdS)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)rQZX509_STORE_CTX_initrrrrrrrxrirFrFrGrs zX509StoreContext._initcCst|jdS)z Internally cleans up the store context. The store context can then be reused with a new call to :meth:`_init`. N)rQZX509_STORE_CTX_cleanuprrwrFrFrG_cleanupszX509StoreContext._cleanupcCsdttt|jd}t|jt|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. r) rTrprQZX509_verify_cert_error_stringZX509_STORE_CTX_get_errorrrZX509_STORE_CTX_get_error_depthZX509_STORE_CTX_get_current_certZX509_dupr+r?r.)rxrrrrpycertrFrFrG_exception_from_contexts      z(X509StoreContext._exception_from_context)rrIcCs ||_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)rrrFrFrG set_stores 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)rrrQX509_verify_certrrrrFrFrGverify_certificates  z#X509StoreContext.verify_certificatecCs||t|j}|dkr4||t|j}t|tj kg}t t |D]2}t ||}t|tj kt |}||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)rrrQrrrZX509_STORE_CTX_get1_chainrWrTrXrrrr+r?r|r)rxriZ cert_stackrrr>rrFrFrGget_verified_chains"      z#X509StoreContext.get_verified_chain)N)rBrCrDrEr-r+r r ry staticmethodrrrr.rrrrrrFrFrFrGr/Ts$     r/)rrLrIcCsvt|tr|d}t|}|tkrrIcCsnt}|tkrt||j}n<|tkr6t||j}n$|tkrRt||jdd}nt dt |dkt |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 rCtype argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXTr) r[rrQZPEM_write_bio_X509rr Z i2d_X509_bior!Z X509_print_exrcrWr])rr>rN result_coderFrFrGr1,s  r1)rrrIcCsPt}|tkrtj}n|tkr&tj}ntd|||j}|dkrHtt |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 rr) r[rrQZPEM_write_bio_PUBKEYr Zi2d_PUBKEY_biorcrrr])rrrNZ write_biorrFrFrGr2Hs  r2)rrcipher passphraserIc Cst}t|tstd|durR|dur0tdtt|}|tjkrXt dntj}t ||}|t krt ||j |tjd|j|j}|nh|tkrt||j }nP|tkrt|j tjkrtdtt|j tj}t||d}nt dt|dkt|S)a Dump the private key *pkey* into a buffer string encoded with the type *type*. Optionally (if *type* is :const:`FILETYPE_PEM`) encrypting it using *cipher* and *passphrase*. :param type: The file type (one of :const:`FILETYPE_PEM`, :const:`FILETYPE_ASN1`, or :const:`FILETYPE_TEXT`) :param PKey pkey: The PKey to dump :param cipher: (optional) if encrypted PEM format, the cipher to use :param passphrase: (optional) if encrypted PEM format, this can be either the passphrase to use, or a callback for providing the passphrase. :return: The buffer with the dumped key in :rtype: bytes zpkey must be a PKeyNzDif a value is given for cipher one must also be given for passphrasezInvalid cipher namerz-Only RSA keys are supported for FILETYPE_TEXTr)r[r`r%rbrQZEVP_get_cipherbynamerrTrXrc_PassphraseHelperrZPEM_write_bio_PrivateKeyrcallback callback_argsraise_if_problemr Zi2d_PrivateKey_bior!rrrYrrZ RSA_printrWr]) rrrrrNZ cipher_objhelperrrrFrFrGr3asH      r3c@seZdZUdZgdZejejee d<ddddZ eddd d Z edd d Z ddd dZ eeddddZeedddZeedddZeddddZeedddZdS)r4z# A certificate revocation. )s unspecifieds keyCompromises CACompromisesaffiliationChangeds supersededscessationOfOperationscertificateHold _crl_reasonsNrucCst}t|tj|_dSrP)rQZX509_REVOKED_newrTrYX509_REVOKED_free_revoked)rxrevokedrFrFrGryszRevoked.__init__)hex_strrIcCsftttj}td}||d<t||}|s.rr)rr`rarbrrerrurQZASN1_ENUMERATED_newrWrTrXrYZASN1_ENUMERATED_freeZASN1_ENUMERATED_setZX509_REVOKED_add1_ext_i2drr)rxrZ reason_codeZnew_reason_extrdrrFrFrG set_reasons     zRevoked.set_reasoncCstt|jD]p}t|j|}t|}t|tjkrt}t ||dd}|stt |t |}t |dkt |SqdS)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)rrQrrrrrrr[rZM_ASN1_OCTET_STRING_printr rWr])rxrr+rrNrrFrFrG get_reason s    zRevoked.get_reasoncCs|jddS)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)rrwrFrFrG all_reasons2 s zRevoked.all_reasonsrfcCs&t|}t|j|}t|dkdS)z Set the revocation timestamp. :param bytes when: The timestamp of the revocation, as ASN.1 TIME. :return: ``None`` rN)rjrQZX509_REVOKED_set_revocationDaterrW)rxr_ZrevocationDaterirFrFrG 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 )rQZ X509_REVOKED_get0_revocationDaterrs)rxZdtrFrFrG get_rev_dateL s zRevoked.get_rev_date)rBrCrDrErr rrrarryrrrr rrrrrrFrFrFrGr4s   % r4zPCRL support in pyOpenSSL is deprecated. You should use the APIs in cryptography.c@seZdZdZddddZejdddZeejdd d d Z e e e d fdd dZ e ddddZedddZeddddZeddddZeddddZeeeddddZedefeeeeeed d!d"ZdS)#r5z( A certificate revocation list. NrucCst}t|tj|_dSrP)rQZ X509_CRL_newrTrYrr)rxrrFrFrGryi sz CRL.__init__cCsddlm}tt|}||S)z Export as a ``cryptography`` CRL. :rtype: ``cryptography.x509.CertificateRevocationList`` .. versionadded:: 17.1.0 r)load_der_x509_crl)rr_dump_crl_internalr )rxrrrFrFrGrm s  zCRL.to_cryptographyr) crypto_crlrIcCs6t|tjstdddlm}||j}tt |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 listrr) r`rrrbrrrr_load_crl_internalr )rrrrrFrFrGrz s    zCRL.from_cryptography.cCsng}t|j}tt|D]>}t||}t|}tt}t |tj |_ | |q|rjt|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)rQZX509_CRL_get_REVOKEDrrZsk_X509_REVOKED_numZsk_X509_REVOKED_valueX509_REVOKED_dup_RevokedInternalrrTrYrrr|tuple)rxresultsZ revoked_stackrrZ revoked_copyZpyrevrFrFrG get_revoked s      zCRL.get_revoked)rrIcCs8t|j}t|tjkt|j|}t|dkdS)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)rQrrrWrTrXZX509_CRL_add0_revokedr)rxrcopyrrFrFrG add_revoked s zCRL.add_revokedcCsBtt|j}t|tjkt|tj}t t }||_ |S)zc Get the CRL's issuer. .. versionadded:: 16.1.0 :rtype: X509Name ) rQrZX509_CRL_get_issuerrrWrTrXrYrr(rr)rxZ_issuerrrFrFrGrp s  zCRL.get_issuerr!cCstt|j|dkdS)z Set the CRL version. .. versionadded:: 16.1.0 :param int version: The version of the CRL. :return: ``None`` rN)rWrQZX509_CRL_set_versionrrCrFrFrGr s zCRL.set_versionrfcCs&t|}t|j|}t|dkdS)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`` rN)rjrQX509_CRL_set1_lastUpdaterrW)rxr_Z lastUpdaterirFrFrGset_lastUpdate s zCRL.set_lastUpdatecCs&t|}t|j|}t|dkdS)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`` rN)rjrQX509_CRL_set1_nextUpdaterrW)rxr_Z nextUpdaterirFrFrGset_nextUpdate s zCRL.set_nextUpdate)r issuer_keyr2rIcCs\t|}t|tjkt|jt|jt |jt |j|j |}t|dkdS)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) rQr6rWrTrXX509_CRL_set_issuer_namerrrrZ X509_CRL_sort X509_CRL_signr)rxrrr2r8rrFrFrGr< s  zCRL.signd)r>rrdaysr2rIc CsDt|tstdt|ts$tdt|ts6td|turFtdt|}|tj krbt dt }t |tj kt |tj}t|d}t |tj kt|j|}t |dkt||dd d }t |tj kt|j|}t |dkt|jt|j}t |dkt|j|j|} | s:tt||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 cert must be an X509 instancezkey must be a PKey instancerzdigest must be providedr5rr<)r`r+rbr%r _UNSPECIFIEDrQr6rTrXrcrgrWrYrhr]rrrrrrrrrrr) rxr>rrrr2r8Zsometimerir9rFrFrGexport s<         z CRL.export)rBrCrDrEryrrrrrr r rrrr(rprrrarrr+r%r<rrrrFrFrFrGr5d s2  r5c@seZdZdZddddZeedddZeddd d Zee dd d Z e dd ddZ ee edfdddZ eeeddddZeeddddZeedddZd eeeeedddZdS)!r6z A PKCS #12 archive. NrucCsd|_d|_d|_d|_dSrP)rr_cacerts _friendlynamerwrFrFrGry^ 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` )rrwrFrFrGget_certificated szPKCS12.get_certificatercCst|tstd||_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`` rN)r`r+rbr)rxr>rFrFrGset_certificatem 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` )rrwrFrFrGget_privatekeyz szPKCS12.get_privatekeyrcCst|tstd||_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`` r:N)r`r%rbrrrFrFrGset_privatekey s zPKCS12.set_privatekey.cCs|jdurt|jSdS)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)rrrwrFrFrGget_ca_certificates s  zPKCS12.get_ca_certificates)cacertsrIcCs>|durd|_n*t|}|D]}t|tstdq||_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)rlistr`r+rb)rxrr>rFrFrGset_ca_certificates s  zPKCS12.set_ca_certificatesrzcCs4|durd|_nt|ts*td|d||_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 ))rr`rarbr}rFrFrGset_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` )rrwrFrFrGget_friendlyname szPKCS12.get_friendlynamer)ritermaciterrIc Cstd|}|jdurtj}n0t}t|tj}|jD]}t||j q8|durZtj}|j }|durntj}|j durtj}n|j j }|j durtj}n|j j }t |||||tjtj||d }|tjkrtt|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: rNr)_text_to_bytes_and_warnrrTrXrQrrYrrrrrrZ PKCS12_createZ&NID_pbe_WithSHA1And3_Key_TripleDES_CBCrZ PKCS12_freer[Zi2d_PKCS12_bior]) rxrrrrr>Z friendlynamerZpkcs12rNrFrFrGr sH       z PKCS12.export)Nrr)rBrCrDrEryr r+rrr%rrr rrrrarrrrrFrFrFrGr6Y s&      r6zTPKCS#12 support in pyOpenSSL is deprecated. You should use the APIs in cryptography.c@sleZdZdZddddZeeddddZeed d d Z e dd d Z edddZ eddddZ dS)r7z! A Netscape SPKI object. NrucCst}t|tj|_dSrP)rQZNETSCAPE_SPKI_newrTrYZNETSCAPE_SPKI_free_spki)rxZspkirFrFrGry% szNetscapeSPKI.__init__r1cCs^|jrtd|jstdtt|}|tjkrsign() is deprecated. Use the equivilant APIs in cryptography.)r> signaturerZr2rIcCstd|}tt|}|tjkr*tdt|j}t |tjkt |tj }t }t |tj }t||t||t|t||t||}|dkrtdS)a Verify the signature for a data string. :param cert: signing certificate (X509 object) corresponding to the private key which generated the signature. :param signature: signature returned by sign function :param data: data to be verified :param digest: message digest to use :return: ``None`` if the signature is correct, raise exception otherwise. .. versionadded:: 0.11 rZr5rN)rrQr6rrTrXrcrDrrWrYrrrZEVP_VerifyInitZEVP_VerifyUpdaterVZEVP_VerifyFinalr)r>rrZr2r8rrZ verify_resultrFrFrGr= s      r=z@verify() is deprecated. Use the equivilant APIs in cryptography.)rrrIcCsjt}|tkrt||j}n8|tkr6t||j}n |tkrNt||j}nt dt |dkt |S)a Dump a certificate revocation list to a buffer. :param type: The file type (one of ``FILETYPE_PEM``, ``FILETYPE_ASN1``, or ``FILETYPE_TEXT``). :param CRL crl: The CRL to dump. :return: The buffer with the CRL. :rtype: bytes rr) r[rrQZPEM_write_bio_X509_CRLrr Zi2d_X509_CRL_bior!ZX509_CRL_printrcrWr])rrrNrirFrFrGr> s  r>cCst|tr|d}t|}|tkrrr?rrFrFrFrGsL    4        %   ) 6gD( ^g/< E2 i < Z N%  )#  &