Files

123 lines
3.2 KiB
Go

package pcsc
import (
"context"
"errors"
"fmt"
"strings"
)
const HardwareKind = "pcsc"
var (
ErrUnsupported = errors.New("pcsc: platform is not supported")
ErrUnavailable = errors.New("pcsc: service is unavailable")
ErrReaderNotFound = errors.New("pcsc: reader not found")
ErrNoCard = errors.New("pcsc: no card is inserted")
ErrPINRequired = errors.New("pcsc: SIM PIN is required")
ErrPINTriesLow = errors.New("pcsc: refusing PIN verification because too few attempts remain")
ErrPINRejected = errors.New("pcsc: SIM PIN was rejected")
ErrPINStatusUnknown = errors.New("pcsc: SIM PIN state or retry count is unknown; verification refused")
ErrPINVerification = errors.New("pcsc: SIM PIN verification failed")
ErrPINRetryBlocked = errors.New("pcsc: automatic SIM PIN retries disabled; correct the PIN and explicitly allow another attempt")
ErrSecurityStatus = errors.New("pcsc: card security status does not allow this operation")
ErrUSIMUnavailable = errors.New("pcsc: no usable USIM application was found")
ErrApplicationNotFound = errors.New("pcsc: smart-card application was not found")
ErrCardChanged = errors.New("pcsc: card identity changed during authentication")
ErrAKARejected = errors.New("pcsc: USIM rejected the network authentication token")
)
type Reader struct {
Name string `json:"name"`
USBPath string `json:"usb_path"`
VendorID string `json:"vendor_id,omitempty"`
ProductID string `json:"product_id,omitempty"`
Manufacturer string `json:"manufacturer,omitempty"`
Product string `json:"product,omitempty"`
CardPresent bool `json:"card_present"`
ATR string `json:"atr,omitempty"`
}
type Selector struct {
USBPath string
ReaderName string
}
func (selector Selector) Validate() error {
if strings.TrimSpace(selector.USBPath) == "" && strings.TrimSpace(selector.ReaderName) == "" {
return errors.New("pcsc: reader selector is empty")
}
return nil
}
type Identity struct {
ICCID string
IMSI string
MNCLength int
USIMAID []byte
SMSC string
SPN string
PINRequired bool
PINTries int
}
type Snapshot struct {
Reader Reader
Identity Identity
}
type AKAChallenge struct {
RAND [16]byte
AUTN [16]byte
}
type AKAResult struct {
RES []byte
CK []byte
IK []byte
AUTS []byte
SynchronizationFailure bool
}
type PINError struct {
Kind error
Tries int
Status uint16
HasStatus bool
}
func (err *PINError) Error() string {
if err == nil {
return "pcsc: SIM PIN error"
}
message := err.Kind.Error()
if err.Tries >= 0 {
message = fmt.Sprintf("%s (%d attempts remain)", message, err.Tries)
}
if err.HasStatus || err.Status != 0 {
message = fmt.Sprintf("%s (SW=%04X)", message, err.Status)
}
return message
}
func (err *PINError) Unwrap() error {
if err == nil {
return nil
}
return err.Kind
}
type Card interface {
Transmit(context.Context, []byte) ([]byte, uint16, error)
Close() error
}
type resettableCard interface {
CloseWithReset() error
}
type Backend interface {
Readers(context.Context) ([]Reader, error)
Open(context.Context, Selector) (Card, error)
}