tinydtls  0.8.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Macros | Functions | Variables
dtls.c File Reference
#include "tinydtls.h"
#include "dtls_config.h"
#include "dtls_time.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "uthash.h"
#include "debug.h"
#include "numeric.h"
#include "netq.h"
#include "dtls.h"
#include "alert.h"
#include "session.h"
#include "prng.h"
#include "sha2/sha2.h"

Go to the source code of this file.

Macros

#define dtls_set_version(H, V)   dtls_int_to_uint16((H)->version, (V))
 
#define dtls_set_content_type(H, V)   ((H)->content_type = (V) & 0xff)
 
#define dtls_set_length(H, V)   ((H)->length = (V))
 
#define dtls_get_content_type(H)   ((H)->content_type & 0xff)
 
#define dtls_get_version(H)   dtls_uint16_to_int((H)->version)
 
#define dtls_get_epoch(H)   dtls_uint16_to_int((H)->epoch)
 
#define dtls_get_sequence_number(H)   dtls_uint48_to_ulong((H)->sequence_number)
 
#define dtls_get_fragment_length(H)   dtls_uint24_to_int((H)->fragment_length)
 
#define HASH_FIND_PEER(head, sess, out)   HASH_FIND(hh,head,sess,sizeof(session_t),out)
 
#define HASH_ADD_PEER(head, sess, add)   HASH_ADD(hh,head,sess,sizeof(session_t),add)
 
#define HASH_DEL_PEER(head, delptr)   HASH_DELETE(hh,head,delptr)
 
#define DTLS_RH_LENGTH   sizeof(dtls_record_header_t)
 
#define DTLS_HS_LENGTH   sizeof(dtls_handshake_header_t)
 
#define DTLS_CH_LENGTH   sizeof(dtls_client_hello_t) /* no variable length fields! */
 
#define DTLS_COOKIE_LENGTH_MAX   32
 
#define DTLS_CH_LENGTH_MAX   sizeof(dtls_client_hello_t) + DTLS_COOKIE_LENGTH_MAX + 12 + 26
 
#define DTLS_HV_LENGTH   sizeof(dtls_hello_verify_t)
 
#define DTLS_SH_LENGTH   (2 + DTLS_RANDOM_LENGTH + 1 + 2 + 1)
 
#define DTLS_CE_LENGTH   (3 + 3 + 27 + DTLS_EC_KEY_SIZE + DTLS_EC_KEY_SIZE)
 
#define DTLS_SKEXEC_LENGTH   (1 + 2 + 1 + 1 + DTLS_EC_KEY_SIZE + DTLS_EC_KEY_SIZE + 1 + 1 + 2 + 70)
 
#define DTLS_SKEXECPSK_LENGTH_MIN   2
 
#define DTLS_SKEXECPSK_LENGTH_MAX   2 + DTLS_PSK_MAX_CLIENT_IDENTITY_LEN
 
#define DTLS_CKXPSK_LENGTH_MIN   2
 
#define DTLS_CKXEC_LENGTH   (1 + 1 + DTLS_EC_KEY_SIZE + DTLS_EC_KEY_SIZE)
 
#define DTLS_CV_LENGTH   (1 + 1 + 2 + 1 + 1 + 1 + 1 + DTLS_EC_KEY_SIZE + 1 + 1 + DTLS_EC_KEY_SIZE)
 
#define DTLS_FIN_LENGTH   12
 
#define HS_HDR_LENGTH   DTLS_RH_LENGTH + DTLS_HS_LENGTH
 
#define HV_HDR_LENGTH   HS_HDR_LENGTH + DTLS_HV_LENGTH
 
#define HIGH(V)   (((V) >> 8) & 0xff)
 
#define LOW(V)   ((V) & 0xff)
 
#define DTLS_RECORD_HEADER(M)   ((dtls_record_header_t *)(M))
 
#define DTLS_HANDSHAKE_HEADER(M)   ((dtls_handshake_header_t *)(M))
 
#define HANDSHAKE(M)   ((dtls_handshake_header_t *)((M) + DTLS_RH_LENGTH))
 
#define CLIENTHELLO(M)   ((dtls_client_hello_t *)((M) + HS_HDR_LENGTH))
 
#define SKIP_VAR_FIELD(P, L, T)
 
#define PRF_LABEL(Label)   prf_label_##Label
 
#define PRF_LABEL_SIZE(Label)   (sizeof(PRF_LABEL(Label)) - 1)
 
#define CALL(Context, which,...)
 
#define A_DATA_LEN   13
 
#define MUST_HASH(Type, Data, Length)
 
#define mycookie   (buf + DTLS_HV_LENGTH)
 
#define R_KEY_OFFSET   (1 + 1 + 2 + 1 + 1 + 1 + 1)
 
#define S_KEY_OFFSET(len_s)   (R_KEY_OFFSET + (len_s) + 1 + 1)
 
#define A_DATA_LEN   13
 

