ExSwan.CBORUtils (exswan v0.1.0)

View Source

CBOR encoding and decoding utilities for WebAuthn objects.

This module provides functions to encode and decode CBOR data structures used in WebAuthn operations, particularly for attestation objects and credential public keys.

Summary

Functions

Decodes a CBOR-encoded attestation object.

Decodes a base64url-encoded CBOR attestation object.

Decodes a CBOR-encoded credential public key.

Decodes CBOR-encoded authenticator data extensions.

Encodes an attestation object map to CBOR format.

Encodes a credential public key map to CBOR format.

Encodes extensions map to CBOR format.

Validates that a decoded CBOR map contains expected WebAuthn fields.

Validates that a decoded public key contains expected COSE key fields.

Functions

decode_attestation_object(cbor_data)

@spec decode_attestation_object(binary()) :: {:ok, map()} | {:error, term()}

Decodes a CBOR-encoded attestation object.

Examples

iex> ExSwan.CBORUtils.decode_attestation_object(cbor_data)
{:ok, %{
  "fmt" => "packed",
  "authData" => <<...>>,
  "attStmt" => %{...}
}}

decode_attestation_object_from_base64(base64_data)

@spec decode_attestation_object_from_base64(binary()) ::
  {:ok, map()} | {:error, term()}

Decodes a base64url-encoded CBOR attestation object.

This function is used when the attestation object comes from client as base64url.

decode_credential_public_key(cbor_data)

@spec decode_credential_public_key(binary()) :: {:ok, map()} | {:error, term()}

Decodes a CBOR-encoded credential public key.

Returns a map containing the public key parameters according to RFC 8152 (CBOR Object Signing and Encryption).

Includes workaround for Firefox 117 EdDSA CBOR encoding bug.

Examples

iex> ExSwan.CBORUtils.decode_credential_public_key(cbor_data)
{:ok, %{
  1 => 2,    # kty (Key Type): EC2
  3 => -7,   # alg (Algorithm): ES256
  -1 => 1,   # crv (Curve): P-256
  -2 => x_coordinate,
  -3 => y_coordinate
}}

decode_extensions(cbor_data)

@spec decode_extensions(binary()) :: {:ok, map()} | {:error, term()}

Decodes CBOR-encoded authenticator data extensions.

encode_attestation_object(attestation_map)

@spec encode_attestation_object(map()) :: {:ok, binary()} | {:error, term()}

Encodes an attestation object map to CBOR format.

encode_credential_public_key(public_key_map)

@spec encode_credential_public_key(map()) :: {:ok, binary()} | {:error, term()}

Encodes a credential public key map to CBOR format.

encode_extensions(extensions_map)

@spec encode_extensions(map()) :: {:ok, binary()} | {:error, term()}

Encodes extensions map to CBOR format.

untag_decoded_cbor_data(data)

validate_attestation_object(decoded_map)

@spec validate_attestation_object(map()) :: :ok | {:error, atom()}

Validates that a decoded CBOR map contains expected WebAuthn fields.

Examples

iex> ExSwan.CBORUtils.validate_attestation_object(%{"fmt" => "packed", "authData" => <<...>>})
:ok

iex> ExSwan.CBORUtils.validate_attestation_object(%{"invalid" => "data"})
{:error, :missing_required_fields}

validate_public_key(decoded_map)

@spec validate_public_key(map()) :: :ok | {:error, atom()}

Validates that a decoded public key contains expected COSE key fields.