clausecker / freefare Goto Github PK
View Code? Open in Web Editor NEWGo bindings for the libfreefare
License: GNU Lesser General Public License v3.0
Go bindings for the libfreefare
License: GNU Lesser General Public License v3.0
Just got my ACR122 with a pack of Mifare Classic 1k and Ultralight C.
Classic gets recognized correctly without a problem. However, with Ultralight C it panics in wrapTag
method, because tag.Type()
returns 6, not 1. I jury rigged tag types enum for my case and it works now, but you should probably look into it.
Hi,
I have built libfreefare 0.4.0 from sources : https://github.com/nfc-tools/libfreefare/releases/tag/libfreefare-0.4.0
The type MifareKeyDeriver
has been added after (year 2018) the tag 0.4.0 has been released (year 2015). Maybe you should state in your doc that your lib needs a self-built version of libfreefare, until a release is done (see nfc-tools/libfreefare#121)
It should be return data, nil
here. I could keep track of any issues that may arise as I work my way through my project that uses this wrapper, and then make a PR with all the fixes I end up with, so as not to spam with any more issues, if that's okay with you.
I try to use you package, but I recive some type error:
$ go get -v ./...
github.com/fuzxxl/freefare/0.3/freefare
# github.com/fuzxxl/freefare/0.3/freefare
../../fuzxxl/freefare/0.3/freefare/tag.go:137:31: cannot use *tagptr (type _Ctype_FreefareTag) as type _Ctype_MifareTag in argument to wrapTag
../../fuzxxl/freefare/0.3/freefare/tag.go:141:10: cannot use (*_Ctype_MifareTag)(unsafe.Pointer(iptr)) (type *_Ctype_MifareTag) as type *_Ctype_FreefareTag in assignment
../../fuzxxl/freefare/0.3/freefare/tag.go:160:42: cannot use *cinfo (type _Ctype_struct___8) as type _Ctype_struct___5 in assignment
../../fuzxxl/freefare/0.3/freefare/tag.go:172:16: cannot use ctag (type _Ctype_FreefareTag) as type _Ctype_MifareTag in argument to wrapTag
Go version:
$ go version
go version go1.12.4 linux/amd64
I'm going to do some more debugging, but I figured I would throw this up here in case anyone can quickly point out something I'm doing wrong.
I am using this library to write a standard data file with 32 bytes of data to a newly created application on a DESFire target:
// Ensure we're on the master application
log.Infof("Switching to the master application...")
if err = target.SelectApplication(mAppId); err != nil {
return err
}
// Authenticate to the target
log.Infof("Authenticating to tag...")
if err = target.Authenticate(0, *defaultDESFireDESKey); err != nil {
return err
}
// Create the application
log.Infof("Creating application in slot %d...", realm.Slot)
if err = target.CreateApplication(appId, initialApplicationSettings, 4|freefare.CryptoAES); err != nil {
return err
}
// Select the newly created application
log.Infof("Selecting application...")
if err = target.SelectApplication(appId); err != nil {
return err
}
// Authenticate to the application
log.Infof("Authenticating to application...")
if err = target.Authenticate(0, *defaultDESFireAESKey); err != nil {
return err
}
// Change the application transport keys
log.Infof("Changing application transport keys...")
if err = target.ChangeKey(1, *appReadKey, *defaultDESFireAESKey); err != nil {
return err
}
if err = target.ChangeKey(2, *appAuthKey, *defaultDESFireAESKey); err != nil {
return err
}
if err = target.ChangeKey(3, *appUpdateKey, *defaultDESFireAESKey); err != nil {
return err
}
// Create the UUID data file
log.Infof("Writing UUID data file...")
if err = target.CreateDataFile(1, freefare.Enciphered, initialFileSettings, mangledUUIDLength, false); err != nil {
return err
}
dataLen, err := target.WriteData(1, 0, []byte(mangledUUID))
if err != nil {
return err
}
if dataLen != mangledUUIDLength {
return errors.New("failed to write UUID to target")
}
The context surrounding this can be found here (subject to change, active development branch): https://github.com/ComputerScienceHouse/gatekeeper/blob/master/device/nfc.go#L231
This all works and dataLen
reports that 32 bytes were written. However, when I go back and read the same file:
appId := freefare.NewDESFireAid(baseAppId + realm.Slot)
appReadKey := keys.GenDESFireKey(realm.ReadKey)
// Select the realm's application
if err := target.SelectApplication(appId); err != nil {
return nil, err
}
// Authenticate to the application
if err := target.Authenticate(1, *appReadKey); err != nil {
return nil, err
}
// Read the UUID from the application
mangledUUID := make([]byte, mangledUUIDLength)
dataLen, err := target.ReadData(1, 0, mangledUUID)
if err != nil {
return nil, err
}
if dataLen != mangledUUIDLength {
return nil, errors.New("failed to read UUID from target")
}
This runs successfully and dataLen
reports that 32 bytes were read, but mangledUUID
(the buffer passed to the read function) is all zeros.
I've tried different communication modes (Plain
, Enciphered
), different data, and attempted to read immediately after writing before any access rights were applied, to no avail. The logic around all of this is based on this project, which uses libfreefare directly: https://github.com/henryk/libopenkey/blob/master/libopenkey/libopenkey.c
Am I doing something incorrectly, or does this have something to do with the read_data
buffer overrun bug in libfreefare <= 0.4.0?
Thanks for any insight you may be able to provide!
Hi,
I'm having some issues dealing with libfreefare.
I would like to transform a nfc.ISO14443aTarget into a freefare.Tag to use the SelectPassiveTarget feature of libnfc.
Here is my code:
func Connect(device nfc.Device) (*freefare.DESFireTag, error) {
// Scan for tags
modulation := nfc.Modulation{
Type: nfc.ISO14443a,
BaudRate: nfc.Nbr424,
}
nfcTag, _ := device.InitiatorSelectPassiveTarget(modulation, nil)
isoTarget, _ := nfcTag.(*nfc.ISO14443aTarget)
rawTag, _ := freefare.NewTag(device, isoTarget)
tag := rawTag.(freefare.DESFireTag)
err = tag.Connect()
if err != nil {
return nil, err
}
return &tag, nil
}
Am I doing things right here?
The result is a segfault at:
runtime.cgocall(0x454930, 0xc82003fdc8, 0x0)
/usr/lib/go/src/runtime/cgocall.go:120 +0x11b fp=0xc82003fd70 sp=0xc82003fd40
github.com/fuzxxl/freefare/0.3/freefare._C2func_mifare_desfire_connect(0x7f4e8c006f10, 0x0, 0x0, 0x0)
??:0 +0x47 fp=0xc82003fdc8 sp=0xc82003fd70
github.com/fuzxxl/freefare/0.3/freefare.DESFireTag.Connect(0xc8200103a0, 0xffff, 0x0, 0x0)
Thank you for your help!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.