
    Bwg                       d dl mZmZmZmZ d dlZd dlZd dlZd dlZddl	m
ZmZmZmZmZmZ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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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 dd	l9m:Z: dd
l;m<Z<m=Z=m>Z> ddl?m@Z@mAZAmBZBmCZC ddlDmEZEmFZFmGZGmHZHmIZImJZJ ddlKmLZL  ej                         ZNeNd    eNd   fZO e8       ZPePdk(  rddlQmRZRmSZSmTZTmUZUmVZV ddlWmXZYmZZ[m\Z\m]Z^m_Z` nddlambZbmcZcmTZTmdZdmeZe g dZfg dZg G d d      Zh G d dehe      Zi G d dehe      Zj G d dehe      Z
dJdZkd ZldKdZmd  ZndJd!Zod" Zpd# Zqd$ Zrd% Zsd& Ztd' Zud( Zvd) Zwd* Zxd+ ZydLd,Zzd- Z{dKd.Z|d/ Z}dKd0Z~dKd1Zd2 Zd3 Zd4 Zd5 Z_dMd6ZdMd7ZdMd8Zd9 Zd: Zd; Zd< Z]dMd=ZdMd>ZdMd?ZdMd@ZdMdAZdMdBZdMdCZdMdDZdMdEZdF ZdG ZdH ZdI Zy)N    )unicode_literalsdivisionabsolute_importprint_functionN   )CertificateDHParameters	DSAParamsDSASignatureECDomainParametersECPrivateKeyIntegerint_from_bytesint_to_bytesPrivateKeyAlgorithmPrivateKeyInfoPublicKeyAlgorithmPublicKeyInfoRSAPrivateKeyRSAPublicKey)	_CertificateBase_fingerprint_parse_pkcs12_PrivateKeyBase_PublicKeyBase_unwrap_private_key_infoparse_certificateparse_privateparse_public)pretty_message)buffer_from_bytesbuffer_from_unicode
byte_arraybytes_from_buffercastderefnativenewnullpointer_setsizeofstructstruct_bytesstruct_from_bufferunwrapwrite_to_buffer)backend)
fill_width)AsymmetricKeyErrorIncompleteAsymmetricKeyErrorSignatureError)	type_namestr_clsbyte_cls	int_types)add_pkcs1v15_signature_paddingadd_pss_paddingraw_rsa_private_cryptraw_rsa_public_crypt!remove_pkcs1v15_signature_paddingverify_pss_padding)constant_compare   	winlegacy)advapi32Advapi32Consthandle_erroropen_context_handleclose_context_handle)ec_generate_pairec_compute_public_key_pointec_public_key_info
ecdsa_signecdsa_verify)bcryptBcryptConstrE   open_alg_handleclose_alg_handle)r   dsa_sign
dsa_verifyrK   rL   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_verify(   r                                 %   )   +   /   5   ;   =   C   G   I   O   S   Y   a   e   g   k   m   q                                                                           i  i  i  i  i  i  i  i%  i3  i7  i9  i=  iK  iQ  i[  i]  ia  ig  io  iu  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  	  i  i  i#  i-  i3  i9  i;  iA  iK  iQ  iW  iY  i_  ie  ii  ik  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i)  i+  i5  i7  i;  i=  iG  iU  iY  i[  i_  im  iq  is  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i  i%  i'  i-  i?  iC  iE  iI  iO  iU  i]  ic  ii  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i  i  i  i'  i)  i/  iQ  iW  i]  ie  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i#  i+  i/  i=  iA  iG  iI  iM  iS  iU  i[  ie  iy  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i'  i7  iE  iK  iO  iQ  iU  iW  ia  im  is  iy  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i!  i#  i'  i)  i3  i?  iA  iQ  iS  iY  i]  i_  ii  iq  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i	  i	  i#	  i%	  i+	  i/	  i5	  iC	  iI	  iM	  iO	  iU	  iY	  i_	  ik	  iq	  iw	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i	  i
  i
  i!
  i1
  i9
  i=
  iI
  iW
  ia
  ic
  ig
  io
  iu
  i{
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i
  i  i  i  i  i#  i)  i-  i?  iG  iQ  iW  i]  ie  io  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i%  i/  i1  iA  i[  i_  ia  im  is  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i!  i+  i-  i=  i?  iO  iU  ii  iy  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i!  i'  i/  i5  i;  iK  iW  iY  i]  ik  iq  iu  i}  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i%  i)  i1  iC  iG  iM  iO  iS  iY  i[  ig  ik  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i!  i%  i+  i9  i=  i?  iQ  ii  is  iy  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i'  i-  i9  iE  iG  iY  i_  ic  ii  io  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i#  i)  i+  i1  i7  iA  iG  iS  i_  iq  is  iy  i}  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i'  i-  i7  iC  iE  iI  iO  iW  i]  ig  ii  im  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i!  i/  i3  i;  iE  iM  iY  ik  io  iq  iu  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i%  i)  i+  i7  i=  iA  iC  iI  i_  ie  ig  ik  i}  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i  i  i  i%  i3  i9  i=  iE  iO  iU  ii  im  io  iu  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i#  i'  i3  iA  i]  ic  iw  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i5  i7  i;  iC  iI  iM  iU  ig  iq  iw  i}  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i1  i3  iE  iI  iQ  i[  iy  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i!  i#  i-  i/  i5  i?  iM  iQ  ii  ik  i{  i}  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i#  i%  i/  i1  i7  i;  iA  iG  iO  iU  iY  ie  ik  is  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i'  i+  i-  i3  i=  iE  iK  iO  iU  is  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i!  i#  i5  i9  i?  iA  iK  iS  i]  ic  ii  iq  iu  i{  i}  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i%  i+  i/  i=  iI  iM  iO  im  iq  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i9  iI  iK  iQ  ig  iu  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i   i   i   i'   i)   i-   i3   iG   iM   iQ   i_   ic   ie   ii   iw   i}   i   i   i   i   i   i   i   i   i   i   i   i   i   i!  i!  i5!  iA!  iI!  iO!  iY!  i[!  i_!  is!  i}!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i!  i"  i	"  i"  i"  i!"  i%"  i+"  i1"  i9"  iK"  iO"  ic"  ig"  is"  iu"  i"  i"  i"  i"  i"  i"  i"  i"  i"  i"  i"  i"  i"  i"  i#  i	#  i#  i'#  i)#  i/#  i3#  i5#  iE#  iQ#  iS#  iY#  ic#  ik#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i#  i$  i$  i$  i$  i)$  i=$  iA$  iC$  iM$  i_$  ig$  ik$  iy$  i}$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i$  i%  i%  i%  i%  i'%  i1%  i=%  iC%  iK%  iO%  is%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i%  i&  i&  i&  i&  i'&  i)&  i5&  i;&  i?&  iK&  iS&  iY&  ie&  ii&  io&  i{&  i&  i&  i&  i&  i&  i&  i&  i&  i&  i&  i&  i&  i&  i&  i'  i'  i5'  i7'  iM'  iS'  iU'  i_'  ik'  im'  is'  iw'  i'  i'  i'  i'  i'  i'  i'  i'  i'  i'  i'  i'  i'  i(  i(  i(  i(  i(  i(  i!(  i1(  i=(  i?(  iI(  iQ(  i[(  i](  ia(  ig(  iu(  i(  i(  i(  i(  i(  i(  i(  i(  i(  i(  i(  i(  i)  i)  i)  i!)  i#)  i?)  iG)  i])  ie)  ii)  io)  iu)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i)  i*  i*  i*  i%*  i/*  iO*  iU*  i_*  ie*  ik*  im*  is*  i*  i*  i*  i*  i*  i*  i*  i*  i*  i*  i*  i*  i*  i*  i+  i'+  i1+  i3+  i=+  i?+  iK+  iO+  iU+  ii+  im+  io+  i{+  i+  i+  i+  i+  i+  i+  i+  i+  i+  i+  i+  i+  i+  i	,  i,  i,  i#,  i/,  i5,  i9,  iA,  iW,  iY,  ii,  iw,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i,  i-  i-  i-  i;-  iC-  iI-  iM-  ia-  ie-  iq-  i-  i-  i-  i-  i-  i-  i-  i-  i-  i-  i.  i.  i.  i.  i.  i.  i%.  i-.  i3.  i7.  i9.  i?.  iW.  i[.  io.  iy.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i.  i/  i	/  i/  i/  i'/  i)/  iA/  iE/  iK/  iM/  iQ/  iW/  io/  iu/  i}/  i/  i/  i/  i/  i/  i/  i/  i/  i/  i/  i/  i/  i/  i/  i0  i0  i#0  i)0  i70  i;0  iU0  iY0  i[0  ig0  iq0  iy0  i}0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i0  i1  i	1  i1  i!1  i'1  i-1  i91  iC1  iE1  iK1  i]1  ia1  ig1  im1  is1  i1  i1  i1  i1  i1  i1  i1  i1  i1  i1  i1  i1  i1  i	2  i2  i2  i2  i)2  i52  iY2  i]2  ic2  ik2  io2  iu2  iw2  i{2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i2  i3  i%3  i+3  i/3  i53  iA3  iG3  i[3  i_3  ig3  ik3  is3  iy3  i3  i3  i3  i3  i3  i3  i3  i3  i3  i3  i3  i3  i4  i4  i4  i4  i4  i74  iE4  iU4  iW4  ic4  ii4  im4  i4  i4  i4  i4  i4  i4  i4  i4  i4  i4  i4  i4  i4  i	5  i5  i5  i-5  i35  i;5  iA5  iQ5  ie5  io5  iq5  iw5  i{5  i}5  i5  i5  i5  i5  i5  i5  i5  i5  i5  i5  i5  i5  i5  i5  i6  i6  i6  i#6  i16  i56  i76  i;6  iM6  iO6  iS6  iY6  ia6  ik6  im6  i6  i6  i6  i6  i6  i6  i6  i6  i6  i6  i6  i7  i7  i7  i7  i?7  iE7  iI7  iO7  i]7  ia7  iu7  i7  i7  i7  i7  i7  i7  i7  i7  i7  i7  i7  i8  i8  i!8  i38  i58  iA8  iG8  iK8  iS8  iW8  i_8  ie8  io8  iq8  i}8  i8  i8  i8  i8  i8  i8  i8  i8  i8  i8  i8  i8  i8  i9  i9  i#9  i%9  i)9  i/9  i=9  iA9  iM9  i[9  ik9  iy9  i}9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i9  i:  i:  i:  i:  i':  i+:  i1:  iK:  iQ:  i[:  ic:  ig:  im:  iy:  i:  i:  i:  i:  i:  i:  i:  i:  i:  i:  i:  i;  i;  i;  i!;  i#;  i-;  i9;  iE;  iS;  iY;  i_;  iq;  i{;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i;  i<  i<  i<  i<  i<  i)<  i5<  iC<  iO<  iS<  i[<  ie<  ik<  iq<  i<  i<  i<  i<  i<  i<  i<  i<  i<  i<  i<  i<  i=  i=  i=  i=  i=  i!=  i-=  i3=  i7=  i?=  iC=  io=  is=  iu=  iy=  i{=  i=  i=  i=  i=  i=  i=  i=  i=  i=  i=  i=  i=  i>  i	>  i>  i>  i>  i#>  i)>  i/>  i3>  iA>  iW>  ic>  ie>  iw>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i>  i?  i?  i7?  i;?  i=?  iA?  iY?  i_?  ie?  ig?  iy?  i}?  i?  i?  i?  i?  i?  i?  i?  i?  i?  i?  i?  i@  i!@  i%@  i+@  i1@  i?@  iC@  iE@  i]@  ia@  ig@  im@  i@  i@  i@  i@  i@  i@  i@  i@  i@  i@  i@  i@  i	A  iA  iA  iA  i!A  i3A  i5A  i;A  i?A  iYA  ieA  ikA  iwA  i{A  iA  iA  iA  iA  iA  iA  iA  iA  iA  iA  iB  iB  iB  iB  i#B  i)B  i/B  iCB  iSB  iUB  i[B  iaB  isB  i}B  iB  iB  iB  iB  iB  iB  iB  iB  iB  iB  iB  iB  iB  iC  iC  iC  i%C  i'C  i3C  i7C  i9C  iOC  iWC  iiC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  iC  i	D  iD  iD  i#D  i)D  i;D  i?D  iED  iKD  iQD  iSD  iYD  ieD  ioD  iD  iD  iD  iD  iD  iD  iD  iD  iD  iD  iD  iD  iD  iE  iE  iE  i+E  i1E  iAE  iIE  iSE  iUE  iaE  iwE  i}E  iE  iE  iE  iE  iE  iE  iE  c                   (    e Zd ZdZdZdZdZd Zd Zy)_WinKeyNc                 `    || _         || _        t        dk(  rt        | _        yt
        | _        y)z
        :param key_handle:
            A CNG BCRYPT_KEY_HANDLE value (Vista and newer) or an HCRYPTKEY
            (XP and 2003) from loading/importing the key

        :param asn1:
            An asn1crypto object for the concrete type
        rB   N)
