smartcard

package module
v0.0.0-...-8d9fba9 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

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/nmelo/smartcard"
  "github.com/nmelo/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/nmelo/smartcard"
	"github.com/nmelo/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
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
github.com/ebfe/scard

/*

Index

Constants

This section is empty.

Variables

View Source
var ErrComm = Error(errors.New("error communication"))

Functions

func Error

func Error(e error) error

Types

type ICard

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

ICard Interface

type IReader

type IReader interface {
	ConnectCard() (ICard, error)
	ConnectSamCard() (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

type SmartcardError

type SmartcardError struct {
	Err error
}

func (*SmartcardError) Error

func (e *SmartcardError) Error() string

func (*SmartcardError) Unwrap

func (e *SmartcardError) Unwrap() error

Directories

Path Synopsis
acs
* package to handle the communication of smartcard devices under the PCSC implementation projects on which it is based: https://github.com/LudovicRousseau/PCSC 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 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 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 github.com/ebfe/scard /*
nxp
mifare
* Implementation to mifare smartcard family (Mifare Plus, Desfire, SamAV2, ...) /*
* Implementation to mifare smartcard family (Mifare Plus, Desfire, SamAV2, ...) /*
* package to handle the communication of smartcard devices under the PCSC implementation projects on which it is based: https://github.com/LudovicRousseau/PCSC 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 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 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 github.com/ebfe/scard /*
samfarmbin
* this app implements a SAMav2 cluster through MQTT topics The messages are APDU Commands ([]byte) sent to "<topicName*Inputs>", and APDU Responses ([]byte) that are left in "<topicNameOutput>" * <topicNameAsyncInputs> is the prefix of topic to generic command (sent to any SAM device).
* this app implements a SAMav2 cluster through MQTT topics The messages are APDU Commands ([]byte) sent to "<topicName*Inputs>", and APDU Responses ([]byte) that are left in "<topicNameOutput>" * <topicNameAsyncInputs> is the prefix of topic to generic command (sent to any SAM device).

Jump to

Keyboard shortcuts

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