3
ãdeå9  ã               @   s†  U d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ	 d dl
mZmZ G dd„ dejƒZdd„ eD ƒZejejejejejejejejejejejejiZejeef ejeef Zejeef Zejd	ejd
ej dej!dej"dejdej#dejdej$di	Z%e%dd„ e%j&ƒ D ƒZ'ej(ee)f edœdd„Z*eedœdd„Z+G dd„ dƒZ,G dd„ dƒZ-G dd„ dƒZ.G dd„ dƒZ/dS ) é    N)Úutils)Úx509)ÚNameOIDÚObjectIdentifierc               @   s<   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdS )Ú	_ASN1Typeé   é   é   é   é   é   é   é   é   é   é   é   N)Ú__name__Ú
__module__Ú__qualname__Ú	BitStringZOctetStringÚ
UTF8StringZNumericStringÚPrintableStringZ	T61StringÚ	IA5StringZUTCTimeZGeneralizedTimeZVisibleStringZUniversalStringZ	BMPString© r   r   úM/var/www/agendate/envp3/lib/python3.6/site-packages/cryptography/x509/name.pyr      s   r   c             C   s   i | ]}||j “qS r   )Úvalue)Ú.0Úir   r   r   ú
<dictcomp>   s    r   ZCNÚLZSTÚOZOUÚCZSTREETZDCZUIDc             C   s   i | ]\}}||“qS r   r   )r   ÚkÚvr   r   r   r   :   s    )ÚvalÚreturnc             C   sº   | sdS t | tƒr&dtj| ƒjdƒ S | jddƒ} | jddƒ} | jdd	ƒ} | jd
dƒ} | jddƒ} | jddƒ} | jddƒ} | jddƒ} | d dkršd|  } | d dkr¶| dd… d } | S )z>Escape special characters in RFC4514 Distinguished Name value.Ú ú#Úutf8ú\z\\ú"z\"ú+z\+ú,z\,ú;z\;ú<z\<ú>z\>ú z\00r   ú é   Nz\ )r(   r2   éÿÿÿÿr4   )Ú
isinstanceÚbytesÚbinasciiÚhexlifyÚdecodeÚreplace)r%   r   r   r   Ú_escape_dn_value=   s"    
r;   c             C   s   | sdS dd„ }t jj|| ƒS )Nr'   c             S   s(   | j dƒ}t|ƒdkr|S tt|dƒƒS )Nr3   é   )ÚgroupÚlenÚchrÚint)Úmr%   r   r   r   Úsubb   s    
z_unescape_dn_value.<locals>.sub)Ú_RFC4514NameParserÚ_PAIR_RErB   )r%   rB   r   r   r   Ú_unescape_dn_valueZ   s    rE   c               @   s¾   e Zd Zdddœeejeef eje	 e
ddœdd„Zeedœdd	„ƒZeejeef dœd
d„ƒZeedœdd„ƒZdeje edœdd„Zee
dœdd„Zedœdd„Zedœdd„ZdS )ÚNameAttributeNT)Ú	_validate)Úoidr   Ú_typerG   r&   c            C   sø   t |tƒstdƒ‚|tjkrB|tjkr.tdƒ‚t |tƒsTtdƒ‚nt |tƒsTtdƒ‚|tj	ksh|tj
krºt |tƒsvt‚t|jdƒƒ}|dkrž|dkržtdƒ‚n|dkrºtjd	j|ƒdd
� |d krÐtj|tjƒ}t |tƒsâtdƒ‚|| _|| _|| _d S )Nz2oid argument must be an ObjectIdentifier instance.z6oid must be X500_UNIQUE_IDENTIFIER for BitString type.z!value must be bytes for BitStringzvalue argument must be a strr)   é   Tz/Country name must be a 2 character country codezUCountry names should be two characters, but the attribute is {} characters in length.)Ú
stacklevelz%_type must be from the _ASN1Type enum)r5   r   Ú	TypeErrorr   r   r   ZX500_UNIQUE_IDENTIFIERr6   ÚstrÚCOUNTRY_NAMEÚJURISDICTION_COUNTRY_NAMEÚAssertionErrorr>   ÚencodeÚ
ValueErrorÚwarningsÚwarnÚformatÚ_NAMEOID_DEFAULT_TYPEÚgetr   Ú_oidÚ_valuerI   )ÚselfrH   r   rI   rG   Zc_lenr   r   r   Ú__init__n   s<    







	
zNameAttribute.__init__)r&   c             C   s   | j S )N)rX   )rZ   r   r   r   rH   ¦   s    zNameAttribute.oidc             C   s   | j S )N)rY   )rZ   r   r   r   r   ª   s    zNameAttribute.valuec             C   s   t j| j| jjƒS )zt
        The short attribute name (for example "CN") if available,
        otherwise the OID dotted string.
        )Ú_NAMEOID_TO_NAMErW   rH   Zdotted_string)rZ   r   r   r   Úrfc4514_attribute_name®   s    z$NameAttribute.rfc4514_attribute_name)Úattr_name_overridesr&   c             C   s6   |r|j | jƒnd}|dkr"| j}|› dt| jƒ› �S )z 
        Format as RFC4514 Distinguished Name string.

        Use short attribute name if available, otherwise fall back to OID
        dotted string.
        Nú=)rW   rH   r]   r;   r   )rZ   r^   Ú	attr_namer   r   r   Úrfc4514_string¶   s    
