
    mg,              
          d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZmZmZ ddlmZmZmZ dd	lmZmZ dd
lmZ dddddddddd	Zeeeeeeeeef   Zeeeeeeeeeef	   Zeeef   Z da!deddfdZ" G d de      Z# ede#d       e#jH                  D ]  Z% ede%e%dz   e#ed        e#jL                  D ]  Z% ee%d        y)z!RSA public key encryption handler    )OptionalTupleUnioncast   )ASN1DecodeErrorObjectIdentifier
der_encode
der_decode)RSAPrivateKeyRSAPublicKey)all_ints)MPIntString	SSHPacket)SSHKeySSHOpenSSHCertificateV01KeyExportError)register_public_key_algregister_certificate_alg)register_x509_certificate_algsha1sha256sha512sha224sha384)	   ssh-rsa   rsa-sha2-256   rsa-sha2-512   ssh-rsa-sha224@ssh.com   ssh-rsa-sha256@ssh.com   ssh-rsa-sha384@ssh.com   ssh-rsa-sha512@ssh.coms   rsa1024-sha1   rsa2048-sha256Fskip_validationreturnNc                     | a y)a\  Set whether to disable RSA key validation in OpenSSL

       OpenSSL 3.x does additional validation when loading RSA keys
       as an added security measure. However, the result is that
       loading a key can take significantly longer than it did before.

       If all your RSA keys are coming from a trusted source, you can
       call this function with a value of `True` to default to skipping
       these checks on RSA keys, reducing the cost back down to what it
       was in earlier releases.

       This can also be set on a case by case basis by using the new
       `unsafe_skip_rsa_key_validation` argument on the functions used
       to load keys. This will only affect loading keys of type RSA.

       .. note:: The extra cost only applies to loading existing keys, and
                 not to generating new keys. Also, in cases where a key is
                 used repeatedly, it can be loaded once into an `SSHKey`
                 object and reused without having to pay the cost each time.
                 So, this call should not be needed in most applications.

                 If an application does need this, it is strongly
                 recommended that the `unsafe_skip_rsa_key_validation`
                 argument be used rather than using this function to
                 change the default behavior for all load operations.

    N) _default_skip_rsa_key_validation)r%   s    ,lib/python3.12/site-packages/asyncssh/rsa.py#set_default_skip_rsa_key_validationr*   5   s    B (7$    c            
          e Zd ZU dZeeef   ed<   dZdZ	dZ
 ed      ZdZdZ ed	 eD              Zd
Z ed eD              Z eeez         ZdedefdZdefdZeddddedededd fd       Zededefd       Zededefd       Zedede e!   fd       Z"edede e#   fd       Z$ededede e!   fd       Z%ededede e#   fd        Z&ed!e'de!fd"       Z(ed!e'de#fd#       Z)defd$Z*defd%Z+de,eef   fd&Z-de,eef   fd'Z.defd(Z/defd)Z0defd*Z1ded+edefd,Z2ded+ed!e'defd-Z3dedede e   fd.Z4dedede e   fd/Z5y0)1RSAKeyz%Handler for RSA public key encryption_keyr   r   s   RSAz1.2.840.113549.1.1.1)r   r   r    r!   r"   r#   r   )r   r   r   c              #   &   K   | ]	  }|d z     yw)   -cert-v01@openssh.comN .0algs     r)   	<genexpr>zRSAKey.<genexpr>g   s      <':  "::':   )r$   r   c              #   &   K   | ]	  }d |z     yw)s   x509v3-Nr1   r2   s     r)   r5   zRSAKey.<genexpr>j   s     L8KJ,8Kr6   otherr&   c                 D   t        |t              st        S | j                  j                  |j                  j                  k(  xr\ | j                  j
                  |j                  j
                  k(  xr- | j                  j                  |j                  j                  k(  S N)
isinstancer-   NotImplementedr.   ned)selfr8   s     r)   __eq__zRSAKey.__eq__m   sk     %(!!		uzz||+ ,		uzz||+,		uzz||+	-r+   c                     t        | j                  j                  | j                  j                  | j                  j                  | j                  j
                  | j                  j                  f      S r:   )hashr.   r=   r>   r?   pqr@   s    r)   __hash__zRSAKey.__hash__x   sG    TYY[[$))++tyy{{YY[[$))++/ 0 	0r+   i   i  )key_sizeexponent	algorithmrH   rI   c                :     | t        j                  ||            S )zGenerate a new RSA private key)r   generate)clsrJ   rH   rI   s       r)   rL   zRSAKey.generate|   s     =))(H=>>r+   
key_paramsc                     t        t        |      \	  }}}}}}}}	}
|
t        }
 | t        j                  ||||||||	|
	            S )zConstruct an RSA private key)r   _PrivateKeyConstructArgsr(   r   	construct)rM   rN   r=   r>   r?   rD   rE   dmp1dmq1iqmpunsafe_skip_rsa_key_validations              r)   make_privatezRSAKey.make_private   sf    
 ):6 	H1aAtT4)G *1-M*=**1aAq$d+IK L 	Lr+   c                 `    t        t        |      \  }} | t        j                  ||            S )zConstruct an RSA public key)r   _PublicKeyArgsr   rQ   )rM   rN   r=   r>   s       r)   make_publiczRSAKey.make_public   s,     NJ/1<))!Q/00r+   key_datac                 |    t        |t              r,t        |      r!t        |      dk\  rt	        t
        |dd       S y)z&Decode a PKCS#1 format RSA private key	   r   N)r;   tupler   lenr   _PrivateKeyArgsrM   rZ   s     r)   decode_pkcs1_privatezRSAKey.decode_pkcs1_private   s7    
 x'HX,>H"!A77r+   c                 v    t        |t              r)t        |      rt        |      dk(  rt	        t
        |      S y)z%Decode a PKCS#1 format RSA public key   N)r;   r]   r   r^   r   rX   r`   s     r)   decode_pkcs1_publiczRSAKey.decode_pkcs1_public   s1    
 x'HX,>H"11r+   
