Your IP : 216.73.216.84
3
l�_)g�@s�ddlmZmZmZddlZddlZddlZddlmZddl Z ddl
mZddlm
Z
ddlmZmZmZmZmZddlmZmZddlmZdd lmZejd
dd�ZGdd
�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
ejdd��Zejd
d��Zejdd��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
ejdd��Zejd
d��Zejdd��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 dd��Zej d
d��Zej 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 �Zdd
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�Zdd
�Zdd�Z dd�Z
ddd�ZdS)�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&td��|jdk 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.)rerZDSAPublicKeyrZRSAPublicKeyr ZEllipticCurvePublicKeyr
ZEd25519PublicKeyrZEd448PublicKeyrfrqrrorprarrrsrtrb)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.z<The not valid after date must be on or after 1950 January 1.zAThe not valid after date must be after the not valid before date.)rer(rfrtrr,r|rsrorprarqrrrb)rr*rrrrAws(
z"CertificateBuilder.not_valid_afterc CsTt|t�std��t|j||�}t||j�t|j|j |j
|j|j|j
|j|g�S)z=
Adds an X.509 extension to the certificate.
z"extension must be an ExtensionType)rerrfr
rr#rbrorprarqrrrsrt)rr rhrrrri�s
z CertificateBuilder.add_extensioncCs�t|�}|jdkrtd��|jdkr,td��|jdkr>td��|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�Zdd
�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 �Zdd
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��enumrrxZcryptographyrZcryptography.hazmat.backendsrZ)cryptography.hazmat.primitives.asymmetricrr r
rrZcryptography.x509.extensionsr
rZcryptography.x509.namerZcryptography.x509.oidrr|� Exceptionrr#r&r,r-r0r3r4r5r6r7r8Z
add_metaclass�ABCMeta�objectr:rQr[r^r`ror}r�r�rrrr�<module>sL
ijRA^v@
?>