a
    !f^u                     @   s   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mZmZ ddlmZmZmZ G d	d
 d
eZd"ddZd#ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Z d$dd Z!e!Z"d!Z#dS )%)generate	construct
import_keyRsaKeyoid    N)Random)tobytesbordtostr)DerSequenceDerNull)Integer)test_probable_primegenerate_probable_prime	COMPOSITE)_expand_subject_public_key_info_create_subject_public_key_info _extract_subject_public_key_infoc                   @   sB  e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdEd5d6ZeZeZd7d8 Z d9d: Z!d;d< Z"d=d> Z#d?d@ Z$dAdB Z%dCdD Z&d3S )Fr   a  Class defining an actual RSA key.
    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

    :undocumented: exportKey, publickey
    c                 K   s   t | }t d}|t dB }|||fvr4td| D ]\}}t| d| | q<||kr| j| jd  | _| j| jd  | _	d| _
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kwargsZ	input_setZ
public_setZprivate_set	componentvalue r,   Y/var/www/html/python-backend/venv/lib/python3.9/site-packages/Cryptodome/PublicKey/RSA.py__init__S   s    zRsaKey.__init__c                 C   s
   t | jS N)int_nr(   r,   r,   r-   r   r   s    zRsaKey.nc                 C   s
   t | jS r/   )r0   _er2   r,   r,   r-   r   v   s    zRsaKey.ec                 C   s   |   stdt| jS )Nz-No private exponent available for public keys)has_privateAttributeErrorr0   r"   r2   r,   r,   r-   r   z   s    zRsaKey.dc                 C   s   |   stdt| jS )Nz.No CRT component 'p' available for public keys)r4   r5   r0   r#   r2   r,   r,   r-   r      s    zRsaKey.pc                 C   s   |   stdt| jS )Nz.No CRT component 'q' available for public keys)r4   r5   r0   r%   r2   r,   r,   r-   r      s    zRsaKey.qc                 C   s   |   stdt| jS )Nz/No CRT component 'dp' available for public keys)r4   r5   r0   r$   r2   r,   r,   r-   dp   s    z	RsaKey.dpc                 C   s   |   stdt| jS )Nz/No CRT component 'dq' available for public keys)r4   r5   r0   r&   r2   r,   r,   r-   dq   s    z	RsaKey.dqc                 C   s4   |   std| jd u r*| j| j| _t| jS )Nz1No CRT component 'invq' available for public keys)r4   r5   r'   r%   inverser#   r0   r2   r,   r,   r-   invq   s
    
zRsaKey.invqc                 C   s   | j S r/   )r   r2   r,   r,   r-   invp   s    zRsaKey.invpc                 C   s   |   stdt| jS )Nz.No CRT component 'u' available for public keys)r4   r5   r0   _ur2   r,   r,   r-   r      s    zRsaKey.uc                 C   s
   | j  S )zSize of the RSA modulus in bitsr1   size_in_bitsr2   r,   r,   r-   r=      s    zRsaKey.size_in_bitsc                 C   s   | j  d d d S )z9The minimal amount of bytes that can hold the RSA modulusr      r<   r2   r,   r,   r-   size_in_bytes   s    zRsaKey.size_in_bytesc                 C   s8   d|  kr| j k s n tdttt|| j| j S )Nr   zPlaintext too large)r1   r   r0   powr   r3   )r(   	plaintextr,   r,   r-   _encrypt   s    zRsaKey._encryptc           	      C   s   d|  kr| j k s n td|  s0tdtjd| j d}t|t|| j| j  | j  }t|| j| j	}t|| j