key_handleasn1_backendrC   _librM   selfr   r   s      O/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_win/asymmetric.py__init__z_WinKey.__init__  s(     %	{" DIDI    c                 X   | j                   rft        dk(  r&| j                  j                  | j                         }n%| j                  j	                  | j                         }t        |       d | _         | j                  r%t        dk(  rt        | j                         d | _        d | _        y )NrB   )r   r   r   CryptDestroyKeyBCryptDestroyKeyrE   context_handlerG   )r   ress     r   __del__z_WinKey.__del__  s}    ??;&ii//@ii00A"DO8{#: !4!45"&D	r   )	__name__
__module____qualname__r   r   ex_key_handler   r   r    r   r   r   r   v  s$     JNM D$r   r   c                   :    e Zd ZdZdZd Zed        Zed        Zy)rY   zM
    Container for the OS crypto library representation of a private key
    Nc                 2    t         j                  | ||       y)z
        :param key_handle:
            A CNG BCRYPT_KEY_HANDLE value (Vista and newer) or an HCRYPTKEY
            (XP and 2003) from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PrivateKeyInfo object
        Nr   r   r   s      r   r   zPrivateKey.__init__       	z40r   c                 `   t         dk(  r[| j                  dk(  rFt        | j                        }t	        dt        || j                              | _        | j                  S | j                  dk(  r| j                  d   d   }t        t        d|d      t        t        |d   j                  | j                  d	   j                  j                  |d
   j                              d      }t        |      | _        | j                  S | j                  d	   j                  }t        t        ddi      t        |d   |d   d      d      }t        |      | _        | j                  S t!        | j                  | j"                  | j$                        \  }}t        |      | _        | j                  S )z\
        :return:
            A PublicKey object corresponding to this private key.
        rB   ecNdsaprivate_key_algorithm
parameters	algorithmr   gprivate_keypr   
public_keyr   rsamoduluspublic_exponentr   r   )r   r   (_pure_python_ec_compute_public_key_pointr   rZ   rJ   curve_public_keyr   r   r   powr'   parsedrW   r   _bcrypt_key_handle_to_asn1bit_sizer   )r   	pub_pointparamspub_asn1r   _s         r   r   zPrivateKey.public_key  s    {"~~%DTYYO	#,T3EiQUQ[Q[3\#] J I 5(
 #:;LI(!3%*&,5 " #*#s**		-077>>s**+ #
* 
 $38#< &  =188(!3#U5 " #/#))#4+12C+D0 #	*  $38#<   5T^^T]]TXTcTcdKHa.x8Dr   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   r   rV   r   s    r   fingerprintzPrivateKey.fingerprint  s0     $ ,TYY8H ID   r   )	r   r   r   __doc__r   r   propertyr   r   r   r   r   rY   rY     s<     K
1 .  . ` ! !r   rY   c                       e Zd ZdZd Zy)rZ   zL
    Container for the OS crypto library representation of a public key
    c                 2    t         j                  | ||       y)z
        :param key_handle:
            A CNG BCRYPT_KEY_HANDLE value (Vista and newer) or an HCRYPTKEY
            (XP and 2003) from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PublicKeyInfo object
        Nr   r   s      r   r   zPublicKey.__init__  r   r   N)r   r   r   r   r   r   r   r   rZ   rZ     s    
1r   rZ   c                   >    e Zd ZdZdZdZd Zed        Zed        Z	y)r   zM
    Container for the OS crypto library representation of a certificate
    Nc                 2    t         j                  | ||       y)z
        :param key_handle:
            A CNG BCRYPT_KEY_HANDLE value (Vista and newer) or an HCRYPTKEY
            (XP and 2003) from loading/importing the certificate

        :param asn1:
            An asn1crypto.x509.Certificate object
        Nr   r   s      r   r   zCertificate.__init__  r   r   c                 r    | j                    t        | j                  d   d         | _         | j                   S )zh
        :return:
            The PublicKey object for the public key this certificate contains
        tbs_certificatesubject_public_key_info)r   rW   r   r   s    r   r   zCertificate.public_key  s9     #.tyy9J/KLe/fgDr   c                 J   | 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                  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_pkcs1v15
