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#define mbedtls_free(a) free(a)
90#ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
92#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
93#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
97#if ! COAP_SERVER_SUPPORT
98#undef MBEDTLS_SSL_SRV_C
100#if ! COAP_CLIENT_SUPPORT
101#undef MBEDTLS_SSL_CLI_C
105#define strcasecmp _stricmp
108#define IS_PSK (1 << 0)
109#define IS_PKI (1 << 1)
110#define IS_CLIENT (1 << 6)
111#define IS_SERVER (1 << 7)
113typedef struct coap_ssl_t {
124typedef struct coap_mbedtls_env_t {
125 mbedtls_ssl_context ssl;
126 mbedtls_entropy_context entropy;
127 mbedtls_ctr_drbg_context ctr_drbg;
128 mbedtls_ssl_config conf;
129 mbedtls_timing_delay_context timer;
130 mbedtls_x509_crt cacert;
131 mbedtls_x509_crt public_cert;
132 mbedtls_pk_context private_key;
133 mbedtls_ssl_cookie_ctx cookie_ctx;
137 int seen_client_hello;
140 unsigned int retry_scalar;
141 coap_ssl_t coap_ssl_data;
142 uint32_t server_hello_cnt;
145typedef struct pki_sni_entry {
148 mbedtls_x509_crt cacert;
149 mbedtls_x509_crt public_cert;
150 mbedtls_pk_context private_key;
153typedef struct psk_sni_entry {
158typedef struct coap_mbedtls_context_t {
160 size_t pki_sni_count;
161 pki_sni_entry *pki_sni_entry_list;
162 size_t psk_sni_count;
163 psk_sni_entry *psk_sni_entry_list;
167} coap_mbedtls_context_t;
169typedef enum coap_enc_method_t {
175#ifndef MBEDTLS_2_X_COMPAT
180coap_rng(
void *ctx
COAP_UNUSED,
unsigned char *buf,
size_t len) {
181 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
186coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl) {
191 if (!c_session->
tls) {
193 return MBEDTLS_ERR_SSL_WANT_READ;
195 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
198 if (data->pdu_len > 0) {
199 if (outl < data->pdu_len) {
200 memcpy(out, data->pdu, outl);
203 data->pdu_len -= outl;
205 memcpy(out, data->pdu, data->pdu_len);
207 if (!data->peekmode) {
213 ret = MBEDTLS_ERR_SSL_WANT_READ;
227coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
228 size_t send_buffer_length) {
233 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
245 send_buffer, send_buffer_length);
246 if (result != (ssize_t)send_buffer_length) {
247 int keep_errno = errno;
250 result, send_buffer_length);
260 m_env->last_timeout = now;
268#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
273psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
274 const unsigned char *identity,
size_t identity_len) {
277 coap_mbedtls_env_t *m_env;
281 if (c_session == NULL)
285 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
286 lidentity.
length = identity ? identity_len : 0;
290 (
int)lidentity.
length, (
const char *)lidentity.
s);
292 m_env = (coap_mbedtls_env_t *)c_session->
tls;
307 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
308 m_env->seen_client_hello = 1;
314get_san_or_cn_from_cert(mbedtls_x509_crt *crt) {
316 const mbedtls_asn1_named_data *cn_data;
318 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
319 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
320 while (seq && seq->buf.p == NULL) {
325 return mbedtls_strndup((
const char *)seq->buf.p,
330 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
332 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
335 return mbedtls_strndup((
const char *)cn_data->val.p,
342#if COAP_MAX_LOGGING_LEVEL > 0
344get_error_string(
int ret) {
345 static char buf[128] = {0};
346 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
352self_signed_cert_verify_callback_mbedtls(
void *data,
357 const coap_mbedtls_context_t *m_context =
361 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
363 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
374cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
375 int depth, uint32_t *flags) {
377 coap_mbedtls_context_t *m_context =
385 cn = get_san_or_cn_from_cert(crt);
387 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
389 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
392 "The certificate has expired", cn ? cn :
"?", depth);
395 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
397 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
400 "The certificate has a future date", cn ? cn :
"?", depth);
403 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
405 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
408 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
411 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
413 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
416 "The certificate has a short RSA length", cn ? cn :
"?", depth);
419 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
421 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
422 self_signed_cert_verify_callback_mbedtls,
424 if (self_signed && depth == 0) {
427 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
431 cn ? cn :
"?", depth);
433 }
else if (self_signed) {
435 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
438 "Self-signed", cn ? cn :
"?", depth);
442 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
445 "The certificate's CA is not trusted", cn ? cn :
"?", depth);
449 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
451 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
454 "The certificate's CRL has expired", cn ? cn :
"?", depth);
456 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
459 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
461 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
464 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
466 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
471 *flags |= MBEDTLS_X509_BADCERT_OTHER;
474 "The certificate's verify depth is too long",
475 cn ? cn :
"?", depth);
478 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
479 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
493 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
499 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
502 tcp = strchr(buf,
'\n');
507 buf, *flags, cn ? cn :
"?", depth);
508 tcp = strchr(tcp+1,
'\n');
511 coap_log_err(
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
512 -ret, get_error_string(ret));
523setup_pki_credentials(mbedtls_x509_crt *cacert,
524 mbedtls_x509_crt *public_cert,
525 mbedtls_pk_context *private_key,
526 coap_mbedtls_env_t *m_env,
527 coap_mbedtls_context_t *m_context,
533 int done_private_key = 0;
534 int done_public_cert = 0;
552#if defined(MBEDTLS_FS_IO)
553 mbedtls_pk_init(private_key);
554#ifdef MBEDTLS_2_X_COMPAT
555 ret = mbedtls_pk_parse_keyfile(private_key,
558 ret = mbedtls_pk_parse_keyfile(private_key,
560 NULL, coap_rng, (
void *)&m_env->ctr_drbg);
567 done_private_key = 1;
575 mbedtls_pk_init(private_key);
579 buffer = mbedtls_malloc(length + 1);
585 buffer[length] =
'\000';
587#ifdef MBEDTLS_2_X_COMPAT
588 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
590 ret = mbedtls_pk_parse_key(private_key, buffer, length,
591 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
593 mbedtls_free(buffer);
595#ifdef MBEDTLS_2_X_COMPAT
596 ret = mbedtls_pk_parse_key(private_key,
600 ret = mbedtls_pk_parse_key(private_key,
603 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
611 done_private_key = 1;
614 mbedtls_pk_init(private_key);
615#ifdef MBEDTLS_2_X_COMPAT
616 ret = mbedtls_pk_parse_key(private_key,
620 ret = mbedtls_pk_parse_key(private_key,
623 (
void *)&m_env->ctr_drbg);
630 done_private_key = 1;
658#if defined(MBEDTLS_FS_IO)
659 mbedtls_x509_crt_init(public_cert);
660 ret = mbedtls_x509_crt_parse_file(public_cert,
667 done_public_cert = 1;
675 mbedtls_x509_crt_init(public_cert);
680 buffer = mbedtls_malloc(length + 1);
686 buffer[length] =
'\000';
688 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
689 mbedtls_free(buffer);
691 ret = mbedtls_x509_crt_parse(public_cert,
700 done_public_cert = 1;
707 mbedtls_x509_crt_init(public_cert);
708 ret = mbedtls_x509_crt_parse(public_cert,
716 done_public_cert = 1;
734 if (done_private_key && done_public_cert) {
735 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
737 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
738 -ret, get_error_string(ret));
747#
if MBEDTLS_VERSION_NUMBER < 0x03060000
756#if defined(MBEDTLS_FS_IO)
757 mbedtls_x509_crt_init(cacert);
758 ret = mbedtls_x509_crt_parse_file(cacert,
765 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
773 mbedtls_x509_crt_init(cacert);
777 buffer = mbedtls_malloc(length + 1);
783 buffer[length] =
'\000';
785 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
786 mbedtls_free(buffer);
788 ret = mbedtls_x509_crt_parse(cacert,
797 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
804 mbedtls_x509_crt_init(cacert);
805 ret = mbedtls_x509_crt_parse(cacert,
813 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
827#if defined(MBEDTLS_FS_IO)
828 if (m_context->root_ca_file) {
829 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
836 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
838 if (m_context->root_ca_path) {
839 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
846 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
855#if defined(MBEDTLS_SSL_SRV_C)
856 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
858 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
859 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
862 MBEDTLS_SSL_VERIFY_REQUIRED :
863 MBEDTLS_SSL_VERIFY_NONE);
868 mbedtls_ssl_conf_verify(&m_env->conf,
869 cert_verify_callback_mbedtls, c_session);
874#if defined(MBEDTLS_SSL_SRV_C)
879pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
880 const unsigned char *uname,
size_t name_len) {
884 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
885 coap_mbedtls_context_t *m_context =
889 name = mbedtls_malloc(name_len+1);
893 memcpy(name, uname, name_len);
894 name[name_len] =
'\000';
897 for (i = 0; i < m_context->pki_sni_count; i++) {
898 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
902 if (i == m_context->pki_sni_count) {
907 pki_sni_entry *pki_sni_entry_list;
910 m_context->setup_data.validate_sni_call_back(name,
911 m_context->setup_data.sni_call_back_arg));
917 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
918 (i+1)*
sizeof(pki_sni_entry));
920 if (pki_sni_entry_list == NULL) {
924 m_context->pki_sni_entry_list = pki_sni_entry_list;
925 memset(&m_context->pki_sni_entry_list[i], 0,
926 sizeof(m_context->pki_sni_entry_list[i]));
927 m_context->pki_sni_entry_list[i].sni = name;
928 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
929 sni_setup_data = m_context->setup_data;
930 sni_setup_data.pki_key = *new_entry;
931 if (setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
932 &m_context->pki_sni_entry_list[i].public_cert,
933 &m_context->pki_sni_entry_list[i].private_key,
942 m_context->pki_sni_count++;
947 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
949 return mbedtls_ssl_set_hs_own_cert(ssl,
950 &m_context->pki_sni_entry_list[i].public_cert,
951 &m_context->pki_sni_entry_list[i].private_key);
954#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
959psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
960 const unsigned char *uname,
size_t name_len) {
963 coap_mbedtls_context_t *m_context =
967 name = mbedtls_malloc(name_len+1);
971 memcpy(name, uname, name_len);
972 name[name_len] =
'\000';
975 for (i = 0; i < m_context->psk_sni_count; i++) {
976 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
980 if (i == m_context->psk_sni_count) {
985 psk_sni_entry *psk_sni_entry_list;
996 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
997 (i+1)*
sizeof(psk_sni_entry));
999 if (psk_sni_entry_list == NULL) {
1003 m_context->psk_sni_entry_list = psk_sni_entry_list;
1004 m_context->psk_sni_entry_list[i].sni = name;
1005 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1007 m_context->psk_sni_count++;
1013 &m_context->psk_sni_entry_list[i].psk_info.hint);
1015 &m_context->psk_sni_entry_list[i].psk_info.key);
1016 return mbedtls_ssl_set_hs_psk(ssl,
1017 m_context->psk_sni_entry_list[i].psk_info.key.s,
1018 m_context->psk_sni_entry_list[i].psk_info.key.length);
1024 coap_mbedtls_env_t *m_env) {
1025 coap_mbedtls_context_t *m_context =
1028 m_context->psk_pki_enabled |= IS_SERVER;
1030 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
1031 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1032 MBEDTLS_SSL_IS_SERVER,
1034 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1035 MBEDTLS_SSL_TRANSPORT_STREAM,
1036 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1037 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1038 -ret, get_error_string(ret));
1042 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1044#if defined(MBEDTLS_SSL_PROTO_DTLS)
1045 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1046 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1049 if (m_context->psk_pki_enabled & IS_PSK) {
1050#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1051 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1053 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1055#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1063 if (m_context->psk_pki_enabled & IS_PKI) {
1064 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1065 &m_env->private_key, m_env, m_context,
1066 c_session, &m_context->setup_data,
1072 if (m_context->setup_data.validate_sni_call_back) {
1073 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1077 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1078 mbedtls_ctr_drbg_random,
1079 &m_env->ctr_drbg)) != 0) {
1080 coap_log_err(
"mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1081 -ret, get_error_string(ret));
1085#if defined(MBEDTLS_SSL_PROTO_DTLS)
1086 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1087 mbedtls_ssl_cookie_check,
1088 &m_env->cookie_ctx);
1089#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1090 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1093#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1101 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1108#if COAP_CLIENT_SUPPORT
1109static int *psk_ciphers = NULL;
1110static int *pki_ciphers = NULL;
1111static int *ecjpake_ciphers = NULL;
1112static int processed_ciphers = 0;
1114#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1116coap_ssl_ciphersuite_uses_psk(
const mbedtls_ssl_ciphersuite_t *info) {
1117#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1118 switch (info->key_exchange) {
1119 case MBEDTLS_KEY_EXCHANGE_PSK:
1120 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1121 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1122 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1124 case MBEDTLS_KEY_EXCHANGE_NONE:
1125 case MBEDTLS_KEY_EXCHANGE_RSA:
1126 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1127 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1128 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1129 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1130 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1131 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1136 return mbedtls_ssl_ciphersuite_uses_psk(info);
1142set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1143 if (!processed_ciphers) {
1144 const int *list = mbedtls_ssl_list_ciphersuites();
1145 const int *base = list;
1148#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1150 int ecjpake_count = 1;
1156 const mbedtls_ssl_ciphersuite_t *cur =
1157 mbedtls_ssl_ciphersuite_from_id(*list);
1160#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1161 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1165 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1169#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1170 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1174#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1175 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1180#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1181 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1193 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
1194 if (psk_ciphers == NULL) {
1195 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1198 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
1199 if (pki_ciphers == NULL) {
1200 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1201 mbedtls_free(psk_ciphers);
1205#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1206 ecjpake_ciphers = mbedtls_malloc(ecjpake_count *
sizeof(ecjpake_ciphers[0]));
1207 if (ecjpake_ciphers == NULL) {
1208 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1209 mbedtls_free(psk_ciphers);
1210 mbedtls_free(pki_ciphers);
1217 psk_list = psk_ciphers;
1218 pki_list = pki_ciphers;
1219#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1220 ecjpake_list = ecjpake_ciphers;
1224 const mbedtls_ssl_ciphersuite_t *cur =
1225 mbedtls_ssl_ciphersuite_from_id(*list);
1227#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1228 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1232 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1236#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1237 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1238 *ecjpake_list = *list;
1242#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1243 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1250#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1251 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1266 processed_ciphers = 1;
1270 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1273 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1275 case COAP_ENC_ECJPAKE:
1276 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1286 coap_mbedtls_env_t *m_env) {
1289 coap_mbedtls_context_t *m_context =
1292 m_context->psk_pki_enabled |= IS_CLIENT;
1294 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1295 MBEDTLS_SSL_IS_CLIENT,
1297 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1298 MBEDTLS_SSL_TRANSPORT_STREAM,
1299 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1300 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1301 -ret, get_error_string(ret));
1305#if defined(MBEDTLS_SSL_PROTO_DTLS)
1306 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1307 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1310 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1311 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1313 if (m_context->psk_pki_enabled & IS_PSK) {
1314#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1322 if (psk_key == NULL || psk_identity == NULL) {
1323 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1327 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->
s,
1328 psk_key->
length, psk_identity->
s,
1329 psk_identity->
length)) != 0) {
1330 coap_log_err(
"mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1331 -ret, get_error_string(ret));
1335 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1337 coap_log_err(
"mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1338 -ret, get_error_string(ret));
1344#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1346 m_env->ec_jpake = 1;
1347 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1348#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1349 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1352 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1355 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1360 }
else if ((m_context->psk_pki_enabled & IS_PKI) ||
1361 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1366 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1367 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1368 &m_env->private_key, m_env, m_context,
1369 c_session, &m_context->setup_data,
1375#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1378 static const char *alpn_list[] = {
"coap", NULL };
1380 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1386 if (m_context->setup_data.client_sni) {
1387 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1389#if defined(MBEDTLS_SSL_PROTO_DTLS)
1390#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1391 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1394 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1404mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1409 mbedtls_x509_crt_free(&m_env->cacert);
1410 mbedtls_x509_crt_free(&m_env->public_cert);
1411 mbedtls_pk_free(&m_env->private_key);
1412 mbedtls_entropy_free(&m_env->entropy);
1413 mbedtls_ssl_config_free(&m_env->conf);
1414 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1415 mbedtls_ssl_free(&m_env->ssl);
1416 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1420coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1422 if (!m_env->sent_alert)
1423 mbedtls_ssl_close_notify(&m_env->ssl);
1424 mbedtls_cleanup(m_env);
1425 mbedtls_free(m_env);
1429#if COAP_MAX_LOGGING_LEVEL > 0
1431report_mbedtls_alert(
unsigned char alert) {
1433 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1434 return ": Bad Record MAC";
1435 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1436 return ": Handshake failure";
1437 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1438 return ": No Certificate provided";
1439 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1440 return ": Certificate is bad";
1441 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1442 return ": Certificate is unknown";
1443 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1444 return ": CA is unknown";
1445 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1446 return ": Access was denied";
1447 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1448 return ": Decrypt error";
1462 coap_mbedtls_env_t *m_env) {
1466 ret = mbedtls_ssl_handshake(&m_env->ssl);
1469 m_env->established = 1;
1473#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1474#if COAP_CLIENT_SUPPORT
1477 coap_mbedtls_context_t *m_context;
1481 m_context->setup_data.use_cid) {
1482 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1484 size_t peer_cid_len;
1487 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1488 enabled == MBEDTLS_SSL_CID_ENABLED) {
1499 case MBEDTLS_ERR_SSL_WANT_READ:
1500 case MBEDTLS_ERR_SSL_WANT_WRITE:
1501 if (m_env->ssl.state == MBEDTLS_SSL_SERVER_HELLO
1502#
if MBEDTLS_VERSION_NUMBER >= 0x03030000
1503 || m_env->ssl.state == MBEDTLS_SSL_NEW_SESSION_TICKET
1506 if (++m_env->server_hello_cnt > 10) {
1514 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1517 case MBEDTLS_ERR_SSL_INVALID_MAC:
1519#ifdef MBEDTLS_2_X_COMPAT
1520 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1522 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1525 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1526 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1528#ifdef MBEDTLS_2_X_COMPAT
1529 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1530 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1531 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1534 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1536 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1537 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1540 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1542 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1543 case MBEDTLS_ERR_SSL_CONN_EOF:
1544 case MBEDTLS_ERR_NET_CONN_RESET:
1550 "returned -0x%x: '%s'\n",
1551 -ret, get_error_string(ret));
1558 mbedtls_ssl_send_alert_message(&m_env->ssl,
1559 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1561 m_env->sent_alert = 1;
1566 get_error_string(ret));
1568 mbedtls_ssl_session_reset(&m_env->ssl);
1569#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1570 if (m_env->ec_jpake) {
1573#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1579#elif COAP_CLIENT_SUPPORT
1585 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1593mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1626#if !COAP_DISABLE_TCP
1634coap_sock_read(
void *ctx,
unsigned char *out,
size_t outl) {
1635 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
1642 if (errno == ECONNRESET) {
1644 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1646 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1648 }
else if (ret == 0) {
1650 ret = MBEDTLS_ERR_SSL_WANT_READ;
1663coap_sock_write(
void *context,
const unsigned char *in,
size_t inl) {
1668 (
const uint8_t *)in,
1674 (errno == EPIPE || errno == ECONNRESET)) {
1689 int lasterror = WSAGetLastError();
1691 if (lasterror == WSAEWOULDBLOCK) {
1692 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1693 }
else if (lasterror == WSAECONNRESET) {
1694 ret = MBEDTLS_ERR_NET_CONN_RESET;
1697 if (errno == EAGAIN || errno == EINTR) {
1698 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1699 }
else if (errno == EPIPE || errno == ECONNRESET) {
1700 ret = MBEDTLS_ERR_NET_CONN_RESET;
1704 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1713 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1719static coap_mbedtls_env_t *
1724 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1729 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1733 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1735 mbedtls_ssl_init(&m_env->ssl);
1736 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1737 mbedtls_ssl_config_init(&m_env->conf);
1738 mbedtls_entropy_init(&m_env->entropy);
1740#if defined(MBEDTLS_PSA_CRYPTO_C)
1744#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1745 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1747 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1748 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1749 if (ret != MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) {
1750 coap_log_info(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1751 -ret, get_error_string(ret));
1754 coap_log_err(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1755 -ret, get_error_string(ret));
1759#if COAP_CLIENT_SUPPORT
1760 if (setup_client_ssl_session(c_session, m_env) != 0) {
1767#if defined(MBEDTLS_SSL_SRV_C)
1768 if (setup_server_ssl_session(c_session, m_env) != 0) {
1778#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1779 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1781 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1782 MBEDTLS_SSL_MINOR_VERSION_3);
1785 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
1789 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1790 coap_dgram_read, NULL);
1791#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1794#if COAP_CLIENT_SUPPORT
1795 coap_mbedtls_context_t *m_context =
1799 m_context->setup_data.use_cid) {
1807 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
1811#if COAP_SERVER_SUPPORT
1812 u_char cid[COAP_DTLS_CID_LENGTH];
1821 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
1829#if !COAP_DISABLE_TCP
1832 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1833 coap_sock_read, NULL);
1836#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1837 coap_mbedtls_context_t *m_context =
1839 if ((m_context->psk_pki_enabled & IS_PSK) &&
1843#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1849#elif COAP_CLIENT_SUPPORT
1854 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1857 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1858 mbedtls_timing_set_delay,
1859 mbedtls_timing_get_delay);
1861 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1866 mbedtls_free(m_env);
1873#if defined(MBEDTLS_SSL_PROTO_DTLS)
1876 static int reported = 0;
1880 " - update Mbed TLS to include DTLS\n");
1888#if !COAP_DISABLE_TCP
1937#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1944#if COAP_CLIENT_SUPPORT
1947#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1960 coap_mbedtls_context_t *m_context;
1963 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
1965 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
1970#if COAP_SERVER_SUPPORT
1979 coap_mbedtls_context_t *m_context =
1982#if !defined(MBEDTLS_SSL_SRV_C)
1984 " libcoap not compiled for Server Mode for Mbed TLS"
1985 " - update Mbed TLS to include Server Mode\n");
1988 if (!m_context || !setup_data)
1992#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1993 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
1996 m_context->psk_pki_enabled |= IS_PSK;
2001#if COAP_CLIENT_SUPPORT
2010#if !defined(MBEDTLS_SSL_CLI_C)
2015 " libcoap not compiled for Client Mode for Mbed TLS"
2016 " - update Mbed TLS to include Client Mode\n");
2019 coap_mbedtls_context_t *m_context =
2022 if (!m_context || !setup_data)
2026 coap_log_warn(
"CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2029#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2030 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
2034#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2035 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2038 m_context->psk_pki_enabled |= IS_PSK;
2048 coap_mbedtls_context_t *m_context =
2051 m_context->setup_data = *setup_data;
2052 if (!m_context->setup_data.verify_peer_cert) {
2054 m_context->setup_data.check_common_ca = 0;
2056 m_context->setup_data.allow_self_signed = 1;
2057 m_context->setup_data.allow_expired_certs = 1;
2058 m_context->setup_data.cert_chain_validation = 1;
2059 m_context->setup_data.cert_chain_verify_depth = 10;
2060 m_context->setup_data.check_cert_revocation = 1;
2061 m_context->setup_data.allow_no_crl = 1;
2062 m_context->setup_data.allow_expired_crl = 1;
2063 m_context->setup_data.allow_bad_md_hash = 1;
2064 m_context->setup_data.allow_short_rsa_length = 1;
2066 m_context->psk_pki_enabled |= IS_PKI;
2068#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2069 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2077 const char *ca_file,
2078 const char *ca_path) {
2079 coap_mbedtls_context_t *m_context =
2083 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
2088 if (ca_file == NULL && ca_path == NULL) {
2089 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
2093 if (m_context->root_ca_file) {
2094 mbedtls_free(m_context->root_ca_file);
2095 m_context->root_ca_file = NULL;
2099 m_context->root_ca_file = mbedtls_strdup(ca_file);
2102 if (m_context->root_ca_path) {
2103 mbedtls_free(m_context->root_ca_path);
2104 m_context->root_ca_path = NULL;
2108 m_context->root_ca_path = mbedtls_strdup(ca_path);
2115 coap_mbedtls_context_t *m_context =
2117 return m_context->psk_pki_enabled ? 1 : 0;
2122 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2125 for (i = 0; i < m_context->pki_sni_count; i++) {
2126 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
2128 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
2130 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
2132 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
2134 if (m_context->pki_sni_entry_list)
2135 mbedtls_free(m_context->pki_sni_entry_list);
2137 for (i = 0; i < m_context->psk_sni_count; i++) {
2138 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2140 if (m_context->psk_sni_entry_list)
2141 mbedtls_free(m_context->psk_sni_entry_list);
2143 if (m_context->root_ca_path)
2144 mbedtls_free(m_context->root_ca_path);
2145 if (m_context->root_ca_file)
2146 mbedtls_free(m_context->root_ca_file);
2148 mbedtls_free(m_context);
2151#if COAP_CLIENT_SUPPORT
2154#if !defined(MBEDTLS_SSL_CLI_C)
2157 " libcoap not compiled for Client Mode for Mbed TLS"
2158 " - update Mbed TLS to include Client Mode\n");
2161 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2170 m_env->last_timeout = now;
2171 ret = do_mbedtls_handshake(c_session, m_env);
2173 coap_dtls_free_mbedtls_env(m_env);
2182#if COAP_SERVER_SUPPORT
2185#if !defined(MBEDTLS_SSL_SRV_C)
2188 " libcoap not compiled for Server Mode for Mbed TLS"
2189 " - update Mbed TLS to include Server Mode\n");
2192 coap_mbedtls_env_t *m_env =
2193 (coap_mbedtls_env_t *)c_session->
tls;
2195#if defined(MBEDTLS_SSL_PROTO_DTLS)
2196#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2197 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2208 if (c_session && c_session->
context && c_session->
tls) {
2209 coap_dtls_free_mbedtls_env(c_session->
tls);
2210 c_session->
tls = NULL;
2218#if defined(MBEDTLS_SSL_PROTO_DTLS)
2219 coap_mbedtls_env_t *m_env =
2220 (coap_mbedtls_env_t *)c_session->
tls;
2222#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2223 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2233 const uint8_t *data,
size_t data_len) {
2235 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2237 assert(m_env != NULL);
2245 if (m_env->established) {
2246 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char *) data, data_len);
2249 case MBEDTLS_ERR_SSL_WANT_READ:
2250 case MBEDTLS_ERR_SSL_WANT_WRITE:
2253 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2259 "returned -0x%x: '%s'\n",
2260 -ret, get_error_string(ret));
2269 ret = do_mbedtls_handshake(c_session, m_env);
2302 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2303 int ret = mbedtls_timing_get_delay(&m_env->timer);
2304 unsigned int scalar = 1 << m_env->retry_scalar;
2314 m_env->last_timeout = now;
2327 m_env->last_timeout = now;
2345 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2348 m_env->retry_scalar++;
2350 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2365 const uint8_t *data,
2370 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2371 coap_ssl_t *ssl_data;
2373 assert(m_env != NULL);
2375 ssl_data = &m_env->coap_ssl_data;
2376 if (ssl_data->pdu_len) {
2377 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2380 ssl_data->pdu = data;
2381 ssl_data->pdu_len = (unsigned)data_len;
2383 if (m_env->established) {
2384#if COAP_CONSTRAINED_STACK
2397 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
2404 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2405 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2408 case MBEDTLS_ERR_SSL_WANT_READ:
2412 "returned -0x%x: '%s' (length %zd)\n",
2413 -ret, get_error_string(ret), data_len);
2418 ret = do_mbedtls_handshake(c_session, m_env);
2423 if (ssl_data->pdu_len) {
2425 ret = do_mbedtls_handshake(c_session, m_env);
2446 if (ssl_data && ssl_data->pdu_len) {
2448 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2449 ssl_data->pdu_len = 0;
2450 ssl_data->pdu = NULL;
2459#if COAP_SERVER_SUPPORT
2467 const uint8_t *data,
2469#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2474 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2475 " - update Mbed TLS to include DTLS and Server Mode\n");
2478 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2479 coap_ssl_t *ssl_data;
2486 c_session->
tls = m_env;
2493 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2496 coap_log_err(
"mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2497 -ret, get_error_string(ret));
2501 ssl_data = &m_env->coap_ssl_data;
2502 if (ssl_data->pdu_len) {
2503 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2506 ssl_data->pdu = data;
2507 ssl_data->pdu_len = (unsigned)data_len;
2509 ret = do_mbedtls_handshake(c_session, m_env);
2510 if (ret == 0 || m_env->seen_client_hello) {
2516 m_env->seen_client_hello = 0;
2522 if (ssl_data->pdu_len) {
2524 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2525 ssl_data->pdu_len = 0;
2526 ssl_data->pdu = NULL;
2535 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2536 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2538 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2544#if !COAP_DISABLE_TCP
2545#if COAP_CLIENT_SUPPORT
2548#if !defined(MBEDTLS_SSL_CLI_C)
2552 " libcoap not compiled for Client Mode for Mbed TLS"
2553 " - update Mbed TLS to include Client Mode\n");
2556 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2566 m_env->last_timeout = now;
2567 c_session->
tls = m_env;
2568 ret = do_mbedtls_handshake(c_session, m_env);
2578#if COAP_SERVER_SUPPORT
2581#if !defined(MBEDTLS_SSL_SRV_C)
2586 " libcoap not compiled for Server Mode for Mbed TLS"
2587 " - update Mbed TLS to include Server Mode\n");
2590 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2598 c_session->
tls = m_env;
2599 ret = do_mbedtls_handshake(c_session, m_env);
2624 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2625 size_t amount_sent = 0;
2627 assert(m_env != NULL);
2634 if (m_env->established) {
2635 while (amount_sent < data_len) {
2636 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
2637 data_len - amount_sent);
2640 case MBEDTLS_ERR_SSL_WANT_READ:
2641 case MBEDTLS_ERR_SSL_WANT_WRITE:
2648 case MBEDTLS_ERR_NET_CONN_RESET:
2649 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2654 "returned -0x%x: '%s'\n",
2655 -ret, get_error_string(ret));
2667 ret = do_mbedtls_handshake(c_session, m_env);
2688 if (ret == (ssize_t)data_len)
2707 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2716 if (!m_env->established && !m_env->sent_alert) {
2717 ret = do_mbedtls_handshake(c_session, m_env);
2726 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2730 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2734 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2736 m_env->sent_alert = 1;
2739#if MBEDTLS_VERSION_NUMBER >= 0x03060000
2740 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
2742 case MBEDTLS_ERR_SSL_WANT_READ:
2748 "returned -0x%x: '%s' (length %zd)\n",
2749 -ret, get_error_string(ret), data_len);
2753 }
else if (ret < (
int)data_len) {
2754 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
2782#if COAP_CLIENT_SUPPORT
2783 mbedtls_free(psk_ciphers);
2784 mbedtls_free(pki_ciphers);
2785 mbedtls_free(ecjpake_ciphers);
2788 ecjpake_ciphers = NULL;
2789 processed_ciphers = 0;
2799 if (c_session && c_session->
tls) {
2800 coap_mbedtls_env_t *m_env;
2803 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
2805 return (
void *)&m_env->ssl;
2814#if !defined(ESPIDF_VERSION)
2824 switch ((
int)level) {
2845 mbedtls_debug_set_threshold(use_level);
2847 keep_log_level = level;
2852 return keep_log_level;
2858 version.
version = mbedtls_version_get_number();
2864#if COAP_SERVER_SUPPORT
2867 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2870 mbedtls_sha256_init(digest_ctx);
2871#ifdef MBEDTLS_2_X_COMPAT
2872 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2874 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2886 mbedtls_sha256_free(digest_ctx);
2887 mbedtls_free(digest_ctx);
2893 const uint8_t *data,
2895#ifdef MBEDTLS_2_X_COMPAT
2896 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2898 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2907#ifdef MBEDTLS_2_X_COMPAT
2908 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t *)digest_buffer);
2910 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t *)digest_buffer);
2918#include <mbedtls/cipher.h>
2919#include <mbedtls/md.h>
2921#ifndef MBEDTLS_CIPHER_MODE_AEAD
2922#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
2925#ifdef MBEDTLS_ERROR_C
2926#include <mbedtls/error.h>
2929#ifdef MBEDTLS_ERROR_C
2932 int c_tmp = (int)(Func); \
2934 char error_buf[64]; \
2935 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
2936 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
2943 int c_tmp = (int)(Func); \
2945 coap_log_err("mbedtls: %d\n", tmp); \
2956static struct hash_algs {
2958 mbedtls_md_type_t hash_type;
2966static mbedtls_md_type_t
2967get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
2970 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2971 if (hashs[idx].alg == alg) {
2972 *hash_len = hashs[idx].hash_size;
2973 return hashs[idx].hash_type;
2976 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2977 return MBEDTLS_MD_NONE;
2984 mbedtls_md_context_t ctx;
2986 const mbedtls_md_info_t *md_info;
2990 mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length);
2992 if (dig_type == MBEDTLS_MD_NONE) {
2993 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2996 md_info = mbedtls_md_info_from_type(dig_type);
2998 len = mbedtls_md_get_size(md_info);
3003 mbedtls_md_init(&ctx);
3004 C(mbedtls_md_setup(&ctx, md_info, 0));
3006 C(mbedtls_md_starts(&ctx));
3007 C(mbedtls_md_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3011 C(mbedtls_md_finish(&ctx,
dummy->s));
3016 mbedtls_md_free(&ctx);
3021#if COAP_OSCORE_SUPPORT
3032static struct cipher_algs {
3034 mbedtls_cipher_type_t cipher_type;
3039static mbedtls_cipher_type_t
3043 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3044 if (ciphers[idx].alg == alg)
3045 return ciphers[idx].cipher_type;
3047 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3056static struct hmac_algs {
3058 mbedtls_md_type_t hmac_type;
3065static mbedtls_md_type_t
3069 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3070 if (hmacs[idx].hmac_alg == hmac_alg)
3071 return hmacs[idx].hmac_type;
3073 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3079 return get_cipher_alg(alg) != 0;
3088 return get_hmac_alg(hmac_alg) != 0;
3096setup_cipher_context(mbedtls_cipher_context_t *ctx,
3098 const uint8_t *key_data,
3100 mbedtls_operation_t mode) {
3101 const mbedtls_cipher_info_t *cipher_info;
3102 mbedtls_cipher_type_t cipher_type;
3106 memset(key, 0,
sizeof(key));
3108 if ((cipher_type = get_cipher_alg(coap_alg)) == 0) {
3109 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3113 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3115 coap_log_crit(
"coap_crypto_encrypt: cannot get cipher info\n");
3119 mbedtls_cipher_init(ctx);
3121 C(mbedtls_cipher_setup(ctx, cipher_info));
3122 klen = mbedtls_cipher_get_key_bitlen(ctx);
3123 if ((klen > (
int)(
sizeof(key) * 8)) || (key_length >
sizeof(key))) {
3127 memcpy(key, key_data, key_length);
3128 C(mbedtls_cipher_setkey(ctx, key, klen, mode));
3133 mbedtls_cipher_free(ctx);
3142 size_t *max_result_len) {
3143 mbedtls_cipher_context_t ctx;
3145#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3146 unsigned char tag[16];
3149 size_t result_len = *max_result_len;
3155 assert(params != NULL);
3162 if (!setup_cipher_context(&ctx,
3177#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3178 C(mbedtls_cipher_auth_encrypt(&ctx,
3191 if ((result_len + ccm->
tag_len) > *max_result_len) {
3196 memcpy(result + result_len, tag, ccm->
tag_len);
3197 *max_result_len = result_len + ccm->
tag_len;
3200 C(mbedtls_cipher_auth_encrypt_ext(&ctx,
3212 *max_result_len = result_len;
3217 mbedtls_cipher_free(&ctx);
3226 size_t *max_result_len) {
3227 mbedtls_cipher_context_t ctx;
3229#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3230 const unsigned char *tag;
3233 size_t result_len = *max_result_len;
3239 assert(params != NULL);
3247 if (!setup_cipher_context(&ctx,
3267#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3269 C(mbedtls_cipher_auth_decrypt(&ctx,
3282 C(mbedtls_cipher_auth_decrypt_ext(&ctx,
3297 *max_result_len = result_len;
3300 mbedtls_cipher_free(&ctx);
3309 mbedtls_md_context_t ctx;
3311 const int use_hmac = 1;
3312 const mbedtls_md_info_t *md_info;
3313 mbedtls_md_type_t mac_algo;
3321 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3322 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3325 md_info = mbedtls_md_info_from_type(mac_algo);
3327 len = mbedtls_md_get_size(md_info);
3332 mbedtls_md_init(&ctx);
3333 C(mbedtls_md_setup(&ctx, md_info, use_hmac));
3335 C(mbedtls_md_hmac_starts(&ctx, key->
s, key->
length));
3336 C(mbedtls_md_hmac_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3340 C(mbedtls_md_hmac_finish(&ctx,
dummy->s));
3345 mbedtls_md_free(&ctx);
3357#pragma GCC diagnostic ignored "-Wunused-function"
#define COAP_SERVER_SUPPORT
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
Library specific build wrapper for coap_internal.h.
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
#define COAP_CRYPTO_MAX_KEY_SIZE
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
int coap_dtls_define_issue(coap_define_issue_key_t type, coap_define_issue_fail_t fail, coap_dtls_key_t *key, const coap_dtls_role_t role, int ret)
Report PKI DEFINE type issue.
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_KEY_ROOT_CA
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
@ COAP_TLS_LIBRARY_MBEDTLS
Using Mbed TLS library.
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
#define coap_lock_callback_ret(r, c, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
#define coap_log_emerg(...)
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_dtls_log(level,...)
Logging function.
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
#define coap_log_crit(...)
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
@ COSE_HMAC_ALG_HMAC384_384
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_ALGORITHM_SHA_256_256
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
coap_proto_t
CoAP protocol types.
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
#define COAP_PROTO_NOT_RELIABLE(p)
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_NONE
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
coap_address_t remote
remote address and port
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
The CoAP stack's global state is stored in a coap_context_t object.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
size_t tag_len
The size of the Tag.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@2 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorith to use.
The structure used for defining the Client PSK setup data to be used.
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
The structure that holds the PKI key information.
coap_pki_key_define_t define
for definable type keys
union coap_dtls_key_t::@3 key
coap_pki_key_t key_type
key format type
The structure used for defining the PKI setup data to be used.
void * cn_call_back_arg
Passed in to the CN callback function.
uint8_t allow_short_rsa_length
1 if small RSA keysizes are allowed
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
uint8_t allow_bad_md_hash
1 if unsupported MD hashes are allowed
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
uint8_t check_cert_revocation
1 if revocation checks wanted
uint8_t cert_chain_verify_depth
recommended depth is 3
uint8_t allow_expired_certs
1 if expired certs are allowed
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
uint8_t allow_self_signed
1 if self-signed certs are allowed.
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
uint8_t allow_expired_crl
1 if expired crl is allowed
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
void * sni_call_back_arg
Passed in to the SNI callback function.
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
coap_const_char_ptr_t private_key
define: Private Key
coap_const_char_ptr_t ca
define: Common CA Certificate
size_t public_cert_len
define Public Cert length (if needed)
size_t ca_len
define CA Cert length (if needed)
coap_pki_define_t private_key_def
define: Private Key type definition
size_t private_key_len
define Private Key length (if needed)
coap_pki_define_t ca_def
define: Common CA type definition
coap_pki_define_t public_cert_def
define: Public Cert type definition
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_endpoint_t * endpoint
session's endpoint
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_bin_const_t * client_cid
Contains client CID or NULL.
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t negotiated_cid
Set for a client if CID negotiated.
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
The structure used for returning the underlying (D)TLS library information.
uint64_t built_version
(D)TLS Built against Library Version
coap_tls_library_t type
Library type.
uint64_t version
(D)TLS runtime Library Version
const char * s_byte
signed char ptr
const uint8_t * u_byte
unsigned char ptr