53#ifdef COAP_WITH_LIBGNUTLS
55#define MIN_GNUTLS_VERSION "3.3.0"
58#include <gnutls/gnutls.h>
59#include <gnutls/x509.h>
60#include <gnutls/dtls.h>
61#include <gnutls/pkcs11.h>
62#include <gnutls/crypto.h>
63#include <gnutls/abstract.h>
65#if (GNUTLS_VERSION_NUMBER >= 0x030606)
66#define COAP_GNUTLS_KEY_RPK GNUTLS_KEY_DIGITAL_SIGNATURE | \
67 GNUTLS_KEY_NON_REPUDIATION | \
68 GNUTLS_KEY_KEY_ENCIPHERMENT | \
69 GNUTLS_KEY_DATA_ENCIPHERMENT | \
70 GNUTLS_KEY_KEY_AGREEMENT | \
71 GNUTLS_KEY_KEY_CERT_SIGN
75#define GNUTLS_CRT_RAW GNUTLS_CRT_RAWPK
79#define strcasecmp _stricmp
82typedef struct coap_ssl_t {
86 gnutls_datum_t cookie_key;
94typedef struct coap_gnutls_env_t {
95 gnutls_session_t g_session;
96 gnutls_psk_client_credentials_t psk_cl_credentials;
97 gnutls_psk_server_credentials_t psk_sv_credentials;
98 gnutls_certificate_credentials_t pki_credentials;
99 coap_ssl_t coap_ssl_data;
102 int doing_dtls_timeout;
107#define IS_PSK (1 << 0)
108#define IS_PKI (1 << 1)
109#define IS_CLIENT (1 << 6)
110#define IS_SERVER (1 << 7)
112typedef struct pki_sni_entry {
115 gnutls_certificate_credentials_t pki_credentials;
118typedef struct psk_sni_entry {
121 gnutls_psk_server_credentials_t psk_credentials;
124typedef struct coap_gnutls_context_t {
127 size_t pki_sni_count;
128 pki_sni_entry *pki_sni_entry_list;
129 size_t psk_sni_count;
130 psk_sni_entry *psk_sni_entry_list;
131 gnutls_datum_t alpn_proto;
134 gnutls_priority_t priority_cache;
135} coap_gnutls_context_t;
137typedef enum coap_free_bye_t {
138 COAP_FREE_BYE_AS_TCP,
139 COAP_FREE_BYE_AS_UDP,
143#define VARIANTS_3_6_6 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL:+SHA256"
144#define VARIANTS_3_5_5 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8"
145#define VARIANTS_BASE "NORMAL:+ECDHE-PSK:+PSK"
147#define VARIANTS_NO_TLS13_3_6_6 VARIANTS_3_6_6 ":-VERS-TLS1.3"
148#define VARIANTS_NO_TLS13_3_6_4 VARIANTS_3_5_5 ":-VERS-TLS1.3"
150#define G_ACTION(xx) do { \
152 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
154#define G_CHECK(xx,func) do { \
155 if ((ret = (xx)) < 0) { \
156 coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \
161#define G_ACTION_CHECK(xx,func) do { \
168#if COAP_SERVER_SUPPORT
169static int post_client_hello_gnutls_pki(gnutls_session_t g_session);
170static int post_client_hello_gnutls_psk(gnutls_session_t g_session);
171static int psk_server_callback(gnutls_session_t g_session,
172 const char *identity,
173 gnutls_datum_t *key);
182 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
183 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
196 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
197 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
239#if (GNUTLS_VERSION_NUMBER >= 0x030606)
255#if COAP_CLIENT_SUPPORT
267 const char *vers = gnutls_check_version(NULL);
273 sscanf(vers,
"%d.%d.%d", &p1, &p2, &p3);
274 version.
version = (p1 << 16) | (p2 << 8) | p3;
282coap_gnutls_audit_log_func(gnutls_session_t g_session,
const char *text) {
283#if COAP_MAX_LOGGING_LEVEL > 0
299coap_gnutls_log_func(
int level,
const char *text) {
317 coap_gnutls_context_t *g_context =
320 if (!g_context || !setup_data)
323 g_context->setup_data = *setup_data;
324 if (!g_context->setup_data.verify_peer_cert) {
326 g_context->setup_data.check_common_ca = 0;
327 if (g_context->setup_data.is_rpk_not_cert) {
329 g_context->setup_data.allow_self_signed = 0;
330 g_context->setup_data.allow_expired_certs = 0;
331 g_context->setup_data.cert_chain_validation = 0;
332 g_context->setup_data.cert_chain_verify_depth = 0;
333 g_context->setup_data.check_cert_revocation = 0;
334 g_context->setup_data.allow_no_crl = 0;
335 g_context->setup_data.allow_expired_crl = 0;
336 g_context->setup_data.allow_bad_md_hash = 0;
337 g_context->setup_data.allow_short_rsa_length = 0;
340 g_context->setup_data.allow_self_signed = 1;
341 g_context->setup_data.allow_expired_certs = 1;
342 g_context->setup_data.cert_chain_validation = 1;
343 g_context->setup_data.cert_chain_verify_depth = 10;
344 g_context->setup_data.check_cert_revocation = 1;
345 g_context->setup_data.allow_no_crl = 1;
346 g_context->setup_data.allow_expired_crl = 1;
347 g_context->setup_data.allow_bad_md_hash = 1;
348 g_context->setup_data.allow_short_rsa_length = 1;
353 g_context->setup_data.pki_key = key;
354 g_context->psk_pki_enabled |= IS_PKI;
368 const char *ca_path) {
369 coap_gnutls_context_t *g_context =
372 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
377 if (ca_file == NULL && ca_path == NULL) {
378 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
382 if (g_context->root_ca_file) {
383 gnutls_free(g_context->root_ca_file);
384 g_context->root_ca_file = NULL;
387 g_context->root_ca_file = gnutls_strdup(ca_file);
389 if (g_context->root_ca_path) {
390 gnutls_free(g_context->root_ca_path);
391 g_context->root_ca_path = NULL;
394#if (GNUTLS_VERSION_NUMBER >= 0x030306)
395 g_context->root_ca_path = gnutls_strdup(ca_path);
397 coap_log_err(
"ca_path not supported in GnuTLS < 3.3.6\n");
403#if COAP_SERVER_SUPPORT
412 coap_gnutls_context_t *g_context =
415 if (!g_context || !setup_data)
421 g_context->psk_pki_enabled |= IS_PSK;
426#if COAP_CLIENT_SUPPORT
435 coap_gnutls_context_t *g_context =
438 if (!g_context || !setup_data)
447 g_context->psk_pki_enabled |= IS_PSK;
458 coap_gnutls_context_t *g_context =
460 return g_context->psk_pki_enabled ? 1 : 0;
465 gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func);
466 gnutls_global_set_log_function(coap_gnutls_log_func);
479 if (c_session && c_session->
tls) {
480 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
482 return g_env->g_session;
509 coap_gnutls_context_t *g_context =
510 (coap_gnutls_context_t *)
511 gnutls_malloc(
sizeof(coap_gnutls_context_t));
515 const char *priority;
517 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
518 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
519 g_context->alpn_proto.data = gnutls_malloc(4);
520 if (g_context->alpn_proto.data) {
521 memcpy(g_context->alpn_proto.data,
"coap", 4);
522 g_context->alpn_proto.size = 4;
525 if (tls_version->
version >= 0x030606) {
526 priority = VARIANTS_3_6_6;
527 }
else if (tls_version->
version >= 0x030505) {
528 priority = VARIANTS_3_5_5;
530 priority = VARIANTS_BASE;
532 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
533 if (ret != GNUTLS_E_SUCCESS) {
534 if (ret == GNUTLS_E_INVALID_REQUEST)
535 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
537 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
552 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
554 gnutls_free(g_context->alpn_proto.data);
555 gnutls_free(g_context->root_ca_file);
556 gnutls_free(g_context->root_ca_path);
557 for (i = 0; i < g_context->pki_sni_count; i++) {
558 gnutls_free(g_context->pki_sni_entry_list[i].sni);
559 gnutls_certificate_free_credentials(
560 g_context->pki_sni_entry_list[i].pki_credentials);
562 if (g_context->pki_sni_entry_list)
563 gnutls_free(g_context->pki_sni_entry_list);
565 for (i = 0; i < g_context->psk_sni_count; i++) {
566 gnutls_free(g_context->psk_sni_entry_list[i].sni);
568 gnutls_psk_free_server_credentials(
569 g_context->psk_sni_entry_list[i].psk_credentials);
571 if (g_context->psk_sni_entry_list)
572 gnutls_free(g_context->psk_sni_entry_list);
574 gnutls_priority_deinit(g_context->priority_cache);
576 gnutls_global_deinit();
577 gnutls_free(g_context);
580#if COAP_CLIENT_SUPPORT
589psk_client_callback(gnutls_session_t g_session,
590 char **username, gnutls_datum_t *key) {
593 coap_gnutls_context_t *g_context;
595 const char *hint = gnutls_psk_client_get_hint(g_session);
605 if (c_session == NULL)
609 if (g_context == NULL)
614 temp.
s = hint ? (
const uint8_t *)hint : (
const uint8_t *)
"";
615 temp.
length = strlen((
const char *)temp.
s);
619 (
const char *)temp.
s);
631 if (cpsk_info == NULL)
636 psk_identity = &cpsk_info->
identity;
637 psk_key = &cpsk_info->
key;
643 if (psk_identity == NULL || psk_key == NULL) {
648 *username = gnutls_malloc(psk_identity->
length+1);
649 if (*username == NULL)
651 memcpy(*username, psk_identity->
s, psk_identity->
length);
652 (*username)[psk_identity->
length] =
'\000';
654 key->data = gnutls_malloc(psk_key->
length);
655 if (key->data == NULL) {
656 gnutls_free(*username);
660 memcpy(key->data, psk_key->
s, psk_key->
length);
661 key->size = psk_key->
length;
667 gnutls_certificate_type_t certificate_type;
669 const gnutls_datum_t *cert_list;
670 unsigned int cert_list_size;
672} coap_gnutls_certificate_info_t;
678static gnutls_certificate_type_t
679get_san_or_cn(gnutls_session_t g_session,
680 coap_gnutls_certificate_info_t *cert_info) {
681 gnutls_x509_crt_t cert;
688#if (GNUTLS_VERSION_NUMBER >= 0x030606)
689 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
692 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
695 cert_info->san_or_cn = NULL;
697 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
698 &cert_info->cert_list_size);
699 if (cert_info->cert_list_size == 0) {
700 return GNUTLS_CRT_UNKNOWN;
703 if (cert_info->certificate_type != GNUTLS_CRT_X509)
704 return cert_info->certificate_type;
706 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
709 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
710 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
712 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
714 size =
sizeof(dn) -1;
716 ret = gnutls_x509_crt_get_subject_alt_name(cert, 0, dn, &size, NULL);
719 gnutls_x509_crt_deinit(cert);
720 cert_info->san_or_cn = gnutls_strdup(dn);
721 return cert_info->certificate_type;
725 G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size),
"gnutls_x509_crt_get_dn");
727 gnutls_x509_crt_deinit(cert);
733 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
734 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
743 char *ecn = strchr(cn,
',');
747 cert_info->san_or_cn = gnutls_strdup(cn);
748 return cert_info->certificate_type;
750 return GNUTLS_CRT_UNKNOWN;
753 return GNUTLS_CRT_UNKNOWN;
756#if (GNUTLS_VERSION_NUMBER >= 0x030606)
757#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
758 cert_info.san_or_cn : \
759 cert_type == GNUTLS_CRT_RAW ? \
760 COAP_DTLS_RPK_CERT_CN : "?")
762#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
763 cert_info.san_or_cn : "?")
766#if (GNUTLS_VERSION_NUMBER >= 0x030606)
768check_rpk_cert(coap_gnutls_context_t *g_context,
769 coap_gnutls_certificate_info_t *cert_info,
773 if (g_context->setup_data.validate_cn_call_back) {
774 gnutls_pcert_st pcert;
778 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
779 GNUTLS_X509_FMT_DER, 0, 0),
780 "gnutls_pcert_import_rawpk_raw");
783 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
784 "gnutls_pubkey_export");
785 gnutls_pcert_deinit(&pcert);
793 g_context->setup_data.cn_call_back_arg));
809cert_verify_gnutls(gnutls_session_t g_session) {
810 unsigned int status = 0;
811 unsigned int fail = 0;
814 coap_gnutls_context_t *g_context =
816 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
817 int alert = GNUTLS_A_BAD_CERTIFICATE;
819 coap_gnutls_certificate_info_t cert_info;
820 gnutls_certificate_type_t cert_type;
822 memset(&cert_info, 0,
sizeof(cert_info));
823 cert_type = get_san_or_cn(g_session, &cert_info);
824#if (GNUTLS_VERSION_NUMBER >= 0x030606)
825 if (cert_type == GNUTLS_CRT_RAW) {
826 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
827 alert = GNUTLS_A_ACCESS_DENIED;
834 if (cert_info.cert_list_size == 0 && !g_context->setup_data.verify_peer_cert)
837 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
838 "gnutls_certificate_verify_peers");
841 status &= ~(GNUTLS_CERT_INVALID);
842 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
843 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
844 if (g_context->setup_data.allow_expired_certs) {
847 "The certificate has an invalid usage date",
853 "The certificate has an invalid usage date",
857 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
858 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
859 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
860 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
861 if (g_context->setup_data.allow_expired_crl) {
864 "The certificate's CRL entry has an invalid usage date",
870 "The certificate's CRL entry has an invalid usage date",
874 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
875 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
876 if (cert_info.self_signed) {
877 if (g_context->setup_data.allow_self_signed &&
878 !g_context->setup_data.check_common_ca) {
885 alert = GNUTLS_A_UNKNOWN_CA;
892 if (!g_context->setup_data.verify_peer_cert) {
895 "The peer certificate's CA is unknown",
899 alert = GNUTLS_A_UNKNOWN_CA;
902 "The peer certificate's CA is unknown",
907 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
908 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
912 "The certificate uses an insecure algorithm",
918 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
920 status, OUTPUT_CERT_NAME);
927 if (g_context->setup_data.validate_cn_call_back) {
928 gnutls_x509_crt_t cert;
933 const int cert_is_trusted = !status;
935 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
938 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
939 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
942 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
943 "gnutls_x509_crt_export");
944 gnutls_x509_crt_deinit(cert);
946 g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
952 g_context->setup_data.cn_call_back_arg));
954 alert = GNUTLS_A_ACCESS_DENIED;
959 if (g_context->setup_data.additional_tls_setup_call_back) {
961 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
962 &g_context->setup_data)) {
968 if (cert_info.san_or_cn)
969 gnutls_free(cert_info.san_or_cn);
974 if (cert_info.san_or_cn)
975 gnutls_free(cert_info.san_or_cn);
977 if (!g_env->sent_alert) {
978 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
979 g_env->sent_alert = 1;
993cert_verify_callback_gnutls(gnutls_session_t g_session) {
994 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
995 if (cert_verify_gnutls(g_session) == 0) {
1003#define min(a,b) ((a) < (b) ? (a) : (b))
1007pin_callback(
void *user_data,
int attempt,
1027check_null_memory(gnutls_datum_t *datum,
1028 const uint8_t *buf,
size_t len,
int *alloced) {
1031 if (buf[len-1] !=
'\000') {
1034 datum->data = gnutls_malloc(len + 1);
1037 return GNUTLS_E_MEMORY_ERROR;
1039 memcpy(datum->data, buf, len);
1040 datum->data[len] =
'\000';
1044 memcpy(&datum->data,
1045 &buf,
sizeof(datum->data));
1055setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1056 gnutls_session_t g_session,
1057 coap_gnutls_context_t *g_context,
1061 gnutls_datum_t cert;
1062 gnutls_datum_t pkey;
1064 int alloced_cert_memory = 0;
1065 int alloced_pkey_memory = 0;
1066 int alloced_ca_memory = 0;
1067 int have_done_key = 0;
1074 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1075 "gnutls_certificate_allocate_credentials");
1092#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1096 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1099 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1105 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1112 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1126 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1130 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1133 GNUTLS_X509_FMT_PEM) < 0)) {
1140 if ((ret = check_null_memory(&cert,
1143 &alloced_cert_memory)) < 0) {
1148 if ((ret = check_null_memory(&pkey,
1151 &alloced_pkey_memory)) < 0) {
1152 if (alloced_cert_memory)
1153 gnutls_free(cert.data);
1158 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1161 GNUTLS_X509_FMT_PEM)) < 0) {
1162 if (alloced_cert_memory)
1163 gnutls_free(cert.data);
1164 if (alloced_pkey_memory)
1165 gnutls_free(pkey.data);
1170 if (alloced_cert_memory)
1171 gnutls_free(cert.data);
1172 if (alloced_pkey_memory)
1173 gnutls_free(pkey.data);
1176#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1177 if ((ret = check_null_memory(&cert,
1180 &alloced_cert_memory)) < 0) {
1185 if ((ret = check_null_memory(&pkey,
1188 &alloced_pkey_memory)) < 0) {
1189 if (alloced_cert_memory)
1190 gnutls_free(cert.data);
1195 if (strstr((
char *)pkey.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1196 gnutls_datum_t der_private;
1198 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &pkey,
1199 &der_private) == 0) {
1204 gnutls_datum_t tspki;
1206 tspki.data = spki->
s;
1207 tspki.size = spki->
length;
1208 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1211 GNUTLS_X509_FMT_DER, NULL,
1212 COAP_GNUTLS_KEY_RPK,
1219 gnutls_free(der_private.data);
1222 if (!have_done_key) {
1223 if ((ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1226 GNUTLS_X509_FMT_PEM, NULL,
1227 COAP_GNUTLS_KEY_RPK,
1229 if (alloced_cert_memory)
1230 gnutls_free(cert.data);
1231 if (alloced_pkey_memory)
1232 gnutls_free(pkey.data);
1235 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1238 if (alloced_cert_memory)
1239 gnutls_free(cert.data);
1240 if (alloced_pkey_memory)
1241 gnutls_free(pkey.data);
1244 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1247 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1250 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1253 GNUTLS_X509_FMT_DER) < 0)) {
1260 if ((ret = check_null_memory(&cert,
1263 &alloced_cert_memory)) < 0) {
1268 if ((ret = check_null_memory(&pkey,
1271 &alloced_pkey_memory)) < 0) {
1272 if (alloced_cert_memory)
1273 gnutls_free(cert.data);
1278 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1281 GNUTLS_X509_FMT_DER)) < 0) {
1282 if (alloced_cert_memory)
1283 gnutls_free(cert.data);
1284 if (alloced_pkey_memory)
1285 gnutls_free(pkey.data);
1290 if (alloced_cert_memory)
1291 gnutls_free(cert.data);
1292 if (alloced_pkey_memory)
1293 gnutls_free(pkey.data);
1296 gnutls_pkcs11_set_pin_function(pin_callback, &setup_data->
pki_key);
1297 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1300 GNUTLS_X509_FMT_DER)) < 0) {
1307#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1308 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1309 if ((ret = gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1312 GNUTLS_X509_FMT_PEM, NULL,
1313 COAP_GNUTLS_KEY_RPK,
1314 NULL, 0, GNUTLS_PKCS_PLAIN, 0))) {
1320 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1321 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1328 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1339 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1341 GNUTLS_X509_FMT_PEM) < 0)) {
1348 if ((ret = check_null_memory(&ca,
1351 &alloced_ca_memory)) < 0) {
1356 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1358 GNUTLS_X509_FMT_PEM)) < 0) {
1359 if (alloced_ca_memory)
1360 gnutls_free(ca.data);
1365 if (alloced_ca_memory)
1366 gnutls_free(ca.data);
1372 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1374 GNUTLS_X509_FMT_DER) < 0)) {
1381 if ((ret = check_null_memory(&ca,
1384 &alloced_ca_memory)) < 0) {
1389 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1391 GNUTLS_X509_FMT_DER)) <= 0) {
1392 if (alloced_ca_memory)
1393 gnutls_free(ca.data);
1398 if (alloced_ca_memory)
1399 gnutls_free(ca.data);
1402 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1404 GNUTLS_X509_FMT_DER)) <= 0) {
1417 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1421 if (g_context->root_ca_file) {
1422 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1423 g_context->root_ca_file,
1424 GNUTLS_X509_FMT_PEM);
1426 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1429 if (g_context->root_ca_path) {
1430#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1431 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1432 g_context->root_ca_path,
1433 GNUTLS_X509_FMT_PEM),
1434 "gnutls_certificate_set_x509_trust_dir");
1437 gnutls_certificate_send_x509_rdn_sequence(g_session,
1439 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1441 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1442 "gnutls_certificate_set_x509_system_trust");
1446 gnutls_certificate_set_verify_function(*pki_credentials,
1447 cert_verify_callback_gnutls);
1451 gnutls_certificate_set_verify_limits(*pki_credentials,
1460 gnutls_certificate_set_verify_flags(*pki_credentials,
1462 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1465 return GNUTLS_E_SUCCESS;
1471#if COAP_SERVER_SUPPORT
1477setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1483 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1484 "gnutls_psk_allocate_server_credentials");
1485 gnutls_psk_set_server_credentials_function(*psk_credentials,
1486 psk_server_callback);
1490 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1491 "gnutls_psk_set_server_credentials_hint");
1494 return GNUTLS_E_SUCCESS;
1505post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1508 coap_gnutls_context_t *g_context =
1510 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1511 int ret = GNUTLS_E_SUCCESS;
1521 name = gnutls_malloc(len);
1523 return GNUTLS_E_MEMORY_ERROR;
1526 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1527 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1529 new_name = gnutls_realloc(name, len);
1530 if (new_name == NULL) {
1531 ret = GNUTLS_E_MEMORY_ERROR;
1539 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1542 if (ret != GNUTLS_E_SUCCESS)
1545 if (type != GNUTLS_NAME_DNS)
1556 for (i = 0; i < g_context->psk_sni_count; i++) {
1557 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1561 if (i == g_context->psk_sni_count) {
1572 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1573 GNUTLS_A_UNRECOGNIZED_NAME));
1574 g_env->sent_alert = 1;
1575 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1579 g_context->psk_sni_entry_list =
1580 gnutls_realloc(g_context->psk_sni_entry_list,
1581 (i+1)*
sizeof(psk_sni_entry));
1582 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1583 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1585 sni_setup_data.
psk_info = *new_entry;
1586 if ((ret = setup_psk_credentials(
1587 &g_context->psk_sni_entry_list[i].psk_credentials,
1589 &sni_setup_data)) < 0) {
1591 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1592 GNUTLS_A_BAD_CERTIFICATE));
1593 g_env->sent_alert = 1;
1597 g_context->psk_sni_count++;
1599 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1600 g_context->psk_sni_entry_list[i].psk_credentials),
1601 "gnutls_credentials_set");
1603 &g_context->psk_sni_entry_list[i].psk_info.hint);
1605 &g_context->psk_sni_entry_list[i].psk_info.key);
1621post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1624 coap_gnutls_context_t *g_context =
1626 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1627 int ret = GNUTLS_E_SUCCESS;
1630 if (g_context->setup_data.validate_sni_call_back) {
1637 name = gnutls_malloc(len);
1639 return GNUTLS_E_MEMORY_ERROR;
1642 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1643 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1645 new_name = gnutls_realloc(name, len);
1646 if (new_name == NULL) {
1647 ret = GNUTLS_E_MEMORY_ERROR;
1655 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1658 if (ret != GNUTLS_E_SUCCESS)
1661 if (type != GNUTLS_NAME_DNS)
1672 for (i = 0; i < g_context->pki_sni_count; i++) {
1673 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1677 if (i == g_context->pki_sni_count) {
1684 g_context->setup_data.validate_sni_call_back(name,
1685 g_context->setup_data.sni_call_back_arg));
1687 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1688 GNUTLS_A_UNRECOGNIZED_NAME));
1689 g_env->sent_alert = 1;
1690 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1694 g_context->pki_sni_entry_list = gnutls_realloc(
1695 g_context->pki_sni_entry_list,
1696 (i+1)*
sizeof(pki_sni_entry));
1697 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1698 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1699 sni_setup_data = g_context->setup_data;
1700 sni_setup_data.
pki_key = *new_entry;
1701 if ((ret = setup_pki_credentials(&g_context->pki_sni_entry_list[i].pki_credentials,
1706 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1707 GNUTLS_A_BAD_CERTIFICATE));
1708 g_env->sent_alert = 1;
1712 g_context->pki_sni_count++;
1714 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1715 g_context->pki_sni_entry_list[i].pki_credentials),
1716 "gnutls_credentials_set");
1728#if COAP_CLIENT_SUPPORT
1734setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1735 coap_gnutls_context_t *g_context =
1739 g_context->psk_pki_enabled |= IS_CLIENT;
1740 if (g_context->psk_pki_enabled & IS_PSK) {
1742 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1743 "gnutls_psk_allocate_client_credentials");
1744 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1745 psk_client_callback);
1746 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1747 g_env->psk_cl_credentials),
1748 "gnutls_credentials_set");
1751 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1754 "gnutls_server_name_set");
1760 if (tls_version->
version >= 0x030604) {
1762 const char *priority;
1764 if (tls_version->
version >= 0x030606) {
1765 priority = VARIANTS_NO_TLS13_3_6_6;
1767 priority = VARIANTS_NO_TLS13_3_6_4;
1769 ret = gnutls_priority_set_direct(g_env->g_session,
1772 if (ret == GNUTLS_E_INVALID_REQUEST)
1773 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1775 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1782 if ((g_context->psk_pki_enabled & IS_PKI) ||
1783 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1789 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1790 g_context, setup_data,
1792 "setup_pki_credentials");
1794 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1795 g_env->pki_credentials),
1796 "gnutls_credentials_set");
1799 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1800 &g_context->alpn_proto, 1, 0),
1801 "gnutls_alpn_set_protocols");
1805 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1808 "gnutls_server_name_set");
1811 return GNUTLS_E_SUCCESS;
1818#if COAP_SERVER_SUPPORT
1827psk_server_callback(gnutls_session_t g_session,
1828 const char *identity,
1829 gnutls_datum_t *key) {
1832 coap_gnutls_context_t *g_context;
1837 if (c_session == NULL)
1841 if (g_context == NULL)
1847 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
1848 lidentity.
length = strlen((
const char *)lidentity.
s);
1852 (
int)lidentity.
length, (
const char *)lidentity.
s);
1864 if (psk_key == NULL)
1867 key->data = gnutls_malloc(psk_key->
length);
1868 if (key->data == NULL)
1870 memcpy(key->data, psk_key->
s, psk_key->
length);
1871 key->size = psk_key->
length;
1880setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1881 coap_gnutls_context_t *g_context =
1883 int ret = GNUTLS_E_SUCCESS;
1885 g_context->psk_pki_enabled |= IS_SERVER;
1886 if (g_context->psk_pki_enabled & IS_PSK) {
1887 G_CHECK(setup_psk_credentials(
1888 &g_env->psk_sv_credentials,
1891 "setup_psk_credentials\n");
1892 G_CHECK(gnutls_credentials_set(g_env->g_session,
1894 g_env->psk_sv_credentials),
1895 "gnutls_credentials_set\n");
1896 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1897 post_client_hello_gnutls_psk);
1900 if (g_context->psk_pki_enabled & IS_PKI) {
1902 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1903 g_context, setup_data,
1905 "setup_pki_credentials");
1908 gnutls_certificate_server_set_request(g_env->g_session,
1909 GNUTLS_CERT_REQUIRE);
1911 gnutls_certificate_server_set_request(g_env->g_session,
1912 GNUTLS_CERT_REQUEST);
1914 gnutls_certificate_server_set_request(g_env->g_session,
1915 GNUTLS_CERT_IGNORE);
1918 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1919 post_client_hello_gnutls_pki);
1921 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1922 g_env->pki_credentials),
1923 "gnutls_credentials_set\n");
1925 return GNUTLS_E_SUCCESS;
1938coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
1943 if (!c_session->
tls) {
1947 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
1950 if (data != NULL && data->pdu_len > 0) {
1951 if (outl < data->pdu_len) {
1952 memcpy(out, data->pdu, outl);
1954 if (!data->peekmode) {
1956 data->pdu_len -= outl;
1959 memcpy(out, data->pdu, data->pdu_len);
1960 ret = data->pdu_len;
1961 if (!data->peekmode) {
1981coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
1982 size_t send_buffer_length) {
1983 ssize_t result = -1;
1988#if COAP_SERVER_SUPPORT
1997 send_buffer, send_buffer_length);
1998 if (result != (
int)send_buffer_length) {
1999 int keep_errno = errno;
2001 coap_log_warn(
"coap_netif_dgrm_write failed (%zd != %zu)\n",
2002 result, send_buffer_length);
2022receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
2026 fd_set readfds, writefds, exceptfds;
2028 int nfds = c_session->
sock.
fd +1;
2029 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2033 g_env->coap_ssl_data.pdu_len > 0) {
2039 FD_ZERO(&exceptfds);
2040 FD_SET(c_session->
sock.
fd, &readfds);
2041 if (!(g_env && g_env->doing_dtls_timeout)) {
2042 FD_SET(c_session->
sock.
fd, &writefds);
2043 FD_SET(c_session->
sock.
fd, &exceptfds);
2049 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2054static coap_gnutls_env_t *
2056 coap_gnutls_context_t *g_context =
2058 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2059#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2060 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2062 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2069 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2073 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2075 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2077 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2078 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2079 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2081 gnutls_transport_set_ptr(g_env->g_session, c_session);
2083 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2084 "gnutls_priority_set");
2086 if (type == GNUTLS_SERVER) {
2087#if COAP_SERVER_SUPPORT
2088 G_CHECK(setup_server_ssl_session(c_session, g_env),
2089 "setup_server_ssl_session");
2094#if COAP_CLIENT_SUPPORT
2095 G_CHECK(setup_client_ssl_session(c_session, g_env),
2096 "setup_client_ssl_session");
2102 gnutls_handshake_set_timeout(g_env->g_session,
2103 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2104 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2105 COAP_DTLS_RETRANSMIT_TOTAL_MS);
2116coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2117 coap_gnutls_env_t *g_env,
2118 coap_free_bye_t free_bye) {
2123 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2125 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2126 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2128 gnutls_deinit(g_env->g_session);
2129 g_env->g_session = NULL;
2130 if (g_context->psk_pki_enabled & IS_PSK) {
2131 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2132 g_env->psk_cl_credentials != NULL) {
2133 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2134 g_env->psk_cl_credentials = NULL;
2137 if (g_env->psk_sv_credentials != NULL)
2138 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2139 g_env->psk_sv_credentials = NULL;
2142 if ((g_context->psk_pki_enabled & IS_PKI) ||
2143 (g_context->psk_pki_enabled &
2144 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2145 gnutls_certificate_free_credentials(g_env->pki_credentials);
2146 g_env->pki_credentials = NULL;
2148 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2153#if COAP_SERVER_SUPPORT
2156 coap_gnutls_env_t *g_env =
2157 (coap_gnutls_env_t *)c_session->
tls;
2159 gnutls_transport_set_ptr(g_env->g_session, c_session);
2167 gnutls_session_t g_session) {
2168#if COAP_MAX_LOGGING_LEVEL > 0
2169 int last_alert = gnutls_alert_get(g_session);
2171 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2174 last_alert, gnutls_alert_get_name(last_alert));
2178 last_alert, gnutls_alert_get_name(last_alert));
2191do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2194 ret = gnutls_handshake(g_env->g_session);
2196 case GNUTLS_E_SUCCESS:
2197 g_env->established = 1;
2202 case GNUTLS_E_INTERRUPTED:
2206 case GNUTLS_E_AGAIN:
2210 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2214 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2216 g_env->sent_alert = 1;
2217 log_last_alert(c_session, g_env->g_session);
2219 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2220 case GNUTLS_E_UNEXPECTED_PACKET:
2224 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2225 log_last_alert(c_session, g_env->g_session);
2229 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2230#if (GNUTLS_VERSION_NUMBER > 0x030606)
2231 case GNUTLS_E_CERTIFICATE_REQUIRED:
2233 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2235 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2236 GNUTLS_A_BAD_CERTIFICATE));
2237 g_env->sent_alert = 1;
2241 case GNUTLS_E_DECRYPTION_FAILED:
2244 gnutls_strerror(ret));
2245 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2246 GNUTLS_A_DECRYPT_ERROR));
2247 g_env->sent_alert = 1;
2251 case GNUTLS_E_CERTIFICATE_ERROR:
2252 if (g_env->sent_alert) {
2258 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2259 case GNUTLS_E_NO_CIPHER_SUITES:
2260 case GNUTLS_E_INVALID_SESSION:
2263 gnutls_strerror(ret));
2264 if (!g_env->sent_alert) {
2265 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2266 GNUTLS_A_HANDSHAKE_FAILURE));
2267 g_env->sent_alert = 1;
2272 case GNUTLS_E_SESSION_EOF:
2273 case GNUTLS_E_PREMATURE_TERMINATION:
2274 case GNUTLS_E_TIMEDOUT:
2275 case GNUTLS_E_PULL_ERROR:
2276 case GNUTLS_E_PUSH_ERROR:
2282 "returned %d: '%s'\n",
2283 ret, gnutls_strerror(ret));
2290#if COAP_CLIENT_SUPPORT
2293 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2297 ret = do_gnutls_handshake(c_session, g_env);
2302 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2312 if (c_session && c_session->
context && c_session->
tls) {
2316 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2317 c_session->
tls = NULL;
2324 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2328 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2329 (
unsigned int)c_session->
mtu),
2330 "gnutls_dtls_set_data_mtu");
2342 const uint8_t *data,
size_t data_len) {
2344 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2346 assert(g_env != NULL);
2351 if (g_env->established) {
2352 ret = gnutls_record_send(g_env->g_session, data, data_len);
2356 case GNUTLS_E_AGAIN:
2359 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2361 g_env->sent_alert = 1;
2362 log_last_alert(c_session, g_env->g_session);
2368 "returned %d: '%s'\n",
2369 ret, gnutls_strerror(ret));
2378 ret = do_gnutls_handshake(c_session, g_env);
2410 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2413 if (g_env && g_env->g_session) {
2414 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2427 g_env->last_timeout = now;
2428 return now + rem_ms;
2440 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2443 g_env->doing_dtls_timeout = 1;
2445 (do_gnutls_handshake(c_session, g_env) < 0)) {
2447 g_env->doing_dtls_timeout = 0;
2451 g_env->doing_dtls_timeout = 0;
2464 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2466 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2470 assert(g_env != NULL);
2472 if (ssl_data->pdu_len)
2473 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2475 ssl_data->pdu = data;
2476 ssl_data->pdu_len = data_len;
2479 if (g_env->established) {
2483 gnutls_transport_set_ptr(g_env->g_session, c_session);
2486 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2489 }
else if (ret == 0) {
2493 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2495 g_env->sent_alert = 1;
2496 log_last_alert(c_session, g_env->g_session);
2500 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2501 log_last_alert(c_session, g_env->g_session);
2506 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2512 ret = do_gnutls_handshake(c_session, g_env);
2517 if (ssl_data->pdu_len && !g_env->sent_alert) {
2519 ret = do_gnutls_handshake(c_session, g_env);
2539 if (ssl_data && ssl_data->pdu_len) {
2541 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2542 ssl_data->pdu_len = 0;
2543 ssl_data->pdu = NULL;
2552#if COAP_SERVER_SUPPORT
2560 const uint8_t *data,
2563 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2564 coap_ssl_t *ssl_data;
2568 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2570 c_session->
tls = g_env;
2571 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2572 GNUTLS_COOKIE_KEY_SIZE);
2579 gnutls_dtls_prestate_st prestate;
2582 memset(&prestate, 0,
sizeof(prestate));
2584 memcpy(&data_rw, &data,
sizeof(data_rw));
2585 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2592 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2600 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2603 ssl_data = &g_env->coap_ssl_data;
2604 ssl_data->pdu = data;
2605 ssl_data->pdu_len = data_len;
2607 ret = do_gnutls_handshake(c_session, g_env);
2614 g_env, COAP_FREE_BYE_NONE);
2615 c_session->
tls = NULL;
2623 if (ssl_data && ssl_data->pdu_len) {
2625 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2626 ssl_data->pdu_len = 0;
2627 ssl_data->pdu = NULL;
2638#if !COAP_DISABLE_TCP
2646coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2668coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2677 (errno == EPIPE || errno == ECONNRESET)) {
2703#if COAP_CLIENT_SUPPORT
2706 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2707 coap_gnutls_context_t *g_context =
2709#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2710 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2712 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2719 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2721 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2723 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2724 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2725 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2727 gnutls_transport_set_ptr(g_env->g_session, c_session);
2729 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2730 setup_client_ssl_session(c_session, g_env);
2732 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2734 c_session->
tls = g_env;
2735 ret = do_gnutls_handshake(c_session, g_env);
2749#if COAP_SERVER_SUPPORT
2752 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2753 coap_gnutls_context_t *g_context =
2755#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2756 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2758 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2764 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2766 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2768 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2769 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2770 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2772 gnutls_transport_set_ptr(g_env->g_session, c_session);
2774 setup_server_ssl_session(c_session, g_env);
2776 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2777 gnutls_handshake_set_timeout(g_env->g_session,
2778 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2780 c_session->
tls = g_env;
2781 ret = do_gnutls_handshake(c_session, g_env);
2808 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2810 assert(g_env != NULL);
2813 if (g_env->established) {
2814 ret = gnutls_record_send(g_env->g_session, data, data_len);
2818 case GNUTLS_E_AGAIN:
2821 case GNUTLS_E_PUSH_ERROR:
2822 case GNUTLS_E_PULL_ERROR:
2823 case GNUTLS_E_PREMATURE_TERMINATION:
2826 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2828 g_env->sent_alert = 1;
2829 log_last_alert(c_session, g_env->g_session);
2834 "returned %d: '%s'\n",
2835 ret, gnutls_strerror(ret));
2844 ret = do_gnutls_handshake(c_session, g_env);
2867 if (ret == (ssize_t)data_len)
2884 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2893 if (!g_env->established && !g_env->sent_alert) {
2894 ret = do_gnutls_handshake(c_session, g_env);
2903 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2909 case GNUTLS_E_AGAIN:
2913 case GNUTLS_E_PULL_ERROR:
2916 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2918 g_env->sent_alert = 1;
2919 log_last_alert(c_session, g_env->g_session);
2922 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2923 log_last_alert(c_session, g_env->g_session);
2928 "returned %d: '%s'\n",
2929 ret, gnutls_strerror(ret));
2954#if COAP_SERVER_SUPPORT
2957 gnutls_hash_hd_t digest_ctx;
2959 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
2967 gnutls_hash_deinit(digest_ctx, NULL);
2972 const uint8_t *data,
2974 int ret = gnutls_hash(digest_ctx, data, data_len);
2982 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
2994static struct hash_algs {
2996 gnutls_digest_algorithm_t dig_type;
3004static gnutls_digest_algorithm_t
3005get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3008 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3009 if (hashs[idx].alg == alg) {
3010 *hash_len = hashs[idx].dig_size;
3011 return hashs[idx].dig_type;
3014 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3015 return GNUTLS_DIG_UNKNOWN;
3023 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3024 gnutls_hash_hd_t digest_ctx;
3028 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3029 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3033 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3036 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
3043 gnutls_hash_output(digest_ctx,
dummy->s);
3046 gnutls_hash_deinit(digest_ctx, NULL);
3051 gnutls_hash_deinit(digest_ctx, NULL);
3056#if COAP_OSCORE_SUPPORT
3067static struct cipher_algs {
3069 gnutls_cipher_algorithm_t cipher_type;
3074static gnutls_cipher_algorithm_t
3078 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3079 if (ciphers[idx].alg == alg)
3080 return ciphers[idx].cipher_type;
3082 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3091static struct hmac_algs {
3093 gnutls_mac_algorithm_t hmac_type;
3099static gnutls_mac_algorithm_t
3103 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3104 if (hmacs[idx].hmac_alg == hmac_alg)
3105 return hmacs[idx].hmac_type;
3107 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3113 return get_cipher_alg(alg);
3122 return get_hmac_alg(hmac_alg);
3130 size_t *max_result_len) {
3131 gnutls_aead_cipher_hd_t ctx;
3135 size_t result_len = *max_result_len;
3136 gnutls_cipher_algorithm_t algo;
3138 uint8_t *key_data_rw;
3144 assert(params != NULL);
3148 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3149 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3153 tag_size = gnutls_cipher_get_tag_size(algo);
3157 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3158 key.data = key_data_rw;
3168 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3170 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3180 "gnutls_aead_cipher_encrypt");
3181 *max_result_len = result_len;
3184 gnutls_aead_cipher_deinit(ctx);
3185 return ret == 1 ? 1 : 0;
3193 size_t *max_result_len) {
3194 gnutls_aead_cipher_hd_t ctx;
3198 size_t result_len = *max_result_len;
3199 gnutls_cipher_algorithm_t algo;
3201 uint8_t *key_data_rw;
3207 assert(params != NULL);
3212 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3213 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3217 tag_size = gnutls_cipher_get_tag_size(algo);
3221 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3222 key.data = key_data_rw;
3232 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3234 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3244 "gnutls_aead_cipher_decrypt");
3245 *max_result_len = result_len;
3248 gnutls_aead_cipher_deinit(ctx);
3249 return ret == 1 ? 1 : 0;
3257 gnutls_hmac_hd_t ctx;
3260 gnutls_mac_algorithm_t mac_algo;
3266 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3267 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3270 len = gnutls_hmac_get_len(mac_algo);
3277 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3278 "gnutls_hmac_init");
3279 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3280 gnutls_hmac_output(ctx,
dummy->s);
3286 gnutls_hmac_deinit(ctx, NULL);
3287 return ret == 1 ? 1 : 0;
3298#pragma GCC diagnostic ignored "-Wunused-function"
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
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)
static coap_log_t dtls_log_level
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)
coap_binary_t * get_asn1_spki(const uint8_t *data, size_t size)
Abstract SPKI public key from the ASN1.
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_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.
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.
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_FAIL_NOT_SUPPORTED
#define COAP_DTLS_HINT_LENGTH
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
#define COAP_DTLS_RPK_CERT_CN
@ 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_GNUTLS
Using GnuTLS 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(...)
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(...)
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_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
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_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.
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
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 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.
size_t length
length of 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 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.
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 that holds the Client PSK information.
coap_bin_const_t identity
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.
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
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 cert_chain_validation
1 if to check cert_chain_verify_depth
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 verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
char * client_sni
If not NULL, SNI to use in client TLS setup.
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.
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_dtls_spsk_info_t psk_info
Server PSK definition.
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
const char * user_pin
define: User pin to access type PKCS11.
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_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)
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_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
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