Documentation
¶
Overview ¶
Package iso7816 encodes and exchanges ISO/IEC 7816-4 command and response APDUs.
The package is independent of the connection backend and smart-card application. A Card can be backed by PC/SC, NFC, a secure element, or a test double. Application packages such as CTAP, PIV, and OpenPGP define their own commands, status semantics, and higher-level workflows on top.
Index ¶
Constants ¶
const CommandChainingBit byte = 0x10
CommandChainingBit is the CLA bit set on every non-final command fragment.
Variables ¶
var ( ErrNilCard = errors.New("iso7816: nil card") ErrInvalidCommand = errors.New("iso7816: invalid command APDU") ErrInvalidResponse = errors.New("iso7816: invalid response APDU") ErrInvalidOption = errors.New("iso7816: invalid option") ErrInvalidFragmentSize = errors.New("iso7816: invalid command fragment size") ErrResponseTooLarge = errors.New("iso7816: response data is too large") )
Functions ¶
This section is empty.
Types ¶
type APDUError ¶
APDUError reports a non-success ISO 7816 status word. Applications may use SW1 and SW2 to map the status to protocol-specific errors.
func (*APDUError) StatusWord ¶
func (e *APDUError) StatusWord() StatusWord
StatusWord returns SW1 and SW2 as one 16-bit value.
type Command ¶
Command is an ISO 7816 command APDU. Le is the maximum expected response data length: zero omits Le, 256 encodes as zero in a short APDU, and 65536 encodes as two zero bytes in an extended APDU.
func Chain ¶
Chain splits command data into ISO 7816 command-chaining fragments. Each intermediate command has CommandChainingBit set and omits Le. The final command retains the original Le.
func (Command) MarshalBinary ¶
MarshalBinary encodes c as an ISO 7816 command APDU.
type ExchangeOption ¶
type ExchangeOption func(*exchangeOptions)
ExchangeOption configures response collection for Exchange.
func WithMaxResponseSize ¶
func WithMaxResponseSize(size int) ExchangeOption
WithMaxResponseSize limits the combined response data size. The default is 65536 bytes.
func WithMoreDataStatusBytes ¶
func WithMoreDataStatusBytes(sw1 ...byte) ExchangeOption
WithMoreDataStatusBytes sets the SW1 values which cause Exchange to issue GET RESPONSE. ISO 7816 defines 0x61; some cards use 0x9f.
type Response ¶
type Response struct {
Data []byte
Status StatusWord
}
Response is an ISO 7816 response APDU.
func Exchange ¶
func Exchange( ctx context.Context, card Card, command Command, opts ...ExchangeOption, ) (Response, error)
Exchange sends a command and collects response data through ISO 7816 GET RESPONSE commands. It returns the final status without interpreting it.
func ParseResponse ¶
ParseResponse parses response data followed by SW1 and SW2. The returned response owns a copy of the data.
func Transmit ¶
Transmit sends one command APDU and parses one response APDU. It does not interpret the status word or issue GET RESPONSE.
func (Response) APDUError ¶
APDUError returns nil for a successful status word and a typed error for every other final status.
func (Response) MarshalBinary ¶
MarshalBinary encodes response data followed by SW1 and SW2.
type StatusWord ¶
type StatusWord uint16
StatusWord contains the SW1 and SW2 bytes returned by a smart card.
const (
StatusSuccess StatusWord = 0x9000
)
func NewStatusWord ¶
func NewStatusWord(sw1, sw2 byte) StatusWord
NewStatusWord combines SW1 and SW2.
func (StatusWord) CorrectLength ¶
func (s StatusWord) CorrectLength() (uint32, bool)
CorrectLength returns the response length indicated by a 0x6cxx status. An SW2 value of zero means 256 bytes. The package does not retry automatically because application commands may have side effects.
func (StatusWord) IsSuccess ¶
func (s StatusWord) IsSuccess() bool
IsSuccess reports whether the status word is 0x9000.
func (StatusWord) MoreDataLength ¶
func (s StatusWord) MoreDataLength() (uint32, bool)
MoreDataLength returns the response length indicated by a 0x61xx or 0x9fxx status. An SW2 value of zero means 256 bytes.
func (StatusWord) SW1 ¶
func (s StatusWord) SW1() byte
SW1 returns the most significant status byte.
func (StatusWord) SW2 ¶
func (s StatusWord) SW2() byte
SW2 returns the least significant status byte.
func (StatusWord) String ¶
func (s StatusWord) String() string
String returns the four hexadecimal status-word digits.