libcoap 4.3.5-develop-daa4e05
Loading...
Searching...
No Matches
coap_wolfssl.c
Go to the documentation of this file.
1/*
2 * coap_wolfssl.c -- wolfSSL Transport Layer Support for libcoap
3 *
4 * Copyright (C) 2017 Jean-Claude Michelou <jcm@spinetix.com>
5 * Copyright (C) 2023 Javier Blanco <frblanco@pa.uc3m.es>
6 * Copyright (C) 2018-2025 Jon Shallow <supjps-libcoap@jpshallow.com>
7 *
8 * SPDX-License-Identifier: BSD-2-Clause
9 *
10 * This file is part of the CoAP library libcoap. Please see README for terms
11 * of use.
12 */
13
20
21#ifdef COAP_WITH_LIBWOLFSSL
22
23/*
24 * Implemented using wolfSSL's OpenSSL compatibility layer based on coap_openssl.c.
25 *
26 * It is possible to override the Ciphers, define the Algorithms or Groups
27 * to use for the SSL negotiations at compile time. This is done by the adding
28 * of the appropriate -D option to the CFLAGS parameter that is used on the
29 * ./configure command line.
30 * E.g. ./configure CFLAGS="-DXX='\"YY\"' -DUU='\"VV\"'"
31 * The parameter value is case-sensitive and needs the extra " wrapper so that
32 * it includes the "text" with quotes in the defined parameter..
33 *
34 * The (client) PKI ciphers can be overridden with (example)
35 * CFLAGS="-DCOAP_WOLFSSL_PKI_CIPHERS='\"TLS13-AES128-GCM-SHA256\"'"
36 *
37 * The (client) PSK ciphers can be overridden with (example)
38 * CFLAGS="-DCOAP_WOLFSSL_PSK_CIPHERS='\"PSK-AES128-CCM\"'"
39 *
40 * The Algorithms can be defined by (example)
41 * CFLAGS="-DCOAP_WOLFSSL_SIGALGS='\"RSA+SHA256\"'"
42 *
43 * The Groups (including post-quantum ones, if wolfSSL has been built with liboqs
44 * and DTLS 1.3 enabled) can be defined using the following example:
45 * CFLAGS="-DCOAP_WOLFSSL_GROUPS=\"\\\"P-384:P-256:KYBER_LEVEL1\\\"\"" ./configure ...
46 *
47 * wolfSSL library building (not libcoap library building)
48 *
49 * If wolfSSL is going to interoperate with TinyDTLS, then the wolfSSL library
50 * needs to be build with
51 * $ ./configure CFLAGS="-DBUILD_TLS_PSK_WITH_AES_128_CCM"
52 * as TinyDTLS currently only supports CCM.
53 *
54 * If wolfSSL debug logging is required, then the wolfSSL library needs to be built with
55 * $ ./configure --enable-debug
56 *
57 * For extra TLS debugging
58 * $./configure --enable-debug CFLAGS="-DWOLFSSL_DEBUG_TLS"
59 *
60 * If wolfSSL dtls1.3 support is required, then the wolfSSL library needs to be built with
61 * $ ./configure --enable-dtls13
62 *
63 * If wolfSSL RPK support is required, then the wolfSSL library needs to be built with
64 * $ ./configure CFLAGS="-DHAVE_RPK"
65 *
66 * If wolfSSL CID support is required, then the wolfSSL library needs to be built with
67 * $ ./configure --enable-dtls13 --enable-dtlscid CFLAGS="-DDTLS_CID_MAX_SIZE=8"
68 * NOTE: For interoperability with MbedTLS, https://github.com/wolfSSL/wolfssl/pull/7841
69 * needs to be installed.
70 *
71 * When building the wolfSSL library from scratch, it is suggested that the library
72 * built with
73 * $ ./configure --enable-all
74 * to get the needed common options, or perhaps
75 * $ ./configure --enable-all --enable-dtls13 CFLAGS="-DBUILD_TLS_PSK_WITH_AES_128_CCM -DHAVE_RPK"
76 */
77
78#include <wolfssl/options.h>
79#include <wolfssl/ssl.h>
80#include <wolfssl/wolfcrypt/settings.h>
81#include <wolfssl/openssl/ssl.h>
82#include <wolfssl/openssl/x509v3.h>
83
84#ifdef COAP_EPOLL_SUPPORT
85# include <sys/epoll.h>
86#endif /* COAP_EPOLL_SUPPORT */
87
88#if LIBWOLFSSL_VERSION_HEX < 0x05002000
89#error Must be compiled against wolfSSL 5.2.0 or later
90#endif
91
92#ifdef _WIN32
93#define strcasecmp _stricmp
94#define strncasecmp _strnicmp
95#endif
96
97/* missing definitions */
98#define WOLFSSL3_AL_FATAL 2
99#define WOLFSSL_TLSEXT_ERR_OK 0
100
101/* This structure encapsulates the wolfSSL context object. */
102typedef struct coap_dtls_context_t {
103 WOLFSSL_CTX *ctx;
104 WOLFSSL_HMAC_CTX *cookie_hmac;
105} coap_dtls_context_t;
106
107typedef struct coap_tls_context_t {
108 WOLFSSL_CTX *ctx;
109} coap_tls_context_t;
110
111#define IS_PSK 0x1
112#define IS_PKI 0x2
113
114typedef struct coap_wolfssl_context_t {
115 coap_dtls_context_t dtls;
116#if !COAP_DISABLE_TCP
117 coap_tls_context_t tls;
118#endif /* !COAP_DISABLE_TCP */
119 coap_dtls_pki_t setup_data;
120 int psk_pki_enabled;
121 char *root_ca_file;
122 char *root_ca_dir;
123 int trust_store_defined;
124} coap_wolfssl_context_t;
125
126typedef struct coap_ssl_data_t {
127 coap_session_t *session;
128 const void *pdu;
129 unsigned pdu_len;
130 unsigned peekmode;
131} coap_ssl_data_t;
132
133typedef struct coap_wolfssl_env_t {
134 WOLFSSL *ssl;
135 coap_tick_t last_timeout;
136 unsigned int retry_scalar;
137 coap_ssl_data_t data;
138 int done_psk_check;
139 coap_dtls_role_t role;
140} coap_wolfssl_env_t;
141
142typedef enum coap_enc_method_t {
143 COAP_ENC_PSK,
144 COAP_ENC_PKI,
145} coap_enc_method_t;
146
147static void *
148wolfssl_malloc(size_t size) {
149 void *ret = XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
150
151 return ret;
152}
153
154static void
155wolfssl_free(void *ptr) {
156 if (ptr)
157 XFREE(ptr, NULL, DYNAMIC_TYPE_TMP_BUFFER);
158}
159
160static char *
161wolfssl_strdup(const char *str) {
162 char *ret = (char *)wolfssl_malloc(strlen(str) + 1);
163
164 if (ret) {
165 strcpy(ret, str);
166 }
167 return ret;
168}
169
170static char *
171wolfssl_strndup(const char *str, size_t n) {
172 size_t len = strnlen(str, n);
173 char *ret = (char *)wolfssl_malloc(len + 1);
174
175 if (ret) {
176 strncpy(ret, str, len);
177 ret[len] = '\0';
178 }
179 return ret;
180}
181
182static coap_wolfssl_env_t *
183coap_dtls_new_wolfssl_env(coap_session_t *c_session, coap_dtls_role_t role) {
184 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)c_session->tls;
185
186 if (w_env)
187 return w_env;
188
189 w_env = (coap_wolfssl_env_t *)wolfssl_malloc(sizeof(coap_wolfssl_env_t));
190 if (!w_env) {
191 return NULL;
192 }
193 memset(w_env, 0, sizeof(coap_wolfssl_env_t));
194 w_env->role = role;
195 return w_env;
196}
197
198static void
199coap_dtls_free_wolfssl_env(coap_wolfssl_env_t *w_env) {
200 if (w_env) {
201 wolfssl_free(w_env);
202 }
203}
204
205#if COAP_CLIENT_SUPPORT
206#ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE
207#define WOLFSSL_CIPHER_LIST_MAX_SIZE 4096
208#endif /* WOLFSSL_CIPHER_LIST_MAX_SIZE */
209
210#ifdef COAP_WOLFSSL_PSK_CIPHERS
211static char psk_ciphers[] = COAP_WOLFSSL_PSK_CIPHERS;
212#else /* ! COAP_WOLFSSL_PSK_CIPHERS */
213static char psk_ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
214#endif /* ! COAP_WOLFSSL_PSK_CIPHERS */
215
216#ifdef COAP_WOLFSSL_PKI_CIPHERS
217static char pki_ciphers[] = COAP_WOLFSSL_PKI_CIPHERS;
218#else /* ! COAP_WOLFSSL_PKI_CIPHERS */
219static char pki_ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
220#endif /* ! COAP_WOLFSSL_PKI_CIPHERS */
221
222static void
223set_ciphersuites(WOLFSSL *ssl, coap_enc_method_t method) {
224#if ! defined(COAP_WOLFSSL_PSK_CIPHERS) || ! defined(COAP_WOLFSSL_PKI_CIPHERS)
225 static int processed_ciphers = 0;
226
227 if (!processed_ciphers) {
228 static char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
229 char *ciphers_ofs = ciphers;
230 char *cp;
231#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
232 char *psk_ofs = psk_ciphers;
233#endif /* ! COAP_WOLFSSL_PSK_CIPHERS */
234#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
235 char *pki_ofs = pki_ciphers;
236#endif /* ! COAP_WOLFSSL_PKI_CIPHERS */
237
238 if (wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers)) != WOLFSSL_SUCCESS) {
239 coap_log_warn("set_ciphersuites: Failed to get ciphers\n");
240 return;
241 }
242
243 while (ciphers_ofs) {
244 cp = strchr(ciphers_ofs, ':');
245 if (cp)
246 *cp = '\000';
247 if (strstr(ciphers_ofs, "NULL")) {
248 /* NULL type not required */
249 goto next_a;
250 }
251 if (strcmp(ciphers_ofs, "RENEGOTIATION-INFO") == 0) {
252 /* Skip for now - adding to end */
253 goto next_a;
254 } else if (strstr(ciphers_ofs, "PSK")) {
255#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
256 if (psk_ofs != psk_ciphers) {
257 psk_ofs[0] = ':';
258 psk_ofs++;
259 }
260 strcpy(psk_ofs, ciphers_ofs);
261 psk_ofs += strlen(ciphers_ofs);
262 psk_ofs[0] = '\000';
263#endif /* ! COAP_WOLFSSL_PSK_CIPHERS */
264 } else {
265#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
266 if (pki_ofs != pki_ciphers) {
267 pki_ofs[0] = ':';
268 pki_ofs++;
269 }
270 strcpy(pki_ofs, ciphers_ofs);
271 pki_ofs += strlen(ciphers_ofs);
272 pki_ofs[0] = '\000';
273#endif /* ! COAP_WOLFSSL_PKI_CIPHERS */
274 }
275next_a:
276 if (cp)
277 ciphers_ofs = cp + 1;
278 else
279 ciphers_ofs = NULL;
280 }
281#ifndef HAVE_SECURE_RENEGOTIATION
282 /*
283 * Need to add in dummy "RENEGOTIATION-INFO" at end.
284 * This addition will get ignored if the complied library does not
285 * support it.
286 */
287#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
288 if (psk_ofs != psk_ciphers) {
289 psk_ofs[0] = ':';
290 psk_ofs++;
291 }
292 strcpy(psk_ofs, "RENEGOTIATION-INFO");
293 psk_ofs += strlen("RENEGOTIATION-INFO");
294 psk_ofs[0] = '\000';
295#endif /* ! COAP_WOLFSSL_PSK_CIPHERS */
296#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
297 if (pki_ofs != pki_ciphers) {
298 pki_ofs[0] = ':';
299 pki_ofs++;
300 }
301 strcpy(pki_ofs, "RENEGOTIATION-INFO");
302 pki_ofs += strlen("RENEGOTIATION-INFO");
303 pki_ofs[0] = '\000';
304#endif /* ! COAP_WOLFSSL_PSK_CIPHERS */
305#endif /* ! HAVE_SECURE_RENEGOTIATION */
306
307 processed_ciphers = 1;
308 }
309#endif /* ! COAP_WOLFSSL_PSK_CIPHERS || ! COAP_WOLFSSL_PKI_CIPHERS */
310
311 if (method == COAP_ENC_PSK) {
312 wolfSSL_set_cipher_list(ssl, psk_ciphers);
313 } else {
314 wolfSSL_set_cipher_list(ssl, pki_ciphers);
315 }
316}
317#endif /* COAP_CLIENT_SUPPORT */
318
319#if COAP_SERVER_SUPPORT
320static int psk_tls_server_name_call_back(WOLFSSL *ssl, int *sd, void *arg);
321#endif /* COAP_SERVER_SUPPORT */
322static int tls_verify_call_back(int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx);
323
324int
326 if (wolfSSL_lib_version_hex() < 0x05002000) {
327 coap_log_warn("wolfSSL version 5.2.0 or later is required\n");
328 return 0;
329 }
330 return 1;
331}
332
333int
335#if !COAP_DISABLE_TCP
336 if (wolfSSL_lib_version_hex() < 0x05002000) {
337 coap_log_warn("wolfSSL version 5.2.0 or later is required\n");
338 return 0;
339 }
340 return 1;
341#else /* COAP_DISABLE_TCP */
342 return 0;
343#endif /* COAP_DISABLE_TCP */
344}
345
346/*
347 * return 0 failed
348 * 1 passed
349 */
350int
352 return 1;
353}
354
355/*
356 * return 0 failed
357 * 1 passed
358 */
359int
361 return 1;
362}
363
364/*
365 * return 0 failed
366 * 1 passed
367 */
368int
370 return 0;
371}
372
373/*
374 * return 0 failed
375 * 1 passed
376 */
377int
379 return 0;
380}
381
382/*
383 * return 0 failed
384 * 1 passed
385 */
386int
388#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
389 return 1;
390#else /* ! HAVE_RPK || LIBWOLFSSL_VERSION_HEX < 0x05006004 */
391 return 0;
392#endif /* ! HAVE_RPK || LIBWOLFSSL_VERSION_HEX < 0x05006004 */
393}
394
395#if COAP_CLIENT_SUPPORT
396int
397coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
398#if defined(WOLFSSL_DTLS_CID)
399 c_context->testing_cids = every;
400 return 1;
401#else /* ! WOLFSSL_DTLS_CID */
402 (void)c_context;
403 (void)every;
404 return 0;
405#endif /* ! WOLFSSL_DTLS_CID */
406}
407#endif /* COAP_CLIENT_SUPPORT */
408
411 static coap_tls_version_t version;
412 version.version = wolfSSL_lib_version_hex();
413 version.built_version = LIBWOLFSSL_VERSION_HEX;
415
416 return &version;
417}
418
419static void
420coap_wolfssl_log_func(int level, const char *text) {
421 int use_level;
422
423 switch ((int)level) {
424 case ERROR_LOG:
425 use_level = COAP_LOG_DEBUG;
426 break;
427 case INFO_LOG:
428 use_level = COAP_LOG_INFO;
429 break;
430 case ENTER_LOG:
431 use_level = COAP_LOG_INFO;
432 break;
433 case LEAVE_LOG:
434 use_level = COAP_LOG_INFO;
435 break;
436 case OTHER_LOG:
437 use_level = COAP_LOG_DEBUG;
438 break;
439 default:
440 use_level = COAP_LOG_DEBUG;
441 break;
442 }
443 coap_dtls_log(use_level, "%s\n", text);
444}
445
446void
447coap_dtls_startup(void) {
448 if (wolfSSL_library_init() != WOLFSSL_SUCCESS) {
449 coap_log_err("wolfSSL_library_init: Fail\n");
450 return;
451 }
452 wolfSSL_load_error_strings();
453 wolfSSL_SetLoggingCb(coap_wolfssl_log_func);
454 wolfSSL_Debugging_ON();
455}
456
457void
458coap_dtls_shutdown(void) {
459 wolfSSL_ERR_free_strings();
461 wolfSSL_Debugging_OFF();
462}
463
464void *
465coap_dtls_get_tls(const coap_session_t *c_session,
466 coap_tls_library_t *tls_lib) {
467 if (tls_lib)
468 *tls_lib = COAP_TLS_LIBRARY_WOLFSSL;
469 if (c_session) {
470 coap_wolfssl_env_t *w_env;
471
472 /* To get around const issue */
473 memcpy(&w_env, &c_session->tls, sizeof(w_env));
474
475 return (void *)&w_env->ssl;
476 }
477 return NULL;
478}
479
480/*
481 * Logging levels use the standard CoAP logging levels
482 */
484
485void
487 dtls_log_level = level;
488}
489
492 return dtls_log_level;
493}
494
495static int
496coap_dgram_read(WOLFSSL *ssl, char *out, int outl, void *ctx) {
497 int ret = 0;
498 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
499 coap_ssl_data_t *data = w_env ? &w_env->data : NULL;
500 coap_tick_t now;
501
502 (void)ssl;
503 if (w_env && !w_env->done_psk_check && w_env->ssl) {
504 if (wolfSSL_SSL_in_init(w_env->ssl)) {
505 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
506
507 if (name) {
508 coap_dtls_log(COAP_LOG_DEBUG," Cipher Suite: %s\n", name);
509
510 if (strstr(name, "PSK") && w_env->role == COAP_DTLS_ROLE_SERVER) {
511 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
512 w_env->done_psk_check = 1;
513 }
514 }
515 }
516 }
517 if (out != NULL) {
518 if (data != NULL && data->pdu_len > 0) {
519 if (outl < (int)data->pdu_len) {
520 memcpy(out, data->pdu, outl);
521 ret = outl;
522 } else {
523 memcpy(out, data->pdu, data->pdu_len);
524 ret = (int)data->pdu_len;
525 }
526 if (!data->peekmode) {
527 data->pdu_len = 0;
528 data->pdu = NULL;
529 }
530 coap_ticks(&now);
531 w_env->last_timeout = now;
532 } else {
533 ret = WANT_READ;
534 }
535 }
536 return ret;
537}
538
539static int
540coap_dgram_write(WOLFSSL *ssl, char *in, int inl, void *ctx) {
541 int ret = 0;
542 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
543 coap_ssl_data_t *data = w_env ? &w_env->data : NULL;
544 coap_tick_t now;
545
546 (void)ssl;
547 assert(data);
548 if (data->session) {
549 if (!coap_netif_available(data->session)
551 && data->session->endpoint == NULL
552#endif /* COAP_SERVER_SUPPORT */
553 ) {
554 /* socket was closed on client due to error */
555 errno = ECONNRESET;
556 return -1;
557 }
558 ret = (int)data->session->sock.lfunc[COAP_LAYER_TLS].l_write(data->session,
559 (const uint8_t *)in,
560 inl);
561 if (ret > 0) {
562 coap_ticks(&now);
563 w_env->last_timeout = now;
564 } else if (ret < 0) {
565 if (errno == ENOTCONN || errno == ECONNREFUSED)
566 data->session->dtls_event = COAP_EVENT_DTLS_ERROR;
567 }
568 } else {
569 ret = -1;
570 }
571 return ret;
572}
573
574#if COAP_CLIENT_SUPPORT
575static unsigned int
576coap_dtls_psk_client_callback(WOLFSSL *ssl,
577 const char *hint,
578 char *identity,
579 unsigned int max_identity_len,
580 unsigned char *psk,
581 unsigned int max_psk_len) {
582 coap_session_t *c_session;
583 coap_wolfssl_context_t *w_context;
584 coap_dtls_cpsk_t *setup_data;
585 const coap_dtls_cpsk_info_t *cpsk_info;
586 const coap_bin_const_t *psk_key;
587 const coap_bin_const_t *psk_identity;
588
589 c_session = (coap_session_t *)wolfSSL_get_app_data(ssl);
590 if (c_session == NULL)
591 return 0;
592 w_context = (coap_wolfssl_context_t *)c_session->context->dtls_context;
593 if (w_context == NULL)
594 return 0;
595 setup_data = &c_session->cpsk_setup_data;
596
597 if (setup_data->validate_ih_call_back) {
598 coap_bin_const_t temp;
599 coap_str_const_t lhint;
600
601 temp.s = hint ? (const uint8_t *)hint : (const uint8_t *)"";
602 temp.length = strlen((const char *)temp.s);
603 coap_session_refresh_psk_hint(c_session, &temp);
604
605 coap_log_debug("got psk_identity_hint: '%.*s'\n", (int)temp.length,
606 (const char *)temp.s);
607
608
609 lhint.s = temp.s;
610 lhint.length = temp.length;
611 coap_lock_callback_ret(cpsk_info, c_session->context,
612 setup_data->validate_ih_call_back(&lhint,
613 c_session,
614 setup_data->ih_call_back_arg));
615
616 if (cpsk_info == NULL)
617 return 0;
618
619 coap_session_refresh_psk_identity(c_session, &cpsk_info->identity);
620 coap_session_refresh_psk_key(c_session, &cpsk_info->key);
621 psk_identity = &cpsk_info->identity;
622 psk_key = &cpsk_info->key;
623 } else {
624 psk_identity = coap_get_session_client_psk_identity(c_session);
625 psk_key = coap_get_session_client_psk_key(c_session);
626 }
627
628 if (psk_identity == NULL || psk_key == NULL) {
629 coap_log_warn("no PSK available\n");
630 return 0;
631 }
632
633 /* identity has to be NULL terminated */
634 if (!max_identity_len)
635 return 0;
636 max_identity_len--;
637 if (psk_identity->length > max_identity_len) {
638 coap_log_warn("psk_identity too large, truncated to %d bytes\n",
639 max_identity_len);
640 } else {
641 /* Reduce to match */
642 max_identity_len = (unsigned int)psk_identity->length;
643 }
644 memcpy(identity, psk_identity->s, max_identity_len);
645 identity[max_identity_len] = '\000';
646
647 if (psk_key->length > max_psk_len) {
648 coap_log_warn("psk_key too large, truncated to %d bytes\n",
649 max_psk_len);
650 } else {
651 /* Reduce to match */
652 max_psk_len = (unsigned int)psk_key->length;
653 }
654 memcpy(psk, psk_key->s, max_psk_len);
655 return max_psk_len;
656}
657
658static unsigned int
659coap_dtls_psk_client_cs_callback(WOLFSSL *ssl, const char *hint,
660 char *identity, unsigned int max_identity_len,
661 unsigned char *psk, unsigned int max_psk_len,
662 const char *ciphersuite) {
663 int key_len = coap_dtls_psk_client_callback(ssl,
664 hint,
665 identity,
666 max_identity_len,
667 psk,
668 max_psk_len);
669
670 (void)ciphersuite;
671 return key_len;
672}
673
674#endif /* COAP_CLIENT_SUPPORT */
675
676#if COAP_SERVER_SUPPORT
677static unsigned int
678coap_dtls_psk_server_callback(
679 WOLFSSL *ssl,
680 const char *identity,
681 unsigned char *psk,
682 unsigned int max_psk_len) {
683 coap_session_t *c_session;
684 coap_dtls_spsk_t *setup_data;
685 coap_bin_const_t lidentity;
686 const coap_bin_const_t *psk_key;
687
688 c_session = (coap_session_t *)wolfSSL_get_app_data(ssl);
689 if (c_session == NULL)
690 return 0;
691
692 setup_data = &c_session->context->spsk_setup_data;
693
694 /* Track the Identity being used */
695 lidentity.s = identity ? (const uint8_t *)identity : (const uint8_t *)"";
696 lidentity.length = strlen((const char *)lidentity.s);
697 coap_session_refresh_psk_identity(c_session, &lidentity);
698
699 coap_log_debug("got psk_identity: '%.*s'\n",
700 (int)lidentity.length, (const char *)lidentity.s);
701
702 if (setup_data->validate_id_call_back) {
703 psk_key = setup_data->validate_id_call_back(&lidentity,
704 c_session,
705 setup_data->id_call_back_arg);
706
707 coap_session_refresh_psk_key(c_session, psk_key);
708 } else {
709 psk_key = coap_get_session_server_psk_key(c_session);
710 }
711
712 if (psk_key == NULL)
713 return 0;
714
715 if (psk_key->length > max_psk_len) {
716 coap_log_warn("psk_key too large, truncated to %d bytes\n",
717 max_psk_len);
718 } else {
719 /* Reduce to match */
720 max_psk_len = (unsigned int)psk_key->length;
721 }
722 memcpy(psk, psk_key->s, max_psk_len);
723 return max_psk_len;
724}
725#endif /* COAP_SERVER_SUPPORT */
726
727static const char *
728ssl_function_definition(unsigned long e) {
729 static char buff[80];
730
731 snprintf(buff, sizeof(buff), " at %s:%s",
732 wolfSSL_ERR_lib_error_string(e), wolfSSL_ERR_func_error_string(e));
733 return buff;
734}
735
736static void
737coap_dtls_info_callback(const WOLFSSL *ssl, int where, int ret) {
738 coap_session_t *session = (coap_session_t *)wolfSSL_get_app_data(ssl);
739 const char *pstr;
740 int w = where &~SSL_ST_MASK;
741
742 if (w & SSL_ST_CONNECT)
743 pstr = "wolfSSL_connect";
744 else if (w & SSL_ST_ACCEPT)
745 pstr = "wolfSSL_accept";
746 else
747 pstr = "undefined";
748
749 if (where & SSL_CB_LOOP) {
750 coap_dtls_log(COAP_LOG_DEBUG, "* %s: %s:%s\n",
751 coap_session_str(session), pstr, wolfSSL_state_string_long(ssl));
752 } else if (where & SSL_CB_ALERT) {
753 coap_log_t log_level = COAP_LOG_INFO;
754 pstr = (where & SSL_CB_READ) ? "read" : "write";
755 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == WOLFSSL3_AL_FATAL) {
757 if ((ret & 0xff) != close_notify)
758 log_level = COAP_LOG_WARN;
759 }
760
761 /* Need to let CoAP logging know why this session is dying */
762 coap_log(log_level, "* %s: SSL3 alert %s:%s:%s\n",
763 coap_session_str(session),
764 pstr,
765 wolfSSL_alert_type_string_long(ret),
766 wolfSSL_alert_desc_string_long(ret));
767 } else if (where & SSL_CB_EXIT) {
768 if (ret == 0) {
770 unsigned long e;
771 coap_dtls_log(COAP_LOG_WARN, "* %s: %s:failed in %s\n",
772 coap_session_str(session), pstr, wolfSSL_state_string_long(ssl));
773 while ((e = wolfSSL_ERR_get_error()))
774 coap_dtls_log(COAP_LOG_WARN, "* %s: %s%s\n",
775 coap_session_str(session), wolfSSL_ERR_reason_error_string(e),
776 ssl_function_definition(e));
777 }
778 } else if (ret < 0) {
780 WOLFSSL *rw_ssl;
781
782 /* Need to do this to not get a compiler warning about const parameters */
783 memcpy(&rw_ssl, &ssl, sizeof(rw_ssl));
784 int err = wolfSSL_get_error(rw_ssl, ret);
785 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE &&
786 err != WOLFSSL_ERROR_WANT_CONNECT && err != WOLFSSL_ERROR_WANT_ACCEPT &&
787 err != WOLFSSL_ERROR_WANT_X509_LOOKUP) {
788 long e;
789 coap_dtls_log(COAP_LOG_WARN, "* %s: %s:error in %s\n",
790 coap_session_str(session), pstr, wolfSSL_state_string_long(ssl));
791 while ((e = wolfSSL_ERR_get_error()))
792 coap_dtls_log(COAP_LOG_WARN, "* %s: %s%s\n",
793 coap_session_str(session), wolfSSL_ERR_reason_error_string(e),
794 ssl_function_definition(e));
795 }
796 }
797 }
798 }
799
800 if (where == SSL_CB_HANDSHAKE_START) {
801 WOLFSSL *rw_ssl;
802
803 /* Need to do this to not get a compiler warning about const parameters */
804 memcpy(&rw_ssl, &ssl, sizeof(rw_ssl));
805 if (wolfSSL_is_init_finished(rw_ssl))
807 }
808}
809
810/*
811 * strm
812 * return +ve data amount
813 * 0 no more
814 * -1 error
815 */
816static int
817coap_sock_read(WOLFSSL *ssl, char *out, int outl, void *ctx) {
818 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
819 int ret = 0;
820 coap_session_t *session = w_env ? w_env->data.session : NULL;
821
822 (void)ssl;
823 assert(session);
824 if (w_env && !w_env->done_psk_check && w_env->ssl &&
825 w_env->role == COAP_DTLS_ROLE_SERVER) {
826 if (wolfSSL_SSL_in_init(w_env->ssl)) {
827 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
828
829 if (name) {
830 coap_dtls_log(COAP_LOG_DEBUG,"Cipher Suite: %s\n", name);
831
832 if (strstr(name, "PSK")) {
833 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
834 w_env->done_psk_check = 1;
835 }
836 }
837 }
838 }
839 if (out != NULL) {
840 ret =(int)session->sock.lfunc[COAP_LAYER_TLS].l_read(session, (uint8_t *)out,
841 outl);
842 if (ret == 0) {
843 ret = WANT_READ;
844 }
845 }
846 return ret;
847}
848
849/*
850 * strm
851 * return +ve data amount
852 * 0 no more
853 * -1 error (error in errno)
854 */
855static int
856coap_sock_write(WOLFSSL *ssl, char *in, int inl, void *ctx) {
857 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
858 int ret = 0;
859 coap_session_t *session = w_env ? w_env->data.session : NULL;
860
861 (void)ssl;
862 assert(session);
863 ret = (int)session->sock.lfunc[COAP_LAYER_TLS].l_write(session,
864 (const uint8_t *)in,
865 inl);
866 /* Translate layer what returns into what wolfSSL expects */
867 if (ret == 0) {
868 ret = -1;
869 } else {
870 if (ret == -1) {
871 if ((session->state == COAP_SESSION_STATE_CSM ||
872 session->state == COAP_SESSION_STATE_HANDSHAKE) &&
873 (errno == EPIPE || errno == ECONNRESET)) {
874 /*
875 * Need to handle a TCP timing window where an agent continues with
876 * the sending of the next handshake or a CSM.
877 * However, the peer does not like a certificate and so sends a
878 * fatal alert and closes the TCP session.
879 * The sending of the next handshake or CSM may get terminated because
880 * of the closed TCP session, but there is still an outstanding alert
881 * to be read in and reported on.
882 * In this case, pretend that sending the info was fine so that the
883 * alert can be read (which effectively is what happens with DTLS).
884 */
885 ret = inl;
886 }
887 }
888 }
889 return ret;
890}
891
892static void
893coap_set_user_prefs(WOLFSSL_CTX *ctx) {
894 (void)ctx;
895
896#ifdef COAP_WOLFSSL_SIGALGS
897 wolfSSL_CTX_set1_sigalgs_list(ctx, COAP_WOLFSSL_SIGALGS);
898#endif
899#ifdef COAP_WOLFSSL_GROUPS
900 int ret;
901 ret = wolfSSL_CTX_set1_groups_list(ctx,
902 (char *) COAP_WOLFSSL_GROUPS);
903 if (ret != WOLFSSL_SUCCESS) {
904 coap_log_debug("Failed to set group list\n");
905 }
906#endif
907}
908
909/* Set up DTLS context if not alread done */
910static int
911setup_dtls_context(coap_wolfssl_context_t *w_context) {
912 if (!w_context->dtls.ctx) {
913 uint8_t cookie_secret[32];
914
915 /* Set up DTLS context */
916 w_context->dtls.ctx = wolfSSL_CTX_new(wolfDTLS_method());
917 if (!w_context->dtls.ctx)
918 goto error;
919 wolfSSL_CTX_set_min_proto_version(w_context->dtls.ctx,
920 DTLS1_2_VERSION);
921 wolfSSL_CTX_set_ex_data(w_context->dtls.ctx, 0, &w_context->dtls);
922 coap_set_user_prefs(w_context->dtls.ctx);
923 memset(cookie_secret, 0, sizeof(cookie_secret));
924 if (!wolfSSL_RAND_bytes(cookie_secret, (int)sizeof(cookie_secret))) {
926 "Insufficient entropy for random cookie generation");
927 coap_prng_lkd(cookie_secret, sizeof(cookie_secret));
928 }
929 w_context->dtls.cookie_hmac = wolfSSL_HMAC_CTX_new();
930 if (!wolfSSL_HMAC_Init_ex(w_context->dtls.cookie_hmac, cookie_secret, (int)sizeof(cookie_secret),
931 wolfSSL_EVP_sha256(), NULL))
932 goto error;
933
934 wolfSSL_CTX_set_info_callback(w_context->dtls.ctx, coap_dtls_info_callback);
935 wolfSSL_CTX_set_options(w_context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
936 wolfSSL_SetIORecv(w_context->dtls.ctx, coap_dgram_read);
937 wolfSSL_SetIOSend(w_context->dtls.ctx, coap_dgram_write);
938#ifdef WOLFSSL_DTLS_MTU
939 wolfSSL_CTX_dtls_set_mtu(w_context->dtls.ctx, COAP_DEFAULT_MTU);
940#endif /* WOLFSSL_DTLS_MTU */
941#ifdef WOLFSSL_SYS_CA_CERTS
942 if (w_context->trust_store_defined) {
943 if (!wolfSSL_CTX_load_system_CA_certs(w_context->dtls.ctx)) {
944 coap_log_warn("Unable to load trusted root CAs\n");
945 goto error;
946 }
947 }
948#endif
949 if (w_context->root_ca_file || w_context->root_ca_dir) {
950 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->dtls.ctx,
951 w_context->root_ca_file,
952 w_context->root_ca_dir,
953 w_context->setup_data.allow_expired_certs ?
954 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
955 coap_log_warn("Unable to install root CAs (%s : %s)\n",
956 w_context->root_ca_file ? w_context->root_ca_file : "NULL",
957 w_context->root_ca_dir ? w_context->root_ca_dir : "NULL");
958 goto error;
959 }
960 }
961 /* Verify Peer */
962 if (w_context->setup_data.verify_peer_cert)
963 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
964 WOLFSSL_VERIFY_PEER |
965 WOLFSSL_VERIFY_CLIENT_ONCE |
966 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
967 tls_verify_call_back);
968 else
969 wolfSSL_CTX_set_verify(w_context->dtls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
970 }
971 return 1;
972
973error:
974 coap_log_warn("wolfssl: unable to set up DTLS context\n");
975 return 0;
976}
977
978#if !COAP_DISABLE_TCP
979
980/* Set up TLS context if not alread done */
981static int
982setup_tls_context(coap_wolfssl_context_t *w_context) {
983 if (!w_context->tls.ctx) {
984 /* Set up TLS context */
985 w_context->tls.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
986 if (!w_context->tls.ctx)
987 goto error;
988 wolfSSL_CTX_set_ex_data(w_context->tls.ctx, 0, &w_context->tls);
989 wolfSSL_CTX_set_min_proto_version(w_context->tls.ctx, TLS1_VERSION);
990 coap_set_user_prefs(w_context->tls.ctx);
991 wolfSSL_CTX_set_info_callback(w_context->tls.ctx, coap_dtls_info_callback);
992 wolfSSL_SetIORecv(w_context->tls.ctx, coap_sock_read);
993 wolfSSL_SetIOSend(w_context->tls.ctx, coap_sock_write);
994#if COAP_CLIENT_SUPPORT
995 if (w_context->psk_pki_enabled & IS_PSK) {
996 wolfSSL_CTX_set_psk_client_cs_callback(w_context->tls.ctx,
997 coap_dtls_psk_client_cs_callback);
998 }
999#endif /* COAP_CLIENT_SUPPORT */
1000 if (w_context->root_ca_file || w_context->root_ca_dir) {
1001 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->tls.ctx,
1002 w_context->root_ca_file,
1003 w_context->root_ca_dir,
1004 w_context->setup_data.allow_expired_certs ?
1005 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1006 coap_log_warn("Unable to install root CAs (%s : %s)\n",
1007 w_context->root_ca_file ? w_context->root_ca_file : "NULL",
1008 w_context->root_ca_dir ? w_context->root_ca_dir : "NULL");
1009 goto error;
1010 }
1011 }
1012 /* Verify Peer */
1013#ifdef WOLFSSL_SYS_CA_CERTS
1014 if (w_context->trust_store_defined) {
1015 if (!wolfSSL_CTX_load_system_CA_certs(w_context->tls.ctx)) {
1016 coap_log_warn("Unable to load trusted root CAs\n");
1017 goto error;
1018 }
1019 }
1020#endif
1021 if (w_context->setup_data.verify_peer_cert)
1022 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1023 WOLFSSL_VERIFY_PEER |
1024 WOLFSSL_VERIFY_CLIENT_ONCE |
1025 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1026 tls_verify_call_back);
1027 else
1028 wolfSSL_CTX_set_verify(w_context->tls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1029 }
1030 return 1;
1031
1032error:
1033 coap_log_warn("wolfssl: unable to set up TLS context\n");
1034 return 0;
1035}
1036#endif /* ! COAP_DISABLE_TCP */
1037
1038void *
1040 coap_wolfssl_context_t *w_context;
1041 (void)c_context;
1042
1043 w_context = (coap_wolfssl_context_t *)wolfssl_malloc(sizeof(coap_wolfssl_context_t));
1044 if (w_context) {
1045 memset(w_context, 0, sizeof(coap_wolfssl_context_t));
1046 }
1047
1048 return w_context;
1049}
1050
1051#if COAP_SERVER_SUPPORT
1052int
1054 coap_dtls_spsk_t *setup_data
1055 ) {
1056 coap_wolfssl_context_t *w_context =
1057 ((coap_wolfssl_context_t *)c_context->dtls_context);
1058
1059 if (!setup_data || !w_context)
1060 return 0;
1061
1062 if (!setup_dtls_context(w_context))
1063 return 0;
1064#if !COAP_DISABLE_TCP
1065 if (!setup_tls_context(w_context))
1066 return 0;
1067#endif /* !COAP_DISABLE_TCP */
1068
1069 wolfSSL_CTX_set_psk_server_callback(w_context->dtls.ctx,
1070 coap_dtls_psk_server_callback);
1071
1072#if !COAP_DISABLE_TCP
1073 wolfSSL_CTX_set_psk_server_callback(w_context->tls.ctx,
1074 coap_dtls_psk_server_callback);
1075#endif /* !COAP_DISABLE_TCP */
1076 if (setup_data->psk_info.hint.s) {
1077 char hint[COAP_DTLS_HINT_LENGTH];
1078 snprintf(hint, sizeof(hint), "%.*s", (int)setup_data->psk_info.hint.length,
1079 setup_data->psk_info.hint.s);
1080 wolfSSL_CTX_use_psk_identity_hint(w_context->dtls.ctx, hint);
1081#if !COAP_DISABLE_TCP
1082 wolfSSL_CTX_use_psk_identity_hint(w_context->tls.ctx, hint);
1083#endif /* !COAP_DISABLE_TCP */
1084 }
1085 if (setup_data->validate_sni_call_back) {
1086 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1087 &c_context->spsk_setup_data);
1088 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1089 psk_tls_server_name_call_back);
1090#if !COAP_DISABLE_TCP
1091 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1092 &c_context->spsk_setup_data);
1093 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1094 psk_tls_server_name_call_back);
1095#endif /* !COAP_DISABLE_TCP */
1096 }
1097 if (setup_data->ec_jpake) {
1098 coap_log_warn("wolfSSL has no EC-JPAKE support\n");
1099 }
1100 w_context->psk_pki_enabled |= IS_PSK;
1101 return 1;
1102}
1103#endif /* COAP_SERVER_SUPPORT */
1104
1105#if COAP_CLIENT_SUPPORT
1106int
1108 coap_dtls_cpsk_t *setup_data
1109 ) {
1110 coap_wolfssl_context_t *w_context =
1111 ((coap_wolfssl_context_t *)c_context->dtls_context);
1112
1113 if (!setup_data || !w_context)
1114 return 0;
1115
1116 if (setup_data->ec_jpake) {
1117 coap_log_warn("wolfSSL has no EC-JPAKE support\n");
1118 }
1119 if (setup_data->use_cid) {
1120#if ! defined(WOLFSSL_DTLS_CID)
1121 coap_log_warn("wolfSSL has no Connection-ID support\n");
1122#endif /* ! WOLFSSL_DTLS_CID */
1123 }
1124 w_context->psk_pki_enabled |= IS_PSK;
1125 return 1;
1126}
1127#endif /* COAP_CLIENT_SUPPORT */
1128
1129#if !COAP_DISABLE_TCP
1130static uint8_t coap_alpn[] = { 4, 'c', 'o', 'a', 'p' };
1131
1132#if COAP_SERVER_SUPPORT
1133static int
1134server_alpn_callback(WOLFSSL *ssl COAP_UNUSED,
1135 const unsigned char **out,
1136 unsigned char *outlen,
1137 const unsigned char *in,
1138 unsigned int inlen,
1139 void *arg COAP_UNUSED
1140 ) {
1141 unsigned char *tout = NULL;
1142 int ret;
1143 if (inlen == 0)
1144 return SSL_TLSEXT_ERR_NOACK;
1145 ret = wolfSSL_select_next_proto(&tout,
1146 outlen,
1147 coap_alpn,
1148 sizeof(coap_alpn),
1149 in,
1150 inlen);
1151 *out = tout;
1152 return (ret != OPENSSL_NPN_NEGOTIATED) ? noack_return : WOLFSSL_TLSEXT_ERR_OK;
1153}
1154#endif /* COAP_SERVER_SUPPORT */
1155#endif /* !COAP_DISABLE_TCP */
1156
1157static int
1158setup_pki_ssl(WOLFSSL *ssl,
1159 coap_dtls_pki_t *setup_data, coap_dtls_role_t role) {
1160 coap_dtls_key_t key;
1161 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
1162
1163 /* Map over to the new define format to save code duplication */
1164 coap_dtls_map_key_type_to_define(setup_data, &key);
1165
1166 assert(key.key_type == COAP_PKI_KEY_DEFINE);
1167
1168 /*
1169 * Configure the Private Key
1170 */
1171 if (key.key.define.private_key.u_byte &&
1172 key.key.define.private_key.u_byte[0]) {
1173 switch (key.key.define.private_key_def) {
1174 case COAP_PKI_KEY_DEF_PEM: /* define private key */
1175 if (!(wolfSSL_use_PrivateKey_file(ssl,
1177 WOLFSSL_FILETYPE_PEM))) {
1180 &key, role, 0);
1181 }
1182 break;
1183 case COAP_PKI_KEY_DEF_PEM_BUF: /* define private key */
1184 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1186 (long)key.key.define.private_key_len,
1187 WOLFSSL_FILETYPE_PEM))) {
1190 &key, role, 0);
1191 }
1192 break;
1193 case COAP_PKI_KEY_DEF_RPK_BUF: /* define private key */
1194#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1195 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1197 (long)key.key.define.private_key_len,
1198 WOLFSSL_FILETYPE_PEM))) {
1201 &key, role, 0);
1202 }
1203 break;
1204#else /* ! HAVE_RPK || ! LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1207 &key, role, 0);
1208#endif /* ! HAVE_RPK || ! LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1209 case COAP_PKI_KEY_DEF_DER: /* define private key */
1210 if (!(wolfSSL_use_PrivateKey_file(ssl,
1212 WOLFSSL_FILETYPE_ASN1))) {
1215 &key, role, 0);
1216 }
1217 break;
1218 case COAP_PKI_KEY_DEF_DER_BUF: /* define private key */
1219 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1221 (long)key.key.define.private_key_len,
1222 WOLFSSL_FILETYPE_ASN1))) {
1225 &key, role, 0);
1226 }
1227 break;
1228 case COAP_PKI_KEY_DEF_PKCS11: /* define private key */
1229 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define private key */
1230 case COAP_PKI_KEY_DEF_ENGINE: /* define private key */
1231 default:
1234 &key, role, 0);
1235 }
1236 } else if (role == COAP_DTLS_ROLE_SERVER ||
1238 key.key.define.public_cert.u_byte[0])) {
1241 &key, role, 0);
1242 }
1243
1244 /*
1245 * Configure the Public Certificate / Key
1246 */
1247 if (key.key.define.public_cert.u_byte &&
1248 key.key.define.public_cert.u_byte[0]) {
1249 switch (key.key.define.public_cert_def) {
1250 case COAP_PKI_KEY_DEF_PEM: /* define public cert */
1251 if (!(wolfSSL_use_certificate_chain_file(ssl,
1252 key.key.define.public_cert.s_byte))) {
1255 &key, role, 0);
1256 }
1257 break;
1258 case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */
1259 if (!(wolfSSL_use_certificate_chain_buffer(ssl,
1261 (long)key.key.define.private_key_len))) {
1264 &key, role, 0);
1265 }
1266 break;
1267 case COAP_PKI_KEY_DEF_RPK_BUF: /* define public cert */
1268#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1269 {
1270 unsigned char der_buff[512];
1271 int ret = -1;;
1272 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1273 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1274
1275 wolfSSL_set_client_cert_type(ssl, ctype, sizeof(ctype)/sizeof(ctype[0]));
1276 wolfSSL_set_server_cert_type(ssl, stype, sizeof(stype)/sizeof(stype[0]));
1277
1278 ret = wolfSSL_PubKeyPemToDer(key.key.define.public_cert.u_byte,
1279 (int)key.key.define.public_cert_len,
1280 der_buff, (int)sizeof(der_buff));
1281 if (ret <= 0) {
1282 ret = wolfSSL_KeyPemToDer(key.key.define.public_cert.u_byte,
1283 (int)key.key.define.public_cert_len,
1284 der_buff, (int)sizeof(der_buff), NULL);
1285 if (ret > 0) {
1286 coap_binary_t *spki = get_asn1_spki(der_buff, ret);
1287
1288 if (!spki) {
1291 &key, role, 0);
1292 }
1293 if (!wolfSSL_use_PrivateKey_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1296 &key, role, 0);
1297 }
1298 if (!wolfSSL_use_certificate_buffer(ssl, spki->s, spki->length, WOLFSSL_FILETYPE_ASN1)) {
1299 coap_delete_binary(spki);
1302 &key, role, 0);
1303 }
1304 coap_delete_binary(spki);
1305 break;
1306 }
1307 }
1308 if (ret <= 0) {
1311 &key, role, 0);
1312 }
1313 if (!wolfSSL_use_certificate_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1316 &key, role, 0);
1317 }
1318 }
1319 break;
1320#else /* ! HAVE_RPK || ! LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1323 &key, role, 0);
1324#endif /* ! HAVE_RPK || ! LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1325 case COAP_PKI_KEY_DEF_DER: /* define public cert */
1326 if (!(wolfSSL_use_certificate_file(ssl,
1328 WOLFSSL_FILETYPE_ASN1))) {
1331 &key, role, 0);
1332 }
1333 break;
1334 case COAP_PKI_KEY_DEF_DER_BUF: /* define public cert */
1335 if (!(wolfSSL_use_certificate_buffer(ssl,
1337 (int)key.key.define.public_cert_len,
1338 WOLFSSL_FILETYPE_ASN1))) {
1341 &key, role, 0);
1342 }
1343 break;
1344 case COAP_PKI_KEY_DEF_PKCS11: /* define public cert */
1345 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define public cert */
1346 case COAP_PKI_KEY_DEF_ENGINE: /* define public cert */
1347 default:
1350 &key, role, 0);
1351 }
1352 } else if (role == COAP_DTLS_ROLE_SERVER ||
1354 key.key.define.private_key.u_byte[0])) {
1357 &key, role, 0);
1358 }
1359#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1360 else {
1361 char stype[] = {WOLFSSL_CERT_TYPE_X509, WOLFSSL_CERT_TYPE_RPK};
1362 wolfSSL_set_server_cert_type(ssl, stype, sizeof(stype)/sizeof(stype[0]));
1363 }
1364#endif /* HAVE_RPK && LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1365
1366 /*
1367 * Configure the CA
1368 */
1369 if (key.key.define.ca.u_byte &&
1370 key.key.define.ca.u_byte[0]) {
1371 switch (key.key.define.ca_def) {
1373 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1374 key.key.define.ca.s_byte,
1375 NULL,
1376 setup_data->allow_expired_certs ?
1377 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1380 &key, role, 0);
1381 }
1382 break;
1383 case COAP_PKI_KEY_DEF_PEM_BUF: /* define ca */
1384 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1385 key.key.define.ca.u_byte,
1386 key.key.define.ca_len,
1387 SSL_FILETYPE_PEM,
1388 0,
1389 setup_data->allow_expired_certs ?
1390 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1393 &key, role, 0);
1394 }
1395 break;
1396 case COAP_PKI_KEY_DEF_RPK_BUF: /* define ca */
1397 /* Ignore if set */
1398 break;
1399 case COAP_PKI_KEY_DEF_DER: /* define ca */
1400 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1401 key.key.define.ca.s_byte,
1402 NULL,
1403 setup_data->allow_expired_certs ?
1404 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1407 &key, role, 0);
1408 }
1409 break;
1410 case COAP_PKI_KEY_DEF_DER_BUF: /* define ca */
1411 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1412 key.key.define.ca.u_byte,
1413 key.key.define.ca_len,
1414 SSL_FILETYPE_ASN1,
1415 0,
1416 setup_data->allow_expired_certs ?
1417 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1420 &key, role, 0);
1421 }
1422 break;
1423 case COAP_PKI_KEY_DEF_PKCS11: /* define ca */
1424 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define ca */
1425 case COAP_PKI_KEY_DEF_ENGINE: /* define ca */
1426 default:
1429 &key, role, 0);
1430 }
1431 }
1432 return 1;
1433}
1434
1435static char *
1436get_san_or_cn_from_cert(WOLFSSL_X509 *x509) {
1437 if (x509) {
1438 char *cn;
1439 int n;
1440 WOLF_STACK_OF(WOLFSSL_GENERAL_NAME) *san_list;
1441 char buffer[256];
1442
1443 buffer[0] = '\000';
1444 san_list = wolfSSL_X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
1445 if (san_list) {
1446 int san_count = wolfSSL_sk_GENERAL_NAME_num(san_list);
1447
1448 for (n = 0; n < san_count; n++) {
1449 const WOLFSSL_GENERAL_NAME *name = wolfSSL_sk_GENERAL_NAME_value(san_list, n);
1450
1451 if (name->type == GEN_DNS) {
1452 const char *dns_name = (const char *)wolfSSL_ASN1_STRING_get0_data(name->d.dNSName);
1453
1454 /* Make sure that there is not an embedded NUL in the dns_name */
1455 if (wolfSSL_ASN1_STRING_length(name->d.dNSName) != (int)strlen(dns_name))
1456 continue;
1457 cn = wolfssl_strdup(dns_name);
1458 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1459 return cn;
1460 }
1461 }
1462 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1463 }
1464 /* Otherwise look for the CN= field */
1465 wolfSSL_X509_NAME_oneline(wolfSSL_X509_get_subject_name((WOLFSSL_X509 *)(x509)), buffer,
1466 sizeof(buffer));
1467
1468 /* Need to emulate strcasestr() here. Looking for CN= */
1469 n = (int)strlen(buffer) - 3;
1470 cn = buffer;
1471 while (n > 0) {
1472 if (((cn[0] == 'C') || (cn[0] == 'c')) &&
1473 ((cn[1] == 'N') || (cn[1] == 'n')) &&
1474 (cn[2] == '=')) {
1475 cn += 3;
1476 break;
1477 }
1478 cn++;
1479 n--;
1480 }
1481 if (n > 0) {
1482 char *ecn = strchr(cn, '/');
1483 if (ecn) {
1484 return wolfssl_strndup(cn, ecn-cn);
1485 } else {
1486 return wolfssl_strdup(cn);
1487 }
1488 }
1489 }
1490 return NULL;
1491}
1492
1493static int
1494tls_verify_call_back(int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx) {
1495 WOLFSSL *ssl = wolfSSL_X509_STORE_CTX_get_ex_data(ctx,
1496 wolfSSL_get_ex_data_X509_STORE_CTX_idx());
1497 coap_session_t *session = wolfSSL_get_app_data(ssl);
1498 coap_wolfssl_context_t *w_context =
1499 ((coap_wolfssl_context_t *)session->context->dtls_context);
1500 coap_dtls_pki_t *setup_data = &w_context->setup_data;
1501 int depth = wolfSSL_X509_STORE_CTX_get_error_depth(ctx);
1502 int err = wolfSSL_X509_STORE_CTX_get_error(ctx);
1503 WOLFSSL_X509 *x509 = wolfSSL_X509_STORE_CTX_get_current_cert(ctx);
1504 char *cn = NULL;
1505 int keep_preverify_ok = preverify_ok;
1506
1507 if (setup_data->is_rpk_not_cert) {
1508 cn = wolfssl_strdup("RPK");
1509 } else {
1510 cn = get_san_or_cn_from_cert(x509);
1511 }
1512 if (!preverify_ok) {
1513 switch (err) {
1514 case X509_V_ERR_CERT_NOT_YET_VALID:
1515 case X509_V_ERR_CERT_HAS_EXPIRED:
1516 case ASN_NO_SIGNER_E:
1517 case ASN_AFTER_DATE_E:
1518 if (setup_data->allow_expired_certs)
1519 preverify_ok = 1;
1520 break;
1521 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1522 if (setup_data->allow_self_signed && !setup_data->check_common_ca)
1523 preverify_ok = 1;
1524 break;
1525 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: /* Set if the CA is not known */
1526 if (!setup_data->verify_peer_cert)
1527 preverify_ok = 1;
1528 break;
1529 case X509_V_ERR_UNABLE_TO_GET_CRL:
1530 if (setup_data->allow_no_crl)
1531 preverify_ok = 1;
1532 break;
1533 case X509_V_ERR_CRL_NOT_YET_VALID:
1534 case X509_V_ERR_CRL_HAS_EXPIRED:
1535 if (setup_data->allow_expired_crl)
1536 preverify_ok = 1;
1537 break;
1538 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1539 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1540 case X509_V_ERR_AKID_SKID_MISMATCH:
1541 if (!setup_data->verify_peer_cert)
1542 preverify_ok = 1;
1543 break;
1544 default:
1545 break;
1546 }
1547 if (setup_data->cert_chain_validation &&
1548 depth > (setup_data->cert_chain_verify_depth + 1)) {
1549 preverify_ok = 0;
1550 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1551 wolfSSL_X509_STORE_CTX_set_error(ctx, err);
1552 }
1553 if (!preverify_ok) {
1554 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1555 coap_log_warn(" %s: %s: '%s' depth=%d\n",
1556 coap_session_str(session),
1557 "Unknown CA", cn ? cn : "?", depth);
1558 } else {
1559 coap_log_warn(" %s: %s: '%s' depth=%d\n",
1560 coap_session_str(session),
1561 wolfSSL_X509_verify_cert_error_string(err), cn ? cn : "?", depth);
1562 }
1563 } else {
1564 coap_log_info(" %s: %s: overridden: '%s' depth=%d\n",
1565 coap_session_str(session),
1566 wolfSSL_X509_verify_cert_error_string(err), cn ? cn : "?", depth);
1567 }
1568 }
1569 /* Certificate - depth == 0 is the Client Cert */
1570 if (setup_data->validate_cn_call_back && keep_preverify_ok) {
1571 int length = wolfSSL_i2d_X509(x509, NULL);
1572
1573 if (length > 0) {
1574 uint8_t *base_buf;
1575 uint8_t *base_buf2 = base_buf = wolfssl_malloc(length);
1576 int ret;
1577
1578 /* base_buf2 gets moved to the end */
1579 wolfSSL_i2d_X509(x509, &base_buf2);
1580 coap_lock_callback_ret(ret, session->context,
1581 setup_data->validate_cn_call_back(cn, base_buf, length, session,
1582 depth, preverify_ok,
1583 setup_data->cn_call_back_arg));
1584 if (!ret) {
1585 if (depth == 0) {
1586 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1587 } else {
1588 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1589 }
1590 preverify_ok = 0;
1591 }
1592 wolfssl_free(base_buf);
1593 }
1594 }
1595 wolfssl_free(cn);
1596 return preverify_ok;
1597}
1598
1599#if COAP_SERVER_SUPPORT
1600
1601/*
1602 * During the SSL/TLS initial negotiations, tls_server_name_call_back() is
1603 * called so it is possible to set up an extra callback to determine whether
1604 * this is a PKI or PSK incoming request and adjust the ciphers if necessary
1605 *
1606 * Set up by SSL_CTX_set_tlsext_servername_callback() in
1607 * coap_dtls_context_set_pki()
1608 */
1609static int
1610tls_server_name_call_back(WOLFSSL *ssl,
1611 int *sd COAP_UNUSED,
1612 void *arg) {
1613 coap_dtls_pki_t *setup_data = (coap_dtls_pki_t *)arg;
1614 coap_session_t *session = (coap_session_t *)wolfSSL_get_app_data(ssl);
1615 coap_wolfssl_context_t *w_context =
1616 ((coap_wolfssl_context_t *)session->context->dtls_context);
1617
1618 if (!ssl) {
1619 return noack_return;
1620 }
1621
1622 if (setup_data->validate_sni_call_back) {
1623 /* SNI checking requested */
1624 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1625 coap_dtls_pki_t sni_setup_data;
1626 coap_dtls_key_t *new_entry;
1627
1628 if (!sni || !sni[0]) {
1629 sni = "";
1630 }
1631 coap_lock_callback_ret(new_entry, session->context,
1632 setup_data->validate_sni_call_back(sni,
1633 setup_data->sni_call_back_arg));
1634 if (!new_entry) {
1635 return fatal_return;
1636 }
1637 sni_setup_data = *setup_data;
1638 sni_setup_data.pki_key = *new_entry;
1639 setup_pki_ssl(ssl, &sni_setup_data, COAP_DTLS_ROLE_SERVER);
1640 }
1641
1642 if (w_context->psk_pki_enabled & IS_PSK) {
1643 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1644 }
1645 return SSL_TLSEXT_ERR_OK;
1646}
1647
1648/*
1649 * During the SSL/TLS initial negotiations, psk_tls_server_name_call_back() is
1650 * called to see if SNI is being used.
1651 *
1652 * Set up by SSL_CTX_set_tlsext_servername_callback()
1653 * in coap_dtls_context_set_spsk()
1654 */
1655static int
1656psk_tls_server_name_call_back(WOLFSSL *ssl,
1657 int *sd COAP_UNUSED,
1658 void *arg
1659 ) {
1660 coap_dtls_spsk_t *setup_data = (coap_dtls_spsk_t *)arg;
1661 coap_session_t *c_session = (coap_session_t *)wolfSSL_get_app_data(ssl);
1662 coap_wolfssl_context_t *w_context =
1663 ((coap_wolfssl_context_t *)c_session->context->dtls_context);
1664
1665 if (!ssl) {
1666 return noack_return;
1667 }
1668
1669 if (setup_data->validate_sni_call_back) {
1670 /* SNI checking requested */
1671 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1672 char lhint[COAP_DTLS_HINT_LENGTH];
1673 const coap_dtls_spsk_info_t *new_entry;
1674
1675 if (!sni || !sni[0]) {
1676 sni = "";
1677 }
1678 coap_lock_callback_ret(new_entry, c_session->context,
1679 setup_data->validate_sni_call_back(sni,
1680 c_session,
1681 setup_data->sni_call_back_arg));
1682 if (new_entry) {
1683 coap_session_refresh_psk_key(c_session, &new_entry->key);
1684 snprintf(lhint, sizeof(lhint), "%.*s",
1685 (int)new_entry->hint.length,
1686 new_entry->hint.s);
1687 wolfSSL_use_psk_identity_hint(ssl, lhint);
1688 }
1689 }
1690
1691 if (w_context->psk_pki_enabled & IS_PSK) {
1692 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1693 }
1694 return SSL_TLSEXT_ERR_OK;
1695}
1696#endif /* COAP_SERVER_SUPPORT */
1697
1698int
1700 const coap_dtls_pki_t *setup_data,
1701 const coap_dtls_role_t role) {
1702 coap_wolfssl_context_t *w_context =
1703 ((coap_wolfssl_context_t *)ctx->dtls_context);
1704
1705 if (!setup_data)
1706 return 0;
1707
1708 w_context->setup_data = *setup_data;
1709 if (!w_context->setup_data.verify_peer_cert) {
1710 /* Needs to be clear so that no CA DNs are transmitted */
1711 w_context->setup_data.check_common_ca = 0;
1712 if (w_context->setup_data.is_rpk_not_cert) {
1713 /* Disable all of these as they cannot be checked */
1714 w_context->setup_data.allow_self_signed = 0;
1715 w_context->setup_data.allow_expired_certs = 0;
1716 w_context->setup_data.cert_chain_validation = 0;
1717 w_context->setup_data.cert_chain_verify_depth = 0;
1718 w_context->setup_data.check_cert_revocation = 0;
1719 w_context->setup_data.allow_no_crl = 0;
1720 w_context->setup_data.allow_expired_crl = 0;
1721 w_context->setup_data.allow_bad_md_hash = 0;
1722 w_context->setup_data.allow_short_rsa_length = 0;
1723 } else {
1724 /* Allow all of these but warn if issue */
1725 w_context->setup_data.allow_self_signed = 1;
1726 w_context->setup_data.allow_expired_certs = 1;
1727 w_context->setup_data.cert_chain_validation = 1;
1728 w_context->setup_data.cert_chain_verify_depth = 10;
1729 w_context->setup_data.check_cert_revocation = 1;
1730 w_context->setup_data.allow_no_crl = 1;
1731 w_context->setup_data.allow_expired_crl = 1;
1732 w_context->setup_data.allow_bad_md_hash = 1;
1733 w_context->setup_data.allow_short_rsa_length = 1;
1734 }
1735 }
1736#if COAP_SERVER_SUPPORT
1737 if (role == COAP_DTLS_ROLE_SERVER) {
1738 if (!setup_dtls_context(w_context))
1739 return 0;
1740 if (w_context->dtls.ctx) {
1741#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1742 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1743 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1744#endif /* HAVE_RPK && LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1745
1746 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1747 &w_context->setup_data);
1748 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1749 tls_server_name_call_back);
1750
1751#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1752 if (w_context->setup_data.is_rpk_not_cert) {
1753 wolfSSL_CTX_set_client_cert_type(w_context->dtls.ctx, ctype, sizeof(ctype)/sizeof(ctype[0]));
1754 wolfSSL_CTX_set_server_cert_type(w_context->dtls.ctx, stype, sizeof(stype)/sizeof(stype[0]));
1755 }
1756#endif /* HAVE_RPK && LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
1757 }
1758#if !COAP_DISABLE_TCP
1759 if (!setup_tls_context(w_context))
1760 return 0;
1761 if (w_context->tls.ctx) {
1762 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1763 &w_context->setup_data);
1764 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1765 tls_server_name_call_back);
1766
1767 /* For TLS only */
1768 wolfSSL_CTX_set_alpn_select_cb(w_context->tls.ctx,
1769 server_alpn_callback, NULL);
1770 }
1771#endif /* !COAP_DISABLE_TCP */
1772 /* Certificate Revocation */
1773 if (w_context->setup_data.check_cert_revocation) {
1774 WOLFSSL_X509_VERIFY_PARAM *param;
1775
1776 param = wolfSSL_X509_VERIFY_PARAM_new();
1777 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
1778 wolfSSL_CTX_set1_param(w_context->dtls.ctx, param);
1779#if !COAP_DISABLE_TCP
1780 wolfSSL_CTX_set1_param(w_context->tls.ctx, param);
1781#endif /* !COAP_DISABLE_TCP */
1782 wolfSSL_X509_VERIFY_PARAM_free(param);
1783 }
1784 /* Verify Peer */
1785 if (w_context->setup_data.verify_peer_cert) {
1786 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1787 WOLFSSL_VERIFY_PEER |
1788 WOLFSSL_VERIFY_CLIENT_ONCE |
1789 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1790 tls_verify_call_back);
1791#if !COAP_DISABLE_TCP
1792 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1793 WOLFSSL_VERIFY_PEER |
1794 WOLFSSL_VERIFY_CLIENT_ONCE |
1795 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1796 tls_verify_call_back);
1797#endif /* !COAP_DISABLE_TCP */
1798 } else {
1799 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1800 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1801#if !COAP_DISABLE_TCP
1802 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1803 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1804#endif /* !COAP_DISABLE_TCP */
1805 }
1806
1807 /* Check CA Chain */
1808 if (w_context->setup_data.cert_chain_validation) {
1809 wolfSSL_CTX_set_verify_depth(w_context->dtls.ctx,
1810 setup_data->cert_chain_verify_depth + 1);
1811#if !COAP_DISABLE_TCP
1812 wolfSSL_CTX_set_verify_depth(w_context->tls.ctx,
1813 setup_data->cert_chain_verify_depth + 1);
1814#endif /* !COAP_DISABLE_TCP */
1815 }
1816 }
1817#else /* ! COAP_SERVER_SUPPORT */
1818 (void)role;
1819#endif /* ! COAP_SERVER_SUPPORT */
1820
1821 w_context->psk_pki_enabled |= IS_PKI;
1822 if (setup_data->use_cid) {
1823#if ! defined(WOLFSSL_DTLS_CID)
1824 coap_log_warn("wolfSSL has no Connection-ID support\n");
1825#endif /* ! WOLFSSL_DTLS_CID */
1826 }
1827 return 1;
1828}
1829
1830int
1832 const char *ca_file,
1833 const char *ca_dir) {
1834 coap_wolfssl_context_t *w_context =
1835 ((coap_wolfssl_context_t *)ctx->dtls_context);
1836
1837 if (!w_context) {
1838 coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
1839 "not set up\n");
1840 return 0;
1841 }
1842 if (ca_file == NULL && ca_dir == NULL) {
1843 coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_dir "
1844 "not defined\n");
1845 return 0;
1846 }
1847 if (w_context->root_ca_file) {
1848 wolfssl_free(w_context->root_ca_file);
1849 w_context->root_ca_file = NULL;
1850 }
1851 if (ca_file) {
1852 w_context->root_ca_file = wolfssl_strdup(ca_file);
1853 }
1854 if (w_context->root_ca_dir) {
1855 wolfssl_free(w_context->root_ca_dir);
1856 w_context->root_ca_dir = NULL;
1857 }
1858 if (ca_dir) {
1859 w_context->root_ca_dir = wolfssl_strdup(ca_dir);
1860 }
1861 return 1;
1862}
1863
1864int
1866 coap_wolfssl_context_t *w_context =
1867 ((coap_wolfssl_context_t *)ctx->dtls_context);
1868
1869 if (!w_context) {
1870 coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment "
1871 "not set up\n");
1872 return 0;
1873 }
1874#ifdef WOLFSSL_SYS_CA_CERTS
1875 w_context->trust_store_defined = 1;
1876 return 1;
1877#else /* LIBWOLFSSL_VERSION_HEX < 0x05005002 */
1878 coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment "
1879 "not supported for wolfSSL < v5.5.2 or –enable-sys-ca-certs not defined\n");
1880#endif /* WOLFSSL_SYS_CA_CERTS */
1881}
1882
1883int
1885 coap_wolfssl_context_t *w_context =
1886 ((coap_wolfssl_context_t *)ctx->dtls_context);
1887 return w_context->psk_pki_enabled ? 1 : 0;
1888}
1889
1890void
1891coap_dtls_free_context(void *handle) {
1892 coap_wolfssl_context_t *w_context = (coap_wolfssl_context_t *)handle;
1893
1894 if (!w_context)
1895 return;
1896 wolfssl_free(w_context->root_ca_file);
1897 wolfssl_free(w_context->root_ca_dir);
1898
1899 if (w_context->dtls.ctx)
1900 wolfSSL_CTX_free(w_context->dtls.ctx);
1901 if (w_context->dtls.cookie_hmac)
1902 wolfSSL_HMAC_CTX_free(w_context->dtls.cookie_hmac);
1903
1904#if !COAP_DISABLE_TCP
1905 if (w_context->tls.ctx)
1906 wolfSSL_CTX_free(w_context->tls.ctx);
1907#endif /* !COAP_DISABLE_TCP */
1908 wolfssl_free(w_context);
1909}
1910
1911#if COAP_SERVER_SUPPORT
1912void *
1914 coap_wolfssl_context_t *w_context =
1915 ((coap_wolfssl_context_t *)session->context->dtls_context);
1916 coap_dtls_context_t *dtls;
1917 WOLFSSL *ssl = NULL;
1918 int r;
1919 const coap_bin_const_t *psk_hint;
1920 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
1921 coap_tick_t now;
1922
1923 if (!w_env)
1924 goto error;
1925
1926 if (!setup_dtls_context(w_context))
1927 goto error;
1928 dtls = &w_context->dtls;
1929
1930 ssl = wolfSSL_new(dtls->ctx);
1931 if (!ssl) {
1932 goto error;
1933 }
1934 wolfSSL_set_app_data(ssl, NULL);
1935 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
1936#ifdef WOLFSSL_DTLS_MTU
1937 wolfSSL_dtls_set_mtu(ssl, (long)session->mtu);
1938#endif /* WOLFSSL_DTLS_MTU */
1939 w_env->ssl = ssl;
1940 wolfSSL_SetIOWriteCtx(ssl, w_env);
1941 wolfSSL_SetIOReadCtx(ssl, w_env);
1942 wolfSSL_set_app_data(ssl, session);
1943 w_env->data.session = session;
1944
1945#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
1946 if (wolfSSL_send_hrr_cookie(ssl, NULL, 0) != WOLFSSL_SUCCESS)
1947 coap_log_debug("Error: Unable to set cookie with Hello Retry Request\n");
1948#endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE */
1949
1950#ifdef HAVE_SERVER_RENEGOTIATION_INFO
1951 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
1952 coap_log_debug("Error: wolfSSL_UseSecureRenegotiation failed\n");
1953 }
1954#endif /* HAVE_SERVER_RENEGOTIATION_INFO */
1955
1956 if (w_context->psk_pki_enabled & IS_PSK) {
1957 /* hint may get updated if/when handling SNI callback */
1958 psk_hint = coap_get_session_server_psk_hint(session);
1959 if (psk_hint != NULL && psk_hint->length) {
1960 char *hint = wolfssl_malloc(psk_hint->length + 1);
1961
1962 if (hint) {
1963 memcpy(hint, psk_hint->s, psk_hint->length);
1964 hint[psk_hint->length] = '\000';
1965 wolfSSL_use_psk_identity_hint(ssl, hint);
1966 wolfssl_free(hint);
1967 } else {
1968 coap_log_warn("hint malloc failure\n");
1969 }
1970 }
1971 }
1972 if (w_context->psk_pki_enabled & IS_PKI) {
1973 if (!setup_pki_ssl(ssl, &w_context->setup_data, COAP_DTLS_ROLE_SERVER))
1974 goto error;
1975 }
1976
1977#if defined(WOLFSSL_DTLS_CH_FRAG) && defined(WOLFSSL_DTLS13)
1978 if (wolfSSL_dtls13_allow_ch_frag(ssl, 1) != WOLFSSL_SUCCESS) {
1979 coap_log_debug("Error: wolfSSL_dtls13_allow_ch_frag failed\n");
1980 }
1981#endif /* WOLFSSL_DTLS_CH_FRAG && WOLFSSL_DTLS13 */
1982
1983#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
1984
1985#if COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
1986#bad COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
1987#endif /* COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE */
1988
1989 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
1990 goto error;
1991 uint8_t cid[COAP_DTLS_CID_LENGTH];
1992 /*
1993 * Enable server DTLS CID support.
1994 */
1995 coap_prng_lkd(cid, sizeof(cid));
1996 if (wolfSSL_dtls_cid_set(ssl, cid, sizeof(cid)) != WOLFSSL_SUCCESS)
1997 goto error;
1998 session->client_cid = coap_new_bin_const(cid, sizeof(cid));
1999#endif /* WOLFSSL_DTLS_CID && WOLFSSL_DTLS13 */
2000
2001 coap_ticks(&now);
2002 w_env->last_timeout = now;
2003 w_env->ssl = ssl;
2004
2005 r = wolfSSL_accept(ssl);
2006 if (r == -1) {
2007 int err = wolfSSL_get_error(ssl, r);
2008 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE)
2009 r = 0;
2010 }
2011
2012 if (r == 0) {
2013 goto error;
2014 }
2015
2016 return w_env;
2017
2018error:
2019 if (ssl)
2020 wolfSSL_free(ssl);
2021 coap_dtls_free_wolfssl_env(w_env);
2022 session->tls = NULL;
2023 return NULL;
2024}
2025#endif /* COAP_SERVER_SUPPORT */
2026
2027#if COAP_CLIENT_SUPPORT
2028static int
2029setup_client_ssl_session(coap_session_t *session, WOLFSSL *ssl) {
2030 coap_wolfssl_context_t *w_context =
2031 ((coap_wolfssl_context_t *)session->context->dtls_context);
2032
2033 if (w_context->psk_pki_enabled & IS_PSK) {
2034 coap_dtls_cpsk_t *setup_data = &session->cpsk_setup_data;
2035
2036 if (setup_data->validate_ih_call_back) {
2037 if (session->proto == COAP_PROTO_DTLS) {
2038 wolfSSL_set_max_proto_version(ssl,
2039 DTLS1_2_VERSION);
2040 }
2041#if !COAP_DISABLE_TCP
2042 else {
2043 wolfSSL_set_max_proto_version(ssl,
2044 TLS1_2_VERSION);
2045 wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_3);
2046 }
2047#endif /* !COAP_DISABLE_TCP */
2048 coap_log_debug("CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2049 }
2050 set_ciphersuites(ssl, COAP_ENC_PSK);
2051
2052 /* Issue SNI if requested */
2053 if (setup_data->client_sni &&
2054 wolfSSL_set_tlsext_host_name(ssl, setup_data->client_sni) != 1) {
2055 coap_log_warn("wolfSSL_set_tlsext_host_name: set '%s' failed",
2056 setup_data->client_sni);
2057 }
2058 wolfSSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2059
2060#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2061 if (setup_data->use_cid) {
2062 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2063 return 0;
2064 /*
2065 * Enable client DTLS CID negotiation.
2066 */
2067 if (wolfSSL_dtls_cid_set(ssl, NULL, 0) != WOLFSSL_SUCCESS)
2068 return 0;
2069 }
2070#endif /* WOLFSSL_DTLS_CID && WOLFSSL_DTLS13 */
2071 }
2072 if (w_context->psk_pki_enabled & IS_PKI) {
2073 coap_dtls_pki_t *setup_data = &w_context->setup_data;
2074
2075 set_ciphersuites(ssl, COAP_ENC_PKI);
2076 if (!setup_pki_ssl(ssl, setup_data, COAP_DTLS_ROLE_CLIENT))
2077 return 0;
2078 /* libcoap is managing (D)TLS connection based on setup_data options */
2079#if !COAP_DISABLE_TCP
2080 if (session->proto == COAP_PROTO_TLS)
2081 wolfSSL_set_alpn_protos(ssl, coap_alpn, sizeof(coap_alpn));
2082#endif /* !COAP_DISABLE_TCP */
2083
2084 /* Issue SNI if requested */
2085 if (setup_data->client_sni &&
2086 wolfSSL_set_tlsext_host_name(ssl, setup_data->client_sni) != 1) {
2087 coap_log_warn("wolfSSL_set_tlsext_host_name: set '%s' failed",
2088 setup_data->client_sni);
2089 }
2090 /* Certificate Revocation */
2091 if (setup_data->check_cert_revocation) {
2092 WOLFSSL_X509_VERIFY_PARAM *param;
2093
2094 param = wolfSSL_X509_VERIFY_PARAM_new();
2095 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
2096 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
2097 /* TODO: we cannot set parameters at ssl level with wolfSSL, review*/
2098 wolfSSL_CTX_set1_param(ctx, param);
2099 wolfSSL_X509_VERIFY_PARAM_free(param);
2100 }
2101 /* Verify Peer */
2102 if (setup_data->verify_peer_cert)
2103 wolfSSL_set_verify(ssl,
2104 WOLFSSL_VERIFY_PEER |
2105 WOLFSSL_VERIFY_CLIENT_ONCE |
2106 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2107 tls_verify_call_back);
2108 else
2109 wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
2110
2111 /* Check CA Chain */
2112 if (setup_data->cert_chain_validation)
2113 wolfSSL_set_verify_depth(ssl, setup_data->cert_chain_verify_depth + 1);
2114
2115#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2116 if (setup_data->use_cid) {
2117 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2118 return 0;
2119 /*
2120 * Enable client DTLS CID negotiation.
2121 */
2122 if (wolfSSL_dtls_cid_set(ssl, NULL, 0) != WOLFSSL_SUCCESS)
2123 return 0;
2124 }
2125#endif /* WOLFSSL_DTLS_CID && WOLFSSL_DTLS13 */
2126
2127 }
2128 return 1;
2129}
2130
2131void *
2133 WOLFSSL *ssl = NULL;
2134 int r;
2135 coap_wolfssl_context_t *w_context =
2136 ((coap_wolfssl_context_t *)session->context->dtls_context);
2137 coap_dtls_context_t *dtls;
2138 coap_wolfssl_env_t *w_env =
2139 coap_dtls_new_wolfssl_env(session, COAP_DTLS_ROLE_CLIENT);
2140 coap_tick_t now;
2141
2142 if (!w_env)
2143 goto error;
2144
2145 if (!setup_dtls_context(w_context))
2146 goto error;
2147 dtls = &w_context->dtls;
2148
2149 ssl = wolfSSL_new(dtls->ctx);
2150 if (!ssl) {
2151 goto error;
2152 }
2153 w_env->data.session = session;
2154 wolfSSL_set_app_data(ssl, session);
2155 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2156 wolfSSL_SetIOWriteCtx(ssl, w_env);
2157 wolfSSL_SetIOReadCtx(ssl, w_env);
2158#ifdef WOLFSSL_DTLS_MTU
2159 wolfSSL_dtls_set_mtu(ssl, (long)session->mtu);
2160#endif /* WOLFSSL_DTLS_MTU */
2161
2162 if (!setup_client_ssl_session(session, ssl))
2163 goto error;
2164#ifdef HAVE_SERVER_RENEGOTIATION_INFO
2165 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
2166 coap_log_debug("Error: wolfSSL_UseSecureRenegotiation failed\n");
2167 }
2168#endif /* HAVE_SERVER_RENEGOTIATION_INFO */
2169
2170 session->dtls_timeout_count = 0;
2171
2172#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
2173 wolfSSL_NoKeyShares(ssl);
2174#endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE */
2175 r = wolfSSL_connect(ssl);
2176 if (r == -1) {
2177 int ret = wolfSSL_get_error(ssl, r);
2178 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2179 r = 0;
2180 }
2181
2182 if (r == 0)
2183 goto error;
2184
2185 coap_ticks(&now);
2186 w_env->last_timeout = now;
2187 w_env->ssl = ssl;
2188 return w_env;
2189
2190error:
2191 if (ssl)
2192 wolfSSL_free(ssl);
2193 return NULL;
2194}
2195
2196void
2198#ifdef WOLFSSL_DTLS_MTU
2199 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2200 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2201
2202 if (ssl)
2203 wolfSSL_dtls_set_mtu(ssl, (long)session->mtu); /* Instead of SSL_set_mtu */
2204#else /* ! WOLFSSL_DTLS_MTU */
2205 (void)session;
2206#endif /* ! WOLFSSL_DTLS_MTU */
2207}
2208#endif /* COAP_CLIENT_SUPPORT */
2209
2210void
2212 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2213 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2214
2215 if (ssl) {
2216 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2217 int r = wolfSSL_shutdown(ssl);
2218 if (r == 0)
2219 wolfSSL_shutdown(ssl);
2220 }
2221 w_env->ssl = NULL;
2222 wolfSSL_free(ssl);
2223 if (session->context)
2225 }
2226 coap_dtls_free_wolfssl_env(w_env);
2227 session->tls = NULL;
2228}
2229
2230ssize_t
2232 const uint8_t *data, size_t data_len) {
2233 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2234 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2235 int r;
2236
2237 assert(ssl != NULL);
2238
2239 session->dtls_event = -1;
2240 coap_log_debug("* %s: dtls: sent %4d bytes\n",
2241 coap_session_str(session), (int)data_len);
2242 r = wolfSSL_write(ssl, data, (int)data_len);
2243
2244 if (r <= 0) {
2245 int err = wolfSSL_get_error(ssl, r);
2246 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2247 r = 0;
2248 } else {
2249 coap_log_warn("coap_dtls_send: cannot send PDU\n");
2250 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2252 else if (err == WOLFSSL_ERROR_SSL)
2254 r = -1;
2255 }
2256 }
2257
2258 if (session->dtls_event >= 0) {
2259 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2260 if (session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2261 coap_handle_event_lkd(session->context, session->dtls_event, session);
2262 if (session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2263 session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2265 r = -1;
2266 }
2267 }
2268
2269 return r;
2270}
2271
2272int
2274 return 0;
2275}
2276
2278coap_dtls_get_context_timeout(void *dtls_context) {
2279 (void)dtls_context;
2280 return 0;
2281}
2282
2285 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2286 unsigned int scalar;
2287
2288 if (!w_env)
2289 return now;
2290
2291 assert(session->state == COAP_SESSION_STATE_HANDSHAKE);
2292
2293 scalar = 1 << w_env->retry_scalar;
2294 if (w_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar > now) {
2295 /* Need to indicate remaining timeout time */
2296 return w_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS * scalar;
2297 }
2298 return now;
2299}
2300
2301/*
2302 * return 1 timed out
2303 * 0 still timing out
2304 */
2305int
2307 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2308 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2309
2310 assert(ssl != NULL && session->state == COAP_SESSION_STATE_HANDSHAKE);
2311 w_env->retry_scalar++;
2312 if (++session->dtls_timeout_count > session->max_retransmit) {
2313 /* Too many retries */
2315 return 1;
2316 }
2317 wolfSSL_dtls_retransmit(ssl);
2318 return 0;
2319}
2320
2321#if COAP_SERVER_SUPPORT
2322
2323int
2325 const uint8_t *data, size_t data_len) {
2326 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2327 coap_ssl_data_t *ssl_data;
2328
2329 if (!w_env) {
2330 w_env = coap_dtls_new_wolfssl_env(session, COAP_DTLS_ROLE_SERVER);
2331 if (w_env) {
2332 session->tls = w_env;
2333 } else {
2334 /* error should have already been reported */
2335 return -1;
2336 }
2337 }
2338
2339 ssl_data = w_env ? &w_env->data : NULL;
2340 assert(ssl_data != NULL);
2341
2342 if (ssl_data->pdu_len) {
2343 coap_log_err("** %s: Previous data not read %u bytes\n",
2344 coap_session_str(session), ssl_data->pdu_len);
2345 }
2346
2347 ssl_data->session = session;
2348 ssl_data->pdu = data;
2349 ssl_data->pdu_len = (unsigned)data_len;
2350
2351 return 1;
2352}
2353
2354#endif /* COAP_SERVER_SUPPORT */
2355
2356int
2357coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) {
2358 coap_ssl_data_t *ssl_data;
2359 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2360 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2361 int r;
2362 int in_init = wolfSSL_SSL_in_init(ssl);
2363 uint8_t pdu[COAP_RXBUFFER_SIZE];
2364
2365 assert(ssl != NULL);
2366
2367 ssl_data = &w_env->data;
2368
2369 if (ssl_data->pdu_len) {
2370 coap_log_err("** %s: Previous data not read %u bytes\n",
2371 coap_session_str(session), ssl_data->pdu_len);
2372 }
2373 ssl_data->pdu = data;
2374 ssl_data->pdu_len = (unsigned)data_len;
2375
2376 session->dtls_event = -1;
2377 r = wolfSSL_read(ssl, pdu, (int)sizeof(pdu));
2378 if (r > 0) {
2379 coap_log_debug("* %s: dtls: recv %4d bytes\n",
2380 coap_session_str(session), r);
2381 r = coap_handle_dgram(session->context, session, pdu, (size_t)r);
2382 goto finished;
2383 } else {
2384 int err = wolfSSL_get_error(ssl, r);
2385 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2386 if (in_init && wolfSSL_is_init_finished(ssl)) {
2387 coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
2388 coap_session_str(session), wolfSSL_get_cipher((ssl)));
2389#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) && COAP_CLIENT_SUPPORT
2390 if (session->type == COAP_SESSION_TYPE_CLIENT &&
2391 session->proto == COAP_PROTO_DTLS) {
2392 if (wolfSSL_dtls_cid_is_enabled(ssl)) {
2393 session->negotiated_cid = 1;
2394 } else {
2395 coap_log_info("** %s: CID was not negotiated\n", coap_session_str(session));
2396 session->negotiated_cid = 0;
2397 }
2398 }
2399#endif /* WOLFSSL_DTLS_CID && WOLFSSL_DTLS13 && COAP_CLIENT_SUPPORT */
2400 if (!strcmp(wolfSSL_get_version(ssl), "DTLSv1.3")) {
2401 session->is_dtls13 = 1;
2402 } else {
2403 session->is_dtls13 = 0;
2404 }
2406 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2407 }
2408 r = 0;
2409 } else if (err == APP_DATA_READY) {
2410 r = wolfSSL_read(ssl, pdu, (int)sizeof(pdu));
2411 if (r > 0) {
2412 r = coap_handle_dgram(session->context, session, pdu, (size_t)r);
2413 goto finished;
2414 }
2416 r = -1;
2417 } else {
2418 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2419 /* Got a close notify alert from the remote side */
2421 } else {
2423 if (err == FATAL_ERROR) {
2424 WOLFSSL_ALERT_HISTORY h;
2425
2426 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2427 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2428 coap_log_warn("***%s: Alert '%d': %s\n",
2429 coap_session_str(session), h.last_rx.code,
2430 wolfSSL_alert_desc_string_long(h.last_rx.code));
2431 }
2432 }
2433 }
2434 }
2435 r = -1;
2436 }
2437 if (session->dtls_event >= 0) {
2438 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2439 if (session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2440 coap_handle_event_lkd(session->context, session->dtls_event, session);
2441 if (session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2442 session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2444 ssl_data = NULL;
2445 r = -1;
2446 }
2447 }
2448 }
2449
2450finished:
2451 if (ssl_data && ssl_data->pdu_len) {
2452 /* pdu data is held on stack which will not stay there */
2453 coap_log_debug("coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
2454 ssl_data->pdu_len = 0;
2455 ssl_data->pdu = NULL;
2456 }
2457 return r;
2458}
2459
2460unsigned int
2462 unsigned int overhead = 37;
2463 const WOLFSSL_CIPHER *s_ciph = NULL;
2464 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2465 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2466
2467 if (ssl != NULL)
2468 s_ciph = wolfSSL_get_current_cipher(ssl);
2469 if (s_ciph) {
2470 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
2471
2472 const WOLFSSL_EVP_CIPHER *e_ciph;
2473 const WOLFSSL_EVP_MD *e_md;
2474 char cipher[128];
2475
2476 e_ciph = wolfSSL_EVP_get_cipherbynid(wolfSSL_CIPHER_get_cipher_nid(s_ciph));
2477
2478 switch (WOLFSSL_EVP_CIPHER_mode(e_ciph)) {
2479
2480 case WOLFSSL_EVP_CIPH_GCM_MODE:
2481#ifndef WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN
2482#define WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN 8
2483#endif
2484#ifndef WOLFSSL_EVP_GCM_TLS_TAG_LEN
2485#define WOLFSSL_EVP_GCM_TLS_TAG_LEN 16
2486#endif
2487 ivlen = WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN;
2488 maclen = WOLFSSL_EVP_GCM_TLS_TAG_LEN;
2489 break;
2490
2491 case WOLFSSL_EVP_CIPH_CCM_MODE:
2492#ifndef WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN
2493#define WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN 8
2494#endif
2495 ivlen = WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN;
2496 wolfSSL_CIPHER_description(s_ciph, cipher, sizeof(cipher));
2497 if (strstr(cipher, "CCM8"))
2498 maclen = 8;
2499 else
2500 maclen = 16;
2501 break;
2502
2503 case WOLFSSL_EVP_CIPH_CBC_MODE:
2504 e_md = wolfSSL_EVP_get_digestbynid(wolfSSL_CIPHER_get_digest_nid(s_ciph));
2505 blocksize = wolfSSL_EVP_CIPHER_block_size(e_ciph);
2506 ivlen = wolfSSL_EVP_CIPHER_iv_length(e_ciph);
2507 pad = 1;
2508 maclen = wolfSSL_EVP_MD_size(e_md);
2509 break;
2510
2511 case WOLFSSL_EVP_CIPH_STREAM_CIPHER:
2512 /* Seen with PSK-CHACHA20-POLY1305 */
2513 ivlen = 8;
2514 maclen = 8;
2515 break;
2516
2517 default:
2518 wolfSSL_CIPHER_description(s_ciph, cipher, sizeof(cipher));
2519 coap_log_warn("Unknown overhead for DTLS with cipher %s\n",
2520 cipher);
2521 ivlen = 8;
2522 maclen = 16;
2523 break;
2524 }
2525#ifndef WOLFSSL_DTLS13_RT_HEADER_LENGTH
2526#define WOLFSSL_DTLS13_RT_HEADER_LENGTH 13
2527#endif
2528 overhead = WOLFSSL_DTLS13_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 +
2529 pad;
2530 }
2531 return overhead;
2532}
2533
2534#if !COAP_DISABLE_TCP
2535#if COAP_CLIENT_SUPPORT
2536void *
2538 WOLFSSL *ssl = NULL;
2539 int r;
2540 coap_wolfssl_context_t *w_context =
2541 ((coap_wolfssl_context_t *)session->context->dtls_context);
2542 coap_tls_context_t *tls;
2543 coap_wolfssl_env_t *w_env =
2544 coap_dtls_new_wolfssl_env(session, COAP_DTLS_ROLE_CLIENT);
2545 coap_tick_t now;
2546
2547 if (!w_env)
2548 goto error;
2549
2550 if (!setup_tls_context(w_context))
2551 goto error;
2552 tls = &w_context->tls;
2553
2554 ssl = wolfSSL_new(tls->ctx);
2555 if (!ssl)
2556 goto error;
2557 wolfSSL_SetIOWriteCtx(ssl, w_env);
2558 wolfSSL_SetIOReadCtx(ssl, w_env);
2559 wolfSSL_set_app_data(ssl, session);
2560 w_env->data.session = session;
2561
2562 if (!setup_client_ssl_session(session, ssl))
2563 return 0;
2564
2565 session->tls = w_env;
2566 w_env->ssl = ssl;
2567 r = wolfSSL_connect(ssl);
2568 if (r == -1) {
2569 int ret = wolfSSL_get_error(ssl, r);
2570 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2571 r = 0;
2572 if (ret == WOLFSSL_ERROR_WANT_READ)
2573 session->sock.flags |= COAP_SOCKET_WANT_READ;
2574 if (ret == WOLFSSL_ERROR_WANT_WRITE) {
2575 session->sock.flags |= COAP_SOCKET_WANT_WRITE;
2576#ifdef COAP_EPOLL_SUPPORT
2577 coap_epoll_ctl_mod(&session->sock,
2578 EPOLLOUT |
2579 ((session->sock.flags & COAP_SOCKET_WANT_READ) ?
2580 EPOLLIN : 0),
2581 __func__);
2582#endif /* COAP_EPOLL_SUPPORT */
2583 }
2584 }
2585
2586 if (r == 0)
2587 goto error;
2588
2589 coap_ticks(&now);
2590 w_env->last_timeout = now;
2591 if (wolfSSL_is_init_finished(ssl)) {
2593 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2594 }
2595
2596 return w_env;
2597
2598error:
2599 coap_dtls_free_wolfssl_env(w_env);
2600 if (ssl)
2601 wolfSSL_free(ssl);
2602 return NULL;
2603}
2604#endif /* COAP_CLIENT_SUPPORT */
2605
2606#if COAP_SERVER_SUPPORT
2607void *
2609 WOLFSSL *ssl = NULL;
2610 coap_wolfssl_context_t *w_context =
2611 ((coap_wolfssl_context_t *)session->context->dtls_context);
2612 coap_tls_context_t *tls;
2613 int r;
2614 const coap_bin_const_t *psk_hint;
2615 coap_wolfssl_env_t *w_env =
2616 coap_dtls_new_wolfssl_env(session, COAP_DTLS_ROLE_SERVER);
2617 coap_tick_t now;
2618
2619 if (!w_env)
2620 goto error;
2621
2622 if (!setup_tls_context(w_context))
2623 goto error;
2624 tls = &w_context->tls;
2625
2626 ssl = wolfSSL_new(tls->ctx);
2627 if (!ssl)
2628 goto error;
2629 wolfSSL_SetIOWriteCtx(ssl, w_env);
2630 wolfSSL_SetIOReadCtx(ssl, w_env);
2631 wolfSSL_set_app_data(ssl, session);
2632
2633 wolfSSL_set_cipher_list(ssl, "ALL");
2634
2635 if (w_context->psk_pki_enabled & IS_PSK) {
2636 psk_hint = coap_get_session_server_psk_hint(session);
2637 if (psk_hint != NULL && psk_hint->length) {
2638 char *hint = wolfssl_malloc(psk_hint->length + 1);
2639
2640 if (hint) {
2641 memcpy(hint, psk_hint->s, psk_hint->length);
2642 hint[psk_hint->length] = '\000';
2643 wolfSSL_use_psk_identity_hint(ssl, hint);
2644 wolfssl_free(hint);
2645 } else {
2646 coap_log_warn("hint malloc failure\n");
2647 }
2648 }
2649 }
2650 if (w_context->psk_pki_enabled & IS_PKI) {
2651 if (!setup_pki_ssl(ssl, &w_context->setup_data, COAP_DTLS_ROLE_SERVER))
2652 goto error;
2653 }
2654#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
2655 if (w_context->setup_data.is_rpk_not_cert) {
2656 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
2657
2658 wolfSSL_set_server_cert_type(ssl, stype, sizeof(stype)/sizeof(stype[0]));
2659 }
2660#endif /* HAVE_RPK && LIBWOLFSSL_VERSION_HEX >= 0x05006004 */
2661
2662 coap_ticks(&now);
2663 w_env->last_timeout = now;
2664 w_env->ssl = ssl;
2665 w_env->data.session = session;
2666
2667 r = wolfSSL_accept(ssl);
2668 if (r == -1) {
2669 int err = wolfSSL_get_error(ssl, r);
2670 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE) {
2671 r = 0;
2672 }
2673 if (err == WOLFSSL_ERROR_WANT_READ) {
2674 session->sock.flags |= COAP_SOCKET_WANT_READ;
2675 }
2676 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2677 session->sock.flags |= COAP_SOCKET_WANT_WRITE;
2678#ifdef COAP_EPOLL_SUPPORT
2679 coap_epoll_ctl_mod(&session->sock,
2680 EPOLLOUT |
2681 ((session->sock.flags & COAP_SOCKET_WANT_READ) ?
2682 EPOLLIN : 0),
2683 __func__);
2684#endif /* COAP_EPOLL_SUPPORT */
2685 }
2686 }
2687
2688 if (r == 0)
2689 goto error;
2690
2691 session->tls = w_env;
2692 if (wolfSSL_is_init_finished(ssl)) {
2694 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2695 }
2696
2697 return w_env;
2698
2699error:
2700 if (ssl)
2701 wolfSSL_free(ssl);
2702 coap_dtls_free_wolfssl_env(w_env);
2703 session->tls = NULL;
2704 return NULL;
2705}
2706#endif /* COAP_SERVER_SUPPORT */
2707
2708void
2710 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2711 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2712
2713 if (ssl) {
2714 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2715 int r = wolfSSL_shutdown(ssl);
2716 if (r == 0)
2717 wolfSSL_shutdown(ssl);
2718 }
2719 wolfSSL_free(ssl);
2720 w_env->ssl = NULL;
2721 if (session->context)
2723 }
2724 coap_dtls_free_wolfssl_env(w_env);
2725 session->tls = NULL;
2726}
2727
2728/*
2729 * strm
2730 * return +ve Number of bytes written.
2731 * -1 Error (error in errno).
2732 */
2733ssize_t
2734coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) {
2735 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2736 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2737 int r, in_init;
2738
2739 if (ssl == NULL)
2740 return -1;
2741
2742 in_init = !wolfSSL_is_init_finished(ssl);
2743 session->dtls_event = -1;
2744 r = wolfSSL_write(ssl, data, (int)data_len);
2745
2746 if (r <= 0) {
2747 int err = wolfSSL_get_error(ssl, r);
2748 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2749 if (in_init && wolfSSL_is_init_finished(ssl)) {
2750 coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
2751 coap_session_str(session), wolfSSL_get_cipher((ssl)));
2753 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2754 }
2755 if (err == WOLFSSL_ERROR_WANT_READ)
2756 session->sock.flags |= COAP_SOCKET_WANT_READ;
2757 else if (err == WOLFSSL_ERROR_WANT_WRITE) {
2758 session->sock.flags |= COAP_SOCKET_WANT_WRITE;
2759#ifdef COAP_EPOLL_SUPPORT
2760 coap_epoll_ctl_mod(&session->sock,
2761 EPOLLOUT |
2762 ((session->sock.flags & COAP_SOCKET_WANT_READ) ?
2763 EPOLLIN : 0),
2764 __func__);
2765#endif /* COAP_EPOLL_SUPPORT */
2766 }
2767 r = 0;
2768 } else {
2769 coap_log_info("***%s: coap_tls_write: cannot send PDU\n",
2770 coap_session_str(session));
2771 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2773 else if (err == WOLFSSL_ERROR_SSL)
2775 r = -1;
2776 }
2777 } else if (in_init && wolfSSL_is_init_finished(ssl)) {
2778 coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
2779 coap_session_str(session), wolfSSL_get_cipher((ssl)));
2781 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2782 }
2783
2784 if (session->dtls_event >= 0) {
2785 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2786 if (session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2787 coap_handle_event_lkd(session->context, session->dtls_event, session);
2788 if (session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2789 session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2791 r = -1;
2792 }
2793 }
2794
2795 if (r >= 0) {
2796 if (r == (ssize_t)data_len)
2797 coap_log_debug("* %s: tls: sent %4d bytes\n",
2798 coap_session_str(session), r);
2799 else
2800 coap_log_debug("* %s: tls: sent %4d of %4zd bytes\n",
2801 coap_session_str(session), r, data_len);
2802 }
2803 return r;
2804}
2805
2806/*
2807 * strm
2808 * return >=0 Number of bytes read.
2809 * -1 Error (error in errno).
2810 */
2811ssize_t
2812coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) {
2813 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->tls;
2814 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2815 int r, in_init;
2816
2817 if (ssl == NULL) {
2818 errno = ENXIO;
2819 return -1;
2820 }
2821
2822 in_init = !wolfSSL_is_init_finished(ssl);
2823 session->dtls_event = -1;
2824 r = wolfSSL_read(ssl, data, (int)data_len);
2825 if (r <= 0) {
2826 int err = wolfSSL_get_error(ssl, r);
2827 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2828 if (in_init && wolfSSL_is_init_finished(ssl)) {
2829 coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
2830 coap_session_str(session), wolfSSL_get_cipher((ssl)));
2832 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2833 }
2834 if (err == WOLFSSL_ERROR_WANT_READ)
2835 session->sock.flags |= COAP_SOCKET_WANT_READ;
2836 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2837 session->sock.flags |= COAP_SOCKET_WANT_WRITE;
2838#ifdef COAP_EPOLL_SUPPORT
2839 coap_epoll_ctl_mod(&session->sock,
2840 EPOLLOUT |
2841 ((session->sock.flags & COAP_SOCKET_WANT_READ) ?
2842 EPOLLIN : 0),
2843 __func__);
2844#endif /* COAP_EPOLL_SUPPORT */
2845 }
2846 r = 0;
2847 } else {
2848 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2849 /* Got a close notify alert from the remote side */
2851 } else if (err == WOLFSSL_ERROR_SSL) {
2853 } else if (err == FATAL_ERROR) {
2854 WOLFSSL_ALERT_HISTORY h;
2855
2857 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2858 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2859 coap_log_warn("***%s: Alert '%d': %s\n",
2860 coap_session_str(session), h.last_rx.code,
2861 wolfSSL_alert_desc_string_long(h.last_rx.code));
2862 }
2863 }
2864 }
2865 r = -1;
2866 }
2867 } else if (in_init && wolfSSL_is_init_finished(ssl)) {
2868 coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
2869 coap_session_str(session), wolfSSL_get_cipher((ssl)));
2871 session->sock.lfunc[COAP_LAYER_TLS].l_establish(session);
2872 }
2873
2874 if (session->dtls_event >= 0) {
2875 /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected_lkd() */
2876 if (session->dtls_event != COAP_EVENT_DTLS_CLOSED)
2877 coap_handle_event_lkd(session->context, session->dtls_event, session);
2878 if (session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2879 session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2881 r = -1;
2882 }
2883 }
2884
2885 if (r > 0) {
2886 coap_log_debug("* %s: tls: recv %4d bytes\n",
2887 coap_session_str(session), r);
2888 }
2889 return r;
2890}
2891#endif /* !COAP_DISABLE_TCP */
2892
2893#if COAP_SERVER_SUPPORT
2895coap_digest_setup(void) {
2896 WOLFSSL_EVP_MD_CTX *digest_ctx = wolfSSL_EVP_MD_CTX_new();
2897
2898 if (digest_ctx) {
2899 wolfSSL_EVP_DigestInit_ex(digest_ctx, wolfSSL_EVP_sha256(), NULL);
2900 }
2901 return digest_ctx;
2902}
2903
2904void
2906 if (digest_ctx)
2907 wolfSSL_EVP_MD_CTX_free(digest_ctx);
2908}
2909
2910int
2912 const uint8_t *data,
2913 size_t data_len) {
2914 return wolfSSL_EVP_DigestUpdate(digest_ctx, data, data_len);
2915}
2916
2917int
2919 coap_digest_t *digest_buffer) {
2920 unsigned int size = sizeof(coap_digest_t);
2921 int ret = wolfSSL_EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
2922
2923 coap_digest_free(digest_ctx);
2924 return ret;
2925}
2926#endif /* COAP_SERVER_SUPPORT */
2927
2928#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
2929static void
2930coap_crypto_output_errors(const char *prefix) {
2931#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
2932 (void)prefix;
2933#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN */
2934 unsigned long e;
2935
2936 while ((e = wolfSSL_ERR_get_error()))
2937 coap_log_warn("%s: %s%s\n",
2938 prefix,
2939 wolfSSL_ERR_reason_error_string(e),
2940 ssl_function_definition(e));
2941#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN */
2942}
2943#endif /* COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT */
2944
2945#if COAP_WS_SUPPORT
2946/*
2947 * The struct hash_algs and the function get_hash_alg() are used to
2948 * determine which hash type to use for creating the required hash object.
2949 */
2950static struct hash_algs {
2951 cose_alg_t alg;
2952 const WOLFSSL_EVP_MD *(*get_hash)(void);
2953 size_t length; /* in bytes */
2954} hashs[] = {
2955 {COSE_ALGORITHM_SHA_1, wolfSSL_EVP_sha1, 20},
2956 {COSE_ALGORITHM_SHA_256_64, wolfSSL_EVP_sha256, 8},
2957 {COSE_ALGORITHM_SHA_256_256, wolfSSL_EVP_sha256, 32},
2958 {COSE_ALGORITHM_SHA_512, wolfSSL_EVP_sha512, 64},
2959};
2960
2961static const WOLFSSL_EVP_MD *
2962get_hash_alg(cose_alg_t alg, size_t *length) {
2963 size_t idx;
2964
2965 for (idx = 0; idx < sizeof(hashs) / sizeof(struct hash_algs); idx++) {
2966 if (hashs[idx].alg == alg) {
2967 *length = hashs[idx].length;
2968 return hashs[idx].get_hash();
2969 }
2970 }
2971 coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg);
2972 return NULL;
2973}
2974
2975int
2977 const coap_bin_const_t *data,
2978 coap_bin_const_t **hash) {
2979 unsigned int length;
2980 const WOLFSSL_EVP_MD *evp_md;
2981 WOLFSSL_EVP_MD_CTX *evp_ctx = NULL;
2982 coap_binary_t *dummy = NULL;
2983 size_t hash_length;
2984
2985 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
2986 coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg);
2987 return 0;
2988 }
2989 evp_ctx = wolfSSL_EVP_MD_CTX_new();
2990 if (evp_ctx == NULL)
2991 goto error;
2992 if (wolfSSL_EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
2993 goto error;
2994 ;
2995 if (wolfSSL_EVP_DigestUpdate(evp_ctx, data->s, data->length) == 0)
2996 goto error;
2997 ;
2998 dummy = coap_new_binary(EVP_MAX_MD_SIZE);
2999 if (dummy == NULL)
3000 goto error;
3001 if (wolfSSL_EVP_DigestFinal_ex(evp_ctx, dummy->s, &length) == 0)
3002 goto error;
3003 dummy->length = length;
3004 if (hash_length < dummy->length)
3005 dummy->length = hash_length;
3006 *hash = (coap_bin_const_t *)(dummy);
3007 wolfSSL_EVP_MD_CTX_free(evp_ctx);
3008 return 1;
3009
3010error:
3011 coap_crypto_output_errors("coap_crypto_hash");
3013 if (evp_ctx)
3014 wolfSSL_EVP_MD_CTX_free(evp_ctx);
3015 return 0;
3016}
3017#endif /* COAP_WS_SUPPORT */
3018
3019#if COAP_OSCORE_SUPPORT
3020#if LIBWOLFSSL_VERSION_HEX < 0x05006000
3021static const WOLFSSL_EVP_CIPHER *
3022EVP_aes_128_ccm(void) {
3023 return "AES-128-CCM";
3024}
3025
3026static const WOLFSSL_EVP_CIPHER *
3027EVP_aes_256_ccm(void) {
3028 return "AES-256-CCM";
3029}
3030#endif /* LIBWOLFSSL_VERSION_HEX < 0x05006000 */
3031
3032int
3034 return 1;
3035}
3036
3037/*
3038 * The struct cipher_algs and the function get_cipher_alg() are used to
3039 * determine which cipher type to use for creating the required cipher
3040 * suite object.
3041 */
3042static struct cipher_algs {
3043 cose_alg_t alg;
3044 const WOLFSSL_EVP_CIPHER *(*get_cipher)(void);
3045} ciphers[] = {{COSE_ALGORITHM_AES_CCM_16_64_128, EVP_aes_128_ccm},
3046 {COSE_ALGORITHM_AES_CCM_16_64_256, EVP_aes_256_ccm}
3047};
3048
3049static const WOLFSSL_EVP_CIPHER *
3050get_cipher_alg(cose_alg_t alg) {
3051 size_t idx;
3052
3053 for (idx = 0; idx < sizeof(ciphers) / sizeof(struct cipher_algs); idx++) {
3054 if (ciphers[idx].alg == alg)
3055 return ciphers[idx].get_cipher();
3056 }
3057 coap_log_debug("get_cipher_alg: COSE cipher %d not supported\n", alg);
3058 return NULL;
3059}
3060
3061/*
3062 * The struct hmac_algs and the function get_hmac_alg() are used to
3063 * determine which hmac type to use for creating the required hmac
3064 * suite object.
3065 */
3066static struct hmac_algs {
3067 cose_hmac_alg_t hmac_alg;
3068 const WOLFSSL_EVP_MD *(*get_hmac)(void);
3069} hmacs[] = {
3070 {COSE_HMAC_ALG_HMAC256_256, wolfSSL_EVP_sha256},
3071 {COSE_HMAC_ALG_HMAC384_384, wolfSSL_EVP_sha384},
3072 {COSE_HMAC_ALG_HMAC512_512, wolfSSL_EVP_sha512},
3073};
3074
3075static const WOLFSSL_EVP_MD *
3076get_hmac_alg(cose_hmac_alg_t hmac_alg) {
3077 size_t idx;
3078
3079 for (idx = 0; idx < sizeof(hmacs) / sizeof(struct hmac_algs); idx++) {
3080 if (hmacs[idx].hmac_alg == hmac_alg)
3081 return hmacs[idx].get_hmac();
3082 }
3083 coap_log_debug("get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3084 return NULL;
3085}
3086
3087int
3089 return get_cipher_alg(alg) != NULL;
3090}
3091
3092int
3094 cose_hmac_alg_t hmac_alg;
3095
3096 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
3097 return 0;
3098 return get_hmac_alg(hmac_alg) != NULL;
3099}
3100
3101#define C(Func) \
3102 if (1 != (Func)) { \
3103 goto error; \
3104 }
3105
3106int
3108 coap_bin_const_t *data,
3109 coap_bin_const_t *aad,
3110 uint8_t *result,
3111 size_t *max_result_len) {
3112
3113 Aes aes;
3114 int ret;
3115 int result_len;
3116 int nonce_length;
3117 byte *authTag = NULL;
3118 const coap_crypto_aes_ccm_t *ccm;
3119
3120 if (data == NULL)
3121 return 0;
3122
3123 assert(params != NULL);
3124 if (!params)
3125 return 0;
3126
3127 ccm = &params->params.aes;
3128
3129 if (ccm->key.s == NULL || ccm->nonce == NULL)
3130 goto error;
3131
3132 result_len = data->length;
3133 nonce_length = 15 - ccm->l;
3134
3135 memset(&aes, 0, sizeof(aes));
3136 ret = wc_AesCcmSetKey(&aes, ccm->key.s, ccm->key.length);
3137 if (ret != 0)
3138 goto error;
3139
3140 authTag = (byte *)malloc(ccm->tag_len * sizeof(byte));
3141 if (!authTag) {
3142 goto error;
3143 }
3144 ret = wc_AesCcmEncrypt(&aes, result, data->s, data->length, ccm->nonce,
3145 nonce_length, authTag, ccm->tag_len,
3146 aad->s, aad->length);
3147
3148 if (ret != 0) {
3149 wolfssl_free(authTag);
3150 goto error;
3151 }
3152
3153 memcpy(result + result_len, authTag, ccm->tag_len);
3154 result_len += sizeof(authTag);
3155 *max_result_len = result_len;
3156 wolfssl_free(authTag);
3157
3158 return 1;
3159error:
3160 coap_crypto_output_errors("coap_crypto_aead_encrypt");
3161 return 0;
3162}
3163
3164
3165int
3167 coap_bin_const_t *data,
3168 coap_bin_const_t *aad,
3169 uint8_t *result,
3170 size_t *max_result_len) {
3171
3172 Aes aes;
3173 int ret;
3174 int len;
3175 const coap_crypto_aes_ccm_t *ccm;
3176
3177 if (data == NULL)
3178 return 0;
3179
3180 if (data == NULL)
3181 return 0;
3182
3183 assert(params != NULL);
3184 if (!params)
3185 return 0;
3186
3187 ccm = &params->params.aes;
3188 byte authTag[ccm->tag_len];
3189
3190 if (data->length < ccm->tag_len) {
3191 return 0;
3192 } else {
3193 memcpy(authTag, data->s + data->length - ccm->tag_len, sizeof(authTag));
3194 data->length -= ccm->tag_len;
3195 }
3196
3197 if (ccm->key.s == NULL || ccm->nonce == NULL)
3198 goto error;
3199
3200 memset(&aes, 0, sizeof(aes));
3201 ret = wc_AesCcmSetKey(&aes, ccm->key.s, ccm->key.length);
3202 if (ret != 0)
3203 goto error;
3204
3205 len = data->length;
3206
3207 ret = wc_AesCcmDecrypt(&aes, result, data->s, len, ccm->nonce,
3208 15 - ccm->l, authTag, sizeof(authTag),
3209 aad->s, aad->length);
3210
3211 if (ret != 0)
3212 goto error;
3213
3214 *max_result_len = len;
3215
3216 return 1;
3217error:
3218 coap_crypto_output_errors("coap_crypto_aead_decrypt");
3219 return 0;
3220}
3221
3222int
3224 coap_bin_const_t *key,
3225 coap_bin_const_t *data,
3226 coap_bin_const_t **hmac) {
3227 unsigned int result_len;
3228 const WOLFSSL_EVP_MD *evp_md;
3229 coap_binary_t *dummy = NULL;
3230
3231 assert(key);
3232 assert(data);
3233 assert(hmac);
3234
3235 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
3236 coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3237 return 0;
3238 }
3239 dummy = coap_new_binary(EVP_MAX_MD_SIZE);
3240 if (dummy == NULL)
3241 return 0;
3242 result_len = (unsigned int)dummy->length;
3243 if (wolfSSL_HMAC(evp_md,
3244 key->s,
3245 (int)key->length,
3246 data->s,
3247 (int)data->length,
3248 dummy->s,
3249 &result_len)) {
3250 dummy->length = result_len;
3251 *hmac = (coap_bin_const_t *)dummy;
3252 return 1;
3253 }
3254
3255 coap_crypto_output_errors("coap_crypto_hmac");
3256 return 0;
3257}
3258
3259#endif /* COAP_OSCORE_SUPPORT */
3260
3261#else /* !COAP_WITH_LIBWOLFSSL */
3262
3263#ifdef __clang__
3264/* Make compilers happy that do not like empty modules. As this function is
3265 * never used, we ignore -Wunused-function at the end of compiling this file
3266 */
3267#pragma GCC diagnostic ignored "-Wunused-function"
3268#endif
3269static inline void
3270dummy(void) {
3271}
3272
3273#endif /* COAP_WITH_LIBWOLFSSL */
static size_t strnlen(const char *s, size_t maxlen)
A length-safe strlen() fake.
Definition coap_debug.c:181
#define COAP_SERVER_SUPPORT
#define COAP_RXBUFFER_SIZE
Definition coap_io.h:29
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:66
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to modify the state of events that epoll is tracking on the appropriate file ...
@ COAP_LAYER_TLS
Library specific build wrapper for coap_internal.h.
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:229
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:301
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:224
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:243
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:261
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:124
static coap_log_t dtls_log_level
Definition coap_notls.c:151
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:212
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:289
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:208
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:238
void coap_dtls_free_context(void *handle COAP_UNUSED)
Definition coap_notls.c:186
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:204
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
Definition coap_notls.c:181
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:280
static void dummy(void)
coap_binary_t * get_asn1_spki(const uint8_t *data, size_t size)
Abstract SPKI public key from the ASN1.
Definition coap_asn1.c:122
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
void coap_digest_ctx_t
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:143
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition coap_prng.c:178
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition coap_net.c:4783
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:2793
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
Definition coap_notls.c:154
int coap_dtls_define_issue(coap_define_issue_key_t type, coap_define_issue_fail_t fail, coap_dtls_key_t *key, const coap_dtls_role_t role, int ret)
Report PKI DEFINE type issue.
Definition coap_dtls.c:165
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Definition coap_notls.c:219
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c: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.
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
Definition coap_dtls.c:26
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
const coap_bin_const_t * coap_get_session_server_psk_hint(const coap_session_t *coap_session)
Get the current server's PSK identity hint.
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_KEY_CA
@ COAP_DEFINE_KEY_PUBLIC
@ COAP_DEFINE_FAIL_NONE
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
@ COAP_DEFINE_FAIL_BAD
#define COAP_DTLS_HINT_LENGTH
Definition coap_dtls.h:35
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
Definition coap_notls.c:100
coap_dtls_role_t
Definition coap_dtls.h:44
coap_tls_library_t
Definition coap_dtls.h:70
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
Definition coap_dtls.h:245
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
Definition coap_dtls.h:242
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
Definition coap_dtls.h:236
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
Definition coap_dtls.h:234
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
Definition coap_dtls.h:251
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
Definition coap_dtls.h:238
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
Definition coap_dtls.h:240
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
Definition coap_dtls.h:248
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
Definition coap_dtls.h:46
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition coap_dtls.h:45
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
Definition coap_dtls.h:172
@ COAP_TLS_LIBRARY_WOLFSSL
Using wolfSSL library.
Definition coap_dtls.h:76
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
Definition coap_event.h:39
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
Definition coap_event.h:41
@ COAP_EVENT_DTLS_RENEGOTIATE
Triggered when (D)TLS session renegotiated.
Definition coap_event.h:43
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
Definition coap_event.h:45
#define coap_lock_callback_ret(r, c, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:120
coap_log_t
Logging type.
Definition coap_debug.h:50
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition coap_notls.c:176
#define coap_dtls_log(level,...)
Logging function.
Definition coap_debug.h:300
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition coap_notls.c:171
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:108
#define coap_log_warn(...)
Definition coap_debug.h:102
#define coap_log_err(...)
Definition coap_debug.h:96
#define coap_log(level,...)
Logging function.
Definition coap_debug.h:284
@ COAP_LOG_INFO
Definition coap_debug.h:57
@ COAP_LOG_EMERG
Definition coap_debug.h:51
@ COAP_LOG_DEBUG
Definition coap_debug.h:58
@ COAP_LOG_WARN
Definition coap_debug.h:55
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
Definition coap_netif.c:25
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
cose_hkdf_alg_t
cose_hmac_alg_t
cose_alg_t
@ COSE_HMAC_ALG_HMAC384_384
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_ALGORITHM_SHA_256_64
@ 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
#define COAP_DEFAULT_MTU
Definition coap_pdu.h:41
@ COAP_PROTO_DTLS
Definition coap_pdu.h:315
@ COAP_PROTO_TLS
Definition coap_pdu.h:317
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).
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.
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_CSM
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:77
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:110
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:105
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
Definition coap_notls.c:86
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
Definition coap_notls.c:50
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:41
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:36
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
Definition coap_notls.c:59
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
Definition coap_notls.c:77
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
Definition coap_notls.c:68
#define COAP_UNUSED
Definition libcoap.h:70
CoAP binary data definition with const data.
Definition coap_str.h:64
size_t length
length of binary data
Definition coap_str.h:65
const uint8_t * s
read-only binary data
Definition coap_str.h:66
CoAP binary data definition.
Definition coap_str.h:56
size_t length
length of binary data
Definition coap_str.h:57
uint8_t * s
binary data
Definition coap_str.h:58
The CoAP stack's global state is stored in a coap_context_t object.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
size_t tag_len
The size of the Tag.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@2 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
The structure that holds the Client PSK information.
Definition coap_dtls.h:379
coap_bin_const_t key
Definition coap_dtls.h:381
coap_bin_const_t identity
Definition coap_dtls.h:380
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:410
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
Definition coap_dtls.h:417
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
Definition coap_dtls.h:434
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:437
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
Definition coap_dtls.h:433
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:415
The structure that holds the PKI key information.
Definition coap_dtls.h:279
coap_pki_key_define_t define
for definable type keys
Definition coap_dtls.h:286
union coap_dtls_key_t::@3 key
coap_pki_key_t key_type
key format type
Definition coap_dtls.h:280
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:312
uint8_t allow_no_crl
1 ignore if CRL not there
Definition coap_dtls.h:326
void * cn_call_back_arg
Passed in to the CN callback function.
Definition coap_dtls.h:351
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
Definition coap_dtls.h:323
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
Definition coap_dtls.h:333
uint8_t check_cert_revocation
1 if revocation checks wanted
Definition coap_dtls.h:325
coap_dtls_pki_sni_callback_t validate_sni_call_back
SNI check callback function.
Definition coap_dtls.h:358
uint8_t cert_chain_verify_depth
recommended depth is 3
Definition coap_dtls.h:324
uint8_t allow_expired_certs
1 if expired certs are allowed
Definition coap_dtls.h:322
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:317
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:368
uint8_t allow_self_signed
1 if self-signed certs are allowed.
Definition coap_dtls.h:320
void * sni_call_back_arg
Passed in to the sni callback function.
Definition coap_dtls.h:359
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
Definition coap_dtls.h:350
uint8_t allow_expired_crl
1 if expired crl is allowed
Definition coap_dtls.h:327
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
Definition coap_dtls.h:330
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
Definition coap_dtls.h:318
coap_dtls_key_t pki_key
PKI key definition.
Definition coap_dtls.h:373
The structure that holds the Server Pre-Shared Key and Identity Hint information.
Definition coap_dtls.h:450
coap_bin_const_t hint
Definition coap_dtls.h:451
coap_bin_const_t key
Definition coap_dtls.h:452
The structure used for defining the Server PSK setup data to be used.
Definition coap_dtls.h:501
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
Definition coap_dtls.h:530
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
Definition coap_dtls.h:522
void * id_call_back_arg
Passed in to the Identity callback function.
Definition coap_dtls.h:523
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:506
void * sni_call_back_arg
Passed in to the SNI callback function.
Definition coap_dtls.h:531
coap_dtls_spsk_info_t psk_info
Server PSK definition.
Definition coap_dtls.h:533
coap_layer_read_t l_read
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
Definition coap_dtls.h:261
coap_const_char_ptr_t private_key
define: Private Key
Definition coap_dtls.h:262
coap_const_char_ptr_t ca
define: Common CA Certificate
Definition coap_dtls.h:260
size_t public_cert_len
define Public Cert length (if needed)
Definition coap_dtls.h:264
size_t ca_len
define CA Cert length (if needed)
Definition coap_dtls.h:263
coap_pki_define_t private_key_def
define: Private Key type definition
Definition coap_dtls.h:268
size_t private_key_len
define Private Key length (if needed)
Definition coap_dtls.h:265
coap_pki_define_t ca_def
define: Common CA type definition
Definition coap_dtls.h:266
coap_pki_define_t public_cert_def
define: Public Cert type definition
Definition coap_dtls.h:267
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_bin_const_t * client_cid
Contains client CID or NULL.
coap_proto_t proto
protocol used
uint8_t is_dtls13
Set if session is DTLS1.3.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t negotiated_cid
Set for a client if CID negotiated.
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition with const data.
Definition coap_str.h:46
const uint8_t * s
read-only string data
Definition coap_str.h:48
size_t length
length of string
Definition coap_str.h:47
The structure used for returning the underlying (D)TLS library information.
Definition coap_dtls.h:83
uint64_t built_version
(D)TLS Built against Library Version
Definition coap_dtls.h:86
coap_tls_library_t type
Library type.
Definition coap_dtls.h:85
uint64_t version
(D)TLS runtime Library Version
Definition coap_dtls.h:84
const char * s_byte
signed char ptr
Definition coap_str.h:73
const uint8_t * u_byte
unsigned char ptr
Definition coap_str.h:74