rsassa_pssr   ecdsaz
                        Unable to verify the signature of the certificate since
                        it uses the unsupported algorithm %s
                        signature_valuer   T)_self_signedr   self_signedsetsignature_algo	hash_algor`   rb   rR   rL   OSErrorr    r'   dumpr5   )r   r   r   verify_funcs       r   r   zCertificate.self_signed*  s0    $ %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")
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s@     KL
1     )! )!r   r   c                    | t        g d      vrt        t        dt        |                   | dk(  r-|t        g d      vrt        t        dt        |                  | dk(  rbt        dk  s	t
        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        |                  t
        dk(  r7| dk(  r&t        |      \  }}t        d|      t        d|      fS t        | |      S t        | ||      S )aB  
    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 on Windows 8 or newer.

    :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().
    )r   r   r   zM
            algorithm must be one of "rsa", "dsa", "ec", not %s
            r   )            zX
                bit_size must be one of 1024, 2048, 3072, 4096, not %s
                r      r   rB   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	secp521r1zd
                curve must be one of "secp256r1", "secp384r1", "secp521r1", not %s
                N)r   
ValueErrorr    repr_win_version_infor   _pure_python_ec_generate_pairrZ   rY   _advapi32_generate_pair_bcrypt_generate_pair)r   r   r   pub_info	priv_infos        r   rS   rS   W  ss   4 011 O	
  	 E3788^ X	   
e	v%[)@4  N	"   s#566  N	"   
d	CDD^ U	   ;"?"FHidH-z$	/JKK&y(;;$Y%@@r   c                    | dk(  rd}nd}t        t        d      }t        j                  |t               t        j
                  dt               |      }t        |       t        |      }t        |      }t        j                  |t               t        j
                  d||      }t        |       t        t        ||      }t        |      }	t        t        |	      }
t        ||      |
d }| dk(  rt        ||	|      \  }}||fS t        t        d      }t        j                  |t               t        j                  dt               |      }t        |       t        |      }t        |      }t        j                  |t               t        j                  d||      }t        |       t        ||      |
d }t        |||      \  }}||fS )ao  
    Accepts an key handle and exports it to ASN.1

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

    :param bit_size:
        An integer - only used when algorithm is "rsa"

    :param key_handle:
        The handle to export

    :return:
        A 2-element tuple of asn1crypto.keys.PrivateKeyInfo and
        asn1crypto.keys.PublicKeyInfo
    r   RSABLOBHEADERDSSBLOBHEADERDWORD *r   N)r(   rC   CryptExportKeyr)   rD   PRIVATEKEYBLOBrE   r&   r!   r.   r/   r+   r$    _advapi32_interpret_rsa_key_blobPUBLICKEYBLOB _advapi32_interpret_dsa_key_blob)r   r   r   struct_typeout_lenr   buffer_lengthbuffer_blob_struct_pointerblob_structstruct_sizeprivate_blobpublic_infoprivate_infopublic_out_lenpublic_buffer_lengthpublic_bufferpublic_blobs                     r   _advapi32_key_handle_to_asn1r    s   $ E%%(I&G

!
!$$	C 'NM.G

!
!$$	C ,X{GL,-K;/K$Wm<[\JLE$DX{\h$i!\@ &&7 Xy1%%F''F
 	S$^4)*>?%%F''
 	S'7KL[\Z$DX{\h$i!\&&r   c                 D   | dk(  r!t         j                  }t         j                  }n t         j                  }t         j                  }d}d}	 t        |d      }t        t        d      }|dz  t         j                  z  }t        j                  ||||      }t        |       t        |      }t        | ||      \  }	}
t        |	      t        |
      f|rt        |       |rt        j                   |       S S # |rt        |       |rt        j                   |       w w xY w)a  
    Generates a public/private key pair using CryptoAPI

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

    :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.

    :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().
    r   NFverify_onlyHCRYPTKEY *   )rD   MS_ENH_RSA_AES_PROVCALG_RSA_SIGNMS_ENH_DSS_DH_PROVCALG_DSS_SIGNrF   r(   rC   CRYPT_EXPORTABLECryptGenKeyrE   r/   r  rW   rV   rG   r   )r   r   provideralgorithm_idr   r   key_handle_pointerflagsr   r  r  s              r   r   r     s   * E 44$22 33$22NJ1,X5I =9R=#A#AA""><HZ[S./
$@HV`$a!\,.>|.LM  0$$Z0   0$$Z0 s   BC8 8'Dc           	      D   | dk(  r#d}t         j                  }t         j                  }nR| dk(  r+|dkD  rd}nd}t         j                  }t         j                  }n"d}t         j
                  }t         j                  }t        t        d      }t        j                  |t               |t               d	|d	      }t        |       t        |      }t        |      }	t        j                  |t               ||	||d	      }t        |       t        t        ||	      }
t        |
      }t!        t        |      }t#        |	|      |d
 }| dk(  rt%        d||      }n5| dk(  r#|dkD  rt'        dd||      }nt'        dd||      }nt)        d||      }t        t        d      }t        j                  |t               |t               d	|d	      }t        |       t        |      }t        |      }t        j                  |t               ||||d	      }t        |       t        t        ||      }t        |      }t!        t        |      }t#        ||      |d
 }| dk(  rt%        d||      }||fS | dk(  r)|dkD  rt'        dd||      }||fS t'        dd||      }||fS t)        d||      }||fS )au  
    Accepts an key handle and exports it to ASN.1

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

    :param bit_size:
        An integer - only used when algorithm is "dsa"

    :param key_handle:
        The handle to export

    :return:
        A 2-element tuple of asn1crypto.keys.PrivateKeyInfo and
        asn1crypto.keys.PublicKeyInfo
    r   BCRYPT_RSAKEY_BLOBr   r   BCRYPT_DSA_KEY_BLOB_V2BCRYPT_DSA_KEY_BLOBBCRYPT_ECCKEY_BLOBULONG *r   Nprivater   rA   public)rN   BCRYPT_RSAFULLPRIVATE_BLOBBCRYPT_RSAPUBLIC_BLOBBCRYPT_DSA_PRIVATE_BLOBBCRYPT_DSA_PUBLIC_BLOBBCRYPT_ECCPRIVATE_BLOBBCRYPT_ECCPUBLIC_BLOBr(   rM   BCryptExportKeyr)   rE   r&   r!   r.   r/   r+   r$   _bcrypt_interpret_rsa_key_blob_bcrypt_interpret_dsa_key_blob_bcrypt_interpret_ec_key_blob)r   r   r   r  private_blob_typepublic_blob_typeprivate_out_lenr   private_buffer_lengthprivate_bufferprivate_blob_struct_pointerprivate_blob_structr  r  r   r  r  r  public_blob_struct_pointerpublic_blob_structr  r   s                         r   r   r   3  s   $ E*'BB&<<	e	d?2K/K'??&== +'>>&<<&),O

 
 TV5FPQSbde
fC!/2&'<=N

 
 	C "4V[."Y !<=!45K$^5JKKLYLE4Y@SUab	e	d?8AGZ\hiK8AGZ\hiK3I?RT`a+N

 
 TV5EtvqR`bc
dC 0%&:;M

 
 	C !3FK!W :;!34K#M3GHVKE3H>PR]^
 $$ 
e	d?7!EWYdeJ $$	 8!EWYdeJ $$ 38=OQ\]
$$r   c                 b   | dk(  rt         j                  }nR| dk(  rt         j                  }n<t         j                  t         j                  t         j
                  d|   }dddd|   }d}	 t        |      }t        t        d      }t        j                  |||d	      }t        |       t        |      }t        j                  |d	      }t        |       t        | ||      \  }}	|rt        j                  |       	 t        |      t!        |	      fS # |rt        j                  |       w w xY w)
aL  
    Generates a public/private key pair using CNG

    :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 on Windows 8 or newer.

    :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().
    r   r   r      i  r   NBCRYPT_KEY_HANDLE *r   )rN   BCRYPT_RSA_ALGORITHMBCRYPT_DSA_ALGORITHMBCRYPT_ECDSA_P256_ALGORITHMBCRYPT_ECDSA_P384_ALGORITHMBCRYPT_ECDSA_P521_ALGORITHMrO   r(   rM   BCryptGenerateKeyPairrE   r/   BCryptFinalizeKeyPairr   r   rW   rV   )
r   r   r   alg_constantr   
alg_handler#  r   r   r   s
             r   r   r     s4   4 E"77	e	"77 %@@$@@$@@
 	 
 	 J0$\2
 )>?**:7I8UVWS./
**:q9S"<YR\"]
K ##J/J')9+)FGG ##J/ s   ,A:D D.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	}d
}	 | dz  }t        dk(  r$t        t        j                        }t        |      }	 t        dk(  rt        j                  |      }n.t        j                  ||d      }t        |       t!        |      }t#        |      }|d
z  dk(  rb|d
k(  r	|dz  dk7  rp|dk(  r|dz  }|dk7  r|dk7  rd}	t$        D ]  }
||
z  dk(  sd}	 n |	s9t'        | |      r-|d
z  }t'        | |      rt)        ||d      |rt+        |       S S # |rt+        |       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 64Nr      winTrB      rf   rd   
   rc   re   F)r   r   )
isinstancer9   	TypeErrorr    r6   r   r   rO   rN   BCRYPT_RNG_ALGORITHMr!   osurandomrM   BCryptGenRandomrE   r$   r   _SMALL_PRIMES	_is_primer	   rP   )r   rK  r   	byte_sizebufferrbr   r   rem	divisibleprimeqs               r   generate_dh_parametersra    s   0 h	* h	
  	 #~HII$FGG"}<==J 	
