
    dfl:                        d dl Z d dlmZ 	 d dlZd dlmZ d dlm	Z
 d dlmZ d dlZddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ  G d	 d
ej<                        Z G d dej<                        Z  G d dej<                        Z! G d dej<                        Z" G d dej<                        Z# G d dej<                        Z$ G d dej<                        Z% G d dej<                        Z& G d dej<                        Z' G d dej<                        Z(e
jR                  ddz  dfd        Z* e e*             d!        Z+y# e$ r d dlZY Fw xY w)"    N)hexlify)b)given   )str_idx_as_int)NIST256pNIST224p)
remove_integerUnexpectedDERread_lengthencode_bitstringremove_bitstringremove_object
encode_oidremove_constructedremove_octet_stringremove_sequencec                   N    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y)TestRemoveIntegerc                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nz assertRaisesr   r
   r   selfs    G/home/api-vastappli/venv/lib/python3.12/site-packages/ecdsa/test_der.pytest_non_minimal_encodingz+TestRemoveInteger.test_non_minimal_encoding!   s3    }- 	21/01	2 	2 	2   4=c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nu   r   r   s    r   test_negative_with_high_bit_setz1TestRemoveInteger.test_negative_with_high_bit_set%   s2    }- 	.1^,-	. 	. 	.r   c                 z    t        t        d            \  }}| j                  |d       | j                  |d       y Nu           r
   r   assertEqualr   valrems      r   test_minimal_with_high_bit_setz0TestRemoveInteger.test_minimal_with_high_bit_set)   s6    !!$6"78Sd#c"r#   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nu     ÿr   r   s    r   %test_two_zero_bytes_with_high_bit_setz7TestRemoveInteger.test_two_zero_bytes_with_high_bit_set/   s3    }- 	61345	6 	6 	6r   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nz r   r   s    r   test_zero_length_integerz*TestRemoveInteger.test_zero_length_integer3   s1    }- 	*1Z=)	* 	* 	*r   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY wN r   r   s    r   test_empty_stringz#TestRemoveInteger.test_empty_string7   s1    }- 	"1R5!	" 	" 	"r   c                 z    t        t        d            \  }}| j                  |d       | j                  |d       y )Nz r   r#   r$   r&   s      r   test_encoding_of_zeroz'TestRemoveInteger.test_encoding_of_zero;   s5    !!N"34Sa c"r#   c                 z    t        t        d            \  }}| j                  |d       | j                  |d       y )Nz   r#   r$   r&   s      r   test_encoding_of_127z&TestRemoveInteger.test_encoding_of_127A   s5    !!N"34Sc"c"r#   c                 z    t        t        d            \  }}| j                  |d       | j                  |d       y r!   r$   r&   s      r   test_encoding_of_128z&TestRemoveInteger.test_encoding_of_128G   s6    !!$6"78Sc"c"r#   c                     | j                  t              5 }t        d       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Ns    zwanted type 'integer'r   r   r
   assertInstr	exceptionr   es     r   test_wrong_tagz TestRemoveInteger.test_wrong_tagM   sJ    }- 	0./	0 	-s1;;/?@	0 	0   AAc                     | j                  t              5 }t        d       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Ns    Length longerr:   r>   s     r   test_wrong_lengthz#TestRemoveInteger.test_wrong_lengthS   sI    }- 	0./	0 	os1;;'78	0 	0rA   N)__name__
