3
K^9                  @   s  d dl mZmZmZ d dlmZ d dlZd dlmZ d dl	m
Z
mZ G dd de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 )    )absolute_importdivisionprint_function)EnumN)utils)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   :/tmp/pip-unpacked-wheel-vvkwn1hz/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 | 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. \z\\"z\"+z\+,z\,;z\;<z\<>z\> z\00r   #    Nz\ )r,   r-   r/   )replace)valr   r   r   _escape_dn_value6   s    r2   c               @   sT   e Zd ZefddZejdZejdZdd Z	dd Z
d	d
 Zdd Zdd ZdS )NameAttributec             C   s   t |tstdt |tjs&td|tjks:|tjkrTt|j	ddkrTt
d|tkrjtj|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six	text_typer   COUNTRY_NAMEJURISDICTION_COUNTRY_NAMElenencode
ValueError	_SENTINEL_NAMEOID_DEFAULT_TYPEgetr	   r   _oid_value_type)selfoidr   rD   r   r   r   __init__O   s$    


	
zNameAttribute.__init__rB   rC   c             C   s$   t j| 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_NAMErA   rF   Zdotted_stringr2   r   )rE   keyr   r   r   rfc4514_stringv   s    zNameAttribute.rfc4514_stringc             C   s&   t |tstS | j|jko$| j|jkS )N)r6   r3   NotImplementedrF   r   )rE   otherr   r   r   __eq__   s    
zNameAttribute.__eq__c             C   s
   | |k S )Nr   )rE   rL   r   r   r   __ne__   s    zNameAttribute.__ne__c             C   s   t | j| jfS )N)hashrF   r   )rE   r   r   r   __hash__   s    zNameAttribute.__hash__c             C   s
   dj | S )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)format)rE   r   r   r   __repr__   s    zNameAttribute.__repr__N)r   r   r   r?   rG   r   Zread_only_propertyrF   r   rJ   rM   rN   rP   rR   r   r   r   r   r3   N   s   $


	r3   c               @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )RelativeDistinguishedNamec             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)r6   r3   )r   xr   r   r   	<genexpr>   s    z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)listr>   allr7   _attributes	frozenset_attribute_setr<   )rE   
attributesr   r   r   rG      s    
z"RelativeDistinguishedName.__init__c                s    fdd| D S )Nc                s   g | ]}|j  kr|qS r   )rF   )r   r   )rF   r   r   
<listcomp>   s    zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   )rE   rF   r   )rF   r   get_attributes_for_oid   s    z0RelativeDistinguishedName.get_attributes_for_oidc             C   s   dj 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   s   | ]}|j  V  qd S )N)rJ   )r   attrr   r   r   rU      s    z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>)joinrX   )rE   r   r   r   rJ      s    z(RelativeDistinguishedName.rfc4514_stringc             C   s   t |tstS | j|jkS )N)r6   rS   rK   rZ   )rE   rL   r   r   r   rM      s    
z RelativeDistinguishedName.__eq__c             C   s
   | |k S )Nr   )rE   rL   r   r   r   rN      s    z RelativeDistinguishedName.__ne__c             C   s
   t | jS )N)rO   rZ   )rE   r   r   r   rP      s    z"RelativeDistinguishedName.__hash__c             C   s
   t | jS )N)iterrX   )rE   r   r   r   __iter__   s    z"RelativeDistinguishedName.__iter__c             C   s
   t | jS )N)r<   rX   )rE   r   r   r   __len__   s    z!RelativeDistinguishedName.__len__c             C   s   dj | j S )Nz<RelativeDistinguishedName({})>)rQ   rJ   )rE   r   r   r   rR      s    z"RelativeDistinguishedName.__repr__N)r   r   r   rG   r]   rJ   rM   rN   rP   ra   rb   rR   r   r   r   r   rS      s   	rS   c               @   sh   e Zd Zdd Zdd Zdd Zedd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )Namec             C   sR   t |}tdd |D r,dd |D | _n"tdd |D rF|| _ntdd S )Nc             s   s   | ]}t |tV  qd S )N)r6   r3   )r   rT   r   r   r   rU      s    z Name.__init__.<locals>.<genexpr>c             S   s   g | ]}t |gqS r   )rS   )r   rT   r   r   r   r\      s    z!Name.__init__.<locals>.<listcomp>c             s   s   | ]}t |tV  qd S )N)r6   rS   )r   rT   r   r   r   rU      s    zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)rV   rW   rX   r7   )rE   r[   r   r   r   rG      s    zName.__init__c             C   s   dj 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   s   | ]}|j  V  qd S )N)rJ   )r   r^   r   r   r   rU      s    z&Name.rfc4514_string.<locals>.<genexpr>)r_   reversedrX   )rE   r   r   r   rJ      s    zName.rfc4514_stringc                s    fdd| D S )Nc                s   g | ]}|j  kr|qS r   )rF   )r   r   )rF   r   r   r\      s    z/Name.get_attributes_for_oid.<locals>.<listcomp>r   )rE   rF   r   )rF   r   r]      s    zName.get_attributes_for_oidc             C   s   | j S )N)rX   )rE   r   r   r   rdns   s    z	Name.rdnsc             C   s
   |j | S )N)Zx509_name_bytes)rE   backendr   r   r   public_bytes   s    zName.public_bytesc             C   s   t |tstS | j|jkS )N)r6   rc   rK   rX   )rE   rL   r   r   r   rM      s    
zName.__eq__c             C   s
   | |k S )Nr   )rE   rL   r   r   r   rN      s    zName.__ne__c             C   s   t t| jS )N)rO   tuplerX   )rE   r   r   r   rP      s    zName.__hash__c             c   s(   x"| j D ]}x|D ]
}|V  qW qW d S )N)rX   )rE   rdnZavar   r   r   ra      s    
zName.__iter__c             C   s   t dd | jD S )Nc             s   s   | ]}t |V  qd S )N)r<   )r   ri   r   r   r   rU      s    zName.__len__.<locals>.<genexpr>)sumrX   )rE   r   r   r   rb      s    zName.__len__c             C   s:   dj dd | jD }tjr,dj|jdS dj|S d S )Nr'   c             s   s   | ]}|j  V  qd S )N)rJ   )r   r^   r   r   r   rU     s    z Name.__repr__.<locals>.<genexpr>z
<Name({})>r4   )r_   rX   r8   PY2rQ   r=   )rE   re   r   r   r   rR     s    zName.__repr__N)r   r   r   rG   rJ   r]   propertyre   rg   rM   rN   rP   ra   rb   rR   r   r   r   r   rc      s   rc   )%
__future__r   r   r   enumr   r8   Zcryptographyr   Zcryptography.x509.oidr   r   r	   Z_ASN1_TYPE_TO_ENUMobjectr?   r:   r   r;   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_IDrH   r2   r3   rS   rc   r   r   r   r   <module>   s4   
E1