fido

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: BSD-3-Clause Imports: 13 Imported by: 3 modules, 3 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

fido

Go Reference License CI

Reaches a FIDO security key on Linux, over hidraw, in pure Go with CGO_ENABLED=0.

k, err := fido.Open(ctx)   // finds the key, speaks the CTAPHID handshake
defer k.Close()
fmt.Println(k)             // YubiKey FIDO (CTAPHID v2, firmware 5.7.4, wink, ctap2, ctap1)

This is a transport and nothing else. The protocol — CTAPHID, CTAP2, ClientPIN, credentials — lives in go-authn/fido and knows about no operating system at all. The macOS twin is go-macos/fido.

No libudev, and therefore no cgo

The reference implementation enumerates with libudev, which is C. It does not have to: udev is used only to list the hidraw nodes, and /sys/class/hidraw already lists them. Everything else libfido2 wants, it parses by hand out of the sysfs uevent file — HID_ID=bus:vendor:product, HID_NAME= — so this package reads the same text and stays pure Go.

The FIDO test is the same on every platform: usage page 0xF1D0, read from the report descriptor through HIDIOCGRDESCSIZE and HIDIOCGRDESC.

The ioctl number is not the same on every architecture

That is not a detail this package inherited; it is one it nearly got wrong. asm-generic/ioctl.h wraps _IOC_SIZEBITS in an #ifndef, and powerpc and mips override it — 13 bits instead of 14, which moves the direction field down one bit and changes every request number:

HIDIOCGRDESCSIZE HIDIOCGRDESC
generic 0x80044801 0x90044802
ppc64, ppc64le, mips* 0x40044801 0x50044802

The kernel answers a wrong request with ENOTTY, which this package would have reported as not a security key — a real key found, filtered out, and never mentioned. Cross-compiling all eleven architectures does not catch it: the arithmetic compiles perfectly and produces the wrong number. Reading the architectures own header does. Both encodings are pinned by a test, and the width is build-tagged.

The trap that does not exist on macOS

A hidraw write carries one byte more than the report. A read does not.

Linux expects a leading report-id byte on the way out — zero, for CTAPHID — and returns the report without it. On macOS the report id is a separate argument to IOKit, so a transport ported across without noticing sends every report shifted by one byte. A key answers that with silence, not with an error.

What it refuses to confuse

  • A key you may not open is not an absent key. /dev/hidraw* is root-only on a stock system; libfido2 ships udev/70-u2f.rules to change that. ErrPermission is separate from ErrNoKey and names the rule, because the two send a person to different places — a cable, or a file in /etc/udev.
  • One unreadable node does not hide the next. A machine can hold a hidraw node this user may not touch beside the key they may. Abandoning the scan at the first refusal would report no key at all.
  • A report length read from a device does not size an allocation. A descriptor item is four bytes wide, so a report count of 0x80000000 is a two-gigabyte make() away from whoever plugged something in. The value is bounded. A test caught this: the first version only checked it was positive.
  • A FIDO collection behind another one is still found. libfido2 overwrites a single variable as it walks, so it judges by the last usage page it saw; this asks whether any is 0xF1D0, which cannot lose a device and gives the same answer on the single-collection descriptors real keys emit.
  • A blocking read is not used. A blocking read on hidraw parks a goroutine in the kernel until somebody touches the key, and neither a cancelled context nor Close gets it back. The descriptor is opened O_NONBLOCK and polled, so cancellation stays in this package's hands.

What is proven, and what is not

The wire half runs, against a device the kernel makes. /dev/uhid is the kernel's userspace HID transport: hand it a report descriptor and it produces a real hidraw node, indistinguishable from a plugged device to everything above it. So the sysfs walk against a real /sys, the ioctl numbers on the running architecture, the usage-page filter, both directions of the wire and the poll loop are all exercised — with no security key and nobody to touch one.

found     go-gnulinux fido test device (1050:0402) at /dev/hidraw0
handshake CTAPHID v2, firmware 5.7.4, wink, ctap2, ctap1
ping      came back byte for byte

It has run on aarch64 (Linux 6.11, a local VM) and on amd64 (the CI runner, on every pull request). The test skips where uhid is unavailable, and the CI step raises a warning naming the kernel when that happens, because a skip that looks like a pass is worse than no test.

⭐ And it corrected something written here. The report reaches the device as 65 bytes whose first is the report id, not 64. hidraw's write() ABI is [report id][payload] — which is what this package sends and what libfido2 sends — and what differs is who strips it: usbhid drops the id before the wire, so a real key sees 64; uhid is a raw transport and passes down what the HID core held. The code was right and the description of what a device sees was not. The fake strips it and asserts the byte was the id rather than data, so the distinction cannot quietly become a fudge.

