
    C                     >    S r SSKrSSKJr  SSKJr   " S S\5      rg)zThis module implements the low level device API.

This module exposes a low level SecurityKey class,
representing the physical security key device.
    N)apdu)errorsc                   N    \ rS rSrSrS rS r SS jrS rS r	S r
S	 rS
 rSrg)SecurityKey   zLow level api for talking to a security key.

This class implements the low level api specified in FIDO
U2F for talking to a security key.
c                 T    Xl         SU l        [        R                  " S5      U l        g )NFzpyu2f.hardware)	transportuse_legacy_formatlogging	getLoggerlogger)selfr	   s     !lib/third_party/pyu2f/hardware.py__init__SecurityKey.__init__!   s"    N"D##$45DK    c           	      ^   U R                   R                  S5        [        U5      S:w  d  [        U5      S:w  a  [        R                  " 5       e[        X-   5      nU R                  [        R                  " S[        R                  SSU5      5      nUR                  5         UR                  $ )a  Register security key.

Ask the security key to register with a particular origin & client.

Args:
  challenge_param: Arbitrary 32 byte challenge string.
  app_param: Arbitrary 32 byte applciation parameter.

Returns:
  A binary structure containing the key handle, attestation, and a
  signature over that by the attestation key.  The precise format
  is dictated by the FIDO U2F specs.

Raises:
  TUPRequiredError: A Test of User Precense is required to proceed.
  ApduError: Something went wrong on the device.
CmdRegister    r      )r   debuglenr   InvalidRequestError	bytearrayInternalSendApdur   CommandApduCMD_REGISTERCheckSuccessOrRaisebody)r   challenge_param	app_paramr   responses        r   r   SecurityKey.CmdRegister&   s    $ 	KKm$
?r!S^r%9&&((_01D$$T%5%5	& H   "==r   c           	         U R                   R                  S5        [        U5      S:w  d  [        U5      S:w  a  [        R                  " 5       eU(       a  SOSn[        X-   [        [        U5      /5      -   U-   5      nU R                  [        R                  " S[        R                  USU5      5      nUR                  5         UR                  $ )a  Attempt to obtain an authentication signature.

Ask the security key to sign a challenge for a particular key handle
in order to authenticate the user.

Args:
  challenge_param: SHA-256 hash of client_data object as a bytes
      object.
  app_param: SHA-256 hash of the app id as a bytes object.
  key_handle: The key handle to use to issue the signature as a bytes
      object.
  check_only: If true, only check if key_handle is valid.

Returns:
  A binary structure containing the key handle, attestation, and a
  signature over that by the attestation key.  The precise format
  is dictated by the FIDO U2F specs.

Raises:
  TUPRequiredError: If check_only is False, a Test of User Precense
      is required to proceed.  If check_only is True, this means
      the key_handle is valid.
  InvalidKeyHandleError: The key_handle is not valid for this device.
  ApduError: Something else went wrong on the device.
CmdAuthenticater      r   r   )r   r   r   r   r   r   r   r   r   CMD_AUTHr   r   )r   r    r!   
key_handle
check_onlycontrolr   r"   s           r   r%   SecurityKey.CmdAuthenticateG   s    < 	KK'(
?r!S^r%9&&(( ddG_0J0124>? @D$$T%5%5	4=='4&/ 0H  "==r   c                 <   U R                   R                  S5        U R                  [        R                  " S[        R
                  SS5      5      nUR                  5       (       d+  [        R                  " UR                  UR                  5      eUR                  $ )aj  Obtain the version of the device and test transport format.

Obtains the version of the device and determines whether to use ISO
7816-4 or the U2f variant.  This function should be called at least once
before CmdAuthenticate or CmdRegister to make sure the object is using the
proper transport for the device.

Returns:
  The version of the U2F protocol in use.

CmdVersionr   )r   r   r   r   r   CMD_VERSION	IsSuccessr   	ApduErrorsw1sw2r   )r   r"   s     r   r-   SecurityKey.CmdVersionr   sx     	KKl#$$T%5%5	4T4&) *H X\\8<<88==r   c                 p    U R                   R                  S5        U R                  R                  U5        g )NCmdBlink)r   r   r	   	SendBlink)r   times     r   r5   SecurityKey.CmdBlink   s&    KKj!NNT"r   c                 n    U R                   R                  S5        U R                  R                  5         g )NCmdWink)r   r   r	   SendWink)r   s    r   r:   SecurityKey.CmdWink   s$    KKi NNr   c                 n    U R                   R                  S5        U R                  R                  U5      $ )NCmdPing)r   r   r	   SendPing)r   datas     r   r>   SecurityKey.CmdPing   s)    KKi >>""4((r   c                    SnU R                   (       dw  [        R                  " U R                  R	                  UR                  5       5      5      nUR                  S:X  a(  UR                  S:X  a  SU l         U R                  U5      $ U$ [        R                  " U R                  R	                  UR                  5       5      5      nU$ )a  Send an APDU to the device.

Sends an APDU to the device, possibly falling back to the legacy
encoding format that is not ISO7816-4 compatible.

Args:
  apdu_to_send: The CommandApdu object to send

Returns:
  The ResponseApdu object constructed out of the devices reply.
Ng   r   T)
r
   r   ResponseApdur	   SendMsgBytesToByteArrayr1   r2   r   ToLegacyU2FByteArray)r   apdu_to_sendr"   s      r   r   SecurityKey.InternalSendApdu   s     H!!""4>>#>#>

"
"
$$& 'h		(,,$"6 "&$$\22 O ""4>>#>#>

+
+
-$/ 0hOr   )r   r	   r
   N)F)__name__
__module____qualname____firstlineno____doc__r   r   r%   r-   r5   r:   r>   r   __static_attributes__ r   r   r   r      s6    6
J "'	)V(#)r   r   )rN   r   pyu2fr   r   objectr   rP   r   r   <module>rS      s#   
   P& Pr   