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;
166 int trust_store_defined;
168} coap_mbedtls_context_t;
170typedef enum coap_enc_method_t {
176#ifndef MBEDTLS_2_X_COMPAT
181coap_rng(
void *ctx
COAP_UNUSED,
unsigned char *buf,
size_t len) {
182 return coap_prng_lkd(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
187coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl) {
192 if (!c_session->
tls) {
194 return MBEDTLS_ERR_SSL_WANT_READ;
196 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
199 if (data->pdu_len > 0) {
200 if (outl < data->pdu_len) {
201 memcpy(out, data->pdu, outl);
204 data->pdu_len -= outl;
206 memcpy(out, data->pdu, data->pdu_len);
208 if (!data->peekmode) {
214 ret = MBEDTLS_ERR_SSL_WANT_READ;
228coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
229 size_t send_buffer_length) {
234 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
246 send_buffer, send_buffer_length);
247 if (result != (ssize_t)send_buffer_length) {
248 int keep_errno = errno;
251 result, send_buffer_length);
261 m_env->last_timeout = now;
269#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
274psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
275 const unsigned char *identity,
size_t identity_len) {
278 coap_mbedtls_env_t *m_env;
282 if (c_session == NULL)
286 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
287 lidentity.
length = identity ? identity_len : 0;
291 (
int)lidentity.
length, (
const char *)lidentity.
s);
293 m_env = (coap_mbedtls_env_t *)c_session->
tls;
308 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
309 m_env->seen_client_hello = 1;
315get_san_or_cn_from_cert(mbedtls_x509_crt *crt) {
317 const mbedtls_asn1_named_data *cn_data;
319 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
320 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
321 while (seq && seq->buf.p == NULL) {
326 return mbedtls_strndup((
const char *)seq->buf.p,
331 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
333 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
336 return mbedtls_strndup((
const char *)cn_data->val.p,
343#if COAP_MAX_LOGGING_LEVEL > 0
345get_error_string(
int ret) {
346 static char buf[128] = {0};
347 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
353self_signed_cert_verify_callback_mbedtls(
void *data,
358 const coap_mbedtls_context_t *m_context =
362 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
364 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
375cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
376 int depth, uint32_t *flags) {
378 coap_mbedtls_context_t *m_context =
386 cn = get_san_or_cn_from_cert(crt);
388 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
390 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
393 "The certificate has expired", cn ? cn :
"?", depth);
396 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
398 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
401 "The certificate has a future date", cn ? cn :
"?", depth);
404 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
406 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
409 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
412 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
414 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
417 "The certificate has a short RSA length", cn ? cn :
"?", depth);
420 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
422 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
423 self_signed_cert_verify_callback_mbedtls,
425 if (self_signed && depth == 0) {
428 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
432 cn ? cn :
"?", depth);
434 }
else if (self_signed) {
436 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
439 "Self-signed", cn ? cn :
"?", depth);
443 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
446 "The certificate's CA is not trusted", cn ? cn :
"?", depth);
450 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
452 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
455 "The certificate's CRL has expired", cn ? cn :
"?", depth);
457 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
460 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
462 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
465 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
467 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
472 *flags |= MBEDTLS_X509_BADCERT_OTHER;
475 "The certificate's verify depth is too long",
476 cn ? cn :
"?", depth);
479 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
480 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
494 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
500 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
503 tcp = strchr(buf,
'\n');
508 buf, *flags, cn ? cn :
"?", depth);
509 tcp = strchr(tcp+1,
'\n');
512 coap_log_err(
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
513 -ret, get_error_string(ret));
524setup_pki_credentials(mbedtls_x509_crt *cacert,
525 mbedtls_x509_crt *public_cert,
526 mbedtls_pk_context *private_key,
527 coap_mbedtls_env_t *m_env,
528 coap_mbedtls_context_t *m_context,
534 int done_private_key = 0;
535 int done_public_cert = 0;
553#if defined(MBEDTLS_FS_IO)
554 mbedtls_pk_init(private_key);
555#ifdef MBEDTLS_2_X_COMPAT
556 ret = mbedtls_pk_parse_keyfile(private_key,
559 ret = mbedtls_pk_parse_keyfile(private_key,
561 NULL, coap_rng, (
void *)&m_env->ctr_drbg);
568 done_private_key = 1;
576 mbedtls_pk_init(private_key);
580 buffer = mbedtls_malloc(length + 1);
586 buffer[length] =
'\000';
588#ifdef MBEDTLS_2_X_COMPAT
589 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
591 ret = mbedtls_pk_parse_key(private_key, buffer, length,
592 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
594 mbedtls_free(buffer);
596#ifdef MBEDTLS_2_X_COMPAT
597 ret = mbedtls_pk_parse_key(private_key,
601 ret = mbedtls_pk_parse_key(private_key,
604 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
612 done_private_key = 1;
615 mbedtls_pk_init(private_key);
616#ifdef MBEDTLS_2_X_COMPAT
617 ret = mbedtls_pk_parse_key(private_key,
621 ret = mbedtls_pk_parse_key(private_key,
624 (
void *)&m_env->ctr_drbg);
631 done_private_key = 1;
659#if defined(MBEDTLS_FS_IO)
660 mbedtls_x509_crt_init(public_cert);
661 ret = mbedtls_x509_crt_parse_file(public_cert,
668 done_public_cert = 1;
676 mbedtls_x509_crt_init(public_cert);
681 buffer = mbedtls_malloc(length + 1);
687 buffer[length] =
'\000';
689 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
690 mbedtls_free(buffer);
692 ret = mbedtls_x509_crt_parse(public_cert,
701 done_public_cert = 1;
708 mbedtls_x509_crt_init(public_cert);
709 ret = mbedtls_x509_crt_parse(public_cert,
717 done_public_cert = 1;
735 if (done_private_key && done_public_cert) {
736 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
738 coap_log_err(
"mbedtls_ssl_conf_own_cert returned -0x%x: '%s'\n",
739 -ret, get_error_string(ret));
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_path(cacert, m_context->root_ca_path);
844 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
846 if (m_context->trust_store_defined) {
848 const char *trust_list[] = {
849 "/etc/ssl/ca-bundle.pem",
850 "/etc/ssl/certs/ca-certificates.crt",
851 "/etc/pki/tls/cert.pem",
852 "/usr/local/share/certs/ca-root-nss.crt",
855 static const char *trust_file_found = NULL;
856 static int trust_file_done = 0;
859 if (trust_file_found) {
860 ret = mbedtls_x509_crt_parse_file(cacert, trust_file_found);
862 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
867 }
else if (!trust_file_done) {
869 for (i = 0; i <
sizeof(trust_list)/
sizeof(trust_list[0]); i++) {
870 ret = mbedtls_x509_crt_parse_file(cacert, trust_list[i]);
872 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
873 trust_file_found = trust_list[i];
877 if (i ==
sizeof(trust_list)/
sizeof(trust_list[0])) {
889#if defined(MBEDTLS_SSL_SRV_C)
890 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
892 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
893 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
896 MBEDTLS_SSL_VERIFY_REQUIRED :
897 MBEDTLS_SSL_VERIFY_NONE);
902 mbedtls_ssl_conf_verify(&m_env->conf,
903 cert_verify_callback_mbedtls, c_session);
908#if defined(MBEDTLS_SSL_SRV_C)
913pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
914 const unsigned char *uname,
size_t name_len) {
918 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
919 coap_mbedtls_context_t *m_context =
923 name = mbedtls_malloc(name_len+1);
927 memcpy(name, uname, name_len);
928 name[name_len] =
'\000';
931 for (i = 0; i < m_context->pki_sni_count; i++) {
932 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
936 if (i == m_context->pki_sni_count) {
941 pki_sni_entry *pki_sni_entry_list;
944 m_context->setup_data.validate_sni_call_back(name,
945 m_context->setup_data.sni_call_back_arg));
951 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
952 (i+1)*
sizeof(pki_sni_entry));
954 if (pki_sni_entry_list == NULL) {
958 m_context->pki_sni_entry_list = pki_sni_entry_list;
959 memset(&m_context->pki_sni_entry_list[i], 0,
960 sizeof(m_context->pki_sni_entry_list[i]));
961 m_context->pki_sni_entry_list[i].sni = name;
962 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
963 sni_setup_data = m_context->setup_data;
964 sni_setup_data.pki_key = *new_entry;
965 if (setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
966 &m_context->pki_sni_entry_list[i].public_cert,
967 &m_context->pki_sni_entry_list[i].private_key,
976 m_context->pki_sni_count++;
981 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
983 return mbedtls_ssl_set_hs_own_cert(ssl,
984 &m_context->pki_sni_entry_list[i].public_cert,
985 &m_context->pki_sni_entry_list[i].private_key);
988#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
993psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
994 const unsigned char *uname,
size_t name_len) {
997 coap_mbedtls_context_t *m_context =
1001 name = mbedtls_malloc(name_len+1);
1005 memcpy(name, uname, name_len);
1006 name[name_len] =
'\000';
1009 for (i = 0; i < m_context->psk_sni_count; i++) {
1010 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
1014 if (i == m_context->psk_sni_count) {
1019 psk_sni_entry *psk_sni_entry_list;
1030 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
1031 (i+1)*
sizeof(psk_sni_entry));
1033 if (psk_sni_entry_list == NULL) {
1037 m_context->psk_sni_entry_list = psk_sni_entry_list;
1038 m_context->psk_sni_entry_list[i].sni = name;
1039 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
1041 m_context->psk_sni_count++;
1047 &m_context->psk_sni_entry_list[i].psk_info.hint);
1049 &m_context->psk_sni_entry_list[i].psk_info.key);
1050 return mbedtls_ssl_set_hs_psk(ssl,
1051 m_context->psk_sni_entry_list[i].psk_info.key.s,
1052 m_context->psk_sni_entry_list[i].psk_info.key.length);
1058 coap_mbedtls_env_t *m_env) {
1059 coap_mbedtls_context_t *m_context =
1062 m_context->psk_pki_enabled |= IS_SERVER;
1064 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
1065 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1066 MBEDTLS_SSL_IS_SERVER,
1068 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1069 MBEDTLS_SSL_TRANSPORT_STREAM,
1070 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1071 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1072 -ret, get_error_string(ret));
1076 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1078#if defined(MBEDTLS_SSL_PROTO_DTLS)
1079 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1080 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1083 if (m_context->psk_pki_enabled & IS_PSK) {
1084#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1085 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
1087 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1089#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1097 if (m_context->psk_pki_enabled & IS_PKI) {
1098 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1099 &m_env->private_key, m_env, m_context,
1100 c_session, &m_context->setup_data,
1106 if (m_context->setup_data.validate_sni_call_back) {
1107 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1111 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1112 mbedtls_ctr_drbg_random,
1113 &m_env->ctr_drbg)) != 0) {
1114 coap_log_err(
"mbedtls_ssl_cookie_setup: returned -0x%x: '%s'\n",
1115 -ret, get_error_string(ret));
1119#if defined(MBEDTLS_SSL_PROTO_DTLS)
1120 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1121 mbedtls_ssl_cookie_check,
1122 &m_env->cookie_ctx);
1123#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1124 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1127#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1135 mbedtls_ssl_conf_cid(&m_env->conf, COAP_DTLS_CID_LENGTH, MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1142#if COAP_CLIENT_SUPPORT
1143static int *psk_ciphers = NULL;
1144static int *pki_ciphers = NULL;
1145static int *ecjpake_ciphers = NULL;
1146static int processed_ciphers = 0;
1148#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1150coap_ssl_ciphersuite_uses_psk(
const mbedtls_ssl_ciphersuite_t *info) {
1151#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1152 switch (info->key_exchange) {
1153 case MBEDTLS_KEY_EXCHANGE_PSK:
1154 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
1155 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
1156 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
1158 case MBEDTLS_KEY_EXCHANGE_NONE:
1159 case MBEDTLS_KEY_EXCHANGE_RSA:
1160 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
1161 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
1162 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
1163 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
1164 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
1165 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
1170 return mbedtls_ssl_ciphersuite_uses_psk(info);
1176set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method) {
1177 if (!processed_ciphers) {
1178 const int *list = mbedtls_ssl_list_ciphersuites();
1179 const int *base = list;
1182#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1184 int ecjpake_count = 1;
1190 const mbedtls_ssl_ciphersuite_t *cur =
1191 mbedtls_ssl_ciphersuite_from_id(*list);
1194#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1195 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1199 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1203#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1204 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1208#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1209 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1214#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1215 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1227 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
1228 if (psk_ciphers == NULL) {
1229 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1232 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
1233 if (pki_ciphers == NULL) {
1234 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1235 mbedtls_free(psk_ciphers);
1239#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1240 ecjpake_ciphers = mbedtls_malloc(ecjpake_count *
sizeof(ecjpake_ciphers[0]));
1241 if (ecjpake_ciphers == NULL) {
1242 coap_log_err(
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1243 mbedtls_free(psk_ciphers);
1244 mbedtls_free(pki_ciphers);
1251 psk_list = psk_ciphers;
1252 pki_list = pki_ciphers;
1253#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1254 ecjpake_list = ecjpake_ciphers;
1258 const mbedtls_ssl_ciphersuite_t *cur =
1259 mbedtls_ssl_ciphersuite_from_id(*list);
1261#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1262 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1266 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1270#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1271 else if (cur->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1272 *ecjpake_list = *list;
1276#if MBEDTLS_VERSION_NUMBER >= 0x03060000
1277 else if (cur->min_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3) {
1284#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1285 else if (coap_ssl_ciphersuite_uses_psk(cur)) {
1300 processed_ciphers = 1;
1304 mbedtls_ssl_conf_ciphersuites(conf, psk_ciphers);
1307 mbedtls_ssl_conf_ciphersuites(conf, pki_ciphers);
1309 case COAP_ENC_ECJPAKE:
1310 mbedtls_ssl_conf_ciphersuites(conf, ecjpake_ciphers);
1320 coap_mbedtls_env_t *m_env) {
1323 coap_mbedtls_context_t *m_context =
1326 m_context->psk_pki_enabled |= IS_CLIENT;
1328 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1329 MBEDTLS_SSL_IS_CLIENT,
1331 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1332 MBEDTLS_SSL_TRANSPORT_STREAM,
1333 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1334 coap_log_err(
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1335 -ret, get_error_string(ret));
1339#if defined(MBEDTLS_SSL_PROTO_DTLS)
1340 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1341 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1344 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1345 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1347 if (m_context->psk_pki_enabled & IS_PSK) {
1348#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1356 if (psk_key == NULL || psk_identity == NULL) {
1357 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1361 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->
s,
1362 psk_key->
length, psk_identity->
s,
1363 psk_identity->
length)) != 0) {
1364 coap_log_err(
"mbedtls_ssl_conf_psk returned -0x%x: '%s'\n",
1365 -ret, get_error_string(ret));
1369 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1371 coap_log_err(
"mbedtls_ssl_set_hostname returned -0x%x: '%s'\n",
1372 -ret, get_error_string(ret));
1378#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1380 m_env->ec_jpake = 1;
1381 set_ciphersuites(&m_env->conf, COAP_ENC_ECJPAKE);
1382#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1383 mbedtls_ssl_conf_max_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1386 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1389 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1394 }
else if ((m_context->psk_pki_enabled & IS_PKI) ||
1395 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1402 if (!(m_context->psk_pki_enabled & IS_PKI)) {
1416 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1417 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1418 &m_env->private_key, m_env, m_context,
1419 c_session, setup_data,
1425#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1428 static const char *alpn_list[] = {
"coap", NULL };
1430 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1436 if (m_context->setup_data.client_sni) {
1437 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1439#if defined(MBEDTLS_SSL_PROTO_DTLS)
1440#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1441 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1444 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1454mbedtls_cleanup(coap_mbedtls_env_t *m_env) {
1459 mbedtls_x509_crt_free(&m_env->cacert);
1460 mbedtls_x509_crt_free(&m_env->public_cert);
1461 mbedtls_pk_free(&m_env->private_key);
1462 mbedtls_entropy_free(&m_env->entropy);
1463 mbedtls_ssl_config_free(&m_env->conf);
1464 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1465 mbedtls_ssl_free(&m_env->ssl);
1466 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1470coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1472 if (!m_env->sent_alert)
1473 mbedtls_ssl_close_notify(&m_env->ssl);
1474 mbedtls_cleanup(m_env);
1475 mbedtls_free(m_env);
1479#if COAP_MAX_LOGGING_LEVEL > 0
1481report_mbedtls_alert(
unsigned char alert) {
1483 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
1484 return ": Bad Record MAC";
1485 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
1486 return ": Handshake failure";
1487 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
1488 return ": No Certificate provided";
1489 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
1490 return ": Certificate is bad";
1491 case MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN:
1492 return ": Certificate is unknown";
1493 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
1494 return ": CA is unknown";
1495 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
1496 return ": Access was denied";
1497 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
1498 return ": Decrypt error";
1512 coap_mbedtls_env_t *m_env) {
1516 ret = mbedtls_ssl_handshake(&m_env->ssl);
1519 m_env->established = 1;
1523#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1524#if COAP_CLIENT_SUPPORT
1527 coap_mbedtls_context_t *m_context;
1531 m_context->setup_data.use_cid) {
1532 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1534 size_t peer_cid_len;
1537 if (mbedtls_ssl_get_peer_cid(&m_env->ssl, &enabled, peer_cid, &peer_cid_len) == 0 &&
1538 enabled == MBEDTLS_SSL_CID_ENABLED) {
1549 case MBEDTLS_ERR_SSL_WANT_READ:
1550 case MBEDTLS_ERR_SSL_WANT_WRITE:
1551 if (m_env->ssl.state == MBEDTLS_SSL_SERVER_HELLO
1552#
if MBEDTLS_VERSION_NUMBER >= 0x03030000
1553 || m_env->ssl.state == MBEDTLS_SSL_NEW_SESSION_TICKET
1556 if (++m_env->server_hello_cnt > 10) {
1564 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1567 case MBEDTLS_ERR_SSL_INVALID_MAC:
1569#ifdef MBEDTLS_2_X_COMPAT
1570 case MBEDTLS_ERR_SSL_UNKNOWN_CIPHER:
1572 case MBEDTLS_ERR_SSL_DECODE_ERROR:
1575 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1576 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1578#ifdef MBEDTLS_2_X_COMPAT
1579 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1580 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1581 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1584 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1586 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1587 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1590 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1592 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1593 case MBEDTLS_ERR_SSL_CONN_EOF:
1594 case MBEDTLS_ERR_NET_CONN_RESET:
1600 "returned -0x%x: '%s'\n",
1601 -ret, get_error_string(ret));
1608 mbedtls_ssl_send_alert_message(&m_env->ssl,
1609 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1611 m_env->sent_alert = 1;
1616 get_error_string(ret));
1618 mbedtls_ssl_session_reset(&m_env->ssl);
1619#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1620 if (m_env->ec_jpake) {
1623#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1629#elif COAP_CLIENT_SUPPORT
1635 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1643mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1676#if !COAP_DISABLE_TCP
1684coap_sock_read(
void *ctx,
unsigned char *out,
size_t outl) {
1685 int ret = MBEDTLS_ERR_SSL_CONN_EOF;
1692 if (errno == ECONNRESET) {
1694 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1696 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1698 }
else if (ret == 0) {
1700 ret = MBEDTLS_ERR_SSL_WANT_READ;
1713coap_sock_write(
void *context,
const unsigned char *in,
size_t inl) {
1718 (
const uint8_t *)in,
1724 (errno == EPIPE || errno == ECONNRESET)) {
1739 int lasterror = WSAGetLastError();
1741 if (lasterror == WSAEWOULDBLOCK) {
1742 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1743 }
else if (lasterror == WSAECONNRESET) {
1744 ret = MBEDTLS_ERR_NET_CONN_RESET;
1747 if (errno == EAGAIN || errno == EINTR) {
1748 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1749 }
else if (errno == EPIPE || errno == ECONNRESET) {
1750 ret = MBEDTLS_ERR_NET_CONN_RESET;
1754 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1763 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1769static coap_mbedtls_env_t *
1774 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1779 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1783 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1785 mbedtls_ssl_init(&m_env->ssl);
1786 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1787 mbedtls_ssl_config_init(&m_env->conf);
1788 mbedtls_entropy_init(&m_env->entropy);
1790#if defined(MBEDTLS_PSA_CRYPTO_C)
1794#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1795 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1797 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1798 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1799 if (ret != MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) {
1800 coap_log_info(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1801 -ret, get_error_string(ret));
1804 coap_log_err(
"mbedtls_ctr_drbg_seed returned -0x%x: '%s'\n",
1805 -ret, get_error_string(ret));
1809#if COAP_CLIENT_SUPPORT
1810 if (setup_client_ssl_session(c_session, m_env) != 0) {
1817#if defined(MBEDTLS_SSL_SRV_C)
1818 if (setup_server_ssl_session(c_session, m_env) != 0) {
1828#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1829 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1831 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1832 MBEDTLS_SSL_MINOR_VERSION_3);
1835 if (mbedtls_ssl_setup(&m_env->ssl, &m_env->conf) != 0) {
1839 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1840 coap_dgram_read, NULL);
1841#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1844#if COAP_CLIENT_SUPPORT
1845 coap_mbedtls_context_t *m_context =
1849 m_context->setup_data.use_cid) {
1857 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
1861#if COAP_SERVER_SUPPORT
1862 uint8_t cid[COAP_DTLS_CID_LENGTH];
1871 mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, cid,
1879#if !COAP_DISABLE_TCP
1882 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1883 coap_sock_read, NULL);
1886#ifdef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1887 coap_mbedtls_context_t *m_context =
1889 if ((m_context->psk_pki_enabled & IS_PSK) &&
1893#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
1899#elif COAP_CLIENT_SUPPORT
1904 mbedtls_ssl_set_hs_ecjpake_password(&m_env->ssl, psk_key->
s, psk_key->
length);
1907 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1908 mbedtls_timing_set_delay,
1909 mbedtls_timing_get_delay);
1911 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1916 mbedtls_free(m_env);
1923#if defined(MBEDTLS_SSL_PROTO_DTLS)
1926 static int reported = 0;
1930 " - update Mbed TLS to include DTLS\n");
1938#if !COAP_DISABLE_TCP
1987#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
1994#if COAP_CLIENT_SUPPORT
1997#ifdef MBEDTLS_SSL_DTLS_CONNECTION_ID
2010 coap_mbedtls_context_t *m_context;
2013 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
2015 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
2020#if COAP_SERVER_SUPPORT
2029 coap_mbedtls_context_t *m_context =
2032#if !defined(MBEDTLS_SSL_SRV_C)
2034 " libcoap not compiled for Server Mode for Mbed TLS"
2035 " - update Mbed TLS to include Server Mode\n");
2038 if (!m_context || !setup_data)
2042#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2043 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
2046 m_context->psk_pki_enabled |= IS_PSK;
2051#if COAP_CLIENT_SUPPORT
2060#if !defined(MBEDTLS_SSL_CLI_C)
2065 " libcoap not compiled for Client Mode for Mbed TLS"
2066 " - update Mbed TLS to include Client Mode\n");
2069 coap_mbedtls_context_t *m_context =
2072 if (!m_context || !setup_data)
2076 coap_log_warn(
"CoAP Client with Mbed TLS does not support Identity Hint selection\n");
2079#ifndef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2080 coap_log_warn(
"Mbed TLS not compiled for EC-JPAKE support\n");
2084#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2085 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2088 m_context->psk_pki_enabled |= IS_PSK;
2098 coap_mbedtls_context_t *m_context =
2101 m_context->setup_data = *setup_data;
2102 if (!m_context->setup_data.verify_peer_cert) {
2104 m_context->setup_data.check_common_ca = 0;
2106 m_context->setup_data.allow_self_signed = 1;
2107 m_context->setup_data.allow_expired_certs = 1;
2108 m_context->setup_data.cert_chain_validation = 1;
2109 m_context->setup_data.cert_chain_verify_depth = 10;
2110 m_context->setup_data.check_cert_revocation = 1;
2111 m_context->setup_data.allow_no_crl = 1;
2112 m_context->setup_data.allow_expired_crl = 1;
2113 m_context->setup_data.allow_bad_md_hash = 1;
2114 m_context->setup_data.allow_short_rsa_length = 1;
2116 m_context->psk_pki_enabled |= IS_PKI;
2118#ifndef MBEDTLS_SSL_DTLS_CONNECTION_ID
2119 coap_log_warn(
"Mbed TLS not compiled for Connection-ID support\n");
2127 const char *ca_file,
2128 const char *ca_path) {
2129 coap_mbedtls_context_t *m_context =
2133 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
2138 if (ca_file == NULL && ca_path == NULL) {
2139 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
2143 if (m_context->root_ca_file) {
2144 mbedtls_free(m_context->root_ca_file);
2145 m_context->root_ca_file = NULL;
2149 m_context->root_ca_file = mbedtls_strdup(ca_file);
2152 if (m_context->root_ca_path) {
2153 mbedtls_free(m_context->root_ca_path);
2154 m_context->root_ca_path = NULL;
2158 m_context->root_ca_path = mbedtls_strdup(ca_path);
2169 coap_mbedtls_context_t *m_context =
2173 coap_log_warn(
"coap_context_load_pki_trust_store: (D)TLS environment "
2177 m_context->trust_store_defined = 1;
2186 coap_mbedtls_context_t *m_context =
2188 return m_context->psk_pki_enabled ? 1 : 0;
2193 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
2196 for (i = 0; i < m_context->pki_sni_count; i++) {
2197 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
2199 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
2201 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
2203 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
2205 if (m_context->pki_sni_entry_list)
2206 mbedtls_free(m_context->pki_sni_entry_list);
2208 for (i = 0; i < m_context->psk_sni_count; i++) {
2209 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
2211 if (m_context->psk_sni_entry_list)
2212 mbedtls_free(m_context->psk_sni_entry_list);
2214 if (m_context->root_ca_path)
2215 mbedtls_free(m_context->root_ca_path);
2216 if (m_context->root_ca_file)
2217 mbedtls_free(m_context->root_ca_file);
2219 mbedtls_free(m_context);
2222#if COAP_CLIENT_SUPPORT
2225#if !defined(MBEDTLS_SSL_CLI_C)
2228 " libcoap not compiled for Client Mode for Mbed TLS"
2229 " - update Mbed TLS to include Client Mode\n");
2232 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2241 m_env->last_timeout = now;
2242 ret = do_mbedtls_handshake(c_session, m_env);
2244 coap_dtls_free_mbedtls_env(m_env);
2253#if COAP_SERVER_SUPPORT
2256#if !defined(MBEDTLS_SSL_SRV_C)
2259 " libcoap not compiled for Server Mode for Mbed TLS"
2260 " - update Mbed TLS to include Server Mode\n");
2263 coap_mbedtls_env_t *m_env =
2264 (coap_mbedtls_env_t *)c_session->
tls;
2266#if defined(MBEDTLS_SSL_PROTO_DTLS)
2267#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2268 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2279 if (c_session && c_session->
context && c_session->
tls) {
2280 coap_dtls_free_mbedtls_env(c_session->
tls);
2281 c_session->
tls = NULL;
2289#if defined(MBEDTLS_SSL_PROTO_DTLS)
2290 coap_mbedtls_env_t *m_env =
2291 (coap_mbedtls_env_t *)c_session->
tls;
2293#if MBEDTLS_VERSION_NUMBER >= 0x02100100
2294 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
2304 const uint8_t *data,
size_t data_len) {
2306 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2308 assert(m_env != NULL);
2316 if (m_env->established) {
2317 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char *) data, data_len);
2320 case MBEDTLS_ERR_SSL_WANT_READ:
2321 case MBEDTLS_ERR_SSL_WANT_WRITE:
2324 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2330 "returned -0x%x: '%s'\n",
2331 -ret, get_error_string(ret));
2340 ret = do_mbedtls_handshake(c_session, m_env);
2373 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2374 int ret = mbedtls_timing_get_delay(&m_env->timer);
2375 unsigned int scalar = 1 << m_env->retry_scalar;
2385 m_env->last_timeout = now;
2398 m_env->last_timeout = now;
2416 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2419 m_env->retry_scalar++;
2421 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2436 const uint8_t *data,
2441 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2442 coap_ssl_t *ssl_data;
2444 assert(m_env != NULL);
2446 ssl_data = &m_env->coap_ssl_data;
2447 if (ssl_data->pdu_len) {
2448 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2451 ssl_data->pdu = data;
2452 ssl_data->pdu_len = (unsigned)data_len;
2454 if (m_env->established) {
2455#if COAP_CONSTRAINED_STACK
2468 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
2477 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2478 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2481 case MBEDTLS_ERR_SSL_WANT_READ:
2485 "returned -0x%x: '%s' (length %zd)\n",
2486 -ret, get_error_string(ret), data_len);
2491 ret = do_mbedtls_handshake(c_session, m_env);
2496 if (ssl_data->pdu_len) {
2498 ret = do_mbedtls_handshake(c_session, m_env);
2519 if (ssl_data && ssl_data->pdu_len) {
2521 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2522 ssl_data->pdu_len = 0;
2523 ssl_data->pdu = NULL;
2528#if COAP_SERVER_SUPPORT
2536 const uint8_t *data,
2538#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2543 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2544 " - update Mbed TLS to include DTLS and Server Mode\n");
2547 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2548 coap_ssl_t *ssl_data;
2555 c_session->
tls = m_env;
2562 if ((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2565 coap_log_err(
"mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2566 -ret, get_error_string(ret));
2570 ssl_data = &m_env->coap_ssl_data;
2571 if (ssl_data->pdu_len) {
2572 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2575 ssl_data->pdu = data;
2576 ssl_data->pdu_len = (unsigned)data_len;
2578 ret = do_mbedtls_handshake(c_session, m_env);
2579 if (ret == 0 || m_env->seen_client_hello) {
2585 m_env->seen_client_hello = 0;
2591 if (ssl_data->pdu_len) {
2593 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2594 ssl_data->pdu_len = 0;
2595 ssl_data->pdu = NULL;
2604 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2605 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2607 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2613#if !COAP_DISABLE_TCP
2614#if COAP_CLIENT_SUPPORT
2617#if !defined(MBEDTLS_SSL_CLI_C)
2621 " libcoap not compiled for Client Mode for Mbed TLS"
2622 " - update Mbed TLS to include Client Mode\n");
2625 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2635 m_env->last_timeout = now;
2636 c_session->
tls = m_env;
2637 ret = do_mbedtls_handshake(c_session, m_env);
2647#if COAP_SERVER_SUPPORT
2650#if !defined(MBEDTLS_SSL_SRV_C)
2655 " libcoap not compiled for Server Mode for Mbed TLS"
2656 " - update Mbed TLS to include Server Mode\n");
2659 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2667 c_session->
tls = m_env;
2668 ret = do_mbedtls_handshake(c_session, m_env);
2693 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2694 size_t amount_sent = 0;
2696 assert(m_env != NULL);
2703 if (m_env->established) {
2704 while (amount_sent < data_len) {
2705 ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent],
2706 data_len - amount_sent);
2709 case MBEDTLS_ERR_SSL_WANT_READ:
2710 case MBEDTLS_ERR_SSL_WANT_WRITE:
2717 case MBEDTLS_ERR_NET_CONN_RESET:
2718 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2723 "returned -0x%x: '%s'\n",
2724 -ret, get_error_string(ret));
2736 ret = do_mbedtls_handshake(c_session, m_env);
2757 if (ret == (ssize_t)data_len)
2776 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2785 if (!m_env->established && !m_env->sent_alert) {
2786 ret = do_mbedtls_handshake(c_session, m_env);
2795 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2799 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2803 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2805 m_env->sent_alert = 1;
2808#if MBEDTLS_VERSION_NUMBER >= 0x03060000
2809 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
2811 case MBEDTLS_ERR_SSL_WANT_READ:
2817 "returned -0x%x: '%s' (length %zd)\n",
2818 -ret, get_error_string(ret), data_len);
2822 }
else if (ret < (
int)data_len) {
2823 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
2851#if COAP_CLIENT_SUPPORT
2852 mbedtls_free(psk_ciphers);
2853 mbedtls_free(pki_ciphers);
2854 mbedtls_free(ecjpake_ciphers);
2857 ecjpake_ciphers = NULL;
2858 processed_ciphers = 0;
2868 if (c_session && c_session->
tls) {
2869 coap_mbedtls_env_t *m_env;
2872 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
2874 return (
void *)&m_env->ssl;
2883#if !defined(ESPIDF_VERSION)
2893 switch ((
int)level) {
2914 mbedtls_debug_set_threshold(use_level);
2916 keep_log_level = level;
2921 return keep_log_level;
2927 version.
version = mbedtls_version_get_number();
2933#if COAP_SERVER_SUPPORT
2936 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2939 mbedtls_sha256_init(digest_ctx);
2940#ifdef MBEDTLS_2_X_COMPAT
2941 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2943 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2955 mbedtls_sha256_free(digest_ctx);
2956 mbedtls_free(digest_ctx);
2962 const uint8_t *data,
2964#ifdef MBEDTLS_2_X_COMPAT
2965 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2967 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2976#ifdef MBEDTLS_2_X_COMPAT
2977 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t *)digest_buffer);
2979 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t *)digest_buffer);
2987#include <mbedtls/cipher.h>
2988#include <mbedtls/md.h>
2990#ifndef MBEDTLS_CIPHER_MODE_AEAD
2991#error need MBEDTLS_CIPHER_MODE_AEAD, please enable MBEDTLS_CCM_C
2994#ifdef MBEDTLS_ERROR_C
2995#include <mbedtls/error.h>
2998#ifdef MBEDTLS_ERROR_C
3001 int c_tmp = (int)(Func); \
3003 char error_buf[64]; \
3004 mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \
3005 coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \
3012 int c_tmp = (int)(Func); \
3014 coap_log_err("mbedtls: %d\n", tmp); \
3025static struct hash_algs {
3027 mbedtls_md_type_t hash_type;
3035static mbedtls_md_type_t
3036get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3039 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3040 if (hashs[idx].alg == alg) {
3041 *hash_len = hashs[idx].hash_size;
3042 return hashs[idx].hash_type;
3045 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3046 return MBEDTLS_MD_NONE;
3053 mbedtls_md_context_t ctx;
3055 const mbedtls_md_info_t *md_info;
3059 mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length);
3061 if (dig_type == MBEDTLS_MD_NONE) {
3062 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3065 md_info = mbedtls_md_info_from_type(dig_type);
3067 len = mbedtls_md_get_size(md_info);
3072 mbedtls_md_init(&ctx);
3073 C(mbedtls_md_setup(&ctx, md_info, 0));
3075 C(mbedtls_md_starts(&ctx));
3076 C(mbedtls_md_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3080 C(mbedtls_md_finish(&ctx,
dummy->s));
3085 mbedtls_md_free(&ctx);
3090#if COAP_OSCORE_SUPPORT
3101static struct cipher_algs {
3103 mbedtls_cipher_type_t cipher_type;
3108static mbedtls_cipher_type_t
3112 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3113 if (ciphers[idx].alg == alg)
3114 return ciphers[idx].cipher_type;
3116 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3125static struct hmac_algs {
3127 mbedtls_md_type_t hmac_type;
3134static mbedtls_md_type_t
3138 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3139 if (hmacs[idx].hmac_alg == hmac_alg)
3140 return hmacs[idx].hmac_type;
3142 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3148 return get_cipher_alg(alg) != 0;
3157 return get_hmac_alg(hmac_alg) != 0;
3165setup_cipher_context(mbedtls_cipher_context_t *ctx,
3167 const uint8_t *key_data,
3169 mbedtls_operation_t mode) {
3170 const mbedtls_cipher_info_t *cipher_info;
3171 mbedtls_cipher_type_t cipher_type;
3175 memset(key, 0,
sizeof(key));
3177 if ((cipher_type = get_cipher_alg(coap_alg)) == 0) {
3178 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3182 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
3184 coap_log_crit(
"coap_crypto_encrypt: cannot get cipher info\n");
3188 mbedtls_cipher_init(ctx);
3190 C(mbedtls_cipher_setup(ctx, cipher_info));
3191 klen = mbedtls_cipher_get_key_bitlen(ctx);
3192 if ((klen > (
int)(
sizeof(key) * 8)) || (key_length >
sizeof(key))) {
3196 memcpy(key, key_data, key_length);
3197 C(mbedtls_cipher_setkey(ctx, key, klen, mode));
3202 mbedtls_cipher_free(ctx);
3211 size_t *max_result_len) {
3212 mbedtls_cipher_context_t ctx;
3214#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3215 unsigned char tag[16];
3218 size_t result_len = *max_result_len;
3224 assert(params != NULL);
3231 if (!setup_cipher_context(&ctx,
3246#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3247 C(mbedtls_cipher_auth_encrypt(&ctx,
3260 if ((result_len + ccm->
tag_len) > *max_result_len) {
3265 memcpy(result + result_len, tag, ccm->
tag_len);
3266 *max_result_len = result_len + ccm->
tag_len;
3269 C(mbedtls_cipher_auth_encrypt_ext(&ctx,
3281 *max_result_len = result_len;
3286 mbedtls_cipher_free(&ctx);
3295 size_t *max_result_len) {
3296 mbedtls_cipher_context_t ctx;
3298#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3299 const unsigned char *tag;
3302 size_t result_len = *max_result_len;
3308 assert(params != NULL);
3316 if (!setup_cipher_context(&ctx,
3336#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
3338 C(mbedtls_cipher_auth_decrypt(&ctx,
3351 C(mbedtls_cipher_auth_decrypt_ext(&ctx,
3366 *max_result_len = result_len;
3369 mbedtls_cipher_free(&ctx);
3378 mbedtls_md_context_t ctx;
3380 const int use_hmac = 1;
3381 const mbedtls_md_info_t *md_info;
3382 mbedtls_md_type_t mac_algo;
3390 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3391 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3394 md_info = mbedtls_md_info_from_type(mac_algo);
3396 len = mbedtls_md_get_size(md_info);
3401 mbedtls_md_init(&ctx);
3402 C(mbedtls_md_setup(&ctx, md_info, use_hmac));
3404 C(mbedtls_md_hmac_starts(&ctx, key->
s, key->
length));
3405 C(mbedtls_md_hmac_update(&ctx, (
const unsigned char *)data->
s, data->
length));
3409 C(mbedtls_md_hmac_finish(&ctx,
dummy->s));
3414 mbedtls_md_free(&ctx);
3426#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_load_pki_trust_store(coap_context_t *ctx 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 allow_no_crl
1 ignore if CRL not there
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 is_rpk_not_cert
1 is RPK instead of Public Certificate.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
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