alg_paramsdatac                 `    |y	 t        |      }| j                  |      S # t        $ r Y yw xY w)z&Decode a PKCS#8 format RSA private keyN)r   r   ra   rM   re   rf   rZ   s       r)   decode_pkcs8_privatezRSAKey.decode_pkcs8_private   sC    
 !	!$'H ''11  		   ! 	--c                 `    |y	 t        |      }| j                  |      S # t        $ r Y yw xY w)z%Decode a PKCS#8 format RSA public keyN)r   r   rd   rh   s       r)   decode_pkcs8_publiczRSAKey.decode_pkcs8_public   sC    
 !	!$'H &&x00  		rj   packetc           	          |j                         }|j                         }|j                         }|j                         }|j                         }|j                         }|||||||dz
  z  ||dz
  z  |fS )z$Decode an SSH format RSA private keyr   	get_mpint)rM   rm   r=   r>   r?   rT   rD   rE   s           r)   decode_ssh_privatezRSAKey.decode_ssh_private   s     !!Q1a1Q3iacD88r+   c                 J    |j                         }|j                         }||fS )z#Decode an SSH format RSA public keyro   )rM   rm   r>   r=   s       r)   decode_ssh_publiczRSAKey.decode_ssh_public   s)     !tr+   c           	         | j                   j                  st        d      d| j                   j                  | j                   j                  | j                   j                  | j                   j
                  | j                   j                  | j                   j                  | j                   j                  | j                   j                  f	S )z&Encode a PKCS#1 format RSA private keyKey is not privater   )
r.   r?   r   r=   r>   rD   rE   rR   rS   rT   rF   s    r)   encode_pkcs1_privatezRSAKey.encode_pkcs1_private   s{     yy{{ !566499;;		TYY[[$))++		TYY^^TYY^^TYY^^M 	Mr+   c                 Z    | j                   j                  | j                   j                  fS )z%Encode a PKCS#1 format RSA public key)r.   r=   r>   rF   s    r)   encode_pkcs1_publiczRSAKey.encode_pkcs1_public   s     yy{{DIIKK''r+   c                 8    dt        | j                               fS )z&Encode a PKCS#8 format RSA private keyN)r
   rv   rF   s    r)   encode_pkcs8_privatezRSAKey.encode_pkcs8_private   s     Z 9 9 ;<<<r+   c                 8    dt        | j                               fS )z%Encode a PKCS#8 format RSA public keyN)r
   rx   rF   s    r)   encode_pkcs8_publiczRSAKey.encode_pkcs8_public   s     Z 8 8 :;;;r+   c           
      ^   | j                   j                  st        d      | j                   j                  J | j                   j                  J | j                   j
                  J dj                  t        | j                   j                        t        | j                   j                        t        | j                   j                        t        | j                   j                        t        | j                   j                        t        | j                   j
                        f      S )z$Encode an SSH format RSA private keyru   r+   )
