CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cbor_helpers.cpp File Reference

Minimal CBOR reader/writer helpers used by CTAP2 implementation. More...

#include "mod_fido2/cbor_helpers.h"
#include "cdc_log.h"
#include <string.h>

Go to the source code of this file.

Macros

#define CBOR_MAX_RECURSION_DEPTH   8
 Defensive limits for recursive container skipping.
#define CBOR_MAX_CONTAINER_SIZE   256

Functions

void cbor_writer_init (cbor_writer_t *w, uint8_t *buffer, size_t size)
 CBOR writer implementation.
size_t cbor_writer_length (const cbor_writer_t *w)
 Returns number of bytes written by CBOR writer.
bool cbor_writer_error (const cbor_writer_t *w)
 Returns whether writer encountered an error.
static void write_byte (cbor_writer_t *w, uint8_t b)
 Writes one byte to CBOR output buffer.
static void write_bytes (cbor_writer_t *w, const uint8_t *data, size_t len)
 Writes byte span to CBOR output buffer.
static void write_type_value (cbor_writer_t *w, uint8_t type, uint64_t value)
 Encodes CBOR major type and value header.
void cbor_encode_uint (cbor_writer_t *w, uint64_t value)
 Encodes CBOR unsigned integer.
void cbor_encode_int (cbor_writer_t *w, int64_t value)
 Encodes CBOR signed integer.
void cbor_encode_bytes (cbor_writer_t *w, const uint8_t *data, size_t len)
 Encodes CBOR byte-string.
void cbor_encode_text (cbor_writer_t *w, const char *str)
 Encodes CBOR text string.
void cbor_encode_text_len (cbor_writer_t *w, const char *str, size_t len)
 Encodes CBOR text string with explicit length.
void cbor_encode_bool (cbor_writer_t *w, bool value)
 Encodes CBOR boolean.
void cbor_encode_null (cbor_writer_t *w)
 Encodes CBOR null.
void cbor_encode_array (cbor_writer_t *w, size_t count)
 Encodes CBOR array header.
void cbor_encode_map (cbor_writer_t *w, size_t count)
 Encodes CBOR map header.
void cbor_encode_cose_key_p256 (cbor_writer_t *w, const uint8_t *x, const uint8_t *y)
 Encodes COSE P-256 public key map.
void cbor_encode_cose_key_ed25519 (cbor_writer_t *w, const uint8_t *pubkey)
 Encodes COSE Ed25519 public key map.
void cbor_reader_init (cbor_reader_t *r, const uint8_t *data, size_t size)
 CBOR reader implementation.
bool cbor_reader_error (const cbor_reader_t *r)
 Returns whether reader is in error state.
bool cbor_reader_available (const cbor_reader_t *r)
 Returns whether unread data remains.
int cbor_reader_peek_type (const cbor_reader_t *r)
 Peeks major type of next CBOR item.
static bool read_byte (cbor_reader_t *r, uint8_t *b)
 Reads one byte from CBOR reader.
static bool read_type_value (cbor_reader_t *r, uint8_t *type, uint64_t *value)
 Reads CBOR item header and decoded value.
bool cbor_read_item (cbor_reader_t *r, cbor_item_t *item)
 Reads next CBOR item metadata and optional inline payload pointer.
bool cbor_read_uint (cbor_reader_t *r, uint64_t *value)
 Reads CBOR unsigned integer.
bool cbor_read_int (cbor_reader_t *r, int64_t *value)
 Reads CBOR integer (positive or negative).
bool cbor_read_bytes (cbor_reader_t *r, uint8_t *out, size_t max_len, size_t *out_len)
 Reads CBOR byte-string into optional output buffer.
bool cbor_read_text (cbor_reader_t *r, char *out, size_t max_len, size_t *out_len)
 Reads CBOR text string into output buffer.
bool cbor_read_bool (cbor_reader_t *r, bool *value)
 Reads CBOR boolean simple value.
int cbor_read_map (cbor_reader_t *r)
 Reads CBOR map header and returns pair count.
int cbor_read_array (cbor_reader_t *r)
 Reads CBOR array header and returns element count.
static bool cbor_skip_item_impl (cbor_reader_t *r, uint8_t depth)
bool cbor_skip_item (cbor_reader_t *r)
 Skips one complete CBOR item including nested container content.
bool cbor_parse_cose_key (cbor_reader_t *r, int *kty, int *alg, uint8_t *x, uint8_t *y)
 Parses COSE key map and extracts key type, algorithm, and coordinates.

Variables

