21#ifdef COAP_WITH_LIBWOLFSSL
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>
84#ifdef COAP_EPOLL_SUPPORT
85# include <sys/epoll.h>
88#if LIBWOLFSSL_VERSION_HEX < 0x05002000
89#error Must be compiled against wolfSSL 5.2.0 or later
93#define strcasecmp _stricmp
94#define strncasecmp _strnicmp
98#define WOLFSSL3_AL_FATAL 2
99#define WOLFSSL_TLSEXT_ERR_OK 0
102typedef struct coap_dtls_context_t {
104 WOLFSSL_HMAC_CTX *cookie_hmac;
105} coap_dtls_context_t;
107typedef struct coap_tls_context_t {
114typedef struct coap_wolfssl_context_t {
115 coap_dtls_context_t dtls;
117 coap_tls_context_t tls;
123} coap_wolfssl_context_t;
125typedef struct coap_ssl_data_t {
132typedef struct coap_wolfssl_env_t {
135 unsigned int retry_scalar;
136 coap_ssl_data_t data;
141typedef enum coap_enc_method_t {
147wolfssl_malloc(
size_t size) {
148 void *ret = XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
154wolfssl_free(
void *ptr) {
156 XFREE(ptr, NULL, DYNAMIC_TYPE_TMP_BUFFER);
160wolfssl_strdup(
const char *str) {
161 char *ret = (
char *)wolfssl_malloc(strlen(str) + 1);
170wolfssl_strndup(
const char *str,
size_t n) {
172 char *ret = (
char *)wolfssl_malloc(len + 1);
175 strncpy(ret, str, len);
181static coap_wolfssl_env_t *
183 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)c_session->
tls;
185 assert(w_env == NULL);
186 w_env = (coap_wolfssl_env_t *)wolfssl_malloc(
sizeof(coap_wolfssl_env_t));
190 memset(w_env, 0,
sizeof(coap_wolfssl_env_t));
196coap_dtls_free_wolfssl_env(coap_wolfssl_env_t *w_env) {
202#if COAP_CLIENT_SUPPORT
203#ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE
204#define WOLFSSL_CIPHER_LIST_MAX_SIZE 4096
207#ifdef COAP_WOLFSSL_PSK_CIPHERS
208static char psk_ciphers[] = COAP_WOLFSSL_PSK_CIPHERS;
210static char psk_ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
213#ifdef COAP_WOLFSSL_PKI_CIPHERS
214static char pki_ciphers[] = COAP_WOLFSSL_PKI_CIPHERS;
216static char pki_ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
220set_ciphersuites(WOLFSSL *ssl, coap_enc_method_t method) {
221#if ! defined(COAP_WOLFSSL_PSK_CIPHERS) || ! defined(COAP_WOLFSSL_PKI_CIPHERS)
222 static int processed_ciphers = 0;
224 if (!processed_ciphers) {
225 static char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
226 char *ciphers_ofs = ciphers;
228#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
229 char *psk_ofs = psk_ciphers;
231#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
232 char *pki_ofs = pki_ciphers;
235 if (wolfSSL_get_ciphers(ciphers, (
int)
sizeof(ciphers)) != WOLFSSL_SUCCESS) {
240 while (ciphers_ofs) {
241 cp = strchr(ciphers_ofs,
':');
244 if (strstr(ciphers_ofs,
"NULL")) {
248 if (strcmp(ciphers_ofs,
"RENEGOTIATION-INFO") == 0) {
251 }
else if (strstr(ciphers_ofs,
"PSK")) {
252#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
253 if (psk_ofs != psk_ciphers) {
257 strcpy(psk_ofs, ciphers_ofs);
258 psk_ofs += strlen(ciphers_ofs);
262#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
263 if (pki_ofs != pki_ciphers) {
267 strcpy(pki_ofs, ciphers_ofs);
268 pki_ofs += strlen(ciphers_ofs);
274 ciphers_ofs = cp + 1;
278#ifndef HAVE_SECURE_RENEGOTIATION
284#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
285 if (psk_ofs != psk_ciphers) {
289 strcpy(psk_ofs,
"RENEGOTIATION-INFO");
290 psk_ofs += strlen(
"RENEGOTIATION-INFO");
293#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
294 if (pki_ofs != pki_ciphers) {
298 strcpy(pki_ofs,
"RENEGOTIATION-INFO");
299 pki_ofs += strlen(
"RENEGOTIATION-INFO");
304 processed_ciphers = 1;
308 if (method == COAP_ENC_PSK) {
309 wolfSSL_set_cipher_list(ssl, psk_ciphers);
311 wolfSSL_set_cipher_list(ssl, pki_ciphers);
316#if COAP_SERVER_SUPPORT
317static int psk_tls_server_name_call_back(WOLFSSL *ssl,
int *sd,
void *arg);
319static int tls_verify_call_back(
int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx);
323 if (wolfSSL_lib_version_hex() < 0x05002000) {
324 coap_log_warn(
"wolfSSL version 5.2.0 or later is required\n");
333 if (wolfSSL_lib_version_hex() < 0x05002000) {
334 coap_log_warn(
"wolfSSL version 5.2.0 or later is required\n");
385#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
392#if COAP_CLIENT_SUPPORT
395#if defined(WOLFSSL_DTLS_CID)
409 version.
version = wolfSSL_lib_version_hex();
417coap_wolfssl_log_func(
int level,
const char *text) {
420 switch ((
int)level) {
445 if (wolfSSL_library_init() != WOLFSSL_SUCCESS) {
449 wolfSSL_load_error_strings();
450 wolfSSL_SetLoggingCb(coap_wolfssl_log_func);
451 wolfSSL_Debugging_ON();
456 wolfSSL_ERR_free_strings();
458 wolfSSL_Debugging_OFF();
467 coap_wolfssl_env_t *w_env;
470 memcpy(&w_env, &c_session->
tls,
sizeof(w_env));
472 return (
void *)&w_env->ssl;
493coap_dgram_read(WOLFSSL *ssl,
char *out,
int outl,
void *ctx) {
495 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
496 coap_ssl_data_t *data = w_env ? &w_env->data : NULL;
500 if (w_env && !w_env->done_psk_check && w_env->ssl) {
501 if (wolfSSL_SSL_in_init(w_env->ssl)) {
502 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
508 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
509 w_env->done_psk_check = 1;
515 if (data != NULL && data->pdu_len > 0) {
516 if (outl < (
int)data->pdu_len) {
517 memcpy(out, data->pdu, outl);
520 memcpy(out, data->pdu, data->pdu_len);
521 ret = (int)data->pdu_len;
523 if (!data->peekmode) {
528 w_env->last_timeout = now;
537coap_dgram_write(WOLFSSL *ssl,
char *in,
int inl,
void *ctx) {
539 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
540 coap_ssl_data_t *data = w_env ? &w_env->data : NULL;
548 && data->session->endpoint == NULL
555 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
560 w_env->last_timeout = now;
568#if COAP_CLIENT_SUPPORT
570coap_dtls_psk_client_callback(WOLFSSL *ssl,
573 unsigned int max_identity_len,
575 unsigned int max_psk_len) {
577 coap_wolfssl_context_t *w_context;
584 if (c_session == NULL)
587 if (w_context == NULL)
595 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
596 temp.
length = strlen((
const char *)temp.
s);
600 (
const char *)temp.
s);
610 if (cpsk_info == NULL)
615 psk_identity = &cpsk_info->
identity;
616 psk_key = &cpsk_info->
key;
622 if (psk_identity == NULL || psk_key == NULL) {
628 if (!max_identity_len)
631 if (psk_identity->
length > max_identity_len) {
632 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
636 max_identity_len = (
unsigned int)psk_identity->
length;
638 memcpy(identity, psk_identity->
s, max_identity_len);
639 identity[max_identity_len] =
'\000';
641 if (psk_key->
length > max_psk_len) {
646 max_psk_len = (
unsigned int)psk_key->
length;
648 memcpy(psk, psk_key->
s, max_psk_len);
653coap_dtls_psk_client_cs_callback(WOLFSSL *ssl,
const char *hint,
654 char *identity,
unsigned int max_identity_len,
655 unsigned char *psk,
unsigned int max_psk_len,
656 const char *ciphersuite) {
657 int key_len = coap_dtls_psk_client_callback(ssl,
670#if COAP_SERVER_SUPPORT
672coap_dtls_psk_server_callback(
674 const char *identity,
676 unsigned int max_psk_len) {
683 if (c_session == NULL)
689 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
690 lidentity.
length = strlen((
const char *)lidentity.
s);
694 (
int)lidentity.
length, (
const char *)lidentity.
s);
709 if (psk_key->
length > max_psk_len) {
714 max_psk_len = (
unsigned int)psk_key->
length;
716 memcpy(psk, psk_key->
s, max_psk_len);
722ssl_function_definition(
unsigned long e) {
723 static char buff[80];
725 snprintf(buff,
sizeof(buff),
" at %s:%s",
726 wolfSSL_ERR_lib_error_string(e), wolfSSL_ERR_func_error_string(e));
731coap_dtls_info_callback(
const WOLFSSL *ssl,
int where,
int ret) {
734 int w = where &~SSL_ST_MASK;
736 if (w & SSL_ST_CONNECT)
737 pstr =
"wolfSSL_connect";
738 else if (w & SSL_ST_ACCEPT)
739 pstr =
"wolfSSL_accept";
743 if (where & SSL_CB_LOOP) {
746 }
else if (where & SSL_CB_ALERT) {
748 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
749 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == WOLFSSL3_AL_FATAL) {
751 if ((ret & 0xff) != close_notify)
756 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
759 wolfSSL_alert_type_string_long(ret),
760 wolfSSL_alert_desc_string_long(ret));
761 }
else if (where & SSL_CB_EXIT) {
767 while ((e = wolfSSL_ERR_get_error()))
770 ssl_function_definition(e));
772 }
else if (ret < 0) {
777 memcpy(&rw_ssl, &ssl,
sizeof(rw_ssl));
778 int err = wolfSSL_get_error(rw_ssl, ret);
779 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE &&
780 err != WOLFSSL_ERROR_WANT_CONNECT && err != WOLFSSL_ERROR_WANT_ACCEPT &&
781 err != WOLFSSL_ERROR_WANT_X509_LOOKUP) {
785 while ((e = wolfSSL_ERR_get_error()))
788 ssl_function_definition(e));
794 if (where == SSL_CB_HANDSHAKE_START) {
798 memcpy(&rw_ssl, &ssl,
sizeof(rw_ssl));
799 if (wolfSSL_is_init_finished(rw_ssl))
811coap_sock_read(WOLFSSL *ssl,
char *out,
int outl,
void *ctx) {
812 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
818 if (w_env && !w_env->done_psk_check && w_env->ssl &&
820 if (wolfSSL_SSL_in_init(w_env->ssl)) {
821 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
826 if (strstr(name,
"PSK")) {
827 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
828 w_env->done_psk_check = 1;
850coap_sock_write(WOLFSSL *ssl,
char *in,
int inl,
void *ctx) {
851 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
867 (errno == EPIPE || errno == ECONNRESET)) {
887coap_set_user_prefs(WOLFSSL_CTX *ctx) {
890#ifdef COAP_WOLFSSL_SIGALGS
891 wolfSSL_CTX_set1_sigalgs_list(ctx, COAP_WOLFSSL_SIGALGS);
893#ifdef COAP_WOLFSSL_GROUPS
895 ret = wolfSSL_CTX_set1_groups_list(ctx,
896 (
char *) COAP_WOLFSSL_GROUPS);
897 if (ret != WOLFSSL_SUCCESS) {
905setup_dtls_context(coap_wolfssl_context_t *w_context) {
906 if (!w_context->dtls.ctx) {
907 uint8_t cookie_secret[32];
910 w_context->dtls.ctx = wolfSSL_CTX_new(wolfDTLS_method());
911 if (!w_context->dtls.ctx)
913 wolfSSL_CTX_set_min_proto_version(w_context->dtls.ctx,
915 wolfSSL_CTX_set_ex_data(w_context->dtls.ctx, 0, &w_context->dtls);
916 coap_set_user_prefs(w_context->dtls.ctx);
917 memset(cookie_secret, 0,
sizeof(cookie_secret));
918 if (!wolfSSL_RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
920 "Insufficient entropy for random cookie generation");
923 w_context->dtls.cookie_hmac = wolfSSL_HMAC_CTX_new();
924 if (!wolfSSL_HMAC_Init_ex(w_context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
925 wolfSSL_EVP_sha256(), NULL))
928 wolfSSL_CTX_set_info_callback(w_context->dtls.ctx, coap_dtls_info_callback);
929 wolfSSL_CTX_set_options(w_context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
930 wolfSSL_SetIORecv(w_context->dtls.ctx, coap_dgram_read);
931 wolfSSL_SetIOSend(w_context->dtls.ctx, coap_dgram_write);
932#ifdef WOLFSSL_DTLS_MTU
935 if (w_context->root_ca_file || w_context->root_ca_dir) {
936 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->dtls.ctx,
937 w_context->root_ca_file,
938 w_context->root_ca_dir,
939 w_context->setup_data.allow_expired_certs ?
940 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
942 w_context->root_ca_file ? w_context->root_ca_file :
"NULL",
943 w_context->root_ca_dir ? w_context->root_ca_dir :
"NULL");
948 if (w_context->setup_data.verify_peer_cert)
949 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
950 WOLFSSL_VERIFY_PEER |
951 WOLFSSL_VERIFY_CLIENT_ONCE |
952 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
953 tls_verify_call_back);
955 wolfSSL_CTX_set_verify(w_context->dtls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
968setup_tls_context(coap_wolfssl_context_t *w_context) {
969 if (!w_context->tls.ctx) {
971 w_context->tls.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
972 if (!w_context->tls.ctx)
974 wolfSSL_CTX_set_ex_data(w_context->tls.ctx, 0, &w_context->tls);
975 wolfSSL_CTX_set_min_proto_version(w_context->tls.ctx, TLS1_VERSION);
976 coap_set_user_prefs(w_context->tls.ctx);
977 wolfSSL_CTX_set_info_callback(w_context->tls.ctx, coap_dtls_info_callback);
978 wolfSSL_SetIORecv(w_context->tls.ctx, coap_sock_read);
979 wolfSSL_SetIOSend(w_context->tls.ctx, coap_sock_write);
980#if COAP_CLIENT_SUPPORT
981 if (w_context->psk_pki_enabled & IS_PSK) {
982 wolfSSL_CTX_set_psk_client_cs_callback(w_context->tls.ctx,
983 coap_dtls_psk_client_cs_callback);
986 if (w_context->root_ca_file || w_context->root_ca_dir) {
987 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->tls.ctx,
988 w_context->root_ca_file,
989 w_context->root_ca_dir,
990 w_context->setup_data.allow_expired_certs ?
991 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
993 w_context->root_ca_file ? w_context->root_ca_file :
"NULL",
994 w_context->root_ca_dir ? w_context->root_ca_dir :
"NULL");
999 if (w_context->setup_data.verify_peer_cert)
1000 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1001 WOLFSSL_VERIFY_PEER |
1002 WOLFSSL_VERIFY_CLIENT_ONCE |
1003 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1004 tls_verify_call_back);
1006 wolfSSL_CTX_set_verify(w_context->tls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1018 coap_wolfssl_context_t *w_context;
1021 w_context = (coap_wolfssl_context_t *)wolfssl_malloc(
sizeof(coap_wolfssl_context_t));
1023 memset(w_context, 0,
sizeof(coap_wolfssl_context_t));
1029#if COAP_SERVER_SUPPORT
1034 coap_wolfssl_context_t *w_context =
1037 if (!setup_data || !w_context)
1040 if (!setup_dtls_context(w_context))
1042#if !COAP_DISABLE_TCP
1043 if (!setup_tls_context(w_context))
1047 wolfSSL_CTX_set_psk_server_callback(w_context->dtls.ctx,
1048 coap_dtls_psk_server_callback);
1050#if !COAP_DISABLE_TCP
1051 wolfSSL_CTX_set_psk_server_callback(w_context->tls.ctx,
1052 coap_dtls_psk_server_callback);
1058 wolfSSL_CTX_use_psk_identity_hint(w_context->dtls.ctx, hint);
1059#if !COAP_DISABLE_TCP
1060 wolfSSL_CTX_use_psk_identity_hint(w_context->tls.ctx, hint);
1064 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1066 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1067 psk_tls_server_name_call_back);
1068#if !COAP_DISABLE_TCP
1069 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1071 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1072 psk_tls_server_name_call_back);
1078 w_context->psk_pki_enabled |= IS_PSK;
1083#if COAP_CLIENT_SUPPORT
1088 coap_wolfssl_context_t *w_context =
1091 if (!setup_data || !w_context)
1098#if ! defined(WOLFSSL_DTLS_CID)
1102 w_context->psk_pki_enabled |= IS_PSK;
1107#if !COAP_DISABLE_TCP
1108static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1110#if COAP_SERVER_SUPPORT
1113 const unsigned char **out,
1114 unsigned char *outlen,
1115 const unsigned char *in,
1119 unsigned char *tout = NULL;
1122 return SSL_TLSEXT_ERR_NOACK;
1123 ret = wolfSSL_select_next_proto(&tout,
1130 return (ret != OPENSSL_NPN_NEGOTIATED) ? noack_return : WOLFSSL_TLSEXT_ERR_OK;
1136setup_pki_ssl(WOLFSSL *ssl,
1139 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
1153 if (!(wolfSSL_use_PrivateKey_file(ssl,
1155 WOLFSSL_FILETYPE_PEM))) {
1162 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1165 WOLFSSL_FILETYPE_PEM))) {
1172#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1173 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1176 WOLFSSL_FILETYPE_PEM))) {
1188 if (!(wolfSSL_use_PrivateKey_file(ssl,
1190 WOLFSSL_FILETYPE_ASN1))) {
1197 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1200 WOLFSSL_FILETYPE_ASN1))) {
1229 if (!(wolfSSL_use_certificate_chain_file(ssl,
1237 if (!(wolfSSL_use_certificate_chain_buffer(ssl,
1246#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1248 unsigned char der_buff[512];
1250 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1251 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1253 wolfSSL_set_client_cert_type(ssl, ctype,
sizeof(ctype)/
sizeof(ctype[0]));
1254 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
1258 der_buff, (
int)
sizeof(der_buff));
1262 der_buff, (
int)
sizeof(der_buff), NULL);
1271 if (!wolfSSL_use_PrivateKey_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1276 if (!wolfSSL_use_certificate_buffer(ssl, spki->
s, spki->
length, WOLFSSL_FILETYPE_ASN1)) {
1291 if (!wolfSSL_use_certificate_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1304 if (!(wolfSSL_use_certificate_file(ssl,
1306 WOLFSSL_FILETYPE_ASN1))) {
1313 if (!(wolfSSL_use_certificate_buffer(ssl,
1316 WOLFSSL_FILETYPE_ASN1))) {
1337#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1339 char stype[] = {WOLFSSL_CERT_TYPE_X509, WOLFSSL_CERT_TYPE_RPK};
1340 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
1351 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1355 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1362 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1368 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1378 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1382 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1389 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1395 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1414get_san_or_cn_from_cert(WOLFSSL_X509 *x509) {
1418 WOLF_STACK_OF(WOLFSSL_GENERAL_NAME) *san_list;
1422 san_list = wolfSSL_X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
1424 int san_count = wolfSSL_sk_GENERAL_NAME_num(san_list);
1426 for (n = 0; n < san_count; n++) {
1427 const WOLFSSL_GENERAL_NAME *name = wolfSSL_sk_GENERAL_NAME_value(san_list, n);
1429 if (name->type == GEN_DNS) {
1430 const char *dns_name = (
const char *)wolfSSL_ASN1_STRING_get0_data(name->d.dNSName);
1433 if (wolfSSL_ASN1_STRING_length(name->d.dNSName) != (int)strlen(dns_name))
1435 cn = wolfssl_strdup(dns_name);
1436 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1440 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1443 wolfSSL_X509_NAME_oneline(wolfSSL_X509_get_subject_name((WOLFSSL_X509 *)(x509)), buffer,
1447 n = (int)strlen(buffer) - 3;
1450 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
1451 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
1460 char *ecn = strchr(cn,
'/');
1462 return wolfssl_strndup(cn, ecn-cn);
1464 return wolfssl_strdup(cn);
1472tls_verify_call_back(
int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx) {
1473 WOLFSSL *ssl = wolfSSL_X509_STORE_CTX_get_ex_data(ctx,
1474 wolfSSL_get_ex_data_X509_STORE_CTX_idx());
1476 coap_wolfssl_context_t *w_context =
1479 int depth = wolfSSL_X509_STORE_CTX_get_error_depth(ctx);
1480 int err = wolfSSL_X509_STORE_CTX_get_error(ctx);
1481 WOLFSSL_X509 *x509 = wolfSSL_X509_STORE_CTX_get_current_cert(ctx);
1483 int keep_preverify_ok = preverify_ok;
1486 cn = wolfssl_strdup(
"RPK");
1488 cn = get_san_or_cn_from_cert(x509);
1490 if (!preverify_ok) {
1492 case X509_V_ERR_CERT_NOT_YET_VALID:
1493 case X509_V_ERR_CERT_HAS_EXPIRED:
1494 case ASN_NO_SIGNER_E:
1495 case ASN_AFTER_DATE_E:
1499 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1503 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1507 case X509_V_ERR_UNABLE_TO_GET_CRL:
1511 case X509_V_ERR_CRL_NOT_YET_VALID:
1512 case X509_V_ERR_CRL_HAS_EXPIRED:
1516 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1517 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1518 case X509_V_ERR_AKID_SKID_MISMATCH:
1528 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1529 wolfSSL_X509_STORE_CTX_set_error(ctx, err);
1531 if (!preverify_ok) {
1532 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1535 "Unknown CA", cn ? cn :
"?", depth);
1539 wolfSSL_X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1544 wolfSSL_X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1549 int length = wolfSSL_i2d_X509(x509, NULL);
1553 uint8_t *base_buf2 = base_buf = wolfssl_malloc(length);
1557 wolfSSL_i2d_X509(x509, &base_buf2);
1560 depth, preverify_ok,
1564 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1566 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1570 wolfssl_free(base_buf);
1574 return preverify_ok;
1577#if COAP_SERVER_SUPPORT
1588tls_server_name_call_back(WOLFSSL *ssl,
1593 coap_wolfssl_context_t *w_context =
1597 return noack_return;
1602 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1606 if (!sni || !sni[0]) {
1613 return fatal_return;
1615 sni_setup_data = *setup_data;
1616 sni_setup_data.
pki_key = *new_entry;
1620 if (w_context->psk_pki_enabled & IS_PSK) {
1621 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1623 return SSL_TLSEXT_ERR_OK;
1634psk_tls_server_name_call_back(WOLFSSL *ssl,
1640 coap_wolfssl_context_t *w_context =
1644 return noack_return;
1649 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1653 if (!sni || !sni[0]) {
1662 snprintf(lhint,
sizeof(lhint),
"%.*s",
1665 wolfSSL_use_psk_identity_hint(ssl, lhint);
1669 if (w_context->psk_pki_enabled & IS_PSK) {
1670 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1672 return SSL_TLSEXT_ERR_OK;
1680 coap_wolfssl_context_t *w_context =
1686 w_context->setup_data = *setup_data;
1687 if (!w_context->setup_data.verify_peer_cert) {
1689 w_context->setup_data.check_common_ca = 0;
1690 if (w_context->setup_data.is_rpk_not_cert) {
1692 w_context->setup_data.allow_self_signed = 0;
1693 w_context->setup_data.allow_expired_certs = 0;
1694 w_context->setup_data.cert_chain_validation = 0;
1695 w_context->setup_data.cert_chain_verify_depth = 0;
1696 w_context->setup_data.check_cert_revocation = 0;
1697 w_context->setup_data.allow_no_crl = 0;
1698 w_context->setup_data.allow_expired_crl = 0;
1699 w_context->setup_data.allow_bad_md_hash = 0;
1700 w_context->setup_data.allow_short_rsa_length = 0;
1703 w_context->setup_data.allow_self_signed = 1;
1704 w_context->setup_data.allow_expired_certs = 1;
1705 w_context->setup_data.cert_chain_validation = 1;
1706 w_context->setup_data.cert_chain_verify_depth = 10;
1707 w_context->setup_data.check_cert_revocation = 1;
1708 w_context->setup_data.allow_no_crl = 1;
1709 w_context->setup_data.allow_expired_crl = 1;
1710 w_context->setup_data.allow_bad_md_hash = 1;
1711 w_context->setup_data.allow_short_rsa_length = 1;
1714#if COAP_SERVER_SUPPORT
1716 if (!setup_dtls_context(w_context))
1718 if (w_context->dtls.ctx) {
1719#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1720 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1721 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1724 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1725 &w_context->setup_data);
1726 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1727 tls_server_name_call_back);
1729#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1730 if (w_context->setup_data.is_rpk_not_cert) {
1731 wolfSSL_CTX_set_client_cert_type(w_context->dtls.ctx, ctype,
sizeof(ctype)/
sizeof(ctype[0]));
1732 wolfSSL_CTX_set_server_cert_type(w_context->dtls.ctx, stype,
sizeof(stype)/
sizeof(stype[0]));
1736#if !COAP_DISABLE_TCP
1737 if (!setup_tls_context(w_context))
1739 if (w_context->tls.ctx) {
1740 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1741 &w_context->setup_data);
1742 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1743 tls_server_name_call_back);
1746 wolfSSL_CTX_set_alpn_select_cb(w_context->tls.ctx,
1747 server_alpn_callback, NULL);
1751 if (w_context->setup_data.check_cert_revocation) {
1752 WOLFSSL_X509_VERIFY_PARAM *param;
1754 param = wolfSSL_X509_VERIFY_PARAM_new();
1755 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
1756 wolfSSL_CTX_set1_param(w_context->dtls.ctx, param);
1757#if !COAP_DISABLE_TCP
1758 wolfSSL_CTX_set1_param(w_context->tls.ctx, param);
1760 wolfSSL_X509_VERIFY_PARAM_free(param);
1763 if (w_context->setup_data.verify_peer_cert) {
1764 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1765 WOLFSSL_VERIFY_PEER |
1766 WOLFSSL_VERIFY_CLIENT_ONCE |
1767 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1768 tls_verify_call_back);
1769#if !COAP_DISABLE_TCP
1770 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1771 WOLFSSL_VERIFY_PEER |
1772 WOLFSSL_VERIFY_CLIENT_ONCE |
1773 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1774 tls_verify_call_back);
1777 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1778 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1779#if !COAP_DISABLE_TCP
1780 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1781 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1786 if (w_context->setup_data.cert_chain_validation) {
1787 wolfSSL_CTX_set_verify_depth(w_context->dtls.ctx,
1789#if !COAP_DISABLE_TCP
1790 wolfSSL_CTX_set_verify_depth(w_context->tls.ctx,
1799 w_context->psk_pki_enabled |= IS_PKI;
1801#if ! defined(WOLFSSL_DTLS_CID)
1810 const char *ca_file,
1811 const char *ca_dir) {
1812 coap_wolfssl_context_t *w_context =
1816 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
1820 if (ca_file == NULL && ca_dir == NULL) {
1821 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_dir "
1825 if (w_context->root_ca_file) {
1826 wolfssl_free(w_context->root_ca_file);
1827 w_context->root_ca_file = NULL;
1830 w_context->root_ca_file = wolfssl_strdup(ca_file);
1832 if (w_context->root_ca_dir) {
1833 wolfssl_free(w_context->root_ca_dir);
1834 w_context->root_ca_dir = NULL;
1837 w_context->root_ca_dir = wolfssl_strdup(ca_dir);
1844 coap_wolfssl_context_t *w_context =
1846 return w_context->psk_pki_enabled ? 1 : 0;
1852 coap_wolfssl_context_t *w_context = (coap_wolfssl_context_t *)handle;
1856 wolfssl_free(w_context->root_ca_file);
1857 wolfssl_free(w_context->root_ca_dir);
1859 if (w_context->dtls.ctx)
1860 wolfSSL_CTX_free(w_context->dtls.ctx);
1861 if (w_context->dtls.cookie_hmac)
1862 wolfSSL_HMAC_CTX_free(w_context->dtls.cookie_hmac);
1864#if !COAP_DISABLE_TCP
1865 if (w_context->tls.ctx)
1866 wolfSSL_CTX_free(w_context->tls.ctx);
1868 wolfssl_free(w_context);
1871#if COAP_SERVER_SUPPORT
1874 coap_wolfssl_context_t *w_context =
1876 coap_dtls_context_t *dtls;
1877 WOLFSSL *ssl = NULL;
1880 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
1886 if (!setup_dtls_context(w_context))
1888 dtls = &w_context->dtls;
1890 ssl = wolfSSL_new(dtls->ctx);
1894 wolfSSL_set_app_data(ssl, NULL);
1895 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
1896#ifdef WOLFSSL_DTLS_MTU
1897 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
1900 wolfSSL_SetIOWriteCtx(ssl, w_env);
1901 wolfSSL_SetIOReadCtx(ssl, w_env);
1902 wolfSSL_set_app_data(ssl, session);
1903 w_env->data.session = session;
1905#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
1906 if (wolfSSL_send_hrr_cookie(ssl, NULL, 0) != WOLFSSL_SUCCESS)
1907 coap_log_debug(
"Error: Unable to set cookie with Hello Retry Request\n");
1910#ifdef HAVE_SERVER_RENEGOTIATION_INFO
1911 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
1912 coap_log_debug(
"Error: wolfSSL_UseSecureRenegotiation failed\n");
1916 if (w_context->psk_pki_enabled & IS_PSK) {
1919 if (psk_hint != NULL && psk_hint->length) {
1920 char *hint = wolfssl_malloc(psk_hint->length + 1);
1923 memcpy(hint, psk_hint->s, psk_hint->length);
1924 hint[psk_hint->length] =
'\000';
1925 wolfSSL_use_psk_identity_hint(ssl, hint);
1932 if (w_context->psk_pki_enabled & IS_PKI) {
1937#if defined(WOLFSSL_DTLS_CH_FRAG) && defined(WOLFSSL_DTLS13)
1938 if (wolfSSL_dtls13_allow_ch_frag(ssl, 1) != WOLFSSL_SUCCESS) {
1943#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
1945#if COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
1946#bad COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
1949 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
1951 u_char cid[COAP_DTLS_CID_LENGTH];
1956 if (wolfSSL_dtls_cid_set(ssl, cid,
sizeof(cid)) != WOLFSSL_SUCCESS)
1962 w_env->last_timeout = now;
1965 r = wolfSSL_accept(ssl);
1967 int err = wolfSSL_get_error(ssl, r);
1968 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE)
1981 coap_dtls_free_wolfssl_env(w_env);
1986#if COAP_CLIENT_SUPPORT
1989 coap_wolfssl_context_t *w_context =
1992 if (w_context->psk_pki_enabled & IS_PSK) {
1997 wolfSSL_set_max_proto_version(ssl,
2000#if !COAP_DISABLE_TCP
2002 wolfSSL_set_max_proto_version(ssl,
2004 wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_3);
2007 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2009 set_ciphersuites(ssl, COAP_ENC_PSK);
2013 wolfSSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2014 coap_log_warn(
"wolfSSL_set_tlsext_host_name: set '%s' failed",
2017 wolfSSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2019#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2021 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2026 if (wolfSSL_dtls_cid_set(ssl, NULL, 0) != WOLFSSL_SUCCESS)
2031 if (w_context->psk_pki_enabled & IS_PKI) {
2034 set_ciphersuites(ssl, COAP_ENC_PKI);
2038#if !COAP_DISABLE_TCP
2040 wolfSSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
2045 wolfSSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2046 coap_log_warn(
"wolfSSL_set_tlsext_host_name: set '%s' failed",
2051 WOLFSSL_X509_VERIFY_PARAM *param;
2053 param = wolfSSL_X509_VERIFY_PARAM_new();
2054 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
2055 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
2057 wolfSSL_CTX_set1_param(ctx, param);
2058 wolfSSL_X509_VERIFY_PARAM_free(param);
2062 wolfSSL_set_verify(ssl,
2063 WOLFSSL_VERIFY_PEER |
2064 WOLFSSL_VERIFY_CLIENT_ONCE |
2065 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2066 tls_verify_call_back);
2068 wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
2074#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2076 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2081 if (wolfSSL_dtls_cid_set(ssl, NULL, 0) != WOLFSSL_SUCCESS)
2092 WOLFSSL *ssl = NULL;
2094 coap_wolfssl_context_t *w_context =
2096 coap_dtls_context_t *dtls;
2097 coap_wolfssl_env_t *w_env =
2104 if (!setup_dtls_context(w_context))
2106 dtls = &w_context->dtls;
2108 ssl = wolfSSL_new(dtls->ctx);
2112 w_env->data.session = session;
2113 wolfSSL_set_app_data(ssl, session);
2114 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2115 wolfSSL_SetIOWriteCtx(ssl, w_env);
2116 wolfSSL_SetIOReadCtx(ssl, w_env);
2117#ifdef WOLFSSL_DTLS_MTU
2118 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2121 if (!setup_client_ssl_session(session, ssl))
2123#ifdef HAVE_SERVER_RENEGOTIATION_INFO
2124 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
2125 coap_log_debug(
"Error: wolfSSL_UseSecureRenegotiation failed\n");
2131#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
2132 wolfSSL_NoKeyShares(ssl);
2134 r = wolfSSL_connect(ssl);
2136 int ret = wolfSSL_get_error(ssl, r);
2137 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2145 w_env->last_timeout = now;
2157#ifdef WOLFSSL_DTLS_MTU
2158 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2159 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2162 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2171 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2172 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2175 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2176 int r = wolfSSL_shutdown(ssl);
2178 wolfSSL_shutdown(ssl);
2185 coap_dtls_free_wolfssl_env(w_env);
2190 const uint8_t *data,
size_t data_len) {
2191 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2192 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2195 assert(ssl != NULL);
2200 r = wolfSSL_write(ssl, data, (
int)data_len);
2203 int err = wolfSSL_get_error(ssl, r);
2204 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2208 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2210 else if (err == WOLFSSL_ERROR_SSL)
2243 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2244 unsigned int scalar;
2251 scalar = 1 << w_env->retry_scalar;
2265 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2266 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2269 w_env->retry_scalar++;
2275 wolfSSL_dtls_retransmit(ssl);
2279#if COAP_SERVER_SUPPORT
2283 const uint8_t *data,
size_t data_len) {
2284 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2285 coap_ssl_data_t *ssl_data;
2290 session->
tls = w_env;
2297 ssl_data = w_env ? &w_env->data : NULL;
2298 assert(ssl_data != NULL);
2300 if (ssl_data->pdu_len) {
2301 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2305 ssl_data->session = session;
2306 ssl_data->pdu = data;
2307 ssl_data->pdu_len = (unsigned)data_len;
2316 coap_ssl_data_t *ssl_data;
2317 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2318 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2320 int in_init = wolfSSL_SSL_in_init(ssl);
2323 assert(ssl != NULL);
2325 ssl_data = &w_env->data;
2327 if (ssl_data->pdu_len) {
2328 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2331 ssl_data->pdu = data;
2332 ssl_data->pdu_len = (unsigned)data_len;
2335 r = wolfSSL_read(ssl, pdu, (
int)
sizeof(pdu));
2342 int err = wolfSSL_get_error(ssl, r);
2343 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2344 if (in_init && wolfSSL_is_init_finished(ssl)) {
2347#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) && COAP_CLIENT_SUPPORT
2350 if (wolfSSL_dtls_cid_is_enabled(ssl)) {
2358 if (!strcmp(wolfSSL_get_version(ssl),
"DTLSv1.3")) {
2367 }
else if (err == APP_DATA_READY) {
2368 r = wolfSSL_read(ssl, pdu, (
int)
sizeof(pdu));
2376 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2381 if (err == FATAL_ERROR) {
2382 WOLFSSL_ALERT_HISTORY h;
2384 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2385 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2388 wolfSSL_alert_desc_string_long(h.last_rx.code));
2409 if (ssl_data && ssl_data->pdu_len) {
2411 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
2412 ssl_data->pdu_len = 0;
2413 ssl_data->pdu = NULL;
2420 unsigned int overhead = 37;
2421 const WOLFSSL_CIPHER *s_ciph = NULL;
2422 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2423 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2426 s_ciph = wolfSSL_get_current_cipher(ssl);
2428 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
2430 const WOLFSSL_EVP_CIPHER *e_ciph;
2431 const WOLFSSL_EVP_MD *e_md;
2434 e_ciph = wolfSSL_EVP_get_cipherbynid(wolfSSL_CIPHER_get_cipher_nid(s_ciph));
2436 switch (WOLFSSL_EVP_CIPHER_mode(e_ciph)) {
2438 case WOLFSSL_EVP_CIPH_GCM_MODE:
2439#ifndef WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN
2440#define WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN 8
2442#ifndef WOLFSSL_EVP_GCM_TLS_TAG_LEN
2443#define WOLFSSL_EVP_GCM_TLS_TAG_LEN 16
2445 ivlen = WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN;
2446 maclen = WOLFSSL_EVP_GCM_TLS_TAG_LEN;
2449 case WOLFSSL_EVP_CIPH_CCM_MODE:
2450#ifndef WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN
2451#define WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN 8
2453 ivlen = WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN;
2454 wolfSSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
2455 if (strstr(cipher,
"CCM8"))
2461 case WOLFSSL_EVP_CIPH_CBC_MODE:
2462 e_md = wolfSSL_EVP_get_digestbynid(wolfSSL_CIPHER_get_digest_nid(s_ciph));
2463 blocksize = wolfSSL_EVP_CIPHER_block_size(e_ciph);
2464 ivlen = wolfSSL_EVP_CIPHER_iv_length(e_ciph);
2466 maclen = wolfSSL_EVP_MD_size(e_md);
2469 case WOLFSSL_EVP_CIPH_STREAM_CIPHER:
2476 wolfSSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
2483#ifndef WOLFSSL_DTLS13_RT_HEADER_LENGTH
2484#define WOLFSSL_DTLS13_RT_HEADER_LENGTH 13
2486 overhead = WOLFSSL_DTLS13_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 +
2492#if !COAP_DISABLE_TCP
2493#if COAP_CLIENT_SUPPORT
2496 WOLFSSL *ssl = NULL;
2498 coap_wolfssl_context_t *w_context =
2500 coap_tls_context_t *tls;
2501 coap_wolfssl_env_t *w_env =
2508 if (!setup_tls_context(w_context))
2510 tls = &w_context->tls;
2512 ssl = wolfSSL_new(tls->ctx);
2515 wolfSSL_SetIOWriteCtx(ssl, w_env);
2516 wolfSSL_SetIOReadCtx(ssl, w_env);
2517 wolfSSL_set_app_data(ssl, session);
2518 w_env->data.session = session;
2520 if (!setup_client_ssl_session(session, ssl))
2523 session->
tls = w_env;
2525 r = wolfSSL_connect(ssl);
2527 int ret = wolfSSL_get_error(ssl, r);
2528 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2530 if (ret == WOLFSSL_ERROR_WANT_READ)
2532 if (ret == WOLFSSL_ERROR_WANT_WRITE) {
2534#ifdef COAP_EPOLL_SUPPORT
2548 w_env->last_timeout = now;
2549 if (wolfSSL_is_init_finished(ssl)) {
2557 coap_dtls_free_wolfssl_env(w_env);
2564#if COAP_SERVER_SUPPORT
2567 WOLFSSL *ssl = NULL;
2568 coap_wolfssl_context_t *w_context =
2570 coap_tls_context_t *tls;
2573 coap_wolfssl_env_t *w_env =
2580 if (!setup_tls_context(w_context))
2582 tls = &w_context->tls;
2584 ssl = wolfSSL_new(tls->ctx);
2587 wolfSSL_SetIOWriteCtx(ssl, w_env);
2588 wolfSSL_SetIOReadCtx(ssl, w_env);
2589 wolfSSL_set_app_data(ssl, session);
2591 wolfSSL_set_cipher_list(ssl,
"ALL");
2593 if (w_context->psk_pki_enabled & IS_PSK) {
2595 if (psk_hint != NULL && psk_hint->
length) {
2596 char *hint = wolfssl_malloc(psk_hint->
length + 1);
2599 memcpy(hint, psk_hint->
s, psk_hint->
length);
2600 hint[psk_hint->
length] =
'\000';
2601 wolfSSL_use_psk_identity_hint(ssl, hint);
2608 if (w_context->psk_pki_enabled & IS_PKI) {
2612#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
2613 if (w_context->setup_data.is_rpk_not_cert) {
2614 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
2616 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
2621 w_env->last_timeout = now;
2623 w_env->data.session = session;
2625 r = wolfSSL_accept(ssl);
2627 int err = wolfSSL_get_error(ssl, r);
2628 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE) {
2631 if (err == WOLFSSL_ERROR_WANT_READ) {
2634 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2636#ifdef COAP_EPOLL_SUPPORT
2649 session->
tls = w_env;
2650 if (wolfSSL_is_init_finished(ssl)) {
2660 coap_dtls_free_wolfssl_env(w_env);
2667 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2668 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2671 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2672 int r = wolfSSL_shutdown(ssl);
2674 wolfSSL_shutdown(ssl);
2681 coap_dtls_free_wolfssl_env(w_env);
2691 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2692 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2698 in_init = !wolfSSL_is_init_finished(ssl);
2700 r = wolfSSL_write(ssl, data, (
int)data_len);
2703 int err = wolfSSL_get_error(ssl, r);
2704 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2705 if (in_init && wolfSSL_is_init_finished(ssl)) {
2711 if (err == WOLFSSL_ERROR_WANT_READ)
2713 else if (err == WOLFSSL_ERROR_WANT_WRITE) {
2715#ifdef COAP_EPOLL_SUPPORT
2727 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2729 else if (err == WOLFSSL_ERROR_SSL)
2733 }
else if (in_init && wolfSSL_is_init_finished(ssl)) {
2752 if (r == (ssize_t)data_len)
2769 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2770 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2778 in_init = !wolfSSL_is_init_finished(ssl);
2780 r = wolfSSL_read(ssl, data, (
int)data_len);
2782 int err = wolfSSL_get_error(ssl, r);
2783 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2784 if (in_init && wolfSSL_is_init_finished(ssl)) {
2790 if (err == WOLFSSL_ERROR_WANT_READ)
2792 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2794#ifdef COAP_EPOLL_SUPPORT
2804 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2807 }
else if (err == WOLFSSL_ERROR_SSL) {
2809 }
else if (err == FATAL_ERROR) {
2810 WOLFSSL_ALERT_HISTORY h;
2813 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2814 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2817 wolfSSL_alert_desc_string_long(h.last_rx.code));
2823 }
else if (in_init && wolfSSL_is_init_finished(ssl)) {
2849#if COAP_SERVER_SUPPORT
2852 WOLFSSL_EVP_MD_CTX *digest_ctx = wolfSSL_EVP_MD_CTX_new();
2855 wolfSSL_EVP_DigestInit_ex(digest_ctx, wolfSSL_EVP_sha256(), NULL);
2863 wolfSSL_EVP_MD_CTX_free(digest_ctx);
2868 const uint8_t *data,
2870 return wolfSSL_EVP_DigestUpdate(digest_ctx, data, data_len);
2877 int ret = wolfSSL_EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
2884#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
2886coap_crypto_output_errors(
const char *prefix) {
2887#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
2892 while ((e = wolfSSL_ERR_get_error()))
2895 wolfSSL_ERR_reason_error_string(e),
2896 ssl_function_definition(e));
2906static struct hash_algs {
2908 const WOLFSSL_EVP_MD *(*get_hash)(void);
2917static const WOLFSSL_EVP_MD *
2918get_hash_alg(
cose_alg_t alg,
size_t *length) {
2921 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2922 if (hashs[idx].alg == alg) {
2923 *length = hashs[idx].length;
2924 return hashs[idx].get_hash();
2927 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2935 unsigned int length;
2936 const WOLFSSL_EVP_MD *evp_md;
2937 WOLFSSL_EVP_MD_CTX *evp_ctx = NULL;
2941 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
2942 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2945 evp_ctx = wolfSSL_EVP_MD_CTX_new();
2946 if (evp_ctx == NULL)
2948 if (wolfSSL_EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
2951 if (wolfSSL_EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
2957 if (wolfSSL_EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
2959 dummy->length = length;
2960 if (hash_length < dummy->length)
2961 dummy->length = hash_length;
2963 wolfSSL_EVP_MD_CTX_free(evp_ctx);
2967 coap_crypto_output_errors(
"coap_crypto_hash");
2970 wolfSSL_EVP_MD_CTX_free(evp_ctx);
2975#if COAP_OSCORE_SUPPORT
2976#if LIBWOLFSSL_VERSION_HEX < 0x05006000
2977static const WOLFSSL_EVP_CIPHER *
2978EVP_aes_128_ccm(
void) {
2979 return "AES-128-CCM";
2982static const WOLFSSL_EVP_CIPHER *
2983EVP_aes_256_ccm(
void) {
2984 return "AES-256-CCM";
2998static struct cipher_algs {
3000 const WOLFSSL_EVP_CIPHER *(*get_cipher)(void);
3005static const WOLFSSL_EVP_CIPHER *
3009 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3010 if (ciphers[idx].alg == alg)
3011 return ciphers[idx].get_cipher();
3013 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3022static struct hmac_algs {
3024 const WOLFSSL_EVP_MD *(*get_hmac)(void);
3031static const WOLFSSL_EVP_MD *
3035 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3036 if (hmacs[idx].hmac_alg == hmac_alg)
3037 return hmacs[idx].get_hmac();
3039 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3045 return get_cipher_alg(alg) != NULL;
3054 return get_hmac_alg(hmac_alg) != NULL;
3058 if (1 != (Func)) { \
3067 size_t *max_result_len) {
3073 byte *authTag = NULL;
3079 assert(params != NULL);
3085 if (ccm->
key.
s == NULL || ccm->
nonce == NULL)
3088 result_len = data->
length;
3089 nonce_length = 15 - ccm->
l;
3091 memset(&aes, 0,
sizeof(aes));
3096 authTag = (
byte *)malloc(ccm->
tag_len *
sizeof(
byte));
3100 ret = wc_AesCcmEncrypt(&aes, result, data->
s, data->
length, ccm->
nonce,
3101 nonce_length, authTag, ccm->
tag_len,
3105 wolfssl_free(authTag);
3109 memcpy(result + result_len, authTag, ccm->
tag_len);
3110 result_len +=
sizeof(authTag);
3111 *max_result_len = result_len;
3112 wolfssl_free(authTag);
3116 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
3126 size_t *max_result_len) {
3139 assert(params != NULL);
3149 memcpy(authTag, data->
s + data->
length - ccm->
tag_len,
sizeof(authTag));
3153 if (ccm->
key.
s == NULL || ccm->
nonce == NULL)
3156 memset(&aes, 0,
sizeof(aes));
3163 ret = wc_AesCcmDecrypt(&aes, result, data->
s, len, ccm->
nonce,
3164 15 - ccm->
l, authTag,
sizeof(authTag),
3170 *max_result_len = len;
3174 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
3183 unsigned int result_len;
3184 const WOLFSSL_EVP_MD *evp_md;
3191 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
3192 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3198 result_len = (
unsigned int)
dummy->length;
3199 if (wolfSSL_HMAC(evp_md,
3206 dummy->length = result_len;
3211 coap_crypto_output_errors(
"coap_crypto_hmac");
3223#pragma GCC diagnostic ignored "-Wunused-function"
static size_t strnlen(const char *s, size_t maxlen)
A length-safe strlen() fake.
#define COAP_SERVER_SUPPORT
#define COAP_RXBUFFER_SIZE
#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 ...
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)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
static coap_log_t dtls_log_level
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
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)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
coap_binary_t * get_asn1_spki(const uint8_t *data, size_t size)
Abstract SPKI public key from the ASN1.
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
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.
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.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
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.
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
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_FAIL_NOT_SUPPORTED
#define COAP_DTLS_HINT_LENGTH
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
@ COAP_TLS_LIBRARY_WOLFSSL
Using wolfSSL library.
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ COAP_EVENT_DTLS_RENEGOTIATE
Triggered when (D)TLS session renegotiated.
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
#define coap_lock_callback_ret(r, c, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_dtls_log(level,...)
Logging function.
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
#define coap_log(level,...)
Logging function.
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
@ 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_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
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...
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
The CoAP stack's global state is stored in a coap_context_t object.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
size_t tag_len
The size of the Tag.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@2 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
The structure that holds the Client PSK information.
coap_bin_const_t identity
The structure used for defining the Client PSK setup data to be used.
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
The structure that holds the PKI key information.
coap_pki_key_define_t define
for definable type keys
union coap_dtls_key_t::@3 key
coap_pki_key_t key_type
key format type
The structure used for defining the PKI setup data to be used.
uint8_t allow_no_crl
1 ignore if CRL not there
void * cn_call_back_arg
Passed in to the CN callback function.
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
uint8_t check_cert_revocation
1 if revocation checks wanted
coap_dtls_pki_sni_callback_t validate_sni_call_back
SNI check callback function.
uint8_t cert_chain_verify_depth
recommended depth is 3
uint8_t allow_expired_certs
1 if expired certs are allowed
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
char * client_sni
If not NULL, SNI to use in client TLS setup.
uint8_t allow_self_signed
1 if self-signed certs are allowed.
void * sni_call_back_arg
Passed in to the sni callback function.
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
uint8_t allow_expired_crl
1 if expired crl is allowed
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
coap_dtls_key_t pki_key
PKI key definition.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
void * sni_call_back_arg
Passed in to the SNI callback function.
coap_dtls_spsk_info_t psk_info
Server PSK definition.
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
coap_const_char_ptr_t private_key
define: Private Key
coap_const_char_ptr_t ca
define: Common CA Certificate
size_t public_cert_len
define Public Cert length (if needed)
size_t ca_len
define CA Cert length (if needed)
coap_pki_define_t private_key_def
define: Private Key type definition
size_t private_key_len
define Private Key length (if needed)
coap_pki_define_t ca_def
define: Common CA type definition
coap_pki_define_t public_cert_def
define: Public Cert type definition
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_bin_const_t * client_cid
Contains client CID or NULL.
coap_proto_t proto
protocol used
uint8_t is_dtls13
Set if session is DTLS1.3.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t negotiated_cid
Set for a client if CID negotiated.
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
The structure used for returning the underlying (D)TLS library information.
uint64_t built_version
(D)TLS Built against Library Version
coap_tls_library_t type
Library type.
uint64_t version
(D)TLS runtime Library Version
const char * s_byte
signed char ptr
const uint8_t * u_byte
unsigned char ptr