37#ifdef COAP_WITH_LIBMBEDTLS
53#include <mbedtls/version.h>
56#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
57#define MBEDTLS_2_X_COMPAT
60#ifndef MBEDTLS_ALLOW_PRIVATE_ACCESS
61#define MBEDTLS_ALLOW_PRIVATE_ACCESS
65#include <mbedtls/platform.h>
66#include <mbedtls/net_sockets.h>
67#include <mbedtls/ssl.h>
68#include <mbedtls/entropy.h>
69#include <mbedtls/ctr_drbg.h>
70#include <mbedtls/error.h>
71#include <mbedtls/timing.h>
72#include <mbedtls/ssl_cookie.h>
73#include <mbedtls/oid.h>
74#include <mbedtls/debug.h>
75#include <mbedtls/sha256.h>
76#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
77#include <mbedtls/esp_debug.h>
79#if defined(MBEDTLS_PSA_CRYPTO_C)
80#include <psa/crypto.h>
83#define mbedtls_malloc(a) malloc(a)
84#define mbedtls_realloc(a,b) realloc(a,b)
85#define mbedtls_strdup(a) strdup(a)
86#define mbedtls_strndup(a,b) strndup(a,b)
88#ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
90#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
91#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
95#if ! COAP_SERVER_SUPPORT
96#undef MBEDTLS_SSL_SRV_C
98#if ! COAP_CLIENT_SUPPORT
99#undef MBEDTLS_SSL_CLI_C
103#define strcasecmp _stricmp
106#define IS_PSK (1 << 0)
107#define IS_PKI (1 << 1)
108#define IS_CLIENT (1 << 6)
109#define IS_SERVER (1 << 7)
111typedef struct coap_ssl_t {
122typedef struct coap_mbedtls_env_t {
123 mbedtls_ssl_context ssl;
124 mbedtls_entropy_context entropy;
125 mbedtls_ctr_drbg_context ctr_drbg;
126 mbedtls_ssl_config conf;
127 mbedtls_timing_delay_context timer;
128 mbedtls_x509_crt cacert;
129 mbedtls_x509_crt public_cert;
130 mbedtls_pk_context private_key;
131 mbedtls_ssl_cookie_ctx cookie_ctx;
135 int seen_client_hello;
138 unsigned int retry_scalar;
139 coap_ssl_t coap_ssl_data;
142typedef struct pki_sni_entry {
145 mbedtls_x509_crt cacert;
146 mbedtls_x509_crt public_cert;
147 mbedtls_pk_context private_key;
150typedef struct psk_sni_entry {
155typedef struct coap_mbedtls_context_t {
157 size_t pki_sni_count;
158 pki_sni_entry *pki_sni_entry_list;
159 size_t psk_sni_count;
160 psk_sni_entry *psk_sni_entry_list;
164} coap_mbedtls_context_t;
166typedef enum coap_enc_method_t {
172#ifndef MBEDTLS_2_X_COMPAT
177coap_rng(
void *ctx
COAP_UNUSED,
unsigned char *buf,
size_t len) {
178 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
183coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl) {
188 if (!c_session->
tls) {
190 return MBEDTLS_ERR_SSL_WANT_READ;
192 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
195 if (data->pdu_len > 0) {
196 if (outl < data->pdu_len) {
197 memcpy(out, data->pdu, outl);
200 data->pdu_len -= outl;
202 memcpy(out, data->pdu, data->pdu_len);
204 if (!data->peekmode) {
210 ret = MBEDTLS_ERR_SSL_WANT_READ;
224coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
225 size_t send_buffer_length) {
230 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
242 send_buffer, send_buffer_length);
243 if (result != (ssize_t)send_buffer_length) {
244 int keep_errno = errno;
247 result, send_buffer_length);
257 m_env->last_timeout = now;
265#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
270psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
271 const unsigned char *identity,
size_t identity_len) {
274 coap_mbedtls_env_t *m_env;
278 if (c_session == NULL)
282 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
283 lidentity.
length = identity ? identity_len : 0;
287 (
int)lidentity.
length, (
const char *)lidentity.
s);
289 m_env = (coap_mbedtls_env_t *)c_session->
tls;
304 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
305 m_env->seen_client_hello = 1;
311get_san_or_cn_from_cert(mbedtls_x509_crt *crt) {
313 const mbedtls_asn1_named_data *cn_data;
315 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
316 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
317 while (seq && seq->buf.p == NULL) {
322 return mbedtls_strndup((
const char *)seq->buf.p,
327 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
329 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
332 return mbedtls_strndup((
const char *)cn_data->val.p,
339#if COAP_MAX_LOGGING_LEVEL > 0
341get_error_string(
int ret) {
342 static char buf[128] = {0};
343 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
349self_signed_cert_verify_callback_mbedtls(
void *data,
354 const coap_mbedtls_context_t *m_context =
358 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
360 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
371cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
372 int depth, uint32_t *flags) {
374 coap_mbedtls_context_t *m_context =
382 cn = get_san_or_cn_from_cert(crt);
384 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
386 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
389 "The certificate has expired", cn ? cn :
"?", depth);
392 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
394 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
397 "The certificate has a future date", cn ? cn :
"?", depth);
400 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
402 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
405 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
408 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
410 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
413 "The certificate has a short RSA length", cn ? cn :
"?", depth);
416 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
418 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
419 self_signed_cert_verify_callback_mbedtls,
421 if (self_signed && depth == 0) {
424 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
428 cn ? cn :
"?", depth);
430 }
else if (self_signed) {
432 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
435 "Self-signed", cn ? cn :
"?", depth);
439 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
442 "The certificate's CA is not trusted", cn ? cn :
"?", depth);
446 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
448 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
451 "The certificate's CRL has expired", cn ? cn :
"?", depth);
453 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
456 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
458 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
461 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
463 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
468 *flags |= MBEDTLS_X509_BADCERT_OTHER;
471 "The certificate's verify depth is too long",
472 cn ? cn :
"?", depth);
475 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
476 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
490 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
496 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
499 tcp = strchr(buf,
'\n');
504 buf, *flags, cn ? cn :
"?", depth);
505 tcp = strchr(tcp+1,
'\n');
508 coap_log_err(
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
509 -ret, get_error_string(ret));
520setup_pki_credentials(mbedtls_x509_crt *cacert,
521 mbedtls_x509_crt *public_cert,
522 mbedtls_pk_context *private_key,
523 coap_mbedtls_env_t *m_env,
524 coap_mbedtls_context_t *m_context,
530 int done_private_key = 0;
531 int done_public_cert = 0;
549#if defined(MBEDTLS_FS_IO)
550 mbedtls_pk_init(private_key);
551#ifdef MBEDTLS_2_X_COMPAT
552 ret = mbedtls_pk_parse_keyfile(private_key,
555 ret = mbedtls_pk_parse_keyfile(private_key,
557 NULL, coap_rng, (
void *)&m_env->ctr_drbg);
564 done_private_key = 1;
572 mbedtls_pk_init(private_key);
576 buffer = mbedtls_malloc(length + 1);
582 buffer[length] =
'\000';
584#ifdef MBEDTLS_2_X_COMPAT
585 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
587 ret = mbedtls_pk_parse_key(private_key, buffer, length,
588 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
590 mbedtls_free(buffer);
592#ifdef MBEDTLS_2_X_COMPAT
593 ret = mbedtls_pk_parse_key(private_key,
597 ret = mbedtls_pk_parse_key(private_key,
600 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
608 done_private_key = 1;
611 mbedtls_pk_init(private_key);
612#ifdef MBEDTLS_2_X_COMPAT
613 ret = mbedtls_pk_parse_key(private_key,
617 ret = mbedtls_pk_parse_key(private_key,
620 (
void *)&m_env->ctr_drbg);
627 done_private_key = 1;
655#if defined(MBEDTLS_FS_IO)
656 mbedtls_x509_crt_init(public_cert);
657 ret = mbedtls_x509_crt_parse_file(public_cert,
664 done_public_cert = 1;
672 mbedtls_x509_crt_init(public_cert);
677 buffer = mbedtls_malloc(length + 1);
683 buffer[length] =
'\000';
685 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
686 mbedtls_free(buffer);
688 ret = mbedtls_x509_crt_parse(public_cert,
697 done_public_cert = 1;
704 mbedtls_x509_crt_init(public_cert);
705 ret = mbedtls_x509_crt_parse(public_cert,
713 done_public_cert = 1;
731 if (done_private_key && done_public_cert) {
732 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
734 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
735 -ret, get_error_string(ret));
744#
if MBEDTLS_VERSION_NUMBER < 0x03060000
753#if defined(MBEDTLS_FS_IO)
754 mbedtls_x509_crt_init(cacert);
755 ret = mbedtls_x509_crt_parse_file(cacert,
762 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
770 mbedtls_x509_crt_init(cacert);
774 buffer = mbedtls_malloc(length + 1);
780 buffer[length] =
'\000';
782 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
783 mbedtls_free(buffer);
785 ret = mbedtls_x509_crt_parse(cacert,
794 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
801 mbedtls_x509_crt_init(cacert);
802 ret = mbedtls_x509_crt_parse(cacert,
810 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
824#if defined(MBEDTLS_FS_IO)
825 if (m_context->root_ca_file) {
826 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
833 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
835 if (m_context->root_ca_path) {
836 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
843 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
852#if defined(MBEDTLS_SSL_SRV_C)
853 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
855 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
856 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
859 MBEDTLS_SSL_VERIFY_REQUIRED :
860 MBEDTLS_SSL_VERIFY_NONE);
865 mbedtls_ssl_conf_verify(&m_env->conf,
866 cert_verify_callback_mbedtls, c_session);
871#if defined(MBEDTLS_SSL_SRV_C)
876pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
877 const unsigned char *uname,
size_t name_len) {
881 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
882 coap_mbedtls_context_t *m_context =
886 name = mbedtls_malloc(name_len+1);
890 memcpy(name, uname, name_len);
891 name[name_len] =
'\000';
894 for (i = 0; i < m_context->pki_sni_count; i++) {
895 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
899 if (i == m_context->pki_sni_count) {
904 pki_sni_entry *pki_sni_entry_list;
907 m_context->setup_data.validate_sni_call_back(name,
908 m_context->setup_data.sni_call_back_arg));
914 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
915 (i+1)*
sizeof(pki_sni_entry));
917 if (pki_sni_entry_list == NULL) {
921 m_context->pki_sni_entry_list = pki_sni_entry_list;
922 memset(&m_context->pki_sni_entry_list[i], 0,
923 sizeof(m_context->pki_sni_entry_list[i]));
924 m_context->pki_sni_entry_list[i].sni = name;
925 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
926 sni_setup_data = m_context->setup_data;
927 sni_setup_data.pki_key = *new_entry;
928 if (setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
929 &m_context->pki_sni_entry_list[i].public_cert,
930 &m_context->pki_sni_entry_list[i].private_key,
939 m_context->pki_sni_count++;
944 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
946 return mbedtls_ssl_set_hs_own_cert(ssl,
947 &m_context->pki_sni_entry_list[i].public_cert,
948 &m_context->pki_sni_entry_list[i].private_key);
951#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
956psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
957 const unsigned char *uname,
size_t name_len) {
960 coap_mbedtls_context_t *m_context =
964 name = mbedtls_malloc(name_len+1);
968 memcpy(name, uname, name_len);
969 name[name_len] =
'\000';
972 for (i = 0; i < m_context->psk_sni_count; i++) {
973 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
977 if (i == m_context->psk_sni_count) {
982 psk_sni_entry *psk_sni_entry_list;
993 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
994 (i+1)*
sizeof(psk_sni_entry));
996 if (psk_sni_entry_list == NULL) {
1000 m_context->psk_sni_entry_list = psk_sni_entry_list;
1001 m_context->psk_sni_entry_list[i].sni = name;
1002 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1004 m_context->psk_sni_count++;
1010 &m_context->psk_sni_entry_list[i].psk_info.hint);
1012 &m_context->psk_sni_entry_list[i].psk_info.key);
1013 return mbedtls_ssl_set_hs_psk(ssl,
1014 m_context->psk_sni_entry_list[i].psk_info.key.s,
1015 m_context->psk_sni_entry_list[i].psk_info.key.length);
1021 coap_mbedtls_env_t *m_env) {
1022 coap_mbedtls_context_t *m_context =
1025 m_context->psk_pki_enabled |= IS_SERVER;
1027 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
1028 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1029 MBEDTLS_SSL_IS_SERVER,
1031 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1032 MBEDTLS_SSL_TRANSPORT_STREAM,
1033 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1034 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1035 -ret, get_error_string(ret));
1039 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1041#if defined(MBEDTLS_SSL_PROTO_DTLS)
1042 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1043 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1046 if (m_context->psk_pki_enabled & IS_PSK) {
1047#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1048 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1050 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1052#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1060 if (m_context->psk_pki_enabled & IS_PKI) {
1061 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1062 &m_env->private_key, m_env, m_context,
1063 c_session, &m_context->setup_data,
1069 if (m_context->setup_data.validate_sni_call_back) {
1070 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1074 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1075 mbedtls_ctr_drbg_random,
1076 &m_env->ctr_drbg)) != 0) {
1077 coap_log_err(
"mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1078 -ret, get_error_string(ret));
1082#if defined(MBEDTLS_SSL_PROTO_DTLS)
1083 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1084 mbedtls_ssl_cookie_check,
1085 &m_env->cookie_ctx);
1086#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1087 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1090#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1098 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1105#if COAP_CLIENT_SUPPORT
1106static int *psk_ciphers = NULL;
1107static int *pki_ciphers = NULL;
1108static int *ecjpake_ciphers = NULL;
1109static int processed_ciphers = 0;
1111#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1113coap_ssl_ciphersuite_uses_psk(
const mbedtls_ssl_ciphersuite_t *info) {
1114#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1115 switch (info->key_exchange) {
1116 case MBEDTLS_KEY_EXCHANGE_PSK:
1117 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1118 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1119 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1121 case MBEDTLS_KEY_EXCHANGE_NONE:
1122 case MBEDTLS_KEY_EXCHANGE_RSA:
1123 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1124 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1125 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1126 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1127 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1128 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1133 return mbedtls_ssl_ciphersuite_uses_psk(info);
1139set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1140 if (!processed_ciphers) {
1141 const int *list = mbedtls_ssl_list_ciphersuites();
1142 const int *base = list;
1145#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1147 int ecjpake_count = 1;
1153 const mbedtls_ssl_ciphersuite_t *cur =
1154 mbedtls_ssl_ciphersuite_from_id(*list);
1157#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1158 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1162 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1166#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1167 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1171#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1172 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1177#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1178 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1190 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
1191 if (psk_ciphers == NULL) {
1192 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1195 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
1196 if (pki_ciphers == NULL) {
1197 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1198 mbedtls_free(psk_ciphers);
1202#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1203 ecjpake_ciphers = mbedtls_malloc(ecjpake_count *
sizeof(ecjpake_ciphers[0]));
1204 if (ecjpake_ciphers == NULL) {
1205 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1206 mbedtls_free(psk_ciphers);
1207 mbedtls_free(pki_ciphers);
1214 psk_list = psk_ciphers;
1215 pki_list = pki_ciphers;
1216#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1217 ecjpake_list = ecjpake_ciphers;
1221 const mbedtls_ssl_ciphersuite_t *cur =
1222 mbedtls_ssl_ciphersuite_from_id(*list);
1224#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1225 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1229 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1233#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1234 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1235 *ecjpake_list = *list;
1239#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1240 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1247#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1248 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1263 processed_ciphers = 1;
1267 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1270 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1272 case COAP_ENC_ECJPAKE:
1273 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1283 coap_mbedtls_env_t *m_env) {
1286 coap_mbedtls_context_t *m_context =
1289 m_context->psk_pki_enabled |= IS_CLIENT;
1291 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1292 MBEDTLS_SSL_IS_CLIENT,
1294 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1295 MBEDTLS_SSL_TRANSPORT_STREAM,
1296 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1297 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1298 -ret, get_error_string(ret));
1302#if defined(MBEDTLS_SSL_PROTO_DTLS)
1303 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1304 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1307 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1308 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1310 if (m_context->psk_pki_enabled & IS_PSK) {
1311#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1319 if (psk_key == NULL || psk_identity == NULL) {
1320 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1324 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->
s,
1325 psk_key->
length, psk_identity->
s,
1326 psk_identity->
length)) != 0) {
1327 coap_log_err(
"mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1328 -ret, get_error_string(ret));
1332 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1334 coap_log_err(
"mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1335 -ret, get_error_string(ret));
1341#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1343 m_env->ec_jpake = 1;
1344 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1345#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1346 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1349 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1352 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1357 }
else if ((m_context->psk_pki_enabled & IS_PKI) ||
1358 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1363 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1364 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1365 &m_env->private_key, m_env, m_context,
1366 c_session, &m_context->setup_data,
1372#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1375 static const char *alpn_list[] = {
"coap", NULL };
1377 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1383 if (m_context->setup_data.client_sni) {
1384 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1386#if defined(MBEDTLS_SSL_PROTO_DTLS)
1387#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1388 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1391 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1401mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1406 mbedtls_x509_crt_free(&m_env->cacert);
1407 mbedtls_x509_crt_free(&m_env->public_cert);
1408 mbedtls_pk_free(&m_env->private_key);
1409 mbedtls_entropy_free(&m_env->entropy);
1410 mbedtls_ssl_config_free(&m_env->conf);
1411 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1412 mbedtls_ssl_free(&m_env->ssl);
1413 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1417coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1419 if (!m_env->sent_alert)
1420 mbedtls_ssl_close_notify(&m_env->ssl);
1421 mbedtls_cleanup(m_env);
1422 mbedtls_free(m_env);
1426#if COAP_MAX_LOGGING_LEVEL > 0
1428report_mbedtls_alert(
unsigned char alert) {
1430 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1431 return ": Bad Record MAC";
1432 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1433 return ": Handshake failure";
1434 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1435 return ": No Certificate provided";
1436 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1437 return ": Certificate is bad";
1438 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1439 return ": Certificate is unknown";
1440 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1441 return ": CA is unknown";
1442 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1443 return ": Access was denied";
1444 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1445 return ": Decrypt error";
1459 coap_mbedtls_env_t *m_env) {
1463 ret = mbedtls_ssl_handshake(&m_env->ssl);
1466 m_env->established = 1;
1470#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1471#if COAP_CLIENT_SUPPORT
1474 coap_mbedtls_context_t *m_context;
1478 m_context->setup_data.use_cid) {
1479 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1481 size_t peer_cid_len;
1484 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1485 enabled == MBEDTLS_SSL_CID_ENABLED) {
1496 case MBEDTLS_ERR_SSL_WANT_READ:
1497 case MBEDTLS_ERR_SSL_WANT_WRITE:
1501 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1504 case MBEDTLS_ERR_SSL_INVALID_MAC:
1506#ifdef MBEDTLS_2_X_COMPAT
1507 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1509 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1512 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1513 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1515#ifdef MBEDTLS_2_X_COMPAT
1516 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1517 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1518 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1521 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1523 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1524 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1527 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1529 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1530 case MBEDTLS_ERR_SSL_CONN_EOF:
1531 case MBEDTLS_ERR_NET_CONN_RESET:
1537 "returned -0x%x: '%s'\n",
1538 -ret, get_error_string(ret));
1545 mbedtls_ssl_send_alert_message(&m_env->ssl,
1546 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1548 m_env->sent_alert = 1;
1553 get_error_string(ret));
1555 mbedtls_ssl_session_reset(&m_env->ssl);
1556#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1557 if (m_env->ec_jpake) {
1560#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1566#elif COAP_CLIENT_SUPPORT
1572 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1580mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1613#if !COAP_DISABLE_TCP
1621coap_sock_read(
void *ctx,
unsigned char *out,
size_t outl) {
1622 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
1629 if (errno == ECONNRESET) {
1631 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1633 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1635 }
else if (ret == 0) {
1637 ret = MBEDTLS_ERR_SSL_WANT_READ;
1650coap_sock_write(
void *context,
const unsigned char *in,
size_t inl) {
1655 (
const uint8_t *)in,
1661 (errno == EPIPE || errno == ECONNRESET)) {
1676 int lasterror = WSAGetLastError();
1678 if (lasterror == WSAEWOULDBLOCK) {
1679 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1680 }
else if (lasterror == WSAECONNRESET) {
1681 ret = MBEDTLS_ERR_NET_CONN_RESET;
1684 if (errno == EAGAIN || errno == EINTR) {
1685 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1686 }
else if (errno == EPIPE || errno == ECONNRESET) {
1687 ret = MBEDTLS_ERR_NET_CONN_RESET;
1691 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1700 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1706static coap_mbedtls_env_t *
1711 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1716 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1720 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1722 mbedtls_ssl_init(&m_env->ssl);
1723 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1724 mbedtls_ssl_config_init(&m_env->conf);
1725 mbedtls_entropy_init(&m_env->entropy);
1727#if defined(MBEDTLS_PSA_CRYPTO_C)
1731#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1732 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1734 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1735 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1736 coap_log_err(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1737 -ret, get_error_string(ret));
1742#if COAP_CLIENT_SUPPORT
1743 if (setup_client_ssl_session(c_session, m_env) != 0) {
1750#if defined(MBEDTLS_SSL_SRV_C)
1751 if (setup_server_ssl_session(c_session, m_env) != 0) {
1761#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1762 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1764 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1765 MBEDTLS_SSL_MINOR_VERSION_3);
1768 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
1772 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1773 coap_dgram_read, NULL);
1774#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1777#if COAP_CLIENT_SUPPORT
1778 coap_mbedtls_context_t *m_context =
1782 m_context->setup_data.use_cid) {
1790 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
1794#if COAP_SERVER_SUPPORT
1795 u_char cid[COAP_DTLS_CID_LENGTH];
1804 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
1812#if !COAP_DISABLE_TCP
1815 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1816 coap_sock_read, NULL);
1819#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1820 coap_mbedtls_context_t *m_context =
1822 if ((m_context->psk_pki_enabled & IS_PSK) &&
1826#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1832#elif COAP_CLIENT_SUPPORT
1837 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1840 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1841 mbedtls_timing_set_delay,
1842 mbedtls_timing_get_delay);
1844 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1849 mbedtls_free(m_env);
1856#if defined(MBEDTLS_SSL_PROTO_DTLS)
1859 static int reported = 0;
1863 " - update Mbed TLS to include DTLS\n");
1871#if !COAP_DISABLE_TCP
1920#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1927#if COAP_CLIENT_SUPPORT
1930#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1943 coap_mbedtls_context_t *m_context;
1946 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
1948 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
1953#if COAP_SERVER_SUPPORT
1962 coap_mbedtls_context_t *m_context =
1965#if !defined(MBEDTLS_SSL_SRV_C)
1967 " libcoap not compiled for Server Mode for Mbed TLS"
1968 " - update Mbed TLS to include Server Mode\n");
1971 if (!m_context || !setup_data)
1975#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1976 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
1979 m_context->psk_pki_enabled |= IS_PSK;
1984#if COAP_CLIENT_SUPPORT
1993#if !defined(MBEDTLS_SSL_CLI_C)
1998 " libcoap not compiled for Client Mode for Mbed TLS"
1999 " - update Mbed TLS to include Client Mode\n");
2002 coap_mbedtls_context_t *m_context =
2005 if (!m_context || !setup_data)
2009 coap_log_warn(
"CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2012#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2013 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
2017#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2018 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2021 m_context->psk_pki_enabled |= IS_PSK;
2031 coap_mbedtls_context_t *m_context =
2034 m_context->setup_data = *setup_data;
2035 if (!m_context->setup_data.verify_peer_cert) {
2037 m_context->setup_data.check_common_ca = 0;
2039 m_context->setup_data.allow_self_signed = 1;
2040 m_context->setup_data.allow_expired_certs = 1;
2041 m_context->setup_data.cert_chain_validation = 1;
2042 m_context->setup_data.cert_chain_verify_depth = 10;
2043 m_context->setup_data.check_cert_revocation = 1;
2044 m_context->setup_data.allow_no_crl = 1;
2045 m_context->setup_data.allow_expired_crl = 1;
2046 m_context->setup_data.allow_bad_md_hash = 1;
2047 m_context->setup_data.allow_short_rsa_length = 1;
2049 m_context->psk_pki_enabled |= IS_PKI;
2051#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2052 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2060 const char *ca_file,
2061 const char *ca_path) {
2062 coap_mbedtls_context_t *m_context =
2066 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
2071 if (ca_file == NULL && ca_path == NULL) {
2072 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
2076 if (m_context->root_ca_file) {
2077 mbedtls_free(m_context->root_ca_file);
2078 m_context->root_ca_file = NULL;
2082 m_context->root_ca_file = mbedtls_strdup(ca_file);
2085 if (m_context->root_ca_path) {
2086 mbedtls_free(m_context->root_ca_path);
2087 m_context->root_ca_path = NULL;
2091 m_context->root_ca_path = mbedtls_strdup(ca_path);
2098 coap_mbedtls_context_t *m_context =
2100 return m_context->psk_pki_enabled ? 1 : 0;
2105 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2108 for (i = 0; i < m_context->pki_sni_count; i++) {
2109 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
2111 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
2113 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
2115 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
2117 if (m_context->pki_sni_entry_list)
2118 mbedtls_free(m_context->pki_sni_entry_list);
2120 for (i = 0; i < m_context->psk_sni_count; i++) {
2121 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2123 if (m_context->psk_sni_entry_list)
2124 mbedtls_free(m_context->psk_sni_entry_list);
2126 if (m_context->root_ca_path)
2127 mbedtls_free(m_context->root_ca_path);
2128 if (m_context->root_ca_file)
2129 mbedtls_free(m_context->root_ca_file);
2131 mbedtls_free(m_context);
2134#if COAP_CLIENT_SUPPORT
2137#if !defined(MBEDTLS_SSL_CLI_C)
2140 " libcoap not compiled for Client Mode for Mbed TLS"
2141 " - update Mbed TLS to include Client Mode\n");
2144 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2153 m_env->last_timeout = now;
2154 ret = do_mbedtls_handshake(c_session, m_env);
2156 coap_dtls_free_mbedtls_env(m_env);
2165#if COAP_SERVER_SUPPORT
2168#if !defined(MBEDTLS_SSL_SRV_C)
2171 " libcoap not compiled for Server Mode for Mbed TLS"
2172 " - update Mbed TLS to include Server Mode\n");
2175 coap_mbedtls_env_t *m_env =
2176 (coap_mbedtls_env_t *)c_session->
tls;
2178#if defined(MBEDTLS_SSL_PROTO_DTLS)
2179#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2180 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2191 if (c_session && c_session->
context && c_session->
tls) {
2192 coap_dtls_free_mbedtls_env(c_session->
tls);
2193 c_session->
tls = NULL;
2201#if defined(MBEDTLS_SSL_PROTO_DTLS)
2202 coap_mbedtls_env_t *m_env =
2203 (coap_mbedtls_env_t *)c_session->
tls;
2205#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2206 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2216 const uint8_t *data,
size_t data_len) {
2218 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2220 assert(m_env != NULL);
2228 if (m_env->established) {
2229 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char *) data, data_len);
2232 case MBEDTLS_ERR_SSL_WANT_READ:
2233 case MBEDTLS_ERR_SSL_WANT_WRITE:
2236 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2242 "returned -0x%x: '%s'\n",
2243 -ret, get_error_string(ret));
2252 ret = do_mbedtls_handshake(c_session, m_env);
2285 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2286 int ret = mbedtls_timing_get_delay(&m_env->timer);
2287 unsigned int scalar = 1 << m_env->retry_scalar;
2297 m_env->last_timeout = now;
2310 m_env->last_timeout = now;
2328 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2331 m_env->retry_scalar++;
2333 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2348 const uint8_t *data,
2353 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2354 coap_ssl_t *ssl_data;
2356 assert(m_env != NULL);
2358 ssl_data = &m_env->coap_ssl_data;
2359 if (ssl_data->pdu_len) {
2360 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2363 ssl_data->pdu = data;
2364 ssl_data->pdu_len = (unsigned)data_len;
2366 if (m_env->established) {
2367#if COAP_CONSTRAINED_STACK
2380 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
2387 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2388 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2391 case MBEDTLS_ERR_SSL_WANT_READ:
2395 "returned -0x%x: '%s' (length %zd)\n",
2396 -ret, get_error_string(ret), data_len);
2401 ret = do_mbedtls_handshake(c_session, m_env);
2406 if (ssl_data->pdu_len) {
2408 ret = do_mbedtls_handshake(c_session, m_env);
2429 if (ssl_data && ssl_data->pdu_len) {
2431 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2432 ssl_data->pdu_len = 0;
2433 ssl_data->pdu = NULL;
2442#if COAP_SERVER_SUPPORT
2450 const uint8_t *data,
2452#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2457 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2458 " - update Mbed TLS to include DTLS and Server Mode\n");
2461 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2462 coap_ssl_t *ssl_data;
2469 c_session->
tls = m_env;
2476 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2479 coap_log_err(
"mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2480 -ret, get_error_string(ret));
2484 ssl_data = &m_env->coap_ssl_data;
2485 if (ssl_data->pdu_len) {
2486 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2489 ssl_data->pdu = data;
2490 ssl_data->pdu_len = (unsigned)data_len;
2492 ret = do_mbedtls_handshake(c_session, m_env);
2493 if (ret == 0 || m_env->seen_client_hello) {
2499 m_env->seen_client_hello = 0;
2505 if (ssl_data->pdu_len) {
2507 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2508 ssl_data->pdu_len = 0;
2509 ssl_data->pdu = NULL;
2518 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2519 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2521 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2527#if !COAP_DISABLE_TCP
2528#if COAP_CLIENT_SUPPORT
2531#if !defined(MBEDTLS_SSL_CLI_C)
2535 " libcoap not compiled for Client Mode for Mbed TLS"
2536 " - update Mbed TLS to include Client Mode\n");
2539 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2549 m_env->last_timeout = now;
2550 c_session->
tls = m_env;
2551 ret = do_mbedtls_handshake(c_session, m_env);
2561#if COAP_SERVER_SUPPORT
2564#if !defined(MBEDTLS_SSL_SRV_C)
2569 " libcoap not compiled for Server Mode for Mbed TLS"
2570 " - update Mbed TLS to include Server Mode\n");
2573 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2581 c_session->
tls = m_env;
2582 ret = do_mbedtls_handshake(c_session, m_env);
2607 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2608 size_t amount_sent = 0;
2610 assert(m_env != NULL);
2617 if (m_env->established) {
2618 while (amount_sent < data_len) {
2619 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
2620 data_len - amount_sent);
2623 case MBEDTLS_ERR_SSL_WANT_READ:
2624 case MBEDTLS_ERR_SSL_WANT_WRITE:
2631 case MBEDTLS_ERR_NET_CONN_RESET:
2632 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2637 "returned -0x%x: '%s'\n",
2638 -ret, get_error_string(ret));
2650 ret = do_mbedtls_handshake(c_session, m_env);
2671 if (ret == (ssize_t)data_len)
2690 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2699 if (!m_env->established && !m_env->sent_alert) {
2700 ret = do_mbedtls_handshake(c_session, m_env);
2709 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2713 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2717 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2719 m_env->sent_alert = 1;
2722#if MBEDTLS_VERSION_NUMBER >= 0x03060000
2723 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
2725 case MBEDTLS_ERR_SSL_WANT_READ:
2731 "returned -0x%x: '%s' (length %zd)\n",
2732 -ret, get_error_string(ret), data_len);
2736 }
else if (ret < (
int)data_len) {
2737 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
2765#if COAP_CLIENT_SUPPORT
2766 mbedtls_free(psk_ciphers);
2767 mbedtls_free(pki_ciphers);
2768 mbedtls_free(ecjpake_ciphers);
2771 ecjpake_ciphers = NULL;
2772 processed_ciphers = 0;
2782 if (c_session && c_session->
tls) {
2783 coap_mbedtls_env_t *m_env;
2786 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
2788 return (
void *)&m_env->ssl;
2797#if !defined(ESPIDF_VERSION)
2807 switch ((
int)level) {
2828 mbedtls_debug_set_threshold(use_level);
2830 keep_log_level = level;
2835 return keep_log_level;
2841 version.
version = mbedtls_version_get_number();
2847#if COAP_SERVER_SUPPORT
2850 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2853 mbedtls_sha256_init(digest_ctx);
2854#ifdef MBEDTLS_2_X_COMPAT
2855 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2857 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2868 mbedtls_sha256_free(digest_ctx);
2869 mbedtls_free(digest_ctx);
2874 const uint8_t *data,
2876#ifdef MBEDTLS_2_X_COMPAT
2877 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2879 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2888#ifdef MBEDTLS_2_X_COMPAT
2889 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t *)digest_buffer);
2891 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t *)digest_buffer);
2899#include <mbedtls/cipher.h>
2900#include <mbedtls/md.h>
2902#ifndef MBEDTLS_CIPHER_MODE_AEAD
2903#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
2906#ifdef MBEDTLS_ERROR_C
2907#include <mbedtls/error.h>
2910#ifdef MBEDTLS_ERROR_C
2913 int c_tmp = (int)(Func); \
2915 char error_buf[64]; \
2916 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
2917 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
2924 int c_tmp = (int)(Func); \
2926 coap_log_err("mbedtls: %d\n", tmp); \
2937static struct hash_algs {
2939 mbedtls_md_type_t hash_type;
2947static mbedtls_md_type_t
2948get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
2951 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2952 if (hashs[idx].alg == alg) {
2953 *hash_len = hashs[idx].hash_size;
2954 return hashs[idx].hash_type;
2957 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2958 return MBEDTLS_MD_NONE;
2965 mbedtls_md_context_t ctx;
2967 const mbedtls_md_info_t *md_info;
2971 mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length);
2973 if (dig_type == MBEDTLS_MD_NONE) {
2974 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2977 md_info = mbedtls_md_info_from_type(dig_type);
2979 len = mbedtls_md_get_size(md_info);
2984 mbedtls_md_init(&ctx);
2985 C(mbedtls_md_setup(&ctx, md_info, 0));
2987 C(mbedtls_md_starts(&ctx));
2988 C(mbedtls_md_update(&ctx, (
const unsigned char *)data->
s, data->
length));
2992 C(mbedtls_md_finish(&ctx,
dummy->s));
2997 mbedtls_md_free(&ctx);
3002#if COAP_OSCORE_SUPPORT
3013static struct cipher_algs {
3015 mbedtls_cipher_type_t cipher_type;
3020static mbedtls_cipher_type_t
3024 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3025 if (ciphers[idx].alg == alg)
3026 return ciphers[idx].cipher_type;
3028 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3037static struct hmac_algs {
3039 mbedtls_md_type_t hmac_type;
3046static mbedtls_md_type_t
3050 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3051 if (hmacs[idx].hmac_alg == hmac_alg)
3052 return hmacs[idx].hmac_type;
3054 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3060 return get_cipher_alg(alg) != 0;
3069 return get_hmac_alg(hmac_alg) != 0;
3077setup_cipher_context(mbedtls_cipher_context_t *ctx,
3079 const uint8_t *key_data,
3081 mbedtls_operation_t mode) {
3082 const mbedtls_cipher_info_t *cipher_info;
3083 mbedtls_cipher_type_t cipher_type;
3087 memset(key, 0,
sizeof(key));
3089 if ((cipher_type = get_cipher_alg(coap_alg)) == 0) {
3090 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3094 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3096 coap_log_crit(
"coap_crypto_encrypt: cannot get cipher info\n");
3100 mbedtls_cipher_init(ctx);
3102 C(mbedtls_cipher_setup(ctx, cipher_info));
3103 klen = mbedtls_cipher_get_key_bitlen(ctx);
3104 if ((klen > (
int)(
sizeof(key) * 8)) || (key_length >
sizeof(key))) {
3108 memcpy(key, key_data, key_length);
3109 C(mbedtls_cipher_setkey(ctx, key, klen, mode));
3114 mbedtls_cipher_free(ctx);
3123 size_t *max_result_len) {
3124 mbedtls_cipher_context_t ctx;
3126#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3127 unsigned char tag[16];
3130 size_t result_len = *max_result_len;
3136 assert(params != NULL);
3143 if (!setup_cipher_context(&ctx,
3158#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3159 C(mbedtls_cipher_auth_encrypt(&ctx,
3172 if ((result_len + ccm->
tag_len) > *max_result_len) {
3177 memcpy(result + result_len, tag, ccm->
tag_len);
3178 *max_result_len = result_len + ccm->
tag_len;
3181 C(mbedtls_cipher_auth_encrypt_ext(&ctx,
3193 *max_result_len = result_len;
3198 mbedtls_cipher_free(&ctx);
3207 size_t *max_result_len) {
3208 mbedtls_cipher_context_t ctx;
3210#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3211 const unsigned char *tag;
3214 size_t result_len = *max_result_len;
3220 assert(params != NULL);
3228 if (!setup_cipher_context(&ctx,
3248#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3250 C(mbedtls_cipher_auth_decrypt(&ctx,
3263 C(mbedtls_cipher_auth_decrypt_ext(&ctx,
3278 *max_result_len = result_len;
3281 mbedtls_cipher_free(&ctx);
3290 mbedtls_md_context_t ctx;
3292 const int use_hmac = 1;
3293 const mbedtls_md_info_t *md_info;
3294 mbedtls_md_type_t mac_algo;
3302 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3303 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3306 md_info = mbedtls_md_info_from_type(mac_algo);
3308 len = mbedtls_md_get_size(md_info);
3313 mbedtls_md_init(&ctx);
3314 C(mbedtls_md_setup(&ctx, md_info, use_hmac));
3316 C(mbedtls_md_hmac_starts(&ctx, key->
s, key->
length));
3317 C(mbedtls_md_hmac_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3321 C(mbedtls_md_hmac_finish(&ctx,
dummy->s));
3326 mbedtls_md_free(&ctx);
3338#pragma GCC diagnostic ignored "-Wunused-function"
#define COAP_SERVER_SUPPORT
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
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_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)
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.
#define COAP_CRYPTO_MAX_KEY_SIZE
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.
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_KEY_ROOT_CA
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
@ COAP_TLS_LIBRARY_MBEDTLS
Using Mbed TLS library.
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ 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(...)
#define coap_log_emerg(...)
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_crit(...)
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_256
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
coap_proto_t
CoAP protocol types.
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).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
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_SESSION_STATE_NONE
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...
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_address_t remote
remote address and port
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.
The CoAP stack's global state is stored in a coap_context_t object.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
size_t tag_len
The size of the Tag.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@2 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorith to use.
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.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
The structure that holds the PKI key information.
coap_pki_key_define_t define
for definable type keys
union coap_dtls_key_t::@3 key
coap_pki_key_t key_type
key format type
The structure used for defining the PKI setup data to be used.
void * cn_call_back_arg
Passed in to the CN callback function.
uint8_t allow_short_rsa_length
1 if small RSA keysizes are allowed
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
uint8_t allow_bad_md_hash
1 if unsupported MD hashes are allowed
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
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.
uint8_t allow_self_signed
1 if self-signed certs are allowed.
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 check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
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_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_endpoint_t * endpoint
session's endpoint
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_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
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
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