o
    6h<@                     @   s  d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 ddl
mZmZ G dd deZd	d
 Zd4ddZdd Ze ZefddZdd Zdd Zdd Zdd Zdd Zdd Zd4ddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Z d*d+ Z!d,d- Z"efd.d/Z#d0d1 Z$d2d3 Z%dS )5    )divisionN)chain)int2byte	text_type   )compat26_strstr_idx_as_intc                   @   s   e Zd ZdS )UnexpectedDERN)__name__
__module____qualname__ r   r   ]/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/der.pyr	      s    r	   c                 C   s   t d|  tt| | S )N   )r   encode_lengthlen)tagvaluer   r   r   encode_constructed   s   r   context-specificc                 C   sh   |dvrt d| dkrt d|dkrd}n|dkrd}n|d	ks$J d
}t||  tt| | S )a-  
    Encode and IMPLICIT value using :term:`DER`.

    :param int tag: the tag value to encode, must be between 0 an 31 inclusive
    :param bytes value: the data to encode
    :param str cls: the class of the tag to encode: "application",
      "context-specific", or "private"
    :rtype: bytes
    )applicationr   privatezinvalid tag class   zLong tags not supportedr   @   r      r      )
ValueErrorr   r   r   )r   r   cls	tag_classr   r   r   encode_implicit   s   
r   c                 C   sx   | dksJ d|    }t|d rd| }t|}t|d}|dkr.dtt| | S dtt|d  d | S )	Nr   %x      0      r       )encoder   binascii	unhexlifyr   r   )rhsnumr   r   r   encode_integer-   s   

r-   c                 C   s   d}d}|t u rtdt n5|durDd|  kr dks%td td|r>| s-tdt| d}|d	| d
 @ r>tdt|}d
}dtt| |  | |  S )a  
    Encode a binary string as a BIT STRING using :term:`DER` encoding.

    Note, because there is no native Python object that can encode an actual
    bit string, this function only accepts byte strings as the `s` argument.
    The byte string is the actual bit string that will be encoded, padded
    on the right (least significant bits, looking from big endian perspective)
    to the first full byte. If the bit string has a bit length that is multiple
    of 8, then the padding should not be included. For correct DER encoding
    the padding bits MUST be set to 0.

    Number of bits of padding need to be provided as the `unused` parameter.
    In case they are specified as None, it means the number of unused bits
    is already encoded in the string as the first byte.

    The deprecated call convention specifies just the `s` parameters and
    encodes the number of unused bits as first parameter (same convention
    as with None).

    Empty string must be encoded with `unused` specified as 0.

    Future version of python-ecdsa will make specifying the `unused` argument
    mandatory.

    :param s: bytes to encode
    :type s: bytes like object
    :param unused: number of bits at the end of `s` that are unused, must be
        between 0 and 7 (inclusive)
    :type unused: int or None

    :raises ValueError: when `unused` is too large or too small

    :return: `s` encoded using DER
    :rtype: bytes
        r   z:Legacy call convention used, unused= needs to be specifiedN   z&unused must be integer between 0 and 7z!unused is non-zero but s is emptyr!   r   z unused bits must be zeros in DER   )	_sentrywarningswarnDeprecationWarningr   r   r   r   r   )r+   unusedencoded_unused	len_extralastr   r   r   encode_bitstringB   s*   $
r:   c                 C   s   dt t|  |  S )N   )r   r   )r+   r   r   r   encode_octet_string{      r<   c                 G   s~   d|   kr
dk rn nd|  krdks!n | dkrd|ks!J d ttd|  | gdd |D }dtt| | S )	Nr   r!   '   r.   (   c                 s   s    | ]}t |V  qd S )N)encode_number.0pr   r   r   	<genexpr>   s    zencode_oid.<locals>.<genexpr>   )joinr   r@   r   r   )firstsecondpiecesbodyr   r   r   
encode_oid   s   BrK   c                  G   s(   t dd | D }dt| d|  S )Nc                 S      g | ]}t |qS r   )r   rA   r   r   r   
<listcomp>       z#encode_sequence.<locals>.<listcomp>r"   r.   )sumr   rF   )encoded_pieces	total_lenr   r   r   encode_sequence   s   rR   c                 C   sZ   g }| r| d| d@ dB  | d? } | s|s|d |d  dM  < ddd |D S )	Nr   r#   r   r/   r0   r.   c                 S   rL   r   )r   )rB   dr   r   r   rM      rN   z!encode_number.<locals>.<listcomp>)insertappendrF   )nb128_digitsr   r   r   r@      s   
r@   c                 C   s   | o	| d d dkS )Nr   r"   r   )stringr   r   r   is_sequence   r=   rY   c                 C   st   t | d}|d@ dkrtd| |d@ }t| dd  \}}| d| d| |  }| d| | d  }|||fS )Nr      r   z5wanted type 'constructed tag' (0xa0-0xbf), got 0x%02xr   r   r   r	   read_length)rX   s0r   lengthllenrJ   restr   r   r   remove_constructed   s   

