libcoap 4.3.5-develop-b2eb3a0
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-2026 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
17
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#if COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE
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/version.h>
69
70/* Auto-detect mbedTLS 4.x which requires PSA Crypto APIs */
71#if MBEDTLS_VERSION_NUMBER >= 0x04000000
72#define COAP_USE_PSA_CRYPTO 1
73#else
74#define COAP_USE_PSA_CRYPTO 0
75#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
76
77#if COAP_USE_PSA_CRYPTO
78#include <psa/crypto.h>
79#else
80#include <mbedtls/entropy.h>
81#include <mbedtls/ctr_drbg.h>
82#include <mbedtls/sha256.h>
83#include <mbedtls/md.h>
84#include <mbedtls/cipher.h>
85#endif /* COAP_USE_PSA_CRYPTO */
86
87#include <mbedtls/error.h>
88#include <mbedtls/timing.h>
89#include <mbedtls/ssl_cookie.h>
90#include <mbedtls/oid.h>
91#include <mbedtls/debug.h>
92#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
93#include <mbedtls/esp_debug.h>
94#endif /* ESPIDF_VERSION && CONFIG_MBEDTLS_DEBUG */
95#if !COAP_USE_PSA_CRYPTO && defined(MBEDTLS_PSA_CRYPTO_C)
96#include <psa/crypto.h>
97#endif /* !COAP_USE_PSA_CRYPTO && MBEDTLS_PSA_CRYPTO_C */
98
99/*
100 * Crypto abstraction types for mbedTLS version compatibility
101 */
102#if COAP_USE_PSA_CRYPTO
103typedef psa_hash_operation_t coap_crypto_sha256_ctx_t;
104typedef psa_algorithm_t coap_crypto_md_type_t;
105
106#define COAP_CRYPTO_MD_SHA1 PSA_ALG_SHA_1
107#define COAP_CRYPTO_MD_SHA256 PSA_ALG_SHA_256
108#define COAP_CRYPTO_MD_SHA384 PSA_ALG_SHA_384
109#define COAP_CRYPTO_MD_SHA512 PSA_ALG_SHA_512
110#define COAP_CRYPTO_MD_NONE PSA_ALG_NONE
111
112#else /* !COAP_USE_PSA_CRYPTO */
113typedef mbedtls_sha256_context coap_crypto_sha256_ctx_t;
114typedef mbedtls_md_type_t coap_crypto_md_type_t;
115
116#define COAP_CRYPTO_MD_SHA1 MBEDTLS_MD_SHA1
117#define COAP_CRYPTO_MD_SHA256 MBEDTLS_MD_SHA256
118#define COAP_CRYPTO_MD_SHA384 MBEDTLS_MD_SHA384
119#define COAP_CRYPTO_MD_SHA512 MBEDTLS_MD_SHA512
120#define COAP_CRYPTO_MD_NONE MBEDTLS_MD_NONE
121#endif /* !COAP_USE_PSA_CRYPTO */
122
125 static coap_tls_version_t version;
126 version.version = mbedtls_version_get_number();
127 version.built_version = MBEDTLS_VERSION_NUMBER;
129 return &version;
130}
131
132#if COAP_WITH_LIBMBEDTLS_OSCORE
133void
134coap_dtls_startup(void) {
135#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
136 psa_crypto_init();
137#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
138}
139
140void
141coap_dtls_shutdown(void) {
143#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
144 mbedtls_psa_crypto_free();
145#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
146}
147
148#endif /* COAP_WITH_LIBMBEDTLS_OSCORE */
149
150void
152}
153
154#ifdef _WIN32
155#include <stdlib.h>
156#include <string.h>
157#endif /* _WIN32 */
158
159/*
160 * Default mbedTLS helpers in this file to the C library heap. Zephyr can set
161 * mbedtls_malloc / mbedtls_realloc / mbedtls_free via zephyr/CMakeLists.txt.
162 */
163
164#ifndef mbedtls_malloc
165#define mbedtls_malloc(a) malloc(a)
166#endif
167#ifndef mbedtls_realloc
168#define mbedtls_realloc(a, b) realloc((a), (b))
169#endif
170#ifndef mbedtls_free
171#define mbedtls_free(a) free(a)
172#endif
173
174#if ! COAP_WITH_LIBMBEDTLS_OSCORE
175static char *
176coap_mbedtls_strdup_alloc(const char *s) {
177 size_t len;
178 char *copy;
179
180 if (!s) {
181 return NULL;
182 }
183 len = strlen(s) + 1;
184 copy = (char *)mbedtls_malloc(len);
185 if (copy) {
186 memcpy(copy, s, len);
187 }
188 return copy;
189}
190
191static char *
192coap_mbedtls_strndup_alloc(const char *s, size_t n) {
193 size_t len;
194 char *copy;
195
196 if (!s) {
197 return NULL;
198 }
199 len = strnlen(s, n);
200 copy = (char *)mbedtls_malloc(len + 1);
201 if (copy) {
202 memcpy(copy, s, len);
203 copy[len] = '\0';
204 }
205 return copy;
206}
207#endif /* ! COAP_WITH_LIBMBEDTLS_OSCORE */
208
209#ifndef mbedtls_strdup
210#define mbedtls_strdup(a) coap_mbedtls_strdup_alloc(a)
211#endif
212#ifndef mbedtls_strndup
213#define mbedtls_strndup(a, b) coap_mbedtls_strndup_alloc((a), (b))
214#endif
215
216#ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
217/* definition changed in later mbedtls code versions */
218#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
219#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
220#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
221#endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
222
223#if ! COAP_SERVER_SUPPORT
224#undef MBEDTLS_SSL_SRV_C
225#endif /* ! COAP_SERVER_SUPPORT */
226#if ! COAP_CLIENT_SUPPORT
227#undef MBEDTLS_SSL_CLI_C
228#endif /* ! COAP_CLIENT_SUPPORT */
229
230#ifdef _WIN32
231#define strcasecmp _stricmp
232#endif
233
234/*
235 * Crypto Abstraction Functions
236 * These provide a unified API for both legacy mbedTLS and PSA Crypto.
237 */
238
239#if COAP_SERVER_SUPPORT
240/* SHA-256 Digest Functions */
241
242static int
243coap_crypto_sha256_init(coap_crypto_sha256_ctx_t *ctx) {
244#if COAP_USE_PSA_CRYPTO
245 *ctx = psa_hash_operation_init();
246 return (psa_hash_setup(ctx, PSA_ALG_SHA_256) == PSA_SUCCESS) ? 0 : -1;
247#else
248 mbedtls_sha256_init(ctx);
249#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
250 return mbedtls_sha256_starts_ret(ctx, 0);
251#else
252 return mbedtls_sha256_starts(ctx, 0);
253#endif
254#endif
255}
256
257static int
258coap_crypto_sha256_update(coap_crypto_sha256_ctx_t *ctx,
259 const uint8_t *data, size_t len) {
260#if COAP_USE_PSA_CRYPTO
261 return (psa_hash_update(ctx, data, len) == PSA_SUCCESS) ? 0 : -1;
262#else
263#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
264 return mbedtls_sha256_update_ret(ctx, data, len);
265#else
266 return mbedtls_sha256_update(ctx, data, len);
267#endif
268#endif
269}
270
271static int
272coap_crypto_sha256_finish(coap_crypto_sha256_ctx_t *ctx, uint8_t *output) {
273#if COAP_USE_PSA_CRYPTO
274 size_t hash_len;
275 return (psa_hash_finish(ctx, output, 32, &hash_len) == PSA_SUCCESS) ? 0 : -1;
276#else
277#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
278 return mbedtls_sha256_finish_ret(ctx, output);
279#else
280 return mbedtls_sha256_finish(ctx, output);
281#endif
282#endif
283}
284
285static void
286coap_crypto_sha256_free(coap_crypto_sha256_ctx_t *ctx) {
287#if COAP_USE_PSA_CRYPTO
288 psa_hash_abort(ctx);
289#else
290 mbedtls_sha256_free(ctx);
291#endif
292}
293#endif /* COAP_SERVER_SUPPORT */
294
295/* General Hash Functions */
296#if COAP_WS_SUPPORT
297static size_t
298coap_crypto_hash_size(coap_crypto_md_type_t md_type) {
299#if COAP_USE_PSA_CRYPTO
300 switch ((int)md_type) {
301 case PSA_ALG_SHA_1:
302 return 20;
303 case PSA_ALG_SHA_256:
304 return 32;
305 case PSA_ALG_SHA_384:
306 return 48;
307 case PSA_ALG_SHA_512:
308 return 64;
309 default:
310 return 0;
311 }
312#else
313 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
314 return md_info ? mbedtls_md_get_size(md_info) : 0;
315#endif
316}
317
318static int
319coap_crypto_hash_compute(coap_crypto_md_type_t md_type,
320 const uint8_t *input, size_t ilen,
321 uint8_t *output, size_t output_size,
322 size_t *output_len) {
323#if COAP_USE_PSA_CRYPTO
324 size_t actual_len;
325 psa_status_t status = psa_hash_compute(md_type, input, ilen,
326 output, output_size, &actual_len);
327 if (output_len)
328 *output_len = actual_len;
329 return (status == PSA_SUCCESS) ? 0 : -1;
330#else
331 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
332 if (!md_info)
333 return -1;
334 size_t hash_len = mbedtls_md_get_size(md_info);
335 if (hash_len > output_size)
336 return -1;
337 if (output_len)
338 *output_len = hash_len;
339 return mbedtls_md(md_info, input, ilen, output);
340#endif
341}
342#endif /* COAP_WS_SUPPORT */
343
344
345#endif /* COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE */
346
347#if COAP_WITH_LIBMBEDTLS
348
349#define IS_PSK (1 << 0)
350#define IS_PKI (1 << 1)
351#define IS_CLIENT (1 << 6)
352#define IS_SERVER (1 << 7)
353
354typedef struct coap_ssl_t {
355 const uint8_t *pdu;
356 unsigned pdu_len;
357 unsigned peekmode;
358} coap_ssl_t;
359
360/*
361 * This structure encapsulates the Mbed TLS session object.
362 * It handles both TLS and DTLS.
363 * c_session->tls points to this.
364 */
365typedef struct coap_mbedtls_env_t {
366 mbedtls_ssl_context ssl;
367#if !COAP_USE_PSA_CRYPTO
368 mbedtls_entropy_context entropy;
369 mbedtls_ctr_drbg_context ctr_drbg;
370#endif /* !COAP_USE_PSA_CRYPTO */
371 mbedtls_ssl_config conf;
372 mbedtls_timing_delay_context timer;
373 mbedtls_x509_crt cacert;
374 mbedtls_x509_crt public_cert;
375 mbedtls_pk_context private_key;
376 mbedtls_ssl_cookie_ctx cookie_ctx;
377 /* If not set, need to do do_mbedtls_handshake */
378 int established;
379 int sent_alert;
380 int seen_client_hello;
381 int ec_jpake;
382 coap_tick_t last_timeout;
383 unsigned int retry_scalar;
384 coap_ssl_t coap_ssl_data;
385 uint32_t server_hello_cnt;
386} coap_mbedtls_env_t;
387
388typedef struct pki_sni_entry {
389 char *sni;
390 coap_dtls_key_t pki_key;
391 mbedtls_x509_crt cacert;
392 mbedtls_x509_crt public_cert;
393 mbedtls_pk_context private_key;
394} pki_sni_entry;
395
396typedef struct psk_sni_entry {
397 char *sni;
398 coap_dtls_spsk_info_t psk_info;
399} psk_sni_entry;
400
401typedef struct coap_mbedtls_context_t {
402 coap_dtls_pki_t setup_data;
403 size_t pki_sni_count;
404 /* Entries are not moved, as handshakes refer to their certificates */
405 pki_sni_entry **pki_sni_entry_list;
406 size_t psk_sni_count;
407 psk_sni_entry *psk_sni_entry_list;
408 char *root_ca_file;
409 char *root_ca_path;
410 int trust_store_defined;
411 int psk_pki_enabled;
412} coap_mbedtls_context_t;
413
414typedef enum coap_enc_method_t {
415 COAP_ENC_PSK,
416 COAP_ENC_PKI,
417 COAP_ENC_ECJPAKE,
418} coap_enc_method_t;
419
420#ifdef __ZEPHYR__
421
422typedef struct {
423 uint32_t start_time;
424 uint32_t int_time;
425 uint32_t fin_time;
426} zephyr_timing_delay_context;
427
428static void
429zephyr_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) {
430 zephyr_timing_delay_context *ctx = (zephyr_timing_delay_context *)data;
431
432 if (ctx == NULL) {
433 return;
434 }
435
436 ctx->start_time = k_uptime_get_32();
437
438 if (fin_ms != 0) {
439 ctx->int_time = ctx->start_time + int_ms;
440 ctx->fin_time = ctx->start_time + fin_ms;
441 } else {
442 ctx->int_time = 0;
443 ctx->fin_time = 0;
444 }
445}
446
447static int
448zephyr_timing_get_delay(void *data) {
449 zephyr_timing_delay_context *ctx = (zephyr_timing_delay_context *)data;
450 uint32_t now;
451
452 if (ctx == NULL || ctx->fin_time == 0) {
453 return -1;
454 }
455
456 now = k_uptime_get_32();
457
458 if (now >= ctx->fin_time) {
459 return 2;
460 }
461
462 if (now >= ctx->int_time) {
463 return 1;
464 }
465
466 return 0;
467}
468
469#endif /* __ZEPHYR__ */
470
471#if !COAP_USE_PSA_CRYPTO
472#ifndef MBEDTLS_2_X_COMPAT
473/*
474 * mbedtls_ callback functions expect 0 on success, -ve on failure.
475 */
476static int
477coap_rng(void *ctx COAP_UNUSED, unsigned char *buf, size_t len) {
478 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
479}
480#endif /* MBEDTLS_2_X_COMPAT */
481#endif /* !COAP_USE_PSA_CRYPTO */
482
483static int
484coap_dgram_read(void *ctx, unsigned char *out, size_t outl) {
485 ssize_t ret = 0;
486 coap_session_t *c_session = (coap_session_t *)ctx;
487 coap_ssl_t *data;
488
489 if (!c_session->tls) {
490 errno = EAGAIN;
491 return MBEDTLS_ERR_SSL_WANT_READ;
492 }
493 data = &((coap_mbedtls_env_t *)c_session->tls)->coap_ssl_data;
494
495 if (out != NULL) {
496 if (data->pdu_len > 0) {
497 if (outl < data->pdu_len) {
498 memcpy(out, data->pdu, outl);
499 ret = outl;
500 data->pdu += outl;
501 data->pdu_len -= outl;
502 } else {
503 memcpy(out, data->pdu, data->pdu_len);
504 ret = data->pdu_len;
505 if (!data->peekmode) {
506 data->pdu_len = 0;
507 data->pdu = NULL;
508 }
509 }
510 } else {
511 ret = MBEDTLS_ERR_SSL_WANT_READ;
512 errno = EAGAIN;
513 }
514 }
515 return ret;
516}
517
518/*
519 * return +ve data amount
520 * 0 no more
521 * -ve Mbed TLS error
522 */
523/* callback function given to mbedtls for sending data over socket */
524static int
525coap_dgram_write(void *ctx, const unsigned char *send_buffer,
526 size_t send_buffer_length) {
527 ssize_t result = -1;
528 coap_session_t *c_session = (coap_session_t *)ctx;
529
530 if (c_session) {
531 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
532
533 if (!coap_netif_available(c_session)
535 && c_session->endpoint == NULL
536#endif /* COAP_SERVER_SUPPORT */
537 ) {
538 /* socket was closed on client due to error */
539 errno = ECONNRESET;
540 return -1;
541 }
542 result = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
543 send_buffer, send_buffer_length);
544 if (result != (ssize_t)send_buffer_length) {
545 int keep_errno = errno;
546
547 coap_log_warn("coap_netif_dgrm_write failed (%" PRIdS " != %" PRIuS ")\n",
548 result, send_buffer_length);
549 errno = keep_errno;
550 if (result < 0) {
551 if (errno == ENOTCONN || errno == ECONNREFUSED)
553 return -1;
554 } else {
555 result = 0;
556 }
557 } else if (m_env) {
558 coap_tick_t now;
559 coap_ticks(&now);
560 m_env->last_timeout = now;
561 }
562 } else {
563 result = 0;
564 }
565 return result;
566}
567
568#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
569/*
570 * Server side PSK callback
571 */
572static int
573psk_server_callback(void *p_info, mbedtls_ssl_context *ssl,
574 const unsigned char *identity, size_t identity_len) {
575 coap_session_t *c_session = (coap_session_t *)p_info;
576 coap_dtls_spsk_t *setup_data;
577 coap_mbedtls_env_t *m_env;
578 coap_bin_const_t lidentity;
579 const coap_bin_const_t *psk_key;
580
581 if (c_session == NULL)
582 return -1;
583
584 /* Track the Identity being used */
585 lidentity.s = identity ? (const uint8_t *)identity : (const uint8_t *)"";
586 lidentity.length = identity ? identity_len : 0;
587 coap_session_refresh_psk_identity(c_session, &lidentity);
588
589 coap_log_debug("got psk_identity: '%.*s'\n",
590 (int)lidentity.length, (const char *)lidentity.s);
591
592 m_env = (coap_mbedtls_env_t *)c_session->tls;
593 setup_data = &c_session->context->spsk_setup_data;
594
595 if (setup_data->validate_id_call_back) {
596 psk_key = setup_data->validate_id_call_back(&lidentity,
597 c_session,
598 setup_data->id_call_back_arg);
599
600 coap_session_refresh_psk_key(c_session, psk_key);
601 } else {
602 psk_key = coap_get_session_server_psk_key(c_session);
603 }
604
605 if (psk_key == NULL)
606 return -1;
607 mbedtls_ssl_set_hs_psk(ssl, psk_key->s, psk_key->length);
608 m_env->seen_client_hello = 1;
609 return 0;
610}
611#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED && MBEDTLS_SSL_SRV_C */
612
613static char *
614get_san_or_cn_from_cert(coap_session_t *session, mbedtls_x509_crt *crt, const char *sni_match) {
615 if (crt) {
616 const mbedtls_asn1_named_data *cn_data;
617
618 (void)session;
619 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
620 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
621 while (seq) {
622 while (seq->buf.p == NULL) {
623 seq = seq->next;
624 }
625 if (seq) {
626 if (sni_match) {
627 if (strlen(sni_match) != seq->buf.len ||
628 memcmp(seq->buf.p, sni_match, seq->buf.len)) {
629 seq = seq->next;
630 continue;
631 }
632 }
633 /* Return the Subject Alt Name */
634 return mbedtls_strndup((const char *)seq->buf.p,
635 seq->buf.len);
636 }
637 }
638 }
639
640 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
641 MBEDTLS_OID_AT_CN,
642 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
643 if (cn_data) {
644 if (sni_match) {
645 if (!coap_pki_name_match_sni((char *)cn_data->val.p, cn_data->val.len, sni_match)) {
646 coap_log_debug(" %s: got CN '%*.*s'\n",
647 coap_session_str(session), (int)cn_data->val.len,
648 (int)cn_data->val.len, cn_data->val.p);
649 goto no_match;
650 }
651 }
652 /* Return the Common Name */
653 return mbedtls_strndup((const char *)cn_data->val.p,
654 cn_data->val.len);
655 }
656no_match:
657 if (!sni_match) {
658 return NULL;
659 }
660 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
661 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
662 int n = 0;
663
664 while (seq) {
665 while (seq->buf.p == NULL) {
666 seq = seq->next;
667 }
668 if (seq) {
669 coap_log_debug(" %s: got DNS.%d '%*.*s'\n",
670 coap_session_str(session), n + 1, (int)seq->buf.len,
671 (int)seq->buf.len, seq->buf.p);
672 seq = seq->next;
673 n++;
674 continue;
675 }
676 }
677 }
678 }
679 return NULL;
680}
681
682#if COAP_MAX_LOGGING_LEVEL > 0
683static char *
684get_error_string(int ret) {
685 static char buf[128] = {0};
686 mbedtls_strerror(ret, buf, sizeof(buf)-1);
687 return buf;
688}
689#endif /* COAP_MAX_LOGGING_LEVEL */
690
691static int
692self_signed_cert_verify_callback_mbedtls(void *data,
693 mbedtls_x509_crt *crt COAP_UNUSED,
694 int depth COAP_UNUSED,
695 uint32_t *flags) {
696 const coap_session_t *c_session = (coap_session_t *)data;
697 const coap_mbedtls_context_t *m_context =
698 (coap_mbedtls_context_t *)c_session->context->dtls_context;
699 const coap_dtls_pki_t *setup_data = &m_context->setup_data;
700
701 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
702 if (setup_data->allow_expired_certs) {
703 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
704 }
705 }
706 return 0;
707}
708
709/*
710 * return 0 All OK
711 * -ve Error Code
712 */
713static int
714cert_verify_callback_mbedtls(void *data, mbedtls_x509_crt *crt,
715 int depth, uint32_t *flags) {
716 coap_session_t *c_session = (coap_session_t *)data;
717 coap_mbedtls_context_t *m_context =
718 (coap_mbedtls_context_t *)c_session->context->dtls_context;
719 coap_dtls_pki_t *setup_data = &m_context->setup_data;
720 char *cn = NULL;
721
722 if (!setup_data->allow_sni_cn_mismatch && depth == 0 &&
723 c_session->type == COAP_SESSION_TYPE_CLIENT && setup_data->client_sni) {
724
725 cn = get_san_or_cn_from_cert(c_session, crt, setup_data->client_sni);
726 if (!cn) {
727 coap_log_info(" %s: SNI '%s' not returned in certificate\n",
728 coap_session_str(c_session), setup_data->client_sni);
729 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
730 }
731 }
732 if (!cn) {
733 cn = get_san_or_cn_from_cert(c_session, crt, NULL);
734 }
735 coap_dtls_log(COAP_LOG_DEBUG, "depth %d flags %x cert '%s'\n",
736 depth, *flags, cn);
737 if (setup_data->validate_cn_call_back) {
738 int ret;
739
741 setup_data->validate_cn_call_back(cn,
742 crt->raw.p,
743 crt->raw.len,
744 c_session,
745 depth,
746 *flags == 0,
747 setup_data->cn_call_back_arg));
748 if (!ret) {
749 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
750 }
751 }
752 if (setup_data->cert_chain_validation &&
753 depth > (setup_data->cert_chain_verify_depth + 2)) {
754 *flags |= MBEDTLS_X509_BADCERT_OTHER;
755 coap_log_warn(" %s: %s: '%s' depth %d\n",
756 coap_session_str(c_session),
757 "The certificate's verify depth is too long",
758 cn ? cn : "?", depth);
759 }
760 if (*flags == 0) {
761 if (cn)
762 mbedtls_free(cn);
763 return 0;
764 }
765
766 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
767 if (setup_data->allow_expired_certs) {
768 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
769 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
770 coap_session_str(c_session),
771 "The certificate has expired", cn ? cn : "?", depth);
772 }
773 }
774 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
775 if (setup_data->allow_expired_certs) {
776 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
777 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
778 coap_session_str(c_session),
779 "The certificate has a future date", cn ? cn : "?", depth);
780 }
781 }
782 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
783 if (setup_data->allow_bad_md_hash) {
784 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
785 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
786 coap_session_str(c_session),
787 "The certificate has a bad MD hash", cn ? cn : "?", depth);
788 }
789 }
790 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
791 if (setup_data->allow_short_rsa_length) {
792 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
793 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
794 coap_session_str(c_session),
795 "The certificate has a short RSA length", cn ? cn : "?", depth);
796 }
797 }
798 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
799 if (setup_data->allow_sni_cn_mismatch) {
800 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
801 coap_log_info(" %s: %s: overridden: '%s' v '%s' depth %d\n",
802 coap_session_str(c_session),
803 "The SNI does not match the returned CN",
804 setup_data->client_sni ? setup_data->client_sni : "",
805 cn ? cn : "?", depth);
806 }
807 }
808 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
809 uint32_t lflags;
810 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
811 self_signed_cert_verify_callback_mbedtls,
812 data);
813 if (self_signed && depth == 0) {
814 if (setup_data->allow_self_signed &&
815 !setup_data->check_common_ca) {
816 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
817 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
818 coap_session_str(c_session),
819 "Self-signed",
820 cn ? cn : "?", depth);
821 }
822 } else if (self_signed) {
823 if (!setup_data->verify_peer_cert) {
824 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
825 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
826 coap_session_str(c_session),
827 "Self-signed", cn ? cn : "?", depth);
828 }
829 } else {
830 if (!setup_data->verify_peer_cert) {
831 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
832 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
833 coap_session_str(c_session),
834 "The certificate's CA is not trusted", cn ? cn : "?", depth);
835 }
836 }
837 }
838 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
839 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
840 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
841 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
842 coap_session_str(c_session),
843 "The certificate's CRL has expired", cn ? cn : "?", depth);
844 } else if (!setup_data->check_cert_revocation) {
845 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
846 }
847 }
848 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
849 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
850 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
851 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
852 coap_session_str(c_session),
853 "The certificate's CRL has a future date", cn ? cn : "?", depth);
854 } else if (!setup_data->check_cert_revocation) {
855 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
856 }
857 }
858
859 if (*flags != 0) {
860 char buf[128];
861 char *tcp;
862 int ret = mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", *flags);
863
864 if (ret >= 0) {
865 tcp = strchr(buf, '\n');
866 while (tcp) {
867 *tcp = '\000';
868 coap_log_warn(" %s: %s: issue 0x%" PRIx32 ": '%s' depth %d\n",
869 coap_session_str(c_session),
870 buf, *flags, cn ? cn : "?", depth);
871 tcp = strchr(tcp+1, '\n');
872 }
873 } else {
874 coap_log_err("mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
875 -ret, get_error_string(ret));
876 }
877 }
878
879 if (cn)
880 mbedtls_free(cn);
881
882 return 0;
883}
884
885static int
886setup_pki_credentials(mbedtls_x509_crt *cacert,
887 mbedtls_x509_crt *public_cert,
888 mbedtls_pk_context *private_key,
889 coap_mbedtls_env_t *m_env,
890 coap_mbedtls_context_t *m_context,
891 coap_session_t *c_session,
892 coap_dtls_pki_t *setup_data,
893 coap_dtls_role_t role) {
894 coap_dtls_key_t key;
895 int ret;
896 int done_private_key = 0;
897 int done_public_cert = 0;
898 uint8_t *buffer;
899 size_t length;
900
901 /* Map over to the new define format to save code duplication */
902 coap_dtls_map_key_type_to_define(setup_data, &key);
903
904 assert(key.key_type == COAP_PKI_KEY_DEFINE);
905
906 /*
907 * Configure the Private Key
908 */
909 if (key.key.define.private_key.u_byte &&
910 key.key.define.private_key.u_byte[0]) {
911 switch (key.key.define.private_key_def) {
912 case COAP_PKI_KEY_DEF_DER: /* define private key */
913 /* Fall Through */
914 case COAP_PKI_KEY_DEF_PEM: /* define private key */
915#if defined(MBEDTLS_FS_IO)
916 mbedtls_pk_init(private_key);
917#if COAP_USE_PSA_CRYPTO
918 ret = mbedtls_pk_parse_keyfile(private_key,
920#elif defined(MBEDTLS_2_X_COMPAT)
921 ret = mbedtls_pk_parse_keyfile(private_key,
923#else
924 ret = mbedtls_pk_parse_keyfile(private_key,
926 NULL, coap_rng, (void *)&m_env->ctr_drbg);
927#endif
928 if (ret < 0) {
931 &key, role, ret);
932 }
933 done_private_key = 1;
934 break;
935#else /* ! MBEDTLS_FS_IO */
938 &key, role, -1);
939#endif /* ! MBEDTLS_FS_IO */
940 case COAP_PKI_KEY_DEF_PEM_BUF: /* define private key */
941 mbedtls_pk_init(private_key);
942 length = key.key.define.private_key_len;
943 if (key.key.define.private_key.u_byte[length-1] != '\000') {
944 /* Need to allocate memory to add in NULL terminator */
945 buffer = mbedtls_malloc(length + 1);
946 if (!buffer) {
947 coap_log_err("mbedtls_malloc failed\n");
948 return -1;
949 }
950 memcpy(buffer, key.key.define.private_key.u_byte, length);
951 buffer[length] = '\000';
952 length++;
953#if COAP_USE_PSA_CRYPTO
954 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
955#elif defined(MBEDTLS_2_X_COMPAT)
956 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
957#else
958 ret = mbedtls_pk_parse_key(private_key, buffer, length,
959 NULL, 0, coap_rng, (void *)&m_env->ctr_drbg);
960#endif
961 mbedtls_free(buffer);
962 } else {
963#if COAP_USE_PSA_CRYPTO
964 ret = mbedtls_pk_parse_key(private_key,
967#elif defined(MBEDTLS_2_X_COMPAT)
968 ret = mbedtls_pk_parse_key(private_key,
971#else
972 ret = mbedtls_pk_parse_key(private_key,
975 NULL, 0, coap_rng, (void *)&m_env->ctr_drbg);
976#endif
977 }
978 if (ret < 0) {
981 &key, role, ret);
982 }
983 done_private_key = 1;
984 break;
985 case COAP_PKI_KEY_DEF_DER_BUF: /* define private key */
986 mbedtls_pk_init(private_key);
987#if COAP_USE_PSA_CRYPTO
988 ret = mbedtls_pk_parse_key(private_key,
991#elif defined(MBEDTLS_2_X_COMPAT)
992 ret = mbedtls_pk_parse_key(private_key,
995#else
996 ret = mbedtls_pk_parse_key(private_key,
998 key.key.define.private_key_len, NULL, 0, coap_rng,
999 (void *)&m_env->ctr_drbg);
1000#endif
1001 if (ret < 0) {
1004 &key, role, ret);
1005 }
1006 done_private_key = 1;
1007 break;
1008 case COAP_PKI_KEY_DEF_RPK_BUF: /* define private key */
1009 case COAP_PKI_KEY_DEF_PKCS11: /* define private key */
1010 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define private key */
1011 case COAP_PKI_KEY_DEF_ENGINE: /* define private key */
1012 default:
1015 &key, role, -1);
1016 }
1017 } else if (role == COAP_DTLS_ROLE_SERVER ||
1019 key.key.define.public_cert.u_byte[0])) {
1022 &key, role, -1);
1023 }
1024
1025 /*
1026 * Configure the Public Certificate / Key
1027 */
1028 if (key.key.define.public_cert.u_byte &&
1029 key.key.define.public_cert.u_byte[0]) {
1030 switch (key.key.define.public_cert_def) {
1031 case COAP_PKI_KEY_DEF_DER: /* define public cert */
1032 /* Fall Through */
1033 case COAP_PKI_KEY_DEF_PEM: /* define public cert */
1034#if defined(MBEDTLS_FS_IO)
1035 mbedtls_x509_crt_init(public_cert);
1036 ret = mbedtls_x509_crt_parse_file(public_cert,
1038 if (ret < 0) {
1041 &key, role, ret);
1042 }
1043 done_public_cert = 1;
1044 break;
1045#else /* ! MBEDTLS_FS_IO */
1048 &key, role, -1);
1049#endif /* ! MBEDTLS_FS_IO */
1050 case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */
1051 mbedtls_x509_crt_init(public_cert);
1052
1053 length = key.key.define.public_cert_len;
1054 if (key.key.define.public_cert.u_byte[length-1] != '\000') {
1055 /* Need to allocate memory to add in NULL terminator */
1056 buffer = mbedtls_malloc(length + 1);
1057 if (!buffer) {
1058 coap_log_err("mbedtls_malloc failed\n");
1059 return -1;
1060 }
1061 memcpy(buffer, key.key.define.public_cert.u_byte, length);
1062 buffer[length] = '\000';
1063 length++;
1064 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
1065 mbedtls_free(buffer);
1066 } else {
1067 ret = mbedtls_x509_crt_parse(public_cert,
1070 }
1071 if (ret < 0) {
1074 &key, role, ret);
1075 }
1076 done_public_cert = 1;
1077 break;
1078 case COAP_PKI_KEY_DEF_RPK_BUF: /* define public cert */
1081 &key, role, -1);
1082 case COAP_PKI_KEY_DEF_DER_BUF: /* define public cert */
1083 mbedtls_x509_crt_init(public_cert);
1084 ret = mbedtls_x509_crt_parse(public_cert,
1087 if (ret < 0) {
1090 &key, role, ret);
1091 }
1092 done_public_cert = 1;
1093 break;
1094 case COAP_PKI_KEY_DEF_PKCS11: /* define public cert */
1095 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define public cert */
1096 case COAP_PKI_KEY_DEF_ENGINE: /* define public cert */
1097 default:
1100 &key, role, -1);
1101 }
1102 } else if (role == COAP_DTLS_ROLE_SERVER ||
1104 key.key.define.private_key.u_byte[0])) {
1107 &key, role, -1);
1108 }
1109
1110 if (done_private_key && done_public_cert) {
1111 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
1112 if (ret < 0) {
1113 coap_log_err("mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
1114 -ret, get_error_string(ret));
1115 return -1;
1116 }
1117 }
1118
1119 /*
1120 * Configure the CA
1121 */
1122 if (
1123 key.key.define.ca.u_byte &&
1124 key.key.define.ca.u_byte[0]) {
1125 switch (key.key.define.ca_def) {
1126 case COAP_PKI_KEY_DEF_DER: /* define ca */
1127 /* Fall Through */
1129#if defined(MBEDTLS_FS_IO)
1130 mbedtls_x509_crt_init(cacert);
1131 ret = mbedtls_x509_crt_parse_file(cacert,
1132 key.key.define.ca.s_byte);
1133 if (ret < 0) {
1136 &key, role, ret);
1137 }
1138 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1139#else /* ! MBEDTLS_FS_IO */
1142 &key, role, -1);
1143#endif /* ! MBEDTLS_FS_IO */
1144 break;
1145 case COAP_PKI_KEY_DEF_PEM_BUF: /* define ca */
1146 mbedtls_x509_crt_init(cacert);
1147 length = key.key.define.ca_len;
1148 if (key.key.define.ca.u_byte[length-1] != '\000') {
1149 /* Need to allocate memory to add in NULL terminator */
1150 buffer = mbedtls_malloc(length + 1);
1151 if (!buffer) {
1152 coap_log_err("mbedtls_malloc failed\n");
1153 return -1;
1154 }
1155 memcpy(buffer, key.key.define.ca.u_byte, length);
1156 buffer[length] = '\000';
1157 length++;
1158 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
1159 mbedtls_free(buffer);
1160 } else {
1161 ret = mbedtls_x509_crt_parse(cacert,
1162 key.key.define.ca.u_byte,
1163 key.key.define.ca_len);
1164 }
1165 if (ret < 0) {
1168 &key, role, ret);
1169 }
1170 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1171 break;
1172 case COAP_PKI_KEY_DEF_RPK_BUF: /* define ca */
1175 &key, role, -1);
1176 case COAP_PKI_KEY_DEF_DER_BUF: /* define ca */
1177 mbedtls_x509_crt_init(cacert);
1178 ret = mbedtls_x509_crt_parse(cacert,
1179 key.key.define.ca.u_byte,
1180 key.key.define.ca_len);
1181 if (ret < 0) {
1184 &key, role, ret);
1185 }
1186 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1187 break;
1188 case COAP_PKI_KEY_DEF_PKCS11: /* define ca */
1189 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define ca */
1190 case COAP_PKI_KEY_DEF_ENGINE: /* define ca */
1191 default:
1194 &key, role, -1);
1195 }
1196 }
1197
1198 /* Add in any root CA definitions */
1199
1200#if defined(MBEDTLS_FS_IO)
1201 if (m_context->root_ca_file) {
1202 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
1203 if (ret < 0) {
1207 &key, role, ret);
1208 }
1209 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1210 }
1211 if (m_context->root_ca_path) {
1212 ret = mbedtls_x509_crt_parse_path(cacert, m_context->root_ca_path);
1213 if (ret < 0) {
1217 &key, role, ret);
1218 }
1219 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1220 }
1221#if MBEDTLS_VERSION_NUMBER >= 0x04000000
1222 if (m_context->trust_store_defined)
1223#else /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1224 if (m_context->trust_store_defined ||
1225 (role == COAP_DTLS_ROLE_CLIENT && !m_context->setup_data.verify_peer_cert))
1226#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1227 {
1228 /* Until Trust Store is implemented in MbedTLS */
1229 const char *trust_list[] = {
1230 "/etc/ssl/ca-bundle.pem",
1231 "/etc/ssl/certs/ca-certificates.crt",
1232 "/etc/pki/tls/cert.pem",
1233 "/usr/local/share/certs/ca-root-nss.crt",
1234 "/etc/ssl/cert.pem"
1235 };
1236 static const char *trust_file_found = NULL;
1237 static int trust_file_done = 0;
1238 unsigned int i;
1239
1240 if (trust_file_found) {
1241 ret = mbedtls_x509_crt_parse_file(cacert, trust_file_found);
1242 if (ret >= 0) {
1243 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1244 } else {
1245 coap_log_warn("Unable to load trusted root CAs (%s)\n",
1246 trust_file_found);
1247 }
1248 } else if (!trust_file_done) {
1249 trust_file_done = 1;
1250 for (i = 0; i < sizeof(trust_list)/sizeof(trust_list[0]); i++) {
1251 ret = mbedtls_x509_crt_parse_file(cacert, trust_list[i]);
1252 if (ret >= 0) {
1253 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1254 trust_file_found = trust_list[i];
1255 break;
1256 }
1257 }
1258 if (i == sizeof(trust_list)/sizeof(trust_list[0])) {
1259 coap_log_warn("Unable to load trusted root CAs\n");
1260 }
1261 }
1262 }
1263#else /* ! MBEDTLS_FS_IO */
1264 if (m_context->root_ca_file || m_context->root_ca_path ||
1265 m_context->trust_store_defined) {
1266 /* No FS to read these files */
1269 &key, role, -1);
1270 }
1271#endif /* ! MBEDTLS_FS_IO */
1272
1273#if defined(MBEDTLS_SSL_SRV_C)
1274 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
1275 setup_data->check_common_ca ?
1276 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
1277 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
1278#endif
1279 mbedtls_ssl_conf_authmode(&m_env->conf, setup_data->verify_peer_cert ?
1280 MBEDTLS_SSL_VERIFY_REQUIRED :
1281 MBEDTLS_SSL_VERIFY_NONE);
1282 /*
1283 * Verify Peer.
1284 * Need to do all checking, even if setup_data->verify_peer_cert is not set
1285 */
1286 mbedtls_ssl_conf_verify(&m_env->conf,
1287 cert_verify_callback_mbedtls, c_session);
1288
1289 return 1;
1290}
1291
1292#if defined(MBEDTLS_SSL_SRV_C)
1293/*
1294 * PKI SNI callback.
1295 */
1296static int
1297pki_sni_callback(void *p_info, mbedtls_ssl_context *ssl,
1298 const unsigned char *uname, size_t name_len) {
1299 unsigned int i;
1300 coap_dtls_pki_t sni_setup_data;
1301 coap_session_t *c_session = (coap_session_t *)p_info;
1302 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
1303 coap_mbedtls_context_t *m_context =
1304 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1305 char *name;
1306
1307 name = mbedtls_malloc(name_len+1);
1308 if (!name)
1309 return -1;
1310
1311 memcpy(name, uname, name_len);
1312 name[name_len] = '\000';
1313
1314 /* Is this a cached entry? */
1315 for (i = 0; i < m_context->pki_sni_count; i++) {
1316 if (strcasecmp(name, m_context->pki_sni_entry_list[i]->sni) == 0) {
1317 break;
1318 }
1319 }
1320 if (i == m_context->pki_sni_count) {
1321 /*
1322 * New PKI SNI request
1323 */
1324 coap_dtls_key_t *new_entry;
1325 pki_sni_entry **pki_sni_entry_list;
1326 pki_sni_entry *entry;
1327
1328 coap_lock_callback_ret(new_entry,
1329 m_context->setup_data.validate_sni_call_back(name,
1330 m_context->setup_data.sni_call_back_arg));
1331 if (!new_entry) {
1332 mbedtls_free(name);
1333 return -1;
1334 }
1335
1336 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
1337 (i+1)*sizeof(pki_sni_entry *));
1338
1339 if (pki_sni_entry_list == NULL) {
1340 mbedtls_free(name);
1341 return -1;
1342 }
1343 m_context->pki_sni_entry_list = pki_sni_entry_list;
1344 entry = mbedtls_malloc(sizeof(pki_sni_entry));
1345 if (entry == NULL) {
1346 mbedtls_free(name);
1347 return -1;
1348 }
1349 memset(entry, 0, sizeof(*entry));
1350 mbedtls_x509_crt_init(&entry->cacert);
1351 mbedtls_x509_crt_init(&entry->public_cert);
1352 mbedtls_pk_init(&entry->private_key);
1353 entry->pki_key = *new_entry;
1354 sni_setup_data = m_context->setup_data;
1355 sni_setup_data.pki_key = *new_entry;
1356 if (setup_pki_credentials(&entry->cacert,
1357 &entry->public_cert,
1358 &entry->private_key,
1359 m_env,
1360 m_context,
1361 c_session,
1362 &sni_setup_data, COAP_DTLS_ROLE_SERVER) < 0) {
1363 mbedtls_x509_crt_free(&entry->public_cert);
1364 mbedtls_pk_free(&entry->private_key);
1365 mbedtls_x509_crt_free(&entry->cacert);
1366 mbedtls_free(entry);
1367 mbedtls_free(name);
1368 return -1;
1369 }
1370 /* name has been absorbed into pki_sni_entry_list[]->sni entry */
1371 entry->sni = name;
1372 m_context->pki_sni_entry_list[i] = entry;
1373 m_context->pki_sni_count++;
1374 } else {
1375 mbedtls_free(name);
1376 }
1377
1378 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i]->cacert,
1379 NULL);
1380 return mbedtls_ssl_set_hs_own_cert(ssl,
1381 &m_context->pki_sni_entry_list[i]->public_cert,
1382 &m_context->pki_sni_entry_list[i]->private_key);
1383}
1384
1385#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1386/*
1387 * PSK SNI callback.
1388 */
1389static int
1390psk_sni_callback(void *p_info, mbedtls_ssl_context *ssl,
1391 const unsigned char *uname, size_t name_len) {
1392 unsigned int i;
1393 coap_session_t *c_session = (coap_session_t *)p_info;
1394 coap_mbedtls_context_t *m_context =
1395 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1396 char *name;
1397
1398 name = mbedtls_malloc(name_len+1);
1399 if (!name)
1400 return -1;
1401
1402 memcpy(name, uname, name_len);
1403 name[name_len] = '\000';
1404
1405 /* Is this a cached entry? */
1406 for (i = 0; i < m_context->psk_sni_count; i++) {
1407 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
1408 break;
1409 }
1410 }
1411 if (i == m_context->psk_sni_count) {
1412 /*
1413 * New PSK SNI request
1414 */
1415 const coap_dtls_spsk_info_t *new_entry;
1416 psk_sni_entry *psk_sni_entry_list;
1417
1418 coap_lock_callback_ret(new_entry,
1419 c_session->context->spsk_setup_data.validate_sni_call_back(name,
1420 c_session,
1421 c_session->context->spsk_setup_data.sni_call_back_arg));
1422 if (!new_entry) {
1423 mbedtls_free(name);
1424 return -1;
1425 }
1426
1427 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
1428 (i+1)*sizeof(psk_sni_entry));
1429
1430 if (psk_sni_entry_list == NULL) {
1431 mbedtls_free(name);
1432 return -1;
1433 }
1434 m_context->psk_sni_entry_list = psk_sni_entry_list;
1435 m_context->psk_sni_entry_list[i].sni = name;
1436 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1437 /* name has been absorbed into psk_sni_entry_list[].sni entry */
1438 m_context->psk_sni_count++;
1439 } else {
1440 mbedtls_free(name);
1441 }
1442
1444 &m_context->psk_sni_entry_list[i].psk_info.hint);
1446 &m_context->psk_sni_entry_list[i].psk_info.key);
1447#if MBEDTLS_VERSION_NUMBER >= 0x04000000
1448 (void)ssl;
1449 return 0;
1450#else /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1451 return mbedtls_ssl_set_hs_psk(ssl,
1452 m_context->psk_sni_entry_list[i].psk_info.key.s,
1453 m_context->psk_sni_entry_list[i].psk_info.key.length);
1454#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1455}
1456#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1457
1458static int
1459setup_server_ssl_session(coap_session_t *c_session,
1460 coap_mbedtls_env_t *m_env) {
1461 coap_mbedtls_context_t *m_context =
1462 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1463 int ret = 0;
1464 m_context->psk_pki_enabled |= IS_SERVER;
1465
1466 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1467 MBEDTLS_SSL_IS_SERVER,
1468 c_session->proto == COAP_PROTO_DTLS ?
1469 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1470 MBEDTLS_SSL_TRANSPORT_STREAM,
1471 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1472 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1473 -ret, get_error_string(ret));
1474 goto fail;
1475 }
1476
1477#if !COAP_USE_PSA_CRYPTO
1478 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1479#endif /* !COAP_USE_PSA_CRYPTO */
1480
1481#if defined(MBEDTLS_SSL_PROTO_DTLS)
1482 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1484#endif /* MBEDTLS_SSL_PROTO_DTLS */
1485
1486 if (m_context->psk_pki_enabled & IS_PSK) {
1487#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1488 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1489 if (c_session->context->spsk_setup_data.validate_sni_call_back) {
1490 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1491 }
1492#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1493 m_env->ec_jpake = c_session->context->spsk_setup_data.ec_jpake;
1494#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1495#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1496 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1497#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1498 }
1499
1500 if (m_context->psk_pki_enabled & IS_PKI) {
1501 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1502 &m_env->private_key, m_env, m_context,
1503 c_session, &m_context->setup_data,
1505 if (ret < 0) {
1506 coap_log_err("PKI setup failed\n");
1507 return ret;
1508 }
1509 if (m_context->setup_data.validate_sni_call_back) {
1510 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1511 }
1512 }
1513
1514#if COAP_USE_PSA_CRYPTO
1515 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx)) != 0) {
1516#else /* !COAP_USE_PSA_CRYPTO */
1517 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1518 mbedtls_ctr_drbg_random,
1519 &m_env->ctr_drbg)) != 0) {
1520#endif /* !COAP_USE_PSA_CRYPTO */
1521 coap_log_err("mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1522 -ret, get_error_string(ret));
1523 goto fail;
1524 }
1525
1526#if defined(MBEDTLS_SSL_PROTO_DTLS)
1527 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1528 mbedtls_ssl_cookie_check,
1529 &m_env->cookie_ctx);
1530#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1531 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1532#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1533#endif /* MBEDTLS_SSL_PROTO_DTLS */
1534#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1535 /*
1536 * Configure CID max length.
1537 *
1538 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
1539 * to use RFC9146 extension ID of 54, rather than the draft version -05
1540 * value of 254.
1541 */
1542 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1544fail:
1545 return ret;
1546}
1547#endif /* MBEDTLS_SSL_SRV_C */
1548
1549#if COAP_CLIENT_SUPPORT
1550static int *psk_ciphers = NULL;
1551static int *pki_ciphers = NULL;
1552static int *ecjpake_ciphers = NULL;
1553static int processed_ciphers = 0;
1554
1555#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1556static int
1557coap_ssl_ciphersuite_uses_psk(const mbedtls_ssl_ciphersuite_t *info) {
1558#if COAP_USE_PSA_CRYPTO
1559 switch (info->key_exchange) {
1560 case MBEDTLS_KEY_EXCHANGE_PSK:
1561 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1562 return 1;
1563 case MBEDTLS_KEY_EXCHANGE_NONE:
1564 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1565 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1566 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1567 default:
1568 return 0;
1569 }
1570#elif MBEDTLS_VERSION_NUMBER >= 0x03060000
1571 switch (info->key_exchange) {
1572 case MBEDTLS_KEY_EXCHANGE_PSK:
1573 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1574 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1575 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1576 return 1;
1577 case MBEDTLS_KEY_EXCHANGE_NONE:
1578 case MBEDTLS_KEY_EXCHANGE_RSA:
1579 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1580 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1581 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1582 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1583 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1584 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1585 default:
1586 return 0;
1587 }
1588#else /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1589 return mbedtls_ssl_ciphersuite_uses_psk(info);
1590#endif /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1591}
1592#endif /* defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) */
1593
1594/* Cipher suite lists a cipher suite is put in by set_ciphersuites() */
1595#define COAP_CIPHER_IN_PSK 0x01
1596#define COAP_CIPHER_IN_PKI 0x02
1597#define COAP_CIPHER_IN_ECJPAKE 0x04
1598
1599/*
1600 * Returns which of the PSK, PKI and EC JPAKE lists the cipher suite
1601 * belongs in (0 if none). Used for both counting and filling the lists
1602 * so that they always agree.
1603 */
1604static int
1605coap_ciphersuite_lists(int id) {
1606 const mbedtls_ssl_ciphersuite_t *cur = mbedtls_ssl_ciphersuite_from_id(id);
1607
1608 if (!cur)
1609 return 0;
1610#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1611 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1612 /* Minimum of TLS1.2 required - skip */
1613 return 0;
1614 }
1615#else
1616 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1617 /* Minimum of TLS1.2 required - skip */
1618 return 0;
1619 }
1620#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1621#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1622 if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE)
1623 return COAP_CIPHER_IN_ECJPAKE;
1624#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1625#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1626 if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3)
1627 return COAP_CIPHER_IN_PSK | COAP_CIPHER_IN_PKI;
1628#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
1629#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1630 if (coap_ssl_ciphersuite_uses_psk(cur))
1631 return COAP_CIPHER_IN_PSK;
1632#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1633 return COAP_CIPHER_IN_PKI;
1634}
1635
1636static void
1637set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1638 if (!processed_ciphers) {
1639 const int *base = mbedtls_ssl_list_ciphersuites();
1640 const int *list;
1641 int *psk_list;
1642 int *pki_list;
1643 int *ecjpake_list;
1644 int psk_count = 1; /* account for empty terminator */
1645 int pki_count = 1;
1646 int ecjpake_count = 1;
1647
1648 for (list = base; *list; list++) {
1649 int in = coap_ciphersuite_lists(*list);
1650
1651 if (in & COAP_CIPHER_IN_PSK)
1652 psk_count++;
1653 if (in & COAP_CIPHER_IN_PKI)
1654 pki_count++;
1655 if (in & COAP_CIPHER_IN_ECJPAKE)
1656 ecjpake_count++;
1657 }
1658
1659 psk_ciphers = mbedtls_malloc(psk_count * sizeof(psk_ciphers[0]));
1660 pki_ciphers = mbedtls_malloc(pki_count * sizeof(pki_ciphers[0]));
1661 ecjpake_ciphers = mbedtls_malloc(ecjpake_count * sizeof(ecjpake_ciphers[0]));
1662 if (psk_ciphers == NULL || pki_ciphers == NULL || ecjpake_ciphers == NULL) {
1663 coap_log_err("set_ciphers: mbedtls_malloc failed\n");
1664 mbedtls_free(psk_ciphers);
1665 mbedtls_free(pki_ciphers);
1666 mbedtls_free(ecjpake_ciphers);
1667 psk_ciphers = NULL;
1668 pki_ciphers = NULL;
1669 ecjpake_ciphers = NULL;
1670 return;
1671 }
1672
1673 psk_list = psk_ciphers;
1674 pki_list = pki_ciphers;
1675 ecjpake_list = ecjpake_ciphers;
1676
1677 for (list = base; *list; list++) {
1678 int in = coap_ciphersuite_lists(*list);
1679
1680 if (in & COAP_CIPHER_IN_PSK)
1681 *psk_list++ = *list;
1682 if (in & COAP_CIPHER_IN_PKI)
1683 *pki_list++ = *list;
1684 if (in & COAP_CIPHER_IN_ECJPAKE)
1685 *ecjpake_list++ = *list;
1686 }
1687 /* zero terminate */
1688 *psk_list = 0;
1689 *pki_list = 0;
1690 *ecjpake_list = 0;
1691 processed_ciphers = 1;
1692 }
1693 switch (method) {
1694 case COAP_ENC_PSK:
1695 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1696 break;
1697 case COAP_ENC_PKI:
1698 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1699 break;
1700 case COAP_ENC_ECJPAKE:
1701 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1702 break;
1703 default:
1704 assert(0);
1705 break;
1706 }
1707}
1708
1709static int
1710setup_client_ssl_session(coap_session_t *c_session,
1711 coap_mbedtls_env_t *m_env) {
1712 int ret;
1713
1714 coap_mbedtls_context_t *m_context =
1715 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1716
1717 m_context->psk_pki_enabled |= IS_CLIENT;
1718
1719 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1720 MBEDTLS_SSL_IS_CLIENT,
1721 c_session->proto == COAP_PROTO_DTLS ?
1722 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1723 MBEDTLS_SSL_TRANSPORT_STREAM,
1724 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1725 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1726 -ret, get_error_string(ret));
1727 goto fail;
1728 }
1729
1730#if defined(MBEDTLS_SSL_PROTO_DTLS)
1731 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1733#endif /* MBEDTLS_SSL_PROTO_DTLS */
1734
1735 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1736#if !COAP_USE_PSA_CRYPTO
1737 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1738#endif /* !COAP_USE_PSA_CRYPTO */
1739
1740 if (m_context->psk_pki_enabled & IS_PSK) {
1741#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1742 const coap_bin_const_t *psk_key;
1743 const coap_bin_const_t *psk_identity;
1744
1745 coap_log_info("Setting PSK key\n");
1746
1747 psk_key = coap_get_session_client_psk_key(c_session);
1748 psk_identity = coap_get_session_client_psk_identity(c_session);
1749 if (psk_key == NULL || psk_identity == NULL) {
1750 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1751 goto fail;
1752 }
1753
1754 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->s,
1755 psk_key->length, psk_identity->s,
1756 psk_identity->length)) != 0) {
1757 coap_log_err("mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1758 -ret, get_error_string(ret));
1759 goto fail;
1760 }
1761 if (c_session->cpsk_setup_data.client_sni) {
1762 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1763 c_session->cpsk_setup_data.client_sni)) != 0) {
1764 coap_log_err("mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1765 -ret, get_error_string(ret));
1766 goto fail;
1767 }
1768 }
1769 /* Identity Hint currently not supported in Mbed TLS so code removed */
1770
1771#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1772 if (c_session->cpsk_setup_data.ec_jpake) {
1773 m_env->ec_jpake = 1;
1774 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1775#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1776 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1777#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1778 } else {
1779 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1780 }
1781#else /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1782 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1783#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1784#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1785 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1786#endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1787 } else if ((m_context->psk_pki_enabled & IS_PKI) ||
1788 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1789 /*
1790 * If neither PSK or PKI have been set up, use PKI basics.
1791 * This works providing COAP_PKI_KEY_PEM has a value of 0.
1792 */
1793 coap_dtls_pki_t *setup_data = &m_context->setup_data;
1794
1795 if (!(m_context->psk_pki_enabled & IS_PKI)) {
1796 /* PKI not defined - set up some defaults */
1797 setup_data->verify_peer_cert = 1;
1798 setup_data->check_common_ca = 0;
1799 setup_data->allow_self_signed = 1;
1800 setup_data->allow_expired_certs = 1;
1801 setup_data->cert_chain_validation = 1;
1802 setup_data->cert_chain_verify_depth = 2;
1803 setup_data->check_cert_revocation = 1;
1804 setup_data->allow_no_crl = 1;
1805 setup_data->allow_expired_crl = 1;
1806 setup_data->is_rpk_not_cert = 0;
1807 setup_data->use_cid = 0;
1808 }
1809 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1810 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1811 &m_env->private_key, m_env, m_context,
1812 c_session, setup_data,
1814 if (ret < 0) {
1815 coap_log_err("PKI setup failed\n");
1816 return ret;
1817 }
1818#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1819 if (c_session->proto == COAP_PROTO_TLS ||
1820 c_session->proto == COAP_PROTO_WSS) {
1821 static const char *alpn_list[] = { "coap", NULL };
1822
1823 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1824 if (ret != 0) {
1825 coap_log_err("ALPN setup failed %d)\n", ret);
1826 }
1827 }
1828#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN */
1829 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1830#if defined(MBEDTLS_SSL_PROTO_DTLS)
1831#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1832 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1833#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1834#endif /* MBEDTLS_SSL_PROTO_DTLS */
1835 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1836 }
1837 return 0;
1838
1839fail:
1840 return ret;
1841}
1842#endif /* COAP_CLIENT_SUPPORT */
1843
1844static void
1845mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1846 if (!m_env) {
1847 return;
1848 }
1849
1850 mbedtls_x509_crt_free(&m_env->cacert);
1851 mbedtls_x509_crt_free(&m_env->public_cert);
1852 mbedtls_pk_free(&m_env->private_key);
1853#if !COAP_USE_PSA_CRYPTO
1854 mbedtls_entropy_free(&m_env->entropy);
1855 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1856#endif /* !COAP_USE_PSA_CRYPTO */
1857 mbedtls_ssl_config_free(&m_env->conf);
1858 mbedtls_ssl_free(&m_env->ssl);
1859 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1860}
1861
1862static void
1863coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1864 if (m_env) {
1865 if (!m_env->sent_alert)
1866 mbedtls_ssl_close_notify(&m_env->ssl);
1867 mbedtls_cleanup(m_env);
1868 mbedtls_free(m_env);
1869 }
1870}
1871
1872#if COAP_MAX_LOGGING_LEVEL > 0
1873static const char *
1874report_mbedtls_alert(unsigned char alert) {
1875 switch (alert) {
1876 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1877 return ": Bad Record MAC";
1878 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1879 return ": Handshake failure";
1880 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1881 return ": No Certificate provided";
1882 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1883 return ": Certificate is bad";
1884 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1885 return ": Certificate is unknown";
1886 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1887 return ": CA is unknown";
1888 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1889 return ": Access was denied";
1890 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1891 return ": Decrypt error";
1892 default:
1893 return "";
1894 }
1895}
1896#endif /* COAP_MAX_LOGGING_LEVEL */
1897
1898/*
1899 * return -1 failure
1900 * 0 not completed
1901 * 1 established
1902 */
1903static int
1904do_mbedtls_handshake(coap_session_t *c_session,
1905 coap_mbedtls_env_t *m_env) {
1906 int ret;
1907 int alert;
1908
1909 ret = mbedtls_ssl_handshake(&m_env->ssl);
1910 switch (ret) {
1911 case 0:
1912 m_env->established = 1;
1913 coap_log_debug("* %s: Mbed TLS established\n",
1914 coap_session_str(c_session));
1915 ret = 1;
1916#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1917#if COAP_CLIENT_SUPPORT
1918 if (c_session->type == COAP_SESSION_TYPE_CLIENT &&
1919 c_session->proto == COAP_PROTO_DTLS) {
1920 coap_mbedtls_context_t *m_context;
1921
1922 m_context = (coap_mbedtls_context_t *)c_session->context->dtls_context;
1923 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
1924 m_context->setup_data.use_cid) {
1925 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1926 int enabled;
1927 size_t peer_cid_len;
1928
1929 /* See whether CID was negotiated */
1930 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1931 enabled == MBEDTLS_SSL_CID_ENABLED) {
1932 c_session->negotiated_cid = 1;
1933 } else {
1934 coap_log_info("** %s: CID was not negotiated\n", coap_session_str(c_session));
1935 c_session->negotiated_cid = 0;
1936 }
1937 }
1938 }
1939#endif /* COAP_CLIENT_SUPPORT */
1940#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1942 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
1943 break;
1944 case MBEDTLS_ERR_SSL_WANT_READ:
1945 case MBEDTLS_ERR_SSL_WANT_WRITE:
1946 if (m_env->ssl.state == MBEDTLS_SSL_SERVER_HELLO
1947#if MBEDTLS_VERSION_NUMBER >= 0x03030000
1948 || m_env->ssl.state == MBEDTLS_SSL_NEW_SESSION_TICKET
1949#endif /* MBEDTLS_VERSION_NUMBER >= 0x03030000 */
1950 ) {
1951 if (++m_env->server_hello_cnt > 10) {
1952 /* retried this too many times */
1953 goto fail;
1954 }
1955 }
1956 errno = EAGAIN;
1957 ret = 0;
1958 break;
1959 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1960 coap_log_debug("hello verification requested\n");
1961 goto reset;
1962 case MBEDTLS_ERR_SSL_INVALID_MAC:
1963 goto fail;
1964#ifdef MBEDTLS_2_X_COMPAT
1965 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1966#else /* ! MBEDTLS_2_X_COMPAT */
1967 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1968#endif /* ! MBEDTLS_2_X_COMPAT */
1969 goto fail;
1970 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1971 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1972 goto fail_alert;
1973#ifdef MBEDTLS_2_X_COMPAT
1974 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1975 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1976 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1977 goto fail_alert;
1978#endif /* MBEDTLS_2_X_COMPAT */
1979 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1980 goto fail;
1981 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1982 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1983 coap_log_warn("***%s: Alert '%d'%s\n",
1984 coap_session_str(c_session), m_env->ssl.in_msg[1],
1985 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1986 /* Fall through */
1987 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1988 case MBEDTLS_ERR_SSL_CONN_EOF:
1989 case MBEDTLS_ERR_NET_CONN_RESET:
1991 ret = -1;
1992 break;
1993 default:
1994 coap_log_warn("do_mbedtls_handshake: session establish "
1995 "returned -0x%x: '%s'\n",
1996 -ret, get_error_string(ret));
1997 ret = -1;
1998 break;
1999 }
2000 return ret;
2001
2002fail_alert:
2003 mbedtls_ssl_send_alert_message(&m_env->ssl,
2004 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2005 alert);
2006 m_env->sent_alert = 1;
2007fail:
2008 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
2009 coap_log_warn("do_mbedtls_handshake: session establish "
2010 "returned '%s'\n",
2011 get_error_string(ret));
2012reset:
2013 mbedtls_ssl_session_reset(&m_env->ssl);
2014#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2015 if (m_env->ec_jpake) {
2016 const coap_bin_const_t *psk_key;
2017
2018#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2019 if (c_session->type == COAP_SESSION_TYPE_CLIENT) {
2020 psk_key = coap_get_session_client_psk_key(c_session);
2021 } else {
2022 psk_key = coap_get_session_server_psk_key(c_session);
2023 }
2024#elif COAP_CLIENT_SUPPORT
2025 psk_key = coap_get_session_client_psk_key(c_session);
2026#else /* COAP_SERVER_SUPPORT */
2027 psk_key = coap_get_session_server_psk_key(c_session);
2028#endif /* COAP_SERVER_SUPPORT */
2029 if (psk_key) {
2030 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
2031 }
2032 }
2033#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2034 return -1;
2035}
2036
2037static void
2038mbedtls_debug_out(void *ctx COAP_UNUSED, int level,
2039 const char *file COAP_UNUSED,
2040 int line COAP_UNUSED, const char *str) {
2041
2042 coap_log_t coap_level = COAP_LOG_DEBUG;
2043 /*
2044 * 0 No debug
2045 * 1 Error
2046 * 2 State change
2047 * 3 Informational
2048 * 4 Verbose
2049 */
2050 switch (level) {
2051 case 0:
2052 coap_level = COAP_LOG_EMERG;
2053 break;
2054 case 1:
2055 coap_level = COAP_LOG_WARN;
2056 break;
2057 case 2:
2058 coap_level = COAP_LOG_NOTICE;
2059 break;
2060 case 3:
2061 coap_level = COAP_LOG_INFO;
2062 break;
2063 case 4:
2064 default:
2065 coap_level = COAP_LOG_DEBUG;
2066 break;
2067 }
2068 coap_dtls_log(coap_level, "%s", str);
2069}
2070
2071#if !COAP_DISABLE_TCP
2072/*
2073 * strm
2074 * return +ve data amount
2075 * 0 no more
2076 * -ve Mbed TLS error
2077 */
2078static int
2079coap_sock_read(void *ctx, unsigned char *out, size_t outl) {
2080 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
2081 coap_session_t *c_session = (coap_session_t *)ctx;
2082
2083 if (out != NULL) {
2084 ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_read(c_session, out, outl);
2085 /* Translate layer returns into what MbedTLS expects */
2086 if (ret == -1) {
2087 if (errno == ECONNRESET) {
2088 /* graceful shutdown */
2089 ret = MBEDTLS_ERR_SSL_CONN_EOF;
2090 } else {
2091 ret = MBEDTLS_ERR_NET_RECV_FAILED;
2092 }
2093 } else if (ret == 0) {
2094 errno = EAGAIN;
2095 ret = MBEDTLS_ERR_SSL_WANT_READ;
2096 }
2097 }
2098 return ret;
2099}
2100
2101/*
2102 * strm
2103 * return +ve data amount
2104 * 0 no more
2105 * -ve Mbed TLS error
2106 */
2107static int
2108coap_sock_write(void *context, const unsigned char *in, size_t inl) {
2109 int ret = 0;
2110 coap_session_t *c_session = (coap_session_t *)context;
2111
2112 ret = c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
2113 (const uint8_t *)in,
2114 inl);
2115 /* Translate layer what returns into what MbedTLS expects */
2116 if (ret < 0) {
2117 if ((c_session->state == COAP_SESSION_STATE_CSM ||
2118 c_session->state == COAP_SESSION_STATE_HANDSHAKE) &&
2119 (errno == EPIPE || errno == ECONNRESET)) {
2120 /*
2121 * Need to handle a TCP timing window where an agent continues with
2122 * the sending of the next handshake or a CSM.
2123 * However, the peer does not like a certificate and so sends a
2124 * fatal alert and closes the TCP session.
2125 * The sending of the next handshake or CSM may get terminated because
2126 * of the closed TCP session, but there is still an outstanding alert
2127 * to be read in and reported on.
2128 * In this case, pretend that sending the info was fine so that the
2129 * alert can be read (which effectively is what happens with DTLS).
2130 */
2131 ret = inl;
2132 } else {
2133#ifdef _WIN32
2134 int lasterror = WSAGetLastError();
2135
2136 if (lasterror == WSAEWOULDBLOCK) {
2137 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2138 } else if (lasterror == WSAECONNRESET) {
2139 ret = MBEDTLS_ERR_NET_CONN_RESET;
2140 }
2141#else
2142 if (errno == EAGAIN || errno == EINTR) {
2143 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2144 } else if (errno == EPIPE || errno == ECONNRESET) {
2145 ret = MBEDTLS_ERR_NET_CONN_RESET;
2146 }
2147#endif
2148 else {
2149 ret = MBEDTLS_ERR_NET_SEND_FAILED;
2150 }
2151 coap_log_debug("* %s: failed to send %" PRIdS " bytes (%s) state %d\n",
2152 coap_session_str(c_session), inl, coap_socket_strerror(),
2153 c_session->state);
2154 }
2155 }
2156 if (ret == 0) {
2157 errno = EAGAIN;
2158 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2159 }
2160 return ret;
2161}
2162#endif /* !COAP_DISABLE_TCP */
2163
2164static coap_mbedtls_env_t *
2165coap_dtls_new_mbedtls_env(coap_session_t *c_session,
2166 coap_dtls_role_t role,
2167 coap_proto_t proto) {
2168#if !COAP_USE_PSA_CRYPTO
2169 int ret = 0;
2170#endif /* !COAP_USE_PSA_CRYPTO */
2171 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2172
2173 if (m_env)
2174 return m_env;
2175
2176 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(sizeof(coap_mbedtls_env_t));
2177 if (!m_env) {
2178 return NULL;
2179 }
2180 memset(m_env, 0, sizeof(coap_mbedtls_env_t));
2181
2182 mbedtls_ssl_init(&m_env->ssl);
2183#if !COAP_USE_PSA_CRYPTO
2184 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
2185 mbedtls_entropy_init(&m_env->entropy);
2186#endif /* !COAP_USE_PSA_CRYPTO */
2187 mbedtls_ssl_config_init(&m_env->conf);
2188 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
2189
2190#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
2191 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
2192#endif /* ESPIDF_VERSION && CONFIG_MBEDTLS_DEBUG */
2193
2194#if !COAP_USE_PSA_CRYPTO
2195 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
2196 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
2197 if (ret != MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) {
2198 coap_log_info("mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
2199 -ret, get_error_string(ret));
2200 goto fail;
2201 }
2202 coap_log_err("mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
2203 -ret, get_error_string(ret));
2204 }
2205#endif /* !COAP_USE_PSA_CRYPTO */
2206
2207 if (role == COAP_DTLS_ROLE_CLIENT) {
2208#if COAP_CLIENT_SUPPORT
2209 if (setup_client_ssl_session(c_session, m_env) != 0) {
2210 goto fail;
2211 }
2212#else /* !COAP_CLIENT_SUPPORT */
2213 goto fail;
2214#endif /* !COAP_CLIENT_SUPPORT */
2215 } else if (role == COAP_DTLS_ROLE_SERVER) {
2216#if defined(MBEDTLS_SSL_SRV_C)
2217 if (setup_server_ssl_session(c_session, m_env) != 0) {
2218 goto fail;
2219 }
2220#else /* ! MBEDTLS_SSL_SRV_C */
2221 goto fail;
2222#endif /* ! MBEDTLS_SSL_SRV_C */
2223 } else {
2224 goto fail;
2225 }
2226
2227#if MBEDTLS_VERSION_NUMBER >= 0x03020000
2228 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
2229#else
2230 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2231 MBEDTLS_SSL_MINOR_VERSION_3);
2232#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
2233
2234 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
2235 goto fail;
2236 }
2237 if (proto == COAP_PROTO_DTLS) {
2238 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
2239 coap_dgram_read, NULL);
2240#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2241 if (COAP_PROTO_NOT_RELIABLE(c_session->proto)) {
2242 if (role == COAP_DTLS_ROLE_CLIENT) {
2243#if COAP_CLIENT_SUPPORT
2244 coap_mbedtls_context_t *m_context =
2245 (coap_mbedtls_context_t *)c_session->context->dtls_context;
2246
2247 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
2248 m_context->setup_data.use_cid) {
2249 /*
2250 * Enable passive DTLS CID support.
2251 *
2252 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
2253 * to use RFC9146 extension ID of 54, rather than the draft version -05
2254 * value of 254.
2255 */
2256 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
2257 }
2258#endif /* COAP_CLIENT_SUPPORT */
2259 } else {
2260#if COAP_SERVER_SUPPORT
2261 uint8_t cid[COAP_DTLS_CID_LENGTH];
2262 /*
2263 * Enable server DTLS CID support.
2264 *
2265 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
2266 * to use RFC9146 extension ID of 54, rather than the draft version -05
2267 * value of 254.
2268 */
2269 coap_prng_lkd(cid, sizeof(cid));
2270 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
2271 sizeof(cid));
2272 c_session->client_cid = coap_new_bin_const(cid, sizeof(cid));
2273#endif /* COAP_SERVER_SUPPORT */
2274 }
2275 }
2276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2277 }
2278#if !COAP_DISABLE_TCP
2279 else {
2280 assert(proto == COAP_PROTO_TLS);
2281 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
2282 coap_sock_read, NULL);
2283 }
2284#endif /* ! COAP_DISABLE_TCP */
2285#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2286 coap_mbedtls_context_t *m_context =
2287 ((coap_mbedtls_context_t *)c_session->context->dtls_context);
2288 if ((m_context->psk_pki_enabled & IS_PSK) &&
2289 m_env->ec_jpake) {
2290 const coap_bin_const_t *psk_key;
2291
2292#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2293 if (role == COAP_DTLS_ROLE_CLIENT) {
2294 psk_key = coap_get_session_client_psk_key(c_session);
2295 } else {
2296 psk_key = coap_get_session_server_psk_key(c_session);
2297 }
2298#elif COAP_CLIENT_SUPPORT
2299 psk_key = coap_get_session_client_psk_key(c_session);
2300#else /* COAP_SERVER_SUPPORT */
2301 psk_key = coap_get_session_server_psk_key(c_session);
2302#endif /* COAP_SERVER_SUPPORT */
2303 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
2304 }
2305#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2306#ifdef __ZEPHYR__
2307 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
2308 zephyr_timing_set_delay,
2309 zephyr_timing_get_delay);
2310#else
2311 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
2312 mbedtls_timing_set_delay,
2313 mbedtls_timing_get_delay);
2314#endif
2315
2316 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
2317 return m_env;
2318
2319fail:
2320 if (m_env) {
2321 mbedtls_cleanup(m_env);
2322 mbedtls_free(m_env);
2323 }
2324 return NULL;
2325}
2326
2327int
2329#if defined(MBEDTLS_SSL_PROTO_DTLS)
2330 return 1;
2331#else /* !MBEDTLS_SSL_PROTO_DTLS */
2332 static int reported = 0;
2333 if (!reported) {
2334 reported = 1;
2335 coap_log_emerg("libcoap not compiled for DTLS with Mbed TLS"
2336 " - update Mbed TLS to include DTLS\n");
2337 }
2338 return 0;
2339#endif /* !MBEDTLS_SSL_PROTO_DTLS */
2340}
2341
2342int
2344#if !COAP_DISABLE_TCP
2345 return 1;
2346#else /* COAP_DISABLE_TCP */
2347 return 0;
2348#endif /* COAP_DISABLE_TCP */
2349}
2350
2351/*
2352 * return 0 failed
2353 * 1 passed
2354 */
2355int
2357 return 1;
2358}
2359
2360/*
2361 * return 0 failed
2362 * 1 passed
2363 */
2364int
2366 return 1;
2367}
2368
2369/*
2370 * return 0 failed
2371 * 1 passed
2372 */
2373int
2375 return 0;
2376}
2377
2378/*
2379 * return 0 failed
2380 * 1 passed
2381 */
2382int
2384 return 0;
2385}
2386
2387/*
2388 * return 0 failed
2389 * 1 passed
2390 */
2391int
2393#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2394 return 1;
2395#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2396 return 0;
2397#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2398}
2399
2400#if COAP_CLIENT_SUPPORT
2401int
2402coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
2403#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2404 c_context->testing_cids = every;
2405 return 1;
2406#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2407 (void)c_context;
2408 (void)every;
2409 return 0;
2410#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2411}
2412#endif /* COAP_CLIENT_SUPPORT */
2413
2414void *
2416 coap_mbedtls_context_t *m_context;
2417 (void)c_context;
2418
2419 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(sizeof(coap_mbedtls_context_t));
2420 if (m_context) {
2421 memset(m_context, 0, sizeof(coap_mbedtls_context_t));
2422 }
2423 return m_context;
2424}
2425
2426#if COAP_SERVER_SUPPORT
2427/*
2428 * return 0 failed
2429 * 1 passed
2430 */
2431int
2432coap_dtls_context_set_spsk(coap_context_t *c_context,
2433 coap_dtls_spsk_t *setup_data
2434 ) {
2435 coap_mbedtls_context_t *m_context =
2436 ((coap_mbedtls_context_t *)c_context->dtls_context);
2437
2438#if !defined(MBEDTLS_SSL_SRV_C)
2439 coap_log_emerg("coap_context_set_spsk:"
2440 " libcoap not compiled for Server Mode for Mbed TLS"
2441 " - update Mbed TLS to include Server Mode\n");
2442 return 0;
2443#endif /* !MBEDTLS_SSL_SRV_C */
2444 if (!m_context || !setup_data)
2445 return 0;
2446
2447 if (setup_data->ec_jpake) {
2448#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2449 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2450#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2451 }
2452 if (setup_data->psk_info.hint.length) {
2453 coap_log_warn("CoAP Server with Mbed TLS does not support Identity Hint\n");
2454 }
2455 m_context->psk_pki_enabled |= IS_PSK;
2456 return 1;
2457}
2458#endif /* COAP_SERVER_SUPPORT */
2459
2460#if COAP_CLIENT_SUPPORT
2461/*
2462 * return 0 failed
2463 * 1 passed
2464 */
2465int
2466coap_dtls_context_set_cpsk(coap_context_t *c_context,
2467 coap_dtls_cpsk_t *setup_data
2468 ) {
2469#if !defined(MBEDTLS_SSL_CLI_C)
2470 (void)c_context;
2471 (void)setup_data;
2472
2473 coap_log_emerg("coap_context_set_cpsk:"
2474 " libcoap not compiled for Client Mode for Mbed TLS"
2475 " - update Mbed TLS to include Client Mode\n");
2476 return 0;
2477#else /* MBEDTLS_SSL_CLI_C */
2478 coap_mbedtls_context_t *m_context =
2479 ((coap_mbedtls_context_t *)c_context->dtls_context);
2480
2481 if (!m_context || !setup_data)
2482 return 0;
2483
2484 if (setup_data->validate_ih_call_back) {
2485 coap_log_warn("CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2486 }
2487 if (setup_data->ec_jpake) {
2488#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2489 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2490#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2491 }
2492 if (setup_data->use_cid) {
2493#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2494 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2495#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2496 }
2497 m_context->psk_pki_enabled |= IS_PSK;
2498 return 1;
2499#endif /* MBEDTLS_SSL_CLI_C */
2500}
2501#endif /* COAP_CLIENT_SUPPORT */
2502
2503int
2505 const coap_dtls_pki_t *setup_data,
2506 const coap_dtls_role_t role COAP_UNUSED) {
2507 coap_mbedtls_context_t *m_context =
2508 ((coap_mbedtls_context_t *)c_context->dtls_context);
2509
2510 m_context->setup_data = *setup_data;
2511 if (!m_context->setup_data.verify_peer_cert) {
2512 /* Needs to be clear so that no CA DNs are transmitted */
2513 m_context->setup_data.check_common_ca = 0;
2514 /* Allow all of these but warn if issue */
2515 m_context->setup_data.allow_self_signed = 1;
2516 m_context->setup_data.allow_expired_certs = 1;
2517 m_context->setup_data.cert_chain_validation = 1;
2518 m_context->setup_data.cert_chain_verify_depth = 10;
2519 m_context->setup_data.check_cert_revocation = 1;
2520 m_context->setup_data.allow_no_crl = 1;
2521 m_context->setup_data.allow_expired_crl = 1;
2522 m_context->setup_data.allow_bad_md_hash = 1;
2523 m_context->setup_data.allow_short_rsa_length = 1;
2524 }
2525 m_context->psk_pki_enabled |= IS_PKI;
2526 if (setup_data->use_cid) {
2527#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2528 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2529#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2530 }
2531 return 1;
2532}
2533
2534int
2536 const char *ca_file,
2537 const char *ca_path) {
2538 coap_mbedtls_context_t *m_context =
2539 ((coap_mbedtls_context_t *)c_context->dtls_context);
2540
2541 if (!m_context) {
2542 coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
2543 "not set up\n");
2544 return 0;
2545 }
2546
2547 if (ca_file == NULL && ca_path == NULL) {
2548 coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path "
2549 "not defined\n");
2550 return 0;
2551 }
2552 if (m_context->root_ca_file) {
2553 mbedtls_free(m_context->root_ca_file);
2554 m_context->root_ca_file = NULL;
2555 }
2556
2557 if (ca_file) {
2558 m_context->root_ca_file = mbedtls_strdup(ca_file);
2559 }
2560
2561 if (m_context->root_ca_path) {
2562 mbedtls_free(m_context->root_ca_path);
2563 m_context->root_ca_path = NULL;
2564 }
2565
2566 if (ca_path) {
2567 m_context->root_ca_path = mbedtls_strdup(ca_path);
2568 }
2569 return 1;
2570}
2571
2572/*
2573 * return 0 failed
2574 * 1 passed
2575 */
2576int
2578 coap_mbedtls_context_t *m_context =
2579 ((coap_mbedtls_context_t *)c_context->dtls_context);
2580
2581 if (!m_context) {
2582 coap_log_warn("coap_context_load_pki_trust_store: (D)TLS environment "
2583 "not set up\n");
2584 return 0;
2585 }
2586 m_context->trust_store_defined = 1;
2587
2588 /* No proper support for this in MbedTLS at this point */
2589 return 1;
2590}
2591
2592
2593int
2595 coap_mbedtls_context_t *m_context =
2596 ((coap_mbedtls_context_t *)c_context->dtls_context);
2597 return m_context->psk_pki_enabled ? 1 : 0;
2598}
2599
2600void
2601coap_dtls_free_context(void *dtls_context) {
2602 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2603 unsigned int i;
2604
2605 for (i = 0; i < m_context->pki_sni_count; i++) {
2606 mbedtls_free(m_context->pki_sni_entry_list[i]->sni);
2607
2608 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i]->public_cert);
2609
2610 mbedtls_pk_free(&m_context->pki_sni_entry_list[i]->private_key);
2611
2612 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i]->cacert);
2613
2614 mbedtls_free(m_context->pki_sni_entry_list[i]);
2615 }
2616 if (m_context->pki_sni_entry_list)
2617 mbedtls_free(m_context->pki_sni_entry_list);
2618
2619 for (i = 0; i < m_context->psk_sni_count; i++) {
2620 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2621 }
2622 if (m_context->psk_sni_entry_list)
2623 mbedtls_free(m_context->psk_sni_entry_list);
2624
2625 if (m_context->root_ca_path)
2626 mbedtls_free(m_context->root_ca_path);
2627 if (m_context->root_ca_file)
2628 mbedtls_free(m_context->root_ca_file);
2629
2630 mbedtls_free(m_context);
2631}
2632
2633#if COAP_CLIENT_SUPPORT
2634void *
2635coap_dtls_new_client_session(coap_session_t *c_session) {
2636#if !defined(MBEDTLS_SSL_CLI_C)
2637 (void)c_session;
2638 coap_log_emerg("coap_dtls_new_client_session:"
2639 " libcoap not compiled for Client Mode for Mbed TLS"
2640 " - update Mbed TLS to include Client Mode\n");
2641 return NULL;
2642#else /* MBEDTLS_SSL_CLI_C */
2643 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2646 int ret;
2647
2648 if (m_env) {
2649 coap_tick_t now;
2650
2651 coap_ticks(&now);
2652 m_env->last_timeout = now;
2653 ret = do_mbedtls_handshake(c_session, m_env);
2654 if (ret == -1) {
2655 coap_dtls_free_mbedtls_env(m_env);
2656 return NULL;
2657 }
2658 }
2659 return m_env;
2660#endif /* MBEDTLS_SSL_CLI_C */
2661}
2662#endif /* COAP_CLIENT_SUPPORT */
2663
2664#if COAP_SERVER_SUPPORT
2665void *
2666coap_dtls_new_server_session(coap_session_t *c_session) {
2667#if !defined(MBEDTLS_SSL_SRV_C)
2668 (void)c_session;
2669 coap_log_emerg("coap_dtls_new_server_session:"
2670 " libcoap not compiled for Server Mode for Mbed TLS"
2671 " - update Mbed TLS to include Server Mode\n");
2672 return NULL;
2673#else /* MBEDTLS_SSL_SRV_C */
2674 coap_mbedtls_env_t *m_env =
2675 (coap_mbedtls_env_t *)c_session->tls;
2676 if (m_env) {
2677#if defined(MBEDTLS_SSL_PROTO_DTLS)
2678#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2679 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2680#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2681#endif /* MBEDTLS_SSL_PROTO_DTLS */
2682 }
2683 return m_env;
2684#endif /* MBEDTLS_SSL_SRV_C */
2685}
2686#endif /* COAP_SERVER_SUPPORT */
2687
2688void
2690 if (c_session && c_session->context && c_session->tls) {
2691 coap_dtls_free_mbedtls_env(c_session->tls);
2692 c_session->tls = NULL;
2694 }
2695 return;
2696}
2697
2698void
2700#if defined(MBEDTLS_SSL_PROTO_DTLS)
2701 coap_mbedtls_env_t *m_env =
2702 (coap_mbedtls_env_t *)c_session->tls;
2703 if (m_env) {
2704#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2705 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2706#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2707 }
2708#else /* ! MBEDTLS_SSL_PROTO_DTLS */
2709 (void)c_session;
2710#endif /* MBEDTLS_SSL_PROTO_DTLS */
2711}
2712
2713ssize_t
2715 const uint8_t *data, size_t data_len) {
2716 int ret;
2717 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2718
2719 if (!m_env) {
2720 errno = ENOTCONN;
2721 return -1;
2722 }
2723 c_session->dtls_event = -1;
2724 coap_log_debug("* %s: dtls: sent %4d bytes\n",
2725 coap_session_str(c_session), (int)data_len);
2726 if (m_env->established) {
2727 ret = mbedtls_ssl_write(&m_env->ssl, (const unsigned char *) data, data_len);
2728 if (ret <= 0) {
2729 switch (ret) {
2730 case MBEDTLS_ERR_SSL_WANT_READ:
2731 case MBEDTLS_ERR_SSL_WANT_WRITE:
2732 ret = 0;
2733 break;
2734 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2736 ret = -1;
2737 break;
2738 default:
2739 coap_log_warn("coap_dtls_send: "
2740 "returned -0x%x: '%s'\n",
2741 -ret, get_error_string(ret));
2742 ret = -1;
2743 break;
2744 }
2745 if (ret == -1) {
2746 coap_log_warn("coap_dtls_send: cannot send PDU\n");
2747 }
2748 }
2749 } else {
2750 ret = do_mbedtls_handshake(c_session, m_env);
2751 if (ret == 1) {
2752 /* Just connected, so send the data */
2753 return coap_dtls_send(c_session, data, data_len);
2754 }
2755 ret = -1;
2756 }
2757
2758 if (c_session->dtls_event >= 0) {
2759 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2760 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2761 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2762 ret = -1;
2763 }
2764 }
2765 return ret;
2766}
2767
2768int
2770 return 0;
2771}
2772
2774coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
2775 return 0;
2776}
2777
2780 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2781#ifdef __ZEPHYR__
2782 int ret = zephyr_timing_get_delay(&m_env->timer);
2783#else
2784 int ret = mbedtls_timing_get_delay(&m_env->timer);
2785#endif
2786 unsigned int scalar = 1 << m_env->retry_scalar;
2787
2788 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2789 switch (ret) {
2790 case 0:
2791 /* int_ms has not timed out */
2792 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2793 /* Need to indicate remaining timeout time */
2794 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2795 }
2796 m_env->last_timeout = now;
2797 /* This may cause a minor extra delay */
2798 return now + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2799 case 1:
2800 /* int_ms has timed out, but not fin_ms */
2801 /*
2802 * Need to make sure that we do not do this too frequently
2803 */
2804 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2805 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2806 }
2807
2808 /* Reset for the next time */
2809 m_env->last_timeout = now;
2810 return now;
2811 case 2:
2812 /* fin_ms has timed out - timed out - one final try */
2813 return now;
2814 default:
2815 break;
2816 }
2817
2818 return 0;
2819}
2820
2821/*
2822 * return 1 timed out
2823 * 0 still timing out
2824 */
2825int
2827 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2828
2829 if (m_env == NULL) {
2830 errno = ENOTCONN;
2831 return 1;
2832 }
2833 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2834 m_env->retry_scalar++;
2835 if ((++c_session->dtls_timeout_count > c_session->max_retransmit) ||
2836 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2837 /* Too many retries */
2839 return 1;
2840 }
2841 return 0;
2842}
2843
2844static void
2845coap_dtls_receive_done(coap_ssl_t *ssl_data, int ret) {
2846 if (ssl_data->pdu_len) {
2847 /* pdu data is held on stack which will not stay there */
2848 coap_log_debug("coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2849 ssl_data->pdu_len = 0;
2850 ssl_data->pdu = NULL;
2851 }
2852}
2853
2854/*
2855 * return +ve data amount
2856 * 0 no more
2857 * -1 error
2858 */
2859int
2861 const uint8_t *data,
2862 size_t data_len) {
2863 int ret = 1;
2864
2865 c_session->dtls_event = -1;
2866 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2867 coap_ssl_t *ssl_data;
2868
2869 if (m_env == NULL) {
2870 errno = ENOTCONN;
2871 return -1;
2872 }
2873
2874 ssl_data = &m_env->coap_ssl_data;
2875 if (ssl_data->pdu_len) {
2876 coap_log_err("** %s: Previous data not read %u bytes\n",
2877 coap_session_str(c_session), ssl_data->pdu_len);
2878 }
2879 ssl_data->pdu = data;
2880 ssl_data->pdu_len = (unsigned)data_len;
2881
2882 if (m_env->established) {
2883#if COAP_CONSTRAINED_STACK
2884 /* pdu can be protected by global_lock if needed */
2885 static uint8_t pdu[COAP_RXBUFFER_SIZE];
2886#else /* ! COAP_CONSTRAINED_STACK */
2887 uint8_t pdu[COAP_RXBUFFER_SIZE];
2888#endif /* ! COAP_CONSTRAINED_STACK */
2889
2890 ret = mbedtls_ssl_read(&m_env->ssl, pdu, sizeof(pdu));
2891 if (ret > 0) {
2892 coap_log_debug("* %s: dtls: recv %4d bytes\n",
2893 coap_session_str(c_session), ret);
2894 coap_dtls_receive_done(ssl_data, ret);
2895 /* May close c_session->tls, so ssl_data is no longer used */
2896 return coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret);
2897 }
2898 switch (ret) {
2899 case 0:
2900 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2901 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2903 break;
2904 case MBEDTLS_ERR_SSL_WANT_READ:
2905 break;
2906 default:
2907 coap_log_warn("coap_dtls_receive: "
2908 "returned -0x%x: '%s' (length %" PRIdS ")\n",
2909 -ret, get_error_string(ret), data_len);
2910 break;
2911 }
2912 ret = -1;
2913 } else {
2914 ret = do_mbedtls_handshake(c_session, m_env);
2915 if (ret == 1) {
2916 /* Just connected, so send the data */
2917 coap_session_connected(c_session);
2918 } else {
2919 if (ssl_data->pdu_len) {
2920 /* Do the handshake again in case of internal timeout */
2921 ret = do_mbedtls_handshake(c_session, m_env);
2922 if (ret == 1) {
2923 /* Just connected, so send the data */
2924 coap_session_connected(c_session);
2925 }
2926 }
2927 ret = -1;
2928 }
2929 }
2930 if (c_session->dtls_event >= 0) {
2931 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2932 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2933 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2935 ssl_data = NULL;
2936 ret = -1;
2937 }
2938 }
2939 if (ssl_data)
2940 coap_dtls_receive_done(ssl_data, ret);
2941 return ret;
2942}
2943
2944#if COAP_SERVER_SUPPORT
2945/*
2946 * return -1 failure
2947 * 0 not completed
2948 * 1 client hello seen
2949 */
2950int
2951coap_dtls_hello(coap_session_t *c_session,
2952 const uint8_t *data,
2953 size_t data_len) {
2954#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2955 (void)c_session;
2956 (void)data;
2957 (void)data_len;
2958 coap_log_emerg("coap_dtls_hello:"
2959 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2960 " - update Mbed TLS to include DTLS and Server Mode\n");
2961 return -1;
2962#else /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
2963 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2964 coap_ssl_t *ssl_data;
2965 int ret;
2966
2967 if (!m_env) {
2968 m_env = coap_dtls_new_mbedtls_env(c_session, COAP_DTLS_ROLE_SERVER,
2970 if (m_env) {
2971 c_session->tls = m_env;
2972 } else {
2973 /* error should have already been reported */
2974 return -1;
2975 }
2976 }
2977
2978 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2979 (unsigned char *)&c_session->addr_info.remote,
2980 sizeof(c_session->addr_info.remote))) != 0) {
2981 coap_log_err("mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2982 -ret, get_error_string(ret));
2983 return -1;
2984 }
2985
2986 ssl_data = &m_env->coap_ssl_data;
2987 if (ssl_data->pdu_len) {
2988 coap_log_err("** %s: Previous data not read %u bytes\n",
2989 coap_session_str(c_session), ssl_data->pdu_len);
2990 }
2991 ssl_data->pdu = data;
2992 ssl_data->pdu_len = (unsigned)data_len;
2993
2994 ret = do_mbedtls_handshake(c_session, m_env);
2995 if (ret == 0 || m_env->seen_client_hello) {
2996 /* The test for seen_client_hello gives the ability to setup a new
2997 c_session to continue the do_mbedtls_handshake past the client hello
2998 and safely allow updating of the m_env and separately
2999 letting a new session cleanly start up.
3000 */
3001 m_env->seen_client_hello = 0;
3002 ret = 1;
3003 } else {
3004 ret = 0;
3005 }
3006
3007 if (ssl_data->pdu_len) {
3008 /* pdu data is held on stack which will not stay there */
3009 coap_log_debug("coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
3010 ssl_data->pdu_len = 0;
3011 ssl_data->pdu = NULL;
3012 }
3013 return ret;
3014#endif /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
3015}
3016#endif /* COAP_SERVER_SUPPORT */
3017
3018unsigned int
3020 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3021 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
3022
3023 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
3024 return 13 + 8 + 8;
3025 }
3026 return expansion;
3027}
3028
3029#if !COAP_DISABLE_TCP
3030#if COAP_CLIENT_SUPPORT
3031void *
3032coap_tls_new_client_session(coap_session_t *c_session) {
3033#if !defined(MBEDTLS_SSL_CLI_C)
3034 (void)c_session;
3035 *connected = 0;
3036 coap_log_emerg("coap_tls_new_client_session:"
3037 " libcoap not compiled for Client Mode for Mbed TLS"
3038 " - update Mbed TLS to include Client Mode\n");
3039 return NULL;
3040#else /* MBEDTLS_SSL_CLI_C */
3041 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
3044 coap_tick_t now;
3045 coap_ticks(&now);
3046
3047 if (!m_env)
3048 return NULL;
3049
3050 m_env->last_timeout = now;
3051 c_session->tls = m_env;
3052 do_mbedtls_handshake(c_session, m_env);
3053 return m_env;
3054#endif /* MBEDTLS_SSL_CLI_C */
3055}
3056#endif /* COAP_CLIENT_SUPPORT */
3057
3058#if COAP_SERVER_SUPPORT
3059void *
3060coap_tls_new_server_session(coap_session_t *c_session) {
3061#if !defined(MBEDTLS_SSL_SRV_C)
3062 (void)c_session;
3063 (void)connected;
3064
3065 coap_log_emerg("coap_tls_new_server_session:"
3066 " libcoap not compiled for Server Mode for Mbed TLS"
3067 " - update Mbed TLS to include Server Mode\n");
3068 return NULL;
3069#else /* MBEDTLS_SSL_SRV_C */
3070 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
3073
3074 if (!m_env)
3075 return NULL;
3076
3077 c_session->tls = m_env;
3078 do_mbedtls_handshake(c_session, m_env);
3079 return m_env;
3080#endif /* MBEDTLS_SSL_SRV_C */
3081}
3082#endif /* COAP_SERVER_SUPPORT */
3083
3084void
3086 coap_dtls_free_session(c_session);
3087 return;
3088}
3089
3090/*
3091 * strm
3092 * return +ve Number of bytes written.
3093 * -1 Error (error in errno).
3094 */
3095ssize_t
3096coap_tls_write(coap_session_t *c_session, const uint8_t *data,
3097 size_t data_len) {
3098 int ret = 0;
3099 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3100 size_t amount_sent = 0;
3101
3102 if (!m_env) {
3103 errno = ENOTCONN;
3104 return -1;
3105 }
3106 c_session->dtls_event = -1;
3107 if (m_env->established) {
3108 while (amount_sent < data_len) {
3109 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
3110 data_len - amount_sent);
3111 if (ret <= 0) {
3112 switch (ret) {
3113 case MBEDTLS_ERR_SSL_WANT_READ:
3114 case MBEDTLS_ERR_SSL_WANT_WRITE:
3115 if (amount_sent)
3116 ret = amount_sent;
3117 else
3118 ret = 0;
3119 c_session->sock.flags |= COAP_SOCKET_WANT_WRITE;
3120 break;
3121 case MBEDTLS_ERR_NET_CONN_RESET:
3122 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
3124 break;
3125 default:
3126 coap_log_warn("coap_tls_write: "
3127 "returned -0x%x: '%s'\n",
3128 -ret, get_error_string(ret));
3129 ret = -1;
3130 break;
3131 }
3132 if (ret == -1) {
3133 coap_log_warn("coap_tls_write: cannot send PDU\n");
3134 }
3135 break;
3136 }
3137 amount_sent += ret;
3138 /* Report all TLS records written, not just the final record. */
3139 ret = (int)amount_sent;
3140 }
3141 } else {
3142 ret = do_mbedtls_handshake(c_session, m_env);
3143 if (ret != 1) {
3144 ret = -1;
3145 }
3146 }
3147
3148 if (c_session->dtls_event >= 0) {
3149 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3150 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3151 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3152 ret = -1;
3153 }
3154 }
3155 if (ret > 0) {
3156 if (ret == (ssize_t)data_len)
3157 coap_log_debug("* %s: tls: sent %4d bytes\n",
3158 coap_session_str(c_session), ret);
3159 else
3160 coap_log_debug("* %s: tls: sent %4d of %4" PRIdS " bytes\n",
3161 coap_session_str(c_session), ret, data_len);
3162 }
3163 return ret;
3164}
3165
3166/*
3167 * strm
3168 * return >=0 Number of bytes read.
3169 * -1 Error (error in errno).
3170 */
3171ssize_t
3172coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) {
3173 int ret = -1;
3174
3175 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3176
3177 if (!m_env) {
3178 errno = ENOTCONN;
3179 return -1;
3180 }
3181
3182 c_session->dtls_event = -1;
3183
3184 if (!m_env->established && !m_env->sent_alert) {
3185 ret = do_mbedtls_handshake(c_session, m_env);
3186 }
3187
3188 if (c_session->state != COAP_SESSION_STATE_NONE && m_env->established) {
3189 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
3190 if (ret <= 0) {
3191 switch (ret) {
3192 case 0:
3193 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3195 ret = -1;
3196 break;
3197 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
3198 /* Stop the sending of an alert on closedown */
3199 m_env->sent_alert = 1;
3201 break;
3202#if MBEDTLS_VERSION_NUMBER >= 0x03060000
3203 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
3204#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
3205 case MBEDTLS_ERR_SSL_WANT_READ:
3206 errno = EAGAIN;
3207 ret = 0;
3208 break;
3209 default:
3210 coap_log_warn("coap_tls_read: "
3211 "returned -0x%x: '%s' (length %" PRIdS ")\n",
3212 -ret, get_error_string(ret), data_len);
3213 ret = -1;
3214 break;
3215 }
3216 } else if (ret < (int)data_len) {
3217 c_session->sock.flags &= ~COAP_SOCKET_CAN_READ;
3218 }
3219 }
3220
3221 if (c_session->dtls_event >= 0) {
3222 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3223 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3224 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3226 ret = -1;
3227 }
3228 }
3229 if (ret > 0) {
3230 coap_log_debug("* %s: tls: recv %4d bytes\n",
3231 coap_session_str(c_session), ret);
3232 }
3233 return ret;
3234}
3235#endif /* !COAP_DISABLE_TCP */
3236
3237void
3238coap_dtls_startup(void) {
3239#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
3240 psa_crypto_init();
3241#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
3242}
3243
3244void
3245coap_dtls_shutdown(void) {
3246#if COAP_CLIENT_SUPPORT
3247 mbedtls_free(psk_ciphers);
3248 mbedtls_free(pki_ciphers);
3249 mbedtls_free(ecjpake_ciphers);
3250 psk_ciphers = NULL;
3251 pki_ciphers = NULL;
3252 ecjpake_ciphers = NULL;
3253 processed_ciphers = 0;
3254#endif /* COAP_CLIENT_SUPPORT */
3256#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
3257 mbedtls_psa_crypto_free();
3258#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
3259}
3260
3261
3262void *
3263coap_dtls_get_tls(const coap_session_t *c_session,
3264 coap_tls_library_t *tls_lib) {
3265 if (tls_lib)
3266 *tls_lib = COAP_TLS_LIBRARY_MBEDTLS;
3267 if (c_session && c_session->tls) {
3268 coap_mbedtls_env_t *m_env;
3269
3270 /* To get around const issue */
3271 memcpy(&m_env, &c_session->tls, sizeof(m_env));
3272
3273 return (void *)&m_env->ssl;
3274 }
3275 return NULL;
3276}
3277
3278static coap_log_t keep_log_level = COAP_LOG_EMERG;
3279
3280void
3282#if !defined(ESPIDF_VERSION)
3283 int use_level;
3284 /*
3285 * Mbed TLS debug levels filter
3286 * 0 No debug
3287 * 1 Error
3288 * 2 State change
3289 * 3 Informational
3290 * 4 Verbose
3291 */
3292 switch ((int)level) {
3293 case COAP_LOG_EMERG:
3294 use_level = 0;
3295 break;
3296 case COAP_LOG_ALERT:
3297 case COAP_LOG_CRIT:
3298 case COAP_LOG_ERR:
3299 case COAP_LOG_WARN:
3300 use_level = 1;
3301 break;
3302 case COAP_LOG_NOTICE:
3303 use_level = 2;
3304 break;
3305 case COAP_LOG_INFO:
3306 use_level = 3;
3307 break;
3308 case COAP_LOG_DEBUG:
3309 default:
3310 use_level = 4;
3311 break;
3312 }
3313 mbedtls_debug_set_threshold(use_level);
3314#endif /* !ESPIDF_VERSION) */
3315 keep_log_level = level;
3316}
3317
3320 return keep_log_level;
3321}
3322
3323#endif /* COAP_WITH_LIBMBEDTLS */
3324
3325#if COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE
3326
3327#if COAP_SERVER_SUPPORT
3328coap_digest_ctx_t *
3329coap_digest_setup(void) {
3330 coap_crypto_sha256_ctx_t *digest_ctx = mbedtls_malloc(sizeof(coap_crypto_sha256_ctx_t));
3331
3332 if (digest_ctx) {
3333 if (coap_crypto_sha256_init(digest_ctx) != 0) {
3334 coap_digest_free(digest_ctx);
3335 return NULL;
3336 }
3337 }
3338 return digest_ctx;
3339}
3340
3341void
3342coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3343 if (digest_ctx) {
3344 coap_crypto_sha256_free(digest_ctx);
3345 mbedtls_free(digest_ctx);
3346 }
3347}
3348
3349int
3350coap_digest_update(coap_digest_ctx_t *digest_ctx,
3351 const uint8_t *data,
3352 size_t data_len) {
3353 return coap_crypto_sha256_update(digest_ctx, data, data_len) == 0;
3354}
3355
3356int
3357coap_digest_final(coap_digest_ctx_t *digest_ctx,
3358 coap_digest_t *digest_buffer) {
3359 int ret = coap_crypto_sha256_finish(digest_ctx, (uint8_t *)digest_buffer) == 0;
3360 coap_digest_free(digest_ctx);
3361 return ret;
3362}
3363#endif /* COAP_SERVER_SUPPORT */
3364
3365#if COAP_WS_SUPPORT
3366int
3368 const coap_bin_const_t *data,
3369 coap_bin_const_t **hash) {
3370 coap_crypto_md_type_t md_type;
3371 size_t hash_len;
3372
3373 switch ((int)alg) {
3375 md_type = COAP_CRYPTO_MD_SHA1;
3376 break;
3378 md_type = COAP_CRYPTO_MD_SHA256;
3379 break;
3381 md_type = COAP_CRYPTO_MD_SHA512;
3382 break;
3383 default:
3384 coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg);
3385 return 0;
3386 }
3387
3388 hash_len = coap_crypto_hash_size(md_type);
3389 if (hash_len == 0)
3390 return 0;
3391
3392 coap_binary_t *dummy = coap_new_binary(hash_len);
3393 if (dummy == NULL)
3394 return 0;
3395
3396 if (coap_crypto_hash_compute(md_type, data->s, data->length,
3397 dummy->s, hash_len, NULL) != 0) {
3399 return 0;
3400 }
3401
3402 *hash = (coap_bin_const_t *)dummy;
3403 return 1;
3404}
3405#endif /* COAP_WS_SUPPORT */
3406
3407/* HMAC Functions */
3408
3409#if COAP_OSCORE_SUPPORT
3410static int
3411coap_crypto_hmac_compute(coap_crypto_md_type_t md_type,
3412 const uint8_t *key, size_t key_len,
3413 const uint8_t *input, size_t ilen,
3414 uint8_t *output, size_t output_size,
3415 size_t *output_len) {
3416#if COAP_USE_PSA_CRYPTO
3417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3418 psa_key_id_t key_id;
3419 psa_algorithm_t psa_alg = PSA_ALG_HMAC(md_type);
3420 size_t mac_len;
3421 psa_status_t status;
3422
3423 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3424 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3425 psa_set_key_algorithm(&attributes, psa_alg);
3426
3427 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3428 return -1;
3429 }
3430
3431 status = psa_mac_compute(key_id, psa_alg, input, ilen,
3432 output, output_size, &mac_len);
3433 psa_destroy_key(key_id);
3434
3435 if (output_len)
3436 *output_len = mac_len;
3437 return (status == PSA_SUCCESS) ? 0 : -1;
3438#else
3439 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
3440 if (!md_info)
3441 return -1;
3442 size_t mac_size = mbedtls_md_get_size(md_info);
3443 if (mac_size > output_size)
3444 return -1;
3445 if (output_len)
3446 *output_len = mac_size;
3447 return mbedtls_md_hmac(md_info, key, key_len, input, ilen, output);
3448#endif
3449}
3450
3451/* AEAD (AES-CCM) Functions */
3452
3453static int
3454coap_crypto_aead_encrypt_ccm(const uint8_t *key, size_t key_len,
3455 const uint8_t *nonce, size_t nonce_len,
3456 const uint8_t *aad, size_t aad_len,
3457 const uint8_t *plaintext, size_t plaintext_len,
3458 uint8_t *ciphertext, size_t ciphertext_size,
3459 size_t *ciphertext_len, size_t tag_len) {
3460#if COAP_USE_PSA_CRYPTO
3461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3462 psa_key_id_t key_id;
3463 psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len);
3464 size_t output_len;
3465 psa_status_t status;
3466
3467 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
3468 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3469 psa_set_key_algorithm(&attributes, psa_alg);
3470 psa_set_key_bits(&attributes, key_len * 8);
3471
3472 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3473 return -1;
3474 }
3475
3476 status = psa_aead_encrypt(key_id, psa_alg,
3477 nonce, nonce_len,
3478 aad, aad_len,
3479 plaintext, plaintext_len,
3480 ciphertext, ciphertext_size, &output_len);
3481 psa_destroy_key(key_id);
3482
3483 if (ciphertext_len)
3484 *ciphertext_len = output_len;
3485 return (status == PSA_SUCCESS) ? 0 : -1;
3486#else
3487 mbedtls_cipher_context_t ctx;
3488 const mbedtls_cipher_info_t *cipher_info;
3489 mbedtls_cipher_type_t cipher_type;
3490 int ret = -1;
3491 size_t result_len = ciphertext_size;
3492
3493 if (key_len == 16) {
3494 cipher_type = MBEDTLS_CIPHER_AES_128_CCM;
3495 } else if (key_len == 32) {
3496 cipher_type = MBEDTLS_CIPHER_AES_256_CCM;
3497 } else {
3498 return -1;
3499 }
3500
3501 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3502 if (!cipher_info)
3503 return -1;
3504
3505 mbedtls_cipher_init(&ctx);
3506 if (mbedtls_cipher_setup(&ctx, cipher_info) != 0)
3507 goto cleanup;
3508 if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_ENCRYPT) != 0)
3509 goto cleanup;
3510
3511#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3512 {
3513 unsigned char tag[16];
3514 if (mbedtls_cipher_auth_encrypt(&ctx,
3515 nonce, nonce_len,
3516 aad, aad_len,
3517 plaintext, plaintext_len,
3518 ciphertext, &result_len,
3519 tag, tag_len) != 0) {
3520 goto cleanup;
3521 }
3522 if ((result_len + tag_len) > ciphertext_size)
3523 goto cleanup;
3524 memcpy(ciphertext + result_len, tag, tag_len);
3525 result_len += tag_len;
3526 }
3527#else
3528 if (mbedtls_cipher_auth_encrypt_ext(&ctx,
3529 nonce, nonce_len,
3530 aad, aad_len,
3531 plaintext, plaintext_len,
3532 ciphertext, ciphertext_size,
3533 &result_len, tag_len) != 0) {
3534 goto cleanup;
3535 }
3536#endif
3537
3538 if (ciphertext_len)
3539 *ciphertext_len = result_len;
3540 ret = 0;
3541
3542cleanup:
3543 mbedtls_cipher_free(&ctx);
3544 return ret;
3545#endif
3546}
3547
3548static int
3549coap_crypto_aead_decrypt_ccm(const uint8_t *key, size_t key_len,
3550 const uint8_t *nonce, size_t nonce_len,
3551 const uint8_t *aad, size_t aad_len,
3552 const uint8_t *ciphertext, size_t ciphertext_len,
3553 uint8_t *plaintext, size_t plaintext_size,
3554 size_t *plaintext_len, size_t tag_len) {
3555#if COAP_USE_PSA_CRYPTO
3556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3557 psa_key_id_t key_id;
3558 psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len);
3559 size_t output_len;
3560 psa_status_t status;
3561
3562 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
3563 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3564 psa_set_key_algorithm(&attributes, psa_alg);
3565 psa_set_key_bits(&attributes, key_len * 8);
3566
3567 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3568 return -1;
3569 }
3570
3571 status = psa_aead_decrypt(key_id, psa_alg,
3572 nonce, nonce_len,
3573 aad, aad_len,
3574 ciphertext, ciphertext_len,
3575 plaintext, plaintext_size, &output_len);
3576 psa_destroy_key(key_id);
3577
3578 if (plaintext_len)
3579 *plaintext_len = output_len;
3580 return (status == PSA_SUCCESS) ? 0 : -1;
3581#else
3582 mbedtls_cipher_context_t ctx;
3583 const mbedtls_cipher_info_t *cipher_info;
3584 mbedtls_cipher_type_t cipher_type;
3585 int ret = -1;
3586 size_t result_len = plaintext_size;
3587
3588 if (key_len == 16) {
3589 cipher_type = MBEDTLS_CIPHER_AES_128_CCM;
3590 } else if (key_len == 32) {
3591 cipher_type = MBEDTLS_CIPHER_AES_256_CCM;
3592 } else {
3593 return -1;
3594 }
3595
3596 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3597 if (!cipher_info)
3598 return -1;
3599
3600 mbedtls_cipher_init(&ctx);
3601 if (mbedtls_cipher_setup(&ctx, cipher_info) != 0)
3602 goto cleanup;
3603 if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_DECRYPT) != 0)
3604 goto cleanup;
3605
3606#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3607 {
3608 const unsigned char *tag = ciphertext + ciphertext_len - tag_len;
3609 if (mbedtls_cipher_auth_decrypt(&ctx,
3610 nonce, nonce_len,
3611 aad, aad_len,
3612 ciphertext, ciphertext_len - tag_len,
3613 plaintext, &result_len,
3614 tag, tag_len) != 0) {
3615 goto cleanup;
3616 }
3617 }
3618#else
3619 if (mbedtls_cipher_auth_decrypt_ext(&ctx,
3620 nonce, nonce_len,
3621 aad, aad_len,
3622 ciphertext, ciphertext_len,
3623 plaintext, plaintext_size,
3624 &result_len, tag_len) != 0) {
3625 goto cleanup;
3626 }
3627#endif
3628
3629 if (plaintext_len)
3630 *plaintext_len = result_len;
3631 ret = 0;
3632
3633cleanup:
3634 mbedtls_cipher_free(&ctx);
3635 return ret;
3636#endif
3637}
3638
3639/* End of Crypto Abstraction Functions */
3640
3641int
3643 return 1;
3644}
3645
3646/*
3647 * Helper to check if COSE cipher algorithm is supported and get key size.
3648 */
3649static int
3650get_cipher_key_size(cose_alg_t alg, size_t *key_size) {
3651 switch ((int)alg) {
3653 if (key_size)
3654 *key_size = 16;
3655 return 1;
3657 if (key_size)
3658 *key_size = 32;
3659 return 1;
3660 default:
3661 coap_log_debug("get_cipher_key_size: COSE cipher %d not supported\n", alg);
3662 return 0;
3663 }
3664}
3665
3666/*
3667 * Helper to get HMAC algorithm parameters from COSE HMAC algorithm.
3668 */
3669static int
3670get_hmac_params(cose_hmac_alg_t hmac_alg,
3671 coap_crypto_md_type_t *md_type,
3672 size_t *mac_len) {
3673 switch ((int)hmac_alg) {
3675 if (md_type)
3676 *md_type = COAP_CRYPTO_MD_SHA256;
3677 if (mac_len)
3678 *mac_len = 32;
3679 return 1;
3681 if (md_type)
3682 *md_type = COAP_CRYPTO_MD_SHA384;
3683 if (mac_len)
3684 *mac_len = 48;
3685 return 1;
3687 if (md_type)
3688 *md_type = COAP_CRYPTO_MD_SHA512;
3689 if (mac_len)
3690 *mac_len = 64;
3691 return 1;
3692 default:
3693 coap_log_debug("get_hmac_params: COSE HMAC %d not supported\n", hmac_alg);
3694 return 0;
3695 }
3696}
3697
3698int
3700 return get_cipher_key_size(alg, NULL);
3701}
3702
3703int
3705 cose_hmac_alg_t hmac_alg;
3706
3707 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
3708 return 0;
3709 return get_hmac_params(hmac_alg, NULL, NULL);
3710}
3711
3712int
3714 coap_bin_const_t *data,
3715 coap_bin_const_t *aad,
3716 uint8_t *result,
3717 size_t *max_result_len) {
3718 const coap_crypto_aes_ccm_t *ccm;
3719
3720 if (data == NULL)
3721 return 0;
3722
3723 assert(params != NULL);
3724
3725 if (!params) {
3726 return 0;
3727 }
3728
3729 if (!get_cipher_key_size(params->alg, NULL)) {
3730 coap_log_debug("coap_crypto_aead_encrypt: algorithm %d not supported\n",
3731 params->alg);
3732 return 0;
3733 }
3734
3735 ccm = &params->params.aes;
3736
3737 if (coap_crypto_aead_encrypt_ccm(ccm->key.s, ccm->key.length,
3738 ccm->nonce, 15 - ccm->l,
3739 aad ? aad->s : NULL, aad ? aad->length : 0,
3740 data->s, data->length,
3741 result, *max_result_len, max_result_len,
3742 ccm->tag_len) != 0) {
3743 coap_log_debug("coap_crypto_aead_encrypt: encryption failed\n");
3744 return 0;
3745 }
3746
3747 return 1;
3748}
3749
3750int
3752 coap_bin_const_t *data,
3753 coap_bin_const_t *aad,
3754 uint8_t *result,
3755 size_t *max_result_len) {
3756 const coap_crypto_aes_ccm_t *ccm;
3757
3758 if (data == NULL)
3759 return 0;
3760
3761 assert(params != NULL);
3762
3763 if (!params) {
3764 return 0;
3765 }
3766
3767 if (!get_cipher_key_size(params->alg, NULL)) {
3768 coap_log_debug("coap_crypto_aead_decrypt: algorithm %d not supported\n",
3769 params->alg);
3770 return 0;
3771 }
3772
3773 ccm = &params->params.aes;
3774
3775 if (data->length < ccm->tag_len) {
3776 coap_log_err("coap_decrypt: invalid tag length\n");
3777 return 0;
3778 }
3779
3780 if (coap_crypto_aead_decrypt_ccm(ccm->key.s, ccm->key.length,
3781 ccm->nonce, 15 - ccm->l,
3782 aad ? aad->s : NULL, aad ? aad->length : 0,
3783 data->s, data->length,
3784 result, *max_result_len, max_result_len,
3785 ccm->tag_len) != 0) {
3786 coap_log_debug("coap_crypto_aead_decrypt: decryption failed\n");
3787 return 0;
3788 }
3789
3790 return 1;
3791}
3792
3793int
3795 coap_bin_const_t *key,
3796 coap_bin_const_t *data,
3797 coap_bin_const_t **hmac) {
3798 coap_crypto_md_type_t md_type;
3799 size_t mac_len;
3801
3802 assert(key);
3803 assert(data);
3804 assert(hmac);
3805
3806 if (!get_hmac_params(hmac_alg, &md_type, &mac_len)) {
3807 coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3808 return 0;
3809 }
3810
3811 dummy = coap_new_binary(mac_len);
3812 if (dummy == NULL)
3813 return 0;
3814
3815 if (coap_crypto_hmac_compute(md_type, key->s, key->length,
3816 data->s, data->length,
3817 dummy->s, mac_len, NULL) != 0) {
3818 coap_log_debug("coap_crypto_hmac: computation failed\n");
3820 return 0;
3821 }
3822
3823 *hmac = (coap_bin_const_t *)dummy;
3824 return 1;
3825}
3826
3827#endif /* COAP_OSCORE_SUPPORT */
3828
3829#else /* ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE*/
3830
3831#ifdef __clang__
3832/* Make compilers happy that do not like empty modules. As this function is
3833 * never used, we ignore -Wunused-function at the end of compiling this file
3834 */
3835#pragma GCC diagnostic ignored "-Wunused-function"
3836#endif
3837static inline void
3838dummy(void) {
3839}
3840
3841#endif /* ! COAP_WITH_LIBMBEDTLS */
static size_t strnlen(const char *s, size_t maxlen)
A length-safe strlen() fake.
Definition coap_debug.c:183
static void dummy(void)
#define COAP_SERVER_SUPPORT
#define PRIuS
#define PRIx32
#define PRIdS
const char * coap_socket_strerror(void)
Definition coap_io.c:958
#define COAP_RXBUFFER_SIZE
Definition coap_io.h:31
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:68
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
@ 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:252
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
Definition coap_notls.c:364
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:436
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:359
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:378
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:298
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:396
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:268
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:291
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:347
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:424
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:343
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:260
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:373
void coap_dtls_free_context(void *handle COAP_UNUSED)
Definition coap_notls.c:321
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:339
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
Definition coap_notls.c:316
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:415
#define NULL
Definition coap_option.h:30
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
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:195
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:5458
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:3148
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
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.
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.
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:109
#define COAP_DTLS_RETRANSMIT_MS
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:159
int coap_pki_name_match_sni(const char *name_entry, size_t name_length, const char *sni_match)
Check if the PKI name entry is a (wildcard) match for the requested SNI.
Definition coap_dtls.c:288
void coap_dtls_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c:118
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.
#define COAP_DTLS_CID_LENGTH
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Definition coap_notls.c:354
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c:113
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
#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.
#define COAP_DTLS_RETRANSMIT_TOTAL_MS
@ 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:101
struct coap_dtls_key_t coap_dtls_key_t
The structure that holds the PKI key information.
coap_dtls_role_t
Definition coap_dtls.h:48
struct coap_dtls_spsk_info_t coap_dtls_spsk_info_t
The structure that holds the Server Pre-Shared Key and Identity Hint information.
coap_tls_library_t
Definition coap_dtls.h:74
struct coap_dtls_pki_t coap_dtls_pki_t
Definition coap_dtls.h:36
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
Definition coap_dtls.h:250
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
Definition coap_dtls.h:247
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
Definition coap_dtls.h:241
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
Definition coap_dtls.h:239
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
Definition coap_dtls.h:256
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
Definition coap_dtls.h:243
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
Definition coap_dtls.h:245
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
Definition coap_dtls.h:253
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
Definition coap_dtls.h:50
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition coap_dtls.h:49
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
Definition coap_dtls.h:177
@ COAP_TLS_LIBRARY_MBEDTLS
Using Mbed TLS library.
Definition coap_dtls.h:79
@ COAP_EVENT_DTLS_CLOSED
Triggerrd when (D)TLS session closed.
Definition coap_event.h:41
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
Definition coap_event.h:43
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
Definition coap_event.h:47
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
#define coap_log_emerg(...)
Definition coap_debug.h:87
coap_log_t
Logging type.
Definition coap_debug.h:56
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition coap_notls.c:311
#define coap_dtls_log(level,...)
Logging function.
Definition coap_debug.h:306
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition coap_notls.c:306
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
@ COAP_LOG_INFO
Definition coap_debug.h:63
@ COAP_LOG_EMERG
Definition coap_debug.h:57
@ COAP_LOG_NOTICE
Definition coap_debug.h:62
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
@ COAP_LOG_ALERT
Definition coap_debug.h:58
@ COAP_LOG_CRIT
Definition coap_debug.h:59
@ COAP_LOG_ERR
Definition coap_debug.h:60
@ COAP_LOG_WARN
Definition coap_debug.h:61
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 Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:234
@ COAP_PROTO_DTLS
Definition coap_pdu.h:237
@ COAP_PROTO_TLS
Definition coap_pdu.h:239
@ COAP_PROTO_WSS
Definition coap_pdu.h:241
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:81
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:119
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
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:74
coap_address_t remote
remote address and port
Definition coap_io.h:58
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
The CoAP stack's global state is stored in a coap_context_t object.
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::@116123127237115130276143055073025260011070157353 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorithm to use.
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:418
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
Definition coap_dtls.h:425
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:445
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
Definition coap_dtls.h:441
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:423
The structure that holds the PKI key information.
Definition coap_dtls.h:284
coap_pki_key_define_t define
for definable type keys
Definition coap_dtls.h:291
coap_pki_key_t key_type
key format type
Definition coap_dtls.h:285
union coap_dtls_key_t::@213054141046010326177203305133000126243235267332 key
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:317
uint8_t allow_no_crl
1 ignore if CRL not there
Definition coap_dtls.h:331
void * cn_call_back_arg
Passed in to the CN callback function.
Definition coap_dtls.h:359
uint8_t allow_short_rsa_length
1 if small RSA keysizes are allowed
Definition coap_dtls.h:334
uint8_t allow_sni_cn_mismatch
1 if SNI and returned CN allowed to mismatch (Client only).
Definition coap_dtls.h:341
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
Definition coap_dtls.h:328
uint8_t allow_bad_md_hash
1 if unsupported MD hashes are allowed
Definition coap_dtls.h:333
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
Definition coap_dtls.h:338
uint8_t check_cert_revocation
1 if revocation checks wanted
Definition coap_dtls.h:330
uint8_t cert_chain_verify_depth
recommended depth is 3
Definition coap_dtls.h:329
uint8_t allow_expired_certs
1 if expired certs are allowed
Definition coap_dtls.h:327
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:322
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:376
uint8_t allow_self_signed
1 if self-signed certs are allowed.
Definition coap_dtls.h:325
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
Definition coap_dtls.h:358
uint8_t allow_expired_crl
1 if expired crl is allowed
Definition coap_dtls.h:332
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
Definition coap_dtls.h:335
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:323
The structure that holds the Server Pre-Shared Key and Identity Hint information.
Definition coap_dtls.h:458
coap_bin_const_t hint
Definition coap_dtls.h:459
The structure used for defining the Server PSK setup data to be used.
Definition coap_dtls.h:509
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
Definition coap_dtls.h:530
void * id_call_back_arg
Passed in to the Identity callback function.
Definition coap_dtls.h:531
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:514
coap_dtls_spsk_info_t psk_info
Server PSK definition.
Definition coap_dtls.h:541
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:266
coap_const_char_ptr_t private_key
define: Private Key
Definition coap_dtls.h:267
coap_const_char_ptr_t ca
define: Common CA Certificate
Definition coap_dtls.h:265
size_t public_cert_len
define Public Cert length (if needed)
Definition coap_dtls.h:269
size_t ca_len
define CA Cert length (if needed)
Definition coap_dtls.h:268
coap_pki_define_t private_key_def
define: Private Key type definition
Definition coap_dtls.h:273
size_t private_key_len
define Private Key length (if needed)
Definition coap_dtls.h:270
coap_pki_define_t ca_def
define: Common CA type definition
Definition coap_dtls.h:271
coap_pki_define_t public_cert_def
define: Public Cert type definition
Definition coap_dtls.h:272
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_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
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)
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:88
uint64_t built_version
(D)TLS Built against Library Version
Definition coap_dtls.h:91
coap_tls_library_t type
Library type.
Definition coap_dtls.h:90
uint64_t version
(D)TLS runtime Library Version
Definition coap_dtls.h:89
const char * s_byte
signed char ptr
Definition coap_str.h:84
const uint8_t * u_byte
unsigned char ptr
Definition coap_str.h:85