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