
    DjO                       U d dl mZ d dlZd dlZd dlmZ d dl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 ddl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"m#Z#m$Z$m%Z%m&Z& d
dl'm(Z(m)Z)m*Z*m+Z+m,Z, d
dl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF  ejG        eH          ZIdZJ e
ee          K                    deJ          ZLeeeedZMdeNd<   d ZO G d de          ZP G d de+          ZQdS )    )annotationsN)Thread)AnyMappingSequencecast   )AssertionResponse)&AuthenticatorExtensionsLargeBlobInputs'AuthenticatorExtensionsLargeBlobOutputs AuthenticatorExtensionsPRFInputs!AuthenticatorExtensionsPRFOutputsCredentialPropertiesOutputCredProtectExtensionHMACGetSecretInputHMACGetSecretOutput)_JsonDataObjectwebsafe_decode)AttestationConveyancePreferenceAttestationObject%AuthenticationExtensionsClientOutputsAuthenticatorAttachment AuthenticatorAttestationResponseAuthenticatorDataAuthenticatorSelectionCriteria"PublicKeyCredentialCreationOptions!PublicKeyCredentialRequestOptionsPublicKeyCredentialTypeRegistrationResponseResidentKeyRequirement_as_cbor   )AssertionSelectionClientDataCollectorClientErrorWebAuthnClient
_cbor_list)BOOLGUIDWEBAUTHNWEBAUTHN_API_VERSIONWebAuthNAssertion'WebAuthNAttestationConveyancePreferenceWebAuthNAuthenticatorAttachmentWebAuthNClientData WebAuthNCoseCredentialParametersWebAuthNCredBlobExtensionWebAuthNCredentialAttestationWebAuthNCredProtectExtensionInWebAuthNCredWithHmacSecretSaltWebAuthNEnterpriseAttestationWebAuthNExtensionWebAuthNGetAssertionOptionsWebAuthNHmacSecretSaltWebAuthNHmacSecretSaltValuesWebAuthNLargeBlobOperationWebAuthNLargeBlobSupportWebAuthNMakeCredentialOptionsWebAuthNRpEntityInformationWebAuthNUserEntityInformationWebAuthNUserVerification#WebAuthNUserVerificationRequirementi   user32)winmode)hmacGetSecretprf	largeBlob	credPropsz dict[str, type[_JsonDataObject]]_extension_output_typesc                X    | t           v r t           |                              |          S |S N)rG   	from_dict)keyvalues     O/home/thesage/.hermes/venv/lib/python3.11/site-packages/fido2/client/windows.py	_wrap_extrN   n   s,    
%%%&s+55e<<<L    c                  *     e Zd Z fdZd Zd Z xZS )CancelThreadc                    t                                                       d| _        d| _        || _        t                      | _        t          j        t          j
        | j                             d S )NTF)super__init__daemon
_completedeventr)   guidr*   WebAuthNGetCancellationIdctypesbyref)selfrW   	__class__s     rM   rT   zCancelThread.__init__u   s\    
FF	*6<	+B+BCCCCCrO   c                    | j                                          | j        s-t          j        t          j        | j                             d S d S rI   )rW   waitrV   r*   WebAuthNCancelCurrentOperationrZ   r[   rX   r\   s    rM   runzCancelThread.run}   sN    
 	M3FL4K4KLLLLL	M 	MrO   c                n    d| _         | j                                         |                                  d S )NT)rV   rW   setjoinra   s    rM   completezCancelThread.complete   s,    
		rO   )__name__
__module____qualname__rT   rb   rf   __classcell__)r]   s   @rM   rQ   rQ   t   s^        D D D D DM M M
      rO   rQ   c                  H    e Zd ZdZ	 	 dddZedd	            Zdd