A-)M	u()I)IJJ&y1F;&ZZ	*,,ZANS!&v.r"A 1uz Avr6R<a"f!8qI& u9> $I 8Q!7FXq)'aa(89 Z( M L Z( s   'C
E3 21E3 2E3 3Fc                 t   d}|dz
  }|dz  dk(  r|dz  }|dz  }|dz  dk(  r| dk\  rd}n| dk\  rd}n| dk\  rd}n| d	k\  rd
}n| dk\  rd}t              D ]a  }t        j                  d|dz
        }t        |||      }|dk(  s||dz
  k(  r7t        |dz
        D ]  }t        |d|      }||dz
  k(  s `  y y)u   
    An implementation of Miller–Rabin for checking if a number is prime.

    :param bit_size:
        An integer of the number of bits in the prime number

    :param n:
        An integer, the prime number

    :return:
        A boolean
    r   rA   r   i  iR  rc   i     i&  rd   i  r   FT)rangerandom	randranger   )r   nrskr   axs           r   rY  rY  <  s    	
A	AA
a%1*	Q	a a%1* 4	S	S	S	S1X 
QA&1aL6Q!a%Zq1u 	AAq!AAEz	
 
 r   c                 F   | dz  }| dz  }|}||z   }||z   }||z   }||z   }	|	|z   }
|j                   j                  }t        |d| ddd         }t        ||| ddd         }t        ||| ddd         }t        ||| ddd         }t        |||	 ddd         }t        ||	|
 ddd         }t        ||
|
|z    ddd         }t        t	        ddi      t        ||d      d	      }t        d
||||||||d	      }t        dt        ddi      |d      }||fS )a  
    Takes a CryptoAPI RSA private key blob and converts it into the ASN.1
    structures for the public and private keys

    :param bit_size:
        The integer bit size of the key

    :param blob_struct:
        An instance of the advapi32.RSAPUBKEY struct

    :param blob:
        A byte string of the binary data after the header

    :return:
        A 2-element tuple of (asn1crypto.keys.PublicKeyInfo,
        asn1crypto.keys.PrivateKeyInfo)
    rN  r  r   Nr   r   r   r   	two-prime	versionr   r   private_exponentprime1prime2	exponent1	exponent2coefficientrq  r   r   )		rsapubkeypubexpr   r   r   r   r   r   r   )r   r  bloblen1len2prime1_offsetprime2_offsetexponent1_offsetexponent2_offsetcoefficient_offsetprivate_exponent_offsetr   r   rs  rt  ru  rv  rw  rr  public_key_inforsa_private_keyprivate_key_infos                         r   r  r  j  s   & q=Dr>DM!D(M$t+'$.)D0047!++22OT!M24R489GD}=ddCDFD/?@2FGFt$45EFttLMIt$45GH2NOI &89P!QRVTVRV!WXK%d+BCZ]aCa&bcgegcg&hi#')
  #.$
 	% O $*,"
% 
O &!46
 " ''  -..r   c                    d}| dz  }|}||z   }||z   }|}t        |d| ddd         }	t        ||| ddd         }
t        ||| ddd         }t        ||||z    ddd         }t        ||||z    ddd         }t        t        dt        |	|
|d      d      t	        |      d	      }t        dt        dt        |	|
|d      d      t	        |      d
      }||fS )a  
    Takes a CryptoAPI DSS private key blob and converts it into the ASN.1
    structures for the public and private keys

    :param bit_size:
        The integer bit size of the key

    :param public_blob:
        A byte string of the binary data after the public key header

    :param private_blob:
        A byte string of the binary data after the private key header

    :return:
        A 2-element tuple of (asn1crypto.keys.PublicKeyInfo,
        asn1crypto.keys.PrivateKeyInfo)
       rN  r   Nrn  r   r   r`  r   r   r   rx  )r   r   r   r
   r   r   r   )r   r  r  r|  r}  q_offsetg_offsetx_offsety_offsetr   r`  r   rl  yr  r  s                   r   r  r    sH   & Dq=DH$H$HH|Ah/"56A|HX6tt<=A|HX6tt<=A|HX_=ddCDA{8HtO<TrTBCA#'#% )
  aj
% 
O &!4#% 6
 " qz'  -..r   c                    t        t        |j                        }t        t        |j                        }|}t	        |d|       }t	        ||||z          }| dk(  r%t        t        ddi      t        ||d      d      S | dk(  rt        t        |j                        }t        t        |j                        }	||z   }
|
|z   }||	z   }||	z   }||	z   }||z   }t	        ||
|       }t	        |||       }t	        |||       }t	        |||       }t	        |||       }t	        ||||z          }t        d||||||||d		      }t        dt        ddi      |d
      S t        t        dt        |                   )a  
    Take a CNG BCRYPT_RSAFULLPRIVATE_BLOB and converts it into an ASN.1
    structure

    :param key_type:
        A unicode string of "private" or "public"

    :param blob_struct:
        An instance of BCRYPT_RSAKEY_BLOB

    :param blob:
        A byte string of the binary data contained after the struct

    :return:
        An asn1crypto.keys.PrivateKeyInfo or asn1crypto.keys.PublicKeyInfo
        object, based on the key_type param
    r   r,  r   r   r   r   r+  ro  rp  rx  M
            key_type must be one of "public", "private", not %s
            )r'   intcbPublicExp	cbModulusr   r   r   r   cbPrime1cbPrime2r   r   r   r   r    r   )key_typer  r{  public_exponent_byte_lengthmodulus_byte_lengthmodulus_offsetr   r   prime1_byte_lengthprime2_byte_lengthr~  r  r  r  r  r  rs  rt  ru  rv  rw  rr  r  s                          r   r4  r4    s    & #)k.E.E"F k&;&;<0N$T!N%;<OT.BU1UVWG8+U-  '"#2( 	
  	 
Y	#C)=)=>#C)=)=>&)<<%(::(+==+.@@-0BB"47I"I]= AB]3C DE"4(89I#JK	"4(89K#LM	$T*<=T%UV)$/FG^atGt*uv'". 0""&
)
 
 %8U: & +
  	  N	
  	r   c                 J   t        t        |j                        }|dk(  rLt        t        t        |j
                              }|}||z   }||z   }t        |d|       }	t        |||       }
n|dk(  rut        t        |j                        }t        t        |j                        }|}||z   }||z   }||z   }||z   }t        |||       }t        |||       }	t        |||       }
nt        dt        |      z        | dk(  r>t        |||       }t        t        dt        |	||
d      d      t        |      d	      S | d
k(  rBt        ||||z          }t        dt        dt        |	||
d      d      t        |      d      S t        t!        dt        |                   )an  
    Take a CNG BCRYPT_DSA_KEY_BLOB or BCRYPT_DSA_KEY_BLOB_V2 and converts it
    into an ASN.1 structure

    :param key_type:
        A unicode string of "private" or "public"

    :param version:
        An integer - 1 or 2, indicating the blob is BCRYPT_DSA_KEY_BLOB or
        BCRYPT_DSA_KEY_BLOB_V2

    :param blob_struct:
        An instance of BCRYPT_DSA_KEY_BLOB or BCRYPT_DSA_KEY_BLOB_V2

    :param blob:
        A byte string of the binary data contained after the struct

    :return:
        An asn1crypto.keys.PrivateKeyInfo or asn1crypto.keys.PublicKeyInfo
        object, based on the key_type param
    rA   r   r   zversion must be 1 or 2, not %sr,  r   r  r   r   r+  rx  r  )r'   r  cbKeyr   r8   r`  cbSeedLengthcbGroupSizer   r   r   r   r
   r   r   r   r    )r  rq  r  r{  key_byte_lengthr`  r  public_offsetprivate_offsetr   r   seed_byte_lengthgroup_byte_lengthr  p_offsetr,  r+  s                    r   r5  r5  C  s   . S+"3"34O!|6(KMM:;" ?2&84(+,478	A!#{'?'?@"3(?(?@#//o- ?2&8 423423478 9DMIJJ8]> BC+"') -  "&/

 
 
	 
Y	 n^o5U!VW%8"') : & #7+
  	  N	
  	r   c                 r   t        t        |j                        }t        t        |j                        }t        j
                  dt        j                  dt        j                  dt        j                  dt        j                  dt        j                  di|   }d|d|dz   z   }| dk(  r%t        t        dt        d	|
      d      |d      S | dk(  rGt        ||dz  |dz         }t        dt!        dt        d	|
      d      t#        d||d      d      S t%        t'        dt)        |                   )a  
    Take a CNG BCRYPT_ECCKEY_BLOB and converts it into an ASN.1 structure

    :param key_type:
        A unicode string of "private" or "public"

    :param blob_struct:
        An instance of BCRYPT_ECCKEY_BLOB

    :param blob:
        A byte string of the binary data contained after the struct

    :return:
        An asn1crypto.keys.PrivateKeyInfo or asn1crypto.keys.PublicKeyInfo
        object, based on the key_type param
    r   r   r      r   r   r,  r   named)namevaluer   r   r+  rc   ecPrivkeyVer1)rq  r   r   rx  r  )r'   r  dwMagicr  rN   BCRYPT_ECDSA_PRIVATE_P256_MAGICBCRYPT_ECDSA_PRIVATE_P384_MAGICBCRYPT_ECDSA_PRIVATE_P521_MAGICBCRYPT_ECDSA_PUBLIC_P256_MAGICBCRYPT_ECDSA_PUBLIC_P384_MAGICBCRYPT_ECDSA_PUBLIC_P521_MAGICr   r   r   r   r   r   r   r   r    r   )r  r  r{  magicr  r   r,  r+  s           r   r6  r6    sf   $ 3++,ES+"3"34O 	33[33[33[22K22K22K E tAo122F8+!0 -  !	
 	 		 
Y	 o&9/A:M!NO%8!0 : & (*&$) 
  	"  N	
  	r   c                 R   t        | t              r| }nyt        | t              rt        |       }n]t        | t              r/t        | d      5 }t        |j                               }ddd       nt        t        dt        |                   t        t              S # 1 sw Y   xY w)a  
    Loads an x509 certificate into a Certificate object

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

    :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
    r\  Nz
            source must be a byte string, unicode string or
            asn1crypto.x509.Certificate object, not %s
            )rR  Asn1Certificater8   r   r7   openreadrS  r    r6   	_load_keyr   )sourcecertificatefs      r   rT   rT     s      &/*	FH	%'/	FG	$&$ 	61+AFFH5K	6 	6  f
  	 [+..	6 	6   BB&c                    | }t        | t              r| d   d   }|j                  }d}|dk(  rK|j                  \  }}|dk7  rt	        t        d            |t        g d      vrt	        t        d            |d	k(  r|j                  t        t        d
            |j                  dkD  rJt        dk  s	t        dk(  r8t	        t        d|j                  j                         |j                              |j                  dk(  r#|j                  dk(  rt	        t        d            t        dk(  r|dk(  r	 |d|       S t        | ||      S t        | |||      S )a  
    Loads a certificate, public key or private key into a Certificate,
    PublicKey or PrivateKey object

    :param key_object:
        An asn1crypto.x509.Certificate, asn1crypto.keys.PublicKeyInfo or
        asn1crypto.keys.PrivateKeyInfo object

    :param container:
        The class of the object to hold the key_handle

    :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 key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey, PublicKey or Certificate object, based on container
    r   r   Nr   r  zR
                Windows only supports EC keys using named curves
                r   z
                Windows only supports EC keys using the named curves
                secp256r1, secp384r1 and secp521r1
                r   z
                The DSA key does not contain the necessary p, q and g
                parameters and can not be used
                r   r   rB   z
                Windows XP, 2003, Vista, 7 and Server 2008 only support DSA
                keys based on SHA1 (1024 bits or less) - this key is based
                on %s and is %s bits
                r   sha1a  
                Windows only supports 2048 bit DSA keys based on SHA2 - this
                key is 2048 bits and based on SHA1, a non-standard
                combination that is usually generated by old versions
                of OpenSSL
                )rR  r  r   r   r3   r    r   r   r4   r   r   r   upper_advapi32_load_key_bcrypt_load_key)
key_object	containerkey_infoalgo
curve_name
curve_types         r   r  r    s   , H*o./01JKDJt|!)
J $^&  
 S!HII$^&   
%.~0   %+<v+EU`I`$^
 ""((*!!&   $&8+=+=+G$^&   ;4<T:..!*h	BBJ)ZHHr   c                    t        |t              rdnd}|j                  }|dk(  rd}|dk(  s|dk(  rt        j                  }nt        j
                  }d}d}	 t        ||dk(        }t        |||      }t        |      }	t        t        d      }
t        j                  ||	t        |      t               d|
      }t        |       t        |
      } |||       }||_        |dk(  rpt        |||d	
      }t        |      }t        t        d      }t        j                  ||t        |      t               d|      }t        |       t        |      |_        |S # t$        $ r& |rt        j&                  |       |rt)        |        w xY w)ah  
    Loads a certificate, public key or private key into a Certificate,
    PublicKey or PrivateKey object via CryptoAPI

    :param key_object:
        An asn1crypto.x509.Certificate, asn1crypto.keys.PublicKeyInfo or
        asn1crypto.keys.PrivateKeyInfo object

    :param key_info:
        An asn1crypto.keys.PublicKeyInfo or asn1crypto.keys.PrivateKeyInfo
        object

    :param container:
        The class of the object to hold the key_handle

    :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 key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey, PublicKey or Certificate object, based on container
    r,  r+  r   r   Nr  r  r   F)signing)rR  r   r   rD   r  r  rF   _advapi32_create_blobr!   r(   rC   CryptImportKeylenr)   rE   r/   r   r   	Exceptionr   rG   )r  r  r  r  r  r!  r   r   r{  r
  r#  r   outputex_blob	ex_bufferex_key_handle_pointers                   r   r  r  [  s   4 &h>xIHD|u}, 44 33NJ-,X8xCWX$Xx>#D) =9%%IF
 	S./
:z2 .5=+HheTG)'2I$'-$@!))G%C #)*?#@F  $$Z0 0s   C?E /Fc                    |dk(  rt         j                  }nt         j                  }|dk(  r&d}|rt         j                  }n#t         j                  }nd}t         j
                  }t        t        d      }t        |      }||_	        t         j                  |_        d|_        ||_        t        t        |      }	t        |	      }
||
_        | j                  }|dz  }|dz  }|dk(  rt        t        d	      }t        |      }||_        |dk(  r[| d
   j"                  }t         j$                  |_        |d   j(                  |_        t-        |d   j(                  d|      ddd   }n1| d   j"                  }t         j.                  |_        |d   j(                  |_        t-        |d   j(                  d|      ddd   }|t-        |d   j(                  d|      ddd   z  }|t-        |d   j(                  d|      ddd   z  }|t-        |d   j(                  d|      ddd   z  }|t-        |d   j(                  d|      ddd   z  }|t-        |d   j(                  d|      ddd   z  }|t-        |d   j(                  d|      ddd   z  }||
_        nSt        t        d      }t        |      }||_        |dk(  rSt         j2                  |_        | d   d   j(                  }t-        | d
   j"                  j(                  d|      ddd   }nRt         j4                  |_        | d   d   j(                  }t-        | d   j"                  j(                  dd      ddd   }||
_        t-        |d   d|      ddd   }|t-        |d   dd      ddd   z  }|t-        |d   d|      ddd   z  }||z  }t        t        d       }t        |      }d!|_        |t;        |      z  }t;        |	      |z   S )"a  
    Generates a blob for importing a key to CryptoAPI

    :param key_info:
        An asn1crypto.keys.PublicKeyInfo or asn1crypto.keys.PrivateKeyInfo
        object

    :param key_type:
        A unicode string of "public" or "private"

    :param algo:
        A unicode string of "rsa" or "dsa"

    :param signing:
        If the key handle is for signing - may only be False for rsa keys

    :return:
        A byte string of a blob to pass to advapi32.CryptImportKey()
    r,  r   r   r   
BLOBHEADERr   rN  r  	RSAPUBKEYr   r   r   F)signedwidthNrn  r   rs  rt  ru  rv  rw  rr  	DSSPUBKEYr   r   r   r  r   r`  r   DSSSEEDl    )rD   r  r  r  CALG_RSA_KEYXr  r,   rC   r/   bTypeCUR_BLOB_VERSIONbVersionreservedaiKeyAlgpublickeystrucr   bitlenr   RSA1r  r'   rz  r   RSA2ry  DSS1DSS2	dsspubkeycounterr-   )r  r  r  r  	blob_typer  r"  blob_header_pointerblob_headerr  r  r   r|  r}  pubkey_pointerpubkeyparsed_key_info	blob_datar   key_datadssseed_pointerdssseeds                         r   r  r    s)   * 8!//	!00	u}%(66L(66L%$22 <8,-K!K(99KK'K ;7,-K!,K  Hq=Dr>Du}+6' x&|4;;O(--FL+,=>EEFM$_Y%?%F%Fu\`abfdfbfgI&}5<<O(--FL+,=>EEFM$_Y%?%F%Fu\`abfdfbfgIoh&?&F&Fu\`abfdfbfggIoh&?&F&Fu\`abfdfbfggIok&B&I&IRW_cdeigieijjIok&B&I&IRW_cdeigieijjIom&D&K&KTYaefgkikgkllIo6H&I&P&PY^fjklpnplpqqI &  +6' x(--FLk*<8??F#H\$:$A$A$H$HQV^bcdhfhdhiH(--FL56|DKKF#H]$;$B$B$I$IRW_abcgegcghH & U$G"M	\&+e2FttLL	\&+e4H2NN	X	 95)$\/22	+,y88r   c           
         d}d}t        |t              rdnd}|j                  }|dk(  rd}	 |dk(  r|j                  d   n|}t        j
                  t        j                  t        j                  t        j                  t        j                  d|   }	t        |	      }|dk(  r|dk(  r4t        j                  }
t        j                  }|d	   j                  }d
}d
}nt        j                  }
t        j                  }|d   j                  }t!        |d   j"                        }t!        |d   j"                        }t!        |d   j"                        }t!        |d   j"                        }t!        |d   j"                        }t!        |d   j"                        }t%        |      }t%        |      }t!        |d   j"                        }t!        |d   j"                        }t'        t(        d      }t+        |      }||_        |j.                  |_        t%        |      |_        t%        |      |_        ||_        ||_        t;        |      |z   |z   }|dk(  r|z   z  }|t=        |      z  }|t=        |      z  }|t=        |      z  }|t=        t%        |            z  }n[|dk(  r|dk(  r2t        j>                  }
|d	   j                  j"                  }|d   d   }nRt        j@                  }
tC        |      d	   j"                  }t!        |d   j                  j"                        }|d   d   }t!        |      }t!        |d   j"                        }t!        |d   j"                        }t!        |d   j"                        } |j.                  dkD  rt%        |       }!nd}!tE        t%        |      t%        |      t%        |            }"t=        ||"      }t=        ||"      }t=        ||"      }t=        | |!      } d}#d|!z  }$|j.                  dkD  r|dk(  rt        jF                  }nt        jH                  }t'        t(        d       }t+        |      }||_%        |"|_&        t        jN                  |_(        t        jR                  |_*        |!|_+        |!|_,        t[        |#      |_.        t;        |      }||$| z   |z   |z   |z   z  }|dk(  r5|t=        |!      z  }n$|dk(  rt        j^                  }nt        j`                  }t'        t(        d!      }t+        |      }||_%        |"|_&        t[        |#      |_.        t[        |$      |_1        t[        |       |_2        t;        |      |z   |z   |z   }|dk(  r|t=        |!      z  }nz|dk(  rt|dk(  r't        jf                  }
|d	   ji                         \  }%}&nat        jj                  }
|d   j                  d	   }|r|ji                         \  }%}&nd
}%d
}&t!        |d   j                  d   j"                        }t'        t(        d"      }t+        |      }t        jl                  t        jn                  t        jp                  t        jr                  t        jt                  t        jv                  d#||f   }d$d%d&d'|   }"t!        |%      }'t!        |&      }(t=        |'|"      }'t=        |(|"      }(||_%        |"|_&        t;        |      |'z   |(z   }|dk(  r|t=        |"      z  }ty        t(        d(      })t)        jz                  |t}               
|)t%        |      t        j~                        }*t        |*       t+        |)      } |||       |rt        |       S S # |rt        |       w w xY w))a  
    Loads a certificate, public key or private key into a Certificate,
    PublicKey or PrivateKey object via CNG

    :param key_object:
        An asn1crypto.x509.Certificate, asn1crypto.keys.PublicKeyInfo or
        asn1crypto.keys.PrivateKeyInfo object

    :param key_info:
        An asn1crypto.keys.PublicKeyInfo or asn1crypto.keys.PrivateKeyInfo
        object

    :param container:
        The class of the object to hold the key_handle

    :param curve_name:
        None or a unicode string of the curve name for an EC key

    :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 key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey, PublicKey or Certificate object, based on container
    Nr,  r+  r   r   r   rA   )r   r   r   r   r   r   r   r   rs  rt  ru  rv  rw  rr  r   r   r&  r   r   r   r   r   r   r`  r   r  s      r'  r(  r)  ))r,  r   )r,  r   )r,  r   )r+  r   )r+  r   )r+  r       0   B   r   rB  )BrR  r   r   r   rN   rC  rD  rE  rF  rG  rO   r.  BCRYPT_RSAPUBLIC_MAGICr   r-  BCRYPT_RSAFULLPRIVATE_MAGICr   r'   r  r,   rM   r/   Magicr   	BitLengthr  r  r  r  r-   r2   r0  r/  r   maxBCRYPT_DSA_PUBLIC_MAGIC_V2BCRYPT_DSA_PRIVATE_MAGIC_V2r  r  DSA_HASH_ALGORITHM_SHA256hashAlgorithmDSA_FIPS186_3standardVersionr  r  r#   CountBCRYPT_DSA_PUBLIC_MAGICBCRYPT_DSA_PRIVATE_MAGICSeedr`  r2  	to_coordsr1  r  r  r  r  r  r  r(   BCryptImportKeyPairr)   BCRYPT_NO_KEY_VALIDATIONrE   rP   )+r  r  r  r  rK  r   r  r  alg_selectorrJ  r  r  