static const char * TAG = "CBOR"

Detailed Description

Minimal CBOR reader/writer helpers used by CTAP2 implementation.

Definition in file cbor_helpers.cpp.

Macro Definition Documentation

◆ CBOR_MAX_CONTAINER_SIZE

#define CBOR_MAX_CONTAINER_SIZE   256

Definition at line 561 of file cbor_helpers.cpp.

Referenced by cbor_skip_item_impl().

◆ CBOR_MAX_RECURSION_DEPTH

#define CBOR_MAX_RECURSION_DEPTH   8

Defensive limits for recursive container skipping.

Definition at line 560 of file cbor_helpers.cpp.

Referenced by cbor_skip_item_impl().

Function Documentation

◆ cbor_encode_array()

void cbor_encode_array ( cbor_writer_t * w,
size_t count )

Encodes CBOR array header.

Parameters
wWriter context.
countElement count.

Definition at line 197 of file cbor_helpers.cpp.

References CBOR_ARRAY, and write_type_value().

Referenced by ctap2_build_make_credential_response_packed(), encode_info_algorithms(), encode_info_extensions(), encode_info_pin_uv_auth_protocols(), encode_info_transports(), and encode_info_versions().

◆ cbor_encode_bool()

void cbor_encode_bool ( cbor_writer_t * w,
bool value )

Encodes CBOR boolean.

Parameters
wWriter context.
valueBoolean value.

Definition at line 180 of file cbor_helpers.cpp.

References CBOR_FALSE, CBOR_TRUE, and write_byte().

Referenced by ctap2_build_appid_extension(), and encode_info_options().

◆ cbor_encode_bytes()

void cbor_encode_bytes ( cbor_writer_t * w,
const uint8_t * data,
size_t len )

◆ cbor_encode_cose_key_ed25519()

void cbor_encode_cose_key_ed25519 ( cbor_writer_t * w,
const uint8_t * pubkey )

Encodes COSE Ed25519 public key map.

Parameters
wWriter context.
pubkeyPublic key bytes (32 bytes).

Definition at line 248 of file cbor_helpers.cpp.

References cbor_encode_bytes(), cbor_encode_int(), cbor_encode_map(), and cbor_encode_uint().

Referenced by cred_mgmt_encode_credential(), and ctap2_build_attested_cred().

◆ cbor_encode_cose_key_p256()

void cbor_encode_cose_key_p256 ( cbor_writer_t * w,
const uint8_t * x,
const uint8_t * y )

Encodes COSE P-256 public key map.

Parameters
wWriter context.
xX coordinate (32 bytes).
yY coordinate (32 bytes).

Definition at line 216 of file cbor_helpers.cpp.

References cbor_encode_bytes(), cbor_encode_int(), cbor_encode_map(), and cbor_encode_uint().

Referenced by cred_mgmt_encode_credential(), and ctap2_build_attested_cred().

◆ cbor_encode_int()

void cbor_encode_int ( cbor_writer_t * w,
int64_t value )

Encodes CBOR signed integer.

Parameters
wWriter context.
valueSigned value.

Definition at line 128 of file cbor_helpers.cpp.

References CBOR_NEGATIVE, CBOR_UNSIGNED, and write_type_value().

Referenced by cbor_encode_cose_key_ed25519(), cbor_encode_cose_key_p256(), client_pin_get_key_agreement(), ctap2_build_make_credential_response_packed(), and encode_info_algorithms().

◆ cbor_encode_map()

◆ cbor_encode_null()

void cbor_encode_null ( cbor_writer_t * w)

Encodes CBOR null.

Parameters
wWriter context.

Definition at line 188 of file cbor_helpers.cpp.

References CBOR_NULL, and write_byte().

◆ cbor_encode_text()

◆ cbor_encode_text_len()

void cbor_encode_text_len ( cbor_writer_t * w,
const char * str,
size_t len )

Encodes CBOR text string with explicit length.

Parameters
wWriter context.
strText pointer.
lenText length.

Definition at line 168 of file cbor_helpers.cpp.

References CBOR_TEXT, write_bytes(), and write_type_value().

◆ cbor_encode_uint()

◆ cbor_parse_cose_key()

bool cbor_parse_cose_key ( cbor_reader_t * r,
int * kty,
int * alg,
uint8_t * x,
uint8_t * y )

Parses COSE key map and extracts key type, algorithm, and coordinates.

Parameters
rReader context positioned at COSE key map.
ktyOutput key type.
algOutput algorithm identifier.
xOutput x-coordinate/public key bytes.
yOptional output y-coordinate bytes.
Returns
true on success, otherwise false.