| j}|| | j | j }|| j	 | }|| j | | j  }|t|| j| j krtd|S )Nr   zCiphertext too largezThis is not a private keyr   )Zmin_inclusiveZmax_exclusivez Fault detected in RSA decryption)r1   r   r4   	TypeErrorr   Zrandom_ranger@   r3   r$   r#   r&   r%   r;   r8   )	r(   
ciphertextrcpm1m2hmpresultr,   r,   r-   _decrypt   s    zRsaKey._decryptc                 C   s
   t | dS )z"Whether this is an RSA private keyr"   )hasattrr2   r,   r,   r-   r4      s    zRsaKey.has_privatec                 C   s   dS NTr,   r2   r,   r,   r-   can_encrypt   s    zRsaKey.can_encryptc                 C   s   dS rN   r,   r2   r,   r,   r-   can_sign   s    zRsaKey.can_signc                 C   s   t | j| jdS )z^A matching RSA public key.

        Returns:
            a new :class:`RsaKey` object
        r   )r   r1   r3   r2   r,   r,   r-   
public_key   s    zRsaKey.public_keyc                 C   sH   |   |  krdS | j|jks,| j|jkr0dS |   s<dS | j|jkS )NFT)r4   r   r   r   r(   otherr,   r,   r-   __eq__   s    zRsaKey.__eq__c                 C   s
   | |k S r/   r,   rR   r,   r,   r-   __ne__   s    zRsaKey.__ne__c                 C   s   ddl m} |d S )Nr   )PicklingError)picklerV   )r(   rV   r,   r,   r-   __getstate__   s    zRsaKey.__getstate__c                 C   sP   |   r2dt| jt| jt| jt| jf }nd}dt| jt| j|f S )Nz, d=%d, p=%d, q=%d, u=%d zRsaKey(n=%d, e=%d%s))r4   r0   r"   r#   r%   r;   r1   r3   )r(   extrar,   r,   r-   __repr__   s    zRsaKey.__repr__c                 C   s"   |   rd}nd}d|t| f S )NZPrivateZPublicz%s RSA key at 0x%X)r4   id)r(   key_typer,   r,   r-   __str__   s    zRsaKey.__str__PEMNr   c                 C   s  |durt |}|du rtj}|dkrdd | j| jfD \}}t|d d@ rXd| }t|d d@ rpd| }d||g}d	d
d |D }	dt|	dd  S | 	 rt
d| j| j| j| j| j| j| jd  | j| jd  t| j| jg	 }
|dkr$d}|dkr|rtdnbddlm} |dkr^|du r^d}|j|
tdt d}
n(d}|sld}|j|
t||t d}
d}nd}ttt
| j| jgt }
|dkr|
S |dkrddlm} ||
|||}t |S td| dS )a5  Export this RSA key.

        Args:
          format (string):
            The format to use for wrapping the key:

            - *'PEM'*. (*Default*) Text encoding, done according to `RFC1421`_/`RFC1423`_.
            - *'DER'*. Binary encoding.
            - *'OpenSSH'*. Textual encoding, done according to OpenSSH specification.
              Only suitable for public keys (not private keys).

          passphrase (string):
            (*For private keys only*) The pass phrase used for protecting the output.

          pkcs (integer):
            (*For private keys only*) The ASN.1 structure to use for
            serializing the key. Note that even in case of PEM
            encoding, there is an inner ASN.1 DER structure.

            With ``pkcs=1`` (*default*), the private key is encoded in a
            simple `PKCS#1`_ structure (``RSAPrivateKey``).

            With ``pkcs=8``, the private key is encoded in a `PKCS#8`_ structure
            (``PrivateKeyInfo``).

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

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

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

            - For *'DER'*, the *PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC*
              scheme is used. The following operations are performed:

                1. A 16 byte Triple DES key is derived from the passphrase
                   using :func:`Cryptodome.Protocol.KDF.PBKDF2` with 8 bytes salt,
                   and 1 000 iterations of :mod:`Cryptodome.Hash.HMAC`.
                2. The private key is encrypted using CBC.
                3. The encrypted key is encoded according to PKCS#8.

            - For *'PEM'*, the obsolete PEM encryption scheme is used.
              It is based on MD5 for key derivation, and Triple DES for encryption.

            Specifying a value for :attr:`protection` is only meaningful for PKCS#8
            (that is, ``pkcs=8``) and only if a pass phrase is present too.

            The supported schemes for PKCS#8 are listed in the
            :mod:`Cryptodome.IO.PKCS8` module (see :attr:`wrap_algo` parameter).

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

        Returns:
          byte string: 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
        NZOpenSSHc                 S   s   g | ]}|  qS r,   )to_bytes.0xr,   r,   r-   