zNameAttribute.rfc4514_string)Úotherr&   c             C   s&   t |tƒstS | j|jko$| j|jkS )N)r5   rF   ÚNotImplementedrH   r   )rZ   rb   r   r   r   Ú__eq__Ç   s    
zNameAttribute.__eq__c             C   s   t | j| jfƒS )N)ÚhashrH   r   )rZ   r   r   r   Ú__hash__Í   s    zNameAttribute.__hash__c             C   s
   dj | ƒS )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)rU   )rZ   r   r   r   Ú__repr__Ð   s    zNameAttribute.__repr__)N)N)r   r   r   r   ÚtypingÚUnionrM   r6   ÚOptionalr   Úboolr[   ÚpropertyrH   r   r]   Ú_OidNameMapra   Úobjectrd   r@   rf   rg   r   r   r   r   rF   m   s   1
rF   c               @   sœ   e Zd Zeje dœdd„Zeeje dœdd„Z	dej
e edœd	d
„Zeedœdd„Zedœdd„Zeje dœdd„Zedœdd„Zedœdd„ZdS )ÚRelativeDistinguishedName)Ú
attributesc             C   s\   t |ƒ}|stdƒ‚tdd„ |D ƒƒs.tdƒ‚|| _t|ƒ| _t| jƒt|ƒkrXtdƒ‚d S )Nz-a relative distinguished name cannot be emptyc             s   s   | ]}t |tƒV  qd S )N)r5   rF   )r   Úxr   r   r   ú	<genexpr>Ù   s    z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)ÚlistrR   ÚallrL   Ú_attributesÚ	frozensetÚ_attribute_setr>   )rZ   rp   r   r   r   r[   Õ   s    
z"RelativeDistinguishedName.__init__)rH   r&   c                s   ‡ fdd„| D ƒS )Nc                s   g | ]}|j ˆ kr|‘qS r   )rH   )r   r   )rH   r   r   ú
<listcomp>æ   s    zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   )rZ   rH   r   )rH   r   Úget_attributes_for_oidã   s    z0RelativeDistinguishedName.get_attributes_for_oidN)r^   r&   c                s   dj ‡ fdd„| jD ƒƒS )z¬
        Format as RFC4514 Distinguished Name string.

        Within each RDN, attributes are joined by '+', although that is rarely
        used in certificates.
        r,   c             3   s   | ]}|j ˆ ƒV  qd S )N)ra   )r   Úattr)r^   r   r   rr   ò   s   z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>)Újoinru   )rZ   r^   r   )r^   r   ra   è   s    	
z(RelativeDistinguishedName.rfc4514_string)rb   r&   c             C   s   t |tƒstS | j|jkS )N)r5   ro   rc   rw   )rZ   rb   r   r   r   rd   ö   s    
z RelativeDistinguishedName.__eq__)r&   c             C   s
   t | jƒS )N)re   rw   )rZ   r   r   r   rf   ü   s    z"RelativeDistinguishedName.__hash__c             C   s
   t | jƒS )N)Úiterru   )rZ   r   r   r   Ú__iter__ÿ   s    z"RelativeDistinguishedName.__iter__c             C   s
   t | jƒS )N)r>   ru   )rZ   r   r   r   Ú__len__  s    z!RelativeDistinguishedName.__len__c             C   s   d| j ƒ › d�S )Nz<RelativeDistinguishedName(z)>)ra   )rZ   r   r   r   rg     s    z"RelativeDistinguishedName.__repr__)N)r   r   r   rh   ÚIterablerF   r[   r   ÚListry   rj   rm   rM   ra   rn   rk   rd   r@   rf   ÚIteratorr}   r~   rg   r   r   r   r   ro   Ô   s   
