libfido2 Documentation

repository·main·Indexed 20 days ago

https://github.com/yubico/libfido2

A C library providing core functionality to communicate with FIDO (U2F and FIDO2) devices over USB or NFC. It is used to implement FIDO authentication and verify cryptographic signatures. The library includes a CLI for managing device state, configuring PINs, creating credentials, and performing FIDO2 assertions, as well as APIs for converting between libfido2 and OpenSSL public keys (EDDSA, ES256, ES384).

Tokens
29.2K
Snippets
97
Records
140
Agent score
64%

What's inside libfido2

  1. Overview of libfido2

    main

    libfido2 is a library and command-line toolset used to communicate with FIDO devices over USB or NFC. It enables developers to implement FIDO U2F (CTAP 1) and FIDO2 (CTAP 2) protocols, allowing for the verification of attestation and assertion signatures.

    Key features:

    • Support for FIDO U2F and FIDO2 protocols.
    • Communication via USB or NFC.
    • Tools for interacting with FIDO devices.
    • Available bindings for .NET, Go, Perl, and Rust.
  2. Understand libfido2 CLI command arguments

    main

    When using the libfido2 command-line examples, several placeholder terms are used to represent specific inputs:

    • <device>: The file system path or subsystem-specific identification string of a FIDO device.
    • <pin>, [oldpin]: Strings passed directly as command arguments.
    • <cred_id>: A file system path to a file containing a FIDO credential ID in binary representation.
    • <pubkey>: A file system path to a file containing a public key in PEM format.
    • <blobkey>: A file system path to a file containing a credential's associated CTAP 2.1 "largeBlob" symmetric key.
  3. Install libfido2 on Linux

    main

    Installation methods vary by distribution. On Ubuntu, you can use the standard repositories or Yubico's PPA for newer versions.

    Fedora 34 and later

    Use dnf to install the library, development headers, and tools.

    Ubuntu 20.04 (Focal) and later

    Use apt to install the library, development headers, documentation, and tools.

    Ubuntu 18.04 (Bionic)

    To get newer versions on Ubuntu 18.04, you must first add the Yubico PPA.

    Note for Linux users: You may need to configure a udev rule to allow non-root access to the FIDO device (HID access).

    # Fedora 34+
    $ sudo dnf install libfido2 libfido2-devel fido2-tools
    
    # Ubuntu 20.04+
    $ sudo apt install libfido2-1 libfido2-dev libfido2-doc fido2-tools
    
    # Ubuntu 18.04 (using Yubico PPA)
    $ sudo apt install software-properties-common
    $ sudo apt-add-repository ppa:yubico/stable
    $ sudo apt update
    $ sudo apt install libfido2-1 libfido2-dev libfido2-doc fido2-tools
  4. Build libfido2 from source

    main

    To build libfido2 on UNIX-like systems, use CMake and Make. Ensure pkg-config is installed or the PKG_CONFIG_PATH environment variable is set if necessary.

    $ cmake -B build
    $ make -C build
    $ sudo make -C build install
  5. Install libfido2 on macOS and Windows

    main

    macOS

    Install via Homebrew.

    Windows

    Download the appropriate artefacts (ARM, ARM64, Win32, or Win64) from the Yubico release page.

    Deployment Note: For safe usage, unpack artefacts in a trusted, non-writable directory with DLLs located alongside the executable. The dynamically linked artefacts are built with /MD and may require the corresponding Visual C++ runtime.

    # macOS
    $ brew install libfido2
  6. Manage FIDO2 authenticators with fido2-token

    main

    The fido2-token CLI tool is used to find and manage FIDO2 authenticators. It allows for tasks such as changing PINs, managing resident credentials, handling largeBlobs, and performing device resets.

    General Behavior:

    • If a tty is available, the tool uses it to prompt for PINs. Otherwise, it uses stdin.
    • The tool exits with 0 on success and 1 on error.
    • Use the -d flag to emit debugging output to stderr.
    fido2-token [options] [device]
  7. Manage resident credentials with the Credential Management API

    main

    The libfido2 Credential Management API allows you to list, inspect, modify, and remove resident credentials (also known as discoverable credentials in CTAP 2.1) on a FIDO2 authenticator.

    Note: Not all FIDO2 authenticators support credential management. You should check support using fido_cbor_info_new or fido_dev_supports_credman before attempting these operations.

    Key workflows include:

    • Retrieving Metadata: Use fido_credman_get_dev_metadata to populate a fido_credman_metadata_t object with information from the device.
    • Inspecting Capacity: Use fido_credman_rk_existing to see how many credentials are currently stored, and fido_credman_rk_remaining to see how many more can be created.
    • Managing Relying Parties (RP): Use fido_credman_get_dev_rp to list all RPs that have resident credentials on the device.
    • Managing Resident Keys (RK): Use fido_credman_get_dev_rk to retrieve the set of credentials belonging to a specific rp_id.
  8. Manage FIDO2 assertion lifecycles with fido_assert_t

    main

    In libfido2, a FIDO2 assertion is represented by the fido_assert_t type. An assertion is a collection of statements, where each statement maps a challenge and credential to a signature and various ancillary attributes.

    To use assertions, you must allocate them using fido_assert_new and ensure they are properly deallocated using fido_assert_free to prevent memory leaks. When using fido_assert_free, the pointer passed is set to NULL automatically.

    // Allocation
    fido_assert_t *assert = fido_assert_new();
    if (assert == NULL) {
        // Handle allocation error
    }
    
    // ... use assert ...
    
    // Deallocation
    fido_assert_free(&assert);
    // assert is now NULL
  9. Manage FIDO2 PIN/UV Auth tokens

    main

    The PIN/UV Auth token (PUAT) allows you to associate a token with a fido_dev_t device to minimize user interaction. By using a token, you can perform a sequence of operations that require User Verification (UV) or a PIN without prompting the user for the PIN repeatedly, or without having to cache the raw PIN in your application memory.

    Once a token is associated with a device, it takes precedence over regular PIN/UV authentication for all supported operations until it is cleared or expires. Note that the authenticator determines token validity; if a token expires or its permissions change, it is not automatically cleared from the device object.

    Key Lifecycle Rules:

    • The pointer returned by fido_dev_puat_ptr is valid until fido_dev_get_puat, fido_dev_set_puat, or fido_dev_free is called.
    • To clear an existing token, call fido_dev_set_puat with a NULL pointer or a length of 0.
  10. Customize libfido2 build with CMake options

    main

    You can customize the build using various CMake options. Note that options disabled by default are not officially supported. Some options require additional dependencies (e.g., hidapi or pcsc-lite).

    |===
    |*Option*           |*Description*                            |*Default*
    | BUILD_EXAMPLES    | Build example programs                  | ON
    | BUILD_MANPAGES    | Build man pages                         | ON
    | BUILD_SHARED_LIBS | Build a shared library                  | ON
    | BUILD_STATIC_LIBS | Build a static library                   | ON
    | BUILD_TOOLS       | Build auxiliary tools                   | ON
    | FUZZ              | Enable fuzzing instrumentation          | OFF
    | NFC_LINUX         | Enable netlink NFC support on Linux     | ON
    | USE_HIDAPI        | Use hidapi as the HID backend           | OFF
    | USE_PCSC          | Enable experimental PCSC support        | OFF
    | USE_WINHELLO      | Abstract Windows Hello as a FIDO device | ON
    |===|
  11. Set parameters of a FIDO2 credential

    main

    The fido_cred_set_* family of functions allows you to populate a fido_cred_t object with the necessary parameters for FIDO2 credential operations. This is used in two primary scenarios:

    1. FIDO2 Client: Preparing a credential object before calling fido_dev_make_cred to generate a new credential on a device.
    2. FIDO2 Server: Preparing a credential object for validation using fido_cred_verify.

    Most functions copy the provided data, so the caller does not need to maintain the original pointers after the function call.

    // See cred.c in the libfido2 repository for a concrete utilization example.
  12. How deterministic failure simulation works in libfido2 fuzzing

    main

    When -DFUZZ=ON is enabled, symbols listed in wrapped.sym are wrapped in the resulting shared object. These wrapper functions simulate failures using a deterministic RNG and probabilities defined in wrap.c.

    To use this functionality in a harness:

    1. Call prng_init() with a seed obtained from the fuzzer's corpus.
    2. For libFuzzer, to mutate only the seed part of the corpora, use the flag: -reduce_inputs=0 --fido-mutate=seed.
    // In your libFuzzer harness
    // prng_init(seed_from_corpus);