<listcomp>V      z%RsaKey.export_key.<locals>.<listcomp>r          s   ssh-rsare   c                 S   s    g | ]}t d t|| qS )>I)structpacklen)rb   kpr,   r,   r-   rd   \  re      ssh-rsa r   zRSA PRIVATE KEYZDERz&PKCS#1 private key cannot be encryptedPKCS8r_   zPRIVATE KEY)Z
key_paramszENCRYPTED PRIVATE KEYz"PBKDF2WithHMAC-SHA1AndDES-EDE3-CBCz
PUBLIC KEYr_   z3Unknown key format '%s'. Cannot export the RSA key.)r   r   get_random_bytesr3   r1   r	   joinbinascii
b2a_base64r4   r   r   r   r   r   r   r   r8   encoder   Cryptodome.IOrp   wrapr   r   r   r_   )r(   format
passphraseZpkcsZ
protectionrandfuncZe_bytesZn_byteskeyparts	keystringZ
binary_keyr]   rp   r_   Zpem_strr,   r,   r-   
export_key  sp    M








zRsaKey.export_keyc                 C   s   t dd S Nz0Use module Cryptodome.Signature.pkcs1_15 insteadNotImplementedError)r(   MKr,   r,   r-   sign  s    zRsaKey.signc                 C   s   t dd S r   r   )r(   r   	signaturer,   r,   r-   verify  s    zRsaKey.verifyc                 C   s   t dd S Nz/Use module Cryptodome.Cipher.PKCS1_OAEP insteadr   )r(   rA   r   r,   r,   r-   encrypt  s    zRsaKey.encryptc                 C   s   t dd S r   r   )r(   rD   r,   r,   r-   decrypt  s    zRsaKey.decryptc                 C   s   t d S r/   r   r(   r   Br,   r,   r-   blind  s    zRsaKey.blindc                 C   s   t d S r/   r   r   r,   r,   r-   unblind  s    zRsaKey.unblindc                 C   s   t d S r/   r   r2   r,   r,   r-   size  s    zRsaKey.size)r_   Nr   NN)'__name__
__module____qualname____doc__r.   propertyr   r   r   r   r   r6   r7   r9   r:   r   r=   r?   rB   rL   r4   rO   rP   rQ   rT   rU   rX   r[   r^   r~   Z	exportKeyZ	publickeyr   r   r   r   r   r   r   r,   r,   r,   r-   r   3   s`   









	  
 r     c                    sb  | dk rt d d dks$ dk r,t d|du r:tj}td }}t  | | kr0|d| d > k r0| d }| | }tdd| d >   ||krtdd| d >   fd	d
}t|||dtd| d d >  fdd}t|||d}	|	 }d |	d }
 |
}qN|	krD|	 }	|	}t	| ||	|dS )a4  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.
      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                    s   | ko| d   dkS Nr   )gcd	candidate)r   min_pr,   r-   filter_p  s    zgenerate.<locals>.filter_p)Z
exact_bitsr{   Zprime_filterd   c                    s*   | ko(| d   dko(t|  kS r   )r   absr   )r   min_distancemin_qr   r,   r-   filter_q  s
    zgenerate.<locals>.filter_qr   r   r   r   r   r   )
r   r   rr   r   r=   sqrtr   lcmr8   r   )bitsr{   r   r   r   Zsize_qZsize_pr   r   r   r   r   r,   )r   r   r   r   r   r-   r     s@     


r   Tc                 C   s  G dd dt }| }td| D ]\}}t||t| q |j}|j}t|ds`t||d}n0|j}	t|dr~|j	}
|j
}n|	| d }|}|d d	kr|d }qd
}td}|s6|dk r6t|}||k r,t|||}|dkr"||d kr"t|d|dkr"t||d }
d}q,|d9 }q|d7 }q|sDtd||
 d	ksVJ ||
 }t|drr|j}n
