3 F\$@sddlZddlmZddlmZddlmZmZmZm Z m Z m Z ddl m ZmZmZddlmZddlmZmZddlmZdd lmZmZmZ m!Z"m#Z$m%Z&m'Z(m)Z*d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-g$Z+ej,Z-ej.Z/dnZ0ej1Z2ej3Z4ej5Z6ej7Z8Gd1dde9Z:ee e:Z;e*e:Zdod6d7Z?d8d9Z@d:d;ZAdd?d?eCZDGd@ddeCZEeeEeFdAeGZHGdBdCdCeCZIdDdZJdEdZKGdFddeCZLeeLeFdGeGZMGdHddeCZNeeNeFdIeGZOGdJddeCZPeePeFdKeGZQGdLddeCZReeReFdMeGZSGdNddeCZTGdOddeCZUeeUeFdPeGZVGdQdde9ZWGdRddeCZXdSdZYdTdZZdUdZ[dpdVdZ\GdWddeCZ]GdXd d eCZ^ee^eFdYeGZ_GdZd!d!eCZ`ee`eFd[eGZaGd\d"d"eCZbeebeFd]eGZcGd^d#d#eCZdeedeFd_eGZeGd`dadaeCZfdbd$Zgdqdcd%Zhddd&Zided'Zjdfd(Zkdgd)Zldhd*Zmdid+Zndjd,Zodrdkd-ZpejqejrejsdldS)sN) b16encode)partial)__eq____ne____lt____le____gt____ge__) integer_types text_typePY3)x509)dsarsa) deprecated)ffilibexception_from_error_queue byte_stringnative 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_pkcs12c@seZdZdZdS)rz7 An error occurred in an `OpenSSL.crypto` API. N)__name__ __module__ __qualname____doc__rDrD/usr/lib/python3.6/crypto.pyrNscCsddlm}|S)ap Importing the backend from cryptography has the side effect of activating the osrandom engine. This mutates the global state of OpenSSL in the process and causes issues for various programs that use subinterpreters or embed Python. By putting the import in this function we can avoid triggering this side effect unless _get_backend is called. r)backend)Z,cryptography.hazmat.backends.openssl.backendrF)rFrDrDrE _get_backendXs rGcCstd|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)whererDrDrE_untested_errordsrJcCsd|dkrtjtj}tj}n(tjd|}tj|t|}|fdd}t|tj ktj ||}|S)z Allocate a new OpenSSL memory BIO. Arrange for the garbage collector to clean it up automatically. :param buffer: None or some bytes to use to put into the BIO so that they can be read out. Nzchar[]cSs tj|S)N)_libBIO_free)biorefrDrDrEfree~sz_new_mem_buf..free) rKBIO_new BIO_s_memrL_ffinewZBIO_new_mem_buflen_openssl_assertNULLgc)bufferrMrOdatarDrDrE _new_mem_bufms    rZcCs.tjd}tj||}tj|d|ddS)zO Copy the contents of an OpenSSL BIO object into a Python byte string. zchar**rN)rRrSrKZBIO_get_mem_datarX)rM result_bufferZ buffer_lengthrDrDrE_bio_to_strings  r\cCs2t|tstdtj||}|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 TypeErrorrKZASN1_TIME_set_string ValueError)boundarywhen set_resultrDrDrE_set_asn1_times   rdcCstjd|}tj|dkrdStj|tjkr>tjtj|Stjd}tj |||dtj krlt dn6tjd|d}tj|}tj|}tj |d|SdS)a] Retrieve the time value of an ASN1 time object. @param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to that type) from which the time value will be retrieved. @return: The time value from C{timestamp} as a L{bytes} string in a certain format. Or C{None} if the object contains no time value. z ASN1_STRING*rNzASN1_GENERALIZEDTIME**ASN1_TIME_to_generalizedtime) rRcastrKASN1_STRING_lengthZASN1_STRING_typeZV_ASN1_GENERALIZEDTIMEstringASN1_STRING_datarSrerVrJZASN1_GENERALIZEDTIME_free)Z timestampZstring_timestampZgeneralized_timestampZ string_data string_resultrDrDrE_get_asn1_times       rkc@s$eZdZddZddZddZdS)_X509NameInvalidatorcCs g|_dS)N)_names)selfrDrDrE__init__sz_X509NameInvalidator.__init__cCs|jj|dS)N)rmappend)rnnamerDrDrEaddsz_X509NameInvalidator.addcCsx|jD]}|`qWdS)N)rm_name)rnrqrDrDrEclears z_X509NameInvalidator.clearN)r@rArBrorrrtrDrDrDrErlsrlc@sTeZdZdZdZdZddZddZedd Z d d Z d d Z ddZ ddZ dS)rzD A class representing an DSA or RSA public key or key pair. FTcCs"tj}tj|tj|_d|_dS)NF)rKZ EVP_PKEY_newrRrW EVP_PKEY_free_pkey _initialized)rnpkeyrDrDrErosz PKey.__init__cCs(t}|jr|j|jS|j|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)rG _only_publicZ_evp_pkey_to_public_keyrvZ_evp_pkey_to_private_key)rnrFrDrDrEto_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_pkeyrvryrw)clsZ crypto_keyrxrDrDrEfrom_cryptography_keys  zPKey.from_cryptography_keycCs6t|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`` ztype must be an integerzbits must be an integerrzInvalid number of bitsr?zNo such key typeTN)r]intr_rr`rKBN_newrRrWBN_freeZ BN_set_wordZRSA_F4ZRSA_newZRSA_generate_key_exrVrUZEVP_PKEY_assign_RSArvrZDSA_newZDSA_freeZDSA_generate_parameters_exZDSA_generate_keyZEVP_PKEY_set1_DSArrw)rntypebitsZexponentrresultrresrDrDrE generate_keys4     zPKey.generate_keycCs`|jrtdtj|jtjkr*tdtj|j}tj |tj }tj |}|rVdSt 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 unsupportedTN) ryr_rKZ EVP_PKEY_typer EVP_PKEY_RSAEVP_PKEY_get1_RSArvrRrWRSA_freeZ RSA_check_key_raise_current_error)rnrrrDrDrEcheckGs   z PKey.checkcCs tj|jS)zT Returns the type of the key :return: The type of the key. )rK EVP_PKEY_idrv)rnrDrDrErasz PKey.typecCs tj|jS)zh Returns the number of bits of the key :return: The number of bits of the key. )rKZ EVP_PKEY_bitsrv)rnrDrDrErisz PKey.bitsN)r@rArBrCryrwrorz classmethodr|rrrrrDrDrDrErs 8z.PKeyType has been deprecated, use PKey insteadcsdeZdZdZdZer 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|j|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)rnother) __class__rDrErs z_EllipticCurve.__ne__cs>jtjd}tjd|}j||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|]}j|jVqdS)N)from_nidnid).0c)r{rrDrE sz7_EllipticCurve._load_elliptic_curves..)ZEC_get_builtin_curvesrRrVrSset)r{rZ num_curvesZbuiltin_curvesrD)r{rrE_load_elliptic_curvess    z$_EllipticCurve._load_elliptic_curvescCs|jdkr|j||_|jS)a Get, cache, and return the curves supported by OpenSSL. :param lib: The OpenSSL library binding object. :return: A :py:type:`set` of ``cls`` instances giving the names of the elliptic curves the underlying library supports. N)_curvesr)r{rrDrDrE_get_elliptic_curvess  z#_EllipticCurve._get_elliptic_curvescCs|||tj|j|jdS)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)rRrh OBJ_nid2sndecode)r{rrrDrDrErsz_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)rK_nidrq)rnrrrqrDrDrEros z_EllipticCurve.__init__cCs d|jfS)Nz )rq)rnrDrDrE__repr__sz_EllipticCurve.__repr__cCs|jj|j}tj|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. )rKZEC_KEY_new_by_curve_namerrRrWZ EC_KEY_free)rnkeyrDrDrE _to_EC_KEYsz_EllipticCurve._to_EC_KEY)r@rArBrCr_PY3rrrrrrorr __classcell__rDrD)rrErys   rcCs tjtS)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. )rrrKrDrDrDrEr s cCs,xtD]}|j|kr|SqWtd|dS)aT Return a single curve object selected by name. See :py:func:`get_elliptic_curves` for information about curve objects. :param name: The OpenSSL short name identifying the curve object to retrieve. :type name: :py:class:`unicode` If the named curve is not supported then :py:class:`ValueError` is raised. zunknown curve nameN)r rqr`)rqZcurverDrDrEr!s  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 tj|j}tj|tj|_dS)z Create a new X509Name, copying the given X509Name instance. :param name: The name to copy. :type name: :py:class:`X509Name` N)rK X509_NAME_duprsrRrWX509_NAME_free)rnrqrDrDrEros zX509Name.__init__c s|jdrtt|j||St|tk r.frD)rrrD)rrE_cmpss z X509Name._cmpcCsBtjdd}tj|j|t|}t|tjkdttj |fS)z6 String representation of an X509Name zchar[]iz) rRrSrKZX509_NAME_onelinersrTrUrV_nativerh)rnr[Z format_resultrDrDrErs  zX509Name.__repr__cCs tj|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` )rKZX509_NAME_hashrs)rnrDrDrEhashs z X509Name.hashcCsNtjd}tj|j|}t|dktj|d|dd}tj|d|S)z Return the DER encoding of this name. :return: The DER encoded form of this name. :rtype: :py:class:`bytes` zunsigned char**rN)rRrSrKZ i2d_X509_NAMErsrUrXr)rnr[Z encode_resultrjrDrDrEders   z X509Name.derc Csg}xttj|jD]n}tj|j|}tj|}tj|}tj|}tj|}t j tj |tj |dd}|j t j||fqW|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)rrKrrsrrrrrrRrXrirgrprh) rnrrrZfnameZfvalrrqrrDrDrEget_componentss     zX509Name.get_components)r@rArBrCrorrrrrrrrr rrrrrrDrD)rrEr"s $ (  z6X509NameType has been deprecated, use X509Name insteadc@sdeZdZdZdddZeddZejdej dej d iZ d d Z d d Z ddZddZddZdS)r#z, An X.509 v3 certificate extension. NcCstjd}tj|tjtjtjtjdtj||dk rRt|tsJtd|j |_ |dk rtt|tsltd|j |_ |rd|}tj tj|||}|tjkrt tj|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,)rRrSrKZX509V3_set_ctxrVZX509V3_set_ctx_nodbr]r%r__x509 issuer_certZ subject_certZX509V3_EXT_nconfrrWX509_EXTENSION_free _extension)rnZ type_nameZcriticalrsubjectissuerZctx extensionrDrDrEros"     zX509Extension.__init__cCstjtj|jS)N)rKrX509_EXTENSION_get_objectr)rnrDrDrErszX509Extension._nidZemailZDNSZURIc Cstjdtj|j}tj|tj}g}xttj|D]}tj ||}y|j |j }Wn8t k rt }tj|||jtt|Yq6Xttj|jjj|jjjdd}|j|d|q6Wdj|S)NzGENERAL_NAMES*:z, )rRrfrKZX509V3_EXT_d2irrWZGENERAL_NAMES_freerZsk_GENERAL_NAME_numZsk_GENERAL_NAME_value _prefixesrKeyErrorrZZGENERAL_NAME_printrprr\rXdZia5rYlengthjoin)rnnamespartsrrqZlabelrMrrDrDrE_subjectAltNameStrings   "z#X509Extension._subjectAltNameStringcCsDtj|jkr|jSt}tj||jdd}t|dktt |S)zF :return: a nice text representation of the extension r) rKZNID_subject_alt_namerrrZX509V3_EXT_printrrUrr\)rnrM print_resultrDrDrE__str__/s   zX509Extension.__str__cCs tj|jS)zk Returns the critical field of this X.509 extension. :return: The critical field. )rKZX509_EXTENSION_get_criticalr)rnrDrDrE get_critical<szX509Extension.get_criticalcCs&tj|j}tj|}tjtj|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 )rKrrrrRrhr)rnobjrrDrDrEget_short_nameDs  zX509Extension.get_short_namecCs@tj|j}tjd|}tj|}tj|}tj||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 z ASN1_STRING*N)rKX509_EXTENSION_get_datarrRrfrirgrX)rnZ octet_resultrjZ char_result result_lengthrDrDrEget_dataSs    zX509Extension.get_data)NN)r@rArBrCropropertyrrKZ GEN_EMAILZGEN_DNSZGEN_URIrrrrrrrDrDrDrEr#s =   z@X509ExtensionType has been deprecated, use X509Extension insteadc@steZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZdS)r$z0 An X.509 certificate signing requests. cCs&tj}tj|tj|_|jddS)Nr)rKZ X509_REQ_newrRrW X509_REQ_free_req set_version)rnreqrDrDrErooszX509Req.__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.x509rrGr)rnrrFrDrDrEto_cryptographyus 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]r ZCertificateSigningRequestr_Z _x509_reqr)r{Z crypto_reqrrDrDrEfrom_cryptographys zX509Req.from_cryptographycCs tj|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`` r?N)rKZX509_REQ_set_pubkeyrrvrU)rnrxrcrDrDrE set_pubkeys zX509Req.set_pubkeycCsDtjt}tj|j|_t|jtjktj |jtj |_d|_ |S)z Get the public key of the certificate signing request. :return: The public key. :rtype: :py:class:`PKey` T) r__new__rKZX509_REQ_get_pubkeyrrvrUrRrVrWrury)rnrxrDrDrE get_pubkeys  zX509Req.get_pubkeycCstj|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`` r?N)rKZX509_REQ_set_versionrrU)rnversionrcrDrDrErszX509Req.set_versioncCs tj|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` )rKZX509_REQ_get_versionr)rnrDrDrE get_versionszX509Req.get_versioncCs2tjt}tj|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"rrKZX509_REQ_get_subject_namerrsrUrRrV_owner)rnrqrDrDrE get_subjects zX509Req.get_subjectcCsptj}t|tjktj|tj}x,|D]$}t|ts@t dtj ||j q*Wtj |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`` z+One of the elements is not an X509Extensionr?N)rKZsk_X509_EXTENSION_new_nullrUrRrVrWZsk_X509_EXTENSION_freer]r#r`Zsk_X509_EXTENSION_pushrZX509_REQ_add_extensionsr)rn extensionsstackextrrDrDrEadd_extensionss  zX509Req.add_extensionscCsNg}tj|j}x8ttj|D]&}tjt}tj|||_|j |q W|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 ) rKZX509_REQ_get_extensionsrrZsk_X509_EXTENSION_numr#rZsk_X509_EXTENSION_valuerrp)rnZextsZnative_exts_objrrrDrDrEget_extensionss   zX509Req.get_extensionscCs^|jrtd|jstdtjt|}|tjkrd?Z#d@dAZ$dBS)Cr%z An X.509 certificate. cCs:tj}t|tjktj|tj|_t|_ t|_ dS)N) rKZX509_newrUrRrVrW X509_freerrl_issuer_invalidator_subject_invalidator)rnr rDrDrEro<s z X509.__init__cCs.|j|}tj|tj|_t|_t|_|S)N) rrRrWrKrrrlrr)r{r certrDrDrE_from_raw_x509_ptrDs  zX509._from_raw_x509_ptrcCsddlm}t}|||jS)z Export as a ``cryptography`` certificate. :rtype: ``cryptography.x509.Certificate`` .. versionadded:: 17.1.0 r) _Certificate)rrrGr)rnrrFrDrDrErLs 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]r Z Certificater_r)r{Z crypto_certrrDrDrErXs zX509.from_cryptographycCs$t|tstdtj|j|dS)a  Set the version number of the certificate. Note that the version value is zero-based, eg. a value of 0 is V1. :param version: The version number of the certificate. :type version: :py:class:`int` :return: ``None`` zversion must be an integerN)r]r}r_rKZX509_set_versionr)rnrrDrDrErks zX509.set_versioncCs tj|jS)z Return the version number of the certificate. :return: The version number of the certificate. :rtype: :py:class:`int` )rKZX509_get_versionr)rnrDrDrErzszX509.get_versioncCsFtjt}tj|j|_|jtjkr*ttj |jtj |_d|_ |S)z{ Get the public key of the certificate. :return: The public key. :rtype: :py:class:`PKey` T) rrrKX509_get_pubkeyrrvrRrVrrWrury)rnrxrDrDrErs  zX509.get_pubkeycCs2t|tstdtj|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` zpkey must be a PKey instancer?N)r]rr_rKZX509_set_pubkeyrrvrU)rnrxrcrDrDrErs zX509.set_pubkeycCspt|tstd|jr td|js.tdtjt|}|t j krNtdtj |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` zpkey must be a PKey instancezKey only has public partzKey is uninitializedzNo such digest methodrN)r]rr_ryr`rwrKrrrRrVZ X509_signrrvrU)rnrxrZevp_mdrrDrDrEr7s  z X509.signcCs:tj|j}tj|j}|tjkr*tdtjtj |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) rKZX509_get0_tbs_sigalgrr algorithmrr`rRrhZ OBJ_nid2ln)rnZalgorrrDrDrEget_signature_algorithms   zX509.get_signature_algorithmcCstjt|}|tjkr tdtjdtj}tjdd}t||d<tj |j |||}t |dkdj ddtj ||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` zNo such digest methodzunsigned char[]zunsigned int[]r?r:cSsg|]}t|jqSrD)rupper)rZchrDrDrE szX509.digest..)rKrrrRrVr`rSZEVP_MAX_MD_SIZErTZ X509_digestrrUrrX)rnZ digest_namerr[rZ digest_resultrDrDrErs     z X509.digestcCs tj|jS)z Return the hash of the X509 subject. :return: The hash of the subject. :rtype: :py:class:`bytes` )rKZX509_subject_name_hashr)rnrDrDrEsubject_name_hashszX509.subject_name_hashcCst|tstdt|dd}t|ts6|jd}tjd}tj ||}|dtj krztj tj |j |}|rtnXtj|dtj }tj|d|tj krttj|tj}tj|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 integerr=NrzBIGNUM**rr?)r]_integer_typesr_hexr^rrRrSrK BN_hex2bnrVZASN1_INTEGER_setX509_get_serialNumberrrBN_to_ASN1_INTEGERrrWASN1_INTEGER_freeZX509_set_serialNumberrU)rnserial hex_serial bignum_serialZ small_serialrc asn1_serialrDrDrEset_serial_numbers&      zX509.set_serial_numbercCs`tj|j}tj|tj}z4tj|}ztj|}t|d}|Stj |XWdtj |XdS)zx Return the serial number of this certificate. :return: The serial number. :rtype: int r>N) rKrrZASN1_INTEGER_to_BNrRrVZ BN_bn2hexrhr}rr)rnr r r Zhexstring_serialr rDrDrEget_serial_numbers    zX509.get_serial_numbercCs.t|tstdtj|j}tj||dS)z Adjust the time stamp on which the certificate stops being valid. :param int amount: The number of seconds by which to adjust the timestamp. :return: ``None`` zamount must be an integerN)r]r}r_rKX509_get_notAfterrX509_gmtime_adj)rnamountZnotAfterrDrDrEgmtime_adj_notAfter-s  zX509.gmtime_adj_notAftercCs.t|tstdtj|j}tj||dS)z Adjust the timestamp on which the certificate starts being valid. :param amount: The number of seconds by which to adjust the timestamp. :return: ``None`` zamount must be an integerN)r]r}r_rKX509_get_notBeforerr)rnrZ notBeforerDrDrEgmtime_adj_notBefore;s  zX509.gmtime_adj_notBeforecCs(t|j}tjj|d}|tjjkS)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_notAfterdatetimeZstrptimeZutcnow)rnZ time_stringZ not_afterrDrDrE has_expiredHs zX509.has_expiredcCst||jS)N)rkr)rnwhichrDrDrE_get_boundary_timeTszX509._get_boundary_timecCs |jtjS)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 )rrKr)rnrDrDrE get_notBeforeWs zX509.get_notBeforecCst||j|S)N)rdr)rnrrbrDrDrE_set_boundary_timedszX509._set_boundary_timecCs|jtj|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`` )rrKr)rnrbrDrDrE set_notBeforegs zX509.set_notBeforecCs |jtjS)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 )rrKr)rnrDrDrErts zX509.get_notAftercCs|jtj|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`` )rrKr)rnrbrDrDrE set_notAfters zX509.set_notAftercCs0tjt}||j|_t|jtjk||_|S)N)r"rrrsrUrRrVr)rnrrqrDrDrE _get_names   zX509._get_namecCs0t|tstd||j|j}t|dkdS)Nzname must be an X509Namer?)r]r"r_rrsrU)rnrrqrcrDrDrE _set_names zX509._set_namecCs|jtj}|jj||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` )rrKZX509_get_issuer_namerrr)rnrqrDrDrE get_issuers  zX509.get_issuercCs|jtj||jjdS)z Set the issuer of this certificate. :param issuer: The issuer. :type issuer: :py:class:`X509Name` :return: ``None`` N)r rKZX509_set_issuer_namerrt)rnrrDrDrE set_issuers zX509.set_issuercCs|jtj}|jj||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` )rrKX509_get_subject_namerrr)rnrqrDrDrErs  zX509.get_subjectcCs|jtj||jjdS)z Set the subject of this certificate. :param subject: The subject. :type subject: :py:class:`X509Name` :return: ``None`` N)r rKZX509_set_subject_namerrt)rnrrDrDrE set_subjects zX509.set_subjectcCs tj|jS)z Get the number of extensions on this certificate. :return: The number of extensions. :rtype: :py:class:`int` .. versionadded:: 0.12 )rKZX509_get_ext_countr)rnrDrDrEget_extension_counts zX509.get_extension_countcCs@x:|D]2}t|tstdtj|j|jd}|stqWdS)z Add extensions to the certificate. :param extensions: The extensions to add. :type extensions: An iterable of :py:class:`X509Extension` objects. :return: ``None`` z+One of the elements is not an X509Extensionr?Nr)r]r#r`rKZ X509_add_extrrr)rnrrrrDrDrErs   zX509.add_extensionscCsNtjt}tj|j||_|jtjkr.tdtj |j}tj |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#rrKZ X509_get_extrrrRrV IndexErrorZX509_EXTENSION_duprWr)rnindexrrrDrDrE get_extensions   zX509.get_extensionN)%r@rArBrCrorrrrrrrrr7rrrrrrrrrrrrrrrr r!r"rr$r%rr(rDrDrDrEr%8sB    '          z.X509Type has been deprecated, use X509 insteadc@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@rArBrCrKZX509_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_CHECKrDrDrDrEr&s c@s8eZdZdZddZddZddZdd Zd d Zd 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`. cCstj}tj|tj|_dS)N)rKZX509_STORE_newrRrWZX509_STORE_free_store)rnstorerDrDrEro6szX509Store.__init__cCsPt|tsttj|j|jdkrLtj}tj|}t |tj ktj dS)a Adds a trusted certificate to this store. Adding a certificate with this method adds this certificate as a *trusted* certificate. :param X509 cert: The certificate to add to this store. :raises TypeError: If the certificate is not an :class:`X509`. :raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your certificate. :return: ``None`` if the certificate was added successfully. rN) r]r%r_rKZX509_STORE_add_certr)rZERR_peek_errorZERR_GET_REASONrUZ!X509_R_CERT_ALREADY_IN_HASH_TABLEZERR_clear_error)rnrcodeZ err_reasonrDrDrEadd_cert:s   zX509Store.add_certcCsttj|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)rUrKZX509_STORE_add_crlr)_crl)rncrlrDrDrEadd_crlXszX509Store.add_crlcCsttj|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)rUrKZX509_STORE_set_flagsr))rnflagsrDrDrE set_flagshszX509Store.set_flagscCsFtj}tj|tj}tj|t|jdttj |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. z%srN) rKZX509_VERIFY_PARAM_newrRrWZX509_VERIFY_PARAM_freeZX509_VERIFY_PARAM_set_timer}ZstrftimerUZX509_STORE_set1_paramr))rnZvfy_timeZparamrDrDrEset_timeszX509Store.set_timeN) r@rArBrCror,r/r1r2rDrDrDrEr'(s  z8X509StoreType has been deprecated, use X509Store insteadcs 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|j|||_dS)N)rr(ro certificate)rnmessager3)rrDrEroszX509StoreContextError.__init__)r@rArBrCrorrDrD)rrEr(sc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)r)a An X.509 store context. An X.509 store context is used to carry out the actual verification process of a certificate in a described context. For describing such a context, see :class:`X509Store`. :ivar _store_ctx: The underlying X509_STORE_CTX structure used by this instance. It is dynamically allocated and automatically garbage collected. :ivar _store: See the ``store`` ``__init__`` parameter. :ivar _cert: See the ``certificate`` ``__init__`` parameter. :param X509Store store: The certificates which will be trusted for the purposes of any verifications. :param X509 certificate: The certificate to be verified. cCs0tj}tj|tj|_||_||_|jdS)N) rKZX509_STORE_CTX_newrRrWZX509_STORE_CTX_free _store_ctxr)_cert_init)rnr*r3Z store_ctxrDrDrEros zX509StoreContext.__init__cCs.tj|j|jj|jjtj}|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) rKZX509_STORE_CTX_initr5r)r6rrRrVr)rnretrDrDrEr7szX509StoreContext._initcCstj|jdS)z Internally cleans up the store context. The store context can then be reused with a new call to :meth:`_init`. N)rKZX509_STORE_CTX_cleanupr5)rnrDrDrE_cleanupszX509StoreContext._cleanupcCs\tj|jtj|jttjtjtj|jg}tj|j}tj |}t j |}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. ) rKZX509_STORE_CTX_get_errorr5ZX509_STORE_CTX_get_error_depthrrRrhZX509_verify_cert_error_stringZX509_STORE_CTX_get_current_certZX509_dupr%rr()rnerrorsrr6pycertrDrDrE_exception_from_contexts      z(X509StoreContext._exception_from_contextcCs ||_dS)z Set the context's X.509 store. .. versionadded:: 0.15 :param X509Store store: The store description which will be used for the purposes of any *future* verifications. N)r))rnr*rDrDrE set_stores zX509StoreContext.set_storecCs8|j|jtj|j}|j|dkr4|jdS)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)r9r7rKZX509_verify_certr5r<)rnr8rDrDrEverify_certificates  z#X509StoreContext.verify_certificateN) r@rArBrCror7r9r<r=r>rDrDrDrEr)s   cCsvt|tr|jd}t|}|tkr.rr?)rNr]r^r_rQreplace _crl_reasonsr'rKZASN1_ENUMERATED_newrUrRrVrWZASN1_ENUMERATED_freeZASN1_ENUMERATED_setZX509_REVOKED_add1_ext_i2drGrM)rnreasonZ reason_codeZnew_reason_extrcrrDrDrE set_reasons    zRevoked.set_reasoncCsxttj|jD]l}tj|j|}tj|}tj|tjkrt}tj ||dd}|svtj |tj |}t |dkt |SqWdS)a  Get the reason of this revocation. :return: The reason, or ``None`` if there is none. :rtype: bytes or NoneType .. seealso:: :meth:`all_reasons`, which gives you a list of all supported reasons this method might return. rN)rrKrKrGrLrrrMrZrZM_ASN1_OCTET_STRING_printrrUr\)rnrrrrMrrDrDrE 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)rT)rnrDrDrE all_reasons)s zRevoked.all_reasonscCstj|j}t||S)z Set the revocation timestamp. :param bytes when: The timestamp of the revocation, as ASN.1 TIME. :return: ``None`` )rK X509_REVOKED_get0_revocationDaterGrd)rnrbdtrDrDrE set_rev_date5s zRevoked.set_rev_datecCstj|j}t|S)z Get the revocation timestamp. :return: The timestamp of the revocation, as ASN.1 TIME. :rtype: bytes )rKrYrGrk)rnrZrDrDrE get_rev_date@s zRevoked.get_rev_dateN)r@rArBrCrTrorIrJrNrVrWrXr[r\rDrDrDrEr.s" $  c@s|eZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZedefddZdS)r/z( A certificate revocation list. cCstj}tj|tj|_dS)N)rKZ X509_CRL_newrRrW X509_CRL_freer-)rnr.rDrDrEroPsz CRL.__init__cCsddlm}t}|||jS)z Export as a ``cryptography`` CRL. :rtype: ``cryptography.x509.CertificateRevocationList`` .. versionadded:: 17.1.0 r)_CertificateRevocationList)rr^rGr-)rnr^rFrDrDrErTs 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]r ZCertificateRevocationListr_Z _x509_crlr-)r{Z crypto_crlr.rDrDrErbs zCRL.from_cryptographycCsrg}tj|j}xPttj|D]>}tj||}tj|}tjt}t j |tj |_ |j |q W|rnt|SdS)aA Return the revocations in this certificate revocation list. These revocations will be provided by value, not by reference. That means it's okay to mutate them: it won't affect this CRL. :return: The revocations in this CRL. :rtype: :class:`tuple` of :class:`Revocation` N)rKZX509_CRL_get_REVOKEDr-rZsk_X509_REVOKED_numZsk_X509_REVOKED_valueCryptography_X509_REVOKED_dupr.rrRrWrFrGrptuple)rnresultsZ revoked_stackrrHZ revoked_copyZpyrevrDrDrE get_revokedus     zCRL.get_revokedcCs8tj|j}t|tjktj|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)rKr_rGrUrRrVZX509_CRL_add0_revokedr-)rnrHcopyrrDrDrE add_revokeds zCRL.add_revokedcCsBtjtj|j}t|tjktj|tj}t j t }||_ |S)zc Get the CRL's issuer. .. versionadded:: 16.1.0 :rtype: X509Name ) rKrZX509_CRL_get_issuerr-rUrRrVrWrr"rrs)rnZ_issuerrrDrDrEr!s  zCRL.get_issuercCsttj|j|dkdS)z Set the CRL version. .. versionadded:: 16.1.0 :param int version: The version of the CRL. :return: ``None`` rN)rUrKZX509_CRL_set_versionr-)rnrrDrDrErs zCRL.set_versioncCst||j|S)N)rdr-)rnrrbrDrDrErszCRL._set_boundary_timecCs|jtj|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`` )rrKZX509_CRL_get_lastUpdate)rnrbrDrDrEset_lastUpdates zCRL.set_lastUpdatecCs|jtj|S)z Set when the CRL will next be udpated. The timestamp is formatted as an ASN.1 TIME:: YYYYMMDDhhmmssZ .. versionadded:: 16.1.0 :param bytes when: A timestamp string. :return: ``None`` )rrKZX509_CRL_get_nextUpdate)rnrbrDrDrEset_nextUpdates zCRL.set_nextUpdatecCs\tj|}t|tjktj|jtj|jtj |jtj |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) rKrrUrRrVX509_CRL_set_issuer_namer-r#rZ X509_CRL_sort X509_CRL_signrv)rnrZ issuer_keyrrrrDrDrEr7s  zCRL.signdc Cst|tstdt|ts$tdt|ts6td|tkrFtdtj|}|tj krbt dtj tj }t |tj ktj}t |tj ktj|dtj|j|tj||dddtj|j|tj|jtj|jtj|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"sha2566"``). :rtype: bytes zcert must be an X509 instancezkey must be a PKey instanceztype must be an integerzdigest must be providedzNo such digest methodr<)r]r%r_rr} _UNSPECIFIEDrKrrRrVr`rPrQrUZ ASN1_TIME_newrZX509_CRL_set_lastUpdater-ZX509_CRL_set_nextUpdatergr#rrhrvrr9) rnrrrZdaysrrrMZsometimerrDrDrEexports2      z CRL.exportN)r@rArBrCrorrrrbrdr!rrrerfr7rrlrmrDrDrDrEr/Ks  z,CRLType has been deprecated, use CRL insteadc@s4eZdZddZddZddZddZd d Zd S) r0cCsttj|jS)zm Check if this NID_pkcs7_signed object :return: True if the PKCS7 is of type signed )boolrKZPKCS7_type_is_signed_pkcs7)rnrDrDrEtype_is_signed- szPKCS7.type_is_signedcCsttj|jS)zt Check if this NID_pkcs7_enveloped object :returns: True if the PKCS7 is of type enveloped )rnrKZPKCS7_type_is_envelopedro)rnrDrDrEtype_is_enveloped5 szPKCS7.type_is_envelopedcCsttj|jS)z Check if this NID_pkcs7_signedAndEnveloped object :returns: True if the PKCS7 is of type signedAndEnveloped )rnrKZ PKCS7_type_is_signedAndEnvelopedro)rnrDrDrEtype_is_signedAndEnveloped= sz PKCS7.type_is_signedAndEnvelopedcCsttj|jS)zi Check if this NID_pkcs7_data object :return: True if the PKCS7 is of type data )rnrKZPKCS7_type_is_dataro)rnrDrDrE type_is_dataE szPKCS7.type_is_datacCs"tj|jj}tj|}tj|S)zk Returns the type name of the PKCS7 structure :return: A string with the typename )rKrrorrrRrh)rnrZ string_typerDrDrE get_type_nameM s zPKCS7.get_type_nameN)r@rArBrprqrrrsrtrDrDrDrEr0, s z0PKCS7Type has been deprecated, use PKCS7 insteadc@sbeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dddZ dS)r1z A PKCS #12 archive. cCsd|_d|_d|_d|_dS)N)rvr6_cacerts _friendlyname)rnrDrDrErod 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` )r6)rnrDrDrEget_certificatej 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`` zcert must be an X509 instanceN)r]r%r_r6)rnrrDrDrEset_certificates 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` )rv)rnrDrDrEget_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`` zpkey must be a PKey instanceN)r]rr_rv)rnrxrDrDrEset_privatekey s zPKCS12.set_privatekeycCs|jdk rt|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)rur`)rnrDrDrEget_ca_certificates s zPKCS12.get_ca_certificatescCsB|dkrd|_n.t|}x|D]}t|tstdqW||_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)rulistr]r%r_)rncacertsrrDrDrEset_ca_certificates s    zPKCS12.set_ca_certificatescCs2|dkrd|_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))rvr]r^r_)rnrqrDrDrEset_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` )rv)rnrDrDrEget_friendlyname szPKCS12.get_friendlynameNr?c Cstd|}|jdkrtj}n4tj}tj|tj}x|jD]}tj||j q:W|dkr^tj}|j }|dkrrtj}|j dkrtj}n|j j }|j dkrtj}n|j j }tj |||||tjtj||d }|tjkrttj|tj}t} tj| |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: rDNr)_text_to_bytes_and_warnrurRrVrKZsk_X509_new_nullrW sk_X509_freeZ sk_X509_pushrrvrvr6Z PKCS12_createZ&NID_pbe_WithSHA1And3_Key_TripleDES_CBCr PKCS12_freerZZi2d_PKCS12_bior\) rnrDiterZmaciterr}r friendlynamerxpkcs12rMrDrDrErm s:         z PKCS12.export)Nrr?)r@rArBrCrorwrxryrzr{r~rrrmrDrDrDrEr1_ s      z2PKCS12Type has been deprecated, use PKCS12 insteadc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)r2z! A Netscape SPKI object. cCstj}tj|tj|_dS)N)rKZNETSCAPE_SPKI_newrRrWZNETSCAPE_SPKI_free_spki)rnZspkirDrDrEro szNetscapeSPKI.__init__cCs^|jrtd|jstdtjt|}|tjkrNetscapeSPKIType has been deprecated, use NetscapeSPKI insteadc@sBeZdZd ddZeddZeddZefdd Zd d Z d S)r@FcCs4|tkr|dk rtd||_||_||_g|_dS)Nz0only FILETYPE_PEM key format supports encryption)rr` _passphrase _more_args _truncate _problems)rnrrDZ more_argstruncaterDrDrErov sz_PassphraseHelper.__init__cCsF|jdkrtjSt|jtr"tjSt|jr:tjd|jStddS)NZpem_password_cbz2Last argument must be a byte string or a callable.) rrRrVr]r^callablerA_read_passphraser_)rnrDrDrErA s   z_PassphraseHelper.callbackcCs>|jdkrtjSt|jtr"|jSt|jr2tjStddS)Nz2Last argument must be a byte string or a callable.)rrRrVr]r^rr_)rnrDrDrErB s   z_PassphraseHelper.callback_argsc Cs8|jr4y t|Wn|k r&YnX|jjddS)Nr)r_exception_from_error_queuepop)rnZ exceptionTyperDrDrErC s  z"_PassphraseHelper.raise_if_problemcCsy|jr|j|||}n |j|}t|ts4tdt||kr\|jrT|d|}ntdx(tt|D]}|||d||<qjWt|Stk r}z|j j |dSd}~XnXdS)NzString expectedz+passphrase returned by callback is too longr?r) rrr]r^r`rTrr Exceptionrrp)rnZbufsizeZrwflagZuserdatarrerDrDrEr s"    z"_PassphraseHelper._read_passphraseN)FF) r@rArBrorrArBrrCrrDrDrDrEr@u s  r@cCst|tr|jd}t|}|tkrs$     (     ( jBHUoh ;+[,/ZG! $"$ `