U
    e]                     @   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   8/usr/lib/python3/dist-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    r2   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   rC   r   r   r   __init__L   s2    

zNameAttribute.__init__rA   rB   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@   rE   Zdotted_stringr2   r   )rD   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 r   )r6   r3   NotImplementedrE   r   rD   otherr   r   r   __eq__   s
    

zNameAttribute.__eq__c                 C   s
   | |k S r   r   rK   r   r   r   __ne__   s    zNameAttribute.__ne__c                 C   s   t | j| jfS r   )hashrE   r   rD   r   r   r   __hash__   s    zNameAttribute.__hash__c                 C   s
   d | S )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)formatrP   r   r   r   __repr__   s    zNameAttribute.__repr__N)r   r   r   r>   rF   r   Zread_only_propertyrE   r   rI   rM   rN   rQ   rS   r   r   r   r   r3   K   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 r   r6   r3   r   xr   r   r   r!      s     z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)listr=   allr7   _attributes	frozenset_attribute_setr;   rD   Z
attributesr   r   r   rF      s    
z"RelativeDistinguishedName.__init__c                    s    fdd| D S )Nc                    s   g | ]}|j  kr|qS r   rE   r   r^   r   r   
<listcomp>   s     
 zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   rD   rE   r   r^   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 r   rI   r   attrr   r   r   r!      s     z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>joinrZ   rP   r   r   r   rI      s    z(RelativeDistinguishedName.rfc4514_stringc                 C   s   t |tstS | j|jkS r   )r6   rT   rJ   r\   rK   r   r   r   rM      s    
z RelativeDistinguishedName.__eq__c                 C   s
   | |k S r   r   rK   r   r   r   rN      s    z RelativeDistinguishedName.__ne__c                 C   s
   t | jS r   )rO   r\   rP   r   r   r   rQ      s    z"RelativeDistinguishedName.__hash__c                 C   s
   t | jS r   )iterrZ   rP   r   r   r   __iter__   s    z"RelativeDistinguishedName.__iter__c                 C   s
   t | jS r   )r;   rZ   rP   r   r   r   __len__   s    z!RelativeDistinguishedName.__len__c                 C   s   d |  S )Nz<RelativeDistinguishedName({})>)rR   rI   rP   r   r   r   rS      s    z"RelativeDistinguishedName.__repr__N)r   r   r   rF   ra   rI   rM   rN   rQ   rh   ri   rS   r   r   r   r   rT      s   	rT   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 r   rU   rV   r   r   r   r!      s     z Name.__init__.<locals>.<genexpr>c                 S   s   g | ]}t |gqS r   )rT   rV   r   r   r   r_      s    z!Name.__init__.<locals>.<listcomp>c                 s   s   | ]}t |tV  qd S r   )r6   rT   rV   r   r   r   r!      s     zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)rX   rY   rZ   r7   r]   r   r   r   rF      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 r   rb   rc   r   r   r   r!      s     z&Name.rfc4514_string.<locals>.<genexpr>re   rP   r   r   r   rI      s    
zName.rfc4514_stringc                    s    fdd| D S )Nc                    s   g | ]}|j  kr|qS r   r^   r   r^   r   r   r_      s     
 z/Name.get_attributes_for_oid.<locals>.<listcomp>r   r`   r   r^   r   ra      s    zName.get_attributes_for_oidc                 C   s   | j S r   rZ   rP   r   r   r   rdns   s    z	Name.rdnsc                 C   s
   | | S r   )Zx509_name_bytes)rD   Zbackendr   r   r   public_bytes   s    zName.public_bytesc                 C   s   t |tstS | j|jkS r   )r6   rj   rJ   rZ   rK   r   r   r   rM      s    
zName.__eq__c                 C   s
   | |k S r   r   rK   r   r   r   rN      s    zName.__ne__c                 C   s   t t| jS r   )rO   tuplerZ   rP   r   r   r   rQ      s    zName.__hash__c                 c   s    | j D ]}|D ]
}|V  qqd S r   rk   )rD   rdnZavar   r   r   rh      s    
zName.__iter__c                 C   s   t dd | jD S )Nc                 s   s   | ]}t |V  qd S r   )r;   )r   ro   r   r   r   r!      s     zName.__len__.<locals>.<genexpr>)sumrZ   rP   r   r   r   ri      s    zName.__len__c                 C   s,   t jrd|  dS d|  S d S )Nz
<Name({})>r4   )r8   ZPY2rR   rI   r<   rP   r   r   r   rS      s    zName.__repr__N)r   r   r   rF   rI   ra   propertyrl   rm   rM   rN   rQ   rh   ri   rS   r   r   r   r   rj      s   
rj   )&Z
__future__r   r   r   enumr   r8   Zcryptographyr   Zcryptography.x509.oidr   r   r	   dictZ_ASN1_TYPE_TO_ENUMobjectr>   r9   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_IDrG   r2   r3   rT   rj   r   r   r   r   <module>   sV                  H1