Skip to content

Commit

Permalink
Encode empty prot params as an empty bstr (#268)
Browse files Browse the repository at this point in the history
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
  • Loading branch information
laurencelundblade and Laurence Lundblade authored Nov 4, 2023
1 parent 58e70ca commit 437a35b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
26 changes: 19 additions & 7 deletions src/t_cose_parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ t_cose_headers_encode(QCBOREncodeContext *cbor_encoder,
*/

enum t_cose_err_t return_value;
bool protected_present;
const struct t_cose_parameter *p_param;

// TODO: allow disabling this check to save object code
if(param_dup_detect(parameters)) {
Expand All @@ -695,14 +697,24 @@ t_cose_headers_encode(QCBOREncodeContext *cbor_encoder,
}

/* --- Protected Headers --- */
QCBOREncode_BstrWrap(cbor_encoder);
return_value = t_cose_params_encode(cbor_encoder,
parameters,
true);
if(return_value != T_COSE_SUCCESS) {
goto Done;
protected_present = false;
for(p_param = parameters; p_param != NULL; p_param = p_param->next) {
if(p_param->in_protected) {
protected_present = true;
}
}
if(!protected_present) {
QCBOREncode_AddBytes(cbor_encoder, NULL_Q_USEFUL_BUF_C);
} else {
QCBOREncode_BstrWrap(cbor_encoder);
return_value = t_cose_params_encode(cbor_encoder,
parameters,
true);
if(return_value != T_COSE_SUCCESS) {
goto Done;
}
QCBOREncode_CloseBstrWrap2(cbor_encoder, false, protected_parameters);
}
QCBOREncode_CloseBstrWrap2(cbor_encoder, false, protected_parameters);


/* --- Unprotected Parameters --- */
Expand Down
16 changes: 8 additions & 8 deletions test/t_cose_param_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ static const uint8_t crit_custom_float_param_encoded_cbor[] = {
0xEB, 0x85, 0x1F, 0x02, 0x81, 0x18, 0x2C, 0xA0};

static const uint8_t unprot_bstr_param_encoded_cbor[] = {
0x41, 0xA0, 0xA1, 0x18, 0x21, 0x43, 0x01, 0x02, 0x03};
0x40, 0xA1, 0x18, 0x21, 0x43, 0x01, 0x02, 0x03};

static const uint8_t b1[] = {0x01, 0x02, 0x03};

Expand Down Expand Up @@ -386,22 +386,22 @@ static const uint8_t alg_id_param_encoded_cbor[] = {
0x43, 0xA1, 0x01, 0x26, 0xA0};

static const uint8_t uint_ct_encoded_cbor[] = {
0x41, 0xA0, 0xA1, 0x03, 0x18, 0x2A};
0x40, 0xA1, 0x03, 0x18, 0x2A};

static const uint8_t tstr_ct_param_encoded_cbor[] = {
0x41, 0xA0, 0xA1, 0x03, 0x6A, 0x74, 0x65, 0x78, 0x74, 0x2F,
0x40, 0xA1, 0x03, 0x6A, 0x74, 0x65, 0x78, 0x74, 0x2F,
0x70, 0x6C, 0x61, 0x69, 0x6E};

static const uint8_t kid_param_encoded_cbor[] = {
0x41, 0xA0, 0xA1, 0x04, 0x4D, 0x74, 0x68, 0x69, 0x73, 0x2D,
0x40, 0xA1, 0x04, 0x4D, 0x74, 0x68, 0x69, 0x73, 0x2D,
0x69, 0x73, 0x2D, 0x61, 0x2D, 0x6B, 0x69, 0x64};

static const uint8_t iv_param_encoded_cbor[] = {
0x41, 0xA0, 0xA1, 0x05, 0x48, 0x69, 0x76, 0x69, 0x76, 0x69,
0x40, 0xA1, 0x05, 0x48, 0x69, 0x76, 0x69, 0x76, 0x69,
0x76, 0x69, 0x76};

static const uint8_t partial_iv_encoded_cbor[] = {
0x41, 0xA0, 0xA1, 0x06, 0x43, 0x70, 0x69, 0x76};
0x40, 0xA1, 0x06, 0x43, 0x70, 0x69, 0x76};

static const uint8_t not_well_formed_crit_encoded_cbor[] = {
0x47, 0xA2, 0x18, 0x2c, 0x00, 0x02, 0x81, 0xff, 0xA0};
Expand Down Expand Up @@ -432,15 +432,15 @@ static const uint8_t too_many_tstr_in_crit_encoded_cbor[] = {

/* */
static const uint8_t iv_and_partial_iv_encoded_cbor[] = {
0x41, 0xA0, 0xA2, 0x05, 0x48, 0x69, 0x76, 0x69, 0x76, 0x69,
0x40, 0xA2, 0x05, 0x48, 0x69, 0x76, 0x69, 0x76, 0x69,
0x76, 0x69, 0x76, 0x06, 0x41, 0xDD};

static const uint8_t crit_alg_id_encoded_cbor[] = {
0x46, 0xA2, 0x01, 0x26, 0x02, 0x81, 0x01, 0xA0};

static const uint8_t empty_preferred_encoded_cbor[] = {0x40, 0xA0};

static const uint8_t empty_alt_encoded_cbor[] = {0x41, 0xA0, 0xA0};
static const uint8_t empty_alt_encoded_cbor[] = {0x40, 0xA0};

#if FIXES_FOR_INDEF_LEN
static const uint8_t empty_preferred_indef[] = {0x5f, 0xff, 0xbf, 0xff};
Expand Down

0 comments on commit 437a35b

Please sign in to comment.