card

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package card provides functions for communication with smart cards. It includes implementations for handling different types of smart cards and reading associated documents.

Index

Constants

View Source
const (
	UnknownDocumentCardType = CardDocumentType(iota)
	ApolloIdDocumentCardType
	GemaltoIdDocumentCardType
	MedicalDocumentCardType
	VehicleDocumentCardType
)

Variables

View Source
var APOLLO_ATR = Atr([]byte{
	0x3B, 0xB9, 0x18, 0x00, 0x81, 0x31, 0xFE, 0x9E, 0x80,
	0x73, 0xFF, 0x61, 0x40, 0x83, 0x00, 0x00, 0x00, 0xDF,
})
View Source
var ErrUnknownCard = errors.New("unknown card")
View Source
var GEMALTO_ATR_1 = Atr([]byte{
	0x3B, 0xFF, 0x94, 0x00, 0x00, 0x81, 0x31, 0x80,
	0x43, 0x80, 0x31, 0x80, 0x65, 0xB0, 0x85, 0x02,
	0x01, 0xF3, 0x12, 0x0F, 0xFF, 0x82, 0x90, 0x00,
	0x79,
})
View Source
var GEMALTO_ATR_2 = Atr([]byte{
	0x3B, 0xF9, 0x96, 0x00, 0x00, 0x80, 0x31, 0xFE,
	0x45, 0x53, 0x43, 0x45, 0x37, 0x20, 0x47, 0x43,
	0x4E, 0x33, 0x5E,
})

Available since January 2023 (maybe). Replaced very soon with an even newer version.

View Source
var GEMALTO_ATR_3 = Atr([]byte{
	0x3B, 0x9E, 0x96, 0x80, 0x31, 0xFE, 0x45, 0x53,
	0x43, 0x45, 0x20, 0x38, 0x2E, 0x30, 0x2D, 0x43,
	0x31, 0x56, 0x30, 0x0D, 0x0A, 0x6F,
})

Available since July 2023.

View Source
var GEMALTO_ATR_4 = Atr([]byte{
	0x3B, 0x9E, 0x96, 0x80, 0x31, 0xFE, 0x45, 0x53,
	0x43, 0x45, 0x20, 0x38, 0x2E, 0x30, 0x2D, 0x43,
	0x32, 0x56, 0x30, 0x0D, 0x0A, 0x6C,
})

Available since June 2024.

View Source
var ID_DOCUMENT_FILE_LOC = []byte{0x0F, 0x02}

Location of the file with document data.

View Source
var ID_PERSONAL_FILE_LOC = []byte{0x0F, 0x03}

Location of the file with personal data.

View Source
var ID_PHOTO_FILE_LOC = []byte{0x0F, 0x06}

Location of the the portrait. Portrait is encoded as JPEG.

View Source
var ID_RESIDENCE_FILE_LOC = []byte{0x0F, 0x04}

Location of the file with residence data.

View Source
var MEDICAL_ATR_1 = Atr([]byte{
	0x3B, 0xF4, 0x13, 0x00, 0x00, 0x81, 0x31, 0xFE,
	0x45, 0x52, 0x46, 0x5A, 0x4F, 0xED,
})

Possibly the first version of the medical card. Newer version has the GEMALTO_ATR_2 for the ATR.

View Source
var MEDICAL_ATR_2 = Atr([]byte{
	0x3B, 0x9E, 0x97, 0x80, 0x31, 0xFE, 0x45, 0x53,
	0x43, 0x45, 0x20, 0x38, 0x2E, 0x30, 0x2D, 0x43,
	0x31, 0x56, 0x30, 0x0D, 0x0A, 0x6E,
})

Available since March 2023?

View Source
var MED_DOCUMENT_FILE_LOC = []byte{0x0D, 0x01}

Location of the file with document data.

View Source
var MED_FIXED_PERSONAL_FILE_LOC = []byte{0x0D, 0x02}

Location of the file with fixed personal data.

View Source
var MED_VARIABLE_ADMIN_FILE_LOC = []byte{0x0D, 0x04}

Location of the file with variable administrative data.

View Source
var MED_VARIABLE_PERSONAL_FILE_LOC = []byte{0x0D, 0x03}

Location of the file with variable personal data.

