libcoap 4.3.5-develop-5f2ea98
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#if !COAP_USE_PSA_CRYPTO
345#ifndef MBEDTLS_2_X_COMPAT
346/*
347 * mbedtls_ callback functions expect 0 on success, -ve on failure.
348 */
349static int
350coap_rng(void *ctx COAP_UNUSED, unsigned char *buf, size_t len) {
351 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
352}
353#endif /* MBEDTLS_2_X_COMPAT */
354#endif /* COAP_USE_PSA_CRYPTO */
355
356#endif /* COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE */
357
358#if COAP_WITH_LIBMBEDTLS
359
360#define IS_PSK (1 << 0)
361#define IS_PKI (1 << 1)
362#define IS_CLIENT (1 << 6)
363#define IS_SERVER (1 << 7)
364
365typedef struct coap_ssl_t {
366 const uint8_t *pdu;
367 unsigned pdu_len;
368 unsigned peekmode;
369} coap_ssl_t;
370
371/*
372 * This structure encapsulates the Mbed TLS session object.
373 * It handles both TLS and DTLS.
374 * c_session->tls points to this.
375 */
376typedef struct coap_mbedtls_env_t {
377 mbedtls_ssl_context ssl;
378#if !COAP_USE_PSA_CRYPTO
379 mbedtls_entropy_context entropy;
380 mbedtls_ctr_drbg_context ctr_drbg;
381#endif /* !COAP_USE_PSA_CRYPTO */
382 mbedtls_ssl_config conf;
383 mbedtls_timing_delay_context timer;
384 mbedtls_x509_crt cacert;
385 mbedtls_x509_crt public_cert;
386 mbedtls_pk_context private_key;
387 mbedtls_ssl_cookie_ctx cookie_ctx;
388 /* If not set, need to do do_mbedtls_handshake */
389 int established;
390 int sent_alert;
391 int seen_client_hello;
392 int ec_jpake;
393 coap_tick_t last_timeout;
394 unsigned int retry_scalar;
395 coap_ssl_t coap_ssl_data;
396 uint32_t server_hello_cnt;
397} coap_mbedtls_env_t;
398
399typedef struct pki_sni_entry {
400 char *sni;
401 coap_dtls_key_t pki_key;
402 mbedtls_x509_crt cacert;
403 mbedtls_x509_crt public_cert;
404 mbedtls_pk_context private_key;
405} pki_sni_entry;
406
407typedef struct psk_sni_entry {
408 char *sni;
409 coap_dtls_spsk_info_t psk_info;
410} psk_sni_entry;
411
412typedef struct coap_mbedtls_context_t {
413 coap_dtls_pki_t setup_data;
414 size_t pki_sni_count;
415 /* Entries are not moved, as handshakes refer to their certificates */
416 pki_sni_entry **pki_sni_entry_list;
417 size_t psk_sni_count;
418 psk_sni_entry *psk_sni_entry_list;
419 char *root_ca_file;
420 char *root_ca_path;
421 int trust_store_defined;
422 int psk_pki_enabled;
423} coap_mbedtls_context_t;
424
425typedef enum coap_enc_method_t {
426 COAP_ENC_PSK,
427 COAP_ENC_PKI,
428 COAP_ENC_ECJPAKE,
429} coap_enc_method_t;
430
431#ifdef __ZEPHYR__
432
433typedef struct {
434 uint32_t start_time;
435 uint32_t int_time;
436 uint32_t fin_time;
437} zephyr_timing_delay_context;
438
439static void
440zephyr_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) {
441 zephyr_timing_delay_context *ctx = (zephyr_timing_delay_context *)data;
442
443 if (ctx == NULL) {
444 return;
445 }
446
447 ctx->start_time = k_uptime_get_32();
448
449 if (fin_ms != 0) {
450 ctx->int_time = ctx->start_time + int_ms;
451 ctx->fin_time = ctx->start_time + fin_ms;
452 } else {
453 ctx->int_time = 0;
454 ctx->fin_time = 0;
455 }
456}
457
458static int
459zephyr_timing_get_delay(void *data) {
460 zephyr_timing_delay_context *ctx = (zephyr_timing_delay_context *)data;
461 uint32_t now;
462
463 if (ctx == NULL || ctx->fin_time == 0) {
464 return -1;
465 }
466
467 now = k_uptime_get_32();
468
469 if (now >= ctx->fin_time) {
470 return 2;
471 }
472
473 if (now >= ctx->int_time) {
474 return 1;
475 }
476
477 return 0;
478}
479
480#endif /* __ZEPHYR__ */
481
482static int
483coap_dgram_read(void *ctx, unsigned char *out, size_t outl) {
484 ssize_t ret = 0;
485 coap_session_t *c_session = (coap_session_t *)ctx;
486 coap_ssl_t *data;
487
488 if (!c_session->tls) {
489 errno = EAGAIN;
490 return MBEDTLS_ERR_SSL_WANT_READ;
491 }
492 data = &((coap_mbedtls_env_t *)c_session->tls)->coap_ssl_data;
493
494 if (out != NULL) {
495 if (data->pdu_len > 0) {
496 if (outl < data->pdu_len) {
497 memcpy(out, data->pdu, outl);
498 ret = outl;
499 data->pdu += outl;
500 data->pdu_len -= outl;
501 } else {
502 memcpy(out, data->pdu, data->pdu_len);
503 ret = data->pdu_len;
504 if (!data->peekmode) {
505 data->pdu_len = 0;
506 data->pdu = NULL;
507 }
508 }
509 } else {
510 ret = MBEDTLS_ERR_SSL_WANT_READ;
511 errno = EAGAIN;
512 }
513 }
514 return ret;
515}
516
517/*
518 * return +ve data amount
519 * 0 no more
520 * -ve Mbed TLS error
521 */
522/* callback function given to mbedtls for sending data over socket */
523static int
524coap_dgram_write(void *ctx, const unsigned char *send_buffer,
525 size_t send_buffer_length) {
526 ssize_t result = -1;
527 coap_session_t *c_session = (coap_session_t *)ctx;
528
529 if (c_session) {
530 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
531
532 if (!coap_netif_available(c_session)
534 && c_session->endpoint == NULL
535#endif /* COAP_SERVER_SUPPORT */
536 ) {
537 /* socket was closed on client due to error */
538 errno = ECONNRESET;
539 return -1;
540 }
541 result = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
542 send_buffer, send_buffer_length);
543 if (result != (ssize_t)send_buffer_length) {
544 int keep_errno = errno;
545
546 coap_log_warn("coap_netif_dgrm_write failed (%" PRIdS " != %" PRIuS ")\n",
547 result, send_buffer_length);
548 errno = keep_errno;
549 if (result < 0) {
550 if (errno == ENOTCONN || errno == ECONNREFUSED)
552 return -1;
553 } else {
554 result = 0;
555 }
556 } else if (m_env) {
557 coap_tick_t now;
558 coap_ticks(&now);
559 m_env->last_timeout = now;
560 }
561 } else {
562 result = 0;
563 }
564 return result;
565}
566
567#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
568/*
569 * Server side PSK callback
570 */
571static int
572psk_server_callback(void *p_info, mbedtls_ssl_context *ssl,
573 const unsigned char *identity, size_t identity_len) {
574 coap_session_t *c_session = (coap_session_t *)p_info;
575 coap_dtls_spsk_t *setup_data;
576 coap_mbedtls_env_t *m_env;
577 coap_bin_const_t lidentity;
578 const coap_bin_const_t *psk_key;
579
580 if (c_session == NULL)
581 return -1;
582
583 /* Track the Identity being used */
584 lidentity.s = identity ? (const uint8_t *)identity : (const uint8_t *)"";
585 lidentity.length = identity ? identity_len : 0;
586 coap_session_refresh_psk_identity(c_session, &lidentity);
587
588 coap_log_debug("got psk_identity: '%.*s'\n",
589 (int)lidentity.length, (const char *)lidentity.s);
590
591 m_env = (coap_mbedtls_env_t *)c_session->tls;
592 setup_data = &c_session->context->spsk_setup_data;
593
594 if (setup_data->validate_id_call_back) {
595 psk_key = setup_data->validate_id_call_back(&lidentity,
596 c_session,
597 setup_data->id_call_back_arg);
598
599 coap_session_refresh_psk_key(c_session, psk_key);
600 } else {
601 psk_key = coap_get_session_server_psk_key(c_session);
602 }
603
604 if (psk_key == NULL)
605 return -1;
606 mbedtls_ssl_set_hs_psk(ssl, psk_key->s, psk_key->length);
607 m_env->seen_client_hello = 1;
608 return 0;
609}
610#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED && MBEDTLS_SSL_SRV_C */
611
612static char *
613get_san_or_cn_from_cert(coap_session_t *session, mbedtls_x509_crt *crt, const char *sni_match) {
614 if (crt) {
615 const mbedtls_asn1_named_data *cn_data;
616
617 (void)session;
618 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
619 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
620 while (seq) {
621 while (seq->buf.p == NULL) {
622 seq = seq->next;
623 }
624 if (seq) {
625 if (sni_match) {
626 if (strlen(sni_match) != seq->buf.len ||
627 memcmp(seq->buf.p, sni_match, seq->buf.len)) {
628 seq = seq->next;
629 continue;
630 }
631 }
632 /* Return the Subject Alt Name */
633 return mbedtls_strndup((const char *)seq->buf.p,
634 seq->buf.len);
635 }
636 }
637 }
638
639 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
640 MBEDTLS_OID_AT_CN,
641 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
642 if (cn_data) {
643 if (sni_match) {
644 if (!coap_pki_name_match_sni((char *)cn_data->val.p, cn_data->val.len, sni_match)) {
645 coap_log_debug(" %s: got CN '%*.*s'\n",
646 coap_session_str(session), (int)cn_data->val.len,
647 (int)cn_data->val.len, cn_data->val.p);
648 goto no_match;
649 }
650 }
651 /* Return the Common Name */
652 return mbedtls_strndup((const char *)cn_data->val.p,
653 cn_data->val.len);
654 }
655no_match:
656 if (!sni_match) {
657 return NULL;
658 }
659 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
660 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
661 int n = 0;
662
663 while (seq) {
664 while (seq->buf.p == NULL) {
665 seq = seq->next;
666 }
667 if (seq) {
668 coap_log_debug(" %s: got DNS.%d '%*.*s'\n",
669 coap_session_str(session), n + 1, (int)seq->buf.len,
670 (int)seq->buf.len, seq->buf.p);
671 seq = seq->next;
672 n++;
673 continue;
674 }
675 }
676 }
677 }
678 return NULL;
679}
680
681#if COAP_MAX_LOGGING_LEVEL > 0
682static char *
683get_error_string(int ret) {
684 static char buf[128] = {0};
685 mbedtls_strerror(ret, buf, sizeof(buf)-1);
686 return buf;
687}
688#endif /* COAP_MAX_LOGGING_LEVEL */
689
690static int
691self_signed_cert_verify_callback_mbedtls(void *data,
692 mbedtls_x509_crt *crt COAP_UNUSED,
693 int depth COAP_UNUSED,
694 uint32_t *flags) {
695 const coap_session_t *c_session = (coap_session_t *)data;
696 const coap_mbedtls_context_t *m_context =
697 (coap_mbedtls_context_t *)c_session->context->dtls_context;
698 const coap_dtls_pki_t *setup_data = &m_context->setup_data;
699
700 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
701 if (setup_data->allow_expired_certs) {
702 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
703 }
704 }
705 return 0;
706}
707
708/*
709 * return 0 All OK
710 * -ve Error Code
711 */
712static int
713cert_verify_callback_mbedtls(void *data, mbedtls_x509_crt *crt,
714 int depth, uint32_t *flags) {
715 coap_session_t *c_session = (coap_session_t *)data;
716 coap_mbedtls_context_t *m_context =
717 (coap_mbedtls_context_t *)c_session->context->dtls_context;
718 coap_dtls_pki_t *setup_data = &m_context->setup_data;
719 char *cn = NULL;
720
721 if (!setup_data->allow_sni_cn_mismatch && depth == 0 &&
722 c_session->type == COAP_SESSION_TYPE_CLIENT && setup_data->client_sni) {
723
724 cn = get_san_or_cn_from_cert(c_session, crt, setup_data->client_sni);
725 if (!cn) {
726 coap_log_info(" %s: SNI '%s' not returned in certificate\n",
727 coap_session_str(c_session), setup_data->client_sni);
728 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
729 }
730 }
731 if (!cn) {
732 cn = get_san_or_cn_from_cert(c_session, crt, NULL);
733 }
734 coap_dtls_log(COAP_LOG_DEBUG, "depth %d flags %x cert '%s'\n",
735 depth, *flags, cn);
736 if (setup_data->validate_cn_call_back) {
737 int ret;
738
740 setup_data->validate_cn_call_back(cn,
741 crt->raw.p,
742 crt->raw.len,
743 c_session,
744 depth,
745 *flags == 0,
746 setup_data->cn_call_back_arg));
747 if (!ret) {
748 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
749 }
750 }
751 if (setup_data->cert_chain_validation &&
752 depth > (setup_data->cert_chain_verify_depth + 2)) {
753 *flags |= MBEDTLS_X509_BADCERT_OTHER;
754 coap_log_warn(" %s: %s: '%s' depth %d\n",
755 coap_session_str(c_session),
756 "The certificate's verify depth is too long",
757 cn ? cn : "?", depth);
758 }
759 if (*flags == 0) {
760 if (cn)
761 mbedtls_free(cn);
762 return 0;
763 }
764
765 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
766 if (setup_data->allow_expired_certs) {
767 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
768 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
769 coap_session_str(c_session),
770 "The certificate has expired", cn ? cn : "?", depth);
771 }
772 }
773 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
774 if (setup_data->allow_expired_certs) {
775 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
776 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
777 coap_session_str(c_session),
778 "The certificate has a future date", cn ? cn : "?", depth);
779 }
780 }
781 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
782 if (setup_data->allow_bad_md_hash) {
783 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
784 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
785 coap_session_str(c_session),
786 "The certificate has a bad MD hash", cn ? cn : "?", depth);
787 }
788 }
789 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
790 if (setup_data->allow_short_rsa_length) {
791 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
792 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
793 coap_session_str(c_session),
794 "The certificate has a short RSA length", cn ? cn : "?", depth);
795 }
796 }
797 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
798 if (setup_data->allow_sni_cn_mismatch) {
799 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
800 coap_log_info(" %s: %s: overridden: '%s' v '%s' depth %d\n",
801 coap_session_str(c_session),
802 "The SNI does not match the returned CN",
803 setup_data->client_sni ? setup_data->client_sni : "",
804 cn ? cn : "?", depth);
805 }
806 }
807 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
808 uint32_t lflags;
809 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
810 self_signed_cert_verify_callback_mbedtls,
811 data);
812 if (self_signed && depth == 0) {
813 if (setup_data->allow_self_signed &&
814 !setup_data->check_common_ca) {
815 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
816 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
817 coap_session_str(c_session),
818 "Self-signed",
819 cn ? cn : "?", depth);
820 }
821 } else if (self_signed) {
822 if (!setup_data->verify_peer_cert) {
823 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
824 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
825 coap_session_str(c_session),
826 "Self-signed", cn ? cn : "?", depth);
827 }
828 } else {
829 if (!setup_data->verify_peer_cert) {
830 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
831 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
832 coap_session_str(c_session),
833 "The certificate's CA is not trusted", cn ? cn : "?", depth);
834 }
835 }
836 }
837 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
838 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
839 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
840 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
841 coap_session_str(c_session),
842 "The certificate's CRL has expired", cn ? cn : "?", depth);
843 } else if (!setup_data->check_cert_revocation) {
844 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
845 }
846 }
847 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
848 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
849 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
850 coap_log_info(" %s: %s: overridden: '%s' depth %d\n",
851 coap_session_str(c_session),
852 "The certificate's CRL has a future date", cn ? cn : "?", depth);
853 } else if (!setup_data->check_cert_revocation) {
854 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
855 }
856 }
857
858 if (*flags != 0) {
859 char buf[128];
860 char *tcp;
861 int ret = mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", *flags);
862
863 if (ret >= 0) {
864 tcp = strchr(buf, '\n');
865 while (tcp) {
866 *tcp = '\000';
867 coap_log_warn(" %s: %s: issue 0x%" PRIx32 ": '%s' depth %d\n",
868 coap_session_str(c_session),
869 buf, *flags, cn ? cn : "?", depth);
870 tcp = strchr(tcp+1, '\n');
871 }
872 } else {
873 coap_log_err("mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
874 -ret, get_error_string(ret));
875 }
876 }
877
878 if (cn)
879 mbedtls_free(cn);
880
881 return 0;
882}
883
884static int
885setup_pki_credentials(mbedtls_x509_crt *cacert,
886 mbedtls_x509_crt *public_cert,
887 mbedtls_pk_context *private_key,
888 coap_mbedtls_env_t *m_env,
889 coap_mbedtls_context_t *m_context,
890 coap_session_t *c_session,
891 coap_dtls_pki_t *setup_data,
892 coap_dtls_role_t role) {
893 coap_dtls_key_t key;
894 int ret;
895 int done_private_key = 0;
896 int done_public_cert = 0;
897 uint8_t *buffer;
898 size_t length;
899
900 /* Map over to the new define format to save code duplication */
901 coap_dtls_map_key_type_to_define(setup_data, &key);
902
903 assert(key.key_type == COAP_PKI_KEY_DEFINE);
904
905 /*
906 * Configure the Private Key
907 */
908 if (key.key.define.private_key.u_byte &&
909 key.key.define.private_key.u_byte[0]) {
910 switch (key.key.define.private_key_def) {
911 case COAP_PKI_KEY_DEF_DER: /* define private key */
912 /* Fall Through */
913 case COAP_PKI_KEY_DEF_PEM: /* define private key */
914#if defined(MBEDTLS_FS_IO)
915 mbedtls_pk_init(private_key);
916#if COAP_USE_PSA_CRYPTO
917 ret = mbedtls_pk_parse_keyfile(private_key,
919#elif defined(MBEDTLS_2_X_COMPAT)
920 ret = mbedtls_pk_parse_keyfile(private_key,
922#else
923 ret = mbedtls_pk_parse_keyfile(private_key,
925 NULL, coap_rng, (void *)&m_env->ctr_drbg);
926#endif
927 if (ret < 0) {
930 &key, role, ret);
931 }
932 done_private_key = 1;
933 break;
934#else /* ! MBEDTLS_FS_IO */
937 &key, role, -1);
938#endif /* ! MBEDTLS_FS_IO */
939 case COAP_PKI_KEY_DEF_PEM_BUF: /* define private key */
940 mbedtls_pk_init(private_key);
941 length = key.key.define.private_key_len;
942 if (key.key.define.private_key.u_byte[length-1] != '\000') {
943 /* Need to allocate memory to add in NULL terminator */
944 buffer = mbedtls_malloc(length + 1);
945 if (!buffer) {
946 coap_log_err("mbedtls_malloc failed\n");
947 return -1;
948 }
949 memcpy(buffer, key.key.define.private_key.u_byte, length);
950 buffer[length] = '\000';
951 length++;
952#if COAP_USE_PSA_CRYPTO
953 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
954#elif defined(MBEDTLS_2_X_COMPAT)
955 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
956#else
957 ret = mbedtls_pk_parse_key(private_key, buffer, length,
958 NULL, 0, coap_rng, (void *)&m_env->ctr_drbg);
959#endif
960 mbedtls_free(buffer);
961 } else {
962#if COAP_USE_PSA_CRYPTO
963 ret = mbedtls_pk_parse_key(private_key,
966#elif defined(MBEDTLS_2_X_COMPAT)
967 ret = mbedtls_pk_parse_key(private_key,
970#else
971 ret = mbedtls_pk_parse_key(private_key,
974 NULL, 0, coap_rng, (void *)&m_env->ctr_drbg);
975#endif
976 }
977 if (ret < 0) {
980 &key, role, ret);
981 }
982 done_private_key = 1;
983 break;
984 case COAP_PKI_KEY_DEF_DER_BUF: /* define private key */
985 mbedtls_pk_init(private_key);
986#if COAP_USE_PSA_CRYPTO
987 ret = mbedtls_pk_parse_key(private_key,
990#elif defined(MBEDTLS_2_X_COMPAT)
991 ret = mbedtls_pk_parse_key(private_key,
994#else
995 ret = mbedtls_pk_parse_key(private_key,
997 key.key.define.private_key_len, NULL, 0, coap_rng,
998 (void *)&m_env->ctr_drbg);
999#endif
1000 if (ret < 0) {
1003 &key, role, ret);
1004 }
1005 done_private_key = 1;
1006 break;
1007 case COAP_PKI_KEY_DEF_RPK_BUF: /* define private key */
1008 case COAP_PKI_KEY_DEF_PKCS11: /* define private key */
1009 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define private key */
1010 case COAP_PKI_KEY_DEF_ENGINE: /* define private key */
1011 default:
1014 &key, role, -1);
1015 }
1016 } else if (role == COAP_DTLS_ROLE_SERVER ||
1018 key.key.define.public_cert.u_byte[0])) {
1021 &key, role, -1);
1022 }
1023
1024 /*
1025 * Configure the Public Certificate / Key
1026 */
1027 if (key.key.define.public_cert.u_byte &&
1028 key.key.define.public_cert.u_byte[0]) {
1029 switch (key.key.define.public_cert_def) {
1030 case COAP_PKI_KEY_DEF_DER: /* define public cert */
1031 /* Fall Through */
1032 case COAP_PKI_KEY_DEF_PEM: /* define public cert */
1033#if defined(MBEDTLS_FS_IO)
1034 mbedtls_x509_crt_init(public_cert);
1035 ret = mbedtls_x509_crt_parse_file(public_cert,
1037 if (ret < 0) {
1040 &key, role, ret);
1041 }
1042 done_public_cert = 1;
1043 break;
1044#else /* ! MBEDTLS_FS_IO */
1047 &key, role, -1);
1048#endif /* ! MBEDTLS_FS_IO */
1049 case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */
1050 mbedtls_x509_crt_init(public_cert);
1051
1052 length = key.key.define.public_cert_len;
1053 if (key.key.define.public_cert.u_byte[length-1] != '\000') {
1054 /* Need to allocate memory to add in NULL terminator */
1055 buffer = mbedtls_malloc(length + 1);
1056 if (!buffer) {
1057 coap_log_err("mbedtls_malloc failed\n");
1058 return -1;
1059 }
1060 memcpy(buffer, key.key.define.public_cert.u_byte, length);
1061 buffer[length] = '\000';
1062 length++;
1063 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
1064 mbedtls_free(buffer);
1065 } else {
1066 ret = mbedtls_x509_crt_parse(public_cert,
1069 }
1070 if (ret < 0) {
1073 &key, role, ret);
1074 }
1075 done_public_cert = 1;
1076 break;
1077 case COAP_PKI_KEY_DEF_RPK_BUF: /* define public cert */
1080 &key, role, -1);
1081 case COAP_PKI_KEY_DEF_DER_BUF: /* define public cert */
1082 mbedtls_x509_crt_init(public_cert);
1083 ret = mbedtls_x509_crt_parse(public_cert,
1086 if (ret < 0) {
1089 &key, role, ret);
1090 }
1091 done_public_cert = 1;
1092 break;
1093 case COAP_PKI_KEY_DEF_PKCS11: /* define public cert */
1094 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define public cert */
1095 case COAP_PKI_KEY_DEF_ENGINE: /* define public cert */
1096 default:
1099 &key, role, -1);
1100 }
1101 } else if (role == COAP_DTLS_ROLE_SERVER ||
1103 key.key.define.private_key.u_byte[0])) {
1106 &key, role, -1);
1107 }
1108
1109 if (done_private_key && done_public_cert) {
1110 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
1111 if (ret < 0) {
1112 coap_log_err("mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
1113 -ret, get_error_string(ret));
1114 return -1;
1115 }
1116 }
1117
1118 /*
1119 * Configure the CA
1120 */
1121 if (
1122 key.key.define.ca.u_byte &&
1123 key.key.define.ca.u_byte[0]) {
1124 switch (key.key.define.ca_def) {
1125 case COAP_PKI_KEY_DEF_DER: /* define ca */
1126 /* Fall Through */
1128#if defined(MBEDTLS_FS_IO)
1129 mbedtls_x509_crt_init(cacert);
1130 ret = mbedtls_x509_crt_parse_file(cacert,
1131 key.key.define.ca.s_byte);
1132 if (ret < 0) {
1135 &key, role, ret);
1136 }
1137 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1138#else /* ! MBEDTLS_FS_IO */
1141 &key, role, -1);
1142#endif /* ! MBEDTLS_FS_IO */
1143 break;
1144 case COAP_PKI_KEY_DEF_PEM_BUF: /* define ca */
1145 mbedtls_x509_crt_init(cacert);
1146 length = key.key.define.ca_len;
1147 if (key.key.define.ca.u_byte[length-1] != '\000') {
1148 /* Need to allocate memory to add in NULL terminator */
1149 buffer = mbedtls_malloc(length + 1);
1150 if (!buffer) {
1151 coap_log_err("mbedtls_malloc failed\n");
1152 return -1;
1153 }
1154 memcpy(buffer, key.key.define.ca.u_byte, length);
1155 buffer[length] = '\000';
1156 length++;
1157 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
1158 mbedtls_free(buffer);
1159 } else {
1160 ret = mbedtls_x509_crt_parse(cacert,
1161 key.key.define.ca.u_byte,
1162 key.key.define.ca_len);
1163 }
1164 if (ret < 0) {
1167 &key, role, ret);
1168 }
1169 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1170 break;
1171 case COAP_PKI_KEY_DEF_RPK_BUF: /* define ca */
1174 &key, role, -1);
1175 case COAP_PKI_KEY_DEF_DER_BUF: /* define ca */
1176 mbedtls_x509_crt_init(cacert);
1177 ret = mbedtls_x509_crt_parse(cacert,
1178 key.key.define.ca.u_byte,
1179 key.key.define.ca_len);
1180 if (ret < 0) {
1183 &key, role, ret);
1184 }
1185 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1186 break;
1187 case COAP_PKI_KEY_DEF_PKCS11: /* define ca */
1188 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define ca */
1189 case COAP_PKI_KEY_DEF_ENGINE: /* define ca */
1190 default:
1193 &key, role, -1);
1194 }
1195 }
1196
1197 /* Add in any root CA definitions */
1198
1199#if defined(MBEDTLS_FS_IO)
1200 if (m_context->root_ca_file) {
1201 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
1202 if (ret < 0) {
1206 &key, role, ret);
1207 }
1208 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1209 }
1210 if (m_context->root_ca_path) {
1211 ret = mbedtls_x509_crt_parse_path(cacert, m_context->root_ca_path);
1212 if (ret < 0) {
1216 &key, role, ret);
1217 }
1218 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1219 }
1220#if MBEDTLS_VERSION_NUMBER >= 0x04000000
1221 if (m_context->trust_store_defined)
1222#else /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1223 if (m_context->trust_store_defined ||
1224 (role == COAP_DTLS_ROLE_CLIENT && !m_context->setup_data.verify_peer_cert))
1225#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1226 {
1227 /* Until Trust Store is implemented in MbedTLS */
1228 const char *trust_list[] = {
1229 "/etc/ssl/ca-bundle.pem",
1230 "/etc/ssl/certs/ca-certificates.crt",
1231 "/etc/pki/tls/cert.pem",
1232 "/usr/local/share/certs/ca-root-nss.crt",
1233 "/etc/ssl/cert.pem"
1234 };
1235 static const char *trust_file_found = NULL;
1236 static int trust_file_done = 0;
1237 unsigned int i;
1238
1239 if (trust_file_found) {
1240 ret = mbedtls_x509_crt_parse_file(cacert, trust_file_found);
1241 if (ret >= 0) {
1242 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1243 } else {
1244 coap_log_warn("Unable to load trusted root CAs (%s)\n",
1245 trust_file_found);
1246 }
1247 } else if (!trust_file_done) {
1248 trust_file_done = 1;
1249 for (i = 0; i < sizeof(trust_list)/sizeof(trust_list[0]); i++) {
1250 ret = mbedtls_x509_crt_parse_file(cacert, trust_list[i]);
1251 if (ret >= 0) {
1252 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
1253 trust_file_found = trust_list[i];
1254 break;
1255 }
1256 }
1257 if (i == sizeof(trust_list)/sizeof(trust_list[0])) {
1258 coap_log_warn("Unable to load trusted root CAs\n");
1259 }
1260 }
1261 }
1262#else /* ! MBEDTLS_FS_IO */
1263 if (m_context->root_ca_file || m_context->root_ca_path ||
1264 m_context->trust_store_defined) {
1265 /* No FS to read these files */
1268 &key, role, -1);
1269 }
1270#endif /* ! MBEDTLS_FS_IO */
1271
1272#if defined(MBEDTLS_SSL_SRV_C)
1273 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
1274 setup_data->check_common_ca ?
1275 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
1276 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
1277#endif
1278 mbedtls_ssl_conf_authmode(&m_env->conf, setup_data->verify_peer_cert ?
1279 MBEDTLS_SSL_VERIFY_REQUIRED :
1280 MBEDTLS_SSL_VERIFY_NONE);
1281 /*
1282 * Verify Peer.
1283 * Need to do all checking, even if setup_data->verify_peer_cert is not set
1284 */
1285 mbedtls_ssl_conf_verify(&m_env->conf,
1286 cert_verify_callback_mbedtls, c_session);
1287
1288 return 1;
1289}
1290
1291#if defined(MBEDTLS_SSL_SRV_C)
1292/*
1293 * PKI SNI callback.
1294 */
1295static int
1296pki_sni_callback(void *p_info, mbedtls_ssl_context *ssl,
1297 const unsigned char *uname, size_t name_len) {
1298 unsigned int i;
1299 coap_dtls_pki_t sni_setup_data;
1300 coap_session_t *c_session = (coap_session_t *)p_info;
1301 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
1302 coap_mbedtls_context_t *m_context =
1303 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1304 char *name;
1305
1306 name = mbedtls_malloc(name_len+1);
1307 if (!name)
1308 return -1;
1309
1310 memcpy(name, uname, name_len);
1311 name[name_len] = '\000';
1312
1313 /* Is this a cached entry? */
1314 for (i = 0; i < m_context->pki_sni_count; i++) {
1315 if (strcasecmp(name, m_context->pki_sni_entry_list[i]->sni) == 0) {
1316 break;
1317 }
1318 }
1319 if (i == m_context->pki_sni_count) {
1320 /*
1321 * New PKI SNI request
1322 */
1323 coap_dtls_key_t *new_entry;
1324 pki_sni_entry **pki_sni_entry_list;
1325 pki_sni_entry *entry;
1326
1327 coap_lock_callback_ret(new_entry,
1328 m_context->setup_data.validate_sni_call_back(name,
1329 m_context->setup_data.sni_call_back_arg));
1330 if (!new_entry) {
1331 mbedtls_free(name);
1332 return -1;
1333 }
1334
1335 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
1336 (i+1)*sizeof(pki_sni_entry *));
1337
1338 if (pki_sni_entry_list == NULL) {
1339 mbedtls_free(name);
1340 return -1;
1341 }
1342 m_context->pki_sni_entry_list = pki_sni_entry_list;
1343 entry = mbedtls_malloc(sizeof(pki_sni_entry));
1344 if (entry == NULL) {
1345 mbedtls_free(name);
1346 return -1;
1347 }
1348 memset(entry, 0, sizeof(*entry));
1349 mbedtls_x509_crt_init(&entry->cacert);
1350 mbedtls_x509_crt_init(&entry->public_cert);
1351 mbedtls_pk_init(&entry->private_key);
1352 entry->pki_key = *new_entry;
1353 sni_setup_data = m_context->setup_data;
1354 sni_setup_data.pki_key = *new_entry;
1355 if (setup_pki_credentials(&entry->cacert,
1356 &entry->public_cert,
1357 &entry->private_key,
1358 m_env,
1359 m_context,
1360 c_session,
1361 &sni_setup_data, COAP_DTLS_ROLE_SERVER) < 0) {
1362 mbedtls_x509_crt_free(&entry->public_cert);
1363 mbedtls_pk_free(&entry->private_key);
1364 mbedtls_x509_crt_free(&entry->cacert);
1365 mbedtls_free(entry);
1366 mbedtls_free(name);
1367 return -1;
1368 }
1369 /* name has been absorbed into pki_sni_entry_list[]->sni entry */
1370 entry->sni = name;
1371 m_context->pki_sni_entry_list[i] = entry;
1372 m_context->pki_sni_count++;
1373 } else {
1374 mbedtls_free(name);
1375 }
1376
1377 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i]->cacert,
1378 NULL);
1379 return mbedtls_ssl_set_hs_own_cert(ssl,
1380 &m_context->pki_sni_entry_list[i]->public_cert,
1381 &m_context->pki_sni_entry_list[i]->private_key);
1382}
1383
1384#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1385/*
1386 * PSK SNI callback.
1387 */
1388static int
1389psk_sni_callback(void *p_info, mbedtls_ssl_context *ssl,
1390 const unsigned char *uname, size_t name_len) {
1391 unsigned int i;
1392 coap_session_t *c_session = (coap_session_t *)p_info;
1393 coap_mbedtls_context_t *m_context =
1394 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1395 char *name;
1396
1397 name = mbedtls_malloc(name_len+1);
1398 if (!name)
1399 return -1;
1400
1401 memcpy(name, uname, name_len);
1402 name[name_len] = '\000';
1403
1404 /* Is this a cached entry? */
1405 for (i = 0; i < m_context->psk_sni_count; i++) {
1406 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
1407 break;
1408 }
1409 }
1410 if (i == m_context->psk_sni_count) {
1411 /*
1412 * New PSK SNI request
1413 */
1414 const coap_dtls_spsk_info_t *new_entry;
1415 psk_sni_entry *psk_sni_entry_list;
1416
1417 coap_lock_callback_ret(new_entry,
1418 c_session->context->spsk_setup_data.validate_sni_call_back(name,
1419 c_session,
1420 c_session->context->spsk_setup_data.sni_call_back_arg));
1421 if (!new_entry) {
1422 mbedtls_free(name);
1423 return -1;
1424 }
1425
1426 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
1427 (i+1)*sizeof(psk_sni_entry));
1428
1429 if (psk_sni_entry_list == NULL) {
1430 mbedtls_free(name);
1431 return -1;
1432 }
1433 m_context->psk_sni_entry_list = psk_sni_entry_list;
1434 m_context->psk_sni_entry_list[i].sni = name;
1435 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1436 /* name has been absorbed into psk_sni_entry_list[].sni entry */
1437 m_context->psk_sni_count++;
1438 } else {
1439 mbedtls_free(name);
1440 }
1441
1443 &m_context->psk_sni_entry_list[i].psk_info.hint);
1445 &m_context->psk_sni_entry_list[i].psk_info.key);
1446#if MBEDTLS_VERSION_NUMBER >= 0x04000000
1447 (void)ssl;
1448 return 0;
1449#else /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1450 return mbedtls_ssl_set_hs_psk(ssl,
1451 m_context->psk_sni_entry_list[i].psk_info.key.s,
1452 m_context->psk_sni_entry_list[i].psk_info.key.length);
1453#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
1454}
1455#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1456
1457static int
1458setup_server_ssl_session(coap_session_t *c_session,
1459 coap_mbedtls_env_t *m_env) {
1460 coap_mbedtls_context_t *m_context =
1461 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1462 int ret = 0;
1463 m_context->psk_pki_enabled |= IS_SERVER;
1464
1465 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1466 MBEDTLS_SSL_IS_SERVER,
1467 c_session->proto == COAP_PROTO_DTLS ?
1468 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1469 MBEDTLS_SSL_TRANSPORT_STREAM,
1470 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1471 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1472 -ret, get_error_string(ret));
1473 goto fail;
1474 }
1475
1476#if !COAP_USE_PSA_CRYPTO
1477 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1478#endif /* !COAP_USE_PSA_CRYPTO */
1479
1480#if defined(MBEDTLS_SSL_PROTO_DTLS)
1481 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1483#endif /* MBEDTLS_SSL_PROTO_DTLS */
1484
1485 if (m_context->psk_pki_enabled & IS_PSK) {
1486#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1487 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1488 if (c_session->context->spsk_setup_data.validate_sni_call_back) {
1489 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1490 }
1491#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1492 m_env->ec_jpake = c_session->context->spsk_setup_data.ec_jpake;
1493#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1494#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1495 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1496#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1497 }
1498
1499 if (m_context->psk_pki_enabled & IS_PKI) {
1500 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1501 &m_env->private_key, m_env, m_context,
1502 c_session, &m_context->setup_data,
1504 if (ret < 0) {
1505 coap_log_err("PKI setup failed\n");
1506 return ret;
1507 }
1508 if (m_context->setup_data.validate_sni_call_back) {
1509 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1510 }
1511 }
1512
1513#if COAP_USE_PSA_CRYPTO
1514 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx)) != 0) {
1515#else /* !COAP_USE_PSA_CRYPTO */
1516 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1517 mbedtls_ctr_drbg_random,
1518 &m_env->ctr_drbg)) != 0) {
1519#endif /* !COAP_USE_PSA_CRYPTO */
1520 coap_log_err("mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1521 -ret, get_error_string(ret));
1522 goto fail;
1523 }
1524
1525#if defined(MBEDTLS_SSL_PROTO_DTLS)
1526 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1527 mbedtls_ssl_cookie_check,
1528 &m_env->cookie_ctx);
1529#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1530 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1531#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1532#endif /* MBEDTLS_SSL_PROTO_DTLS */
1533#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1534 /*
1535 * Configure CID max length.
1536 *
1537 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
1538 * to use RFC9146 extension ID of 54, rather than the draft version -05
1539 * value of 254.
1540 */
1541 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1542#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1543fail:
1544 return ret;
1545}
1546#endif /* MBEDTLS_SSL_SRV_C */
1547
1548#if COAP_CLIENT_SUPPORT
1549static int *psk_ciphers = NULL;
1550static int *pki_ciphers = NULL;
1551static int *ecjpake_ciphers = NULL;
1552static int processed_ciphers = 0;
1553
1554#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1555static int
1556coap_ssl_ciphersuite_uses_psk(const mbedtls_ssl_ciphersuite_t *info) {
1557#if COAP_USE_PSA_CRYPTO
1558 switch (info->key_exchange) {
1559 case MBEDTLS_KEY_EXCHANGE_PSK:
1560 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1561 return 1;
1562 case MBEDTLS_KEY_EXCHANGE_NONE:
1563 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1564 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1565 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1566 default:
1567 return 0;
1568 }
1569#elif MBEDTLS_VERSION_NUMBER >= 0x03060000
1570 switch (info->key_exchange) {
1571 case MBEDTLS_KEY_EXCHANGE_PSK:
1572 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1573 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1574 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1575 return 1;
1576 case MBEDTLS_KEY_EXCHANGE_NONE:
1577 case MBEDTLS_KEY_EXCHANGE_RSA:
1578 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1579 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1580 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1581 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1582 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1583 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1584 default:
1585 return 0;
1586 }
1587#else /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1588 return mbedtls_ssl_ciphersuite_uses_psk(info);
1589#endif /* MBEDTLS_VERSION_NUMBER < 0x03060000 */
1590}
1591#endif /* defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) */
1592
1593/* Cipher suite lists a cipher suite is put in by set_ciphersuites() */
1594#define COAP_CIPHER_IN_PSK 0x01
1595#define COAP_CIPHER_IN_PKI 0x02
1596#define COAP_CIPHER_IN_ECJPAKE 0x04
1597
1598/*
1599 * Returns which of the PSK, PKI and EC JPAKE lists the cipher suite
1600 * belongs in (0 if none). Used for both counting and filling the lists
1601 * so that they always agree.
1602 */
1603static int
1604coap_ciphersuite_lists(int id) {
1605 const mbedtls_ssl_ciphersuite_t *cur = mbedtls_ssl_ciphersuite_from_id(id);
1606
1607 if (!cur)
1608 return 0;
1609#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1610 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1611 /* Minimum of TLS1.2 required - skip */
1612 return 0;
1613 }
1614#else
1615 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1616 /* Minimum of TLS1.2 required - skip */
1617 return 0;
1618 }
1619#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1620#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1621 if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE)
1622 return COAP_CIPHER_IN_ECJPAKE;
1623#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1624#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1625 if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3)
1626 return COAP_CIPHER_IN_PSK | COAP_CIPHER_IN_PKI;
1627#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
1628#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1629 if (coap_ssl_ciphersuite_uses_psk(cur))
1630 return COAP_CIPHER_IN_PSK;
1631#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1632 return COAP_CIPHER_IN_PKI;
1633}
1634
1635static void
1636set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1637 if (!processed_ciphers) {
1638 const int *base = mbedtls_ssl_list_ciphersuites();
1639 const int *list;
1640 int *psk_list;
1641 int *pki_list;
1642 int *ecjpake_list;
1643 int psk_count = 1; /* account for empty terminator */
1644 int pki_count = 1;
1645 int ecjpake_count = 1;
1646
1647 for (list = base; *list; list++) {
1648 int in = coap_ciphersuite_lists(*list);
1649
1650 if (in & COAP_CIPHER_IN_PSK)
1651 psk_count++;
1652 if (in & COAP_CIPHER_IN_PKI)
1653 pki_count++;
1654 if (in & COAP_CIPHER_IN_ECJPAKE)
1655 ecjpake_count++;
1656 }
1657
1658 psk_ciphers = mbedtls_malloc(psk_count * sizeof(psk_ciphers[0]));
1659 pki_ciphers = mbedtls_malloc(pki_count * sizeof(pki_ciphers[0]));
1660 ecjpake_ciphers = mbedtls_malloc(ecjpake_count * sizeof(ecjpake_ciphers[0]));
1661 if (psk_ciphers == NULL || pki_ciphers == NULL || ecjpake_ciphers == NULL) {
1662 coap_log_err("set_ciphers: mbedtls_malloc failed\n");
1663 mbedtls_free(psk_ciphers);
1664 mbedtls_free(pki_ciphers);
1665 mbedtls_free(ecjpake_ciphers);
1666 psk_ciphers = NULL;
1667 pki_ciphers = NULL;
1668 ecjpake_ciphers = NULL;
1669 return;
1670 }
1671
1672 psk_list = psk_ciphers;
1673 pki_list = pki_ciphers;
1674 ecjpake_list = ecjpake_ciphers;
1675
1676 for (list = base; *list; list++) {
1677 int in = coap_ciphersuite_lists(*list);
1678
1679 if (in & COAP_CIPHER_IN_PSK)
1680 *psk_list++ = *list;
1681 if (in & COAP_CIPHER_IN_PKI)
1682 *pki_list++ = *list;
1683 if (in & COAP_CIPHER_IN_ECJPAKE)
1684 *ecjpake_list++ = *list;
1685 }
1686 /* zero terminate */
1687 *psk_list = 0;
1688 *pki_list = 0;
1689 *ecjpake_list = 0;
1690 processed_ciphers = 1;
1691 }
1692 switch (method) {
1693 case COAP_ENC_PSK:
1694 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1695 break;
1696 case COAP_ENC_PKI:
1697 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1698 break;
1699 case COAP_ENC_ECJPAKE:
1700 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1701 break;
1702 default:
1703 assert(0);
1704 break;
1705 }
1706}
1707
1708static int
1709setup_client_ssl_session(coap_session_t *c_session,
1710 coap_mbedtls_env_t *m_env) {
1711 int ret;
1712
1713 coap_mbedtls_context_t *m_context =
1714 (coap_mbedtls_context_t *)c_session->context->dtls_context;
1715
1716 m_context->psk_pki_enabled |= IS_CLIENT;
1717
1718 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1719 MBEDTLS_SSL_IS_CLIENT,
1720 c_session->proto == COAP_PROTO_DTLS ?
1721 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1722 MBEDTLS_SSL_TRANSPORT_STREAM,
1723 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1724 coap_log_err("mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1725 -ret, get_error_string(ret));
1726 goto fail;
1727 }
1728
1729#if defined(MBEDTLS_SSL_PROTO_DTLS)
1730 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1732#endif /* MBEDTLS_SSL_PROTO_DTLS */
1733
1734 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1735#if !COAP_USE_PSA_CRYPTO
1736 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1737#endif /* !COAP_USE_PSA_CRYPTO */
1738
1739 if (m_context->psk_pki_enabled & IS_PSK) {
1740#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1741 const coap_bin_const_t *psk_key;
1742 const coap_bin_const_t *psk_identity;
1743
1744 coap_log_info("Setting PSK key\n");
1745
1746 psk_key = coap_get_session_client_psk_key(c_session);
1747 psk_identity = coap_get_session_client_psk_identity(c_session);
1748 if (psk_key == NULL || psk_identity == NULL) {
1749 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1750 goto fail;
1751 }
1752
1753 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->s,
1754 psk_key->length, psk_identity->s,
1755 psk_identity->length)) != 0) {
1756 coap_log_err("mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1757 -ret, get_error_string(ret));
1758 goto fail;
1759 }
1760 if (c_session->cpsk_setup_data.client_sni) {
1761 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1762 c_session->cpsk_setup_data.client_sni)) != 0) {
1763 coap_log_err("mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1764 -ret, get_error_string(ret));
1765 goto fail;
1766 }
1767 }
1768 /* Identity Hint currently not supported in Mbed TLS so code removed */
1769
1770#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1771 if (c_session->cpsk_setup_data.ec_jpake) {
1772 m_env->ec_jpake = 1;
1773 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1774#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1775 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1776#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
1777 } else {
1778 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1779 }
1780#else /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1781 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1782#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1783#else /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1784 coap_log_warn("PSK not enabled in Mbed TLS library\n");
1785#endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1786 } else if ((m_context->psk_pki_enabled & IS_PKI) ||
1787 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1788 /*
1789 * If neither PSK or PKI have been set up, use PKI basics.
1790 * This works providing COAP_PKI_KEY_PEM has a value of 0.
1791 */
1792 coap_dtls_pki_t *setup_data = &m_context->setup_data;
1793
1794 if (!(m_context->psk_pki_enabled & IS_PKI)) {
1795 /* PKI not defined - set up some defaults */
1796 setup_data->verify_peer_cert = 1;
1797 setup_data->check_common_ca = 0;
1798 setup_data->allow_self_signed = 1;
1799 setup_data->allow_expired_certs = 1;
1800 setup_data->cert_chain_validation = 1;
1801 setup_data->cert_chain_verify_depth = 2;
1802 setup_data->check_cert_revocation = 1;
1803 setup_data->allow_no_crl = 1;
1804 setup_data->allow_expired_crl = 1;
1805 setup_data->is_rpk_not_cert = 0;
1806 setup_data->use_cid = 0;
1807 }
1808 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1809 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1810 &m_env->private_key, m_env, m_context,
1811 c_session, setup_data,
1813 if (ret < 0) {
1814 coap_log_err("PKI setup failed\n");
1815 return ret;
1816 }
1817#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1818 if (c_session->proto == COAP_PROTO_TLS ||
1819 c_session->proto == COAP_PROTO_WSS) {
1820 static const char *alpn_list[] = { "coap", NULL };
1821
1822 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1823 if (ret != 0) {
1824 coap_log_err("ALPN setup failed %d)\n", ret);
1825 }
1826 }
1827#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN */
1828 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1829#if defined(MBEDTLS_SSL_PROTO_DTLS)
1830#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1831 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
1832#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
1833#endif /* MBEDTLS_SSL_PROTO_DTLS */
1834 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1835 }
1836 return 0;
1837
1838fail:
1839 return ret;
1840}
1841#endif /* COAP_CLIENT_SUPPORT */
1842
1843static void
1844mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1845 if (!m_env) {
1846 return;
1847 }
1848
1849 mbedtls_x509_crt_free(&m_env->cacert);
1850 mbedtls_x509_crt_free(&m_env->public_cert);
1851 mbedtls_pk_free(&m_env->private_key);
1852#if !COAP_USE_PSA_CRYPTO
1853 mbedtls_entropy_free(&m_env->entropy);
1854 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1855#endif /* !COAP_USE_PSA_CRYPTO */
1856 mbedtls_ssl_config_free(&m_env->conf);
1857 mbedtls_ssl_free(&m_env->ssl);
1858 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1859}
1860
1861static void
1862coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1863 if (m_env) {
1864 if (!m_env->sent_alert)
1865 mbedtls_ssl_close_notify(&m_env->ssl);
1866 mbedtls_cleanup(m_env);
1867 mbedtls_free(m_env);
1868 }
1869}
1870
1871#if COAP_MAX_LOGGING_LEVEL > 0
1872static const char *
1873report_mbedtls_alert(unsigned char alert) {
1874 switch (alert) {
1875 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1876 return ": Bad Record MAC";
1877 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1878 return ": Handshake failure";
1879 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1880 return ": No Certificate provided";
1881 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1882 return ": Certificate is bad";
1883 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1884 return ": Certificate is unknown";
1885 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1886 return ": CA is unknown";
1887 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1888 return ": Access was denied";
1889 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1890 return ": Decrypt error";
1891 default:
1892 return "";
1893 }
1894}
1895#endif /* COAP_MAX_LOGGING_LEVEL */
1896
1897/*
1898 * return -1 failure
1899 * 0 not completed
1900 * 1 established
1901 */
1902static int
1903do_mbedtls_handshake(coap_session_t *c_session,
1904 coap_mbedtls_env_t *m_env) {
1905 int ret;
1906 int alert;
1907
1908 ret = mbedtls_ssl_handshake(&m_env->ssl);
1909 switch (ret) {
1910 case 0:
1911 m_env->established = 1;
1912 coap_log_debug("* %s: Mbed TLS established\n",
1913 coap_session_str(c_session));
1914 ret = 1;
1915#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1916#if COAP_CLIENT_SUPPORT
1917 if (c_session->type == COAP_SESSION_TYPE_CLIENT &&
1918 c_session->proto == COAP_PROTO_DTLS) {
1919 coap_mbedtls_context_t *m_context;
1920
1921 m_context = (coap_mbedtls_context_t *)c_session->context->dtls_context;
1922 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
1923 m_context->setup_data.use_cid) {
1924 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1925 int enabled;
1926 size_t peer_cid_len;
1927
1928 /* See whether CID was negotiated */
1929 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1930 enabled == MBEDTLS_SSL_CID_ENABLED) {
1931 c_session->negotiated_cid = 1;
1932 coap_log_info("** %s: CID was negotiated\n", coap_session_str(c_session));
1933 } else {
1934 coap_log_info("** %s: CID was not negotiated\n", coap_session_str(c_session));
1935 c_session->negotiated_cid = 0;
1936 }
1937 }
1938 }
1939#endif /* COAP_CLIENT_SUPPORT */
1940#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1942 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
1943 break;
1944 case MBEDTLS_ERR_SSL_WANT_READ:
1945 case MBEDTLS_ERR_SSL_WANT_WRITE:
1946 if (m_env->ssl.state == MBEDTLS_SSL_SERVER_HELLO
1947#if MBEDTLS_VERSION_NUMBER >= 0x03030000
1948 || m_env->ssl.state == MBEDTLS_SSL_NEW_SESSION_TICKET
1949#endif /* MBEDTLS_VERSION_NUMBER >= 0x03030000 */
1950 ) {
1951 if (++m_env->server_hello_cnt > 10) {
1952 /* retried this too many times */
1953 goto fail;
1954 }
1955 }
1956 errno = EAGAIN;
1957 ret = 0;
1958 break;
1959 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1960 coap_log_debug("hello verification requested\n");
1961 goto reset;
1962 case MBEDTLS_ERR_SSL_INVALID_MAC:
1963 goto fail;
1964#ifdef MBEDTLS_2_X_COMPAT
1965 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1966#else /* ! MBEDTLS_2_X_COMPAT */
1967 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1968#endif /* ! MBEDTLS_2_X_COMPAT */
1969 goto fail;
1970 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1971 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1972 goto fail_alert;
1973#ifdef MBEDTLS_2_X_COMPAT
1974 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1975 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1976 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1977 goto fail_alert;
1978#endif /* MBEDTLS_2_X_COMPAT */
1979 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1980 goto fail;
1981 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1982 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1983 coap_log_warn("***%s: Alert '%d'%s\n",
1984 coap_session_str(c_session), m_env->ssl.in_msg[1],
1985 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1986 /* Fall through */
1987 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1988 case MBEDTLS_ERR_SSL_CONN_EOF:
1989 case MBEDTLS_ERR_NET_CONN_RESET:
1991 ret = -1;
1992 break;
1993 default:
1994 coap_log_warn("do_mbedtls_handshake: session establish "
1995 "returned -0x%x: '%s'\n",
1996 -ret, get_error_string(ret));
1997 ret = -1;
1998 break;
1999 }
2000 return ret;
2001
2002fail_alert:
2003 mbedtls_ssl_send_alert_message(&m_env->ssl,
2004 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2005 alert);
2006 m_env->sent_alert = 1;
2007fail:
2008 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
2009 coap_log_warn("do_mbedtls_handshake: session establish "
2010 "returned '%s'\n",
2011 get_error_string(ret));
2012reset:
2013 mbedtls_ssl_session_reset(&m_env->ssl);
2014#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2015 if (m_env->ec_jpake) {
2016 const coap_bin_const_t *psk_key;
2017
2018#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2019 if (c_session->type == COAP_SESSION_TYPE_CLIENT) {
2020 psk_key = coap_get_session_client_psk_key(c_session);
2021 } else {
2022 psk_key = coap_get_session_server_psk_key(c_session);
2023 }
2024#elif COAP_CLIENT_SUPPORT
2025 psk_key = coap_get_session_client_psk_key(c_session);
2026#else /* COAP_SERVER_SUPPORT */
2027 psk_key = coap_get_session_server_psk_key(c_session);
2028#endif /* COAP_SERVER_SUPPORT */
2029 if (psk_key) {
2030 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
2031 }
2032 }
2033#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2034 return -1;
2035}
2036
2037static void
2038mbedtls_debug_out(void *ctx COAP_UNUSED, int level,
2039 const char *file COAP_UNUSED,
2040 int line COAP_UNUSED, const char *str) {
2041
2042 coap_log_t coap_level = COAP_LOG_DEBUG;
2043 /*
2044 * 0 No debug
2045 * 1 Error
2046 * 2 State change
2047 * 3 Informational
2048 * 4 Verbose
2049 */
2050 switch (level) {
2051 case 0:
2052 coap_level = COAP_LOG_EMERG;
2053 break;
2054 case 1:
2055 coap_level = COAP_LOG_WARN;
2056 break;
2057 case 2:
2058 coap_level = COAP_LOG_NOTICE;
2059 break;
2060 case 3:
2061 coap_level = COAP_LOG_INFO;
2062 break;
2063 case 4:
2064 default:
2065 coap_level = COAP_LOG_DEBUG;
2066 break;
2067 }
2068 coap_dtls_log(coap_level, "%s", str);
2069}
2070
2071#if !COAP_DISABLE_TCP
2072/*
2073 * strm
2074 * return +ve data amount
2075 * 0 no more
2076 * -ve Mbed TLS error
2077 */
2078static int
2079coap_sock_read(void *ctx, unsigned char *out, size_t outl) {
2080 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
2081 coap_session_t *c_session = (coap_session_t *)ctx;
2082
2083 if (out != NULL) {
2084 ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_read(c_session, out, outl);
2085 /* Translate layer returns into what MbedTLS expects */
2086 if (ret == -1) {
2087 if (errno == ECONNRESET) {
2088 /* graceful shutdown */
2089 ret = MBEDTLS_ERR_SSL_CONN_EOF;
2090 } else {
2091 ret = MBEDTLS_ERR_NET_RECV_FAILED;
2092 }
2093 } else if (ret == 0) {
2094 errno = EAGAIN;
2095 ret = MBEDTLS_ERR_SSL_WANT_READ;
2096 }
2097 }
2098 return ret;
2099}
2100
2101/*
2102 * strm
2103 * return +ve data amount
2104 * 0 no more
2105 * -ve Mbed TLS error
2106 */
2107static int
2108coap_sock_write(void *context, const unsigned char *in, size_t inl) {
2109 int ret = 0;
2110 coap_session_t *c_session = (coap_session_t *)context;
2111
2112 ret = c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
2113 (const uint8_t *)in,
2114 inl);
2115 /* Translate layer what returns into what MbedTLS expects */
2116 if (ret < 0) {
2117 if ((c_session->state == COAP_SESSION_STATE_CSM ||
2118 c_session->state == COAP_SESSION_STATE_HANDSHAKE) &&
2119 (errno == EPIPE || errno == ECONNRESET)) {
2120 /*
2121 * Need to handle a TCP timing window where an agent continues with
2122 * the sending of the next handshake or a CSM.
2123 * However, the peer does not like a certificate and so sends a
2124 * fatal alert and closes the TCP session.
2125 * The sending of the next handshake or CSM may get terminated because
2126 * of the closed TCP session, but there is still an outstanding alert
2127 * to be read in and reported on.
2128 * In this case, pretend that sending the info was fine so that the
2129 * alert can be read (which effectively is what happens with DTLS).
2130 */
2131 ret = inl;
2132 } else {
2133#ifdef _WIN32
2134 int lasterror = WSAGetLastError();
2135
2136 if (lasterror == WSAEWOULDBLOCK) {
2137 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2138 } else if (lasterror == WSAECONNRESET) {
2139 ret = MBEDTLS_ERR_NET_CONN_RESET;
2140 }
2141#else
2142 if (errno == EAGAIN || errno == EINTR) {
2143 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2144 } else if (errno == EPIPE || errno == ECONNRESET) {
2145 ret = MBEDTLS_ERR_NET_CONN_RESET;
2146 }
2147#endif
2148 else {
2149 ret = MBEDTLS_ERR_NET_SEND_FAILED;
2150 }
2151 coap_log_debug("* %s: failed to send %" PRIdS " bytes (%s) state %d\n",
2152 coap_session_str(c_session), inl, coap_socket_strerror(),
2153 c_session->state);
2154 }
2155 }
2156 if (ret == 0) {
2157 errno = EAGAIN;
2158 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
2159 }
2160 return ret;
2161}
2162#endif /* !COAP_DISABLE_TCP */
2163
2164static coap_mbedtls_env_t *
2165coap_dtls_new_mbedtls_env(coap_session_t *c_session,
2166 coap_dtls_role_t role,
2167 coap_proto_t proto) {
2168#if !COAP_USE_PSA_CRYPTO
2169 int ret = 0;
2170#endif /* !COAP_USE_PSA_CRYPTO */
2171 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2172
2173 if (m_env)
2174 return m_env;
2175
2176 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(sizeof(coap_mbedtls_env_t));
2177 if (!m_env) {
2178 return NULL;
2179 }
2180 memset(m_env, 0, sizeof(coap_mbedtls_env_t));
2181
2182 mbedtls_ssl_init(&m_env->ssl);
2183#if !COAP_USE_PSA_CRYPTO
2184 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
2185 mbedtls_entropy_init(&m_env->entropy);
2186#endif /* !COAP_USE_PSA_CRYPTO */
2187 mbedtls_ssl_config_init(&m_env->conf);
2188 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
2189
2190#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
2191 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
2192#endif /* ESPIDF_VERSION && CONFIG_MBEDTLS_DEBUG */
2193
2194#if !COAP_USE_PSA_CRYPTO
2195 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
2196 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
2197 if (ret != MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) {
2198 coap_log_info("mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
2199 -ret, get_error_string(ret));
2200 goto fail;
2201 }
2202 coap_log_err("mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
2203 -ret, get_error_string(ret));
2204 }
2205#endif /* !COAP_USE_PSA_CRYPTO */
2206
2207 if (role == COAP_DTLS_ROLE_CLIENT) {
2208#if COAP_CLIENT_SUPPORT
2209 if (setup_client_ssl_session(c_session, m_env) != 0) {
2210 goto fail;
2211 }
2212#else /* !COAP_CLIENT_SUPPORT */
2213 goto fail;
2214#endif /* !COAP_CLIENT_SUPPORT */
2215 } else if (role == COAP_DTLS_ROLE_SERVER) {
2216#if defined(MBEDTLS_SSL_SRV_C)
2217 if (setup_server_ssl_session(c_session, m_env) != 0) {
2218 goto fail;
2219 }
2220#else /* ! MBEDTLS_SSL_SRV_C */
2221 goto fail;
2222#endif /* ! MBEDTLS_SSL_SRV_C */
2223 } else {
2224 goto fail;
2225 }
2226
2227#if MBEDTLS_VERSION_NUMBER >= 0x03020000
2228 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
2229#else
2230 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2231 MBEDTLS_SSL_MINOR_VERSION_3);
2232#endif /* MBEDTLS_VERSION_NUMBER >= 0x03020000 */
2233
2234 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
2235 goto fail;
2236 }
2237 if (proto == COAP_PROTO_DTLS) {
2238 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
2239 coap_dgram_read, NULL);
2240#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2241 if (COAP_PROTO_NOT_RELIABLE(c_session->proto)) {
2242 if (role == COAP_DTLS_ROLE_CLIENT) {
2243#if COAP_CLIENT_SUPPORT
2244 coap_mbedtls_context_t *m_context =
2245 (coap_mbedtls_context_t *)c_session->context->dtls_context;
2246
2247 if ((m_context->psk_pki_enabled & IS_PSK && c_session->cpsk_setup_data.use_cid) ||
2248 m_context->setup_data.use_cid) {
2249 /*
2250 * Enable passive DTLS CID support.
2251 *
2252 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
2253 * to use RFC9146 extension ID of 54, rather than the draft version -05
2254 * value of 254.
2255 */
2256 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
2257 }
2258#endif /* COAP_CLIENT_SUPPORT */
2259 } else {
2260#if COAP_SERVER_SUPPORT
2261 uint8_t cid[COAP_DTLS_CID_LENGTH];
2262 /*
2263 * Enable server DTLS CID support.
2264 *
2265 * Note: Set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT to 0 (the default)
2266 * to use RFC9146 extension ID of 54, rather than the draft version -05
2267 * value of 254.
2268 */
2269 coap_prng_lkd(cid, sizeof(cid));
2270 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
2271 sizeof(cid));
2272 c_session->client_cid = coap_new_bin_const(cid, sizeof(cid));
2273#endif /* COAP_SERVER_SUPPORT */
2274 }
2275 }
2276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2277 }
2278#if !COAP_DISABLE_TCP
2279 else {
2280 assert(proto == COAP_PROTO_TLS);
2281 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
2282 coap_sock_read, NULL);
2283 }
2284#endif /* ! COAP_DISABLE_TCP */
2285#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2286 coap_mbedtls_context_t *m_context =
2287 ((coap_mbedtls_context_t *)c_session->context->dtls_context);
2288 if ((m_context->psk_pki_enabled & IS_PSK) &&
2289 m_env->ec_jpake) {
2290 const coap_bin_const_t *psk_key;
2291
2292#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2293 if (role == COAP_DTLS_ROLE_CLIENT) {
2294 psk_key = coap_get_session_client_psk_key(c_session);
2295 } else {
2296 psk_key = coap_get_session_server_psk_key(c_session);
2297 }
2298#elif COAP_CLIENT_SUPPORT
2299 psk_key = coap_get_session_client_psk_key(c_session);
2300#else /* COAP_SERVER_SUPPORT */
2301 psk_key = coap_get_session_server_psk_key(c_session);
2302#endif /* COAP_SERVER_SUPPORT */
2303 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->s, psk_key->length);
2304 }
2305#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2306#ifdef __ZEPHYR__
2307 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
2308 zephyr_timing_set_delay,
2309 zephyr_timing_get_delay);
2310#else
2311 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
2312 mbedtls_timing_set_delay,
2313 mbedtls_timing_get_delay);
2314#endif
2315
2316 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
2317 return m_env;
2318
2319fail:
2320 if (m_env) {
2321 mbedtls_cleanup(m_env);
2322 mbedtls_free(m_env);
2323 }
2324 return NULL;
2325}
2326
2327int
2329#if defined(MBEDTLS_SSL_PROTO_DTLS)
2330 return 1;
2331#else /* !MBEDTLS_SSL_PROTO_DTLS */
2332 static int reported = 0;
2333 if (!reported) {
2334 reported = 1;
2335 coap_log_emerg("libcoap not compiled for DTLS with Mbed TLS"
2336 " - update Mbed TLS to include DTLS\n");
2337 }
2338 return 0;
2339#endif /* !MBEDTLS_SSL_PROTO_DTLS */
2340}
2341
2342int
2344#if !COAP_DISABLE_TCP
2345 return 1;
2346#else /* COAP_DISABLE_TCP */
2347 return 0;
2348#endif /* COAP_DISABLE_TCP */
2349}
2350
2351/*
2352 * return 0 failed
2353 * 1 passed
2354 */
2355int
2357 return 1;
2358}
2359
2360/*
2361 * return 0 failed
2362 * 1 passed
2363 */
2364int
2366 return 1;
2367}
2368
2369/*
2370 * return 0 failed
2371 * 1 passed
2372 */
2373int
2375 return 0;
2376}
2377
2378/*
2379 * return 0 failed
2380 * 1 passed
2381 */
2382int
2384 return 0;
2385}
2386
2387/*
2388 * return 0 failed
2389 * 1 passed
2390 */
2391int
2393#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2394 return 1;
2395#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2396 return 0;
2397#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2398}
2399
2400#if COAP_CLIENT_SUPPORT
2401int
2402coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
2403#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2404 c_context->testing_cids = every;
2405 return 1;
2406#else /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2407 (void)c_context;
2408 (void)every;
2409 return 0;
2410#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2411}
2412#endif /* COAP_CLIENT_SUPPORT */
2413
2414void *
2416 coap_mbedtls_context_t *m_context;
2417 (void)c_context;
2418
2419 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(sizeof(coap_mbedtls_context_t));
2420 if (m_context) {
2421 memset(m_context, 0, sizeof(coap_mbedtls_context_t));
2422 }
2423 return m_context;
2424}
2425
2426#if COAP_SERVER_SUPPORT
2427/*
2428 * return 0 failed
2429 * 1 passed
2430 */
2431int
2432coap_dtls_context_set_spsk(coap_context_t *c_context,
2433 coap_dtls_spsk_t *setup_data
2434 ) {
2435 coap_mbedtls_context_t *m_context =
2436 ((coap_mbedtls_context_t *)c_context->dtls_context);
2437
2438#if !defined(MBEDTLS_SSL_SRV_C)
2439 coap_log_emerg("coap_context_set_spsk:"
2440 " libcoap not compiled for Server Mode for Mbed TLS"
2441 " - update Mbed TLS to include Server Mode\n");
2442 return 0;
2443#endif /* !MBEDTLS_SSL_SRV_C */
2444 if (!m_context || !setup_data)
2445 return 0;
2446
2447 if (setup_data->ec_jpake) {
2448#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2449 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2450#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2451 }
2452 if (setup_data->psk_info.hint.length) {
2453 coap_log_warn("CoAP Server with Mbed TLS does not support Identity Hint\n");
2454 }
2455 m_context->psk_pki_enabled |= IS_PSK;
2456 return 1;
2457}
2458#endif /* COAP_SERVER_SUPPORT */
2459
2460#if COAP_CLIENT_SUPPORT
2461/*
2462 * return 0 failed
2463 * 1 passed
2464 */
2465int
2466coap_dtls_context_set_cpsk(coap_context_t *c_context,
2467 coap_dtls_cpsk_t *setup_data
2468 ) {
2469#if !defined(MBEDTLS_SSL_CLI_C)
2470 (void)c_context;
2471 (void)setup_data;
2472
2473 coap_log_emerg("coap_context_set_cpsk:"
2474 " libcoap not compiled for Client Mode for Mbed TLS"
2475 " - update Mbed TLS to include Client Mode\n");
2476 return 0;
2477#else /* MBEDTLS_SSL_CLI_C */
2478 coap_mbedtls_context_t *m_context =
2479 ((coap_mbedtls_context_t *)c_context->dtls_context);
2480
2481 if (!m_context || !setup_data)
2482 return 0;
2483
2484 if (setup_data->validate_ih_call_back) {
2485 coap_log_warn("CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2486 }
2487 if (setup_data->ec_jpake) {
2488#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2489 coap_log_warn("Mbed TLS not compiled for EC-JPAKE support\n");
2490#endif /* ! MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2491 }
2492 if (setup_data->use_cid) {
2493#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2494 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2495#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2496 }
2497 m_context->psk_pki_enabled |= IS_PSK;
2498 return 1;
2499#endif /* MBEDTLS_SSL_CLI_C */
2500}
2501#endif /* COAP_CLIENT_SUPPORT */
2502
2503int
2505 const coap_dtls_pki_t *setup_data,
2506 const coap_dtls_role_t role COAP_UNUSED) {
2507 coap_mbedtls_context_t *m_context =
2508 ((coap_mbedtls_context_t *)c_context->dtls_context);
2509
2510 m_context->setup_data = *setup_data;
2511 if (!m_context->setup_data.verify_peer_cert) {
2512 /* Needs to be clear so that no CA DNs are transmitted */
2513 m_context->setup_data.check_common_ca = 0;
2514 /* Allow all of these but warn if issue */
2515 m_context->setup_data.allow_self_signed = 1;
2516 m_context->setup_data.allow_expired_certs = 1;
2517 m_context->setup_data.cert_chain_validation = 1;
2518 m_context->setup_data.cert_chain_verify_depth = 10;
2519 m_context->setup_data.check_cert_revocation = 1;
2520 m_context->setup_data.allow_no_crl = 1;
2521 m_context->setup_data.allow_expired_crl = 1;
2522 m_context->setup_data.allow_bad_md_hash = 1;
2523 m_context->setup_data.allow_short_rsa_length = 1;
2524 }
2525 m_context->psk_pki_enabled |= IS_PKI;
2526 if (setup_data->use_cid) {
2527#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2528 coap_log_warn("Mbed TLS not compiled for Connection-ID support\n");
2529#endif /* ! MBEDTLS_SSL_DTLS_CONNECTION_ID */
2530 }
2531 return 1;
2532}
2533
2534int
2536 const char *ca_file,
2537 const char *ca_path) {
2538 coap_mbedtls_context_t *m_context =
2539 ((coap_mbedtls_context_t *)c_context->dtls_context);
2540
2541 if (!m_context) {
2542 coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
2543 "not set up\n");
2544 return 0;
2545 }
2546
2547 if (ca_file == NULL && ca_path == NULL) {
2548 coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path "
2549 "not defined\n");
2550 return 0;
2551 }
2552 if (m_context->root_ca_file) {
2553 mbedtls_free(m_context->root_ca_file);
2554 m_context->root_ca_file = NULL;
2555 }
2556
2557 if (ca_file) {
2558 m_context->root_ca_file = mbedtls_strdup(ca_file);
2559 }
2560
2561 if (m_context->root_ca_path) {
2562 mbedtls_free(m_context->root_ca_path);
2563 m_context->root_ca_path = NULL;
2564 }
2565
2566 if (ca_path) {
2567 m_context->root_ca_path = mbedtls_strdup(ca_path);
2568 }
2569 return 1;
2570}
2571
2572/*
2573 * return 0 failed
2574 * 1 passed
2575 */
2576int
2578 coap_mbedtls_context_t *m_context =
2579 ((coap_mbedtls_context_t *)c_context->dtls_context);
2580
2581 if (!m_context) {
2582 coap_log_warn("coap_context_load_pki_trust_store: (D)TLS environment "
2583 "not set up\n");
2584 return 0;
2585 }
2586 m_context->trust_store_defined = 1;
2587
2588 /* No proper support for this in MbedTLS at this point */
2589 return 1;
2590}
2591
2592
2593int
2595 coap_mbedtls_context_t *m_context =
2596 ((coap_mbedtls_context_t *)c_context->dtls_context);
2597 return m_context->psk_pki_enabled ? 1 : 0;
2598}
2599
2600void
2601coap_dtls_free_context(void *dtls_context) {
2602 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2603 unsigned int i;
2604
2605 for (i = 0; i < m_context->pki_sni_count; i++) {
2606 mbedtls_free(m_context->pki_sni_entry_list[i]->sni);
2607
2608 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i]->public_cert);
2609
2610 mbedtls_pk_free(&m_context->pki_sni_entry_list[i]->private_key);
2611
2612 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i]->cacert);
2613
2614 mbedtls_free(m_context->pki_sni_entry_list[i]);
2615 }
2616 if (m_context->pki_sni_entry_list)
2617 mbedtls_free(m_context->pki_sni_entry_list);
2618
2619 for (i = 0; i < m_context->psk_sni_count; i++) {
2620 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2621 }
2622 if (m_context->psk_sni_entry_list)
2623 mbedtls_free(m_context->psk_sni_entry_list);
2624
2625 if (m_context->root_ca_path)
2626 mbedtls_free(m_context->root_ca_path);
2627 if (m_context->root_ca_file)
2628 mbedtls_free(m_context->root_ca_file);
2629
2630 mbedtls_free(m_context);
2631}
2632
2633#if COAP_CLIENT_SUPPORT
2634void *
2635coap_dtls_new_client_session(coap_session_t *c_session) {
2636#if !defined(MBEDTLS_SSL_CLI_C)
2637 (void)c_session;
2638 coap_log_emerg("coap_dtls_new_client_session:"
2639 " libcoap not compiled for Client Mode for Mbed TLS"
2640 " - update Mbed TLS to include Client Mode\n");
2641 return NULL;
2642#else /* MBEDTLS_SSL_CLI_C */
2643 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2646 int ret;
2647
2648 if (m_env) {
2649 coap_tick_t now;
2650
2651 coap_ticks(&now);
2652 m_env->last_timeout = now;
2653 ret = do_mbedtls_handshake(c_session, m_env);
2654 if (ret == -1) {
2655 coap_dtls_free_mbedtls_env(m_env);
2656 return NULL;
2657 }
2658 }
2659 return m_env;
2660#endif /* MBEDTLS_SSL_CLI_C */
2661}
2662#endif /* COAP_CLIENT_SUPPORT */
2663
2664#if COAP_SERVER_SUPPORT
2665void *
2666coap_dtls_new_server_session(coap_session_t *c_session) {
2667#if !defined(MBEDTLS_SSL_SRV_C)
2668 (void)c_session;
2669 coap_log_emerg("coap_dtls_new_server_session:"
2670 " libcoap not compiled for Server Mode for Mbed TLS"
2671 " - update Mbed TLS to include Server Mode\n");
2672 return NULL;
2673#else /* MBEDTLS_SSL_SRV_C */
2674 coap_mbedtls_env_t *m_env =
2675 (coap_mbedtls_env_t *)c_session->tls;
2676 if (m_env) {
2677#if defined(MBEDTLS_SSL_PROTO_DTLS)
2678#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2679 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2680#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2681#endif /* MBEDTLS_SSL_PROTO_DTLS */
2682 }
2683 return m_env;
2684#endif /* MBEDTLS_SSL_SRV_C */
2685}
2686#endif /* COAP_SERVER_SUPPORT */
2687
2688void
2690 if (c_session && c_session->context && c_session->tls) {
2691 coap_dtls_free_mbedtls_env(c_session->tls);
2692 c_session->tls = NULL;
2694 }
2695 return;
2696}
2697
2698void
2700#if defined(MBEDTLS_SSL_PROTO_DTLS)
2701 coap_mbedtls_env_t *m_env =
2702 (coap_mbedtls_env_t *)c_session->tls;
2703 if (m_env) {
2704#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2705 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu);
2706#endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */
2707 }
2708#else /* ! MBEDTLS_SSL_PROTO_DTLS */
2709 (void)c_session;
2710#endif /* MBEDTLS_SSL_PROTO_DTLS */
2711}
2712
2713ssize_t
2715 const uint8_t *data, size_t data_len) {
2716 int ret;
2717 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2718
2719 if (!m_env) {
2720 errno = ENOTCONN;
2721 return -1;
2722 }
2723 c_session->dtls_event = -1;
2724 coap_log_debug("* %s: dtls: sent %4d bytes\n",
2725 coap_session_str(c_session), (int)data_len);
2726 if (m_env->established) {
2727 ret = mbedtls_ssl_write(&m_env->ssl, (const unsigned char *) data, data_len);
2728 if (ret <= 0) {
2729 switch (ret) {
2730 case MBEDTLS_ERR_SSL_WANT_READ:
2731 case MBEDTLS_ERR_SSL_WANT_WRITE:
2732 ret = 0;
2733 break;
2734 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2736 ret = -1;
2737 break;
2738 default:
2739 coap_log_warn("coap_dtls_send: "
2740 "returned -0x%x: '%s'\n",
2741 -ret, get_error_string(ret));
2742 ret = -1;
2743 break;
2744 }
2745 if (ret == -1) {
2746 coap_log_warn("coap_dtls_send: cannot send PDU\n");
2747 }
2748 }
2749 } else {
2750 ret = do_mbedtls_handshake(c_session, m_env);
2751 if (ret == 1) {
2752 /* Just connected, so send the data */
2753 return coap_dtls_send(c_session, data, data_len);
2754 }
2755 ret = -1;
2756 }
2757
2758 if (c_session->dtls_event >= 0) {
2759 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2760 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2761 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2762 ret = -1;
2763 }
2764 }
2765 return ret;
2766}
2767
2768int
2770 return 0;
2771}
2772
2774coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
2775 return 0;
2776}
2777
2780 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2781#ifdef __ZEPHYR__
2782 int ret = zephyr_timing_get_delay(&m_env->timer);
2783#else
2784 int ret = mbedtls_timing_get_delay(&m_env->timer);
2785#endif
2786 unsigned int scalar = 1 << m_env->retry_scalar;
2787
2788 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2789 switch (ret) {
2790 case 0:
2791 /* int_ms has not timed out */
2792 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2793 /* Need to indicate remaining timeout time */
2794 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2795 }
2796 m_env->last_timeout = now;
2797 /* This may cause a minor extra delay */
2798 return now + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2799 case 1:
2800 /* int_ms has timed out, but not fin_ms */
2801 /*
2802 * Need to make sure that we do not do this too frequently
2803 */
2804 if (m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2805 return m_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2806 }
2807
2808 /* Reset for the next time */
2809 m_env->last_timeout = now;
2810 return now;
2811 case 2:
2812 /* fin_ms has timed out - timed out - one final try */
2813 return now;
2814 default:
2815 break;
2816 }
2817
2818 return 0;
2819}
2820
2821/*
2822 * return 1 timed out
2823 * 0 still timing out
2824 */
2825int
2827 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2828
2829 if (m_env == NULL) {
2830 errno = ENOTCONN;
2831 return 1;
2832 }
2833 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2834 m_env->retry_scalar++;
2835 if ((++c_session->dtls_timeout_count > c_session->max_retransmit) ||
2836 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2837 /* Too many retries */
2839 return 1;
2840 }
2841 return 0;
2842}
2843
2844static void
2845coap_dtls_receive_done(coap_ssl_t *ssl_data, int ret) {
2846 if (ssl_data->pdu_len) {
2847 /* pdu data is held on stack which will not stay there */
2848 coap_log_debug("coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2849 ssl_data->pdu_len = 0;
2850 ssl_data->pdu = NULL;
2851 }
2852}
2853
2854/*
2855 * return +ve data amount
2856 * 0 no more
2857 * -1 error
2858 */
2859int
2861 const uint8_t *data,
2862 size_t data_len) {
2863 int ret = 1;
2864
2865 c_session->dtls_event = -1;
2866 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2867 coap_ssl_t *ssl_data;
2868
2869 if (m_env == NULL) {
2870 errno = ENOTCONN;
2871 return -1;
2872 }
2873
2874 ssl_data = &m_env->coap_ssl_data;
2875 if (ssl_data->pdu_len) {
2876 coap_log_err("** %s: Previous data not read %u bytes\n",
2877 coap_session_str(c_session), ssl_data->pdu_len);
2878 }
2879 ssl_data->pdu = data;
2880 ssl_data->pdu_len = (unsigned)data_len;
2881
2882 if (m_env->established) {
2883#if COAP_CONSTRAINED_STACK
2884 /* pdu can be protected by global_lock if needed */
2885 static uint8_t pdu[COAP_RXBUFFER_SIZE];
2886#else /* ! COAP_CONSTRAINED_STACK */
2887 uint8_t pdu[COAP_RXBUFFER_SIZE];
2888#endif /* ! COAP_CONSTRAINED_STACK */
2889
2890 ret = mbedtls_ssl_read(&m_env->ssl, pdu, sizeof(pdu));
2891 if (ret > 0) {
2892 coap_log_debug("* %s: dtls: recv %4d bytes\n",
2893 coap_session_str(c_session), ret);
2894 coap_dtls_receive_done(ssl_data, ret);
2895 /* May close c_session->tls, so ssl_data is no longer used */
2896 return coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret);
2897 }
2898 switch (ret) {
2899 case 0:
2900 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2901 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2903 break;
2904 case MBEDTLS_ERR_SSL_WANT_READ:
2905 break;
2906 default:
2907 coap_log_warn("coap_dtls_receive: "
2908 "returned -0x%x: '%s' (length %" PRIdS ")\n",
2909 -ret, get_error_string(ret), data_len);
2910 break;
2911 }
2912 ret = -1;
2913 } else {
2914 ret = do_mbedtls_handshake(c_session, m_env);
2915 if (ret == 1) {
2916 /* Just connected, so send the data */
2917 coap_session_connected(c_session);
2918 } else {
2919 if (ssl_data->pdu_len) {
2920 /* Do the handshake again in case of internal timeout */
2921 ret = do_mbedtls_handshake(c_session, m_env);
2922 if (ret == 1) {
2923 /* Just connected, so send the data */
2924 coap_session_connected(c_session);
2925 }
2926 }
2927 ret = -1;
2928 }
2929 }
2930 if (c_session->dtls_event >= 0) {
2931 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2932 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2933 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2935 ssl_data = NULL;
2936 ret = -1;
2937 }
2938 }
2939 if (ssl_data)
2940 coap_dtls_receive_done(ssl_data, ret);
2941 return ret;
2942}
2943
2944#if COAP_SERVER_SUPPORT
2945/*
2946 * return -1 failure
2947 * 0 not completed
2948 * 1 client hello seen
2949 */
2950int
2951coap_dtls_hello(coap_session_t *c_session,
2952 const uint8_t *data,
2953 size_t data_len) {
2954#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2955 (void)c_session;
2956 (void)data;
2957 (void)data_len;
2958 coap_log_emerg("coap_dtls_hello:"
2959 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2960 " - update Mbed TLS to include DTLS and Server Mode\n");
2961 return -1;
2962#else /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
2963 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
2964 coap_ssl_t *ssl_data;
2965 int ret;
2966
2967 if (!m_env) {
2968 m_env = coap_dtls_new_mbedtls_env(c_session, COAP_DTLS_ROLE_SERVER,
2970 if (m_env) {
2971 c_session->tls = m_env;
2972 } else {
2973 /* error should have already been reported */
2974 return -1;
2975 }
2976 }
2977
2978 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2979 (unsigned char *)&c_session->addr_info.remote,
2980 sizeof(c_session->addr_info.remote))) != 0) {
2981 coap_log_err("mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2982 -ret, get_error_string(ret));
2983 return -1;
2984 }
2985
2986 ssl_data = &m_env->coap_ssl_data;
2987 if (ssl_data->pdu_len) {
2988 coap_log_err("** %s: Previous data not read %u bytes\n",
2989 coap_session_str(c_session), ssl_data->pdu_len);
2990 }
2991 ssl_data->pdu = data;
2992 ssl_data->pdu_len = (unsigned)data_len;
2993
2994 ret = do_mbedtls_handshake(c_session, m_env);
2995 if (ret == 0 || m_env->seen_client_hello) {
2996 /* The test for seen_client_hello gives the ability to setup a new
2997 c_session to continue the do_mbedtls_handshake past the client hello
2998 and safely allow updating of the m_env and separately
2999 letting a new session cleanly start up.
3000 */
3001 m_env->seen_client_hello = 0;
3002 ret = 1;
3003 } else {
3004 ret = 0;
3005 }
3006
3007 if (ssl_data->pdu_len) {
3008 /* pdu data is held on stack which will not stay there */
3009 coap_log_debug("coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
3010 ssl_data->pdu_len = 0;
3011 ssl_data->pdu = NULL;
3012 }
3013 return ret;
3014#endif /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */
3015}
3016#endif /* COAP_SERVER_SUPPORT */
3017
3018unsigned int
3020 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3021 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
3022
3023 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
3024 return 13 + 8 + 8;
3025 }
3026 return expansion;
3027}
3028
3029#if !COAP_DISABLE_TCP
3030#if COAP_CLIENT_SUPPORT
3031void *
3032coap_tls_new_client_session(coap_session_t *c_session) {
3033#if !defined(MBEDTLS_SSL_CLI_C)
3034 (void)c_session;
3035 *connected = 0;
3036 coap_log_emerg("coap_tls_new_client_session:"
3037 " libcoap not compiled for Client Mode for Mbed TLS"
3038 " - update Mbed TLS to include Client Mode\n");
3039 return NULL;
3040#else /* MBEDTLS_SSL_CLI_C */
3041 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
3044 coap_tick_t now;
3045 coap_ticks(&now);
3046
3047 if (!m_env)
3048 return NULL;
3049
3050 m_env->last_timeout = now;
3051 c_session->tls = m_env;
3052 do_mbedtls_handshake(c_session, m_env);
3053 return m_env;
3054#endif /* MBEDTLS_SSL_CLI_C */
3055}
3056#endif /* COAP_CLIENT_SUPPORT */
3057
3058#if COAP_SERVER_SUPPORT
3059void *
3060coap_tls_new_server_session(coap_session_t *c_session) {
3061#if !defined(MBEDTLS_SSL_SRV_C)
3062 (void)c_session;
3063 (void)connected;
3064
3065 coap_log_emerg("coap_tls_new_server_session:"
3066 " libcoap not compiled for Server Mode for Mbed TLS"
3067 " - update Mbed TLS to include Server Mode\n");
3068 return NULL;
3069#else /* MBEDTLS_SSL_SRV_C */
3070 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
3073
3074 if (!m_env)
3075 return NULL;
3076
3077 c_session->tls = m_env;
3078 do_mbedtls_handshake(c_session, m_env);
3079 return m_env;
3080#endif /* MBEDTLS_SSL_SRV_C */
3081}
3082#endif /* COAP_SERVER_SUPPORT */
3083
3084void
3086 coap_dtls_free_session(c_session);
3087 return;
3088}
3089
3090/*
3091 * strm
3092 * return +ve Number of bytes written.
3093 * -1 Error (error in errno).
3094 */
3095ssize_t
3096coap_tls_write(coap_session_t *c_session, const uint8_t *data,
3097 size_t data_len) {
3098 int ret = 0;
3099 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3100 size_t amount_sent = 0;
3101
3102 if (!m_env) {
3103 errno = ENOTCONN;
3104 return -1;
3105 }
3106 c_session->dtls_event = -1;
3107 if (m_env->established) {
3108 while (amount_sent < data_len) {
3109 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
3110 data_len - amount_sent);
3111 if (ret <= 0) {
3112 switch (ret) {
3113 case MBEDTLS_ERR_SSL_WANT_READ:
3114 case MBEDTLS_ERR_SSL_WANT_WRITE:
3115 if (amount_sent)
3116 ret = amount_sent;
3117 else
3118 ret = 0;
3119 c_session->sock.flags |= COAP_SOCKET_WANT_WRITE;
3120 break;
3121 case MBEDTLS_ERR_NET_CONN_RESET:
3122 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
3124 break;
3125 default:
3126 coap_log_warn("coap_tls_write: "
3127 "returned -0x%x: '%s'\n",
3128 -ret, get_error_string(ret));
3129 ret = -1;
3130 break;
3131 }
3132 if (ret == -1) {
3133 coap_log_warn("coap_tls_write: cannot send PDU\n");
3134 }
3135 break;
3136 }
3137 amount_sent += ret;
3138 /* Report all TLS records written, not just the final record. */
3139 ret = (int)amount_sent;
3140 }
3141 } else {
3142 ret = do_mbedtls_handshake(c_session, m_env);
3143 if (ret != 1) {
3144 ret = -1;
3145 }
3146 }
3147
3148 if (c_session->dtls_event >= 0) {
3149 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3150 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3151 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3152 ret = -1;
3153 }
3154 }
3155 if (ret > 0) {
3156 if (ret == (ssize_t)data_len)
3157 coap_log_debug("* %s: tls: sent %4d bytes\n",
3158 coap_session_str(c_session), ret);
3159 else
3160 coap_log_debug("* %s: tls: sent %4d of %4" PRIdS " bytes\n",
3161 coap_session_str(c_session), ret, data_len);
3162 }
3163 return ret;
3164}
3165
3166/*
3167 * strm
3168 * return >=0 Number of bytes read.
3169 * -1 Error (error in errno).
3170 */
3171ssize_t
3172coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) {
3173 int ret = -1;
3174
3175 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls;
3176
3177 if (!m_env) {
3178 errno = ENOTCONN;
3179 return -1;
3180 }
3181
3182 c_session->dtls_event = -1;
3183
3184 if (!m_env->established && !m_env->sent_alert) {
3185 ret = do_mbedtls_handshake(c_session, m_env);
3186 }
3187
3188 if (c_session->state != COAP_SESSION_STATE_NONE && m_env->established) {
3189 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
3190 if (ret <= 0) {
3191 switch (ret) {
3192 case 0:
3193 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3195 ret = -1;
3196 break;
3197 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
3198 /* Stop the sending of an alert on closedown */
3199 m_env->sent_alert = 1;
3201 break;
3202#if MBEDTLS_VERSION_NUMBER >= 0x03060000
3203 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
3204#endif /* MBEDTLS_VERSION_NUMBER >= 0x03060000 */
3205 case MBEDTLS_ERR_SSL_WANT_READ:
3206 errno = EAGAIN;
3207 ret = 0;
3208 break;
3209 default:
3210 coap_log_warn("coap_tls_read: "
3211 "returned -0x%x: '%s' (length %" PRIdS ")\n",
3212 -ret, get_error_string(ret), data_len);
3213 ret = -1;
3214 break;
3215 }
3216 } else if (ret < (int)data_len) {
3217 c_session->sock.flags &= ~COAP_SOCKET_CAN_READ;
3218 }
3219 }
3220
3221 if (c_session->dtls_event >= 0) {
3222 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3223 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3224 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3226 ret = -1;
3227 }
3228 }
3229 if (ret > 0) {
3230 coap_log_debug("* %s: tls: recv %4d bytes\n",
3231 coap_session_str(c_session), ret);
3232 }
3233 return ret;
3234}
3235#endif /* !COAP_DISABLE_TCP */
3236
3237void
3238coap_dtls_startup(void) {
3239#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
3240 psa_crypto_init();
3241#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
3242}
3243
3244void
3245coap_dtls_shutdown(void) {
3246#if COAP_CLIENT_SUPPORT
3247 mbedtls_free(psk_ciphers);
3248 mbedtls_free(pki_ciphers);
3249 mbedtls_free(ecjpake_ciphers);
3250 psk_ciphers = NULL;
3251 pki_ciphers = NULL;
3252 ecjpake_ciphers = NULL;
3253 processed_ciphers = 0;
3254#endif /* COAP_CLIENT_SUPPORT */
3256#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C)
3257 mbedtls_psa_crypto_free();
3258#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */
3259}
3260
3261
3262void *
3263coap_dtls_get_tls(const coap_session_t *c_session,
3264 coap_tls_library_t *tls_lib) {
3265 if (tls_lib)
3266 *tls_lib = COAP_TLS_LIBRARY_MBEDTLS;
3267 if (c_session && c_session->tls) {
3268 coap_mbedtls_env_t *m_env;
3269
3270 /* To get around const issue */
3271 memcpy(&m_env, &c_session->tls, sizeof(m_env));
3272
3273 return (void *)&m_env->ssl;
3274 }
3275 return NULL;
3276}
3277
3278static coap_log_t keep_log_level = COAP_LOG_EMERG;
3279
3280void
3282#if !defined(ESPIDF_VERSION)
3283 int use_level;
3284 /*
3285 * Mbed TLS debug levels filter
3286 * 0 No debug
3287 * 1 Error
3288 * 2 State change
3289 * 3 Informational
3290 * 4 Verbose
3291 */
3292 switch ((int)level) {
3293 case COAP_LOG_EMERG:
3294 use_level = 0;
3295 break;
3296 case COAP_LOG_ALERT:
3297 case COAP_LOG_CRIT:
3298 case COAP_LOG_ERR:
3299 case COAP_LOG_WARN:
3300 use_level = 1;
3301 break;
3302 case COAP_LOG_NOTICE:
3303 use_level = 2;
3304 break;
3305 case COAP_LOG_INFO:
3306 use_level = 3;
3307 break;
3308 case COAP_LOG_DEBUG:
3309 default:
3310 use_level = 4;
3311 break;
3312 }
3313 mbedtls_debug_set_threshold(use_level);
3314#endif /* !ESPIDF_VERSION) */
3315 keep_log_level = level;
3316}
3317
3320 return keep_log_level;
3321}
3322
3323#endif /* COAP_WITH_LIBMBEDTLS */
3324
3325#if COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE
3326
3327#if COAP_SERVER_SUPPORT
3328coap_digest_ctx_t *
3329coap_digest_setup(void) {
3330 coap_crypto_sha256_ctx_t *digest_ctx = mbedtls_malloc(sizeof(coap_crypto_sha256_ctx_t));
3331
3332 if (digest_ctx) {
3333 if (coap_crypto_sha256_init(digest_ctx) != 0) {
3334 coap_digest_free(digest_ctx);
3335 return NULL;
3336 }
3337 }
3338 return digest_ctx;
3339}
3340
3341void
3342coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3343 if (digest_ctx) {
3344 coap_crypto_sha256_free(digest_ctx);
3345 mbedtls_free(digest_ctx);
3346 }
3347}
3348
3349int
3350coap_digest_update(coap_digest_ctx_t *digest_ctx,
3351 const uint8_t *data,
3352 size_t data_len) {
3353 return coap_crypto_sha256_update(digest_ctx, data, data_len) == 0;
3354}
3355
3356int
3357coap_digest_final(coap_digest_ctx_t *digest_ctx,
3358 coap_digest_t *digest_buffer) {
3359 int ret = coap_crypto_sha256_finish(digest_ctx, (uint8_t *)digest_buffer) == 0;
3360 coap_digest_free(digest_ctx);
3361 return ret;
3362}
3363#endif /* COAP_SERVER_SUPPORT */
3364
3365#ifndef MBEDTLS_CIPHER_MODE_AEAD
3366#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
3367#endif /* MBEDTLS_CIPHER_MODE_AEAD */
3368
3369#ifdef MBEDTLS_ERROR_C
3370#include <mbedtls/error.h>
3371#endif /* MBEDTLS_ERROR_C */
3372
3373#ifdef MBEDTLS_ERROR_C
3374#define C(Func) \
3375 do { \
3376 int c_tmp = (int)(Func); \
3377 if (c_tmp != 0) { \
3378 char error_buf[64]; \
3379 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
3380 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
3381 goto error; \
3382 } \
3383 } while (0);
3384#else /* !MBEDTLS_ERROR_C */
3385#define C(Func) \
3386 do { \
3387 int c_tmp = (int)(Func); \
3388 if (c_tmp != 0) { \
3389 coap_log_err("mbedtls: -0x%04x\n", -c_tmp); \
3390 goto error; \
3391 } \
3392 } while (0);
3393#endif /* !MBEDTLS_ERROR_C */
3394
3395#if COAP_OSCORE_GROUP_SUPPORT
3396/*
3397 * The struct hash_algs and the function get_hash_alg() are used to
3398 * determine which hash type to use for creating the required hash object.
3399 */
3400static struct hash_algs {
3401 cose_alg_t alg;
3402 mbedtls_md_type_t hash_type;
3403 size_t hash_size;
3404} hashes[] = {
3405 {COSE_ALGORITHM_SHA_1, MBEDTLS_MD_SHA1, 20},
3406 {COSE_ALGORITHM_SHA_256_256, MBEDTLS_MD_SHA256, 32},
3407 {COSE_ALGORITHM_SHA_512, MBEDTLS_MD_SHA512, 64},
3408};
3409
3410static mbedtls_md_type_t
3411get_hash_alg(cose_alg_t alg, size_t *hash_len) {
3412 size_t idx;
3413
3414 for (idx = 0; idx < sizeof(hashes) / sizeof(struct hash_algs); idx++) {
3415 if (hashes[idx].alg == alg) {
3416 *hash_len = hashes[idx].hash_size;
3417 return hashes[idx].hash_type;
3418 }
3419 }
3420 coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg);
3421 return MBEDTLS_MD_NONE;
3422}
3423#endif /* COAP_OSCORE_GROUP_SUPPORT */
3424
3425#if COAP_WS_SUPPORT || COAP_OSCORE_GROUP_SUPPORT
3426int
3428 const coap_bin_const_t *data,
3429 coap_bin_const_t **hash) {
3430 coap_crypto_md_type_t md_type;
3431 size_t hash_len;
3432
3433 switch ((int)alg) {
3435 md_type = COAP_CRYPTO_MD_SHA1;
3436 break;
3438 md_type = COAP_CRYPTO_MD_SHA256;
3439 break;
3441 md_type = COAP_CRYPTO_MD_SHA512;
3442 break;
3443 default:
3444 coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg);
3445 return 0;
3446 }
3447
3448 hash_len = coap_crypto_hash_size(md_type);
3449 if (hash_len == 0)
3450 return 0;
3451
3452 coap_binary_t *dummy = coap_new_binary(hash_len);
3453 if (dummy == NULL)
3454 return 0;
3455
3456 if (coap_crypto_hash_compute(md_type, data->s, data->length,
3457 dummy->s, hash_len, NULL) != 0) {
3459 return 0;
3460 }
3461
3462 *hash = (coap_bin_const_t *)dummy;
3463 return 1;
3464}
3465#endif /* COAP_WS_SUPPORT || COAP_OSCORE_GROUP_SUPPORT */
3466
3467/* HMAC Functions */
3468
3469#if COAP_OSCORE_SUPPORT
3470static int
3471coap_crypto_hmac_compute(coap_crypto_md_type_t md_type,
3472 const uint8_t *key, size_t key_len,
3473 const uint8_t *input, size_t ilen,
3474 uint8_t *output, size_t output_size,
3475 size_t *output_len) {
3476#if COAP_USE_PSA_CRYPTO
3477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3478 psa_key_id_t key_id;
3479 psa_algorithm_t psa_alg = PSA_ALG_HMAC(md_type);
3480 size_t mac_len;
3481 psa_status_t status;
3482
3483 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3484 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3485 psa_set_key_algorithm(&attributes, psa_alg);
3486
3487 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3488 return -1;
3489 }
3490
3491 status = psa_mac_compute(key_id, psa_alg, input, ilen,
3492 output, output_size, &mac_len);
3493 psa_destroy_key(key_id);
3494
3495 if (output_len)
3496 *output_len = mac_len;
3497 return (status == PSA_SUCCESS) ? 0 : -1;
3498#else
3499 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
3500 if (!md_info)
3501 return -1;
3502 size_t mac_size = mbedtls_md_get_size(md_info);
3503 if (mac_size > output_size)
3504 return -1;
3505 if (output_len)
3506 *output_len = mac_size;
3507 return mbedtls_md_hmac(md_info, key, key_len, input, ilen, output);
3508#endif
3509}
3510
3511/* AEAD (AES-CCM) Functions */
3512
3513static int
3514coap_crypto_aead_encrypt_ccm(const uint8_t *key, size_t key_len,
3515 const uint8_t *nonce, size_t nonce_len,
3516 const uint8_t *aad, size_t aad_len,
3517 const uint8_t *plaintext, size_t plaintext_len,
3518 uint8_t *ciphertext, size_t ciphertext_size,
3519 size_t *ciphertext_len, size_t tag_len) {
3520#if COAP_USE_PSA_CRYPTO
3521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3522 psa_key_id_t key_id;
3523 psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len);
3524 size_t output_len;
3525 psa_status_t status;
3526
3527 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
3528 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3529 psa_set_key_algorithm(&attributes, psa_alg);
3530 psa_set_key_bits(&attributes, key_len * 8);
3531
3532 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3533 return -1;
3534 }
3535
3536 status = psa_aead_encrypt(key_id, psa_alg,
3537 nonce, nonce_len,
3538 aad, aad_len,
3539 plaintext, plaintext_len,
3540 ciphertext, ciphertext_size, &output_len);
3541 psa_destroy_key(key_id);
3542
3543 if (ciphertext_len)
3544 *ciphertext_len = output_len;
3545 return (status == PSA_SUCCESS) ? 0 : -1;
3546#else
3547 mbedtls_cipher_context_t ctx;
3548 const mbedtls_cipher_info_t *cipher_info;
3549 mbedtls_cipher_type_t cipher_type;
3550 int ret = -1;
3551 size_t result_len = ciphertext_size;
3552
3553 if (key_len == 16) {
3554 cipher_type = MBEDTLS_CIPHER_AES_128_CCM;
3555 } else if (key_len == 32) {
3556 cipher_type = MBEDTLS_CIPHER_AES_256_CCM;
3557 } else {
3558 return -1;
3559 }
3560
3561 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3562 if (!cipher_info)
3563 return -1;
3564
3565 mbedtls_cipher_init(&ctx);
3566 if (mbedtls_cipher_setup(&ctx, cipher_info) != 0)
3567 goto cleanup;
3568 if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_ENCRYPT) != 0)
3569 goto cleanup;
3570
3571#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3572 {
3573 unsigned char tag[16];
3574 if (mbedtls_cipher_auth_encrypt(&ctx,
3575 nonce, nonce_len,
3576 aad, aad_len,
3577 plaintext, plaintext_len,
3578 ciphertext, &result_len,
3579 tag, tag_len) != 0) {
3580 goto cleanup;
3581 }
3582 if ((result_len + tag_len) > ciphertext_size)
3583 goto cleanup;
3584 memcpy(ciphertext + result_len, tag, tag_len);
3585 result_len += tag_len;
3586 }
3587#else
3588 if (mbedtls_cipher_auth_encrypt_ext(&ctx,
3589 nonce, nonce_len,
3590 aad, aad_len,
3591 plaintext, plaintext_len,
3592 ciphertext, ciphertext_size,
3593 &result_len, tag_len) != 0) {
3594 goto cleanup;
3595 }
3596#endif
3597
3598 if (ciphertext_len)
3599 *ciphertext_len = result_len;
3600 ret = 0;
3601
3602cleanup:
3603 mbedtls_cipher_free(&ctx);
3604 return ret;
3605#endif
3606}
3607
3608static int
3609coap_crypto_aead_decrypt_ccm(const uint8_t *key, size_t key_len,
3610 const uint8_t *nonce, size_t nonce_len,
3611 const uint8_t *aad, size_t aad_len,
3612 const uint8_t *ciphertext, size_t ciphertext_len,
3613 uint8_t *plaintext, size_t plaintext_size,
3614 size_t *plaintext_len, size_t tag_len) {
3615#if COAP_USE_PSA_CRYPTO
3616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3617 psa_key_id_t key_id;
3618 psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len);
3619 size_t output_len;
3620 psa_status_t status;
3621
3622 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
3623 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3624 psa_set_key_algorithm(&attributes, psa_alg);
3625 psa_set_key_bits(&attributes, key_len * 8);
3626
3627 if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) {
3628 return -1;
3629 }
3630
3631 status = psa_aead_decrypt(key_id, psa_alg,
3632 nonce, nonce_len,
3633 aad, aad_len,
3634 ciphertext, ciphertext_len,
3635 plaintext, plaintext_size, &output_len);
3636 psa_destroy_key(key_id);
3637
3638 if (plaintext_len)
3639 *plaintext_len = output_len;
3640 return (status == PSA_SUCCESS) ? 0 : -1;
3641#else
3642 mbedtls_cipher_context_t ctx;
3643 const mbedtls_cipher_info_t *cipher_info;
3644 mbedtls_cipher_type_t cipher_type;
3645 int ret = -1;
3646 size_t result_len = plaintext_size;
3647
3648 if (key_len == 16) {
3649 cipher_type = MBEDTLS_CIPHER_AES_128_CCM;
3650 } else if (key_len == 32) {
3651 cipher_type = MBEDTLS_CIPHER_AES_256_CCM;
3652 } else {
3653 return -1;
3654 }
3655
3656 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3657 if (!cipher_info)
3658 return -1;
3659
3660 mbedtls_cipher_init(&ctx);
3661 if (mbedtls_cipher_setup(&ctx, cipher_info) != 0)
3662 goto cleanup;
3663 if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_DECRYPT) != 0)
3664 goto cleanup;
3665
3666#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3667 {
3668 const unsigned char *tag = ciphertext + ciphertext_len - tag_len;
3669 if (mbedtls_cipher_auth_decrypt(&ctx,
3670 nonce, nonce_len,
3671 aad, aad_len,
3672 ciphertext, ciphertext_len - tag_len,
3673 plaintext, &result_len,
3674 tag, tag_len) != 0) {
3675 goto cleanup;
3676 }
3677 }
3678#else
3679 if (mbedtls_cipher_auth_decrypt_ext(&ctx,
3680 nonce, nonce_len,
3681 aad, aad_len,
3682 ciphertext, ciphertext_len,
3683 plaintext, plaintext_size,
3684 &result_len, tag_len) != 0) {
3685 goto cleanup;
3686 }
3687#endif
3688
3689 if (plaintext_len)
3690 *plaintext_len = result_len;
3691 ret = 0;
3692
3693cleanup:
3694 mbedtls_cipher_free(&ctx);
3695 return ret;
3696#endif
3697}
3698
3699/* End of Crypto Abstraction Functions */
3700
3701int
3703 return 1;
3704}
3705
3706int
3708#if COAP_OSCORE_GROUP_SUPPORT
3709 return 1;
3710#else /* !COAP_OSCORE_GROUP_SUPPORT */
3711 return 0;
3712#endif /* !COAP_OSCORE_GROUP_SUPPORT */
3713}
3714
3715int
3717#if COAP_OSCORE_GROUP_SUPPORT
3718 return 0;
3719#else /* !COAP_OSCORE_GROUP_SUPPORT */
3720 return 0;
3721#endif /* !COAP_OSCORE_GROUP_SUPPORT */
3722}
3723
3724/*
3725 * Helper to check if COSE cipher algorithm is supported and get key size.
3726 */
3727static int
3728get_cipher_key_size(cose_alg_t alg, size_t *key_size) {
3729 switch ((int)alg) {
3731 if (key_size)
3732 *key_size = 16;
3733 return 1;
3735 if (key_size)
3736 *key_size = 32;
3737 return 1;
3738 default:
3739 coap_log_debug("get_cipher_key_size: COSE cipher %d not supported\n", alg);
3740 return 0;
3741 }
3742}
3743
3744/*
3745 * Helper to get HMAC algorithm parameters from COSE HMAC algorithm.
3746 */
3747static int
3748get_hmac_params(cose_hmac_alg_t hmac_alg,
3749 coap_crypto_md_type_t *md_type,
3750 size_t *mac_len) {
3751 switch ((int)hmac_alg) {
3753 if (md_type)
3754 *md_type = COAP_CRYPTO_MD_SHA256;
3755 if (mac_len)
3756 *mac_len = 32;
3757 return 1;
3759 if (md_type)
3760 *md_type = COAP_CRYPTO_MD_SHA384;
3761 if (mac_len)
3762 *mac_len = 48;
3763 return 1;
3765 if (md_type)
3766 *md_type = COAP_CRYPTO_MD_SHA512;
3767 if (mac_len)
3768 *mac_len = 64;
3769 return 1;
3770 default:
3771 coap_log_debug("get_hmac_params: COSE HMAC %d not supported\n", hmac_alg);
3772 return 0;
3773 }
3774}
3775
3776#if COAP_OSCORE_GROUP_SUPPORT
3777
3778/*
3779 * The struct curve_algs and the function get_curve_alg() are used to
3780 * determine which curve type to use for creating the required curve
3781 * output object.
3782 */
3783static struct curve_algs {
3784 cose_curve_t alg;
3785 int get_curve;
3786} curves[] = {
3787 {COSE_CURVE_P_256, MBEDTLS_ECP_DP_SECP256R1},
3788 {COSE_CURVE_SECP256K1, MBEDTLS_ECP_DP_SECP256K1},
3789 {COSE_CURVE_X25519, MBEDTLS_ECP_DP_CURVE25519},
3790};
3791
3792static int
3793get_curve_alg(cose_curve_t alg) {
3794 size_t idx;
3795
3796 for (idx = 0; idx < sizeof(curves) / sizeof(struct curve_algs); idx++) {
3797 if (curves[idx].alg == alg)
3798 return curves[idx].get_curve;
3799 }
3800 coap_log_debug("get_curve_alg: COSE curve %d not supported\n", alg);
3801 return 0;
3802}
3803
3804#if 0
3805static cose_curve_t
3806get_alg_curve(int curve) {
3807 size_t idx;
3808
3809 for (idx = 0; idx < sizeof(curves) / sizeof(struct curve_algs); idx++) {
3810 if (curves[idx].get_curve == curve)
3811 return curves[idx].alg;
3812 }
3813 coap_log_debug("get_alg_curve: MbedTLS curve %d not supported\n", curve);
3814 return 0;
3815}
3816#endif
3817
3818int
3819coap_crypto_check_curve_alg(cose_curve_t alg) {
3820 return get_curve_alg(alg) != 0;
3821}
3822
3823int
3824coap_crypto_check_hash_alg(cose_alg_t alg) {
3825 size_t hash_length;
3826
3827 return get_hash_alg(alg, &hash_length) != 0;
3828}
3829#endif /* COAP_OSCORE_GROUP_SUPPORT */
3830
3831int
3833 return get_cipher_key_size(alg, NULL);
3834}
3835
3836int
3838 cose_hmac_alg_t hmac_alg;
3839
3840 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
3841 return 0;
3842 return get_hmac_params(hmac_alg, NULL, NULL);
3843}
3844
3845int
3847 coap_bin_const_t *data,
3848 coap_bin_const_t *aad,
3849 uint8_t *result,
3850 size_t *max_result_len) {
3851 const coap_crypto_aes_ccm_t *ccm;
3852
3853 if (data == NULL)
3854 return 0;
3855
3856 assert(params != NULL);
3857
3858 if (!params) {
3859 return 0;
3860 }
3861
3862 if (!get_cipher_key_size(params->alg, NULL)) {
3863 coap_log_debug("coap_crypto_aead_encrypt: algorithm %d not supported\n",
3864 params->alg);
3865 return 0;
3866 }
3867
3868 ccm = &params->params.aes;
3869
3870 if (coap_crypto_aead_encrypt_ccm(ccm->key.s, ccm->key.length,
3871 ccm->nonce, 15 - ccm->l,
3872 aad ? aad->s : NULL, aad ? aad->length : 0,
3873 data->s, data->length,
3874 result, *max_result_len, max_result_len,
3875 ccm->tag_len) != 0) {
3876 coap_log_debug("coap_crypto_aead_encrypt: encryption failed\n");
3877 return 0;
3878 }
3879
3880 return 1;
3881}
3882
3883int
3885 coap_bin_const_t *data,
3886 coap_bin_const_t *aad,
3887 uint8_t *result,
3888 size_t *max_result_len) {
3889 const coap_crypto_aes_ccm_t *ccm;
3890
3891 if (data == NULL)
3892 return 0;
3893
3894 assert(params != NULL);
3895
3896 if (!params) {
3897 return 0;
3898 }
3899
3900 if (!get_cipher_key_size(params->alg, NULL)) {
3901 coap_log_debug("coap_crypto_aead_decrypt: algorithm %d not supported\n",
3902 params->alg);
3903 return 0;
3904 }
3905
3906 ccm = &params->params.aes;
3907
3908 if (data->length < ccm->tag_len) {
3909 coap_log_err("coap_decrypt: invalid tag length\n");
3910 return 0;
3911 }
3912
3913 if (coap_crypto_aead_decrypt_ccm(ccm->key.s, ccm->key.length,
3914 ccm->nonce, 15 - ccm->l,
3915 aad ? aad->s : NULL, aad ? aad->length : 0,
3916 data->s, data->length,
3917 result, *max_result_len, max_result_len,
3918 ccm->tag_len) != 0) {
3919 coap_log_debug("coap_crypto_aead_decrypt: decryption failed\n");
3920 return 0;
3921 }
3922
3923 return 1;
3924}
3925
3926int
3928 coap_bin_const_t *key,
3929 coap_bin_const_t *data,
3930 coap_bin_const_t **hmac) {
3931 coap_crypto_md_type_t md_type;
3932 size_t mac_len;
3934
3935 assert(key);
3936 assert(data);
3937 assert(hmac);
3938
3939 if (!get_hmac_params(hmac_alg, &md_type, &mac_len)) {
3940 coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3941 return 0;
3942 }
3943
3944 dummy = coap_new_binary(mac_len);
3945 if (dummy == NULL)
3946 return 0;
3947
3948 if (coap_crypto_hmac_compute(md_type, key->s, key->length,
3949 data->s, data->length,
3950 dummy->s, mac_len, NULL) != 0) {
3951 coap_log_debug("coap_crypto_hmac: computation failed\n");
3953 return 0;
3954 }
3955
3956 *hmac = (coap_bin_const_t *)dummy;
3957 return 1;
3958}
3959
3960#if COAP_OSCORE_GROUP_SUPPORT
3961
3962static int
3963fix_pivate_key_info(mbedtls_pk_context *pk_ctx, coap_crypto_pri_key_t *local) {
3964 int ret;
3965 uint8_t buf[200];
3966
3967 memset(local, 0, sizeof(*local));
3968 local->key_tls = pk_ctx;
3969
3970 ret = mbedtls_pk_write_key_der(pk_ctx, buf, sizeof(buf));
3971 if (ret <= 0)
3972 goto error;
3973 local->pri_der = coap_new_bin_const(&buf[sizeof(buf) - ret], ret);
3974 if (local->pri_der == NULL)
3975 goto error;
3976
3977#if 0
3978 switch ((int)mbedtls_pk_get_type(pk_ctx)) {
3979 case MBEDTLS_PK_ECKEY:
3980 local->wire_sign_size = 64;
3981 break;
3982 default:
3983 coap_log_warn("Fix MbedTLS key type %d\n",
3984 mbedtls_pk_get_type(pk_ctx));
3985 break;
3986 }
3987#endif
3988 return 1;
3989
3990error:
3991 if (local->pri_der) {
3993 local->pri_der = NULL;
3994 }
3995 return 0;
3996}
3997
3998int
3999coap_crypto_read_pem_private_key(const char *filename,
4000 coap_crypto_pri_key_t **private) {
4001 mbedtls_pk_context *pk_ctx = mbedtls_malloc(sizeof(mbedtls_pk_context));
4002 coap_crypto_pri_key_t *local = mbedtls_malloc(sizeof(coap_crypto_pri_key_t));
4003 int pk_ctx_init = 0;
4004
4005 if (pk_ctx == NULL || local == NULL)
4006 goto error;
4007
4008 mbedtls_pk_init(pk_ctx);
4009 pk_ctx_init = 1;
4010#if 1
4011#if defined(MBEDTLS_2_X_COMPAT) || COAP_USE_PSA_CRYPTO
4012 C(mbedtls_pk_parse_keyfile(pk_ctx, filename, NULL));
4013#else
4014 C(mbedtls_pk_parse_keyfile(pk_ctx, filename, NULL, coap_rng, NULL));
4015#endif /* !(MBEDTLS_2_X_COMPAT || COAP_USE_PSA_CRYPTO) */
4016#endif
4017#if COAP_USE_PSA_CRYPTO
4018 C(mbedtls_pk_parse_keyfile(pk_ctx, filename, NULL));
4019#elif defined(MBEDTLS_2_X_COMPAT)
4020 C(mbedtls_pk_parse_keyfile(pk_ctx, filename, NULL);
4021#else
4022 mbedtls_ctr_drbg_context ctr_drbg;
4023 mbedtls_entropy_context entropy;
4024 mbedtls_ssl_config conf;
4025 int ret;
4026
4027 mbedtls_ctr_drbg_init(&ctr_drbg);
4028 mbedtls_entropy_init(&entropy);
4029 mbedtls_ssl_config_init(&conf);
4030 mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
4031 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
4032
4033 ret = mbedtls_pk_parse_keyfile(pk_ctx, filename, NULL, coap_rng, (void *)&ctr_drbg);
4034 mbedtls_entropy_free(&entropy);
4035 mbedtls_ctr_drbg_free(&ctr_drbg);
4036 mbedtls_ssl_config_free(&conf);
4037 if (ret != 0) {
4038 coap_log_err("mbedtls: -0x%04x\n", -ret);
4039 goto error;
4040 }
4041#endif
4042
4043 if (fix_pivate_key_info(pk_ctx, local) == 0)
4044 goto error;
4045
4046 *private = local;
4047 return 1;
4048
4049error:
4050 mbedtls_free(local);
4051 if (pk_ctx_init)
4052 mbedtls_pk_free(pk_ctx);
4053 mbedtls_free(pk_ctx);
4054 return 0;
4055}
4056
4057int
4058coap_crypto_read_asn1_private_key(coap_bin_const_t *binary,
4059 coap_crypto_pri_key_t **private) {
4060 mbedtls_pk_context *pk_ctx = mbedtls_malloc(sizeof(mbedtls_pk_context));
4061 coap_crypto_pri_key_t *local = mbedtls_malloc(sizeof(coap_crypto_pri_key_t));
4062 int pk_ctx_init = 0;
4063
4064 if (pk_ctx == NULL || local == NULL)
4065 goto error;
4066
4067 mbedtls_pk_init(pk_ctx);
4068 pk_ctx_init = 1;
4069#if defined(MBEDTLS_2_X_COMPAT) || COAP_USE_PSA_CRYPTO
4070 C(mbedtls_pk_parse_key(pk_ctx, binary->s, binary->length, NULL, 0));
4071#else
4072 C(mbedtls_pk_parse_key(pk_ctx, binary->s, binary->length, NULL, 0, coap_rng, NULL));
4073#endif /* !(MBEDTLS_2_X_COMPAT || COAP_USE_PSA_CRYPTO) */
4074
4075 if (fix_pivate_key_info(pk_ctx, local) == 0)
4076 goto error;
4077
4078 *private = local;
4079 coap_delete_bin_const(binary);
4080 return 1;
4081
4082error:
4083 mbedtls_free(local);
4084 if (pk_ctx_init)
4085 mbedtls_pk_free(pk_ctx);
4086 mbedtls_free(pk_ctx);
4087 coap_delete_bin_const(binary);
4088 return 0;
4089}
4090
4091int
4092coap_crypto_read_raw_private_key(cose_curve_t curve,
4093 coap_bin_const_t *binary,
4094 coap_crypto_pri_key_t **private) {
4095 (void)curve;
4096 (void)private;
4097
4098 coap_delete_bin_const(binary);
4099 return 0;
4100}
4101
4103coap_crypto_duplicate_private_key(coap_crypto_pri_key_t *key) {
4104 (void)key;
4105 return NULL;
4106}
4107
4108void
4109coap_crypto_delete_private_key(coap_crypto_pri_key_t *private) {
4110 if (private) {
4111 mbedtls_pk_context *pk_ctx = (mbedtls_pk_context *)private->key_tls;
4112
4113 mbedtls_pk_free(pk_ctx);
4114 mbedtls_free(pk_ctx);
4115 coap_delete_bin_const(private->pri_der);
4116 coap_delete_bin_const(private->pri_raw);
4117 mbedtls_free(private);
4118 }
4119}
4120
4121static int
4122fix_public_key_info(mbedtls_pk_context *pk_ctx, coap_crypto_cred_t *local) {
4123 int ret;
4124 uint8_t buf[200];
4125
4126 memset(local, 0, sizeof(*local));
4127 local->key_tls = pk_ctx;
4128
4129 ret = mbedtls_pk_write_pubkey_der(pk_ctx, buf, sizeof(buf));
4130 if (ret <= 0)
4131 goto error;
4132 local->pub_der = coap_new_bin_const(&buf[sizeof(buf) - ret], ret);
4133 if (local->pub_der == NULL)
4134 goto error;
4135
4136#if 0
4137 mbedtls_ecp_keypair *ecp_key_pair;
4138 const mbedtls_ecp_curve_info *m_curve;
4139
4140 switch ((int)mbedtls_pk_get_type(pk_ctx)) {
4141 case MBEDTLS_PK_ECKEY:
4142 ecp_key_pair = mbedtls_pk_ec(*pk_ctx);
4143 m_curve = mbedtls_ecp_curve_info_from_grp_id(ecp_key_pair->grp.id);
4144 local->sign_curve = get_alg_curve(m_curve->grp_id);
4145 switch ((int)local->sign_curve) {
4146 case COSE_CURVE_P_256:
4147 local->wire_sign_size = 2 * 32;
4149 break;
4151 local->wire_sign_size = 2 * 32;
4153 break;
4154 default:
4155 coap_log_warn("Fix MbedTLS for curve %d\n",
4156 local->sign_curve);
4157 break;
4158 }
4159 break;
4160 default:
4161 coap_log_warn("Fix MbedTLS key type %d\n",
4162 mbedtls_pk_get_type(pk_ctx));
4163 break;
4164 }
4165#endif
4166 return 1;
4167
4168error:
4169 if (local->pub_der) {
4171 local->pub_der = NULL;
4172 }
4173 return 0;
4174}
4175
4176int
4177coap_crypto_read_pem_public_cred(const char *filename,
4178 coap_crypto_cred_t **public) {
4179 mbedtls_pk_context *pk_ctx = mbedtls_malloc(sizeof(mbedtls_pk_context));
4180 coap_crypto_cred_t *local = mbedtls_malloc(sizeof(coap_crypto_cred_t));
4181 int pk_ctx_init = 0;
4182
4183 if (pk_ctx == NULL || local == NULL)
4184 goto error;
4185
4186 mbedtls_pk_init(pk_ctx);
4187 pk_ctx_init = 1;
4188 C(mbedtls_pk_parse_public_keyfile(pk_ctx, filename));
4189
4190 if (fix_public_key_info(pk_ctx, local) == 0)
4191 goto error;
4192
4193 *public = local;
4194 return 1;
4195
4196error:
4197 mbedtls_free(local);
4198 if (pk_ctx_init)
4199 mbedtls_pk_free(pk_ctx);
4200 mbedtls_free(pk_ctx);
4201 return 0;
4202}
4203
4204int
4205coap_crypto_read_asn1_public_cred(coap_bin_const_t *binary,
4206 coap_crypto_cred_t **public) {
4207 mbedtls_pk_context *pk_ctx = mbedtls_malloc(sizeof(mbedtls_pk_context));
4208 coap_crypto_cred_t *local = mbedtls_malloc(sizeof(coap_crypto_cred_t));
4209 int pk_ctx_init = 0;
4210
4211 if (pk_ctx == NULL || local == NULL)
4212 goto error;
4213
4214 memset(local, 0, sizeof *local);
4215 mbedtls_pk_init(pk_ctx);
4216 pk_ctx_init = 1;
4217 if (binary->s[2] == 0x02) {
4218 /* Is an Integer, not a Sequence, hence private key */
4219#if defined(MBEDTLS_2_X_COMPAT) || COAP_USE_PSA_CRYPTO
4220 C(mbedtls_pk_parse_key(pk_ctx, binary->s, binary->length, NULL, 0));
4221#else
4222 C(mbedtls_pk_parse_key(pk_ctx, binary->s, binary->length, NULL, 0, coap_rng, NULL));
4223#endif /* !(MBEDTLS_2_X_COMPAT || COAP_USE_PSA_CRYPTO) */
4224 } else {
4225 C(mbedtls_pk_parse_public_key(pk_ctx, binary->s, binary->length));
4226 }
4227
4228 if (fix_public_key_info(pk_ctx, local) == 0)
4229 goto error;
4230
4231 *public = local;
4232 coap_delete_bin_const(binary);
4233 return 1;
4234
4235error:
4236 mbedtls_free(local);
4237 if (pk_ctx_init)
4238 mbedtls_pk_free(pk_ctx);
4239 mbedtls_free(pk_ctx);
4240 coap_delete_bin_const(binary);
4241 return 0;
4242}
4243
4244int
4245coap_crypto_read_raw_public_cred(cose_curve_t curve,
4246 coap_bin_const_t *binary,
4247 coap_crypto_cred_t **public) {
4248 (void)curve;
4249 (void)public;
4250
4251 coap_delete_bin_const(binary);
4252 return 0;
4253}
4254
4256coap_crypto_duplicate_public_cred(coap_crypto_cred_t *key) {
4257 (void)key;
4258 return NULL;
4259}
4260
4261void
4262coap_crypto_delete_public_cred(coap_crypto_cred_t *public) {
4263 if (public) {
4264 mbedtls_pk_context *pk_ctx = (mbedtls_pk_context *)public->key_tls;
4265
4266 mbedtls_pk_free(pk_ctx);
4267 mbedtls_free(pk_ctx);
4268 coap_delete_bin_const(public->pub_der);
4269 coap_delete_bin_const(public->pub_raw);
4270 coap_delete_bin_const(public->auth_cred);
4271 mbedtls_free(public);
4272 }
4273}
4274
4275int
4276coap_crypto_hash_sign(cose_alg_t hash,
4277 coap_binary_t *signature,
4278 coap_bin_const_t *text,
4279 coap_crypto_pri_key_t *private_key) {
4280 size_t hash_length;
4281 mbedtls_md_type_t hash_alg = get_hash_alg(hash, &hash_length);
4282 mbedtls_pk_context *pk_ctx = (mbedtls_pk_context *)private_key->key_tls;
4283 size_t length;
4284 coap_binary_t *sign_bp = NULL;
4285
4286 if (hash_alg == 0)
4287 goto error;
4288
4289 sign_bp = coap_new_binary(private_key->wire_sign_size + 8);
4290 if (sign_bp == NULL)
4291 goto error;
4292#if defined(MBEDTLS_2_X_COMPAT)
4293 C(mbedtls_pk_sign(pk_ctx,
4294 hash_alg,
4295 text->s,
4296 text->length,
4297 sign_bp->s,
4298 &length,
4299 NULL,
4300 NULL));
4301#elif COAP_USE_PSA_CRYPTO
4302 C(mbedtls_pk_sign(pk_ctx,
4303 hash_alg,
4304 text->s,
4305 text->length,
4306 sign_bp->s,
4307 length,
4308 &length));
4309#else
4310 C(mbedtls_pk_sign(pk_ctx,
4311 hash_alg,
4312 text->s,
4313 text->length,
4314 sign_bp->s,
4315 sign_bp->length,
4316 &length,
4317 coap_rng,
4318 NULL));
4319#endif /* !(MBEDTLS_2_X_COMPAT || COAP_USE_PSA_CRYPTO) */
4320
4321 assert(sign_bp->length >= length);
4322#if 0
4323 coap_binary_t *sign;
4324
4325 switch ((int)mbedtls_pk_get_type(pk_ctx)) {
4326 case MBEDTLS_PK_ECKEY:
4327 sign_bp->length = length;
4329 "ASN.1 Sign",
4330 (coap_bin_const_t *)sign_bp);
4331 /* Need to convert asn.1 to 64 bytes */
4332 sign = coap_asn1_split_r_s(sign_bp, private_key->wire_sign_size);
4333 if (sign) {
4334 length = sign->length;
4335 memcpy(signature->s, sign->s, length);
4336 coap_delete_binary(sign);
4337 }
4338 break;
4339 default:
4340 assert(sign_bp->length >= signature->length);
4341 memcpy(signature->s, sign_bp->s, length);
4342 break;
4343 }
4344#endif
4345 coap_delete_binary(sign_bp);
4346 signature->length = length;
4347
4348#if 0
4349 const mbedtls_ecp_curve_info *m_curve = NULL;
4350 mbedtls_ctr_drbg_context ctr_drbg;
4351 mbedtls_pk_context key;
4352 m_curve = mbedtls_ecp_curve_info_from_grp_id(key_type);
4353 if (m_curve == NULL)
4354 goto error;
4355 mbedtls_pk_init(&key);
4356 C(mbedtls_ecp_gen_key(key_type, mbedtls_pk_ec(key),
4357 mbedtls_ctr_drbg_random, &ctr_drbg));
4358 ed_key = EVP_PKEY_new_raw_private_key(key_type, NULL, private_key->s,
4359 private_key->length);
4360 if (ed_key == NULL)
4361 goto error;
4362
4363 md_ctx = EVP_MD_CTX_new();
4364 if (EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, ed_key) != 1)
4365 goto error;
4366 /* Calculate the requires size for the signature by passing a NULL buffer */
4367 if (EVP_DigestSign(md_ctx, NULL, &signature->length, ciphertext->s,
4368 ciphertext->length) != 1)
4369 goto error;
4370 if ((sig = OPENSSL_zalloc(signature->length)) == NULL)
4371 goto error;
4372
4373 if (EVP_DigestSign(md_ctx, signature->s, &signature->length, ciphertext->s,
4374 ciphertext->length) != 1)
4375 goto error;
4376 OPENSSL_free(sig);
4377 EVP_MD_CTX_free(md_ctx);
4378 EVP_PKEY_free(ed_key);
4379#endif
4380 return length;
4381
4382error:
4383 return 0;
4384}
4385
4386int
4387coap_crypto_hash_verify(cose_alg_t hash,
4388 coap_binary_t *signature,
4389 coap_bin_const_t *text,
4390 coap_crypto_cred_t *public_cred) {
4391 size_t hash_length;
4392 mbedtls_md_type_t hash_alg = get_hash_alg(hash, &hash_length);
4393 mbedtls_pk_context *pk_ctx = (mbedtls_pk_context *)public_cred->key_tls;
4394
4395 if (hash_alg == 0)
4396 goto error;
4397
4398 C(mbedtls_pk_verify(pk_ctx,
4399 hash_alg,
4400 text->s,
4401 text->length,
4402 signature->s,
4403 signature->length));
4404 return 1;
4405
4406error:
4407 return 0;
4408}
4409
4410int
4411coap_crypto_gen_pkey(cose_curve_t curve,
4412 coap_bin_const_t **private,
4413 coap_bin_const_t **public) {
4414 (void)curve;
4415 (void)private;
4416 (void)public;
4417 return 0;
4418}
4419
4420int
4421coap_crypto_derive_shared_secret(cose_alg_t alg_pairwise,
4422 coap_bin_const_t *raw_local_private,
4423 coap_bin_const_t *raw_peer_public,
4424 coap_bin_const_t **shared_secret) {
4425 (void)alg_pairwise;
4426 (void)raw_local_private;
4427 (void)raw_peer_public;
4428 (void)shared_secret;
4429 return 0;
4430}
4431
4432#endif /* COAP_OSCORE_GROUP_SUPPORT */
4433
4434#endif /* COAP_OSCORE_SUPPORT */
4435
4436#else /* ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE*/
4437
4438#ifdef __clang__
4439/* Make compilers happy that do not like empty modules. As this function is
4440 * never used, we ignore -Wunused-function at the end of compiling this file
4441 */
4442#pragma GCC diagnostic ignored "-Wunused-function"
4443#endif
4444static inline void
4445dummy(void) {
4446}
4447
4448#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:969
#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:395
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
Definition coap_notls.c:507
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:579
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:502
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:521
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:441
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:539
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:411
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:434
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:490
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:567
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:486
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:403
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:516
void coap_dtls_free_context(void *handle COAP_UNUSED)
Definition coap_notls.c:464
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:482
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
Definition coap_notls.c:459
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:558
#define NULL
Definition coap_option.h:30
coap_binary_t * coap_asn1_split_r_s(coap_binary_t *asn1, size_t size)
Split the ASN.1 signature into the r and s components.
Definition coap_asn1.c:192
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:198
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:5534
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:3185
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:497
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:454
#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:449
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_OSCORE
Definition coap_debug.h:65
@ 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_curve_t
Definition oscore_cose.h:62
cose_alg_t
@ COSE_HMAC_ALG_HMAC384_384
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_CURVE_X25519
Definition oscore_cose.h:64
@ COSE_CURVE_P_256
Definition oscore_cose.h:63
@ COSE_CURVE_SECP256K1
Definition oscore_cose.h:68
@ 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
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:245
@ COAP_PROTO_DTLS
Definition coap_pdu.h:248
@ COAP_PROTO_TLS
Definition coap_pdu.h:250
@ COAP_PROTO_WSS
Definition coap_pdu.h:252
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)
The session is to be disconnected (a send on it has failed, the peer or the (D)TLS layer has closed i...
#define COAP_PROTO_NOT_RELIABLE(p)
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_NONE
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
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_group_is_supported(void)
Check whether OSCORE Group support is available.
int coap_oscore_pairwise_is_supported(void)
Check whether OSCORE Pairwise support is available.
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
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
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.
Holds credential definitions.
size_t wire_sign_size
Size of signature on wire.
cose_curve_t sign_curve
Sign Curve.
coap_bin_const_t * pub_der
DER Value of the public key abstracted from key_tls.
void * key_tls
Public Key in the used TLS format.
cose_alg_t sign_hash
Sign Hash Algorithm.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@120001245125233110152067110350100212120063070365 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorithm to use.
Holds private key definitions.
size_t wire_sign_size
Size of signature on wire.
coap_bin_const_t * pri_der
DER Value of the private key abstracted from key_tls.
void * key_tls
Private Key in the used TLS format.
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:418
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
Definition coap_dtls.h:425
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:445
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
Definition coap_dtls.h:441
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:423
The structure that holds the PKI key information.
Definition coap_dtls.h:284
coap_pki_key_define_t define
for definable type keys
Definition coap_dtls.h:291
coap_pki_key_t key_type
key format type
Definition coap_dtls.h:285
union coap_dtls_key_t::@164253005300366252035030064113114242056046345264 key
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:317
uint8_t allow_no_crl
1 ignore if CRL not there
Definition coap_dtls.h:331
void * cn_call_back_arg
Passed in to the CN callback function.
Definition coap_dtls.h:359
uint8_t allow_short_rsa_length
1 if small RSA keysizes are allowed
Definition coap_dtls.h:334
uint8_t allow_sni_cn_mismatch
1 if SNI and returned CN allowed to mismatch (Client only).
Definition coap_dtls.h:341
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
Definition coap_dtls.h:328
uint8_t allow_bad_md_hash
1 if unsupported MD hashes are allowed
Definition coap_dtls.h:333
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
Definition coap_dtls.h:338
uint8_t check_cert_revocation
1 if revocation checks wanted
Definition coap_dtls.h:330
uint8_t cert_chain_verify_depth
recommended depth is 3
Definition coap_dtls.h:329
uint8_t allow_expired_certs
1 if expired certs are allowed
Definition coap_dtls.h:327
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:322
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:376
uint8_t allow_self_signed
1 if self-signed certs are allowed.
Definition coap_dtls.h:325
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
Definition coap_dtls.h:358
uint8_t allow_expired_crl
1 if expired crl is allowed
Definition coap_dtls.h:332
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
Definition coap_dtls.h:335
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
Definition coap_dtls.h:323
The structure that holds the Server Pre-Shared Key and Identity Hint information.
Definition coap_dtls.h:458
coap_bin_const_t hint
Definition coap_dtls.h:459
The structure used for defining the Server PSK setup data to be used.
Definition coap_dtls.h:509
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
Definition coap_dtls.h:530
void * id_call_back_arg
Passed in to the Identity callback function.
Definition coap_dtls.h:531
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:514
coap_dtls_spsk_info_t psk_info
Server PSK definition.
Definition coap_dtls.h:541
coap_layer_read_t l_read
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
Definition coap_dtls.h:266
coap_const_char_ptr_t private_key
define: Private Key
Definition coap_dtls.h:267
coap_const_char_ptr_t ca
define: Common CA Certificate
Definition coap_dtls.h:265
size_t public_cert_len
define Public Cert length (if needed)
Definition coap_dtls.h:269
size_t ca_len
define CA Cert length (if needed)
Definition coap_dtls.h:268
coap_pki_define_t private_key_def
define: Private Key type definition
Definition coap_dtls.h:273
size_t private_key_len
define Private Key length (if needed)
Definition coap_dtls.h:270
coap_pki_define_t ca_def
define: Common CA type definition
Definition coap_dtls.h:271
coap_pki_define_t public_cert_def
define: Public Cert type definition
Definition coap_dtls.h:272
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
The structure used for returning the underlying (D)TLS library information.
Definition coap_dtls.h:88
uint64_t built_version
(D)TLS Built against Library Version
Definition coap_dtls.h:91
coap_tls_library_t type
Library type.
Definition coap_dtls.h:90
uint64_t version
(D)TLS runtime Library Version
Definition coap_dtls.h:89
const char * s_byte
signed char ptr
Definition coap_str.h:84
const uint8_t * u_byte
unsigned char ptr
Definition coap_str.h:85