The parser has an external witness. It is checked against 17 real descriptors read off a live machine's HID devices, with IOKit's own PrimaryUsagePage as the answer — bytes this project did not write and a verdict it did not compute. One- and two-byte item widths both appear, so an item mis-sized by one byte shifts every page and fails the comparison. None of the 17 is a security key, which is the negative control.

Everything that runs without a key is covered to 100%.

Not proven: a real security key. No key has been plugged into a machine running this. What a kernel-made device cannot show is the behaviour of an actual authenticator — a key that takes its time, that answers KEEPALIVE while somebody decides whether to touch it, that is unplugged mid-exchange. That still wants one run with a key in a Linux machine.

Documentation

Overview

Package fido reaches a FIDO security key on Linux, over hidraw, in pure Go with CGO_ENABLED=0.

It is a transport and nothing else. The protocol — CTAPHID, CTAP2, ClientPIN, credentials — lives in github.com/go-authn/fido, which knows nothing about any operating system. What is here is the part that cannot be portable: finding the device and moving 64-byte reports.

k, err := fido.Open(ctx)   // finds the key, speaks the handshake
defer k.Close()
fmt.Println(k)             // YubiKey FIDO (CTAPHID v2, firmware 5.7.4, ...)

No libudev

The reference implementation enumerates with libudev, which is a C library and would cost cgo. It does not have to: libudev is used only to LIST the hidraw nodes, and /sys/class/hidraw already lists them. Everything else libfido2 reads, it reads out of the sysfs "uevent" file by hand — so this package does the same, and stays pure Go.

The permission is the obstacle, not the API

/dev/hidraw* belongs to root on a stock system. libfido2 ships a udev rule (70-u2f.rules) that hands the console user access to known keys. Without it a plugged-in key is found and then refuses to open, so ErrPermission says exactly that rather than letting it look like an absent key.

Index

Constants

View Source
const ReportSize = 64

ReportSize is the CTAPHID report size, which every FIDO device uses.

View Source
const UsagePageFIDO = 0xF1D0

UsagePageFIDO is the HID usage page that marks a device as speaking CTAP. It is the same number on every operating system; it is how a security key is told apart from a keyboard on the same bus.

Variables

View Source
var (
	// ErrNoKey is returned when nothing on this machine speaks CTAP.
	ErrNoKey = errors.New("fido: no security key is attached")

	// ErrPermission is returned when a key was FOUND and could not be opened.
	//
	// It is deliberately distinct from ErrNoKey. On a stock system
	// /dev/hidraw* is root-only, and a caller that reported "no key" here
	// would send a person to check a cable when the answer is a udev rule.
	ErrPermission = errors.New("fido: a security key is attached but this user cannot open it; " +
		"install a udev rule for /dev/hidraw* (see libfido2's udev/70-u2f.rules)")
)

Functions

func Open

func Open(ctx context.Context) (*authn.Key, error)

Open finds a key and speaks the CTAPHID handshake to it.

func OpenDevice

func OpenDevice(d Device) (authn.Transport, error)

OpenDevice opens one particular key, for a machine with more than one.

func Transport

func Transport() (authn.Transport, error)

Transport opens the first attached security key.

It is the seam github.com/go-authn/fido asks for: everything above this -- the handshake, CTAP2, ClientPIN -- is portable and lives there.

Types

type Device

type Device struct {
	// Path is the node to open, e.g. "/dev/hidraw3".
	Path string
	// Node is the sysfs name, e.g. "hidraw3".
	Node string
	// Bus is the HID bus number from HID_ID (3 is USB, 5 is Bluetooth).
	Bus uint16
	// VendorID and ProductID identify the model.
	VendorID, ProductID uint16
	// Name is what the device calls itself, for a person to read.
	Name string
	// Phys is the physical port the key sits in (HID_PHYS, e.g.
	// "usb-0000:00:14.0-3/input0"). Unlike Path, which is numbered in the order
	// the kernel met the devices, it does not change when another key is
	// plugged in first, so it is what to give back as a stable identifier.
	Phys string
	// Serial is the device's unique string (HID_UNIQ), empty when it has none.
	Serial string
}

Device is one hidraw node that speaks CTAP.

func Devices

func Devices() ([]Device, error)

Devices lists the security keys attached to this machine.

func (Device) String

func (d Device) String() string

String names the device the way an error message should.

Jump to

Keyboard shortcuts

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