o
    >bA!                     @   s   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 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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dd ZG dd deZG dd deZ G dd deZ!dS )    N)utils)_get_backend)NameOIDObjectIdentifierc                   @   s4   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S )	_ASN1Type                              N)__name__
__module____qualname__
UTF8StringZNumericStringPrintableStringZ	T61String	IA5StringZUTCTimeZGeneralizedTimeZVisibleStringZUniversalStringZ	BMPString r   r   8/usr/lib/python3/dist-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   | sdS |  dd} |  dd} |  dd} |  dd	} |  d
d} |  dd} |  dd} |  dd} | d dv r>d|  } | d dkrL| dd d } | S )z>Escape special characters in RFC4514 Distinguished Name value. \z\\"z\"+z\+,z\,;z\;<z\<>z\> z\00r   )# r+   Nz\ )replace)valr   r   r   _escape_dn_value3   s   r/   c                   @   s   e Zd ZefdedefddZedZ	edZ
defdd	Zd
edefddZd
edefddZdefddZdefddZdS )NameAttributeoidr   c                 C   s   t |ts	tdt |tstd|tjks|tjkr)t|ddkr)t	d|t
kr4t|tj}t |ts=td|| _|| _|| _d S )Nz2oid argument must be an ObjectIdentifier instance.z#value argument must be a text type.utf8   z/Country name must be a 2 character country codez%_type must be from the _ASN1Type enum)
isinstancer   	TypeErrorstrr   COUNTRY_NAMEJURISDICTION_COUNTRY_NAMElenencode
ValueError	_SENTINEL_NAMEOID_DEFAULT_TYPEgetr   r   _oid_value_type)selfr1   r   rA   r   r   r   __init__L   s*   




zNameAttribute.__init__r?   r@   returnc                 C   s$   t | j| jj}d|t| jf S )z
        Format as RFC4514 Distinguished Name string.

        Use short attribute name if available, otherwise fall back to OID
        dotted string.
        z%s=%s)_NAMEOID_TO_NAMEr>   r1   Zdotted_stringr/   r   )rB   keyr   r   r   rfc4514_stringq   s   zNameAttribute.rfc4514_stringotherc                 C   s&   t |tstS | j|jko| j|jkS N)r4   r0   NotImplementedr1   r   rB   rH   r   r   r   __eq__{   s   
zNameAttribute.__eq__c                 C   
   | |k S rI   r   rK   r   r   r   __ne__      
zNameAttribute.__ne__c                 C   s   t | j| jfS rI   )hashr1   r   rB   r   r   r   __hash__   s   zNameAttribute.__hash__c                 C   s
   d | S )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)formatrQ   r   r   r   __repr__   rO   zNameAttribute.__repr__N)r   r   r   r<   r   r6   rC   r   Zread_only_propertyr1   r   rG   objectboolrL   rN   intrR   rT   r   r   r   r   r0   K   s    
"

r0   c                   @   s   e Zd Zdeje fddZdeje fddZde	fddZ
d	edefd
dZd	edefddZdefddZdeje fddZdefddZde	f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r,tdd S )Nz-a relative distinguished name cannot be emptyc                 s       | ]}t |tV  qd S rI   r4   r0   r   xr   r   r   	<genexpr>       z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)listr;   allr5   _attributes	frozenset_attribute_setr9   rB   rY   r   r   r   rC      s   
z"RelativeDistinguishedName.__init__rD   c                        fdd| D S )Nc                       g | ]	}|j  kr|qS r   r1   r   rh   r   r   
<listcomp>       zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   rB   r1   r   rh   r   get_attributes_for_oid      z0RelativeDistinguishedName.get_attributes_for_oidc                 C   s   d 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                 s       | ]}|  V  qd S rI   rG   r   attrr   r   r   r^          z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>)joinrb   rQ   r   r   r   rG      s   z(RelativeDistinguishedName.rfc4514_stringrH   c                 C      t |tstS | j|jkS rI   )r4   rX   rJ   rd   rK   r   r   r   rL         
z RelativeDistinguishedName.__eq__c                 C   rM   rI   r   rK   r   r   r   rN      rO   z RelativeDistinguishedName.__ne__c                 C   
   t | jS rI   )rP   rd   rQ   r   r   r   rR      rO   z"RelativeDistinguishedName.__hash__c                 C   rv   rI   )iterrb   rQ   r   r   r   __iter__   rO   z"RelativeDistinguishedName.__iter__c                 C   rv   rI   )r9   rb   rQ   r   r   r   __len__   rO   z!RelativeDistinguishedName.__len__c                 C   s   d |  S )Nz<RelativeDistinguishedName({})>)rS   rG   rQ   r   r   r   rT      s   z"RelativeDistinguishedName.__repr__N)r   r   r   typingIterabler0   rC   Listrl   r6   rG   rU   rV   rL   rN   rW   rR   Iteratorrx   ry   rT   r   r   r   r   rX      s    	rX   c                   @   s   e Zd Zdd ZdefddZdeje fddZ	e
