U
    [eQ!                     @   s
  d dl Z d dlmZ d dlm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 )    N)Enum)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   X/var/www/html/services/stratfitenv/lib/python3.8/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DCUIDc                 C   s   | sdS |  dd} |  dd} |  dd} |  dd	} |  d
d} |  dd} |  dd} |  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   )# r-   Nz\ )replace)valr   r   r   _escape_dn_value4   s    r1   c                   @   s~   e Zd ZefeedddZedZ	edZ
edddZeed	d
dZeed	ddZedddZedddZdS )NameAttribute)oidr   c                 C   s   t |tstdt |ts$td|tjks8|tjkrRt|ddkrRt	d|t
krht|tj}t |tszt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)selfr3   r   rC   r   r   r   __init__M   s*    



zNameAttribute.__init__rA   rB   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@   r3   Zdotted_stringr1   r   )rD   keyr   r   r   rfc4514_stringr   s    zNameAttribute.rfc4514_stringotherrG   c                 C   s&   t |tstS | j|jko$| j|jkS N)r6   r2   NotImplementedr3   r   rD   rL   r   r   r   __eq__|   s    
zNameAttribute.__eq__c                 C   s
   | |k S rM   r   rO   r   r   r   __ne__   s    zNameAttribute.__ne__c                 C   s   t | j| jfS rM   )hashr3   r   rD   r   r   r   __hash__   s    zNameAttribute.__hash__c                 C   s
   d | S )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)formatrS   r   r   r   __repr__   s    zNameAttribute.__repr__N)r   r   r   r>   r   r8   rE   r   Zread_only_propertyr3   r   rJ   objectboolrP   rQ   intrT   rV   r   r   r   r   r2   L   s   "


r2   c                   @   s   e Zd Zeje dddZeje dddZe	dddZ
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 rM   r6   r2   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;   rD   r[   r   r   r   rE      s    
z"RelativeDistinguishedName.__init__rF   c                    s    fdd| D S )Nc                    s   g | ]}|j  kr|qS r   r3   r   rf   r   r   
<listcomp>   s     
 zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   rD   r3   r   rf   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 rM   rJ   r   attrr   r   r   r_      s     z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>)joinrb   rS   r   r   r   rJ      s    z(RelativeDistinguishedName.rfc4514_stringrK   c                 C   s   t |tstS | j|jkS rM   )r6   rZ   rN   rd   rO   r   r   r   rP      s    
z RelativeDistinguishedName.__eq__c                 C   s
   | |k S rM   r   rO   r   r   r   rQ      s    z RelativeDistinguishedName.__ne__c                 C   s
   t | jS rM   )rR   rd   rS   r   r   r   rT      s    z"RelativeDistinguishedName.__hash__c                 C   s
   t | jS rM   )iterrb   rS   r   r   r   __iter__   s    z"RelativeDistinguishedName.__iter__c                 C   s
   t | jS rM   )r;   rb   rS   r   r   r   __len__   s    z!RelativeDistinguishedName.__len__c                 C   s   d |  S )Nz<RelativeDistinguishedName({})>)rU   rJ   rS   r   r   r   rV      s    z"RelativeDistinguishedName.__repr__N)r   r   r   typingIterabler2   rE   Listri   r8   rJ   rW   rX   rP   rQ   rY   rT   Iteratorro   rp   rV   r   r   r   r   rZ      s   	rZ   c                   @   s   e Zd Zdd ZedddZeje dddZ	e
eje ddd	ZdedddZ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 )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 rM   r\   r]   r   r   r   r_      s     z Name.__init__.<locals>.<genexpr>c                 S   s   g | ]}t |gqS r   )rZ   r]   r   r   r   rg      s    z!Name.__init__.<locals>.<listcomp>c                 s   s   | ]}t |tV  qd S rM   )r6   rZ   r]   r   r   r   r_      s     zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)r`   ra   rb   r7   re   r   r   r   rE      s    
zName.__init__rF   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   s   | ]}|  V  qd S rM   rj   rk   r   r   r   r_      s    z&Name.rfc4514_string.<locals>.<genexpr>)rm   reversedrb   rS   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   rf   r   r   rg      s     
 z/Name.get_attributes_for_oid.<locals>.<listcomp>r   rh   r   rf   r   ri      s    zName.get_attributes_for_oidc                 C   s   | j S rM   rb   rS   r   r   r   rdns   s    z	Name.rdnsNc                 C   s   t |}|| S rM   )r   Zx509_name_bytes)rD   backendr   r   r   public_bytes   s    zName.public_bytesrK   c                 C   s   t |tstS | j|jkS rM   )r6   ru   rN   rb   rO   r   r   r   rP      s    
zName.__eq__c                 C   s
   | |k S rM   r   rO   r   r   r   rQ      s    zName.__ne__c                 C   s   t t| jS rM   )rR   tuplerb   rS   r   r   r   rT      s    zName.__hash__c                 c   s    | j D ]}|D ]
}|V  qqd S rM   rw   )rD   rdnZavar   r   r   ro      s    
zName.__iter__c                 C   s   t dd | jD S )Nc                 s   s   | ]}t |V  qd S rM   )r;   )r   r|   r   r   r   r_      s     zName.__len__.<locals>.<genexpr>)sumrb   rS   r   r   r   rp      s    zName.__len__c                 C   s    d dd | jD }d|S )Nr'   c                 s   s   | ]}|  V  qd S rM   rj   rk   r   r   r   r_      s     z Name.__repr__.<locals>.<genexpr>z
<Name({})>)rm   rb   rU   )rD   rx   r   r   r   rV      s    zName.__repr__)N)r   r   r   rE   r8   rJ   rq   rs   r2   ri   propertyrr   rZ   rx   bytesrz   rW   rX   rP   rQ   rY   rT   rt   ro   rp   rV   r   r   r   r   ru      s   ru   )#rq   enumr   Zcryptographyr   Zcryptography.hazmat.backendsr   Zcryptography.x509.oidr   r   r   Z_ASN1_TYPE_TO_ENUMrW   r>   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_IDrH   r1   r2   rZ   ru   r   r   r   r   <module>   sV                  @1