o
    b9%                     @   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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ejdi
Zde de f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)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   U/var/www/secure340b-portal/env/lib/python3.10/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UIDEvalreturnc                 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   )# r0   Nz\ )replace)r$   r   r   r   _escape_dn_value5   s   r3   c                   @   s   e Zd ZefdededdfddZedefddZedefd	d
Z	ed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fddZdS )NameAttributeoidr   r%   Nc                 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 str.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)selfr5   r   rE   r   r   r   __init__N   s*   




zNameAttribute.__init__c                 C      | j S N)rC   rF   r   r   r   r5   r      zNameAttribute.oidc                 C   rH   rI   )rD   rJ   r   r   r   r   v   rK   zNameAttribute.valuec                 C   s   t | j| jjS )zt
        The short attribute name (for example "CN") if available,
        otherwise the OID dotted string.
        )_NAMEOID_TO_NAMErB   r5   Zdotted_stringrJ   r   r   r   rfc4514_attribute_namez   s   z$NameAttribute.rfc4514_attribute_namec                 C   s   d| j 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)rM   r3   r   rJ   r   r   r   rfc4514_string   s   zNameAttribute.rfc4514_stringotherc                 C   s&   t |tstS | j|jko| j|jkS rI   )r8   r4   NotImplementedr5   r   rF   rO   r   r   r   __eq__   s   
zNameAttribute.__eq__c                 C   
   | |k S rI   r   rQ   r   r   r   __ne__      
zNameAttribute.__ne__c                 C   s   t | j| jfS rI   )hashr5   r   rJ   r   r   r   __hash__   s   zNameAttribute.__hash__c                 C   s
   d | S )Nz/<NameAttribute(oid={0.oid}, value={0.value!r})>)formatrJ   r   r   r   __repr__   rU   zNameAttribute.__repr__)r   r   r   r@   r   r:   rG   propertyr5   r   rM   rN   objectboolrR   rT   intrW   rY   r   r   r   r   r4   M   s(    
$r4   c                   @   s   e Zd Zdeje fddZde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   r8   r4   r   xr   r   r   	<genexpr>       z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)listr?   allr9   _attributes	frozenset_attribute_setr=   rF   r_   r   r   r   rG      s   
z"RelativeDistinguishedName.__init__r5   r%   c                        fdd| D S )Nc                       g | ]	}|j  kr|qS r   r5   r   rn   r   r   
<listcomp>       zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r   rF   r5   r   rn   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   rN   r   attrr   r   r   rd          z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>)joinrh   rJ   r   r   r   rN      s   z(RelativeDistinguishedName.rfc4514_stringrO   c                 C      t |tstS | j|jkS rI   )r8   r^   rP   rj   rQ   r   r   r   rR         
z RelativeDistinguishedName.__eq__c                 C   rS   rI   r   rQ   r   r   r   rT      rU   z RelativeDistinguishedName.__ne__c                 C   
   t | jS rI   )rV   rj   rJ   r   r   r   rW      rU   z"RelativeDistinguishedName.__hash__c                 C   r|   rI   )iterrh   rJ   r   r   r   __iter__   rU   z"RelativeDistinguishedName.__iter__c                 C   r|   rI   )r=   rh   rJ   r   r   r   __len__   rU   z!RelativeDistinguishedName.__len__c                 C   s   d |  S )Nz<RelativeDistinguishedName({})>)rX   rN   rJ   r   r   r   rY      s   z"RelativeDistinguishedName.__repr__N)r   r   r   typingIterabler4   rG   r   Listrr   r:   rN   r[   r\   rR   rT   r]   rW   Iteratorr~   r   rY   r   r   r   r   r^      s    
	r^   c                   @   s$  e Zd Zejdeje ddfddZejdeje ddfddZdejej	eef  ddfddZde
fdd	Zd
edeje fddZedeje fddZddeje 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 ) Namer_   r%   Nc                 C      d S rI   r   rk   r   r   r   rG      s   zName.__init__c                 C   r   rI   r   rk   r   r   r   rG      s   c                 C   s`   t |}tdd |D rdd |D | _d S tdd |D r,ttjt || _d S td)Nc                 s   r`   rI   ra   rb   r   r   r   rd      re   z Name.__init__.<locals>.<genexpr>c                 S   s   g | ]}t tt|gqS r   )r^   r   castr4   rb   r   r   r   ro      s    z!Name.__init__.<locals>.<listcomp>c                 s   r`   rI   )r8   r^   rb   r   r   r   rd      re   zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)rf   rg   rh   r   r   r   r^   r9   rk   r   r   r   rG      s   

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   rt   rI   ru   rv   r   r   r   rd      s    
z&Name.rfc4514_string.<locals>.<genexpr>)ry   reversedrh   rJ   r   r   r   rN      s   
zName.rfc4514_stringr5   c                    rl   )Nc                    rm   r   rn   r   rn   r   r   ro     rp   z/Name.get_attributes_for_oid.<locals>.<listcomp>r   rq   r   rn   r   rr     rs   zName.get_attributes_for_oidc                 C   rH   rI   rh   rJ   r   r   r   rdns  rK   z	Name.rdnsbackendc                 C   s   t |}|| S rI   )r   Zx509_name_bytes)rF   r   r   r   r   public_bytes
  s   
zName.public_bytesrO   c                 C   rz   rI   )r8   r   rP   rh   rQ   r   r   r   rR     r{   zName.__eq__c                 C   rS   rI   r   rQ   r   r   r   rT     rU   zName.__ne__c                 C   s   t t| jS rI   )rV   tuplerh   rJ   r   r   r   rW     s   zName.__hash__c                 c   s"    | j D ]
}|D ]}|V  qqd S rI   r   )rF   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 rI   )r=   )r   r   r   r   r   rd   "  rx   zName.__len__.<locals>.<genexpr>)sumrh   rJ   r   r   r   r   !  s   zName.__len__c                 C   s    d dd | jD }d|S )Nr*   c                 s   rt   rI   ru   rv   r   r   r   rd   %  rx   z Name.__repr__.<locals>.<genexpr>z
<Name({})>)ry   rh   rX   )rF   r   r   r   r   rY   $  s   
zName.__repr__rI   )r   r   r   r   overloadr   r4   rG   r^   Unionr:   rN   r   r   rr   rZ   r   Optionalr   bytesr   r[   r\   rR   rT   r]   rW   r   r~   r   rY   r   r   r   r   r      s>    

r   )%r   Zcryptographyr   Zcryptography.hazmat.backendsr   Z'cryptography.hazmat.backends.interfacesr   Zcryptography.x509.oidr   r   Enumr   Z_ASN1_TYPE_TO_ENUMr[   r@   r;   r   r<   ZSERIAL_NUMBERZDN_QUALIFIERZEMAIL_ADDRESSr   ZDOMAIN_COMPONENTrA   ZCOMMON_NAMEZLOCALITY_NAMEZSTATE_OR_PROVINCE_NAMEZORGANIZATION_NAMEZORGANIZATIONAL_UNIT_NAMEZSTREET_ADDRESSZUSER_IDrL   r:   r3   r4   r^   r   r   r   r   r   <module>   s<   Q3