
    ÿigy                         g d Z ddlZddlZddl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lmZ ddlmZmZmZ dd	lmZmZmZ  G d
 de          ZddZddZd Zd Zd Zd Zd Z d Z!d Z"ddZ#e#Z$dZ%dS ))generate	construct
import_keyRsaKeyoid    N)Random)tobytesbordtostr)DerSequenceDerNull)bytes_to_long)Integer)test_probable_primegenerate_probable_prime	COMPOSITE)_expand_subject_public_key_info_create_subject_public_key_info _extract_subject_public_key_infoc                      e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Ze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	 	 d(dZd Zd  Z d! Z!d" Z"d# Z#d$ Z$d% Z%d& Z&d' Z'dS ))r   a  Class defining an RSA key, private or public.
    Do not instantiate directly.
    Use :func:`generate`, :func:`construct` or :func:`import_key` instead.

    :ivar n: RSA modulus
    :vartype n: integer

    :ivar e: RSA public exponent
    :vartype e: integer

    :ivar d: RSA private exponent
    :vartype d: integer

    :ivar p: First factor of the RSA modulus
    :vartype p: integer

    :ivar q: Second factor of the RSA modulus
    :vartype q: integer

    :ivar invp: Chinese remainder component (:math:`p^{-1} \text{mod } q`)
    :vartype invp: integer

    :ivar invq: Chinese remainder component (:math:`q^{-1} \text{mod } p`)
    :vartype invq: integer

    :ivar u: Same as ``invp``
    :vartype u: integer
    c                    t          |                                          }t          d          }|t          d          z  }|||fvrt          d          |                                D ]\  }}t	          | d|z   |           ||k    r7| j        | j        dz
  z  | _        | j        | j        dz
  z  | _	        d| _
        dS dS )a.  Build an RSA key.

        :Keywords:
          n : integer
            The modulus.
          e : integer
            The public exponent.
          d : integer
            The private exponent. Only required for private keys.
          p : integer
            The first factor of the modulus. Only required for private keys.
          q : integer
            The second factor of the modulus. Only required for private keys.
          u : integer
            The CRT coefficient (inverse of p modulo q). Only required for
            private keys.
        ne)pqduzSome RSA components are missing_   N)setkeys
ValueErroritemssetattr_d_p_dp_q_dq_invq)selfkwargs	input_set
public_setprivate_set	componentvalues          T/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/PublicKey/RSA.py__init__zRsaKey.__init__R   s    & &&	__
 3';#<#<<[*555>??? & 	2 	2IuD#	/51111##w$'A+.DHw$'A+.DHDJJJ $#    c                 *    t          | j                  S N)int_nr,   s    r3   r   zRsaKey.nq       47||r5   c                 *    t          | j                  S r7   )r8   _er:   s    r3   r   zRsaKey.eu   r;   r5   c                 p    |                                  st          d          t          | j                  S )Nz-No private exponent available for public keys)has_privateAttributeErrorr8   r&   r:   s    r3   r   zRsaKey.dy   s4    !! 	R !PQQQ47||r5   c                 p    |                                  st          d          t          | j                  S )Nz.No CRT component 'p' available for public keys)r?   r@   r8   r'   r:   s    r3   r   zRsaKey.p   4    !! 	S !QRRR47||r5   c                 p    |                                  st          d          t          | j                  S )Nz.No CRT component 'q' available for public keys)r?   r@   r8   r)   r:   s    r3   r   zRsaKey.q   rB   r5   c                 p    |                                  st          d          t          | j                  S )Nz/No CRT component 'dp' available for public keys)r?   r@   r8   r(   r:   s    r3   dpz	RsaKey.dp   4    !! 	T !RSSS48}}r5   c                 p    |                                  st          d          t          | j                  S )Nz/No CRT component 'dq' available for public keys)r?   r@   r8   r*   r:   s    r3   dqz	RsaKey.dq   rF   r5   c                     |                                  st          d          | j        $| j                            | j                  | _        t          | j                  S )Nz1No CRT component 'invq' available for public keys)r?   r@   r+   r)   inverser'   r8   r:   s    r3   invqzRsaKey.invq   sS    !! 	V !TUUU:11DJ4:r5   c                     | j         S r7   )r   r:   s    r3   invpzRsaKey.invp   s	    vr5   c                 p    |                                  st          d          t          | j                  S )Nz.No CRT component 'u' available for public keys)r?   r@   r8   _ur:   s    r3   r   zRsaKey.u   rB   r5   c                 4    | j                                         S )zSize of the RSA modulus in bitsr9   size_in_bitsr:   s    r3   rR   zRsaKey.size_in_bits   s    w##%%%r5   c                 F    | j                                         dz
  dz  dz   S )z9The minimal amount of bytes that can hold the RSA modulusr       rQ   r:   s    r3   size_in_byteszRsaKey.size_in_bytes   s%    $$&&*q0144r5   c                     d|cxk    r| j         k     sn t          d          t          t          t	          |          | j        | j                             S )Nr   zPlaintext too large)r9   r#   r8   powr   r=   )r,   	plaintexts     r3   _encryptzRsaKey._encrypt   sX    I''''''''23333wy))47DG<<===r5   c                 D   d|cxk    r| j         k     sn t          d          |                                 st          d          t	          j        d| j                   }t	          |          t          || j        | j                   z  | j         z  }t          || j        | j	                  }t          || j
        | j                  }||z
  | j        z  | j        z  }|| j	        z  |z   }t	          j        |                    | j                   || j                   }|S )Nr   zCiphertext too largezThis is not a private keyr    )min_inclusivemax_exclusive)r9   r#   r?   	TypeErrorr   random_rangerW   r=   r(   r'   r*   r)   rO   _mult_modulo_bytesrJ   )	r,   