ZddZdS )WindowsClienta  Fido2Client-like class using the Windows WebAuthn API.

    Note: This class only works on Windows 10 19H1 or later. This is also when Windows
    started restricting access to FIDO devices, causing the standard client classes to
    require admin priveleges to run (unlike this one).

    :param str origin: The origin to use.
    :param verify: Function to verify an RP ID for a given origin.
    :param ctypes.wintypes.HWND handle: (optional) Window reference to use.
    NFclient_data_collectorr$   c                p    |pt                                           | _        || _        || _        d | _        d S rI   )rA   GetForegroundWindowhandle_client_data_collector_allow_hmac_secret_enterprise_rpid_list)r\   rm   rp   allow_hmac_secrets       rM   rT   zWindowsClient.__init__   s=     < : : < <&;#"3 <@"""rO   returnboolc                     t           dk    S )Nr   )r+    rO   rM   is_availablezWindowsClient.is_available   s    #a''rO   c                   t          j        |          }| j                            |          \  }}t                              d|            |j        pt                      }|j        pt          j
        }t          j        }|j        t          j        k    r@t           j        }| j         |j        j        | j        v rt          j        }n(t          j        }nt!          j        |j        pd          }g }	t0          j        }
d}d}|j        rd|j        v r|	                    t7          dt9          t;          t<          j                             |j        d                             |j        !                    dd                                         d|j        v r;|	                    t7          dtE          |j        d                                        d	|j        v r3t1          j        |j        d	         !                    d
d                    }
|j        !                    dd          r0|	                    t7          dtG          d                               tI          j        tK          tL          dz  |j        !                    d                              }|r^d}|	                    t7          dtG          d                               |j'        r$tQ          |j'        j)        |j'        j*                  }nd|j        v r| j+        r|	                    t7          dtG          d                               tY          j        tK          tL          dz  |j        !                    d                              }|rtQ          |j-        |j.                  }|r$t_          |          }|0                                 nd} tc          j2        tf                                }	 ti          j5        | j6        tc          j7        tq          ts          |j                                      tc          j7        tu          ts          |j;                                      tc          j7        ty          t{          |j>                                      tc          j7        t          |                    tc          j7        t          |jA        pd|t          jB        k    t          j        |jD        pd          t          j        |jF        pd          |t{          |jG                  pg |r|jH        nd||
|t          jI        k    ||	||jJ                            tc          j7        |                     n1# t          $ r$}t          jM        N                    |          d}~ww xY w|r|O                                 |jP        }t          |jR                  }i }|j        r|jS        j        pi }|jT        dk    r3|j        !                    d          rdt          |jV                  i|d<   d|v r|jT        dk    r"|jW        r|jW        jP        }|j)        |j*        f}nd}|r8d|d         i|d<   |r(d|d         i}|d         r|d         |d<   ||d         d<   n/|d         |d<   |r"d|d         i}|d         r|d         |d<   ||d<   |jT        dk    r"d	|j        v rdt          |jX                  i|d	<   t          Y                    d            |jS        jZ        }|J t          |j\        t          ||!          t          j_        t          d" |a                                D                       t          jc        #          S )$zCreate a credential using Windows WebAuthN APIs.

        :param options: PublicKeyCredentialCreationOptions data.
        :param threading.Event event: (optional) Signal to abort the operation.
        z%Register a new credential for RP ID: NnoneFcredentialProtectionPolicycredProtect!enforceCredentialProtectionPolicycredBlobrE   supportminPinLengthTrD   zhmac-secrethmacCreateSecretrC   r   anydiscouraged   rF   rk   enabledfirstr"   secondresultsoutput1output2	supportedzNew credential registered)client_dataattestation_objectc                6    i | ]\  }}|t          ||          S rx   rN   .0kvs      rM   