Definition at line 613 of file cbor_helpers.cpp.

References cbor_read_bytes(), cbor_read_int(), cbor_read_map(), cbor_read_uint(), and cbor_skip_item().

◆ cbor_read_array()

int cbor_read_array ( cbor_reader_t * r)

Reads CBOR array header and returns element count.

Parameters
rReader context.
Returns
Element count or -1 on error.

Definition at line 549 of file cbor_helpers.cpp.

References CBOR_ARRAY, and cbor_read_item().

Referenced by ga_parse_allow_list(), and cdc::mod_fido2::parse_pubkey_cred_params().

◆ cbor_read_bool()

bool cbor_read_bool ( cbor_reader_t * r,
bool * value )

Reads CBOR boolean simple value.

Parameters
rReader context.
valueOutput boolean.
Returns
true on success.

Definition at line 513 of file cbor_helpers.cpp.

References CBOR_FALSE, CBOR_TRUE, and read_byte().

Referenced by ga_parse_options(), and cdc::mod_fido2::parse_options_map().

◆ cbor_read_bytes()

bool cbor_read_bytes ( cbor_reader_t * r,
uint8_t * out,
size_t max_len,
size_t * out_len )

Reads CBOR byte-string into optional output buffer.

Parameters
rReader context.
outOptional output data buffer.
max_lenOutput capacity.
out_lenOptional output original item length.
Returns
true on success.

Definition at line 462 of file cbor_helpers.cpp.

References CBOR_BYTES, and cbor_read_item().

Referenced by cbor_parse_cose_key(), client_pin_get_pin_token(), client_pin_get_pin_uv_auth_token(), ctap2_cred_management(), ga_parse_allow_list_credential(), ga_parse_params(), cdc::mod_fido2::parse_make_credential_params(), and cdc::mod_fido2::parse_user_map().

◆ cbor_read_int()

bool cbor_read_int ( cbor_reader_t * r,
int64_t * value )

Reads CBOR integer (positive or negative).

Parameters
rReader context.
valueOutput value.
Returns
true on success.

Definition at line 439 of file cbor_helpers.cpp.

References CBOR_NEGATIVE, cbor_read_item(), and CBOR_UNSIGNED.

Referenced by cbor_parse_cose_key(), and cdc::mod_fido2::parse_pubkey_cred_params().

◆ cbor_read_item()

bool cbor_read_item ( cbor_reader_t * r,
cbor_item_t * item )

Reads next CBOR item metadata and optional inline payload pointer.

Parameters
rReader context.
itemOutput item descriptor.
Returns
true on success.

Definition at line 391 of file cbor_helpers.cpp.

References CBOR_BYTES, CBOR_TEXT, and read_type_value().

Referenced by cbor_read_array(), cbor_read_bytes(), cbor_read_int(), cbor_read_map(), cbor_read_text(), cbor_read_uint(), cbor_skip_item_impl(), client_pin_get_pin_token(), and client_pin_get_pin_uv_auth_token().

◆ cbor_read_map()

◆ cbor_read_text()

bool cbor_read_text ( cbor_reader_t * r,
char * out,
size_t max_len,
size_t * out_len )

Reads CBOR text string into output buffer.

Parameters
rReader context.
outOutput text buffer.
max_lenOutput capacity including terminator.
out_lenOptional output original text length.
Returns
true on success.

Definition at line 486 of file cbor_helpers.cpp.

References cbor_read_item(), and CBOR_TEXT.

Referenced by client_pin_get_pin_uv_auth_token(), ctap2_cred_management(), ga_parse_allow_list_credential(), ga_parse_extensions(), ga_parse_options(), ga_parse_params(), cdc::mod_fido2::parse_extensions_map(), cdc::mod_fido2::parse_options_map(), cdc::mod_fido2::parse_pubkey_cred_params(), cdc::mod_fido2::parse_rp_map(), and cdc::mod_fido2::parse_user_map().

◆ cbor_read_uint()

bool cbor_read_uint ( cbor_reader_t * r,
uint64_t * value )

Reads CBOR unsigned integer.

Parameters
rReader context.
valueOutput value.
Returns
true on success.

Definition at line 422 of file cbor_helpers.cpp.

References cbor_read_item(), and CBOR_UNSIGNED.

Referenced by cbor_parse_cose_key(), client_pin_get_pin_token(), client_pin_get_pin_uv_auth_token(), ctap2_client_pin(), ctap2_cred_management(), ga_parse_params(), cdc::mod_fido2::parse_extensions_map(), and cdc::mod_fido2::parse_make_credential_params().

