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;
544 if (data && data->session) {
547 && data->session->endpoint == NULL
554 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
559 w_env->last_timeout = now;
560 }
else if (ret < 0 && (errno == ENOTCONN || errno == ECONNREFUSED)) {
569#if COAP_CLIENT_SUPPORT
571coap_dtls_psk_client_callback(WOLFSSL *ssl,
574 unsigned int max_identity_len,
576 unsigned int max_psk_len) {
578 coap_wolfssl_context_t *w_context;
585 if (c_session == NULL || c_session->
context == NULL)
588 if (w_context == NULL)
596 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
597 temp.
length = strlen((
const char *)temp.
s);
601 (
const char *)temp.
s);
611 if (cpsk_info == NULL)
616 psk_identity = &cpsk_info->
identity;
617 psk_key = &cpsk_info->
key;
623 if (psk_identity == NULL || psk_key == NULL) {
629 if (!max_identity_len)
632 if (psk_identity->
length > max_identity_len) {
633 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
637 max_identity_len = (
unsigned int)psk_identity->
length;
639 memcpy(identity, psk_identity->
s, max_identity_len);
640 identity[max_identity_len] =
'\000';
642 if (psk_key->
length > max_psk_len) {
647 max_psk_len = (
unsigned int)psk_key->
length;
649 memcpy(psk, psk_key->
s, max_psk_len);
654coap_dtls_psk_client_cs_callback(WOLFSSL *ssl,
const char *hint,
655 char *identity,
unsigned int max_identity_len,
656 unsigned char *psk,
unsigned int max_psk_len,
657 const char *ciphersuite) {
658 int key_len = coap_dtls_psk_client_callback(ssl,
671#if COAP_SERVER_SUPPORT
673coap_dtls_psk_server_callback(
675 const char *identity,
677 unsigned int max_psk_len) {
684 if (c_session == NULL || c_session->
context == NULL)
690 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
691 lidentity.
length = strlen((
const char *)lidentity.
s);
695 (
int)lidentity.
length, (
const char *)lidentity.
s);
710 if (psk_key->
length > max_psk_len) {
715 max_psk_len = (
unsigned int)psk_key->
length;
717 memcpy(psk, psk_key->
s, max_psk_len);
723ssl_function_definition(
unsigned long e) {
724 static char buff[80];
726 snprintf(buff,
sizeof(buff),
" at %s:%s",
727 wolfSSL_ERR_lib_error_string(e), wolfSSL_ERR_func_error_string(e));
732coap_dtls_info_callback(
const WOLFSSL *ssl,
int where,
int ret) {
735 int w = where &~SSL_ST_MASK;
739 "coap_dtls_info_callback: session not determined, where 0x%0x and ret 0x%0x\n", where, ret);
743 if (w & SSL_ST_CONNECT)
744 pstr =
"wolfSSL_connect";
745 else if (w & SSL_ST_ACCEPT)
746 pstr =
"wolfSSL_accept";
750 if (where & SSL_CB_LOOP) {
753 }
else if (where & SSL_CB_ALERT) {
755 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
756 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == WOLFSSL3_AL_FATAL) {
758 if ((ret & 0xff) != close_notify)
763 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
766 wolfSSL_alert_type_string_long(ret),
767 wolfSSL_alert_desc_string_long(ret));
768 }
else if (where & SSL_CB_EXIT) {
774 while ((e = wolfSSL_ERR_get_error()))
777 ssl_function_definition(e));
779 }
else if (ret < 0) {
784 memcpy(&rw_ssl, &ssl,
sizeof(rw_ssl));
785 int err = wolfSSL_get_error(rw_ssl, ret);
786 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE &&
787 err != WOLFSSL_ERROR_WANT_CONNECT && err != WOLFSSL_ERROR_WANT_ACCEPT &&
788 err != WOLFSSL_ERROR_WANT_X509_LOOKUP) {
792 while ((e = wolfSSL_ERR_get_error()))
795 ssl_function_definition(e));
801 if (where == SSL_CB_HANDSHAKE_START) {
805 memcpy(&rw_ssl, &ssl,
sizeof(rw_ssl));
806 if (wolfSSL_is_init_finished(rw_ssl))
818coap_sock_read(WOLFSSL *ssl,
char *out,
int outl,
void *ctx) {
819 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
824 if (w_env && !w_env->done_psk_check && w_env->ssl &&
826 if (wolfSSL_SSL_in_init(w_env->ssl)) {
827 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
832 if (strstr(name,
"PSK")) {
833 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
834 w_env->done_psk_check = 1;
839 if (session && out != NULL) {
856coap_sock_write(WOLFSSL *ssl,
char *in,
int inl,
void *ctx) {
857 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
877 (errno == EPIPE || errno == ECONNRESET)) {
897coap_set_user_prefs(WOLFSSL_CTX *ctx) {
900#ifdef COAP_WOLFSSL_SIGALGS
901 wolfSSL_CTX_set1_sigalgs_list(ctx, COAP_WOLFSSL_SIGALGS);
903#ifdef COAP_WOLFSSL_GROUPS
905 ret = wolfSSL_CTX_set1_groups_list(ctx,
906 (
char *) COAP_WOLFSSL_GROUPS);
907 if (ret != WOLFSSL_SUCCESS) {
915setup_dtls_context(coap_wolfssl_context_t *w_context) {
916 if (!w_context->dtls.ctx) {
917 uint8_t cookie_secret[32];
920 w_context->dtls.ctx = wolfSSL_CTX_new(wolfDTLS_method());
921 if (!w_context->dtls.ctx)
923 wolfSSL_CTX_set_min_proto_version(w_context->dtls.ctx,
925 wolfSSL_CTX_set_ex_data(w_context->dtls.ctx, 0, &w_context->dtls);
926 coap_set_user_prefs(w_context->dtls.ctx);
927 memset(cookie_secret, 0,
sizeof(cookie_secret));
928 if (!wolfSSL_RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
930 "Insufficient entropy for random cookie generation");
933 w_context->dtls.cookie_hmac = wolfSSL_HMAC_CTX_new();
934 if (!wolfSSL_HMAC_Init_ex(w_context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
935 wolfSSL_EVP_sha256(), NULL))
938 wolfSSL_CTX_set_info_callback(w_context->dtls.ctx, coap_dtls_info_callback);
939 wolfSSL_CTX_set_options(w_context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
940 wolfSSL_SetIORecv(w_context->dtls.ctx, coap_dgram_read);
941 wolfSSL_SetIOSend(w_context->dtls.ctx, coap_dgram_write);
942#ifdef WOLFSSL_DTLS_MTU
945 if (w_context->root_ca_file || w_context->root_ca_dir) {
946 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->dtls.ctx,
947 w_context->root_ca_file,
948 w_context->root_ca_dir,
949 w_context->setup_data.allow_expired_certs ?
950 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
952 w_context->root_ca_file ? w_context->root_ca_file :
"NULL",
953 w_context->root_ca_dir ? w_context->root_ca_dir :
"NULL");
958 if (w_context->setup_data.verify_peer_cert)
959 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
960 WOLFSSL_VERIFY_PEER |
961 WOLFSSL_VERIFY_CLIENT_ONCE |
962 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
963 tls_verify_call_back);
965 wolfSSL_CTX_set_verify(w_context->dtls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
978setup_tls_context(coap_wolfssl_context_t *w_context) {
979 if (!w_context->tls.ctx) {
981 w_context->tls.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
982 if (!w_context->tls.ctx)
984 wolfSSL_CTX_set_ex_data(w_context->tls.ctx, 0, &w_context->tls);
985 wolfSSL_CTX_set_min_proto_version(w_context->tls.ctx, TLS1_VERSION);
986 coap_set_user_prefs(w_context->tls.ctx);
987 wolfSSL_CTX_set_info_callback(w_context->tls.ctx, coap_dtls_info_callback);
988 wolfSSL_SetIORecv(w_context->tls.ctx, coap_sock_read);
989 wolfSSL_SetIOSend(w_context->tls.ctx, coap_sock_write);
990#if COAP_CLIENT_SUPPORT
991 if (w_context->psk_pki_enabled & IS_PSK) {
992 wolfSSL_CTX_set_psk_client_cs_callback(w_context->tls.ctx,
993 coap_dtls_psk_client_cs_callback);
996 if (w_context->root_ca_file || w_context->root_ca_dir) {
997 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->tls.ctx,
998 w_context->root_ca_file,
999 w_context->root_ca_dir,
1000 w_context->setup_data.allow_expired_certs ?
1001 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1003 w_context->root_ca_file ? w_context->root_ca_file :
"NULL",
1004 w_context->root_ca_dir ? w_context->root_ca_dir :
"NULL");
1009 if (w_context->setup_data.verify_peer_cert)
1010 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1011 WOLFSSL_VERIFY_PEER |
1012 WOLFSSL_VERIFY_CLIENT_ONCE |
1013 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1014 tls_verify_call_back);
1016 wolfSSL_CTX_set_verify(w_context->tls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1028 coap_wolfssl_context_t *w_context;
1031 w_context = (coap_wolfssl_context_t *)wolfssl_malloc(
sizeof(coap_wolfssl_context_t));
1033 memset(w_context, 0,
sizeof(coap_wolfssl_context_t));
1039#if COAP_SERVER_SUPPORT
1044 coap_wolfssl_context_t *w_context =
1047 if (!setup_data || !w_context)
1050 if (!setup_dtls_context(w_context))
1052#if !COAP_DISABLE_TCP
1053 if (!setup_tls_context(w_context))
1057 wolfSSL_CTX_set_psk_server_callback(w_context->dtls.ctx,
1058 coap_dtls_psk_server_callback);
1060#if !COAP_DISABLE_TCP
1061 wolfSSL_CTX_set_psk_server_callback(w_context->tls.ctx,
1062 coap_dtls_psk_server_callback);
1068 wolfSSL_CTX_use_psk_identity_hint(w_context->dtls.ctx, hint);
1069#if !COAP_DISABLE_TCP
1070 wolfSSL_CTX_use_psk_identity_hint(w_context->tls.ctx, hint);
1074 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1076 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1077 psk_tls_server_name_call_back);
1078#if !COAP_DISABLE_TCP
1079 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1081 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1082 psk_tls_server_name_call_back);
1088 w_context->psk_pki_enabled |= IS_PSK;
1093#if COAP_CLIENT_SUPPORT
1098 coap_wolfssl_context_t *w_context =
1101 if (!setup_data || !w_context)
1108#if ! defined(WOLFSSL_DTLS_CID)
1112 w_context->psk_pki_enabled |= IS_PSK;
1117#if !COAP_DISABLE_TCP
1118static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1120#if COAP_SERVER_SUPPORT
1123 const unsigned char **out,
1124 unsigned char *outlen,
1125 const unsigned char *in,
1129 unsigned char *tout = NULL;
1132 return SSL_TLSEXT_ERR_NOACK;
1133 ret = wolfSSL_select_next_proto(&tout,
1140 return (ret != OPENSSL_NPN_NEGOTIATED) ? noack_return : WOLFSSL_TLSEXT_ERR_OK;
1146setup_pki_ssl(WOLFSSL *ssl,
1149 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
1163 if (!(wolfSSL_use_PrivateKey_file(ssl,
1165 WOLFSSL_FILETYPE_PEM))) {
1172 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1175 WOLFSSL_FILETYPE_PEM))) {
1182#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1183 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1186 WOLFSSL_FILETYPE_PEM))) {
1198 if (!(wolfSSL_use_PrivateKey_file(ssl,
1200 WOLFSSL_FILETYPE_ASN1))) {
1207 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1210 WOLFSSL_FILETYPE_ASN1))) {
1239 if (!(wolfSSL_use_certificate_chain_file(ssl,
1247 if (!(wolfSSL_use_certificate_chain_buffer(ssl,
1256#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1258 unsigned char der_buff[512];
1260 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1261 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1263 wolfSSL_set_client_cert_type(ssl, ctype,
sizeof(ctype)/
sizeof(ctype[0]));
1264 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
1268 der_buff, (
int)
sizeof(der_buff));
1272 der_buff, (
int)
sizeof(der_buff), NULL);
1281 if (!wolfSSL_use_PrivateKey_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1286 if (!wolfSSL_use_certificate_buffer(ssl, spki->
s, spki->
length, WOLFSSL_FILETYPE_ASN1)) {
1301 if (!wolfSSL_use_certificate_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1314 if (!(wolfSSL_use_certificate_file(ssl,
1316 WOLFSSL_FILETYPE_ASN1))) {
1323 if (!(wolfSSL_use_certificate_buffer(ssl,
1326 WOLFSSL_FILETYPE_ASN1))) {
1347#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1349 char stype[] = {WOLFSSL_CERT_TYPE_X509, WOLFSSL_CERT_TYPE_RPK};
1350 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
1361 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1365 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1372 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1378 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1388 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1392 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1399 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1405 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1424get_san_or_cn_from_cert(WOLFSSL_X509 *x509) {
1428 WOLF_STACK_OF(WOLFSSL_GENERAL_NAME) *san_list;
1432 san_list = wolfSSL_X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
1434 int san_count = wolfSSL_sk_GENERAL_NAME_num(san_list);
1436 for (n = 0; n < san_count; n++) {
1437 const WOLFSSL_GENERAL_NAME *name = wolfSSL_sk_GENERAL_NAME_value(san_list, n);
1439 if (name && name->type == GEN_DNS) {
1440 const char *dns_name = (
const char *)wolfSSL_ASN1_STRING_get0_data(name->d.dNSName);
1443 if (wolfSSL_ASN1_STRING_length(name->d.dNSName) != (
int)strlen(dns_name))
1445 cn = wolfssl_strdup(dns_name);
1446 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1450 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1453 wolfSSL_X509_NAME_oneline(wolfSSL_X509_get_subject_name((WOLFSSL_X509 *)(x509)), buffer,
1457 n = (int)strlen(buffer) - 3;
1460 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
1461 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
1470 char *ecn = strchr(cn,
'/');
1472 return wolfssl_strndup(cn, ecn-cn);
1474 return wolfssl_strdup(cn);
1482tls_verify_call_back(
int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx) {
1483 int index = wolfSSL_get_ex_data_X509_STORE_CTX_idx();
1484 WOLFSSL *ssl = index >= 0 ? wolfSSL_X509_STORE_CTX_get_ex_data(ctx, index) : NULL;
1485 coap_session_t *session = ssl ? wolfSSL_get_app_data(ssl) : NULL;
1486 coap_wolfssl_context_t *w_context = (session && session->
context) ?
1488 coap_dtls_pki_t *setup_data = w_context ? &w_context->setup_data : NULL;
1489 int depth = wolfSSL_X509_STORE_CTX_get_error_depth(ctx);
1490 int err = wolfSSL_X509_STORE_CTX_get_error(ctx);
1491 WOLFSSL_X509 *x509 = wolfSSL_X509_STORE_CTX_get_current_cert(ctx);
1493 int keep_preverify_ok = preverify_ok;
1496 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
1501 cn = wolfssl_strdup(
"RPK");
1503 cn = x509 ? get_san_or_cn_from_cert(x509) : NULL;
1506 depth, err, preverify_ok, cn ? cn :
"");
1507 if (!preverify_ok) {
1509 case X509_V_ERR_CERT_NOT_YET_VALID:
1510 case X509_V_ERR_CERT_HAS_EXPIRED:
1511 case ASN_NO_SIGNER_E:
1512 case ASN_AFTER_DATE_E:
1516 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1520 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1524 case X509_V_ERR_UNABLE_TO_GET_CRL:
1528 case X509_V_ERR_CRL_NOT_YET_VALID:
1529 case X509_V_ERR_CRL_HAS_EXPIRED:
1533 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1534 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1535 case X509_V_ERR_AKID_SKID_MISMATCH:
1545 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1546 wolfSSL_X509_STORE_CTX_set_error(ctx, err);
1548 if (!preverify_ok) {
1549 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1552 "Unknown CA", cn ? cn :
"?", depth);
1556 wolfSSL_X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1561 wolfSSL_X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1566 int length = wolfSSL_i2d_X509(x509, NULL);
1570 uint8_t *base_buf2 = base_buf = wolfssl_malloc(length);
1575 wolfSSL_i2d_X509(x509, &base_buf2);
1578 depth, preverify_ok,
1582 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1584 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1588 wolfssl_free(base_buf);
1590 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
1596 return preverify_ok;
1599#if COAP_SERVER_SUPPORT
1610tls_server_name_call_back(WOLFSSL *ssl,
1615 coap_wolfssl_context_t *w_context = (session && session->
context) ?
1619 return noack_return;
1624 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1628 if (!sni || !sni[0]) {
1635 return fatal_return;
1637 sni_setup_data = *setup_data;
1638 sni_setup_data.
pki_key = *new_entry;
1642 if (w_context->psk_pki_enabled & IS_PSK) {
1643 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1645 return SSL_TLSEXT_ERR_OK;
1656psk_tls_server_name_call_back(WOLFSSL *ssl,
1662 coap_wolfssl_context_t *w_context = (c_session && c_session->
context) ?
1666 return noack_return;
1671 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1675 if (!sni || !sni[0]) {
1684 snprintf(lhint,
sizeof(lhint),
"%.*s",
1687 wolfSSL_use_psk_identity_hint(ssl, lhint);
1691 if (w_context->psk_pki_enabled & IS_PSK) {
1692 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1694 return SSL_TLSEXT_ERR_OK;
1702 coap_wolfssl_context_t *w_context =
1708 w_context->setup_data = *setup_data;
1709 if (!w_context->setup_data.verify_peer_cert) {
1711 w_context->setup_data.check_common_ca = 0;
1712 if (w_context->setup_data.is_rpk_not_cert) {
1714 w_context->setup_data.allow_self_signed = 0;
1715 w_context->setup_data.allow_expired_certs = 0;
1716 w_context->setup_data.cert_chain_validation = 0;
1717 w_context->setup_data.cert_chain_verify_depth = 0;
1718 w_context->setup_data.check_cert_revocation = 0;
1719 w_context->setup_data.allow_no_crl = 0;
1720 w_context->setup_data.allow_expired_crl = 0;
1721 w_context->setup_data.allow_bad_md_hash = 0;
1722 w_context->setup_data.allow_short_rsa_length = 0;
1725 w_context->setup_data.allow_self_signed = 1;
1726 w_context->setup_data.allow_expired_certs = 1;
1727 w_context->setup_data.cert_chain_validation = 1;
1728 w_context->setup_data.cert_chain_verify_depth = 10;
1729 w_context->setup_data.check_cert_revocation = 1;
1730 w_context->setup_data.allow_no_crl = 1;
1731 w_context->setup_data.allow_expired_crl = 1;
1732 w_context->setup_data.allow_bad_md_hash = 1;
1733 w_context->setup_data.allow_short_rsa_length = 1;
1736#if COAP_SERVER_SUPPORT
1738 if (!setup_dtls_context(w_context))
1740 if (w_context->dtls.ctx) {
1741#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1742 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1743 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1746 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1747 &w_context->setup_data);
1748 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1749 tls_server_name_call_back);
1751#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1752 if (w_context->setup_data.is_rpk_not_cert) {
1753 wolfSSL_CTX_set_client_cert_type(w_context->dtls.ctx, ctype,
sizeof(ctype)/
sizeof(ctype[0]));
1754 wolfSSL_CTX_set_server_cert_type(w_context->dtls.ctx, stype,
sizeof(stype)/
sizeof(stype[0]));
1758#if !COAP_DISABLE_TCP
1759 if (!setup_tls_context(w_context))
1761 if (w_context->tls.ctx) {
1762 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1763 &w_context->setup_data);
1764 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1765 tls_server_name_call_back);
1768 wolfSSL_CTX_set_alpn_select_cb(w_context->tls.ctx,
1769 server_alpn_callback, NULL);
1773 if (w_context->setup_data.check_cert_revocation) {
1774 WOLFSSL_X509_VERIFY_PARAM *param;
1776 param = wolfSSL_X509_VERIFY_PARAM_new();
1777 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
1778 wolfSSL_CTX_set1_param(w_context->dtls.ctx, param);
1779#if !COAP_DISABLE_TCP
1780 wolfSSL_CTX_set1_param(w_context->tls.ctx, param);
1782 wolfSSL_X509_VERIFY_PARAM_free(param);
1785 if (w_context->setup_data.verify_peer_cert) {
1786 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1787 WOLFSSL_VERIFY_PEER |
1788 WOLFSSL_VERIFY_CLIENT_ONCE |
1789 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1790 tls_verify_call_back);
1791#if !COAP_DISABLE_TCP
1792 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1793 WOLFSSL_VERIFY_PEER |
1794 WOLFSSL_VERIFY_CLIENT_ONCE |
1795 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1796 tls_verify_call_back);
1799 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1800 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1801#if !COAP_DISABLE_TCP
1802 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1803 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1808 if (w_context->setup_data.cert_chain_validation) {
1809 wolfSSL_CTX_set_verify_depth(w_context->dtls.ctx,
1811#if !COAP_DISABLE_TCP
1812 wolfSSL_CTX_set_verify_depth(w_context->tls.ctx,
1821 w_context->psk_pki_enabled |= IS_PKI;
1823#if ! defined(WOLFSSL_DTLS_CID)
1832 const char *ca_file,
1833 const char *ca_dir) {
1834 coap_wolfssl_context_t *w_context =
1838 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
1842 if (ca_file == NULL && ca_dir == NULL) {
1843 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_dir "
1847 if (w_context->root_ca_file) {
1848 wolfssl_free(w_context->root_ca_file);
1849 w_context->root_ca_file = NULL;
1852 w_context->root_ca_file = wolfssl_strdup(ca_file);
1854 if (w_context->root_ca_dir) {
1855 wolfssl_free(w_context->root_ca_dir);
1856 w_context->root_ca_dir = NULL;
1859 w_context->root_ca_dir = wolfssl_strdup(ca_dir);
1866 coap_wolfssl_context_t *w_context =
1868 return w_context->psk_pki_enabled ? 1 : 0;
1874 coap_wolfssl_context_t *w_context = (coap_wolfssl_context_t *)handle;
1878 wolfssl_free(w_context->root_ca_file);
1879 wolfssl_free(w_context->root_ca_dir);
1881 if (w_context->dtls.ctx)
1882 wolfSSL_CTX_free(w_context->dtls.ctx);
1883 if (w_context->dtls.cookie_hmac)
1884 wolfSSL_HMAC_CTX_free(w_context->dtls.cookie_hmac);
1886#if !COAP_DISABLE_TCP
1887 if (w_context->tls.ctx)
1888 wolfSSL_CTX_free(w_context->tls.ctx);
1890 wolfssl_free(w_context);
1893#if COAP_SERVER_SUPPORT
1896 coap_wolfssl_context_t *w_context = session && session->
context ?
1898 coap_dtls_context_t *dtls;
1899 WOLFSSL *ssl = NULL;
1902 coap_wolfssl_env_t *w_env = session ? (coap_wolfssl_env_t *)session->
tls : NULL;
1905 if (!w_env || !w_context)
1908 if (!setup_dtls_context(w_context))
1910 dtls = &w_context->dtls;
1912 ssl = wolfSSL_new(dtls->ctx);
1916 wolfSSL_set_app_data(ssl, NULL);
1917 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
1918#ifdef WOLFSSL_DTLS_MTU
1919 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
1922 wolfSSL_SetIOWriteCtx(ssl, w_env);
1923 wolfSSL_SetIOReadCtx(ssl, w_env);
1924 wolfSSL_set_app_data(ssl, session);
1925 w_env->data.session = session;
1927#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
1928 if (wolfSSL_send_hrr_cookie(ssl, NULL, 0) != WOLFSSL_SUCCESS)
1929 coap_log_debug(
"Error: Unable to set cookie with Hello Retry Request\n");
1932#ifdef HAVE_SERVER_RENEGOTIATION_INFO
1933 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
1934 coap_log_debug(
"Error: wolfSSL_UseSecureRenegotiation failed\n");
1938 if (w_context->psk_pki_enabled & IS_PSK) {
1941 if (psk_hint != NULL && psk_hint->
length) {
1942 char *hint = wolfssl_malloc(psk_hint->
length + 1);
1945 memcpy(hint, psk_hint->
s, psk_hint->
length);
1946 hint[psk_hint->
length] =
'\000';
1947 wolfSSL_use_psk_identity_hint(ssl, hint);
1954 if (w_context->psk_pki_enabled & IS_PKI) {
1959#if defined(WOLFSSL_DTLS_CH_FRAG) && defined(WOLFSSL_DTLS13)
1960 if (wolfSSL_dtls13_allow_ch_frag(ssl, 1) != WOLFSSL_SUCCESS) {
1965#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
1967#if COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
1968#bad COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
1971 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
1973 u_char cid[COAP_DTLS_CID_LENGTH];
1978 if (wolfSSL_dtls_cid_set(ssl, cid,
sizeof(cid)) != WOLFSSL_SUCCESS)
1984 w_env->last_timeout = now;
1987 r = wolfSSL_accept(ssl);
1989 int err = wolfSSL_get_error(ssl, r);
1990 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE)
2003 coap_dtls_free_wolfssl_env(w_env);
2008#if COAP_CLIENT_SUPPORT
2011 coap_wolfssl_context_t *w_context =
2014 if (w_context->psk_pki_enabled & IS_PSK) {
2019 wolfSSL_set_max_proto_version(ssl,
2022#if !COAP_DISABLE_TCP
2024 wolfSSL_set_max_proto_version(ssl,
2026 wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_3);
2029 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2031 set_ciphersuites(ssl, COAP_ENC_PSK);
2035 wolfSSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2036 coap_log_warn(
"wolfSSL_set_tlsext_host_name: set '%s' failed",
2039 wolfSSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2041#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2043 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2048 if (wolfSSL_dtls_cid_set(ssl, NULL, 0) != WOLFSSL_SUCCESS)
2053 if ((w_context->psk_pki_enabled & IS_PKI) ||
2054 (w_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
2061 if (!(w_context->psk_pki_enabled & IS_PKI)) {
2075 set_ciphersuites(ssl, COAP_ENC_PKI);
2079#if !COAP_DISABLE_TCP
2081 wolfSSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
2086 wolfSSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2087 coap_log_warn(
"wolfSSL_set_tlsext_host_name: set '%s' failed",
2092 WOLFSSL_X509_VERIFY_PARAM *param;
2094 param = wolfSSL_X509_VERIFY_PARAM_new();
2095 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
2096 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
2098 wolfSSL_CTX_set1_param(ctx, param);
2099 wolfSSL_X509_VERIFY_PARAM_free(param);
2103 wolfSSL_set_verify(ssl,
2104 WOLFSSL_VERIFY_PEER |
2105 WOLFSSL_VERIFY_CLIENT_ONCE |
2106 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2107 tls_verify_call_back);
2109 wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
2115#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2117 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2122 if (wolfSSL_dtls_cid_set(ssl, NULL, 0) != WOLFSSL_SUCCESS)
2133 WOLFSSL *ssl = NULL;
2135 coap_wolfssl_context_t *w_context = session && session->
context ?
2137 coap_dtls_context_t *dtls;
2138 coap_wolfssl_env_t *w_env = session ?
2142 if (!w_env || !w_context)
2145 if (!setup_dtls_context(w_context))
2147 dtls = &w_context->dtls;
2149 ssl = wolfSSL_new(dtls->ctx);
2154 w_env->data.session = session;
2155 wolfSSL_set_app_data(ssl, session);
2156 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2157 wolfSSL_SetIOWriteCtx(ssl, w_env);
2158 wolfSSL_SetIOReadCtx(ssl, w_env);
2159#ifdef WOLFSSL_DTLS_MTU
2160 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2163 if (!setup_client_ssl_session(session, ssl))
2165#ifdef HAVE_SERVER_RENEGOTIATION_INFO
2166 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
2167 coap_log_debug(
"Error: wolfSSL_UseSecureRenegotiation failed\n");
2173#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
2174 wolfSSL_NoKeyShares(ssl);
2176 r = wolfSSL_connect(ssl);
2178 int ret = wolfSSL_get_error(ssl, r);
2179 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2187 w_env->last_timeout = now;
2199#ifdef WOLFSSL_DTLS_MTU
2200 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2201 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2204 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2213 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2214 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2217 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2218 int r = wolfSSL_shutdown(ssl);
2220 wolfSSL_shutdown(ssl);
2227 coap_dtls_free_wolfssl_env(w_env);
2232 const uint8_t *data,
size_t data_len) {
2233 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2234 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2237 assert(ssl != NULL);
2242 r = wolfSSL_write(ssl, data, (
int)data_len);
2245 int err = wolfSSL_get_error(ssl, r);
2246 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2250 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2252 else if (err == WOLFSSL_ERROR_SSL)
2285 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2286 unsigned int scalar;
2293 scalar = 1 << w_env->retry_scalar;
2307 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2308 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2311 w_env->retry_scalar++;
2317 wolfSSL_dtls_retransmit(ssl);
2321#if COAP_SERVER_SUPPORT
2325 const uint8_t *data,
size_t data_len) {
2326 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2327 coap_ssl_data_t *ssl_data;
2332 session->
tls = w_env;
2339 ssl_data = w_env ? &w_env->data : NULL;
2340 assert(ssl_data != NULL);
2345 if (ssl_data->pdu_len) {
2346 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2350 ssl_data->session = session;
2351 ssl_data->pdu = data;
2352 ssl_data->pdu_len = (unsigned)data_len;
2361 coap_ssl_data_t *ssl_data;
2362 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2363 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2365 int in_init = wolfSSL_SSL_in_init(ssl);
2368 assert(ssl != NULL);
2370 ssl_data = &w_env->data;
2372 if (ssl_data->pdu_len) {
2373 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2376 ssl_data->pdu = data;
2377 ssl_data->pdu_len = (unsigned)data_len;
2380 r = wolfSSL_read(ssl, pdu, (
int)
sizeof(pdu));
2387 int err = wolfSSL_get_error(ssl, r);
2388 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2389 if (in_init && wolfSSL_is_init_finished(ssl)) {
2392#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) && COAP_CLIENT_SUPPORT
2395 if (wolfSSL_dtls_cid_is_enabled(ssl)) {
2403 if (!strcmp(wolfSSL_get_version(ssl),
"DTLSv1.3")) {
2412 }
else if (err == APP_DATA_READY) {
2413 r = wolfSSL_read(ssl, pdu, (
int)
sizeof(pdu));
2421 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2426 if (err == FATAL_ERROR) {
2427 WOLFSSL_ALERT_HISTORY h;
2429 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2430 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2433 wolfSSL_alert_desc_string_long(h.last_rx.code));
2454 if (ssl_data && ssl_data->pdu_len) {
2456 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
2457 ssl_data->pdu_len = 0;
2458 ssl_data->pdu = NULL;
2465 unsigned int overhead = 37;
2466 const WOLFSSL_CIPHER *s_ciph = NULL;
2467 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2468 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2471 s_ciph = wolfSSL_get_current_cipher(ssl);
2473 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
2475 const WOLFSSL_EVP_CIPHER *e_ciph;
2476 const WOLFSSL_EVP_MD *e_md;
2479 e_ciph = wolfSSL_EVP_get_cipherbynid(wolfSSL_CIPHER_get_cipher_nid(s_ciph));
2481 switch (WOLFSSL_EVP_CIPHER_mode(e_ciph)) {
2483 case WOLFSSL_EVP_CIPH_GCM_MODE:
2484#ifndef WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN
2485#define WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN 8
2487#ifndef WOLFSSL_EVP_GCM_TLS_TAG_LEN
2488#define WOLFSSL_EVP_GCM_TLS_TAG_LEN 16
2490 ivlen = WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN;
2491 maclen = WOLFSSL_EVP_GCM_TLS_TAG_LEN;
2494 case WOLFSSL_EVP_CIPH_CCM_MODE:
2495#ifndef WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN
2496#define WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN 8
2498 ivlen = WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN;
2499 wolfSSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
2500 if (strstr(cipher,
"CCM8"))
2506 case WOLFSSL_EVP_CIPH_CBC_MODE:
2507 e_md = wolfSSL_EVP_get_digestbynid(wolfSSL_CIPHER_get_digest_nid(s_ciph));
2508 blocksize = wolfSSL_EVP_CIPHER_block_size(e_ciph);
2509 ivlen = wolfSSL_EVP_CIPHER_iv_length(e_ciph);
2511 maclen = wolfSSL_EVP_MD_size(e_md);
2514 case WOLFSSL_EVP_CIPH_STREAM_CIPHER:
2521 wolfSSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
2528#ifndef WOLFSSL_DTLS13_RT_HEADER_LENGTH
2529#define WOLFSSL_DTLS13_RT_HEADER_LENGTH 13
2531 overhead = WOLFSSL_DTLS13_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 +
2537#if !COAP_DISABLE_TCP
2538#if COAP_CLIENT_SUPPORT
2541 WOLFSSL *ssl = NULL;
2543 coap_wolfssl_context_t *w_context =
2545 coap_tls_context_t *tls;
2546 coap_wolfssl_env_t *w_env =
2553 if (!setup_tls_context(w_context))
2555 tls = &w_context->tls;
2557 ssl = wolfSSL_new(tls->ctx);
2560 wolfSSL_SetIOWriteCtx(ssl, w_env);
2561 wolfSSL_SetIOReadCtx(ssl, w_env);
2562 wolfSSL_set_app_data(ssl, session);
2563 w_env->data.session = session;
2565 if (!setup_client_ssl_session(session, ssl))
2568 session->
tls = w_env;
2570 r = wolfSSL_connect(ssl);
2572 int ret = wolfSSL_get_error(ssl, r);
2573 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2575 if (ret == WOLFSSL_ERROR_WANT_READ)
2577 if (ret == WOLFSSL_ERROR_WANT_WRITE) {
2579#ifdef COAP_EPOLL_SUPPORT
2593 w_env->last_timeout = now;
2594 if (wolfSSL_is_init_finished(ssl)) {
2602 coap_dtls_free_wolfssl_env(w_env);
2609#if COAP_SERVER_SUPPORT
2612 WOLFSSL *ssl = NULL;
2613 coap_wolfssl_context_t *w_context =
2615 coap_tls_context_t *tls;
2618 coap_wolfssl_env_t *w_env =
2625 if (!setup_tls_context(w_context))
2627 tls = &w_context->tls;
2629 ssl = wolfSSL_new(tls->ctx);
2632 wolfSSL_SetIOWriteCtx(ssl, w_env);
2633 wolfSSL_SetIOReadCtx(ssl, w_env);
2634 wolfSSL_set_app_data(ssl, session);
2636 wolfSSL_set_cipher_list(ssl,
"ALL");
2638 if (w_context->psk_pki_enabled & IS_PSK) {
2640 if (psk_hint != NULL && psk_hint->
length) {
2641 char *hint = wolfssl_malloc(psk_hint->
length + 1);
2644 memcpy(hint, psk_hint->
s, psk_hint->
length);
2645 hint[psk_hint->
length] =
'\000';
2646 wolfSSL_use_psk_identity_hint(ssl, hint);
2653 if (w_context->psk_pki_enabled & IS_PKI) {
2657#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
2658 if (w_context->setup_data.is_rpk_not_cert) {
2659 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
2661 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
2666 w_env->last_timeout = now;
2668 w_env->data.session = session;
2670 r = wolfSSL_accept(ssl);
2672 int err = wolfSSL_get_error(ssl, r);
2673 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE) {
2676 if (err == WOLFSSL_ERROR_WANT_READ) {
2679 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2681#ifdef COAP_EPOLL_SUPPORT
2694 session->
tls = w_env;
2695 if (wolfSSL_is_init_finished(ssl)) {
2705 coap_dtls_free_wolfssl_env(w_env);
2712 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2713 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2716 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2717 int r = wolfSSL_shutdown(ssl);
2719 wolfSSL_shutdown(ssl);
2726 coap_dtls_free_wolfssl_env(w_env);
2736 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2737 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2743 in_init = !wolfSSL_is_init_finished(ssl);
2745 r = wolfSSL_write(ssl, data, (
int)data_len);
2748 int err = wolfSSL_get_error(ssl, r);
2749 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2750 if (in_init && wolfSSL_is_init_finished(ssl)) {
2756 if (err == WOLFSSL_ERROR_WANT_READ)
2758 else if (err == WOLFSSL_ERROR_WANT_WRITE) {
2760#ifdef COAP_EPOLL_SUPPORT
2772 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2774 else if (err == WOLFSSL_ERROR_SSL)
2778 }
else if (in_init && wolfSSL_is_init_finished(ssl)) {
2797 if (r == (ssize_t)data_len)
2814 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2815 WOLFSSL *ssl = w_env ? w_env->ssl : NULL;
2823 in_init = !wolfSSL_is_init_finished(ssl);
2825 r = wolfSSL_read(ssl, data, (
int)data_len);
2827 int err = wolfSSL_get_error(ssl, r);
2828 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2829 if (in_init && wolfSSL_is_init_finished(ssl)) {
2835 if (err == WOLFSSL_ERROR_WANT_READ)
2837 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2839#ifdef COAP_EPOLL_SUPPORT
2849 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2852 }
else if (err == WOLFSSL_ERROR_SSL) {
2854 }
else if (err == FATAL_ERROR) {
2855 WOLFSSL_ALERT_HISTORY h;
2858 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2859 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2862 wolfSSL_alert_desc_string_long(h.last_rx.code));
2868 }
else if (in_init && wolfSSL_is_init_finished(ssl)) {
2894#if COAP_SERVER_SUPPORT
2897 WOLFSSL_EVP_MD_CTX *digest_ctx = wolfSSL_EVP_MD_CTX_new();
2900 wolfSSL_EVP_DigestInit_ex(digest_ctx, wolfSSL_EVP_sha256(), NULL);
2908 wolfSSL_EVP_MD_CTX_free(digest_ctx);
2913 const uint8_t *data,
2915 return wolfSSL_EVP_DigestUpdate(digest_ctx, data, data_len);
2922 int ret = wolfSSL_EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
2929#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
2931coap_crypto_output_errors(
const char *prefix) {
2932#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
2937 while ((e = wolfSSL_ERR_get_error()))
2940 wolfSSL_ERR_reason_error_string(e),
2941 ssl_function_definition(e));
2951static struct hash_algs {
2953 const WOLFSSL_EVP_MD *(*get_hash)(void);
2962static const WOLFSSL_EVP_MD *
2963get_hash_alg(
cose_alg_t alg,
size_t *length) {
2966 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2967 if (hashs[idx].alg == alg) {
2968 *length = hashs[idx].length;
2969 return hashs[idx].get_hash();
2972 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2980 unsigned int length;
2981 const WOLFSSL_EVP_MD *evp_md;
2982 WOLFSSL_EVP_MD_CTX *evp_ctx = NULL;
2986 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
2987 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2990 evp_ctx = wolfSSL_EVP_MD_CTX_new();
2991 if (evp_ctx == NULL)
2993 if (wolfSSL_EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
2996 if (wolfSSL_EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
3002 if (wolfSSL_EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
3004 dummy->length = length;
3005 if (hash_length < dummy->length)
3006 dummy->length = hash_length;
3008 wolfSSL_EVP_MD_CTX_free(evp_ctx);
3012 coap_crypto_output_errors(
"coap_crypto_hash");
3015 wolfSSL_EVP_MD_CTX_free(evp_ctx);
3020#if COAP_OSCORE_SUPPORT
3021#if LIBWOLFSSL_VERSION_HEX < 0x05006000
3022static const WOLFSSL_EVP_CIPHER *
3023EVP_aes_128_ccm(
void) {
3024 return "AES-128-CCM";
3027static const WOLFSSL_EVP_CIPHER *
3028EVP_aes_256_ccm(
void) {
3029 return "AES-256-CCM";
3043static struct cipher_algs {
3045 const WOLFSSL_EVP_CIPHER *(*get_cipher)(void);
3050static const WOLFSSL_EVP_CIPHER *
3054 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3055 if (ciphers[idx].alg == alg)
3056 return ciphers[idx].get_cipher();
3058 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3067static struct hmac_algs {
3069 const WOLFSSL_EVP_MD *(*get_hmac)(void);
3076static const WOLFSSL_EVP_MD *
3080 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3081 if (hmacs[idx].hmac_alg == hmac_alg)
3082 return hmacs[idx].get_hmac();
3084 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3090 return get_cipher_alg(alg) != NULL;
3099 return get_hmac_alg(hmac_alg) != NULL;
3103 if (1 != (Func)) { \
3112 size_t *max_result_len) {
3118 byte *authTag = NULL;
3124 assert(params != NULL);
3130 if (ccm->
key.
s == NULL || ccm->
nonce == NULL)
3133 result_len = data->
length;
3134 nonce_length = 15 - ccm->
l;
3136 memset(&aes, 0,
sizeof(aes));
3141 authTag = (
byte *)malloc(ccm->
tag_len *
sizeof(
byte));
3145 ret = wc_AesCcmEncrypt(&aes, result, data->
s, data->
length, ccm->
nonce,
3146 nonce_length, authTag, ccm->
tag_len,
3150 wolfssl_free(authTag);
3154 memcpy(result + result_len, authTag, ccm->
tag_len);
3155 result_len +=
sizeof(authTag);
3156 *max_result_len = result_len;
3157 wolfssl_free(authTag);
3161 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
3171 size_t *max_result_len) {
3184 assert(params != NULL);
3194 memcpy(authTag, data->
s + data->
length - ccm->
tag_len,
sizeof(authTag));
3198 if (ccm->
key.
s == NULL || ccm->
nonce == NULL)
3201 memset(&aes, 0,
sizeof(aes));
3208 ret = wc_AesCcmDecrypt(&aes, result, data->
s, len, ccm->
nonce,
3209 15 - ccm->
l, authTag,
sizeof(authTag),
3215 *max_result_len = len;
3219 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
3228 unsigned int result_len;
3229 const WOLFSSL_EVP_MD *evp_md;
3236 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
3237 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3243 result_len = (
unsigned int)
dummy->length;
3244 if (wolfSSL_HMAC(evp_md,
3251 dummy->length = result_len;
3256 coap_crypto_output_errors(
"coap_crypto_hmac");
3268#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
struct coap_session_t coap_session_t
#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.
struct coap_dtls_pki_t coap_dtls_pki_t
@ 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::@347070033161156116347020364174372227171250157130 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
coap_pki_key_t key_type
key format type
union coap_dtls_key_t::@113076155176127235351156302031377057233373343157 key
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