<dictcomp>z1WindowsClient.make_credential.<locals>.<dictcomp>_  s&    JJJ1IaOOJJJrO   )raw_idresponseauthenticator_attachmentclient_extension_resultstype)dr   rJ   rq   collect_client_dataloggerdebugauthenticator_selectionr   resident_keyr    DISCOURAGEDr5   NONEattestationr   
ENTERPRISEr-   ANYrs   rpidPLATFORM_MANAGEDVENDOR_FACILITATEDfrom_stringr;   
extensionsappendr6   r3   r?   r   POLICYstr2intgetr1   r(   r   r   r   evalr8   r   r   rr   r   salt1salt2rQ   startrZ   POINTERr2   r*   #WebAuthNAuthenticatorMakeCredentialrp   r[   r=   r!   r>   userr0   r'   pub_key_cred_paramsr/   r<   timeoutREQUIREDr.   r   r@   user_verificationexclude_credentialsrX   	PREFERREDhintsOSErrorr%   ERROTHER_ERRORrf   contentsr   r   	auth_data	dwVersionrv   bResidentKeypHmacSecretbLargeBlobSupportedinfocredential_datar   credential_idr   r   CROSS_PLATFORMr   itemsr   
PUBLIC_KEY)r\   optionsrW   r   rp_id	selectionr   enterprise_attestationr   win_extensionslarge_blob_support
enable_prf
hmac_saltsrD   hmac_get_secrettimerattestation_pointereobjatt_objextension_outputsextensions_outsecretsecretsr   
credentials                             rM   make_credentialzWindowsClient.make_credential   s    5>wGG "8LLWUUUDUDDEEE3W7U7W7W	 -S1G1S!>!C"A"LLLAEK)5:=D$>>>5F + 2D '& BM#-v K 5:

 .	+w/AAA%%%%64 4 ; C C$+$67S$T!" !" 
 $.22 CU 	 	    W///%%%"1'2DZ2PQQ    g000%=%I&{377	6JJ& &" !%%nd;; U%%&7T

&S&STTT2<Wt^W%7%;%;E%B%BCC C  !
%%&7tDzz&R&RSSS8 Y!7!X!XJ#w'999d>U9%%&7tDzz&R&RSSS"4">4);)?)?)P)PQQ# # # !7'-/D" "J  	 ''EKKMMMMEKfn-JKKMM$	188'*9M9MNNOO:8GL;Q;QRRSS4"7#>??  
 /<<==1,1$(>(GG7C%>G%  <G%7H=  $"7#>??E2&+5

.*$(>(HH"&"%  , 011A! ! ! !D  	1 	1 	1/--a000	1  	NN!*#C$:;;,. 	$.9?RN}!!g&8&<&<[&I&I!26S=M8N8N1O!+...=A%%#/% _5F%|V];GG"G E!>-#@0%e,  F#*GAJ"7"1: ;07
GH->E)%0;<J%=%&89  E#,gaj"9"1: <18GI.=D)/:}!!kW5G&G&Gc&=!>!>2!+. 	/000&6
%%%#+5'G   &=%K%JJJ0A0G0G0I0IJJJ& & )3

 

 

 
	
s   *FU1 1
V;VVc                
   t          j        |          }| j                            |          \  }}t                              d|            t          j        }|j        pg D ]+}|xdk    r t          j	        }ndk    rt          j
        }n	 + d}d}t          j        }	d}
g }d}t          d          }|j        r|j                            d          r|j        d         }|j                            d          r0|                    t#          d	t          d
                               t%          j        t'          t(          dz  |j                            d                              }|r'|j        rt          j        }	n|j        }t          j        }	t3          j        t'          t(          dz  |j                            d                              }|ra|j        pi }t7          |j        r$t;          |j        j        |j        j                  ndd |                                 D                       }
nnd|j        v re| j!        r^|dz  }tE          j        t'          t(          |j        d                             }t7          t;          |j#        |j$                            }
|r$tK          |          }|&                                 nd} tO          j(        tR                                }	 tU          j+        | j,        |j-        tO          j.        t_          |                    tO          j.        ta          |j1        pd|te          j3        |j4        pd          tk          |j6                  pg |r|j7        nd|	||
|||||j                            tO          j.        |                     n1# tp          $ r$}tr          j:        ;                    |          d}~ww xY w|r|<                                 |j=        }t}          |j?                  }i }|j@        dk    r|rt          |jB                  |d<   |j        r|j@        dk    r]|jC        rV|jC        j=        }d|j        v r"d|j        i}|j        r
|j        |d<   d|i|d<   nd|j        i}|j        r
|j        |d<   ||d<   |j@        dk    rI|jD        dk    r>|j        d                             dd          rd|jE        i|d<   nd|jD        dk    i|d<   |jF        jG        |jF        jH        d}t          |t          |||jK        |jL        r	d|jL        ind          gd |                                 D                       S ) zGet assertion using Windows WebAuthN APIs.

        :param options: PublicKeyCredentialRequestOptions data.
        :param threading.Event event: (optional) Signal to abort the operation.
        zAssert a credential for RP ID: zsecurity-keyzclient-devicer   NFappidgetCredBlobr   TrE   rD   c           	     ~    g | ]:\  }}t          t          |          t          |j        |j                            ;S rx   )r4   r   r8   r   r   )r   cred_idsaltss      rM   