__module____qualname__r   r   r)   r+   r-   r1   r3   r6   r8   r@   rD    r#   r   r   r      s:    2.#6*"###A9r#   r   c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)TestReadLengthc                 L    | j                  dt        t        d                   y )N)r   r    r%   r   r   r   s    r   test_zero_lengthzTestReadLength.test_zero_length^   s    QvY!78r#   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nu    r   r   r   r   r   s    r   test_two_byte_zero_lengthz(TestReadLength.test_two_byte_zero_lengtha   1    }- 	'*&	' 	' 	'r   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nu   rP   r   s    r   test_two_byte_small_lengthz)TestReadLength.test_two_byte_small_lengthe   rR   r   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)N   rP   r   s    r   test_long_form_with_zero_lengthz.TestReadLength.test_long_form_with_zero_lengthi   s1    }- 	#&	"	# 	# 	#r   c                 L    | j                  dt        t        d                   y )N)r"      u   rM   r   s    r   test_smallest_two_byte_lengthz,TestReadLength.test_smallest_two_byte_lengthm   s    ;q}#=>r#   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nu    rP   r   s    r   test_zero_padded_lengthz&TestReadLength.test_zero_padded_lengthp   2    }- 	+.)*	+ 	+ 	+r   c                 :    | j                  dt        d             y )N)      s    )r%   r   r   s    r   test_two_three_byte_lengthz)TestReadLength.test_two_three_byte_lengtht   s    ;#?@r#   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY wr/   rP   r   s    r   r1   z TestReadLength.test_empty_stringw   s1    }- 	"	 	 	r   c                     | j                  t              5  t        t        d             d d d        y # 1 sw Y   y xY w)Nu    rP   r   s    r   test_length_overflowz#TestReadLength.test_length_overflow{   r]   r   N)rE   rF   rG   rN   rQ   rT   rW   rZ   r\   ra   r1   rd   rH   r#   r   rJ   rJ   Z   s0    9''#?+A+r#   rJ   c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
TestEncodeBitstringc                 R   t        j                  d       t        j                  t              5 }t        d      }ddd       | j                  t              d       | j                  d|d   j                  j                  d          | j                  d       y# 1 sw Y   cxY w)z(This is the old way to use the function.always    Nr   zunused= needs to be specifiedr       )warningssimplefilterpytestwarnsDeprecationWarningr   r%   lenr;   messageargsr   rn   ders      r   test_old_call_conventionz,TestEncodeBitstring.test_old_call_convention   s    h'\\,- 	0";/C	0 	UQ'+U1X-=-=-B-B1-E	
 	12	0 	0s   BB&c                     t        j                  d       t        j                  d      5 }t	        dd      }ddd       | j                  t              d       | j                  d       y# 1 sw Y   7xY w)z$This is how it should be called now.rh   N   r   rj   rk   rl   rm   rn   r   r%   rp   rs   s      r   test_new_call_conventionz,TestEncodeBitstring.test_new_call_convention   sf    h'\\$ 	/5"7A.C	/ 	UQ'12	/ 	/   A..A7c                     t        j                  d       t        j                  d      5 }t	        dd      }ddd       | j                  t              d       | j                  d       y# 1 sw Y   7xY w)zU
        Writing bit string with already included the number of unused bits.
        rh   Nri   r   rj   rx   rs   s      r   test_implicit_unused_bitsz-TestEncodeBitstring.test_implicit_unused_bits   sh     	h'\\$ 	65";5C	6 	UQ'12	6 	6rz   c                 @    t        dd      }| j                  |d       y )Ns      s   )r   r%   )r   rt   s     r   test_explicit_unused_bitsz-TestEncodeBitstring.test_explicit_unused_bits   s    {A.56r#   c                 <    | j                  t        dd      d       y )Nr#   r   s    )r%   r   r   s    r   r1   z%TestEncodeBitstring.test_empty_string   s    )#q1?Cr#   c                 p    | j                  t              5  t        dd       d d d        y # 1 sw Y   y xY w)Ns       r   
ValueErrorr   r   s    r   test_invalid_unused_countz-TestEncodeBitstring.test_invalid_unused_count   s/    z* 	-[!,	- 	- 	-   ,5c                 p    | j                  t              5  t        dd       d d d        y # 1 sw Y   y xY w)Nr#   r   r   r   s    r   %test_invalid_unused_with_empty_stringz9TestEncodeBitstring.test_invalid_unused_with_empty_string   s/    z* 	%S!$	% 	% 	%r   c                 p    | j                  t              5  t        dd       d d d        y # 1 sw Y   y xY w)Nrw   rY   r   r   s    r   test_non_zero_padding_bitsz.TestEncodeBitstring.test_non_zero_padding_bits   s/    z* 	)Wa(	) 	) 	)r   N)rE   rF   rG   ru   ry   r|   r   r1   r   r   r   rH   r#   r   rf   rf      s+    3	337
D-%)r#   rf   c                   T    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y)TestRemoveBitstringc                 |   t        j                  d       t        j                  t              5 }t        d      \  }}ddd       | j                  t              d       | j                  d|d   j                  j                  d          | j                  d       | j                  d       y# 1 sw Y   uxY w)	z)This is the old way to call the function.rh   rj   Nr   z$expect_unused= needs to be specifiedr   ri   r#   )rk   rl   rm   rn   ro   r   r%   rp   r;   rq   rr   r   rn   bitsrests       r   ru   z,TestRemoveBitstring.test_old_call_convention   s    h'\\,- 	?)*=>JD$	? 	UQ'2E!H4D4D4I4I!4L	
 	{+s#	? 	?s   B22B;c                    t        j                  d       t        j                  d       5 }t	        dd      \  }}d d d        | j                  t              d       | j                  d       | j                  d       y # 1 sw Y   IxY w)Nrh   rj   r   rw   r#   rk   rl   rm   rn   r   r%   rp   r   s       r   ry   z,TestRemoveBitstring.test_new_call_convention   s~    h'\\$ 	B5)*=qAJD$	B 	UQ'w's#	B 	B   BBc                    t        j                  d       t        j                  d       5 }t	        dd       \  }}d d d        | j                  t              d       | j                  d       | j                  d       y # 1 sw Y   IxY w)Nrh   rj   r   )rw   r   r#   r   r   s       r   test_implicit_unexpected_unusedz3TestRemoveBitstring.test_implicit_unexpected_unused   s~    h'\\$ 	E5)*=tDJD$	E 	UQ'|,s#	E 	Er   c                 j    t        dd       \  }}| j                  |d       | j                  |d       y )Ns   )   r~   r#   )r   r%   )r   retr   s      r   test_with_paddingz%TestRemoveBitstring.test_with_padding   s3    $%8$?	Tl+s#r#   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY w)Ns    r   r   r   r   s    r   test_not_a_bitstringz(TestRemoveBitstring.test_not_a_bitstring   0    }- 	80$7	8 	8 	8r   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY w)Ns    r   r   s    r   test_empty_encodingz'TestRemoveBitstring.test_empty_encoding   s/    }- 	0[$/	0 	0 	0r   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY wNr#   r   r   s    r   r1   z%TestRemoveBitstring.test_empty_string   s/    }- 	(S$'	( 	( 	(r   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY w)N   r   r   s    r   test_no_lengthz"TestRemoveBitstring.test_no_length   s/    }- 	,Wd+	, 	, 	,r   c                 p    | j                  t              5  t        dd       d d d        y # 1 sw Y   y xY w)Nrj   r   r   r   s    r   %test_unexpected_number_of_unused_bitsz9TestRemoveBitstring.test_unexpected_number_of_unused_bits   s0    }- 	50!4	5 	5 	5r   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY w)Ns    r   r   s    r   $test_invalid_encoding_of_unused_bitsz8TestRemoveBitstring.test_invalid_encoding_of_unused_bits   s0    }- 	<4d;	< 	< 	<r   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY w)Ns   r   r   s    r   %test_invalid_encoding_of_empty_stringz9TestRemoveBitstring.test_invalid_encoding_of_empty_string   s/    }- 	4_d3	4 	4 	4r   c                 p    | j                  t              5  t        dd        d d d        y # 1 sw Y   y xY w)Ns   r   r   s    r   test_invalid_padding_bitsz-TestRemoveBitstring.test_invalid_padding_bits  r   r   N)rE   rF   rG   ru   ry   r   r   r   r   r1   r   r   r   r   r   rH   r#   r   r   r      s>    $$$$80(,5<48r#   r   c                       e Zd Zd Zd Zd Zy)TestStrIdxAsIntc                 <    | j                  dt        dd             y )Ns   r<   r   r%   r   r   s    r   test_strzTestStrIdxAsInt.test_str	  s    nUA67r#   c                 <    | j                  dt        dd             y Nr   s   strr   r   r   s    r   
test_byteszTestStrIdxAsInt.test_bytes  s    nVQ78r#   c                 N    | j                  dt        t        d      d             y r   )r%   r   	bytearrayr   s    r   test_bytearrayzTestStrIdxAsInt.test_bytearray  s    nYv->BCr#   N)rE   rF   rG   r   r   r   rH   r#   r   r   r     s    89Dr#   r   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)TestEncodeOidc                 l    t        dddddd      }| j                  t        |      t        d             y )Nr   rY   H  ='  06072a8648ce3d0201)r   r%   r   r   )r   oid_ecPublicKeys     r   test_pub_key_oidzTestEncodeOid.test_pub_key_oid  s1    $Q3q!<115I3JKr#   c                 h    | j                  t        t        j                        t	        d             y )N06052b81040021)r%   r   r	   encoded_oidr   r   s    r   test_nist224p_oidzTestEncodeOid.test_nist224p_oid  s#    !5!56:J8KLr#   c                 V    | j                  t        t        j                        d       y )Ns   06082a8648ce3d030107)r%   r   r   r   r   s    r   test_nist256p_oidzTestEncodeOid.test_nist256p_oid  s!    H(()+B	
r#   c                 B    t        ddd      }| j                  |d       y )NrY     r`      7r   r%   r   oids     r   test_large_second_subidz%TestEncodeOid.test_large_second_subid   s!    C#56r#   c                 @    t        dd      }| j                  |d       y )NrY   r      7r   r   s     r   test_with_two_subidsz"TestEncodeOid.test_with_two_subids%  s    C 12r#   c                 @    t        dd      }| j                  |d       y )Nr       r   r   s     r   test_zero_zerozTestEncodeOid.test_zero_zero)  s    Ao.r#   c                 |    | j                  t        t        f      5  t        dd        d d d        y # 1 sw Y   y xY w)Nr   )r   	TypeErrorAssertionErrorr   r   s    r   test_with_wrong_typesz#TestEncodeOid.test_with_wrong_types-  s4    	>:; 	 q$	  	  	 s   2;c                 p    | j                  t              5  t        dd       d d d        y # 1 sw Y   y xY w)Nr   (   r   r   r   r   s    r   "test_with_small_first_large_secondz0TestEncodeOid.test_with_small_first_large_second1  /    ~. 	q"	 	 	r   c                 @    t        dd      }| j                  |d       y )Nr   '   s   Or   r   s     r   test_small_first_max_secondz)TestEncodeOid.test_small_first_max_second5  s    Bo.r#   c                 p    | j                  t              5  t        dd       d d d        y # 1 sw Y   y xY w)Nr`   r   r   r   s    r   test_with_invalid_firstz%TestEncodeOid.test_with_invalid_first9  r   r   N)rE   rF   rG   r   r   r   r   r   r   r   r   r   r   rH   r#   r   r   r     s6    LM

7
3/ /r#   r   c                   p    e Zd Ze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 Zd Zy)TestRemoveObjectc                 .    t        dddddd      | _        y )Nr   rY   r   r   )r   r   )clss    r   
setUpClasszTestRemoveObject.setUpClass?  s    (AsE1a@r#   c                 |    t        | j                        \  }}| j                  |d       | j                  |d       y )Nr#   r   rY   r   r   rY   r   r   r   r%   r   r   r   s      r   r   z!TestRemoveObject.test_pub_key_oidC  s7    !$"6"67	Ts#67r#   c                     t        | j                  dz         \  }}| j                  |d       | j                  |d       y )Ns   morer   r   r   s      r   test_with_extra_bytesz&TestRemoveObject.test_with_extra_bytesH  s<    !$"6"6"@A	Tw'67r#   c                 h    t        d      \  }}| j                  |d       | j                  |d       y )Nr   r#   )rY   r   r`   r   r%   r   s      r   test_with_large_second_subidz-TestRemoveObject.test_with_large_second_subidM  s1    !"9:	Ts#k*r#   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns    r   r   r   r   s    r   test_with_padded_first_subidz-TestRemoveObject.test_with_padded_first_subidS  .    }- 	/-.	/ 	/ 	/   +4c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns   7r   r   s    r   test_with_padded_second_subidz.TestRemoveObject.test_with_padded_second_subidW  s.    }- 	756	7 	7 	7r   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns   7r   r   s    r   )test_with_missing_last_byte_of_multi_bytez:TestRemoveObject.test_with_missing_last_byte_of_multi_byte[  s.    }- 	312	3 	3 	3r   c                 h    t        d      \  }}| j                  |d       | j                  |d       y )Nr   r#   )rY   r   r   r   s      r   r   z%TestRemoveObject.test_with_two_subids_  s1    !"56	Ts#h'r#   c                 h    t        d      \  }}| j                  |d       | j                  |d       y )Nr   r#   )r   r   r   r   s      r   r   zTestRemoveObject.test_zero_zerod  s0    !/2	Ts#f%r#   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY wr   r   r   s    r   r1   z"TestRemoveObject.test_empty_stringi  s-    }- 	#	 	 	r   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)N   r   r   s    r   test_missing_lengthz$TestRemoveObject.test_missing_lengthm  s-    }- 	#'"	# 	# 	#r   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns    r   r   s    r   test_empty_oidzTestRemoveObject.test_empty_oidq  -    }- 	'+&	' 	' 	'r   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns   r   r   s    r   test_empty_oid_overflowz(TestRemoveObject.test_empty_oid_overflowu  r  r   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns   7r   r   s    r   test_with_wrong_typez%TestRemoveObject.test_with_wrong_typey  r   r   c                 n    | j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Ns   7r   r   s    r   test_with_too_long_lengthz*TestRemoveObject.test_with_too_long_length}  r   r   N)rE   rF   rG   classmethodr   r   r   r   r   r   r   r   r   r1   r   r   r  r  r  rH   r#   r   r   r   >  s\    A A8
8
+/73(
&
#''//r#   r   c                       e Zd Zd Zd Zy)TestRemoveConstructedc                     d}t        |      \  }}}| j                  |d       | j                  |d       | j                  |d       y )Ns   r      r#   )r   r%   )r   datatagbodyr   s        r   test_simplez!TestRemoveConstructed.test_simple  sG    ",T2T4d#{+s#r#   c                     d}| j                  t              5 }t        |       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Ns   zconstructed tag)r   r   r   r;   r<   r=   r   r  r?   s      r   test_with_malformed_tagz-TestRemoveConstructed.test_with_malformed_tag  sN    "}- 	%t$	% 	'Q[[)9:	% 	%   AANrE   rF   rG   r  r  rH   r#   r   r
  r
    s    $;r#   r
  c                       e Zd Zd Zd Zy)TestRemoveOctetStringc                 l    d}t        |      \  }}| j                  |d       | j                  |d       y )Ns   s   r#   )r   r%   r   r  r  r   s       r   r  z!TestRemoveOctetString.test_simple  s5    &(.
