o
    Vh9                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dl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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d
ed< ejeef Zejeef 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ed< dd	 e'( D Z)ejdejdej diZ*d*ddZ+d+d d!Z,G d"d# d#Z-G d$d% d%Z.G d&d' d'Z/G d(d) d)Z0dS ),    )annotationsN)utils)x509)NameOIDObjectIdentifierc                   @  s<   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ZdZdS )	_ASN1Type                                    N)__name__
__module____qualname__	BitStringOctetString
UTF8StringNumericStringPrintableString	T61String	IA5StringUTCTimeGeneralizedTimeVisibleStringUniversalString	BMPString r#   r#   j/var/www/html/construction_image-detection-poc/venv/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!dict[ObjectIdentifier, _ASN1Type]_NAMEOID_DEFAULT_TYPECNLSTOOUCSTREETDCUID_OidNameMap_NAMEOID_TO_NAMEc                 C  s   i | ]\}}||qS r#   r#   )r'   kvr#   r#   r$   r)   <   s    )   r8   )   @   valstr | bytesreturnstrc                 C  s   | sdS t | trdt| d S | dd} | dd} | dd	} | d
d} | dd} | dd} | dd} | dd} | d dv rMd|  } | d dkr[| dd d } | S )z>Escape special characters in RFC4514 Distinguished Name value. #utf8\z\\"z\"+z\+,z\,;z\;<z\<>z\> z\00r   )r@    rJ   Nz\ )
isinstancebytesbinasciihexlifydecodereplace)r;   r#   r#   r$   _escape_dn_valueE   s"   
rR   c                 C  s   | sdS dd }t j|| S )Nr?   c                 S  s(   |  d}t|dkr|S tt|dS )Nr9      )grouplenchrint)mr;   r#   r#   r$   subj   s   
z_unescape_dn_value.<locals>.sub)_RFC4514NameParser_PAIR_RErY   )r;   rY   r#   r#   r$   _unescape_dn_valueb   s   r\   c                   @  sv   e Zd Z	d$ddd%ddZed&ddZed'ddZed(ddZ	d$d)ddZd*ddZ	d+d d!Z
d(d"d#ZdS ),NameAttributeNT)	_validateoidr   r%   r<   _type_ASN1Type | Noner^   boolr=   Nonec          
      C  s  t |ts	td|tjkr!|tjkrtdt |ts tdn	t |ts*tdt	
|}|d urg|\}}t |ts>J t|d}||k sM||krgd| d| d| }	|d	u r`t|	tj|	d
d |d u rrt
|tj}t |ts{td|| _|| _|| _d S )Nz2oid argument must be an ObjectIdentifier instance.z6oid must be X500_UNIQUE_IDENTIFIER for BitString type.z!value must be bytes for BitStringzvalue argument must be a strrA   zAttribute's length must be >= z and <= z, but it was Tr8   )
stacklevelz%_type must be from the _ASN1Type enum)rL   r   	TypeErrorr   r   r   X500_UNIQUE_IDENTIFIERrM   r>   _NAMEOID_LENGTH_LIMITgetrU   encode
ValueErrorwarningswarnr*   r   _oid_valuer`   )
selfr_   r%   r`   r^   length_limits
min_length
max_lengthc_lenmsgr#   r#   r$   __init__v   sH   







zNameAttribute.__init__c                 C     | j S N)rm   ro   r#   r#   r$   r_         zNameAttribute.oidc                 C  rv   rw   )rn   rx   r#   r#   r$   r%      ry   zNameAttribute.valuer>   c                 C  s   t | j| jjS )zt
        The short attribute name (for example "CN") if available,
        otherwise the OID dotted string.
        )r5   rh   r_   dotted_stringrx   r#   r#   r$   rfc4514_attribute_name      z$NameAttribute.rfc4514_attribute_nameattr_name_overrides_OidNameMap | Nonec                 C  s6   |r| | jnd}|du r| j}| dt| j S )z
        Format as RFC4514 Distinguished Name string.

        Use short attribute name if available, otherwise fall back to OID
        dotted string.
        N=)rh   r_   r{   rR   r%   )ro   r}   	attr_namer#   r#   r$   rfc4514_string   s
   
zNameAttribute.rfc4514_stringotherobjectc                 C  s&   t |tstS | j|jko| j|jkS rw   )rL   r]   NotImplementedr_   r%   ro   r   r#   r#   r$   __eq__   s   
zNameAttribute.__eq__rW   c                 C  s   t | j| jfS rw   )hashr_   r%   rx   r#   r#   r$   __hash__      zNameAttribute.__hash__c                 C  s   d| j  d| jdS )Nz<NameAttribute(oid=z, value=)>)r_   r%   rx   r#   r#   r$   __repr__   s   zNameAttribute.__repr__rw   )
r_   r   r%   r<   r`   ra   r^   rb   r=   rc   )r=   r   )r=   r<   r=   r>   r}   r~   r=   r>   r   r   r=   rb   r=   rW   )r   r   r   ru   propertyr_   r%   r{   r   r   r   r   r#   r#   r#   r$   r]   u   s     6

r]   c                   @  s`   e Zd Zd ddZd!d	d
Z	d"d#ddZd$ddZd%ddZd&ddZd%ddZ	d'ddZ
dS )(RelativeDistinguishedName
attributestyping.Iterable[NameAttribute]c                 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 rw   rL   r]   r'   xr#   r#   r$   	<genexpr>       z5RelativeDistinguishedName.__init__.<locals>.<genexpr>z/attributes must be an iterable of NameAttributez$duplicate attributes are not allowed)listrj   allre   _attributes	frozenset_attribute_setrU   ro   r   r#   r#   r$   ru      s   
z"RelativeDistinguishedName.__init__r_   r   r=   list[NameAttribute]c                       fdd| D S )Nc                      g | ]	}|j  kr|qS r#   r_   r&   r   r#   r$   
<listcomp>       zDRelativeDistinguishedName.get_attributes_for_oid.<locals>.<listcomp>r#   ro   r_   r#   r   r$   get_attributes_for_oid      z0RelativeDistinguishedName.get_attributes_for_oidNr}   r~   r>   c                   s   d  f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.
        rD   c                 3      | ]}|  V  qd S rw   r   r'   attrr}   r#   r$   r      
    
z;RelativeDistinguishedName.rfc4514_string.<locals>.<genexpr>joinr   ro   r}   r#   r   r$   r      s   	z(RelativeDistinguishedName.rfc4514_stringr   r   rb   c                 C     t |tstS | j|jkS rw   )rL   r   r   r   r   r#   r#   r$   r         
z RelativeDistinguishedName.__eq__rW   c                 C  
   t | jS rw   )r   r   rx   r#   r#   r$   r        
z"RelativeDistinguishedName.__hash__typing.Iterator[NameAttribute]c                 C  r   rw   )iterr   rx   r#   r#   r$   __iter__  r   z"RelativeDistinguishedName.__iter__c                 C  r   rw   )rU   r   rx   r#   r#   r$   __len__  r   z!RelativeDistinguishedName.__len__c                 C  s   d|    dS )Nz<RelativeDistinguishedName(r   r   rx   r#   r#   r$   r     r   z"RelativeDistinguishedName.__repr__)r   r   r_   r   r=   r   rw   r   r   r   r=   r   r   )r   r   r   ru   r   r   r   r   r   r   r   r#   r#   r#   r$   r      s    





r   c                   @  s   e Zd Zejd1ddZejd2ddZd3d
dZe	d4d5ddZ	d4d6ddZd7ddZ	e
d8ddZd4d9d d!Zd:d%d&Zd;d(d)Zd<d+d,Zd;d-d.Zd=d/d0ZdS )>Namer   r   r=   rc   c                 C     d S rw   r#   r   r#   r#   r$   ru     s   zName.__init__*typing.Iterable[RelativeDistinguishedName]c                 C  r   rw   r#   r   r#   r#   r$   ru     s   :typing.Iterable[NameAttribute | RelativeDistinguishedName]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   rw   r   r   r#   r#   r$   r     r   z Name.__init__.<locals>.<genexpr>c                 S  s   g | ]}t tt|gqS r#   )r   typingcastr]   r   r#   r#   r$   r     s    z!Name.__init__.<locals>.<listcomp>c                 s  r   rw   )rL   r   r   r#   r#   r$   r   "  r   zNattributes must be a list of NameAttribute or a list RelativeDistinguishedName)r   r   r   r   r   Listr   re   r   r#   r#   r$   ru     s   

Ndatar>   r}   _NameOidMap | Nonec                 C  s   t ||pi  S rw   )rZ   parse)clsr   r}   r#   r#   r$   from_rfc4514_string,  r|   zName.from_rfc4514_stringr~   c                   s   d  f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.
        rE   c                 3  r   rw   r   r   r   r#   r$   r   A  r   z&Name.rfc4514_string.<locals>.<genexpr>)r   reversedr   r   r#   r   r$   r   4  s   zName.rfc4514_stringr_   r   r   c                   r   )Nc                   r   r#   r   r&   r   r#   r$   r   I  r   z/Name.get_attributes_for_oid.<locals>.<listcomp>r#   r   r#   r   r$   r   F  r   zName.get_attributes_for_oidlist[RelativeDistinguishedName]c                 C  rv   rw   r   rx   r#   r#   r$   rdnsK  ry   z	Name.rdnsbackend
typing.AnyrM   c                 C  s
   t | S rw   )	rust_x509encode_name_bytes)ro   r   r#   r#   r$   public_bytesO  r   zName.public_bytesr   r   rb   c                 C  r   rw   )rL   r   r   r   r   r#   r#   r$   r   R  r   zName.__eq__rW   c                 C  s   t t| jS rw   )r   tupler   rx   r#   r#   r$   r   X  s   zName.__hash__r   c                 c  s    | j D ]}|E d H  qd S rw   r   )ro   rdnr#   r#   r$   r   ]  s   
zName.__iter__c                 C  s   t dd | jD S )Nc                 s  s    | ]}t |V  qd S rw   )rU   )r'   r   r#   r#   r$   r   b      zName.__len__.<locals>.<genexpr>)sumr   rx   r#   r#   r$   r   a  s   zName.__len__c                 C  s"   d dd | jD }d| dS )NrE   c                 s  s    | ]}|  V  qd S rw   r   r   r#   r#   r$   r   e  r   z Name.__repr__.<locals>.<genexpr>z<Name(r   r   ro   r   r#   r#   r$   r   d  s   zName.__repr__)r   r   r=   rc   )r   r   r=   rc   )r   r   r=   rc   rw   )r   r>   r}   r   r=   r   r   r   )r=   r   )r   r   r=   rM   r   r   r   r   )r   r   r   r   overloadru   classmethodr   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   r     s(    





r   c                   @  s  e Zd ZedZedZdZeeZdZ	dZ
dZdeej dZe	 d	e Ze
 d	e Ze d	e Zed
e d	e de d	e de d	e dejZedZd,ddZd-ddZd.ddZd/ddZd0d d!Zd1d#d$Zd2d&d'Zd3d)d*Zd+S )4rZ   z!(0|([1-9]\d*))(\.(0|([1-9]\d*)))+z[a-zA-Z][a-zA-Z\d-]*z!\\([\\ #=\"\+,;<>]|[\da-zA-Z]{2})z7[\x01-\x1f\x21\x24-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]z3[\x01-\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]z7[\x01-\x1F\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]z[\x80-]|z
        (
            (z!)
            (
                (z)*
                (z$)
            )?
        )?
        z#([\da-zA-Z]{2})+r   r>   r}   _NameOidMapr=   rc   c                 C  s   || _ d| _|| _d S )Nr   )_data_idx_attr_name_overrides)ro   r   r}   r#   r#   r$   ru     s   
z_RFC4514NameParser.__init__rb   c                 C  s   | j t| jk S rw   )r   rU   r   rx   r#   r#   r$   	_has_data  r   z_RFC4514NameParser._has_data
str | Nonec                 C  s   |   r
| j| j S d S rw   )r   r   r   rx   r#   r#   r$   _peek  s   z_RFC4514NameParser._peekchc                 C  s"   |   |krt|  jd7  _d S )Nr9   )r   rj   r   )ro   r   r#   r#   r$   
_read_char  s   z_RFC4514NameParser._read_charc                 C  s<   |j | j| jd}|d u rt| }|  jt|7  _|S )N)pos)matchr   r   rj   rT   rU   )ro   patr   r;   r#   r#   r$   _read_re  s   z_RFC4514NameParser._read_rer   c                 C  sN   |   stg S |  g}|   r!| d ||   |   stt|S )a'  
        Parses the `data` string and converts it to a Name.

        According to RFC4514 section 2.1 the RDNSequence must be
        reversed when converting to string representation. So, when
        we parse it, we need to reverse again to get the RDNs on the
        correct order.
        rE   )r   r   
_parse_rdnr   appendr   r   r#   r#   r$   r     s   


z_RFC4514NameParser.parser   c                 C  sB   |   g}|  dkr| d ||    |  dkst|S )NrD   )	_parse_nar   r   r   r   )ro   nasr#   r#   r$   r     s   

z_RFC4514NameParser._parse_rdnr]   c              	   C  s   z|  | j}W n ty'   |  | j}| j|t|}|d u r%tY nw t|}| d | 	 dkrI|  | j
}t|dd   }n
|  | j}t|}t||S )Nr   r@   r9   )r   _OID_RErj   	_DESCR_REr   rh   _NAME_TO_NAMEOIDr   r   r   _HEXSTRING_RErN   	unhexlifyrP   
_STRING_REr\   r]   )ro   	oid_valuenamer_   r%   	raw_valuer#   r#   r$   r     s&   


z_RFC4514NameParser._parse_naN)r   r>   r}   r   r=   rc   )r=   rb   )r=   r   )r   r>   r=   rc   r   )r=   r   )r=   r   )r=   r]   )r   r   r   recompiler   r   _PAIRr[   _LUTF1_SUTF1_TUTF1rV   sys
maxunicode_UTFMB	_LEADCHAR_STRINGCHAR
_TRAILCHARVERBOSEr   r   ru   r   r   r   r   r   r   r   r#   r#   r#   r$   rZ   i  sJ    


	







rZ   )r;   r<   r=   r>   )r;   r>   r=   r>   )1
__future__r   rN   r   r   r   rk   cryptographyr   "cryptography.hazmat.bindings._rustr   r   cryptography.x509.oidr   r   Enumr   _ASN1_TYPE_TO_ENUMCOUNTRY_NAMEr   JURISDICTION_COUNTRY_NAMESERIAL_NUMBERDN_QUALIFIEREMAIL_ADDRESSr   DOMAIN_COMPONENTr*   __annotations__Mappingr>   r4   r   COMMON_NAMELOCALITY_NAMESTATE_OR_PROVINCE_NAMEORGANIZATION_NAMEORGANIZATIONAL_UNIT_NAMESTREET_ADDRESSUSER_IDr5   itemsr   rg   rR   r\   r]   r   r   rZ   r#   r#   r#   r$   <module>   sR   


e5Z