
    Bwg;                        d dl mZmZmZmZ d dlZddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZ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+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 g dZ4 G d de      Z5 G d de      Z6 G d de      Zd.dZ7d Z8d Z9d Z:d/dZ;d Z<d Z=d/dZ>d/dZ?d Z@d ZAd ZBd  ZCd! ZDd" ZEd# ZFd$ ZGd% ZHd& ZId' ZJd0d(ZKd) ZLd* ZMd+ ZNd, ZOd0d-ZPy)1    )unicode_literalsdivisionabsolute_importprint_functionN   )CertificateDHParametersECDomainParametersPrivateKeyInfoPublicKeyAlgorithmPublicKeyInfo)	_CertificateBase_fingerprint_parse_pkcs12_PrivateKeyBase_PublicKeyBase_unwrap_private_key_infoparse_certificateparse_privateparse_public)pretty_message)	buffer_from_bytesbuffer_pointerbytes_from_bufferderefis_nullnewnullunwrapwrite_to_buffer   )	libcryptoLibcryptoConstlibcrypto_version_infohandle_openssl_error)AsymmetricKeyErrorIncompleteAsymmetricKeyErrorSignatureError)	type_namestr_clsbyte_cls	int_types)constant_compare)r   dsa_sign
dsa_verify
ecdsa_signecdsa_verifygenerate_pairload_certificateload_pkcs12load_private_keyload_public_keyparse_pkcs12
PrivateKey	PublicKeyrsa_oaep_decryptrsa_oaep_encryptrsa_pkcs1v15_decryptrsa_pkcs1v15_encryptrsa_pkcs1v15_signrsa_pkcs1v15_verifyrsa_pss_signrsa_pss_verifyc                   H    e Zd ZdZdZdZdZd Zed        Z	ed        Z
d Zy)r8   zC
    Container for the OpenSSL representation of a private key
    Nc                 6    || _         || _        t        | _        y)z
        :param evp_pkey:
            An OpenSSL EVP_PKEY value from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PrivateKeyInfo object
        Nevp_pkeyasn1r"   _libselfrE   rF   s      S/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_openssl/asymmetric.py__init__zPrivateKey.__init__P        !		    c                    | j                   0t        j                  | j                  t	                     }t        |      }t        |      }t        j                  | j                  |      }t        |       t        ||      }t        j                  |      }t        dk  rN|j                  dk(  r?|j                         }d|d   d<   |j                         }t        ||       t!        |      }t        j"                  t	               t        |      |      }	t%        |	      rt        d       t'        |	|      | _         | j                   S )z\
        :return:
            A PublicKey object corresponding to this private key.
           
rsassa_pssrsa	algorithmr   )_public_keyr"   
i2d_PUBKEYrE   r   r   r   r%   r   r   loadr$   rS   copydumpr    len
d2i_PUBKEYr   r9   )
rI   buffer_sizepubkey_bufferpubkey_pointerpubkey_lengthpubkey_datarF   	temp_asn1	temp_datapub_evp_pkeys
             rJ   
public_keyzPrivateKey.public_key]   s    ##..t}}dfEK-k:M+M:N%00OM /+M=IK %%k2D &,<1O IIK	6;	+&{3%NN,	y9 #I$//}8UWdeL|$$Q'(t<DrM   c                 p    | j                   t        | j                  t              | _         | j                   S )aY  
        Creates a fingerprint that can be compared with a public key to see if
        the two form a pair.

        This fingerprint is not compatible with fingerprints generated by any
        other software.

        :return:
            A byte string that is a sha256 hash of selected components (based
            on the key type)
        )r   rF   r5   rI   s    rJ   fingerprintzPrivateKey.fingerprint   s0     $ ,TYY8H ID   rM   c                     | j                   r4| j                  j                  | j                          d | _        d | _         y y NrE   rG   EVP_PKEY_freere   s    rJ   __del__zPrivateKey.__del__   2    ==II##DMM2DI DM rM   )__name__
__module____qualname____doc__rE   rT   rG   rK   propertyrc   rf   rk    rM   rJ   r8   r8   D   sM     HK D    B ! !"!rM   r8   c                   $    e Zd ZdZdZdZd Zd Zy)r9   zB
    Container for the OpenSSL representation of a public key
    Nc                 6    || _         || _        t        | _        y)z
        :param evp_pkey:
            An OpenSSL EVP_PKEY value from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PublicKeyInfo object
        NrD   rH   s      rJ   rK   zPublicKey.__init__   rL   rM   c                     | j                   r4| j                  j                  | j                          d | _        d | _         y y rh   ri   re   s    rJ   rk   zPublicKey.__del__   rl   rM   )rm   rn   ro   rp   rE   rG   rK   rk   rr   rM   rJ   r9   r9      s     H D!rM   r9   c                   \    e Zd ZdZdZdZdZdZd Ze	d        Z
