smartcard

package module
v0.0.0-...-b03e5a5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 4 Imported by: 0

README

This repository is currently not maintained

Go Smart Card API

High level API for smart card integration written in pure Go. On Linx and other Unix-like systems, this package talks directly to the PCSC-Lite daemon, and on Windows it accesses the smart card service via winscard.dll.

Note: macOS isn't and won't be supported, because its modified PCSC-Lite variant can't be accessed without cgo. PCSC-Lite will most likely be (already is?) deprecated in favour of CryptoTokenKit.

Prerequisites

Linux

Install the PCSC-Lite daemon and CCID driver.

Ubuntu:

sudo apt-get install pcscd libccid

Arch Linux:

sudo pacman -S pcsclite ccid
sudo systemctl enable pcscd
Windows

None

Usage

import (
    "fmt"
    "github.com/agambier/smartcard"
)

ctx, err := smartcard.EstablishContext()
// handle error, if any
defer ctx.Release()

reader, err := ctx.WaitForCardPresent()
// handle error, if any

card, err := reader.Connect()
// handle error, if any
defer card.Disconnect()

fmt.Printf("Card ATR: %s\n", card.ATR())
command := SelectCommand(0xa0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0xc, 0x01, 0x01)
response, err := card.TransmitAPDU(command)
// handle error, if any
fmt.Printf("Response: %s\n", response)

License

MIT. See LICENSE

Documentation

Overview

Package smartcard implements a portable high-level API for communicating with smart cards.

Example:

ctx, err := smartcard.EstablishContext()
// handle error, if any
defer ctx.Release()

reader, err := ctx.WaitForCardPresent()
// handle error, if any

card, err := reader.Connect()
// handle error, if any
defer card.Disconnect()

fmt.Printf("Card ATR: %s\n", card.ATR())
command := SelectCommand(0xa0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0xc, 0x01, 0x01)
response, err := card.TransmitAPDU(command)
// handle error, if any
fmt.Printf("Response: %s\n", response)

Index

Constants

View Source
const (
	// Scope
	SCOPE_USER     = pcsc.CARD_SCOPE_USER
	SCOPE_TERMINAL = pcsc.CARD_SCOPE_TERMINAL
	SCOPE_SYSTEM   = pcsc.CARD_SCOPE_SYSTEM
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ATR

type ATR []byte

func (ATR) String

func (atr ATR) String() string

Return string form of ATR.

type Card

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

Smart card.

func (*Card) ATR

func (c *Card) ATR() ATR

Return card ATR (answer to reset).

func (*Card) Disconnect

func (c *Card) Disconnect() error

Disconnect from card.

func (*Card) Transmit

func (c *Card) Transmit(command []byte) ([]byte, error)

Trasmit bytes to card and return response.

func (*Card) TransmitAPDU

func (c *Card) TransmitAPDU(cmd CommandAPDU) (ResponseAPDU, error)

Transmit command APDU to the card and return response.

type CommandAPDU

type CommandAPDU []byte

ISO7816-4 command APDU.

func Command1

func Command1(cla, ins, p1, p2 byte) CommandAPDU

Create command APDU with CLA, INS, P1, P2 as specified. No command data, no response required.

func Command2

func Command2(cla, ins, p1, p2, le byte) CommandAPDU

Create command APDU with CLA, INS, P1, P2 as specified. Response of length Le required.

func Command3

func Command3(cla, ins, p1, p2 byte, data []byte) CommandAPDU

Create command APDU with CLA, INS, P1, P2 and data as specified. No response required.

func Command4

func Command4(cla, ins, p1, p2 byte, data []byte, le byte) CommandAPDU

Create command APDU with CLA, INS, P1, P2 and data as specified. Response of length Le required.

func SelectCommand

func SelectCommand(aid ...byte) CommandAPDU

Create ISO7816-4 SELECT FILE APDU.

func (CommandAPDU) IsValid

func (cmd CommandAPDU) IsValid() bool

Check if command APDU is valid

func (CommandAPDU) String

func (cmd CommandAPDU) String() string

Return string form of APDU.

type Context

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

A smart card context is required to access readers and cards.

func EstablishContext

func EstablishContext(scope ...uint32) (*Context, error)

Establish smart card context. This should be the first function to be called.

func (*Context) ListReaders

func (ctx *Context) ListReaders() ([]*Reader, error)

List all smart card readers.

func (*Context) ListReadersWithCard

func (ctx *Context) ListReadersWithCard() ([]*Reader, error)

List smart card readers with inserted cards.

func (*Context) Release

func (ctx *Context) Release() error

Release resources associated with smart card context.

func (*Context) WaitForCardPresent

func (ctx *Context) WaitForCardPresent() (*Reader, error)

Block until a smart card is inserted into any reader. Returns immediately if card already present.

type Reader

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

Smart card reader. Note that physical card readers with slots for multiple cards are represented by one Reader instance per slot.

func (*Reader) Connect

func (r *Reader) Connect(protocols uint32) (*Card, error)

Connect to card.

func (*Reader) IsCardPresent

func (r *Reader) IsCardPresent() bool

Check if card is present.

func (*Reader) Name

func (r *Reader) Name() string

Return name of card reader.

func (*Reader) WaitUntilCardRemoved

func (r *Reader) WaitUntilCardRemoved()

type ResponseAPDU

type ResponseAPDU []byte

ISO7816-4 response APDU.

func Response

func Response(bytes []byte) (ResponseAPDU, error)

func (ResponseAPDU) Data

func (r ResponseAPDU) Data() []byte

Return data part of response

func (ResponseAPDU) SW

func (r ResponseAPDU) SW() uint16

Return 16-bit status word.

func (ResponseAPDU) SW1

func (r ResponseAPDU) SW1() uint8

Return SW1

func (ResponseAPDU) SW2

func (r ResponseAPDU) SW2() uint8

Return SW2

func (ResponseAPDU) String

func (r ResponseAPDU) String() string

Return string form of APDU.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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