token2

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 5 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

telesma-app/token2

Go Reference Go

Go library for Token2 device information, reader settings and PIV extensions over PC/SC, USB HID feature reports and CTAPHID.

[!WARNING] This module is under active development. Its public API may change during v0.x.

Features

  • Device serial numbers, ATR identity and configuration through the available PC/SC and HID interfaces.
  • Model identification from reported serial numbers, with parsers for device configuration, enabled interfaces and capabilities.
  • Reader sound-level and NFC settings on compatible Token2 readers.
  • PIV version, slot metadata and attestation certificates.
  • PIV key import, generation, management-key changes and administrative commands.

Available information and commands depend on the device, connection and firmware. USB and NFC interfaces may expose different information for the same token. PIV extensions use an existing connection from telesma-app/piv.

Installation

go get github.com/telesma-app/token2@latest

See go.mod for the required Go version.

Quick start

Open a device by its exact PC/SC reader name and print its serial number and interface configuration. Replace the reader name with one from your system:

package main

import (
	"context"
	"fmt"
	"log"

	token2pcsc "github.com/telesma-app/token2/transport/pcsc"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	device, err := token2pcsc.Open("exact PC/SC reader name")
	if err != nil {
		return err
	}
	defer device.Close()

	ctx := context.Background()
	serial, err := device.SerialNumber(ctx)
	if err != nil {
		return err
	}

	config, err := device.Configuration(ctx)
	if err != nil {
		return err
	}

	fmt.Printf("Serial: %s\nInterfaces: %+v\n", serial, config.InterfaceState())
	return nil
}

This example only reads information. PC/SC access requires a running native smart-card service and a connected token with its CCID interface enabled. Close devices when finished; the PC/SC connection leaves the card powered by default.

Packages

Package Purpose
token2 Device data, response parsers and model identification
transport/pcsc Device information and reader settings over PC/SC
transport/hid Serial numbers through USB HID feature reports
transport/ctaphid ATR identity through a FIDO HID connection
piv Token2 PIV extensions

Examples

Runnable examples cover PC/SC information, HID serial numbers, CTAPHID identity and reader settings. Each example is a separate Go module.

cd examples/pcsc
go run .

It uses the first matching reader; set PCSC_READER to a reader-name substring to select one. The reader-settings example requires explicit settings and changes the reader configuration; the information examples only read data.

Development

GOWORK=off go test ./...
make lint

Normal tests do not require hardware. The lint target checks the root module and each example with golangci-lint v2.

License

Apache License 2.0.

References

Documentation

Overview

Package token2 parses vendor responses and identifies models by full serial. Device access lives in transport; physical-device correlation belongs to the consuming runtime.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidATR = errors.New("token2: invalid ATR")

ErrInvalidATR reports malformed ATR data or malformed Token2 identity bytes.

View Source
var ErrInvalidConfiguration = errors.New("token2: invalid configuration")

ErrInvalidConfiguration reports malformed configuration data received from a Token2 device.

View Source
var ErrInvalidSerialResponse = errors.New("token2: invalid serial-number response")

ErrInvalidSerialResponse reports a malformed serial-number response received from a Token2 device.

Functions

func ParseSerialNumber

func ParseSerialNumber(response []byte) (string, error)

ParseSerialNumber parses the TAG-LENGTH-VALUE response returned by the Token2 serial-number command.

Types

type ATR

type ATR struct {
	Raw      []byte       `json:"raw"`
	Identity *ATRIdentity `json:"identity,omitempty"`
}

ATR contains Token2 identity data encoded in a PC/SC answer-to-reset.

func ParseATR

func ParseATR(atr []byte) (ATR, error)

ParseATR extracts the USB product ID and decimal serial suffix from a Token2 ATR. Raw refers to the supplied ATR slice. A valid ATR without Token2 historical bytes has a nil Identity and is a successful result.

type ATRIdentity added in v0.12.0

