apdu

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2023 License: LGPL-3.0 Imports: 4 Imported by: 0

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

View Source
const (
	INSSelect = byte(0xA4)
	INSRead   = byte(0xB0)
	INSUpdate = byte(0xD6)
)

CAPDU.INS relevant to the Type 4 Tag Specification

View Source
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

func NewReadBinaryAPDU(offset uint16, length uint16) *CAPDU

NewReadBinaryAPDU returns a new CAPDU to perform a binary read with the indicated offset and length.

func NewSelectAPDU

func NewSelectAPDU(fileID uint16) *CAPDU

NewSelectAPDU returns a new CAPDU to perform a select operation by ID with the provided fileID

func NewUpdateBinaryAPDU

func NewUpdateBinaryAPDU(data []byte, offset uint16) *CAPDU

NewUpdateBinaryAPDU returns a new CAPDU to perform a binary update operation with the provided data and offset.

func (*CAPDU) GetLc

func (apdu *CAPDU) GetLc() uint16

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

func (apdu *CAPDU) GetLe() uint16

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

func (apdu *CAPDU) Marshal() ([]byte, error)

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

func (apdu *CAPDU) SetLc(n uint16)

SetLc allows to easily set the value of the Lc bytes making sure they comply to the specification.

func (*CAPDU) SetLe

func (apdu *CAPDU) SetLe(n uint16)

SetLe allows to easily set the value of the Le bytes making sure they comply to the specification.

func (*CAPDU) String

func (apdu *CAPDU) String() string

String provides a readable representation of the CAPDU

func (*CAPDU) Unmarshal

func (apdu *CAPDU) Unmarshal(buf []byte) (rLen int, err error)

Unmarshal parses a byte slice and sets the CAPDU fields accordingly. It resets the CAPDU structure before parsing. It returns the number of bytes parsed or an error if something goes wrong.

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

func NewRAPDU(which int) *RAPDU

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

func (apdu *RAPDU) CommandCompleted() bool

CommandCompleted checks if the RAPDU indicates a successful completion of a command.

func (*RAPDU) FileNotFound

func (apdu *RAPDU) FileNotFound() bool

FileNotFound checks if the RAPDU indicates that a file was not found (usually in response to a Select operation).

func (*RAPDU) Marshal

func (apdu *RAPDU) Marshal() ([]byte, error)

Marshal returns the byte slice representation of the RAPDU

func (*RAPDU) Reset

func (apdu *RAPDU) Reset()

Reset clears the fields of the RAPDU to their default values

func (*RAPDU) String

func (apdu *RAPDU) String() string

String provides a string representation of the RAPDU

func (*RAPDU) Unmarshal

func (apdu *RAPDU) Unmarshal(buf []byte) (rLen int, err error)

Unmarshal parses a byte slice and sets the RAPDU fields accordingly. It always resets the RAPDU before parsing. It returns the number of bytes parsed or an error if something goes wrong.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL