21#if 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#if DTLS_CID_MAX_SIZE == 0
85#undef WOLFSSL_DTLS_CID
88#ifdef COAP_EPOLL_SUPPORT
89# include <sys/epoll.h>
92#if LIBWOLFSSL_VERSION_HEX < 0x05002000
93#error Must be compiled against wolfSSL 5.2.0 or later
97#define strcasecmp _stricmp
98#define strncasecmp _strnicmp
102#define WOLFSSL3_AL_FATAL 2
103#define WOLFSSL_TLSEXT_ERR_OK 0
106typedef struct coap_dtls_context_t {
108 WOLFSSL_HMAC_CTX *cookie_hmac;
109} coap_dtls_context_t;
111typedef struct coap_tls_context_t {
118typedef struct coap_wolfssl_context_t {
119 coap_dtls_context_t dtls;
121 coap_tls_context_t tls;
127 int trust_store_defined;
128} coap_wolfssl_context_t;
130typedef struct coap_ssl_data_t {
137typedef struct coap_wolfssl_env_t {
140 unsigned int retry_scalar;
141 coap_ssl_data_t data;
146typedef enum coap_enc_method_t {
152wolfssl_malloc(
size_t size) {
153 void *ret = XMALLOC(size,
NULL, DYNAMIC_TYPE_TMP_BUFFER);
159wolfssl_free(
void *ptr) {
161 XFREE(ptr,
NULL, DYNAMIC_TYPE_TMP_BUFFER);
165wolfssl_strdup(
const char *str) {
166 char *ret = (
char *)wolfssl_malloc(strlen(str) + 1);
174static coap_wolfssl_env_t *
176 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)c_session->
tls;
181 w_env = (coap_wolfssl_env_t *)wolfssl_malloc(
sizeof(coap_wolfssl_env_t));
185 memset(w_env, 0,
sizeof(coap_wolfssl_env_t));
191coap_dtls_free_wolfssl_env(coap_wolfssl_env_t *w_env) {
197#if COAP_CLIENT_SUPPORT
198#ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE
199#define WOLFSSL_CIPHER_LIST_MAX_SIZE 4096
202#ifdef COAP_WOLFSSL_PSK_CIPHERS
203static char psk_ciphers[] = COAP_WOLFSSL_PSK_CIPHERS;
205static char psk_ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
208#ifdef COAP_WOLFSSL_PKI_CIPHERS
209static char pki_ciphers[] = COAP_WOLFSSL_PKI_CIPHERS;
211static char pki_ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
215set_ciphersuites(WOLFSSL *ssl, coap_enc_method_t method) {
216#if ! defined(COAP_WOLFSSL_PSK_CIPHERS) || ! defined(COAP_WOLFSSL_PKI_CIPHERS)
217 static int processed_ciphers = 0;
219 if (!processed_ciphers) {
220 static char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
221 char *ciphers_ofs = ciphers;
223#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
224 char *psk_ofs = psk_ciphers;
226#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
227 char *pki_ofs = pki_ciphers;
230 if (wolfSSL_get_ciphers(ciphers, (
int)
sizeof(ciphers)) != WOLFSSL_SUCCESS) {
235 while (ciphers_ofs) {
236 cp = strchr(ciphers_ofs,
':');
239 if (strstr(ciphers_ofs,
"NULL")) {
243 if (strcmp(ciphers_ofs,
"RENEGOTIATION-INFO") == 0) {
246 }
else if (strstr(ciphers_ofs,
"PSK")) {
247#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
248 if (psk_ofs != psk_ciphers) {
252 strcpy(psk_ofs, ciphers_ofs);
253 psk_ofs += strlen(ciphers_ofs);
257#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
258 if (pki_ofs != pki_ciphers) {
262 strcpy(pki_ofs, ciphers_ofs);
263 pki_ofs += strlen(ciphers_ofs);
269 ciphers_ofs = cp + 1;
273#ifndef HAVE_SECURE_RENEGOTIATION
279#if ! defined(COAP_WOLFSSL_PSK_CIPHERS)
280 if (psk_ofs != psk_ciphers) {
284 strcpy(psk_ofs,
"RENEGOTIATION-INFO");
285 psk_ofs += strlen(
"RENEGOTIATION-INFO");
288#if ! defined(COAP_WOLFSSL_PKI_CIPHERS)
289 if (pki_ofs != pki_ciphers) {
293 strcpy(pki_ofs,
"RENEGOTIATION-INFO");
294 pki_ofs += strlen(
"RENEGOTIATION-INFO");
299 processed_ciphers = 1;
303 if (method == COAP_ENC_PSK) {
304 wolfSSL_set_cipher_list(ssl, psk_ciphers);
306 wolfSSL_set_cipher_list(ssl, pki_ciphers);
311#if COAP_SERVER_SUPPORT
312static int psk_tls_server_name_call_back(WOLFSSL *ssl,
int *sd,
void *arg);
314static int tls_verify_call_back(
int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx);
318 if (wolfSSL_lib_version_hex() < 0x05002000) {
319 coap_log_warn(
"wolfSSL version 5.2.0 or later is required\n");
328 if (wolfSSL_lib_version_hex() < 0x05002000) {
329 coap_log_warn(
"wolfSSL version 5.2.0 or later is required\n");
380#if defined(WOLFSSL_DTLS_CID)
387#if COAP_CLIENT_SUPPORT
390#if defined(WOLFSSL_DTLS_CID)
391 c_context->testing_cids = every;
404 version.
version = wolfSSL_lib_version_hex();
412coap_wolfssl_log_func(
int level,
const char *text) {
415 switch ((
int)level) {
440 if (wolfSSL_library_init() != WOLFSSL_SUCCESS) {
444 wolfSSL_load_error_strings();
445 wolfSSL_SetLoggingCb(coap_wolfssl_log_func);
446 wolfSSL_Debugging_ON();
451 wolfSSL_ERR_free_strings();
453 wolfSSL_Debugging_OFF();
474 coap_wolfssl_env_t *w_env;
477 memcpy(&w_env, &c_session->
tls,
sizeof(w_env));
479 return (
void *)&w_env->ssl;
500coap_dgram_read(WOLFSSL *ssl,
char *out,
int outl,
void *ctx) {
502 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
503 coap_ssl_data_t *data = w_env ? &w_env->data :
NULL;
507 if (w_env && !w_env->done_psk_check && w_env->ssl) {
508 if (wolfSSL_SSL_in_init(w_env->ssl)) {
509 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
515 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
516 w_env->done_psk_check = 1;
522 if (data !=
NULL && data->pdu_len > 0) {
523 if (outl < (
int)data->pdu_len) {
524 memcpy(out, data->pdu, outl);
527 memcpy(out, data->pdu, data->pdu_len);
528 ret = (int)data->pdu_len;
530 if (!data->peekmode) {
535 w_env->last_timeout = now;
544coap_dgram_write(WOLFSSL *ssl,
char *in,
int inl,
void *ctx) {
546 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
547 coap_ssl_data_t *data = w_env ? &w_env->data :
NULL;
551 if (data && data->session) {
554 && data->session->endpoint ==
NULL
561 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
566 w_env->last_timeout = now;
567 }
else if (ret < 0) {
568 if (errno == ENOTCONN || errno == ECONNREFUSED)
577#if COAP_CLIENT_SUPPORT
579coap_dtls_psk_client_callback(WOLFSSL *ssl,
582 unsigned int max_identity_len,
584 unsigned int max_psk_len) {
586 coap_wolfssl_context_t *w_context;
596 if (w_context ==
NULL)
604 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
605 temp.
length = strlen((
const char *)temp.
s);
609 (
const char *)temp.
s);
619 if (cpsk_info ==
NULL)
624 psk_identity = &cpsk_info->
identity;
625 psk_key = &cpsk_info->
key;
631 if (psk_identity ==
NULL || psk_key ==
NULL) {
637 if (!max_identity_len)
640 if (psk_identity->
length > max_identity_len) {
641 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
645 max_identity_len = (
unsigned int)psk_identity->
length;
647 memcpy(identity, psk_identity->
s, max_identity_len);
648 identity[max_identity_len] =
'\000';
650 if (psk_key->
length > max_psk_len) {
655 max_psk_len = (
unsigned int)psk_key->
length;
657 memcpy(psk, psk_key->
s, max_psk_len);
664coap_dtls_psk_client_cs_callback(WOLFSSL *ssl,
const char *hint,
665 char *identity,
unsigned int max_identity_len,
666 unsigned char *psk,
unsigned int max_psk_len,
667 const char *ciphersuite) {
668 int key_len = coap_dtls_psk_client_callback(ssl,
680coap_tls_psk_client_cs_callback(WOLFSSL *ssl,
const char *hint,
681 char *identity,
unsigned int max_identity_len,
682 unsigned char *psk,
unsigned int max_psk_len,
683 const char **ciphersuite) {
684 int key_len = coap_dtls_psk_client_callback(ssl,
691 *ciphersuite =
"TLS13-AES128-GCM-SHA256";
698#if COAP_SERVER_SUPPORT
700coap_dtls_psk_server_callback(
702 const char *identity,
704 unsigned int max_psk_len) {
714 setup_data = &c_session->
context->spsk_setup_data;
717 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
718 lidentity.
length = strlen((
const char *)lidentity.
s);
722 (
int)lidentity.
length, (
const char *)lidentity.
s);
737 if (psk_key->
length > max_psk_len) {
742 max_psk_len = (
unsigned int)psk_key->
length;
744 memcpy(psk, psk_key->
s, max_psk_len);
750ssl_function_definition(
unsigned long e) {
751 static char buff[80];
753 snprintf(buff,
sizeof(buff),
" at %s:%s",
754 wolfSSL_ERR_lib_error_string(e), wolfSSL_ERR_func_error_string(e));
759coap_dtls_info_callback(
const WOLFSSL *ssl,
int where,
int ret) {
762 int w = where &~SSL_ST_MASK;
766 "coap_dtls_info_callback: session not determined, where 0x%0x and ret 0x%0x\n", where, ret);
770 if (w & SSL_ST_CONNECT)
771 pstr =
"wolfSSL_connect";
772 else if (w & SSL_ST_ACCEPT)
773 pstr =
"wolfSSL_accept";
777 if (where & SSL_CB_LOOP) {
780 }
else if (where & SSL_CB_ALERT) {
782 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
783 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == WOLFSSL3_AL_FATAL) {
785 if ((ret & 0xff) != close_notify)
790 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
793 wolfSSL_alert_type_string_long(ret),
794 wolfSSL_alert_desc_string_long(ret));
795 }
else if (where & SSL_CB_EXIT) {
801 while ((e = wolfSSL_ERR_get_error()))
804 ssl_function_definition(e));
806 }
else if (ret < 0) {
811 memcpy(&rw_ssl, &ssl,
sizeof(rw_ssl));
812 int err = wolfSSL_get_error(rw_ssl, ret);
813 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE &&
814 err != WOLFSSL_ERROR_WANT_CONNECT && err != WOLFSSL_ERROR_WANT_ACCEPT &&
815 err != WOLFSSL_ERROR_WANT_X509_LOOKUP) {
819 while ((e = wolfSSL_ERR_get_error()))
822 ssl_function_definition(e));
828 if (where == SSL_CB_HANDSHAKE_START) {
832 memcpy(&rw_ssl, &ssl,
sizeof(rw_ssl));
833 if (wolfSSL_is_init_finished(rw_ssl))
846coap_sock_read(WOLFSSL *ssl,
char *out,
int outl,
void *ctx) {
847 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
852 if (w_env && !w_env->done_psk_check && w_env->ssl &&
854 if (wolfSSL_SSL_in_init(w_env->ssl)) {
855 const char *name = wolfSSL_get_cipher_name(w_env->ssl);
860 if (strstr(name,
"PSK")) {
861 wolfSSL_set_verify(w_env->ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
862 w_env->done_psk_check = 1;
867 if (session && out !=
NULL) {
884coap_sock_write(WOLFSSL *ssl,
char *in,
int inl,
void *ctx) {
885 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)ctx;
902 if (ret == -1 && session) {
905 (errno == EPIPE || errno == ECONNRESET)) {
926coap_set_user_prefs(WOLFSSL_CTX *ctx) {
929#ifdef COAP_WOLFSSL_SIGALGS
930 wolfSSL_CTX_set1_sigalgs_list(ctx, COAP_WOLFSSL_SIGALGS);
932#ifdef COAP_WOLFSSL_GROUPS
934 ret = wolfSSL_CTX_set1_groups_list(ctx,
935 (
char *) COAP_WOLFSSL_GROUPS);
936 if (ret != WOLFSSL_SUCCESS) {
944setup_dtls_context(coap_wolfssl_context_t *w_context) {
945 if (!w_context->dtls.ctx) {
946 uint8_t cookie_secret[32];
949 w_context->dtls.ctx = wolfSSL_CTX_new(wolfDTLS_method());
950 if (!w_context->dtls.ctx)
952 wolfSSL_CTX_set_min_proto_version(w_context->dtls.ctx,
954 wolfSSL_CTX_set_ex_data(w_context->dtls.ctx, 0, &w_context->dtls);
955 coap_set_user_prefs(w_context->dtls.ctx);
956 memset(cookie_secret, 0,
sizeof(cookie_secret));
957 if (!wolfSSL_RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
959 "Insufficient entropy for random cookie generation");
962 w_context->dtls.cookie_hmac = wolfSSL_HMAC_CTX_new();
963 if (!wolfSSL_HMAC_Init_ex(w_context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
964 wolfSSL_EVP_sha256(),
NULL))
967 wolfSSL_CTX_set_info_callback(w_context->dtls.ctx, coap_dtls_info_callback);
968 wolfSSL_CTX_set_options(w_context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
969 wolfSSL_SetIORecv(w_context->dtls.ctx, coap_dgram_read);
970 wolfSSL_SetIOSend(w_context->dtls.ctx, coap_dgram_write);
971#ifdef WOLFSSL_DTLS_MTU
974#ifdef WOLFSSL_SYS_CA_CERTS
975 if (w_context->trust_store_defined) {
976 if (!wolfSSL_CTX_load_system_CA_certs(w_context->dtls.ctx)) {
982 if (w_context->root_ca_file || w_context->root_ca_dir) {
983 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->dtls.ctx,
984 w_context->root_ca_file,
985 w_context->root_ca_dir,
986 w_context->setup_data.allow_expired_certs ?
987 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
989 w_context->root_ca_file ? w_context->root_ca_file :
"NULL",
990 w_context->root_ca_dir ? w_context->root_ca_dir :
"NULL");
995 if (w_context->setup_data.verify_peer_cert)
996 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
997 WOLFSSL_VERIFY_PEER |
998 WOLFSSL_VERIFY_CLIENT_ONCE |
999 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1000 tls_verify_call_back);
1002 wolfSSL_CTX_set_verify(w_context->dtls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1011#if !COAP_DISABLE_TCP
1015setup_tls_context(coap_wolfssl_context_t *w_context) {
1016 if (!w_context->tls.ctx) {
1018 w_context->tls.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
1019 if (!w_context->tls.ctx)
1021 wolfSSL_CTX_set_ex_data(w_context->tls.ctx, 0, &w_context->tls);
1022 wolfSSL_CTX_set_min_proto_version(w_context->tls.ctx, TLS1_VERSION);
1023 coap_set_user_prefs(w_context->tls.ctx);
1024 wolfSSL_CTX_set_info_callback(w_context->tls.ctx, coap_dtls_info_callback);
1025 wolfSSL_SetIORecv(w_context->tls.ctx, coap_sock_read);
1026 wolfSSL_SetIOSend(w_context->tls.ctx, coap_sock_write);
1027#if COAP_CLIENT_SUPPORT
1028 if (w_context->psk_pki_enabled & IS_PSK) {
1030 wolfSSL_CTX_set_psk_client_tls13_callback(w_context->tls.ctx,
1031 coap_tls_psk_client_cs_callback);
1033 wolfSSL_CTX_set_psk_client_cs_callback(w_context->tls.ctx,
1034 coap_dtls_psk_client_cs_callback);
1038 if (w_context->root_ca_file || w_context->root_ca_dir) {
1039 if (!wolfSSL_CTX_load_verify_locations_ex(w_context->tls.ctx,
1040 w_context->root_ca_file,
1041 w_context->root_ca_dir,
1042 w_context->setup_data.allow_expired_certs ?
1043 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1045 w_context->root_ca_file ? w_context->root_ca_file :
"NULL",
1046 w_context->root_ca_dir ? w_context->root_ca_dir :
"NULL");
1051#ifdef WOLFSSL_SYS_CA_CERTS
1052 if (w_context->trust_store_defined) {
1053 if (!wolfSSL_CTX_load_system_CA_certs(w_context->tls.ctx)) {
1059 if (w_context->setup_data.verify_peer_cert)
1060 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1061 WOLFSSL_VERIFY_PEER |
1062 WOLFSSL_VERIFY_CLIENT_ONCE |
1063 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1064 tls_verify_call_back);
1066 wolfSSL_CTX_set_verify(w_context->tls.ctx, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1078 coap_wolfssl_context_t *w_context;
1081 w_context = (coap_wolfssl_context_t *)wolfssl_malloc(
sizeof(coap_wolfssl_context_t));
1083 memset(w_context, 0,
sizeof(coap_wolfssl_context_t));
1089#if COAP_SERVER_SUPPORT
1094 coap_wolfssl_context_t *w_context =
1097 if (!setup_data || !w_context)
1100 if (!setup_dtls_context(w_context))
1102#if !COAP_DISABLE_TCP
1103 if (!setup_tls_context(w_context))
1107 wolfSSL_CTX_set_psk_server_callback(w_context->dtls.ctx,
1108 coap_dtls_psk_server_callback);
1110#if !COAP_DISABLE_TCP
1111 wolfSSL_CTX_set_psk_server_callback(w_context->tls.ctx,
1112 coap_dtls_psk_server_callback);
1118 wolfSSL_CTX_use_psk_identity_hint(w_context->dtls.ctx, hint);
1119 wolfSSL_CTX_set_max_proto_version(w_context->dtls.ctx,
1121#if !COAP_DISABLE_TCP
1122 wolfSSL_CTX_use_psk_identity_hint(w_context->tls.ctx, hint);
1123 wolfSSL_CTX_set_max_proto_version(w_context->tls.ctx,
1128 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1129 &c_context->spsk_setup_data);
1130 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1131 psk_tls_server_name_call_back);
1132#if !COAP_DISABLE_TCP
1133 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1134 &c_context->spsk_setup_data);
1135 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1136 psk_tls_server_name_call_back);
1142 w_context->psk_pki_enabled |= IS_PSK;
1147#if COAP_CLIENT_SUPPORT
1152 coap_wolfssl_context_t *w_context =
1155 if (!setup_data || !w_context)
1162#if ! defined(WOLFSSL_DTLS_CID)
1166 w_context->psk_pki_enabled |= IS_PSK;
1171#if !COAP_DISABLE_TCP
1172static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1174#if COAP_SERVER_SUPPORT
1177 const unsigned char **out,
1178 unsigned char *outlen,
1179 const unsigned char *in,
1183 unsigned char *tout =
NULL;
1186 return SSL_TLSEXT_ERR_NOACK;
1187 ret = wolfSSL_select_next_proto(&tout,
1194 return (ret != OPENSSL_NPN_NEGOTIATED) ? noack_return : WOLFSSL_TLSEXT_ERR_OK;
1200setup_pki_ssl(WOLFSSL *ssl,
1203 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
1217 if (!(wolfSSL_use_PrivateKey_file(ssl,
1219 WOLFSSL_FILETYPE_PEM))) {
1226 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1229 WOLFSSL_FILETYPE_PEM))) {
1236#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1237 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1240 WOLFSSL_FILETYPE_PEM))) {
1252 if (!(wolfSSL_use_PrivateKey_file(ssl,
1254 WOLFSSL_FILETYPE_ASN1))) {
1261 if (!(wolfSSL_use_PrivateKey_buffer(ssl,
1264 WOLFSSL_FILETYPE_ASN1))) {
1293 if (!(wolfSSL_use_certificate_chain_file(ssl,
1301 if (!(wolfSSL_use_certificate_chain_buffer(ssl,
1310#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1312 unsigned char der_buff[512];
1314 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1315 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1317 wolfSSL_set_client_cert_type(ssl, ctype,
sizeof(ctype)/
sizeof(ctype[0]));
1318 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
1322 der_buff, (
int)
sizeof(der_buff));
1326 der_buff, (
int)
sizeof(der_buff),
NULL);
1335 if (!wolfSSL_use_PrivateKey_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1340 if (!wolfSSL_use_certificate_buffer(ssl, spki->
s, spki->
length, WOLFSSL_FILETYPE_ASN1)) {
1355 if (!wolfSSL_use_certificate_buffer(ssl, der_buff, ret, WOLFSSL_FILETYPE_ASN1)) {
1368 if (!(wolfSSL_use_certificate_file(ssl,
1370 WOLFSSL_FILETYPE_ASN1))) {
1377 if (!(wolfSSL_use_certificate_buffer(ssl,
1380 WOLFSSL_FILETYPE_ASN1))) {
1401#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1403 char stype[] = {WOLFSSL_CERT_TYPE_X509, WOLFSSL_CERT_TYPE_RPK};
1404 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
1415 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1419 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1426 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1432 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1442 if (!wolfSSL_CTX_load_verify_locations_ex(ctx,
1446 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1453 if (!wolfSSL_CTX_load_verify_buffer_ex(ctx,
1459 WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) {
1478get_san_or_cn_from_cert(
coap_session_t *session, WOLFSSL_X509 *x509,
const char *sni_match) {
1482 WOLF_STACK_OF(WOLFSSL_GENERAL_NAME) *san_list;
1487 san_list = wolfSSL_X509_get_ext_d2i(x509, NID_subject_alt_name,
NULL,
NULL);
1489 int san_count = wolfSSL_sk_GENERAL_NAME_num(san_list);
1491 for (n = 0; n < san_count; n++) {
1492 const WOLFSSL_GENERAL_NAME *name = wolfSSL_sk_GENERAL_NAME_value(san_list, n);
1494 if (name && name->type == GEN_DNS) {
1495 const char *dns_name = (
const char *)wolfSSL_ASN1_STRING_get0_data(name->d.dNSName);
1498 if (wolfSSL_ASN1_STRING_length(name->d.dNSName) != (
int)strlen(dns_name))
1501 if (strcmp(dns_name, sni_match)) {
1505 cn = wolfssl_strdup(dns_name);
1506 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1510 wolfSSL_sk_GENERAL_NAME_pop_free(san_list, wolfSSL_GENERAL_NAME_free);
1513 wolfSSL_X509_NAME_oneline(wolfSSL_X509_get_subject_name((WOLFSSL_X509 *)(x509)), buffer,
1517 n = (int)strlen(buffer) - 3;
1520 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
1521 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
1530 char *ecn = strchr(cn,
'/');
1532 cn[ecn-cn] =
'\000';
1541 return wolfssl_strdup(cn);
1547 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name,
NULL,
NULL);
1549 int san_count = sk_GENERAL_NAME_num(san_list);
1551 for (n = 0; n < san_count; n++) {
1552 const GENERAL_NAME *name = sk_GENERAL_NAME_value(san_list, n);
1554 if (name && name->type == GEN_DNS) {
1555 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
1558 if (ASN1_STRING_length(name->d.dNSName) != (
int)strlen(dns_name))
1564 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1571tls_verify_call_back(
int preverify_ok, WOLFSSL_X509_STORE_CTX *ctx) {
1572 int index = wolfSSL_get_ex_data_X509_STORE_CTX_idx();
1573 WOLFSSL *ssl = index >= 0 ? wolfSSL_X509_STORE_CTX_get_ex_data(ctx, index) :
NULL;
1575 coap_wolfssl_context_t *w_context = (session && session->
context) ?
1578 int depth = wolfSSL_X509_STORE_CTX_get_error_depth(ctx);
1579 int err = wolfSSL_X509_STORE_CTX_get_error(ctx);
1580 WOLFSSL_X509 *x509 = wolfSSL_X509_STORE_CTX_get_current_cert(ctx);
1583 if (!setup_data || !x509) {
1584 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
1589 cn = wolfssl_strdup(
"RPK");
1593 cn = get_san_or_cn_from_cert(session, x509, setup_data->
client_sni);
1596 coap_log_info(
" %s: SNI '%s' not returned in certificate\n",
1599 X509_STORE_CTX_set_error(ctx, X509_V_ERR_SUBJECT_ISSUER_MISMATCH);
1600 err = X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
1604 cn = get_san_or_cn_from_cert(session, x509,
NULL);
1607 depth, err, preverify_ok, cn ? cn :
"");
1610 int length = wolfSSL_i2d_X509(x509,
NULL);
1614 uint8_t *base_buf2 = base_buf = wolfssl_malloc(length);
1619 wolfSSL_i2d_X509(x509, &base_buf2);
1622 depth, preverify_ok,
1626 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1628 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1632 wolfssl_free(base_buf);
1634 wolfSSL_X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
1639 if (!preverify_ok) {
1641 case X509_V_ERR_CERT_NOT_YET_VALID:
1642 case X509_V_ERR_CERT_HAS_EXPIRED:
1643 case ASN_NO_SIGNER_E:
1644 case ASN_AFTER_DATE_E:
1648 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1652 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1656 case X509_V_ERR_UNABLE_TO_GET_CRL:
1660 case X509_V_ERR_CRL_NOT_YET_VALID:
1661 case X509_V_ERR_CRL_HAS_EXPIRED:
1665 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1666 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1667 case X509_V_ERR_AKID_SKID_MISMATCH:
1671 case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
1681 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1682 wolfSSL_X509_STORE_CTX_set_error(ctx, err);
1684 if (!preverify_ok) {
1685 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1688 "Unknown CA", cn ? cn :
"?", depth);
1692 wolfSSL_X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1697 wolfSSL_X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1701 return preverify_ok;
1704#if COAP_SERVER_SUPPORT
1715tls_server_name_call_back(WOLFSSL *ssl,
1720 coap_wolfssl_context_t *w_context = (session && session->
context) ?
1724 return noack_return;
1729 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1733 if (!sni || !sni[0]) {
1740 return fatal_return;
1742 sni_setup_data = *setup_data;
1743 sni_setup_data.
pki_key = *new_entry;
1747 if (w_context->psk_pki_enabled & IS_PSK) {
1748 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1750 return SSL_TLSEXT_ERR_OK;
1761psk_tls_server_name_call_back(WOLFSSL *ssl,
1767 coap_wolfssl_context_t *w_context = (c_session && c_session->
context) ?
1771 return noack_return;
1776 const char *sni = wolfSSL_get_servername(ssl, WOLFSSL_SNI_HOST_NAME);
1780 if (!sni || !sni[0]) {
1789 snprintf(lhint,
sizeof(lhint),
"%.*s",
1792 wolfSSL_use_psk_identity_hint(ssl, lhint);
1796 if (w_context->psk_pki_enabled & IS_PSK) {
1797 wolfSSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1799 return SSL_TLSEXT_ERR_OK;
1807 coap_wolfssl_context_t *w_context =
1813 w_context->setup_data = *setup_data;
1814 if (!w_context->setup_data.verify_peer_cert) {
1816 w_context->setup_data.check_common_ca = 0;
1817 if (w_context->setup_data.is_rpk_not_cert) {
1819 w_context->setup_data.allow_self_signed = 0;
1820 w_context->setup_data.allow_expired_certs = 0;
1821 w_context->setup_data.cert_chain_validation = 0;
1822 w_context->setup_data.cert_chain_verify_depth = 0;
1823 w_context->setup_data.check_cert_revocation = 0;
1824 w_context->setup_data.allow_no_crl = 0;
1825 w_context->setup_data.allow_expired_crl = 0;
1826 w_context->setup_data.allow_bad_md_hash = 0;
1827 w_context->setup_data.allow_short_rsa_length = 0;
1830 w_context->setup_data.allow_self_signed = 1;
1831 w_context->setup_data.allow_expired_certs = 1;
1832 w_context->setup_data.cert_chain_validation = 1;
1833 w_context->setup_data.cert_chain_verify_depth = 10;
1834 w_context->setup_data.check_cert_revocation = 1;
1835 w_context->setup_data.allow_no_crl = 1;
1836 w_context->setup_data.allow_expired_crl = 1;
1837 w_context->setup_data.allow_bad_md_hash = 1;
1838 w_context->setup_data.allow_short_rsa_length = 1;
1841#if COAP_SERVER_SUPPORT
1843 if (!setup_dtls_context(w_context))
1845 if (w_context->dtls.ctx) {
1846#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1847 char ctype[] = {WOLFSSL_CERT_TYPE_RPK};
1848 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
1851 wolfSSL_CTX_set_servername_arg(w_context->dtls.ctx,
1852 &w_context->setup_data);
1853 wolfSSL_CTX_set_tlsext_servername_callback(w_context->dtls.ctx,
1854 tls_server_name_call_back);
1856#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
1857 if (w_context->setup_data.is_rpk_not_cert) {
1858 wolfSSL_CTX_set_client_cert_type(w_context->dtls.ctx, ctype,
sizeof(ctype)/
sizeof(ctype[0]));
1859 wolfSSL_CTX_set_server_cert_type(w_context->dtls.ctx, stype,
sizeof(stype)/
sizeof(stype[0]));
1863#if !COAP_DISABLE_TCP
1864 if (!setup_tls_context(w_context))
1866 if (w_context->tls.ctx) {
1867 wolfSSL_CTX_set_servername_arg(w_context->tls.ctx,
1868 &w_context->setup_data);
1869 wolfSSL_CTX_set_tlsext_servername_callback(w_context->tls.ctx,
1870 tls_server_name_call_back);
1873 wolfSSL_CTX_set_alpn_select_cb(w_context->tls.ctx,
1874 server_alpn_callback,
NULL);
1878 if (w_context->setup_data.check_cert_revocation) {
1879 WOLFSSL_X509_VERIFY_PARAM *param;
1881 param = wolfSSL_X509_VERIFY_PARAM_new();
1882 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
1883 wolfSSL_CTX_set1_param(w_context->dtls.ctx, param);
1884#if !COAP_DISABLE_TCP
1885 wolfSSL_CTX_set1_param(w_context->tls.ctx, param);
1887 wolfSSL_X509_VERIFY_PARAM_free(param);
1890 if (w_context->setup_data.verify_peer_cert) {
1891 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1892 WOLFSSL_VERIFY_PEER |
1893 WOLFSSL_VERIFY_CLIENT_ONCE |
1894 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1895 tls_verify_call_back);
1896#if !COAP_DISABLE_TCP
1897 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1898 WOLFSSL_VERIFY_PEER |
1899 WOLFSSL_VERIFY_CLIENT_ONCE |
1900 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1901 tls_verify_call_back);
1904 wolfSSL_CTX_set_verify(w_context->dtls.ctx,
1905 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1906#if !COAP_DISABLE_TCP
1907 wolfSSL_CTX_set_verify(w_context->tls.ctx,
1908 WOLFSSL_VERIFY_NONE, tls_verify_call_back);
1913 if (w_context->setup_data.cert_chain_validation) {
1914 wolfSSL_CTX_set_verify_depth(w_context->dtls.ctx,
1916#if !COAP_DISABLE_TCP
1917 wolfSSL_CTX_set_verify_depth(w_context->tls.ctx,
1926 w_context->psk_pki_enabled |= IS_PKI;
1928#if ! defined(WOLFSSL_DTLS_CID)
1937 const char *ca_file,
1938 const char *ca_dir) {
1939 coap_wolfssl_context_t *w_context =
1943 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
1947 if (ca_file ==
NULL && ca_dir ==
NULL) {
1948 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_dir "
1952 if (w_context->root_ca_file) {
1953 wolfssl_free(w_context->root_ca_file);
1954 w_context->root_ca_file =
NULL;
1957 w_context->root_ca_file = wolfssl_strdup(ca_file);
1959 if (w_context->root_ca_dir) {
1960 wolfssl_free(w_context->root_ca_dir);
1961 w_context->root_ca_dir =
NULL;
1964 w_context->root_ca_dir = wolfssl_strdup(ca_dir);
1971 coap_wolfssl_context_t *w_context =
1975 coap_log_warn(
"coap_context_set_pki_trust_store: (D)TLS environment "
1979#ifdef WOLFSSL_SYS_CA_CERTS
1980 w_context->trust_store_defined = 1;
1983 coap_log_warn(
"coap_context_set_pki_trust_store: (D)TLS environment "
1984 "not supported for wolfSSL < v5.5.2 or –enable-sys-ca-certs not defined\n");
1991 coap_wolfssl_context_t *w_context =
1993 return w_context->psk_pki_enabled ? 1 : 0;
1998 coap_wolfssl_context_t *w_context = (coap_wolfssl_context_t *)handle;
2002 wolfssl_free(w_context->root_ca_file);
2003 wolfssl_free(w_context->root_ca_dir);
2005 if (w_context->dtls.ctx)
2006 wolfSSL_CTX_free(w_context->dtls.ctx);
2007 if (w_context->dtls.cookie_hmac)
2008 wolfSSL_HMAC_CTX_free(w_context->dtls.cookie_hmac);
2010#if !COAP_DISABLE_TCP
2011 if (w_context->tls.ctx)
2012 wolfSSL_CTX_free(w_context->tls.ctx);
2014 wolfssl_free(w_context);
2017#if COAP_SERVER_SUPPORT
2020 coap_wolfssl_context_t *w_context = session && session->
context ?
2022 coap_dtls_context_t *dtls;
2023 WOLFSSL *ssl =
NULL;
2026 coap_wolfssl_env_t *w_env = session ? (coap_wolfssl_env_t *)session->
tls :
NULL;
2029 if (!w_env || !w_context)
2032 if (!setup_dtls_context(w_context))
2034 dtls = &w_context->dtls;
2036 ssl = wolfSSL_new(dtls->ctx);
2040 wolfSSL_set_app_data(ssl,
NULL);
2041 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2042#ifdef WOLFSSL_DTLS_MTU
2043 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2046 wolfSSL_SetIOWriteCtx(ssl, w_env);
2047 wolfSSL_SetIOReadCtx(ssl, w_env);
2048 wolfSSL_set_app_data(ssl, session);
2049 w_env->data.session = session;
2051#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
2052 if (wolfSSL_send_hrr_cookie(ssl,
NULL, 0) != WOLFSSL_SUCCESS)
2053 coap_log_debug(
"Error: Unable to set cookie with Hello Retry Request\n");
2056#ifdef HAVE_SERVER_RENEGOTIATION_INFO
2057 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
2058 coap_log_debug(
"Error: wolfSSL_UseSecureRenegotiation failed\n");
2062 if (w_context->psk_pki_enabled & IS_PSK) {
2066 char *hint = wolfssl_malloc(psk_hint->
length + 1);
2069 memcpy(hint, psk_hint->
s, psk_hint->
length);
2070 hint[psk_hint->
length] =
'\000';
2071 wolfSSL_use_psk_identity_hint(ssl, hint);
2078 if (w_context->psk_pki_enabled & IS_PKI) {
2083#if defined(WOLFSSL_DTLS_CH_FRAG) && defined(WOLFSSL_DTLS13)
2084 if (wolfSSL_dtls13_allow_ch_frag(ssl, 1) != WOLFSSL_SUCCESS) {
2089#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2091#if COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
2092#error COAP_DTLS_CID_LENGTH > DTLS_CID_MAX_SIZE
2095 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2102 if (wolfSSL_dtls_cid_set(ssl, cid,
sizeof(cid)) != WOLFSSL_SUCCESS)
2108 w_env->last_timeout = now;
2111 r = wolfSSL_accept(ssl);
2113 int err = wolfSSL_get_error(ssl, r);
2114 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE)
2127 coap_dtls_free_wolfssl_env(w_env);
2134#if COAP_CLIENT_SUPPORT
2137 coap_wolfssl_context_t *w_context =
2140 if (w_context->psk_pki_enabled & IS_PSK) {
2145 wolfSSL_set_max_proto_version(ssl,
2148#if !COAP_DISABLE_TCP
2150 wolfSSL_set_max_proto_version(ssl,
2152 wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_3);
2155 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2156 set_ciphersuites(ssl, COAP_ENC_PSK);
2158 set_ciphersuites(ssl, COAP_ENC_PSK);
2163 wolfSSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2164 coap_log_warn(
"wolfSSL_set_tlsext_host_name: set '%s' failed",
2167 wolfSSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2169#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2171 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2176 if (wolfSSL_dtls_cid_set(ssl,
NULL, 0) != WOLFSSL_SUCCESS)
2181 if ((w_context->psk_pki_enabled & IS_PKI) ||
2182 (w_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
2189 if (!(w_context->psk_pki_enabled & IS_PKI)) {
2203 set_ciphersuites(ssl, COAP_ENC_PKI);
2207#if !COAP_DISABLE_TCP
2209 wolfSSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
2214 wolfSSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2215 coap_log_warn(
"wolfSSL_set_tlsext_host_name: set '%s' failed",
2220 WOLFSSL_X509_VERIFY_PARAM *param;
2222 param = wolfSSL_X509_VERIFY_PARAM_new();
2223 wolfSSL_X509_VERIFY_PARAM_set_flags(param, WOLFSSL_CRL_CHECK);
2224 WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
2226 wolfSSL_CTX_set1_param(ctx, param);
2227 wolfSSL_X509_VERIFY_PARAM_free(param);
2231 wolfSSL_set_verify(ssl,
2232 WOLFSSL_VERIFY_PEER |
2233 WOLFSSL_VERIFY_CLIENT_ONCE |
2234 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2235 tls_verify_call_back);
2237 wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, tls_verify_call_back);
2243#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13)
2245 if (wolfSSL_dtls_cid_use(ssl) != WOLFSSL_SUCCESS)
2250 if (wolfSSL_dtls_cid_set(ssl,
NULL, 0) != WOLFSSL_SUCCESS)
2261 WOLFSSL *ssl =
NULL;
2263 coap_wolfssl_context_t *w_context = session && session->
context ?
2265 coap_dtls_context_t *dtls;
2266 coap_wolfssl_env_t *w_env = session ?
2270 if (!w_env || !w_context)
2273 if (!setup_dtls_context(w_context))
2275 dtls = &w_context->dtls;
2277 ssl = wolfSSL_new(dtls->ctx);
2282 w_env->data.session = session;
2283 wolfSSL_set_app_data(ssl, session);
2284 wolfSSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2285 wolfSSL_SetIOWriteCtx(ssl, w_env);
2286 wolfSSL_SetIOReadCtx(ssl, w_env);
2287#ifdef WOLFSSL_DTLS_MTU
2288 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2291 if (!setup_client_ssl_session(session, ssl))
2293#ifdef HAVE_SERVER_RENEGOTIATION_INFO
2294 if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
2295 coap_log_debug(
"Error: wolfSSL_UseSecureRenegotiation failed\n");
2301#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
2302 wolfSSL_NoKeyShares(ssl);
2304 r = wolfSSL_connect(ssl);
2306 int ret = wolfSSL_get_error(ssl, r);
2307 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2315 w_env->last_timeout = now;
2322 coap_dtls_free_wolfssl_env(w_env);
2330#ifdef WOLFSSL_DTLS_MTU
2331 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2332 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2335 wolfSSL_dtls_set_mtu(ssl, (
long)session->
mtu);
2344 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2345 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2348 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2349 int r = wolfSSL_shutdown(ssl);
2351 wolfSSL_shutdown(ssl);
2358 coap_dtls_free_wolfssl_env(w_env);
2364 const uint8_t *data,
size_t data_len) {
2365 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2366 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2377 r = wolfSSL_write(ssl, data, (
int)data_len);
2380 int err = wolfSSL_get_error(ssl, r);
2381 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2385 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2387 else if (err == WOLFSSL_ERROR_SSL)
2417 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2418 unsigned int scalar;
2425 scalar = 1 << w_env->retry_scalar;
2439 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2440 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2443 w_env->retry_scalar++;
2449 wolfSSL_dtls_got_timeout(ssl);
2453#if COAP_SERVER_SUPPORT
2457 const uint8_t *data,
size_t data_len) {
2458 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2459 coap_ssl_data_t *ssl_data;
2464 session->
tls = w_env;
2471 ssl_data = w_env ? &w_env->data :
NULL;
2472 assert(ssl_data !=
NULL);
2477 if (ssl_data->pdu_len) {
2478 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2482 ssl_data->session = session;
2483 ssl_data->pdu = data;
2484 ssl_data->pdu_len = (unsigned)data_len;
2493 coap_ssl_data_t *ssl_data;
2494 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2495 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2497 int in_init = wolfSSL_SSL_in_init(ssl);
2500 assert(ssl !=
NULL);
2502 ssl_data = &w_env->data;
2504 if (ssl_data->pdu_len) {
2505 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2508 ssl_data->pdu = data;
2509 ssl_data->pdu_len = (unsigned)data_len;
2512 r = wolfSSL_read(ssl, pdu, (
int)
sizeof(pdu));
2519 int err = wolfSSL_get_error(ssl, r);
2520 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2521 if (in_init && wolfSSL_is_init_finished(ssl)) {
2524#if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) && COAP_CLIENT_SUPPORT
2527 if (wolfSSL_dtls_cid_is_enabled(ssl)) {
2528 session->negotiated_cid = 1;
2531 session->negotiated_cid = 0;
2535 if (!strcmp(wolfSSL_get_version(ssl),
"DTLSv1.3")) {
2544 }
else if (err == APP_DATA_READY) {
2545 r = wolfSSL_read(ssl, pdu, (
int)
sizeof(pdu));
2553 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2558 if (err == FATAL_ERROR) {
2559 WOLFSSL_ALERT_HISTORY h;
2561 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2562 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2565 wolfSSL_alert_desc_string_long(h.last_rx.code));
2584 if (ssl_data && ssl_data->pdu_len) {
2586 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
2587 ssl_data->pdu_len = 0;
2588 ssl_data->pdu =
NULL;
2595 unsigned int overhead = 37;
2596 const WOLFSSL_CIPHER *s_ciph =
NULL;
2597 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2598 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2601 s_ciph = wolfSSL_get_current_cipher(ssl);
2603 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
2605 const WOLFSSL_EVP_CIPHER *e_ciph;
2606 const WOLFSSL_EVP_MD *e_md;
2609 e_ciph = wolfSSL_EVP_get_cipherbynid(wolfSSL_CIPHER_get_cipher_nid(s_ciph));
2611 switch (WOLFSSL_EVP_CIPHER_mode(e_ciph)) {
2613 case WOLFSSL_EVP_CIPH_GCM_MODE:
2614#ifndef WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN
2615#define WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN 8
2617#ifndef WOLFSSL_EVP_GCM_TLS_TAG_LEN
2618#define WOLFSSL_EVP_GCM_TLS_TAG_LEN 16
2620 ivlen = WOLFSSL_EVP_GCM_TLS_EXPLICIT_IV_LEN;
2621 maclen = WOLFSSL_EVP_GCM_TLS_TAG_LEN;
2624 case WOLFSSL_EVP_CIPH_CCM_MODE:
2625#ifndef WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN
2626#define WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN 8
2628 ivlen = WOLFSSL_EVP_CCM_TLS_EXPLICIT_IV_LEN;
2629 wolfSSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
2630 if (strstr(cipher,
"CCM8"))
2636 case WOLFSSL_EVP_CIPH_CBC_MODE:
2637 e_md = wolfSSL_EVP_get_digestbynid(wolfSSL_CIPHER_get_digest_nid(s_ciph));
2638 blocksize = wolfSSL_EVP_CIPHER_block_size(e_ciph);
2639 ivlen = wolfSSL_EVP_CIPHER_iv_length(e_ciph);
2641 maclen = wolfSSL_EVP_MD_size(e_md);
2644 case WOLFSSL_EVP_CIPH_STREAM_CIPHER:
2651 wolfSSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
2658#ifndef WOLFSSL_DTLS13_RT_HEADER_LENGTH
2659#define WOLFSSL_DTLS13_RT_HEADER_LENGTH 13
2661 overhead = WOLFSSL_DTLS13_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 +
2667#if !COAP_DISABLE_TCP
2668#if COAP_CLIENT_SUPPORT
2671 WOLFSSL *ssl =
NULL;
2673 coap_wolfssl_context_t *w_context =
2675 coap_tls_context_t *tls;
2676 coap_wolfssl_env_t *w_env =
2680 if (!w_env || !w_context)
2683 if (!setup_tls_context(w_context))
2685 tls = &w_context->tls;
2687 ssl = wolfSSL_new(tls->ctx);
2690 wolfSSL_SetIOWriteCtx(ssl, w_env);
2691 wolfSSL_SetIOReadCtx(ssl, w_env);
2692 wolfSSL_set_app_data(ssl, session);
2693 w_env->data.session = session;
2695 if (!setup_client_ssl_session(session, ssl))
2698 session->
tls = w_env;
2700 r = wolfSSL_connect(ssl);
2702 int ret = wolfSSL_get_error(ssl, r);
2703 if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE)
2705 if (ret == WOLFSSL_ERROR_WANT_READ)
2707 if (ret == WOLFSSL_ERROR_WANT_WRITE) {
2709#ifdef COAP_EPOLL_SUPPORT
2723 w_env->last_timeout = now;
2724 if (wolfSSL_is_init_finished(ssl)) {
2732 coap_dtls_free_wolfssl_env(w_env);
2739#if COAP_SERVER_SUPPORT
2742 WOLFSSL *ssl =
NULL;
2743 coap_wolfssl_context_t *w_context =
2745 coap_tls_context_t *tls;
2748 coap_wolfssl_env_t *w_env =
2755 if (!setup_tls_context(w_context))
2757 tls = &w_context->tls;
2759 ssl = wolfSSL_new(tls->ctx);
2762 wolfSSL_SetIOWriteCtx(ssl, w_env);
2763 wolfSSL_SetIOReadCtx(ssl, w_env);
2764 wolfSSL_set_app_data(ssl, session);
2766 wolfSSL_set_cipher_list(ssl,
"ALL");
2768 if (w_context->psk_pki_enabled & IS_PSK) {
2771 char *hint = wolfssl_malloc(psk_hint->
length + 1);
2774 memcpy(hint, psk_hint->
s, psk_hint->
length);
2775 hint[psk_hint->
length] =
'\000';
2776 wolfSSL_use_psk_identity_hint(ssl, hint);
2783 if (w_context->psk_pki_enabled & IS_PKI) {
2787#if defined(HAVE_RPK) && LIBWOLFSSL_VERSION_HEX >= 0x05006004
2788 if (w_context->setup_data.is_rpk_not_cert) {
2789 char stype[] = {WOLFSSL_CERT_TYPE_RPK};
2791 wolfSSL_set_server_cert_type(ssl, stype,
sizeof(stype)/
sizeof(stype[0]));
2796 w_env->last_timeout = now;
2798 w_env->data.session = session;
2800 r = wolfSSL_accept(ssl);
2802 int err = wolfSSL_get_error(ssl, r);
2803 if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_WANT_WRITE) {
2806 if (err == WOLFSSL_ERROR_WANT_READ) {
2809 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2811#ifdef COAP_EPOLL_SUPPORT
2824 session->
tls = w_env;
2825 if (wolfSSL_is_init_finished(ssl)) {
2835 coap_dtls_free_wolfssl_env(w_env);
2843 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2844 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2847 if (!wolfSSL_SSL_in_init(ssl) && !(wolfSSL_get_shutdown(ssl) & WOLFSSL_SENT_SHUTDOWN)) {
2848 int r = wolfSSL_shutdown(ssl);
2850 wolfSSL_shutdown(ssl);
2857 coap_dtls_free_wolfssl_env(w_env);
2868 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2869 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2875 in_init = !wolfSSL_is_init_finished(ssl);
2877 r = wolfSSL_write(ssl, data, (
int)data_len);
2880 int err = wolfSSL_get_error(ssl, r);
2881 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2882 if (in_init && wolfSSL_is_init_finished(ssl)) {
2888 if (err == WOLFSSL_ERROR_WANT_READ)
2890 else if (err == WOLFSSL_ERROR_WANT_WRITE) {
2892#ifdef COAP_EPOLL_SUPPORT
2904 if (err == WOLFSSL_ERROR_ZERO_RETURN)
2906 else if (err == WOLFSSL_ERROR_SSL)
2910 }
else if (in_init && wolfSSL_is_init_finished(ssl)) {
2926 if (r == (ssize_t)data_len)
2943 coap_wolfssl_env_t *w_env = (coap_wolfssl_env_t *)session->
tls;
2944 WOLFSSL *ssl = w_env ? w_env->ssl :
NULL;
2952 in_init = !wolfSSL_is_init_finished(ssl);
2954 r = wolfSSL_read(ssl, data, (
int)data_len);
2956 int err = wolfSSL_get_error(ssl, r);
2957 if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) {
2958 if (in_init && wolfSSL_is_init_finished(ssl)) {
2964 if (err == WOLFSSL_ERROR_WANT_READ)
2966 if (err == WOLFSSL_ERROR_WANT_WRITE) {
2968#ifdef COAP_EPOLL_SUPPORT
2978 if (err == WOLFSSL_ERROR_ZERO_RETURN) {
2981 }
else if (err == WOLFSSL_ERROR_SSL) {
2983 }
else if (err == FATAL_ERROR) {
2984 WOLFSSL_ALERT_HISTORY h;
2987 if (wolfSSL_get_alert_history(ssl, &h) == WOLFSSL_SUCCESS) {
2988 if (h.last_rx.code != close_notify && h.last_rx.code != -1) {
2991 wolfSSL_alert_desc_string_long(h.last_rx.code));
2997 }
else if (in_init && wolfSSL_is_init_finished(ssl)) {
3021#if COAP_SERVER_SUPPORT
3023coap_digest_setup(
void) {
3024 WOLFSSL_EVP_MD_CTX *digest_ctx = wolfSSL_EVP_MD_CTX_new();
3027 wolfSSL_EVP_DigestInit_ex(digest_ctx, wolfSSL_EVP_sha256(),
NULL);
3033coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3035 wolfSSL_EVP_MD_CTX_free(digest_ctx);
3039coap_digest_update(coap_digest_ctx_t *digest_ctx,
3040 const uint8_t *data,
3042 return wolfSSL_EVP_DigestUpdate(digest_ctx, data, data_len);
3046coap_digest_final(coap_digest_ctx_t *digest_ctx,
3047 coap_digest_t *digest_buffer) {
3048 unsigned int size =
sizeof(coap_digest_t);
3049 int ret = wolfSSL_EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
3051 coap_digest_free(digest_ctx);
3056#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
3058coap_crypto_output_errors(
const char *prefix) {
3059#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
3064 while ((e = wolfSSL_ERR_get_error()))
3067 wolfSSL_ERR_reason_error_string(e),
3068 ssl_function_definition(e));
3078static struct hash_algs {
3080 const WOLFSSL_EVP_MD *(*get_hash)(void);
3089static const WOLFSSL_EVP_MD *
3090get_hash_alg(
cose_alg_t alg,
size_t *length) {
3093 for (idx = 0; idx <
sizeof(hashes) /
sizeof(
struct hash_algs); idx++) {
3094 if (hashes[idx].alg == alg) {
3095 *length = hashes[idx].length;
3096 return hashes[idx].get_hash();
3099 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3107 unsigned int length;
3108 const WOLFSSL_EVP_MD *evp_md;
3109 WOLFSSL_EVP_MD_CTX *evp_ctx =
NULL;
3113 if ((evp_md = get_hash_alg(alg, &hash_length)) ==
NULL) {
3114 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3117 evp_ctx = wolfSSL_EVP_MD_CTX_new();
3118 if (evp_ctx ==
NULL)
3120 if (wolfSSL_EVP_DigestInit_ex(evp_ctx, evp_md,
NULL) == 0)
3123 if (wolfSSL_EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
3129 if (wolfSSL_EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
3131 dummy->length = length;
3132 if (hash_length < dummy->length)
3133 dummy->length = hash_length;
3135 wolfSSL_EVP_MD_CTX_free(evp_ctx);
3139 coap_crypto_output_errors(
"coap_crypto_hash");
3142 wolfSSL_EVP_MD_CTX_free(evp_ctx);
3147#if COAP_OSCORE_SUPPORT
3148#if LIBWOLFSSL_VERSION_HEX < 0x05006000
3149static const WOLFSSL_EVP_CIPHER *
3150EVP_aes_128_ccm(
void) {
3151 return "AES-128-CCM";
3154static const WOLFSSL_EVP_CIPHER *
3155EVP_aes_256_ccm(
void) {
3156 return "AES-256-CCM";
3170static struct cipher_algs {
3172 const WOLFSSL_EVP_CIPHER *(*get_cipher)(void);
3177static const WOLFSSL_EVP_CIPHER *
3181 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3182 if (ciphers[idx].alg == alg)
3183 return ciphers[idx].get_cipher();
3185 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3194static struct hmac_algs {
3196 const WOLFSSL_EVP_MD *(*get_hmac)(void);
3203static const WOLFSSL_EVP_MD *
3207 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3208 if (hmacs[idx].hmac_alg == hmac_alg)
3209 return hmacs[idx].get_hmac();
3211 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3217 return get_cipher_alg(alg) !=
NULL;
3226 return get_hmac_alg(hmac_alg) !=
NULL;
3230 if (1 != (Func)) { \
3239 size_t *max_result_len) {
3245 byte *authTag =
NULL;
3251 assert(params !=
NULL);
3260 result_len = data->
length;
3261 nonce_length = 15 - ccm->
l;
3263 memset(&aes, 0,
sizeof(aes));
3268 authTag = (
byte *)wolfssl_malloc(ccm->
tag_len);
3272 ret = wc_AesCcmEncrypt(&aes, result, data->
s, data->
length, ccm->
nonce,
3273 nonce_length, authTag, ccm->
tag_len,
3280 memcpy(result + result_len, authTag, ccm->
tag_len);
3282 *max_result_len = result_len;
3283 wolfssl_free(authTag);
3287 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
3288 wolfssl_free(authTag);
3298 size_t *max_result_len) {
3303 byte *authTag =
NULL;
3312 assert(params !=
NULL);
3320 authTag = (
byte *)wolfssl_malloc(ccm->
tag_len);
3331 memset(&aes, 0,
sizeof(aes));
3338 ret = wc_AesCcmDecrypt(&aes, result, data->
s, len, ccm->
nonce,
3339 15 - ccm->
l, authTag, ccm->
tag_len,
3345 *max_result_len = len;
3346 wolfssl_free(authTag);
3350 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
3351 wolfssl_free(authTag);
3360 unsigned int result_len;
3361 const WOLFSSL_EVP_MD *evp_md;
3368 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
3369 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3375 result_len = (
unsigned int)
dummy->length;
3376 if (wolfSSL_HMAC(evp_md,
3383 dummy->length = result_len;
3388 coap_crypto_output_errors(
"coap_crypto_hmac");
3400#pragma GCC diagnostic ignored "-Wunused-function"
struct coap_session_t coap_session_t
#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)
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx 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.
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 *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.
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.
int coap_pki_name_match_sni(const char *name_entry, size_t name_length, const char *sni_match)
Check if the PKI name entry is a (wildcard) match for the requested SNI.
void coap_dtls_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
#define COAP_DTLS_CID_LENGTH
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
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.
#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
Triggerrd 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, 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.
#define COAP_PROTO_NOT_RELIABLE(p)
@ 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.
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::@115100030174240050243254031377105115063160056142 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::@355263010106203056112023075220063075203230161256 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 allow_sni_cn_mismatch
1 if SNI and returned CN allowed to mismatch (Client only).
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_proto_t proto
protocol used
uint8_t is_dtls13
Set if session is DTLS1.3.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition with const data.
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