a _<@sddlZddlZddlmZddlmZddlmZmZm Z m Z m Z m Z ddl mZmZmZddlmZmZddlmZmZddlmZmZmZ m!Z"m#Z$m%Z&m'Z(m)Z*m+Z,gd Z-ej.Z/ej0Z1d Z2ej3Z4ej5Z6ej7Z8ej9Z:Gd d d e;Zd dZ?ddZ@d^ddZAddZBddZCddZDGdddeEZFGdddeEZGGdddeEZHdd ZId!d"ZJGd#d$d$eEZKGd%d&d&eEZLGd'd(d(eEZMGd)d*d*eEZNGd+d,d,eEZOGd-d.d.eEZPGd/d0d0e;ZQGd1d2d2eEZRd3d4ZSd5d6ZTd7d8ZUd_d9d:ZVGd;d<dd>eEZXGd?d@d@eEZYGdAdBdBeEZZGdCdDdDeEZ[GdEdFdFeEZ\dGdHZ]d`dIdJZ^dKdLZ_dMdNZ`dOdPZadQdRZbdSdTZcdUdVZddWdXZeefeeegdYehZedadZd[Ziefeiegd\ehZiejekeld]dS)bN) b16encode)partial)__eq____ne____lt____le____gt____ge__) integer_types text_typePY2)utilsx509)dsarsa) ffilibexception_from_error_queue byte_stringnative path_string UNSPECIFIEDtext_to_bytes_and_warn make_assert)$ 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_pkcs12ic@seZdZdZdS)rz7 An error occurred in an `OpenSSL.crypto` API. N)__name__ __module__ __qualname____doc__rBrB0/usr/lib/python3/dist-packages/OpenSSL/crypto.pyrPsrcCsddlm}|S)ap Importing the backend from cryptography has the side effect of activating the osrandom engine. This mutates the global state of OpenSSL in the process and causes issues for various programs that use subinterpreters or embed Python. By putting the import in this function we can avoid triggering this side effect unless _get_backend is called. rbackend)Z,cryptography.hazmat.backends.openssl.backendrErDrBrBrC _get_backendZs rFcCstd|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)whererBrBrC_untested_errorgsrIcCsd|durtt}tj}n(td|}t|t|}|fdd}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[]cSs t|SN)_libBIO_free)biorefrBrBrCfreesz_new_mem_buf..free) rLBIO_new BIO_s_memrM_ffinewZBIO_new_mem_buflen_openssl_assertNULLgc)bufferrNrPdatarBrBrC _new_mem_bufps    r[cCs.td}t||}t|d|ddS)zO Copy the contents of an OpenSSL BIO object into a Python byte string. zchar**rN)rSrTrLZBIO_get_mem_datarY)rN result_bufferZ buffer_lengthrBrBrC_bio_to_strings  r]cCs2t|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 TypeErrorrLZASN1_TIME_set_string ValueError)boundarywhen set_resultrBrBrC_set_asn1_times   recCstd|}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) rScastrLASN1_STRING_lengthZASN1_STRING_typeZV_ASN1_GENERALIZEDTIMEstringASN1_STRING_datarTrgrWrIZASN1_GENERALIZEDTIME_free)Z timestampZstring_timestampZgeneralized_timestampZ string_data string_resultrBrBrC_get_asn1_times"      rmc@s$eZdZddZddZddZdS)_X509NameInvalidatorcCs g|_dSrK)_namesselfrBrBrC__init__sz_X509NameInvalidator.__init__cCs|j|dSrK)roappendrqnamerBrBrCaddsz_X509NameInvalidator.addcCs|jD]}|`qdSrK)ro_namertrBrBrCclears z_X509NameInvalidator.clearN)r>r?r@rrrvrxrBrBrBrCrnsrnc@sTeZdZdZdZdZddZddZedd Z d d Z d d Z ddZ ddZ dS)r zD A class representing an DSA or RSA public key or key pair. FTcCs"t}t|tj|_d|_dS)NF)rLZ EVP_PKEY_newrSrX EVP_PKEY_free_pkey _initializedrqpkeyrBrBrCrrsz PKey.__init__cCs(t}|jr||jS||jSdS)a Export as a ``cryptography`` key. :rtype: One of ``cryptography``'s `key interfaces`_. .. _key interfaces: https://cryptography.io/en/latest/hazmat/ primitives/asymmetric/rsa/#key-interfaces .. versionadded:: 16.1.0 N)rF _only_publicZ_evp_pkey_to_public_keyrzZ_evp_pkey_to_private_key)rqrErBrBrCto_cryptography_keys  zPKey.to_cryptography_keycCsR|}t|tjtjtjtjfs(td|j|_ t|tjtjfrHd|_ d|_ |S)z Construct based on a ``cryptography`` *crypto_key*. :param crypto_key: A ``cryptography`` key. :type crypto_key: One of ``cryptography``'s `key interfaces`_. :rtype: PKey .. versionadded:: 16.1.0 zUnsupported key typeT) r^rZ RSAPublicKeyZ RSAPrivateKeyrZ DSAPublicKeyZ DSAPrivateKeyr`Z _evp_pkeyrzr~r{)clsZ crypto_keyr}rBrBrCfrom_cryptography_keys  zPKey.from_cryptography_keyc 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^intr`rrarLBN_newrSrXBN_freeZ BN_set_wordZRSA_F4ZRSA_newZRSA_generate_key_exrWrVZEVP_PKEY_assign_RSArzrZDSA_newZDSA_freeZDSA_generate_parameters_exZDSA_generate_keyZEVP_PKEY_set1_DSArr{)rqtypebitsZexponentrresultrresrBrBrC generate_keys6     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 onlyzkey type unsupportedrTN) r~r`rLZ EVP_PKEY_typer EVP_PKEY_RSAEVP_PKEY_get1_RSArzrSrXRSA_freeZ RSA_check_key_raise_current_error)rqrrrBrBrCcheckSs   z PKey.checkcCs t|jS)zT Returns the type of the key :return: The type of the key. )rL EVP_PKEY_idrzrprBrBrCrmsz PKey.typecCs t|jS)zh Returns the number of bits of the key :return: The number of bits of the key. )rLZ EVP_PKEY_bitsrzrprBrBrCrusz PKey.bitsN)r>r?r@rAr~r{rrr classmethodrrrrrrBrBrBrCr s 8r csdeZdZdZdZes fddZeddZeddZ ed d Z d d Z d dZ ddZ ZS)_EllipticCurveaZ A representation of a supported elliptic curve. @cvar _curves: :py:obj:`None` until an attempt is made to load the curves. Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve` instances each of which represents one curve supported by the system. @type _curves: :py:type:`NoneType` or :py:type:`set` Ncst|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. )r^rsuperrNotImplemented)rqother __class__rBrCrs z_EllipticCurve.__ne__cs>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|]}|jVqdSrK)from_nidnid).0crrrBrC z7_EllipticCurve._load_elliptic_curves..)ZEC_get_builtin_curvesrSrWrTset)rrZ num_curvesZbuiltin_curvesrBrrC_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)_curvesrrrBrBrC_get_elliptic_curvess  z#_EllipticCurve._get_elliptic_curvescCs|||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)rSrj OBJ_nid2sndecode)rrrrBrBrCrsz_EllipticCurve.from_nidcCs||_||_||_dS)a :param _lib: The :py:mod:`cryptography` binding instance used to interface with OpenSSL. :param _nid: The OpenSSL NID identifying the curve this object represents. :type _nid: :py:class:`int` :param name: The OpenSSL short name identifying the curve this object represents. :type name: :py:class:`unicode` N)rL_nidru)rqrrrurBrBrCrrs z_EllipticCurve.__init__cCs d|jfS)Nz )rurprBrBrC__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. )rLZEC_KEY_new_by_curve_namerrSrXZ EC_KEY_free)rqkeyrBrBrC _to_EC_KEYsz_EllipticCurve._to_EC_KEY)r>r?r@rAr_PY2rrrrrrrrr __classcell__rBrBrrCr~s     rcCs 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. )rrrLrBrBrBrCr!s r!cCs,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!rura)ruZcurverBrBrCr"s   r"cseZdZdZddZfddZfddZdd ZeeZee Z ee Z ee Z ee Z ee Z d d Zd d ZddZddZZS)r#a An X.509 Distinguished Name. :ivar countryName: The country of the entity. :ivar C: Alias for :py:attr:`countryName`. :ivar stateOrProvinceName: The state or province of the entity. :ivar ST: Alias for :py:attr:`stateOrProvinceName`. :ivar localityName: The locality of the entity. :ivar L: Alias for :py:attr:`localityName`. :ivar organizationName: The organization name of the entity. :ivar O: Alias for :py:attr:`organizationName`. :ivar organizationalUnitName: The organizational unit of the entity. :ivar OU: Alias for :py:attr:`organizationalUnitName` :ivar commonName: The common name of the entity. :ivar CN: Alias for :py:attr:`commonName`. :ivar emailAddress: The e-mail address of the entity. cCs t|j}t|tj|_dS)z Create a new X509Name, copying the given X509Name instance. :param name: The name to copy. :type name: :py:class:`X509Name` N)rL X509_NAME_duprwrSrXX509_NAME_freertrBrBrCrr!s zX509Name.__init__c s|drtt|||St|turrL OBJ_txt2nid _byte_string NID_undefrrAttributeErrorrangeX509_NAME_entry_countrwX509_NAME_get_entryX509_NAME_ENTRY_get_object OBJ_obj2nidZX509_NAME_delete_entryZX509_NAME_ENTRY_freer^ _text_typeencodeZX509_NAME_add_entry_by_NIDZ MBSTRING_UTF8) rqruvaluerientZent_objZent_nid add_resultrrBrCr+s<           zX509Name.__setattr__c stt|}|tjkrFz tWnty4Yn0tt||St |j |d}|dkrbdSt |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... rNunsigned char**rr)rLrrrrrrr# __getattr__ZX509_NAME_get_index_by_NIDrwrX509_NAME_ENTRY_get_datarSrTZASN1_STRING_to_UTF8rVrYr OPENSSL_free) rqrurZ entry_indexentryrZr\Z data_lengthrrrBrCrRs*        zX509Name.__getattr__csfdd}|S)Ncs(t|tstSt|j|j}|dSNr)r^r#rrLZ X509_NAME_cmprw)rqrroprBrCf{s zX509Name._cmp..frB)rrrBrrC_cmpzs z X509Name._cmpcCsBtdd}t|j|t|}t|tjkdtt |fS)z6 String representation of an X509Name rJiz) rSrTrLZX509_NAME_onelinerwrUrVrW_nativerj)rqr\Z format_resultrBrBrCrs   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` )rLZX509_NAME_hashrwrprBrBrChashs 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)rSrTrLZ i2d_X509_NAMErwrVrYr)rqr\Z encode_resultrlrBrBrCders   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)rrLrrwrrrrrrSrYrkrirsrj) rqrrrfnameZfvalrrurrBrBrCget_componentss    zX509Name.get_components)r>r?r@rArrrrrrrrrrr rrrrrrBrBrrCr#s ' (  r#c@sdeZdZdZdddZeddZejdej dej d iZ d d Z d d Z ddZddZddZdS)r$z, An X.509 v3 certificate extension. NcCstd}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,)rSrTrLZX509V3_set_ctxrWZX509V3_set_ctx_nodbr^r&r`_x509 issuer_certZ subject_certZX509V3_EXT_nconfrrXX509_EXTENSION_free _extension)rqZ type_nameZcriticalrsubjectissuerctx extensionrBrBrCrrs"     zX509Extension.__init__cCstt|jSrK)rLrX509_EXTENSION_get_objectrrprBrBrCrs zX509Extension._nidZemailZDNSZURIc Cstdt|j}t|tj}g}tt|D]}t ||}z|j |j }Wn6t yt }t|||tt|Yq40tt|jjj|jjjdd}||d|q4d|S)NzGENERAL_NAMES*:z, )rSrhrLZX509V3_EXT_d2irrXZGENERAL_NAMES_freerZsk_GENERAL_NAME_numZsk_GENERAL_NAME_value _prefixesrKeyErrorr[ZGENERAL_NAME_printrsrr]rYdZia5rZlengthjoin)rqnamespartsrruZlabelrNrrBrBrC_subjectAltNameStrings$    z#X509Extension._subjectAltNameStringcCsDtj|jkr|St}t||jdd}t|dktt |S)zF :return: a nice text representation of the extension r) rLZNID_subject_alt_namerrr[X509V3_EXT_printrrVrr])rqrN print_resultrBrBrC__str__4s   zX509Extension.__str__cCs t|jS)zk Returns the critical field of this X.509 extension. :return: The critical field. )rLZX509_EXTENSION_get_criticalrrprBrBrC get_criticalAszX509Extension.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 )rLrrrrSrjr)rqobjrrBrBrCget_short_nameIs  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 rfN)rLX509_EXTENSION_get_datarrSrhrkrirY)rqZ octet_resultrlZ char_result result_lengthrBrBrCget_dataXs    zX509Extension.get_data)NN)r>r?r@rArrpropertyrrLZ GEN_EMAILZGEN_DNSZGEN_URIrrrrrrrBrBrBrCr$s =  r$c@steZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZdS)r%z0 An X.509 certificate signing requests. cCs&t}t|tj|_|ddSr)rLZ X509_REQ_newrSrX X509_REQ_free_req set_version)rqreqrBrBrCrrmszX509Req.__init__cCsddlm}t}|||jS)z Export as a ``cryptography`` certificate signing request. :rtype: ``cryptography.x509.CertificateSigningRequest`` .. versionadded:: 17.1.0 r)_CertificateSigningRequest))cryptography.hazmat.backends.openssl.x509rrFr)rqrrErBrBrCto_cryptographyss zX509Req.to_cryptographycCs&t|tjstd|}|j|_|S)a Construct based on a ``cryptography`` *crypto_req*. :param crypto_req: A ``cryptography`` X.509 certificate signing request :type crypto_req: ``cryptography.x509.CertificateSigningRequest`` :rtype: X509Req .. versionadded:: 17.1.0 z%Must be a certificate signing request)r^rZCertificateSigningRequestr`Z _x509_reqr)rZ crypto_reqrrBrBrCfrom_cryptographys zX509Req.from_cryptographycCs 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)rLZX509_REQ_set_pubkeyrrzrVrqr}rdrBrBrC 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__rLZX509_REQ_get_pubkeyrrzrVrSrWrXryr~r|rBrBrC get_pubkeys  zX509Req.get_pubkeycCst|j|}t|dkdS)z Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate request. :param int version: The version number. :return: ``None`` rN)rLZX509_REQ_set_versionrrV)rqversionrdrBrBrCrszX509Req.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` )rLZX509_REQ_get_versionrrprBrBrC 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#rrLZX509_REQ_get_subject_namerrwrVrSrW_ownerrtrBrBrC get_subjects zX509Req.get_subjectcCslt}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)rLZsk_X509_EXTENSION_new_nullrVrSrWrXZsk_X509_EXTENSION_freer^r$raZsk_X509_EXTENSION_pushrZX509_REQ_add_extensionsr)rq extensionsstackextrrBrBrCadd_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)rLZsk_X509_EXTENSION_pop_freerSZ addressofZ _original_lib)xrBrBrCs z(X509Req.get_extensions..)rLZX509_REQ_get_extensionsrrSrXrZsk_X509_EXTENSION_numr$rX509_EXTENSION_dupZsk_X509_EXTENSION_valuerrrs)rqZextsZnative_exts_objrrrrBrBrCget_extensionss     zX509Req.get_extensionscCs^|jrtd|jstdtt|}|tjkrr?r@rArrrrrrrrrrrrr8r9rBrBrBrCr%hs    r%c@seZdZdZddZeddZddZedd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Z d:d;Z!dd?Z#d@dAZ$dBS)Cr&z An X.509 certificate. cCs:t}t|tjkt|tj|_t|_ t|_ dSrK) rLZX509_newrVrSrWrX X509_freerrn_issuer_invalidator_subject_invalidator)rqrrBrBrCrr@s z X509.__init__cCs.||}t|tj|_t|_t|_|SrK) rrSrXrLrrrnrr)rrcertrBrBrC_from_raw_x509_ptrHs  zX509._from_raw_x509_ptrcCsddlm}t}|||jS)z Export as a ``cryptography`` certificate. :rtype: ``cryptography.x509.Certificate`` .. versionadded:: 17.1.0 r) _Certificate)rrrFr)rqrrErBrBrCrPs zX509.to_cryptographycCs&t|tjstd|}|j|_|S)z Construct based on a ``cryptography`` *crypto_cert*. :param crypto_key: A ``cryptography`` X.509 certificate. :type crypto_key: ``cryptography.x509.Certificate`` :rtype: X509 .. versionadded:: 17.1.0 zMust be a certificate)r^rZ Certificater`r)rZ crypto_certrrBrBrCr]s zX509.from_cryptographycCs$t|tstdt|j|dS)a  Set the version number of the certificate. Note that the version value is zero-based, eg. a value of 0 is V1. :param version: The version number of the certificate. :type version: :py:class:`int` :return: ``None`` zversion must be an integerN)r^rr`rLZX509_set_versionrrqrrBrBrCrps zX509.set_versioncCs t|jS)z Return the version number of the certificate. :return: The version number of the certificate. :rtype: :py:class:`int` )rLZX509_get_versionrrprBrBrCrszX509.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 rrLX509_get_pubkeyrrzrSrWrrXryr~r|rBrBrCrs  zX509.get_pubkeycCs2t|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)r^r r`rLZX509_set_pubkeyrrzrVrrBrBrCrs zX509.set_pubkeycCspt|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:`bytes` :return: :py:data:`None` rzKey only has public partr r rN)r^r r`r~rar{rLr rrSrWZ X509_signrrzrV)rqr}r Zevp_mdrrBrBrCr8s  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) rLZX509_get0_tbs_sigalgrr algorithmrrarSrjZ OBJ_nid2ln)rqZalgorrrBrBrCget_signature_algorithms   zX509.get_signature_algorithmcCstt|}|tjkr tdtdtj}tdd}t||d<t |j |||}t |dkd ddt ||dDS) a7 Return the digest of the X509 object. :param digest_name: The name of the digest algorithm to use. :type digest_name: :py:class:`bytes` :return: The digest of the object, formatted as :py:const:`b":"`-delimited hex pairs. :rtype: :py:class:`bytes` r unsigned char[]zunsigned int[]rr:cSsg|]}t|qSrB)rupper)rZchrBrBrC szX509.digest..)rLr rrSrWrarTZEVP_MAX_MD_SIZErUZ X509_digestrrVrrY)rqZ digest_namer r\rZ digest_resultrBrBrCr s      z X509.digestcCs t|jS)z Return the hash of the X509 subject. :return: The hash of the subject. :rtype: :py:class:`bytes` )rLZX509_subject_name_hashrrprBrBrCsubject_name_hashszX509.subject_name_hashcCst|tstdt|dd}t|ts6|d}td}t ||}|dtj krzt t |j |}|rtnXt|dtj }t|d|tj krtt|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^_integer_typesr`hexr_rrSrTrL BN_hex2bnrWZASN1_INTEGER_setX509_get_serialNumberrrBN_to_ASN1_INTEGERrrXASN1_INTEGER_freeZX509_set_serialNumberrV)rqserial hex_serial bignum_serialZ small_serialrd asn1_serialrBrBrCset_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) rLr%rZASN1_INTEGER_to_BNrSrWZ BN_bn2hexrjrrr)rqr+r*r)Zhexstring_serialr(rBrBrCget_serial_number#s      zX509.get_serial_numbercCs.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^rr`rLX509_getm_notAfterrX509_gmtime_adj)rqamountZnotAfterrBrBrCgmtime_adj_notAfter7s  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^rr`rLX509_getm_notBeforerr1)rqr2Z notBeforerBrBrCgmtime_adj_notBeforeEs  zX509.gmtime_adj_notBeforecCs(t|}tj|d}|tjkS)z Check whether the certificate has expired. :return: ``True`` if the certificate has expired, ``False`` otherwise. :rtype: bool z %Y%m%d%H%M%SZ)r get_notAfterdatetimestrptimeZutcnow)rqZ time_stringZ not_afterrBrBrC has_expiredRs zX509.has_expiredcCst||jSrK)rmr)rqwhichrBrBrC_get_boundary_time^szX509._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 )r;rLr4rprBrBrC get_notBeforeas zX509.get_notBeforecCst||j|SrK)rerrqr:rcrBrBrC_set_boundary_timenszX509._set_boundary_timecCs|tj|S)z Set the timestamp at which the certificate starts being valid. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ :param bytes when: A timestamp string. :return: ``None`` )r>rLr4rqrcrBrBrC set_notBeforeqs 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 )r;rLr0rprBrBrCr6~s zX509.get_notAftercCs|tj|S)z Set the timestamp at which the certificate stops being valid. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ :param bytes when: A timestamp string. :return: ``None`` )r>rLr0r?rBrBrC set_notAfters zX509.set_notAftercCs0tt}||j|_t|jtjk||_|SrK)r#rrrwrVrSrWr)rqr:rurBrBrC _get_names   zX509._get_namecCs0t|tstd||j|j}t|dkdS)Nzname must be an X509Namer)r^r#r`rrwrV)rqr:rurdrBrBrC _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` )rBrLZX509_get_issuer_namerrvrtrBrBrC get_issuers  zX509.get_issuercCs|tj||jdS)z Set the issuer of this certificate. :param issuer: The issuer. :type issuer: :py:class:`X509Name` :return: ``None`` N)rCrLZX509_set_issuer_namerrx)rqrrBrBrC 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` )rBrLX509_get_subject_namerrvrtrBrBrCrs  zX509.get_subjectcCs|tj||jdS)z Set the subject of this certificate. :param subject: The subject. :type subject: :py:class:`X509Name` :return: ``None`` N)rCrLZX509_set_subject_namerrx)rqrrBrBrC 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 )rLZX509_get_ext_countrrprBrBrCget_extension_counts zX509.get_extension_countcCs<|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`` rrN)r^r$rarLZ X509_add_extrrr)rqrrrrBrBrCrs  zX509.add_extensionscCsNtt}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$rrLZ X509_get_extrrrSrW IndexErrorrrXr)rqindexrrrBrBrC get_extensions   zX509.get_extensionN)%r>r?r@rArrrrrrrrrrr8rr rr,r.r3r5r9r;r<r>r@r6rArBrCrDrErrGrHrrKrBrBrBrCr&;sF     (          r&c@sReZdZdZejZejZej Z ej Z ej ZejZejZejZejZejZejZdS)r'a  Flags for X509 verification, used to change the behavior of :class:`X509Store`. See `OpenSSL Verification Flags`_ for details. .. _OpenSSL Verification Flags: https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_flags.html N)r>r?r@rArLZX509_V_FLAG_CRL_CHECKZ CRL_CHECKZX509_V_FLAG_CRL_CHECK_ALLZ CRL_CHECK_ALLZX509_V_FLAG_IGNORE_CRITICALZIGNORE_CRITICALZX509_V_FLAG_X509_STRICTZ X509_STRICTZX509_V_FLAG_ALLOW_PROXY_CERTSZALLOW_PROXY_CERTSZX509_V_FLAG_POLICY_CHECKZ POLICY_CHECKZX509_V_FLAG_EXPLICIT_POLICYZEXPLICIT_POLICYZX509_V_FLAG_INHIBIT_MAPZ INHIBIT_MAPZX509_V_FLAG_NOTIFY_POLICYZ NOTIFY_POLICYZX509_V_FLAG_CHECK_SS_SIGNATUREZCHECK_SS_SIGNATUREZX509_V_FLAG_CB_ISSUER_CHECKZCB_ISSUER_CHECKrBrBrBrCr's r'c@sBeZdZdZddZddZddZdd Zd d Zdd dZ d S)r(a An X.509 store. An X.509 store is used to describe a context in which to verify a certificate. A description of a context may include a set of certificates to trust, a set of certificate revocation lists, verification flags and more. An X.509 store, being only a description, cannot be used by itself to verify a certificate. To carry out the actual verification process, see :class:`X509StoreContext`. cCst}t|tj|_dSrK)rLZX509_STORE_newrSrXZX509_STORE_free_storerqstorerBrBrCrr:szX509Store.__init__cCs0t|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&r`rLZX509_STORE_add_certrLrrV)rqrrrBrBrCadd_cert>s zX509Store.add_certcCstt|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)rVrLZX509_STORE_add_crlrL_crlrqcrlrBrBrCadd_crlTszX509Store.add_crlcCstt|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)rVrLZX509_STORE_set_flagsrL)rqflagsrBrBrC set_flagsdszX509Store.set_flagscCsFt}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) rLZX509_VERIFY_PARAM_newrSrXZX509_VERIFY_PARAM_freeZX509_VERIFY_PARAM_set_timecalendarZtimegmZ timetuplerVZX509_STORE_set1_paramrL)rqZvfy_timeZparamrBrBrCset_time|s zX509Store.set_timeNcCsN|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)rSrW _path_stringrLZX509_STORE_load_locationsrLr)rqZcafileZcapathZ load_resultrBrBrCload_locationsszX509Store.load_locations)N) r>r?r@rArrrOrSrUrWrYrBrBrBrCr(,s r(cs eZdZdZfddZZS)r)z An exception raised when an error occurred while verifying a certificate using `OpenSSL.X509StoreContext.verify_certificate`. :ivar certificate: The certificate which caused verificate failure. :type certificate: :class:`X509` cstt||||_dSrK)rr)rr certificate)rqmessagerZrrBrCrrszX509StoreContextError.__init__)r>r?r@rArrrrBrBrrCr)sr)c@sVeZdZdZdddZeddZddZd d Zd d Z d dZ ddZ ddZ dS)r*a An X.509 store context. An X.509 store context is used to carry out the actual verification process of a certificate in a described context. For describing such a context, see :class:`X509Store`. :ivar _store_ctx: The underlying X509_STORE_CTX structure used by this instance. It is dynamically allocated and automatically garbage collected. :ivar _store: See the ``store`` ``__init__`` parameter. :ivar _cert: See the ``certificate`` ``__init__`` parameter. :ivar _chain: See the ``chain`` ``__init__`` parameter. :param X509Store store: The certificates which will be trusted for the purposes of any verifications. :param X509 certificate: The certificate to be verified. :param chain: List of untrusted certificates that may be used for building the certificate chain. May be ``None``. :type chain: :class:`list` of :class:`X509` NcCs<t}t|tj|_||_||_|||_ | dSrK) rLZX509_STORE_CTX_newrSrXZX509_STORE_CTX_free _store_ctxrL_cert_build_certificate_stack_chain_init)rqrNrZchainZ store_ctxrBrBrCrrs  zX509StoreContext.__init__cCsdd}|dust|dkr"tjSt}t|tjkt||}|D]N}t|ts^t dtt |j dkt ||j dkrHt |j tqH|S)NcSs8tt|D]}t||}t|qt|dSrK)rrL sk_X509_num sk_X509_valuer sk_X509_free)srrrBrBrCcleanups  z:X509StoreContext._build_certificate_stack..cleanuprz+One of the elements is not an X509 instance)rUrSrWrLsk_X509_new_nullrVrXr^r&r`Z X509_up_refr sk_X509_pushrr)Z certificatesrfrrrBrBrCr^s   z)X509StoreContext._build_certificate_stackcCs.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)rLZX509_STORE_CTX_initr\rLr]rr_rrqretrBrBrCr`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)rLZX509_STORE_CTX_cleanupr\rprBrBrC_cleanupszX509StoreContext._cleanupc Cs\t|jt|jtttt|jg}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. ) rLZX509_STORE_CTX_get_errorr\ZX509_STORE_CTX_get_error_depthrrSrjZX509_verify_cert_error_stringZX509_STORE_CTX_get_current_certZX509_dupr&rr))rqerrorsrr]pycertrBrBrC_exception_from_context$s      z(X509StoreContext._exception_from_contextcCs ||_dS)z Set the context's X.509 store. .. versionadded:: 0.15 :param X509Store store: The store description which will be used for the purposes of any *future* verifications. N)rLrMrBrBrC set_store>s zX509StoreContext.set_storecCs8||t|j}||dkr4|dS)a" Verify a certificate in a context. .. versionadded:: 0.15 :raises X509StoreContextError: If an error occurred when validating a certificate in the context. Sets ``certificate`` attribute to indicate which certificate caused the error. rN)rkr`rLX509_verify_certr\rnrirBrBrCverify_certificateIs  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)rkr`rLrpr\rnZX509_STORE_CTX_get1_chainrVrSrWrrbrcr&rrsrd)rqrjZ cert_stackrrrrmrBrBrCget_verified_chain_s"      z#X509StoreContext.get_verified_chain)N) r>r?r@rArr staticmethodr^r`rkrnrorqrrrBrBrBrCr*s    r*cCsvt|tr|d}t|}|tkr.rr)rr^r_r`rreplace _crl_reasonsrJrLZASN1_ENUMERATED_newrVrSrWrXZASN1_ENUMERATED_freeZASN1_ENUMERATED_setZX509_REVOKED_add1_ext_i2dr~r)rqreasonZ reason_codeZnew_reason_extrdrrBrBrC set_reasonds     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)rrLrr~rrrrr[rZM_ASN1_OCTET_STRING_printrrVr])rqrrrrNrrBrBrC 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)rrprBrBrC all_reasonss zRevoked.all_reasonscCst|j}t||S)z Set the revocation timestamp. :param bytes when: The timestamp of the revocation, as ASN.1 TIME. :return: ``None`` )rL X509_REVOKED_get0_revocationDater~re)rqrcdtrBrBrC 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 )rLrr~rm)rqrrBrBrC get_rev_dates zRevoked.get_rev_dateN)r>r?r@rArrrrrrrrrrrrBrBrBrCr/s  %  r/c@s|eZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZedefddZdS)r0z( A certificate revocation list. cCst}t|tj|_dSrK)rLZ X509_CRL_newrSrX X509_CRL_freerPrQrBrBrCrrsz CRL.__init__cCsddlm}t}|||jS)z Export as a ``cryptography`` CRL. :rtype: ``cryptography.x509.CertificateRevocationList`` .. versionadded:: 17.1.0 r)_CertificateRevocationList)rrrFrP)rqrrErBrBrCrs zCRL.to_cryptographycCs&t|tjstd|}|j|_|S)a Construct based on a ``cryptography`` *crypto_crl*. :param crypto_crl: A ``cryptography`` certificate revocation list :type crypto_crl: ``cryptography.x509.CertificateRevocationList`` :rtype: CRL .. versionadded:: 17.1.0 z%Must be a certificate revocation list)r^rZCertificateRevocationListr`Z _x509_crlrP)rZ crypto_crlrRrBrBrCrs zCRL.from_cryptographycCsng}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)rLZX509_CRL_get_REVOKEDrPrZsk_X509_REVOKED_numZsk_X509_REVOKED_valueCryptography_X509_REVOKED_dupr/rrSrXr}r~rstuple)rqZresultsZ revoked_stackrrZ revoked_copyZpyrevrBrBrC get_revokeds      zCRL.get_revokedcCs8t|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)rLrr~rVrSrWZX509_CRL_add0_revokedrP)rqrcopyrrBrBrC 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 ) rLrZX509_CRL_get_issuerrPrVrSrWrXrr#rrw)rqZ_issuerrrBrBrCrD s  zCRL.get_issuercCstt|j|dkdS)z Set the CRL version. .. versionadded:: 16.1.0 :param int version: The version of the CRL. :return: ``None`` rN)rVrLZX509_CRL_set_versionrPrrBrBrCr& s zCRL.set_versioncCst||j|SrK)rerPr=rBrBrCr>1 szCRL._set_boundary_timecCs|tj|S)z Set when the CRL was last updated. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ .. versionadded:: 16.1.0 :param bytes when: A timestamp string. :return: ``None`` )r>rLZX509_CRL_get_lastUpdater?rBrBrCset_lastUpdate4 s zCRL.set_lastUpdatecCs|tj|S)z Set when the CRL will next be updated. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ .. versionadded:: 16.1.0 :param bytes when: A timestamp string. :return: ``None`` )r>rLZX509_CRL_get_nextUpdater?rBrBrCset_nextUpdateC s zCRL.set_nextUpdatecCs\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) rLr rVrSrWX509_CRL_set_issuer_namerPrFrZ X509_CRL_sort X509_CRL_signrz)rqrZ issuer_keyr rrrBrBrCr8R s  zCRL.signdc 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 providedr r<)r^r&r`r r _UNSPECIFIEDrLr rSrWrarQrRrVZ ASN1_TIME_newr1ZX509_CRL_set_lastUpdaterPZX509_CRL_set_nextUpdaterrFrrrzrr:) rqrrrZdaysr rrNZsometimerrBrBrCexportl s4      z CRL.exportN)r>r?r@rArrrrrrrrDrr>rrr8rrrrBrBrBrCr0s  r0c@s4eZdZddZddZddZddZd d Zd S) r1cCstt|jS)zm Check if this NID_pkcs7_signed object :return: True if the PKCS7 is of type signed )boolrLZPKCS7_type_is_signed_pkcs7rprBrBrCtype_is_signed szPKCS7.type_is_signedcCstt|jS)zt Check if this NID_pkcs7_enveloped object :returns: True if the PKCS7 is of type enveloped )rrLZPKCS7_type_is_envelopedrrprBrBrCtype_is_enveloped szPKCS7.type_is_envelopedcCstt|jS)z Check if this NID_pkcs7_signedAndEnveloped object :returns: True if the PKCS7 is of type signedAndEnveloped )rrLZ PKCS7_type_is_signedAndEnvelopedrrprBrBrCtype_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 )rrLZPKCS7_type_is_datarrprBrBrC 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 )rLrrrrrSrj)rqrZ string_typerBrBrC get_type_name s zPKCS7.get_type_nameN)r>r?r@rrrrrrBrBrBrCr1 s r1c@sbeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dddZ dS)r2z A PKCS #12 archive. cCsd|_d|_d|_d|_dSrK)rzr]_cacerts _friendlynamerprBrBrCrr 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]rprBrBrCget_certificate szPKCS12.get_certificatecCst|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&r`r])rqrrBrBrCset_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` )rzrprBrBrCget_privatekey szPKCS12.get_privatekeycCst|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)r^r r`rzr|rBrBrCset_privatekey s zPKCS12.set_privatekeycCs|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)rrrprBrBrCget_ca_certificates s zPKCS12.get_ca_certificatescCs>|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&r`)rqcacertsrrBrBrCset_ca_certificates s  zPKCS12.set_ca_certificatescCs2|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))rr^r_r`rtrBrBrCset_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` )rrprBrBrCget_friendlyname7 szPKCS12.get_friendlynameNrc 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_warnrrSrWrLrgrXrdrhrrrzr]Z PKCS12_createZ&NID_pbe_WithSHA1And3_Key_TripleDES_CBCr PKCS12_freer[Zi2d_PKCS12_bior]) rqr{iterZmaciterrr friendlynamer}pkcs12rNrBrBrCr@ sH       z PKCS12.export)Nrr)r>r?r@rArrrrrrrrrrrrBrBrBrCr2 s      r2c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)r3z! A Netscape SPKI object. cCst}t|tj|_dSrK)rLZNETSCAPE_SPKI_newrSrXZNETSCAPE_SPKI_free_spki)rqZspkirBrBrCrr szNetscapeSPKI.__init__cCs^|jrtd|jstdtt|}|tjkrr?r@rArrr8r9rrrrBrBrBrCr3 s r3c@sBeZdZd ddZeddZeddZefdd Zd d Z d S)rwFcCs4|tkr|durtd||_||_||_g|_dS)Nz0only FILETYPE_PEM key format supports encryption)rra _passphrase _more_args _truncate _problems)rqrr{Z more_argstruncaterBrBrCrr sz_PassphraseHelper.__init__cCs@|jdurtjSt|jts&t|jr4td|jStddS)NZpem_password_cb2Last argument must be a byte string or a callable.) rrSrWr^r_callablerx_read_passphraser`rprBrBrCrx s z_PassphraseHelper.callbackcCs8|jdurtjSt|jts&t|jr,tjStddS)Nr)rrSrWr^r_rr`rprBrBrCry s z_PassphraseHelper.callback_argscCs6|jr2z t|Wn|y$Yn0|jddSr)r_exception_from_error_queuepop)rqZ exceptionTyperBrBrCrz s   z"_PassphraseHelper.raise_if_problemc Cszt|jr.|jr"||||}q4||}n|j}t|tsFtdt||krn|jrf|d|}ntdtt|D]}|||d||<qzt|WSt y}z|j |WYd}~dSd}~00dS)NzBytes expectedz+passphrase returned by callback is too longrr) rrrr^r_rarUrr Exceptionrrs)rqZbufsizeZrwflagZuserdatarrerBrBrCr s(      z"_PassphraseHelper._read_passphraseN)FF) r>r?r@rrrrxryrrzrrBrBrBrCrw s   rwcCst|tr|d}t|}|tkrDeprecationWarningr=ZOpenSSL_add_all_algorithmsZSSL_load_error_stringsZ ASN1_STRING_set_default_mask_ascrBrBrBrCs   , '    )  iIT\6 @.^,6ZF" %#$ W