
    5iuy                         / S Qr SSKrSSKrSSKJr  SSKJrJrJr  SSK	J
r
Jr  SSKJr  SSKJr  SSKJrJrJr  SS	KJrJrJr   " S
 S\5      rSS jrSS jrS rS rS rS rS r S r!S r"SS jr#\#r$Sr%g))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                   V   \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       rS rS rS rS rS rS rS rS rS rS rS rS rS rS r  S(S jrS r S r!S  r"S! r#S" r$S# r%S$ r&S% r'S& r(S'r)g))r   4   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                 |   [        UR                  5       5      n[        S5      nU[        S5      -  nX$U4;  a  [        S5      eUR                  5        H  u  pV[	        U SU-   U5        M     X$:X  aJ  U R
                  U R                  S-
  -  U l        U R
                  U R                  S-
  -  U l	        SU l
        gg)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          b/var/www/html/BTCUSD/btcusdt_trading_app/venv/lib/python3.13/site-packages/Crypto/PublicKey/RSA.py__init__RsaKey.__init__R   s    & &	_
 3';#<<*55>?? &ID#	/51 !/#ww$''A+.DHww$''A+.DHDJ $    c                 ,    [        U R                  5      $ N)int_nr-   s    r4   r   RsaKey.nq       477|r7   c                 ,    [        U R                  5      $ r9   )r:   _er<   s    r4   r   RsaKey.eu   r>   r7   c                 l    U R                  5       (       d  [        S5      e[        U R                  5      $ )Nz-No private exponent available for public keys)has_privateAttributeErrorr:   r'   r<   s    r4   r   RsaKey.dy   s+    !! !PQQ477|r7   c                 l    U R                  5       (       d  [        S5      e[        U R                  5      $ )Nz.No CRT component 'p' available for public keys)rC   rD   r:   r(   r<   s    r4   r   RsaKey.p   +    !! !QRR477|r7   c                 l    U R                  5       (       d  [        S5      e[        U R                  5      $ )Nz.No CRT component 'q' available for public keys)rC   rD   r:   r*   r<   s    r4   r   RsaKey.q   rH   r7   c                 l    U R                  5       (       d  [        S5      e[        U R                  5      $ )Nz/No CRT component 'dp' available for public keys)rC   rD   r:   r)   r<   s    r4   dp	RsaKey.dp   +    !! !RSS488}r7   c                 l    U R                  5       (       d  [        S5      e[        U R                  5      $ )Nz/No CRT component 'dq' available for public keys)rC   rD   r:   r+   r<   s    r4   dq	RsaKey.dq   rN   r7   c                     U R                  5       (       d  [        S5      eU R                  c*  U R                  R	                  U R
                  5      U l        [        U R                  5      $ )Nz1No CRT component 'invq' available for public keys)rC   rD   r,   r*   inverser(   r:   r<   s    r4   invqRsaKey.invq   sM    !! !TUU::1DJ4::r7   c                     U R                   $ r9   )r   r<   s    r4   invpRsaKey.invp   s    vvr7   c                 l    U R                  5       (       d  [        S5      e[        U R                  5      $ )Nz.No CRT component 'u' available for public keys)rC   rD   r:   _ur<   s    r4   r   RsaKey.u   rH   r7   c                 6    U R                   R                  5       $ )zSize of the RSA modulus in bitsr;   size_in_bitsr<   s    r4   r^   RsaKey.size_in_bits   s    ww##%%r7   c                 H    U R                   R                  5       S-
  S-  S-   $ )z9The minimal amount of bytes that can hold the RSA modulusr!      r]   r<   s    r4   size_in_bytesRsaKey.size_in_bytes   s$    $$&*q0144r7   c                     SUs=::  a  U R                   :  d  O  [        S5      e[        [        [	        U5      U R
                  U R                   5      5      $ )Nr   zPlaintext too large)r;   r$   r:   powr   r@   )r-   	plaintexts     r4   _encryptRsaKey._encrypt   s@    I''2333wy)477DGG<==r7   c                    SUs=::  a  U R                   :  d  O  [        S5      eU R                  5       (       d  [        S5      e[        R
                  " SU R                   S9n[	        U5      [        X R                  U R                   5      -  U R                   -  n[        X0R                  U R                  5      n[        X0R                  U R                  5      nXT-
  U R                  -  U R                  -  nX`R                  -  U-   n[        R                  " UR                  U R                   5      UU R                   5      nU$ )Nr   zCiphertext too largezThis is not a private keyr!   )min_inclusivemax_exclusive)r;   r$   rC   	TypeErrorr   random_rangere   r@   r)   r(   r+   r*   rZ   _mult_modulo_bytesrS   )	r-   
ciphertextrcpm1m2hmpresults	            r4   _decrypt_to_bytesRsaKey._decrypt_to_bytes   s    J((344!!788
   qHZ 3q''477#;;dggEXXtww'XXtww'g DGG+[2 ++IIdgg&GG r7   c                 6    [        U R                  U5      5      $ )zLegacy private method)r   rw   r-   ro   s     r4   _decryptRsaKey._decrypt   s     T33J?@@r7   c                     [        U S5      $ )z"Whether this is an RSA private keyr'   )hasattrr<   s    r4   rC   RsaKey.has_private   s     tT""r7   c                     gNT r<   s    r4   can_encryptRsaKey.can_encrypt       r7   c                     gr   r   r<   s    r4   can_signRsaKey.can_sign   r   r7   c                 >    [        U R                  U R                  S9$ )zFA matching RSA public key.

Returns:
    a new :class:`RsaKey` object
r   )r   r;   r@   r<   s    r4   
public_keyRsaKey.public_key   s     477++r7   c                    U R                  5       UR                  5       :w  a  gU R                  UR                  :w  d  U R                  UR                  :w  a  gU R                  5       (       d  gU R                  UR                  :H  $ )NFT)rC   r   r   r   r-   others     r4   __eq__RsaKey.__eq__   sf    !2!2!4466UWW%'' 1!!%''!"r7   c                     X:X  + $ r9   r   r   s     r4   __ne__RsaKey.__ne__   s    ""r7   c                     SSK Jn  Ue)Nr   )PicklingError)pickler   )r-   r   s     r4   __getstate__RsaKey.__getstate__   s    (r7   c                 8   U R                  5       (       aV  S[        U R                  5      [        U R                  5      [        U R                  5      [        U R
                  5      4-  nOSnS[        U R                  5      [        U R                  5      U4-  $ )Nz, d=%d, p=%d, q=%d, u=%d zRsaKey(n=%d, e=%d%s))rC   r:   r'   r(   r*   rZ   r;   r@   )r-   extras     r4   __repr__RsaKey.__repr__   st    .#dgg,DGG25dgg,DGG2N NE E%TWWs477|U(KKKr7   c                 V    U R                  5       (       a  SnOSnSU[        U 5      4-  $ )NPrivatePublicz%s RSA key at 0x%X)rC   id)r-   key_types     r4   __str__RsaKey.__str__   s/     HH#xD&:::r7   Nc                    Ub  [        U5      nUc  [        R                  nUS:X  a  U R                  U R                  4 Vs/ s H  owR                  5       PM     snu  p[        US   5      S-  (       a  SU-   n[        U	S   5      S-  (       a  SU	-   n	SX/n
SR                  U
 Vs/ s H&  n[        R                  " S[        U5      5      U-   PM(     sn5      nS	[        R                  " U5      SS
 -   $ U R                  5       (       GaD  [        SU R                  U R                   U R"                  U R$                  U R&                  U R"                  U R$                  S-
  -  U R"                  U R&                  S-
  -  [)        U R&                  5      R+                  U R$                  5      /	5      R-                  5       nUS:X  a  SnUS:X  a  U(       a  [/        S5      eOSSKJn  US:X  a$  Uc!  SnUR5                  U[6        S[9        5       S9nOySnU(       d  U(       a  [/        S5      eSnUR5                  U[6        X$U[9        5       S9nSnO:Sn[;        [6        [        U R                  U R                   /5      [9        5       5      nUS:X  a  U$ US:X  a#  SSKJn  UR-                  XX%5      n[        U5      $ [/        SU-  5      es  snf s  snf )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:`Crypto.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OpenSSHr          s   ssh-rsar7   >I   ssh-rsa r!   zRSA PRIVATE KEYDERz&PKCS#1 private key cannot be encryptedPKCS8PEMz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@   r;   to_bytesr
   joinstructpacklenbinascii
b2a_base64rC   r   r   r   r   r   r   r   rS   encoder$   	Crypto.IOr   wrapr   r   r   r   )r-   format
passphrasepkcs
protectionrandfuncr   xe_bytesn_byteskeypartskp	keystring
binary_keyr   r   r   pem_strs                     r4   
export_keyRsaKey.export_key  s   l ! ,J..HY7;ww6HI6H

6HIGGAJ$&!G+GAJ$&!G+"G5HH!UHb&++dCG"<r"AH!UVI!4!4Y!?!DDD $a&*ff&*ff&*ff&*ff&*ff&*ffq&9&*ffq&9&-dffo&=&=dff&E	&( 	) *0  qy,U?z$%MNN+U?z'9,H!&JT7>y ", "BJ  7H%&",-Q"RR%I
!&J,68C7>y ", "BJ "&J#H89DdffFJffFN :O9@;J U?U?%jjzLG7##NQWWXXy  J "Vs   K)-Kc                 &    U R                   " U0 UD6$ :meta private:)r   )r-   argsr.   s      r4   	exportKeyRsaKey.exportKey  s    ///r7   c                 "    U R                  5       $ r   )r   r<   s    r4   	publickeyRsaKey.publickey  s      r7   c                     [        S5      er   z,Use module Crypto.Signature.pkcs1_15 insteadNotImplementedError)r-   MKs      r4   signRsaKey.sign      !"PQQr7   c                     [        S5      er   r   )r-   r   	signatures      r4   verifyRsaKey.verify  r   r7   c                     [        S5      er   z+Use module Crypto.Cipher.PKCS1_OAEP insteadr   )r-   rf   r   s      r4   encryptRsaKey.encrypt      !"OPPr7   c                     [        S5      er   r   rz   s     r4   decryptRsaKey.decrypt  r   r7   c                     [         er   r   r-   r   Bs      r4   blindRsaKey.blind      !!r7   c                     [         er   r   r   s      r4   unblindRsaKey.unblind  r   r7   c                     [         er   r   r<   s    r4   sizeRsaKey.size  r   r7   )r)   r+   r,   )r   Nr!   NNN)*__name__
__module____qualname____firstlineno____doc__r5   propertyr   r   r   r   r   rL   rP   rT   rW   r   r^   rb   rg   rw   r{   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__r   r7   r4   r   r   4   s]   :>      
  
  
  
  
      
&5>
2A
#
,##
L; >??CYYx0!
RRQQ"""r7   r   c           	        ^^^^^ U S:  a  [        S5      eTS-  S:X  d  TS:  a  [        S5      eUc  [        R                  n[        S5      =p4[        T5      mUR	                  5       U :w  a  USU S-  -  :  a  U S-  nX-
  n[        S5      SU-  S-
  -  R                  5       =mmXV:w  a"  [        S5      SU-  S-
  -  R                  5       mUU4S jn[        UUUS	9m[        S5      U S-  S
-
  -  mUUUU4S jn[        UUUS	9n	TU	-  nTS-
  R                  U	S-
  5      n
TR                  U
5      nUR	                  5       U :w  a  USU S-  -  :  a  M  TW	:  a  U	Tsmn	TR                  U	5      n[        UTUTXS9$ )a  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:`Crypto.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.r!   c                 J   > U T:  =(       a    U S-
  R                  T5      S:H  $ Nr!   )gcd)	candidater   min_ps    r4   filter_pgenerate.<locals>.filter_p  s'    u$D)a-)<)<Q)?1)DDr7   )
exact_bitsr   prime_filterd   c                 z   > U T:  =(       a/    U S-
  R                  T5      S:H  =(       a    [        U T-
  5      T:  $ r  )r  abs)r  r   min_distancemin_qr   s    r4   filter_qgenerate.<locals>.filter_q  sB    % 6]''*a/6	A&57r7   r   r   r   r   r   r   )
r$   r   r   r   r^   sqrtr   lcmrS   r   )bitsr   r   r   r   size_qsize_pr  r  r   r  r   r  r  r  r   s     `         @@@@r4   r   r     s   < d{=>>1uzQU]^^**AJA
A
..
d
"qA$!),<'=  F
Q7==??QZAJN399;E	E $v-519; qzdai#o6	7 	7
 $v-519; E1ukk!a% IIcN? ..
d
"qA$!),<'=B 	1u!1			!AAa1//r7   c           	          " S S[         5      nU" 5       n[        SU 5       H  u  pE[        X4[        U5      5        M     UR                  nUR
                  n[        US5      (       d  [        XgS9nGOMUR                  n	[        US5      (       a  UR                  n
UR                  nOX-  S-
  nUnUS-  S	:X  a  US-  nUS-  S	:X  a  M  S
n[        S5      nU(       d  US:  a  [        U5      nUU:  aZ  [        UUU5      nUS:w  a:  UUS-
  :w  a1  [        USU5      S:X  a   [        U5      R                  US-   5      n
SnOUS-  nUU:  a  MZ  US-  nU(       d  US:  a  M  U(       d  [        S5      eUW
-  S	:X  d   eXj-  n[        US5      (       a  UR                  nOU
R                  U5      n[        XgXUUS9nU(       Ga|  US::  d  Xv:  a  [        S5      e[        U5      R                  U5      S:w  a  [        S5      eUS-  (       d  [        S5      eUR!                  5       (       Ga  W	S::  d  X:  a  [        S5      e[        U5      R                  U	5      S:w  a  [        S5      eW
W-  U:w  a  [        S5      e[#        U
5      [$        :X  a  [        S5      e[#        U5      [$        :X  a  [        S5      eU
S-
  US-
  -  nUU
S-
  R                  US-
  5      -  nXy-  ['        U5      -  S:w  a  [        S5      e[        US5      (       a.  WS::  d  UU:  a  [        S5      eU
U-  U-  S:w  a  [        S5      eU$ )a  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                       \ rS rSrSrg)construct.<locals>.InputCompsiD  r   N)r   r   r   r   r   r   r7   r4   
InputCompsr  D  s    r7   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   r~   r   r   r   r   re   r  r$   r   rS   rC   r   r   r:   )rsa_componentsconsistency_checkr  input_compscompr3   r   r   keyr   r   r   ktottspottedakcandr   phir  s                        r4   r   r     s3   RV  ,K;^L75>2 M 	AA;$$qMM;$$AA
 519D Aa%1*a a%1* G
A!c'AJ$hq!Q<DqyTa!e_T1aA9M $AJNN4!84"&FA $h Q !c'  !UVVUqL!LA;$$A		!A q12  6QV:;;1:>>!!LMM 1u566??Av !?@@qz~~a A% !QRR1uz !CDD"1%2 !<=="1%2 !<==q5QU#C!a%QU++CC Q& !899sC  6Q!V$%>??EAI!#$%EFFJr7   c                     [        5       R                  U SSS9nUS   S:w  a  [        S5      e[        USS [	        US   5      R                  US	   5      /-   5      $ )
N	   Tnr_elementsonly_ints_expectedr   z(No PKCS#1 encoding of an RSA private keyr!            )r   decoder$   r   r   rS   encodedr.   ders      r4   _import_pkcs1_privater5    sd     -

wA$

OC
1v{CDDS1XQ!8!8Q!@ AABBr7   c                 J    [        5       R                  U SSS9n[        U5      $ )Nr   Tr+  )r   r1  r   r2  s      r4   _import_pkcs1_publicr7    s'    
 -

wA$

OCS>r7   c                 j    [         S4n[        U 5      u  p4nX2;  d  Ub  [        S5      e[        U5      $ )N1.2.840.113549.1.1.10zNo RSA subjectPublicKeyInfo)r   r   r$   r7  )r3  r.   oidsalgoidencoded_keyparamss         r4   _import_subjectPublicKeyInfor>    s>    ()D"A'"JFV/677,,r7   c                 .    [        U 5      n[        U5      $ r9   )r   r>  )r3  r.   sp_infos      r4   _import_x509_certrA    s    .w7G'00r7   c                     SSK Jn  [        S4nUR                  X5      nUS   U;  a  [	        S5      e[        US   U5      $ )Nr   r   r9  zNo PKCS#8 encoded RSA keyr!   )r   r   r   unwrapr$   _import_keyDER)r3  r   r   r:  r&  s        r4   _import_pkcs8rE    sG    ()DW)At4455!A$
++r7   c                     [         [        [        [        [        4nU H  n U" X5      s  $    [        S5      e! [
         a     M(  f = f)z@Import an RSA key (public or private half), encoded in DER form.RSA key format is not supported)r5  r7  r>  rA  rE  r$   )
extern_keyr   	decodingsdecodings       r4   rD  rD    sX     '%-"	 I 	J33  6
77  		s   9
AAc                 ^   SSK JnJnJnJn  U" X5      u  pgUS:w  a  [        S5      eU" U5      u  pU" U5      u  pU" U5      u  pU" U5      u  pU" U5      u  pU" U5      u  pU" U5      u  pU" U5        XXX4 Vs/ s H  n[        R                  " U5      PM     nn[        U5      $ s  snf )Nr!   )import_openssh_private_generic
read_bytesread_stringcheck_paddingzssh-rsazThis SSH key is not RSA)	_opensshrL  rM  rN  rO  r$   r   
from_bytesr   )datapasswordrL  rM  rN  rO  ssh_name	decryptedr   r   r   iqmpr   r   r    paddedr   builds                     r4   _import_openssh_private_rsarY    s    C C 9HH9233i(LAi(LAi(LA +ODi(LAi(LAI&IA&-.1,AB,AqW",AEBU Cs   = B*c                 v   SSK Jn  [        U 5      n Ub  [        U5      nU R                  S5      (       a,  [	        U 5      nUR                  X15      u  pEn[        XA5      nU$ U R                  S5      (       a2  UR                  [	        U 5      U5      u  pnU(       a  Sn[        X5      $ U R                  S5      (       a  [        R                  " U R                  S5      S   5      n	/ n
[        U	5      S	:  aM  [        R                  " S
U	SS	 5      S   nU
R                  U	S	S	U-    5        U	S	U-   S n	[        U	5      S	:  a  MM  [        R                   " U
S   5      n[        R                   " U
S   5      n[#        X/5      $ [        U 5      S:  a  [%        U S   5      S:X  a  [        X5      $ ['        S5      e)ay  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   rG  )r   r   r	   
startswithr   r1  rY  rD  r   
a2b_base64splitr   r   unpackappendr   rQ  r   r
   r$   )rH  r   r   text_encodedopenssh_encodedmarkerenc_flagrv   r4  r   r   lengthr   r   s                 r4   r   r     s   R $JZ(
>??Z(,/JJ|,P),_IX&&"%**U:->
"KhJc..[))''
(8(8(>q(AB	)nq ]]42A7:FOOIaF
34!!f*+.I )nq  x{+x{+!  
:tJqM2d:j55
6
77r7   z1.2.840.113549.1.1.1)Ni  )Tr9   )&__all__r   r   Cryptor   Crypto.Util.py3compatr	   r
   r   Crypto.Util.asn1r   r   Crypto.Util.numberr   Crypto.Math.Numbersr   Crypto.Math.Primalityr   r   r   Crypto.PublicKeyr   r   r   r  r   r   r   r5  r7  r>  rA  rE  rD  rY  r   	importKeyr   r   r7   r4   <module>rp     s   @    6 6 1 , 'G G@ @
R"V R"jO0dM`C(-1,8$0L8` 	 r7   