parsed_keyprime1_sizeprime2_sizers  rt  ru  rv  rw  rr  r   r   r  r  r{  r   r   private_bytespublic_bytesr   r   r`  q_len	key_widthcountseedrl  r  x_bytesy_bytesr#  r   s+                                              r   r  r    sB   : JJ%h>xIHD|@),0DLx~~a(d3333$@@$@@$@@
  %\2
5=8#'==	#::%l3::
'BB	#??%m4;;
%j&:&A&AB%j&:&A&AB(K)@)G)GH	(K)@)G)GH	*:m+D+K+KL#/
;M0N0U0U#V !&k!&k*:6G+H+O+OPO":i#8#?#?@G"(1E"F !45K %K$,$5$5K!&)/&:K#$'LK!#.K #.K  34FPD9$'
9k::
9k::
;<<
#3S\BBU]8#'>>	%l3::AA
!+.|<'??	5h?MTT
 ,Xm-D-K-K-R-R S!"9:<H'
3LVC[//0AVC[//0AVC[//0A  4'AC-s1vs1v>I%lI>L1i(A1i(A1e$A  EU?D  4'x''BBE'CCE&,V5M&N#$%89&+#$-! -8,Q,Q).9.G.G++0(*/'$.u$5!#$78q1q(<77y(J}e<<D x''??E'@@E&,V5J&K#$%89&+#$-!$.u$5!#-d#3  *1#$781<q@<Oy(J}e<<DT\8#'==	-7791'>>	%m4;;LI
 %//1DAqAA ,Xm-D-K-KM-Z-a-a b"(1E"F !45K *5)S)S)4)S)S)4)S)S*5*U*U*5*U*U*5*U*U $&E   	I #1oG"1oG )4G )4G"'K )K 34w>HD9$
=)<< )>?((FI00
 	S./
Z0 Z( :Z( s   ] ]< <^c                    t        | t              r| }n|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        |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
                )rR  r   r7   encoder8   rS  r    r6   r  r  r   r  rY   )r  passwordprivate_objectr  s       r   rV   rV     s    . &.) (G,#??73h1 h'	!   fg&fd# "q" " FH-N &!   'vx8^Z00" "s   C%%C.c                 R   t        | t              r| }nyt        | t              rt        |       }n]t        | t              r/t        | d      5 }t        |j                               }ddd       nt        t        dt                          t        t              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
            )rR  r   r8   r   r7   r  r  rS  r    r6   r  rZ   )r  r   r  s      r   rW   rW   9  s    $ &-(
	FH	%!&)
	FG	$&$ 	01%affh/J	0 	0  j!
  	 Z++	0 	0r  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   rV   )datar  s     r   rX   rX   a  s    , x)9::r   c                 j   |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        |t              }|rt        |j                  t              }|D cg c]  }t        |j                  t               }	}|||	fS # 1 sw Y   wx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
            )rR  r7   r  r8   rS  r    r6   r  r  rX   r  rY   r   r   )
r  r  r  r  	cert_infoextra_certs_infokeycertinfoextra_certss
             r   rU   rU   z  s/   , h(w/H(H-N (#	   &'"&$ 	1VVXF	 	 ) f	
  	 -9,J)Hi)
CD*---{;GWXt9T__k:XKX{##1	 	, Ys   .D$<!D0$D-c                 R    | j                   dk7  rt        d      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", "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
    r   *The key specified is not an RSA public keyr   r   _verifycertificate_or_public_key	signaturer  hash_algorithms       r   r`   r`     s0    8 !**e3EFF,i~NNr   c                 d    | j                   }|dk7  r|dk7  rt        d      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", "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   r   r$  Trsa_pss_paddingr%  )r(  r)  r  r*  cp_algs        r   rb   rb     s>    4 '00F6\1EFF,i~_cddr   c                 R    | j                   dk7  rt        d      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", "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   z)The key specified is not a DSA public keyr%  r'  s       r   rR   rR     s0    . !**e3DEE,i~NNr   c                 R    | j                   dk7  rt        d      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", "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   z)The key specified is not an EC public keyr%  r'  s       r   rL   rL   	  s0    . !**d2DEE,i~NNr   c           	      Z   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'|dur#t        t	        d|j                                     |dk(  rDt        |      | j                  dz
  kD  r)t        t	        d| j                  t        |                  t        dk(  r,| j                  dk(  rt        | |||      S t!        | ||||      S t#        | ||||      S )a(  
    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", "sha256", "sha384", "sha512" or "raw"

    :param rsa_pss_padding:
        If PSS padding should be used for RSA keys

    :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
    
            certificate_or_public_key must be an instance of the Certificate or
            PublicKey class, not %s
            zA
            signature must be a byte string, not %s
            <
            data must be a byte string, not %s
            r   r   md5r  sha256sha384sha512raw+"md5", "sha1", "sha256", "sha384", "sha512", "raw"B
            hash_algorithm must be one of %s, not %s
            Fu
            PSS padding may only be used with RSA keys - signing via a %s key
            was requested
            rf   z
                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
                rB   r   )rR  r   rZ   rS  r    r6   r8   r   r   r   r   r  r  rZ  r   _pure_python_ecdsa_verify_advapi32_verify_bcrypt_verify)	r(  r)  r  r*  r-  r.  	cp_is_rsavalid_hash_algorithmsvalid_hash_algorithms_errors	            r   r&  r&  5	  s   4 /+y1IJ /0
  	 i* i 	
  	 dH% dO	
  	 '00F%96\#9I MNeW-22&S#_'94' ( 
  	 5 LLN
  	 t90::R??^
 *33D	   ;$..$6,-F	SWYghh 99dN\kll3YnVeffr   c                    | j                   }|dk(  xs |dk(  }|rM|rKddddddj                  |d	      }t        | |      }| j                  }	t	        |||	||      st        d
      y|r@|dk(  r;t        | |      }
	 t        | j                  |
      }t        ||      s
t               	 yd}	 t        j                  t        j                  t        j                  t        j                  t        j                  d|   }t!        t"        d      }t#        j$                  | j&                  |t)               d	|      }t+        |       t-        |      }t#        j.                  ||t1        |      d	      }t+        |       |dk(  r=	 t3        j4                  |      j7                         }t1        |      dz  }||d |d| z   }|ddd   }t#        j<                  ||t1        |      | j>                  t)               d	      }t+        |       |rt#        j@                  |       yy# t        $ r t        d
      w xY w# t        t8        t:        f$ r t        d
      w xY w# |rt#        j@                  |       w w xY w)a6  
    Verifies an RSA, DSA or ECDSA signature via CryptoAPI

    :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", "sha256", "sha384", "sha512" or "raw"

    :param rsa_pss_padding:
        If PSS padding should be used for RSA keys

    :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   r   r     r  r  rM  r  sha224r6  r7  r8  r   Signature is invalidNr9  r4  HCRYPTHASH *r   r   rn  )!r   getr=   r   r?   r5   r>   rZ  r@   r   rD   CALG_MD5	CALG_SHA1CALG_SHA_256CALG_SHA_384CALG_SHA_512r(   rC   CryptCreateHashr   r)   rE   r/   CryptHashDatar  r   loadto_p1363OverflowErrorrS  CryptVerifySignatureWr   CryptDestroyHash)r(  r)  r  r*  r-  r  algo_is_rsahash_lengthdecrypted_signaturekey_sizepadded_plaintext	plaintexthash_handlealg_idhash_handle_pointerr   half_lenreversed_signatures                     r   r?  r?  	  s   4 %..D%-74<#7K
 #na
  	 33LiX,55!.+xObc !788~./0I9U	99:S:]:]_opI#It4 l" 5 	K43 ))!++#00#00#00
  "(N;&&%44F
 	S01$$[$D	1ES5==(--i8AAC	 y>Q.%hi09Yh3GG	 'tt_,,	N%00F
 	S %%k2 s  	9 !788	9N y9 =$%;<<=$ %%k2 s2   <,H -CI <H6 =AI H36 II I3c           
      n   |dk(  r|}nut         j                  t         j                  t         j                  t         j                  t         j
                  d|   } t        t        |      |      j                         }t               }d}| j                  }	|	dk(  xs |	dk(  }
|
r|r]t         j                  }t        t        d      }t        |      }t              }t!        t        d|      |_        t%        |      |_        nat         j(                  }t        t        d      }t        |      }|dk(  rt               |_        n!t              }t!        t        d|      |_        t!        t        d	|      }n$	 t+        j,                  |      j/                         }t        j8                  | j:                  ||t%        |      |t%        |      |      }|t         j<                  k(  }|xs |t         j>                  k(  }|rt7        d
      tA        |       y# t0        t2        t4        f$ r t7        d
      w xY w)a0  
    Verifies an RSA, DSA or ECDSA signature via CNG

    :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", "sha256", "sha384", "sha512" or "raw"

    :param rsa_pss_padding:
        If PSS padding should be used for RSA keys

    :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
    r9  r4  r   r   r   BCRYPT_PSS_PADDING_INFO	wchar_t *BCRYPT_PKCS1_PADDING_INFOvoid *rH  N)!rN   BCRYPT_MD5_ALGORITHMBCRYPT_SHA1_ALGORITHMBCRYPT_SHA256_ALGORITHMBCRYPT_SHA384_ALGORITHMBCRYPT_SHA512_ALGORITHMgetattrhashlibdigestr)   r   BCRYPT_PAD_PSSr,   rM   r/   r"   r%   pszAlgIdr  cbSaltBCRYPT_PAD_PKCS1r   rR  rS  r   rT  rS  r5   BCryptVerifySignaturer   STATUS_INVALID_SIGNATURESTATUS_INVALID_PARAMETERrE   )r(  r)  r  r*  r-  rn  hash_constantpadding_infor$  r.  rA  padding_info_struct_pointerpadding_info_structhash_bufferr   failures                   r   r@  r@  
  s   4  3355!99!99!99
  2.1$7>>@6LE&00F%96\#9I..E*09R*S'"()D"E-m<K+/[+Q(),V&00E*09T*U'"()D"E&/3v#,1-@/3FK/U#,FH.IJ	9$)))4==?I 
&
&!,,FIC [999GD D DDG344# M95 	9 !788	9s   7#H  H4c                 P    | j                   dk7  rt        d      t        | ||      S )aL  
    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", "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
    r   +The key specified is not an RSA private keyr   r   _signr   r  r*  s      r   r_   r_   ^
  s-    6 %FGGdN33r   c                 b    | j                   }|dk7  r|dk7  rt        d      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", "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   r   r}  Tr,  r~  )r   r  r*  pkey_algs       r   ra   ra   
  s;    2 $$H5X5FGGdNDIIr   c                 P    | j                   dk7  rt        d      t        | ||      S )a7  
    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", "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   z*The key specified is not a DSA private keyr~  r  s      r   rQ   rQ   
  s-    , %EFFdN33r   c                 P    | j                   dk7  rt        d      t        | ||      S )a:  
    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", "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   z*The key specified is not an EC private keyr~  r  s      r   rK   rK   
  s-    , $EFFdN33r   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      }| j                  dk(  r|s|t        dg      z  }||vr*d}|r|s|dz  }t        t        d	|t        |                  |s'|d
ur#t        t        d|j                                     |dk(  rDt        |      | j                  dz
  kD  r)t        t        d| j                  t        |                  t        dk(  r*| j                  dk(  rt        | ||      S t        | |||      S t!        | |||      S )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", "sha256", "sha384", "sha512" or "raw"

    :param rsa_pss_padding:
        If PSS padding should be used for RSA keys

    :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
            r3  r   r   r4  r9  r:  r;  r<  Fr=  rf   z
                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
                rB   r   )rR  rY   rS  r    r6   r8   r   r   r   r   r  r  rZ  r   _pure_python_ecdsa_sign_advapi32_sign_bcrypt_sign)r   r  r*  r-  r  pkey_is_rsarB  rC  s           r   r  r  
  s   2 k:. k"	
  	 dH% dO	
  	 $$He#?x<'?K MN%oeW-22&S#'94' ( 
  	 ?%7 NN
  	 t9{,,r11^
 %%D	   ;  D(*;nMMk4QQT>?KKr   c                 h   | j                   }|dk(  xs |dk(  }|r'|dk(  r"t        | j                  |      }t        | |      S |r>|r<dddddd	j	                  |d
      }t        ||| j                  |      }t        | |      S | j                   dk(  r|dk(  rt        t        d            d}	 t        j                  t        j                  t        j                  t        j                  t        j                  d|   }	t        t         d      }
t!        j"                  | j$                  |	t'               d
|
      }t)        |       t+        |
      }t!        j,                  ||t/        |      d
      }t)        |       t        t         d      }t!        j0                  |t        j2                  t'               d
t'               |      }t)        |       t5        |      }t7        |      }t!        j0                  |t        j2                  t'               d
||      }t)        |       t9        |t5        |            }|ddd   }|dk(  r<t/        |      dz  }||d |d| z   }t;        j<                  |      j?                         }||rt!        j@                  |       S S # |rt!        j@                  |       w w xY w)a  
    Generates an RSA, DSA or ECDSA signature via CryptoAPI

    :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", "sha256", "sha384", "sha512" or "raw"

    :param rsa_pss_padding:
        If PSS padding should be used for RSA keys

    :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   r   r9  r  rE  r  r  rM  rF  r   r   r5  zO
            Windows does not support md5 signatures with DSA keys
            Nr4  rI  r  rn  r   )!r   r:   rZ  r<   rJ  r;   r   r   r    rD   rK  rL  rM  rN  rO  r(   rC   rP  r   r)   rE   r/   rQ  r  CryptSignHashWAT_SIGNATUREr&   r!   r$   r   
from_p1363r   rV  )r   r  r*  r-  r  rW  padded_datarX  r]  r^  r_  r   r  r	  r
  r  r`  s                    r   r  r  0  s   2   D%-74<#7K~.4[5J5JDQ$[+>>
 #na
  	 &nk;CWCWY]^$[+>>%.E*A
  	 KC3 ))!++#00#00#00
  "(N;&&&&F
 	S01$$[$D	1ESh	*%%&&FF
 	Sg#M2%%&&F
 	S"7E'N; "5= 6{a'HHI&	)::F ",,V499;F %%k2 ;%%k2 s   -GJ J1c           
      >   |dk(  r|}nut         j                  t         j                  t         j                  t         j                  t         j
                  d|   } t        t        |      |      j                         }t               }d}| j                  }|dk(  xs |dk(  }	|	r|r_dddd	d
d|   }
t         j                  }t        t        d      }t        |      }t              }t!        t        d|      |_        |
|_        nat         j&                  }t        t        d      }t        |      }|dk(  rt               |_        n!t              }t!        t        d|      |_        t!        t        d|      }|dk(  r2| j(                  dkD  r#|t+        ddg      v rt-        t/        d            t1        t        d      }t        j2                  | j4                  ||t7        |      t               d||      }t9        |       t;        |      }t=        |      }|	rt!        t        d      }t        j2                  | j4                  ||t7        |      ||||      }t9        |       t?        |t;        |            }|	s#tA        jB                  |      jE                         }|S )a  
    Generates an RSA, DSA or ECDSA signature via CNG

    :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", "sha256", "sha384", "sha512" or "raw"

    :param rsa_pss_padding:
        If PSS padding should be used for RSA keys

    :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
    r9  r4  r   r   r   r  r  r  r  rM  rc  rd  re  rf  r   r   r5  r  z~
            Windows does not support sha1 signatures with DSA keys based on
            sha224, sha256 or sha512
            r  )#rN   rg  rh  ri  rj  rk  rl  rm  rn  r)   r   ro  r,   rM   r/   r"   r%   rp  rq  rr  r   r   r   r    r(   BCryptSignHashr   r  rE   r&   r!   r$   r   r  r   )r   r  r*  r-  rn  rv  rw  r$  r  r  rX  rx  ry  rz  r  r   
buffer_lenr[  r)  s                      r   r  r    s   2  3355!99!99!99
  2.1$7>>@6LE$$He#?x<'?K K  ..E*09R*S'"()D"E-m<K+/[+Q()4&00E*09T*U'"()D"E&/3v#,1-@/3FK/U#,FH.IJ5[11D8^sTY[aSbOc=c
  	 &)$G