|
|}t|||	|
||d}|r|dks||krtdt||dkrtd|d@ std| r|	dks|	|krtdt||	dkr"td|
| |kr8tdt|
tkrNtdt|tkrdtd|
d |d  }||
d |d  }||	 t| dkrtdt|dr|dks||krtd|
| | dkrtd|S )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.

        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                   @   s   e Zd ZdS )zconstruct.<locals>.InputCompsN)r   r   r   r,   r,   r,   r-   
InputComps%  s   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   r@   r   r   r   r8   r4   r   r   r0   )Zrsa_componentsZconsistency_checkr   Zinput_compscompr+   r   r   keyr   r   r   ZktottZspottedakcandr   phir   r,   r,   r-   r     s    (



*




r   c                 G   sN   t  j| ddd}|d dkr&tdt|dd t|d |d	 g S )
N	   TZnr_elementsZonly_ints_expectedr   z(No PKCS#1 encoding of an RSA private keyr            )r   decoder   r   r   r8   encodedr)   derr,   r,   r-   _import_pkcs1_private  s    r   c                 G   s   t  j| ddd}t|S )Nr   Tr   )r   r   r   r   r,   r,   r-   _import_pkcs1_public  s    r   c                 G   s.   t | \}}}|tks|d ur&tdt|S )NzNo RSA subjectPublicKeyInfo)r   r   r   r   )r   r)   ZalgoidZencoded_keyparamsr,   r,   r-   _import_subjectPublicKeyInfo  s    r   c                 G   s   t | }t|S r/   )r   r   )r   r)   Zsp_infor,   r,   r-   _import_x509_cert  s    r   c                 C   s:   ddl m} || |}|d tkr,tdt|d |S )Nr   ro   zNo PKCS#8 encoded RSA keyr   )rw   rp   unwrapr   r   _import_keyDER)r   rz   rp   r   r,   r,   r-   _import_pkcs8  s
    r   c              	   C   sH   t ttttf}|D ](}z|| |W   S  ty8   Y q0 qtddS )z@Import an RSA key (public or private half), encoded in DER form.RSA key format is not supportedN)r   r   r   r   r   r   )
extern_keyrz   Z	decodingsZdecodingr,   r,   r-   r     s    r   c                 C   s   ddl m}m}m}m} || |\}}|dkr6td||\}}||\}	}||\}
}||\}}||\}}||\}}||\}}|| 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                 S   s   g | ]}t |qS r,   )r   
from_bytesra   r,   r,   r-   rd     re   z/_import_openssh_private_rsa.<locals>.<listcomp>)Z_opensshr   r   r   r   r   r   )datapasswordr   r   r   r   Zssh_nameZ	decryptedr   r   r   Ziqmpr   r   r   paddedbuildr,   r,   r-   _import_openssh_private_rsa  s    r   c                 C   sV  ddl m} t| } |dur$t|}| drVt| }|||\}}}t||}|S | dr|t| |\}}}|r~d}t||S | dr t	| 
dd }	g }
t|	d	krtd
|	dd	 d }|
|	d	d	|   |	d	| d }	qt|
d }t|
d }t||gS t| dkrJt| d dkrJt| |S tddS )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   rq   Ns   -----BEGIN OPENSSH PRIVATE KEYs   -----rm       r   r   rh   r   0   r   )rw   r_   r   
startswithr
   r   r   r   rt   
a2b_base64splitrk   ri   unpackappendr   r   r   r	   r   )r   rz   r_   Ztext_encodedZopenssh_encodedmarkerZenc_flagrK   r   r}   r|   lengthr   r   r,   r,   r-   r     s6    )



 
r   z1.2.840.113549.1.1.1)Nr   )T)N)$__all__rt   ri   Z
Cryptodomer   ZCryptodome.Util.py3compatr   r	   r
   ZCryptodome.Util.asn1r   r   ZCryptodome.Math.Numbersr   ZCryptodome.Math.Primalityr   r   r   ZCryptodome.PublicKeyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z	importKeyr   r,   r,   r,   r-   <module>    s0     {
Q
 		
P