r.   r?   r   rT   rD   rE   joinr   r=   r>   rF   s    r)   encode_ssh_privatezRSAKey.encode_ssh_private   s     yy{{ !566yy~~)))yy{{&&&yy{{&&&xxtyy{{+U499;;-?tyy{{+U499>>-Btyy{{+U499;;-?A B 	Br+   c                     dj                  t        | j                  j                        t        | j                  j                        f      S )z#Encode an SSH format RSA public keyr+   )r~   r   r.   r>   r=   rF   s    r)   encode_ssh_publiczRSAKey.encode_ssh_public
  s2     xxtyy{{+U499;;-?@AAr+   c                    | j                   j                  st        d      | j                   j                  J | j                   j                  J | j                   j
                  J dj                  t        | j                   j                        t        | j                   j                        t        | j                   j                        t        | j                   j
                        f      S )z1Encode RSA certificate private key data for agentru   r+   )r.   r?   r   rT   rD   rE   r~   r   rF   s    r)   encode_agent_cert_privatez RSAKey.encode_agent_cert_private  s     yy{{ !566yy~~)))yy{{&&&yy{{&&&xxtyy{{+U499>>-Btyy{{+U499;;-?A B 	Br+   sig_algorithmc                     | j                   j                  st        d      t        | j                   j	                  |t
        |               S )z6Compute an SSH-encoded signature of the specified datazPrivate key needed for signing)r.   r?   
ValueErrorr   sign
_hash_algs)r@   rf   r   s      r)   sign_sshzRSAKey.sign_ssh  s:     yy{{=>>diinnT:m+DEFFr+   c                     |j                         }|j                          | j                  j                  ||t        |         S )z5Verify an SSH-encoded signature of the specified data)
get_string	check_endr.   verifyr   )r@   rf   r   rm   sigs        r)   
verify_sshzRSAKey.verify_ssh$  s<     !yyc:m+DEEr+   c                 h    t        t        | j                        }|j                  |t        |         S )z%Encrypt a block of data with this key)r   r   r.   encryptr   )r@   rf   rJ   pub_keys       r)   r   zRSAKey.encrypt-  s*     |TYY/tZ	%:;;r+   c                 h    t        t        | j                        }|j                  |t        |         S )z%Decrypt a block of data with this key)r   r   r.   decryptr   )r@   rf   rJ   priv_keys       r)   r   zRSAKey.decrypt3  s,     tyy1j&;<<r+   N)6__name__
__module____qualname____doc__r   r   r   __annotations__rJ   default_x509_hashpem_namer	   	pkcs8_oidsig_algorithmscert_sig_algorithmsr]   cert_algorithmsx509_sig_algorithmsx509_algorithmssetall_sig_algorithmsobjectboolrA   intrG   classmethodbytesrL   r   rV   rY   r   r_   ra   rX   rd   ri   rl   r   rq   rs   rv   rx   r   rz   r|   r   r   r   r   r   r   r   r1   r+   r)   r-   r-   Y   s   /
|+
,,I H !78I"N I <':< <O9L8KLLO0>AB	-F 	-t 	-0# 0 !%u? ??14?AI? ? 
Lf 
L 
L 
L 1V 1 1 1 F _%  6 ^$  2f 2#(2-5o-F2 2 1V 1"'1,4^,D1 1 
9	 
9o 
9 
9 y ^  Mf M(V (
=eFFN&; =
<U66>%: <
BE BB5 B
B5 BGU G5 GU GFu FU F$F)-F<E <e < <=E =e = =r+   r-   r   Tr0   )'r   typingr   r   r   r   asn1r   r	   r
   r   cryptor   r   miscr   rm   r   r   r   
public_keyr   r   r   r   r   r   r   r   r_   r   rP   rX   r(   r*   r-   r   _algr   r1   r+   r)   <module>r      s  * ( / / K K /  , , H H I 5 *0)1)1)1)1)1)1)/)13
 S#sCc3>? c3S#sC!MN sCx $)  !7 !7$ !7H^=V ^=B 
FD 1&&DQd-E&E#%=tE ' ""D!$- #r+   