F		C wJz*FFH.IJ


F	C !&%.9I !++I6;;=	r   c                 h   t        | t        t        f      st        t	        dt        |                   t        |t              st        t	        dt        |                  t        |t              st        t	        dt        |                  t        dk(  rt        | ||      S t        | ||      S )aG  
    Encrypts a value using an RSA public key

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to encrypt with

    :param data:
        A byte string of the data to encrypt

    :param rsa_oaep_padding:
        If OAEP padding should be used instead of PKCS#1 v1.5

    :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 ciphertext
    r2  r3  A
            rsa_oaep_padding must be a bool, not %s
            rB   )rR  r   rZ   rS  r    r6   r8   boolr   _advapi32_encrypt_bcrypt_encrypt)r(  r  rsa_oaep_paddings      r   _encryptr  #  s    , /+y1IJ /0
  	 dH% dO	
  	 &- &'	
  	 ; !:DBRSS4d<LMMr   c           	         d}|rt         j                  }t        t        dt	        |            }t        j
                  | j                  t               d|t               |d      }t        |       t        |      }t        |      }t        ||       t        |t	        |             t        j
                  | j                  t               d||||      }t        |       t        |t        |            ddd   S )aU  
    Encrypts a value using an RSA public key via CryptoAPI

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to encrypt with

    :param data:
        A byte string of the data to encrypt

    :param rsa_oaep_padding:
        If OAEP padding should be used instead of PKCS#1 v1.5

    :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 ciphertext
    r   r  TNrn  )rD   
