apdu

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2022 License: MIT Imports: 4 Imported by: 6

README

APDU

Build Status Coverage Status GoDoc Go Report Card

Package apdu implements parsing and conversion of Application Protocol Data Units (APDU) which is the communication format between a card and off-card applications. The format of the APDU is defined in ISO specification 7816-4.

The package has support for extended length APDUs as well.

go get github.com/skythen/apdu

Capdu

Create

You can create a Capdu either by creating a Capdu struct:

  capduCase1 := Capdu{Cla: 0x00, Ins: 0xAB, P1: 0xCD, P2: 0xEF}
  capduCase2 := Capdu{Cla: 0x80, Ins: 0xCA, P1: 0x00, P2: 0x66, Ne: 256}
  capduCase3 := Capdu{Cla: 0x80, Ins: 0xF2, P1: 0xE0, P2: 0x02, Data: []byte{0x4F, 0x00}, Ne: 256}
  capduCase4 := Capdu{Cla: 0x00, Ins: 0xAA, P1: 0xBB, P2: 0xCC, Data: make([]byte, 65535), Ne: 65536}

(please note that Ne is the expected length of response in bytes, not encoded as Le)

or by parsing from bytes/strings:

  bCapdu, err := apdu.ParseCapdu([]byte{0x80, 0xF2, 0xE0, 0x02, 0x02, 0x4F, 0x00, 0x00)
  sCapdu, err := apdu.ParseCapduHexString("80F2E002024F0000")
Convert
Bytes

You can convert a Capdu to its bytes representation with the Bytes() function. Case and format (standard/extended) are inferred and applied automatically.

  b, err := capdu.Bytes()
String

You can convert a Capdu to its hex representation as well. The same rules apply as for conversion to bytes:

  s, err := capdu.String()
Utility
IsExtendedLength

Use IsExtendedLength to check if the CAPDU is of extended length (len of Data > 65535 or Ne > 65536):

  ext := capdu.IsExtendedLength()

Rapdu

Create

You can create a Rapdu either by creating a Rapdu struct:

  r1 := Rapdu{SW1: 0x90, SW2: 0x00}
  r2 := Rapdu{Data: []byte{0x01, 0x02, 0x03}, SW1: 0x90, SW2: 0x00}

or by parsing from bytes/strings:

  r1, err := apdu.ParseRapdu([]byte{0x90, 0x00)
  r2, err := apdu.ParseRapduHexString("0102039000")
Convert
Bytes

You can convert a Rapdu to its bytes representation with the Bytes() function.

  b, err := rapdu.Bytes()
String

You can convert a Rapdu to its hex representation as well.

  s, err := rapdu.String()
Utility
Success/Warning/Error

Use IsSuccess/IsWarning/IsError to check the response status.

  if !rapdu.IsSuccess() {
	  ...
  }

  if rapdu.IsWarning() || rapdu.IsError(){
      ...
  }

Documentation

Overview

Package apdu implements parsing and conversion of Application Protocol Data Units (APDU) which is the communication format between a card and off-card applications. The format of the APDU is defined in ISO specification 7816-4. The package has support for extended length APDUs as well.

Index

Constants

View Source
const (
	// OffsetCla defines the offset to the Cla byte of a Capdu.
	OffsetCla int = 0
	// OffsetIns defines the offset to the Ins byte of a Capdu.
	OffsetIns int = 1
	// OffsetP1 defines the offset to the P1 byte of a Capdu.
	OffsetP1 int = 2
	// OffsetP2 defines the offset to the P2 byte of a Capdu.
	OffsetP2 int = 3
	// OffsetLcStandard defines the offset to the LC byte of a standard length Capdu.
	OffsetLcStandard int = 4
	// OffsetLcExtended defines the offset to the LC byte of an extended length Capdu.
	OffsetLcExtended int = 5
	// OffsetCdataStandard defines the offset to the beginning of the data field of a standard length Capdu.
	OffsetCdataStandard int = 5
	// OffsetCdataExtended defines the offset to the beginning of the data field of an extended length Capdu.
	OffsetCdataExtended int = 7
	// MaxLenCommandDataStandard defines the maximum command data length of a standard length Capdu.
	MaxLenCommandDataStandard int = 255
	// MaxLenResponseDataStandard defines the maximum response data length of a standard length RAPDU.
	MaxLenResponseDataStandard int = 256
	// MaxLenCommandDataExtended defines the maximum command data length of an extended length Capdu.
	MaxLenCommandDataExtended int = 65535
	// MaxLenResponseDataExtended defines the maximum response data length of an extended length RAPDU.
	MaxLenResponseDataExtended int = 65536
	// LenHeader defines the length of the header of an APDU.
	LenHeader int = 4
	// LenLCStandard defines the length of the LC of a standard length APDU.
	LenLCStandard int = 1
	// LenLCExtended defines the length of the LC of an extended length APDU.
	LenLCExtended int = 3
	// LenResponseTrailer defines the length of the trailer of a Response APDU.
	LenResponseTrailer int = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Capdu

type Capdu struct {
	Cla  byte   // Cla is the class byte.
	Ins  byte   // Ins is the instruction byte.
	P1   byte   // P1 is the p1 byte.
	P2   byte   // P2 is the p2 byte.
	Data []byte // Data is the data field.
	Ne   int    // Ne is the total number of expected response data byte (not LE encoded).
}

Capdu is a Command APDU.

func ParseCapdu

func ParseCapdu(c []byte) (*Capdu, error)

ParseCapdu parses a Command APDU and returns a Capdu.

func ParseCapduHexString

func ParseCapduHexString(s string) (*Capdu, error)

ParseCapduHexString decodes the hex-string representation of a Command APDU, calls ParseCapdu and returns a Capdu.

func (*Capdu) Bytes

func (c *Capdu) Bytes() ([]byte, error)

Bytes returns the byte representation of the Capdu.

func (*Capdu) IsExtendedLength

func (c *Capdu) IsExtendedLength() bool

IsExtendedLength returns true if the Capdu has extended length (len of Data > 65535 or Ne > 65536), else false.

func (*Capdu) String

func (c *Capdu) String() (string, error)

String calls Bytes and returns the hex encoded string representation of the Capdu.

type Rapdu

type Rapdu struct {
	Data []byte // Data is the data field.
	SW1  byte   // SW1 is the first byte of a status word.
	SW2  byte   // SW2 is the second byte of a status word.
}

Rapdu is a Response APDU.

func ParseRapdu

func ParseRapdu(b []byte) (*Rapdu, error)

ParseRapdu parses a Response APDU and returns a Rapdu.

func ParseRapduHexString

func ParseRapduHexString(s string) (*Rapdu, error)

ParseRapduHexString decodes the hex-string representation of a Response APDU, calls ParseRapdu and returns a Rapdu.

func (*Rapdu) Bytes

func (r *Rapdu) Bytes() ([]byte, error)

Bytes returns the byte representation of the RAPDU.

func (*Rapdu) IsError

func (r *Rapdu) IsError() bool

IsError returns true if the RAPDU indicates an error during the execution of a command ('0x64xx', '0x65xx' or from '0x67xx' to 0x6Fxx'), otherwise false.

func (*Rapdu) IsSuccess

func (r *Rapdu) IsSuccess() bool

IsSuccess returns true if the RAPDU indicates the successful execution of a command ('0x61xx' or '0x9000'), otherwise false.

func (*Rapdu) IsWarning

func (r *Rapdu) IsWarning() bool

IsWarning returns true if the RAPDU indicates the execution of a command with a warning ('0x62xx' or '0x63xx'), otherwise false.

func (*Rapdu) String

func (r *Rapdu) String() (string, error)

String calls Bytes and returns the hex encoded string representation of the RAPDU.

Jump to

Keyboard shortcuts

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