ciphertextrcpm1m2hmpresults	            r3   _decrypt_to_byteszRsaKey._decrypt_to_bytes   s   J((((((((3444!! 	97888
  qHHHZ  3q$'47#;#;;dgETXtw''TXtw''2g DG+[2 +IIdg&&G  r5   c                 F    t          |                     |                    S )zLegacy private method)r   rh   r,   r`   s     r3   _decryptzRsaKey._decrypt   s      T33J??@@@r5   c                 "    t          | d          S )z"Whether this is an RSA private keyr&   )hasattrr:   s    r3   r?   zRsaKey.has_private   s     tT"""r5   c                     dS NT r:   s    r3   can_encryptzRsaKey.can_encrypt       tr5   c                     dS ro   rp   r:   s    r3   can_signzRsaKey.can_sign   rr   r5   c                 8    t          | j        | j                  S )z^A matching RSA public key.

        Returns:
            a new :class:`RsaKey` object
        r   )r   r9   r=   r:   s    r3   
public_keyzRsaKey.public_key   s     47++++r5   c                     |                                  |                                 k    rdS | j        |j        k    s| j        |j        k    rdS |                                  sdS | j        |j        k    S )NFT)r?   r   r   r   r,   others     r3   __eq__zRsaKey.__eq__   st    !2!2!4!44456UW%' 1 15!! 	4%'!"r5   c                     | |k     S r7   rp   rx   s     r3   __ne__zRsaKey.__ne__   s    EM""r5   c                     ddl m} |)Nr   )PicklingError)pickler~   )r,   r~   s     r3   __getstate__zRsaKey.__getstate__   s    ((((((r5   c                 *   |                                  rRdt          | j                  t          | j                  t          | j                  t          | j                  fz  }nd}dt          | j                  t          | j                  |fz  S )Nz, d=%d, p=%d, q=%d, u=%d zRsaKey(n=%d, e=%d%s))r?   r8   r&   r'   r)   rO   r9   r=   )r,   extras     r3   __repr__zRsaKey.__repr__   s{     	.#dg,,DG25dg,,DG2N NEE E%TWs47||U(KKKr5   c                 \    |                                  rd}nd}d|t          |           fz  S )NPrivatePublicz%s RSA key at 0x%X)r?   id)r,   key_types     r3   __str__zRsaKey.__str__   s8     	  HHH#xD&:::r5   PEMNr    c                    |t          |          }|t          j        }|dk    rd | j        | j        fD             \  }}t          |d                   dz  rd|z   }t          |d                   dz  rd|z   }d||g}	d                    d	 |	D                       }
d
t          j        |
          dd         z   S | 	                                r.t          d| j        | j        | j        | j        | j        | j        | j        dz
  z  | j        | j        dz
  z  t!          | j                                      | j                  g	                                          }|dk    rd}|dk    r|rt'          d          nddlm} |dk    r/|-d}|                    |t.          dt1                                }nd}|s|rt'          d          d}|                    |t.          |||t1                                }d}n=d}t3          t.          t          | j        | j        g          t1                                }|dk    r|S |dk    r-ddlm} |                    ||||          }t          |          S t'          d|z            )a  Export this RSA key.

        Keyword Args:
          format (string):
            The desired output format:

            - ``'PEM'``. (default) Text output, according to `RFC1421`_/`RFC1423`_.
            - ``'DER'``. Binary output.
            - ``'OpenSSH'``. Text output, according to the OpenSSH specification.
              Only suitable for public keys (not private keys).

            Note that PEM contains a DER structure.

          passphrase (bytes or string):
            (*Private keys only*) The passphrase to protect the
            private key.

          pkcs (integer):
            (*Private keys only*) The standard to use for
            serializing the key: PKCS#1 or PKCS#8.

            With ``pkcs=1`` (*default*), the private key is encoded with a
            simple `PKCS#1`_ structure (``RSAPrivateKey``). The key cannot be
            securely encrypted.

            With ``pkcs=8``, the private key is encoded with a `PKCS#8`_ structure
            (``PrivateKeyInfo``). PKCS#8 offers the best ways to securely
            encrypt the key.

            .. note::
                This parameter is ignored for a public key.
                For DER and PEM, the output is always an
                ASN.1 DER ``SubjectPublicKeyInfo`` structure.

          protection (string):
            (*For private keys only*)
            The encryption scheme to use for protecting the private key
            using the passphrase.

            You can only specify a value if ``pkcs=8``.
            For all possible protection schemes,
            refer to :ref:`the encryption parameters of PKCS#8<enc_params>`.
            The recommended value is
            ``'PBKDF2WithHMAC-SHA512AndAES256-CBC'``.

            If ``None`` (default), the behavior depends on :attr:`format`:

            - if ``format='PEM'``, the obsolete PEM encryption scheme is used.
              It is based on MD5 for key derivation, and 3DES for encryption.

            - if ``format='DER'``, the ``'PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC'``
              scheme is used.

          prot_params (dict):
            (*For private keys only*)

            The parameters to use to derive the encryption key
            from the passphrase. ``'protection'`` must be also specified.
            For all possible values,
            refer to :ref:`the encryption parameters of PKCS#8<enc_params>`.
            The recommendation is to use ``{'iteration_count':21000}`` for PBKDF2,
            and ``{'iteration_count':131072}`` for scrypt.

          randfunc (callable):
            A function that provides random bytes. Only used for PEM encoding.
            The default is :func:`Cryptodome.Random.get_random_bytes`.

        Returns:
          bytes: the encoded key

        Raises:
          ValueError:when the format is unknown or when you try to encrypt a private
            key with *DER* format and PKCS#1.

        .. warning::
            If you don't provide a pass phrase, the private key will be
            exported in the clear!

        .. _RFC1421:    http://www.ietf.org/rfc/rfc1421.txt
        .. _RFC1423:    http://www.ietf.org/rfc/rfc1423.txt
        .. _`PKCS#1`:   http://www.ietf.org/rfc/rfc3447.txt
        .. _`PKCS#8`:   http://www.ietf.org/rfc/rfc5208.txt
        NOpenSSHc                 6    g | ]}|                                 S rp   )to_bytes.0xs     r3   
<listcomp>z%RsaKey.export_key.<locals>.<listcomp>d  s     III

IIIr5   r          s   ssh-rsar5   c                 X    g | ]'}t          j        d t          |                    |z   (S )>I)structpacklen)r   kps     r3   r   z%RsaKey.export_key.<locals>.<listcomp>j  s/    !U!U!Ub&+dCGG"<"<r"A!U!U!Ur5      ssh-rsa r    zRSA PRIVATE KEYDERz&PKCS#1 private key cannot be encryptedPKCS8r   zPRIVATE KEY)
key_paramszENCRYPTED PRIVATE KEYz"'protection' parameter must be setz"PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC)prot_paramsr   z
PUBLIC KEYr   z3Unknown key format '%s'. Cannot export the RSA key.)r	   r   get_random_bytesr=   r9   r
   joinbinascii
b2a_base64r?   r   r   r   r   r   r   r   rJ   encoder#   Cryptodome.IOr   wrapr   r   r   r   )r,   format
passphrasepkcs
protectionrandfuncr   e_bytesn_byteskeyparts	keystring
binary_keyr   r   r   pem_strs                   r3   
export_keyzRsaKey.export_key  s   l ! ,,J.HYIItw6HIIIGWGAJ$& ,!G+GAJ$& ,!G+"GW5H!U!UH!U!U!UVVI!4Y!?!?!DDD  '	;$a&*f&*f&*f&*f&*f&*fq&9&*fq&9&-dfoo&=&=df&E&E	&( 	) 	) *0  qyy,U??z?$%MNNN//////U??z'9,H!&JT7>yy ", "B "BJJ  7H% J& S",-Q"R"RR%I
!&J,6
8C7>yy ", "B "BJ "&JJ#H89DdfFJfFN :O :O9@; ;J U??U??))))))jjXz8LLG7###NQWWXXXr5   c                      | j         |i |S :meta private:)r   )r,   argsr-   s      r3   	exportKeyzRsaKey.exportKey  s    t////r5   c                 *    |                                  S r   )rv   r:   s    r3   	publickeyzRsaKey.publickey  s       r5   c                      t          d          r   z0Use module Cryptodome.Signature.pkcs1_15 insteadNotImplementedError)r,   MKs      r3   signzRsaKey.sign      !"TUUUr5   c                      t          d          r   r   )r,   r   	signatures      r3   verifyzRsaKey.verify  r   r5   c                      t          d          r   z/Use module Cryptodome.Cipher.PKCS1_OAEP insteadr   )r,   rX   r   s      r3   encryptzRsaKey.encrypt      !"STTTr5   c                      t          d          r   r   rj   s     r3   decryptzRsaKey.decrypt  r   r5   c                     t           r   r   r,   r   Bs      r3   blindzRsaKey.blind      !!r5   c                     t           r   r   r   s      r3   unblindzRsaKey.unblind  r   r5   c                     t           r   r   r:   s    r3   sizezRsaKey.size  r   r5   )r   Nr    NNN)(__name__
__module____qualname____doc__r4   propertyr   r   r   r   r   rE   rH   rK   rM   r   rR   rU   rY   rh   rk   r?   rq   rt   rv   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   r5   r3   r   r   4   s        :  >   X   X   X
   X
   X
   X
   X
   X   X   X
& & &5 5 5> > >
  2A A A
# # #
    , , ,# # ## # #  
L L L; ; ; >??CYY YY YY YYx0 0 0! ! !
V V VV V VU U UU U U" " "" " "" " " " "r5   r     c                    | dk     rt          d          dz  dk    sdk     rt          d          |t          j        }t          d          x}}t                    |                                | k    r|d| dz  z  k     r
| dz  }| |z
  }t          d          d|z  dz
  z                                  x||k    r*t          d          d|z  dz
  z                                  fd	}t          |||
          t          d          | dz  dz
  z  fd}t          |||
          }	|	z  }dz
                      |	dz
            }
                    |
          }|                                | k    r|d| dz  z  k     
|	k    r|	c}	                    |	          }t          |||	|          S )aF  Create a new RSA key pair.

    The algorithm closely follows NIST `FIPS 186-4`_ in its
    sections B.3.1 and B.3.3. The modulus is the product of
    two non-strong probable primes.
    Each prime passes a suitable number of Miller-Rabin tests
    with random bases and a single Lucas test.

    Args:
      bits (integer):
        Key length, or size (in bits) of the RSA modulus.
        It must be at least 1024, but **2048 is recommended.**
        The FIPS standard only defines 1024, 2048 and 3072.
    Keyword Args:
      randfunc (callable):
        Function that returns random bytes.
        The default is :func:`Cryptodome.Random.get_random_bytes`.
      e (integer):
        Public RSA exponent. It must be an odd positive integer.
        It is typically a small number with very few ones in its
        binary representation.
        The FIPS standard requires the public exponent to be
        at least 65537 (the default).

    Returns: an RSA key object (:class:`RsaKey`, with private key).

    .. _FIPS 186-4: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
    i   z"RSA modulus length must be >= 1024   r      zBRSA public exponent must be a positive, odd integer larger than 2.Nr    c                 H    | k    o| dz
                                 dk    S Nr    )gcd)	candidater   min_ps    r3   filter_pzgenerate.<locals>.filter_p  s*    u$D)a-)<)<Q)?)?1)DDr5   )
