a LfI@sxUddlZddlZddlZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZddlmZmZddlmZmZmZmZmZddlmZm Z!m"Z#m$Z%m&Z'm(Z)m*Z+m,Z-gd Z.eej/ej0ej1ej2fZ3ee4e5efZ6ee5e d e5ffZ7e'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 dddZKdtee5e dddZLe e5dddZMe e5ddddZNe ee5d d!d"ZOGd#d$d$ZPGd%d&d&ZQGd'd(d(ZRed(d)d*d+ZSe4eRd,d-d.ZTejUGd/d0d0ZVGd1d2d2ZWGd3d4d4ZXGd5d6d6ZYGd7d8d8ZZGd9d:d:Z[Gd;d<dd>Z]e:e5eYd?d@dAZ^e:eYe5dBdCdDZ_e:eQe5dEdFdGZ`due:eQee4ee7e5dHdIdJZaGdKdLdLZbGdMdNdNZcGdOdPdPZdGdQdRdRZeGdSdTdTZfGdUdVdVZge:ee4e5feQd?dWdXZhdve:ee4e5fee7eQdYdZd[Zie:eXe5d\d]d^Zje:e5eXd?d_d`ZkeQee4e5fe4e5dadbdcZleYe5ee4e5fe4ddddedfZme:ece5dgdhdiZne:ee4e5fecd?djdkZoe:ee4e5fedd?dldmZpejqeperdnesdmdodwee4e5fee5eedpdqdrZtejqeterdsesdrdodS)xN) b16encode)partial)PathLike) AnyCallableIterableListNoReturnOptionalSequenceSetTupleTypeUnion)utilsx509)dsaeced25519ed448rsa) UNSPECIFIED byte_stringexception_from_error_queueffilib make_assert path_bytestext_to_bytes_and_warn)$ FILETYPE_PEM FILETYPE_ASN1 FILETYPE_TEXTTYPE_RSATYPE_DSAErrorPKeyget_elliptic_curvesget_elliptic_curveX509Name X509ExtensionX509ReqX509X509StoreFlags X509StoreX509StoreContextErrorX509StoreContextload_certificatedump_certificatedump_publickeydump_privatekeyRevokedCRLPKCS7PKCS12 NetscapeSPKIload_publickeyload_privatekeydump_certificate_requestload_certificate_requestsignverifydump_crlload_crlload_pkcs7_data load_pkcs12.rr ir"r#TYPE_DHTYPE_ECc@seZdZdZdS)r$z7 An error occurred in an `OpenSSL.crypto` API. N)__name__ __module__ __qualname____doc__rIrIr/sparta/input/_build_configuration/image_build+validate/lib/bmcenv/lib64/python3.9/site-packages/OpenSSL/crypto.pyr$esr$)wherereturncCstd|fdS)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)rKrIrIrJ_untested_errorosrN)bufferrLcCsn|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[])biorefrLcSs t|SN)_libBIO_free)rQrRrIrIrJfreesz_new_mem_buf..free) rTBIO_new BIO_s_memrU_ffinewBIO_new_mem_buflenr_openssl_assertNULLgc)rOrQrVdatarIrIrJ _new_mem_bufxs   ra)rQrLcCs.td}t||}t|d|ddS)zO Copy the contents of an OpenSSL BIO object into a Python byte string. zchar**rN)rYrZrTBIO_get_mem_datarO)rQ result_buffer buffer_lengthrIrIrJ_bio_to_strings  re)boundarywhenrLcCs2t|tstdt||}|dkr.tddS)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) isinstancebytes TypeErrorrTASN1_TIME_set_string ValueError)rfrg set_resultrIrIrJ_set_asn1_times   rn) timestamprLcCstd|}t|dkrdSt|tjkr>tt|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) rYcastrTASN1_STRING_lengthASN1_STRING_typeV_ASN1_GENERALIZEDTIMEstringASN1_STRING_get0_datarZrqr^rNASN1_GENERALIZEDTIME_free)rostring_timestampgeneralized_timestamp string_data string_resultrIrIrJ_get_asn1_times"      r}c@s8eZdZddddZdddddZddd d ZdS) _X509NameInvalidatorNrLcCs g|_dSrS)_namesselfrIrIrJ__init__sz_X509NameInvalidator.__init__r(namerLcCs|j|dSrS)rappendrrrIrIrJaddsz_X509NameInvalidator.addcCs|jD]}|`qdSrS)r_namerrIrIrJclears z_X509NameInvalidator.clear)rErFrGrrrrIrIrIrJr~sr~c@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. FTNrcCs"t}t|tj|_d|_dS)NF)rT EVP_PKEY_newrYr_ EVP_PKEY_free_pkey _initializedrpkeyrIrIrJrsz 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)rrrderrIrIrJto_cryptography_keys   zPKey.to_cryptography_key) crypto_keyrLc 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)rhr RSAPublicKey RSAPrivateKeyr DSAPublicKey DSAPrivateKeyrEllipticCurvePrivateKeyrEd25519PrivateKeyrEd448PrivateKeyrjrrrrrr9r public_bytesDERSubjectPublicKeyInfo private_bytesPKCS8r:)clsrrrrrrrIrIrJfrom_cryptography_key s0   zPKey.from_cryptography_key)typebitsrLc 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)rhintrjr"rlrTBN_newrYr_BN_free BN_set_wordRSA_F4RSA_newRSA_generate_key_exr^r]EVP_PKEY_assign_RSArr#DSA_newDSA_freeDSA_generate_parameters_exDSA_generate_keyEVP_PKEY_set1_DSAr$r)rrrexponentrresultrresrIrIrJ generate_key9s6     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) rrjrT EVP_PKEY_typer EVP_PKEY_RSAEVP_PKEY_get1_RSArrYr_RSA_free RSA_check_key_raise_current_error)rrrrIrIrJcheckqs   z PKey.checkcCs t|jS)zT Returns the type of the key :return: The type of the key. )rT EVP_PKEY_idrrrIrIrJrsz PKey.typecCs t|jS)zh Returns the number of bits of the key :return: The number of bits of the key. )rT EVP_PKEY_bitsrrrIrIrJrsz PKey.bits)rErFrGrHrrr_Keyr classmethodrrrboolrrrrIrIrIrJr%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` NotherrLcst|trtt||StS)z Implement cooperation with the right-hand side argument of ``!=``. Python 3 seems to have dropped this cooperation in this very narrow circumstance. )rhrsuper__ne__NotImplementedrr __class__rIrJrs z_EllipticCurve.__ne__)rrLcs>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|]}|jVqdSrS)from_nidnid).0crrrIrJ z7_EllipticCurve._load_elliptic_curves..)EC_get_builtin_curvesrYr^rZset)rr num_curvesbuiltin_curvesrIrrJ_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)_curvesrrrIrIrJ_get_elliptic_curvess  z#_EllipticCurve._get_elliptic_curves)rrrLcCs|||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)rYrv OBJ_nid2sndecode)rrrrIrIrJrsz_EllipticCurve.from_nid)rrrrLcCs||_||_||_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)rT_nidr)rrrrrIrIrJrs z_EllipticCurve.__init__rcCs d|jfS)Nz rrrIrIrJ__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. )rTEC_KEY_new_by_curve_namerrYr_ EC_KEY_free)rkeyrIrIrJ _to_EC_KEYsz_EllipticCurve._to_EC_KEY)rErFrGrHrrrrrr rrrrstrrrr __classcell__rIrIrrJrs   rrcCs 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. )rrrTrIrIrIrJr&s r&rcCs,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&rrl)rcurverIrIrJr's   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. NrcCs 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)rT X509_NAME_duprrYr_X509_NAME_freerrIrIrJr>s zX509Name.__init__)rvaluerLc s|drtt|||St|turz 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)rTrrrrr$rX509_NAME_get_index_by_NIDrrX509_NAME_ENTRY_get_datarYrZASN1_STRING_to_UTF8r]rOr OPENSSL_free) rrr entry_indexentryr`rc data_lengthrrIrIrJ __getattr__os*        zX509Name.__getattr__rcCs"t|tstSt|j|jdkSNrrhr(rrT X509_NAME_cmprrrIrIrJ__eq__s zX509Name.__eq__cCs"t|tstSt|j|jdkSrrrrIrIrJ__lt__s zX509Name.__lt__rcCsDtdd}t|j|t|}t|tjkdt| dfS)z6 String representation of an X509Name rPizr) rYrZrTX509_NAME_onelinerr\r]r^rvr)rrc format_resultrIrIrJrs  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` )rTX509_NAME_hashrrrIrIrJhashs 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` r rN)rYrZrT i2d_X509_NAMErr]rOr)rrc encode_resultr|rIrIrJrs   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)rrTrrrrr rrrYrOrwrsrrv) rrrrfnamefvalrrrrIrIrJget_componentss    zX509Name.get_components)rErFrGrHrrrrr rrrrrrrrirrr r!rrIrIrrJr($s '( r(c@seZdZdZdeeeededddddZee ddd Z e j d e j d e jd iZedd dZedddZedddZedddZedddZdS)r)z, An X.509 v3 certificate extension. Nr+) type_namecriticalrsubjectissuerrLcCstd}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 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,)rYrZrTX509V3_set_ctxr^X509V3_set_ctx_nodbrhr+rj_x509 issuer_cert subject_certX509V3_EXT_nconfrr_X509_EXTENSION_free _extension)rr"r#rr$r%ctx extensionrIrIrJrs"     zX509Extension.__init__rcCstt|jSrS)rTrX509_EXTENSION_get_objectr-rrIrIrJr0s zX509Extension._nidemailDNSURIc 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, )rYrrrTX509V3_EXT_d2ir-r_GENERAL_NAMES_freersk_GENERAL_NAME_numsk_GENERAL_NAME_value _prefixesrKeyErrorraGENERAL_NAME_printrrerrOdia5r`lengthjoin)rnamespartsrrlabelrQrrIrIrJ_subjectAltNameString<s(    z#X509Extension._subjectAltNameStringcCsFtj|jkr|St}t||jdd}t|dkt| dS)zF :return: a nice text representation of the extension rr) rTNID_subject_alt_namerrCraX509V3_EXT_printr-r]rer)rrQ print_resultrIrIrJ__str__Rs   zX509Extension.__str__cCs t|jS)zk Returns the critical field of this X.509 extension. :return: The critical field. )rTX509_EXTENSION_get_criticalr-rrIrIrJ get_critical_szX509Extension.get_criticalcCs&t|j}t|}tt|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 )rTr0r-rrYrvr)robjrrIrIrJget_short_namegs  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 rpN)rTX509_EXTENSION_get_datar-rYrrrwrsrO)r octet_resultr| char_result result_lengthrIrIrJget_datavs    zX509Extension.get_data)NN)rErFrGrHrirr rpropertyrrrT GEN_EMAILGEN_DNSGEN_URIr9rrCrGrIrKrPrIrIrIrJr)s,  D r)c@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. NrcCs&t}t|tj|_|ddSr)rT X509_REQ_newrYr_ X509_REQ_free_req set_version)rreqrIrIrJrszX509Req.__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.x509rZr;r )rrZrrIrIrJto_cryptographys  zX509Req.to_cryptography) crypto_reqrLcCs6t|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) rhrCertificateSigningRequestrjrrrrr<r )rr]rrrIrIrJfrom_cryptographys    zX509Req.from_cryptographyrrLcCs 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)rTX509_REQ_set_pubkeyrWrr]rrrmrIrIrJ 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__rTX509_REQ_get_pubkeyrWrr]rYr^r_rrrrIrIrJ get_pubkeys  zX509Req.get_pubkeyversionrLcCst|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`` rN)rTX509_REQ_set_versionrWr])rrirmrIrIrJrXszX509Req.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` )rTX509_REQ_get_versionrWrrIrIrJ 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(rerTX509_REQ_get_subject_namerWrr]rYr^_ownerrrIrIrJ get_subjects zX509Req.get_subject extensionsrLcCslt}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)rTsk_X509_EXTENSION_new_nullr]rYr^r_sk_X509_EXTENSION_freerhr)rlsk_X509_EXTENSION_pushr-X509_REQ_add_extensionsrW)rrqstackextr rIrIrJadd_extensionss 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,)rTsk_X509_EXTENSION_pop_freerY addressof _original_lib)xrIrIrJs z(X509Req.get_extensions..)rTX509_REQ_get_extensionsrWrYr_rsk_X509_EXTENSION_numr)reX509_EXTENSION_dupsk_X509_EXTENSION_valuer,r-r)rextsnative_exts_objrrxr/rIrIrJget_extensionss     zX509Req.get_extensionsrdigestrLcCs^|jrtd|jstdtt|}|tjkrEs zX509Req.verify)rErFrGrHrrr_r\rr`r%rdrgrrXrlr(rorr)ryrrrr=rr>rIrIrIrJr*s    r*c@s eZdZdZddddZeeddddZej dd d Z eej dd d d Z e ddddZ e dddZedddZeddddZeeddddZedddZeedddZedd d!Ze dd"d#d$Ze dd%d&Ze dd'd(d)Ze dd'd*d+Zedd,d-Zeeed.d/d0Zeedd1d2Z e!d3efedd4d5d6Z"edd7d8d9Z#eedd:d;Z$edd7dd?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. NrcCs:t}t|tjkt|tj|_t|_ t|_ dSrS) rTX509_newr]rYr^r_ X509_freer(r~_issuer_invalidator_subject_invalidator)rrrIrIrJr`s z X509.__init__)rrLcCs.||}t|tj|_t|_t|_|SrS) rerYr_rTrr(r~rr)rrcertrIrIrJ_from_raw_x509_ptrhs  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)r[rr1r )rrrrIrIrJr\ps  zX509.to_cryptography) crypto_certrLcCs6t|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^) rhr Certificaterjrrrrr0r )rrrrrIrIrJr`}s   zX509.from_cryptographyrhcCs,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)rhrrjr]rTX509_set_versionr(rrirIrIrJrXs zX509.set_versioncCs t|jS)z Return the version number of the certificate. :return: The version number of the certificate. :rtype: :py:class:`int` )rTX509_get_versionr(rrIrIrJrlszX509.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%rerTX509_get_pubkeyr(rrYr^rr_rrrrIrIrJrgs  zX509.get_pubkeyracCs2t|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` rrN)rhr%rjrTX509_set_pubkeyr(rr]rcrIrIrJrds zX509.set_pubkeyrcCspt|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` rzKey only has public partrrrN)rhr%rjrrlrrTrrrYr^ X509_signr(rr])rrrevp_mdrrIrIrJr=s  z X509.signcCs:t|j}t|j}|tjkr*tdtt |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) rTX509_get0_tbs_sigalgr(r algorithmrrlrYrv OBJ_nid2ln)ralgorrrIrIrJget_signature_algorithms   zX509.get_signature_algorithm) digest_namerLcCstt|}|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` runsigned char[]zunsigned int[]rr:cSsg|]}t|qSrI)rupper)rchrIrIrJ szX509.digest..)rTrrrYr^rlrZEVP_MAX_MD_SIZEr\ X509_digestr(r]r?rO)rrrrcrO digest_resultrIrIrJrs      z X509.digestcCs t|jS)z Return the hash of the X509 subject. :return: The hash of the subject. :rtype: :py:class:`bytes` )rTX509_subject_name_hashr(rrIrIrJsubject_name_hashszX509.subject_name_hash)serialrLcCst|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)rhrrjhexrrYrZrT BN_hex2bnr^ASN1_INTEGER_setX509_get_serialNumberr(rBN_to_ASN1_INTEGERrr_ASN1_INTEGER_freeX509_set_serialNumberr])rr hex_serialhex_serial_bytes bignum_serial small_serialrm asn1_serialrIrIrJset_serial_numbers&      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) rTrr(ASN1_INTEGER_to_BNrYr^ BN_bn2hexrvrrr)rrrrhexstring_serialrrIrIrJget_serial_numberCs      zX509.get_serial_number)amountrLcCs.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)rhrrjrTX509_getm_notAfterr(X509_gmtime_adj)rrnotAfterrIrIrJgmtime_adj_notAfterWs  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`` rN)rhrrjrTX509_getm_notBeforer(r)rr notBeforerIrIrJgmtime_adj_notBeforees  zX509.gmtime_adj_notBeforecCs>|}|durtd|d}tj|d}|tjkS)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) get_notAfterrlrdatetimestrptimeutcnow)r time_bytes time_string not_afterrIrIrJ has_expiredrs  zX509.has_expired)whichrLcCst||jSrS)r}r()rrrIrIrJ_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 )rrTrrrIrIrJ get_notBefores zX509.get_notBefore.rrgrLcCst||j|SrS)rnr(rrrgrIrIrJ_set_boundary_timeszX509._set_boundary_timergrLcCs|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`` )rrTrrrgrIrIrJ 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 )rrTrrrIrIrJrs 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`` )rrTrrrIrIrJ set_notAfters zX509.set_notAftercCs0tt}||j|_t|jtjk||_|SrS)r(rer(rr]rYr^rn)rrrrIrIrJ _get_names   zX509._get_name)rrrLcCs0t|tstd||j|j}t|dkdS)Nzname must be an X509Namer)rhr(rjr(rr])rrrrmrIrIrJ _set_names 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` )rrTX509_get_issuer_namerrrrIrIrJ get_issuers  zX509.get_issuer)r%rLcCs|tj||jdS)z Set the issuer of this certificate. :param issuer: The issuer. :type issuer: :py:class:`X509Name` :return: ``None`` N)rrTX509_set_issuer_namerr)rr%rIrIrJ set_issuers 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` )rrTX509_get_subject_namerrrrIrIrJros  zX509.get_subject)r$rLcCs|tj||jdS)z Set the subject of this certificate. :param subject: The subject. :type subject: :py:class:`X509Name` :return: ``None`` N)rrTX509_set_subject_namerr)rr$rIrIrJ set_subjects 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 )rTX509_get_ext_countr(rrIrIrJget_extension_counts zX509.get_extension_countrpcCs<|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`` rrrN)rhr)rlrT X509_add_extr(r-r)rrqrxr rIrIrJrys  zX509.add_extensions)indexrLcCsNtt}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)rerT X509_get_extr(r-rYr^ IndexErrorrr_r,)rrrxr/rIrIrJ get_extension!s   zX509.get_extension)2rErFrGrHrrrrrrr\r`rrXrlr%rgrdrr=rirrrrrrrrrr rrrrrrrr(rrrrrorrrr)ryrrIrIrIrJr+[sJ   '          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_MAP NOTIFY_POLICYCHECK_SS_SIGNATUREN)rErFrGrHrTX509_V_FLAG_CRL_CHECKrr__annotations__X509_V_FLAG_CRL_CHECK_ALLrX509_V_FLAG_IGNORE_CRITICALrX509_V_FLAG_X509_STRICTrX509_V_FLAG_ALLOW_PROXY_CERTSrX509_V_FLAG_POLICY_CHECKrX509_V_FLAG_EXPLICIT_POLICYrX509_V_FLAG_INHIBIT_MAPrX509_V_FLAG_NOTIFY_POLICYrX509_V_FLAG_CHECK_SS_SIGNATURErrIrIrIrJr,9s  r,c@sxeZdZdZddddZeddddZd dd d d Zedd ddZ e j ddddZ de e e ddddZdS)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`. NrcCst}t|tj|_dSrS)rTX509_STORE_newrYr_X509_STORE_free_storerstorerIrIrJr^szX509Store.__init__rrLcCs0t|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)rhr+rjrTX509_STORE_add_certr r(r])rrrrIrIrJadd_certbs zX509Store.add_certr5)crlrLcCstt|j|jdkdS)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)r]rTX509_STORE_add_crlr _crlrrrIrIrJadd_crlxszX509Store.add_crl)flagsrLcCstt|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)r]rTX509_STORE_set_flagsr )rrrIrIrJ set_flagsszX509Store.set_flags)vfy_timerLcCsFt}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) rTX509_VERIFY_PARAM_newrYr_X509_VERIFY_PARAM_freeX509_VERIFY_PARAM_set_timecalendartimegm timetupler]X509_STORE_set1_paramr )rrparamrIrIrJset_times zX509Store.set_time)cafilecapathrLcCsN|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)rYr^ _path_bytesrTX509_STORE_load_locationsr r)rr!r" load_resultrIrIrJload_locationss!zX509Store.load_locations)N)rErFrGrHrr+rrrrrr StrOrBytesPathr r&rIrIrIrJr-Ps 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 certificaterLcs tt||||_||_dSrS)rr.rr)r*)rr(r)r*rrIrJrszX509StoreContextError.__init__) rErFrGrHrrrr+rrrIrIrrJr.s 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)r r*chainrLcCs<t}t|tj|_||_||_|||_ | dSrS) rTX509_STORE_CTX_newrYr_X509_STORE_CTX_free _store_ctxr _cert_build_certificate_stack_chain_init)rr r*r+ store_ctxrIrIrJrs  zX509StoreContext.__init__) certificatesrLcCstdddd}|dus$t|dkr*tjSt}t|tjkt||}|D]N}t|t sft dtt |j dkt ||j dkrPt|j tqP|S)N)srLcSs8tt|D]}t||}t|qt|dSrS)rrT sk_X509_num sk_X509_valuer sk_X509_free)r5rr}rIrIrJcleanup%s  z:X509StoreContext._build_certificate_stack..cleanuprz+One of the elements is not an X509 instance)rr\rYr^rTsk_X509_new_nullr]r_rhr+rj X509_up_refr( sk_X509_pushrr)r4r9rwrrIrIrJr0!s   z)X509StoreContext._build_certificate_stackrcCs.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)rTX509_STORE_CTX_initr.r r/r(r1rrretrIrIrJr2?s 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)rTX509_STORE_CTX_cleanupr.rrIrIrJ_cleanupLszX509StoreContext._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) rYrvrTX509_verify_cert_error_stringX509_STORE_CTX_get_errorr.rX509_STORE_CTX_get_error_depthX509_STORE_CTX_get_current_certX509_dupr+rr.)rr(r)r(r/pycertrIrIrJ_exception_from_contextTs      z(X509StoreContext._exception_from_context)r rLcCs ||_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 r rIrIrJ set_storems 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)rAr2rTX509_verify_certr.rHr>rIrIrJverify_certificatexs  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)rAr2rTrJr.rHX509_STORE_CTX_get1_chainr]rYr^rr6r7r+rrr8)rr? cert_stackrrrrGrIrIrJget_verified_chains"      z#X509StoreContext.get_verified_chain)N)rErFrGrHr-r+r r r staticmethodr0r2rAr.rHrIrKrrNrIrIrIrJr/s$     r/)rrOrLcCsvt|tr|d}t|}|tkr.rr)rxrhrirjr{replace _crl_reasonsrrTASN1_ENUMERATED_newr]rYr^r_ASN1_ENUMERATED_freeASN1_ENUMERATED_setX509_REVOKED_add1_ext_i2drirv)rry reason_codenew_reason_extrmr rIrIrJ 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)rrTrtrirur0rrvrarEM_ASN1_OCTET_STRING_printrLr]re)rrrxrJrQrFrIrIrJ get_reasons    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)r~rrIrIrJ all_reasonss zRevoked.all_reasonsrcCst|j}t||S)z Set the revocation timestamp. :param bytes when: The timestamp of the revocation, as ASN.1 TIME. :return: ``None`` )rT X509_REVOKED_get0_revocationDaterirn)rrgdtrIrIrJ set_rev_dates zRevoked.set_rev_datecCst|j}t|S)z Get the revocation timestamp. :return: The timestamp of the revocation, as ASN.1 TIME. :rtype: bytes )rTrrir})rrrIrIrJ get_rev_dates zRevoked.get_rev_date)rErFrGrHr~rrirorsrxr rrrrrrrIrIrIrJr4Ms  %  r4c@seZdZdZddddZejdddZeejddd d Z e e e d fdd d Z e ddddZedddZeddddZed efeddddZeddddZeddddZeeedddd Zed!efeeeeeed"d#d$ZdS)%r5z( A certificate revocation list. NrcCst}t|tj|_dSrS)rT X509_CRL_newrYr_ X509_CRL_freerrrIrIrJr 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)r[rr?r )rrrrIrIrJr\ s  zCRL.to_cryptography) crypto_crlrLcCs6t|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^) rhrCertificateRevocationListrjrrrrr@r )rrrrrIrIrJr` 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)rTX509_CRL_get_REVOKEDrrsk_X509_REVOKED_numsk_X509_REVOKED_valueX509_REVOKED_dupr4rerYr_rhrirtuple)rresults revoked_stackrrj revoked_copypyrevrIrIrJ get_revoked' s      zCRL.get_revoked)rjrLcCs8t|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)rTrrir]rYr^X509_CRL_add0_revokedr)rrjcopyr rIrIrJ 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 ) rTrX509_CRL_get_issuerrr]rYr^r_rr(rer)r_issuerr%rIrIrJrN s  zCRL.get_issuerrhcCstt|j|dkdS)z Set the CRL version. .. versionadded:: 16.1.0 :param int version: The version of the CRL. :return: ``None`` rN)r]rTX509_CRL_set_versionrrrIrIrJrX] s zCRL.set_versionrcCst||j|SrS)rnrrrIrIrJrh szCRL._set_boundary_timercCs|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`` )rrTX509_CRL_get0_lastUpdaterrIrIrJset_lastUpdatem 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`` )rrTX509_CRL_get0_nextUpdaterrIrIrJset_nextUpdate| s zCRL.set_nextUpdate)r) issuer_keyrrLcCs\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) rTrr]rYr^X509_CRL_set_issuer_namerrr( X509_CRL_sort X509_CRL_signr)rr)rrrrrIrIrJr= s  zCRL.signd)rrrdaysrrLc Cst|tstdt|ts$tdt|ts6td|turFtdt|}|tj krbt dt t }t |tj kt}t |tj kt|dt|j|t||dddt|j|t|jt|jt|j|j|} | stt||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 providedrr<)rhr+rjr%r _UNSPECIFIEDrTrrYr^rlrWrXr] ASN1_TIME_newrX509_CRL_set1_lastUpdaterX509_CRL_set1_nextUpdaterrr(rrrr?) rrrrrrrrQsometimerrIrIrJexport s4      z CRL.export)rErFrGrHrrrr\rr`r r r4rrr(rrrXrrrirrrr+r%r=rrrrIrIrIrJr5s8   r5c@s\eZdZUeed<edddZedddZedddZedd d Z e dd d Z d S)r6_pkcs7rcCstt|jS)zm Check if this NID_pkcs7_signed object :return: True if the PKCS7 is of type signed )rrTPKCS7_type_is_signedrrrIrIrJtype_is_signed szPKCS7.type_is_signedcCstt|jS)zt Check if this NID_pkcs7_enveloped object :returns: True if the PKCS7 is of type enveloped )rrTPKCS7_type_is_envelopedrrrIrIrJtype_is_enveloped szPKCS7.type_is_envelopedcCstt|jS)z Check if this NID_pkcs7_signedAndEnveloped object :returns: True if the PKCS7 is of type signedAndEnveloped )rrT PKCS7_type_is_signedAndEnvelopedrrrIrIrJtype_is_signedAndEnveloped sz PKCS7.type_is_signedAndEnvelopedcCstt|jS)zi Check if this NID_pkcs7_data object :return: True if the PKCS7 is of type data )rrTPKCS7_type_is_datarrrIrIrJ 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 )rTrrrrrYrv)rr string_typerIrIrJ get_type_name s zPKCS7.get_type_nameN) rErFrGrrrrrrrrrrIrIrIrJr6 s r6c@seZdZdZddddZeedddZeddd d Zee dd d Z e dd ddZ ee edfdddZ eeeddddZeeddddZeedddZd eeeeedddZdS)!r7z A PKCS #12 archive. NrcCsd|_d|_d|_d|_dSrS)rr/_cacerts _friendlynamerrIrIrJr 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` )r/rrIrIrJget_certificate szPKCS12.get_certificater cCst|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)rhr+rjr/)rrrIrIrJset_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` )rrrIrIrJget_privatekey1 szPKCS12.get_privatekeyracCst|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`` rN)rhr%rjrrrIrIrJset_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)rrrrIrIrJget_ca_certificatesG s  zPKCS12.get_ca_certificates)cacertsrLcCs>|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)rlistrhr+rj)rrrrIrIrJset_ca_certificatesS s  zPKCS12.set_ca_certificatesrcCs2|durd|_nt|ts(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))rrhrirjrrIrIrJset_friendlynameh 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` )rrrIrIrJget_friendlynamey szPKCS12.get_friendlynamer)r\itermaciterrLc 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: r\Nr)_text_to_bytes_and_warnrrYr^rTr:r_r8r<r(rrr/ PKCS12_create&NID_pbe_WithSHA1And3_Key_TripleDES_CBCr PKCS12_freerai2d_PKCS12_biore) rr\rrrr friendlynamerpkcs12rQrIrIrJr sH       z PKCS12.export)Nrr)rErFrGrHrr r+rrr%rrr rrrrirrrrrIrIrIrJr7 s&      r7c@sleZdZdZddddZeeddddZeed d d Z e dd d Z edddZ eddddZ dS)r8z! A Netscape SPKI object. NrcCst}t|tj|_dSrS)rTNETSCAPE_SPKI_newrYr_NETSCAPE_SPKI_free_spki)rspkirIrIrJr szNetscapeSPKI.__init__rcCs^|jrtd|jstdtt|}|tjkr s zNetscapeSPKI.verifycCs$t|j}t|}t||S)z Generate a base64 encoded representation of this SPKI object. :return: The base64 encoded string. :rtype: :py:class:`bytes` )rTNETSCAPE_SPKI_b64_encoderrYrvr)rencodedrrIrIrJ b64_encode s   zNetscapeSPKI.b64_encodecCsDtt}t|j|_t|jtjkt |jtj |_d|_ |S)z| Get the public key of this certificate. :return: The public key. :rtype: :py:class:`PKey` T) r%rerTNETSCAPE_SPKI_get_pubkeyrrr]rYr^r_rrrrIrIrJrg s  zNetscapeSPKI.get_pubkeyracCs t|j|j}t|dkdS)zv Set the public key of the certificate :param pkey: The public key :return: ``None`` rN)rTNETSCAPE_SPKI_set_pubkeyrrr]rcrIrIrJrd szNetscapeSPKI.set_pubkey)rErFrGrHrr%rr=rr>rirrgrdrIrIrIrJr8 s r8c@szeZdZdeeeeeddddZee dddZ ee dd d Z e fe edd d d Ze ee e edddZdS)r^FN)rr\ more_argstruncaterLcCs4|tkr|durtd||_||_||_g|_dS)Nz0only FILETYPE_PEM key format supports encryption)rrl _passphrase _more_args _truncate _problems)rrr\rrrIrIrJr& sz_PassphraseHelper.__init__rcCs@|jdurtjSt|jts&t|jr4td|jStddS)Npem_password_cb2Last argument must be a byte string or a callable.) rrYr^rhricallabler`_read_passphraserjrrIrIrJr`6 s z_PassphraseHelper.callbackcCs8|jdurtjSt|jts&t|jr,tjStddS)Nr)rrYr^rhrirrjrrIrIrJraA s z_PassphraseHelper.callback_args) exceptionTyperLcCs6|jr2z t|Wn|y$Yn0|jddSr)r_exception_from_error_queuepop)rrrIrIrJrbL s   z"_PassphraseHelper.raise_if_problem)bufsizerwflaguserdatarLc Cszt|jr.|jr"||||}qB||}n|jdussignature_buffersignature_length final_resultrIrIrJr= s"         r=)r signaturer`rrLcCstd|}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 r`rrN)rrTrrrYr^rlrr(r]r_rrrEVP_VerifyInitEVP_VerifyUpdater\EVP_VerifyFinalr)rr r`rrrr verify_resultrIrIrJr> s      r>)rrrLcCsjt}|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 rSr) rarrTPEM_write_bio_X509_CRLrr i2d_X509_CRL_bior!X509_CRL_printrlr]re)rrrQr?rIrIrJr?@ s  r?cCst|tr|d}t|}|tkrr?r@rA deprecatedrEDeprecationWarningrBrIrIrIrJs    4( (   ) 6gC Va< E/g/<ZO%  )$ &  W