nfctype4

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: LGPL-3.0 Imports: 7 Imported by: 0

README

Go-nfctype4

Build Status Coverage Status GoDoc

Package go-nfctype4 implements the NFC Forum Type 4 Tag Operation Specification Version 2.0.

The implementation acts as an NFC Forum Device (an application that can read and write NFC Forum Tags) via a provided Device type which can perform Read, Update and Format operations.

The module and submodules contain all the pieces to implement software-based NFC Type 4 Tags as well. For more information about this check the documentation. You can also check out this snippet showing how this is done using the static tag implementation provided.

nfctype4-tool

nfctype4-tool is a command-line tool to read and write NFC Type 4 tags. It can be installed with:

go get -u github.com/hsanjuan/go-nfctype4/nfctype4-tool

You can then run nfctype4-tool -h to get going.

Note: to turn a Mifare Desfire EV2 (4k) card into an NFC Type 4 Tag check: https://gitlab.com/snippets/18476 .

Packages

go-nfctype4 and its subpackages offer access to the implementation of all the entities described in the NFC Forum Type 4 Tag specification. These are the links to the reference documentation of the most relevant packages:

Documentation

Overview

Package nfctype4 is an implementation of the NFC Forum Type 4 Tag Operation Specification Version 2.0 (NFCForum-TS-Type-4-Tag_2.0)

nfctype4 can be used for both reading Tags, but also for implementing software-based Tags which can be emulated with the help of NFC Readers in "target" mode.

The `Device` type offers functionality to perform `Read` and `Update` on NFC Type 4 Tags.

The bridge between the `Device` and the hardware is covered by the modules in `libnfc4/drivers/*`, which implement the `CommandDriver` interface. A `libnfc` driver is provided, which allows working with any libnfc-supported hardware.

Index

Examples

Constants

View Source
const (
	NFCForumMajorVersion = 2
	NFCForumMinorVersion = 0
)

This is the NFC Type 4 Tag standard version that we are following.

View Source
const NDEFAPPLICATION = uint64(0xD2760000850101)

NDEFAPPLICATION is the name for the NDEF Application.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandDriver

type CommandDriver interface {
	Initialize() error // Makes the driver ready for TransceiveBytes
	Close()            // Tells the driver it won't be used anymore
	String() string    // Provides information about the driver's state
	// Sends and receive bytes to the NFC device
	TransceiveBytes(tx []byte, rxLen int) ([]byte, error)
}

CommandDriver interface is the minimal set of methods the drivers need to satisfy to allow communication between the NFC Device (provided by nfctype4) and the NFC Tag.

Command drivers are in charge of implementing the link that allows this communication between Device and Tag.

nfctype4 provides a LibnfcCommandDriver driver, which uses libnfc to use a connected NFC reader and read from a real NFC Type 4 Tag device.

Other drivers are possible. See the DummyCommandDriver for an example, or the swtag which allows communication with software tags as defined in the Tag interface from the tags module.

type Commander

type Commander struct {
	// Driver is the CommandDriver in charge of communicating with the tags.
	Driver CommandDriver
}

Commander can be used to perform the NDEF Type 4 Tag Command Set operations: Select, ReadBinary and UpdateBinary

A Commander produces the right Command APDUs, serializes them and sends them to a CommandDriver.TransceiveBytes. The response is de-serialized into a Response APDU and processed.

func (*Commander) NDEFApplicationSelect

func (cmder *Commander) NDEFApplicationSelect() error

NDEFApplicationSelect performs a Select operation on the NDEF application (which is basically the first step to use a NDEF Application). It returns an error if something goes wrong.

func (*Commander) ReadBinary

func (cmder *Commander) ReadBinary(offset uint16, length uint16) ([]byte, error)

ReadBinary performs a read binary operation with the given offset and length. It returns the Payload of the response (which may be shorter than the length provided), or an error if the operation is not successful.

func (*Commander) Select

func (cmder *Commander) Select(fileID uint16) error

Select perfoms a select operation by file ID It returns an error if something fails, like cases when the response does not indicate success.

func (*Commander) UpdateBinary

func (cmder *Commander) UpdateBinary(buf []byte, offset uint16) error

UpdateBinary performs an update operation, which allows to erase and write the NDEF file.

type Device

type Device struct {
	MajorVersion byte // 2
	MinorVersion byte // 0
	// contains filtered or unexported fields
}

Device represents an NFC Forum device, that is, an application which allows to perform Read and Update operations on a NFC Type 4 Tag, by following the operation instructions stated in the specification.