exact_bitsr   prime_filterd   c                 t    | k    o1| dz
                                 dk    ot          | z
            k    S r   )r   abs)r   r   min_distancemin_qr   s    r3   filter_qzgenerate.<locals>.filter_q  sH    % 6]''**a/6	A&&57r5   r   r   r   r   r   r   )
r#   r   r   r   rR   sqrtr   lcmrJ   r   )bitsr   r   r   r   size_qsize_pr   r   r   r   r   r   r   r   r   s     `         @@@@r3   r   r     sB   < d{{=>>>1uzzQUU]^^^*AJJA

A
..

d
"
"qA$!),<'='=  F
Q7==???VQZZAJN399;;E	E 	E 	E 	E 	E 	E $v-519; ; ; qzzdai#o6	7 	7 	7 	7 	7 	7 	7 	7
 $v-519; ; ; E1ukk!a%  IIcNN? ..

d
"
"qA$!),<'='=B 	1uu!1			!AAa1Q////r5   Tc                 6    G d dt                     } |            }t          d|           D ]#\  }}t          ||t          |                     $|j        }|j        }t          |d          st          ||          }nM|j        }	t          |d          r|j	        }
|j
        }n|	|z  dz
  }|}|dz  d	k    r|dz  }|dz  d	k    d
}t          d          }|s|dk     rt          |          }||k     rht          |||          }|dk    rF||dz
  k    r=t          |d|          dk    r(t          |                              |dz             }
d}n|dz  }||k     h|dz  }|s|dk     |st          d          ||
z  d	k    sJ ||
z  }t          |d          r|j        }n|
                    |          }t          |||	|
||          }|r|dk    s||k    rt          d          t          |                              |          dk    rt          d          |dz  st          d          |                                rM|	dk    s|	|k    rt          d          t          |                              |	          dk    rt          d          |
|z  |k    rt          d          t#          |
          t$          k    rt          d          t#          |          t$          k    rt          d          |
dz
  |dz
  z  }||
dz
                      |dz
            z  }||	z  t'          |          z  dk    rt          d          t          |d          r6|dk    s||k    rt          d          |
|z  |z  dk    rt          d          |S )a3  Construct an RSA key from a tuple of valid RSA components.

    The modulus **n** must be the product of two primes.
    The public exponent **e** must be odd and larger than 1.

    In case of a private key, the following equations must apply:

    .. math::

        \begin{align}
        p*q &= n \\
        e*d &\equiv 1 ( \text{mod lcm} [(p-1)(q-1)]) \\
        p*u &\equiv 1 ( \text{mod } q)
        \end{align}

    Args:
        rsa_components (tuple):
            A tuple of integers, with at least 2 and no
            more than 6 items. The items come in the following order:

            1. RSA modulus *n*.
            2. Public exponent *e*.
            3. Private exponent *d*.
               Only required if the key is private.
            4. First factor of *n* (*p*).
               Optional, but the other factor *q* must also be present.
            5. Second factor of *n* (*q*). Optional.
            6. CRT coefficient *q*, that is :math:`p^{-1} \text{mod }q`. Optional.

    Keyword Args:
        consistency_check (boolean):
            If ``True``, the library will verify that the provided components
            fulfil the main RSA properties.

    Raises:
        ValueError: when the key being imported fails the most basic RSA validity checks.

    Returns: An RSA key object (:class:`RsaKey`).
    c                       e Zd ZdS )construct.<locals>.InputCompsN)r   r   r   rp   r5   r3   
