smartcard

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 1 Imported by: 4

README

smartcard

smartcard devices under the PCSC implementation

Implementation for mifare smartcard family (Mifare Plus, Desfire, SamAV2, ...)

example 1 (SAMav2)

package main

import (
  "log"
  "strings"
  "github.com/dumacp/smartcard"
  "github.com/dumacp/smartcard/nxp/mifare"
)

func main() {
  ctx, err := smartcard.NewContext()
  if err != nil {
    log.Fatal("Not connection")
  }
  defer ctx.Release()
  readers, err := smartcard.ListReaders(ctx)
  for i, el := range readers {
    log.Printf("reader %v: %s\n", i, el)
  }
  samReaders := make([]smartcard.Reader,0)
  for _, el := range readers {
    if strings.Contains(el, "SAM") {
      samReaders = append(samReaders, smartcard.NewReader(ctx, el))
    }
  }
  for _, samReader := range samReaders {
    sam, err := mifare.ConnectSamAv2(samReader)
    if err != nil {
      log.Printf("%s\n",err)
      continue
    }
    version, err := sam.GetVersion()
    if err != nil {
      log.Fatalln("Not GetVersion: ", err)
    }
    log.Printf("GetVersion sam: % X\n", version)
  }
}

example 2 (Mifare Plus SL3 auth)

package main

import (
	"log"
	"flag"
	"strings"
	"encoding/hex"
	"github.com/dumacp/smartcard"
	"github.com/dumacp/smartcard/nxp/mifare"
)

var keyS string
var keyNbr int

func init() {
        flag.StringVar(&keyS, "key", "00000000000000000000000000000000", "key aes128")
        flag.IntVar(&keyNbr, "keyNbr", 0x4002, "key Number")
}

func main() {
	flag.Parse()

	key, err := hex.DecodeString(keyS)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("key: [% X]\n", key)

	ctx, err := smartcard.NewContext()
	if err != nil {
		log.Fatal("Not connection")
	}
	defer ctx.Release()
	readers, err := smartcard.ListReaders(ctx)
	for i, el := range readers {
		log.Printf("reader %v: %s\n", i, el)
	}
	mplusReaders := make([]smartcard.Reader,0)
	for _, el := range readers {
		if strings.Contains(el, "PICC") {
			mplusReaders = append(mplusReaders, smartcard.NewReader(ctx, el))
		}
	}
	for _, mplusReader := range mplusReaders {
		mplus, err := mifare.ConnectMplus(mplusReader)
		if err != nil {
			log.Printf("%s\n",err)
			continue
		}
		uid, err := mplus.UID()
		if err != nil {
			log.Fatalln("ERROR: ", err)
		}
		log.Printf("card UID: % X\n", uid)

		ats, err := mplus.ATS()
		if err != nil {
			log.Println("ERROR: ", err)
		}
		log.Printf("card ATS: % X\n", ats)

		resp, err := mplus.FirstAuth(keyNbr,key)
		if err != nil {
			log.Fatalf("Error: %s\n",err)
		}
		log.Printf("Auth: % X\n", resp)
	}
}

Documentation

Overview

package to handle the communication of smartcard devices under the PCSC implementation

projects on which it is based:

    https://github.com/LudovicRousseau/PCSC
	https://github.com/ebfe/scard

package to handle the communication of smartcard devices under the PCSC implementation

projects on which it is based:

https://github.com/LudovicRousseau/PCSC
https://github.com/ebfe/scard

Index

Constants

This section is empty.

Variables

View Source
var ErrComm = Error(errors.New("error communication"))
View Source
var ErrNoSmartcard = Error(errors.New("error no smartcard"))
View Source
var ErrTransmit = Error(errors.New("error transmit"))

Functions

func Error

func Error(e error) error

Types

type ICard

type ICard interface {
	Apdu(apdu []byte) ([]byte, error)
	ATR() ([]byte, error)
	GetData(byte) ([]byte, error)
	UID() ([]byte, error)
	ATS() ([]byte, error)
	SAK() byte
	DisconnectCard() error
	DisconnectResetCard() error
	EndTransactionResetCard() error
}

ICard Interface

type IReader

type IReader interface {
	//ConnectCard connect card with protocol T=1
	ConnectCard() (ICard, error)
	//ConnectCard connect card with protocol T=1.
	//Some readers distinguish between the flow to connect a contact-based smart card and a contactless smart card.
	ConnectSamCard() (ICard, error)
	//ConnectSamCard_T0 ConnectCard connect card with protocol T=1.
	ConnectSamCard_T0() (ICard, error)
	//ConnectSamCard_Tany ConnectCard connect card with protocol T=any.
	ConnectSamCard_Tany() (ICard, error)
}

IReader Interface to Reader device

type ISO7816cmd

type ISO7816cmd struct {
	CLA byte
	INS byte
	P1  byte
	P2  byte
	Le  bool
}

ISO7816cmd command ISO7816

func (*ISO7816cmd) PrefixApdu

func (cmd *ISO7816cmd) PrefixApdu() []byte

PrefixApdu build prefix apdu from ISO7816cmd

type SmartcardError

type SmartcardError struct {
	Err error
}

func (*SmartcardError) Error

func (e *SmartcardError) Error() string

Directories

Path Synopsis
acs
* package to handle the communication of smartCard devices under the PCSC implementation
* package to handle the communication of smartCard devices under the PCSC implementation
nxp
mifare
* Implementation to mifare smartcard family (Mifare Plus, Desfire, SamAV2, ...) /*
* Implementation to mifare smartcard family (Mifare Plus, Desfire, SamAV2, ...) /*
mifare/desfire/ev2
* package with common functions to manage a Desfire TAG
* package with common functions to manage a Desfire TAG
* package to handle the communication of smartcard devices under the PCSC implementation
* package to handle the communication of smartcard devices under the PCSC implementation
planeta
samfarmbin
* this app implements a SAMav2 cluster through MQTT topics
* this app implements a SAMav2 cluster through MQTT topics

Jump to

Keyboard shortcuts

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