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;
140 uint32_t server_hello_cnt;
143typedef struct pki_sni_entry {
146 mbedtls_x509_crt cacert;
147 mbedtls_x509_crt public_cert;
148 mbedtls_pk_context private_key;
151typedef struct psk_sni_entry {
156typedef struct coap_mbedtls_context_t {
158 size_t pki_sni_count;
159 pki_sni_entry *pki_sni_entry_list;
160 size_t psk_sni_count;
161 psk_sni_entry *psk_sni_entry_list;
165} coap_mbedtls_context_t;
167typedef enum coap_enc_method_t {
173#ifndef MBEDTLS_2_X_COMPAT
178coap_rng(
void *ctx
COAP_UNUSED,
unsigned char *buf,
size_t len) {
179 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
184coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl) {
189 if (!c_session->
tls) {
191 return MBEDTLS_ERR_SSL_WANT_READ;
193 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
196 if (data->pdu_len > 0) {
197 if (outl < data->pdu_len) {
198 memcpy(out, data->pdu, outl);
201 data->pdu_len -= outl;
203 memcpy(out, data->pdu, data->pdu_len);
205 if (!data->peekmode) {
211 ret = MBEDTLS_ERR_SSL_WANT_READ;
225coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
226 size_t send_buffer_length) {
231 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
234#if COAP_SERVER_SUPPORT
243 send_buffer, send_buffer_length);
244 if (result != (ssize_t)send_buffer_length) {
245 int keep_errno = errno;
248 result, send_buffer_length);
258 m_env->last_timeout = now;
266#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
271psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
272 const unsigned char *identity,
size_t identity_len) {
275 coap_mbedtls_env_t *m_env;
279 if (c_session == NULL)
283 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
284 lidentity.
length = identity ? identity_len : 0;
288 (
int)lidentity.
length, (
const char *)lidentity.
s);
290 m_env = (coap_mbedtls_env_t *)c_session->
tls;
305 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
306 m_env->seen_client_hello = 1;
312get_san_or_cn_from_cert(mbedtls_x509_crt *crt) {
314 const mbedtls_asn1_named_data *cn_data;
316 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
317 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
318 while (seq && seq->buf.p == NULL) {
323 return mbedtls_strndup((
const char *)seq->buf.p,
328 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
330 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
333 return mbedtls_strndup((
const char *)cn_data->val.p,
340#if COAP_MAX_LOGGING_LEVEL > 0
342get_error_string(
int ret) {
343 static char buf[128] = {0};
344 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
350self_signed_cert_verify_callback_mbedtls(
void *data,
355 const coap_mbedtls_context_t *m_context =
359 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
360 if (setup_data->allow_expired_certs) {
361 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
372cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
373 int depth, uint32_t *flags) {
375 coap_mbedtls_context_t *m_context =
383 cn = get_san_or_cn_from_cert(crt);
385 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
386 if (setup_data->allow_expired_certs) {
387 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
390 "The certificate has expired", cn ? cn :
"?", depth);
393 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
394 if (setup_data->allow_expired_certs) {
395 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
398 "The certificate has a future date", cn ? cn :
"?", depth);
401 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
402 if (setup_data->allow_bad_md_hash) {
403 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
406 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
409 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
410 if (setup_data->allow_short_rsa_length) {
411 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
414 "The certificate has a short RSA length", cn ? cn :
"?", depth);
417 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
419 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
420 self_signed_cert_verify_callback_mbedtls,
422 if (self_signed && depth == 0) {
423 if (setup_data->allow_self_signed &&
424 !setup_data->check_common_ca) {
425 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
429 cn ? cn :
"?", depth);
431 }
else if (self_signed) {
432 if (!setup_data->verify_peer_cert) {
433 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
436 "Self-signed", cn ? cn :
"?", depth);
439 if (!setup_data->verify_peer_cert) {
440 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
443 "The certificate's CA is not trusted", cn ? cn :
"?", depth);
447 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
448 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
449 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
452 "The certificate's CRL has expired", cn ? cn :
"?", depth);
453 }
else if (!setup_data->check_cert_revocation) {
454 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
457 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
458 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
459 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
462 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
463 }
else if (!setup_data->check_cert_revocation) {
464 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
467 if (setup_data->cert_chain_validation &&
468 depth > (setup_data->cert_chain_verify_depth + 1)) {
469 *flags |= MBEDTLS_X509_BADCERT_OTHER;
472 "The certificate's verify depth is too long",
473 cn ? cn :
"?", depth);
476 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
477 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
479 if (setup_data->validate_cn_call_back) {
483 setup_data->validate_cn_call_back(cn,
489 setup_data->cn_call_back_arg));
491 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
497 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
500 tcp = strchr(buf,
'\n');
505 buf, *flags, cn ? cn :
"?", depth);
506 tcp = strchr(tcp+1,
'\n');
509 coap_log_err(
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
510 -ret, get_error_string(ret));
521setup_pki_credentials(mbedtls_x509_crt *cacert,
522 mbedtls_x509_crt *public_cert,
523 mbedtls_pk_context *private_key,
524 coap_mbedtls_env_t *m_env,
525 coap_mbedtls_context_t *m_context,
531 int done_private_key = 0;
532 int done_public_cert = 0;
550#if defined(MBEDTLS_FS_IO)
551 mbedtls_pk_init(private_key);
552#ifdef MBEDTLS_2_X_COMPAT
553 ret = mbedtls_pk_parse_keyfile(private_key,
556 ret = mbedtls_pk_parse_keyfile(private_key,
558 NULL, coap_rng, (
void *)&m_env->ctr_drbg);
565 done_private_key = 1;
573 mbedtls_pk_init(private_key);
577 buffer = mbedtls_malloc(length + 1);
583 buffer[length] =
'\000';
585#ifdef MBEDTLS_2_X_COMPAT
586 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
588 ret = mbedtls_pk_parse_key(private_key, buffer, length,
589 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
591 mbedtls_free(buffer);
593#ifdef MBEDTLS_2_X_COMPAT
594 ret = mbedtls_pk_parse_key(private_key,
598 ret = mbedtls_pk_parse_key(private_key,
601 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
609 done_private_key = 1;
612 mbedtls_pk_init(private_key);
613#ifdef MBEDTLS_2_X_COMPAT
614 ret = mbedtls_pk_parse_key(private_key,
618 ret = mbedtls_pk_parse_key(private_key,
621 (
void *)&m_env->ctr_drbg);
628 done_private_key = 1;
656#if defined(MBEDTLS_FS_IO)
657 mbedtls_x509_crt_init(public_cert);
658 ret = mbedtls_x509_crt_parse_file(public_cert,
665 done_public_cert = 1;
673 mbedtls_x509_crt_init(public_cert);
678 buffer = mbedtls_malloc(length + 1);
684 buffer[length] =
'\000';
686 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
687 mbedtls_free(buffer);
689 ret = mbedtls_x509_crt_parse(public_cert,
698 done_public_cert = 1;
705 mbedtls_x509_crt_init(public_cert);
706 ret = mbedtls_x509_crt_parse(public_cert,
714 done_public_cert = 1;
732 if (done_private_key && done_public_cert) {
733 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
735 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
736 -ret, get_error_string(ret));
745#
if MBEDTLS_VERSION_NUMBER < 0x03060000
754#if defined(MBEDTLS_FS_IO)
755 mbedtls_x509_crt_init(cacert);
756 ret = mbedtls_x509_crt_parse_file(cacert,
763 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
771 mbedtls_x509_crt_init(cacert);
775 buffer = mbedtls_malloc(length + 1);
781 buffer[length] =
'\000';
783 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
784 mbedtls_free(buffer);
786 ret = mbedtls_x509_crt_parse(cacert,
795 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
802 mbedtls_x509_crt_init(cacert);
803 ret = mbedtls_x509_crt_parse(cacert,
811 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
825#if defined(MBEDTLS_FS_IO)
826 if (m_context->root_ca_file) {
827 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
834 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
836 if (m_context->root_ca_path) {
837 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
844 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
853#if defined(MBEDTLS_SSL_SRV_C)
854 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
856 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
857 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
860 MBEDTLS_SSL_VERIFY_REQUIRED :
861 MBEDTLS_SSL_VERIFY_NONE);
866 mbedtls_ssl_conf_verify(&m_env->conf,
867 cert_verify_callback_mbedtls, c_session);
872#if defined(MBEDTLS_SSL_SRV_C)
877pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
878 const unsigned char *uname,
size_t name_len) {
882 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
883 coap_mbedtls_context_t *m_context =
887 name = mbedtls_malloc(name_len+1);
891 memcpy(name, uname, name_len);
892 name[name_len] =
'\000';
895 for (i = 0; i < m_context->pki_sni_count; i++) {
896 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
900 if (i == m_context->pki_sni_count) {
905 pki_sni_entry *pki_sni_entry_list;
908 m_context->setup_data.validate_sni_call_back(name,
909 m_context->setup_data.sni_call_back_arg));
915 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
916 (i+1)*
sizeof(pki_sni_entry));
918 if (pki_sni_entry_list == NULL) {
922 m_context->pki_sni_entry_list = pki_sni_entry_list;
923 memset(&m_context->pki_sni_entry_list[i], 0,
924 sizeof(m_context->pki_sni_entry_list[i]));
925 m_context->pki_sni_entry_list[i].sni = name;
926 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
927 sni_setup_data = m_context->setup_data;
928 sni_setup_data.
pki_key = *new_entry;
929 if (setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
930 &m_context->pki_sni_entry_list[i].public_cert,
931 &m_context->pki_sni_entry_list[i].private_key,
940 m_context->pki_sni_count++;
945 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
947 return mbedtls_ssl_set_hs_own_cert(ssl,
948 &m_context->pki_sni_entry_list[i].public_cert,
949 &m_context->pki_sni_entry_list[i].private_key);
952#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
957psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
958 const unsigned char *uname,
size_t name_len) {
961 coap_mbedtls_context_t *m_context =
965 name = mbedtls_malloc(name_len+1);
969 memcpy(name, uname, name_len);
970 name[name_len] =
'\000';
973 for (i = 0; i < m_context->psk_sni_count; i++) {
974 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
978 if (i == m_context->psk_sni_count) {
983 psk_sni_entry *psk_sni_entry_list;
994 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
995 (i+1)*
sizeof(psk_sni_entry));
997 if (psk_sni_entry_list == NULL) {
1001 m_context->psk_sni_entry_list = psk_sni_entry_list;
1002 m_context->psk_sni_entry_list[i].sni = name;
1003 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1005 m_context->psk_sni_count++;
1011 &m_context->psk_sni_entry_list[i].psk_info.hint);
1013 &m_context->psk_sni_entry_list[i].psk_info.key);
1014 return mbedtls_ssl_set_hs_psk(ssl,
1015 m_context->psk_sni_entry_list[i].psk_info.key.s,
1016 m_context->psk_sni_entry_list[i].psk_info.key.length);
1022 coap_mbedtls_env_t *m_env) {
1023 coap_mbedtls_context_t *m_context =
1026 m_context->psk_pki_enabled |= IS_SERVER;
1028 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
1029 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1030 MBEDTLS_SSL_IS_SERVER,
1032 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1033 MBEDTLS_SSL_TRANSPORT_STREAM,
1034 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1035 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1036 -ret, get_error_string(ret));
1040 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1042#if defined(MBEDTLS_SSL_PROTO_DTLS)
1043 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1044 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1047 if (m_context->psk_pki_enabled & IS_PSK) {
1048#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1049 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1051 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1053#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1061 if (m_context->psk_pki_enabled & IS_PKI) {
1062 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1063 &m_env->private_key, m_env, m_context,
1064 c_session, &m_context->setup_data,
1070 if (m_context->setup_data.validate_sni_call_back) {
1071 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1075 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1076 mbedtls_ctr_drbg_random,
1077 &m_env->ctr_drbg)) != 0) {
1078 coap_log_err(
"mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1079 -ret, get_error_string(ret));
1083#if defined(MBEDTLS_SSL_PROTO_DTLS)
1084 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1085 mbedtls_ssl_cookie_check,
1086 &m_env->cookie_ctx);
1087#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1088 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1091#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1099 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1106#if COAP_CLIENT_SUPPORT
1107static int *psk_ciphers = NULL;
1108static int *pki_ciphers = NULL;
1109static int *ecjpake_ciphers = NULL;
1110static int processed_ciphers = 0;
1112#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1114coap_ssl_ciphersuite_uses_psk(
const mbedtls_ssl_ciphersuite_t *info) {
1115#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1116 switch (info->key_exchange) {
1117 case MBEDTLS_KEY_EXCHANGE_PSK:
1118 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1119 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1120 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1122 case MBEDTLS_KEY_EXCHANGE_NONE:
1123 case MBEDTLS_KEY_EXCHANGE_RSA:
1124 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1125 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1126 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1127 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1128 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1129 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1134 return mbedtls_ssl_ciphersuite_uses_psk(info);
1140set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1141 if (!processed_ciphers) {
1142 const int *list = mbedtls_ssl_list_ciphersuites();
1143 const int *base = list;
1146#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1148 int ecjpake_count = 1;
1154 const mbedtls_ssl_ciphersuite_t *cur =
1155 mbedtls_ssl_ciphersuite_from_id(*list);
1158#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1159 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1163 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1167#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1168 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1172#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1173 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1178#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1179 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1191 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
1192 if (psk_ciphers == NULL) {
1193 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1196 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
1197 if (pki_ciphers == NULL) {
1198 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1199 mbedtls_free(psk_ciphers);
1203#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1204 ecjpake_ciphers = mbedtls_malloc(ecjpake_count *
sizeof(ecjpake_ciphers[0]));
1205 if (ecjpake_ciphers == NULL) {
1206 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1207 mbedtls_free(psk_ciphers);
1208 mbedtls_free(pki_ciphers);
1215 psk_list = psk_ciphers;
1216 pki_list = pki_ciphers;
1217#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1218 ecjpake_list = ecjpake_ciphers;
1222 const mbedtls_ssl_ciphersuite_t *cur =
1223 mbedtls_ssl_ciphersuite_from_id(*list);
1225#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1226 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1230 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1234#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1235 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1236 *ecjpake_list = *list;
1240#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1241 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1248#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1249 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1264 processed_ciphers = 1;
1268 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1271 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1273 case COAP_ENC_ECJPAKE:
1274 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1284 coap_mbedtls_env_t *m_env) {
1287 coap_mbedtls_context_t *m_context =
1290 m_context->psk_pki_enabled |= IS_CLIENT;
1292 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1293 MBEDTLS_SSL_IS_CLIENT,
1295 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1296 MBEDTLS_SSL_TRANSPORT_STREAM,
1297 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1298 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1299 -ret, get_error_string(ret));
1303#if defined(MBEDTLS_SSL_PROTO_DTLS)
1304 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1305 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1308 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1309 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1311 if (m_context->psk_pki_enabled & IS_PSK) {
1312#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1320 if (psk_key == NULL || psk_identity == NULL) {
1321 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1325 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->
s,
1326 psk_key->
length, psk_identity->
s,
1327 psk_identity->
length)) != 0) {
1328 coap_log_err(
"mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1329 -ret, get_error_string(ret));
1333 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1335 coap_log_err(
"mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1336 -ret, get_error_string(ret));
1342#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1344 m_env->ec_jpake = 1;
1345 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1346#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1347 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1350 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1353 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1358 }
else if ((m_context->psk_pki_enabled & IS_PKI) ||
1359 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1364 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1365 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1366 &m_env->private_key, m_env, m_context,
1367 c_session, &m_context->setup_data,
1373#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1376 static const char *alpn_list[] = {
"coap", NULL };
1378 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1384 if (m_context->setup_data.client_sni) {
1385 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1387#if defined(MBEDTLS_SSL_PROTO_DTLS)
1388#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1389 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1392 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1402mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1407 mbedtls_x509_crt_free(&m_env->cacert);
1408 mbedtls_x509_crt_free(&m_env->public_cert);
1409 mbedtls_pk_free(&m_env->private_key);
1410 mbedtls_entropy_free(&m_env->entropy);
1411 mbedtls_ssl_config_free(&m_env->conf);
1412 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1413 mbedtls_ssl_free(&m_env->ssl);
1414 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1418coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1420 if (!m_env->sent_alert)
1421 mbedtls_ssl_close_notify(&m_env->ssl);
1422 mbedtls_cleanup(m_env);
1423 mbedtls_free(m_env);
1427#if COAP_MAX_LOGGING_LEVEL > 0
1429report_mbedtls_alert(
unsigned char alert) {
1431 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1432 return ": Bad Record MAC";
1433 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1434 return ": Handshake failure";
1435 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1436 return ": No Certificate provided";
1437 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1438 return ": Certificate is bad";
1439 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1440 return ": Certificate is unknown";
1441 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1442 return ": CA is unknown";
1443 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1444 return ": Access was denied";
1445 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1446 return ": Decrypt error";
1460 coap_mbedtls_env_t *m_env) {
1464 ret = mbedtls_ssl_handshake(&m_env->ssl);
1467 m_env->established = 1;
1471#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1472#if COAP_CLIENT_SUPPORT
1475 coap_mbedtls_context_t *m_context;
1479 m_context->setup_data.use_cid) {
1480 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1482 size_t peer_cid_len;
1485 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1486 enabled == MBEDTLS_SSL_CID_ENABLED) {
1497 case MBEDTLS_ERR_SSL_WANT_READ:
1498 case MBEDTLS_ERR_SSL_WANT_WRITE:
1499 if (m_env->ssl.state == MBEDTLS_SSL_SERVER_HELLO
1500#
if MBEDTLS_VERSION_NUMBER >= 0x03030000
1501 || m_env->ssl.state == MBEDTLS_SSL_NEW_SESSION_TICKET
1504 if (++m_env->server_hello_cnt > 10) {
1512 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1515 case MBEDTLS_ERR_SSL_INVALID_MAC:
1517#ifdef MBEDTLS_2_X_COMPAT
1518 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1520 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1523 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1524 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1526#ifdef MBEDTLS_2_X_COMPAT
1527 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1528 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1529 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1532 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1534 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1535 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1538 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1540 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1541 case MBEDTLS_ERR_SSL_CONN_EOF:
1542 case MBEDTLS_ERR_NET_CONN_RESET:
1548 "returned -0x%x: '%s'\n",
1549 -ret, get_error_string(ret));
1556 mbedtls_ssl_send_alert_message(&m_env->ssl,
1557 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1559 m_env->sent_alert = 1;
1564 get_error_string(ret));
1566 mbedtls_ssl_session_reset(&m_env->ssl);
1567#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1568 if (m_env->ec_jpake) {
1571#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1577#elif COAP_CLIENT_SUPPORT
1583 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1591mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1624#if !COAP_DISABLE_TCP
1632coap_sock_read(
void *ctx,
unsigned char *out,
size_t outl) {
1633 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
1640 if (errno == ECONNRESET) {
1642 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1644 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1646 }
else if (ret == 0) {
1648 ret = MBEDTLS_ERR_SSL_WANT_READ;
1661coap_sock_write(
void *context,
const unsigned char *in,
size_t inl) {
1666 (
const uint8_t *)in,
1672 (errno == EPIPE || errno == ECONNRESET)) {
1687 int lasterror = WSAGetLastError();
1689 if (lasterror == WSAEWOULDBLOCK) {
1690 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1691 }
else if (lasterror == WSAECONNRESET) {
1692 ret = MBEDTLS_ERR_NET_CONN_RESET;
1695 if (errno == EAGAIN || errno == EINTR) {
1696 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1697 }
else if (errno == EPIPE || errno == ECONNRESET) {
1698 ret = MBEDTLS_ERR_NET_CONN_RESET;
1702 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1711 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1717static coap_mbedtls_env_t *
1722 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1727 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1731 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1733 mbedtls_ssl_init(&m_env->ssl);
1734 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1735 mbedtls_ssl_config_init(&m_env->conf);
1736 mbedtls_entropy_init(&m_env->entropy);
1738#if defined(MBEDTLS_PSA_CRYPTO_C)
1742#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1743 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1745 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1746 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1747 if (ret != MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) {
1748 coap_log_info(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1749 -ret, get_error_string(ret));
1752 coap_log_err(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1753 -ret, get_error_string(ret));
1757#if COAP_CLIENT_SUPPORT
1758 if (setup_client_ssl_session(c_session, m_env) != 0) {
1765#if defined(MBEDTLS_SSL_SRV_C)
1766 if (setup_server_ssl_session(c_session, m_env) != 0) {
1776#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1777 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1779 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1780 MBEDTLS_SSL_MINOR_VERSION_3);
1783 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
1787 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1788 coap_dgram_read, NULL);
1789#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1792#if COAP_CLIENT_SUPPORT
1793 coap_mbedtls_context_t *m_context =
1797 m_context->setup_data.use_cid) {
1805 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
1809#if COAP_SERVER_SUPPORT
1810 u_char cid[COAP_DTLS_CID_LENGTH];
1819 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
1827#if !COAP_DISABLE_TCP
1830 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1831 coap_sock_read, NULL);
1834#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1835 coap_mbedtls_context_t *m_context =
1837 if ((m_context->psk_pki_enabled & IS_PSK) &&
1841#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1847#elif COAP_CLIENT_SUPPORT
1852 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1855 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1856 mbedtls_timing_set_delay,
1857 mbedtls_timing_get_delay);
1859 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1864 mbedtls_free(m_env);
1871#if defined(MBEDTLS_SSL_PROTO_DTLS)
1874 static int reported = 0;
1878 " - update Mbed TLS to include DTLS\n");
1886#if !COAP_DISABLE_TCP
1935#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1942#if COAP_CLIENT_SUPPORT
1945#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1958 coap_mbedtls_context_t *m_context;
1961 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
1963 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
1968#if COAP_SERVER_SUPPORT
1977 coap_mbedtls_context_t *m_context =
1980#if !defined(MBEDTLS_SSL_SRV_C)
1982 " libcoap not compiled for Server Mode for Mbed TLS"
1983 " - update Mbed TLS to include Server Mode\n");
1986 if (!m_context || !setup_data)
1990#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1991 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
1994 m_context->psk_pki_enabled |= IS_PSK;
1999#if COAP_CLIENT_SUPPORT
2008#if !defined(MBEDTLS_SSL_CLI_C)
2013 " libcoap not compiled for Client Mode for Mbed TLS"
2014 " - update Mbed TLS to include Client Mode\n");
2017 coap_mbedtls_context_t *m_context =
2020 if (!m_context || !setup_data)
2024 coap_log_warn(
"CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2027#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2028 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
2032#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2033 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2036 m_context->psk_pki_enabled |= IS_PSK;
2046 coap_mbedtls_context_t *m_context =
2049 m_context->setup_data = *setup_data;
2050 if (!m_context->setup_data.verify_peer_cert) {
2054 m_context->setup_data.allow_self_signed = 1;
2055 m_context->setup_data.allow_expired_certs = 1;
2056 m_context->setup_data.cert_chain_validation = 1;
2057 m_context->setup_data.cert_chain_verify_depth = 10;
2058 m_context->setup_data.check_cert_revocation = 1;
2059 m_context->setup_data.allow_no_crl = 1;
2060 m_context->setup_data.allow_expired_crl = 1;
2061 m_context->setup_data.allow_bad_md_hash = 1;
2062 m_context->setup_data.allow_short_rsa_length = 1;
2064 m_context->psk_pki_enabled |= IS_PKI;
2066#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2067 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2075 const char *ca_file,
2076 const char *ca_path) {
2077 coap_mbedtls_context_t *m_context =
2081 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
2086 if (ca_file == NULL && ca_path == NULL) {
2087 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
2091 if (m_context->root_ca_file) {
2092 mbedtls_free(m_context->root_ca_file);
2093 m_context->root_ca_file = NULL;
2097 m_context->root_ca_file = mbedtls_strdup(ca_file);
2100 if (m_context->root_ca_path) {
2101 mbedtls_free(m_context->root_ca_path);
2102 m_context->root_ca_path = NULL;
2106 m_context->root_ca_path = mbedtls_strdup(ca_path);
2113 coap_mbedtls_context_t *m_context =
2115 return m_context->psk_pki_enabled ? 1 : 0;
2120 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2123 for (i = 0; i < m_context->pki_sni_count; i++) {
2124 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
2126 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
2128 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
2130 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
2132 if (m_context->pki_sni_entry_list)
2133 mbedtls_free(m_context->pki_sni_entry_list);
2135 for (i = 0; i < m_context->psk_sni_count; i++) {
2136 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2138 if (m_context->psk_sni_entry_list)
2139 mbedtls_free(m_context->psk_sni_entry_list);
2141 if (m_context->root_ca_path)
2142 mbedtls_free(m_context->root_ca_path);
2143 if (m_context->root_ca_file)
2144 mbedtls_free(m_context->root_ca_file);
2146 mbedtls_free(m_context);
2149#if COAP_CLIENT_SUPPORT
2152#if !defined(MBEDTLS_SSL_CLI_C)
2155 " libcoap not compiled for Client Mode for Mbed TLS"
2156 " - update Mbed TLS to include Client Mode\n");
2159 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2168 m_env->last_timeout = now;
2169 ret = do_mbedtls_handshake(c_session, m_env);
2171 coap_dtls_free_mbedtls_env(m_env);
2180#if COAP_SERVER_SUPPORT
2183#if !defined(MBEDTLS_SSL_SRV_C)
2186 " libcoap not compiled for Server Mode for Mbed TLS"
2187 " - update Mbed TLS to include Server Mode\n");
2190 coap_mbedtls_env_t *m_env =
2191 (coap_mbedtls_env_t *)c_session->
tls;
2193#if defined(MBEDTLS_SSL_PROTO_DTLS)
2194#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2195 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2206 if (c_session && c_session->
context && c_session->
tls) {
2207 coap_dtls_free_mbedtls_env(c_session->
tls);
2208 c_session->
tls = NULL;
2216#if defined(MBEDTLS_SSL_PROTO_DTLS)
2217 coap_mbedtls_env_t *m_env =
2218 (coap_mbedtls_env_t *)c_session->
tls;
2220#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2221 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2231 const uint8_t *data,
size_t data_len) {
2233 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2235 assert(m_env != NULL);
2243 if (m_env->established) {
2244 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char *) data, data_len);
2247 case MBEDTLS_ERR_SSL_WANT_READ:
2248 case MBEDTLS_ERR_SSL_WANT_WRITE:
2251 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2257 "returned -0x%x: '%s'\n",
2258 -ret, get_error_string(ret));
2267 ret = do_mbedtls_handshake(c_session, m_env);
2300 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2301 int ret = mbedtls_timing_get_delay(&m_env->timer);
2302 unsigned int scalar = 1 << m_env->retry_scalar;
2312 m_env->last_timeout = now;
2325 m_env->last_timeout = now;
2343 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2346 m_env->retry_scalar++;
2348 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2363 const uint8_t *data,
2368 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2369 coap_ssl_t *ssl_data;
2371 assert(m_env != NULL);
2373 ssl_data = &m_env->coap_ssl_data;
2374 if (ssl_data->pdu_len) {
2375 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2378 ssl_data->pdu = data;
2379 ssl_data->pdu_len = (unsigned)data_len;
2381 if (m_env->established) {
2382#if COAP_CONSTRAINED_STACK
2395 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
2402 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2403 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2406 case MBEDTLS_ERR_SSL_WANT_READ:
2410 "returned -0x%x: '%s' (length %zd)\n",
2411 -ret, get_error_string(ret), data_len);
2416 ret = do_mbedtls_handshake(c_session, m_env);
2421 if (ssl_data->pdu_len) {
2423 ret = do_mbedtls_handshake(c_session, m_env);
2444 if (ssl_data && ssl_data->pdu_len) {
2446 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2447 ssl_data->pdu_len = 0;
2448 ssl_data->pdu = NULL;
2457#if COAP_SERVER_SUPPORT
2465 const uint8_t *data,
2467#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2472 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2473 " - update Mbed TLS to include DTLS and Server Mode\n");
2476 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2477 coap_ssl_t *ssl_data;
2484 c_session->
tls = m_env;
2491 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2494 coap_log_err(
"mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2495 -ret, get_error_string(ret));
2499 ssl_data = &m_env->coap_ssl_data;
2500 if (ssl_data->pdu_len) {
2501 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2504 ssl_data->pdu = data;
2505 ssl_data->pdu_len = (unsigned)data_len;
2507 ret = do_mbedtls_handshake(c_session, m_env);
2508 if (ret == 0 || m_env->seen_client_hello) {
2514 m_env->seen_client_hello = 0;
2520 if (ssl_data->pdu_len) {
2522 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2523 ssl_data->pdu_len = 0;
2524 ssl_data->pdu = NULL;
2533 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2534 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2536 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2542#if !COAP_DISABLE_TCP
2543#if COAP_CLIENT_SUPPORT
2546#if !defined(MBEDTLS_SSL_CLI_C)
2550 " libcoap not compiled for Client Mode for Mbed TLS"
2551 " - update Mbed TLS to include Client Mode\n");
2554 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2564 m_env->last_timeout = now;
2565 c_session->
tls = m_env;
2566 ret = do_mbedtls_handshake(c_session, m_env);
2576#if COAP_SERVER_SUPPORT
2579#if !defined(MBEDTLS_SSL_SRV_C)
2584 " libcoap not compiled for Server Mode for Mbed TLS"
2585 " - update Mbed TLS to include Server Mode\n");
2588 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2596 c_session->
tls = m_env;
2597 ret = do_mbedtls_handshake(c_session, m_env);
2622 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2623 size_t amount_sent = 0;
2625 assert(m_env != NULL);
2632 if (m_env->established) {
2633 while (amount_sent < data_len) {
2634 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
2635 data_len - amount_sent);
2638 case MBEDTLS_ERR_SSL_WANT_READ:
2639 case MBEDTLS_ERR_SSL_WANT_WRITE:
2646 case MBEDTLS_ERR_NET_CONN_RESET:
2647 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2652 "returned -0x%x: '%s'\n",
2653 -ret, get_error_string(ret));
2665 ret = do_mbedtls_handshake(c_session, m_env);
2686 if (ret == (ssize_t)data_len)
2705 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2714 if (!m_env->established && !m_env->sent_alert) {
2715 ret = do_mbedtls_handshake(c_session, m_env);
2724 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2728 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2732 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2734 m_env->sent_alert = 1;
2737#if MBEDTLS_VERSION_NUMBER >= 0x03060000
2738 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
2740 case MBEDTLS_ERR_SSL_WANT_READ:
2746 "returned -0x%x: '%s' (length %zd)\n",
2747 -ret, get_error_string(ret), data_len);
2751 }
else if (ret < (
int)data_len) {
2752 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
2780#if COAP_CLIENT_SUPPORT
2781 mbedtls_free(psk_ciphers);
2782 mbedtls_free(pki_ciphers);
2783 mbedtls_free(ecjpake_ciphers);
2786 ecjpake_ciphers = NULL;
2787 processed_ciphers = 0;
2797 if (c_session && c_session->
tls) {
2798 coap_mbedtls_env_t *m_env;
2801 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
2803 return (
void *)&m_env->ssl;
2812#if !defined(ESPIDF_VERSION)
2822 switch ((
int)level) {
2843 mbedtls_debug_set_threshold(use_level);
2845 keep_log_level = level;
2850 return keep_log_level;
2856 version.
version = mbedtls_version_get_number();
2862#if COAP_SERVER_SUPPORT
2865 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2868 mbedtls_sha256_init(digest_ctx);
2869#ifdef MBEDTLS_2_X_COMPAT
2870 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2872 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2883 mbedtls_sha256_free(digest_ctx);
2884 mbedtls_free(digest_ctx);
2889 const uint8_t *data,
2891#ifdef MBEDTLS_2_X_COMPAT
2892 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2894 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2903#ifdef MBEDTLS_2_X_COMPAT
2904 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t *)digest_buffer);
2906 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t *)digest_buffer);
2914#include <mbedtls/cipher.h>
2915#include <mbedtls/md.h>
2917#ifndef MBEDTLS_CIPHER_MODE_AEAD
2918#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
2921#ifdef MBEDTLS_ERROR_C
2922#include <mbedtls/error.h>
2925#ifdef MBEDTLS_ERROR_C
2928 int c_tmp = (int)(Func); \
2930 char error_buf[64]; \
2931 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
2932 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
2939 int c_tmp = (int)(Func); \
2941 coap_log_err("mbedtls: %d\n", tmp); \
2952static struct hash_algs {
2954 mbedtls_md_type_t hash_type;
2962static mbedtls_md_type_t
2963get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
2966 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2967 if (hashs[idx].alg == alg) {
2968 *hash_len = hashs[idx].hash_size;
2969 return hashs[idx].hash_type;
2972 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2973 return MBEDTLS_MD_NONE;
2980 mbedtls_md_context_t ctx;
2982 const mbedtls_md_info_t *md_info;
2986 mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length);
2988 if (dig_type == MBEDTLS_MD_NONE) {
2989 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2992 md_info = mbedtls_md_info_from_type(dig_type);
2994 len = mbedtls_md_get_size(md_info);
2999 mbedtls_md_init(&ctx);
3000 C(mbedtls_md_setup(&ctx, md_info, 0));
3002 C(mbedtls_md_starts(&ctx));
3003 C(mbedtls_md_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3007 C(mbedtls_md_finish(&ctx,
dummy->s));
3012 mbedtls_md_free(&ctx);
3017#if COAP_OSCORE_SUPPORT
3028static struct cipher_algs {
3030 mbedtls_cipher_type_t cipher_type;
3035static mbedtls_cipher_type_t
3039 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3040 if (ciphers[idx].alg == alg)
3041 return ciphers[idx].cipher_type;
3043 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3052static struct hmac_algs {
3054 mbedtls_md_type_t hmac_type;
3061static mbedtls_md_type_t
3065 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3066 if (hmacs[idx].hmac_alg == hmac_alg)
3067 return hmacs[idx].hmac_type;
3069 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3075 return get_cipher_alg(alg) != 0;
3084 return get_hmac_alg(hmac_alg) != 0;
3092setup_cipher_context(mbedtls_cipher_context_t *ctx,
3094 const uint8_t *key_data,
3096 mbedtls_operation_t mode) {
3097 const mbedtls_cipher_info_t *cipher_info;
3098 mbedtls_cipher_type_t cipher_type;
3102 memset(key, 0,
sizeof(key));
3104 if ((cipher_type = get_cipher_alg(coap_alg)) == 0) {
3105 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3109 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3111 coap_log_crit(
"coap_crypto_encrypt: cannot get cipher info\n");
3115 mbedtls_cipher_init(ctx);
3117 C(mbedtls_cipher_setup(ctx, cipher_info));
3118 klen = mbedtls_cipher_get_key_bitlen(ctx);
3119 if ((klen > (
int)(
sizeof(key) * 8)) || (key_length >
sizeof(key))) {
3123 memcpy(key, key_data, key_length);
3124 C(mbedtls_cipher_setkey(ctx, key, klen, mode));
3129 mbedtls_cipher_free(ctx);
3138 size_t *max_result_len) {
3139 mbedtls_cipher_context_t ctx;
3141#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3142 unsigned char tag[16];
3145 size_t result_len = *max_result_len;
3151 assert(params != NULL);
3158 if (!setup_cipher_context(&ctx,
3173#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3174 C(mbedtls_cipher_auth_encrypt(&ctx,
3187 if ((result_len + ccm->
tag_len) > *max_result_len) {
3192 memcpy(result + result_len, tag, ccm->
tag_len);
3193 *max_result_len = result_len + ccm->
tag_len;
3196 C(mbedtls_cipher_auth_encrypt_ext(&ctx,
3208 *max_result_len = result_len;
3213 mbedtls_cipher_free(&ctx);
3222 size_t *max_result_len) {
3223 mbedtls_cipher_context_t ctx;
3225#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3226 const unsigned char *tag;
3229 size_t result_len = *max_result_len;
3235 assert(params != NULL);
3243 if (!setup_cipher_context(&ctx,
3263#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3265 C(mbedtls_cipher_auth_decrypt(&ctx,
3278 C(mbedtls_cipher_auth_decrypt_ext(&ctx,
3293 *max_result_len = result_len;
3296 mbedtls_cipher_free(&ctx);
3305 mbedtls_md_context_t ctx;
3307 const int use_hmac = 1;
3308 const mbedtls_md_info_t *md_info;
3309 mbedtls_md_type_t mac_algo;
3317 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3318 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3321 md_info = mbedtls_md_info_from_type(mac_algo);
3323 len = mbedtls_md_get_size(md_info);
3328 mbedtls_md_init(&ctx);
3329 C(mbedtls_md_setup(&ctx, md_info, use_hmac));
3331 C(mbedtls_md_hmac_starts(&ctx, key->
s, key->
length));
3332 C(mbedtls_md_hmac_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3336 C(mbedtls_md_hmac_finish(&ctx,
dummy->s));
3341 mbedtls_md_free(&ctx);
3353#pragma GCC diagnostic ignored "-Wunused-function"
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.
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
coap_dtls_key_t pki_key
PKI key definition.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
void * sni_call_back_arg
Passed in to the SNI callback function.
coap_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