InputCompsr   D  s        r5   r   r   r   r   r   r    r   r   Fr   Tz2Unable to compute factors p and q from exponent d.r   zInvalid RSA public exponentz-RSA public exponent is not coprime to moduluszRSA modulus is not oddzInvalid RSA private exponentz.RSA private exponent is not coprime to modulusz RSA factors do not match moduluszRSA factor p is compositezRSA factor q is compositezInvalid RSA conditionzInvalid RSA component uzInvalid RSA component u with p)objectzipr%   r   r   r   rm   r   r   r   r   rW   r   r#   r   rJ   r?   r   r   r8   )rsa_componentsconsistency_checkr   input_compscompr2   r   r   keyr   r   r   ktottspottedakcandr   phir   s                        r3   r   r     s1   R    V    *,,K;^LL 3 3uT75>>2222AA;$$ 33qAM;$$ '	AAA
 q519D Aa%1**a a%1** G

A !c''AJJ$hhq!Q<<DqyyTa!e__T1aA9M9M $AJJNN4!844"&FA $hh Q  !c''  W !UVVVUqLLLLQA;$$ 	AA		!A qAa1222  #G 66Q!VV:;;;1::>>!!!LMMM 1u 	75666?? 	GAvva !?@@@qzz~~a  A%% !QRRR1uzz !CDDD"1%%22 !<==="1%%22 !<===q5QU#C!a%QU+++CAC Q&& !8999sC   G66Q!VV$%>???EAI!##$%EFFFJr5   c                    t                                          | dd          }|d         dk    rt          d          t          |dd         t	          |d                                       |d	                   gz             S )
N	   Tnr_elementsonly_ints_expectedr   z(No PKCS#1 encoding of an RSA private keyr             )r   decoder#   r   r   rJ   encodedr-   ders      r3   _import_pkcs1_privater    sx     --

wA$

O
OC
1v{{CDDDS1XQ!8!8Q!@!@ AABBBr5   c                 h    t                                          | dd          }t          |          S )Nr   Tr	  )r   r  r   r  s      r3   _import_pkcs1_publicr    s.    
 --

wA$

O
OCS>>r5   c                     t           df}t          |           \  }}}||vs|t          d          t          |          S )N1.2.840.113549.1.1.10zNo RSA subjectPublicKeyInfo)r   r   r#   r  )r  r-   oidsalgoidencoded_keyparamss         r3   _import_subjectPublicKeyInfor    sN    ()D"A'"J"JFKTV/6777,,,r5   c                 >    t          |           }t          |          S r7   )r   r  )r  r-   sp_infos      r3   _import_x509_certr    s    .w77G'000r5   c                     ddl m} t          df}|                    | |          }|d         |vrt	          d          t          |d         |          S )Nr   r   r  zNo PKCS#8 encoded RSA keyr    )r   r   r   unwrapr#   _import_keyDER)r  r   r   r  r  s        r3   _import_pkcs8r#    sd    ######()DWj))At44555!A$
+++r5   c                     t           t          t          t          t          f}|D ] }	  || |          c S # t
          $ r Y w xY wt          d          )z@Import an RSA key (public or private half), encoded in DER form.RSA key format is not supported)r  r  r  r  r#  r#   )
extern_keyr   	decodingsdecodings       r3   r"  r"    sy     '%-"	 I   	8J
33333 	 	 	D	 6
7
77s   4
A Ac                 ~   ddl m}m}m}m}  || |          \  }}|dk    rt          d           ||          \  }} ||          \  }	} ||          \  }
} ||          \  }} ||          \  }} ||          \  }} ||          \  }} ||           d ||	|
|||fD             }t          |          S )Nr    )import_openssh_private_generic
read_bytesread_stringcheck_paddingzssh-rsazThis SSH key is not RSAc                 6    g | ]}t          j        |          S rp   )r   
from_bytesr   s     r3   r   z/_import_openssh_private_rsa.<locals>.<listcomp>	  s#    BBBqW""BBBr5   )_opensshr*  r+  r,  r-  r#   r   )datapasswordr*  r+  r,  r-  ssh_name	decryptedr   r   r   iqmpr   r   r   paddedbuilds                    r3   _import_openssh_private_rsar8    s@   C C C C C C C C C C C C 98xHHHi92333:i((LAy:i((LAy:i((LAy j++OD):i((LAy:i((LAyI&&IAvM&BBQ1aD,ABBBEUr5   c                 "   ddl m} t          |           } |t          |          }|                     d          r;t	          |           }|                    ||          \  }}}t          ||          }|S |                     d          r;|                    t	          |           |          \  }}}|rd}t          ||          S |                     d          rt          j	        | 
                    d          d                   }	g }
