libcoap 4.3.5-develop-7a7336a
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 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
1467 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1468 MBEDTLS_SSL_IS_SERVER,
1469 c_session->proto == COAP_PROTO_DTLS ?
1470 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1471 MBEDTLS_SSL_TRANSPORT_STREAM,
1472 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1473 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1474 -ret, get_error_string(ret));
1475 goto fail;
1476 }
1477
1478#if !COAP_USE_PSA_CRYPTO
1479 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1480#endif /* !COAP_USE_PSA_CRYPTO */
1481
1482#if defined(MBEDTLS_SSL_PROTO_DTLS)
1483 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1485#endif /* MBEDTLS_SSL_PROTO_DTLS */
1486
1487 if (m_context->psk_pki_enabled & IS_PSK) {
1488#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1489 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1490 if (c_session->context->spsk_setup_data.validate_sni_call_back) {
1491 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1492 }
1493#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1494 m_env->ec_jpake = c_session->context->spsk_setup_data.ec_jpake;
1495#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1496#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1497 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1498#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1499 }
1500
1501 if (m_context->psk_pki_enabled & IS_PKI) {
1502 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1503 &m_env->private_key, m_env, m_context,
1504 c_session, &m_context->setup_data,
1506 if (ret < 0) {
1507 coap_log_err("PKI setup failed\n");
1508 return ret;
1509 }
1510 if (m_context->setup_data.validate_sni_call_back) {
1511 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1512 }
1513 }
1514
1515#if COAP_USE_PSA_CRYPTO
1516 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx)) != 0) {
1517#else /* !COAP_USE_PSA_CRYPTO */
1518 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1519 mbedtls_ctr_drbg_random,
1520 &m_env->ctr_drbg)) != 0) {
1521#endif /* !COAP_USE_PSA_CRYPTO */
1522 coap_log_err("mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1523 -ret, get_error_string(ret));
1524 goto fail;
1525 }
1526
1527#if defined(MBEDTLS_SSL_PROTO_DTLS)
1528 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1529 mbedtls_ssl_cookie_check,
1530 &m_env->cookie_ctx);
1531#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1532 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1533#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1534#endif /* MBEDTLS_SSL_PROTO_DTLS */
1535#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1536 /*
1537 * Configure CID max length.
1538 *
1539 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
1540 * to use RFC9146 extension ID of 54, rather than the draft version -05
1541 * value of 254.
1542 */
1543 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1545fail:
1546 return ret;
1547}
1548#endif /* MBEDTLS_SSL_SRV_C */
1549
1550#if COAP_CLIENT_SUPPORT
1551static int *psk_ciphers = NULL;
1552static int *pki_ciphers = NULL;
1553static int *ecjpake_ciphers = NULL;
1554static int processed_ciphers = 0;
1555
1556#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1557static int
1558coap_ssl_ciphersuite_uses_psk(const mbedtls_ssl_ciphersuite_t *info) {
1559#if COAP_USE_PSA_CRYPTO
1560 switch (info->key_exchange) {
1561 case MBEDTLS_KEY_EXCHANGE_PSK:
1562 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1563 return 1;
1564 case MBEDTLS_KEY_EXCHANGE_NONE:
1565 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1566 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1567 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1568 default:
1569 return 0;
1570 }
1571#elif MBEDTLS_VERSION_NUMBER >= 0x03060000
1572 switch (info->key_exchange) {
1573 case MBEDTLS_KEY_EXCHANGE_PSK:
1574 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1575 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1576 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1577 return 1;
1578 case MBEDTLS_KEY_EXCHANGE_NONE:
1579 case MBEDTLS_KEY_EXCHANGE_RSA:
1580 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1581 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1582 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1583 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1584 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1585 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1586 default:
1587 return 0;
1588 }
1589#else /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1590 return mbedtls_ssl_ciphersuite_uses_psk(info);
1591#endif /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1592}
1593#endif /* defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) */
1594
1595/* Cipher suite lists a cipher suite is put in by set_ciphersuites() */
1596#define COAP_CIPHER_IN_PSK 0x01
1597#define COAP_CIPHER_IN_PKI 0x02
1598#define COAP_CIPHER_IN_ECJPAKE 0x04
1599
1600/*
1601 * Returns which of the PSK, PKI and EC JPAKE lists the cipher suite
1602 * belongs in (0 if none). Used for both counting and filling the lists
1603 * so that they always agree.
1604 */
1605static int
1606coap_ciphersuite_lists(int id) {
1607 const mbedtls_ssl_ciphersuite_t *cur = mbedtls_ssl_ciphersuite_from_id(id);
1608
1609 if (!cur)
1610 return 0;
1611#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1612 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1613 /* Minimum of TLS1.2 required - skip */
1614 return 0;
1615 }
1616#else
1617 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1618 /* Minimum of TLS1.2 required - skip */
1619 return 0;
1620 }
1621#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1622#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1623 if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE)
1624 return COAP_CIPHER_IN_ECJPAKE;
1625#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1626#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1627 if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3)
1628 return COAP_CIPHER_IN_PSK | COAP_CIPHER_IN_PKI;
1629#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
1630#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1631 if (coap_ssl_ciphersuite_uses_psk(cur))
1632 return COAP_CIPHER_IN_PSK;
1633#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1634 return COAP_CIPHER_IN_PKI;
1635}
1636
1637static void
1638set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1639 if (!processed_ciphers) {
1640 const int *base = mbedtls_ssl_list_ciphersuites();
1641 const int *list;
1642 int *psk_list;
1643 int *pki_list;
1644 int *ecjpake_list;
1645 int psk_count = 1; /* account for empty terminator */
1646 int pki_count = 1;
1647 int ecjpake_count = 1;
1648
1649 for (list = base; *list; list++) {
1650 int in = coap_ciphersuite_lists(*list);
1651
1652 if (in & COAP_CIPHER_IN_PSK)
1653 psk_count++;
1654 if (in & COAP_CIPHER_IN_PKI)
1655 pki_count++;
1656 if (in & COAP_CIPHER_IN_ECJPAKE)
1657 ecjpake_count++;
1658 }
1659
1660 psk_ciphers = mbedtls_malloc(psk_count * sizeof(psk_ciphers[0]));
1661 pki_ciphers = mbedtls_malloc(pki_count * sizeof(pki_ciphers[0]));
1662 ecjpake_ciphers = mbedtls_malloc(ecjpake_count * sizeof(ecjpake_ciphers[0]));
1663 if (psk_ciphers == NULL || pki_ciphers == NULL || ecjpake_ciphers == NULL) {
1664 coap_log_err("set_ciphers: mbedtls_malloc failed\n");
1665 mbedtls_free(psk_ciphers);
1666 mbedtls_free(pki_ciphers);
1667 mbedtls_free(ecjpake_ciphers);
1668 psk_ciphers = NULL;
1669 pki_ciphers = NULL;
1670 ecjpake_ciphers = NULL;
1671 return;
1672 }
1673
1674 psk_list = psk_ciphers;
1675 pki_list = pki_ciphers;
1676 ecjpake_list = ecjpake_ciphers;
1677
1678 for (list = base; *list; list++) {
1679 int in = coap_ciphersuite_lists(*list);
1680
1681 if (in & COAP_CIPHER_IN_PSK)
1682 *psk_list++ = *list;
1683 if (in & COAP_CIPHER_IN_PKI)
1684 *pki_list++ = *list;
1685 if (in & COAP_CIPHER_IN_ECJPAKE)
1686 *ecjpake_list++ = *list;
1687 }
1688 /* zero terminate */
1689 *psk_list = 0;
1690 *pki_list = 0;
1691 *ecjpake_list = 0;
1692 processed_ciphers = 1;
1693 }
1694 switch (method) {
1695 case COAP_ENC_PSK:
1696 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1697 break;
1698 case COAP_ENC_PKI:
1699 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1700 break;
1701 case COAP_ENC_ECJPAKE:
1702 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1703 break;
1704 default:
1705 assert(0);
1706 break;
1707 }
1708}
1709
1710static int
1711setup_client_ssl_session(coap_session_t *c_session,
1712 coap_mbedtls_env_t *m_env) {
1713 int ret;
1714
1715 coap_mbedtls_context_t *m_context =
1716 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1717
1718 m_context->psk_pki_enabled |= IS_CLIENT;
1719
1720 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1721 MBEDTLS_SSL_IS_CLIENT,
1722 c_session->proto == COAP_PROTO_DTLS ?
1723 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1724 MBEDTLS_SSL_TRANSPORT_STREAM,
1725 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1726 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1727 -ret, get_error_string(ret));
1728 goto fail;
1729 }
1730
1731#if defined(MBEDTLS_SSL_PROTO_DTLS)
1732 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1734#endif /* MBEDTLS_SSL_PROTO_DTLS */
1735
1736 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1737#if !COAP_USE_PSA_CRYPTO
1738 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1739#endif /* !COAP_USE_PSA_CRYPTO */
1740
1741 if (m_context->psk_pki_enabled & IS_PSK) {
1742#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1743 const coap_bin_const_t *psk_key;
1744 const coap_bin_const_t *psk_identity;
1745
1746 coap_log_info("Setting PSK key\n");
1747
1748 psk_key = coap_get_session_client_psk_key(c_session);
1749 psk_identity = coap_get_session_client_psk_identity(c_session);
1750 if (psk_key == NULL || psk_identity == NULL) {
1751 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1752 goto fail;
1753 }
1754
1755 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->s,
1756 psk_key->length, psk_identity->s,
1757 psk_identity->length)) != 0) {
1758 coap_log_err("mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1759 -ret, get_error_string(ret));
1760 goto fail;
1761 }
1762 if (c_session->cpsk_setup_data.client_sni) {
1763 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1764 c_session->cpsk_setup_data.client_sni)) != 0) {
1765 coap_log_err("mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1766 -ret, get_error_string(ret));
1767 goto fail;
1768 }
1769 }
1770 /* Identity Hint currently not supported in Mbed TLS so code removed */
1771
1772#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1773 if (c_session->cpsk_setup_data.ec_jpake) {
1774 m_env->ec_jpake = 1;
1775 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1776#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1777 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1778#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1779 } else {
1780 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1781 }
1782#else /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1783 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1784#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1785#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1786 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1787#endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1788 } else if ((m_context->psk_pki_enabled & IS_PKI) ||
1789 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1790 /*
1791 * If neither PSK or PKI have been set up, use PKI basics.
1792 * This works providing COAP_PKI_KEY_PEM has a value of 0.
1793 */
1794 coap_dtls_pki_t *setup_data = &m_context->setup_data;
1795
1796 if (!(m_context->psk_pki_enabled & IS_PKI)) {
1797 /* PKI not defined - set up some defaults */
1798 setup_data->verify_peer_cert = 1;
1799 setup_data->check_common_ca = 0;
1800 setup_data->allow_self_signed = 1;
1801 setup_data->allow_expired_certs = 1;
1802 setup_data->cert_chain_validation = 1;
1803 setup_data->cert_chain_verify_depth = 2;
1804 setup_data->check_cert_revocation = 1;
1805 setup_data->allow_no_crl = 1;
1806 setup_data->allow_expired_crl = 1;
1807 setup_data->is_rpk_not_cert = 0;
1808 setup_data->use_cid = 0;
1809 }
1810 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1811 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1812 &m_env->private_key, m_env, m_context,
1813 c_session, setup_data,
1815 if (ret < 0) {
1816 coap_log_err("PKI setup failed\n");
1817 return ret;
1818 }
1819#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1820 if (c_session->proto == COAP_PROTO_TLS ||
1821 c_session->proto == COAP_PROTO_WSS) {
1822 static const char *alpn_list[] = { "coap", NULL };
1823
1824 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1825 if (ret != 0) {
1826 coap_log_err("ALPN setup failed %d)\n", ret);
1827 }
1828 }
1829#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN */
1830 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1831#if defined(MBEDTLS_SSL_PROTO_DTLS)
1832#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1833 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1834#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1835#endif /* MBEDTLS_SSL_PROTO_DTLS */
1836 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1837 }
1838 return 0;
1839
1840fail:
1841 return ret;
1842}
1843#endif /* COAP_CLIENT_SUPPORT */
1844
1845static void
1846mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1847 if (!m_env) {
1848 return;
1849 }
1850
1851 mbedtls_x509_crt_free(&m_env->cacert);
1852 mbedtls_x509_crt_free(&m_env->public_cert);
1853 mbedtls_pk_free(&m_env->private_key);
1854#if !COAP_USE_PSA_CRYPTO
1855 mbedtls_entropy_free(&m_env->entropy);
1856 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1857#endif /* !COAP_USE_PSA_CRYPTO */
1858 mbedtls_ssl_config_free(&m_env->conf);
1859 mbedtls_ssl_free(&m_env->ssl);
1860 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1861}
1862
1863static void
1864coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1865 if (m_env) {
1866 if (!m_env->sent_alert)
1867 mbedtls_ssl_close_notify(&m_env->ssl);
1868 mbedtls_cleanup(m_env);
1869 mbedtls_free(m_env);
1870 }
1871}
1872
1873#if COAP_MAX_LOGGING_LEVEL > 0
1874static const char *
1875report_mbedtls_alert(unsigned char alert) {
1876 switch (alert) {
1877 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1878 return ": Bad Record MAC";
1879 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1880 return ": Handshake failure";
1881 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1882 return ": No Certificate provided";
1883 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1884 return ": Certificate is bad";
1885 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1886 return ": Certificate is unknown";
1887 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1888 return ": CA is unknown";
1889 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1890 return ": Access was denied";
1891 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1892 return ": Decrypt error";
1893 default:
1894 return "";
1895 }
1896}
1897#endif /* COAP_MAX_LOGGING_LEVEL */
1898
1899/*
1900 * return -1 failure
1901 * 0 not completed
1902 * 1 established
1903 */
1904static int
1905do_mbedtls_handshake(coap_session_t *c_session,
1906 coap_mbedtls_env_t *m_env) {
1907 int ret;
1908 int alert;
1909
1910 ret = mbedtls_ssl_handshake(&m_env->ssl);
1911 switch (ret) {
1912 case 0:
1913 m_env->established = 1;
1914 coap_log_debug("* %s: Mbed TLS established\n",
1915 coap_session_str(c_session));
1916 ret = 1;
1917#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1918#if COAP_CLIENT_SUPPORT
1919 if (c_session->type == COAP_SESSION_TYPE_CLIENT &&
1920 c_session->proto == COAP_PROTO_DTLS) {
1921 coap_mbedtls_context_t *m_context;
1922
1923 m_context = (coap_mbedtls_context_t *)c_session->context->dtls_context;
1924 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
1925 m_context->setup_data.use_cid) {
1926 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1927 int enabled;
1928 size_t peer_cid_len;
1929
1930 /* See whether CID was negotiated */
1931 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1932 enabled == MBEDTLS_SSL_CID_ENABLED) {
1933 c_session->negotiated_cid = 1;
1934 } else {
1935 coap_log_info("** %s: CID was not negotiated\n", coap_session_str(c_session));
1936 c_session->negotiated_cid = 0;
1937 }
1938 }
1939 }
1940#endif /* COAP_CLIENT_SUPPORT */
1941#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1943 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
1944 break;
1945 case MBEDTLS_ERR_SSL_WANT_READ:
1946 case MBEDTLS_ERR_SSL_WANT_WRITE:
1947 if (m_env->ssl.state == MBEDTLS_SSL_SERVER_HELLO
1948#if MBEDTLS_VERSION_NUMBER >= 0x03030000
1949 || m_env->ssl.state == MBEDTLS_SSL_NEW_SESSION_TICKET
1950#endif /* MBEDTLS_VERSION_NUMBER >= 0x03030000 */
1951 ) {
1952 if (++m_env->server_hello_cnt > 10) {
1953 /* retried this too many times */
1954 goto fail;
1955 }
1956 }
1957 errno = EAGAIN;
1958 ret = 0;
1959 break;
1960 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1961 coap_log_debug("hello verification requested\n");
1962 goto reset;
1963 case MBEDTLS_ERR_SSL_INVALID_MAC:
1964 goto fail;
1965#ifdef MBEDTLS_2_X_COMPAT
1966 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1967#else /* ! MBEDTLS_2_X_COMPAT */
1968 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1969#endif /* ! MBEDTLS_2_X_COMPAT */
1970 goto fail;
1971 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1972 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1973 goto fail_alert;
1974#ifdef MBEDTLS_2_X_COMPAT
1975 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1976 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1977 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1978 goto fail_alert;
1979#endif /* MBEDTLS_2_X_COMPAT */
1980 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1981 goto fail;
1982 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1983 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1984 coap_log_warn("***%s: Alert '%d'%s\n",
1985 coap_session_str(c_session), m_env->ssl.in_msg[1],
1986 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1987 /* Fall through */
1988 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1989 case MBEDTLS_ERR_SSL_CONN_EOF:
1990 case MBEDTLS_ERR_NET_CONN_RESET:
1992 ret = -1;
1993 break;
1994 default:
1995 coap_log_warn("do_mbedtls_handshake: session establish "
1996 "returned -0x%x: '%s'\n",
1997 -ret, get_error_string(ret));
1998 ret = -1;
1999 break;
2000 }
2001 return ret;
2002
2003fail_alert:
2004 mbedtls_ssl_send_alert_message(&m_env->ssl,
2005 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2006 alert);
2007 m_env->sent_alert = 1;
2008fail:
2009 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
2010 coap_log_warn("do_mbedtls_handshake: session establish "
2011 "returned '%s'\n",
2012 get_error_string(ret));
2013reset:
2014 mbedtls_ssl_session_reset(&m_env->ssl);
2015#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2016 if (m_env->ec_jpake) {
2017 const coap_bin_const_t *psk_key;
2018
2019#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2020 if (c_session->type == COAP_SESSION_TYPE_CLIENT) {
2021 psk_key = coap_get_session_client_psk_key(c_session);
2022 } else {
2023 psk_key = coap_get_session_server_psk_key(c_session);
2024 }
2025#elif COAP_CLIENT_SUPPORT
2026 psk_key = coap_get_session_client_psk_key(c_session);
2027#else /* COAP_SERVER_SUPPORT */
2028 psk_key = coap_get_session_server_psk_key(c_session);
2029#endif /* COAP_SERVER_SUPPORT */
2030 if (psk_key) {
2031 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
2032 }
2033 }
2034#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2035 return -1;
2036}
2037
2038static void
2039mbedtls_debug_out(void *ctx COAP_UNUSED, int level,
2040 const char *file COAP_UNUSED,
2041 int line COAP_UNUSED, const char *str) {
2042
2043 coap_log_t coap_level = COAP_LOG_DEBUG;
2044 /*
2045 * 0 No debug
2046 * 1 Error
2047 * 2 State change
2048 * 3 Informational
2049 * 4 Verbose
2050 */
2051 switch (level) {
2052 case 0:
2053 coap_level = COAP_LOG_EMERG;
2054 break;
2055 case 1:
2056 coap_level = COAP_LOG_WARN;
2057 break;
2058 case 2:
2059 coap_level = COAP_LOG_NOTICE;
2060 break;
2061 case 3:
2062 coap_level = COAP_LOG_INFO;
2063 break;
2064 case 4:
2065 default:
2066 coap_level = COAP_LOG_DEBUG;
2067 break;
2068 }
2069 coap_dtls_log(coap_level, "%s", str);
2070}
2071
2072#if !COAP_DISABLE_TCP
2073/*
2074 * strm
2075 * return +ve data amount
2076 * 0 no more
2077 * -ve Mbed TLS error
2078 */
2079static int
2080coap_sock_read(void *ctx, unsigned char *out, size_t outl) {
2081 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
2082 coap_session_t *c_session = (coap_session_t *)ctx;
2083
2084 if (out != NULL) {
2085 ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_read(c_session, out, outl);
2086 /* Translate layer returns into what MbedTLS expects */
2087 if (ret == -1) {
2088 if (errno == ECONNRESET) {
2089 /* graceful shutdown */
2090 ret = MBEDTLS_ERR_SSL_CONN_EOF;
2091 } else {
2092 ret = MBEDTLS_ERR_NET_RECV_FAILED;
2093 }
2094 } else if (ret == 0) {
2095 errno = EAGAIN;
2096 ret = MBEDTLS_ERR_SSL_WANT_READ;
2097 }
2098 }
2099 return ret;
2100}
2101
2102/*
2103 * strm
2104 * return +ve data amount
2105 * 0 no more
2106 * -ve Mbed TLS error
2107 */
2108static int
2109coap_sock_write(void *context, const unsigned char *in, size_t inl) {
2110 int ret = 0;
2111 coap_session_t *c_session = (coap_session_t *)context;
2112
2113 ret = c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
2114 (const uint8_t *)in,
2115 inl);
2116 /* Translate layer what returns into what MbedTLS expects */
2117 if (ret < 0) {
2118 if ((c_session->state == COAP_SESSION_STATE_CSM ||
2119 c_session->state == COAP_SESSION_STATE_HANDSHAKE) &&
2120 (errno == EPIPE || errno == ECONNRESET)) {
2121 /*
2122 * Need to handle a TCP timing window where an agent continues with
2123 * the sending of the next handshake or a CSM.
2124 * However, the peer does not like a certificate and so sends a
2125 * fatal alert and closes the TCP session.
2126 * The sending of the next handshake or CSM may get terminated because
2127 * of the closed TCP session, but there is still an outstanding alert
2128 * to be read in and reported on.
2129 * In this case, pretend that sending the info was fine so that the
2130 * alert can be read (which effectively is what happens with DTLS).
2131 */
2132 ret = inl;
2133 } else {
2134#ifdef _WIN32
2135 int lasterror = WSAGetLastError();
2136
2137 if (lasterror == WSAEWOULDBLOCK) {
2138 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2139 } else if (lasterror == WSAECONNRESET) {
2140 ret = MBEDTLS_ERR_NET_CONN_RESET;
2141 }
2142#else
2143 if (errno == EAGAIN || errno == EINTR) {
2144 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2145 } else if (errno == EPIPE || errno == ECONNRESET) {
2146 ret = MBEDTLS_ERR_NET_CONN_RESET;
2147 }
2148#endif
2149 else {
2150 ret = MBEDTLS_ERR_NET_SEND_FAILED;
2151 }
2152 coap_log_debug("* %s: failed to send %" PRIdS " bytes (%s) state %d\n",
2153 coap_session_str(c_session), inl, coap_socket_strerror(),
2154 c_session->state);
2155 }
2156 }
2157 if (ret == 0) {
2158 errno = EAGAIN;
2159 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2160 }
2161 return ret;
2162}
2163#endif /* !COAP_DISABLE_TCP */
2164
2165static coap_mbedtls_env_t *
2166coap_dtls_new_mbedtls_env(coap_session_t *c_session,
2167 coap_dtls_role_t role,
2168 coap_proto_t proto) {
2169#if !COAP_USE_PSA_CRYPTO
2170 int ret = 0;
2171#endif /* !COAP_USE_PSA_CRYPTO */
2172 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2173
2174 if (m_env)
2175 return m_env;
2176
2177 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(sizeof(coap_mbedtls_env_t));
2178 if (!m_env) {
2179 return NULL;
2180 }
2181 memset(m_env, 0, sizeof(coap_mbedtls_env_t));
2182
2183 mbedtls_ssl_init(&m_env->ssl);
2184#if !COAP_USE_PSA_CRYPTO
2185 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
2186 mbedtls_entropy_init(&m_env->entropy);
2187#endif /* !COAP_USE_PSA_CRYPTO */
2188 mbedtls_ssl_config_init(&m_env->conf);
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_free(m_env);
2322 }
2323 return NULL;
2324}
2325
2326int
2328#if defined(MBEDTLS_SSL_PROTO_DTLS)
2329 return 1;
2330#else /* !MBEDTLS_SSL_PROTO_DTLS */
2331 static int reported = 0;
2332 if (!reported) {
2333 reported = 1;
2334 coap_log_emerg("libcoap not compiled for DTLS with Mbed TLS"
2335 " - update Mbed TLS to include DTLS\n");
2336 }
2337 return 0;
2338#endif /* !MBEDTLS_SSL_PROTO_DTLS */
2339}
2340
2341int
2343#if !COAP_DISABLE_TCP
2344 return 1;
2345#else /* COAP_DISABLE_TCP */
2346 return 0;
2347#endif /* COAP_DISABLE_TCP */
2348}
2349
2350/*
2351 * return 0 failed
2352 * 1 passed
2353 */
2354int
2356 return 1;
2357}
2358
2359/*
2360 * return 0 failed
2361 * 1 passed
2362 */
2363int
2365 return 1;
2366}
2367
2368/*
2369 * return 0 failed
2370 * 1 passed
2371 */
2372int
2374 return 0;
2375}
2376
2377/*
2378 * return 0 failed
2379 * 1 passed
2380 */
2381int
2383 return 0;
2384}
2385
2386/*
2387 * return 0 failed
2388 * 1 passed
2389 */
2390int
2392#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2393 return 1;
2394#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2395 return 0;
2396#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2397}
2398
2399#if COAP_CLIENT_SUPPORT
2400int
2401coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
2402#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2403 c_context->testing_cids = every;
2404 return 1;
2405#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2406 (void)c_context;
2407 (void)every;
2408 return 0;
2409#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2410}
2411#endif /* COAP_CLIENT_SUPPORT */
2412
2413void *
2415 coap_mbedtls_context_t *m_context;
2416 (void)c_context;
2417
2418 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(sizeof(coap_mbedtls_context_t));
2419 if (m_context) {
2420 memset(m_context, 0, sizeof(coap_mbedtls_context_t));
2421 }
2422 return m_context;
2423}
2424
2425#if COAP_SERVER_SUPPORT
2426/*
2427 * return 0 failed
2428 * 1 passed
2429 */
2430int
2431coap_dtls_context_set_spsk(coap_context_t *c_context,
2432 coap_dtls_spsk_t *setup_data
2433 ) {
2434 coap_mbedtls_context_t *m_context =
2435 ((coap_mbedtls_context_t *)c_context->dtls_context);
2436
2437#if !defined(MBEDTLS_SSL_SRV_C)
2438 coap_log_emerg("coap_context_set_spsk:"
2439 " libcoap not compiled for Server Mode for Mbed TLS"
2440 " - update Mbed TLS to include Server Mode\n");
2441 return 0;
2442#endif /* !MBEDTLS_SSL_SRV_C */
2443 if (!m_context || !setup_data)
2444 return 0;
2445
2446 if (setup_data->ec_jpake) {
2447#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2448 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2449#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2450 }
2451 if (setup_data->psk_info.hint.length) {
2452 coap_log_warn("CoAP Server with Mbed TLS does not support Identity Hint\n");
2453 }
2454 m_context->psk_pki_enabled |= IS_PSK;
2455 return 1;
2456}
2457#endif /* COAP_SERVER_SUPPORT */
2458
2459#if COAP_CLIENT_SUPPORT
2460/*
2461 * return 0 failed
2462 * 1 passed
2463 */
2464int
2465coap_dtls_context_set_cpsk(coap_context_t *c_context,
2466 coap_dtls_cpsk_t *setup_data
2467 ) {
2468#if !defined(MBEDTLS_SSL_CLI_C)
2469 (void)c_context;
2470 (void)setup_data;
2471
2472 coap_log_emerg("coap_context_set_cpsk:"
2473 " libcoap not compiled for Client Mode for Mbed TLS"
2474 " - update Mbed TLS to include Client Mode\n");
2475 return 0;
2476#else /* MBEDTLS_SSL_CLI_C */
2477 coap_mbedtls_context_t *m_context =
2478 ((coap_mbedtls_context_t *)c_context->dtls_context);
2479
2480 if (!m_context || !setup_data)
2481 return 0;
2482
2483 if (setup_data->validate_ih_call_back) {
2484 coap_log_warn("CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2485 }
2486 if (setup_data->ec_jpake) {
2487#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2488 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2489#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2490 }
2491 if (setup_data->use_cid) {
2492#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2493 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2494#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2495 }
2496 m_context->psk_pki_enabled |= IS_PSK;
2497 return 1;
2498#endif /* MBEDTLS_SSL_CLI_C */
2499}
2500#endif /* COAP_CLIENT_SUPPORT */
2501
2502int
2504 const coap_dtls_pki_t *setup_data,
2505 const coap_dtls_role_t role COAP_UNUSED) {
2506 coap_mbedtls_context_t *m_context =
2507 ((coap_mbedtls_context_t *)c_context->dtls_context);
2508
2509 m_context->setup_data = *setup_data;
2510 if (!m_context->setup_data.verify_peer_cert) {
2511 /* Needs to be clear so that no CA DNs are transmitted */
2512 m_context->setup_data.check_common_ca = 0;
2513 /* Allow all of these but warn if issue */
2514 m_context->setup_data.allow_self_signed = 1;
2515 m_context->setup_data.allow_expired_certs = 1;
2516 m_context->setup_data.cert_chain_validation = 1;
2517 m_context->setup_data.cert_chain_verify_depth = 10;
2518 m_context->setup_data.check_cert_revocation = 1;
2519 m_context->setup_data.allow_no_crl = 1;
2520 m_context->setup_data.allow_expired_crl = 1;
2521 m_context->setup_data.allow_bad_md_hash = 1;
2522 m_context->setup_data.allow_short_rsa_length = 1;
2523 }
2524 m_context->psk_pki_enabled |= IS_PKI;
2525 if (setup_data->use_cid) {
2526#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2527 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2528#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2529 }
2530 return 1;
2531}
2532
2533int
2535 const char *ca_file,
2536 const char *ca_path) {
2537 coap_mbedtls_context_t *m_context =
2538 ((coap_mbedtls_context_t *)c_context->dtls_context);
2539
2540 if (!m_context) {
2541 coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
2542 "not set up\n");
2543 return 0;
2544 }
2545
2546 if (ca_file == NULL && ca_path == NULL) {
2547 coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path "
2548 "not defined\n");
2549 return 0;
2550 }
2551 if (m_context->root_ca_file) {
2552 mbedtls_free(m_context->root_ca_file);
2553 m_context->root_ca_file = NULL;
2554 }
2555
2556 if (ca_file) {
2557 m_context->root_ca_file = mbedtls_strdup(ca_file);
2558 }
2559
2560 if (m_context->root_ca_path) {
2561 mbedtls_free(m_context->root_ca_path);
2562 m_context->root_ca_path = NULL;
2563 }
2564
2565 if (ca_path) {
2566 m_context->root_ca_path = mbedtls_strdup(ca_path);
2567 }
2568 return 1;
2569}
2570
2571/*
2572 * return 0 failed
2573 * 1 passed
2574 */
2575int
2577 coap_mbedtls_context_t *m_context =
2578 ((coap_mbedtls_context_t *)c_context->dtls_context);
2579
2580 if (!m_context) {
2581 coap_log_warn("coap_context_load_pki_trust_store: (D)TLS environment "
2582 "not set up\n");
2583 return 0;
2584 }
2585 m_context->trust_store_defined = 1;
2586
2587 /* No proper support for this in MbedTLS at this point */
2588 return 1;
2589}
2590
2591
2592int
2594 coap_mbedtls_context_t *m_context =
2595 ((coap_mbedtls_context_t *)c_context->dtls_context);
2596 return m_context->psk_pki_enabled ? 1 : 0;
2597}
2598
2599void
2600coap_dtls_free_context(void *dtls_context) {
2601 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2602 unsigned int i;
2603
2604 for (i = 0; i < m_context->pki_sni_count; i++) {
2605 mbedtls_free(m_context->pki_sni_entry_list[i]->sni);
2606
2607 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i]->public_cert);
2608
2609 mbedtls_pk_free(&m_context->pki_sni_entry_list[i]->private_key);
2610
2611 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i]->cacert);
2612
2613 mbedtls_free(m_context->pki_sni_entry_list[i]);
2614 }
2615 if (m_context->pki_sni_entry_list)
2616 mbedtls_free(m_context->pki_sni_entry_list);
2617
2618 for (i = 0; i < m_context->psk_sni_count; i++) {
2619 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2620 }
2621 if (m_context->psk_sni_entry_list)
2622 mbedtls_free(m_context->psk_sni_entry_list);
2623
2624 if (m_context->root_ca_path)
2625 mbedtls_free(m_context->root_ca_path);
2626 if (m_context->root_ca_file)
2627 mbedtls_free(m_context->root_ca_file);
2628
2629 mbedtls_free(m_context);
2630}
2631
2632#if COAP_CLIENT_SUPPORT
2633void *
2634coap_dtls_new_client_session(coap_session_t *c_session) {
2635#if !defined(MBEDTLS_SSL_CLI_C)
2636 (void)c_session;
2637 coap_log_emerg("coap_dtls_new_client_session:"
2638 " libcoap not compiled for Client Mode for Mbed TLS"
2639 " - update Mbed TLS to include Client Mode\n");
2640 return NULL;
2641#else /* MBEDTLS_SSL_CLI_C */
2642 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2645 int ret;
2646
2647 if (m_env) {
2648 coap_tick_t now;
2649
2650 coap_ticks(&now);
2651 m_env->last_timeout = now;
2652 ret = do_mbedtls_handshake(c_session, m_env);
2653 if (ret == -1) {
2654 coap_dtls_free_mbedtls_env(m_env);
2655 return NULL;
2656 }
2657 }
2658 return m_env;
2659#endif /* MBEDTLS_SSL_CLI_C */
2660}
2661#endif /* COAP_CLIENT_SUPPORT */
2662
2663#if COAP_SERVER_SUPPORT
2664void *
2665coap_dtls_new_server_session(coap_session_t *c_session) {
2666#if !defined(MBEDTLS_SSL_SRV_C)
2667 (void)c_session;
2668 coap_log_emerg("coap_dtls_new_server_session:"
2669 " libcoap not compiled for Server Mode for Mbed TLS"
2670 " - update Mbed TLS to include Server Mode\n");
2671 return NULL;
2672#else /* MBEDTLS_SSL_SRV_C */
2673 coap_mbedtls_env_t *m_env =
2674 (coap_mbedtls_env_t *)c_session->tls;
2675 if (m_env) {
2676#if defined(MBEDTLS_SSL_PROTO_DTLS)
2677#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2678 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2679#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2680#endif /* MBEDTLS_SSL_PROTO_DTLS */
2681 }
2682 return m_env;
2683#endif /* MBEDTLS_SSL_SRV_C */
2684}
2685#endif /* COAP_SERVER_SUPPORT */
2686
2687void
2689 if (c_session && c_session->context && c_session->tls) {
2690 coap_dtls_free_mbedtls_env(c_session->tls);
2691 c_session->tls = NULL;
2693 }
2694 return;
2695}
2696
2697void
2699#if defined(MBEDTLS_SSL_PROTO_DTLS)
2700 coap_mbedtls_env_t *m_env =
2701 (coap_mbedtls_env_t *)c_session->tls;
2702 if (m_env) {
2703#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2704 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2705#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2706 }
2707#else /* ! MBEDTLS_SSL_PROTO_DTLS */
2708 (void)c_session;
2709#endif /* MBEDTLS_SSL_PROTO_DTLS */
2710}
2711
2712ssize_t
2714 const uint8_t *data, size_t data_len) {
2715 int ret;
2716 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2717
2718 if (!m_env) {
2719 errno = ENOTCONN;
2720 return -1;
2721 }
2722 c_session->dtls_event = -1;
2723 coap_log_debug("* %s: dtls: sent %4d bytes\n",
2724 coap_session_str(c_session), (int)data_len);
2725 if (m_env->established) {
2726 ret = mbedtls_ssl_write(&m_env->ssl, (const unsigned char *) data, data_len);
2727 if (ret <= 0) {
2728 switch (ret) {
2729 case MBEDTLS_ERR_SSL_WANT_READ:
2730 case MBEDTLS_ERR_SSL_WANT_WRITE:
2731 ret = 0;
2732 break;
2733 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2735 ret = -1;
2736 break;
2737 default:
2738 coap_log_warn("coap_dtls_send: "
2739 "returned -0x%x: '%s'\n",
2740 -ret, get_error_string(ret));
2741 ret = -1;
2742 break;
2743 }
2744 if (ret == -1) {
2745 coap_log_warn("coap_dtls_send: cannot send PDU\n");
2746 }
2747 }
2748 } else {
2749 ret = do_mbedtls_handshake(c_session, m_env);
2750 if (ret == 1) {
2751 /* Just connected, so send the data */
2752 return coap_dtls_send(c_session, data, data_len);
2753 }
2754 ret = -1;
2755 }
2756
2757 if (c_session->dtls_event >= 0) {
2758 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2759 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2760 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2761 ret = -1;
2762 }
2763 }
2764 return ret;
2765}
2766
2767int
2769 return 0;
2770}
2771
2773coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
2774 return 0;
2775}
2776
2779 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2780#ifdef __ZEPHYR__
2781 int ret = zephyr_timing_get_delay(&m_env->timer);
2782#else
2783 int ret = mbedtls_timing_get_delay(&m_env->timer);
2784#endif
2785 unsigned int scalar = 1 << m_env->retry_scalar;
2786
2787 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2788 switch (ret) {
2789 case 0:
2790 /* int_ms has not timed out */
2791 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2792 /* Need to indicate remaining timeout time */
2793 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2794 }
2795 m_env->last_timeout = now;
2796 /* This may cause a minor extra delay */
2797 return now + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2798 case 1:
2799 /* int_ms has timed out, but not fin_ms */
2800 /*
2801 * Need to make sure that we do not do this too frequently
2802 */
2803 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2804 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2805 }
2806
2807 /* Reset for the next time */
2808 m_env->last_timeout = now;
2809 return now;
2810 case 2:
2811 /* fin_ms has timed out - timed out - one final try */
2812 return now;
2813 default:
2814 break;
2815 }
2816
2817 return 0;
2818}
2819
2820/*
2821 * return 1 timed out
2822 * 0 still timing out
2823 */
2824int
2826 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2827
2828 if (m_env == NULL) {
2829 errno = ENOTCONN;
2830 return 1;
2831 }
2832 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2833 m_env->retry_scalar++;
2834 if ((++c_session->dtls_timeout_count > c_session->max_retransmit) ||
2835 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2836 /* Too many retries */
2838 return 1;
2839 }
2840 return 0;
2841}
2842
2843static void
2844coap_dtls_receive_done(coap_ssl_t *ssl_data, int ret) {
2845 if (ssl_data->pdu_len) {
2846 /* pdu data is held on stack which will not stay there */
2847 coap_log_debug("coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2848 ssl_data->pdu_len = 0;
2849 ssl_data->pdu = NULL;
2850 }
2851}
2852
2853/*
2854 * return +ve data amount
2855 * 0 no more
2856 * -1 error
2857 */
2858int
2860 const uint8_t *data,
2861 size_t data_len) {
2862 int ret = 1;
2863
2864 c_session->dtls_event = -1;
2865 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2866 coap_ssl_t *ssl_data;
2867
2868 if (m_env == NULL) {
2869 errno = ENOTCONN;
2870 return -1;
2871 }
2872
2873 ssl_data = &m_env->coap_ssl_data;
2874 if (ssl_data->pdu_len) {
2875 coap_log_err("** %s: Previous data not read %u bytes\n",
2876 coap_session_str(c_session), ssl_data->pdu_len);
2877 }
2878 ssl_data->pdu = data;
2879 ssl_data->pdu_len = (unsigned)data_len;
2880
2881 if (m_env->established) {
2882#if COAP_CONSTRAINED_STACK
2883 /* pdu can be protected by global_lock if needed */
2884 static uint8_t pdu[COAP_RXBUFFER_SIZE];
2885#else /* ! COAP_CONSTRAINED_STACK */
2886 uint8_t pdu[COAP_RXBUFFER_SIZE];
2887#endif /* ! COAP_CONSTRAINED_STACK */
2888
2889 ret = mbedtls_ssl_read(&m_env->ssl, pdu, sizeof(pdu));
2890 if (ret > 0) {
2891 coap_log_debug("* %s: dtls: recv %4d bytes\n",
2892 coap_session_str(c_session), ret);
2893 coap_dtls_receive_done(ssl_data, ret);
2894 /* May close c_session->tls, so ssl_data is no longer used */
2895 return coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret);
2896 }
2897 switch (ret) {
2898 case 0:
2899 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2900 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2902 break;
2903 case MBEDTLS_ERR_SSL_WANT_READ:
2904 break;
2905 default:
2906 coap_log_warn("coap_dtls_receive: "
2907 "returned -0x%x: '%s' (length %" PRIdS ")\n",
2908 -ret, get_error_string(ret), data_len);
2909 break;
2910 }
2911 ret = -1;
2912 } else {
2913 ret = do_mbedtls_handshake(c_session, m_env);
2914 if (ret == 1) {
2915 /* Just connected, so send the data */
2916 coap_session_connected(c_session);
2917 } else {
2918 if (ssl_data->pdu_len) {
2919 /* Do the handshake again in case of internal timeout */
2920 ret = do_mbedtls_handshake(c_session, m_env);
2921 if (ret == 1) {
2922 /* Just connected, so send the data */
2923 coap_session_connected(c_session);
2924 }
2925 }
2926 ret = -1;
2927 }
2928 }
2929 if (c_session->dtls_event >= 0) {
2930 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2931 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2932 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2934 ssl_data = NULL;
2935 ret = -1;
2936 }
2937 }
2938 if (ssl_data)
2939 coap_dtls_receive_done(ssl_data, ret);
2940 return ret;
2941}
2942
2943#if COAP_SERVER_SUPPORT
2944/*
2945 * return -1 failure
2946 * 0 not completed
2947 * 1 client hello seen
2948 */
2949int
2950coap_dtls_hello(coap_session_t *c_session,
2951 const uint8_t *data,
2952 size_t data_len) {
2953#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2954 (void)c_session;
2955 (void)data;
2956 (void)data_len;
2957 coap_log_emerg("coap_dtls_hello:"
2958 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2959 " - update Mbed TLS to include DTLS and Server Mode\n");
2960 return -1;
2961#else /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
2962 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2963 coap_ssl_t *ssl_data;
2964 int ret;
2965
2966 if (!m_env) {
2967 m_env = coap_dtls_new_mbedtls_env(c_session, COAP_DTLS_ROLE_SERVER,
2969 if (m_env) {
2970 c_session->tls = m_env;
2971 } else {
2972 /* error should have already been reported */
2973 return -1;
2974 }
2975 }
2976
2977 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2978 (unsigned char *)&c_session->addr_info.remote,
2979 sizeof(c_session->addr_info.remote))) != 0) {
2980 coap_log_err("mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2981 -ret, get_error_string(ret));
2982 return -1;
2983 }
2984
2985 ssl_data = &m_env->coap_ssl_data;
2986 if (ssl_data->pdu_len) {
2987 coap_log_err("** %s: Previous data not read %u bytes\n",
2988 coap_session_str(c_session), ssl_data->pdu_len);
2989 }
2990 ssl_data->pdu = data;
2991 ssl_data->pdu_len = (unsigned)data_len;
2992
2993 ret = do_mbedtls_handshake(c_session, m_env);
2994 if (ret == 0 || m_env->seen_client_hello) {
2995 /* The test for seen_client_hello gives the ability to setup a new
2996 c_session to continue the do_mbedtls_handshake past the client hello
2997 and safely allow updating of the m_env and separately
2998 letting a new session cleanly start up.
2999 */
3000 m_env->seen_client_hello = 0;
3001 ret = 1;
3002 } else {
3003 ret = 0;
3004 }
3005
3006 if (ssl_data->pdu_len) {
3007 /* pdu data is held on stack which will not stay there */
3008 coap_log_debug("coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
3009 ssl_data->pdu_len = 0;
3010 ssl_data->pdu = NULL;
3011 }
3012 return ret;
3013#endif /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
3014}
3015#endif /* COAP_SERVER_SUPPORT */
3016
3017unsigned int
3019 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3020 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
3021
3022 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
3023 return 13 + 8 + 8;
3024 }
3025 return expansion;
3026}
3027
3028#if !COAP_DISABLE_TCP
3029#if COAP_CLIENT_SUPPORT
3030void *
3031coap_tls_new_client_session(coap_session_t *c_session) {
3032#if !defined(MBEDTLS_SSL_CLI_C)
3033 (void)c_session;
3034 *connected = 0;
3035 coap_log_emerg("coap_tls_new_client_session:"
3036 " libcoap not compiled for Client Mode for Mbed TLS"
3037 " - update Mbed TLS to include Client Mode\n");
3038 return NULL;
3039#else /* MBEDTLS_SSL_CLI_C */
3040 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
3043 coap_tick_t now;
3044 coap_ticks(&now);
3045
3046 if (!m_env)
3047 return NULL;
3048
3049 m_env->last_timeout = now;
3050 c_session->tls = m_env;
3051 do_mbedtls_handshake(c_session, m_env);
3052 return m_env;
3053#endif /* MBEDTLS_SSL_CLI_C */
3054}
3055#endif /* COAP_CLIENT_SUPPORT */
3056
3057#if COAP_SERVER_SUPPORT
3058void *
3059coap_tls_new_server_session(coap_session_t *c_session) {
3060#if !defined(MBEDTLS_SSL_SRV_C)
3061 (void)c_session;
3062 (void)connected;
3063
3064 coap_log_emerg("coap_tls_new_server_session:"
3065 " libcoap not compiled for Server Mode for Mbed TLS"
3066 " - update Mbed TLS to include Server Mode\n");
3067 return NULL;
3068#else /* MBEDTLS_SSL_SRV_C */
3069 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
3072
3073 if (!m_env)
3074 return NULL;
3075
3076 c_session->tls = m_env;
3077 do_mbedtls_handshake(c_session, m_env);
3078 return m_env;
3079#endif /* MBEDTLS_SSL_SRV_C */
3080}
3081#endif /* COAP_SERVER_SUPPORT */
3082
3083void
3085 coap_dtls_free_session(c_session);
3086 return;
3087}
3088
3089/*
3090 * strm
3091 * return +ve Number of bytes written.
3092 * -1 Error (error in errno).
3093 */
3094ssize_t
3095coap_tls_write(coap_session_t *c_session, const uint8_t *data,
3096 size_t data_len) {
3097 int ret = 0;
3098 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3099 size_t amount_sent = 0;
3100
3101 if (!m_env) {
3102 errno = ENOTCONN;
3103 return -1;
3104 }
3105 c_session->dtls_event = -1;
3106 if (m_env->established) {
3107 while (amount_sent < data_len) {
3108 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
3109 data_len - amount_sent);
3110 if (ret <= 0) {
3111 switch (ret) {
3112 case MBEDTLS_ERR_SSL_WANT_READ:
3113 case MBEDTLS_ERR_SSL_WANT_WRITE:
3114 if (amount_sent)
3115 ret = amount_sent;
3116 else
3117 ret = 0;
3118 c_session->sock.flags |= COAP_SOCKET_WANT_WRITE;
3119 break;
3120 case MBEDTLS_ERR_NET_CONN_RESET:
3121 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
3123 break;
3124 default:
3125 coap_log_warn("coap_tls_write: "
3126 "returned -0x%x: '%s'\n",
3127 -ret, get_error_string(ret));
3128 ret = -1;
3129 break;
3130 }
3131 if (ret == -1) {
3132 coap_log_warn("coap_tls_write: cannot send PDU\n");
3133 }
3134 break;
3135 }
3136 amount_sent += ret;
3137 /* Report all TLS records written, not just the final record. */
3138 ret = (int)amount_sent;
3139 }
3140 } else {
3141 ret = do_mbedtls_handshake(c_session, m_env);
3142 if (ret != 1) {
3143 ret = -1;
3144 }
3145 }
3146
3147 if (c_session->dtls_event >= 0) {
3148 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3149 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3150 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3151 ret = -1;
3152 }
3153 }
3154 if (ret > 0) {
3155 if (ret == (ssize_t)data_len)
3156 coap_log_debug("* %s: tls: sent %4d bytes\n",
3157 coap_session_str(c_session), ret);
3158 else
3159 coap_log_debug("* %s: tls: sent %4d of %4" PRIdS " bytes\n",
3160 coap_session_str(c_session), ret, data_len);
3161 }
3162 return ret;
3163}
3164
3165/*
3166 * strm
3167 * return >=0 Number of bytes read.
3168 * -1 Error (error in errno).
3169 */
3170ssize_t
3171coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) {
3172 int ret = -1;
3173
3174 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3175
3176 if (!m_env) {
3177 errno = ENOTCONN;
3178 return -1;
3179 }
3180
3181 c_session->dtls_event = -1;
3182
3183 if (!m_env->established && !m_env->sent_alert) {
3184 ret = do_mbedtls_handshake(c_session, m_env);
3185 }
3186
3187 if (c_session->state != COAP_SESSION_STATE_NONE && m_env->established) {
3188 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
3189 if (ret <= 0) {
3190 switch (ret) {
3191 case 0:
3192 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3194 ret = -1;
3195 break;
3196 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
3197 /* Stop the sending of an alert on closedown */
3198 m_env->sent_alert = 1;
3200 break;
3201#if MBEDTLS_VERSION_NUMBER >= 0x03060000
3202 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
3203#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
3204 case MBEDTLS_ERR_SSL_WANT_READ:
3205 errno = EAGAIN;
3206 ret = 0;
3207 break;
3208 default:
3209 coap_log_warn("coap_tls_read: "
3210 "returned -0x%x: '%s' (length %" PRIdS ")\n",
3211 -ret, get_error_string(ret), data_len);
3212 ret = -1;
3213 break;
3214 }
3215 } else if (ret < (int)data_len) {
3216 c_session->sock.flags &= ~COAP_SOCKET_CAN_READ;
3217 }
3218 }
3219
3220 if (c_session->dtls_event >= 0) {
3221 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3222 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3223 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3225 ret = -1;
3226 }
3227 }
3228 if (ret > 0) {
3229 coap_log_debug("* %s: tls: recv %4d bytes\n",
3230 coap_session_str(c_session), ret);
3231 }
3232 return ret;
3233}
3234#endif /* !COAP_DISABLE_TCP */
3235
3236void
3237coap_dtls_startup(void) {
3238#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
3239 psa_crypto_init();
3240#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
3241}
3242
3243void
3244coap_dtls_shutdown(void) {
3245#if COAP_CLIENT_SUPPORT
3246 mbedtls_free(psk_ciphers);
3247 mbedtls_free(pki_ciphers);
3248 mbedtls_free(ecjpake_ciphers);
3249 psk_ciphers = NULL;
3250 pki_ciphers = NULL;
3251 ecjpake_ciphers = NULL;
3252 processed_ciphers = 0;
3253#endif /* COAP_CLIENT_SUPPORT */
3255#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
3256 mbedtls_psa_crypto_free();
3257#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
3258}
3259
3260
3261void *
3262coap_dtls_get_tls(const coap_session_t *c_session,
3263 coap_tls_library_t *tls_lib) {
3264 if (tls_lib)
3265 *tls_lib = COAP_TLS_LIBRARY_MBEDTLS;
3266 if (c_session && c_session->tls) {
3267 coap_mbedtls_env_t *m_env;
3268
3269 /* To get around const issue */
3270 memcpy(&m_env, &c_session->tls, sizeof(m_env));
3271
3272 return (void *)&m_env->ssl;
3273 }
3274 return NULL;
3275}
3276
3277static coap_log_t keep_log_level = COAP_LOG_EMERG;
3278
3279void
3281#if !defined(ESPIDF_VERSION)
3282 int use_level;
3283 /*
3284 * Mbed TLS debug levels filter
3285 * 0 No debug
3286 * 1 Error
3287 * 2 State change
3288 * 3 Informational
3289 * 4 Verbose
3290 */
3291 switch ((int)level) {
3292 case COAP_LOG_EMERG:
3293 use_level = 0;
3294 break;
3295 case COAP_LOG_ALERT:
3296 case COAP_LOG_CRIT:
3297 case COAP_LOG_ERR:
3298 case COAP_LOG_WARN:
3299 use_level = 1;
3300 break;
3301 case COAP_LOG_NOTICE:
3302 use_level = 2;
3303 break;
3304 case COAP_LOG_INFO:
3305 use_level = 3;
3306 break;
3307 case COAP_LOG_DEBUG:
3308 default:
3309 use_level = 4;
3310 break;
3311 }
3312 mbedtls_debug_set_threshold(use_level);
3313#endif /* !ESPIDF_VERSION) */
3314 keep_log_level = level;
3315}
3316
3319 return keep_log_level;
3320}
3321
3322#endif /* COAP_WITH_LIBMBEDTLS */
3323
3324#if COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE
3325
3326#if COAP_SERVER_SUPPORT
3327coap_digest_ctx_t *
3328coap_digest_setup(void) {
3329 coap_crypto_sha256_ctx_t *digest_ctx = mbedtls_malloc(sizeof(coap_crypto_sha256_ctx_t));
3330
3331 if (digest_ctx) {
3332 if (coap_crypto_sha256_init(digest_ctx) != 0) {
3333 coap_digest_free(digest_ctx);
3334 return NULL;
3335 }
3336 }
3337 return digest_ctx;
3338}
3339
3340void
3341coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3342 if (digest_ctx) {
3343 coap_crypto_sha256_free(digest_ctx);
3344 mbedtls_free(digest_ctx);
3345 }
3346}
3347
3348int
3349coap_digest_update(coap_digest_ctx_t *digest_ctx,
3350 const uint8_t *data,
3351 size_t data_len) {
3352 return coap_crypto_sha256_update(digest_ctx, data, data_len) == 0;
3353}
3354
3355int
3356coap_digest_final(coap_digest_ctx_t *digest_ctx,
3357 coap_digest_t *digest_buffer) {
3358 int ret = coap_crypto_sha256_finish(digest_ctx, (uint8_t *)digest_buffer) == 0;
3359 coap_digest_free(digest_ctx);
3360 return ret;
3361}
3362#endif /* COAP_SERVER_SUPPORT */
3363
3364#if COAP_WS_SUPPORT
3365int
3367 const coap_bin_const_t *data,
3368 coap_bin_const_t **hash) {
3369 coap_crypto_md_type_t md_type;
3370 size_t hash_len;
3371
3372 switch ((int)alg) {
3374 md_type = COAP_CRYPTO_MD_SHA1;
3375 break;
3377 md_type = COAP_CRYPTO_MD_SHA256;
3378 break;
3380 md_type = COAP_CRYPTO_MD_SHA512;
3381 break;
3382 default:
3383 coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg);
3384 return 0;
3385 }
3386
3387 hash_len = coap_crypto_hash_size(md_type);
3388 if (hash_len == 0)
3389 return 0;
3390
3391 coap_binary_t *dummy = coap_new_binary(hash_len);
3392 if (dummy == NULL)
3393 return 0;
3394
3395 if (coap_crypto_hash_compute(md_type, data->s, data->length,
3396 dummy->s, hash_len, NULL) != 0) {
3398 return 0;
3399 }
3400
3401 *hash = (coap_bin_const_t *)dummy;
3402 return 1;
3403}
3404#endif /* COAP_WS_SUPPORT */
3405
3406/* HMAC Functions */
3407
3408#if COAP_OSCORE_SUPPORT
3409static int
3410coap_crypto_hmac_compute(coap_crypto_md_type_t md_type,
3411 const uint8_t *key, size_t key_len,
3412 const uint8_t *input, size_t ilen,
3413 uint8_t *output, size_t output_size,
3414 size_t *output_len) {
3415#if COAP_USE_PSA_CRYPTO
3416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3417 psa_key_id_t key_id;
3418 psa_algorithm_t psa_alg = PSA_ALG_HMAC(md_type);
3419 size_t mac_len;
3420 psa_status_t status;
3421
3422 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3424 psa_set_key_algorithm(&attributes, psa_alg);
3425
3426 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3427 return -1;
3428 }
3429
3430 status = psa_mac_compute(key_id, psa_alg, input, ilen,
3431 output, output_size, &mac_len);
3432 psa_destroy_key(key_id);
3433
3434 if (output_len)
3435 *output_len = mac_len;
3436 return (status == PSA_SUCCESS) ? 0 : -1;
3437#else
3438 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
3439 if (!md_info)
3440 return -1;
3441 size_t mac_size = mbedtls_md_get_size(md_info);
3442 if (mac_size > output_size)
3443 return -1;
3444 if (output_len)
3445 *output_len = mac_size;
3446 return mbedtls_md_hmac(md_info, key, key_len, input, ilen, output);
3447#endif
3448}
3449
3450/* AEAD (AES-CCM) Functions */
3451
3452static int
3453coap_crypto_aead_encrypt_ccm(const uint8_t *key, size_t key_len,
3454 const uint8_t *nonce, size_t nonce_len,
3455 const uint8_t *aad, size_t aad_len,
3456 const uint8_t *plaintext, size_t plaintext_len,
3457 uint8_t *ciphertext, size_t ciphertext_size,
3458 size_t *ciphertext_len, size_t tag_len) {
3459#if COAP_USE_PSA_CRYPTO
3460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3461 psa_key_id_t key_id;
3462 psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len);
3463 size_t output_len;
3464 psa_status_t status;
3465
3466 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
3467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3468 psa_set_key_algorithm(&attributes, psa_alg);
3469 psa_set_key_bits(&attributes, key_len * 8);
3470
3471 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3472 return -1;
3473 }
3474
3475 status = psa_aead_encrypt(key_id, psa_alg,
3476 nonce, nonce_len,
3477 aad, aad_len,
3478 plaintext, plaintext_len,
3479 ciphertext, ciphertext_size, &output_len);
3480 psa_destroy_key(key_id);
3481
3482 if (ciphertext_len)
3483 *ciphertext_len = output_len;
3484 return (status == PSA_SUCCESS) ? 0 : -1;
3485#else
3486 mbedtls_cipher_context_t ctx;
3487 const mbedtls_cipher_info_t *cipher_info;
3488 mbedtls_cipher_type_t cipher_type;
3489 int ret = -1;
3490 size_t result_len = ciphertext_size;
3491
3492 if (key_len == 16) {
3493 cipher_type = MBEDTLS_CIPHER_AES_128_CCM;
3494 } else if (key_len == 32) {
3495 cipher_type = MBEDTLS_CIPHER_AES_256_CCM;
3496 } else {
3497 return -1;
3498 }
3499
3500 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3501 if (!cipher_info)
3502 return -1;
3503
3504 mbedtls_cipher_init(&ctx);
3505 if (mbedtls_cipher_setup(&ctx, cipher_info) != 0)
3506 goto cleanup;
3507 if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_ENCRYPT) != 0)
3508 goto cleanup;
3509
3510#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3511 {
3512 unsigned char tag[16];
3513 if (mbedtls_cipher_auth_encrypt(&ctx,
3514 nonce, nonce_len,
3515 aad, aad_len,
3516 plaintext, plaintext_len,
3517 ciphertext, &result_len,
3518 tag, tag_len) != 0) {
3519 goto cleanup;
3520 }
3521 if ((result_len + tag_len) > ciphertext_size)
3522 goto cleanup;
3523 memcpy(ciphertext + result_len, tag, tag_len);
3524 result_len += tag_len;
3525 }
3526#else
3527 if (mbedtls_cipher_auth_encrypt_ext(&ctx,
3528 nonce, nonce_len,
3529 aad, aad_len,
3530 plaintext, plaintext_len,
3531 ciphertext, ciphertext_size,
3532 &result_len, tag_len) != 0) {
3533 goto cleanup;
3534 }
3535#endif
3536
3537 if (ciphertext_len)
3538 *ciphertext_len = result_len;
3539 ret = 0;
3540
3541cleanup:
3542 mbedtls_cipher_free(&ctx);
3543 return ret;
3544#endif
3545}
3546
3547static int
3548coap_crypto_aead_decrypt_ccm(const uint8_t *key, size_t key_len,
3549 const uint8_t *nonce, size_t nonce_len,
3550 const uint8_t *aad, size_t aad_len,
3551 const uint8_t *ciphertext, size_t ciphertext_len,
3552 uint8_t *plaintext, size_t plaintext_size,
3553 size_t *plaintext_len, size_t tag_len) {
3554#if COAP_USE_PSA_CRYPTO
3555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3556 psa_key_id_t key_id;
3557 psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len);
3558 size_t output_len;
3559 psa_status_t status;
3560
3561 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
3562 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3563 psa_set_key_algorithm(&attributes, psa_alg);
3564 psa_set_key_bits(&attributes, key_len * 8);
3565
3566 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3567 return -1;
3568 }
3569
3570 status = psa_aead_decrypt(key_id, psa_alg,
3571 nonce, nonce_len,
3572 aad, aad_len,
3573 ciphertext, ciphertext_len,
3574 plaintext, plaintext_size, &output_len);
3575 psa_destroy_key(key_id);
3576
3577 if (plaintext_len)
3578 *plaintext_len = output_len;
3579 return (status == PSA_SUCCESS) ? 0 : -1;
3580#else
3581 mbedtls_cipher_context_t ctx;
3582 const mbedtls_cipher_info_t *cipher_info;
3583 mbedtls_cipher_type_t cipher_type;
3584 int ret = -1;
3585 size_t result_len = plaintext_size;
3586
3587 if (key_len == 16) {
3588 cipher_type = MBEDTLS_CIPHER_AES_128_CCM;
3589 } else if (key_len == 32) {
3590 cipher_type = MBEDTLS_CIPHER_AES_256_CCM;
3591 } else {
3592 return -1;
3593 }
3594
3595 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3596 if (!cipher_info)
3597 return -1;
3598
3599 mbedtls_cipher_init(&ctx);
3600 if (mbedtls_cipher_setup(&ctx, cipher_info) != 0)
3601 goto cleanup;
3602 if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_DECRYPT) != 0)
3603 goto cleanup;
3604
3605#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3606 {
3607 const unsigned char *tag = ciphertext + ciphertext_len - tag_len;
3608 if (mbedtls_cipher_auth_decrypt(&ctx,
3609 nonce, nonce_len,
3610 aad, aad_len,
3611 ciphertext, ciphertext_len - tag_len,
3612 plaintext, &result_len,
3613 tag, tag_len) != 0) {
3614 goto cleanup;
3615 }
3616 }
3617#else
3618 if (mbedtls_cipher_auth_decrypt_ext(&ctx,
3619 nonce, nonce_len,
3620 aad, aad_len,
3621 ciphertext, ciphertext_len,
3622 plaintext, plaintext_size,
3623 &result_len, tag_len) != 0) {
3624 goto cleanup;
3625 }
3626#endif
3627
3628 if (plaintext_len)
3629 *plaintext_len = result_len;
3630 ret = 0;
3631
3632cleanup:
3633 mbedtls_cipher_free(&ctx);
3634 return ret;
3635#endif
3636}
3637
3638/* End of Crypto Abstraction Functions */
3639
3640int
3642 return 1;
3643}
3644
3645/*
3646 * Helper to check if COSE cipher algorithm is supported and get key size.
3647 */
3648static int
3649get_cipher_key_size(cose_alg_t alg, size_t *key_size) {
3650 switch ((int)alg) {
3652 if (key_size)
3653 *key_size = 16;
3654 return 1;
3656 if (key_size)
3657 *key_size = 32;
3658 return 1;
3659 default:
3660 coap_log_debug("get_cipher_key_size: COSE cipher %d not supported\n", alg);
3661 return 0;
3662 }
3663}
3664
3665/*
3666 * Helper to get HMAC algorithm parameters from COSE HMAC algorithm.
3667 */
3668static int
3669get_hmac_params(cose_hmac_alg_t hmac_alg,
3670 coap_crypto_md_type_t *md_type,
3671 size_t *mac_len) {
3672 switch ((int)hmac_alg) {
3674 if (md_type)
3675 *md_type = COAP_CRYPTO_MD_SHA256;
3676 if (mac_len)
3677 *mac_len = 32;
3678 return 1;
3680 if (md_type)
3681 *md_type = COAP_CRYPTO_MD_SHA384;
3682 if (mac_len)
3683 *mac_len = 48;
3684 return 1;
3686 if (md_type)
3687 *md_type = COAP_CRYPTO_MD_SHA512;
3688 if (mac_len)
3689 *mac_len = 64;
3690 return 1;
3691 default:
3692 coap_log_debug("get_hmac_params: COSE HMAC %d not supported\n", hmac_alg);
3693 return 0;
3694 }
3695}
3696
3697int
3699 return get_cipher_key_size(alg, NULL);
3700}
3701
3702int
3704 cose_hmac_alg_t hmac_alg;
3705
3706 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
3707 return 0;
3708 return get_hmac_params(hmac_alg, NULL, NULL);
3709}
3710
3711int
3713 coap_bin_const_t *data,
3714 coap_bin_const_t *aad,
3715 uint8_t *result,
3716 size_t *max_result_len) {
3717 const coap_crypto_aes_ccm_t *ccm;
3718
3719 if (data == NULL)
3720 return 0;
3721
3722 assert(params != NULL);
3723
3724 if (!params) {
3725 return 0;
3726 }
3727
3728 if (!get_cipher_key_size(params->alg, NULL)) {
3729 coap_log_debug("coap_crypto_aead_encrypt: algorithm %d not supported\n",
3730 params->alg);
3731 return 0;
3732 }
3733
3734 ccm = &params->params.aes;
3735
3736 if (coap_crypto_aead_encrypt_ccm(ccm->key.s, ccm->key.length,
3737 ccm->nonce, 15 - ccm->l,
3738 aad ? aad->s : NULL, aad ? aad->length : 0,
3739 data->s, data->length,
3740 result, *max_result_len, max_result_len,
3741 ccm->tag_len) != 0) {
3742 coap_log_debug("coap_crypto_aead_encrypt: encryption failed\n");
3743 return 0;
3744 }
3745
3746 return 1;
3747}
3748
3749int
3751 coap_bin_const_t *data,
3752 coap_bin_const_t *aad,
3753 uint8_t *result,
3754 size_t *max_result_len) {
3755 const coap_crypto_aes_ccm_t *ccm;
3756
3757 if (data == NULL)
3758 return 0;
3759
3760 assert(params != NULL);
3761
3762 if (!params) {
3763 return 0;
3764 }
3765
3766 if (!get_cipher_key_size(params->alg, NULL)) {
3767 coap_log_debug("coap_crypto_aead_decrypt: algorithm %d not supported\n",
3768 params->alg);
3769 return 0;
3770 }
3771
3772 ccm = &params->params.aes;
3773
3774 if (data->length < ccm->tag_len) {
3775 coap_log_err("coap_decrypt: invalid tag length\n");
3776 return 0;
3777 }
3778
3779 if (coap_crypto_aead_decrypt_ccm(ccm->key.s, ccm->key.length,
3780 ccm->nonce, 15 - ccm->l,
3781 aad ? aad->s : NULL, aad ? aad->length : 0,
3782 data->s, data->length,
3783 result, *max_result_len, max_result_len,
3784 ccm->tag_len) != 0) {
3785 coap_log_debug("coap_crypto_aead_decrypt: decryption failed\n");
3786 return 0;
3787 }
3788
3789 return 1;
3790}
3791
3792int
3794 coap_bin_const_t *key,
3795 coap_bin_const_t *data,
3796 coap_bin_const_t **hmac) {
3797 coap_crypto_md_type_t md_type;
3798 size_t mac_len;
3800
3801 assert(key);
3802 assert(data);
3803 assert(hmac);
3804
3805 if (!get_hmac_params(hmac_alg, &md_type, &mac_len)) {
3806 coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3807 return 0;
3808 }
3809
3810 dummy = coap_new_binary(mac_len);
3811 if (dummy == NULL)
3812 return 0;
3813
3814 if (coap_crypto_hmac_compute(md_type, key->s, key->length,
3815 data->s, data->length,
3816 dummy->s, mac_len, NULL) != 0) {
3817 coap_log_debug("coap_crypto_hmac: computation failed\n");
3819 return 0;
3820 }
3821
3822 *hmac = (coap_bin_const_t *)dummy;
3823 return 1;
3824}
3825
3826#endif /* COAP_OSCORE_SUPPORT */
3827
3828#else /* ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE*/
3829
3830#ifdef __clang__
3831/* Make compilers happy that do not like empty modules. As this function is
3832 * never used, we ignore -Wunused-function at the end of compiling this file
3833 */
3834#pragma GCC diagnostic ignored "-Wunused-function"
3835#endif
3836static inline void
3837dummy(void) {
3838}
3839
3840#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:5396
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:3117
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::@052031327027255251031165205274021151110332073203 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
union coap_dtls_key_t::@021233120343020007351201301252264260236226063277 key
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
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