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;
507 const char *err =
"Unknown Error";
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 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
518 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
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) {
835 if (!g_context->setup_data.verify_peer_cert)
841 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
842 "gnutls_certificate_verify_peers");
845 status &= ~(GNUTLS_CERT_INVALID);
846 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
847 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
848 if (g_context->setup_data.allow_expired_certs) {
851 "The certificate has an invalid usage date",
857 "The certificate has an invalid usage date",
861 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
862 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
863 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
864 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
865 if (g_context->setup_data.allow_expired_crl) {
868 "The certificate's CRL entry has an invalid usage date",
874 "The certificate's CRL entry has an invalid usage date",
878 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
879 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
880 if (cert_info.self_signed) {
881 if (g_context->setup_data.allow_self_signed &&
882 !g_context->setup_data.check_common_ca) {
889 alert = GNUTLS_A_UNKNOWN_CA;
896 if (!g_context->setup_data.verify_peer_cert) {
899 "The peer certificate's CA is unknown",
903 alert = GNUTLS_A_UNKNOWN_CA;
906 "The peer certificate's CA is unknown",
911 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
912 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
916 "The certificate uses an insecure algorithm",
922 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
924 status, OUTPUT_CERT_NAME);
931 if (g_context->setup_data.validate_cn_call_back) {
932 gnutls_x509_crt_t cert;
937 const int cert_is_trusted = !status;
939 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
942 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
943 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
946 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
947 "gnutls_x509_crt_export");
948 gnutls_x509_crt_deinit(cert);
950 g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
956 g_context->setup_data.cn_call_back_arg));
958 alert = GNUTLS_A_ACCESS_DENIED;
963 if (g_context->setup_data.additional_tls_setup_call_back) {
965 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
966 &g_context->setup_data)) {
972 if (cert_info.san_or_cn)
973 gnutls_free(cert_info.san_or_cn);
978 if (cert_info.san_or_cn)
979 gnutls_free(cert_info.san_or_cn);
981 if (!g_env->sent_alert) {
982 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
983 g_env->sent_alert = 1;
997cert_verify_callback_gnutls(gnutls_session_t g_session) {
998 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
999 if (cert_verify_gnutls(g_session) == 0) {
1007#define min(a,b) ((a) < (b) ? (a) : (b))
1011pin_callback(
void *user_data,
int attempt,
1031check_null_memory(gnutls_datum_t *datum,
1032 const uint8_t *buf,
size_t len,
int *alloced) {
1035 if (buf[len-1] !=
'\000') {
1038 datum->data = gnutls_malloc(len + 1);
1041 return GNUTLS_E_MEMORY_ERROR;
1043 memcpy(datum->data, buf, len);
1044 datum->data[len] =
'\000';
1048 memcpy(&datum->data,
1049 &buf,
sizeof(datum->data));
1059setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1060 gnutls_session_t g_session,
1061 coap_gnutls_context_t *g_context,
1065 gnutls_datum_t cert;
1066 gnutls_datum_t pkey;
1068 int alloced_cert_memory = 0;
1069 int alloced_pkey_memory = 0;
1070 int alloced_ca_memory = 0;
1071 int have_done_key = 0;
1078 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1079 "gnutls_certificate_allocate_credentials");
1096#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1100 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1103 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1109 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1116 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1130 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1134 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1137 GNUTLS_X509_FMT_PEM) < 0)) {
1144 if ((ret = check_null_memory(&cert,
1147 &alloced_cert_memory)) < 0) {
1152 if ((ret = check_null_memory(&pkey,
1155 &alloced_pkey_memory)) < 0) {
1156 if (alloced_cert_memory)
1157 gnutls_free(cert.data);
1162 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1165 GNUTLS_X509_FMT_PEM)) < 0) {
1166 if (alloced_cert_memory)
1167 gnutls_free(cert.data);
1168 if (alloced_pkey_memory)
1169 gnutls_free(pkey.data);
1174 if (alloced_cert_memory)
1175 gnutls_free(cert.data);
1176 if (alloced_pkey_memory)
1177 gnutls_free(pkey.data);
1180#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1181 if ((ret = check_null_memory(&cert,
1184 &alloced_cert_memory)) < 0) {
1189 if ((ret = check_null_memory(&pkey,
1192 &alloced_pkey_memory)) < 0) {
1193 if (alloced_cert_memory)
1194 gnutls_free(cert.data);
1199 if (strstr((
char *)pkey.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1200 gnutls_datum_t der_private;
1202 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &pkey,
1203 &der_private) == 0) {
1208 gnutls_datum_t tspki;
1210 tspki.data = spki->
s;
1211 tspki.size = spki->
length;
1212 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1215 GNUTLS_X509_FMT_DER, NULL,
1216 COAP_GNUTLS_KEY_RPK,
1223 gnutls_free(der_private.data);
1226 if (!have_done_key) {
1227 if ((ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1230 GNUTLS_X509_FMT_PEM, NULL,
1231 COAP_GNUTLS_KEY_RPK,
1233 if (alloced_cert_memory)
1234 gnutls_free(cert.data);
1235 if (alloced_pkey_memory)
1236 gnutls_free(pkey.data);
1239 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1242 if (alloced_cert_memory)
1243 gnutls_free(cert.data);
1244 if (alloced_pkey_memory)
1245 gnutls_free(pkey.data);
1248 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1251 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1254 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1257 GNUTLS_X509_FMT_DER) < 0)) {
1264 if ((ret = check_null_memory(&cert,
1267 &alloced_cert_memory)) < 0) {
1272 if ((ret = check_null_memory(&pkey,
1275 &alloced_pkey_memory)) < 0) {
1276 if (alloced_cert_memory)
1277 gnutls_free(cert.data);
1282 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1285 GNUTLS_X509_FMT_DER)) < 0) {
1286 if (alloced_cert_memory)
1287 gnutls_free(cert.data);
1288 if (alloced_pkey_memory)
1289 gnutls_free(pkey.data);
1294 if (alloced_cert_memory)
1295 gnutls_free(cert.data);
1296 if (alloced_pkey_memory)
1297 gnutls_free(pkey.data);
1300 gnutls_pkcs11_set_pin_function(pin_callback, &setup_data->
pki_key);
1301 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1304 GNUTLS_X509_FMT_DER)) < 0) {
1311#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1312 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1313 if ((ret = gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1316 GNUTLS_X509_FMT_PEM, NULL,
1317 COAP_GNUTLS_KEY_RPK,
1318 NULL, 0, GNUTLS_PKCS_PLAIN, 0))) {
1324 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1325 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1332 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1343 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1345 GNUTLS_X509_FMT_PEM) < 0)) {
1352 if ((ret = check_null_memory(&ca,
1355 &alloced_ca_memory)) < 0) {
1360 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1362 GNUTLS_X509_FMT_PEM)) < 0) {
1363 if (alloced_ca_memory)
1364 gnutls_free(ca.data);
1369 if (alloced_ca_memory)
1370 gnutls_free(ca.data);
1376 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1378 GNUTLS_X509_FMT_DER) < 0)) {
1385 if ((ret = check_null_memory(&ca,
1388 &alloced_ca_memory)) < 0) {
1393 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1395 GNUTLS_X509_FMT_DER)) <= 0) {
1396 if (alloced_ca_memory)
1397 gnutls_free(ca.data);
1402 if (alloced_ca_memory)
1403 gnutls_free(ca.data);
1406 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1408 GNUTLS_X509_FMT_DER)) <= 0) {
1421 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1425 if (g_context->root_ca_file) {
1426 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1427 g_context->root_ca_file,
1428 GNUTLS_X509_FMT_PEM);
1430 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1433 if (g_context->root_ca_path) {
1434#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1435 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1436 g_context->root_ca_path,
1437 GNUTLS_X509_FMT_PEM),
1438 "gnutls_certificate_set_x509_trust_dir");
1441 gnutls_certificate_send_x509_rdn_sequence(g_session,
1443 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1445 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1446 "gnutls_certificate_set_x509_system_trust");
1450 gnutls_certificate_set_verify_function(*pki_credentials,
1451 cert_verify_callback_gnutls);
1455 gnutls_certificate_set_verify_limits(*pki_credentials,
1464 gnutls_certificate_set_verify_flags(*pki_credentials,
1466 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1469 return GNUTLS_E_SUCCESS;
1475#if COAP_SERVER_SUPPORT
1481setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1487 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1488 "gnutls_psk_allocate_server_credentials");
1489 gnutls_psk_set_server_credentials_function(*psk_credentials,
1490 psk_server_callback);
1494 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1495 "gnutls_psk_set_server_credentials_hint");
1498 return GNUTLS_E_SUCCESS;
1509post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1512 coap_gnutls_context_t *g_context =
1514 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1515 int ret = GNUTLS_E_SUCCESS;
1525 name = gnutls_malloc(len);
1527 return GNUTLS_E_MEMORY_ERROR;
1530 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1531 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1533 new_name = gnutls_realloc(name, len);
1534 if (new_name == NULL) {
1535 ret = GNUTLS_E_MEMORY_ERROR;
1543 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1546 if (ret != GNUTLS_E_SUCCESS)
1549 if (type != GNUTLS_NAME_DNS)
1560 for (i = 0; i < g_context->psk_sni_count; i++) {
1561 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1565 if (i == g_context->psk_sni_count) {
1576 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1577 GNUTLS_A_UNRECOGNIZED_NAME));
1578 g_env->sent_alert = 1;
1579 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1583 g_context->psk_sni_entry_list =
1584 gnutls_realloc(g_context->psk_sni_entry_list,
1585 (i+1)*
sizeof(psk_sni_entry));
1586 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1587 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1589 sni_setup_data.
psk_info = *new_entry;
1590 if ((ret = setup_psk_credentials(
1591 &g_context->psk_sni_entry_list[i].psk_credentials,
1593 &sni_setup_data)) < 0) {
1595 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1596 GNUTLS_A_BAD_CERTIFICATE));
1597 g_env->sent_alert = 1;
1601 g_context->psk_sni_count++;
1603 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1604 g_context->psk_sni_entry_list[i].psk_credentials),
1605 "gnutls_credentials_set");
1607 &g_context->psk_sni_entry_list[i].psk_info.hint);
1609 &g_context->psk_sni_entry_list[i].psk_info.key);
1625post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1628 coap_gnutls_context_t *g_context =
1630 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1631 int ret = GNUTLS_E_SUCCESS;
1634 if (g_context->setup_data.validate_sni_call_back) {
1641 name = gnutls_malloc(len);
1643 return GNUTLS_E_MEMORY_ERROR;
1646 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1647 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1649 new_name = gnutls_realloc(name, len);
1650 if (new_name == NULL) {
1651 ret = GNUTLS_E_MEMORY_ERROR;
1659 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1662 if (ret != GNUTLS_E_SUCCESS)
1665 if (type != GNUTLS_NAME_DNS)
1676 for (i = 0; i < g_context->pki_sni_count; i++) {
1677 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1681 if (i == g_context->pki_sni_count) {
1688 g_context->setup_data.validate_sni_call_back(name,
1689 g_context->setup_data.sni_call_back_arg));
1691 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1692 GNUTLS_A_UNRECOGNIZED_NAME));
1693 g_env->sent_alert = 1;
1694 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1698 g_context->pki_sni_entry_list = gnutls_realloc(
1699 g_context->pki_sni_entry_list,
1700 (i+1)*
sizeof(pki_sni_entry));
1701 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1702 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1703 sni_setup_data = g_context->setup_data;
1704 sni_setup_data.
pki_key = *new_entry;
1705 if ((ret = setup_pki_credentials(&g_context->pki_sni_entry_list[i].pki_credentials,
1710 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1711 GNUTLS_A_BAD_CERTIFICATE));
1712 g_env->sent_alert = 1;
1716 g_context->pki_sni_count++;
1718 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1719 g_context->pki_sni_entry_list[i].pki_credentials),
1720 "gnutls_credentials_set");
1732#if COAP_CLIENT_SUPPORT
1738setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1739 coap_gnutls_context_t *g_context =
1743 g_context->psk_pki_enabled |= IS_CLIENT;
1744 if (g_context->psk_pki_enabled & IS_PSK) {
1746 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1747 "gnutls_psk_allocate_client_credentials");
1748 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1749 psk_client_callback);
1750 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1751 g_env->psk_cl_credentials),
1752 "gnutls_credentials_set");
1755 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1758 "gnutls_server_name_set");
1764 if (tls_version->
version >= 0x030604) {
1766 const char *priority;
1768 if (tls_version->
version >= 0x030606) {
1769 priority = VARIANTS_NO_TLS13_3_6_6;
1771 priority = VARIANTS_NO_TLS13_3_6_4;
1773 ret = gnutls_priority_set_direct(g_env->g_session,
1776 if (ret == GNUTLS_E_INVALID_REQUEST)
1777 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1779 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1786 if ((g_context->psk_pki_enabled & IS_PKI) ||
1787 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1793 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1794 g_context, setup_data,
1796 "setup_pki_credentials");
1798 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1799 g_env->pki_credentials),
1800 "gnutls_credentials_set");
1803 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1804 &g_context->alpn_proto, 1, 0),
1805 "gnutls_alpn_set_protocols");
1809 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1812 "gnutls_server_name_set");
1815 return GNUTLS_E_SUCCESS;
1822#if COAP_SERVER_SUPPORT
1831psk_server_callback(gnutls_session_t g_session,
1832 const char *identity,
1833 gnutls_datum_t *key) {
1836 coap_gnutls_context_t *g_context;
1841 if (c_session == NULL)
1845 if (g_context == NULL)
1851 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
1852 lidentity.
length = strlen((
const char *)lidentity.
s);
1856 (
int)lidentity.
length, (
const char *)lidentity.
s);
1868 if (psk_key == NULL)
1871 key->data = gnutls_malloc(psk_key->
length);
1872 if (key->data == NULL)
1874 memcpy(key->data, psk_key->
s, psk_key->
length);
1875 key->size = psk_key->
length;
1884setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1885 coap_gnutls_context_t *g_context =
1887 int ret = GNUTLS_E_SUCCESS;
1889 g_context->psk_pki_enabled |= IS_SERVER;
1890 if (g_context->psk_pki_enabled & IS_PSK) {
1891 G_CHECK(setup_psk_credentials(
1892 &g_env->psk_sv_credentials,
1895 "setup_psk_credentials\n");
1896 G_CHECK(gnutls_credentials_set(g_env->g_session,
1898 g_env->psk_sv_credentials),
1899 "gnutls_credentials_set\n");
1900 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1901 post_client_hello_gnutls_psk);
1904 if (g_context->psk_pki_enabled & IS_PKI) {
1906 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1907 g_context, setup_data,
1909 "setup_pki_credentials");
1912 gnutls_certificate_server_set_request(g_env->g_session,
1913 GNUTLS_CERT_REQUIRE);
1915 gnutls_certificate_server_set_request(g_env->g_session,
1916 GNUTLS_CERT_REQUEST);
1918 gnutls_certificate_server_set_request(g_env->g_session,
1919 GNUTLS_CERT_IGNORE);
1922 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1923 post_client_hello_gnutls_pki);
1925 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1926 g_env->pki_credentials),
1927 "gnutls_credentials_set\n");
1929 return GNUTLS_E_SUCCESS;
1942coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
1947 if (!c_session->
tls) {
1951 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
1954 if (data != NULL && data->pdu_len > 0) {
1955 if (outl < data->pdu_len) {
1956 memcpy(out, data->pdu, outl);
1958 if (!data->peekmode) {
1960 data->pdu_len -= outl;
1963 memcpy(out, data->pdu, data->pdu_len);
1964 ret = data->pdu_len;
1965 if (!data->peekmode) {
1985coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
1986 size_t send_buffer_length) {
1987 ssize_t result = -1;
2001 send_buffer, send_buffer_length);
2002 if (result != (
int)send_buffer_length) {
2003 int keep_errno = errno;
2005 coap_log_warn(
"coap_netif_dgrm_write failed (%zd != %zu)\n",
2006 result, send_buffer_length);
2026receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
2030 fd_set readfds, writefds, exceptfds;
2032 int nfds = c_session->
sock.
fd +1;
2033 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2037 g_env->coap_ssl_data.pdu_len > 0) {
2043 FD_ZERO(&exceptfds);
2044 FD_SET(c_session->
sock.
fd, &readfds);
2045 if (!(g_env && g_env->doing_dtls_timeout)) {
2046 FD_SET(c_session->
sock.
fd, &writefds);
2047 FD_SET(c_session->
sock.
fd, &exceptfds);
2053 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2058static coap_gnutls_env_t *
2060 coap_gnutls_context_t *g_context =
2062 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2063#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2064 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2066 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2073 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2077 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2079 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2081 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2082 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2083 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2085 gnutls_transport_set_ptr(g_env->g_session, c_session);
2087 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2088 "gnutls_priority_set");
2090 if (type == GNUTLS_SERVER) {
2091#if COAP_SERVER_SUPPORT
2092 G_CHECK(setup_server_ssl_session(c_session, g_env),
2093 "setup_server_ssl_session");
2098#if COAP_CLIENT_SUPPORT
2099 G_CHECK(setup_client_ssl_session(c_session, g_env),
2100 "setup_client_ssl_session");
2106 gnutls_handshake_set_timeout(g_env->g_session,
2107 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2108 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2109 COAP_DTLS_RETRANSMIT_TOTAL_MS);
2120coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2121 coap_gnutls_env_t *g_env,
2122 coap_free_bye_t free_bye) {
2127 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2129 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2130 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2132 gnutls_deinit(g_env->g_session);
2133 g_env->g_session = NULL;
2134 if (g_context->psk_pki_enabled & IS_PSK) {
2135 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2136 g_env->psk_cl_credentials != NULL) {
2137 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2138 g_env->psk_cl_credentials = NULL;
2141 if (g_env->psk_sv_credentials != NULL)
2142 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2143 g_env->psk_sv_credentials = NULL;
2146 if ((g_context->psk_pki_enabled & IS_PKI) ||
2147 (g_context->psk_pki_enabled &
2148 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2149 gnutls_certificate_free_credentials(g_env->pki_credentials);
2150 g_env->pki_credentials = NULL;
2152 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2157#if COAP_SERVER_SUPPORT
2160 coap_gnutls_env_t *g_env =
2161 (coap_gnutls_env_t *)c_session->
tls;
2163 gnutls_transport_set_ptr(g_env->g_session, c_session);
2171 gnutls_session_t g_session) {
2172#if COAP_MAX_LOGGING_LEVEL > 0
2173 int last_alert = gnutls_alert_get(g_session);
2175 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2178 last_alert, gnutls_alert_get_name(last_alert));
2182 last_alert, gnutls_alert_get_name(last_alert));
2195do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2198 ret = gnutls_handshake(g_env->g_session);
2200 case GNUTLS_E_SUCCESS:
2201 g_env->established = 1;
2206 case GNUTLS_E_INTERRUPTED:
2210 case GNUTLS_E_AGAIN:
2214 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2218 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2220 g_env->sent_alert = 1;
2221 log_last_alert(c_session, g_env->g_session);
2223 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2224 case GNUTLS_E_UNEXPECTED_PACKET:
2228 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2229 log_last_alert(c_session, g_env->g_session);
2233 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2234#if (GNUTLS_VERSION_NUMBER > 0x030606)
2235 case GNUTLS_E_CERTIFICATE_REQUIRED:
2237 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2239 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2240 GNUTLS_A_BAD_CERTIFICATE));
2241 g_env->sent_alert = 1;
2245 case GNUTLS_E_DECRYPTION_FAILED:
2248 gnutls_strerror(ret));
2249 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2250 GNUTLS_A_DECRYPT_ERROR));
2251 g_env->sent_alert = 1;
2255 case GNUTLS_E_CERTIFICATE_ERROR:
2256 if (g_env->sent_alert) {
2262 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2263 case GNUTLS_E_NO_CIPHER_SUITES:
2264 case GNUTLS_E_INVALID_SESSION:
2267 gnutls_strerror(ret));
2268 if (!g_env->sent_alert) {
2269 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2270 GNUTLS_A_HANDSHAKE_FAILURE));
2271 g_env->sent_alert = 1;
2276 case GNUTLS_E_SESSION_EOF:
2277 case GNUTLS_E_PREMATURE_TERMINATION:
2278 case GNUTLS_E_TIMEDOUT:
2279 case GNUTLS_E_PULL_ERROR:
2280 case GNUTLS_E_PUSH_ERROR:
2286 "returned %d: '%s'\n",
2287 ret, gnutls_strerror(ret));
2294#if COAP_CLIENT_SUPPORT
2297 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2301 ret = do_gnutls_handshake(c_session, g_env);
2306 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2316 if (c_session && c_session->
context && c_session->
tls) {
2320 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2321 c_session->
tls = NULL;
2328 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2332 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2333 (
unsigned int)c_session->
mtu),
2334 "gnutls_dtls_set_data_mtu");
2346 const uint8_t *data,
size_t data_len) {
2348 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2350 assert(g_env != NULL);
2355 if (g_env->established) {
2356 ret = gnutls_record_send(g_env->g_session, data, data_len);
2360 case GNUTLS_E_AGAIN:
2363 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2365 g_env->sent_alert = 1;
2366 log_last_alert(c_session, g_env->g_session);
2372 "returned %d: '%s'\n",
2373 ret, gnutls_strerror(ret));
2382 ret = do_gnutls_handshake(c_session, g_env);
2414 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2417 if (g_env && g_env->g_session) {
2418 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2431 g_env->last_timeout = now;
2432 return now + rem_ms;
2444 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2447 g_env->doing_dtls_timeout = 1;
2449 (do_gnutls_handshake(c_session, g_env) < 0)) {
2451 g_env->doing_dtls_timeout = 0;
2455 g_env->doing_dtls_timeout = 0;
2468 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2470 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2474 assert(g_env != NULL);
2476 if (ssl_data->pdu_len)
2477 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2479 ssl_data->pdu = data;
2480 ssl_data->pdu_len = data_len;
2483 if (g_env->established) {
2487 gnutls_transport_set_ptr(g_env->g_session, c_session);
2490 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2493 }
else if (ret == 0) {
2497 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2499 g_env->sent_alert = 1;
2500 log_last_alert(c_session, g_env->g_session);
2504 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2505 log_last_alert(c_session, g_env->g_session);
2510 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2516 ret = do_gnutls_handshake(c_session, g_env);
2521 if (ssl_data->pdu_len && !g_env->sent_alert) {
2523 ret = do_gnutls_handshake(c_session, g_env);
2543 if (ssl_data && ssl_data->pdu_len) {
2545 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2546 ssl_data->pdu_len = 0;
2547 ssl_data->pdu = NULL;
2556#if COAP_SERVER_SUPPORT
2564 const uint8_t *data,
2567 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2568 coap_ssl_t *ssl_data;
2572 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2574 c_session->
tls = g_env;
2575 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2576 GNUTLS_COOKIE_KEY_SIZE);
2583 gnutls_dtls_prestate_st prestate;
2586 memset(&prestate, 0,
sizeof(prestate));
2588 memcpy(&data_rw, &data,
sizeof(data_rw));
2589 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2596 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2604 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2607 ssl_data = &g_env->coap_ssl_data;
2608 ssl_data->pdu = data;
2609 ssl_data->pdu_len = data_len;
2611 ret = do_gnutls_handshake(c_session, g_env);
2618 g_env, COAP_FREE_BYE_NONE);
2619 c_session->
tls = NULL;
2627 if (ssl_data && ssl_data->pdu_len) {
2629 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2630 ssl_data->pdu_len = 0;
2631 ssl_data->pdu = NULL;
2642#if !COAP_DISABLE_TCP
2650coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2672coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2681 (errno == EPIPE || errno == ECONNRESET)) {
2707#if COAP_CLIENT_SUPPORT
2710 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2711 coap_gnutls_context_t *g_context =
2713#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2714 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2716 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2723 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2725 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2727 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2728 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2729 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2731 gnutls_transport_set_ptr(g_env->g_session, c_session);
2733 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2734 setup_client_ssl_session(c_session, g_env);
2736 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2738 c_session->
tls = g_env;
2739 ret = do_gnutls_handshake(c_session, g_env);
2753#if COAP_SERVER_SUPPORT
2756 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2757 coap_gnutls_context_t *g_context =
2759#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2760 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2762 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2768 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2770 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2772 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2773 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2774 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2776 gnutls_transport_set_ptr(g_env->g_session, c_session);
2778 setup_server_ssl_session(c_session, g_env);
2780 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2781 gnutls_handshake_set_timeout(g_env->g_session,
2782 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2784 c_session->
tls = g_env;
2785 ret = do_gnutls_handshake(c_session, g_env);
2812 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2814 assert(g_env != NULL);
2817 if (g_env->established) {
2818 ret = gnutls_record_send(g_env->g_session, data, data_len);
2822 case GNUTLS_E_AGAIN:
2825 case GNUTLS_E_PUSH_ERROR:
2826 case GNUTLS_E_PULL_ERROR:
2827 case GNUTLS_E_PREMATURE_TERMINATION:
2830 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2832 g_env->sent_alert = 1;
2833 log_last_alert(c_session, g_env->g_session);
2838 "returned %d: '%s'\n",
2839 ret, gnutls_strerror(ret));
2848 ret = do_gnutls_handshake(c_session, g_env);
2871 if (ret == (ssize_t)data_len)
2888 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2897 if (!g_env->established && !g_env->sent_alert) {
2898 ret = do_gnutls_handshake(c_session, g_env);
2907 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2913 case GNUTLS_E_AGAIN:
2917 case GNUTLS_E_PULL_ERROR:
2920 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2922 g_env->sent_alert = 1;
2923 log_last_alert(c_session, g_env->g_session);
2926 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2927 log_last_alert(c_session, g_env->g_session);
2932 "returned %d: '%s'\n",
2933 ret, gnutls_strerror(ret));
2958#if COAP_SERVER_SUPPORT
2961 gnutls_hash_hd_t digest_ctx;
2963 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
2972 gnutls_hash_deinit(digest_ctx, NULL);
2977 const uint8_t *data,
2979 int ret = gnutls_hash(digest_ctx, data, data_len);
2987 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
2999static struct hash_algs {
3001 gnutls_digest_algorithm_t dig_type;
3009static gnutls_digest_algorithm_t
3010get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3013 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3014 if (hashs[idx].alg == alg) {
3015 *hash_len = hashs[idx].dig_size;
3016 return hashs[idx].dig_type;
3019 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3020 return GNUTLS_DIG_UNKNOWN;
3028 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3029 gnutls_hash_hd_t digest_ctx;
3033 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3034 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3038 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3041 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
3048 gnutls_hash_output(digest_ctx,
dummy->s);
3051 gnutls_hash_deinit(digest_ctx, NULL);
3056 gnutls_hash_deinit(digest_ctx, NULL);
3061#if COAP_OSCORE_SUPPORT
3072static struct cipher_algs {
3074 gnutls_cipher_algorithm_t cipher_type;
3079static gnutls_cipher_algorithm_t
3083 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3084 if (ciphers[idx].alg == alg)
3085 return ciphers[idx].cipher_type;
3087 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3096static struct hmac_algs {
3098 gnutls_mac_algorithm_t hmac_type;
3104static gnutls_mac_algorithm_t
3108 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3109 if (hmacs[idx].hmac_alg == hmac_alg)
3110 return hmacs[idx].hmac_type;
3112 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3118 return get_cipher_alg(alg);
3127 return get_hmac_alg(hmac_alg);
3135 size_t *max_result_len) {
3136 gnutls_aead_cipher_hd_t ctx;
3140 size_t result_len = *max_result_len;
3141 gnutls_cipher_algorithm_t algo;
3143 uint8_t *key_data_rw;
3149 assert(params != NULL);
3153 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3154 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3158 tag_size = gnutls_cipher_get_tag_size(algo);
3162 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3163 key.data = key_data_rw;
3173 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3175 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3185 "gnutls_aead_cipher_encrypt");
3186 *max_result_len = result_len;
3189 gnutls_aead_cipher_deinit(ctx);
3190 return ret == 1 ? 1 : 0;
3198 size_t *max_result_len) {
3199 gnutls_aead_cipher_hd_t ctx;
3203 size_t result_len = *max_result_len;
3204 gnutls_cipher_algorithm_t algo;
3206 uint8_t *key_data_rw;
3212 assert(params != NULL);
3217 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3218 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3222 tag_size = gnutls_cipher_get_tag_size(algo);
3226 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3227 key.data = key_data_rw;
3237 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3239 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3249 "gnutls_aead_cipher_decrypt");
3250 *max_result_len = result_len;
3253 gnutls_aead_cipher_deinit(ctx);
3254 return ret == 1 ? 1 : 0;
3262 gnutls_hmac_hd_t ctx;
3265 gnutls_mac_algorithm_t mac_algo;
3271 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3272 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3275 len = gnutls_hmac_get_len(mac_algo);
3282 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3283 "gnutls_hmac_init");
3284 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3285 gnutls_hmac_output(ctx,
dummy->s);
3291 gnutls_hmac_deinit(ctx, NULL);
3292 return ret == 1 ? 1 : 0;
3303#pragma GCC diagnostic ignored "-Wunused-function"
#define COAP_SERVER_SUPPORT
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.
struct coap_dtls_key_t coap_dtls_key_t
The structure that holds the PKI key information.
#define COAP_DTLS_RPK_CERT_CN
struct coap_dtls_spsk_info_t coap_dtls_spsk_info_t
The structure that holds the Server Pre-Shared Key and Identity Hint information.
struct coap_dtls_pki_t coap_dtls_pki_t
@ 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::@347070033161156116347020364174372227171250157130 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
coap_pki_key_t key_type
key format type
union coap_dtls_key_t::@113076155176127235351156302031377057233373343157 key
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