3 lá˜_)gã@sêddlmZmZmZddlZddlZddlZddlmZddl Z ddl m Z ddl m Z ddlmZmZmZmZmZddlmZmZddlmZdd lmZejd d d ƒZGd d „d eƒZdd„Zdd„Zdd„Z Gdd„deƒZ!d6dd„Z"d7dd„Z#d8dd„Z$d9dd„Z%d:dd„Z&d;d d!„Z'Gd"d#„d#eƒZ(e j)ej*ƒGd$d%„d%e+ƒƒZ,e j)ej*ƒGd&d'„d'e+ƒƒZ-e j)ej*ƒGd(d)„d)e+ƒƒZ.e j)ej*ƒGd*d+„d+e+ƒƒZ/Gd,d-„d-e+ƒZ0Gd.d/„d/e+ƒZ1Gd0d1„d1e+ƒZ2Gd2d3„d3e+ƒZ3d4d5„Z4dS)<é)Úabsolute_importÚdivisionÚprint_functionN)ÚEnum)Úutils)Ú _get_backend)ÚdsaÚecÚed25519Úed448Úrsa)Ú ExtensionÚ ExtensionType)ÚName)ÚObjectIdentifierižécseZdZ‡fdd„Z‡ZS)ÚAttributeNotFoundcstt|ƒj|ƒ||_dS)N)ÚsuperrÚ__init__Úoid)ÚselfÚmsgr)Ú __class__©ú/usr/lib64/python3.6/base.pyr szAttributeNotFound.__init__)Ú__name__Ú __module__Ú __qualname__rÚ __classcell__rr)rrrsrcCs&x |D]}|j|jkrtdƒ‚qWdS)Nz$This extension has already been set.)rÚ ValueError)Ú extensionÚ extensionsÚerrrÚ_reject_duplicate_extension%s  r#cCs&x |D]\}}||krtdƒ‚qWdS)Nz$This attribute has already been set.)r)rÚ attributesZattr_oidÚ_rrrÚ_reject_duplicate_attribute,sr&cCs:|jdk r2|jƒ}|r|ntjƒ}|jdd�|S|SdS)z’Normalizes a datetime to a naive datetime in UTC. time -- datetime to normalize. Assumed to be in UTC if not timezone aware. N)Útzinfo)r'Z utcoffsetÚdatetimeZ timedeltaÚreplace)ÚtimeÚoffsetrrrÚ_convert_to_naive_utc_time3s  r,c@seZdZdZdZdS)ÚVersionréN)rrrZv1Úv3rrrrr-Asr-cCst|ƒ}|j|ƒS)N)rÚload_pem_x509_certificate)ÚdataÚbackendrrrr0Fsr0cCst|ƒ}|j|ƒS)N)rÚload_der_x509_certificate)r1r2rrrr3Ksr3cCst|ƒ}|j|ƒS)N)rÚload_pem_x509_csr)r1r2rrrr4Psr4cCst|ƒ}|j|ƒS)N)rÚload_der_x509_csr)r1r2rrrr5Usr5cCst|ƒ}|j|ƒS)N)rÚload_pem_x509_crl)r1r2rrrr6Zsr6cCst|ƒ}|j|ƒS)N)rÚload_der_x509_crl)r1r2rrrr7_sr7cseZdZ‡fdd„Z‡ZS)ÚInvalidVersioncstt|ƒj|ƒ||_dS)N)rr8rÚparsed_version)rrr9)rrrreszInvalidVersion.__init__)rrrrrrr)rrr8dsr8c@súeZdZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZ ejd d „ƒZ ejd d „ƒZ ejd d„ƒZ ejdd„ƒZ ejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd „ƒZejd!d"„ƒZd#S)$Ú CertificatecCsdS)z4 Returns bytes using digest passed. Nr)rÚ algorithmrrrÚ fingerprintlszCertificate.fingerprintcCsdS)z3 Returns certificate serial number Nr)rrrrÚ serial_numberrszCertificate.serial_numbercCsdS)z1 Returns the certificate version Nr)rrrrÚversionxszCertificate.versioncCsdS)z( Returns the public key Nr)rrrrÚ public_key~szCertificate.public_keycCsdS)z? Not before time (represented as UTC datetime) Nr)rrrrÚnot_valid_before„szCertificate.not_valid_beforecCsdS)z> Not after time (represented as UTC datetime) Nr)rrrrÚnot_valid_afterŠszCertificate.not_valid_aftercCsdS)z1 Returns the issuer name object. Nr)rrrrÚissuer�szCertificate.issuercCsdS)z2 Returns the subject name object. Nr)rrrrÚsubject–szCertificate.subjectcCsdS)zt Returns a HashAlgorithm corresponding to the type of the digest signed in the certificate. Nr)rrrrÚsignature_hash_algorithmœsz$Certificate.signature_hash_algorithmcCsdS)zJ Returns the ObjectIdentifier of the signature algorithm. Nr)rrrrÚsignature_algorithm_oid£sz#Certificate.signature_algorithm_oidcCsdS)z/ Returns an Extensions object. Nr)rrrrr!©szCertificate.extensionscCsdS)z. Returns the signature bytes. Nr)rrrrÚ signature¯szCertificate.signaturecCsdS)zR Returns the tbsCertificate payload bytes as defined in RFC 5280. Nr)rrrrÚtbs_certificate_bytesµsz!Certificate.tbs_certificate_bytescCsdS)z" Checks equality. Nr)rÚotherrrrÚ__eq__»szCertificate.__eq__cCsdS)z# Checks not equal. Nr)rrHrrrÚ__ne__ÁszCertificate.__ne__cCsdS)z" Computes a hash. Nr)rrrrÚ__hash__ÇszCertificate.__hash__cCsdS)zB Serializes the certificate to PEM or DER format. Nr)rÚencodingrrrÚ public_bytesÍszCertificate.public_bytesN)rrrÚabcÚabstractmethodr<Úabstractpropertyr=r>r?r@rArBrCrDrEr!rFrGrIrJrKrMrrrrr:js"r:c@súeZdZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZ ejd d „ƒZ ejd d „ƒZ ejd d„ƒZ ejdd„ƒZ ejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZejdd „ƒZejd!d"„ƒZd#S)$ÚCertificateRevocationListcCsdS)z: Serializes the CRL to PEM or DER format. Nr)rrLrrrrMÖsz&CertificateRevocationList.public_bytescCsdS)z4 Returns bytes using digest passed. Nr)rr;rrrr<Üsz%CertificateRevocationList.fingerprintcCsdS)zs Returns an instance of RevokedCertificate or None if the serial_number is not in the CRL. Nr)rr=rrrÚ(get_revoked_certificate_by_serial_numberâszBCertificateRevocationList.get_revoked_certificate_by_serial_numbercCsdS)zt Returns a HashAlgorithm corresponding to the type of the digest signed in the certificate. Nr)rrrrrDész2CertificateRevocationList.signature_hash_algorithmcCsdS)zJ Returns the ObjectIdentifier of the signature algorithm. Nr)rrrrrEðsz1CertificateRevocationList.signature_algorithm_oidcCsdS)zC Returns the X509Name with the issuer of this CRL. Nr)rrrrrBösz CertificateRevocationList.issuercCsdS)z? Returns the date of next update for this CRL. Nr)rrrrÚ next_updateüsz%CertificateRevocationList.next_updatecCsdS)z? Returns the date of last update for this CRL. Nr)rrrrÚ last_updatesz%CertificateRevocationList.last_updatecCsdS)zS Returns an Extensions object containing a list of CRL extensions. Nr)rrrrr!sz$CertificateRevocationList.extensionscCsdS)z. Returns the signature bytes. Nr)rrrrrFsz#CertificateRevocationList.signaturecCsdS)zO Returns the tbsCertList payload bytes as defined in RFC 5280. Nr)rrrrÚtbs_certlist_bytessz,CertificateRevocationList.tbs_certlist_bytescCsdS)z" Checks equality. Nr)rrHrrrrIsz CertificateRevocationList.__eq__cCsdS)z# Checks not equal. Nr)rrHrrrrJ sz CertificateRevocationList.__ne__cCsdS)z< Number of revoked certificates in the CRL. Nr)rrrrÚ__len__&sz!CertificateRevocationList.__len__cCsdS)zS Returns a revoked certificate (or slice of revoked certificates). Nr)rÚidxrrrÚ __getitem__,sz%CertificateRevocationList.__getitem__cCsdS)z8 Iterator over the revoked certificates Nr)rrrrÚ__iter__2sz"CertificateRevocationList.__iter__cCsdS)zQ Verifies signature of revocation list against given public key. Nr)rr?rrrÚis_signature_valid8sz,CertificateRevocationList.is_signature_validN)rrrrNrOrMr<rRrPrDrErBrSrTr!rFrUrIrJrVrXrYrZrrrrrQÔs"rQc@sÂeZdZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZej d d „ƒZ ej d d „ƒZ ej d d„ƒZ ej dd„ƒZ ejdd„ƒZej dd„ƒZej dd„ƒZej dd„ƒZej dd„ƒZdS)ÚCertificateSigningRequestcCsdS)z" Checks equality. Nr)rrHrrrrIAsz CertificateSigningRequest.__eq__cCsdS)z# Checks not equal. Nr)rrHrrrrJGsz CertificateSigningRequest.__ne__cCsdS)z" Computes a hash. Nr)rrrrrKMsz"CertificateSigningRequest.__hash__cCsdS)z( Returns the public key Nr)rrrrr?Ssz$CertificateSigningRequest.public_keycCsdS)z2 Returns the subject name object. Nr)rrrrrCYsz!CertificateSigningRequest.subjectcCsdS)zt Returns a HashAlgorithm corresponding to the type of the digest signed in the certificate. Nr)rrrrrD_sz2CertificateSigningRequest.signature_hash_algorithmcCsdS)zJ Returns the ObjectIdentifier of the signature algorithm. Nr)rrrrrEfsz1CertificateSigningRequest.signature_algorithm_oidcCsdS)z@ Returns the extensions in the signing request. Nr)rrrrr!lsz$CertificateSigningRequest.extensionscCsdS)z; Encodes the request to PEM or DER format. Nr)rrLrrrrMrsz&CertificateSigningRequest.public_bytescCsdS)z. Returns the signature bytes. Nr)rrrrrFxsz#CertificateSigningRequest.signaturecCsdS)zd Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC 2986. Nr)rrrrÚtbs_certrequest_bytes~sz/CertificateSigningRequest.tbs_certrequest_bytescCsdS)z8 Verifies signature of signing request. Nr)rrrrrZ…sz,CertificateSigningRequest.is_signature_validcCsdS)z: Get the attribute value for a given OID. Nr)rrrrÚget_attribute_for_oid‹sz/CertificateSigningRequest.get_attribute_for_oidN)rrrrNrOrIrJrKr?rPrCrDrEr!rMrFr\rZr]rrrrr[?sr[c@s6eZdZejdd„ƒZejdd„ƒZejdd„ƒZdS)ÚRevokedCertificatecCsdS)zG Returns the serial number of the revoked certificate. Nr)rrrrr=”sz RevokedCertificate.serial_numbercCsdS)zH Returns the date of when this certificate was revoked. Nr)rrrrÚrevocation_datešsz"RevokedCertificate.revocation_datecCsdS)zW Returns an Extensions object containing a list of Revoked extensions. Nr)rrrrr! szRevokedCertificate.extensionsN)rrrrNrPr=r_r!rrrrr^’sr^c@s>eZdZdggfdd„Zdd„Zdd„Zdd „Zd d d „ZdS) Ú CertificateSigningRequestBuilderNcCs||_||_||_dS)zB Creates an empty X.509 certificate request (v1). N)Ú _subject_nameÚ _extensionsÚ _attributes)rÚ subject_namer!r$rrrr¨sz)CertificateSigningRequestBuilder.__init__cCs4t|tƒstdƒ‚|jdk r$tdƒ‚t||j|jƒS)zF Sets the certificate requestor's distinguished name. zExpecting x509.Name object.Nz&The subject name may only be set once.)Ú isinstancerÚ TypeErrorrarr`rbrc)rÚnamerrrrd°s   z-CertificateSigningRequestBuilder.subject_namecCsDt|tƒstdƒ‚t|j||ƒ}t||jƒt|j|j|g|j ƒS)zE Adds an X.509 extension to the certificate request. z"extension must be an ExtensionType) rerrfr rr#rbr`rarc)rr ÚcriticalrrrÚ add_extension¼s   z.CertificateSigningRequestBuilder.add_extensioncCsLt|tƒstdƒ‚t|tƒs$tdƒ‚t||jƒt|j|j|j||fgƒS)zK Adds an X.509 attribute with an OID and associated value. zoid must be an ObjectIdentifierzvalue must be bytes) rerrfÚbytesr&rcr`rarb)rrÚvaluerrrÚ add_attributeÌs   z.CertificateSigningRequestBuilder.add_attributecCs(t|ƒ}|jdkrtdƒ‚|j|||ƒS)zF Signs the request using the requestor's private key. Nz/A CertificateSigningRequest must have a subject)rrarZcreate_x509_csr)rÚ private_keyr;r2rrrÚsignÞs z%CertificateSigningRequestBuilder.sign)N)rrrrrdrirlrnrrrrr`§s  r`c@sfeZdZddddddgfdd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z ddd„Z dS)ÚCertificateBuilderNcCs6tj|_||_||_||_||_||_||_||_ dS)N) r-r/Z_versionÚ _issuer_nameraÚ _public_keyÚ_serial_numberÚ_not_valid_beforeÚ_not_valid_afterrb)rÚ issuer_namerdr?r=r@rAr!rrrrés zCertificateBuilder.__init__cCsDt|tƒstdƒ‚|jdk r$tdƒ‚t||j|j|j|j |j |j ƒS)z3 Sets the CA's distinguished name. zExpecting x509.Name object.Nz%The issuer name may only be set once.) rerrfrprrorarqrrrsrtrb)rrgrrrruüs  zCertificateBuilder.issuer_namecCsDt|tƒstdƒ‚|jdk r$tdƒ‚t|j||j|j|j |j |j ƒS)z: Sets the requestor's distinguished name. zExpecting x509.Name object.Nz&The subject name may only be set once.) rerrfrarrorprqrrrsrtrb)rrgrrrrds  zCertificateBuilder.subject_namecCsXt|tjtjtjtjt j fƒs&t dƒ‚|j dk r8t dƒ‚t|j|j||j|j|j|jƒS)zT Sets the requestor's public key (as found in the signing request). zhExpecting one of DSAPublicKey, RSAPublicKey, EllipticCurvePublicKey, Ed25519PublicKey or Ed448PublicKey.Nz$The public key may only be set once.)rerZ DSAPublicKeyr Z RSAPublicKeyr ZEllipticCurvePublicKeyr ZEd25519PublicKeyr ZEd448PublicKeyrfrqrrorprarrrsrtrb)rÚkeyrrrr? s&  zCertificateBuilder.public_keycCsjt|tjƒstdƒ‚|jdk r&tdƒ‚|dkr6tdƒ‚|jƒdkrJtdƒ‚t|j|j |j ||j |j |j ƒS)z5 Sets the certificate serial number. z'Serial number must be of integral type.Nz'The serial number may only be set once.rz%The serial number should be positive.é z3The serial number should not be more than 159 bits.)reÚsixÚ integer_typesrfrrrÚ bit_lengthrorprarqrsrtrb)rÚnumberrrrr=?s"   z CertificateBuilder.serial_numbercCszt|tjƒstdƒ‚|jdk r&tdƒ‚t|ƒ}|tkr>tdƒ‚|jdk rZ||jkrZtdƒ‚t|j |j |j |j ||j|j ƒS)z7 Sets the certificate activation time. zExpecting datetime object.Nz*The not valid before may only be set once.z>The not valid before date must be on or after 1950 January 1).zBThe not valid before date must be before the not valid after date.)rer(rfrsrr,Ú_EARLIEST_UTC_TIMErtrorprarqrrrb)rr*rrrr@Zs&  z#CertificateBuilder.not_valid_beforecCszt|tjƒstdƒ‚|jdk r&tdƒ‚t|ƒ}|tkr>tdƒ‚|jdk rZ||jkrZtdƒ‚t|j |j |j |j |j||j ƒS)z7 Sets the certificate expiration time. zExpecting datetime object.Nz)The not valid after may only be set once.ztdƒ‚|jdkrPtdƒ‚|jdkrbtdƒ‚|jdkrttdƒ‚|j|||ƒS)zC Signs the certificate using the CA's private key. Nz&A certificate must have a subject namez&A certificate must have an issuer namez'A certificate must have a serial numberz/A certificate must have a not valid before timez.A certificate must have a not valid after timez$A certificate must have a public key) rrarrprrrsrtrqZcreate_x509_certificate)rrmr;r2rrrrn«s      zCertificateBuilder.sign)N) rrrrrurdr?r=r@rArirnrrrrroès  roc@sReZdZdddggfdd„Zdd„Zdd„Zdd „Zd d „Zd d „Zddd„Z dS)Ú CertificateRevocationListBuilderNcCs"||_||_||_||_||_dS)N)rpÚ _last_updateÚ _next_updaterbÚ_revoked_certificates)rrurTrSr!Zrevoked_certificatesrrrrÆs z)CertificateRevocationListBuilder.__init__cCs<t|tƒstdƒ‚|jdk r$tdƒ‚t||j|j|j|j ƒS)NzExpecting x509.Name object.z%The issuer name may only be set once.) rerrfrprr}r~rrbr€)rrurrrruÔs  z,CertificateRevocationListBuilder.issuer_namecCsrt|tjƒstdƒ‚|jdk r&tdƒ‚t|ƒ}|tkr>tdƒ‚|jdk rZ||jkrZtdƒ‚t|j ||j|j |j ƒS)NzExpecting datetime object.z!Last update may only be set once.z8The last update date must be on or after 1950 January 1.z9The last update date must be before the next update date.) rer(rfr~rr,r|rr}rprbr€)rrTrrrrTás"  z,CertificateRevocationListBuilder.last_updatecCsrt|tjƒstdƒ‚|jdk r&tdƒ‚t|ƒ}|tkr>tdƒ‚|jdk rZ||jkrZtdƒ‚t|j |j||j |j ƒS)NzExpecting datetime object.z!Last update may only be set once.z8The last update date must be on or after 1950 January 1.z8The next update date must be after the last update date.) rer(rfrrr,r|r~r}rprbr€)rrSrrrrS÷s"  z,CertificateRevocationListBuilder.next_updatecCsLt|tƒstdƒ‚t|j||ƒ}t||jƒt|j|j |j |j|g|j ƒS)zM Adds an X.509 extension to the certificate revocation list. z"extension must be an ExtensionType) rerrfr rr#rbr}rpr~rr€)rr rhrrrri s   z.CertificateRevocationListBuilder.add_extensioncCs2t|tƒstdƒ‚t|j|j|j|j|j|gƒS)z8 Adds a revoked certificate to the CRL. z)Must be an instance of RevokedCertificate) rer^rfr}rpr~rrbr€)rZrevoked_certificaterrrÚadd_revoked_certificates z8CertificateRevocationListBuilder.add_revoked_certificatecCsLt|ƒ}|jdkrtdƒ‚|jdkr,tdƒ‚|jdkr>tdƒ‚|j|||ƒS)NzA CRL must have an issuer namez"A CRL must have a last update timez"A CRL must have a next update time)rrprr~rZcreate_x509_crl)rrmr;r2rrrrn-s   z%CertificateRevocationListBuilder.sign)N) rrrrrurTrSrir�rnrrrrr}Ås  r}c@s>eZdZddgfdd„Zdd„Zdd„Zdd „Zd d d „ZdS) ÚRevokedCertificateBuilderNcCs||_||_||_dS)N)rrÚ_revocation_daterb)rr=r_r!rrrr<sz"RevokedCertificateBuilder.__init__cCsZt|tjƒstdƒ‚|jdk r&tdƒ‚|dkr6tdƒ‚|jƒdkrJtdƒ‚t||j|j ƒS)Nz'Serial number must be of integral type.z'The serial number may only be set once.rz$The serial number should be positiverwz3The serial number should not be more than 159 bits.) rerxryrfrrrrzr‚rƒrb)rr{rrrr=Cs   z'RevokedCertificateBuilder.serial_numbercCsNt|tjƒstdƒ‚|jdk r&tdƒ‚t|ƒ}|tkr>tdƒ‚t|j||j ƒS)NzExpecting datetime object.z)The revocation date may only be set once.z7The revocation date must be on or after 1950 January 1.) rer(rfrƒrr,r|r‚rrrb)rr*rrrr_Us  z)RevokedCertificateBuilder.revocation_datecCsDt|tƒstdƒ‚t|j||ƒ}t||jƒt|j|j |j|gƒS)Nz"extension must be an ExtensionType) rerrfr rr#rbr‚rrrƒ)rr rhrrrrics  z'RevokedCertificateBuilder.add_extensioncCs6t|ƒ}|jdkrtdƒ‚|jdkr,tdƒ‚|j|ƒS)Nz/A revoked certificate must have a serial numberz1A revoked certificate must have a revocation date)rrrrrƒZcreate_x509_revoked_certificate)rr2rrrÚbuildos  zRevokedCertificateBuilder.build)N)rrrrr=r_rir„rrrrr‚;s  r‚cCstjtjdƒdƒd?S)NéZbigr)rZint_from_bytesÚosÚurandomrrrrÚrandom_serial_number{srˆ)N)N)N)N)N)N)5Z __future__rrrrNr(r†ÚenumrrxZ cryptographyrZcryptography.hazmat.backendsrZ)cryptography.hazmat.primitives.asymmetricrr r r r Zcryptography.x509.extensionsr rZcryptography.x509.namerZcryptography.x509.oidrr|Ú Exceptionrr#r&r,r-r0r3r4r5r6r7r8Z add_metaclassÚABCMetaÚobjectr:rQr[r^r`ror}r‚rˆrrrrÚsL           ijRA^v@