46#include <mbedtls/version.h>
49#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
50#define MBEDTLS_2_X_COMPAT
53#ifndef MBEDTLS_ALLOW_PRIVATE_ACCESS
54#define MBEDTLS_ALLOW_PRIVATE_ACCESS
58#include <mbedtls/platform.h>
59#include <mbedtls/net_sockets.h>
60#include <mbedtls/ssl.h>
61#include <mbedtls/entropy.h>
62#include <mbedtls/ctr_drbg.h>
63#include <mbedtls/error.h>
64#include <mbedtls/timing.h>
65#include <mbedtls/ssl_cookie.h>
66#include <mbedtls/oid.h>
67#include <mbedtls/debug.h>
68#include <mbedtls/sha256.h>
69#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
70#include <mbedtls/esp_debug.h>
80#define mbedtls_malloc(a) malloc(a)
81#define mbedtls_realloc(a,b) realloc(a,b)
82#define mbedtls_strdup(a) strdup(a)
83#define mbedtls_strndup(a,b) strndup(a,b)
85#ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
87#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
88#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
92#if ! COAP_SERVER_SUPPORT
93#undef MBEDTLS_SSL_SRV_C
95#if ! COAP_CLIENT_SUPPORT
96#undef MBEDTLS_SSL_CLI_C
100#define strcasecmp _stricmp
103#define IS_PSK (1 << 0)
104#define IS_PKI (1 << 1)
105#define IS_CLIENT (1 << 6)
106#define IS_SERVER (1 << 7)
108typedef struct coap_ssl_t {
119typedef struct coap_mbedtls_env_t {
120 mbedtls_ssl_context ssl;
121 mbedtls_entropy_context entropy;
122 mbedtls_ctr_drbg_context ctr_drbg;
123 mbedtls_ssl_config conf;
124 mbedtls_timing_delay_context timer;
125 mbedtls_x509_crt cacert;
126 mbedtls_x509_crt public_cert;
127 mbedtls_pk_context private_key;
128 mbedtls_ssl_cookie_ctx cookie_ctx;
132 int seen_client_hello;
134 unsigned int retry_scalar;
135 coap_ssl_t coap_ssl_data;
138typedef struct pki_sni_entry {
141 mbedtls_x509_crt cacert;
142 mbedtls_x509_crt public_cert;
143 mbedtls_pk_context private_key;
146typedef struct psk_sni_entry {
151typedef struct coap_mbedtls_context_t {
153 size_t pki_sni_count;
154 pki_sni_entry *pki_sni_entry_list;
155 size_t psk_sni_count;
156 psk_sni_entry *psk_sni_entry_list;
160} coap_mbedtls_context_t;
162typedef enum coap_enc_method_t {
167#ifndef MBEDTLS_2_X_COMPAT
171static int coap_rng(
void *ctx
COAP_UNUSED,
unsigned char *buf,
size_t len)
173 return coap_prng(buf, len) ? 0 : MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
177static int coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl)
183 if (!c_session->
tls) {
185 return MBEDTLS_ERR_SSL_WANT_READ;
187 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
190 if (data->pdu_len > 0) {
191 if (outl < data->pdu_len) {
192 memcpy(out, data->pdu, outl);
195 data->pdu_len -= outl;
198 memcpy(out, data->pdu, data->pdu_len);
200 if (!data->peekmode) {
207 ret = MBEDTLS_ERR_SSL_WANT_READ;
221coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
222 size_t send_buffer_length)
228 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
230 if (result != (ssize_t)send_buffer_length) {
232 result, send_buffer_length);
238 m_env->last_timeout = now;
246#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
250static int psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
251 const unsigned char *identity,
size_t identity_len )
255 coap_mbedtls_env_t *m_env;
259 if (c_session == NULL)
263 lidentity.
s = identity ? (
const uint8_t*)identity : (
const uint8_t*)
"";
264 lidentity.
length = identity ? identity_len : 0;
268 (
int)lidentity.
length, (
const char *)lidentity.
s);
270 m_env = (coap_mbedtls_env_t *)c_session->
tls;
286 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
287 m_env->seen_client_hello = 1;
293get_san_or_cn_from_cert(mbedtls_x509_crt *crt)
296 const mbedtls_asn1_named_data * cn_data;
298 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
299 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
300 while (seq && seq->buf.p == NULL) {
305 return mbedtls_strndup((
const char *)seq->buf.p,
310 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
312 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
315 return mbedtls_strndup((
const char *)cn_data->val.p,
323get_error_string(
int ret) {
324 static char buf[128] = {0};
325 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
330self_signed_cert_verify_callback_mbedtls(
void *data,
336 const coap_mbedtls_context_t *m_context =
340 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
341 if (setup_data->allow_expired_certs) {
342 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
353cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
354 int depth, uint32_t *flags)
357 coap_mbedtls_context_t *m_context =
365 cn = get_san_or_cn_from_cert(crt);
367 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
368 if (setup_data->allow_expired_certs) {
369 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
371 " %s: %s: overridden: '%s' depth %d\n",
373 "The certificate has expired", cn ? cn :
"?", depth);
376 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
377 if (setup_data->allow_expired_certs) {
378 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
380 " %s: %s: overridden: '%s' depth %d\n",
382 "The certificate has a future date", cn ? cn :
"?", depth);
385 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
386 if (setup_data->allow_bad_md_hash) {
387 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
389 " %s: %s: overridden: '%s' depth %d\n",
391 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
394 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
395 if (setup_data->allow_short_rsa_length) {
396 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
398 " %s: %s: overridden: '%s' depth %d\n",
400 "The certificate has a short RSA length", cn ? cn :
"?", depth);
403 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
405 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
406 self_signed_cert_verify_callback_mbedtls,
408 if (self_signed && depth == 0) {
409 if (setup_data->allow_self_signed &&
410 !setup_data->check_common_ca) {
411 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
413 " %s: %s: overridden: '%s' depth %d\n",
416 cn ? cn :
"?", depth);
420 if (!setup_data->verify_peer_cert) {
421 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
423 " %s: %s: overridden: '%s' depth %d\n",
425 "The certificate's CA does not match", cn ? cn :
"?", depth);
429 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
430 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
431 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
433 " %s: %s: overridden: '%s' depth %d\n",
435 "The certificate's CRL has expired", cn ? cn :
"?", depth);
437 else if (!setup_data->check_cert_revocation) {
438 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
441 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
442 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
443 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
445 " %s: %s: overridden: '%s' depth %d\n",
447 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
449 else if (!setup_data->check_cert_revocation) {
450 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
453 if (setup_data->cert_chain_validation &&
454 depth > (setup_data->cert_chain_verify_depth + 1)) {
455 *flags |= MBEDTLS_X509_BADCERT_OTHER;
457 " %s: %s: '%s' depth %d\n",
459 "The certificate's verify depth is too long",
460 cn ? cn :
"?", depth);
463 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
464 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
466 if (setup_data->validate_cn_call_back) {
467 if (!setup_data->validate_cn_call_back(cn,
473 setup_data->cn_call_back_arg)) {
474 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
480 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
483 tcp = strchr(buf,
'\n');
487 " %s: %s: issue 0x%" PRIx32
": '%s' depth %d\n",
489 buf, *flags, cn ? cn :
"?", depth);
490 tcp = strchr(tcp+1,
'\n');
494 coap_log(
LOG_ERR,
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
495 -ret, get_error_string(ret));
506setup_pki_credentials(mbedtls_x509_crt *cacert,
507 mbedtls_x509_crt *public_cert,
508 mbedtls_pk_context *private_key,
509 coap_mbedtls_env_t *m_env,
510 coap_mbedtls_context_t *m_context,
519 "RPK Support not available in Mbed TLS\n");
529 mbedtls_x509_crt_init(public_cert);
530 mbedtls_pk_init(private_key);
532 ret = mbedtls_x509_crt_parse_file(public_cert,
535 coap_log(
LOG_ERR,
"mbedtls_x509_crt_parse_file returned -0x%x: '%s'\n",
536 -ret, get_error_string(ret));
540#ifdef MBEDTLS_2_X_COMPAT
541 ret = mbedtls_pk_parse_keyfile(private_key,
544 ret = mbedtls_pk_parse_keyfile(private_key,
546 NULL, coap_rng, (
void *)&m_env->ctr_drbg);
550 -ret, get_error_string(ret));
554 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
557 -ret, get_error_string(ret));
563 "***setup_pki: (D)TLS: No Server Certificate + Private "
570 mbedtls_x509_crt_init(cacert);
571 ret = mbedtls_x509_crt_parse_file(cacert,
575 -ret, get_error_string(ret));
578 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
589 mbedtls_x509_crt_init(public_cert);
590 mbedtls_pk_init(private_key);
595 buffer = mbedtls_malloc(length + 1);
598 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
601 buffer[length] =
'\000';
603 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
604 mbedtls_free(buffer);
607 ret = mbedtls_x509_crt_parse(public_cert,
613 -ret, get_error_string(ret));
620 buffer = mbedtls_malloc(length + 1);
623 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
626 buffer[length] =
'\000';
628#ifdef MBEDTLS_2_X_COMPAT
629 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
631 ret = mbedtls_pk_parse_key(private_key, buffer, length,
632 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
634 mbedtls_free(buffer);
637#ifdef MBEDTLS_2_X_COMPAT
638 ret = mbedtls_pk_parse_key(private_key,
642 ret = mbedtls_pk_parse_key(private_key,
645 NULL, 0, coap_rng, (
void *)&m_env->ctr_drbg);
650 -ret, get_error_string(ret));
654 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
657 -ret, get_error_string(ret));
662 "***setup_pki: (D)TLS: No Server Certificate + Private "
672 mbedtls_x509_crt_init(cacert);
676 buffer = mbedtls_malloc(length + 1);
679 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
682 buffer[length] =
'\000';
684 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
685 mbedtls_free(buffer);
688 ret = mbedtls_x509_crt_parse(cacert,
694 -ret, get_error_string(ret));
697 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
706 mbedtls_x509_crt_init(public_cert);
707 mbedtls_pk_init(private_key);
708 ret = mbedtls_x509_crt_parse(public_cert,
713 -ret, get_error_string(ret));
717#ifdef MBEDTLS_2_X_COMPAT
718 ret = mbedtls_pk_parse_key(private_key,
722 ret = mbedtls_pk_parse_key(private_key,
725 (
void *)&m_env->ctr_drbg);
729 -ret, get_error_string(ret));
733 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
736 -ret, get_error_string(ret));
741 "***setup_pki: (D)TLS: No Server Certificate + Private "
748 mbedtls_x509_crt_init(cacert);
749 ret = mbedtls_x509_crt_parse(cacert,
754 -ret, get_error_string(ret));
757 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
763 "***setup_pki: (D)TLS: PKCS11 not currently supported\n");
768 "***setup_pki: (D)TLS: Unknown key type %d\n",
773 if (m_context->root_ca_file) {
774 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
777 -ret, get_error_string(ret));
780 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
782 if (m_context->root_ca_path) {
783 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
786 -ret, get_error_string(ret));
789 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
792#if defined(MBEDTLS_SSL_SRV_C)
793 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
795 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
796 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
799 MBEDTLS_SSL_VERIFY_REQUIRED :
800 MBEDTLS_SSL_VERIFY_NONE);
805 mbedtls_ssl_conf_verify(&m_env->conf,
806 cert_verify_callback_mbedtls, c_session);
811#if defined(MBEDTLS_SSL_SRV_C)
816pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
817 const unsigned char *uname,
size_t name_len)
822 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
823 coap_mbedtls_context_t *m_context =
828 name = mbedtls_malloc(name_len+1);
832 memcpy(name, uname, name_len);
833 name[name_len] =
'\000';
836 for (i = 0; i < m_context->pki_sni_count; i++) {
837 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
841 if (i == m_context->pki_sni_count) {
846 pki_sni_entry *pki_sni_entry_list;
849 m_context->setup_data.validate_sni_call_back(name,
850 m_context->setup_data.sni_call_back_arg);
856 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
857 (i+1)*
sizeof(pki_sni_entry));
859 if (pki_sni_entry_list == NULL) {
863 m_context->pki_sni_entry_list = pki_sni_entry_list;
864 memset(&m_context->pki_sni_entry_list[i], 0,
865 sizeof(m_context->pki_sni_entry_list[i]));
866 m_context->pki_sni_entry_list[i].sni = name;
867 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
868 sni_setup_data = m_context->setup_data;
869 sni_setup_data.
pki_key = *new_entry;
870 if ((ret = setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
871 &m_context->pki_sni_entry_list[i].public_cert,
872 &m_context->pki_sni_entry_list[i].private_key,
881 m_context->pki_sni_count++;
887 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
889 return mbedtls_ssl_set_hs_own_cert(ssl,
890 &m_context->pki_sni_entry_list[i].public_cert,
891 &m_context->pki_sni_entry_list[i].private_key);
894#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
899psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
900 const unsigned char *uname,
size_t name_len)
904 coap_mbedtls_context_t *m_context =
908 name = mbedtls_malloc(name_len+1);
912 memcpy(name, uname, name_len);
913 name[name_len] =
'\000';
916 for (i = 0; i < m_context->psk_sni_count; i++) {
917 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
921 if (i == m_context->psk_sni_count) {
926 psk_sni_entry *psk_sni_entry_list;
937 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
938 (i+1)*
sizeof(psk_sni_entry));
940 if (psk_sni_entry_list == NULL) {
944 m_context->psk_sni_entry_list = psk_sni_entry_list;
945 m_context->psk_sni_entry_list[i].sni = name;
946 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
948 m_context->psk_sni_count++;
955 &m_context->psk_sni_entry_list[i].psk_info.hint);
957 &m_context->psk_sni_entry_list[i].psk_info.key);
958 return mbedtls_ssl_set_hs_psk(ssl,
959 m_context->psk_sni_entry_list[i].psk_info.key.s,
960 m_context->psk_sni_entry_list[i].psk_info.key.length);
965 coap_mbedtls_env_t *m_env)
967 coap_mbedtls_context_t *m_context =
970 m_context->psk_pki_enabled |= IS_SERVER;
972 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
973 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
974 MBEDTLS_SSL_IS_SERVER,
976 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
977 MBEDTLS_SSL_TRANSPORT_STREAM,
978 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
979 coap_log(
LOG_ERR,
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
980 -ret, get_error_string(ret));
984 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
986#if defined(MBEDTLS_SSL_PROTO_DTLS)
987 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
988 COAP_DTLS_RETRANSMIT_TOTAL_MS);
991 if (m_context->psk_pki_enabled & IS_PSK) {
992#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
993 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
995 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
1002 if (m_context->psk_pki_enabled & IS_PKI) {
1003 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1004 &m_env->private_key, m_env, m_context,
1005 c_session, &m_context->setup_data,
1011 if (m_context->setup_data.validate_sni_call_back) {
1012 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
1016 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
1017 mbedtls_ctr_drbg_random,
1018 &m_env->ctr_drbg)) != 0) {
1020 -ret, get_error_string(ret));
1024#if defined(MBEDTLS_SSL_PROTO_DTLS)
1025 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
1026 mbedtls_ssl_cookie_check,
1027 &m_env->cookie_ctx );
1028#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1029 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1037#if COAP_CLIENT_SUPPORT
1038static int *psk_ciphers = NULL;
1039static int *pki_ciphers = NULL;
1040static int processed_ciphers = 0;
1043set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method)
1045 if (!processed_ciphers) {
1046 const int *list = mbedtls_ssl_list_ciphersuites();
1047 const int *base = list;
1054 const mbedtls_ssl_ciphersuite_t *cur =
1055 mbedtls_ssl_ciphersuite_from_id(*list);
1057#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1059 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1064 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1068#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1069 else if (mbedtls_ssl_ciphersuite_uses_psk(cur)) {
1081 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
1082 if (psk_ciphers == NULL) {
1083 coap_log(
LOG_ERR,
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
1086 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
1087 if (pki_ciphers == NULL) {
1088 coap_log(
LOG_ERR,
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1089 mbedtls_free(psk_ciphers);
1094 psk_list = psk_ciphers;
1095 pki_list = pki_ciphers;
1098 const mbedtls_ssl_ciphersuite_t *cur =
1099 mbedtls_ssl_ciphersuite_from_id(*list);
1100#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1102 if (cur->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1107 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1111#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1112 else if (mbedtls_ssl_ciphersuite_uses_psk(cur)) {
1127 processed_ciphers = 1;
1129 mbedtls_ssl_conf_ciphersuites(conf, method == COAP_ENC_PSK ? psk_ciphers : pki_ciphers);
1133 coap_mbedtls_env_t *m_env)
1137 coap_mbedtls_context_t *m_context =
1140 m_context->psk_pki_enabled |= IS_CLIENT;
1142 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1143 MBEDTLS_SSL_IS_CLIENT,
1145 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1146 MBEDTLS_SSL_TRANSPORT_STREAM,
1147 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1148 coap_log(
LOG_ERR,
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1149 -ret, get_error_string(ret));
1153#if defined(MBEDTLS_SSL_PROTO_DTLS)
1154 mbedtls_ssl_conf_handshake_timeout(&m_env->conf, COAP_DTLS_RETRANSMIT_MS,
1155 COAP_DTLS_RETRANSMIT_TOTAL_MS);
1158 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1159 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1161 if (m_context->psk_pki_enabled & IS_PSK) {
1162#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1170 if (psk_key == NULL || psk_identity == NULL) {
1171 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1175 if ((ret = mbedtls_ssl_conf_psk(&m_env->conf, psk_key->
s,
1176 psk_key->
length, psk_identity->
s,
1177 psk_identity->
length)) != 0) {
1179 -ret, get_error_string(ret));
1183 if ((ret = mbedtls_ssl_set_hostname(&m_env->ssl,
1186 -ret, get_error_string(ret));
1192 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1197 else if ((m_context->psk_pki_enabled & IS_PKI) ||
1198 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1203 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
1204 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
1205 &m_env->private_key, m_env, m_context,
1206 c_session, &m_context->setup_data,
1212#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1214 static const char *alpn_list[] = {
"coap", NULL };
1216 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1222 if (m_context->setup_data.client_sni) {
1223 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1225#if defined(MBEDTLS_SSL_PROTO_DTLS)
1226#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1227 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1230 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1239static void mbedtls_cleanup(coap_mbedtls_env_t *m_env)
1245 mbedtls_x509_crt_free(&m_env->cacert);
1246 mbedtls_x509_crt_free(&m_env->public_cert);
1247 mbedtls_pk_free(&m_env->private_key);
1248 mbedtls_entropy_free(&m_env->entropy);
1249 mbedtls_ssl_config_free(&m_env->conf);
1250 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1251 mbedtls_ssl_free(&m_env->ssl);
1252 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1256coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1258 if (!m_env->sent_alert)
1259 mbedtls_ssl_close_notify(&m_env->ssl);
1260 mbedtls_cleanup(m_env);
1261 mbedtls_free(m_env);
1266report_mbedtls_alert(
unsigned char alert) {
1268 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
return ": Bad Record MAC";
1269 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
return ": Handshake failure";
1270 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
return ": No Certificate provided";
1271 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
return ": Certificate is bad";
1272 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
return ": CA is unknown";
1273 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
return ": Access was denied";
1274 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
return ": Decrypt error";
1285 coap_mbedtls_env_t *m_env) {
1289 ret = mbedtls_ssl_handshake(&m_env->ssl);
1292 m_env->established = 1;
1297 case MBEDTLS_ERR_SSL_WANT_READ:
1298 case MBEDTLS_ERR_SSL_WANT_WRITE:
1302 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1305 case MBEDTLS_ERR_SSL_INVALID_MAC:
1307 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1308 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1310#ifdef MBEDTLS_2_X_COMPAT
1311 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1312 case MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO:
1313 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1316 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1318 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1319 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1322 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1324 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1325 case MBEDTLS_ERR_SSL_CONN_EOF:
1326 case MBEDTLS_ERR_NET_CONN_RESET:
1332 "do_mbedtls_handshake: session establish "
1333 "returned -0x%x: '%s'\n",
1334 -ret, get_error_string(ret));
1341 mbedtls_ssl_send_alert_message(&m_env->ssl,
1342 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1344 m_env->sent_alert = 1;
1348 "do_mbedtls_handshake: session establish "
1350 get_error_string(ret));
1352 mbedtls_ssl_session_reset(&m_env->ssl);
1357mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1358 const char *file,
int line,
const char *str) {
1381 coap_log(log_level,
"%s:%04d: %s", file, line, str);
1384#if !COAP_DISABLE_TCP
1391coap_sock_read(
void *ctx,
unsigned char *out,
size_t outl) {
1392 ssize_t ret = MBEDTLS_ERR_SSL_CONN_EOF;
1397 ret = recv(c_session->
sock.
fd, (
char *)out, (
int)outl, 0);
1399 ret = recv(c_session->
sock.
fd, out, outl, 0);
1404 }
else if (ret < 0 && errno != EAGAIN) {
1411 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
1412 ret = MBEDTLS_ERR_SSL_CONN_EOF;
1416 int lasterror = WSAGetLastError();
1418 if (lasterror == WSAEWOULDBLOCK) {
1419 ret = MBEDTLS_ERR_SSL_WANT_READ;
1421 else if (lasterror == WSAECONNRESET) {
1422 ret = MBEDTLS_ERR_NET_CONN_RESET;
1425 if (errno == EAGAIN) {
1426 ret = MBEDTLS_ERR_SSL_WANT_READ;
1428 else if (errno == EPIPE || errno == ECONNRESET) {
1429 ret = MBEDTLS_ERR_NET_CONN_RESET;
1433 ret = MBEDTLS_ERR_NET_RECV_FAILED;
1435 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
1437 else if (ret < (ssize_t)outl) {
1438 c_session->
sock.
flags &= ~COAP_SOCKET_CAN_READ;
1450coap_sock_write(
void *context,
const unsigned char *in,
size_t inl) {
1458 }
else if (ret < 0) {
1461 (errno == EPIPE || errno == ECONNRESET)) {
1477 int lasterror = WSAGetLastError();
1479 if (lasterror == WSAEWOULDBLOCK) {
1480 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1482 else if (lasterror == WSAECONNRESET) {
1483 ret = MBEDTLS_ERR_NET_CONN_RESET;
1486 if (errno == EAGAIN || errno == EINTR) {
1487 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1489 else if (errno == EPIPE || errno == ECONNRESET) {
1490 ret = MBEDTLS_ERR_NET_CONN_RESET;
1494 ret = MBEDTLS_ERR_NET_SEND_FAILED;
1503 ret = MBEDTLS_ERR_SSL_WANT_WRITE;
1509static coap_mbedtls_env_t *coap_dtls_new_mbedtls_env(
coap_session_t *c_session,
1514 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1519 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1523 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1525 mbedtls_ssl_init(&m_env->ssl);
1526 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1527 mbedtls_ssl_config_init(&m_env->conf);
1528 mbedtls_entropy_init(&m_env->entropy);
1530#if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1531 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1533 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1534 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1536 -ret, get_error_string(ret));
1541#if COAP_CLIENT_SUPPORT
1542 if (setup_client_ssl_session(c_session, m_env) != 0) {
1549#if defined(MBEDTLS_SSL_SRV_C)
1550 if (setup_server_ssl_session(c_session, m_env) != 0) {
1560#if MBEDTLS_VERSION_NUMBER >= 0x03020000
1561 mbedtls_ssl_conf_min_tls_version(&m_env->conf, MBEDTLS_SSL_VERSION_TLS1_2);
1563 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1564 MBEDTLS_SSL_MINOR_VERSION_3);
1567 if ((ret = mbedtls_ssl_setup(&m_env->ssl, &m_env->conf)) != 0) {
1571 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1572 coap_dgram_read, NULL);
1574#if !COAP_DISABLE_TCP
1577 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_sock_write,
1578 coap_sock_read, NULL);
1581 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1582 mbedtls_timing_set_delay,
1583 mbedtls_timing_get_delay);
1585 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1590 mbedtls_free(m_env);
1596#if defined(MBEDTLS_SSL_PROTO_DTLS)
1599 static int reported = 0;
1603 "libcoap not compiled for DTLS with Mbed TLS"
1604 " - update Mbed TLS to include DTLS\n");
1612#if !COAP_DISABLE_TCP
1621 coap_mbedtls_context_t *m_context;
1624 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
1626 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
1631#if COAP_SERVER_SUPPORT
1640 coap_mbedtls_context_t *m_context =
1643#if !defined(MBEDTLS_SSL_SRV_C)
1645 " libcoap not compiled for Server Mode for Mbed TLS"
1646 " - update Mbed TLS to include Server Mode\n");
1649 if (!m_context || !setup_data)
1652 m_context->psk_pki_enabled |= IS_PSK;
1657#if COAP_CLIENT_SUPPORT
1666#if !defined(MBEDTLS_SSL_CLI_C)
1668 " libcoap not compiled for Client Mode for Mbed TLS"
1669 " - update Mbed TLS to include Client Mode\n");
1672 coap_mbedtls_context_t *m_context =
1675 if (!m_context || !setup_data)
1680 "CoAP Client with Mbed TLS does not support Identity Hint selection\n");
1682 m_context->psk_pki_enabled |= IS_PSK;
1692 coap_mbedtls_context_t *m_context =
1695 m_context->setup_data = *setup_data;
1696 if (!m_context->setup_data.verify_peer_cert) {
1700 m_context->setup_data.allow_self_signed = 1;
1701 m_context->setup_data.allow_expired_certs = 1;
1702 m_context->setup_data.cert_chain_validation = 1;
1703 m_context->setup_data.cert_chain_verify_depth = 10;
1704 m_context->setup_data.check_cert_revocation = 1;
1705 m_context->setup_data.allow_no_crl = 1;
1706 m_context->setup_data.allow_expired_crl = 1;
1707 m_context->setup_data.allow_bad_md_hash = 1;
1708 m_context->setup_data.allow_short_rsa_length = 1;
1710 m_context->psk_pki_enabled |= IS_PKI;
1715 const char *ca_file,
1716 const char *ca_path)
1718 coap_mbedtls_context_t *m_context =
1723 "coap_context_set_pki_root_cas: (D)TLS environment "
1728 if (ca_file == NULL && ca_path == NULL) {
1730 "coap_context_set_pki_root_cas: ca_file and/or ca_path "
1734 if (m_context->root_ca_file) {
1735 mbedtls_free(m_context->root_ca_file);
1736 m_context->root_ca_file = NULL;
1740 m_context->root_ca_file = mbedtls_strdup(ca_file);
1743 if (m_context->root_ca_path) {
1744 mbedtls_free(m_context->root_ca_path);
1745 m_context->root_ca_path = NULL;
1749 m_context->root_ca_path = mbedtls_strdup(ca_path);
1756 coap_mbedtls_context_t *m_context =
1758 return m_context->psk_pki_enabled ? 1 : 0;
1763 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
1766 for (i = 0; i < m_context->pki_sni_count; i++) {
1767 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
1769 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
1771 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
1773 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
1775 if (m_context->pki_sni_entry_list)
1776 mbedtls_free(m_context->pki_sni_entry_list);
1778 for (i = 0; i < m_context->psk_sni_count; i++) {
1779 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
1781 if (m_context->psk_sni_entry_list)
1782 mbedtls_free(m_context->psk_sni_entry_list);
1784 if (m_context->root_ca_path)
1785 mbedtls_free(m_context->root_ca_path);
1786 if (m_context->root_ca_file)
1787 mbedtls_free(m_context->root_ca_file);
1789 mbedtls_free(m_context);
1792#if COAP_CLIENT_SUPPORT
1795#if !defined(MBEDTLS_SSL_CLI_C)
1798 " libcoap not compiled for Client Mode for Mbed TLS"
1799 " - update Mbed TLS to include Client Mode\n");
1802 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
1810 m_env->last_timeout = now;
1811 ret = do_mbedtls_handshake(c_session, m_env);
1813 coap_dtls_free_mbedtls_env(m_env);
1822#if COAP_SERVER_SUPPORT
1825#if !defined(MBEDTLS_SSL_SRV_C)
1828 " libcoap not compiled for Server Mode for Mbed TLS"
1829 " - update Mbed TLS to include Server Mode\n");
1832 coap_mbedtls_env_t *m_env =
1833 (coap_mbedtls_env_t *)c_session->
tls;
1835#if defined(MBEDTLS_SSL_PROTO_DTLS)
1836#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1837 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1848 if (c_session && c_session->
context && c_session->
tls) {
1849 coap_dtls_free_mbedtls_env(c_session->
tls);
1850 c_session->
tls = NULL;
1858#if defined(MBEDTLS_SSL_PROTO_DTLS)
1859 coap_mbedtls_env_t *m_env =
1860 (coap_mbedtls_env_t *)c_session->
tls;
1862#if MBEDTLS_VERSION_NUMBER >= 0x02100100
1863 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1872 const uint8_t *data,
1876 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1878 assert(m_env != NULL);
1884 if (m_env->established) {
1885 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char*) data, data_len);
1888 case MBEDTLS_ERR_SSL_WANT_READ:
1889 case MBEDTLS_ERR_SSL_WANT_WRITE:
1892 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1899 "returned -0x%x: '%s'\n",
1900 -ret, get_error_string(ret));
1909 ret = do_mbedtls_handshake(c_session, m_env);
1942 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1943 int ret = mbedtls_timing_get_delay(&m_env->timer);
1944 unsigned int scalar = 1 << m_env->retry_scalar;
1954 m_env->last_timeout = now;
1967 m_env->last_timeout = now;
1986 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1989 m_env->retry_scalar++;
1991 (do_mbedtls_handshake(c_session, m_env) < 0)) {
2005 const uint8_t *data,
2011 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2012 coap_ssl_t *ssl_data;
2014 assert(m_env != NULL);
2016 ssl_data = &m_env->coap_ssl_data;
2017 if (ssl_data->pdu_len) {
2021 ssl_data->pdu = data;
2022 ssl_data->pdu_len = (unsigned)data_len;
2024 if (m_env->established) {
2025#if COAP_CONSTRAINED_STACK
2026 static coap_mutex_t b_static_mutex = COAP_MUTEX_INITIALIZER;
2032#if COAP_CONSTRAINED_STACK
2033 coap_mutex_lock(&b_static_mutex);
2042 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
2045#if COAP_CONSTRAINED_STACK
2046 coap_mutex_unlock(&b_static_mutex);
2052 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2053 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2056 case MBEDTLS_ERR_SSL_WANT_READ:
2060 "coap_dtls_receive: "
2061 "returned -0x%x: '%s' (length %zd)\n",
2062 -ret, get_error_string(ret), data_len);
2065#if COAP_CONSTRAINED_STACK
2066 coap_mutex_unlock(&b_static_mutex);
2071 ret = do_mbedtls_handshake(c_session, m_env);
2076 if (ssl_data->pdu_len) {
2078 ret = do_mbedtls_handshake(c_session, m_env);
2101 if (ssl_data && ssl_data->pdu_len) {
2103 coap_log(
LOG_DEBUG,
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2104 ssl_data->pdu_len = 0;
2105 ssl_data->pdu = NULL;
2110#if COAP_SERVER_SUPPORT
2117 const uint8_t *data,
2120#if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
2125 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
2126 " - update Mbed TLS to include DTLS and Server Mode\n");
2129 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2130 coap_ssl_t *ssl_data;
2137 c_session->
tls = m_env;
2145 if((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
2149 "mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
2150 -ret, get_error_string(ret));
2154 ssl_data = &m_env->coap_ssl_data;
2155 if (ssl_data->pdu_len) {
2159 ssl_data->pdu = data;
2160 ssl_data->pdu_len = (unsigned)data_len;
2162 ret = do_mbedtls_handshake(c_session, m_env);
2163 if (ret == 0 || m_env->seen_client_hello) {
2169 m_env->seen_client_hello = 0;
2176 if (ssl_data->pdu_len) {
2178 coap_log(
LOG_DEBUG,
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2179 ssl_data->pdu_len = 0;
2180 ssl_data->pdu = NULL;
2189 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2190 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
2192 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
2198#if !COAP_DISABLE_TCP
2199#if COAP_CLIENT_SUPPORT
2203#if !defined(MBEDTLS_SSL_CLI_C)
2207 " libcoap not compiled for Client Mode for Mbed TLS"
2208 " - update Mbed TLS to include Client Mode\n");
2211 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2222 m_env->last_timeout = now;
2223 c_session->
tls = m_env;
2224 ret = do_mbedtls_handshake(c_session, m_env);
2235#if COAP_SERVER_SUPPORT
2239#if !defined(MBEDTLS_SSL_SRV_C)
2242 " libcoap not compiled for Server Mode for Mbed TLS"
2243 " - update Mbed TLS to include Server Mode\n");
2246 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
2251 c_session->
tls = m_env;
2252 ret = do_mbedtls_handshake(c_session, m_env);
2273 const uint8_t *data,
2278 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2280 assert(m_env != NULL);
2286 if (m_env->established) {
2287 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char*) data, data_len);
2290 case MBEDTLS_ERR_SSL_WANT_READ:
2291 case MBEDTLS_ERR_SSL_WANT_WRITE:
2294 case MBEDTLS_ERR_NET_CONN_RESET:
2295 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2302 "returned -0x%x: '%s'\n",
2303 -ret, get_error_string(ret));
2312 ret = do_mbedtls_handshake(c_session, m_env);
2348 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
2355 if (!m_env->established && !m_env->sent_alert) {
2356 ret = do_mbedtls_handshake(c_session, m_env);
2365 ret = mbedtls_ssl_read(&m_env->ssl, data, data_len);
2369 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2373 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
2375 m_env->sent_alert = 1;
2379 case MBEDTLS_ERR_SSL_WANT_READ:
2386 "returned -0x%x: '%s' (length %zd)\n",
2387 -ret, get_error_string(ret), data_len);
2413#if COAP_CLIENT_SUPPORT
2414 mbedtls_free(psk_ciphers);
2415 mbedtls_free(pki_ciphers);
2418 processed_ciphers = 0;
2427 if (c_session && c_session->
tls) {
2428 coap_mbedtls_env_t *m_env;
2431 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
2433 return (
void *)&m_env->ssl;
2438static int keep_log_level = 0;
2442#if !defined(ESPIDF_VERSION)
2459 mbedtls_debug_set_threshold(use_level);
2461 keep_log_level = level;
2466 return keep_log_level;
2472 version.
version = mbedtls_version_get_number();
2478#if COAP_SERVER_SUPPORT
2481 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2484 mbedtls_sha256_init(digest_ctx);
2485#ifdef MBEDTLS_2_X_COMPAT
2486 if (mbedtls_sha256_starts_ret(digest_ctx, 0) != 0) {
2488 if (mbedtls_sha256_starts(digest_ctx, 0) != 0) {
2498 mbedtls_sha256_free(digest_ctx);
2499 mbedtls_free(digest_ctx);
2504 const uint8_t *data,
2506#ifdef MBEDTLS_2_X_COMPAT
2507 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2509 int ret = mbedtls_sha256_update(digest_ctx, data, data_len);
2518#ifdef MBEDTLS_2_X_COMPAT
2519 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t*)digest_buffer);
2521 int ret = mbedtls_sha256_finish(digest_ctx, (uint8_t*)digest_buffer);
2535#pragma GCC diagnostic ignored "-Wunused-function"
Pulls together all the internal only header files.
const char * coap_socket_strerror(void)
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_ERROR
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)
int 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_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_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.
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_tls_new_client_session(coap_session_t *coap_session, int *connected)
Create a new TLS client-side session.
void * coap_tls_new_server_session(coap_session_t *coap_session, int *connected)
Create a TLS new server-side session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_PKCS11
The PKI key type is PKCS11 (DER)
@ COAP_PKI_KEY_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_ASN1
The PKI key type is ASN.1 (DER) buffer.
@ COAP_TLS_LIBRARY_MBEDTLS
Using Mbed TLS library.
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
void coap_dtls_set_log_level(int level)
Sets the (D)TLS logging level to the specified level.
int coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log(level,...)
Logging function.
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).
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
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(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_NONE
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
The CoAP stack's global state is stored in a coap_context_t object.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure used for defining the Client PSK setup data to be used.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
The structure that holds the PKI key information.
coap_pki_key_pem_t pem
for PEM file keys
union coap_dtls_key_t::@2 key
coap_pki_key_pem_buf_t pem_buf
for PEM memory keys
coap_pki_key_t key_type
key format type
coap_pki_key_asn1_t asn1
for ASN.1 (DER) memory keys
The structure used for defining the PKI setup data to be used.
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
coap_dtls_key_t pki_key
PKI key definition.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
void * sni_call_back_arg
Passed in to the SNI callback function.
const uint8_t * private_key
ASN1 (DER) Private Key.
size_t public_cert_len
ASN1 Public Cert length.
size_t private_key_len
ASN1 Private Key length.
const uint8_t * ca_cert
ASN1 (DER) Common CA Cert.
size_t ca_cert_len
ASN1 CA Cert length.
const uint8_t * public_cert
ASN1 (DER) Public Cert, or Public Key if RPK.
size_t ca_cert_len
PEM buffer CA Cert length.
const uint8_t * ca_cert
PEM buffer Common CA Cert.
size_t private_key_len
PEM buffer Private Key length.
const uint8_t * private_key
PEM buffer Private Key If RPK and 'EC PRIVATE KEY' this can be used for both the public_cert and priv...
size_t public_cert_len
PEM buffer Public Cert length.
const uint8_t * public_cert
PEM buffer Public Cert, or Public Key if RPK.
const char * ca_file
File location of Common CA in PEM format.
const char * public_cert
File location of Public Cert.
const char * private_key
File location of Private Key in PEM format.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationaship with peer
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
int dtls_event
Tracking any (D)TLS events on this sesison.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_context_t * context
session's context
coap_socket_flags_t flags
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