libcoap 4.3.5
Loading...
Searching...
No Matches
coap_mbedtls.c
Go to the documentation of this file.
1/*
2 * coap_mbedtls.c -- Mbed TLS Datagram Transport Layer Support for libcoap
3 *
4 * Copyright (C) 2019-2024 Jon Shallow <supjps-libcoap@jpshallow.com>
5 * 2019 Jitin George <jitin@espressif.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
18/*
19 * Naming used to prevent confusion between coap sessions, mbedtls sessions etc.
20 * when reading the code.
21 *
22 * c_context A coap_context_t *
23 * c_session A coap_session_t *
24 * m_context A coap_mbedtls_context_t * (held in c_context->dtls_context)
25 * m_env A coap_mbedtls_env_t * (held in c_session->tls)
26 */
27
28/*
29 * Notes
30 *
31 * Version 3.2.0 or later is needed to provide Connection ID support (RFC9146).
32 *
33 */
34
36
37#ifdef COAP_WITH_LIBMBEDTLS
38
39/*
40 * This code can be conditionally compiled to remove some components if
41 * they are not required to make a lighter footprint - all based on how
42 * the mbedtls library has been built. These are not defined within the
43 * libcoap environment.
44 *
45 * MBEDTLS_SSL_SRV_C - defined for server side functionality
46 * MBEDTLS_SSL_CLI_C - defined for client side functionality
47 * MBEDTLS_SSL_PROTO_DTLS - defined for DTLS support
48 * MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED - defined if PSK is to be supported
49 * or MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED - defined if PSK is to be supported
50 *
51 */
52
53#include <mbedtls/version.h>
54
55/* Keep forward-compatibility with Mbed TLS 3.x */
56#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
57#define MBEDTLS_2_X_COMPAT
58#else /* !(MBEDTLS_VERSION_NUMBER < 0x03000000) */
59/* Macro wrapper for struct's private members */
60#ifndef MBEDTLS_ALLOW_PRIVATE_ACCESS
61#define MBEDTLS_ALLOW_PRIVATE_ACCESS
62#endif /* MBEDTLS_ALLOW_PRIVATE_ACCESS */
63#endif /* !(MBEDTLS_VERSION_NUMBER < 0x03000000) */
64
65#include <mbedtls/platform.h>
66#include <mbedtls/net_sockets.h>
67#include <mbedtls/ssl.h>
68#include <mbedtls/entropy.h>
69#include <mbedtls/ctr_drbg.h>
70#include <mbedtls/error.h>
71#include <mbedtls/timing.h>
72#include <mbedtls/ssl_cookie.h>
73#include <mbedtls/oid.h>
74#include <mbedtls/debug.h>
75#include <mbedtls/sha256.h>
76#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
77#include <mbedtls/esp_debug.h>
78#endif /* ESPIDF_VERSION && CONFIG_MBEDTLS_DEBUG */
79#if defined(MBEDTLS_PSA_CRYPTO_C)
80#include <psa/crypto.h>
81#endif /* MBEDTLS_PSA_CRYPTO_C */
82
83#define mbedtls_malloc(a) malloc(a)
84#define mbedtls_realloc(a,b) realloc(a,b)
85#define mbedtls_strdup(a) strdup(a)
86#define mbedtls_strndup(a,b) strndup(a,b)
87
88#ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
89/* definition changed in later mbedtls code versions */
90#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
91#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
92#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
93#endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
94
95#if ! COAP_SERVER_SUPPORT
96#undef MBEDTLS_SSL_SRV_C
97#endif /* ! COAP_SERVER_SUPPORT */
98#if ! COAP_CLIENT_SUPPORT
99#undef MBEDTLS_SSL_CLI_C
100#endif /* ! COAP_CLIENT_SUPPORT */
101
102#ifdef _WIN32
103#define strcasecmp _stricmp
104#endif
105
106#define IS_PSK (1 << 0)
107#define IS_PKI (1 << 1)
108#define IS_CLIENT (1 << 6)
109#define IS_SERVER (1 << 7)
110
111typedef struct coap_ssl_t {
112 const uint8_t *pdu;
113 unsigned pdu_len;
114 unsigned peekmode;
115} coap_ssl_t;
116
117/*
118 * This structure encapsulates the Mbed TLS session object.
119 * It handles both TLS and DTLS.
120 * c_session->tls points to this.
121 */
122typedef struct coap_mbedtls_env_t {
123 mbedtls_ssl_context ssl;
124 mbedtls_entropy_context entropy;
125 mbedtls_ctr_drbg_context ctr_drbg;
126 mbedtls_ssl_config conf;
127 mbedtls_timing_delay_context timer;
128 mbedtls_x509_crt cacert;
129 mbedtls_x509_crt public_cert;
130 mbedtls_pk_context private_key;
131 mbedtls_ssl_cookie_ctx cookie_ctx;
132 /* If not set, need to do do_mbedtls_handshake */
133 int established;
134 int sent_alert;
135 int seen_client_hello;
136 int ec_jpake;
137 coap_tick_t last_timeout;
138 unsigned int retry_scalar;
139 coap_ssl_t coap_ssl_data;
140} coap_mbedtls_env_t;
141
142typedef struct pki_sni_entry {
143 char *sni;
144 coap_dtls_key_t pki_key;
145 mbedtls_x509_crt cacert;
146 mbedtls_x509_crt public_cert;
147 mbedtls_pk_context private_key;
148} pki_sni_entry;
149
150typedef struct psk_sni_entry {
151 char *sni;
152 coap_dtls_spsk_info_t psk_info;
153} psk_sni_entry;
154
155typedef struct coap_mbedtls_context_t {
156 coap_dtls_pki_t setup_data;
157 size_t pki_sni_count;
158 pki_sni_entry *pki_sni_entry_list;
159 size_t psk_sni_count;
160 psk_sni_entry *psk_sni_entry_list;
161 char *root_ca_file;
162 char *root_ca_path;
163 int psk_pki_enabled;
164} coap_mbedtls_context_t;
165
166typedef enum coap_enc_method_t {
167 COAP_ENC_PSK,
168 COAP_ENC_PKI,
169 COAP_ENC_ECJPAKE,
170} coap_enc_method_t;
171
172#ifndef MBEDTLS_2_X_COMPAT
173/*
174 * mbedtls_ callback functions expect 0 on success, -ve on failure.
175 */
176static int
177coap_rng(void *ctx COAP_UNUSED, unsigned char *buf, size_t len) {
178 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
179}
180#endif /* MBEDTLS_2_X_COMPAT */
181
182static int
183coap_dgram_read(void *ctx, unsigned char *out, size_t outl) {
184 ssize_t ret = 0;
185 coap_session_t *c_session = (coap_session_t *)ctx;
186 coap_ssl_t *data;
187
188 if (!c_session->tls) {
189 errno = EAGAIN;
190 return MBEDTLS_ERR_SSL_WANT_READ;
191 }
192 data = &((coap_mbedtls_env_t *)c_session->tls)->coap_ssl_data;
193
194 if (out != NULL) {
195 if (data->pdu_len > 0) {
196 if (outl < data->pdu_len) {
197 memcpy(out, data->pdu, outl);
198 ret = outl;
199 data->pdu += outl;
200 data->pdu_len -= outl;
201 } else {
202 memcpy(out, data->pdu, data->pdu_len);
203 ret = data->pdu_len;
204 if (!data->peekmode) {
205 data->pdu_len = 0;
206 data->pdu = NULL;
207 }
208 }
209 } else {
210 ret = MBEDTLS_ERR_SSL_WANT_READ;
211 errno = EAGAIN;
212 }
213 }
214 return ret;
215}
216
217/*
218 * return +ve data amount
219 * 0 no more
220 * -ve Mbed TLS error
221 */
222/* callback function given to mbedtls for sending data over socket */
223static int
224coap_dgram_write(void *ctx, const unsigned char *send_buffer,
225 size_t send_buffer_length) {
226 ssize_t result = -1;
227 coap_session_t *c_session = (coap_session_t *)ctx;
228
229 if (c_session) {
230 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
231
232 if (!coap_netif_available(c_session)
234 && c_session->endpoint == NULL
235#endif /* COAP_SERVER_SUPPORT */
236 ) {
237 /* socket was closed on client due to error */
238 errno = ECONNRESET;
239 return -1;
240 }
241 result = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
242 send_buffer, send_buffer_length);
243 if (result != (ssize_t)send_buffer_length) {
244 int keep_errno = errno;
245
246 coap_log_warn("coap_netif_dgrm_write failed (%zd != %zu)\n",
247 result, send_buffer_length);
248 errno = keep_errno;
249 if (result < 0) {
250 return -1;
251 } else {
252 result = 0;
253 }
254 } else if (m_env) {
255 coap_tick_t now;
256 coap_ticks(&now);
257 m_env->last_timeout = now;
258 }
259 } else {
260 result = 0;
261 }
262 return result;
263}
264
265#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
266/*
267 * Server side PSK callback
268 */
269static int
270psk_server_callback(void *p_info, mbedtls_ssl_context *ssl,
271 const unsigned char *identity, size_t identity_len) {
272 coap_session_t *c_session = (coap_session_t *)p_info;
273 coap_dtls_spsk_t *setup_data;
274 coap_mbedtls_env_t *m_env;
275 coap_bin_const_t lidentity;
276 const coap_bin_const_t *psk_key;
277
278 if (c_session == NULL)
279 return -1;
280
281 /* Track the Identity being used */
282 lidentity.s = identity ? (const uint8_t *)identity : (const uint8_t *)"";
283 lidentity.length = identity ? identity_len : 0;
284 coap_session_refresh_psk_identity(c_session, &lidentity);
285
286 coap_log_debug("got psk_identity: '%.*s'\n",
287 (int)lidentity.length, (const char *)lidentity.s);
288
289 m_env = (coap_mbedtls_env_t *)c_session->tls;
290 setup_data = &c_session->context->spsk_setup_data;
291
292 if (setup_data->validate_id_call_back) {
293 psk_key = setup_data->validate_id_call_back(&lidentity,
294 c_session,
295 setup_data->id_call_back_arg);
296
297 coap_session_refresh_psk_key(c_session, psk_key);
298 } else {
299 psk_key = coap_get_session_server_psk_key(c_session);
300 }
301
302 if (psk_key == NULL)
303 return -1;
304 mbedtls_ssl_set_hs_psk(ssl, psk_key->s, psk_key->length);
305 m_env->seen_client_hello = 1;
306 return 0;
307}
308#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED && MBEDTLS_SSL_SRV_C */
309
310static char *
311get_san_or_cn_from_cert(mbedtls_x509_crt *crt) {
312 if (crt) {
313 const mbedtls_asn1_named_data *cn_data;
314
315 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
316 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
317 while (seq && seq->buf.p == NULL) {
318 seq = seq->next;
319 }
320 if (seq) {
321 /* Return the Subject Alt Name */
322 return mbedtls_strndup((const char *)seq->buf.p,
323 seq->buf.len);
324 }
325 }
326
327 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
328 MBEDTLS_OID_AT_CN,
329 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
330 if (cn_data) {
331 /* Return the Common Name */
332 return mbedtls_strndup((const char *)cn_data->val.p,
333 cn_data->val.len);
334 }
335 }
336 return NULL;
337}
338
339#if COAP_MAX_LOGGING_LEVEL > 0
340static char *
341get_error_string(int ret) {
342 static char buf[128] = {0};
343 mbedtls_strerror(ret, buf, sizeof(buf)-1);
344 return buf;
345}
346#endif /* COAP_MAX_LOGGING_LEVEL */
347
348static int
349self_signed_cert_verify_callback_mbedtls(void *data,
350 mbedtls_x509_crt *crt COAP_UNUSED,
351 int depth COAP_UNUSED,
352 uint32_t *flags) {
353 const coap_session_t *c_session = (coap_session_t *)data;
354 const coap_mbedtls_context_t *m_context =
355 (coap_mbedtls_context_t *)c_session->context->dtls_context;
356 const coap_dtls_pki_t *setup_data = &m_context->setup_data;
357
358 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
359 if (setup_data->allow_expired_certs) {
360 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
361 }
362 }
363 return 0;
364}
365
366/*
367 * return 0 All OK
368 * -ve Error Code
369 */
370static int
371cert_verify_callback_mbedtls(void *data, mbedtls_x509_crt *crt,
372 int depth, uint32_t *flags) {
373 coap_session_t *c_session = (coap_session_t *)data;
374 coap_mbedtls_context_t *m_context =
375 (coap_mbedtls_context_t *)c_session->context->dtls_context;
376 coap_dtls_pki_t *setup_data = &m_context->setup_data;
377 char *cn = NULL;
378
379 if (*flags == 0)
380 return 0;
381
382 cn = get_san_or_cn_from_cert(crt);
383
384 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
385 if (setup_data->allow_expired_certs) {
386 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
387 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
388 coap_session_str(c_session),
389 "The certificate has expired", cn ? cn : "?", depth);
390 }
391 }
392 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
393 if (setup_data->allow_expired_certs) {
394 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
395 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
396 coap_session_str(c_session),
397 "The certificate has a future date", cn ? cn : "?", depth);
398 }
399 }
400 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
401 if (setup_data->allow_bad_md_hash) {
402 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
403 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
404 coap_session_str(c_session),
405 "The certificate has a bad MD hash", cn ? cn : "?", depth);
406 }
407 }
408 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
409 if (setup_data->allow_short_rsa_length) {
410 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
411 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
412 coap_session_str(c_session),
413 "The certificate has a short RSA length", cn ? cn : "?", depth);
414 }
415 }
416 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
417 uint32_t lflags;
418 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
419 self_signed_cert_verify_callback_mbedtls,
420 data);
421 if (self_signed && depth == 0) {
422 if (setup_data->allow_self_signed &&
423 !setup_data->check_common_ca) {
424 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
425 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
426 coap_session_str(c_session),
427 "Self-signed",
428 cn ? cn : "?", depth);
429 }
430 } else if (self_signed) {
431 if (!setup_data->verify_peer_cert) {
432 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
433 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
434 coap_session_str(c_session),
435 "Self-signed", cn ? cn : "?", depth);
436 }
437 } else {
438 if (!setup_data->verify_peer_cert) {
439 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
440 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
441 coap_session_str(c_session),
442 "The certificate's CA is not trusted", cn ? cn : "?", depth);
443 }
444 }
445 }
446 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
447 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
448 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
449 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
450 coap_session_str(c_session),
451 "The certificate's CRL has expired", cn ? cn : "?", depth);
452 } else if (!setup_data->check_cert_revocation) {
453 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
454 }
455 }
456 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
457 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
458 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
459 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
460 coap_session_str(c_session),
461 "The certificate's CRL has a future date", cn ? cn : "?", depth);
462 } else if (!setup_data->check_cert_revocation) {
463 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
464 }
465 }
466 if (setup_data->cert_chain_validation &&
467 depth > (setup_data->cert_chain_verify_depth + 1)) {
468 *flags |= MBEDTLS_X509_BADCERT_OTHER;
469 coap_log_warn(" %s: %s: '%s' depth %d\n",
470 coap_session_str(c_session),
471 "The certificate's verify depth is too long",
472 cn ? cn : "?", depth);
473 }
474
475 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
476 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
477 }
478 if (setup_data->validate_cn_call_back) {
479 int ret;
480
481 coap_lock_callback_ret(ret, c_session->context,
482 setup_data->validate_cn_call_back(cn,
483 crt->raw.p,
484 crt->raw.len,
485 c_session,
486 depth,
487 *flags == 0,
488 setup_data->cn_call_back_arg));
489 if (!ret) {
490 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
491 }
492 }
493 if (*flags != 0) {
494 char buf[128];
495 char *tcp;
496 int ret = mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", *flags);
497
498 if (ret >= 0) {
499 tcp = strchr(buf, '\n');
500 while (tcp) {
501 *tcp = '\000';
502 coap_log_warn(" %s: %s: issue 0x%" PRIx32 ": '%s' depth %d\n",
503 coap_session_str(c_session),
504 buf, *flags, cn ? cn : "?", depth);
505 tcp = strchr(tcp+1, '\n');
506 }
507 } else {
508 coap_log_err("mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
509 -ret, get_error_string(ret));
510 }
511 }
512
513 if (cn)
514 mbedtls_free(cn);
515
516 return 0;
517}
518
519static int
520setup_pki_credentials(mbedtls_x509_crt *cacert,
521 mbedtls_x509_crt *public_cert,
522 mbedtls_pk_context *private_key,
523 coap_mbedtls_env_t *m_env,
524 coap_mbedtls_context_t *m_context,
525 coap_session_t *c_session,
526 coap_dtls_pki_t *setup_data,
527 coap_dtls_role_t role) {
528 coap_dtls_key_t key;
529 int ret;
530 int done_private_key = 0;
531 int done_public_cert = 0;
532 uint8_t *buffer;
533 size_t length;
534
535 /* Map over to the new define format to save code duplication */
536 coap_dtls_map_key_type_to_define(setup_data, &key);
537
538 assert(key.key_type == COAP_PKI_KEY_DEFINE);
539
540 /*
541 * Configure the Private Key
542 */
543 if (key.key.define.private_key.u_byte &&
544 key.key.define.private_key.u_byte[0]) {
545 switch (key.key.define.private_key_def) {
546 case COAP_PKI_KEY_DEF_DER: /* define private key */
547 /* Fall Through */
548 case COAP_PKI_KEY_DEF_PEM: /* define private key */
549#if defined(MBEDTLS_FS_IO)
550 mbedtls_pk_init(private_key);
551#ifdef MBEDTLS_2_X_COMPAT
552 ret = mbedtls_pk_parse_keyfile(private_key,
553 key.key.define.private_key.s_byte, NULL);
554#else
555 ret = mbedtls_pk_parse_keyfile(private_key,
557 NULL, coap_rng, (void *)&m_env->ctr_drbg);
558#endif /* MBEDTLS_2_X_COMPAT */
559 if (ret < 0) {
562 &key, role, ret);
563 }
564 done_private_key = 1;
565 break;
566#else /* ! MBEDTLS_FS_IO */
569 &key, role, -1);
570#endif /* ! MBEDTLS_FS_IO */
571 case COAP_PKI_KEY_DEF_PEM_BUF: /* define private key */
572 mbedtls_pk_init(private_key);
573 length = key.key.define.private_key_len;
574 if (key.key.define.private_key.u_byte[length-1] != '\000') {
575 /* Need to allocate memory to add in NULL terminator */
576 buffer = mbedtls_malloc(length + 1);
577 if (!buffer) {
578 coap_log_err("mbedtls_malloc failed\n");
579 return 0;
580 }
581 memcpy(buffer, key.key.define.private_key.u_byte, length);
582 buffer[length] = '\000';
583 length++;
584#ifdef MBEDTLS_2_X_COMPAT
585 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
586#else
587 ret = mbedtls_pk_parse_key(private_key, buffer, length,
588 NULL, 0, coap_rng, (void *)&m_env->ctr_drbg);
589#endif /* MBEDTLS_2_X_COMPAT */
590 mbedtls_free(buffer);
591 } else {
592#ifdef MBEDTLS_2_X_COMPAT
593 ret = mbedtls_pk_parse_key(private_key,
595 key.key.define.private_key_len, NULL, 0);
596#else
597 ret = mbedtls_pk_parse_key(private_key,
600 NULL, 0, coap_rng, (void *)&m_env->ctr_drbg);
601#endif /* MBEDTLS_2_X_COMPAT */
602 }
603 if (ret < 0) {
606 &key, role, ret);
607 }
608 done_private_key = 1;
609 break;
610 case COAP_PKI_KEY_DEF_DER_BUF: /* define private key */
611 mbedtls_pk_init(private_key);
612#ifdef MBEDTLS_2_X_COMPAT
613 ret = mbedtls_pk_parse_key(private_key,
615 key.key.define.private_key_len, NULL, 0);
616#else
617 ret = mbedtls_pk_parse_key(private_key,
619 key.key.define.private_key_len, NULL, 0, coap_rng,
620 (void *)&m_env->ctr_drbg);
621#endif /* MBEDTLS_2_X_COMPAT */
622 if (ret < 0) {
625 &key, role, ret);
626 }
627 done_private_key = 1;
628 break;
629 case COAP_PKI_KEY_DEF_RPK_BUF: /* define private key */
630 case COAP_PKI_KEY_DEF_PKCS11: /* define private key */
631 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define private key */
632 case COAP_PKI_KEY_DEF_ENGINE: /* define private key */
633 default:
636 &key, role, -1);
637 }
638 } else if (role == COAP_DTLS_ROLE_SERVER ||
640 key.key.define.public_cert.u_byte[0])) {
643 &key, role, -1);
644 }
645
646 /*
647 * Configure the Public Certificate / Key
648 */
649 if (key.key.define.public_cert.u_byte &&
650 key.key.define.public_cert.u_byte[0]) {
651 switch (key.key.define.public_cert_def) {
652 case COAP_PKI_KEY_DEF_DER: /* define public cert */
653 /* Fall Through */
654 case COAP_PKI_KEY_DEF_PEM: /* define public cert */
655#if defined(MBEDTLS_FS_IO)
656 mbedtls_x509_crt_init(public_cert);
657 ret = mbedtls_x509_crt_parse_file(public_cert,
659 if (ret < 0) {
662 &key, role, ret);
663 }
664 done_public_cert = 1;
665 break;
666#else /* ! MBEDTLS_FS_IO */
669 &key, role, -1);
670#endif /* ! MBEDTLS_FS_IO */
671 case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */
672 mbedtls_x509_crt_init(public_cert);
673
674 length = key.key.define.public_cert_len;
675 if (key.key.define.public_cert.u_byte[length-1] != '\000') {
676 /* Need to allocate memory to add in NULL terminator */
677 buffer = mbedtls_malloc(length + 1);
678 if (!buffer) {
679 coap_log_err("mbedtls_malloc failed\n");
680 return 0;
681 }
682 memcpy(buffer, key.key.define.public_cert.u_byte, length);
683 buffer[length] = '\000';
684 length++;
685 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
686 mbedtls_free(buffer);
687 } else {
688 ret = mbedtls_x509_crt_parse(public_cert,
691 }
692 if (ret < 0) {
695 &key, role, ret);
696 }
697 done_public_cert = 1;
698 break;
699 case COAP_PKI_KEY_DEF_RPK_BUF: /* define public cert */
702 &key, role, -1);
703 case COAP_PKI_KEY_DEF_DER_BUF: /* define public cert */
704 mbedtls_x509_crt_init(public_cert);
705 ret = mbedtls_x509_crt_parse(public_cert,
708 if (ret < 0) {
711 &key, role, ret);
712 }
713 done_public_cert = 1;
714 break;
715 case COAP_PKI_KEY_DEF_PKCS11: /* define public cert */
716 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define public cert */
717 case COAP_PKI_KEY_DEF_ENGINE: /* define public cert */
718 default:
721 &key, role, -1);
722 }
723 } else if (role == COAP_DTLS_ROLE_SERVER ||
725 key.key.define.private_key.u_byte[0])) {
728 &key, role, -1);
729 }
730
731 if (done_private_key && done_public_cert) {
732 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
733 if (ret < 0) {
734 coap_log_err("mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
735 -ret, get_error_string(ret));
736 return 0;
737 }
738 }
739
740 /*
741 * Configure the CA
742 */
743 if (
744#if MBEDTLS_VERSION_NUMBER < 0x03060000
745 setup_data->check_common_ca &&
746#endif /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
747 key.key.define.ca.u_byte &&
748 key.key.define.ca.u_byte[0]) {
749 switch (key.key.define.ca_def) {
750 case COAP_PKI_KEY_DEF_DER: /* define ca */
751 /* Fall Through */
753#if defined(MBEDTLS_FS_IO)
754 mbedtls_x509_crt_init(cacert);
755 ret = mbedtls_x509_crt_parse_file(cacert,
756 key.key.define.ca.s_byte);
757 if (ret < 0) {
760 &key, role, ret);
761 }
762 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
763#else /* ! MBEDTLS_FS_IO */
766 &key, role, -1);
767#endif /* ! MBEDTLS_FS_IO */
768 break;
769 case COAP_PKI_KEY_DEF_PEM_BUF: /* define ca */
770 mbedtls_x509_crt_init(cacert);
771 length = key.key.define.ca_len;
772 if (key.key.define.ca.u_byte[length-1] != '\000') {
773 /* Need to allocate memory to add in NULL terminator */
774 buffer = mbedtls_malloc(length + 1);
775 if (!buffer) {
776 coap_log_err("mbedtls_malloc failed\n");
777 return 0;
778 }
779 memcpy(buffer, key.key.define.ca.u_byte, length);
780 buffer[length] = '\000';
781 length++;
782 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
783 mbedtls_free(buffer);
784 } else {
785 ret = mbedtls_x509_crt_parse(cacert,
786 key.key.define.ca.u_byte,
787 key.key.define.ca_len);
788 }
789 if (ret < 0) {
792 &key, role, ret);
793 }
794 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
795 break;
796 case COAP_PKI_KEY_DEF_RPK_BUF: /* define ca */
799 &key, role, -1);
800 case COAP_PKI_KEY_DEF_DER_BUF: /* define ca */
801 mbedtls_x509_crt_init(cacert);
802 ret = mbedtls_x509_crt_parse(cacert,
803 key.key.define.ca.u_byte,
804 key.key.define.ca_len);
805 if (ret < 0) {
808 &key, role, ret);
809 }
810 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
811 break;
812 case COAP_PKI_KEY_DEF_PKCS11: /* define ca */
813 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define ca */
814 case COAP_PKI_KEY_DEF_ENGINE: /* define ca */
815 default:
818 &key, role, -1);
819 }
820 }
821
822 /* Add in any root CA definitons */
823
824#if defined(MBEDTLS_FS_IO)
825 if (m_context->root_ca_file) {
826 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
827 if (ret < 0) {
831 &key, role, ret);
832 }
833 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
834 }
835 if (m_context->root_ca_path) {
836 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
837 if (ret < 0) {
841 &key, role, ret);
842 }
843 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
844 }
845#else /* ! MBEDTLS_FS_IO */
846 (void)m_context;
849 &key, role, -1);
850#endif /* ! MBEDTLS_FS_IO */
851
852#if defined(MBEDTLS_SSL_SRV_C)
853 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
854 setup_data->check_common_ca ?
855 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
856 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
857#endif
858 mbedtls_ssl_conf_authmode(&m_env->conf, setup_data->verify_peer_cert ?
859 MBEDTLS_SSL_VERIFY_REQUIRED :
860 MBEDTLS_SSL_VERIFY_NONE);
861 /*
862 * Verify Peer.
863 * Need to do all checking, even if setup_data->verify_peer_cert is not set
864 */
865 mbedtls_ssl_conf_verify(&m_env->conf,
866 cert_verify_callback_mbedtls, c_session);
867
868 return 1;
869}
870
871#if defined(MBEDTLS_SSL_SRV_C)
872/*
873 * PKI SNI callback.
874 */
875static int
876pki_sni_callback(void *p_info, mbedtls_ssl_context *ssl,
877 const unsigned char *uname, size_t name_len) {
878 unsigned int i;
879 coap_dtls_pki_t sni_setup_data;
880 coap_session_t *c_session = (coap_session_t *)p_info;
881 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
882 coap_mbedtls_context_t *m_context =
883 (coap_mbedtls_context_t *)c_session->context->dtls_context;
884 char *name;
885
886 name = mbedtls_malloc(name_len+1);
887 if (!name)
888 return -1;
889
890 memcpy(name, uname, name_len);
891 name[name_len] = '\000';
892
893 /* Is this a cached entry? */
894 for (i = 0; i < m_context->pki_sni_count; i++) {
895 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
896 break;
897 }
898 }
899 if (i == m_context->pki_sni_count) {
900 /*
901 * New PKI SNI request
902 */
903 coap_dtls_key_t *new_entry;
904 pki_sni_entry *pki_sni_entry_list;
905
906 coap_lock_callback_ret(new_entry, c_session->context,
907 m_context->setup_data.validate_sni_call_back(name,
908 m_context->setup_data.sni_call_back_arg));
909 if (!new_entry) {
910 mbedtls_free(name);
911 return -1;
912 }
913
914 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
915 (i+1)*sizeof(pki_sni_entry));
916
917 if (pki_sni_entry_list == NULL) {
918 mbedtls_free(name);
919 return -1;
920 }
921 m_context->pki_sni_entry_list = pki_sni_entry_list;
922 memset(&m_context->pki_sni_entry_list[i], 0,
923 sizeof(m_context->pki_sni_entry_list[i]));
924 m_context->pki_sni_entry_list[i].sni = name;
925 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
926 sni_setup_data = m_context->setup_data;
927 sni_setup_data.pki_key = *new_entry;
928 if (setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
929 &m_context->pki_sni_entry_list[i].public_cert,
930 &m_context->pki_sni_entry_list[i].private_key,
931 m_env,
932 m_context,
933 c_session,
934 &sni_setup_data, COAP_DTLS_ROLE_SERVER) < 0) {
935 mbedtls_free(name);
936 return -1;
937 }
938 /* name has been absorbed into pki_sni_entry_list[].sni entry */
939 m_context->pki_sni_count++;
940 } else {
941 mbedtls_free(name);
942 }
943
944 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
945 NULL);
946 return mbedtls_ssl_set_hs_own_cert(ssl,
947 &m_context->pki_sni_entry_list[i].public_cert,
948 &m_context->pki_sni_entry_list[i].private_key);
949}
950
951#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
952/*
953 * PSK SNI callback.
954 */
955static int
956psk_sni_callback(void *p_info, mbedtls_ssl_context *ssl,
957 const unsigned char *uname, size_t name_len) {
958 unsigned int i;
959 coap_session_t *c_session = (coap_session_t *)p_info;
960 coap_mbedtls_context_t *m_context =
961 (coap_mbedtls_context_t *)c_session->context->dtls_context;
962 char *name;
963
964 name = mbedtls_malloc(name_len+1);
965 if (!name)
966 return -1;
967
968 memcpy(name, uname, name_len);
969 name[name_len] = '\000';
970
971 /* Is this a cached entry? */
972 for (i = 0; i < m_context->psk_sni_count; i++) {
973 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
974 break;
975 }
976 }
977 if (i == m_context->psk_sni_count) {
978 /*
979 * New PSK SNI request
980 */
981 const coap_dtls_spsk_info_t *new_entry;
982 psk_sni_entry *psk_sni_entry_list;
983
984 coap_lock_callback_ret(new_entry, c_session->context,
986 c_session,
988 if (!new_entry) {
989 mbedtls_free(name);
990 return -1;
991 }
992
993 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
994 (i+1)*sizeof(psk_sni_entry));
995
996 if (psk_sni_entry_list == NULL) {
997 mbedtls_free(name);
998 return -1;
999 }
1000 m_context->psk_sni_entry_list = psk_sni_entry_list;
1001 m_context->psk_sni_entry_list[i].sni = name;
1002 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1003 /* name has been absorbed into psk_sni_entry_list[].sni entry */
1004 m_context->psk_sni_count++;
1005 } else {
1006 mbedtls_free(name);
1007 }
1008
1010 &m_context->psk_sni_entry_list[i].psk_info.hint);
1012 &m_context->psk_sni_entry_list[i].psk_info.key);
1013 return mbedtls_ssl_set_hs_psk(ssl,
1014 m_context->psk_sni_entry_list[i].psk_info.key.s,
1015 m_context->psk_sni_entry_list[i].psk_info.key.length);
1016}
1017#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1018
1019static int
1020setup_server_ssl_session(coap_session_t *c_session,
1021 coap_mbedtls_env_t *m_env) {
1022 coap_mbedtls_context_t *m_context =
1023 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1024 int ret = 0;
1025 m_context->psk_pki_enabled |= IS_SERVER;
1026
1027 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
1028 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1029 MBEDTLS_SSL_IS_SERVER,
1030 c_session->proto == COAP_PROTO_DTLS ?
1031 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1032 MBEDTLS_SSL_TRANSPORT_STREAM,
1033 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1034 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1035 -ret, get_error_string(ret));
1036 goto fail;
1037 }
1038
1039 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1040
1041#if defined(MBEDTLS_SSL_PROTO_DTLS)
1042 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1043 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1044#endif /* MBEDTLS_SSL_PROTO_DTLS */
1045
1046 if (m_context->psk_pki_enabled & IS_PSK) {
1047#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1048 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1050 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1051 }
1052#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1053 m_env->ec_jpake = c_session->context->spsk_setup_data.ec_jpake;
1054#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1055#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1056 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1057#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1058 }
1059
1060 if (m_context->psk_pki_enabled & IS_PKI) {
1061 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1062 &m_env->private_key, m_env, m_context,
1063 c_session, &m_context->setup_data,
1065 if (ret < 0) {
1066 coap_log_err("PKI setup failed\n");
1067 return ret;
1068 }
1069 if (m_context->setup_data.validate_sni_call_back) {
1070 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1071 }
1072 }
1073
1074 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1075 mbedtls_ctr_drbg_random,
1076 &m_env->ctr_drbg)) != 0) {
1077 coap_log_err("mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1078 -ret, get_error_string(ret));
1079 goto fail;
1080 }
1081
1082#if defined(MBEDTLS_SSL_PROTO_DTLS)
1083 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1084 mbedtls_ssl_cookie_check,
1085 &m_env->cookie_ctx);
1086#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1087 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1088#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1089#endif /* MBEDTLS_SSL_PROTO_DTLS */
1090#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1091 /*
1092 * Configure CID max length.
1093 *
1094 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
1095 * to use RFC9146 extension ID of 54, rather than the draft version -05
1096 * value of 254.
1097 */
1098 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1099#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1100fail:
1101 return ret;
1102}
1103#endif /* MBEDTLS_SSL_SRV_C */
1104
1105#if COAP_CLIENT_SUPPORT
1106static int *psk_ciphers = NULL;
1107static int *pki_ciphers = NULL;
1108static int *ecjpake_ciphers = NULL;
1109static int processed_ciphers = 0;
1110
1111#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1112static int
1113coap_ssl_ciphersuite_uses_psk(const mbedtls_ssl_ciphersuite_t *info) {
1114#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1115 switch (info->key_exchange) {
1116 case MBEDTLS_KEY_EXCHANGE_PSK:
1117 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1118 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1119 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1120 return 1;
1121 case MBEDTLS_KEY_EXCHANGE_NONE:
1122 case MBEDTLS_KEY_EXCHANGE_RSA:
1123 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1124 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1125 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1126 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1127 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1128 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1129 default:
1130 return 0;
1131 }
1132#else /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1133 return mbedtls_ssl_ciphersuite_uses_psk(info);
1134#endif /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1135}
1136#endif /* defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) */
1137
1138static void
1139set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1140 if (!processed_ciphers) {
1141 const int *list = mbedtls_ssl_list_ciphersuites();
1142 const int *base = list;
1143 int *psk_list;
1144 int *pki_list;
1145#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1146 int *ecjpake_list;
1147 int ecjpake_count = 1;
1148#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1149 int psk_count = 1; /* account for empty terminator */
1150 int pki_count = 1;
1151
1152 while (*list) {
1153 const mbedtls_ssl_ciphersuite_t *cur =
1154 mbedtls_ssl_ciphersuite_from_id(*list);
1155
1156 if (cur) {
1157#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1158 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1159 /* Minimum of TLS1.2 required - skip */
1160 }
1161#else
1162 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1163 /* Minimum of TLS1.2 required - skip */
1164 }
1165#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1166#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1167 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1168 ecjpake_count++;
1169 }
1170#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1171#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1172 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1173 psk_count++;
1174 pki_count++;
1175 }
1176#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
1177#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1178 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1179 psk_count++;
1180 }
1181#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1182 else {
1183 pki_count++;
1184 }
1185 }
1186 list++;
1187 }
1188 list = base;
1189
1190 psk_ciphers = mbedtls_malloc(psk_count * sizeof(psk_ciphers[0]));
1191 if (psk_ciphers == NULL) {
1192 coap_log_err("set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1193 return;
1194 }
1195 pki_ciphers = mbedtls_malloc(pki_count * sizeof(pki_ciphers[0]));
1196 if (pki_ciphers == NULL) {
1197 coap_log_err("set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1198 mbedtls_free(psk_ciphers);
1199 psk_ciphers = NULL;
1200 return;
1201 }
1202#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1203 ecjpake_ciphers = mbedtls_malloc(ecjpake_count * sizeof(ecjpake_ciphers[0]));
1204 if (ecjpake_ciphers == NULL) {
1205 coap_log_err("set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1206 mbedtls_free(psk_ciphers);
1207 mbedtls_free(pki_ciphers);
1208 psk_ciphers = NULL;
1209 pki_ciphers = NULL;
1210 return;
1211 }
1212#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1213
1214 psk_list = psk_ciphers;
1215 pki_list = pki_ciphers;
1216#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1217 ecjpake_list = ecjpake_ciphers;
1218#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1219
1220 while (*list) {
1221 const mbedtls_ssl_ciphersuite_t *cur =
1222 mbedtls_ssl_ciphersuite_from_id(*list);
1223 if (cur) {
1224#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1225 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1226 /* Minimum of TLS1.2 required - skip */
1227 }
1228#else
1229 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1230 /* Minimum of TLS1.2 required - skip */
1231 }
1232#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1233#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1234 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1235 *ecjpake_list = *list;
1236 ecjpake_list++;
1237 }
1238#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1239#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1240 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1241 *psk_list = *list;
1242 psk_list++;
1243 *pki_list = *list;
1244 pki_list++;
1245 }
1246#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
1247#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1248 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1249 *psk_list = *list;
1250 psk_list++;
1251 }
1252#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1253 else {
1254 *pki_list = *list;
1255 pki_list++;
1256 }
1257 }
1258 list++;
1259 }
1260 /* zero terminate */
1261 *psk_list = 0;
1262 *pki_list = 0;
1263 processed_ciphers = 1;
1264 }
1265 switch (method) {
1266 case COAP_ENC_PSK:
1267 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1268 break;
1269 case COAP_ENC_PKI:
1270 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1271 break;
1272 case COAP_ENC_ECJPAKE:
1273 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1274 break;
1275 default:
1276 assert(0);
1277 break;
1278 }
1279}
1280
1281static int
1282setup_client_ssl_session(coap_session_t *c_session,
1283 coap_mbedtls_env_t *m_env) {
1284 int ret;
1285
1286 coap_mbedtls_context_t *m_context =
1287 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1288
1289 m_context->psk_pki_enabled |= IS_CLIENT;
1290
1291 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1292 MBEDTLS_SSL_IS_CLIENT,
1293 c_session->proto == COAP_PROTO_DTLS ?
1294 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1295 MBEDTLS_SSL_TRANSPORT_STREAM,
1296 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1297 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1298 -ret, get_error_string(ret));
1299 goto fail;
1300 }
1301
1302#if defined(MBEDTLS_SSL_PROTO_DTLS)
1303 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1304 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1305#endif /* MBEDTLS_SSL_PROTO_DTLS */
1306
1307 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1308 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1309
1310 if (m_context->psk_pki_enabled & IS_PSK) {
1311#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1312 const coap_bin_const_t *psk_key;
1313 const coap_bin_const_t *psk_identity;
1314
1315 coap_log_info("Setting PSK key\n");
1316
1317 psk_key = coap_get_session_client_psk_key(c_session);
1318 psk_identity = coap_get_session_client_psk_identity(c_session);
1319 if (psk_key == NULL || psk_identity == NULL) {
1320 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1321 goto fail;
1322 }
1323
1324 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->s,
1325 psk_key->length, psk_identity->s,
1326 psk_identity->length)) != 0) {
1327 coap_log_err("mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1328 -ret, get_error_string(ret));
1329 goto fail;
1330 }
1331 if (c_session->cpsk_setup_data.client_sni) {
1332 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1333 c_session->cpsk_setup_data.client_sni)) != 0) {
1334 coap_log_err("mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1335 -ret, get_error_string(ret));
1336 goto fail;
1337 }
1338 }
1339 /* Identity Hint currently not supported in Mbed TLS so code removed */
1340
1341#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1342 if (c_session->cpsk_setup_data.ec_jpake) {
1343 m_env->ec_jpake = 1;
1344 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1345#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1346 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1347#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1348 } else {
1349 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1350 }
1351#else /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1352 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1353#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1354#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1355 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1356#endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1357 } else if ((m_context->psk_pki_enabled & IS_PKI) ||
1358 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1359 /*
1360 * If neither PSK or PKI have been set up, use PKI basics.
1361 * This works providing COAP_PKI_KEY_PEM has a value of 0.
1362 */
1363 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1364 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1365 &m_env->private_key, m_env, m_context,
1366 c_session, &m_context->setup_data,
1368 if (ret < 0) {
1369 coap_log_err("PKI setup failed\n");
1370 return ret;
1371 }
1372#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1373 if (c_session->proto == COAP_PROTO_TLS ||
1374 c_session->proto == COAP_PROTO_WSS) {
1375 static const char *alpn_list[] = { "coap", NULL };
1376
1377 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1378 if (ret != 0) {
1379 coap_log_err("ALPN setup failed %d)\n", ret);
1380 }
1381 }
1382#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN */
1383 if (m_context->setup_data.client_sni) {
1384 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1385 }
1386#if defined(MBEDTLS_SSL_PROTO_DTLS)
1387#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1388 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1389#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1390#endif /* MBEDTLS_SSL_PROTO_DTLS */
1391 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1392 }
1393 return 0;
1394
1395fail:
1396 return ret;
1397}
1398#endif /* COAP_CLIENT_SUPPORT */
1399
1400static void
1401mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1402 if (!m_env) {
1403 return;
1404 }
1405
1406 mbedtls_x509_crt_free(&m_env->cacert);
1407 mbedtls_x509_crt_free(&m_env->public_cert);
1408 mbedtls_pk_free(&m_env->private_key);
1409 mbedtls_entropy_free(&m_env->entropy);
1410 mbedtls_ssl_config_free(&m_env->conf);
1411 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1412 mbedtls_ssl_free(&m_env->ssl);
1413 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1414}
1415
1416static void
1417coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1418 if (m_env) {
1419 if (!m_env->sent_alert)
1420 mbedtls_ssl_close_notify(&m_env->ssl);
1421 mbedtls_cleanup(m_env);
1422 mbedtls_free(m_env);
1423 }
1424}
1425
1426#if COAP_MAX_LOGGING_LEVEL > 0
1427static const char *
1428report_mbedtls_alert(unsigned char alert) {
1429 switch (alert) {
1430 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1431 return ": Bad Record MAC";
1432 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1433 return ": Handshake failure";
1434 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1435 return ": No Certificate provided";
1436 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1437 return ": Certificate is bad";
1438 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1439 return ": Certificate is unknown";
1440 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1441 return ": CA is unknown";
1442 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1443 return ": Access was denied";
1444 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1445 return ": Decrypt error";
1446 default:
1447 return "";
1448 }
1449}
1450#endif /* COAP_MAX_LOGGING_LEVEL */
1451
1452/*
1453 * return -1 failure
1454 * 0 not completed
1455 * 1 established
1456 */
1457static int
1458do_mbedtls_handshake(coap_session_t *c_session,
1459 coap_mbedtls_env_t *m_env) {
1460 int ret;
1461 int alert;
1462
1463 ret = mbedtls_ssl_handshake(&m_env->ssl);
1464 switch (ret) {
1465 case 0:
1466 m_env->established = 1;
1467 coap_log_debug("* %s: Mbed TLS established\n",
1468 coap_session_str(c_session));
1469 ret = 1;
1470#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1471#if COAP_CLIENT_SUPPORT
1472 if (c_session->type == COAP_SESSION_TYPE_CLIENT &&
1473 c_session->proto == COAP_PROTO_DTLS) {
1474 coap_mbedtls_context_t *m_context;
1475
1476 m_context = (coap_mbedtls_context_t *)c_session->context->dtls_context;
1477 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
1478 m_context->setup_data.use_cid) {
1479 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1480 int enabled;
1481 size_t peer_cid_len;
1482
1483 /* See whether CID was negotiated */
1484 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1485 enabled == MBEDTLS_SSL_CID_ENABLED) {
1486 c_session->negotiated_cid = 1;
1487 } else {
1488 coap_log_info("** %s: CID was not negotiated\n", coap_session_str(c_session));
1489 c_session->negotiated_cid = 0;
1490 }
1491 }
1492 }
1493#endif /* COAP_CLIENT_SUPPORT */
1494#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1495 break;
1496 case MBEDTLS_ERR_SSL_WANT_READ:
1497 case MBEDTLS_ERR_SSL_WANT_WRITE:
1498 errno = EAGAIN;
1499 ret = 0;
1500 break;
1501 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1502 coap_log_debug("hello verification requested\n");
1503 goto reset;
1504 case MBEDTLS_ERR_SSL_INVALID_MAC:
1505 goto fail;
1506#ifdef MBEDTLS_2_X_COMPAT
1507 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1508#else /* ! MBEDTLS_2_X_COMPAT */
1509 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1510#endif /* ! MBEDTLS_2_X_COMPAT */
1511 goto fail;
1512 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1513 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1514 goto fail_alert;
1515#ifdef MBEDTLS_2_X_COMPAT
1516 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1517 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1518 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1519 goto fail_alert;
1520#endif /* MBEDTLS_2_X_COMPAT */
1521 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1522 goto fail;
1523 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1524 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1525 coap_log_warn("***%s: Alert '%d'%s\n",
1526 coap_session_str(c_session), m_env->ssl.in_msg[1],
1527 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1528 /* Fall through */
1529 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1530 case MBEDTLS_ERR_SSL_CONN_EOF:
1531 case MBEDTLS_ERR_NET_CONN_RESET:
1533 ret = -1;
1534 break;
1535 default:
1536 coap_log_warn("do_mbedtls_handshake: session establish "
1537 "returned -0x%x: '%s'\n",
1538 -ret, get_error_string(ret));
1539 ret = -1;
1540 break;
1541 }
1542 return ret;
1543
1544fail_alert:
1545 mbedtls_ssl_send_alert_message(&m_env->ssl,
1546 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1547 alert);
1548 m_env->sent_alert = 1;
1549fail:
1550 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
1551 coap_log_warn("do_mbedtls_handshake: session establish "
1552 "returned '%s'\n",
1553 get_error_string(ret));
1554reset:
1555 mbedtls_ssl_session_reset(&m_env->ssl);
1556#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1557 if (m_env->ec_jpake) {
1558 const coap_bin_const_t *psk_key;
1559
1560#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1561 if (c_session->type == COAP_SESSION_TYPE_CLIENT) {
1562 psk_key = coap_get_session_client_psk_key(c_session);
1563 } else {
1564 psk_key = coap_get_session_server_psk_key(c_session);
1565 }
1566#elif COAP_CLIENT_SUPPORT
1567 psk_key = coap_get_session_client_psk_key(c_session);
1568#else /* COAP_SERVER_SUPPORT */
1569 psk_key = coap_get_session_server_psk_key(c_session);
1570#endif /* COAP_SERVER_SUPPORT */
1571 if (psk_key) {
1572 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
1573 }
1574 }
1575#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1576 return -1;
1577}
1578
1579static void
1580mbedtls_debug_out(void *ctx COAP_UNUSED, int level,
1581 const char *file COAP_UNUSED,
1582 int line COAP_UNUSED, const char *str) {
1583
1584 coap_log_t coap_level = COAP_LOG_DEBUG;
1585 /*
1586 * 0 No debug
1587 * 1 Error
1588 * 2 State change
1589 * 3 Informational
1590 * 4 Verbose
1591 */
1592 switch (level) {
1593 case 0:
1594 coap_level = COAP_LOG_EMERG;
1595 break;
1596 case 1:
1597 coap_level = COAP_LOG_WARN;
1598 break;
1599 case 2:
1600 coap_level = COAP_LOG_NOTICE;
1601 break;
1602 case 3:
1603 coap_level = COAP_LOG_INFO;
1604 break;
1605 case 4:
1606 default:
1607 coap_level = COAP_LOG_DEBUG;
1608 break;
1609 }
1610 coap_dtls_log(coap_level, "%s", str);
1611}
1612
1613#if !COAP_DISABLE_TCP
1614/*
1615 * strm
1616 * return +ve data amount
1617 * 0 no more
1618 * -ve Mbed TLS error
1619 */
1620static int
1621coap_sock_read(void *ctx, unsigned char *out, size_t outl) {
1622 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
1623 coap_session_t *c_session = (coap_session_t *)ctx;
1624
1625 if (out != NULL) {
1626 ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_read(c_session, out, outl);
1627 /* Translate layer returns into what MbedTLS expects */
1628 if (ret == -1) {
1629 if (errno == ECONNRESET) {
1630 /* graceful shutdown */
1631 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1632 } else {
1633 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1634 }
1635 } else if (ret == 0) {
1636 errno = EAGAIN;
1637 ret = MBEDTLS_ERR_SSL_WANT_READ;
1638 }
1639 }
1640 return ret;
1641}
1642
1643/*
1644 * strm
1645 * return +ve data amount
1646 * 0 no more
1647 * -ve Mbed TLS error
1648 */
1649static int
1650coap_sock_write(void *context, const unsigned char *in, size_t inl) {
1651 int ret = 0;
1652 coap_session_t *c_session = (coap_session_t *)context;
1653
1654 ret = c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
1655 (const uint8_t *)in,
1656 inl);
1657 /* Translate layer what returns into what MbedTLS expects */
1658 if (ret < 0) {
1659 if ((c_session->state == COAP_SESSION_STATE_CSM ||
1660 c_session->state == COAP_SESSION_STATE_HANDSHAKE) &&
1661 (errno == EPIPE || errno == ECONNRESET)) {
1662 /*
1663 * Need to handle a TCP timing window where an agent continues with
1664 * the sending of the next handshake or a CSM.
1665 * However, the peer does not like a certificate and so sends a
1666 * fatal alert and closes the TCP session.
1667 * The sending of the next handshake or CSM may get terminated because
1668 * of the closed TCP session, but there is still an outstanding alert
1669 * to be read in and reported on.
1670 * In this case, pretend that sending the info was fine so that the
1671 * alert can be read (which effectively is what happens with DTLS).
1672 */
1673 ret = inl;
1674 } else {
1675#ifdef _WIN32
1676 int lasterror = WSAGetLastError();
1677
1678 if (lasterror == WSAEWOULDBLOCK) {
1679 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1680 } else if (lasterror == WSAECONNRESET) {
1681 ret = MBEDTLS_ERR_NET_CONN_RESET;
1682 }
1683#else
1684 if (errno == EAGAIN || errno == EINTR) {
1685 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1686 } else if (errno == EPIPE || errno == ECONNRESET) {
1687 ret = MBEDTLS_ERR_NET_CONN_RESET;
1688 }
1689#endif
1690 else {
1691 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1692 }
1693 coap_log_debug("* %s: failed to send %zd bytes (%s) state %d\n",
1694 coap_session_str(c_session), inl, coap_socket_strerror(),
1695 c_session->state);
1696 }
1697 }
1698 if (ret == 0) {
1699 errno = EAGAIN;
1700 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1701 }
1702 return ret;
1703}
1704#endif /* !COAP_DISABLE_TCP */
1705
1706static coap_mbedtls_env_t *
1707coap_dtls_new_mbedtls_env(coap_session_t *c_session,
1708 coap_dtls_role_t role,
1709 coap_proto_t proto) {
1710 int ret = 0;
1711 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
1712
1713 if (m_env)
1714 return m_env;
1715
1716 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(sizeof(coap_mbedtls_env_t));
1717 if (!m_env) {
1718 return NULL;
1719 }
1720 memset(m_env, 0, sizeof(coap_mbedtls_env_t));
1721
1722 mbedtls_ssl_init(&m_env->ssl);
1723 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1724 mbedtls_ssl_config_init(&m_env->conf);
1725 mbedtls_entropy_init(&m_env->entropy);
1726
1727#if defined(MBEDTLS_PSA_CRYPTO_C)
1728 psa_crypto_init();
1729#endif /* MBEDTLS_PSA_CRYPTO_C */
1730
1731#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1732 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1733#endif /* ESPIDF_VERSION && CONFIG_MBEDTLS_DEBUG */
1734 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1735 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1736 coap_log_err("mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1737 -ret, get_error_string(ret));
1738 goto fail;
1739 }
1740
1741 if (role == COAP_DTLS_ROLE_CLIENT) {
1742#if COAP_CLIENT_SUPPORT
1743 if (setup_client_ssl_session(c_session, m_env) != 0) {
1744 goto fail;
1745 }
1746#else /* !COAP_CLIENT_SUPPORT */
1747 goto fail;
1748#endif /* !COAP_CLIENT_SUPPORT */
1749 } else if (role == COAP_DTLS_ROLE_SERVER) {
1750#if defined(MBEDTLS_SSL_SRV_C)
1751 if (setup_server_ssl_session(c_session, m_env) != 0) {
1752 goto fail;
1753 }
1754#else /* ! MBEDTLS_SSL_SRV_C */
1755 goto fail;
1756#endif /* ! MBEDTLS_SSL_SRV_C */
1757 } else {
1758 goto fail;
1759 }
1760
1761#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1762 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1763#else
1764 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1765 MBEDTLS_SSL_MINOR_VERSION_3);
1766#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1767
1768 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
1769 goto fail;
1770 }
1771 if (proto == COAP_PROTO_DTLS) {
1772 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1773 coap_dgram_read, NULL);
1774#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1775 if (COAP_PROTO_NOT_RELIABLE(c_session->proto)) {
1776 if (role == COAP_DTLS_ROLE_CLIENT) {
1777#if COAP_CLIENT_SUPPORT
1778 coap_mbedtls_context_t *m_context =
1779 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1780
1781 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
1782 m_context->setup_data.use_cid) {
1783 /*
1784 * Enable passive DTLS CID support.
1785 *
1786 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
1787 * to use RFC9146 extension ID of 54, rather than the draft version -05
1788 * value of 254.
1789 */
1790 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
1791 }
1792#endif /* COAP_CLIENT_SUPPORT */
1793 } else {
1794#if COAP_SERVER_SUPPORT
1795 u_char cid[COAP_DTLS_CID_LENGTH];
1796 /*
1797 * Enable server DTLS CID support.
1798 *
1799 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
1800 * to use RFC9146 extension ID of 54, rather than the draft version -05
1801 * value of 254.
1802 */
1803 coap_prng_lkd(cid, sizeof(cid));
1804 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
1805 sizeof(cid));
1806 c_session->client_cid = coap_new_bin_const(cid, sizeof(cid));
1807#endif /* COAP_SERVER_SUPPORT */
1808 }
1809 }
1810#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1811 }
1812#if !COAP_DISABLE_TCP
1813 else {
1814 assert(proto == COAP_PROTO_TLS);
1815 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1816 coap_sock_read, NULL);
1817 }
1818#endif /* ! COAP_DISABLE_TCP */
1819#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1820 coap_mbedtls_context_t *m_context =
1821 ((coap_mbedtls_context_t *)c_session->context->dtls_context);
1822 if ((m_context->psk_pki_enabled & IS_PSK) &&
1823 m_env->ec_jpake) {
1824 const coap_bin_const_t *psk_key;
1825
1826#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1827 if (role == COAP_DTLS_ROLE_CLIENT) {
1828 psk_key = coap_get_session_client_psk_key(c_session);
1829 } else {
1830 psk_key = coap_get_session_server_psk_key(c_session);
1831 }
1832#elif COAP_CLIENT_SUPPORT
1833 psk_key = coap_get_session_client_psk_key(c_session);
1834#else /* COAP_SERVER_SUPPORT */
1835 psk_key = coap_get_session_server_psk_key(c_session);
1836#endif /* COAP_SERVER_SUPPORT */
1837 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
1838 }
1839#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1840 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1841 mbedtls_timing_set_delay,
1842 mbedtls_timing_get_delay);
1843
1844 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1845 return m_env;
1846
1847fail:
1848 if (m_env) {
1849 mbedtls_free(m_env);
1850 }
1851 return NULL;
1852}
1853
1854int
1856#if defined(MBEDTLS_SSL_PROTO_DTLS)
1857 return 1;
1858#else /* !MBEDTLS_SSL_PROTO_DTLS */
1859 static int reported = 0;
1860 if (!reported) {
1861 reported = 1;
1862 coap_log_emerg("libcoap not compiled for DTLS with Mbed TLS"
1863 " - update Mbed TLS to include DTLS\n");
1864 }
1865 return 0;
1866#endif /* !MBEDTLS_SSL_PROTO_DTLS */
1867}
1868
1869int
1871#if !COAP_DISABLE_TCP
1872 return 1;
1873#else /* COAP_DISABLE_TCP */
1874 return 0;
1875#endif /* COAP_DISABLE_TCP */
1876}
1877
1878/*
1879 * return 0 failed
1880 * 1 passed
1881 */
1882int
1884 return 1;
1885}
1886
1887/*
1888 * return 0 failed
1889 * 1 passed
1890 */
1891int
1893 return 1;
1894}
1895
1896/*
1897 * return 0 failed
1898 * 1 passed
1899 */
1900int
1902 return 0;
1903}
1904
1905/*
1906 * return 0 failed
1907 * 1 passed
1908 */
1909int
1911 return 0;
1912}
1913
1914/*
1915 * return 0 failed
1916 * 1 passed
1917 */
1918int
1920#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1921 return 1;
1922#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
1923 return 0;
1924#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
1925}
1926
1927#if COAP_CLIENT_SUPPORT
1928int
1929coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
1930#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1931 c_context->testing_cids = every;
1932 return 1;
1933#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
1934 (void)c_context;
1935 (void)every;
1936 return 0;
1937#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
1938}
1939#endif /* COAP_CLIENT_SUPPORT */
1940
1941void *
1943 coap_mbedtls_context_t *m_context;
1944 (void)c_context;
1945
1946 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(sizeof(coap_mbedtls_context_t));
1947 if (m_context) {
1948 memset(m_context, 0, sizeof(coap_mbedtls_context_t));
1949 }
1950 return m_context;
1951}
1952
1953#if COAP_SERVER_SUPPORT
1954/*
1955 * return 0 failed
1956 * 1 passed
1957 */
1958int
1960 coap_dtls_spsk_t *setup_data
1961 ) {
1962 coap_mbedtls_context_t *m_context =
1963 ((coap_mbedtls_context_t *)c_context->dtls_context);
1964
1965#if !defined(MBEDTLS_SSL_SRV_C)
1966 coap_log_emerg("coap_context_set_spsk:"
1967 " libcoap not compiled for Server Mode for Mbed TLS"
1968 " - update Mbed TLS to include Server Mode\n");
1969 return 0;
1970#endif /* !MBEDTLS_SSL_SRV_C */
1971 if (!m_context || !setup_data)
1972 return 0;
1973
1974 if (setup_data->ec_jpake) {
1975#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1976 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
1977#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1978 }
1979 m_context->psk_pki_enabled |= IS_PSK;
1980 return 1;
1981}
1982#endif /* COAP_SERVER_SUPPORT */
1983
1984#if COAP_CLIENT_SUPPORT
1985/*
1986 * return 0 failed
1987 * 1 passed
1988 */
1989int
1991 coap_dtls_cpsk_t *setup_data
1992 ) {
1993#if !defined(MBEDTLS_SSL_CLI_C)
1994 (void)c_context;
1995 (void)setup_data;
1996
1997 coap_log_emerg("coap_context_set_cpsk:"
1998 " libcoap not compiled for Client Mode for Mbed TLS"
1999 " - update Mbed TLS to include Client Mode\n");
2000 return 0;
2001#else /* MBEDTLS_SSL_CLI_C */
2002 coap_mbedtls_context_t *m_context =
2003 ((coap_mbedtls_context_t *)c_context->dtls_context);
2004
2005 if (!m_context || !setup_data)
2006 return 0;
2007
2008 if (setup_data->validate_ih_call_back) {
2009 coap_log_warn("CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2010 }
2011 if (setup_data->ec_jpake) {
2012#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2013 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2014#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2015 }
2016 if (setup_data->use_cid) {
2017#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2018 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2019#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2020 }
2021 m_context->psk_pki_enabled |= IS_PSK;
2022 return 1;
2023#endif /* MBEDTLS_SSL_CLI_C */
2024}
2025#endif /* COAP_CLIENT_SUPPORT */
2026
2027int
2029 const coap_dtls_pki_t *setup_data,
2030 const coap_dtls_role_t role COAP_UNUSED) {
2031 coap_mbedtls_context_t *m_context =
2032 ((coap_mbedtls_context_t *)c_context->dtls_context);
2033
2034 m_context->setup_data = *setup_data;
2035 if (!m_context->setup_data.verify_peer_cert) {
2036 /* Needs to be clear so that no CA DNs are transmitted */
2037 m_context->setup_data.check_common_ca = 0;
2038 /* Allow all of these but warn if issue */
2039 m_context->setup_data.allow_self_signed = 1;
2040 m_context->setup_data.allow_expired_certs = 1;
2041 m_context->setup_data.cert_chain_validation = 1;
2042 m_context->setup_data.cert_chain_verify_depth = 10;
2043 m_context->setup_data.check_cert_revocation = 1;
2044 m_context->setup_data.allow_no_crl = 1;
2045 m_context->setup_data.allow_expired_crl = 1;
2046 m_context->setup_data.allow_bad_md_hash = 1;
2047 m_context->setup_data.allow_short_rsa_length = 1;
2048 }
2049 m_context->psk_pki_enabled |= IS_PKI;
2050 if (setup_data->use_cid) {
2051#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2052 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2053#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2054 }
2055 return 1;
2056}
2057
2058int
2060 const char *ca_file,
2061 const char *ca_path) {
2062 coap_mbedtls_context_t *m_context =
2063 ((coap_mbedtls_context_t *)c_context->dtls_context);
2064
2065 if (!m_context) {
2066 coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
2067 "not set up\n");
2068 return 0;
2069 }
2070
2071 if (ca_file == NULL && ca_path == NULL) {
2072 coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path "
2073 "not defined\n");
2074 return 0;
2075 }
2076 if (m_context->root_ca_file) {
2077 mbedtls_free(m_context->root_ca_file);
2078 m_context->root_ca_file = NULL;
2079 }
2080
2081 if (ca_file) {
2082 m_context->root_ca_file = mbedtls_strdup(ca_file);
2083 }
2084
2085 if (m_context->root_ca_path) {
2086 mbedtls_free(m_context->root_ca_path);
2087 m_context->root_ca_path = NULL;
2088 }
2089
2090 if (ca_path) {
2091 m_context->root_ca_path = mbedtls_strdup(ca_path);
2092 }
2093 return 1;
2094}
2095
2096int
2098 coap_mbedtls_context_t *m_context =
2099 ((coap_mbedtls_context_t *)c_context->dtls_context);
2100 return m_context->psk_pki_enabled ? 1 : 0;
2101}
2102
2103void
2104coap_dtls_free_context(void *dtls_context) {
2105 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2106 unsigned int i;
2107
2108 for (i = 0; i < m_context->pki_sni_count; i++) {
2109 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
2110
2111 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
2112
2113 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
2114
2115 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
2116 }
2117 if (m_context->pki_sni_entry_list)
2118 mbedtls_free(m_context->pki_sni_entry_list);
2119
2120 for (i = 0; i < m_context->psk_sni_count; i++) {
2121 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2122 }
2123 if (m_context->psk_sni_entry_list)
2124 mbedtls_free(m_context->psk_sni_entry_list);
2125
2126 if (m_context->root_ca_path)
2127 mbedtls_free(m_context->root_ca_path);
2128 if (m_context->root_ca_file)
2129 mbedtls_free(m_context->root_ca_file);
2130
2131 mbedtls_free(m_context);
2132}
2133
2134#if COAP_CLIENT_SUPPORT
2135void *
2137#if !defined(MBEDTLS_SSL_CLI_C)
2138 (void)c_session;
2139 coap_log_emerg("coap_dtls_new_client_session:"
2140 " libcoap not compiled for Client Mode for Mbed TLS"
2141 " - update Mbed TLS to include Client Mode\n");
2142 return NULL;
2143#else /* MBEDTLS_SSL_CLI_C */
2144 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2147 int ret;
2148
2149 if (m_env) {
2150 coap_tick_t now;
2151
2152 coap_ticks(&now);
2153 m_env->last_timeout = now;
2154 ret = do_mbedtls_handshake(c_session, m_env);
2155 if (ret == -1) {
2156 coap_dtls_free_mbedtls_env(m_env);
2157 return NULL;
2158 }
2159 }
2160 return m_env;
2161#endif /* MBEDTLS_SSL_CLI_C */
2162}
2163#endif /* COAP_CLIENT_SUPPORT */
2164
2165#if COAP_SERVER_SUPPORT
2166void *
2168#if !defined(MBEDTLS_SSL_SRV_C)
2169 (void)c_session;
2170 coap_log_emerg("coap_dtls_new_server_session:"
2171 " libcoap not compiled for Server Mode for Mbed TLS"
2172 " - update Mbed TLS to include Server Mode\n");
2173 return NULL;
2174#else /* MBEDTLS_SSL_SRV_C */
2175 coap_mbedtls_env_t *m_env =
2176 (coap_mbedtls_env_t *)c_session->tls;
2177 if (m_env) {
2178#if defined(MBEDTLS_SSL_PROTO_DTLS)
2179#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2180 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2181#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2182#endif /* MBEDTLS_SSL_PROTO_DTLS */
2183 }
2184 return m_env;
2185#endif /* MBEDTLS_SSL_SRV_C */
2186}
2187#endif /* COAP_SERVER_SUPPORT */
2188
2189void
2191 if (c_session && c_session->context && c_session->tls) {
2192 coap_dtls_free_mbedtls_env(c_session->tls);
2193 c_session->tls = NULL;
2195 }
2196 return;
2197}
2198
2199void
2201#if defined(MBEDTLS_SSL_PROTO_DTLS)
2202 coap_mbedtls_env_t *m_env =
2203 (coap_mbedtls_env_t *)c_session->tls;
2204 if (m_env) {
2205#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2206 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2207#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2208 }
2209#else /* ! MBEDTLS_SSL_PROTO_DTLS */
2210 (void)c_session;
2211#endif /* MBEDTLS_SSL_PROTO_DTLS */
2212}
2213
2214ssize_t
2216 const uint8_t *data, size_t data_len) {
2217 int ret;
2218 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2219
2220 assert(m_env != NULL);
2221
2222 if (!m_env) {
2223 return -1;
2224 }
2225 c_session->dtls_event = -1;
2226 coap_log_debug("* %s: dtls: sent %4d bytes\n",
2227 coap_session_str(c_session), (int)data_len);
2228 if (m_env->established) {
2229 ret = mbedtls_ssl_write(&m_env->ssl, (const unsigned char *) data, data_len);
2230 if (ret <= 0) {
2231 switch (ret) {
2232 case MBEDTLS_ERR_SSL_WANT_READ:
2233 case MBEDTLS_ERR_SSL_WANT_WRITE:
2234 ret = 0;
2235 break;
2236 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2238 ret = -1;
2239 break;
2240 default:
2241 coap_log_warn("coap_dtls_send: "
2242 "returned -0x%x: '%s'\n",
2243 -ret, get_error_string(ret));
2244 ret = -1;
2245 break;
2246 }
2247 if (ret == -1) {
2248 coap_log_warn("coap_dtls_send: cannot send PDU\n");
2249 }
2250 }
2251 } else {
2252 ret = do_mbedtls_handshake(c_session, m_env);
2253 if (ret == 1) {
2254 /* Just connected, so send the data */
2255 return coap_dtls_send(c_session, data, data_len);
2256 }
2257 ret = -1;
2258 }
2259
2260 if (c_session->dtls_event >= 0) {
2261 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2262 if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2263 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2264 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2265 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2267 ret = -1;
2268 }
2269 }
2270 return ret;
2271}
2272
2273int
2275 return 0;
2276}
2277
2279coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
2280 return 0;
2281}
2282
2285 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2286 int ret = mbedtls_timing_get_delay(&m_env->timer);
2287 unsigned int scalar = 1 << m_env->retry_scalar;
2288
2289 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2290 switch (ret) {
2291 case 0:
2292 /* int_ms has not timed out */
2293 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2294 /* Need to indicate remaining timeout time */
2295 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2296 }
2297 m_env->last_timeout = now;
2298 /* This may cause a minor extra delay */
2299 return now + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2300 case 1:
2301 /* int_ms has timed out, but not fin_ms */
2302 /*
2303 * Need to make sure that we do not do this too frequently
2304 */
2305 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2306 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2307 }
2308
2309 /* Reset for the next time */
2310 m_env->last_timeout = now;
2311 return now;
2312 case 2:
2313 /* fin_ms has timed out - timed out - one final try */
2314 return now;
2315 default:
2316 break;
2317 }
2318
2319 return 0;
2320}
2321
2322/*
2323 * return 1 timed out
2324 * 0 still timing out
2325 */
2326int
2328 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2329
2330 assert(m_env != NULL && c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2331 m_env->retry_scalar++;
2332 if ((++c_session->dtls_timeout_count > c_session->max_retransmit) ||
2333 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2334 /* Too many retries */
2336 return 1;
2337 }
2338 return 0;
2339}
2340
2341/*
2342 * return +ve data amount
2343 * 0 no more
2344 * -1 error
2345 */
2346int
2348 const uint8_t *data,
2349 size_t data_len) {
2350 int ret = 1;
2351
2352 c_session->dtls_event = -1;
2353 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2354 coap_ssl_t *ssl_data;
2355
2356 assert(m_env != NULL);
2357
2358 ssl_data = &m_env->coap_ssl_data;
2359 if (ssl_data->pdu_len) {
2360 coap_log_err("** %s: Previous data not read %u bytes\n",
2361 coap_session_str(c_session), ssl_data->pdu_len);
2362 }
2363 ssl_data->pdu = data;
2364 ssl_data->pdu_len = (unsigned)data_len;
2365
2366 if (m_env->established) {
2367#if COAP_CONSTRAINED_STACK
2368 /* pdu can be protected by global_lock if needed */
2369 static uint8_t pdu[COAP_RXBUFFER_SIZE];
2370#else /* ! COAP_CONSTRAINED_STACK */
2371 uint8_t pdu[COAP_RXBUFFER_SIZE];
2372#endif /* ! COAP_CONSTRAINED_STACK */
2373
2374 if (c_session->state == COAP_SESSION_STATE_HANDSHAKE) {
2376 c_session);
2377 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
2378 }
2379
2380 ret = mbedtls_ssl_read(&m_env->ssl, pdu, sizeof(pdu));
2381 if (ret > 0) {
2382 ret = coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret);
2383 goto finish;
2384 }
2385 switch (ret) {
2386 case 0:
2387 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2388 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2390 break;
2391 case MBEDTLS_ERR_SSL_WANT_READ:
2392 break;
2393 default:
2394 coap_log_warn("coap_dtls_receive: "
2395 "returned -0x%x: '%s' (length %zd)\n",
2396 -ret, get_error_string(ret), data_len);
2397 break;
2398 }
2399 ret = -1;
2400 } else {
2401 ret = do_mbedtls_handshake(c_session, m_env);
2402 if (ret == 1) {
2403 /* Just connected, so send the data */
2404 coap_session_connected(c_session);
2405 } else {
2406 if (ssl_data->pdu_len) {
2407 /* Do the handshake again incase of internal timeout */
2408 ret = do_mbedtls_handshake(c_session, m_env);
2409 if (ret == 1) {
2410 /* Just connected, so send the data */
2411 coap_session_connected(c_session);
2412 }
2413 }
2414 ret = -1;
2415 }
2416 }
2417 if (c_session->dtls_event >= 0) {
2418 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2419 if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2420 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2421 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2422 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2424 ssl_data = NULL;
2425 ret = -1;
2426 }
2427 }
2428finish:
2429 if (ssl_data && ssl_data->pdu_len) {
2430 /* pdu data is held on stack which will not stay there */
2431 coap_log_debug("coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2432 ssl_data->pdu_len = 0;
2433 ssl_data->pdu = NULL;
2434 }
2435 if (ret > 0) {
2436 coap_log_debug("* %s: dtls: recv %4d bytes\n",
2437 coap_session_str(c_session), ret);
2438 }
2439 return ret;
2440}
2441
2442#if COAP_SERVER_SUPPORT
2443/*
2444 * return -1 failure
2445 * 0 not completed
2446 * 1 client hello seen
2447 */
2448int
2450 const uint8_t *data,
2451 size_t data_len) {
2452#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2453 (void)c_session;
2454 (void)data;
2455 (void)data_len;
2456 coap_log_emerg("coap_dtls_hello:"
2457 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2458 " - update Mbed TLS to include DTLS and Server Mode\n");
2459 return -1;
2460#else /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
2461 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2462 coap_ssl_t *ssl_data;
2463 int ret;
2464
2465 if (!m_env) {
2466 m_env = coap_dtls_new_mbedtls_env(c_session, COAP_DTLS_ROLE_SERVER,
2468 if (m_env) {
2469 c_session->tls = m_env;
2470 } else {
2471 /* error should have already been reported */
2472 return -1;
2473 }
2474 }
2475
2476 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2477 (unsigned char *)&c_session->addr_info.remote,
2478 sizeof(c_session->addr_info.remote))) != 0) {
2479 coap_log_err("mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2480 -ret, get_error_string(ret));
2481 return -1;
2482 }
2483
2484 ssl_data = &m_env->coap_ssl_data;
2485 if (ssl_data->pdu_len) {
2486 coap_log_err("** %s: Previous data not read %u bytes\n",
2487 coap_session_str(c_session), ssl_data->pdu_len);
2488 }
2489 ssl_data->pdu = data;
2490 ssl_data->pdu_len = (unsigned)data_len;
2491
2492 ret = do_mbedtls_handshake(c_session, m_env);
2493 if (ret == 0 || m_env->seen_client_hello) {
2494 /* The test for seen_client_hello gives the ability to setup a new
2495 c_session to continue the do_mbedtls_handshake past the client hello
2496 and safely allow updating of the m_env and separately
2497 letting a new session cleanly start up.
2498 */
2499 m_env->seen_client_hello = 0;
2500 ret = 1;
2501 } else {
2502 ret = 0;
2503 }
2504
2505 if (ssl_data->pdu_len) {
2506 /* pdu data is held on stack which will not stay there */
2507 coap_log_debug("coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2508 ssl_data->pdu_len = 0;
2509 ssl_data->pdu = NULL;
2510 }
2511 return ret;
2512#endif /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
2513}
2514#endif /* COAP_SERVER_SUPPORT */
2515
2516unsigned int
2518 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2519 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2520
2521 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2522 return 13 + 8 + 8;
2523 }
2524 return expansion;
2525}
2526
2527#if !COAP_DISABLE_TCP
2528#if COAP_CLIENT_SUPPORT
2529void *
2531#if !defined(MBEDTLS_SSL_CLI_C)
2532 (void)c_session;
2533 *connected = 0;
2534 coap_log_emerg("coap_tls_new_client_session:"
2535 " libcoap not compiled for Client Mode for Mbed TLS"
2536 " - update Mbed TLS to include Client Mode\n");
2537 return NULL;
2538#else /* MBEDTLS_SSL_CLI_C */
2539 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2542 int ret;
2543 coap_tick_t now;
2544 coap_ticks(&now);
2545
2546 if (!m_env)
2547 return NULL;
2548
2549 m_env->last_timeout = now;
2550 c_session->tls = m_env;
2551 ret = do_mbedtls_handshake(c_session, m_env);
2552 if (ret == 1) {
2554 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
2555 }
2556 return m_env;
2557#endif /* MBEDTLS_SSL_CLI_C */
2558}
2559#endif /* COAP_CLIENT_SUPPORT */
2560
2561#if COAP_SERVER_SUPPORT
2562void *
2564#if !defined(MBEDTLS_SSL_SRV_C)
2565 (void)c_session;
2566 (void)connected;
2567
2568 coap_log_emerg("coap_tls_new_server_session:"
2569 " libcoap not compiled for Server Mode for Mbed TLS"
2570 " - update Mbed TLS to include Server Mode\n");
2571 return NULL;
2572#else /* MBEDTLS_SSL_SRV_C */
2573 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2576 int ret;
2577
2578 if (!m_env)
2579 return NULL;
2580
2581 c_session->tls = m_env;
2582 ret = do_mbedtls_handshake(c_session, m_env);
2583 if (ret == 1) {
2585 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
2586 }
2587 return m_env;
2588#endif /* MBEDTLS_SSL_SRV_C */
2589}
2590#endif /* COAP_SERVER_SUPPORT */
2591
2592void
2594 coap_dtls_free_session(c_session);
2595 return;
2596}
2597
2598/*
2599 * strm
2600 * return +ve Number of bytes written.
2601 * -1 Error (error in errno).
2602 */
2603ssize_t
2604coap_tls_write(coap_session_t *c_session, const uint8_t *data,
2605 size_t data_len) {
2606 int ret = 0;
2607 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2608 size_t amount_sent = 0;
2609
2610 assert(m_env != NULL);
2611
2612 if (!m_env) {
2613 errno = ENXIO;
2614 return -1;
2615 }
2616 c_session->dtls_event = -1;
2617 if (m_env->established) {
2618 while (amount_sent < data_len) {
2619 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
2620 data_len - amount_sent);
2621 if (ret <= 0) {
2622 switch (ret) {
2623 case MBEDTLS_ERR_SSL_WANT_READ:
2624 case MBEDTLS_ERR_SSL_WANT_WRITE:
2625 if (amount_sent)
2626 ret = amount_sent;
2627 else
2628 ret = 0;
2629 c_session->sock.flags |= COAP_SOCKET_WANT_WRITE;
2630 break;
2631 case MBEDTLS_ERR_NET_CONN_RESET:
2632 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2634 break;
2635 default:
2636 coap_log_warn("coap_tls_write: "
2637 "returned -0x%x: '%s'\n",
2638 -ret, get_error_string(ret));
2639 ret = -1;
2640 break;
2641 }
2642 if (ret == -1) {
2643 coap_log_warn("coap_tls_write: cannot send PDU\n");
2644 }
2645 break;
2646 }
2647 amount_sent += ret;
2648 }
2649 } else {
2650 ret = do_mbedtls_handshake(c_session, m_env);
2651 if (ret == 1) {
2653 c_session);
2654 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
2655 } else {
2656 ret = -1;
2657 }
2658 }
2659
2660 if (c_session->dtls_event >= 0) {
2661 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2662 if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2663 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2664 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2665 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2667 ret = -1;
2668 }
2669 }
2670 if (ret > 0) {
2671 if (ret == (ssize_t)data_len)
2672 coap_log_debug("* %s: tls: sent %4d bytes\n",
2673 coap_session_str(c_session), ret);
2674 else
2675 coap_log_debug("* %s: tls: sent %4d of %4zd bytes\n",
2676 coap_session_str(c_session), ret, data_len);
2677 }
2678 return ret;
2679}
2680
2681/*
2682 * strm
2683 * return >=0 Number of bytes read.
2684 * -1 Error (error in errno).
2685 */
2686ssize_t
2687coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) {
2688 int ret = -1;
2689
2690 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2691
2692 if (!m_env) {
2693 errno = ENXIO;
2694 return -1;
2695 }
2696
2697 c_session->dtls_event = -1;
2698
2699 if (!m_env->established && !m_env->sent_alert) {
2700 ret = do_mbedtls_handshake(c_session, m_env);
2701 if (ret == 1) {
2703 c_session);
2704 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
2705 }
2706 }
2707
2708 if (c_session->state != COAP_SESSION_STATE_NONE && m_env->established) {
2709 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2710 if (ret <= 0) {
2711 switch (ret) {
2712 case 0:
2713 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2715 ret = -1;
2716 break;
2717 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2718 /* Stop the sending of an alert on closedown */
2719 m_env->sent_alert = 1;
2721 break;
2722#if MBEDTLS_VERSION_NUMBER >= 0x03060000
2723 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
2724#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
2725 case MBEDTLS_ERR_SSL_WANT_READ:
2726 errno = EAGAIN;
2727 ret = 0;
2728 break;
2729 default:
2730 coap_log_warn("coap_tls_read: "
2731 "returned -0x%x: '%s' (length %zd)\n",
2732 -ret, get_error_string(ret), data_len);
2733 ret = -1;
2734 break;
2735 }
2736 } else if (ret < (int)data_len) {
2737 c_session->sock.flags &= ~COAP_SOCKET_CAN_READ;
2738 }
2739 }
2740
2741 if (c_session->dtls_event >= 0) {
2742 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2743 if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2744 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2745 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2746 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2748 ret = -1;
2749 }
2750 }
2751 if (ret > 0) {
2752 coap_log_debug("* %s: tls: recv %4d bytes\n",
2753 coap_session_str(c_session), ret);
2754 }
2755 return ret;
2756}
2757#endif /* !COAP_DISABLE_TCP */
2758
2759void
2760coap_dtls_startup(void) {
2761}
2762
2763void
2764coap_dtls_shutdown(void) {
2765#if COAP_CLIENT_SUPPORT
2766 mbedtls_free(psk_ciphers);
2767 mbedtls_free(pki_ciphers);
2768 mbedtls_free(ecjpake_ciphers);
2769 psk_ciphers = NULL;
2770 pki_ciphers = NULL;
2771 ecjpake_ciphers = NULL;
2772 processed_ciphers = 0;
2773#endif /* COAP_CLIENT_SUPPORT */
2775}
2776
2777void *
2778coap_dtls_get_tls(const coap_session_t *c_session,
2779 coap_tls_library_t *tls_lib) {
2780 if (tls_lib)
2781 *tls_lib = COAP_TLS_LIBRARY_MBEDTLS;
2782 if (c_session && c_session->tls) {
2783 coap_mbedtls_env_t *m_env;
2784
2785 /* To get around const issue */
2786 memcpy(&m_env, &c_session->tls, sizeof(m_env));
2787
2788 return (void *)&m_env->ssl;
2789 }
2790 return NULL;
2791}
2792
2793static coap_log_t keep_log_level = COAP_LOG_EMERG;
2794
2795void
2797#if !defined(ESPIDF_VERSION)
2798 int use_level;
2799 /*
2800 * Mbed TLS debug levels filter
2801 * 0 No debug
2802 * 1 Error
2803 * 2 State change
2804 * 3 Informational
2805 * 4 Verbose
2806 */
2807 switch ((int)level) {
2808 case COAP_LOG_EMERG:
2809 use_level = 0;
2810 break;
2811 case COAP_LOG_ALERT:
2812 case COAP_LOG_CRIT:
2813 case COAP_LOG_ERR:
2814 case COAP_LOG_WARN:
2815 use_level = 1;
2816 break;
2817 case COAP_LOG_NOTICE:
2818 use_level = 2;
2819 break;
2820 case COAP_LOG_INFO:
2821 use_level = 3;
2822 break;
2823 case COAP_LOG_DEBUG:
2824 default:
2825 use_level = 4;
2826 break;
2827 }
2828 mbedtls_debug_set_threshold(use_level);
2829#endif /* !ESPIDF_VERSION) */
2830 keep_log_level = level;
2831}
2832
2835 return keep_log_level;
2836}
2837
2840 static coap_tls_version_t version;
2841 version.version = mbedtls_version_get_number();
2842 version.built_version = MBEDTLS_VERSION_NUMBER;
2844 return &version;
2845}
2846
2847#if COAP_SERVER_SUPPORT
2849coap_digest_setup(void) {
2850 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(sizeof(mbedtls_sha256_context));
2851
2852 if (digest_ctx) {
2853 mbedtls_sha256_init(digest_ctx);
2854#ifdef MBEDTLS_2_X_COMPAT
2855 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2856#else
2857 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2858#endif /* MBEDTLS_2_X_COMPAT */
2859 coap_digest_free(digest_ctx);
2860 return NULL;
2861 }
2862 }
2863 return digest_ctx;
2864}
2865
2866void
2868 mbedtls_sha256_free(digest_ctx);
2869 mbedtls_free(digest_ctx);
2870}
2871
2872int
2874 const uint8_t *data,
2875 size_t data_len) {
2876#ifdef MBEDTLS_2_X_COMPAT
2877 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2878#else
2879 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2880#endif /* MBEDTLS_2_X_COMPAT */
2881
2882 return ret == 0;
2883}
2884
2885int
2887 coap_digest_t *digest_buffer) {
2888#ifdef MBEDTLS_2_X_COMPAT
2889 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t *)digest_buffer);
2890#else
2891 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t *)digest_buffer);
2892#endif /* MBEDTLS_2_X_COMPAT */
2893
2894 coap_digest_free(digest_ctx);
2895 return ret == 0;
2896}
2897#endif /* COAP_SERVER_SUPPORT */
2898
2899#include <mbedtls/cipher.h>
2900#include <mbedtls/md.h>
2901
2902#ifndef MBEDTLS_CIPHER_MODE_AEAD
2903#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
2904#endif /* MBEDTLS_CIPHER_MODE_AEAD */
2905
2906#ifdef MBEDTLS_ERROR_C
2907#include <mbedtls/error.h>
2908#endif /* MBEDTLS_ERROR_C */
2909
2910#ifdef MBEDTLS_ERROR_C
2911#define C(Func) \
2912 do { \
2913 int c_tmp = (int)(Func); \
2914 if (c_tmp != 0) { \
2915 char error_buf[64]; \
2916 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
2917 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
2918 goto error; \
2919 } \
2920 } while (0);
2921#else /* !MBEDTLS_ERROR_C */
2922#define C(Func) \
2923 do { \
2924 int c_tmp = (int)(Func); \
2925 if (c_tmp != 0) { \
2926 coap_log_err("mbedtls: %d\n", tmp); \
2927 goto error; \
2928 } \
2929 } while (0);
2930#endif /* !MBEDTLS_ERROR_C */
2931
2932#if COAP_WS_SUPPORT
2933/*
2934 * The struct hash_algs and the function get_hash_alg() are used to
2935 * determine which hash type to use for creating the required hash object.
2936 */
2937static struct hash_algs {
2938 cose_alg_t alg;
2939 mbedtls_md_type_t hash_type;
2940 size_t hash_size;
2941} hashs[] = {
2942 {COSE_ALGORITHM_SHA_1, MBEDTLS_MD_SHA1, 20},
2943 {COSE_ALGORITHM_SHA_256_256, MBEDTLS_MD_SHA256, 32},
2944 {COSE_ALGORITHM_SHA_512, MBEDTLS_MD_SHA512, 64},
2945};
2946
2947static mbedtls_md_type_t
2948get_hash_alg(cose_alg_t alg, size_t *hash_len) {
2949 size_t idx;
2950
2951 for (idx = 0; idx < sizeof(hashs) / sizeof(struct hash_algs); idx++) {
2952 if (hashs[idx].alg == alg) {
2953 *hash_len = hashs[idx].hash_size;
2954 return hashs[idx].hash_type;
2955 }
2956 }
2957 coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg);
2958 return MBEDTLS_MD_NONE;
2959}
2960
2961int
2963 const coap_bin_const_t *data,
2964 coap_bin_const_t **hash) {
2965 mbedtls_md_context_t ctx;
2966 int ret = 0;
2967 const mbedtls_md_info_t *md_info;
2968 unsigned int len;
2969 coap_binary_t *dummy = NULL;
2970 size_t hash_length;
2971 mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length);
2972
2973 if (dig_type == MBEDTLS_MD_NONE) {
2974 coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg);
2975 return 0;
2976 }
2977 md_info = mbedtls_md_info_from_type(dig_type);
2978
2979 len = mbedtls_md_get_size(md_info);
2980 if (len == 0) {
2981 return 0;
2982 }
2983
2984 mbedtls_md_init(&ctx);
2985 C(mbedtls_md_setup(&ctx, md_info, 0));
2986
2987 C(mbedtls_md_starts(&ctx));
2988 C(mbedtls_md_update(&ctx, (const unsigned char *)data->s, data->length));
2989 dummy = coap_new_binary(len);
2990 if (dummy == NULL)
2991 goto error;
2992 C(mbedtls_md_finish(&ctx, dummy->s));
2993
2994 *hash = (coap_bin_const_t *)dummy;
2995 ret = 1;
2996error:
2997 mbedtls_md_free(&ctx);
2998 return ret;
2999}
3000#endif /* COAP_WS_SUPPORT */
3001
3002#if COAP_OSCORE_SUPPORT
3003int
3005 return 1;
3006}
3007
3008/*
3009 * The struct cipher_algs and the function get_cipher_alg() are used to
3010 * determine which cipher type to use for creating the required cipher
3011 * suite object.
3012 */
3013static struct cipher_algs {
3014 cose_alg_t alg;
3015 mbedtls_cipher_type_t cipher_type;
3016} ciphers[] = {{COSE_ALGORITHM_AES_CCM_16_64_128, MBEDTLS_CIPHER_AES_128_CCM},
3017 {COSE_ALGORITHM_AES_CCM_16_64_256, MBEDTLS_CIPHER_AES_256_CCM}
3018};
3019
3020static mbedtls_cipher_type_t
3021get_cipher_alg(cose_alg_t alg) {
3022 size_t idx;
3023
3024 for (idx = 0; idx < sizeof(ciphers) / sizeof(struct cipher_algs); idx++) {
3025 if (ciphers[idx].alg == alg)
3026 return ciphers[idx].cipher_type;
3027 }
3028 coap_log_debug("get_cipher_alg: COSE cipher %d not supported\n", alg);
3029 return 0;
3030}
3031
3032/*
3033 * The struct hmac_algs and the function get_hmac_alg() are used to
3034 * determine which hmac type to use for creating the required hmac
3035 * suite object.
3036 */
3037static struct hmac_algs {
3038 cose_hmac_alg_t hmac_alg;
3039 mbedtls_md_type_t hmac_type;
3040} hmacs[] = {
3041 {COSE_HMAC_ALG_HMAC256_256, MBEDTLS_MD_SHA256},
3042 {COSE_HMAC_ALG_HMAC384_384, MBEDTLS_MD_SHA384},
3043 {COSE_HMAC_ALG_HMAC512_512, MBEDTLS_MD_SHA512},
3044};
3045
3046static mbedtls_md_type_t
3047get_hmac_alg(cose_hmac_alg_t hmac_alg) {
3048 size_t idx;
3049
3050 for (idx = 0; idx < sizeof(hmacs) / sizeof(struct hmac_algs); idx++) {
3051 if (hmacs[idx].hmac_alg == hmac_alg)
3052 return hmacs[idx].hmac_type;
3053 }
3054 coap_log_debug("get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3055 return 0;
3056}
3057
3058int
3060 return get_cipher_alg(alg) != 0;
3061}
3062
3063int
3065 cose_hmac_alg_t hmac_alg;
3066
3067 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
3068 return 0;
3069 return get_hmac_alg(hmac_alg) != 0;
3070}
3071
3076static int
3077setup_cipher_context(mbedtls_cipher_context_t *ctx,
3078 cose_alg_t coap_alg,
3079 const uint8_t *key_data,
3080 size_t key_length,
3081 mbedtls_operation_t mode) {
3082 const mbedtls_cipher_info_t *cipher_info;
3083 mbedtls_cipher_type_t cipher_type;
3084 uint8_t key[COAP_CRYPTO_MAX_KEY_SIZE]; /* buffer for normalizing the key
3085 according to its key length */
3086 int klen;
3087 memset(key, 0, sizeof(key));
3088
3089 if ((cipher_type = get_cipher_alg(coap_alg)) == 0) {
3090 coap_log_debug("coap_crypto_encrypt: algorithm %d not supported\n",
3091 coap_alg);
3092 return 0;
3093 }
3094 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3095 if (!cipher_info) {
3096 coap_log_crit("coap_crypto_encrypt: cannot get cipher info\n");
3097 return 0;
3098 }
3099
3100 mbedtls_cipher_init(ctx);
3101
3102 C(mbedtls_cipher_setup(ctx, cipher_info));
3103 klen = mbedtls_cipher_get_key_bitlen(ctx);
3104 if ((klen > (int)(sizeof(key) * 8)) || (key_length > sizeof(key))) {
3105 coap_log_crit("coap_crypto: cannot set key\n");
3106 goto error;
3107 }
3108 memcpy(key, key_data, key_length);
3109 C(mbedtls_cipher_setkey(ctx, key, klen, mode));
3110
3111 /* On success, the cipher context is released by the caller. */
3112 return 1;
3113error:
3114 mbedtls_cipher_free(ctx);
3115 return 0;
3116}
3117
3118int
3120 coap_bin_const_t *data,
3121 coap_bin_const_t *aad,
3122 uint8_t *result,
3123 size_t *max_result_len) {
3124 mbedtls_cipher_context_t ctx;
3125 const coap_crypto_aes_ccm_t *ccm;
3126#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3127 unsigned char tag[16];
3128#endif /* MBEDTLS_VERSION_NUMBER < 0x02150000 */
3129 int ret = 0;
3130 size_t result_len = *max_result_len;
3131 coap_bin_const_t laad;
3132
3133 if (data == NULL)
3134 return 0;
3135
3136 assert(params != NULL);
3137
3138 if (!params) {
3139 return 0;
3140 }
3141 ccm = &params->params.aes;
3142
3143 if (!setup_cipher_context(&ctx,
3144 params->alg,
3145 ccm->key.s,
3146 ccm->key.length,
3147 MBEDTLS_ENCRYPT)) {
3148 return 0;
3149 }
3150
3151 if (aad) {
3152 laad = *aad;
3153 } else {
3154 laad.s = NULL;
3155 laad.length = 0;
3156 }
3157
3158#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3159 C(mbedtls_cipher_auth_encrypt(&ctx,
3160 ccm->nonce,
3161 15 - ccm->l, /* iv */
3162 laad.s,
3163 laad.length, /* ad */
3164 data->s,
3165 data->length, /* input */
3166 result,
3167 &result_len, /* output */
3168 tag,
3169 ccm->tag_len /* tag */
3170 ));
3171 /* check if buffer is sufficient to hold tag */
3172 if ((result_len + ccm->tag_len) > *max_result_len) {
3173 coap_log_err("coap_encrypt: buffer too small\n");
3174 goto error;
3175 }
3176 /* append tag to result */
3177 memcpy(result + result_len, tag, ccm->tag_len);
3178 *max_result_len = result_len + ccm->tag_len;
3179 ret = 1;
3180#else /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
3181 C(mbedtls_cipher_auth_encrypt_ext(&ctx,
3182 ccm->nonce,
3183 15 - ccm->l, /* iv */
3184 laad.s,
3185 laad.length, /* ad */
3186 data->s,
3187 data->length, /* input */
3188 result,
3189 result_len,
3190 &result_len, /* output */
3191 ccm->tag_len /* tag */
3192 ));
3193 *max_result_len = result_len;
3194 ret = 1;
3195#endif /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
3196
3197error:
3198 mbedtls_cipher_free(&ctx);
3199 return ret;
3200}
3201
3202int
3204 coap_bin_const_t *data,
3205 coap_bin_const_t *aad,
3206 uint8_t *result,
3207 size_t *max_result_len) {
3208 mbedtls_cipher_context_t ctx;
3209 const coap_crypto_aes_ccm_t *ccm;
3210#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3211 const unsigned char *tag;
3212#endif /* MBEDTLS_VERSION_NUMBER < 0x02150000 */
3213 int ret = 0;
3214 size_t result_len = *max_result_len;
3215 coap_bin_const_t laad;
3216
3217 if (data == NULL)
3218 return 0;
3219
3220 assert(params != NULL);
3221
3222 if (!params) {
3223 return 0;
3224 }
3225
3226 ccm = &params->params.aes;
3227
3228 if (!setup_cipher_context(&ctx,
3229 params->alg,
3230 ccm->key.s,
3231 ccm->key.length,
3232 MBEDTLS_DECRYPT)) {
3233 return 0;
3234 }
3235
3236 if (data->length < ccm->tag_len) {
3237 coap_log_err("coap_decrypt: invalid tag length\n");
3238 goto error;
3239 }
3240
3241 if (aad) {
3242 laad = *aad;
3243 } else {
3244 laad.s = NULL;
3245 laad.length = 0;
3246 }
3247
3248#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3249 tag = data->s + data->length - ccm->tag_len;
3250 C(mbedtls_cipher_auth_decrypt(&ctx,
3251 ccm->nonce,
3252 15 - ccm->l, /* iv */
3253 laad.s,
3254 laad.length, /* ad */
3255 data->s,
3256 data->length - ccm->tag_len, /* input */
3257 result,
3258 &result_len, /* output */
3259 tag,
3260 ccm->tag_len /* tag */
3261 ));
3262#else /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
3263 C(mbedtls_cipher_auth_decrypt_ext(&ctx,
3264 ccm->nonce,
3265 15 - ccm->l, /* iv */
3266 laad.s,
3267 laad.length, /* ad */
3268 data->s,
3269 // data->length - ccm->tag_len, /* input */
3270 data->length, /* input */
3271 result,
3272 result_len,
3273 &result_len, /* output */
3274 ccm->tag_len /* tag */
3275 ));
3276#endif /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
3277
3278 *max_result_len = result_len;
3279 ret = 1;
3280error:
3281 mbedtls_cipher_free(&ctx);
3282 return ret;
3283}
3284
3285int
3287 coap_bin_const_t *key,
3288 coap_bin_const_t *data,
3289 coap_bin_const_t **hmac) {
3290 mbedtls_md_context_t ctx;
3291 int ret = 0;
3292 const int use_hmac = 1;
3293 const mbedtls_md_info_t *md_info;
3294 mbedtls_md_type_t mac_algo;
3295 unsigned int len;
3296 coap_binary_t *dummy = NULL;
3297
3298 assert(key);
3299 assert(data);
3300 assert(hmac);
3301
3302 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3303 coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3304 return 0;
3305 }
3306 md_info = mbedtls_md_info_from_type(mac_algo);
3307
3308 len = mbedtls_md_get_size(md_info);
3309 if (len == 0) {
3310 return 0;
3311 }
3312
3313 mbedtls_md_init(&ctx);
3314 C(mbedtls_md_setup(&ctx, md_info, use_hmac));
3315
3316 C(mbedtls_md_hmac_starts(&ctx, key->s, key->length));
3317 C(mbedtls_md_hmac_update(&ctx, (const unsigned char *)data->s, data->length));
3318 dummy = coap_new_binary(len);
3319 if (dummy == NULL)
3320 goto error;
3321 C(mbedtls_md_hmac_finish(&ctx, dummy->s));
3322
3323 *hmac = (coap_bin_const_t *)dummy;
3324 ret = 1;
3325error:
3326 mbedtls_md_free(&ctx);
3327 return ret;
3328}
3329
3330#endif /* COAP_OSCORE_SUPPORT */
3331
3332#else /* !COAP_WITH_LIBMBEDTLS */
3333
3334#ifdef __clang__
3335/* Make compilers happy that do not like empty modules. As this function is
3336 * never used, we ignore -Wunused-function at the end of compiling this file
3337 */
3338#pragma GCC diagnostic ignored "-Wunused-function"
3339#endif
3340static inline void
3341dummy(void) {
3342}
3343
3344#endif /* COAP_WITH_LIBMBEDTLS */
#define COAP_SERVER_SUPPORT
#define PRIx32
const char * coap_socket_strerror(void)
Definition coap_io.c:1900
#define COAP_RXBUFFER_SIZE
Definition coap_io.h:29
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:66
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
@ COAP_LAYER_TLS
Library specific build wrapper for coap_internal.h.
static void dummy(void)
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition coap_notls.c:108
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
Definition coap_notls.c:224
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:296
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:219
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:238
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:153
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:256
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:142
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:207
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:284
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:203
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
Definition coap_notls.c:116
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:233
void coap_dtls_free_context(void *handle COAP_UNUSED)
Definition coap_notls.c:181
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:199
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
Definition coap_notls.c:176
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:275
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
void coap_digest_ctx_t
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:143
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition coap_prng.c:178
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition coap_net.c:4358
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
Definition coap_net.c:2483
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
#define COAP_CRYPTO_MAX_KEY_SIZE
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
Definition coap_notls.c:149
int coap_dtls_define_issue(coap_define_issue_key_t type, coap_define_issue_fail_t fail, coap_dtls_key_t *key, const coap_dtls_role_t role, int ret)
Report PKI DEFINE type issue.
Definition coap_dtls.c:165
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Definition coap_notls.c:214
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c:161
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
Definition coap_dtls.c:26
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_KEY_ROOT_CA
@ COAP_DEFINE_KEY_CA
@ COAP_DEFINE_KEY_PUBLIC
@ COAP_DEFINE_FAIL_NONE
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
@ COAP_DEFINE_FAIL_BAD
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
Definition coap_notls.c:100
coap_dtls_role_t
Definition coap_dtls.h:44
coap_tls_library_t
Definition coap_dtls.h:70
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
Definition coap_dtls.h:245
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
Definition coap_dtls.h:242
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
Definition coap_dtls.h:236
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
Definition coap_dtls.h:234
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
Definition coap_dtls.h:251
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
Definition coap_dtls.h:238
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
Definition coap_dtls.h:240
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
Definition coap_dtls.h:248
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
Definition coap_dtls.h:46
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition coap_dtls.h:45
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
Definition coap_dtls.h:172
@ COAP_TLS_LIBRARY_MBEDTLS
Using Mbed TLS library.
Definition coap_dtls.h:75
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
Definition coap_event.h:39
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
Definition coap_event.h:41
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
Definition coap_event.h:45
#define coap_lock_callback_ret(r, c, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:120
#define coap_log_emerg(...)
Definition coap_debug.h:81
coap_log_t
Logging type.
Definition coap_debug.h:50
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition coap_notls.c:171
#define coap_dtls_log(level,...)
Logging function.
Definition coap_debug.h:300
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition coap_notls.c:166
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:108
#define coap_log_warn(...)
Definition coap_debug.h:102
#define coap_log_err(...)
Definition coap_debug.h:96
#define coap_log_crit(...)
Definition coap_debug.h:90
@ COAP_LOG_INFO
Definition coap_debug.h:57
@ COAP_LOG_EMERG
Definition coap_debug.h:51
@ COAP_LOG_NOTICE
Definition coap_debug.h:56
@ COAP_LOG_DEBUG
Definition coap_debug.h:58
@ COAP_LOG_ALERT
Definition coap_debug.h:52
@ COAP_LOG_CRIT
Definition coap_debug.h:53
@ COAP_LOG_ERR
Definition coap_debug.h:54
@ COAP_LOG_WARN
Definition coap_debug.h:55
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
Definition coap_netif.c:25
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
cose_hkdf_alg_t
cose_hmac_alg_t
cose_alg_t
@ COSE_HMAC_ALG_HMAC384_384
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_ALGORITHM_SHA_256_256
@ COSE_ALGORITHM_SHA_1
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_SHA_512
@ COSE_ALGORITHM_AES_CCM_16_64_256
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:312
@ COAP_PROTO_DTLS
Definition coap_pdu.h:315
@ COAP_PROTO_TLS
Definition coap_pdu.h:317
@ COAP_PROTO_WSS
Definition coap_pdu.h:319
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
#define COAP_PROTO_NOT_RELIABLE(p)
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_NONE
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:77
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:110
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
Definition coap_notls.c:86
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
Definition coap_notls.c:50
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:41
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:36
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
Definition coap_notls.c:59
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
Definition coap_notls.c:77
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
Definition coap_notls.c:68
#define COAP_UNUSED
Definition libcoap.h:70
coap_address_t remote
remote address and port
Definition coap_io.h:56
CoAP binary data definition with const data.
Definition coap_str.h:64
size_t length
length of binary data
Definition coap_str.h:65
const uint8_t * s
read-only binary data
Definition coap_str.h:66
CoAP binary data definition.
Definition coap_str.h:56
The CoAP stack's global state is stored in a coap_context_t object.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
size_t tag_len
The size of the Tag.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@2 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorith to use.
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:410
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
Definition coap_dtls.h:417
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:437
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
Definition coap_dtls.h:433
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:415
The structure that holds the PKI key information.
Definition coap_dtls.h:279
coap_pki_key_define_t define
for definable type keys
Definition coap_dtls.h:286
union coap_dtls_key_t::@3 key
coap_pki_key_t key_type
key format type
Definition coap_dtls.h:280
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:312
void * cn_call_back_arg
Passed in to the CN callback function.
Definition coap_dtls.h:351
uint8_t allow_short_rsa_length
1 if small RSA keysizes are allowed
Definition coap_dtls.h:329
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
Definition coap_dtls.h:323
uint8_t allow_bad_md_hash
1 if unsupported MD hashes are allowed
Definition coap_dtls.h:328
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
Definition coap_dtls.h:333
uint8_t check_cert_revocation
1 if revocation checks wanted
Definition coap_dtls.h:325
uint8_t cert_chain_verify_depth
recommended depth is 3
Definition coap_dtls.h:324
uint8_t allow_expired_certs
1 if expired certs are allowed
Definition coap_dtls.h:322
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:317
uint8_t allow_self_signed
1 if self-signed certs are allowed.
Definition coap_dtls.h:320
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
Definition coap_dtls.h:350
uint8_t allow_expired_crl
1 if expired crl is allowed
Definition coap_dtls.h:327
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
Definition coap_dtls.h:318
The structure that holds the Server Pre-Shared Key and Identity Hint information.
Definition coap_dtls.h:450
The structure used for defining the Server PSK setup data to be used.
Definition coap_dtls.h:501
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
Definition coap_dtls.h:530
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
Definition coap_dtls.h:522
void * id_call_back_arg
Passed in to the Identity callback function.
Definition coap_dtls.h:523
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:506
void * sni_call_back_arg
Passed in to the SNI callback function.
Definition coap_dtls.h:531
coap_layer_read_t l_read
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
Definition coap_dtls.h:261
coap_const_char_ptr_t private_key
define: Private Key
Definition coap_dtls.h:262
coap_const_char_ptr_t ca
define: Common CA Certificate
Definition coap_dtls.h:260
size_t public_cert_len
define Public Cert length (if needed)
Definition coap_dtls.h:264
size_t ca_len
define CA Cert length (if needed)
Definition coap_dtls.h:263
coap_pki_define_t private_key_def
define: Private Key type definition
Definition coap_dtls.h:268
size_t private_key_len
define Private Key length (if needed)
Definition coap_dtls.h:265
coap_pki_define_t ca_def
define: Common CA type definition
Definition coap_dtls.h:266
coap_pki_define_t public_cert_def
define: Public Cert type definition
Definition coap_dtls.h:267
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_endpoint_t * endpoint
session's endpoint
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_bin_const_t * client_cid
Contains client CID or NULL.
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t negotiated_cid
Set for a client if CID negotiated.
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
The structure used for returning the underlying (D)TLS library information.
Definition coap_dtls.h:83
uint64_t built_version
(D)TLS Built against Library Version
Definition coap_dtls.h:86
coap_tls_library_t type
Library type.
Definition coap_dtls.h:85
uint64_t version
(D)TLS runtime Library Version
Definition coap_dtls.h:84
const char * s_byte
signed char ptr
Definition coap_str.h:73
const uint8_t * u_byte
unsigned char ptr
Definition coap_str.h:74