o
    i                     @   sH   d Z ddlZddlmZ dZdZdZG dd deZG d	d
 d
eZ	dS )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                   @   sF   e Zd ZdZdZdZdZdZdZdddZ	dd Z
dd Zd	d
 ZdS )CommandApduzRepresents a Command APDU.

  Represents a Command APDU sent to the security key.  Encoding
  is specified in FIDO U2F standards.
  Nc                 C   sB   || _ || _|| _|| _|rt|dkrt |r|| _d S d S )Ni  )clainsp1p2lenr   InvalidCommandErrordata)selfr   r   r	   r
   r    r   3/tmp/google-cloud-sdk/lib/third_party/pyu2f/apdu.py__init__.   s   
zCommandApdu.__init__c                 C   sx   |   }td}| j|d< | j|d< | j|d< | j|d< | jr3|| || j |ddg |S |g d |S )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outr   r   r   ToByteArray8   s   





zCommandApdu.ToByteArrayc                 C   sf   |   }td}| j|d< | j|d< | j|d< | j|d< || | jr*|| j |ddg |S )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   r   r   r   ToLegacyU2FByteArrayQ   s   





z CommandApdu.ToLegacyU2FByteArrayc                 C   s0   d}| j r
t| j }td|}t|dd  S )Nr   z>Ir   )r   r   structpackr   )r   dlfourbyter   r   r   r   i   s
   
zCommandApdu.InternalEncodeLc)N)__name__
__module____qualname____doc__r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   "   s    

r   c                   @   s4   e Zd ZdZdZdZdZdd Zdd Zdd Z	dS )	ResponseApduzRepresents a Response APDU.

  Represents a Response APU sent by the security key.  Encoding
  is specified in FIDO U2F standards.
  Nc                 C   sP   || _ |rt|dk rt t|dkr|d d | _|d | _|d | _d S )Nr   )dbg_full_packetr   r   InvalidResponseErrorbodysw1sw2)r   r   r   r   r   r   }   s   
zResponseApdu.__init__c                 C   s   | j dko	| jdkS )N   r   )r)   r*   r   r   r   r   	IsSuccess   s   zResponseApdu.IsSuccessc                 C   sp   | j dkr| jdkrt | j dkr| jdkrt | j dkr*| jdkr*t |  s6t| j | jd S )Ni      j      g   r   )r)   r*   r   TUPRequiredErrorInvalidKeyHandleErrorr-   	ApduErrorr,   r   r   r   CheckSuccessOrRaise   s   z ResponseApdu.CheckSuccessOrRaise)
r   r    r!   r"   r(   r)   r*   r   r-   r6   r   r   r   r   r#   s   s    r#   )
r"   r   pyu2fr   CMD_REGISTERCMD_AUTHCMD_VERSIONobjectr   r#   r   r   r   r   <module>   s   
Q