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>
80#define mbedtls_malloc(a) malloc(a)
81#define mbedtls_realloc(a,b) realloc(a,b)
82#define mbedtls_strdup(a) strdup(a)
83#define mbedtls_strndup(a,b) strndup(a,b)
85#ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
87#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
88#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
92#if ! COAP_SERVER_SUPPORT
93#undef MBEDTLS_SSL_SRV_C
95#if ! COAP_CLIENT_SUPPORT
96#undef MBEDTLS_SSL_CLI_C
100#define strcasecmp _stricmp
103#define IS_PSK (1 << 0)
104#define IS_PKI (1 << 1)
105#define IS_CLIENT (1 << 6)
106#define IS_SERVER (1 << 7)
108typedef struct coap_ssl_t {
119typedef struct coap_mbedtls_env_t {
120 mbedtls_ssl_context ssl;
121 mbedtls_entropy_context entropy;
122 mbedtls_ctr_drbg_context ctr_drbg;
123 mbedtls_ssl_config conf;
124 mbedtls_timing_delay_context timer;
125 mbedtls_x509_crt cacert;
126 mbedtls_x509_crt public_cert;
127 mbedtls_pk_context private_key;
128 mbedtls_ssl_cookie_ctx cookie_ctx;
132 int seen_client_hello;
134 unsigned int retry_scalar;
135 coap_ssl_t coap_ssl_data;
138typedef struct pki_sni_entry {
141 mbedtls_x509_crt cacert;
142 mbedtls_x509_crt public_cert;
143 mbedtls_pk_context private_key;
146typedef struct psk_sni_entry {
151typedef struct coap_mbedtls_context_t {
153 size_t pki_sni_count;
154 pki_sni_entry *pki_sni_entry_list;
155 size_t psk_sni_count;
156 psk_sni_entry *psk_sni_entry_list;
160} coap_mbedtls_context_t;
162typedef enum coap_enc_method_t {
167#ifndef MBEDTLS_2_X_COMPAT
172coap_rng(
void *ctx
COAP_UNUSED,
unsigned char *buf,
size_t len) {
173 return coap_prng(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
178coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl) {
183 if (!c_session->
tls) {
185 return MBEDTLS_ERR_SSL_WANT_READ;
187 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
190 if (data->pdu_len > 0) {
191 if (outl < data->pdu_len) {
192 memcpy(out, data->pdu, outl);
195 data->pdu_len -= outl;
197 memcpy(out, data->pdu, data->pdu_len);
199 if (!data->peekmode) {
205 ret = MBEDTLS_ERR_SSL_WANT_READ;
219coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
220 size_t send_buffer_length) {
225 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
228#if COAP_SERVER_SUPPORT
237 send_buffer, send_buffer_length);
238 if (result != (ssize_t)send_buffer_length) {
240 result, send_buffer_length);
245 m_env->last_timeout = now;
253#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
258psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
259 const unsigned char *identity,
size_t identity_len) {
262 coap_mbedtls_env_t *m_env;
266 if (c_session == NULL)
270 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
271 lidentity.
length = identity ? identity_len : 0;
275 (
int)lidentity.
length, (
const char *)lidentity.
s);
277 m_env = (coap_mbedtls_env_t *)c_session->
tls;
292 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
293 m_env->seen_client_hello = 1;
299get_san_or_cn_from_cert(mbedtls_x509_crt *crt) {
301 const mbedtls_asn1_named_data *cn_data;
303 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
304 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
305 while (seq && seq->buf.p == NULL) {
310 return mbedtls_strndup((
const char *)seq->buf.p,
315 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
317 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
320 return mbedtls_strndup((
const char *)cn_data->val.p,
327#if COAP_MAX_LOGGING_LEVEL > 0
329get_error_string(
int ret) {
330 static char buf[128] = {0};
331 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
337self_signed_cert_verify_callback_mbedtls(
void *data,
342 const coap_mbedtls_context_t *m_context =
346 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
347 if (setup_data->allow_expired_certs) {
348 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
359cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
360 int depth, uint32_t *flags) {
362 coap_mbedtls_context_t *m_context =
370 cn = get_san_or_cn_from_cert(crt);
372 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
373 if (setup_data->allow_expired_certs) {
374 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
377 "The certificate has expired", cn ? cn :
"?", depth);
380 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
381 if (setup_data->allow_expired_certs) {
382 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
385 "The certificate has a future date", cn ? cn :
"?", depth);
388 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
389 if (setup_data->allow_bad_md_hash) {
390 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
393 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
396 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
397 if (setup_data->allow_short_rsa_length) {
398 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
401 "The certificate has a short RSA length", cn ? cn :
"?", depth);
404 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
406 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
407 self_signed_cert_verify_callback_mbedtls,
409 if (self_signed && depth == 0) {
410 if (setup_data->allow_self_signed &&
411 !setup_data->check_common_ca) {
412 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
416 cn ? cn :
"?", depth);
419 if (!setup_data->verify_peer_cert) {
420 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
423 "The certificate's CA does not match", cn ? cn :
"?", depth);
427 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
428 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
429 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
432 "The certificate's CRL has expired", cn ? cn :
"?", depth);
433 }
else if (!setup_data->check_cert_revocation) {
434 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
437 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
438 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
439 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
442 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
443 }
else if (!setup_data->check_cert_revocation) {
444 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
447 if (setup_data->cert_chain_validation &&
448 depth > (setup_data->cert_chain_verify_depth + 1)) {
449 *flags |= MBEDTLS_X509_BADCERT_OTHER;
452 "The certificate's verify depth is too long",
453 cn ? cn :
"?", depth);
456 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
457 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
459 if (setup_data->validate_cn_call_back) {
460 if (!setup_data->validate_cn_call_back(cn,
466 setup_data->cn_call_back_arg)) {
467 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
473 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
476 tcp = strchr(buf,
'\n');
481 buf, *flags, cn ? cn :
"?", depth);
482 tcp = strchr(tcp+1,
'\n');
485 coap_log_err(
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
486 -ret, get_error_string(ret));
497setup_pki_credentials(mbedtls_x509_crt *cacert,
498 mbedtls_x509_crt *public_cert,
499 mbedtls_pk_context *private_key,
500 coap_mbedtls_env_t *m_env,
501 coap_mbedtls_context_t *m_context,
508 coap_log_err(
"RPK Support not available in Mbed TLS\n");
518 mbedtls_x509_crt_init(public_cert);
519 mbedtls_pk_init(private_key);
521 ret = mbedtls_x509_crt_parse_file(public_cert,
524 coap_log_err(
"mbedtls_x509_crt_parse_file returned -0x%x: '%s'\n",
525 -ret, get_error_string(ret));
529#ifdef MBEDTLS_2_X_COMPAT
530 ret = mbedtls_pk_parse_keyfile(private_key,
533 ret = mbedtls_pk_parse_keyfile(private_key,
535 NULL, coap_rng, (
void *)&m_env->ctr_drbg);
538 coap_log_err(
"mbedtls_pk_parse_keyfile returned -0x%x: '%s'\n",
539 -ret, get_error_string(ret));
543 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
545 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
546 -ret, get_error_string(ret));
550 coap_log_err(
"***setup_pki: (D)TLS: No Server Certificate + Private "
557 mbedtls_x509_crt_init(cacert);
558 ret = mbedtls_x509_crt_parse_file(cacert,
561 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
562 -ret, get_error_string(ret));
565 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
576 mbedtls_x509_crt_init(public_cert);
577 mbedtls_pk_init(private_key);
582 buffer = mbedtls_malloc(length + 1);
585 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
588 buffer[length] =
'\000';
590 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
591 mbedtls_free(buffer);
593 ret = mbedtls_x509_crt_parse(public_cert,
598 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
599 -ret, get_error_string(ret));
606 buffer = mbedtls_malloc(length + 1);
609 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
612 buffer[length] =
'\000';
614#ifdef MBEDTLS_2_X_COMPAT
615 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
617 ret = mbedtls_pk_parse_key(private_key, buffer, length,
618 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
620 mbedtls_free(buffer);
622#ifdef MBEDTLS_2_X_COMPAT
623 ret = mbedtls_pk_parse_key(private_key,
627 ret = mbedtls_pk_parse_key(private_key,
630 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
634 coap_log_err(
"mbedtls_pk_parse_key returned -0x%x: '%s'\n",
635 -ret, get_error_string(ret));
639 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
641 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
642 -ret, get_error_string(ret));
646 coap_log_err(
"***setup_pki: (D)TLS: No Server Certificate + Private "
656 mbedtls_x509_crt_init(cacert);
660 buffer = mbedtls_malloc(length + 1);
663 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
666 buffer[length] =
'\000';
668 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
669 mbedtls_free(buffer);
671 ret = mbedtls_x509_crt_parse(cacert,
676 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
677 -ret, get_error_string(ret));
680 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
689 mbedtls_x509_crt_init(public_cert);
690 mbedtls_pk_init(private_key);
691 ret = mbedtls_x509_crt_parse(public_cert,
695 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
696 -ret, get_error_string(ret));
700#ifdef MBEDTLS_2_X_COMPAT
701 ret = mbedtls_pk_parse_key(private_key,
705 ret = mbedtls_pk_parse_key(private_key,
708 (
void *)&m_env->ctr_drbg);
711 coap_log_err(
"mbedtls_pk_parse_key returned -0x%x: '%s'\n",
712 -ret, get_error_string(ret));
716 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
718 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
719 -ret, get_error_string(ret));
723 coap_log_err(
"***setup_pki: (D)TLS: No Server Certificate + Private "
730 mbedtls_x509_crt_init(cacert);
731 ret = mbedtls_x509_crt_parse(cacert,
735 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
736 -ret, get_error_string(ret));
739 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
744 coap_log_err(
"***setup_pki: (D)TLS: PKCS11 not currently supported\n");
748 coap_log_err(
"***setup_pki: (D)TLS: Unknown key type %d\n",
753 if (m_context->root_ca_file) {
754 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
756 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
757 -ret, get_error_string(ret));
760 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
762 if (m_context->root_ca_path) {
763 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
765 coap_log_err(
"mbedtls_x509_crt_parse returned -0x%x: '%s'\n",
766 -ret, get_error_string(ret));
769 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
772#if defined(MBEDTLS_SSL_SRV_C)
773 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
775 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
776 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
779 MBEDTLS_SSL_VERIFY_REQUIRED :
780 MBEDTLS_SSL_VERIFY_NONE);
785 mbedtls_ssl_conf_verify(&m_env->conf,
786 cert_verify_callback_mbedtls, c_session);
791#if defined(MBEDTLS_SSL_SRV_C)
796pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
797 const unsigned char *uname,
size_t name_len) {
801 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
802 coap_mbedtls_context_t *m_context =
807 name = mbedtls_malloc(name_len+1);
811 memcpy(name, uname, name_len);
812 name[name_len] =
'\000';
815 for (i = 0; i < m_context->pki_sni_count; i++) {
816 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
820 if (i == m_context->pki_sni_count) {
825 pki_sni_entry *pki_sni_entry_list;
828 m_context->setup_data.validate_sni_call_back(name,
829 m_context->setup_data.sni_call_back_arg);
835 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
836 (i+1)*
sizeof(pki_sni_entry));
838 if (pki_sni_entry_list == NULL) {
842 m_context->pki_sni_entry_list = pki_sni_entry_list;
843 memset(&m_context->pki_sni_entry_list[i], 0,
844 sizeof(m_context->pki_sni_entry_list[i]));
845 m_context->pki_sni_entry_list[i].sni = name;
846 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
847 sni_setup_data = m_context->setup_data;
848 sni_setup_data.
pki_key = *new_entry;
849 if ((ret = setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
850 &m_context->pki_sni_entry_list[i].public_cert,
851 &m_context->pki_sni_entry_list[i].private_key,
860 m_context->pki_sni_count++;
865 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
867 return mbedtls_ssl_set_hs_own_cert(ssl,
868 &m_context->pki_sni_entry_list[i].public_cert,
869 &m_context->pki_sni_entry_list[i].private_key);
872#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
877psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
878 const unsigned char *uname,
size_t name_len) {
881 coap_mbedtls_context_t *m_context =
885 name = mbedtls_malloc(name_len+1);
889 memcpy(name, uname, name_len);
890 name[name_len] =
'\000';
893 for (i = 0; i < m_context->psk_sni_count; i++) {
894 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
898 if (i == m_context->psk_sni_count) {
903 psk_sni_entry *psk_sni_entry_list;
914 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
915 (i+1)*
sizeof(psk_sni_entry));
917 if (psk_sni_entry_list == NULL) {
921 m_context->psk_sni_entry_list = psk_sni_entry_list;
922 m_context->psk_sni_entry_list[i].sni = name;
923 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
925 m_context->psk_sni_count++;
931 &m_context->psk_sni_entry_list[i].psk_info.hint);
933 &m_context->psk_sni_entry_list[i].psk_info.key);
934 return mbedtls_ssl_set_hs_psk(ssl,
935 m_context->psk_sni_entry_list[i].psk_info.key.s,
936 m_context->psk_sni_entry_list[i].psk_info.key.length);
942 coap_mbedtls_env_t *m_env) {
943 coap_mbedtls_context_t *m_context =
946 m_context->psk_pki_enabled |= IS_SERVER;
948 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
949 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
950 MBEDTLS_SSL_IS_SERVER,
952 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
953 MBEDTLS_SSL_TRANSPORT_STREAM,
954 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
955 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
956 -ret, get_error_string(ret));
960 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
962#if defined(MBEDTLS_SSL_PROTO_DTLS)
963 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
964 COAP_DTLS_RETRANSMIT_TOTAL_MS);
967 if (m_context->psk_pki_enabled & IS_PSK) {
968#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
969 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
971 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
978 if (m_context->psk_pki_enabled & IS_PKI) {
979 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
980 &m_env->private_key, m_env, m_context,
981 c_session, &m_context->setup_data,
987 if (m_context->setup_data.validate_sni_call_back) {
988 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
992 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
993 mbedtls_ctr_drbg_random,
994 &m_env->ctr_drbg)) != 0) {
995 coap_log_err(
"mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
996 -ret, get_error_string(ret));
1000#if defined(MBEDTLS_SSL_PROTO_DTLS)
1001 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1002 mbedtls_ssl_cookie_check,
1003 &m_env->cookie_ctx);
1004#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1005 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1008#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1016 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1023#if COAP_CLIENT_SUPPORT
1024static int *psk_ciphers = NULL;
1025static int *pki_ciphers = NULL;
1026static int processed_ciphers = 0;
1029set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1030 if (!processed_ciphers) {
1031 const int *list = mbedtls_ssl_list_ciphersuites();
1032 const int *base = list;
1039 const mbedtls_ssl_ciphersuite_t *cur =
1040 mbedtls_ssl_ciphersuite_from_id(*list);
1042#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1044 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1049 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1053#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1054 else if (mbedtls_ssl_ciphersuite_uses_psk(cur)) {
1066 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
1067 if (psk_ciphers == NULL) {
1068 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1071 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
1072 if (pki_ciphers == NULL) {
1073 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1074 mbedtls_free(psk_ciphers);
1079 psk_list = psk_ciphers;
1080 pki_list = pki_ciphers;
1083 const mbedtls_ssl_ciphersuite_t *cur =
1084 mbedtls_ssl_ciphersuite_from_id(*list);
1085#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1087 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1092 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1096#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1097 else if (mbedtls_ssl_ciphersuite_uses_psk(cur)) {
1112 processed_ciphers = 1;
1114 mbedtls_ssl_conf_ciphersuites(conf, method == COAP_ENC_PSK ? psk_ciphers : pki_ciphers);
1119 coap_mbedtls_env_t *m_env) {
1122 coap_mbedtls_context_t *m_context =
1125 m_context->psk_pki_enabled |= IS_CLIENT;
1127 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1128 MBEDTLS_SSL_IS_CLIENT,
1130 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1131 MBEDTLS_SSL_TRANSPORT_STREAM,
1132 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1133 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1134 -ret, get_error_string(ret));
1138#if defined(MBEDTLS_SSL_PROTO_DTLS)
1139 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1140 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1143 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1144 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1146 if (m_context->psk_pki_enabled & IS_PSK) {
1147#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1155 if (psk_key == NULL || psk_identity == NULL) {
1156 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1160 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->
s,
1161 psk_key->
length, psk_identity->
s,
1162 psk_identity->
length)) != 0) {
1163 coap_log_err(
"mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1164 -ret, get_error_string(ret));
1168 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1170 coap_log_err(
"mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1171 -ret, get_error_string(ret));
1177 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1181 }
else if ((m_context->psk_pki_enabled & IS_PKI) ||
1182 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1187 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1188 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1189 &m_env->private_key, m_env, m_context,
1190 c_session, &m_context->setup_data,
1196#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1199 static const char *alpn_list[] = {
"coap", NULL };
1201 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1207 if (m_context->setup_data.client_sni) {
1208 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1210#if defined(MBEDTLS_SSL_PROTO_DTLS)
1211#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1212 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1215 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1225mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1230 mbedtls_x509_crt_free(&m_env->cacert);
1231 mbedtls_x509_crt_free(&m_env->public_cert);
1232 mbedtls_pk_free(&m_env->private_key);
1233 mbedtls_entropy_free(&m_env->entropy);
1234 mbedtls_ssl_config_free(&m_env->conf);
1235 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1236 mbedtls_ssl_free(&m_env->ssl);
1237 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1241coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1243 if (!m_env->sent_alert)
1244 mbedtls_ssl_close_notify(&m_env->ssl);
1245 mbedtls_cleanup(m_env);
1246 mbedtls_free(m_env);
1250#if COAP_MAX_LOGGING_LEVEL > 0
1252report_mbedtls_alert(
unsigned char alert) {
1254 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1255 return ": Bad Record MAC";
1256 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1257 return ": Handshake failure";
1258 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1259 return ": No Certificate provided";
1260 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1261 return ": Certificate is bad";
1262 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1263 return ": Certificate is unknown";
1264 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1265 return ": CA is unknown";
1266 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1267 return ": Access was denied";
1268 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1269 return ": Decrypt error";
1283 coap_mbedtls_env_t *m_env) {
1287 ret = mbedtls_ssl_handshake(&m_env->ssl);
1290 m_env->established = 1;
1295 case MBEDTLS_ERR_SSL_WANT_READ:
1296 case MBEDTLS_ERR_SSL_WANT_WRITE:
1300 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1303 case MBEDTLS_ERR_SSL_INVALID_MAC:
1305#ifdef MBEDTLS_2_X_COMPAT
1306 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1308 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1311 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1312 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1314#ifdef MBEDTLS_2_X_COMPAT
1315 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1316 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1317 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1320 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1322 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1323 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1326 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1328 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1329 case MBEDTLS_ERR_SSL_CONN_EOF:
1330 case MBEDTLS_ERR_NET_CONN_RESET:
1336 "returned -0x%x: '%s'\n",
1337 -ret, get_error_string(ret));
1344 mbedtls_ssl_send_alert_message(&m_env->ssl,
1345 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1347 m_env->sent_alert = 1;
1352 get_error_string(ret));
1354 mbedtls_ssl_session_reset(&m_env->ssl);
1359mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1392#if !COAP_DISABLE_TCP
1400coap_sock_read(
void *ctx,
unsigned char *out,
size_t outl) {
1401 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
1408 if (errno == ECONNRESET) {
1410 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1412 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1414 }
else if (ret == 0) {
1416 ret = MBEDTLS_ERR_SSL_WANT_READ;
1429coap_sock_write(
void *context,
const unsigned char *in,
size_t inl) {
1434 (
const uint8_t *)in,
1440 (errno == EPIPE || errno == ECONNRESET)) {
1455 int lasterror = WSAGetLastError();
1457 if (lasterror == WSAEWOULDBLOCK) {
1458 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1459 }
else if (lasterror == WSAECONNRESET) {
1460 ret = MBEDTLS_ERR_NET_CONN_RESET;
1463 if (errno == EAGAIN || errno == EINTR) {
1464 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1465 }
else if (errno == EPIPE || errno == ECONNRESET) {
1466 ret = MBEDTLS_ERR_NET_CONN_RESET;
1470 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1479 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1485static coap_mbedtls_env_t *
1490 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1495 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1499 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1501 mbedtls_ssl_init(&m_env->ssl);
1502 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1503 mbedtls_ssl_config_init(&m_env->conf);
1504 mbedtls_entropy_init(&m_env->entropy);
1506#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1507 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1509 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1510 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1511 coap_log_err(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1512 -ret, get_error_string(ret));
1517#if COAP_CLIENT_SUPPORT
1518 if (setup_client_ssl_session(c_session, m_env) != 0) {
1525#if defined(MBEDTLS_SSL_SRV_C)
1526 if (setup_server_ssl_session(c_session, m_env) != 0) {
1536#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1537 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1539 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1540 MBEDTLS_SSL_MINOR_VERSION_3);
1543 if ((ret = mbedtls_ssl_setup(&m_env->ssl, &m_env->conf)) != 0) {
1547 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1548 coap_dgram_read, NULL);
1549#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1552 u_char cid[COAP_DTLS_CID_LENGTH];
1561 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
1566#if !COAP_DISABLE_TCP
1569 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1570 coap_sock_read, NULL);
1573 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1574 mbedtls_timing_set_delay,
1575 mbedtls_timing_get_delay);
1577 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1582 mbedtls_free(m_env);
1589#if defined(MBEDTLS_SSL_PROTO_DTLS)
1592 static int reported = 0;
1596 " - update Mbed TLS to include DTLS\n");
1604#if !COAP_DISABLE_TCP
1649 coap_mbedtls_context_t *m_context;
1652 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
1654 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
1659#if COAP_SERVER_SUPPORT
1668 coap_mbedtls_context_t *m_context =
1671#if !defined(MBEDTLS_SSL_SRV_C)
1673 " libcoap not compiled for Server Mode for Mbed TLS"
1674 " - update Mbed TLS to include Server Mode\n");
1677 if (!m_context || !setup_data)
1680 m_context->psk_pki_enabled |= IS_PSK;
1685#if COAP_CLIENT_SUPPORT
1694#if !defined(MBEDTLS_SSL_CLI_C)
1699 " libcoap not compiled for Client Mode for Mbed TLS"
1700 " - update Mbed TLS to include Client Mode\n");
1703 coap_mbedtls_context_t *m_context =
1706 if (!m_context || !setup_data)
1710 coap_log_warn(
"CoAP Client with Mbed TLS does not support Identity Hint selection\n");
1712 m_context->psk_pki_enabled |= IS_PSK;
1722 coap_mbedtls_context_t *m_context =
1725 m_context->setup_data = *setup_data;
1726 if (!m_context->setup_data.verify_peer_cert) {
1730 m_context->setup_data.allow_self_signed = 1;
1731 m_context->setup_data.allow_expired_certs = 1;
1732 m_context->setup_data.cert_chain_validation = 1;
1733 m_context->setup_data.cert_chain_verify_depth = 10;
1734 m_context->setup_data.check_cert_revocation = 1;
1735 m_context->setup_data.allow_no_crl = 1;
1736 m_context->setup_data.allow_expired_crl = 1;
1737 m_context->setup_data.allow_bad_md_hash = 1;
1738 m_context->setup_data.allow_short_rsa_length = 1;
1740 m_context->psk_pki_enabled |= IS_PKI;
1746 const char *ca_file,
1747 const char *ca_path) {
1748 coap_mbedtls_context_t *m_context =
1752 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
1757 if (ca_file == NULL && ca_path == NULL) {
1758 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
1762 if (m_context->root_ca_file) {
1763 mbedtls_free(m_context->root_ca_file);
1764 m_context->root_ca_file = NULL;
1768 m_context->root_ca_file = mbedtls_strdup(ca_file);
1771 if (m_context->root_ca_path) {
1772 mbedtls_free(m_context->root_ca_path);
1773 m_context->root_ca_path = NULL;
1777 m_context->root_ca_path = mbedtls_strdup(ca_path);
1784 coap_mbedtls_context_t *m_context =
1786 return m_context->psk_pki_enabled ? 1 : 0;
1791 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
1794 for (i = 0; i < m_context->pki_sni_count; i++) {
1795 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
1797 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
1799 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
1801 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
1803 if (m_context->pki_sni_entry_list)
1804 mbedtls_free(m_context->pki_sni_entry_list);
1806 for (i = 0; i < m_context->psk_sni_count; i++) {
1807 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
1809 if (m_context->psk_sni_entry_list)
1810 mbedtls_free(m_context->psk_sni_entry_list);
1812 if (m_context->root_ca_path)
1813 mbedtls_free(m_context->root_ca_path);
1814 if (m_context->root_ca_file)
1815 mbedtls_free(m_context->root_ca_file);
1817 mbedtls_free(m_context);
1820#if COAP_CLIENT_SUPPORT
1823#if !defined(MBEDTLS_SSL_CLI_C)
1826 " libcoap not compiled for Client Mode for Mbed TLS"
1827 " - update Mbed TLS to include Client Mode\n");
1830 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
1837#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1846 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
1850 m_env->last_timeout = now;
1851 ret = do_mbedtls_handshake(c_session, m_env);
1853 coap_dtls_free_mbedtls_env(m_env);
1862#if COAP_SERVER_SUPPORT
1865#if !defined(MBEDTLS_SSL_SRV_C)
1868 " libcoap not compiled for Server Mode for Mbed TLS"
1869 " - update Mbed TLS to include Server Mode\n");
1872 coap_mbedtls_env_t *m_env =
1873 (coap_mbedtls_env_t *)c_session->
tls;
1875#if defined(MBEDTLS_SSL_PROTO_DTLS)
1876#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1877 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1888 if (c_session && c_session->
context && c_session->
tls) {
1889 coap_dtls_free_mbedtls_env(c_session->
tls);
1890 c_session->
tls = NULL;
1898#if defined(MBEDTLS_SSL_PROTO_DTLS)
1899 coap_mbedtls_env_t *m_env =
1900 (coap_mbedtls_env_t *)c_session->
tls;
1902#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1903 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1913 const uint8_t *data,
size_t data_len) {
1915 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1917 assert(m_env != NULL);
1923 if (m_env->established) {
1924 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char *) data, data_len);
1927 case MBEDTLS_ERR_SSL_WANT_READ:
1928 case MBEDTLS_ERR_SSL_WANT_WRITE:
1931 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1937 "returned -0x%x: '%s'\n",
1938 -ret, get_error_string(ret));
1947 ret = do_mbedtls_handshake(c_session, m_env);
1966 if (ret == (ssize_t)data_len)
1988 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1989 int ret = mbedtls_timing_get_delay(&m_env->timer);
1990 unsigned int scalar = 1 << m_env->retry_scalar;
2000 m_env->last_timeout = now;
2013 m_env->last_timeout = now;
2031 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2034 m_env->retry_scalar++;
2036 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2051 const uint8_t *data,
2056 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2057 coap_ssl_t *ssl_data;
2059 assert(m_env != NULL);
2061 ssl_data = &m_env->coap_ssl_data;
2062 if (ssl_data->pdu_len) {
2063 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2066 ssl_data->pdu = data;
2067 ssl_data->pdu_len = (unsigned)data_len;
2069 if (m_env->established) {
2070#if COAP_CONSTRAINED_STACK
2077#if COAP_CONSTRAINED_STACK
2078 coap_mutex_lock(&m_dtls_recv);
2087 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
2090#if COAP_CONSTRAINED_STACK
2091 coap_mutex_unlock(&m_dtls_recv);
2097 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2098 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2101 case MBEDTLS_ERR_SSL_WANT_READ:
2105 "returned -0x%x: '%s' (length %zd)\n",
2106 -ret, get_error_string(ret), data_len);
2109#if COAP_CONSTRAINED_STACK
2110 coap_mutex_unlock(&m_dtls_recv);
2114 ret = do_mbedtls_handshake(c_session, m_env);
2119 if (ssl_data->pdu_len) {
2121 ret = do_mbedtls_handshake(c_session, m_env);
2144 if (ssl_data && ssl_data->pdu_len) {
2146 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2147 ssl_data->pdu_len = 0;
2148 ssl_data->pdu = NULL;
2157#if COAP_SERVER_SUPPORT
2165 const uint8_t *data,
2167#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2172 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2173 " - update Mbed TLS to include DTLS and Server Mode\n");
2176 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2177 coap_ssl_t *ssl_data;
2184 c_session->
tls = m_env;
2191 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2194 coap_log_err(
"mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2195 -ret, get_error_string(ret));
2199 ssl_data = &m_env->coap_ssl_data;
2200 if (ssl_data->pdu_len) {
2201 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2204 ssl_data->pdu = data;
2205 ssl_data->pdu_len = (unsigned)data_len;
2207 ret = do_mbedtls_handshake(c_session, m_env);
2208 if (ret == 0 || m_env->seen_client_hello) {
2214 m_env->seen_client_hello = 0;
2220 if (ssl_data->pdu_len) {
2222 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2223 ssl_data->pdu_len = 0;
2224 ssl_data->pdu = NULL;
2233 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2234 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2236 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2242#if !COAP_DISABLE_TCP
2243#if COAP_CLIENT_SUPPORT
2246#if !defined(MBEDTLS_SSL_CLI_C)
2250 " libcoap not compiled for Client Mode for Mbed TLS"
2251 " - update Mbed TLS to include Client Mode\n");
2254 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2264 m_env->last_timeout = now;
2265 c_session->
tls = m_env;
2266 ret = do_mbedtls_handshake(c_session, m_env);
2276#if COAP_SERVER_SUPPORT
2279#if !defined(MBEDTLS_SSL_SRV_C)
2284 " libcoap not compiled for Server Mode for Mbed TLS"
2285 " - update Mbed TLS to include Server Mode\n");
2288 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2296 c_session->
tls = m_env;
2297 ret = do_mbedtls_handshake(c_session, m_env);
2322 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2323 size_t amount_sent = 0;
2325 assert(m_env != NULL);
2332 if (m_env->established) {
2333 while (amount_sent < data_len) {
2334 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
2335 data_len - amount_sent);
2338 case MBEDTLS_ERR_SSL_WANT_READ:
2339 case MBEDTLS_ERR_SSL_WANT_WRITE:
2346 case MBEDTLS_ERR_NET_CONN_RESET:
2347 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2352 "returned -0x%x: '%s'\n",
2353 -ret, get_error_string(ret));
2365 ret = do_mbedtls_handshake(c_session, m_env);
2386 if (ret == (ssize_t)data_len)
2405 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2414 if (!m_env->established && !m_env->sent_alert) {
2415 ret = do_mbedtls_handshake(c_session, m_env);
2424 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2428 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2432 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2434 m_env->sent_alert = 1;
2437 case MBEDTLS_ERR_SSL_WANT_READ:
2443 "returned -0x%x: '%s' (length %zd)\n",
2444 -ret, get_error_string(ret), data_len);
2448 }
else if (ret < (
int)data_len) {
2449 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
2477#if COAP_CLIENT_SUPPORT
2478 mbedtls_free(psk_ciphers);
2479 mbedtls_free(pki_ciphers);
2482 processed_ciphers = 0;
2492 if (c_session && c_session->
tls) {
2493 coap_mbedtls_env_t *m_env;
2496 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
2498 return (
void *)&m_env->ssl;
2507#if !defined(ESPIDF_VERSION)
2517 switch ((
int)level) {
2538 mbedtls_debug_set_threshold(use_level);
2540 keep_log_level = level;
2545 return keep_log_level;
2551 version.
version = mbedtls_version_get_number();
2557#if COAP_SERVER_SUPPORT
2560 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2563 mbedtls_sha256_init(digest_ctx);
2564#ifdef MBEDTLS_2_X_COMPAT
2565 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2567 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2577 mbedtls_sha256_free(digest_ctx);
2578 mbedtls_free(digest_ctx);
2583 const uint8_t *data,
2585#ifdef MBEDTLS_2_X_COMPAT
2586 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2588 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2597#ifdef MBEDTLS_2_X_COMPAT
2598 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t *)digest_buffer);
2600 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t *)digest_buffer);
2608#include <mbedtls/cipher.h>
2609#include <mbedtls/md.h>
2611#ifndef MBEDTLS_CIPHER_MODE_AEAD
2612#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
2615#ifdef MBEDTLS_ERROR_C
2616#include <mbedtls/error.h>
2619#ifdef MBEDTLS_ERROR_C
2622 int c_tmp = (int)(Func); \
2624 char error_buf[64]; \
2625 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
2626 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
2633 int c_tmp = (int)(Func); \
2635 coap_log_err("mbedtls: %d\n", tmp); \
2646static struct hash_algs {
2648 mbedtls_md_type_t hash_type;
2656static mbedtls_md_type_t
2657get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
2660 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2661 if (hashs[idx].alg == alg) {
2662 *hash_len = hashs[idx].hash_size;
2663 return hashs[idx].hash_type;
2666 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2667 return MBEDTLS_MD_NONE;
2674 mbedtls_md_context_t ctx;
2676 const mbedtls_md_info_t *md_info;
2680 mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length);
2682 if (dig_type == MBEDTLS_MD_NONE) {
2683 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2686 md_info = mbedtls_md_info_from_type(dig_type);
2688 len = mbedtls_md_get_size(md_info);
2693 mbedtls_md_init(&ctx);
2694 C(mbedtls_md_setup(&ctx, md_info, 0));
2696 C(mbedtls_md_starts(&ctx));
2697 C(mbedtls_md_update(&ctx, (
const unsigned char *)data->
s, data->
length));
2701 C(mbedtls_md_finish(&ctx,
dummy->s));
2706 mbedtls_md_free(&ctx);
2711#if COAP_OSCORE_SUPPORT
2722static struct cipher_algs {
2724 mbedtls_cipher_type_t cipher_type;
2729static mbedtls_cipher_type_t
2733 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
2734 if (ciphers[idx].alg == alg)
2735 return ciphers[idx].cipher_type;
2737 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
2746static struct hmac_algs {
2748 mbedtls_md_type_t hmac_type;
2755static mbedtls_md_type_t
2759 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
2760 if (hmacs[idx].hmac_alg == hmac_alg)
2761 return hmacs[idx].hmac_type;
2763 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
2769 return get_cipher_alg(alg) != 0;
2778 return get_hmac_alg(hmac_alg) != 0;
2786setup_cipher_context(mbedtls_cipher_context_t *ctx,
2788 const uint8_t *key_data,
2790 mbedtls_operation_t mode) {
2791 const mbedtls_cipher_info_t *cipher_info;
2792 mbedtls_cipher_type_t cipher_type;
2796 memset(key, 0,
sizeof(key));
2798 if ((cipher_type = get_cipher_alg(coap_alg)) == 0) {
2799 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
2803 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
2805 coap_log_crit(
"coap_crypto_encrypt: cannot get cipher info\n");
2809 mbedtls_cipher_init(ctx);
2811 C(mbedtls_cipher_setup(ctx, cipher_info));
2812 klen = mbedtls_cipher_get_key_bitlen(ctx);
2813 if ((klen > (
int)(
sizeof(key) * 8)) || (key_length >
sizeof(key))) {
2817 memcpy(key, key_data, key_length);
2818 C(mbedtls_cipher_setkey(ctx, key, klen, mode));
2823 mbedtls_cipher_free(ctx);
2832 size_t *max_result_len) {
2833 mbedtls_cipher_context_t ctx;
2835#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
2836 unsigned char tag[16];
2839 size_t result_len = *max_result_len;
2845 assert(params != NULL);
2852 if (!setup_cipher_context(&ctx,
2867#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
2868 C(mbedtls_cipher_auth_encrypt(&ctx,
2881 if ((result_len + ccm->
tag_len) > *max_result_len) {
2886 memcpy(result + result_len, tag, ccm->
tag_len);
2887 *max_result_len = result_len + ccm->
tag_len;
2890 C(mbedtls_cipher_auth_encrypt_ext(&ctx,
2902 *max_result_len = result_len;
2907 mbedtls_cipher_free(&ctx);
2916 size_t *max_result_len) {
2917 mbedtls_cipher_context_t ctx;
2919#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
2920 const unsigned char *tag;
2923 size_t result_len = *max_result_len;
2929 assert(params != NULL);
2937 if (!setup_cipher_context(&ctx,
2957#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
2959 C(mbedtls_cipher_auth_decrypt(&ctx,
2972 C(mbedtls_cipher_auth_decrypt_ext(&ctx,
2987 *max_result_len = result_len;
2990 mbedtls_cipher_free(&ctx);
2999 mbedtls_md_context_t ctx;
3001 const int use_hmac = 1;
3002 const mbedtls_md_info_t *md_info;
3003 mbedtls_md_type_t mac_algo;
3011 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3012 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3015 md_info = mbedtls_md_info_from_type(mac_algo);
3017 len = mbedtls_md_get_size(md_info);
3022 mbedtls_md_init(&ctx);
3023 C(mbedtls_md_setup(&ctx, md_info, use_hmac));
3025 C(mbedtls_md_hmac_starts(&ctx, key->
s, key->
length));
3026 C(mbedtls_md_hmac_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3030 C(mbedtls_md_hmac_finish(&ctx,
dummy->s));
3035 mbedtls_md_free(&ctx);
3047#pragma GCC diagnostic ignored "-Wunused-function"
Pulls together all the internal only header files.
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
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(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
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.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
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 *session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
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_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
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_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
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_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_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_PKCS11
The PKI key type is PKCS11 (DER)
@ COAP_PKI_KEY_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_ASN1
The PKI key type is ASN.1 (DER) buffer.
@ 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_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
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
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).
#define COAP_PROTO_NOT_RELIABLE(p)
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ 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_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.
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.
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.
The structure that holds the PKI key information.
coap_pki_key_pem_t pem
for PEM file keys
union coap_dtls_key_t::@3 key
coap_pki_key_pem_buf_t pem_buf
for PEM memory keys
coap_pki_key_t key_type
key format type
coap_pki_key_asn1_t asn1
for ASN.1 (DER) memory keys
The structure used for defining the PKI setup data to be used.
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
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.
void * sni_call_back_arg
Passed in to the SNI callback function.
coap_layer_write_t l_write
coap_layer_establish_t l_establish
const uint8_t * private_key
ASN1 (DER) Private Key.
size_t public_cert_len
ASN1 Public Cert length.
size_t private_key_len
ASN1 Private Key length.
const uint8_t * ca_cert
ASN1 (DER) Common CA Cert.
size_t ca_cert_len
ASN1 CA Cert length.
const uint8_t * public_cert
ASN1 (DER) Public Cert, or Public Key if RPK.
size_t ca_cert_len
PEM buffer CA Cert length.
const uint8_t * ca_cert
PEM buffer Common CA Cert.
size_t private_key_len
PEM buffer Private Key length.
const uint8_t * private_key
PEM buffer Private Key If RPK and 'EC PRIVATE KEY' this can be used for both the public_cert and priv...
size_t public_cert_len
PEM buffer Public Cert length.
const uint8_t * public_cert
PEM buffer Public Cert, or Public Key if RPK.
const char * ca_file
File location of Common CA in PEM format.
const char * public_cert
File location of Public Cert.
const char * private_key
File location of Private Key in PEM format.
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_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)
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_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