CRYPT_OAEPr(   rC   r  CryptEncryptr   r)   rE   r&   r!   r0   r*   r$   )r(  r  r  r$  r  r   r  r[  s           r   r  r  W  s    , E(((Is4y1G


!//	C wJz*FFD!T#


!//C VU7^4TrT::r   c                    t         j                  }|du rt         j                  }t        t        d      }t        |      }t        t         j                        }t        t        d|      |_	        t               |_        d|_        t        t        d|      }n
t               }t        t        d      }t	        j                  | j                  |t!        |      |t               dt               d||
      }	t#        |	       t%        |      }
t'        |
      }t	        j                  | j                  |t!        |      |t               d||
||
      }	t#        |	       t)        |t%        |            S )aO  
    Encrypts a value using an RSA public key via CNG

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to encrypt with

    :param data:
        A byte string of the data to encrypt

    :param rsa_oaep_padding:
        If OAEP padding should be used instead of PKCS#1 v1.5

    :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 ciphertext
    TBCRYPT_OAEP_PADDING_INFOrd  r   rf  r*  )rN   rr  BCRYPT_PAD_OAEPr,   rM   r/   r"   rh  r%   rp  r)   pbLabelcbLabelr(   BCryptEncryptr   r  rE   r&   r!   r$   )r(  r  r  r$  rx  ry  rz  rw  r  r   r  r[  s               r   r  r    sB   , ((E4++&,V5O&P#$%@A)+*K*KL'+FK'M$&*f#&'#FH.IJv&)$G