View Source
var VEHICLE_ATR_0 = Atr([]byte{
	0x3B, 0xDB, 0x96, 0x00, 0x80, 0xB1, 0xFE, 0x45,
	0x1F, 0x83, 0x00, 0x31, 0xC0, 0x64, 0x1A, 0x18,
	0x01, 0x00, 0x0F, 0x90, 0x00, 0x52,
})

Possibly deprecated.

View Source
var VEHICLE_ATR_1 = Atr([]byte{
	0x3B, 0xFF, 0x94, 0x00, 0x00, 0x81, 0x31, 0x80,
	0x43, 0x80, 0x31, 0x80, 0x65, 0xB0, 0x85, 0x02,
	0x01, 0xF3, 0x12, 0x0F, 0xFF, 0x82, 0x90, 0x00,
	0x79,
})

Same as GEMALTO_ATR_1

View Source
var VEHICLE_ATR_2 = Atr([]byte{
	0x3B, 0x9D, 0x13, 0x81, 0x31, 0x60, 0x37, 0x80,
	0x31, 0xC0, 0x69, 0x4D, 0x54, 0x43, 0x4F, 0x53,
	0x73, 0x02, 0x02, 0x04, 0x40,
})
View Source
var VEHICLE_ATR_3 = Atr([]byte{
	0x3B, 0x9D, 0x13, 0x81, 0x31, 0x60, 0x37, 0x80,
	0x31, 0xC0, 0x69, 0x4D, 0x54, 0x43, 0x4F, 0x53,
	0x73, 0x02, 0x05, 0x04, 0x47,
})
View Source
var VEHICLE_ATR_4 = Atr([]byte{
	0x3B, 0x9D, 0x18, 0x81, 0x31, 0xFC, 0x35, 0x80,
	0x31, 0xC0, 0x69, 0x4D, 0x54, 0x43, 0x4F, 0x53,
	0x73, 0x02, 0x05, 0x02, 0xD4,
})

Functions

func FormatState added in v2.1.4

func FormatState(state scard.StateFlag) string

func PadPin

func PadPin(pin string) []byte

Creates a 8 byte slice containing the PIN at the beginning.

func PinTriesLeft added in v2.1.5

func PinTriesLeft(rsp []byte) int

func Transmit

func Transmit(cmd []byte) ([]byte, error)

func ValidatePin

func ValidatePin(pin string) bool

Checks if the PIN consists only of digits, and it's length is between 4 and 8.

Types

type Apollo

type Apollo struct {
	// contains filtered or unexported fields
}

Apollo is the type of the first smart ID cards. Apollo cards are not manufactured anymore, and this code could be removed in the future.

func (*Apollo) Atr

func (card *Apollo) Atr() Atr

func (*Apollo) GetDocument

func (card *Apollo) GetDocument() (document.Document, error)

func (*Apollo) InitCard

func (card *Apollo) InitCard() error

func (*Apollo) ReadCard

func (card *Apollo) ReadCard() error

func (*Apollo) ReadFile

func (card *Apollo) ReadFile(name []byte) ([]byte, error)

func (*Apollo) Test

func (card *Apollo) Test() bool

type Atr

type Atr []byte

func (Atr) Is

func (atr Atr) Is(otherAtr Atr) bool

func (Atr) String

func (atr Atr) String() string

type Card

type Card interface {
	Status() (*scard.CardStatus, error)
	Transmit([]byte) ([]byte, error)
	BeginTransaction() error
	EndTransaction(scard.Disposition) error
}

Represents a physical or virtual smart card. Essentially it is just a wrapper for the scard.Card type, but it also allows virtual cards which can be useful for testing.

type CardDocument

type CardDocument interface {
	ReadFile([]byte) ([]byte, error)
	InitCard() error
	ReadCard() error
	GetDocument() (doc.Document, error)
	Test() bool
	Atr() Atr
}

Represents a smart card with a document. All types of documents that Bas Celik can read should satisfy this interface

func DetectCardDocument

func DetectCardDocument(sc Card) (CardDocument, error)

Detects Card Document from card's ATR Ambiguous cases are solved by reading specific card content

type CardDocumentType

type CardDocumentType uint8