◆ cbor_reader_available()

bool cbor_reader_available ( const cbor_reader_t * r)

Returns whether unread data remains.

Parameters
rReader context.
Returns
true if more bytes can be read.

Definition at line 300 of file cbor_helpers.cpp.

◆ cbor_reader_error()

bool cbor_reader_error ( const cbor_reader_t * r)

Returns whether reader is in error state.

Parameters
rReader context.
Returns
true when reader has failed.

Definition at line 291 of file cbor_helpers.cpp.

◆ cbor_reader_init()

void cbor_reader_init ( cbor_reader_t * r,
const uint8_t * data,
size_t size )

CBOR reader implementation.

Initializes CBOR reader state.

Parameters
rReader context.
dataInput buffer.
sizeInput size.

Definition at line 279 of file cbor_helpers.cpp.

Referenced by client_pin_get_pin_token(), client_pin_get_pin_uv_auth_token(), ctap2_client_pin(), ctap2_cred_management(), ga_parse_params(), and cdc::mod_fido2::parse_make_credential_params().

◆ cbor_reader_peek_type()

int cbor_reader_peek_type ( const cbor_reader_t * r)

Peeks major type of next CBOR item.

Parameters
rReader context.
Returns
Major type or -1 on error.

Definition at line 309 of file cbor_helpers.cpp.

◆ cbor_skip_item()

◆ cbor_skip_item_impl()

bool cbor_skip_item_impl ( cbor_reader_t * r,
uint8_t depth )
static

◆ cbor_writer_error()

bool cbor_writer_error ( const cbor_writer_t * w)

Returns whether writer encountered an error.

Parameters
wWriter context.
Returns
true if writer is in error state.

Definition at line 41 of file cbor_helpers.cpp.

Referenced by ctap2_build_appid_extension(), ctap2_build_attested_cred(), ctap2_build_cred_protect_extension(), ctap2_build_make_credential_response_packed(), ctap2_cred_management(), ctap2_get_info(), and ga_build_response().

◆ cbor_writer_init()

void cbor_writer_init ( cbor_writer_t * w,
uint8_t * buffer,
size_t size )

◆ cbor_writer_length()

◆ read_byte()

bool read_byte ( cbor_reader_t * r,
uint8_t * b )
static

Reads one byte from CBOR reader.

Parameters
rReader context.
bOutput byte.
Returns
true on success.

Definition at line 320 of file cbor_helpers.cpp.

References LOG_E, and TAG.

Referenced by cbor_read_bool(), and read_type_value().

◆ read_type_value()

bool read_type_value ( cbor_reader_t * r,
uint8_t * type,
uint64_t * value )
static

Reads CBOR item header and decoded value.

Parameters
rReader context.
typeOutput major type.
valueOutput decoded additional value.
Returns
true on success.

Definition at line 338 of file cbor_helpers.cpp.

References LOG_E, read_byte(), and TAG.

Referenced by cbor_read_item().

◆ write_byte()

void write_byte ( cbor_writer_t * w,
uint8_t b )
static

Writes one byte to CBOR output buffer.

Parameters
wWriter context.
bByte value.

Definition at line 50 of file cbor_helpers.cpp.

References LOG_E, and TAG.

Referenced by cbor_encode_bool(), cbor_encode_null(), and write_type_value().

◆ write_bytes()

void write_bytes ( cbor_writer_t * w,
const uint8_t * data,
size_t len )
static

Writes byte span to CBOR output buffer.

Parameters
wWriter context.
dataInput byte span.
lenNumber of bytes to write.

Definition at line 66 of file cbor_helpers.cpp.

References LOG_E, and TAG.

Referenced by cbor_encode_bytes(), cbor_encode_text(), and cbor_encode_text_len().

◆ write_type_value()

void write_type_value ( cbor_writer_t * w,
uint8_t type,
uint64_t value )
static

Encodes CBOR major type and value header.

Parameters
wWriter context.
typeCBOR major type.
valueAdditional value.

Definition at line 83 of file cbor_helpers.cpp.

References write_byte().

Referenced by cbor_encode_array(), cbor_encode_bytes(), cbor_encode_int(), cbor_encode_map(), cbor_encode_text(), cbor_encode_text_len(), and cbor_encode_uint().

Variable Documentation

◆ TAG

const char* TAG = "CBOR"
static

Definition at line 10 of file cbor_helpers.cpp.