Loading...
Searching...
No Matches
oscore_context.h
Go to the documentation of this file.
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* Copyright (c) 2018, SICS, RISE AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#ifndef _OSCORE_CONTEXT_H
#define _OSCORE_CONTEXT_H
#include "coap3/coap_internal.h"
#include "coap3/coap_uthash_internal.h"
#include <stdint.h>
#define CONTEXT_KEY_LEN 16
#define TOKEN_SEQ_NUM 2 /* to be set by application */
#define EP_CTX_NUM 10 /* to be set by application */
#define CONTEXT_INIT_VECT_LEN 13
#define CONTEXT_SEQ_LEN sizeof(uint64_t)
#define ED25519_PRIVATE_KEY_LEN 32
#define ED25519_PUBLIC_KEY_LEN 32
#define ED25519_SEED_LEN 32
#define ED25519_SIGNATURE_LEN 64
#define OSCORE_SEQ_MAX (((uint64_t)1 << 40) - 1)
typedef enum {
OSCORE_MODE_SINGLE = 0,
OSCORE_MODE_GROUP,
OSCORE_MODE_PAIRWISE
} oscore_mode_t;
typedef struct oscore_sender_ctx_t oscore_sender_ctx_t;
typedef struct oscore_recipient_ctx_t oscore_recipient_ctx_t;
typedef struct oscore_association_t oscore_association_t;
struct oscore_ctx_t {
struct oscore_ctx_t *next;
coap_bin_const_t *master_secret;
coap_bin_const_t *master_salt;
coap_bin_const_t *common_iv;
coap_bin_const_t *id_context;
oscore_sender_ctx_t *sender_context;
oscore_recipient_ctx_t *recipient_chain;
cose_alg_t aead_alg;
cose_hkdf_alg_t hkdf_alg;
oscore_mode_t mode;
uint8_t rfc8613_b_1_2;
uint8_t rfc8613_b_2;
uint32_t ssn_freq;
uint32_t replay_window_size;
coap_oscore_save_seq_num_t save_seq_num_func;
void *save_seq_num_func_param;
};
struct oscore_sender_ctx_t {
uint64_t seq;
uint64_t next_seq;
coap_bin_const_t *sender_key;
coap_bin_const_t *sender_id;
};
struct oscore_recipient_ctx_t {
/* This field allows recipient chaining */
oscore_recipient_ctx_t *next_recipient;
oscore_ctx_t *osc_ctx;
uint64_t last_seq;
/* uint64_t highest_seq; */
uint64_t sliding_window;
uint64_t rollback_sliding_window;
uint64_t rollback_last_seq;
coap_bin_const_t *recipient_key;
coap_bin_const_t *recipient_id;
uint8_t echo_value[8];
uint8_t initial_state;
};
#define OSCORE_ASSOCIATIONS_ADD(r, obj) \
HASH_ADD(hh, (r), token->s[0], (obj)->token->length, (obj))
#define OSCORE_ASSOCIATIONS_DELETE(r, obj) HASH_DELETE(hh, (r), (obj))
#define OSCORE_ASSOCIATIONS_ITER(r, tmp) \
oscore_associations_t *tmp, *rtmp; \
HASH_ITER (hh, (r), tmp, rtmp)
#define OSCORE_ASSOCIATIONS_ITER_SAFE(e, el, rtmp) \
for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
#define OSCORE_ASSOCIATIONS_FIND(r, k, res) \
{ HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); }
struct oscore_association_t {
UT_hash_handle hh;
oscore_recipient_ctx_t *recipient_ctx;
coap_pdu_t *sent_pdu;
coap_bin_const_t *token;
coap_bin_const_t *aad;
coap_bin_const_t *nonce;
coap_bin_const_t *partial_iv;
coap_tick_t last_seen;
uint8_t is_observe;
};
oscore_ctx_t *oscore_derive_ctx(coap_context_t *c_context,
coap_oscore_conf_t *oscore_conf);
oscore_ctx_t *oscore_duplicate_ctx(coap_context_t *c_context,
oscore_ctx_t *o_osc_ctx,
coap_bin_const_t *sender_id,
coap_bin_const_t *recipient_id,
coap_bin_const_t *id_context);
void oscore_update_ctx(oscore_ctx_t *osc_ctx, coap_bin_const_t *id_context);
void oscore_free_context(oscore_ctx_t *osc_ctx);
void oscore_free_contexts(coap_context_t *c_context);
int oscore_remove_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx);
oscore_recipient_ctx_t *oscore_add_recipient(oscore_ctx_t *ctx,
coap_bin_const_t *rid,
uint32_t break_key);
int oscore_delete_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid);
uint8_t oscore_bytes_equal(uint8_t *a_ptr,
uint8_t a_len,
uint8_t *b_ptr,
uint8_t b_len);
void oscore_convert_to_hex(const uint8_t *src,
size_t src_len,
char *dest,
size_t dst_len);
void oscore_log_hex_value(coap_log_t level,
const char *name,
coap_bin_const_t *value);
void oscore_log_int_value(coap_log_t level, const char *name, int value);
void oscore_log_char_value(coap_log_t level, const char *name,
const char *value);
oscore_ctx_t *oscore_find_context(const coap_context_t *c_context,
const coap_bin_const_t rcpkey_id,
const coap_bin_const_t *ctxkey_id,
uint8_t *oscore_r2,
oscore_recipient_ctx_t **recipient_ctx);
void oscore_free_association(oscore_association_t *association);
int oscore_new_association(coap_session_t *session,
coap_pdu_t *sent_pdu,
coap_bin_const_t *token,
oscore_recipient_ctx_t *recipient_ctx,
coap_bin_const_t *aad,
coap_bin_const_t *nonce,
coap_bin_const_t *partial_iv,
int is_observe);
oscore_association_t *oscore_find_association(coap_session_t *session,
coap_bin_const_t *token);
int oscore_delete_association(coap_session_t *session,
oscore_association_t *association);
void oscore_delete_server_associations(coap_session_t *session);
int oscore_derive_keystream(oscore_ctx_t *osc_ctx,
cose_encrypt0_t *code,
uint8_t coap_request,
coap_bin_const_t *sender_key,
coap_bin_const_t *id_context,
size_t cs_size,
uint8_t *keystream,
size_t keystream_size);
#endif /* _OSCORE_CONTEXT_H */
coap_internal.h
Pulls together all the internal only header files.
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:143
coap_log_t
coap_log_t
Logging type.
Definition coap_debug.h:50
cose_hkdf_alg_t
cose_hkdf_alg_t
Definition oscore_cose.h:165
cose_alg_t
cose_alg_t
Definition oscore_cose.h:126
oscore_convert_to_hex
void oscore_convert_to_hex(const uint8_t *src, size_t src_len, char *dest, size_t dst_len)
Definition oscore_context.c:273
oscore_delete_association
int oscore_delete_association(coap_session_t *session, oscore_association_t *association)
Definition oscore_context.c:756
oscore_add_recipient
oscore_recipient_ctx_t * oscore_add_recipient(oscore_ctx_t *ctx, coap_bin_const_t *rid, uint32_t break_key)
oscore_add_recipient - add in recipient information
Definition oscore_context.c:600
oscore_delete_recipient
int oscore_delete_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid)
Definition oscore_context.c:654
oscore_mode_t
oscore_mode_t
Definition oscore_context.h:74
oscore_update_ctx
void oscore_update_ctx(oscore_ctx_t *osc_ctx, coap_bin_const_t *id_context)
oscore_update_ctx - update a osc_ctx with a new id_context
Definition oscore_context.c:387
oscore_free_context
void oscore_free_context(oscore_ctx_t *osc_ctx)
Definition oscore_context.c:125
oscore_derive_ctx
oscore_ctx_t * oscore_derive_ctx(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf)
oscore_derive_ctx - derive a osc_ctx from oscore_conf information
Definition oscore_context.c:513
oscore_free_association
void oscore_free_association(oscore_association_t *association)
Definition oscore_context.c:674
oscore_new_association
int oscore_new_association(coap_session_t *session, coap_pdu_t *sent_pdu, coap_bin_const_t *token, oscore_recipient_ctx_t *recipient_ctx, coap_bin_const_t *aad, coap_bin_const_t *nonce, coap_bin_const_t *partial_iv, int is_observe)
Definition oscore_context.c:686
oscore_delete_server_associations
void oscore_delete_server_associations(coap_session_t *session)
Definition oscore_context.c:767
oscore_log_char_value
void oscore_log_char_value(coap_log_t level, const char *name, const char *value)
Definition oscore_context.c:268
oscore_free_contexts
void oscore_free_contexts(coap_context_t *c_context)
Definition oscore_context.c:149
oscore_log_hex_value
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
Definition oscore_context.c:235
oscore_log_int_value
void oscore_log_int_value(coap_log_t level, const char *name, int value)
Definition oscore_context.c:263
oscore_duplicate_ctx
oscore_ctx_t * oscore_duplicate_ctx(coap_context_t *c_context, oscore_ctx_t *o_osc_ctx, coap_bin_const_t *sender_id, coap_bin_const_t *recipient_id, coap_bin_const_t *id_context)
oscore_duplicate_ctx - duplicate a osc_ctx
Definition oscore_context.c:429
oscore_remove_context
int oscore_remove_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx)
Definition oscore_context.c:160
oscore_derive_keystream
int oscore_derive_keystream(oscore_ctx_t *osc_ctx, cose_encrypt0_t *code, uint8_t coap_request, coap_bin_const_t *sender_key, coap_bin_const_t *id_context, size_t cs_size, uint8_t *keystream, size_t keystream_size)
oscore_find_association
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
Definition oscore_context.c:748
oscore_find_context
oscore_ctx_t * oscore_find_context(const coap_context_t *c_context, const coap_bin_const_t rcpkey_id, const coap_bin_const_t *ctxkey_id, uint8_t *oscore_r2, oscore_recipient_ctx_t **recipient_ctx)
oscore_find_context - Locate recipient context (and hence OSCORE context)
Definition oscore_context.c:185
oscore_bytes_equal
uint8_t oscore_bytes_equal(uint8_t *a_ptr, uint8_t a_len, uint8_t *b_ptr, uint8_t b_len)
Definition oscore_context.c:87
OSCORE_MODE_SINGLE
@ OSCORE_MODE_SINGLE
Vanilla RFC8613 support.
Definition oscore_context.h:75
OSCORE_MODE_PAIRWISE
@ OSCORE_MODE_PAIRWISE
TODO draft-ietf-core-oscore-groupcomm.
Definition oscore_context.h:77
OSCORE_MODE_GROUP
@ OSCORE_MODE_GROUP
TODO draft-ietf-core-oscore-groupcomm.
Definition oscore_context.h:76
coap_oscore_save_seq_num_t
int(* coap_oscore_save_seq_num_t)(uint64_t sender_seq_num, void *param)
Definition of the function used to save the current Sender Sequence Number.
Definition coap_oscore.h:130
coap_bin_const_t
CoAP binary data definition with const data.
Definition coap_str.h:64
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition coap_net_internal.h:50
coap_oscore_conf_t
The structure used to hold the OSCORE configuration information.
Definition coap_oscore_internal.h:36
coap_pdu_t
structure for CoAP PDUs
Definition coap_pdu_internal.h:135
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition coap_session_internal.h:68
cose_encrypt0_t
Definition oscore_cose.h:197
oscore_association_t
Definition oscore_context.h:142
oscore_association_t::last_seen
coap_tick_t last_seen
Definition oscore_context.h:150
oscore_association_t::partial_iv
coap_bin_const_t * partial_iv
Definition oscore_context.h:149
oscore_association_t::sent_pdu
coap_pdu_t * sent_pdu
Definition oscore_context.h:145
oscore_association_t::hh
UT_hash_handle hh
Definition oscore_context.h:143
oscore_association_t::aad
coap_bin_const_t * aad
Definition oscore_context.h:147
oscore_association_t::nonce
coap_bin_const_t * nonce
Definition oscore_context.h:148
oscore_association_t::recipient_ctx
oscore_recipient_ctx_t * recipient_ctx
Definition oscore_context.h:144
oscore_association_t::is_observe
uint8_t is_observe
Definition oscore_context.h:151
oscore_association_t::token
coap_bin_const_t * token
Definition oscore_context.h:146
oscore_ctx_t
Definition oscore_context.h:84
oscore_ctx_t::master_secret
coap_bin_const_t * master_secret
Definition oscore_context.h:86
oscore_ctx_t::replay_window_size
uint32_t replay_window_size
Definition oscore_context.h:99
oscore_ctx_t::common_iv
coap_bin_const_t * common_iv
Derived from Master Secret, Master Salt, and ID Context.
Definition oscore_context.h:88
oscore_ctx_t::next
struct oscore_ctx_t * next
Definition oscore_context.h:85
oscore_ctx_t::mode
oscore_mode_t mode
Definition oscore_context.h:95
oscore_ctx_t::rfc8613_b_1_2
uint8_t rfc8613_b_1_2
1 if rfc8613 B.1.2 enabled else 0
Definition oscore_context.h:96
oscore_ctx_t::save_seq_num_func_param
void * save_seq_num_func_param
Passed to save_seq_num_func().
Definition oscore_context.h:102
oscore_ctx_t::sender_context
oscore_sender_ctx_t * sender_context
Definition oscore_context.h:91
oscore_ctx_t::hkdf_alg
cose_hkdf_alg_t hkdf_alg
Definition oscore_context.h:94
oscore_ctx_t::aead_alg
cose_alg_t aead_alg
Definition oscore_context.h:93
oscore_ctx_t::rfc8613_b_2
uint8_t rfc8613_b_2
1 if rfc8613 B.2 protocol else 0
Definition oscore_context.h:97
oscore_ctx_t::master_salt
coap_bin_const_t * master_salt
Definition oscore_context.h:87
oscore_ctx_t::save_seq_num_func
coap_oscore_save_seq_num_t save_seq_num_func
Called every seq num change.
Definition oscore_context.h:100
oscore_ctx_t::recipient_chain
oscore_recipient_ctx_t * recipient_chain
Definition oscore_context.h:92
oscore_ctx_t::id_context
coap_bin_const_t * id_context
contains GID in case of group
Definition oscore_context.h:90
oscore_ctx_t::ssn_freq
uint32_t ssn_freq
Sender Seq Num update frequency.
Definition oscore_context.h:98
oscore_recipient_ctx_t
Definition oscore_context.h:112
oscore_recipient_ctx_t::rollback_sliding_window
uint64_t rollback_sliding_window
Definition oscore_context.h:119
oscore_recipient_ctx_t::next_recipient
oscore_recipient_ctx_t * next_recipient
Definition oscore_context.h:114
oscore_recipient_ctx_t::sliding_window
uint64_t sliding_window
Definition oscore_context.h:118
oscore_recipient_ctx_t::echo_value
uint8_t echo_value[8]
Definition oscore_context.h:123
oscore_recipient_ctx_t::last_seq
uint64_t last_seq
Definition oscore_context.h:116
oscore_recipient_ctx_t::recipient_key
coap_bin_const_t * recipient_key
Definition oscore_context.h:121
oscore_recipient_ctx_t::recipient_id
coap_bin_const_t * recipient_id
Definition oscore_context.h:122
oscore_recipient_ctx_t::initial_state
uint8_t initial_state
Definition oscore_context.h:124
oscore_recipient_ctx_t::osc_ctx
oscore_ctx_t * osc_ctx
Definition oscore_context.h:115
oscore_recipient_ctx_t::rollback_last_seq
uint64_t rollback_last_seq
Definition oscore_context.h:120
oscore_sender_ctx_t
Definition oscore_context.h:105
oscore_sender_ctx_t::sender_id
coap_bin_const_t * sender_id
Definition oscore_context.h:109
oscore_sender_ctx_t::seq
uint64_t seq
Definition oscore_context.h:106
oscore_sender_ctx_t::sender_key
coap_bin_const_t * sender_key
Definition oscore_context.h:108
oscore_sender_ctx_t::next_seq
uint64_t next_seq
Used for ssn_freq updating.
Definition oscore_context.h:107