t          |	          d	k    rct          j        d
|	dd	                   d         }|
                    |	d	d	|z                       |	d	|z   d         }	t          |	          d	k    ct          j        |
d                   }t          j        |
d                   }t#          ||g          S t          |           dk    r)t%          | d                   dk    rt          | |          S t'          d          )a  Import an RSA key (public or private).

    Args:
      extern_key (string or byte string):
        The RSA key to import.

        The following formats are supported for an RSA **public key**:

        - X.509 certificate (binary or PEM format)
        - X.509 ``subjectPublicKeyInfo`` DER SEQUENCE (binary or PEM
          encoding)
        - `PKCS#1`_ ``RSAPublicKey`` DER SEQUENCE (binary or PEM encoding)
        - An OpenSSH line (e.g. the content of ``~/.ssh/id_ecdsa``, ASCII)

        The following formats are supported for an RSA **private key**:

        - PKCS#1 ``RSAPrivateKey`` DER SEQUENCE (binary or PEM encoding)
        - `PKCS#8`_ ``PrivateKeyInfo`` or ``EncryptedPrivateKeyInfo``
          DER SEQUENCE (binary or PEM encoding)
        - OpenSSH (text format, introduced in `OpenSSH 6.5`_)

        For details about the PEM encoding, see `RFC1421`_/`RFC1423`_.

      passphrase (string or byte string):
        For private keys only, the pass phrase that encrypts the key.

    Returns: An RSA key object (:class:`RsaKey`).

    Raises:
      ValueError/IndexError/TypeError:
        When the given key cannot be parsed (possibly because the pass
        phrase is wrong).

    .. _RFC1421: http://www.ietf.org/rfc/rfc1421.txt
    .. _RFC1423: http://www.ietf.org/rfc/rfc1423.txt
    .. _`PKCS#1`: http://www.ietf.org/rfc/rfc3447.txt
    .. _`PKCS#8`: http://www.ietf.org/rfc/rfc5208.txt
    .. _`OpenSSH 6.5`: https://flak.tedunangst.com/post/new-openssh-key-format-and-bcrypt-pbkdf
    r   r   Ns   -----BEGIN OPENSSH PRIVATE KEYs   -----r       r    r  r   r   0   r%  )r   r   r	   
startswithr   r  r8  r"  r   
a2b_base64splitr   r   unpackappendr   r/  r   r
   r#   )r&  r   r   text_encodedopenssh_encodedmarkerenc_flagrg   r  r   r   lengthr   r   s                 r3   r   r     s   R "!!!!!$$JZ((
>?? Z((,/JJ|Z,P,P),_jIIX&& /"%**U:->->
"K"Kfh 	Jc:...[)) 
!'
(8(8(>(>q(ABB	)nnq  ]42A277:FOOIaF
l3444!!f*++.I )nnq   x{++x{++!Q   
:tJqM22d::j*555
6
7
77r5   z1.2.840.113549.1.1.1)Nr   )Tr7   )&__all__r   r   
Cryptodomer   Cryptodome.Util.py3compatr	   r
   r   Cryptodome.Util.asn1r   r   Cryptodome.Util.numberr   Cryptodome.Math.Numbersr   Cryptodome.Math.Primalityr   r   r   Cryptodome.PublicKeyr   r   r   r   r   r   r   r  r  r  r  r#  r"  r8  r   	importKeyr   rp   r5   r3   <module>rO     s  @           : : : : : : : : : : 5 5 5 5 5 5 5 5 0 0 0 0 0 0 + + + + + +G G G G G G G G G G@ @ @ @ @ @ @ @ @ @
R" R" R" R" R"V R" R" R"jO0 O0 O0 O0dM M M M`C C C(  - - -1 1 1, , ,8 8 8$  0L8 L8 L8 L8` 	 r5   