!,,D			C wJz*F


!,,D		C VU7^44r   c                 \   t        | t              st        t        dt	        |                   t        |t
              st        t        dt	        |                  t        |t              st        t        dt	        |                  t        dk(  rt        | ||      S t        | ||      S )a1  
    Encrypts a value using an RSA private key

    :param private_key:
        A PrivateKey instance to decrypt with

    :param ciphertext:
        A byte string of the data to decrypt

    :param rsa_oaep_padding:
        If OAEP padding should be used instead of PKCS#1 v1.5

    :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
            r  rB   )
rR  rY   rS  r    r6   r8   r  r   _advapi32_decrypt_bcrypt_decrypt)r   
ciphertextr  s      r   _decryptr    s    , k:. k"	
  	 j(+ j!	
  	 &- &'	
  	 ; j:JKK;
4DEEr   c                    d}|rt         j                  }|ddd   }t        |      }t        t        dt        |            }t	        j                  | j                  t               d|||      }t        |       t        |t        |            S )a?  
    Encrypts a value using an RSA private key via CryptoAPI

    :param private_key:
        A PrivateKey instance to decrypt with

    :param ciphertext:
        A byte string of the data to decrypt

    :param rsa_oaep_padding:
        If OAEP padding should be used instead of PKCS#1 v1.5

    :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
    r   Nrn  r  T)rD   r  r!   r(   rC   r  CryptDecryptr   r)   rE   r$   r&   )r   r  r  r$  r[  r  r   s          r   r  r    s    , E((DbD!Jz*F(Is:7G


!!C VU7^44r   c                    t         j                  }|du rt         j                  }t        t        d      }t        |      }t        t         j                        }t        t        d|      |_	        t               |_        d|_        t        t        d|      }n
t               }t        t        d      }t	        j                  | j                  |t!        |      |t               dt               d||
      }	t#        |	       t%        |      }
t'        |
      }t	        j                  | j                  |t!        |      |t               d||
||
      }	t#        |	       t)        |t%        |            S )a9  
    Encrypts a value using an RSA private key via CNG

    :param private_key:
        A PrivateKey instance to decrypt with

    :param ciphertext:
        A byte string of the data to decrypt

    :param rsa_oaep_padding:
        If OAEP padding should be used instead of PKCS#1 v1.5

    :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
    Tr  rd  r   rf  r*  )rN   rr  r  r,   rM   r/   r"   rh  r%   rp  r)   r  r  r(   BCryptDecryptr   r  rE   r&   r!   r$   )r   r  r  r$  rx  ry  rz  rw  r  r   r  r[  s               r   r  r  6  sB   , ((E4++&,V5O&P#$%@A)+*K*KL'+FK'M$&*f#&'#FH.IJv&)$G


J		C wJz*F


J	C VU7^44r   c                     t        | |      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
    r  r(  r  s     r   r^   r^   ~  s    * -t44r   c                     t        | |      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
    r  r   r  s     r   r]   r]     s    & K,,r   c                     t        | |d      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
    Tr  r  r  s     r   r\   r\     s    * -tdKKr   c                     t        | |d      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
    Tr  r  r  s     r   r[   r[     s    ( KdCCr   )NN)N)T)F)
__future__r   r   r   r   rU  sysrm  re  _asn1r   r  r	   r
   r   r   r   r   r   r   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)   r*   r+   r,   r-   r.   r/   r0    r1   _intr2   errorsr3   r4   r5   _typesr6   r7   r8   r9   _pkcs1r:   r;   r<   r=   r>   r?   utilr@   getwindowsversion_gwvr   r   	_advapi32rC   rD   rE   rF   rG   _ecdsarH   r   rI   r   rJ   rK   r  rL   r>  _cngrM   rN   rO   rP   __all__rX  r   rY   rZ   rS   r  r   r   r   ra  rY  r  r  r4  r5  r6  rT   r  r  r  r  rV   rW   rX   rU   r`   rb   rR   r&  r?  r@  r_   ra   rQ   r  r  r  r  r  r  r  r  r  r^   r]   r\   r[   r   r   r   <module>r     s>   R R 	 
      "
 
 
 %    $   U U < <  $s!Wd1g& 9{kk  [Z6AH) )XT!/ T!n1 1$I!'+ I!XMA`U'p11hb%J=H@a)H+\D/N:/zRjXvDN#/LMI`Tnd9Ne)P51p%,P;2:$zODeDO:O:_gDk3\Vr4BJB4848ULpw3tvr1Nh6;rE5P0Ff(5VE5P50-,L0Dr   