ro   c               @   s*  e Zd Zejeje ddœdd„ƒZejeje ddœdd„ƒZejej	eef  ddœdd„Ze
d!eeje d dœdd	„ƒZd"eje ed
œdd„Zeeje dœdd„Zeeje dœdd„ƒZd#ejedœdd„Zeedœdd„Zedœdd„Zeje dœdd„Zedœdd„Z edœdd „Z!dS )$ÚNameN)rp   r&   c             C   s   d S )Nr   )rZ   rp   r   r   r   r[   
  s    zName.__init__c             C   s   d S )Nr   )rZ   rp   r   r   r   r[     s    c             C   s`   t |ƒ}tdd„ |D ƒƒr,dd„ |D ƒ| _n0tdd„ |D ƒƒrTtjtjt |ƒ| _ntdƒ‚d S )Nc             s   s   | ]}t |tƒV  qd S )N)r5   rF   )r   rq   r   r   r   rr     s    z Name.__init__.<locals>.<genexpr>c             S   s   g | ]}t tjt|ƒgƒ‘qS r   )ro   rh   ÚcastrF   )r   rq   r   r   r   rx     s   z!Name.__init__.<locals>.<listcomp>c             s   s   | ]}t |tƒV  qd S )N)r5   ro   )r   rq   r   r   r   rr      s    zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)rs   rt   ru   rh   rƒ   r€   ro   rL   )rZ   rp   r   r   r   r[     s    )Údatar^   r&   c             C   s   t ||p
i ƒjƒ S )N)rC   Úparse)Úclsr„   r^   r   r   r   Úfrom_rfc4514_string*  s    zName.from_rfc4514_string)r^   r&   c                s   dj ‡ fdd„t| jƒD ƒƒS )að  
        Format as RFC4514 Distinguished Name string.
        For example 'CN=foobar.com,O=Foo Corp,C=US'

        An X.509 name is a two-level structure: a list of sets of attributes.
        Each list element is separated by ',' and within each list element, set
        elements are separated by '+'. The latter is almost never used in
        real world certificates. According to RFC4514 section 2.1 the
        RDNSequence must be reversed when converting to string representation.
        r-   c             3   s   | ]}|j ˆ ƒV  qd S )N)ra   )r   rz   )r^   r   r   rr   @  s   z&Name.rfc4514_string.<locals>.<genexpr>)r{   Úreversedru   )rZ   r^   r   )r^   r   ra   2  s    
zName.rfc4514_string)rH   r&   c                s   ‡ fdd„| D ƒS )Nc                s   g | ]}|j ˆ kr|‘qS r   )rH   )r   r   )rH   r   r   rx   G  s    z/Name.get_attributes_for_oid.<locals>.<listcomp>r   )rZ   rH   r   )rH   r   ry   D  s    zName.get_attributes_for_oid)r&   c             C   s   | j S )N)ru   )rZ   r   r   r   ÚrdnsI  s    z	Name.rdns)Úbackendr&   c             C   s
   t j| ƒS )N)Ú	rust_x509Zencode_name_bytes)rZ   rŠ   r   r   r   Úpublic_bytesM  s    zName.public_bytes)rb   r&   c             C   s   t |tƒstS | j|jkS )N)r5   r‚   rc   ru   )rZ   rb   r   r   r   rd   P  s    
zName.__eq__c             C   s   t t| jƒƒS )N)re   Útupleru   )rZ   r   r   r   rf   V  s    zName.__hash__c             c   s(   x"| j D ]}x|D ]
}|V  qW qW d S )N)ru   )rZ   ÚrdnZavar   r   r   r}   [  s    
zName.__iter__c             C   s   t dd„ | jD ƒƒS )Nc             s   s   | ]}t |ƒV  qd S )N)r>   )r   rŽ   r   r   r   rr   a  s    zName.__len__.<locals>.<genexpr>)Úsumru   )rZ   r   r   r   r~   `  s    zName.__len__c             C   s"   dj dd„ | jD ƒƒ}d|› d�S )Nr-   c             s   s   | ]}|j ƒ V  qd S )N)ra   )r   rz   r   r   r   rr   d  s    z Name.__repr__.<locals>.<genexpr>z<Name(z)>)r{   ru   )rZ   r‰   r   r   r   rg   c  s    zName.__repr__)N)N)N)"r   r   r   rh   Úoverloadr   rF   r[   ro   ri   ÚclassmethodrM   rj   Ú_NameOidMapr‡   rm   ra   r   r€   ry   rl   r‰   ÚAnyr6   rŒ   rn   rk   rd   r@   rf   r�   r}   r~   rg   r   r   r   r   r‚   	  s.   
r‚   c               @   s0  e Zd ZejdƒZejdƒZdZejeƒZdZ	dZ
dZdeejƒ› d�Ze	› d	e› �Ze
› d	e› �Ze› d	e› �Zejd
e› d	e› de› d	e› de› d	e› d�ejƒZejdƒZeeddœdd„Zedœdd„Zeje dœdd„Zeddœdd„Zedœdd„Ze dœdd„Z!e"dœdd „Z#e$dœd!d"„Z%dS )#rC   z!(0|([1-9]\d*))(\.(0|([1-9]\d*)))+z[a-zA-Z][a-zA-Z\d-]*z!\\([\\ #=\"\+,;<>]|[\da-zA-Z]{2})z7[\x01-\x1f\x21\x24-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]z3[\x01-\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]z7[\x01-\x1F\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]z[\x80-ú]ú|z
        (
            (z!)
            (
                (z)*
                (z$)
            )?
        )?
        z#([\da-zA-Z]{2})+N)r„   r^   r&   c             C   s   || _ d| _|| _d S )Nr   )Ú_dataÚ_idxÚ_attr_name_overrides)rZ   r„   r^   r   r   r   r[   ƒ  s    z_RFC4514NameParser.__init__)r&   c             C   s   | j t| jƒk S )N)r—   r>   r–   )rZ   r   r   r   Ú	_has_data‰  s    z_RFC4514NameParser._has_datac             C   s   | j ƒ r| j| j S d S )N)r™   r–   r—   )rZ   r   r   r   Ú_peekŒ  s    z_RFC4514NameParser._peek)Úchr&   c             C   s"   | j ƒ |krt‚|  jd7  _d S )Nr3   )rš   rR   r—   )rZ   r›   r   r   r   Ú
