42 #include <mbedtls/version.h>
43 #include <mbedtls/platform.h>
44 #include <mbedtls/net_sockets.h>
45 #include <mbedtls/ssl.h>
46 #include <mbedtls/entropy.h>
47 #include <mbedtls/ctr_drbg.h>
48 #include <mbedtls/error.h>
49 #include <mbedtls/certs.h>
50 #include <mbedtls/timing.h>
51 #include <mbedtls/ssl_cookie.h>
52 #include <mbedtls/oid.h>
53 #include <mbedtls/debug.h>
54 #include <mbedtls/sha256.h>
55 #if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
56 #include <mbedtls/esp_debug.h>
60 #define mbedtls_malloc(a) malloc(a)
61 #define mbedtls_realloc(a,b) realloc(a,b)
62 #define mbedtls_strdup(a) strdup(a)
63 #define mbedtls_strndup(a,b) strndup(a,b)
65 #ifndef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
67 #ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
68 #define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
73 #define strcasecmp _stricmp
76 #define IS_PSK (1 << 0)
77 #define IS_PKI (1 << 1)
78 #define IS_CLIENT (1 << 6)
79 #define IS_SERVER (1 << 7)
81 typedef struct coap_ssl_t {
92 typedef struct coap_mbedtls_env_t {
93 mbedtls_ssl_context ssl;
94 mbedtls_entropy_context entropy;
95 mbedtls_ctr_drbg_context ctr_drbg;
96 mbedtls_ssl_config conf;
97 mbedtls_timing_delay_context timer;
98 mbedtls_x509_crt cacert;
99 mbedtls_x509_crt public_cert;
100 mbedtls_pk_context private_key;
101 mbedtls_ssl_cookie_ctx cookie_ctx;
104 int seen_client_hello;
106 unsigned int retry_scalar;
107 coap_ssl_t coap_ssl_data;
108 } coap_mbedtls_env_t;
110 typedef struct pki_sni_entry {
113 mbedtls_x509_crt cacert;
114 mbedtls_x509_crt public_cert;
115 mbedtls_pk_context private_key;
118 typedef struct psk_sni_entry {
123 typedef struct coap_mbedtls_context_t {
125 size_t pki_sni_count;
126 pki_sni_entry *pki_sni_entry_list;
127 size_t psk_sni_count;
128 psk_sni_entry *psk_sni_entry_list;
132 } coap_mbedtls_context_t;
134 typedef enum coap_enc_method_t {
139 static int coap_dgram_read(
void *ctx,
unsigned char *out,
size_t outl)
145 if (!c_session->
tls) {
147 return MBEDTLS_ERR_SSL_WANT_READ;
149 data = &((coap_mbedtls_env_t *)c_session->
tls)->coap_ssl_data;
152 if (data->pdu_len > 0) {
153 if (outl < data->pdu_len) {
154 memcpy(out, data->pdu, outl);
157 data->pdu_len -= outl;
160 memcpy(out, data->pdu, data->pdu_len);
162 if (!data->peekmode) {
169 ret = MBEDTLS_ERR_SSL_WANT_READ;
183 coap_dgram_write(
void *ctx,
const unsigned char *send_buffer,
184 size_t send_buffer_length)
190 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
192 if (result != (ssize_t)send_buffer_length) {
194 result, send_buffer_length);
200 m_env->last_timeout = now;
208 #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && defined(MBEDTLS_SSL_SRV_C)
212 static int psk_server_callback(
void *p_info, mbedtls_ssl_context *ssl,
213 const unsigned char *name,
size_t name_len )
219 coap_mbedtls_env_t *m_env;
222 (
int)name_len, name);
224 if (c_session == NULL || c_session->
context == NULL ||
228 m_env = (coap_mbedtls_env_t *)c_session->
tls;
233 lidentity.
length = name_len;
234 lidentity.
s = (
const uint8_t*)name;
242 mbedtls_ssl_set_hs_psk(ssl, psk_key->
s, psk_key->
length);
244 m_env->seen_client_hello = 1;
249 (
const uint8_t*)name,
251 (uint8_t*)buf,
sizeof(buf));
252 m_env->seen_client_hello = 1;
253 mbedtls_ssl_set_hs_psk(ssl, buf, psk_len);
259 get_san_or_cn_from_cert(mbedtls_x509_crt *crt)
262 mbedtls_asn1_named_data * cn_data;
264 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
265 mbedtls_asn1_sequence *seq = &crt->subject_alt_names;
266 while (seq && seq->buf.p == NULL) {
271 return mbedtls_strndup((
const char *)seq->buf.p,
276 cn_data = mbedtls_asn1_find_named_data(&crt->subject,
278 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN));
281 return mbedtls_strndup((
const char *)cn_data->val.p,
289 get_error_string(
int ret) {
290 static char buf[128] = {0};
291 mbedtls_strerror(ret, buf,
sizeof(buf)-1);
300 cert_verify_callback_mbedtls(
void *data, mbedtls_x509_crt *crt,
301 int depth, uint32_t *flags)
304 coap_mbedtls_context_t *m_context =
312 cn = get_san_or_cn_from_cert(crt);
314 if (*flags & MBEDTLS_X509_BADCERT_EXPIRED) {
315 if (setup_data->allow_expired_certs) {
316 *flags &= ~MBEDTLS_X509_BADCERT_EXPIRED;
318 " %s: %s: overridden: '%s' depth %d\n",
320 "The certificate has expired", cn ? cn :
"?", depth);
323 if (*flags & MBEDTLS_X509_BADCERT_FUTURE) {
324 if (setup_data->allow_expired_certs) {
325 *flags &= ~MBEDTLS_X509_BADCERT_FUTURE;
327 " %s: %s: overridden: '%s' depth %d\n",
329 "The certificate has a future date", cn ? cn :
"?", depth);
332 if (*flags & MBEDTLS_X509_BADCERT_BAD_MD) {
333 if (setup_data->allow_bad_md_hash) {
334 *flags &= ~MBEDTLS_X509_BADCERT_BAD_MD;
336 " %s: %s: overridden: '%s' depth %d\n",
338 "The certificate has a bad MD hash", cn ? cn :
"?", depth);
341 if (*flags & MBEDTLS_X509_BADCERT_BAD_KEY) {
342 if (setup_data->allow_short_rsa_length) {
343 *flags &= ~MBEDTLS_X509_BADCERT_BAD_KEY;
345 " %s: %s: overridden: '%s' depth %d\n",
347 "The certificate has a short RSA length", cn ? cn :
"?", depth);
350 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
352 int self_signed = !mbedtls_x509_crt_verify(crt, crt, NULL, NULL, &lflags,
354 if (self_signed && depth == 0) {
355 if (setup_data->allow_self_signed &&
356 !setup_data->check_common_ca) {
357 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
359 " %s: %s: overridden: '%s' depth %d\n",
362 cn ? cn :
"?", depth);
366 if (!setup_data->verify_peer_cert) {
367 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
369 " %s: %s: overridden: '%s' depth %d\n",
371 "The certificate's CA does not match", cn ? cn :
"?", depth);
375 if (*flags & MBEDTLS_X509_BADCRL_EXPIRED) {
376 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
377 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
379 " %s: %s: overridden: '%s' depth %d\n",
381 "The certificate's CRL has expired", cn ? cn :
"?", depth);
383 else if (!setup_data->check_cert_revocation) {
384 *flags &= ~MBEDTLS_X509_BADCRL_EXPIRED;
387 if (*flags & MBEDTLS_X509_BADCRL_FUTURE) {
388 if (setup_data->check_cert_revocation && setup_data->allow_expired_crl) {
389 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
391 " %s: %s: overridden: '%s' depth %d\n",
393 "The certificate's CRL has a future date", cn ? cn :
"?", depth);
395 else if (!setup_data->check_cert_revocation) {
396 *flags &= ~MBEDTLS_X509_BADCRL_FUTURE;
399 if (setup_data->cert_chain_validation &&
400 depth > (setup_data->cert_chain_verify_depth + 1)) {
401 *flags |= MBEDTLS_X509_BADCERT_OTHER;
403 " %s: %s: '%s' depth %d\n",
405 "The certificate's verify depth is too long",
406 cn ? cn :
"?", depth);
409 if (*flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
410 *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
412 if (setup_data->validate_cn_call_back) {
413 if (!setup_data->validate_cn_call_back(cn,
419 setup_data->cn_call_back_arg)) {
420 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
426 int ret = mbedtls_x509_crt_verify_info(buf,
sizeof(buf),
"", *flags);
429 tcp = strchr(buf,
'\n');
433 " %s: %s: issue 0x%x: '%s' depth %d\n",
435 buf, *flags, cn ? cn :
"?", depth);
436 tcp = strchr(tcp+1,
'\n');
440 coap_log(
LOG_ERR,
"mbedtls_x509_crt_verify_info returned -0x%x: '%s'\n",
441 -ret, get_error_string(ret));
452 setup_pki_credentials(mbedtls_x509_crt *cacert,
453 mbedtls_x509_crt *public_cert,
454 mbedtls_pk_context *private_key,
455 coap_mbedtls_env_t *m_env,
456 coap_mbedtls_context_t *m_context,
465 "RPK Support not available in Mbed TLS\n");
475 mbedtls_x509_crt_init(public_cert);
476 mbedtls_pk_init(private_key);
478 ret = mbedtls_x509_crt_parse_file(public_cert,
481 coap_log(
LOG_ERR,
"mbedtls_x509_crt_parse_file returned -0x%x: '%s'\n",
482 -ret, get_error_string(ret));
486 ret = mbedtls_pk_parse_keyfile(private_key,
490 -ret, get_error_string(ret));
494 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
497 -ret, get_error_string(ret));
503 "***setup_pki: (D)TLS: No Server Certificate + Private "
510 mbedtls_x509_crt_init(cacert);
511 ret = mbedtls_x509_crt_parse_file(cacert,
515 -ret, get_error_string(ret));
518 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
529 mbedtls_x509_crt_init(public_cert);
530 mbedtls_pk_init(private_key);
535 buffer = mbedtls_malloc(length + 1);
538 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
541 buffer[length] =
'\000';
543 ret = mbedtls_x509_crt_parse(public_cert, buffer, length);
544 mbedtls_free(buffer);
547 ret = mbedtls_x509_crt_parse(public_cert,
553 -ret, get_error_string(ret));
560 buffer = mbedtls_malloc(length + 1);
563 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
566 buffer[length] =
'\000';
568 ret = mbedtls_pk_parse_key(private_key, buffer, length, NULL, 0);
569 mbedtls_free(buffer);
572 ret = mbedtls_pk_parse_key(private_key,
578 -ret, get_error_string(ret));
582 ret = mbedtls_ssl_conf_own_cert(&m_env->conf, public_cert, private_key);
585 -ret, get_error_string(ret));
590 "***setup_pki: (D)TLS: No Server Certificate + Private "
600 mbedtls_x509_crt_init(cacert);
604 buffer = mbedtls_malloc(length + 1);
607 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
610 buffer[length] =
'\000';
612 ret = mbedtls_x509_crt_parse(cacert, buffer, length);
613 mbedtls_free(buffer);
616 ret = mbedtls_x509_crt_parse(cacert,
622 -ret, get_error_string(ret));
625 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
634 mbedtls_x509_crt_init(public_cert);
635 mbedtls_pk_init(private_key);
636 ret = mbedtls_x509_crt_parse(public_cert,
641 -ret, get_error_string(ret));
645 ret = mbedtls_pk_parse_key(private_key,
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 "
669 mbedtls_x509_crt_init(cacert);
670 ret = mbedtls_x509_crt_parse(cacert,
675 -ret, get_error_string(ret));
678 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
684 "***setup_pki: (D)TLS: PKCS11 not currently supported\n");
689 "***setup_pki: (D)TLS: Unknown key type %d\n",
694 if (m_context->root_ca_file) {
695 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
698 -ret, get_error_string(ret));
701 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
703 if (m_context->root_ca_path) {
704 ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path);
707 -ret, get_error_string(ret));
710 mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
713 mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
715 MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED :
716 MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
718 MBEDTLS_SSL_VERIFY_REQUIRED :
719 MBEDTLS_SSL_VERIFY_NONE);
724 mbedtls_ssl_conf_verify(&m_env->conf,
725 cert_verify_callback_mbedtls, c_session);
730 #if defined(MBEDTLS_SSL_SRV_C)
735 pki_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
736 const unsigned char *uname,
size_t name_len)
741 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
742 coap_mbedtls_context_t *m_context =
747 name = mbedtls_malloc(name_len+1);
751 memcpy(name, uname, name_len);
752 name[name_len] =
'\000';
755 for (i = 0; i < m_context->pki_sni_count; i++) {
756 if (strcasecmp(name, m_context->pki_sni_entry_list[i].sni) == 0) {
760 if (i == m_context->pki_sni_count) {
765 pki_sni_entry *pki_sni_entry_list;
768 m_context->setup_data.validate_sni_call_back(name,
769 m_context->setup_data.sni_call_back_arg);
775 pki_sni_entry_list = mbedtls_realloc(m_context->pki_sni_entry_list,
776 (i+1)*
sizeof(pki_sni_entry));
778 if (pki_sni_entry_list == NULL) {
782 m_context->pki_sni_entry_list = pki_sni_entry_list;
783 memset(&m_context->pki_sni_entry_list[i], 0,
784 sizeof(m_context->pki_sni_entry_list[i]));
785 m_context->pki_sni_entry_list[i].sni = name;
786 m_context->pki_sni_entry_list[i].pki_key = *new_entry;
787 sni_setup_data = m_context->setup_data;
788 sni_setup_data.
pki_key = *new_entry;
789 if ((ret = setup_pki_credentials(&m_context->pki_sni_entry_list[i].cacert,
790 &m_context->pki_sni_entry_list[i].public_cert,
791 &m_context->pki_sni_entry_list[i].private_key,
800 m_context->pki_sni_count++;
806 mbedtls_ssl_set_hs_ca_chain(ssl, &m_context->pki_sni_entry_list[i].cacert,
808 return mbedtls_ssl_set_hs_own_cert(ssl,
809 &m_context->pki_sni_entry_list[i].public_cert,
810 &m_context->pki_sni_entry_list[i].private_key);
813 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
818 psk_sni_callback(
void *p_info, mbedtls_ssl_context *ssl,
819 const unsigned char *uname,
size_t name_len)
823 coap_mbedtls_context_t *m_context =
827 name = mbedtls_malloc(name_len+1);
831 memcpy(name, uname, name_len);
832 name[name_len] =
'\000';
835 for (i = 0; i < m_context->psk_sni_count; i++) {
836 if (strcasecmp(name, m_context->psk_sni_entry_list[i].sni) == 0) {
840 if (i == m_context->psk_sni_count) {
845 psk_sni_entry *psk_sni_entry_list;
856 psk_sni_entry_list = mbedtls_realloc(m_context->psk_sni_entry_list,
857 (i+1)*
sizeof(psk_sni_entry));
859 if (psk_sni_entry_list == NULL) {
863 m_context->psk_sni_entry_list = psk_sni_entry_list;
864 m_context->psk_sni_entry_list[i].sni = name;
865 m_context->psk_sni_entry_list[i].psk_info = *new_entry;
867 m_context->psk_sni_count++;
874 &m_context->psk_sni_entry_list[i].psk_info.hint);
876 &m_context->psk_sni_entry_list[i].psk_info.key);
877 return mbedtls_ssl_set_hs_psk(ssl,
878 m_context->psk_sni_entry_list[i].psk_info.key.s,
879 m_context->psk_sni_entry_list[i].psk_info.key.length);
884 coap_mbedtls_env_t *m_env)
886 coap_mbedtls_context_t *m_context =
889 m_context->psk_pki_enabled |= IS_SERVER;
891 mbedtls_ssl_cookie_init(&m_env->cookie_ctx);
892 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
893 MBEDTLS_SSL_IS_SERVER,
895 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
896 MBEDTLS_SSL_TRANSPORT_STREAM,
897 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
898 coap_log(
LOG_ERR,
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
899 -ret, get_error_string(ret));
903 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
905 #if defined(MBEDTLS_SSL_PROTO_DTLS)
909 if (m_context->psk_pki_enabled & IS_PSK) {
910 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
911 mbedtls_ssl_conf_psk_cb(&m_env->conf, psk_server_callback, c_session);
913 mbedtls_ssl_conf_sni(&m_env->conf, psk_sni_callback, c_session);
921 if (m_context->psk_pki_enabled & IS_PKI) {
922 ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert,
923 &m_env->private_key, m_env, m_context,
924 c_session, &m_context->setup_data,
930 if (m_context->setup_data.validate_sni_call_back) {
931 mbedtls_ssl_conf_sni(&m_env->conf, pki_sni_callback, c_session);
935 if ((ret = mbedtls_ssl_cookie_setup(&m_env->cookie_ctx,
936 mbedtls_ctr_drbg_random,
937 &m_env->ctr_drbg)) != 0) {
939 -ret, get_error_string(ret));
943 #if defined(MBEDTLS_SSL_PROTO_DTLS)
944 mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write,
945 mbedtls_ssl_cookie_check,
946 &m_env->cookie_ctx );
947 #if MBEDTLS_VERSION_NUMBER >= 0x02100100
948 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
956 static int *psk_ciphers = NULL;
957 static int *pki_ciphers = NULL;
958 static int processed_ciphers = 0;
961 set_ciphersuites(mbedtls_ssl_config *conf, coap_enc_method_t method)
963 if (!processed_ciphers) {
964 const int *list = mbedtls_ssl_list_ciphersuites();
965 const int *base = list;
972 const mbedtls_ssl_ciphersuite_t *cur =
973 mbedtls_ssl_ciphersuite_from_id(*list);
976 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
979 else if (mbedtls_ssl_ciphersuite_uses_psk(cur)) {
990 psk_ciphers = mbedtls_malloc(psk_count *
sizeof(psk_ciphers[0]));
991 if (psk_ciphers == NULL) {
992 coap_log(
LOG_ERR,
"set_ciphers: mbedtls_malloc with count %d failed\n", psk_count);
995 pki_ciphers = mbedtls_malloc(pki_count *
sizeof(pki_ciphers[0]));
996 if (pki_ciphers == NULL) {
997 coap_log(
LOG_ERR,
"set_ciphers: mbedtls_malloc with count %d failed\n", pki_count);
1001 psk_list = psk_ciphers;
1002 pki_list = pki_ciphers;
1005 const mbedtls_ssl_ciphersuite_t *cur =
1006 mbedtls_ssl_ciphersuite_from_id(*list);
1008 if (cur->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
1011 else if (mbedtls_ssl_ciphersuite_uses_psk(cur)) {
1025 processed_ciphers = 1;
1027 mbedtls_ssl_conf_ciphersuites(conf, method == COAP_ENC_PSK ? psk_ciphers : pki_ciphers);
1031 coap_mbedtls_env_t *m_env)
1035 coap_mbedtls_context_t *m_context =
1038 m_context->psk_pki_enabled |= IS_CLIENT;
1040 if ((ret = mbedtls_ssl_config_defaults(&m_env->conf,
1041 MBEDTLS_SSL_IS_CLIENT,
1043 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1044 MBEDTLS_SSL_TRANSPORT_STREAM,
1045 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1046 coap_log(
LOG_ERR,
"mbedtls_ssl_config_defaults returned -0x%x: '%s'\n",
1047 -ret, get_error_string(ret));
1051 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1056 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
1057 mbedtls_ssl_conf_rng(&m_env->conf, mbedtls_ctr_drbg_random, &m_env->ctr_drbg);
1059 if (m_context->psk_pki_enabled & IS_PSK) {
1060 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1061 uint8_t identity[64];
1062 size_t identity_len;
1063 uint8_t psk_key[64];
1065 size_t max_identity_len =
sizeof(identity);
1076 assert(identity_len <=
sizeof(identity));
1077 mbedtls_ssl_conf_psk(&m_env->conf, (
const unsigned char *)psk_key,
1078 psk_len, (
const unsigned char *)identity,
1081 mbedtls_ssl_set_hostname(&m_env->ssl,
1086 set_ciphersuites(&m_env->conf, COAP_ENC_PSK);
1091 else if ((m_context->psk_pki_enabled & IS_PKI) ||
1092 (m_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1097 mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
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 defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN)
1108 const char *alpn_list[] = {
"coap", NULL };
1110 ret = mbedtls_ssl_conf_alpn_protocols(&m_env->conf, alpn_list);
1116 if (m_context->setup_data.client_sni) {
1117 mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni);
1119 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1120 #if MBEDTLS_VERSION_NUMBER >= 0x02100100
1121 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1124 set_ciphersuites(&m_env->conf, COAP_ENC_PKI);
1132 static void mbedtls_cleanup(coap_mbedtls_env_t *m_env)
1138 mbedtls_x509_crt_free(&m_env->cacert);
1139 mbedtls_x509_crt_free(&m_env->public_cert);
1140 mbedtls_pk_free(&m_env->private_key);
1141 mbedtls_entropy_free(&m_env->entropy);
1142 mbedtls_ssl_config_free(&m_env->conf);
1143 mbedtls_ctr_drbg_free(&m_env->ctr_drbg);
1144 mbedtls_ssl_free(&m_env->ssl);
1145 mbedtls_ssl_cookie_free(&m_env->cookie_ctx);
1149 coap_dtls_free_mbedtls_env(coap_mbedtls_env_t *m_env) {
1151 mbedtls_cleanup(m_env);
1152 mbedtls_free(m_env);
1157 report_mbedtls_alert(
unsigned char alert) {
1159 case MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC:
return ": Bad Record MAC";
1160 case MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE:
return ": Handshake failure";
1161 case MBEDTLS_SSL_ALERT_MSG_NO_CERT:
return ": No Certificate provided";
1162 case MBEDTLS_SSL_ALERT_MSG_BAD_CERT:
return ": Certificate is bad";
1163 case MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA:
return ": CA is unknown";
1164 case MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED:
return ": Access was denied";
1165 case MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR:
return ": Decrypt error";
1176 coap_mbedtls_env_t *m_env) {
1180 ret = mbedtls_ssl_handshake(&m_env->ssl);
1183 m_env->established = 1;
1188 case MBEDTLS_ERR_SSL_WANT_READ:
1189 case MBEDTLS_ERR_SSL_WANT_WRITE:
1193 case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
1196 case MBEDTLS_ERR_SSL_INVALID_MAC:
1198 case MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE:
1199 alert = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
1201 case MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO:
1202 alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE;
1204 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
1206 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1207 if (m_env->ssl.in_msg[1] != MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)
1210 report_mbedtls_alert(m_env->ssl.in_msg[1]));
1212 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1218 "do_mbedtls_handshake: session establish "
1219 "returned -0x%x: '%s'\n",
1220 -ret, get_error_string(ret));
1227 mbedtls_ssl_send_alert_message(&m_env->ssl,
1228 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1233 "do_mbedtls_handshake: session establish "
1235 get_error_string(ret));
1237 mbedtls_ssl_session_reset(&m_env->ssl);
1242 mbedtls_debug_out(
void *ctx
COAP_UNUSED,
int level,
1243 const char *file,
int line,
const char *str) {
1266 coap_log(log_level,
"%s:%04d: %s", file, line, str);
1269 static coap_mbedtls_env_t *coap_dtls_new_mbedtls_env(
coap_session_t *c_session,
1273 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1278 m_env = (coap_mbedtls_env_t *)mbedtls_malloc(
sizeof(coap_mbedtls_env_t));
1282 memset(m_env, 0,
sizeof(coap_mbedtls_env_t));
1284 mbedtls_ssl_init(&m_env->ssl);
1285 mbedtls_ctr_drbg_init(&m_env->ctr_drbg);
1286 mbedtls_ssl_config_init(&m_env->conf);
1287 mbedtls_entropy_init(&m_env->entropy);
1289 #if defined(ESPIDF_VERSION) && defined(CONFIG_MBEDTLS_DEBUG)
1290 mbedtls_esp_enable_debug_log(&m_env->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
1292 if ((ret = mbedtls_ctr_drbg_seed(&m_env->ctr_drbg,
1293 mbedtls_entropy_func, &m_env->entropy, NULL, 0)) != 0) {
1295 -ret, get_error_string(ret));
1300 if (setup_client_ssl_session(c_session, m_env) != 0) {
1303 #if defined(MBEDTLS_SSL_SRV_C)
1305 if (setup_server_ssl_session(c_session, m_env) != 0) {
1313 mbedtls_ssl_conf_min_version(&m_env->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1314 MBEDTLS_SSL_MINOR_VERSION_3);
1316 if ((ret = mbedtls_ssl_setup(&m_env->ssl, &m_env->conf)) != 0) {
1319 mbedtls_ssl_set_bio(&m_env->ssl, c_session, coap_dgram_write,
1320 coap_dgram_read, NULL);
1321 mbedtls_ssl_set_timer_cb(&m_env->ssl, &m_env->timer,
1322 mbedtls_timing_set_delay,
1323 mbedtls_timing_get_delay);
1325 mbedtls_ssl_conf_dbg(&m_env->conf, mbedtls_debug_out, stdout);
1330 mbedtls_free(m_env);
1336 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1340 "libcoap not compiled for DTLS with Mbed TLS"
1341 " - update Mbed TLS to include DTLS\n");
1353 coap_mbedtls_context_t *m_context;
1356 m_context = (coap_mbedtls_context_t *)mbedtls_malloc(
sizeof(coap_mbedtls_context_t));
1358 memset(m_context, 0,
sizeof(coap_mbedtls_context_t));
1371 coap_mbedtls_context_t *m_context =
1374 #if !defined(MBEDTLS_SSL_SRV_C)
1376 " libcoap not compiled for Server Mode for Mbed TLS"
1377 " - update Mbed TLS to include Server Mode\n");
1380 if (!m_context || !setup_data)
1383 m_context->psk_pki_enabled |= IS_PSK;
1395 #if !defined(MBEDTLS_SSL_CLI_C)
1397 " libcoap not compiled for Client Mode for Mbed TLS"
1398 " - update Mbed TLS to include Client Mode\n");
1401 coap_mbedtls_context_t *m_context =
1404 if (!m_context || !setup_data)
1409 "CoAP Client with Mbed TLS does not support Identity Hint selection\n");
1411 m_context->psk_pki_enabled |= IS_PSK;
1419 coap_mbedtls_context_t *m_context =
1422 m_context->setup_data = *setup_data;
1423 if (!m_context->setup_data.verify_peer_cert) {
1427 m_context->setup_data.allow_self_signed = 1;
1428 m_context->setup_data.allow_expired_certs = 1;
1429 m_context->setup_data.cert_chain_validation = 1;
1430 m_context->setup_data.cert_chain_verify_depth = 10;
1431 m_context->setup_data.check_cert_revocation = 1;
1432 m_context->setup_data.allow_no_crl = 1;
1433 m_context->setup_data.allow_expired_crl = 1;
1434 m_context->setup_data.allow_bad_md_hash = 1;
1435 m_context->setup_data.allow_short_rsa_length = 1;
1437 m_context->psk_pki_enabled |= IS_PKI;
1442 const char *ca_file,
1443 const char *ca_path)
1445 coap_mbedtls_context_t *m_context =
1450 "coap_context_set_pki_root_cas: (D)TLS environment "
1455 if (ca_file == NULL && ca_path == NULL) {
1457 "coap_context_set_pki_root_cas: ca_file and/or ca_path "
1461 if (m_context->root_ca_file) {
1462 mbedtls_free(m_context->root_ca_file);
1463 m_context->root_ca_file = NULL;
1467 m_context->root_ca_file = mbedtls_strdup(ca_file);
1470 if (m_context->root_ca_path) {
1471 mbedtls_free(m_context->root_ca_path);
1472 m_context->root_ca_path = NULL;
1476 m_context->root_ca_path = mbedtls_strdup(ca_path);
1483 coap_mbedtls_context_t *m_context =
1485 return m_context->psk_pki_enabled ? 1 : 0;
1490 coap_mbedtls_context_t *m_context = (coap_mbedtls_context_t *)dtls_context;
1493 for (i = 0; i < m_context->pki_sni_count; i++) {
1494 mbedtls_free(m_context->pki_sni_entry_list[i].sni);
1496 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].public_cert);
1498 mbedtls_pk_free(&m_context->pki_sni_entry_list[i].private_key);
1500 mbedtls_x509_crt_free(&m_context->pki_sni_entry_list[i].cacert);
1502 if (m_context->pki_sni_entry_list)
1503 mbedtls_free(m_context->pki_sni_entry_list);
1505 for (i = 0; i < m_context->psk_sni_count; i++) {
1506 mbedtls_free(m_context->psk_sni_entry_list[i].sni);
1508 if (m_context->psk_sni_entry_list)
1509 mbedtls_free(m_context->psk_sni_entry_list);
1511 if (m_context->root_ca_path)
1512 mbedtls_free(m_context->root_ca_path);
1513 if (m_context->root_ca_file)
1514 mbedtls_free(m_context->root_ca_file);
1516 mbedtls_free(m_context);
1521 #if !defined(MBEDTLS_SSL_CLI_C)
1524 " libcoap not compiled for Client Mode for Mbed TLS"
1525 " - update Mbed TLS to include Client Mode\n");
1528 coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session,
1535 m_env->last_timeout = now;
1536 ret = do_mbedtls_handshake(c_session, m_env);
1538 coap_dtls_free_mbedtls_env(m_env);
1548 coap_mbedtls_env_t *m_env =
1549 (coap_mbedtls_env_t *)c_session->
tls;
1551 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1552 #if MBEDTLS_VERSION_NUMBER >= 0x02100100
1553 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1562 if (c_session && c_session->
context && c_session->
tls) {
1563 coap_dtls_free_mbedtls_env(c_session->
tls);
1564 c_session->
tls = NULL;
1572 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1573 coap_mbedtls_env_t *m_env =
1574 (coap_mbedtls_env_t *)c_session->
tls;
1576 #if MBEDTLS_VERSION_NUMBER >= 0x02100100
1577 mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->
mtu);
1584 const uint8_t *data,
1588 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1590 assert(m_env != NULL);
1596 if (m_env->established) {
1597 ret = mbedtls_ssl_write(&m_env->ssl, (
const unsigned char*) data, data_len);
1600 case MBEDTLS_ERR_SSL_WANT_READ:
1601 case MBEDTLS_ERR_SSL_WANT_WRITE:
1604 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1611 "returned -0x%x: '%s'\n",
1612 -ret, get_error_string(ret));
1621 ret = do_mbedtls_handshake(c_session, m_env);
1654 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1655 int ret = mbedtls_timing_get_delay(&m_env->timer);
1656 unsigned int scalar = 1 << m_env->retry_scalar;
1666 m_env->last_timeout = now;
1679 m_env->last_timeout = now;
1693 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1696 m_env->retry_scalar++;
1698 (do_mbedtls_handshake(c_session, m_env) < 0)) {
1711 const uint8_t *data,
1717 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1718 coap_ssl_t *ssl_data;
1720 assert(m_env != NULL);
1722 ssl_data = &m_env->coap_ssl_data;
1723 if (ssl_data->pdu_len) {
1727 ssl_data->pdu = data;
1728 ssl_data->pdu_len = (unsigned)data_len;
1730 if (m_env->established) {
1731 #if COAP_CONSTRAINED_STACK
1732 static coap_mutex_t b_static_mutex = COAP_MUTEX_INITIALIZER;
1738 #if COAP_CONSTRAINED_STACK
1739 coap_mutex_lock(&b_static_mutex);
1748 ret = mbedtls_ssl_read(&m_env->ssl, pdu,
sizeof(pdu));
1751 #if COAP_CONSTRAINED_STACK
1752 coap_mutex_unlock(&b_static_mutex);
1758 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1759 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
1762 case MBEDTLS_ERR_SSL_WANT_READ:
1766 "coap_dtls_receive: "
1767 "returned -0x%x: '%s' (length %zd)\n",
1768 -ret, get_error_string(ret), data_len);
1771 #if COAP_CONSTRAINED_STACK
1772 coap_mutex_unlock(&b_static_mutex);
1777 ret = do_mbedtls_handshake(c_session, m_env);
1782 if (ssl_data->pdu_len) {
1784 ret = do_mbedtls_handshake(c_session, m_env);
1807 if (ssl_data && ssl_data->pdu_len) {
1809 coap_log(
LOG_DEBUG,
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
1810 ssl_data->pdu_len = 0;
1811 ssl_data->pdu = NULL;
1822 const uint8_t *data,
1825 #if !defined(MBEDTLS_SSL_PROTO_DTLS) || !defined(MBEDTLS_SSL_SRV_C)
1830 " libcoap not compiled for DTLS or Server Mode for Mbed TLS"
1831 " - update Mbed TLS to include DTLS and Server Mode\n");
1834 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1835 coap_ssl_t *ssl_data;
1841 c_session->
tls = m_env;
1849 if((ret = mbedtls_ssl_set_client_transport_id(&m_env->ssl,
1853 "mbedtls_ssl_set_client_transport_id() returned -0x%x: '%s'\n",
1854 -ret, get_error_string(ret));
1858 ssl_data = &m_env->coap_ssl_data;
1859 if (ssl_data->pdu_len) {
1863 ssl_data->pdu = data;
1864 ssl_data->pdu_len = (unsigned)data_len;
1866 ret = do_mbedtls_handshake(c_session, m_env);
1867 if (ret == 0 || m_env->seen_client_hello) {
1873 m_env->seen_client_hello = 0;
1880 if (ssl_data->pdu_len) {
1882 coap_log(
LOG_DEBUG,
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
1883 ssl_data->pdu_len = 0;
1884 ssl_data->pdu = NULL;
1892 coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->
tls;
1893 int expansion = mbedtls_ssl_get_record_expansion(&m_env->ssl);
1895 if (expansion == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
1901 #if !COAP_DISABLE_TCP
1947 if (c_session && c_session->
tls) {
1948 coap_mbedtls_env_t *m_env;
1951 memcpy(&m_env, &c_session->
tls,
sizeof(m_env));
1953 return (
void *)&m_env->ssl;
1958 static int keep_log_level = 0;
1962 #if !defined(ESPIDF_VERSION)
1979 mbedtls_debug_set_threshold(use_level);
1981 keep_log_level = level;
1986 return keep_log_level;
1992 version.
version = mbedtls_version_get_number();
2000 mbedtls_sha256_context *digest_ctx = mbedtls_malloc(
sizeof(mbedtls_sha256_context));
2003 mbedtls_sha256_init(digest_ctx);
2004 mbedtls_sha256_starts_ret(digest_ctx, 0);
2011 mbedtls_sha256_free(digest_ctx);
2012 mbedtls_free(digest_ctx);
2017 const uint8_t *data,
2019 int ret = mbedtls_sha256_update_ret(digest_ctx, data, data_len);
2027 int ret = mbedtls_sha256_finish_ret(digest_ctx, (uint8_t*)digest_buffer);
2039 #pragma GCC diagnostic ignored "-Wunused-function"
Pulls together all the internal only header files.
#define COAP_RXBUFFER_SIZE
void * coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_spsk_t *setup_data COAP_UNUSED)
void coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
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)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
int coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_new_client_session(coap_session_t *session 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)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
void * coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
void * coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
int coap_dtls_hello(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
int coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_cpsk_t *setup_data 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)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
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.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
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.
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_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
#define COAP_DTLS_RETRANSMIT_MS
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
#define COAP_DTLS_RETRANSMIT_TOTAL_MS
int coap_tls_is_supported(void)
Check whether TLS is available.
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
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.
#define COAP_EVENT_DTLS_ERROR
#define COAP_EVENT_DTLS_CLOSED
(D)TLS events for COAP_PROTO_DTLS and COAP_PROTO_TLS
#define COAP_EVENT_DTLS_CONNECTED
void coap_dtls_set_log_level(int level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
int coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_log(level,...)
Logging function.
@ COAP_LOG_CIPHERS
CipherInfo.
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).
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.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_STATE_HANDSHAKE
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.
size_t(* get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
size_t(* get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
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
unsigned int max_retransmit
maximum re-transmit count (default 4)
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
int dtls_event
Tracking any (D)TLS events on this sesison.
void * tls
security parameters
coap_context_t * context
session's context
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