type ATRIdentity struct {
	ProductID    uint16 `json:"productId"`
	SerialSuffix string `json:"serialSuffix"`
}

ATRIdentity is present only for Token2's vendor historical-byte format.

type Appearance

type Appearance [4]byte

Appearance identifies the Token2 device appearance reported by its firmware.

type Capabilities added in v0.12.0

type Capabilities struct {
	FIDOPINSet                      bool `json:"fidoPINSet"`
	FIDOPINLocked                   bool `json:"fidoPINLocked"`
	SupportsHOTP                    bool `json:"supportsHOTP"`
	SupportsTOTP                    bool `json:"supportsTOTP"`
	SupportsNFC                     bool `json:"supportsNFC"`
	SupportsCCID                    bool `json:"supportsCCID"`
	SupportsFIDO21                  bool `json:"supportsFIDO21"`
	HasFingerprintSensor            bool `json:"hasFingerprintSensor"`
	SupportsFingerprintRegistration bool `json:"supportsFingerprintRegistration"`
	SupportsMandatoryFingerprint    bool `json:"supportsMandatoryFingerprint"`
	OTPRequiresFingerprint          bool `json:"otpRequiresFingerprint"`
	SupportsButtonHOTP              bool `json:"supportsButtonHOTP"`
	ButtonHOTPConfigured            bool `json:"buttonHOTPConfigured"`
	ButtonHOTPSendsEnter            bool `json:"buttonHOTPSendsEnter"`
	ButtonHOTPRequiresLongPress     bool `json:"buttonHOTPRequiresLongPress"`
	ButtonHOTPUsesNumericKeypad     bool `json:"buttonHOTPUsesNumericKeypad"`
}

Capabilities contains flags reported in a modern configuration.

type Configuration

type Configuration struct {
	Raw []byte `json:"raw"`

	TransferType        TransferType        `json:"transferType"`
	DeviceConfiguration DeviceConfiguration `json:"deviceConfiguration"`
	Appearance          Appearance          `json:"appearance"`
	FIDOVersion         FIDOVersion         `json:"fidoVersion"`
	DeviceExtension     DeviceExtension     `json:"deviceExtension"`
}

Configuration holds the decoded response; Raw retains all bytes, including unknown fields.

func ParseConfiguration

func ParseConfiguration(response []byte) (Configuration, error)

ParseConfiguration parses either a one-byte legacy response or a modern response of at least ten bytes. Additional bytes are retained in Raw.

func (Configuration) Capabilities added in v0.12.0

func (c Configuration) Capabilities() (Capabilities, bool)

Capabilities decodes the modern configuration masks. The boolean is false for a legacy response, which supplies only interface state.

func (Configuration) InterfaceState added in v0.12.0

func (c Configuration) InterfaceState() InterfaceState

InterfaceState decodes the transfer-type mask, including legacy responses.

type Descriptor added in v0.12.0

type Descriptor[T any] struct {
	Value *T    `json:"value"`
	Err   error `json:"-"`
}

Descriptor holds a device observation. A non-nil Value is a completed read, including zero values and empty strings. Err holds the original read failure. Both may be set when data was received before an error. Neither is set when the interface did not provide a reading. Errors are not serialized to JSON.

type DeviceConfiguration added in v0.12.0

type DeviceConfiguration byte

DeviceConfiguration contains the device's configuration and capability bits.

const (
	DeviceConfigurationHOTPSuppressEnterMask    DeviceConfiguration = 0x01
	DeviceConfigurationFIDOPINSetMask           DeviceConfiguration = 0x02
	DeviceConfigurationHOTPMask                 DeviceConfiguration = 0x04
	DeviceConfigurationFingerprintSensorMask    DeviceConfiguration = 0x08
	DeviceConfigurationNFCMask                  DeviceConfiguration = 0x10
	DeviceConfigurationHOTPLongPressMask        DeviceConfiguration = 0x20
	DeviceConfigurationFIDOPINLockedMask        DeviceConfiguration = 0x40
	DeviceConfigurationButtonHOTPConfiguredMask DeviceConfiguration = 0x80
)

type DeviceExtension added in v0.12.0

type DeviceExtension byte

DeviceExtension contains the extended configuration and capability bits.

const (
	DeviceExtensionTOTPMask                        DeviceExtension = 0x01
	DeviceExtensionFIDO21Mask                      DeviceExtension = 0x02
	DeviceExtensionFingerprintRegistrationMask     DeviceExtension = 0x04
	DeviceExtensionHOTPNumericKeypadMask           DeviceExtension = 0x08
	DeviceExtensionCCIDMask                        DeviceExtension = 0x10
	DeviceExtensionButtonHOTPUnsupportedMask       DeviceExtension = 0x20
	DeviceExtensionOTPRequiresFingerprintMask      DeviceExtension = 0x40
	DeviceExtensionMandatoryFingerprintSupportMask DeviceExtension = 0x80
)

type DeviceInfo

type DeviceInfo struct {
	SerialNumber  Descriptor[string]        `json:"serialNumber"`
	ATR           Descriptor[ATR]           `json:"atr"`
	Configuration Descriptor[Configuration] `json:"configuration"`
	Model         *Model                    `json:"model,omitempty"`
}

DeviceInfo contains the independent vendor readings from one interface. Correlation with other interfaces belongs to the consuming device runtime.

func (DeviceInfo) HasData added in v0.12.0

func (info DeviceInfo) HasData() bool

type FIDOVersion

type FIDOVersion struct {
	Major byte `json:"major"`
	Minor byte `json:"minor"`
	Patch byte `json:"patch"`
}

FIDOVersion is the three-component FIDO version reported by a Token2 device.

type InterfaceState added in v0.12.0

type InterfaceState struct {
	FIDOEnabled          bool `json:"fidoEnabled"`
	HOTPKeystrokeEnabled bool `json:"hotpKeystrokeEnabled"`
	CCIDEnabled          bool `json:"ccidEnabled"`
}

InterfaceState describes the interfaces enabled by the configuration mask.

type Model added in v0.12.0

type Model struct {
	Release    string `json:"release"`
	FormFactor string `json:"formFactor"`
	Branding   string `json:"branding"`
}

Model is a serial-catalogue match, independent of raw command readings.

func Identify

func Identify(serialNumber string) (Model, bool)

Identify looks up a full vendor serial in the model catalogue. The boolean is false for unknown or malformed serials.

func (Model) Name added in v0.12.0

func (m Model) Name() string

Name combines the catalogue's branding and form factor.

type TransferType added in v0.12.0

type TransferType byte

TransferType is the interface-state bitmask reported by the device.

const (
	TransferTypeFIDODisabledMask          TransferType = 0x01
	TransferTypeHOTPKeystrokeDisabledMask TransferType = 0x02
	TransferTypeCCIDDisabledMask          TransferType = 0x04
)

Directories

Path Synopsis
cmd
token2-probe command
token2-probe records read-only observations from one manually labelled token.
token2-probe records read-only observations from one manually labelled token.
internal
protocol
Package protocol defines the Token2 commands shared by transport adapters.
Package protocol defines the Token2 commands shared by transport adapters.
Package piv implements TOKEN2 PIV extensions over a borrowed, selected PIV connection.
Package piv implements TOKEN2 PIV extensions over a borrowed, selected PIV connection.
transport
ctaphid
Package ctaphid accesses Token2 vendor commands over the CTAPHID protocol.
Package ctaphid accesses Token2 vendor commands over the CTAPHID protocol.
hid
Package hid accesses Token2 devices through USB HID feature reports.
Package hid accesses Token2 devices through USB HID feature reports.
pcsc
Package pcsc provides access to Token2 devices over PC/SC.
Package pcsc provides access to Token2 devices over PC/SC.

Jump to

Keyboard shortcuts

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