ra   c           
      C   s   |dvrt d|dkrd}n|dkrd}n|dksJ d}d}t| d	}||@ |kr3td
|||d@ d	kr@td||d@ }t| dd \}}| d| d| |  }| d| | d }	|||	fS )a>  
    Removes an IMPLICIT tagged value from ``string`` following :term:`DER`.

    :param bytes string: a byte string that can have one or more
      DER elements.
    :param str exp_class: the expected tag class of the implicitly
      encoded value. Possible values are: "context-specific", "application",
      and "private".
    :return: a tuple with first value being the tag without indicator bits,
      second being the raw bytes of the value and the third one being
      remaining bytes (or an empty string if there are none)
    :rtype: tuple(int,bytes,bytes)
    )r   r   r   zinvalid `exp_class` valuer   r   r   r   r   r   r   z#wanted class {0}, got 0x{1:02x} tag    z(wanted type primitive, got 0x{0:02x} tagr   r   N)r   r   r	   formatr\   )
rX   	exp_classr   tag_maskr]   r   r^   r_   rJ   r`   r   r   r   remove_implicit   s.   


rf   c                 C   s   | st d| d d dkrt| d}t d| t| dd  \}}|t| d | kr1t dd| | }| d| | | |d  fS )Nz'Empty string does not encode a sequencer   r"   r   z)wanted type 'sequence' (0x30), got 0x%02xz&Length longer than the provided buffer)r	   r   r\   r   )rX   rV   r^   lengthlengthendseqr   r   r   remove_sequence   s   
ri   c                 C   sn   | d d dkrt | d}td| t| dd  \}}| d| d| |  }| d| | d  }||fS )Nr   r;   r   z,wanted type 'octetstring' (0x04), got 0x%02xr[   )rX   rV   r^   r_   rJ   r`   r   r   r   remove_octet_string   s   
rj   c                 C   s  | st d| d d dkrt| d}t d| t| dd  \}}| d| d| |  }| d| | d  }|s?t dt||krIt dg }|r`t|\}}|| ||d  }|sM|d}|dk rn|d	 }	nd
}	|d	|	  }
|d|	 |d|
 t||fS )Nz1Empty string does not encode an object identifierr   rE   r   z'wanted type 'object' (0x06), got 0x%02xzEmpty object identifierz;Length of object identifier longer than the provided bufferP   r?   r!   )	r	   r   r\   r   read_numberrU   poprT   tuple)rX   rV   r^   rg   rJ   r`   numberslln0rG   rH   r   r   r   remove_object   s<   



rr   c                 C   s   | st d| d d dkrt| d}t d| t| dd  \}}|t| d | kr1t d|dkr9t d| d| d| |  }| d| | d  }t|d}|dk s\t d	|dkro|sot|d}|dk rot d
tt|d|fS )Nz1Empty string is an invalid encoding of an integerr   r$   r   z(wanted type 'integer' (0x02), got 0x%02xz"Length longer than provided bufferz0-byte long encoding of integerr   z#Negative integers are not supportedz;Invalid encoding of integer, unnecessary zero padding bytes   )r	   r   r\   r   intr'   hexlify)rX   rV   r^   r_   numberbytesr`   msbsmsbr   r   r   remove_integer  s0   


ry   c                 C   sn   d}d}t | ddkrtd	 |t| krtd|d> }t | |}||d@ 7 }|d7 }|d@ s6	 ||fS q)	Nr   r   z)Non minimal encoding of OID subidentifierTzran out of length bytesr/   r#   r   )r   r	   r   )rX   numberr_   rS   r   r   r   rl   -  s   
rl   c                 C   s^   | dksJ | dk rt | S d|   }t|d rd| }t|}t|}t d|B | S )Nr   r   r    r!   r"   )r   r&   r   r'   r(   )lr+   r_   r   r   r   r   @  s   
r   c                 C   s   | st dt| d}|d@ s|d@ dfS |d@ }|st d|t| d kr+t dt| d}|r:|dkr>|dk r>t dtt| dd|  d	d| fS )
Nz,Empty string can't encode valid length valuer   r   r#   r   z.Invalid length encoding, length of length is 0z,Length of length longer than provided bufferzNot minimal encoding of lengthrs   )r	   r   r   rt   r'   ru   )rX   r,   r_   rw   r   r   r   r\   L  s   

