B
    f]                 @   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e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   5lib/python3.7/site-packages/cryptography/x509/name.pyr	      s   r	   c             c   s   | ]}|j |fV  qd S )N)value).0ir   r   r   	<genexpr>   s    r   ZCNLZSTOZOUCZSTREETZDCZUIDc             C   s   |  dd} |  dd} |  dd} |  dd} |  d	d
} |  dd} |  dd} |  dd} | d dkrtd|  } | 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   )# r,   Nz\ )replace)valr   r   r   _escape_dn_value6   s    r0   c               @   sT   e Zd ZefddZedZe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|	ddkrTt
dt|dkrht
d|tkr~t|tj}t |tst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 coder   zValue cannot be an empty stringz%_type must be from the _ASN1Type enum)
isinstancer   	TypeErrorsixZ	text_typer   COUNTRY_NAMEJURISDICTION_COUNTRY_NAMElenencode
ValueError	_SENTINEL_NAMEOID_DEFAULT_TYPEgetr	   r   _oid_value_type)selfoidr   rA   r   r   r   __init__L   s(    



zNameAttribute.__init__r?   r@   c             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>   rC   Zdotted_stringr0   r   )rB   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)r4   r1   NotImplementedrC   r   )rB   otherr   r   r   __eq__   s    
zNameAttribute.__eq__c             C   s
   | |k S )Nr   )rB   rI   r   r   r   __ne__   s    zNameAttribute.__ne__c             C   s   t | j| jfS )N)hashrC   r   )rB   r   r   r   __hash__   s    zNameAttribute.__hash__c             C   s
   d | S )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)format)rB   r   r   r   __repr__   s    zNameAttribute.__repr__N)r   r   r   r<   rD   r   Zread_only_propertyrC   r   rG   rJ   rK   rM   rO   r   r   r   r   r1   K   s   '


	r1   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)r4   r1   )r   xr   r   r   r      s    z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)listr;   allr5   _attributes	frozenset_attribute_setr9   )rB   
attributesr   r   r   rD      s    
z"RelativeDistinguishedName.__init__c                s    fdd| D S )Nc                s   g | ]}|j  kr|qS r   )rC   )r   r   )rC   r   r   
<listcomp>   s    zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   )rB   rC   r   )rC   r   get_attributes_for_oid   s    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   s   | ]}|  V  qd S )N)rG   )r   attrr   r   r   r      s    z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>)joinrT   )rB   r   r   r   rG      s    z(RelativeDistinguishedName.rfc4514_stringc             C   s   t |tstS | j|jkS )N)r4   rP   rH   rV   )rB   rI   r   r   r   rJ      s    
z RelativeDistinguishedName.__eq__c             C   s
   | |k S )Nr   )rB   rI   r   r   r   rK      s    z RelativeDistinguishedName.__ne__c             C   s
   t | jS )N)rL   rV   )rB   r   r   r   rM      s    z"RelativeDistinguishedName.__hash__c             C   s
   t | jS )N)iterrT   )rB   r   r   r   __iter__   s    z"RelativeDistinguishedName.__iter__c             C   s
   t | jS )N)r9   rT   )rB   r   r   r   __len__   s    z!RelativeDistinguishedName.__len__c             C   s   d |  S )Nz<RelativeDistinguishedName({})>)rN   rG   )rB   r   r   r   rO      s    z"RelativeDistinguishedName.__repr__N)r   r   r   rD   rY   rG   rJ   rK   rM   r]   r^   rO   r   r   r   r   rP      s   	rP   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)r4   r1   )r   rQ   r   r   r   r      s    z Name.__init__.<locals>.<genexpr>c             S   s   g | ]}t |gqS r   )rP   )r   rQ   r   r   r   rX      s    z!Name.__init__.<locals>.<listcomp>c             s   s   | ]}t |tV  qd S )N)r4   rP   )r   rQ   r   r   r   r      s    zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)rR   rS   rT   r5   )rB   rW   r   r   r   rD      s    zName.__init__c             C   s   d dd | 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.
        r&   c             s   s   | ]}|  V  qd S )N)rG   )r   rZ   r   r   r   r      s    z&Name.rfc4514_string.<locals>.<genexpr>)r[   rT   )rB   r   r   r   rG      s    
zName.rfc4514_stringc                s    fdd| D S )Nc                s   g | ]}|j  kr|qS r   )rC   )r   r   )rC   r   r   rX      s    z/Name.get_attributes_for_oid.<locals>.<listcomp>r   )rB   rC   r   )rC   r   rY      s    zName.get_attributes_for_oidc             C   s   | j S )N)rT   )rB   r   r   r   rdns   s    z	Name.rdnsc             C   s
   | | S )N)Zx509_name_bytes)rB   Zbackendr   r   r   public_bytes   s    zName.public_bytesc             C   s   t |tstS | j|jkS )N)r4   r_   rH   rT   )rB   rI   r   r   r   rJ      s    
zName.__eq__c             C   s
   | |k S )Nr   )rB   rI   r   r   r   rK      s    zName.__ne__c             C   s   t t| jS )N)rL   tuplerT   )rB   r   r   r   rM      s    zName.__hash__c             c   s(   x"| j D ]}x|D ]
}|V  qW qW d S )N)rT   )rB   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)r9   )r   rc   r   r   r   r      s    zName.__len__.<locals>.<genexpr>)sumrT   )rB   r   r   r   r^      s    zName.__len__c             C   s,   t jrd|  dS d|  S d S )Nz
<Name({})>r2   )r6   ZPY2rN   rG   r:   )rB   r   r   r   rO      s    zName.__repr__N)r   r   r   rD   rG   rY   propertyr`   ra   rJ   rK   rM   r]   r^   rO   r   r   r   r   r_      s   r_   )&Z
__future__r   r   r   enumr   r6   Zcryptographyr   Zcryptography.x509.oidr   r   r	   dictZ_ASN1_TYPE_TO_ENUMobjectr<   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   r0   r1   rP   r_   r   r   r   r   <module>   s4   
H1