CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cbor_helpers.h File Reference
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Classes

struct  cdc::mod_fido2::cbor_writer_t
struct  cdc::mod_fido2::cbor_reader_t
struct  cdc::mod_fido2::cbor_item_t

Namespaces

namespace  cdc
namespace  cdc::mod_fido2

Macros

#define CBOR_UNSIGNED   0
#define CBOR_NEGATIVE   1
#define CBOR_BYTES   2
#define CBOR_TEXT   3
#define CBOR_ARRAY   4
#define CBOR_MAP   5
#define CBOR_TAG   6
#define CBOR_SIMPLE   7
#define CBOR_FALSE   0xF4
#define CBOR_TRUE   0xF5
#define CBOR_NULL   0xF6
#define CBOR_UNDEFINED   0xF7

Functions

void cdc::mod_fido2::cbor_writer_init (cbor_writer_t *w, uint8_t *buffer, size_t size)
size_t cdc::mod_fido2::cbor_writer_length (const cbor_writer_t *w)
bool cdc::mod_fido2::cbor_writer_error (const cbor_writer_t *w)
void cdc::mod_fido2::cbor_encode_uint (cbor_writer_t *w, uint64_t value)
void cdc::mod_fido2::cbor_encode_int (cbor_writer_t *w, int64_t value)
void cdc::mod_fido2::cbor_encode_bytes (cbor_writer_t *w, const uint8_t *data, size_t len)
void cdc::mod_fido2::cbor_encode_text (cbor_writer_t *w, const char *str)
void cdc::mod_fido2::cbor_encode_text_len (cbor_writer_t *w, const char *str, size_t len)
void cdc::mod_fido2::cbor_encode_bool (cbor_writer_t *w, bool value)
void cdc::mod_fido2::cbor_encode_null (cbor_writer_t *w)
void cdc::mod_fido2::cbor_encode_array (cbor_writer_t *w, size_t count)
void cdc::mod_fido2::cbor_encode_map (cbor_writer_t *w, size_t count)
void cdc::mod_fido2::cbor_encode_cose_key_p256 (cbor_writer_t *w, const uint8_t *x, const uint8_t *y)
void cdc::mod_fido2::cbor_encode_cose_key_ed25519 (cbor_writer_t *w, const uint8_t *pubkey)
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.
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.
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.

Macro Definition Documentation

◆ CBOR_ARRAY

#define CBOR_ARRAY   4

Definition at line 21 of file cbor_helpers.h.

Referenced by cbor_encode_array(), cbor_read_array(), and cbor_skip_item_impl().

◆ CBOR_BYTES

#define CBOR_BYTES   2

Definition at line 19 of file cbor_helpers.h.

Referenced by cbor_encode_bytes(), cbor_read_bytes(), and cbor_read_item().

◆ CBOR_FALSE

#define CBOR_FALSE   0xF4

Definition at line 27 of file cbor_helpers.h.

Referenced by cbor_encode_bool(), and cbor_read_bool().

◆ CBOR_MAP

#define CBOR_MAP   5

Definition at line 22 of file cbor_helpers.h.

Referenced by cbor_encode_map(), cbor_read_map(), and cbor_skip_item_impl().

◆ CBOR_NEGATIVE

#define CBOR_NEGATIVE   1

◆ CBOR_NULL

#define CBOR_NULL   0xF6

Definition at line 29 of file cbor_helpers.h.

Referenced by cbor_encode_null().

◆ CBOR_SIMPLE

#define CBOR_SIMPLE   7

Definition at line 24 of file cbor_helpers.h.

◆ CBOR_TAG

#define CBOR_TAG   6

Definition at line 23 of file cbor_helpers.h.

◆ CBOR_TEXT

#define CBOR_TEXT   3

◆ CBOR_TRUE

#define CBOR_TRUE   0xF5

Definition at line 28 of file cbor_helpers.h.

Referenced by cbor_encode_bool(), and cbor_read_bool().

◆ CBOR_UNDEFINED

#define CBOR_UNDEFINED   0xF7

Definition at line 30 of file cbor_helpers.h.

◆ CBOR_UNSIGNED

Function Documentation

◆ 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.

Parse COSE_Key from CBOR.

Parameters
rReader context (positioned at key map)
ktyOutput: key type (1=OKP, 2=EC2)
algOutput: algorithm
xOutput: X coordinate (32 bytes)
yOutput: Y coordinate (32 bytes, NULL for OKP)
Returns
true on success
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.

Read array header and return item count.

Returns
Item count, or -1 on error
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.

Read boolean 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.

Read byte string.

Parameters
rReader context
outOutput buffer
max_lenMaximum length
out_lenActual length
Returns
true on success
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).

Read signed integer.

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.

Read next CBOR item.

Parameters
rReader context
itemOutput item
Returns
true on success
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()

int cbor_read_map ( cbor_reader_t * r)

◆ 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.

Read text string.

Parameters
rReader context
outOutput buffer (null-terminated)
max_lenMaximum length including null
out_lenActual length excluding null
Returns
true on success
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.

Read 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.

Check if more data available.

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.

Check for read errors.

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.

Initialize CBOR reader.

Parameters
rReader context
dataInput data
sizeData size

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.

Peek at next item type without consuming.

Returns
Major type (0-7) or -1 on error
Parameters
rReader context.
Returns
Major type or -1 on error.

Definition at line 309 of file cbor_helpers.cpp.

◆ cbor_skip_item()

bool cbor_skip_item ( cbor_reader_t * r)