Represents a different types of smart card documents. Each value of `CardDocumentType` is represented with a struct that satisfies `CardDocument` interface.

func DetectCardDocumentByAtr

func DetectCardDocumentByAtr(atr Atr) []CardDocumentType

type Gemalto

type Gemalto struct {
	// contains filtered or unexported fields
}

Gemalto represents ID cards based with Gemalto Java OS. Gemalto replaced Apollo cards around 2014.

func (*Gemalto) Atr

func (card *Gemalto) Atr() Atr

func (*Gemalto) ChangePin

func (card *Gemalto) ChangePin(newPin, oldPin string) (int, error)

Returns number of tries left, and occurred error. -1 signifies unknown number of tries left

func (*Gemalto) GetCertificates added in v2.2.0

func (card *Gemalto) GetCertificates() []x509.Certificate

func (*Gemalto) GetDocument

func (card *Gemalto) GetDocument() (document.Document, error)

func (*Gemalto) InitCard

func (card *Gemalto) InitCard() error

func (*Gemalto) InitCrypto

func (card *Gemalto) InitCrypto() error

Initialize card's cryptography application

func (*Gemalto) LoadCertificates added in v2.2.0

func (card *Gemalto) LoadCertificates() error

func (*Gemalto) ReadCard

func (card *Gemalto) ReadCard() error

func (*Gemalto) ReadFile

func (card *Gemalto) ReadFile(name []byte) ([]byte, error)

func (*Gemalto) ReadSignatures added in v2.2.0

func (card *Gemalto) ReadSignatures() error

func (*Gemalto) Test

func (card *Gemalto) Test() bool

type MedicalCard

type MedicalCard struct {
	// contains filtered or unexported fields
}

Represents a smart card that holds a Serbian medical insurance document.

func (*MedicalCard) Atr

func (card *MedicalCard) Atr() Atr

func (*MedicalCard) GetDocument

func (card *MedicalCard) GetDocument() (document.Document, error)

func (*MedicalCard) InitCard

func (card *MedicalCard) InitCard() error

func (*MedicalCard) ReadCard

func (card *MedicalCard) ReadCard() error

func (*MedicalCard) ReadFile

func (card *MedicalCard) ReadFile(name []byte) ([]byte, error)

func (*MedicalCard) Test

func (card *MedicalCard) Test() bool

Newer medical cards share ATR with the ID cards (GEMALTO_ATR_2)

type UnknownDocumentCard

type UnknownDocumentCard struct {
	// contains filtered or unexported fields
}

func (*UnknownDocumentCard) Atr

func (card *UnknownDocumentCard) Atr() Atr

func (*UnknownDocumentCard) GetDocument

func (card *UnknownDocumentCard) GetDocument() (document.Document, error)

func (*UnknownDocumentCard) InitCard

func (card *UnknownDocumentCard) InitCard() error

func (*UnknownDocumentCard) ReadCard

func (card *UnknownDocumentCard) ReadCard() error

func (*UnknownDocumentCard) ReadFile

func (card *UnknownDocumentCard) ReadFile(_ []byte) ([]byte, error)

func (*UnknownDocumentCard) Test

func (card *UnknownDocumentCard) Test() bool

type VehicleCard

type VehicleCard struct {
	// contains filtered or unexported fields
}

Represents a smart card that contains a Serbian vehicle document.

func (*VehicleCard) Atr

func (card *VehicleCard) Atr() Atr

func (*VehicleCard) GetDocument

func (card *VehicleCard) GetDocument() (document.Document, error)

func (*VehicleCard) InitCard

func (card *VehicleCard) InitCard() error

Initializes vehicle card by trying three different sets of commands. The procedure is reverse-engineered from the official binary.

func (*VehicleCard) ReadCard

func (card *VehicleCard) ReadCard() error

func (*VehicleCard) ReadFile

func (card *VehicleCard) ReadFile(name []byte) ([]byte, error)

func (*VehicleCard) Test

func (card *VehicleCard) Test() bool

type VirtualCard

type VirtualCard struct {
	// contains filtered or unexported fields
}

func MakeVirtualCard

func MakeVirtualCard(atr []byte, fs map[uint32][]byte) *VirtualCard

func (*VirtualCard) Status

func (card *VirtualCard) Status() (*scard.CardStatus, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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