$r\   c           	      C   s6  | st d|tu rtdt t| d}| dd dkr#t d| t| dd \}}|s3t d| d| d| |  }| d| | d }|turt|d}d|  kr_d	ksdt d
 t d
|durp||krpt d|dd }|r|s~t dt|d}|d| d @ rt d|du r||f}||fS )aD  
    Remove a BIT STRING object from `string` following :term:`DER`.

    The `expect_unused` can be used to specify if the bit string should
    have the amount of unused bits decoded or not. If it's an integer, any
    read BIT STRING that has number of unused bits different from specified
    value will cause UnexpectedDER exception to be raised (this is especially
    useful when decoding BIT STRINGS that have DER encoded object in them;
    DER encoding is byte oriented, so the unused bits will always equal 0).

    If the `expect_unused` is specified as None, the first element returned
    will be a tuple, with the first value being the extracted bit string
    while the second value will be the decoded number of unused bits.

    If the `expect_unused` is unspecified, the decoding of byte with
    number of unused bits will not be attempted and the bit string will be
    returned as-is, the callee will be required to decode it and verify its
    correctness.

    Future version of python will require the `expected_unused` parameter
    to be specified.

    :param string: string of bytes to extract the BIT STRING from
    :type string: bytes like object
    :param expect_unused: number of bits that should be unused in the BIT
        STRING, or None, to return it to caller
    :type expect_unused: int or None

    :raises UnexpectedDER: when the encoding does not follow DER.

    :return: a tuple with first element being the extracted bit string and
        the second being the remaining bytes in the string (if any); if the
        `expect_unused` is specified as None, the first element of the returned
        tuple will be a tuple itself, with first element being the bit string
        as bytes and the second element being the number of unused bits at the
        end of the byte array as an integer
    :rtype: tuple
    z(Empty string does not encode a bitstringzALegacy call convention used, expect_unused= needs to be specifiedr   Nr   r1   z#wanted bitstring (0x03), got 0x%02xz(Invalid length of bit string, can't be 0r/   zInvalid encoding of unused bitsz Unexpected number of unused bitsz$Invalid encoding of empty bit stringr0   r!   z#Non zero padding bits in bit string)r	   r2   r3   r4   r5   r   r\   )	rX   expect_unusedr,   r^   r_   rJ   r`   r6   r9   r   r   r   remove_bitstringa  sB   '


r}   c                 C   s6   t | tr	|  } ddd | dD }t|S )Nr.   c                 S   s"   g | ]}|r| d s| qS )s   -----)
startswithstrip)rB   r{   r   r   r   rM     s    zunpem.<locals>.<listcomp>   
)
isinstancer   r&   rF   splitbase64	b64decode)pemrS   r   r   r   unpem  s   

r   c                    s\   t t|  d|  g}| fddtdt dD  |d|   d|S )Nz-----BEGIN %s-----
c                    s    g | ]} ||d   d qS )L   r   r   )rB   startb64r   r   rM     s     ztopem.<locals>.<listcomp>r   r   z-----END %s-----
r.   )	r   	b64encoder   r&   extendranger   rU   rF   )dernamelinesr   r   r   topem  s   
r   )r   )&
__future__r   r'   r   r3   	itertoolsr   sixr   r   _compatr   r   	Exceptionr	   r   r   r-   objectr2   r:   r<   rK   rR   r@   rY   ra   rf   ri   rj   rr   ry   rl   r   r\   r}   r   r   r   r   r   r   <module>   s<    
9
+
!h