deje fdd	ZddefddZdedefddZdedefddZdefddZdeje fddZdefddZdefddZd
S )Namec                 C   sR   t |}tdd |D rdd |D | _d S tdd |D r%|| _d S td)Nc                 s   rZ   rI   r[   r\   r   r   r   r^      r_   z Name.__init__.<locals>.<genexpr>c                 S   s   g | ]}t |gqS r   )rX   r\   r   r   r   ri      s    
z!Name.__init__.<locals>.<listcomp>c                 s   rZ   rI   )r4   rX   r\   r   r   r   r^      r_   zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)r`   ra   rb   r5   re   r   r   r   rC      s   
zName.__init__rD   c                 C   s   d 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                 s   rn   rI   ro   rp   r   r   r   r^      s    
z&Name.rfc4514_string.<locals>.<genexpr>)rs   reversedrb   rQ   r   r   r   rG      s   
zName.rfc4514_stringc                    rf   )Nc                    rg   r   rh   r   rh   r   r   ri      rj   z/Name.get_attributes_for_oid.<locals>.<listcomp>r   rk   r   rh   r   rl      rm   zName.get_attributes_for_oidc                 C   s   | j S rI   rb   rQ   r   r   r   rdns   s   z	Name.rdnsNc                 C   s   t |}|| S rI   )r   Zx509_name_bytes)rB   Zbackendr   r   r   public_bytes   s   
zName.public_bytesrH   c                 C   rt   rI   )r4   r~   rJ   rb   rK   r   r   r   rL      ru   zName.__eq__c                 C   rM   rI   r   rK   r   r   r   rN      rO   zName.__ne__c                 C   s   t t| jS rI   )rP   tuplerb   rQ   r   r   r   rR      s   zName.__hash__c                 c   s"    | j D ]
}|D ]}|V  qqd S rI   r   )rB   rdnZavar   r   r   rx      s   
zName.__iter__c                 C   s   t dd | jD S )Nc                 s   s    | ]}t |V  qd S rI   )r9   )r   r   r   r   r   r^      rr   zName.__len__.<locals>.<genexpr>)sumrb   rQ   r   r   r   ry      s   zName.__len__c                 C   s    d dd | jD }d|S )Nr%   c                 s   rn   rI   ro   rp   r   r   r   r^      rr   z Name.__repr__.<locals>.<genexpr>z
<Name({})>)rs   rb   rS   )rB   r   r   r   r   rT      s   
zName.__repr__rI   )r   r   r   rC   r6   rG   rz   r|   r0   rl   propertyr{   rX   r   bytesr   rU   rV   rL   rN   rW   rR   r}   rx   ry   rT   r   r   r   r   r~      s    r~   )"rz   Zcryptographyr   Zcryptography.hazmat.backendsr   Zcryptography.x509.oidr   r   Enumr   Z_ASN1_TYPE_TO_ENUMrU   r<   r7   r   r8   ZSERIAL_NUMBERZDN_QUALIFIERZEMAIL_ADDRESSr   ZDOMAIN_COMPONENTr=   ZCOMMON_NAMEZLOCALITY_NAMEZSTATE_OR_PROVINCE_NAMEZORGANIZATION_NAMEZORGANIZATIONAL_UNIT_NAMEZSTREET_ADDRESSZUSER_IDrE   r/   r0   rX   r~   r   r   r   r   <module>   s8   @1