<listcomp>z/WindowsClient.get_assertion.<locals>.<listcomp>  sT       
 +GU	 7*73325;MM   rO   rC   i   r   r	      r   r   r   r   r   readblobwrittenr"   )r   r   r   )r   r   	signaturer   c                6    i | ]\  }}|t          ||          S rx   r   r   s      rM   r   z/WindowsClient.get_assertion.<locals>.<dictcomp>  s&    FFFDAqQ	!QFFFrO   )Mr   rJ   rq   r   r   r   r.   r   r   r   PLATFORMr:   r   r(   r   r   r   r6   r   r   r   r   GETwriteSETr   eval_by_credentialr9   r   r8   r   r   r   rr   r   r   r   rQ   r   rZ   r   r,   r*   !WebAuthNAuthenticatorGetAssertionrp   r   r[   r/   r7   r   r@   r   r   r'   allow_credentialsrX   r   r%   r   r   rf   r   r   r   r   rv   rL   r   dwCredLargeBlobStatuscred_large_blob
CredentialpwszCredentialTyper   r#   r
   r   user_id)r\   r   rW   r   r   
attachmenthintflags
large_bloblarge_blob_operationhmac_secret_saltsr   	u2f_appidu2f_appid_usedlg_blobrD   
cred_saltsr   r   assertion_pointerr   r   r   r   r   resultr   s                              rM   get_assertionzWindowsClient.get_assertiond  s    4=gFF "8LLWUUU>u>>???48
M'R 	 	D#^^^^!@!OJJ$__!@!IJJ
9> 	e )	!%%g.. 8#.w7	!%%m44 Q%%&7
DJJ&O&OPPP<FWt^W%7%;%;K%H%HII G  J< J+E+I((!(J+E+I(2<Wt^W%7%;%;E%B%BCC C   39r
$@ 8".sx~sxOOO! 
 /9.>.>.@.@  % %!! !G$6664;R6#*4'"4_"EFF  %A*5;DD% %!  	 ''EKKMMMME=FN+<==??	16/<<==/,1";G#5F  #7#<==C&+5

,")&!&  & .///   2  	1 	1 	1/--a000	1  	NN(%cm44	,.=A))-n.B)C)Cg& 	}!!co!1G...%v|4F} 9+1=x(096/B%e,,'6F} :,2My)9?%o6}!!c&?1&D&D%k266vuEE 6<c>Q5R%k22 "3#<#A6%k2
 N5.#
 

 "!)'!m03E$,,	   GF,=,C,C,E,EFFF
 
 	
s   6C N7 7
O%O  O%)NF)rm   r$   )ru   rv   rI   )	rg   rh   ri   __doc__rT   staticmethodry   r   r  rx   rO   rM   rl   rl      s        	 	 	@ @ @ @ @ ( ( ( \(|
 |
 |
 |
|^
 ^
 ^
 ^
 ^
 ^
rO   rl   )R
__future__r   rZ   logging	threadingr   typingr   r   r   r   ctap2r
   ctap2.extensionsr   r   r   r   r   r   r   r   utilsr   r   webauthnr   r   r   r   r   r   r   r   r   r   r   r    r!    r#   r$   r%   r&   r'   win_apir(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   	getLoggerrg   r   LOAD_LIBRARY_SEARCH_SYSTEM32WinDLLrA   rG   __annotations__rN   rQ   rl   rx   rO   rM   <module>r     s  8 # " " " " " "         / / / / / / / / / / / / % % % % % %	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 4 3 3 3 3 3 3 3                                                                                               8 
	8	$	$) 	c6			!	!(4P	!	Q	Q ),8+	= =           6   (z
 z
 z
 z
 z
N z
 z
 z
 z
 z
rO   