Functions

static dtls_context_tmalloc_context ()
 
static void free_context (dtls_context_t *context)
 
void dtls_init ()
 
static int dtls_send_multi (dtls_context_t *ctx, dtls_peer_t *peer, dtls_security_parameters_t *security, session_t *session, unsigned char type, uint8 *buf_array[], size_t buf_len_array[], size_t buf_array_len)
 
static int dtls_send (dtls_context_t *ctx, dtls_peer_t *peer, unsigned char type, uint8 *buf, size_t buflen)
 
static void dtls_stop_retransmission (dtls_context_t *context, dtls_peer_t *peer)
 
dtls_peer_tdtls_get_peer (const dtls_context_t *ctx, const session_t *session)
 
static void dtls_add_peer (dtls_context_t *ctx, dtls_peer_t *peer)
 
int dtls_write (struct dtls_context_t *ctx, session_t *dst, uint8 *buf, size_t len)
 
static int dtls_get_cookie (uint8 *msg, size_t msglen, uint8 **cookie)
 
static int dtls_create_cookie (dtls_context_t *ctx, session_t *session, uint8 *msg, size_t msglen, uint8 *cookie, int *clen)
 
static unsigned int is_record (uint8 *msg, size_t msglen)
 
static uint8dtls_set_record_header (uint8 type, dtls_security_parameters_t *security, uint8 *buf)
 
static uint8dtls_set_handshake_header (uint8 type, dtls_peer_t *peer, int length, int frag_offset, int frag_length, uint8 *buf)
 
static int is_tls_ecdhe_ecdsa_with_aes_128_ccm_8 (dtls_cipher_t cipher)
 
static int is_tls_psk_with_aes_128_ccm_8 (dtls_cipher_t cipher)
 
static int is_psk_supported (dtls_context_t *ctx)
 
static int is_ecdsa_supported (dtls_context_t *ctx, int is_client)
 
static int is_ecdsa_client_auth_supported (dtls_context_t *ctx)
 
static int known_cipher (dtls_context_t *ctx, dtls_cipher_t code, int is_client)
 
static void dtls_debug_keyblock (dtls_security_parameters_t *config)
 
static char * dtls_handshake_type_to_name (int type)
 
static int calculate_key_block (dtls_context_t *ctx, dtls_handshake_parameters_t *handshake, dtls_peer_t *peer, session_t *session, dtls_peer_type role)
 
static int verify_ext_eliptic_curves (uint8 *data, size_t data_length)
 
static int verify_ext_cert_type (uint8 *data, size_t data_length)
 
static int verify_ext_ec_point_formats (uint8 *data, size_t data_length)
 
static int dtls_check_tls_extension (dtls_peer_t *peer, uint8 *data, size_t data_length, int client_hello)
 