d/s#r#   c                     d}| j                  t              5 }t        |       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Ns   octetstring)r   r   r   r;   r<   r=   r  s      r   r  z-TestRemoveOctetString.test_with_malformed_tag  sM    &}- 	&%	& 	mS%56	& 	&r  Nr  rH   r#   r   r  r    s    $7r#   r  c                   $    e Zd Zd Zd Zd Zd Zy)TestRemoveSequencec                 l    d}t        |      \  }}| j                  |d       | j                  |d       y )Ns   0r  r#   )r   r%   r  s       r   r  zTestRemoveSequence.test_simple  s5    "$T*
d{+s#r#   c                     | j                  t              5 }t        d       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Nr#   zEmpty stringr   r   r   r;   r<   r=   r>   s     r   test_with_empty_stringz)TestRemoveSequence.test_with_empty_string  sH    }- 	!C 	! 	nc!++&67	! 	!rA   c                     d}| j                  t              5 }t        |       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Ns    zwanted type 'sequence'r   r  s      r   test_with_wrong_tagz&TestRemoveSequence.test_with_wrong_tag  sN    "}- 	"D!	" 	.AKK0@A	" 	"r  c                     d}| j                  t              5 }t        |       d d d        | j                  dt	        j
                               y # 1 sw Y   /xY w)Ns   0rC   r   r  s      r   test_with_wrong_lengthz)TestRemoveSequence.test_with_wrong_length  sM    "}- 	"D!	" 	os1;;'78	" 	"r  N)rE   rF   rG   r  r!  r#  r%  rH   r#   r   r  r    s    $8B9r#   r  rY   i   2   c                 @    | t        j                  dd            }|dk  r | t        j                  dd            }n | t        j                  d|            } | t        j                  t        j                  d|      |            }||ft        |      z   S )z
    Hypothesis strategy that returns valid OBJECT IDENTIFIERs as tuples

    :param max_value: maximum value of any single sub-identifier
    :param max_size: maximum length of the generated OID
    r   rY   )	min_value	max_valuer   )max_size)stintegersliststuple)drawr)  r*  firstsecondr   s         r   st_oidr2    s     qA67EqybkkA<=bkkACD
KK!y9H	
D
 6?U4[((r#   c                 L    t        |  }t        |      \  }}|dk(  sJ || k(  sJ y r   )r   r   )idsr   decoded_oidr   s       r   	test_oidsr6    s3    c"K%k2K3;#r#   ),rk   binasciir   	unittest2unittestImportErrorsixr   hypothesis.strategies
strategiesr+  
hypothesisr   rm   _compatr   curvesr   r	   rt   r
   r   r   r   r   r   r   r   r   r   TestCaser   rJ   rf   r   r   r   r   r
  r  r  	compositer2  r6  rH   r#   r   <module>rC     sH       "   # &  99)) 99x#+X&& #+L;)(++ ;)|G8(++ G8TDh'' D(H%% (VA/x(( A/H;H-- ;&7H-- 79** 9< c6B ) )( vx a  s   E
 
	EE