Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for key ID as integer or byte string #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions inc/edhoc/plaintext.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include "common/oscore_edhoc_error.h"

/* draft-ietf-lake-edhoc-22: 3.3.2. Representation of Byte String Identifiers */
#define ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL ( -24 )
#define ONE_BYTE_CBOR_ENCODED_INT_MAX_VAL ( 23 )

/**
* @brief Decodes id_cred to kid.
*
Expand Down
36 changes: 31 additions & 5 deletions src/edhoc/plaintext_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include "edhoc/retrieve_cred.h"
#include "edhoc/plaintext.h"
Expand Down Expand Up @@ -43,13 +44,33 @@ static enum err id_cred_x_encode(enum id_cred_x_label label, int algo,

switch (label) {
case kid:
{
//todo update that to v15
map._id_cred_x_map_kid_present = true;
map._id_cred_x_map_kid._id_cred_x_map_kid_choice =
_id_cred_x_map_kid_int;
map._id_cred_x_map_kid._id_cred_x_map_kid_int =
*((const int32_t *)id);

int32_t kid_as_int = 0;
const size_t kid_as_int_len =
(id_len < sizeof(kid_as_int)) ?
id_len : sizeof(kid_as_int);
memcpy(&kid_as_int, id, kid_as_int_len);

if (kid_as_int >= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL &&
kid_as_int <= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL) {
map._id_cred_x_map_kid._id_cred_x_map_kid_choice =
_id_cred_x_map_kid_int;
map._id_cred_x_map_kid._id_cred_x_map_kid_int =
*((const int32_t *)id);
} else {
map._id_cred_x_map_kid._id_cred_x_map_kid_choice =
_id_cred_x_map_kid_bstr;
map._id_cred_x_map_kid._id_cred_x_map_kid_bstr.value =
(const uint8_t *)id;
map._id_cred_x_map_kid._id_cred_x_map_kid_bstr.len =
id_len;
}

break;
}
case x5chain:
map._id_cred_x_map_x5chain_present = true;
map._id_cred_x_map_x5chain._id_cred_x_map_x5chain.value = id;
Expand Down Expand Up @@ -120,7 +141,12 @@ enum err plaintext_split(struct byte_array *ptxt, struct byte_array *id_cred_x,
kid, 0, p._plaintext_ID_CRED_x_bstr.value,
(uint32_t)p._plaintext_ID_CRED_x_bstr.len,
id_cred_x));

} else if (_plaintext_ID_CRED_x_bstr ==
p._plaintext_ID_CRED_x_choice) {
TRY(id_cred_x_encode(
kid, 0, p._plaintext_ID_CRED_x_bstr.value,
(uint32_t)p._plaintext_ID_CRED_x_bstr.len,
id_cred_x));
} else {
int _kid = p._plaintext_ID_CRED_x_int;
TRY(id_cred_x_encode(kid, 0, &_kid, 1, id_cred_x));
Expand Down
32 changes: 26 additions & 6 deletions src/edhoc/plaintext_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include <stdint.h>
#include <stddef.h>

#include "edhoc/retrieve_cred.h"
#include "edhoc/signature_or_mac_msg.h"
Expand All @@ -22,6 +23,7 @@

#include "cbor/edhoc_decode_id_cred_x.h"
#include "cbor/edhoc_encode_int_type.h"
#include "cbor/edhoc_encode_bstr_type.h"

enum err id_cred2kid(const struct byte_array *id_cred, struct byte_array *kid)
{
Expand All @@ -33,12 +35,30 @@ enum err id_cred2kid(const struct byte_array *id_cred, struct byte_array *kid)
0);

if (map._id_cred_x_map_kid_present) {
TRY_EXPECT(
cbor_encode_int_type_i(
kid->ptr, kid->len,
&map._id_cred_x_map_kid._id_cred_x_map_kid_int,
&payload_len_out),
ZCBOR_SUCCESS);
int32_t kid_as_int = 0;
const size_t kid_as_int_len =
(id_cred->len < sizeof(kid_as_int)) ?
id_cred->len : sizeof(kid_as_int);
memcpy(&kid_as_int, id_cred->ptr, kid_as_int_len);

if (_id_cred_x_map_kid_int == map._id_cred_x_map_kid._id_cred_x_map_kid_choice &&
kid_as_int >= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL &&
kid_as_int <= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL) {
TRY_EXPECT(
cbor_encode_int_type_i(
kid->ptr, kid->len,
&map._id_cred_x_map_kid._id_cred_x_map_kid_int,
&payload_len_out),
ZCBOR_SUCCESS);
} else {
TRY_EXPECT(
cbor_encode_bstr_type_b_str(
kid->ptr, kid->len,
&map._id_cred_x_map_kid._id_cred_x_map_kid_bstr,
&payload_len_out),
ZCBOR_SUCCESS);
}

kid->len = (uint32_t)payload_len_out;
} else {
kid->len = 0;
Expand Down
Loading