
    i                     T    S r SSKrSSKJr  SrSrSr " S S\5      r " S	 S
\5      r	g)a  Implement the U2F variant of ISO 7816 extended APDU.

This module implements a subset ISO 7816 APDU encoding.  In particular,
it only supports extended length encoding, it only supports commands
that expect a reply, and it does not support explicitly specifying
the length of the expected reply.

It also implements the U2F variant of ISO 7816 where the Lc field
is always specified, even if there is no data.
    N)errors         c                   H    \ rS rSrSrSrSrSrSrSr	S	S jr
S rS rS rSrg)
CommandApdu"   z~Represents a Command APDU.

Represents a Command APDU sent to the security key.  Encoding
is specified in FIDO U2F standards.
Nc                     Xl         X l        X0l        X@l        U(       a$  [	        U5      S:  a  [
        R                  " 5       eU(       a  XPl        g g )Ni  )clainsp1p2lenr   InvalidCommandErrordata)selfr   r   r   r   r   s         lib/third_party/pyu2f/apdu.py__init__CommandApdu.__init__.   s@    HHGGD	E!&&((i     c                 ~   U R                  5       n[        S5      nU R                  US'   U R                  US'   U R                  US'   U R
                  US'   U R                  (       aA  UR                  U5        UR                  U R                  5        UR                  SS/5        U$ UR                  / SQ5        U$ )zSerialize the command.

Encodes the command as per the U2F specs, using the standard
ISO 7816-4 extended encoding.  All Commands expect data, so
Le is always present.

Returns:
  Python bytearray of the encoded command.
   r   r   r   r   )r   r   r   )InternalEncodeLc	bytearrayr   r   r   r   r   extendr   lcouts      r   ToByteArrayCommandApdu.ToByteArray8   s     
			 B
A,CXXCFXXCFWWCFWWCFyy	jjn	jj	jj$ J 
jj#$Jr   c                 T   U R                  5       n[        S5      nU R                  US'   U R                  US'   U R                  US'   U R
                  US'   UR                  U5        U R                  (       a  UR                  U R                  5        UR                  SS/5        U$ )zSerialize the command in the legacy format.

Encodes the command as per the U2F specs, using the legacy
encoding in which LC is always present.

Returns:
  Python bytearray of the encoded command.
r   r   r   r   r   )r   r   r   r   r   r   r   r   r   s      r   ToLegacyU2FByteArray CommandApdu.ToLegacyU2FByteArrayQ   s     
			 B
A,CXXCFXXCFWWCFWWCFJJrNyy	jjJJd|Jr   c                     SnU R                   (       a  [        U R                   5      n[        R                  " SU5      n[	        USS  5      $ )Nr   z>Ir   )r   r   structpackr   )r   dlfourbytes      r   r   CommandApdu.InternalEncodeLci   s>    	
Byytyy>b {{4$HXab\""r   )r   r   r   r   r   )N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r"   r   __static_attributes__ r   r   r   r   "   s6    
 	##""	$20#r   r   c                   6    \ rS rSrSrSrSrSrS rS r	S r
Srg)ResponseApdus   zRepresents a Response APDU.

Represents a Response APU sent by the security key.  Encoding
is specified in FIDO U2F standards.
Nc                     Xl         U(       a  [        U5      S:  a  [        R                  " 5       e[        U5      S:  a
  US S U l        US   U l        US   U l        g )Nr   )dbg_full_packetr   r   InvalidResponseErrorbodysw1sw2)r   r   s     r   r   ResponseApdu.__init__}   sR    3t9q=''))
4y1}s)diBxDHBxDHr   c                 L    U R                   S:H  =(       a    U R                  S:H  $ )N   r   )r:   r;   r   s    r   	IsSuccessResponseApdu.IsSuccess   s    88t0D 00r   c                    U R                   S:X  a%  U R                  S:X  a  [        R                  " 5       eU R                   S:X  a%  U R                  S:X  a  [        R                  " 5       eU R                   S:X  a%  U R                  S:X  a  [        R                  " 5       eU R                  5       (       d+  [        R                  " U R                   U R                  5      eg )Ni      j      g   r   )r:   r;   r   TUPRequiredErrorInvalidKeyHandleErrorr@   	ApduErrorr?   s    r   CheckSuccessOrRaise ResponseApdu.CheckSuccessOrRaise   s    xx4DHH,##%%	T	dhh$.((**	T	dhh$.((**^^TXXtxx00 r   )r9   r7   r:   r;   )r*   r+   r,   r-   r.   r9   r:   r;   r   r@   rK   r/   r0   r   r   r2   r2   s   s'    
 
$##	11r   r2   )
r.   r%   pyu2fr   CMD_REGISTERCMD_AUTHCMD_VERSIONobjectr   r2   r0   r   r   <module>rR      s<   	  N#& N#b 16  1r   