static int dtls_update_parameters (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_client_keyexchange (dtls_context_t *ctx, dtls_handshake_parameters_t *handshake, uint8 *data, size_t length)
 
static void update_hs_hash (dtls_peer_t *peer, uint8 *data, size_t length)
 
static void copy_hs_hash (dtls_peer_t *peer, dtls_hash_ctx *hs_hash)
 
static size_t finalize_hs_hash (dtls_peer_t *peer, uint8 *buf)
 
static void clear_hs_hash (dtls_peer_t *peer)
 
static int check_finished (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int dtls_prepare_record (dtls_peer_t *peer, dtls_security_parameters_t *security, unsigned char type, uint8 *data_array[], size_t data_len_array[], size_t data_array_len, uint8 *sendbuf, size_t *rlen)
 
static int dtls_send_handshake_msg_hash (dtls_context_t *ctx, dtls_peer_t *peer, session_t *session, uint8 header_type, uint8 *data, size_t data_length, int add_hash)
 
static int dtls_send_handshake_msg (dtls_context_t *ctx, dtls_peer_t *peer, uint8 header_type, uint8 *data, size_t data_length)
 
static int dtls_send_alert (dtls_context_t *ctx, dtls_peer_t *peer, dtls_alert_level_t level, dtls_alert_t description)
 
int dtls_close (dtls_context_t *ctx, const session_t *remote)
 
static void dtls_destroy_peer (dtls_context_t *ctx, dtls_peer_t *peer, int unlink)
 
static int dtls_verify_peer (dtls_context_t *ctx, dtls_peer_t *peer, session_t *session, uint8 *data, size_t data_length)
 
static int dtls_check_ecdsa_signature_elem (uint8 *data, size_t data_length, unsigned char **result_r, unsigned char **result_s)
 
static int check_client_certificate_verify (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int dtls_send_server_hello (dtls_context_t *ctx, dtls_peer_t *peer)
 
static int dtls_send_certificate_ecdsa (dtls_context_t *ctx, dtls_peer_t *peer, const dtls_ecdsa_key_t *key)
 
static uint8dtls_add_ecdsa_signature_elem (uint8 *p, uint32_t *point_r, uint32_t *point_s)
 
static int dtls_send_server_key_exchange_ecdh (dtls_context_t *ctx, dtls_peer_t *peer, const dtls_ecdsa_key_t *key)
 
static int dtls_send_server_key_exchange_psk (dtls_context_t *ctx, dtls_peer_t *peer, const unsigned char *psk_hint, size_t len)
 
static int dtls_send_server_certificate_request (dtls_context_t *ctx, dtls_peer_t *peer)
 
static int dtls_send_server_hello_done (dtls_context_t *ctx, dtls_peer_t *peer)
 
static int dtls_send_server_hello_msgs (dtls_context_t *ctx, dtls_peer_t *peer)
 
static int dtls_send_ccs (dtls_context_t *ctx, dtls_peer_t *peer)
 
static int dtls_send_client_key_exchange (dtls_context_t *ctx, dtls_peer_t *peer)
 
static int dtls_send_certificate_verify_ecdh (dtls_context_t *ctx, dtls_peer_t *peer, const dtls_ecdsa_key_t *key)
 
static int dtls_send_finished (dtls_context_t *ctx, dtls_peer_t *peer, const unsigned char *label, size_t labellen)
 
static int dtls_send_client_hello (dtls_context_t *ctx, dtls_peer_t *peer, uint8 cookie[], size_t cookie_length)
 
static int check_server_hello (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_server_hello_verify_request (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_server_certificate (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_server_key_exchange_ecdsa (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_server_key_exchange_psk (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_certificate_request (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int check_server_hellodone (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *data, size_t data_length)
 
static int decrypt_verify (dtls_peer_t *peer, uint8 *packet, size_t length, uint8 **cleartext)
 
static int dtls_send_hello_request (dtls_context_t *ctx, dtls_peer_t *peer)
 
int dtls_renegotiate (dtls_context_t *ctx, const session_t *dst)
 
static int handle_handshake_msg (dtls_context_t *ctx, dtls_peer_t *peer, session_t *session, const dtls_peer_type role, const dtls_state_t state, uint8 *data, size_t data_length)
 
static int handle_handshake (dtls_context_t *ctx, dtls_peer_t *peer, session_t *session, const dtls_peer_type role, const dtls_state_t state, uint8 *data, size_t data_length)
 
static int handle_ccs (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *record_header, uint8 *data, size_t data_length)
 
static int handle_alert (dtls_context_t *ctx, dtls_peer_t *peer, uint8 *record_header, uint8 *data, size_t data_length)
 
static int dtls_alert_send_from_err (dtls_context_t *ctx, dtls_peer_t *peer, session_t *session, int err)
 
int dtls_handle_message (dtls_context_t *ctx, session_t *session, uint8 *msg, int msglen)
 
dtls_context_tdtls_new_context (void *app_data)
 
void dtls_free_context (dtls_context_t *ctx)
 
int dtls_connect_peer (dtls_context_t *ctx, dtls_peer_t *peer)
 
int dtls_connect (dtls_context_t *ctx, const session_t *dst)
 
static void dtls_retransmit (dtls_context_t *context, netq_t *node)
 
void dtls_check_retransmit (dtls_context_t *context, clock_time_t *next)
 

Variables

static const unsigned char prf_label_master [] = "master secret"
 
static const unsigned char prf_label_key [] = "key expansion"
 
static const unsigned char prf_label_client [] = "client"
 
static const unsigned char prf_label_server [] = "server"
 
static const unsigned char prf_label_finished [] = " finished"
 
static const unsigned char cert_asn1_header []
 
static uint8 compression_methods []
 

Macro Definition Documentation

#define A_DATA_LEN   13
#define A_DATA_LEN   13
#define CALL (   Context,
  which,
  ... 
)
Value:
((Context)->h && (Context)->h->which \
? (Context)->h->which((Context), ##__VA_ARGS__) \
: -1)

Definition at line 174 of file dtls.c.

#define CLIENTHELLO (   M)    ((dtls_client_hello_t *)((M) + HS_HDR_LENGTH))

Definition at line 99 of file dtls.c.

#define DTLS_CE_LENGTH   (3 + 3 + 27 + DTLS_EC_KEY_SIZE + DTLS_EC_KEY_SIZE)

Definition at line 80 of file dtls.c.

#define DTLS_CH_LENGTH   sizeof(dtls_client_hello_t) /* no variable length fields! */

Definition at line 75 of file dtls.c.

#define DTLS_CH_LENGTH_MAX   sizeof(dtls_client_hello_t) + DTLS_COOKIE_LENGTH_MAX + 12 + 26

Definition at line 77 of file dtls.c.

#define DTLS_CKXEC_LENGTH   (1 + 1 + DTLS_EC_KEY_SIZE + DTLS_EC_KEY_SIZE)

Definition at line 85 of file dtls.c.

#define DTLS_CKXPSK_LENGTH_MIN   2

Definition at line 84 of file dtls.c.

#define DTLS_COOKIE_LENGTH_MAX   32

Definition at line 76 of file dtls.c.

#define DTLS_CV_LENGTH   (1 + 1 + 2 + 1 + 1 + 1 + 1 + DTLS_EC_KEY_SIZE + 1 + 1 + DTLS_EC_KEY_SIZE)

Definition at line 86 of file dtls.c.

#define DTLS_FIN_LENGTH   12

Definition at line 87 of file dtls.c.

#define dtls_get_content_type (   H)    ((H)->content_type & 0xff)

Definition at line 58 of file dtls.c.

#define dtls_get_epoch (   H)    dtls_uint16_to_int((H)->epoch)

Definition at line 60 of file dtls.c.

#define dtls_get_fragment_length (   H)    dtls_uint24_to_int((H)->fragment_length)

Definition at line 62 of file dtls.c.

#define dtls_get_sequence_number (   H)    dtls_uint48_to_ulong((H)->sequence_number)

Definition at line 61 of file dtls.c.

#define dtls_get_version (   H)    dtls_uint16_to_int((H)->version)

Definition at line 59 of file dtls.c.

#define DTLS_HANDSHAKE_HEADER (   M)    ((dtls_handshake_header_t *)(M))

Definition at line 96 of file dtls.c.

#define DTLS_HS_LENGTH   sizeof(dtls_handshake_header_t)

Definition at line 74 of file dtls.c.

#define DTLS_HV_LENGTH   sizeof(dtls_hello_verify_t)

Definition at line 78 of file dtls.c.

#define DTLS_RECORD_HEADER (   M)    ((dtls_record_header_t *)(M))

Definition at line 95 of file dtls.c.

#define DTLS_RH_LENGTH   sizeof(dtls_record_header_t)

Definition at line 73 of file dtls.c.

#define dtls_set_content_type (   H,
 
)    ((H)->content_type = (V) & 0xff)

Definition at line 55 of file dtls.c.

#define dtls_set_length (   H,
 
)    ((H)->length = (V))

Definition at line 56 of file dtls.c.

#define dtls_set_version (   H,
 
)    dtls_int_to_uint16((H)->version, (V))

Definition at line 54 of file dtls.c.

#define DTLS_SH_LENGTH   (2 + DTLS_RANDOM_LENGTH + 1 + 2 + 1)

Definition at line 79 of file dtls.c.

#define DTLS_SKEXEC_LENGTH   (1 + 2 + 1 + 1 + DTLS_EC_KEY_SIZE + DTLS_EC_KEY_SIZE + 1 + 1 + 2 + 70)

Definition at line 81 of file dtls.c.

#define DTLS_SKEXECPSK_LENGTH_MAX   2 + DTLS_PSK_MAX_CLIENT_IDENTITY_LEN

Definition at line 83 of file dtls.c.

#define DTLS_SKEXECPSK_LENGTH_MIN   2

Definition at line 82 of file dtls.c.

#define HANDSHAKE (   M)    ((dtls_handshake_header_t *)((M) + DTLS_RH_LENGTH))

Definition at line 98 of file dtls.c.

#define HASH_ADD_PEER (   head,
  sess,
  add 
)    HASH_ADD(hh,head,sess,sizeof(session_t),add)

Definition at line 67 of file dtls.c.

#define HASH_DEL_PEER (   head,
  delptr 
)    HASH_DELETE(hh,head,delptr)

Definition at line 69 of file dtls.c.

#define HASH_FIND_PEER (   head,
  sess,
  out 
)    HASH_FIND(hh,head,sess,sizeof(session_t),out)

Definition at line 65 of file dtls.c.

#define HIGH (   V)    (((V) >> 8) & 0xff)

Definition at line 92 of file dtls.c.

#define HS_HDR_LENGTH   DTLS_RH_LENGTH + DTLS_HS_LENGTH

Definition at line 89 of file dtls.c.

#define HV_HDR_LENGTH   HS_HDR_LENGTH + DTLS_HV_LENGTH

Definition at line 90 of file dtls.c.

#define LOW (   V)    ((V) & 0xff)

Definition at line 93 of file dtls.c.

#define MUST_HASH (   Type,
  Data,
  Length 
)

Returns true if the message Data is a handshake message that must be included in the calculation of verify_data in the Finished message.

Parameters
TypeThe message type. Only handshake messages but the initial Client Hello and Hello Verify Request are included in the hash,
DataThe PDU to examine.
LengthThe length of Data.
Returns
1 if Data must be included in hash, 0 otherwise.

Definition at line 1384 of file dtls.c.

#define mycookie   (buf + DTLS_HV_LENGTH)
#define PRF_LABEL (   Label)    prf_label_##Label

Definition at line 114 of file dtls.c.

#define PRF_LABEL_SIZE (   Label)    (sizeof(PRF_LABEL(Label)) - 1)

Definition at line 115 of file dtls.c.

#define R_KEY_OFFSET   (1 + 1 + 2 + 1 + 1 + 1 + 1)
#define S_KEY_OFFSET (   len_s)    (R_KEY_OFFSET + (len_s) + 1 + 1)
#define SKIP_VAR_FIELD (   P,
  L,
 
)
Value:
{ \
if (L < dtls_ ## T ## _to_int(P) + sizeof(T)) \
goto error; \
L -= dtls_ ## T ## _to_int(P) + sizeof(T); \
P += dtls_ ## T ## _to_int(P) + sizeof(T); \
}

Definition at line 106 of file dtls.c.

Function Documentation

static int calculate_key_block ( dtls_context_t ctx,
dtls_handshake_parameters_t handshake,
dtls_peer_t peer,
session_t session,
dtls_peer_type  role 
)
static

Calculate the pre master secret and after that calculate the master-secret.

Definition at line 599 of file dtls.c.

static int check_certificate_request ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2726 of file dtls.c.

static int check_client_certificate_verify ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 1691 of file dtls.c.

static int check_client_keyexchange ( dtls_context_t ctx,
dtls_handshake_parameters_t handshake,
uint8 data,
size_t  length 
)
inlinestatic

Parse the ClientKeyExchange and update the internal handshake state with the new data.

Definition at line 1005 of file dtls.c.

static int check_finished ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Checks if record + data contain a Finished message with valid verify_data.

Parameters
ctxThe current DTLS context.
peerThe remote peer of the security association.
dataThe cleartext payload of the message.
data_lengthActual length of data.
Returns
0 if the Finished message is valid, negative number otherwise.

Definition at line 1104 of file dtls.c.

static int check_server_certificate ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2552 of file dtls.c.

static int check_server_hello ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2460 of file dtls.c.

static int check_server_hello_verify_request ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2530 of file dtls.c.

static int check_server_hellodone ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2804 of file dtls.c.

static int check_server_key_exchange_ecdsa ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2604 of file dtls.c.

static int check_server_key_exchange_psk ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Definition at line 2687 of file dtls.c.

static void clear_hs_hash ( dtls_peer_t peer)
inlinestatic

Definition at line 1087 of file dtls.c.

static void copy_hs_hash ( dtls_peer_t peer,
dtls_hash_ctx hs_hash 
)
static

Definition at line 1076 of file dtls.c.

static int decrypt_verify ( dtls_peer_t peer,
uint8 packet,
size_t  length,
uint8 **  cleartext 
)
static

length of additional_data for the AEAD cipher which consists of seq_num(2+6) + type(1) + version(2) + length(2)

Definition at line 2877 of file dtls.c.

static uint8* dtls_add_ecdsa_signature_elem ( uint8 p,
uint32_t *  point_r,
uint32_t *  point_s 
)
static

Definition at line 1867 of file dtls.c.

static void dtls_add_peer ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 227 of file dtls.c.

static int dtls_alert_send_from_err ( dtls_context_t ctx,
dtls_peer_t peer,
session_t session,
int  err 
)
static

Definition at line 3559 of file dtls.c.

static int dtls_check_ecdsa_signature_elem ( uint8 data,
size_t  data_length,
unsigned char **  result_r,
unsigned char **  result_s 
)
static

Definition at line 1611 of file dtls.c.

void dtls_check_retransmit ( dtls_context_t context,
clock_time_t next 
)

Checks sendqueue of given DTLS context object for any outstanding packets to be transmitted.

Parameters
contextThe DTLS context object to use.
nextIf not NULL, next is filled with the timestamp of the next scheduled retransmission, or 0 when no packets are waiting.

Definition at line 3957 of file dtls.c.

static int dtls_check_tls_extension ( dtls_peer_t peer,
uint8 data,
size_t  data_length,
int  client_hello 
)
static

Definition at line 777 of file dtls.c.

int dtls_close ( dtls_context_t ctx,
const session_t remote 
)

Closes the DTLS connection associated with remote. This function returns zero on success, and a value less than zero on error.

Definition at line 1494 of file dtls.c.

int dtls_connect ( dtls_context_t ctx,
const session_t dst 
)

Establishes a DTLS channel with the specified remote peer dst. This function returns 0 if that channel already exists, a value greater than zero when a new ClientHello message was sent, and a value less than zero on error.

Parameters
ctxThe DTLS context to use.
dstThe remote party to connect to.
Returns
A value less than zero on error, greater or equal otherwise.

Definition at line 3862 of file dtls.c.

int dtls_connect_peer ( dtls_context_t ctx,
dtls_peer_t peer 
)

Establishes a DTLS channel with the specified remote peer. This function returns 0 if that channel already exists, a value greater than zero when a new ClientHello message was sent, and a value less than zero on error.

Parameters
ctxThe DTLS context to use.
peerThe peer object that describes the session.
Returns
A value less than zero on error, greater or equal otherwise.

Definition at line 3826 of file dtls.c.

static int dtls_create_cookie ( dtls_context_t ctx,
session_t session,
uint8 msg,
size_t  msglen,
uint8 cookie,
int *  clen 
)
static

Definition at line 286 of file dtls.c.

static void dtls_debug_keyblock ( dtls_security_parameters_t config)
static

Dump out the cipher keys and IVs used for the symetric cipher.

Definition at line 533 of file dtls.c.

static void dtls_destroy_peer ( dtls_context_t ctx,
dtls_peer_t peer,
int  unlink 
)
static

Definition at line 1508 of file dtls.c.

void dtls_free_context ( dtls_context_t ctx)

Releases any storage that has been allocated for ctx.

Definition at line 3802 of file dtls.c.

static int dtls_get_cookie ( uint8 msg,
size_t  msglen,
uint8 **  cookie 
)
static

Definition at line 261 of file dtls.c.

dtls_peer_t* dtls_get_peer ( const dtls_context_t context,
const session_t session 
)

Check if session is associated with a peer object in context. This function returns a pointer to the peer if found, NULL otherwise.

Parameters
contextThe DTLS context to search.
sessionThe remote address and local interface
Returns
A pointer to the peer associated with session or NULL if none exists.

Definition at line 212 of file dtls.c.

int dtls_handle_message ( dtls_context_t ctx,
session_t session,
uint8 msg,
int  msglen 
)

Handles incoming data as DTLS message from given peer.

Definition at line 3591 of file dtls.c.

static char* dtls_handshake_type_to_name ( int  type)
static

returns the name of the goven handshake type number. see IANA for a full list of types: https://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-7

Definition at line 565 of file dtls.c.

void dtls_init ( )

This function initializes the tinyDTLS memory management and must be called first.

Definition at line 163 of file dtls.c.

dtls_context_t* dtls_new_context ( void *  app_data)

Creates a new context object. The storage allocated for the new object must be released with dtls_free_context().

Definition at line 3740 of file dtls.c.

static int dtls_prepare_record ( dtls_peer_t peer,
dtls_security_parameters_t security,
unsigned char  type,
uint8 data_array[],
size_t  data_len_array[],
size_t  data_array_len,
uint8 sendbuf,
size_t *  rlen 
)
static

Prepares the payload given in data for sending with dtls_send(). The data is encrypted and compressed according to the current security parameters of peer. The result of this operation is put into sendbuf with a prepended record header of type type ready for sending. As some cipher suites add a MAC before encryption, data must be large enough to hold this data as well (usually dtls_kb_digest_size(CURRENT_CONFIG(peer)).

Parameters
peerThe remote peer the packet will be sent to.
securityThe encryption paramater used to encrypt
typeThe content type of this record.
data_arrayArray with payloads in correct order.
data_len_arraysizes of the payloads in correct order.
data_array_lenThe number of payloads given.
sendbufThe output buffer where the encrypted record will be placed.
rlenThis parameter must be initialized with the maximum size of sendbuf and will be updated to hold the actual size of the stored packet on success. On error, the value of rlen is undefined.
Returns
Less than zero on error, or greater than zero success.

length of additional_data for the AEAD cipher which consists of seq_num(2+6) + type(1) + version(2) + length(2)

Definition at line 1180 of file dtls.c.

int dtls_renegotiate ( dtls_context_t ctx,
const session_t dst 
)

Definition at line 2956 of file dtls.c.

static void dtls_retransmit ( dtls_context_t context,
netq_t node 
)
static

Definition at line 3890 of file dtls.c.

static int dtls_send ( dtls_context_t ctx,
dtls_peer_t peer,
unsigned char  type,
uint8 buf,
size_t  buflen 
)
static

Sends the fragment of length buflen given in buf to the specified peer. The data will be MAC-protected and encrypted according to the selected cipher and split into one or more DTLS records of the specified type. This function returns the number of bytes that were sent, or -1 if an error occurred.

Parameters
ctxThe DTLS context to use.
peerThe remote peer.
typeThe content type of the record.
bufThe data to send.
buflenThe actual length of buf.
Returns
Less than zero on error, the number of bytes written otherwise.

Definition at line 200 of file dtls.c.

static int dtls_send_alert ( dtls_context_t ctx,
dtls_peer_t peer,
dtls_alert_level_t  level,
dtls_alert_t  description 
)
inlinestatic

Definition at line 1485 of file dtls.c.

static int dtls_send_ccs ( dtls_context_t ctx,
dtls_peer_t peer 
)
inlinestatic

Definition at line 2156 of file dtls.c.

static int dtls_send_certificate_ecdsa ( dtls_context_t ctx,
dtls_peer_t peer,
const dtls_ecdsa_key_t key 
)
static

Definition at line 1834 of file dtls.c.

static int dtls_send_certificate_verify_ecdh ( dtls_context_t ctx,
dtls_peer_t peer,
const dtls_ecdsa_key_t key 
)
static

Definition at line 2241 of file dtls.c.

static int dtls_send_client_hello ( dtls_context_t ctx,
dtls_peer_t peer,
uint8  cookie[],
size_t  cookie_length 
)
static

Definition at line 2308 of file dtls.c.

static int dtls_send_client_key_exchange ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 2164 of file dtls.c.

static int dtls_send_finished ( dtls_context_t ctx,
dtls_peer_t peer,
const unsigned char *  label,
size_t  labellen 
)
static

Definition at line 2277 of file dtls.c.

static int dtls_send_handshake_msg ( dtls_context_t ctx,
dtls_peer_t peer,
uint8  header_type,
uint8 data,
size_t  data_length 
)
static

Definition at line 1361 of file dtls.c.

static int dtls_send_handshake_msg_hash ( dtls_context_t ctx,
dtls_peer_t peer,
session_t session,
uint8  header_type,
uint8 data,
size_t  data_length,
int  add_hash 
)
static

Definition at line 1323 of file dtls.c.

static int dtls_send_hello_request ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 2948 of file dtls.c.

static int dtls_send_multi ( dtls_context_t ctx,
dtls_peer_t peer,
dtls_security_parameters_t security,
session_t session,
unsigned char  type,
uint8 buf_array[],
size_t  buf_len_array[],
size_t  buf_array_len 
)
static

Sends the data passed in buf as a DTLS record of type type to the given peer. The data will be encrypted and compressed according to the security parameters for peer.

Parameters
ctxThe DTLS context in effect.
peerThe remote party where the packet is sent.
typeThe content type of this record.
bufThe data to send.
buflenThe number of bytes to send from buf.
Returns
Less than zero in case of an error or the number of bytes that have been sent otherwise.

Definition at line 1407 of file dtls.c.

static int dtls_send_server_certificate_request ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 2019 of file dtls.c.

static int dtls_send_server_hello ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 1736 of file dtls.c.

static int dtls_send_server_hello_done ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 2061 of file dtls.c.

static int dtls_send_server_hello_msgs ( dtls_context_t ctx,
dtls_peer_t peer 
)
static

Definition at line 2073 of file dtls.c.

static int dtls_send_server_key_exchange_ecdh ( dtls_context_t ctx,
dtls_peer_t peer,
const dtls_ecdsa_key_t key 
)
static

Definition at line 1924 of file dtls.c.

static int dtls_send_server_key_exchange_psk ( dtls_context_t ctx,
dtls_peer_t peer,
const unsigned char *  psk_hint,
size_t  len 
)
static

Definition at line 1989 of file dtls.c.

static uint8* dtls_set_handshake_header ( uint8  type,
dtls_peer_t peer,
int  length,
int  frag_offset,
int  frag_length,
uint8 buf 
)
inlinestatic

Initializes buf as handshake header. The caller must ensure that buf is capable of holding at least sizeof(dtls_handshake_header_t) bytes. Increments message sequence number counter of peer.

Returns
pointer to the next byte after buf

Definition at line 424 of file dtls.c.

static uint8* dtls_set_record_header ( uint8  type,
dtls_security_parameters_t security,
uint8 buf 
)
inlinestatic

Initializes buf as record header. The caller must ensure that buf is capable of holding at least sizeof(dtls_record_header_t) bytes. Increments sequence number counter of security.

Returns
pointer to the next byte after the written header. The length will be set to 0 and has to be changed before sending.

Definition at line 390 of file dtls.c.

static void dtls_stop_retransmission ( dtls_context_t context,
dtls_peer_t peer 
)
static

Stops ongoing retransmissions of handshake messages for peer.

Definition at line 3941 of file dtls.c.

static int dtls_update_parameters ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 data,
size_t  data_length 
)
static

Parses the ClientHello from the client and updates the internal handshake parameters with the new data for the given peer. When the ClientHello handshake message in data does not contain a cipher suite or compression method, it is copied from the the current security parameters.

Parameters
ctxThe current DTLS context.
peerThe remote peer whose security parameters are about to change.
dataThe handshake message with a ClientHello.
data_lengthThe actual size of data.
Returns
-Something if an error occurred, 0 on success.

Definition at line 900 of file dtls.c.

static int dtls_verify_peer ( dtls_context_t ctx,
dtls_peer_t peer,
session_t session,
uint8 data,
size_t  data_length 
)
static

Checks a received Client Hello message for a valid cookie. When the Client Hello contains no cookie, the function fails and a Hello Verify Request is sent to the peer (using the write callback function registered with ctx). The return value is -1 on error, 0 when undecided, and 1 if the Client Hello was good.

Parameters
ctxThe DTLS context.
peerThe remote party we are talking to, if any.
sessionTransport address of the remote peer.
msgThe received datagram.
msglenLength of msg.
Returns
1 if msg is a Client Hello with a valid cookie, 0 or -1 otherwise.

Definition at line 1540 of file dtls.c.

int dtls_write ( struct dtls_context_t ctx,
session_t session,
uint8 buf,
size_t  len 
)

Writes the application data given in buf to the peer specified by session.

Parameters
ctxThe DTLS context to use.
sessionThe remote transport address and local interface.
bufThe data to write.
lenThe actual length of data.
Returns
The number of bytes written or -1 on error.

Definition at line 236 of file dtls.c.

static size_t finalize_hs_hash ( dtls_peer_t peer,
uint8 buf 
)
inlinestatic

Definition at line 1082 of file dtls.c.

static void free_context ( dtls_context_t context)
inlinestatic

Definition at line 157 of file dtls.c.

static int handle_alert ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 record_header,
uint8 data,
size_t  data_length 
)
static

Handles incoming Alert messages. This function returns 1 if the connection should be closed and the peer is to be invalidated.

Definition at line 3496 of file dtls.c.

static int handle_ccs ( dtls_context_t ctx,
dtls_peer_t peer,
uint8 record_header,
uint8 data,
size_t  data_length 
)
static

Definition at line 3457 of file dtls.c.

static int handle_handshake ( dtls_context_t ctx,
dtls_peer_t peer,
session_t session,
const dtls_peer_type  role,
const dtls_state_t  state,
uint8 data,
size_t  data_length 
)
static

Definition at line 3347 of file dtls.c.

static int handle_handshake_msg ( dtls_context_t ctx,
dtls_peer_t peer,
session_t session,
const dtls_peer_type  role,
const dtls_state_t  state,
uint8 data,
size_t  data_length 
)
static

Definition at line 2992 of file dtls.c.

static int is_ecdsa_client_auth_supported ( dtls_context_t ctx)
inlinestatic

Returns true if the application is configured for ecdhe_ecdsa with client authentication

Definition at line 503 of file dtls.c.

static int is_ecdsa_supported ( dtls_context_t ctx,
int  is_client 
)
inlinestatic

returns true if the application is configured for ecdhe_ecdsa

Definition at line 491 of file dtls.c.

static int is_psk_supported ( dtls_context_t ctx)
inlinestatic

returns true if the application is configured for psk

Definition at line 481 of file dtls.c.

static unsigned int is_record ( uint8 msg,
size_t  msglen 
)
static

Checks if msg points to a valid DTLS record. If

Definition at line 361 of file dtls.c.

static int is_tls_ecdhe_ecdsa_with_aes_128_ccm_8 ( dtls_cipher_t  cipher)
inlinestatic

returns true if the cipher matches TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8

Definition at line 461 of file dtls.c.

static int is_tls_psk_with_aes_128_ccm_8 ( dtls_cipher_t  cipher)
inlinestatic

returns true if the cipher matches TLS_PSK_WITH_AES_128_CCM_8

Definition at line 471 of file dtls.c.

static int known_cipher ( dtls_context_t ctx,
dtls_cipher_t  code,
int  is_client 
)
static

Returns 1 if code is a cipher suite other than TLS_NULL_WITH_NULL_NULL that we recognize.

Parameters
ctxThe current DTLS context
codeThe cipher suite identifier to check
is_client1 for a dtls client, 0 for server
Returns
1 iff code is recognized,

Definition at line 522 of file dtls.c.

static dtls_context_t* malloc_context ( )
inlinestatic

Definition at line 152 of file dtls.c.

static void update_hs_hash ( dtls_peer_t peer,
uint8 data,
size_t  length 
)
inlinestatic

Definition at line 1070 of file dtls.c.

static int verify_ext_cert_type ( uint8 data,
size_t  data_length 
)
static

Definition at line 725 of file dtls.c.

static int verify_ext_ec_point_formats ( uint8 data,
size_t  data_length 
)
static

Definition at line 749 of file dtls.c.

static int verify_ext_eliptic_curves ( uint8 data,
size_t  data_length 
)
static

Definition at line 701 of file dtls.c.

Variable Documentation

const unsigned char cert_asn1_header[]
static
Initial value:
= {
0x30, 0x59,
0x30, 0x13,
0x06, 0x07,
0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
0x06, 0x08,
0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07,
0x03, 0x42, 0x00,
0x04
}

Definition at line 124 of file dtls.c.

uint8 compression_methods[]
static
Initial value:

only one compression method is currently defined

Definition at line 456 of file dtls.c.

const unsigned char prf_label_client[] = "client"
static

Definition at line 119 of file dtls.c.

const unsigned char prf_label_finished[] = " finished"
static

Definition at line 121 of file dtls.c.

const unsigned char prf_label_key[] = "key expansion"
static

Definition at line 118 of file dtls.c.

const unsigned char prf_label_master[] = "master secret"
static

Definition at line 117 of file dtls.c.

const unsigned char prf_label_server[] = "server"
static

Definition at line 120 of file dtls.c.