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
- Variables
- type APDUError
- type Card
- type Command
- type DeviceInvalidatedError
- type Encoding
- type ExchangeOption
- type Response
- func Exchange(ctx context.Context, card Card, command Command, opts ...ExchangeOption) (Response, error)
- func ExchangeChain(ctx context.Context, card Card, commands []Command, opts ...ExchangeOption) (Response, error)
- func ParseResponse(raw []byte) (Response, error)
- func Transmit(ctx context.Context, card Card, command Command) (Response, error)
- type StatusWord
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 a command so each encoded APDU fits maxAPDULength, including its header, Lc and Le. All fragments use the same data limit, reserving room for the final Le. The limit must hold the required fields; case 1 commands need only their four-byte header.
EncodingAuto chooses short encoding when the original command's data and Le fit short length fields, otherwise extended encoding. All fragments retain that choice. Intermediate fragments set CommandChainingBit and omit Le; the final fragment retains the original Le.
Returned data are owned snapshots. Changing or appending one fragment's data does not affect the original command or another fragment.
func (Command) MarshalBinary ¶
MarshalBinary encodes c as an ISO 7816 command APDU.
type DeviceInvalidatedError ¶ added in v0.4.0
type DeviceInvalidatedError struct {
Err error
}
DeviceInvalidatedError marks a connection that must not be reused. Owned transports close it before returning; borrowed-card callers must close it. It preserves the underlying cause, including an interrupted APDU exchange.
func (*DeviceInvalidatedError) Error ¶ added in v0.4.0
func (e *DeviceInvalidatedError) Error() string
func (*DeviceInvalidatedError) Unwrap ¶ added in v0.4.0
func (e *DeviceInvalidatedError) Unwrap() error
type ExchangeOption ¶
type ExchangeOption func(*exchangeOptions)
ExchangeOption configures response collection for Exchange and ExchangeChain.
func WithGetResponseCLA ¶ added in v0.5.0
func WithGetResponseCLA(cla byte) ExchangeOption
WithGetResponseCLA sets the class byte of automatically generated GET RESPONSE commands. By default, they inherit the original command's CLA with the command chaining bit cleared. The original command and prepared chain are unchanged.
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 or the final command of ExchangeChain 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 ExchangeChain ¶ added in v0.3.0
func ExchangeChain( ctx context.Context, card Card, commands []Command, opts ...ExchangeOption, ) (Response, error)
ExchangeChain sends a nonempty prepared command chain, such as the result of Chain. Every intermediate response must have status 0x9000 and no data; an intermediate rejection returns an APDUError and stops the chain. The final command collects GET RESPONSE data and returns its final status uninterpreted. The caller serializes access to card and manages its lifetime after errors.
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.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
scp11
Package scp11 implements the cryptographic mechanisms used by SCP11b.
|
Package scp11 implements the cryptographic mechanisms used by SCP11b. |
|
Package scp11 establishes GlobalPlatform SCP11b channels over complete ISO 7816 APDU exchanges, independently of the connection backend and card application.
|
Package scp11 establishes GlobalPlatform SCP11b channels over complete ISO 7816 APDU exchanges, independently of the connection backend and card application. |