_read_char‘  s    z_RFC4514NameParser._read_charc             C   s<   |j | j| jd�}|d krt‚|jƒ }|  jt|ƒ7  _|S )N)Úpos)Úmatchr–   r—   rR   r=   r>   )rZ   Úpatrž   r%   r   r   r   Ú_read_re–  s    z_RFC4514NameParser._read_rec             C   s<   | j ƒ g}x$| jƒ r.| jdƒ |j| j ƒ ƒ qW tt|ƒƒS )a'  
        Parses the `data` string and converts it to a Name.

        According to RFC4514 section 2.1 the RDNSequence must be
        reversed when converting to string representation. So, when
        we parse it, we need to reverse again to get the RDNs on the
        correct order.
        r-   )Ú
_parse_rdnr™   rœ   Úappendr‚   rˆ   )rZ   r‰   r   r   r   r…   ž  s
    	


z_RFC4514NameParser.parsec             C   s<   | j ƒ g}x(| jƒ dkr2| jdƒ |j| j ƒ ƒ qW t|ƒS )Nr,   )Ú	_parse_narš   rœ   r¢   ro   )rZ   Znasr   r   r   r¡   ¯  s
    

z_RFC4514NameParser._parse_rdnc             C   s²   y| j | jƒ}W n@ tk
rP   | j | jƒ}| jj|tj|ƒƒ}|d krLt‚Y n
X t|ƒ}| jdƒ | j	ƒ dkr”| j | j
ƒ}tj|dd … ƒjƒ }n| j | jƒ}t|ƒ}t||ƒS )Nr_   r(   r3   )r    Ú_OID_RErR   Ú	_DESCR_REr˜   rW   Ú_NAME_TO_NAMEOIDr   rœ   rš   Ú_HEXSTRING_REr7   Ú	unhexlifyr9   Ú
_STRING_RErE   rF   )rZ   Z	oid_valueÚnamerH   r   Z	raw_valuer   r   r   r£   ·  s     

z_RFC4514NameParser._parse_na)&r   r   r   ÚreÚcompiler¤   r¥   Z_PAIRrD   Z_LUTF1Z_SUTF1Z_TUTF1r?   ÚsysÚ
maxunicodeZ_UTFMBZ	_LEADCHARZ_STRINGCHARZ
_TRAILCHARÚVERBOSEr©   r§   rM   r’   r[   rk   r™   rh   rj   rš   rœ   r    r‚   r…   ro   r¡   rF   r£   r   r   r   r   rC   h  s.   


	(
rC   )0r7   r«   r­   rh   rS   Zcryptographyr   Z"cryptography.hazmat.bindings._rustr   r‹   Zcryptography.x509.oidr   r   ÚEnumr   Z_ASN1_TYPE_TO_ENUMrN   r   rO   ZSERIAL_NUMBERZDN_QUALIFIERZEMAIL_ADDRESSr   ZDOMAIN_COMPONENTrV   ÚDictÚMappingrM   rm   r’   ZCOMMON_NAMEZLOCALITY_NAMEZSTATE_OR_PROVINCE_NAMEZORGANIZATION_NAMEZORGANIZATIONAL_UNIT_NAMEZSTREET_ADDRESSZUSER_IDr\   Úitemsr¦   ri   r6   r;   rE   rF   ro   r‚   rC   r   r   r   r   Ú<module>   sD    g5_