3 l_)g@sddlmZmZmZddlZddlZddlZddlmZddl Z ddl m Z ddl m Z ddlmZmZmZmZmZddlmZmZddlmZdd lmZejd d d ZGd d d eZddZddZddZ GdddeZ!d6ddZ"d7ddZ#d8ddZ$d9ddZ%d:ddZ&d;d d!Z'Gd"d#d#eZ(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+Z1Gd0d1d1e+Z2Gd2d3d3e+Z3d4d5Z4dS)<)absolute_importdivisionprint_functionN)Enum)utils) _get_backend)dsaeced25519ed448rsa) Extension ExtensionType)Name)ObjectIdentifiericseZdZfddZZS)AttributeNotFoundcstt|j|||_dS)N)superr__init__oid)selfmsgr) __class__/usr/lib64/python3.6/base.pyr szAttributeNotFound.__init__)__name__ __module__ __qualname__r __classcell__rr)rrrsrcCs&x |D]}|j|jkrtdqWdS)Nz$This extension has already been set.)r ValueError) extension extensionserrr_reject_duplicate_extension%s  r#cCs&x |D]\}}||krtdqWdS)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)zNormalizes 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 utcoffsetdatetimeZ timedeltareplace)timeoffsetrrr_convert_to_naive_utc_time3s  r,c@seZdZdZdZdS)VersionrN)rrrZv1v3rrrrr-Asr-cCst|}|j|S)N)rload_pem_x509_certificate)databackendrrrr0Fsr0cCst|}|j|S)N)rload_der_x509_certificate)r1r2rrrr3Ksr3cCst|}|j|S)N)rload_pem_x509_csr)r1r2rrrr4Psr4cCst|}|j|S)N)rload_der_x509_csr)r1r2rrrr5Usr5cCst|}|j|S)N)rload_pem_x509_crl)r1r2rrrr6Zsr6cCst|}|j|S)N)rload_der_x509_crl)r1r2rrrr7_sr7cseZdZfddZZS)InvalidVersioncstt|j|||_dS)N)rr8rparsed_version)rrr9)rrrreszInvalidVersion.__init__)rrrrrrr)rrr8dsr8c@seZdZejddZejddZejddZejddZ ejd d Z ejd d Z ejd dZ ejddZ ejddZejddZejddZejddZejddZejddZejddZejdd 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)rrrrversionxszCertificate.versioncCsdS)z( Returns the public key Nr)rrrr public_key~szCertificate.public_keycCsdS)z? Not before time (represented as UTC datetime) Nr)rrrrnot_valid_beforeszCertificate.not_valid_beforecCsdS)z> Not after time (represented as UTC datetime) Nr)rrrrnot_valid_afterszCertificate.not_valid_aftercCsdS)z1 Returns the issuer name object. Nr)rrrrissuerszCertificate.issuercCsdS)z2 Returns the subject name object. Nr)rrrrsubjectszCertificate.subjectcCsdS)zt Returns a HashAlgorithm corresponding to the type of the digest signed in the certificate. Nr)rrrrsignature_hash_algorithmsz$Certificate.signature_hash_algorithmcCsdS)zJ Returns the ObjectIdentifier of the signature algorithm. Nr)rrrrsignature_algorithm_oidsz#Certificate.signature_algorithm_oidcCsdS)z/ Returns an Extensions object. Nr)rrrrr!szCertificate.extensionscCsdS)z. Returns the signature bytes. Nr)rrrr signatureszCertificate.signaturecCsdS)zR Returns the tbsCertificate payload bytes as defined in RFC 5280. Nr)rrrrtbs_certificate_bytessz!Certificate.tbs_certificate_bytescCsdS)z" Checks equality. Nr)rotherrrr__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)rencodingrrr public_bytesszCertificate.public_bytesN)rrrabcabstractmethodr<abstractpropertyr=r>r?r@rArBrCrDrEr!rFrGrIrJrKrMrrrrr:js"r:c@seZdZejddZejddZejddZejddZ ejd d Z ejd d Z ejd dZ ejddZ ejddZejddZejddZejddZejddZejddZejddZejdd Zejd!d"Zd#S)$CertificateRevocationListcCsdS)z: Serializes the CRL to PEM or DER format. Nr)rrLrrrrMsz&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_numberszBCertificateRevocationList.get_revoked_certificate_by_serial_numbercCsdS)zt Returns a HashAlgorithm corresponding to the type of the digest signed in the certificate. Nr)rrrrrDsz2CertificateRevocationList.signature_hash_algorithmcCsdS)zJ Returns the ObjectIdentifier of the signature algorithm. Nr)rrrrrEsz1CertificateRevocationList.signature_algorithm_oidcCsdS)zC Returns the X509Name with the issuer of this CRL. Nr)rrrrrBsz CertificateRevocationList.issuercCsdS)z? Returns the date of next update for this CRL. Nr)rrrr next_updatesz%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)rrrrtbs_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)ridxrrr __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?rrris_signature_valid8sz,CertificateRevocationList.is_signature_validN)rrrrNrOrMr<rRrPrDrErBrSrTr!rFrUrIrJrVrXrYrZrrrrrQs"rQc@seZdZejddZejddZejddZejddZej d d Z ej d d Z ej d dZ ej ddZ ejddZej ddZej ddZej ddZej ddZdS)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)rrrrtbs_certrequest_bytes~sz/CertificateSigningRequest.tbs_certrequest_bytescCsdS)z8 Verifies signature of signing request. Nr)rrrrrZsz,CertificateSigningRequest.is_signature_validcCsdS)z: Get the attribute value for a given OID. Nr)rrrrget_attribute_for_oidsz/CertificateSigningRequest.get_attribute_for_oidN)rrrrNrOrIrJrKr?rPrCrDrEr!rMrFr\rZr]rrrrr[?sr[c@s6eZdZejddZejddZejddZdS)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)rrrrrevocation_datesz"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>eZdZdggfddZddZddZdd Zd d d ZdS) CertificateSigningRequestBuilderNcCs||_||_||_dS)zB Creates an empty X.509 certificate request (v1). N) _subject_name _extensions _attributes)r subject_namer!r$rrrrsz)CertificateSigningRequestBuilder.__init__cCs4t|tstd|jdk r$tdt||j|jS)zF Sets the certificate requestor's distinguished name. zExpecting x509.Name object.Nz&The subject name may only be set once.) isinstancer TypeErrorrarr`rbrc)rnamerrrrds   z-CertificateSigningRequestBuilder.subject_namecCsDt|tstdt|j||}t||jt|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_extensions   z.CertificateSigningRequestBuilder.add_extensioncCsLt|tstdt|ts$tdt||jt|j|j|j||fgS)zK Adds an X.509 attribute with an OID and associated value. zoid must be an ObjectIdentifierzvalue must be bytes) rerrfbytesr&rcr`rarb)rrvaluerrr add_attributes   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;r2rrrsigns z%CertificateSigningRequestBuilder.sign)N)rrrrrdrirlrnrrrrr`s  r`c@sfeZdZddddddgfddZddZddZdd Zd d Zd d ZddZ ddZ dddZ 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!rrrrs zCertificateBuilder.__init__cCsDt|tstd|jdk r$tdt||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)rrgrrrrus  zCertificateBuilder.issuer_namecCsDt|tstd|jdk r$tdt|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 fs&t d|j dk r8t dt|j|j||j|j|j|jS)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)rkeyrrrr? s&  zCertificateBuilder.public_keycCsjt|tjstd|jdk r&td|dkr6td|jdkrJtdt|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.)resix integer_typesrfrrr bit_lengthrorprarqrsrtrb)rnumberrrrr=?s"   z CertificateBuilder.serial_numbercCszt|tjstd|jdk r&tdt|}|tkr>td|jdk rZ||jkrZtdt|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|tjstd|jdk r&tdt|}|tkr>td|jdk rZ||jkrZtdt|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;r2rrrrns      zCertificateBuilder.sign)N) rrrrrurdr?r=r@rArirnrrrrros  roc@sReZdZdddggfddZddZddZdd Zd d Zd d ZdddZ dS) CertificateRevocationListBuilderNcCs"||_||_||_||_||_dS)N)rp _last_update _next_updaterb_revoked_certificates)rrurTrSr!Zrevoked_certificatesrrrrs z)CertificateRevocationListBuilder.__init__cCs<t|tstd|jdk r$tdt||j|j|j|j S)NzExpecting x509.Name object.z%The issuer name may only be set once.) rerrfrprr}r~rrbr)rrurrrrus  z,CertificateRevocationListBuilder.issuer_namecCsrt|tjstd|jdk r&tdt|}|tkr>td|jdk rZ||jkrZtdt|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)rrTrrrrTs"  z,CertificateRevocationListBuilder.last_updatecCsrt|tjstd|jdk r&tdt|}|tkr>td|jdk rZ||jkrZtdt|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)rrSrrrrSs"  z,CertificateRevocationListBuilder.next_updatecCsLt|tstdt|j||}t||jt|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|tstdt|j|j|j|j|j|gS)z8 Adds a revoked certificate to the CRL. z)Must be an instance of RevokedCertificate) rer^rfr}rpr~rrbr)rZrevoked_certificaterrradd_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) rrrrrurTrSrirrnrrrrr}s  r}c@s>eZdZddgfddZddZddZdd Zd d d ZdS) RevokedCertificateBuilderNcCs||_||_||_dS)N)rr_revocation_daterb)rr=r_r!rrrr<sz"RevokedCertificateBuilder.__init__cCsZt|tjstd|jdk r&td|dkr6td|jdkrJtdt||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.) rerxryrfrrrrzrrrb)rr{rrrr=Cs   z'RevokedCertificateBuilder.serial_numbercCsNt|tjstd|jdk r&tdt|}|tkr>tdt|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(rfrrr,r|rrrrb)rr*rrrr_Us  z)RevokedCertificateBuilder.revocation_datecCsDt|tstdt|j||}t||jt|j|j |j|gS)Nz"extension must be an ExtensionType) rerrfr rr#rbrrrr)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)rrrrrZcreate_x509_revoked_certificate)rr2rrrbuildos  zRevokedCertificateBuilder.build)N)rrrrr=r_rirrrrrr;s  rcCstjtjddd?S)NZbigr)rZint_from_bytesosurandomrrrrrandom_serial_number{sr)N)N)N)N)N)N)5Z __future__rrrrNr(renumrrxZ cryptographyrZcryptography.hazmat.backendsrZ)cryptography.hazmat.primitives.asymmetricrr r r r Zcryptography.x509.extensionsr rZcryptography.x509.namerZcryptography.x509.oidrr| Exceptionrr#r&r,r-r0r3r4r5r6r7r8Z add_metaclassABCMetaobjectr:rQr[r^r`ror}rrrrrrsL           ijRA^v@