Documentation ¶
Overview ¶
Package apdu provides support for Command and Response APDUs (Application Protocol Data Units). APDUs are used to send commands to NFC Tags and to obtain responses for them.
Index ¶
- Constants
- type CAPDU
- func (apdu *CAPDU) GetLc() uint16
- func (apdu *CAPDU) GetLe() uint16
- func (apdu *CAPDU) Marshal() ([]byte, error)
- func (apdu *CAPDU) Reset()
- func (apdu *CAPDU) SetLc(n uint16)
- func (apdu *CAPDU) SetLe(n uint16)
- func (apdu *CAPDU) String() string
- func (apdu *CAPDU) Unmarshal(buf []byte) (rLen int, err error)
- type RAPDU
- Bugs
Constants ¶
const ( INSSelect = byte(0xA4) INSRead = byte(0xB0) INSUpdate = byte(0xD6) )
CAPDU.INS relevant to the Type 4 Tag Specification
const ( RAPDUCommandCompleted = iota RAPDUCommandNotAllowed RAPDUFileNotFound RAPDUInactiveState )
RAPDU types which come handy
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CAPDU ¶
type CAPDU struct { // CLA byte //Class byte INS byte //Instruction byte P1 byte //Param byte 1 P2 byte //Param byte 2 Lc []byte //Data field length // 0, 1 or 3 bytes Data []byte //Data field Le []byte //Expected response length // 0, 1, 2, or 3 bytes }
CAPDU represents a Command APDU (https://en.wikipedia.org/wiki/Smart_card_application_protocol_data_unit) which is used to send instructions and data to the NFC devices.
func NewCapabilityContainerReadAPDU ¶
func NewCapabilityContainerReadAPDU() *CAPDU
NewCapabilityContainerReadAPDU returns a new CAPDU to perform a binary read of 15 bytes with 0 offset (the regular size of a standard capability container).
func NewNDEFTagApplicationSelectAPDU ¶
func NewNDEFTagApplicationSelectAPDU() *CAPDU
NewNDEFTagApplicationSelectAPDU returns a new CAPDU which performs a Select operation by name with the NDEF Application Name.
func NewReadBinaryAPDU ¶
NewReadBinaryAPDU returns a new CAPDU to perform a binary read with the indicated offset and length.
func NewSelectAPDU ¶
NewSelectAPDU returns a new CAPDU to perform a select operation by ID with the provided fileID
func NewUpdateBinaryAPDU ¶
NewUpdateBinaryAPDU returns a new CAPDU to perform a binary update operation with the provided data and offset.
func (*CAPDU) GetLc ¶
GetLc computes the actual Lc value from the Lc bytes. Lc indicates the length of the data sent with a Command APDU and goes from 0 to 2^16-1. Note this method will return 0 if it cannot make sense of the Lc bytes.
func (*CAPDU) GetLe ¶
GetLe computes the actual Le value from the Le bytes. Le indicates the maximum length of the data to be received Command APDU and goes from 0 to 2^16. Note this method will return 0 if it cannot make sense of the Le bytes.
func (*CAPDU) Marshal ¶
Marshal provides the byte-slice value for a CAPDU, so it can be sent to the NFC device. It returns an error when something goes wrong (uses Test()).
func (*CAPDU) Reset ¶
func (apdu *CAPDU) Reset()
Reset clears the fields of the CAPDU to their default values.
func (*CAPDU) SetLc ¶
SetLc allows to easily set the value of the Lc bytes making sure they comply to the specification.
func (*CAPDU) SetLe ¶
SetLe allows to easily set the value of the Le bytes making sure they comply to the specification.
type RAPDU ¶
type RAPDU struct { ResponseBody []byte //Data bytes SW1 byte //Status Word 1 SW2 byte //Status Word 2 }
RAPDU represents a Response APDU, which is received as an answer to Command APDUs. Response APDUs may contain data, along with two trailer bytes indicating the status.
func NewRAPDU ¶
NewRAPDU provides a quick way to obtain some commonly used Response APDUs. See the RAPDU constants for the types which are supported
func (*RAPDU) CommandCompleted ¶
CommandCompleted checks if the RAPDU indicates a successful completion of a command.
func (*RAPDU) FileNotFound ¶
FileNotFound checks if the RAPDU indicates that a file was not found (usually in response to a Select operation).
func (*RAPDU) Reset ¶
func (apdu *RAPDU) Reset()
Reset clears the fields of the RAPDU to their default values
Notes ¶
Bugs ¶
APDU's Le field could theoretically be 65536 (2^16), but this overflows uint16 so it's unsupported by SetLe and GetLe. It only happens in the case when Le has two bytes and both are 0 and in this case GetLe returns 2^16 -1.
Capability Containers with more than 15 bytes (because they include optional TLV fields), will fail, as we only read 15 bytes and the CCLEN will not match the parsed data size.