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