Interaction with physical and software Tags is done via CommandDrivers. A device is configured with a CommandDriver which is in charge of sending and receiving bytes from the Tags. The `nfctype4/drivers/libnfc` driver, for example, supports using a libnfc-supported reader to talk to a real NFC Type 4 Tag.

func New

func New(cmdDriver CommandDriver) *Device

New returns a pointer to a new Device configured with the provided CommandDriver to perform operations on the Tags.

func (*Device) Format

func (dev *Device) Format() error

Format performs an update operation which erases a tag. It does this by writing to the first two bytes of the NDEF File and setting their value to 0 (zero-length for the file).

Be aware that the memory is not wiped or overwritten. An attacker may likely recover the values stored in the tag by resetting the length of the NDEF File to the maximum.

To wipe the memory, issue an Update() with a Message of the maximum length supported by the tag and a randomized/meaningless payload.

Format returns an error when a problem happens.

func (*Device) Read

func (dev *Device) Read() (*ndef.Message, error)

Read performs a full read operation on a NFC Type 4 tag.

The CommandDriver provided with Setup is initialized and closed at the end of the operation.

Read performs the NDEF Detect Procedure and, if successful, performs a read operation on the NDEF File.

It returns the NDEFMessage stored in the tag, or an error if something went wrong.

Example
// For libnfc, use "&libnfc.Driver{} instead of mockDriver()"
driver := mockDriver()
device := New(driver)
message, err := device.Read()
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(message)
	// To obtain the message payload only:
	payload, _ := message.Records[0].Payload()
	fmt.Println(payload)
}
Output:

urn:nfc:wkt:U:https://my.yubico.com/neo/cccccccccccccccccccccccccccccccccccccccccccc
https://my.yubico.com/neo/cccccccccccccccccccccccccccccccccccccccccccc

func (*Device) Setup

func (dev *Device) Setup(cmdDriver CommandDriver)

Setup [re]configures this device to use the provided command driver to perform operations on the tags.

func (*Device) Update

func (dev *Device) Update(m *ndef.Message) error

Update performs an update operation on a NFC Type 4 tag.

The CommandDriver provided with Setup is initialized and closed at the end of the operation.

The update operation starts by performing the NDEF Detection Procedure and the writing the provided NDEF Message to the NDEF File in the Tag, when the tag is not read-only.

Note that update cannot be used to format a tag (clear the NDEF Message). For that, use Format().

Update returns an error when there is a problem at some point in the process.

Example
// For libnfc, use "&libnfc.Driver{} instead of mockDriver()"
driver := mockDriver()
device := New(driver)
message := ndef.NewTextMessage("Hey this is a test!", "en")
err := device.Update(message)
if err != nil {
	fmt.Println(err)
}
readMsg, err := device.Read()
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(readMsg)
}
Output:

urn:nfc:wkt:T:Hey this is a test!

Directories

Path Synopsis
Package apdu provides support for Command and Response APDUs (Application Protocol Data Units).
Package apdu provides support for Command and Response APDUs (Application Protocol Data Units).
Package capabilitycontainer provides support for Capability Containers and TLV Blocks.
Package capabilitycontainer provides support for Capability Containers and TLV Blocks.
drivers
dummy
Package dummy provides a trivial CommandDriver implementation used for testing and examples.
Package dummy provides a trivial CommandDriver implementation used for testing and examples.
libnfc
Package libnfc provides a CommandDriver implementation which allows to use libnfc devices (readers etc.) to read and update Type 4 Tags.
Package libnfc provides a CommandDriver implementation which allows to use libnfc devices (readers etc.) to read and update Type 4 Tags.
swtag
Package swtag provides a CommandDriver implementation which acts as a binary interface to software-based NFC Type 4 Tags which implement the tags.Tag interface.
Package swtag provides a CommandDriver implementation which acts as a binary interface to software-based NFC Type 4 Tags which implement the tags.Tag interface.
Package helpers provides some useful functions common to nfctype4.
Package helpers provides some useful functions common to nfctype4.
Package main provides a simple tool to read and write nfctype4 tags.
Package main provides a simple tool to read and write nfctype4 tags.
Package tags provides a Tag interface to be implemented by software tags.
Package tags provides a Tag interface to be implemented by software tags.
static
Package static provides the implementation of a static software-based NFC Forum Type 4 Tag which holds a NDEF Message.
Package static provides the implementation of a static software-based NFC Forum Type 4 Tag which holds a NDEF Message.

Jump to

Keyboard shortcuts

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