e	d        Ze	d        Zd Zy)r   zC
    Container for the OpenSSL representation of a certificate
    Nc                 6    || _         || _        t        | _        y)z
        :param x509:
            An OpenSSL X509 value from loading/importing the certificate

        :param asn1:
            An asn1crypto.x509.Certificate object
        N)x509rF   r"   rG   )rI   rx   rF   s      rJ   rK   zCertificate.__init__   s     			rM   c                 .    | j                   j                  S )z_
        :return:
            The EVP_PKEY of the public key this certificate contains
        )rc   rE   re   s    rJ   rE   zCertificate.evp_pkey   s     '''rM   c                    | j                   s| j                  rt        dk  rS| j                  j                  j
                  dk(  r0t        | j                  j                        | _         | j                   S t        j                  | j                        }t        || j                  j                        | _         | j                   S )zh
        :return:
            The PublicKey object for the public key this certificate contains
        rO   rQ   )
rT   rx   r$   rF   rc   rS   r6   r"   X509_get_pubkeyr9   )rI   rE   s     rJ   rc   zCertificate.public_key   s     DII &,1E1E1O1OS_1_#24993G3G#H 
  %44TYY?#,Xtyy7K7K#L rM   c                 ^   | j                   d| _         | j                  j                  t        ddg      v r| j                  d   j                  }| j                  d   j
                  }|dk(  rt        }n9|dk(  rt        }n-|dk(  rt        }n!|dk(  rt        }nt        t        d	|            	  || j                  | j                  d
   j                  | j                  d   j                         |       d| _         | j                   S | j                   S # t        $ r Y | j                   S w xY w)zT
        :return:
            A boolean - if the certificate is self-signed
        Fyesmaybesignature_algorithmrsassa_pkcs1v15rQ   dsaecdsaz
                        Unable to verify the signature of the certificate since
                        it uses the unsupported algorithm %s
                        signature_valuetbs_certificateT)_self_signedrF   self_signedsetsignature_algo	hash_algor?   rA   r/   r1   OSErrorr   rc   nativerX   r(   )rI   r   r   verify_funcs       rJ   r   zCertificate.self_signed   s4    $ %Dyy$$UG,<(==!%+@!A!P!P II&;<FF	!%66"5K#|3"0K#u,",K#w.".K!. '#  			"34;;		"3499;!	 )-D%    t    '    s   /AD 	D,+D,c                     | j                   r!| j                   j                          d | _         | j                  r4| j                  j	                  | j                         d | _        d | _        y y rh   )rT   rk   rx   rG   	X509_freere   s    rJ   rk   zCertificate.__del__  sU    $$&#D99II		*DIDI rM   )rm   rn   ro   rp   rx   rT   r   rG   rK   rq   rE   rc   r   rk   rr   rM   rJ   r   r      sf     DKL D ( (    " )! )!VrM   r   c           
      H   | t        g d      vrt        t        dt        |                   | dk(  r-|t        g d      vrt        t        dt        |                  | dk(  rYt        dk  r#|dk7  r}t        t        d	t        |                  |t        g d
      vrPt        t        dt        |                  | dk(  r-|t        g d      vrt        t        dt        |                  | dk(  rd}d}	 t        j                         }t        |      rt        d       t        t
        d      }t        j                  |d      }t        |       t        |      }t        j                  |||t                     }t        |       t        j                  |t                     }|dk  rt        |       t        |      }t        j                  |t!        |            }|dk  rt        |       t#        ||      }	t        j$                  |t                     }|dk  rt        |       t        |      }t        j$                  |t!        |            }|dk  rt        |       t#        ||      }
|rt        j&                  |       |rt        j(                  |       n{| dk(  rd}	 t        j*                         }t        |      rt        d       t        j,                  ||t               dt               t               t                     }t        |       t        j.                  |      }t        |       t        j0                  |t                     }|dk  rt        |       t        |      }t        j0                  |t!        |            }|dk  rt        |       t#        ||      }	t        j2                  |t                     }|dk  rt        |       t        |      }t        j2                  |t!        |            }|dk  rt        |       t#        ||      }
|rt        j4                  |       n| dk(  rd}	 t6        j8                  t6        j:                  t6        j<                  d|   }t        j>                  |      }t        |      rt        d       t        j@                  |      }t        |       t        jB                  |t6        jD                         t        jF                  |t                     }|dk  rt        |       t        |      }t        jF                  |t!        |            }|dk  rt        |       t#        ||      }tI        tK        dtM        d|      d      |d      }|jO                         }	t        jP                  |t                     }|dk  rt        |       t        |      }t        jP                  |t!        |            }|dk  rt        |       t#        ||      }
|rt        jR                  |       	 tU        	      tW        
      fS # |rt        j&                  |       |rt        j(                  |       w w xY w# |rt        j4                  |       w w xY w# |rt        jR                  |       w w xY w)aP  
    Generates a public/private key pair

    :param algorithm:
        The key algorithm - "rsa", "dsa" or "ec"

    :param bit_size:
        An integer - used for "rsa" and "dsa". For "rsa" the value maye be 1024,
        2048, 3072 or 4096. For "dsa" the value may be 1024, plus 2048 or 3072
        if OpenSSL 1.0.0 or newer is available.

    :param curve:
        A unicode string - used for "ec" keys. Valid values include "secp256r1",
        "secp384r1" and "secp521r1".

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A 2-element tuple of (PublicKey, PrivateKey). The contents of each key
        may be saved by calling .asn1.dump().
    )rR   r   eczM
            algorithm must be one of "rsa", "dsa", "ec", not %s
            rR   )            zX
                bit_size must be one of 1024, 2048, 3072, 4096, not %s
                r   r!   r   zG
                    bit_size must be 1024, not %s
                    )r   r   r   zZ
                    bit_size must be one of 1024, 2048, 3072, not %s
                    r   )	secp256r1	secp384r1	secp521r1zt
                curve must be one of "secp256r1", "secp384r1", "secp521r1",
                not %s
                Nr   z	BIGNUM **s   65537named)namevalue)rS   
parameters)rS   rc   ),r   
ValueErrorr   reprr$   r"   RSA_newr   r%   r   	BN_dec2bnr   RSA_generate_key_exr   i2d_RSAPublicKeyr   r   r   i2d_RSAPrivateKeyRSA_freeBN_freeDSA_newDSA_generate_parameters_exDSA_generate_keyi2d_DSA_PUBKEYi2d_DSAPrivateKeyDSA_freer#   NID_X9_62_prime256v1NID_secp384r1NID_secp521r1EC_KEY_new_by_curve_nameEC_KEY_generate_keyEC_KEY_set_asn1_flagOPENSSL_EC_NAMED_CURVEi2o_ECPublicKeyr   r   r
   rX   i2d_ECPrivateKeyEC_KEY_freer6   r5   )rS   bit_sizecurverR   exponentexponent_pointerresultbuffer_lengthbufferpublic_key_bytesprivate_key_bytesr   ec_keycurve_idpublic_key_point_bytesrc   s                   rJ   r2   r2   #  s   4 011 O	
  	 E3788^ X	   
e	!D(4  N	"   s#566  N	"   
d	CDD^ U   E#	,##%Cs|$Q'"9k:(()98DF (./H223(DFSF (%66sDFCMq $]3&}5F//^F5KLFz$V,0G%77TVDMq $]3&}5F00nV6LMFz$V, 1&- H ""3'!!(+	e		(##%Cs|$Q'99#xQRTXTZ\`\bdhdjkF (//4F (%44S$&AMq $]3&}5F--c>&3IJFz$V,0G%77TVDMq $]3&}5F00nV6LMFz$V, 1&- H ""3'	d	2	.+@@+99+99 	H 77AFv$Q'226:F (**6>3X3XY%55fdfEMq $]3&}5F..v~f7MNFz$V,%6v}%M" '/!%"4$##1  5	( 	J  *0%66vtvFMq $]3&}5F//v8NOFz$V, 1&- H %%f-,-/?@Q/RSSA ""3'!!(+ H ""3' n %%f- s'   ;E9X6 .E8Y* 	F?Z 61Y'*ZZ!c                    t        | t              st        t        dt	        |                   | dk  rt        d      | dkD  rt        d      | dz  dk7  rt        d      d	}	 t        j                         }t        |      rt        d       t        j                  || t        j                  t                     }t        |       t        j                  |t                     }|dk  rt        |       t        |      }t        j                  |t!        |            }|dk  rt        |       t#        ||      }t%        j&                  |      |rt        j(                  |       S S # |rt        j(                  |       w w xY w)
a`  
    Generates DH parameters for use with Diffie-Hellman key exchange. Returns
    a structure in the format of DHParameter defined in PKCS#3, which is also
    used by the OpenSSL dhparam tool.

    THIS CAN BE VERY TIME CONSUMING!

    :param bit_size:
        The integer bit size of the parameters to generate. Must be between 512
        and 4096, and divisible by 64. Recommended secure value as of early 2016
        is 2048, with an absolute minimum of 1024.

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        An asn1crypto.algos.DHParameters object. Use
        oscrypto.asymmetric.dump_dh_parameters() to save to disk for usage with
        web servers.
    z=
            bit_size must be an integer, not %s
            i   z-bit_size must be greater than or equal to 512r   z+bit_size must be less than or equal to 4096@   r   z!bit_size must be a multiple of 64N)
isinstancer,   	TypeErrorr   r)   r   r"   DH_newr   r%   DH_generate_parameters_exr#   DH_GENERATOR_2r   i2d_DHparamsr   r   r   r	   rV   DH_free)r   dhr   r   r   dh_params_bytess         rJ   generate_dh_parametersr     s]   0 h	* h	
  	 #~HII$FGG"}<==	B"2; #44R>C`C`bfbhiV$!..r46:1 /"=1''N6,BCA: (+FMB  1 b! 2b! s   %C+E) )Fc                    t        | t              r| }t        |      S t        | t              rt        |       }t        |      S t        | t              r9t        | d      5 }t        |j                               }ddd       t        |      S t        t        dt        |                   # 1 sw Y   t              S xY w)a  
    Loads an x509 certificate into a Certificate object

    :param source:
        A byte string of file contents, a unicode string filename or an
        asn1crypto.x509.Certificate object

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A Certificate object
    rbNz
            source must be a byte string, unicode string or
            asn1crypto.x509.Certificate object, not %s
            )r   Asn1Certificater+   r   r*   openreadr   r   r)   
_load_x509)sourcecertificatefs      rJ   r3   r3   4  s    " &/*$ k""! 
FH	%'/ k"" 
FG	$&$ 	61+AFFH5K	6 k""  f
  			6 k""s    B++B>c                     | j                         }t        |      }t        j                  t	               t        |      t        |            }t        |      rt        d       t        ||       S )z
    Loads an ASN.1 object of an x509 certificate into a Certificate object

    :param certificate:
        An asn1crypto.x509.Certificate object

    :return:
        A Certificate object
    r   )
rX   r   r"   d2i_X509r   r   rY   r   r%   r   )r   r   r   rE   s       rJ   r   r   [  sX     Fv&F!!$&.*@#f+NHxQx--rM   c                    t        | t              r| }t        |      S |Ot        |t              r|j                  d      }t        |t              st        t        dt        |                  t        | t              r&t        | d      5 }|j                         } ddd       n.t        | t              st        t        dt        |                   t        | |      }t        |      S # 1 sw Y    xY w)a   
    Loads a private key into a PrivateKey object

    :param source:
        A byte string of file contents, a unicode string filename or an
        asn1crypto.keys.PrivateKeyInfo object

    :param password:
        A byte or unicode string to decrypt the private key file. Unicode
        strings will be encoded using UTF-8. Not used is the source is a
        PrivateKeyInfo object.

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        oscrypto.errors.AsymmetricKeyError - when the private key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey object
    Nutf-8zP
                    password must be a byte string, not %s
                    r   z
                source must be a byte string, unicode string or
                asn1crypto.keys.PrivateKeyInfo object, not %s
                )r   r   r*   encoder+   r   r   r)   r   r   r   	_load_key)r   passwordprivate_objectr   s       rJ   r5   r5   o  s    . &.): ^$$5 (G,#??73h1 h'	!   fg&fd# "q" " FH-N &!   'vx8^$$" "s   C**C3c                 ^   t        | t              r| }nyt        | t              rt        |       }n]t        | t              r/t        | d      5 }t        |j                               }ddd       nt        t        dt        |                   j                  dk(  rWt        dk  r.|j                  dk(  rt        t        d|j                              |j                  t        t        d            t        d	k  r8|j                  d
k(  r)|j!                         }d|d   d<   |j#                         }n|j#                         }t%        |      }t'        j(                  t+               t-        |      t/        |            }t1        |      rt3        d       t5        ||      S # 1 sw Y   xY w)a3  
    Loads a public key into a PublicKey object

    :param source:
        A byte string of file contents, a unicode string filename or an
        asn1crypto.keys.PublicKeyInfo object

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        oscrypto.errors.AsymmetricKeyError - when the public key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PublicKey object
    r   Nz
            source must be a byte string, unicode string or
            asn1crypto.keys.PublicKeyInfo object, not %s
            r   r   sha2z
                OpenSSL 0.9.8 only supports DSA keys based on SHA1 (2048 bits or
                less) - this key is based on SHA2 and is %s bits
                z
                The DSA key does not contain the necessary p, q and g
                parameters and can not be used
                rO   rQ   rR   rS   r   )r   r   r+   r   r*   r   r   r   r   r)   rS   r$   r   r&   r   r'   rW   rX   r   r"   rZ   r   r   rY   r   r%   r9   )r   rc   r   temp_keydatar   rE   s          rJ   r6   r6     s   $ &-(
	FH	%!&)
	FG	$&$ 	01%affh/J	0 	0  f
  	 u$!D(Z-A-AV-K$^ ##&   !!).~0   $)=)=)M??$-2k*}} t$F##DFN6,BCINHxQXz**U	0 	0s   F""F,c                 |   t         dk  r=| j                  dk(  r.| j                  dk(  rt        t	        d| j
                              t        |       j                         }t        |      }t        j                  t               t        |      t        |            }t        |      rt        d       t!        ||       S )z
    Loads a private key into a PrivateKey object

    :param private_object:
        An asn1crypto.keys.PrivateKeyInfo object

    :return:
        A PrivateKey object
    r   r   r   z
            OpenSSL 0.9.8 only supports DSA keys based on SHA1 (2048 bits or
            less) - this key is based on SHA2 and is %s bits
            r   )r$   rS   r   r&   r   r   r   rX   r   r"   d2i_AutoPrivateKeyr   r   rY   r   r%   r8   )r   r   r   rE   s       rJ   r   r     s     $)A)AU)J~OgOgkqOq  ##"
  	 &n5::<Fv&F++DFN64JCPVKXHxQh//rM   c                 $    t        | |t              S )a  
    Parses a PKCS#12 ANS.1 DER-encoded structure and extracts certs and keys

    :param data:
        A byte string of a DER-encoded PKCS#12 file

    :param password:
        A byte string of the password to any encrypted data

    :raises:
        ValueError - when any of the parameters are of the wrong type or value
        OSError - when an error is returned by one of the OS decryption functions

    :return:
        A three-element tuple of:
         1. An asn1crypto.keys.PrivateKeyInfo object
         2. An asn1crypto.x509.Certificate object
         3. A list of zero or more asn1crypto.x509.Certificate objects that are
            "extra" certificates, possibly intermediates from the cert chain
    )r   r5   )r   r   s     rJ   r7   r7   
  s    , x)9::rM   c                 $   |Ot        |t              r|j                  d      }t        |t              st	        t        dt        |                  t        | t              r&t        | d      5 }|j                         } ddd       n.t        | t              st	        t        dt        |                   t        | |      \  }}}d}d}|rt        |      }|rt        |      }|D cg c]  }t        |       }	}|||	fS # 1 sw Y   TxY wc c}w )a  
    Loads a .p12 or .pfx file into a PrivateKey object and one or more
    Certificates objects

    :param source:
        A byte string of file contents or a unicode string filename

    :param password:
        A byte or unicode string to decrypt the PKCS12 file. Unicode strings
        will be encoded using UTF-8.

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        oscrypto.errors.AsymmetricKeyError - when a contained key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A three-element tuple containing (PrivateKey, Certificate, [Certificate, ...])
    Nr   zH
                password must be a byte string, not %s
                r   zR
            source must be a byte string or a unicode string, not %s
            )r   r*   r   r+   r   r   r)   r   r   r7   r   r   )
r   r   r   key_info	cert_infoextra_certs_infokeycertinfoextra_certss
             rJ   r4   r4   #  s   , h(w/H(H-N (#	   &'"&$ 	1VVXF	 	 ) f	
  	 -9,J)Hi)
CD!)$0@A:d#AKA{##1	 	, Bs   .D(DD
c                 8    t        | |t        j                        S )aF  
    Encrypts a byte string using an RSA public key or certificate. Uses PKCS#1
    v1.5 padding.

    :param certificate_or_public_key:
        A PublicKey or Certificate object

    :param data:
        A byte string, with a maximum length 11 bytes less than the key length
        (in bytes)

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the encrypted data
    )_encryptr#   RSA_PKCS1_PADDINGcertificate_or_public_keyr   s     rJ   r=   r=   `  s    * -t^5U5UVVrM   c                 8    t        | |t        j                        S )a  
    Decrypts a byte string using an RSA private key. Uses PKCS#1 v1.5 padding.

    :param private_key:
        A PrivateKey object

    :param ciphertext:
        A byte string of the encrypted data

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the original plaintext
    )_decryptr#   r   private_key
ciphertexts     rJ   r<   r<   x  s    & K^-M-MNNrM   c                 8    t        | |t        j                        S )aZ  
    Encrypts a byte string using an RSA public key or certificate. Uses PKCS#1
    OAEP padding with SHA1.

    :param certificate_or_public_key:
        A PublicKey or Certificate object

    :param data:
        A byte string, with a maximum length 41 bytes (or more) less than the
        key length (in bytes)

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the encrypted data
    )r   r#   RSA_PKCS1_OAEP_PADDINGr   s     rJ   r;   r;     s    * -t^5Z5Z[[rM   c                 8    t        | |t        j                        S )a  
    Decrypts a byte string using an RSA private key. Uses PKCS#1 OAEP padding
    with SHA1.

    :param private_key:
        A PrivateKey object

    :param ciphertext:
        A byte string of the encrypted data

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the original plaintext
    )r   r#   r   r   s     rJ   r:   r:     s    ( K^-R-RSSrM   c                 h    t         dk  rt        j                  |       S t        j                  |       S )z
    Handles the function name change from OpenSSL 1.1 -> 3.0

    :param evp_pkey:
        The EVP_PKEY of the Certificte or PublicKey to get the size of

    :return:
        An int of the number of bytes necessary for the key
    rO   )r$   r"   EVP_PKEY_sizeEVP_PKEY_get_size)rE   s    rJ   _evp_pkey_get_sizer     s.     %&&x00&&x00rM   c                 &   t        | t        t        f      st        t	        dt        |                   t        |t              st        t	        dt        |                  d}	 t        | j                        }t        |      }t        j                  | j                        }t        j                  t        |      ||||      }t        |       t        ||      |rt        j                   |       S S # |rt        j                   |       w w xY w)a%  
    Encrypts plaintext using an RSA public key or certificate

    :param certificate_or_public_key:
        A PublicKey, Certificate or PrivateKey object

    :param data:
        The byte string to encrypt

    :param padding:
        The padding mode to use

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the encrypted data
    
            certificate_or_public_key must be an instance of the Certificate or
            PublicKey class, not %s
            <
            data must be a byte string, not %s
            N)r   r   r9   r   r   r)   r+   r   rE   r   r"   EVP_PKEY_get1_RSARSA_public_encryptrY   r%   r   r   )r   r   paddingrR   r[   r   ress          rJ   r   r     s   , /+y1IJ /0
  	 dH% dO	
  	 C$()B)K)KL";/))*C*L*LM**3t9dFCQS! - s# 3s# s   &A7C6 6Dc                    t        | t              st        t        dt	        |                   t        |t
              st        t        dt	        |                  d}	 t        | j                        }t        |      }t        j                  | j                        }t        j                  t        |      ||||      }t        |       t        ||      |rt        j                  |       S S # |rt        j                  |       w w xY w)a  
    Decrypts RSA ciphertext using a private key

    :param private_key:
        A PrivateKey object

    :param ciphertext:
        The ciphertext - a byte string

    :param padding:
        The padding mode to use

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    zY
            private_key must be an instance of the PrivateKey class, not %s
            zB
            ciphertext must be a byte string, not %s
            N)r   r8   r   r   r)   r+   r   rE   r   r"   r  RSA_private_decryptrY   r%   r   r   )r   r   r  rR   r[   r   r  s          rJ   r   r     s    , k:. k"	
  	 j(+ j!	
  	 C$()=)=>";/))+*>*>?++C
OZQTV]^S! - s# 3s# s    A7C0 0D
c                     | j                   dk7  r-t        t        d| j                   j                                     t	        | |||      S )a  
    Verifies an RSASSA-PKCS-v1.5 signature.

    When the hash_algorithm is "raw", the operation is identical to RSA
    public key decryption. That is: the data is not hashed and no ASN.1
    structure with an algorithm identifier of the hash algorithm is placed in
    the encrypted byte string.

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384",
        "sha512" or "raw"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    rR   L
            The key specified is not an RSA public key, but %s
            rS   r   r   upper_verifyr   	signaturer   hash_algorithms       rJ   r?   r?   <  sS    : !**e3 &//557	
  	 ,i~NNrM   c                     | j                   }|dk7  r2|dk7  r-t        t        d| j                   j                                     t	        | |||d      S )a  
    Verifies an RSASSA-PSS signature. For the PSS padding the mask gen algorithm
    will be mgf1 using the same hash algorithm as the signature. The salt length
    with be the length of the hash algorithm, and the trailer field with be the
    standard 0xBC byte.

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    rR   rQ   r	  Trsa_pss_paddingr
  )r   r  r   r  cp_algs        rJ   rA   rA   d  sa    4 '00F6\1 &//557	
  	 ,i~_cddrM   c                     | j                   dk7  r-t        t        d| j                   j                                     t	        | |||      S )a  
    Verifies a DSA signature

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    r   zK
            The key specified is not a DSA public key, but %s
            r
  r  s       rJ   r/   r/     sS    . !**e3 &//557	
  	 ,i~NNrM   c                     | j                   dk7  r-t        t        d| j                   j                                     t	        | |||      S )a  
    Verifies an ECDSA signature

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    r   zK
            The key specified is not an EC public key, but %s
            r
  r  s       rJ   r1   r1     sS    . !**d2 &//557	
  	 ,i~NNrM   c           	         t        | t        t        f      st        t	        dt        |                   t        |t              st        t	        dt        |                  t        |t              st        t	        dt        |                  | j                  }|dk(  xs |dk(  }t        g d      }|r|s|t        dg      z  }||vr*d}|r|s|d	z  }t        t	        d
|t        |                  |s%|r#t        t	        d|j                                     |r|dk(  rt        |      | j                  dz
  kD  r)t        t	        d| j                  t        |                  d}		 t        j                  | j                         }	t#        |	      rt%        d       t'        | j                         }
t)        |
      }t        j*                  t        |      |||	t,        j.                        }t%        |       t1        ||      }t3        ||      st5        d      	 |	rt        j6                  |	       yyd}d}	d}d}d}d}	 t8        dk  rt        j:                         }nt        j<                         }	 t        j>                  t        j@                  t        jB                  t        jD                  t        jF                  t        jH                  d|          }t8        dk  r|r|r tK        tL        |      |      jO                         }t        j                  | j                         }	t#        |	      rt%        d       t'        | j                         }
t)        |
      }t        j*                  t        |      |||	t,        jP                        }t%        |       t        jR                  |	|||t,        jT                        }n|rt        jV                  ||tY                     }t%        |       t        jZ                  ||t        |            }t%        |       t        j\                  ||t        |      | j                         }n|dk(  r tK        tL        |      |      jO                         }t)        |      }t_        |      }t        j`                  tY               |t        |            }t#        |      rt5        d      t        jb                  | j                         }t#        |      rt%        d       t        jd                  |t        |      ||      }n<|dk(  r6 tK        tL        |      |      jO                         }t)        |      }t_        |      }t        jf                  tY               |t        |            }t#        |      rt5        d      t        jh                  | j                         }t#        |      rt%        d       t        jj                  |t        |      ||      }nftm        t        d      }t        jn                  |||tY               | j                         }t%        |       tq        |      }|rt        jr                  |t,        jt                  dt,        jv                  t,        jx                  tY                     }t%        |       t8        dk  rht        jr                  |t,        jt                  t,        jz                  t,        j|                  z  t,        j~                  dtY                     }t%        |       t        jZ                  ||t        |            }t%        |       t        j                  ||t        |            }dk  rt5        d      t%        |       |r4t8        dk  rt        j                  |       nt        j                  |       |	rt        j6                  |	       |rt        j                  |       |rt        j                  |       |rt        j                  |       |rt        j                  |       yy# |	rt        j6                  |	       w w xY w# |r4t8        dk  rt        j                  |       nt        j                  |       |	rt        j6                  |	       |rt        j                  |       |rt        j                  |       |rt        j                  |       |rt        j                  |       w w xY w)aI  
    Verifies an RSA, DSA or ECDSA signature

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :param rsa_pss_padding:
        If the certificate_or_public_key is an RSA key, this enables PSS padding

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    r   zA
            signature must be a byte string, not %s
            r  rR   rQ   md5sha1sha224sha256sha384sha512raw5"md5", "sha1", "sha224", "sha256", "sha384", "sha512", "raw"B
            hash_algorithm must be one of %s, not %s
            o
            PSS padding can only be used with RSA keys - the key provided is a
            %s key
               
                data must be 11 bytes shorter than the key size when
                hash_algorithm is "raw" - key size is %s bytes, but data is
                %s bytes long
                Nr   zSignature is invalidr!   r!   r   r   r   EVP_PKEY_CTX **rP   r   r!   )Gr   r   r9   r   r   r)   r+   rS   r   r   r   r  rY   	byte_sizer"   r  rE   r   r%   r   r   RSA_public_decryptr#   r   r   r-   r(   r   r$   EVP_MD_CTX_createEVP_MD_CTX_newEVP_md5EVP_sha1
EVP_sha224
EVP_sha256
EVP_sha384
EVP_sha512getattrhashlibdigestRSA_NO_PADDINGRSA_verify_PKCS1_PSSEVP_MD_CTX_FLAG_PSS_MDLENEVP_DigestInit_exr   EVP_DigestUpdateEVP_VerifyFinalr   d2i_DSA_SIGEVP_PKEY_get1_DSADSA_do_verifyd2i_ECDSA_SIGEVP_PKEY_get1_EC_KEYECDSA_do_verifyr   EVP_DigestVerifyInitr   EVP_PKEY_CTX_ctrlEVP_PKEY_RSAEVP_PKEY_CTRL_RSA_PADDINGRSA_PKCS1_PSS_PADDINGEVP_PKEY_OP_SIGNEVP_PKEY_OP_VERIFYEVP_PKEY_CTRL_RSA_PSS_SALTLENEVP_DigestVerifyFinalEVP_MD_CTX_destroyEVP_MD_CTX_freer   DSA_SIG_freer   ECDSA_SIG_free)r   r  r   r  r  r  	cp_is_rsavalid_hash_algorithmsvalid_hash_algorithms_errorrR   r[   decrypted_bufferdecrypted_lengthdecrypted_bytes
evp_md_ctxr   dsa_sigr   	ecdsa_sigevp_mdr5  decoded_bufferdecoded_lengthr  signature_buffersignature_pointerevp_pkey_ctx_pointer_pointerevp_pkey_ctx_pointers                               rJ   r  r    s   4 /+y1IJ /0
  	 i* i 	
  	 dH% dO	
  	 '00F%96\#9I WXeW-22&]#_'94' ( 
  	  LLN
  	 ^u,t90::R??^
 *33D	   	(--.G.P.PQCs|$Q',-F-O-OPK0=(;;I 00  !!12/0@BRSO#D/:$%;<< ""3'  J
C
CGFIR0!F*"446J"113J$$&&********
   "D(_9.9$?FFH112K2T2TU3<(+01J1S1ST!2;!?!*!=!=	N""11" %^444""<< 11*fdfM$S)00T3t9M$S)//	N-66	 59.9$?FFH#4Y#? $23C$D!#//8I3y>Z7#()?@@112K2T2TU3<(+--fc&k7CP49.9$?FFH#4Y#? $23C$D!%33DF<MsS\~^	9%()?@@"778Q8Z8Z[6?(+//FYPVW ,/y:K+L(00,)22C !%#)*F#G 11("//"<<"88F %S) *F2#55,&33&77.:[:[[&DDC )-,,Zs4yIC %11*iYXC7 !788S! %.,,Z8))*5s#s#""7+!!&)$$Y/ w ""3' \ %.,,Z8))*5s#s#""7+!!&)$$Y/ s   .B3`% T2a %`?B,c.c                     | j                   dk7  r-t        t        d| j                   j                                     t	        | ||      S )a^  
    Generates an RSASSA-PKCS-v1.5 signature.

    When the hash_algorithm is "raw", the operation is identical to RSA
    private key encryption. That is: the data is not hashed and no ASN.1
    structure with an algorithm identifier of the hash algorithm is placed in
    the encrypted byte string.

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384",
        "sha512" or "raw"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    rR   M
            The key specified is not an RSA private key, but %s
            rS   r   r   r  _signr   r   r  s      rJ   r>   r>     sP    8 % !!'')	
  	 dN33rM   c                     | j                   }|dk7  r(|dk7  r#t        t        d|j                                     t	        | ||d      S )a.  
    Generates an RSASSA-PSS signature. For the PSS padding the mask gen
    algorithm will be mgf1 using the same hash algorithm as the signature. The
    salt length with be the length of the hash algorithm, and the trailer field
    with be the standard 0xBC byte.

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    rR   rQ   r`  Tr  ra  )r   r   r  pkey_algs       rJ   r@   r@   	  sV    2 $$H5X5 NN	
  	 dNDIIrM   c                     | j                   dk7  r-t        t        d| j                   j                                     t	        | ||      S )aA  
    Generates a DSA signature

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    r   zL
            The key specified is not a DSA private key, but %s
            ra  rc  s      rJ   r.   r.   /  sP    , % !!'')	
  	 dN33rM   c                     | j                   dk7  r-t        t        d| j                   j                                     t	        | ||      S )aD  
    Generates an ECDSA signature

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    r   zL
            The key specified is not an EC private key, but %s
            ra  rc  s      rJ   r0   r0   P  sP    , $ !!'')	
  	 dN33rM   c           	         t        | t              st        t        dt	        |                   t        |t
              st        t        dt	        |                  | j                  }|dk(  xs |dk(  }t        g d      }|dk(  r|s|t        dg      z  }||vr*d}|r|s|dz  }t        t        d	|t        |                  |s%|r#t        t        d
|j                                     |r |dk(  rt        |      | j                  dz
  kD  r)t        t        d| j                  t        |                  d}	 t        j                  | j                        }t!        |      rt#        d       t%        | j                        }	t'        |	      }
t        j(                  t        |      ||
|t*        j,                        }t#        |       t/        |
|      |rt        j0                  |       S S d}d}d}d}d}d}	 t2        dk  rt        j4                         }nt        j6                         }	 t        j8                  t        j:                  t        j<                  t        j>                  t        j@                  t        jB                  d|          }t2        dk  r|r|r tE        tF        |      |      jI                         }t        j                  | j                        }t!        |      rt#        d       t%        | j                        }	t'        |	      }t        jJ                  ||||t*        jL                        }t#        |       t'        |	      }
t        j(                  |	||
|t*        jN                        }t#        |       n<|rt%        | j                        }	t'        |	      }
tQ        t        d      }t        jR                  ||tU                     }t#        |       t        jV                  ||t        |            }t#        |       t        jX                  ||
|| j                        }t#        |       t[        |      }n{|dk(  r tE        tF        |      |      jI                         }t        j\                  | j                        }t!        |      rt#        d       t        j^                  |t        |      |      }t!        |      rt#        d       t        j`                  |tU                     }	t'        |	      }
tc        |
      }t        j`                  ||      }t#        |       n|dk(  r tE        tF        |      |      jI                         }t        jd                  | j                        }t!        |      rt#        d       t        jf                  |t        |      |      }t!        |      rt#        d       t        jh                  |tU                     }	t'        |	      }
tc        |
      }t        jh                  ||      }t#        |       nt%        | j                        }	t'        |	      }
tQ        t        d|	      }tQ        t        d      }t        jj                  |||tU               | j                        }t#        |       tm        |      }|rt        jn                  |t*        jp                  dt*        jr                  t*        jt                  tU                     }t#        |       t2        dk  rht        jn                  |t*        jp                  t*        jv                  t*        jx                  z  t*        jz                  dtU                     }t#        |       t        jV                  ||t        |            }t#        |       t        j|                  ||
|      }t#        |       t[        |      }t/        
      |r4t2        dk  rt        j~                  |       nt        j                  |       |rt        j0                  |       |rt        j                  |       |rt        j                  |       |rt        j                  |       |rt        j                  |       S S # |rt        j0                  |       w w xY w# |r4t2        dk  rt        j~                  |       nt        j                  |       |rt        j0                  |       |rt        j                  |       |rt        j                  |       |rt        j                  |       |rt        j                  |       w w xY w)a  
    Generates an RSA, DSA or ECDSA signature

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :param rsa_pss_padding:
        If the private_key is an RSA key, this enables PSS padding

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    zO
            private_key must be an instance of PrivateKey, not %s
            r  rR   rQ   r  r  r  r   r!  r"  r#  r$  Nr   r%  r   zunsigned int *r   r   zsize_t *r&  r'  r(  )Er   r8   r   r   r)   r+   rS   r   r   r   r  rY   r)  r"   r  rE   r   r%   r   r   RSA_private_encryptr#   r   r   r   r$   r+  r,  r-  r.  r/  r0  r1  r2  r3  r4  r5  RSA_padding_add_PKCS1_PSSr8  r6  r   r9  r   r:  EVP_SignFinalr   r=  DSA_do_signi2d_DSA_SIGr   r@  ECDSA_do_signi2d_ECDSA_SIGEVP_DigestSignInitr   rC  rD  rE  rF  rG  rH  rI  EVP_DigestSignFinalrK  rL  r   rM  r   rN  )r   r   r  r  re  pkey_is_rsarP  rQ  rR   r[   r[  signature_lengthrU  r   rV  r   rW  rX  r5  	em_bufferr  r\  r]  r^  s                           rJ   rb  rb  q  s   2 k:. k"	
  	 dH% dO	
  	 $$He#?x<'?K WX5eW-22&]#'94' ( 
  	 ? NN
  	 ~.t9{,,r11^
 %%D	   	(--k.B.BCCs|$Q',[-A-ABK0=(<<D	 00  !!12$%57GH ""3'  J
C
CGFId0!F*"446J"113J$$&&********
   "D(9.9$?FFH11+2F2FG3<(+01E1EF-k:	99"<< %S)#4[#A #,#@#@$"11$  %%5601E1EF#4[#A #&y2B#C 11*fdfM$S)00T3t9M$S)--$$((	 %S)#()9#: U"9.9$?FFH11+2F2FG3<(+#//FSI7#(+'33GTVD#4[#A $23C$D!#,#8#8BS#T $%56T!9.9$?FFH"778L8LM6?(+%33FCKP	9%(+'55iH#4[#A $23C$D!#,#:#:9FW#X $%56 -[-A-ABK0="9j+F+.y:K+L(..,$$C !%#)*F#G 11("//"<<"88F %S) *F2#55,&33&77.:[:[[&DDC )-,,Zs4yIC %//
<LN^_C %$%56 !13CD %.,,Z8))*5s#s#""7+!!&)$$Y/ [ ""3' @ %.,,Z8))*5s#s#""7+!!&)$$Y/ s   <Ba> =Wb >bB,e)NNrh   )F)Q
__future__r   r   r   r   r4  _asn1r   r   r	   r
   r   r   r   _asymmetricr   r   r   r   r   r   r   r   r   _errorsr   _ffir   r   r   r   r   r   r   r   r    
_libcryptor"   r#   r$   r%   errorsr&   r'   r(   _typesr)   r*   r+   r,   utilr-   __all__r8   r9   r2   r   r3   r   r5   r6   r   r7   r4   r=   r<   r;   r:   r   r   r   r?   rA   r/   r1   r  r>   r@   r.   r0   rb  rr   rM   rJ   <module>r     s'   R R  
 
 
 %
 
 
 ` _ U U < < #2Q! Q!h! !>i" iXKT\@"F$#N.(5%pC+L0:;2:$zW0O,\0T.1 5$p4$n%OP$eNODODP0f$4N#JL4B4BU0rM   