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);
483 if (c_session && c_session->
tls) {
484 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
486 return g_env->g_session;
511 const char *err =
"Unknown Error";
513 coap_gnutls_context_t *g_context =
514 (coap_gnutls_context_t *)
515 gnutls_malloc(
sizeof(coap_gnutls_context_t));
519 const char *priority;
521 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
522 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
523 g_context->alpn_proto.data = gnutls_malloc(4);
524 if (g_context->alpn_proto.data) {
525 memcpy(g_context->alpn_proto.data,
"coap", 4);
526 g_context->alpn_proto.size = 4;
529 if (tls_version->
version >= 0x030606) {
530 priority = VARIANTS_3_6_6;
531 }
else if (tls_version->
version >= 0x030505) {
532 priority = VARIANTS_3_5_5;
534 priority = VARIANTS_BASE;
536 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
537 if (ret != GNUTLS_E_SUCCESS) {
538 if (ret == GNUTLS_E_INVALID_REQUEST)
539 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
541 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
556 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
558 gnutls_free(g_context->alpn_proto.data);
559 gnutls_free(g_context->root_ca_file);
560 gnutls_free(g_context->root_ca_path);
561 for (i = 0; i < g_context->pki_sni_count; i++) {
562 gnutls_free(g_context->pki_sni_entry_list[i].sni);
563 gnutls_certificate_free_credentials(
564 g_context->pki_sni_entry_list[i].pki_credentials);
566 if (g_context->pki_sni_entry_list)
567 gnutls_free(g_context->pki_sni_entry_list);
569 for (i = 0; i < g_context->psk_sni_count; i++) {
570 gnutls_free(g_context->psk_sni_entry_list[i].sni);
572 gnutls_psk_free_server_credentials(
573 g_context->psk_sni_entry_list[i].psk_credentials);
575 if (g_context->psk_sni_entry_list)
576 gnutls_free(g_context->psk_sni_entry_list);
578 gnutls_priority_deinit(g_context->priority_cache);
580 gnutls_global_deinit();
581 gnutls_free(g_context);
584#if COAP_CLIENT_SUPPORT
593psk_client_callback(gnutls_session_t g_session,
594 char **username, gnutls_datum_t *key) {
597 coap_gnutls_context_t *g_context;
599 const char *hint = gnutls_psk_client_get_hint(g_session);
609 if (c_session == NULL)
613 if (g_context == NULL)
618 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
619 temp.
length = strlen((
const char *)temp.
s);
623 (
const char *)temp.
s);
635 if (cpsk_info == NULL)
640 psk_identity = &cpsk_info->
identity;
641 psk_key = &cpsk_info->
key;
647 if (psk_identity == NULL || psk_key == NULL) {
652 *username = gnutls_malloc(psk_identity->
length+1);
653 if (*username == NULL)
655 memcpy(*username, psk_identity->
s, psk_identity->
length);
656 (*username)[psk_identity->
length] =
'\000';
658 key->data = gnutls_malloc(psk_key->
length);
659 if (key->data == NULL) {
660 gnutls_free(*username);
664 memcpy(key->data, psk_key->
s, psk_key->
length);
665 key->size = psk_key->
length;
671 gnutls_certificate_type_t certificate_type;
673 const gnutls_datum_t *cert_list;
674 unsigned int cert_list_size;
676} coap_gnutls_certificate_info_t;
682static gnutls_certificate_type_t
683get_san_or_cn(gnutls_session_t g_session,
684 coap_gnutls_certificate_info_t *cert_info) {
685 gnutls_x509_crt_t cert;
692#if (GNUTLS_VERSION_NUMBER >= 0x030606)
693 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
696 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
699 cert_info->san_or_cn = NULL;
701 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
702 &cert_info->cert_list_size);
703 if (cert_info->cert_list_size == 0) {
704 return GNUTLS_CRT_UNKNOWN;
707 if (cert_info->certificate_type != GNUTLS_CRT_X509)
708 return cert_info->certificate_type;
710 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
713 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
714 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
716 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
718 size =
sizeof(dn) -1;
720 ret = gnutls_x509_crt_get_subject_alt_name(cert, 0, dn, &size, NULL);
723 gnutls_x509_crt_deinit(cert);
724 cert_info->san_or_cn = gnutls_strdup(dn);
725 return cert_info->certificate_type;
729 G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size),
"gnutls_x509_crt_get_dn");
731 gnutls_x509_crt_deinit(cert);
737 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
738 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
747 char *ecn = strchr(cn,
',');
751 cert_info->san_or_cn = gnutls_strdup(cn);
752 return cert_info->certificate_type;
754 return GNUTLS_CRT_UNKNOWN;
757 return GNUTLS_CRT_UNKNOWN;
760#if (GNUTLS_VERSION_NUMBER >= 0x030606)
761#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
762 cert_info.san_or_cn : \
763 cert_type == GNUTLS_CRT_RAW ? \
764 COAP_DTLS_RPK_CERT_CN : "?")
766#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
767 cert_info.san_or_cn : "?")
770#if (GNUTLS_VERSION_NUMBER >= 0x030606)
772check_rpk_cert(coap_gnutls_context_t *g_context,
773 coap_gnutls_certificate_info_t *cert_info,
777 if (g_context->setup_data.validate_cn_call_back) {
778 gnutls_pcert_st pcert;
782 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
783 GNUTLS_X509_FMT_DER, 0, 0),
784 "gnutls_pcert_import_rawpk_raw");
787 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
788 "gnutls_pubkey_export");
789 gnutls_pcert_deinit(&pcert);
797 g_context->setup_data.cn_call_back_arg));
813cert_verify_gnutls(gnutls_session_t g_session) {
814 unsigned int status = 0;
815 unsigned int fail = 0;
818 coap_gnutls_context_t *g_context =
820 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
821 int alert = GNUTLS_A_BAD_CERTIFICATE;
823 coap_gnutls_certificate_info_t cert_info;
824 gnutls_certificate_type_t cert_type;
826 memset(&cert_info, 0,
sizeof(cert_info));
827 cert_type = get_san_or_cn(g_session, &cert_info);
828#if (GNUTLS_VERSION_NUMBER >= 0x030606)
829 if (cert_type == GNUTLS_CRT_RAW) {
830 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
831 alert = GNUTLS_A_ACCESS_DENIED;
838 if (cert_info.cert_list_size == 0) {
839 if (!g_context->setup_data.verify_peer_cert)
845 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
846 "gnutls_certificate_verify_peers");
849 status &= ~(GNUTLS_CERT_INVALID);
850 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
851 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
852 if (g_context->setup_data.allow_expired_certs) {
855 "The certificate has an invalid usage date",
861 "The certificate has an invalid usage date",
865 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
866 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
867 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
868 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
869 if (g_context->setup_data.allow_expired_crl) {
872 "The certificate's CRL entry has an invalid usage date",
878 "The certificate's CRL entry has an invalid usage date",
882 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
883 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
884 if (cert_info.self_signed) {
885 if (g_context->setup_data.allow_self_signed &&
886 !g_context->setup_data.check_common_ca) {
893 alert = GNUTLS_A_UNKNOWN_CA;
900 if (!g_context->setup_data.verify_peer_cert) {
903 "The peer certificate's CA is unknown",
907 alert = GNUTLS_A_UNKNOWN_CA;
910 "The peer certificate's CA is unknown",
915 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
916 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
920 "The certificate uses an insecure algorithm",
926 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
928 status, OUTPUT_CERT_NAME);
935 if (g_context->setup_data.validate_cn_call_back) {
936 gnutls_x509_crt_t cert;
941 const int cert_is_trusted = !status;
943 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
946 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
947 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
950 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
951 "gnutls_x509_crt_export");
952 gnutls_x509_crt_deinit(cert);
954 g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
960 g_context->setup_data.cn_call_back_arg));
962 alert = GNUTLS_A_ACCESS_DENIED;
967 if (g_context->setup_data.additional_tls_setup_call_back) {
969 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
970 &g_context->setup_data)) {
976 if (cert_info.san_or_cn)
977 gnutls_free(cert_info.san_or_cn);
982 if (cert_info.san_or_cn)
983 gnutls_free(cert_info.san_or_cn);
985 if (!g_env->sent_alert) {
986 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
987 g_env->sent_alert = 1;
1001cert_verify_callback_gnutls(gnutls_session_t g_session) {
1002 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
1003 if (cert_verify_gnutls(g_session) == 0) {
1011#define min(a,b) ((a) < (b) ? (a) : (b))
1015pin_callback(
void *user_data,
int attempt,
1035check_null_memory(gnutls_datum_t *datum,
1036 const uint8_t *buf,
size_t len,
int *alloced) {
1039 if (buf[len-1] !=
'\000') {
1042 datum->data = gnutls_malloc(len + 1);
1045 return GNUTLS_E_MEMORY_ERROR;
1047 memcpy(datum->data, buf, len);
1048 datum->data[len] =
'\000';
1052 memcpy(&datum->data,
1053 &buf,
sizeof(datum->data));
1063setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1064 gnutls_session_t g_session,
1065 coap_gnutls_context_t *g_context,
1069 gnutls_datum_t cert;
1070 gnutls_datum_t pkey;
1072 int alloced_cert_memory = 0;
1073 int alloced_pkey_memory = 0;
1074 int alloced_ca_memory = 0;
1075 int have_done_key = 0;
1082 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1083 "gnutls_certificate_allocate_credentials");
1100#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1104 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1107 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1113 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1120 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1134 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1138 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1141 GNUTLS_X509_FMT_PEM) < 0)) {
1148 if ((ret = check_null_memory(&cert,
1151 &alloced_cert_memory)) < 0) {
1156 if ((ret = check_null_memory(&pkey,
1159 &alloced_pkey_memory)) < 0) {
1160 if (alloced_cert_memory)
1161 gnutls_free(cert.data);
1166 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1169 GNUTLS_X509_FMT_PEM)) < 0) {
1170 if (alloced_cert_memory)
1171 gnutls_free(cert.data);
1172 if (alloced_pkey_memory)
1173 gnutls_free(pkey.data);
1178 if (alloced_cert_memory)
1179 gnutls_free(cert.data);
1180 if (alloced_pkey_memory)
1181 gnutls_free(pkey.data);
1184#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1185 if ((ret = check_null_memory(&cert,
1188 &alloced_cert_memory)) < 0) {
1193 if ((ret = check_null_memory(&pkey,
1196 &alloced_pkey_memory)) < 0) {
1197 if (alloced_cert_memory)
1198 gnutls_free(cert.data);
1203 if (strstr((
char *)pkey.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1204 gnutls_datum_t der_private;
1206 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &pkey,
1207 &der_private) == 0) {
1212 gnutls_datum_t tspki;
1214 tspki.data = spki->
s;
1215 tspki.size = spki->
length;
1216 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1219 GNUTLS_X509_FMT_DER, NULL,
1220 COAP_GNUTLS_KEY_RPK,
1227 gnutls_free(der_private.data);
1230 if (!have_done_key) {
1231 if ((ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1234 GNUTLS_X509_FMT_PEM, NULL,
1235 COAP_GNUTLS_KEY_RPK,
1237 if (alloced_cert_memory)
1238 gnutls_free(cert.data);
1239 if (alloced_pkey_memory)
1240 gnutls_free(pkey.data);
1243 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1246 if (alloced_cert_memory)
1247 gnutls_free(cert.data);
1248 if (alloced_pkey_memory)
1249 gnutls_free(pkey.data);
1252 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1255 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1258 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1261 GNUTLS_X509_FMT_DER) < 0)) {
1268 if ((ret = check_null_memory(&cert,
1271 &alloced_cert_memory)) < 0) {
1276 if ((ret = check_null_memory(&pkey,
1279 &alloced_pkey_memory)) < 0) {
1280 if (alloced_cert_memory)
1281 gnutls_free(cert.data);
1286 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1289 GNUTLS_X509_FMT_DER)) < 0) {
1290 if (alloced_cert_memory)
1291 gnutls_free(cert.data);
1292 if (alloced_pkey_memory)
1293 gnutls_free(pkey.data);
1298 if (alloced_cert_memory)
1299 gnutls_free(cert.data);
1300 if (alloced_pkey_memory)
1301 gnutls_free(pkey.data);
1304 gnutls_pkcs11_set_pin_function(pin_callback, &setup_data->
pki_key);
1305 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1308 GNUTLS_X509_FMT_DER)) < 0) {
1315#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1316 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1317 if ((ret = gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1320 GNUTLS_X509_FMT_PEM, NULL,
1321 COAP_GNUTLS_KEY_RPK,
1322 NULL, 0, GNUTLS_PKCS_PLAIN, 0))) {
1328 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1329 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1336 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1347 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1349 GNUTLS_X509_FMT_PEM) < 0)) {
1356 if ((ret = check_null_memory(&ca,
1359 &alloced_ca_memory)) < 0) {
1364 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1366 GNUTLS_X509_FMT_PEM)) < 0) {
1367 if (alloced_ca_memory)
1368 gnutls_free(ca.data);
1373 if (alloced_ca_memory)
1374 gnutls_free(ca.data);
1380 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1382 GNUTLS_X509_FMT_DER) < 0)) {
1389 if ((ret = check_null_memory(&ca,
1392 &alloced_ca_memory)) < 0) {
1397 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1399 GNUTLS_X509_FMT_DER)) <= 0) {
1400 if (alloced_ca_memory)
1401 gnutls_free(ca.data);
1406 if (alloced_ca_memory)
1407 gnutls_free(ca.data);
1410 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1412 GNUTLS_X509_FMT_DER)) <= 0) {
1425 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1429 if (g_context->root_ca_file) {
1430 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1431 g_context->root_ca_file,
1432 GNUTLS_X509_FMT_PEM);
1434 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1437 if (g_context->root_ca_path) {
1438#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1439 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1440 g_context->root_ca_path,
1441 GNUTLS_X509_FMT_PEM),
1442 "gnutls_certificate_set_x509_trust_dir");
1445 gnutls_certificate_send_x509_rdn_sequence(g_session,
1447 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1449 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1450 "gnutls_certificate_set_x509_system_trust");
1454 gnutls_certificate_set_verify_function(*pki_credentials,
1455 cert_verify_callback_gnutls);
1459 gnutls_certificate_set_verify_limits(*pki_credentials,
1468 gnutls_certificate_set_verify_flags(*pki_credentials,
1470 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1473 return GNUTLS_E_SUCCESS;
1479#if COAP_SERVER_SUPPORT
1485setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1491 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1492 "gnutls_psk_allocate_server_credentials");
1493 gnutls_psk_set_server_credentials_function(*psk_credentials,
1494 psk_server_callback);
1498 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1499 "gnutls_psk_set_server_credentials_hint");
1502 return GNUTLS_E_SUCCESS;
1513post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1516 coap_gnutls_context_t *g_context =
1518 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1519 int ret = GNUTLS_E_SUCCESS;
1529 name = gnutls_malloc(len);
1531 return GNUTLS_E_MEMORY_ERROR;
1534 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1535 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1537 new_name = gnutls_realloc(name, len);
1538 if (new_name == NULL) {
1539 ret = GNUTLS_E_MEMORY_ERROR;
1547 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1550 if (ret != GNUTLS_E_SUCCESS)
1553 if (type != GNUTLS_NAME_DNS)
1564 for (i = 0; i < g_context->psk_sni_count; i++) {
1565 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1569 if (i == g_context->psk_sni_count) {
1580 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1581 GNUTLS_A_UNRECOGNIZED_NAME));
1582 g_env->sent_alert = 1;
1583 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1587 g_context->psk_sni_entry_list =
1588 gnutls_realloc(g_context->psk_sni_entry_list,
1589 (i+1)*
sizeof(psk_sni_entry));
1590 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1591 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1593 sni_setup_data.
psk_info = *new_entry;
1594 if ((ret = setup_psk_credentials(
1595 &g_context->psk_sni_entry_list[i].psk_credentials,
1597 &sni_setup_data)) < 0) {
1599 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1600 GNUTLS_A_BAD_CERTIFICATE));
1601 g_env->sent_alert = 1;
1605 g_context->psk_sni_count++;
1607 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1608 g_context->psk_sni_entry_list[i].psk_credentials),
1609 "gnutls_credentials_set");
1611 &g_context->psk_sni_entry_list[i].psk_info.hint);
1613 &g_context->psk_sni_entry_list[i].psk_info.key);
1629post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1632 coap_gnutls_context_t *g_context =
1634 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1635 int ret = GNUTLS_E_SUCCESS;
1638 if (g_context->setup_data.validate_sni_call_back) {
1645 name = gnutls_malloc(len);
1647 return GNUTLS_E_MEMORY_ERROR;
1650 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1651 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1653 new_name = gnutls_realloc(name, len);
1654 if (new_name == NULL) {
1655 ret = GNUTLS_E_MEMORY_ERROR;
1663 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1666 if (ret != GNUTLS_E_SUCCESS)
1669 if (type != GNUTLS_NAME_DNS)
1680 for (i = 0; i < g_context->pki_sni_count; i++) {
1681 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1685 if (i == g_context->pki_sni_count) {
1692 g_context->setup_data.validate_sni_call_back(name,
1693 g_context->setup_data.sni_call_back_arg));
1695 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1696 GNUTLS_A_UNRECOGNIZED_NAME));
1697 g_env->sent_alert = 1;
1698 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1702 g_context->pki_sni_entry_list = gnutls_realloc(
1703 g_context->pki_sni_entry_list,
1704 (i+1)*
sizeof(pki_sni_entry));
1705 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1706 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1707 sni_setup_data = g_context->setup_data;
1708 sni_setup_data.
pki_key = *new_entry;
1709 if ((ret = setup_pki_credentials(&g_context->pki_sni_entry_list[i].pki_credentials,
1714 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1715 GNUTLS_A_BAD_CERTIFICATE));
1716 g_env->sent_alert = 1;
1720 g_context->pki_sni_count++;
1722 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1723 g_context->pki_sni_entry_list[i].pki_credentials),
1724 "gnutls_credentials_set");
1736#if COAP_CLIENT_SUPPORT
1742setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1743 coap_gnutls_context_t *g_context =
1747 g_context->psk_pki_enabled |= IS_CLIENT;
1748 if (g_context->psk_pki_enabled & IS_PSK) {
1750 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1751 "gnutls_psk_allocate_client_credentials");
1752 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1753 psk_client_callback);
1754 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1755 g_env->psk_cl_credentials),
1756 "gnutls_credentials_set");
1759 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1762 "gnutls_server_name_set");
1768 if (tls_version->
version >= 0x030604) {
1770 const char *priority;
1772 if (tls_version->
version >= 0x030606) {
1773 priority = VARIANTS_NO_TLS13_3_6_6;
1775 priority = VARIANTS_NO_TLS13_3_6_4;
1777 ret = gnutls_priority_set_direct(g_env->g_session,
1780 if (ret == GNUTLS_E_INVALID_REQUEST)
1781 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1783 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1790 if ((g_context->psk_pki_enabled & IS_PKI) ||
1791 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1797 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1798 g_context, setup_data,
1800 "setup_pki_credentials");
1802 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1803 g_env->pki_credentials),
1804 "gnutls_credentials_set");
1807 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1808 &g_context->alpn_proto, 1, 0),
1809 "gnutls_alpn_set_protocols");
1813 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1816 "gnutls_server_name_set");
1819 return GNUTLS_E_SUCCESS;
1826#if COAP_SERVER_SUPPORT
1835psk_server_callback(gnutls_session_t g_session,
1836 const char *identity,
1837 gnutls_datum_t *key) {
1840 coap_gnutls_context_t *g_context;
1845 if (c_session == NULL)
1849 if (g_context == NULL)
1855 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
1856 lidentity.
length = strlen((
const char *)lidentity.
s);
1860 (
int)lidentity.
length, (
const char *)lidentity.
s);
1872 if (psk_key == NULL)
1875 key->data = gnutls_malloc(psk_key->
length);
1876 if (key->data == NULL)
1878 memcpy(key->data, psk_key->
s, psk_key->
length);
1879 key->size = psk_key->
length;
1888setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1889 coap_gnutls_context_t *g_context =
1891 int ret = GNUTLS_E_SUCCESS;
1893 g_context->psk_pki_enabled |= IS_SERVER;
1894 if (g_context->psk_pki_enabled & IS_PSK) {
1895 G_CHECK(setup_psk_credentials(
1896 &g_env->psk_sv_credentials,
1899 "setup_psk_credentials\n");
1900 G_CHECK(gnutls_credentials_set(g_env->g_session,
1902 g_env->psk_sv_credentials),
1903 "gnutls_credentials_set\n");
1904 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1905 post_client_hello_gnutls_psk);
1908 if (g_context->psk_pki_enabled & IS_PKI) {
1910 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1911 g_context, setup_data,
1913 "setup_pki_credentials");
1916 gnutls_certificate_server_set_request(g_env->g_session,
1917 GNUTLS_CERT_REQUIRE);
1919 gnutls_certificate_server_set_request(g_env->g_session,
1920 GNUTLS_CERT_REQUEST);
1922 gnutls_certificate_server_set_request(g_env->g_session,
1923 GNUTLS_CERT_IGNORE);
1926 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1927 post_client_hello_gnutls_pki);
1929 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1930 g_env->pki_credentials),
1931 "gnutls_credentials_set\n");
1933 return GNUTLS_E_SUCCESS;
1946coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
1951 if (!c_session->
tls) {
1955 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
1958 if (data != NULL && data->pdu_len > 0) {
1959 if (outl < data->pdu_len) {
1960 memcpy(out, data->pdu, outl);
1962 if (!data->peekmode) {
1964 data->pdu_len -= outl;
1967 memcpy(out, data->pdu, data->pdu_len);
1968 ret = data->pdu_len;
1969 if (!data->peekmode) {
1989coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
1990 size_t send_buffer_length) {
1991 ssize_t result = -1;
2005 send_buffer, send_buffer_length);
2006 if (result != (
int)send_buffer_length) {
2007 int keep_errno = errno;
2009 coap_log_warn(
"coap_netif_dgrm_write failed (%zd != %zu)\n",
2010 result, send_buffer_length);
2030receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
2034 fd_set readfds, writefds, exceptfds;
2036 int nfds = c_session->
sock.
fd +1;
2037 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2041 g_env->coap_ssl_data.pdu_len > 0) {
2047 FD_ZERO(&exceptfds);
2048 FD_SET(c_session->
sock.
fd, &readfds);
2049 if (!(g_env && g_env->doing_dtls_timeout)) {
2050 FD_SET(c_session->
sock.
fd, &writefds);
2051 FD_SET(c_session->
sock.
fd, &exceptfds);
2057 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2062static coap_gnutls_env_t *
2064 coap_gnutls_context_t *g_context =
2066 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2067#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2068 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2070 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2077 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2081 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2083 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2085 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2086 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2087 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2089 gnutls_transport_set_ptr(g_env->g_session, c_session);
2091 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2092 "gnutls_priority_set");
2094 if (type == GNUTLS_SERVER) {
2095#if COAP_SERVER_SUPPORT
2096 G_CHECK(setup_server_ssl_session(c_session, g_env),
2097 "setup_server_ssl_session");
2102#if COAP_CLIENT_SUPPORT
2103 G_CHECK(setup_client_ssl_session(c_session, g_env),
2104 "setup_client_ssl_session");
2110 gnutls_handshake_set_timeout(g_env->g_session,
2111 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2112 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2113 COAP_DTLS_RETRANSMIT_TOTAL_MS);
2124coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2125 coap_gnutls_env_t *g_env,
2126 coap_free_bye_t free_bye) {
2131 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2133 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2134 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2136 gnutls_deinit(g_env->g_session);
2137 g_env->g_session = NULL;
2138 if (g_context->psk_pki_enabled & IS_PSK) {
2139 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2140 g_env->psk_cl_credentials != NULL) {
2141 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2142 g_env->psk_cl_credentials = NULL;
2145 if (g_env->psk_sv_credentials != NULL)
2146 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2147 g_env->psk_sv_credentials = NULL;
2150 if ((g_context->psk_pki_enabled & IS_PKI) ||
2151 (g_context->psk_pki_enabled &
2152 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2153 gnutls_certificate_free_credentials(g_env->pki_credentials);
2154 g_env->pki_credentials = NULL;
2156 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2161#if COAP_SERVER_SUPPORT
2164 coap_gnutls_env_t *g_env =
2165 (coap_gnutls_env_t *)c_session->
tls;
2167 gnutls_transport_set_ptr(g_env->g_session, c_session);
2175 gnutls_session_t g_session) {
2176#if COAP_MAX_LOGGING_LEVEL > 0
2177 int last_alert = gnutls_alert_get(g_session);
2179 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2182 last_alert, gnutls_alert_get_name(last_alert));
2186 last_alert, gnutls_alert_get_name(last_alert));
2199do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2202 ret = gnutls_handshake(g_env->g_session);
2204 case GNUTLS_E_SUCCESS:
2205 g_env->established = 1;
2210 case GNUTLS_E_INTERRUPTED:
2214 case GNUTLS_E_AGAIN:
2218 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2222 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2224 g_env->sent_alert = 1;
2225 log_last_alert(c_session, g_env->g_session);
2227 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2228 case GNUTLS_E_UNEXPECTED_PACKET:
2232 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2233 log_last_alert(c_session, g_env->g_session);
2237 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2238#if (GNUTLS_VERSION_NUMBER > 0x030606)
2239 case GNUTLS_E_CERTIFICATE_REQUIRED:
2241 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2243 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2244 GNUTLS_A_BAD_CERTIFICATE));
2245 g_env->sent_alert = 1;
2249 case GNUTLS_E_DECRYPTION_FAILED:
2252 gnutls_strerror(ret));
2253 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2254 GNUTLS_A_DECRYPT_ERROR));
2255 g_env->sent_alert = 1;
2259 case GNUTLS_E_CERTIFICATE_ERROR:
2260 if (g_env->sent_alert) {
2266 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2267 case GNUTLS_E_NO_CIPHER_SUITES:
2268 case GNUTLS_E_INVALID_SESSION:
2271 gnutls_strerror(ret));
2272 if (!g_env->sent_alert) {
2273 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2274 GNUTLS_A_HANDSHAKE_FAILURE));
2275 g_env->sent_alert = 1;
2280 case GNUTLS_E_SESSION_EOF:
2281 case GNUTLS_E_PREMATURE_TERMINATION:
2282 case GNUTLS_E_TIMEDOUT:
2283 case GNUTLS_E_PULL_ERROR:
2284 case GNUTLS_E_PUSH_ERROR:
2290 "returned %d: '%s'\n",
2291 ret, gnutls_strerror(ret));
2298#if COAP_CLIENT_SUPPORT
2301 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2305 ret = do_gnutls_handshake(c_session, g_env);
2310 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2320 if (c_session && c_session->
context && c_session->
tls) {
2324 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2325 c_session->
tls = NULL;
2332 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2336 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2337 (
unsigned int)c_session->
mtu),
2338 "gnutls_dtls_set_data_mtu");
2350 const uint8_t *data,
size_t data_len) {
2352 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2354 assert(g_env != NULL);
2359 if (g_env->established) {
2360 ret = gnutls_record_send(g_env->g_session, data, data_len);
2364 case GNUTLS_E_AGAIN:
2367 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2369 g_env->sent_alert = 1;
2370 log_last_alert(c_session, g_env->g_session);
2376 "returned %d: '%s'\n",
2377 ret, gnutls_strerror(ret));
2386 ret = do_gnutls_handshake(c_session, g_env);
2417 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2420 if (g_env && g_env->g_session) {
2421 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2434 g_env->last_timeout = now;
2435 return now + rem_ms;
2447 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2450 g_env->doing_dtls_timeout = 1;
2452 (do_gnutls_handshake(c_session, g_env) < 0)) {
2454 g_env->doing_dtls_timeout = 0;
2458 g_env->doing_dtls_timeout = 0;
2471 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2473 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2477 assert(g_env != NULL);
2479 if (ssl_data->pdu_len)
2480 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2482 ssl_data->pdu = data;
2483 ssl_data->pdu_len = data_len;
2486 if (g_env->established) {
2490 gnutls_transport_set_ptr(g_env->g_session, c_session);
2493 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2496 }
else if (ret == 0) {
2500 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2502 g_env->sent_alert = 1;
2503 log_last_alert(c_session, g_env->g_session);
2507 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2508 log_last_alert(c_session, g_env->g_session);
2513 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2519 ret = do_gnutls_handshake(c_session, g_env);
2524 if (ssl_data->pdu_len && !g_env->sent_alert) {
2526 ret = do_gnutls_handshake(c_session, g_env);
2544 if (ssl_data && ssl_data->pdu_len) {
2546 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2547 ssl_data->pdu_len = 0;
2548 ssl_data->pdu = NULL;
2557#if COAP_SERVER_SUPPORT
2565 const uint8_t *data,
2568 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2569 coap_ssl_t *ssl_data;
2573 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2575 c_session->
tls = g_env;
2576 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2577 GNUTLS_COOKIE_KEY_SIZE);
2584 gnutls_dtls_prestate_st prestate;
2587 memset(&prestate, 0,
sizeof(prestate));
2589 memcpy(&data_rw, &data,
sizeof(data_rw));
2590 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2597 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2605 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2608 ssl_data = &g_env->coap_ssl_data;
2609 ssl_data->pdu = data;
2610 ssl_data->pdu_len = data_len;
2612 ret = do_gnutls_handshake(c_session, g_env);
2619 g_env, COAP_FREE_BYE_NONE);
2620 c_session->
tls = NULL;
2628 if (ssl_data && ssl_data->pdu_len) {
2630 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2631 ssl_data->pdu_len = 0;
2632 ssl_data->pdu = NULL;
2643#if !COAP_DISABLE_TCP
2651coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2673coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2682 (errno == EPIPE || errno == ECONNRESET)) {
2708#if COAP_CLIENT_SUPPORT
2711 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2712 coap_gnutls_context_t *g_context =
2714#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2715 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2717 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2724 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2726 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2728 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2729 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2730 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2732 gnutls_transport_set_ptr(g_env->g_session, c_session);
2734 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2735 setup_client_ssl_session(c_session, g_env);
2737 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2739 c_session->
tls = g_env;
2740 ret = do_gnutls_handshake(c_session, g_env);
2754#if COAP_SERVER_SUPPORT
2757 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2758 coap_gnutls_context_t *g_context =
2760#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2761 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2763 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2769 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2771 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2773 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2774 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2775 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2777 gnutls_transport_set_ptr(g_env->g_session, c_session);
2779 setup_server_ssl_session(c_session, g_env);
2781 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2782 gnutls_handshake_set_timeout(g_env->g_session,
2783 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2785 c_session->
tls = g_env;
2786 ret = do_gnutls_handshake(c_session, g_env);
2813 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2815 assert(g_env != NULL);
2818 if (g_env->established) {
2819 ret = gnutls_record_send(g_env->g_session, data, data_len);
2823 case GNUTLS_E_AGAIN:
2826 case GNUTLS_E_PUSH_ERROR:
2827 case GNUTLS_E_PULL_ERROR:
2828 case GNUTLS_E_PREMATURE_TERMINATION:
2831 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2833 g_env->sent_alert = 1;
2834 log_last_alert(c_session, g_env->g_session);
2839 "returned %d: '%s'\n",
2840 ret, gnutls_strerror(ret));
2849 ret = do_gnutls_handshake(c_session, g_env);
2869 if (ret == (ssize_t)data_len)
2886 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2895 if (!g_env->established && !g_env->sent_alert) {
2896 ret = do_gnutls_handshake(c_session, g_env);
2905 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2911 case GNUTLS_E_AGAIN:
2915 case GNUTLS_E_PULL_ERROR:
2918 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2920 g_env->sent_alert = 1;
2921 log_last_alert(c_session, g_env->g_session);
2924 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2925 log_last_alert(c_session, g_env->g_session);
2930 "returned %d: '%s'\n",
2931 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)) {
2968 gnutls_hash_deinit(digest_ctx, NULL);
2973 const uint8_t *data,
2975 int ret = gnutls_hash(digest_ctx, data, data_len);
2983 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
2995static struct hash_algs {
2997 gnutls_digest_algorithm_t dig_type;
3005static gnutls_digest_algorithm_t
3006get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3009 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3010 if (hashs[idx].alg == alg) {
3011 *hash_len = hashs[idx].dig_size;
3012 return hashs[idx].dig_type;
3015 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3016 return GNUTLS_DIG_UNKNOWN;
3024 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3025 gnutls_hash_hd_t digest_ctx;
3029 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3030 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3034 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3037 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
3044 gnutls_hash_output(digest_ctx,
dummy->s);
3047 gnutls_hash_deinit(digest_ctx, NULL);
3052 gnutls_hash_deinit(digest_ctx, NULL);
3057#if COAP_OSCORE_SUPPORT
3068static struct cipher_algs {
3070 gnutls_cipher_algorithm_t cipher_type;
3075static gnutls_cipher_algorithm_t
3079 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3080 if (ciphers[idx].alg == alg)
3081 return ciphers[idx].cipher_type;
3083 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3092static struct hmac_algs {
3094 gnutls_mac_algorithm_t hmac_type;
3100static gnutls_mac_algorithm_t
3104 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3105 if (hmacs[idx].hmac_alg == hmac_alg)
3106 return hmacs[idx].hmac_type;
3108 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3114 return get_cipher_alg(alg);
3123 return get_hmac_alg(hmac_alg);
3131 size_t *max_result_len) {
3132 gnutls_aead_cipher_hd_t ctx;
3136 size_t result_len = *max_result_len;
3137 gnutls_cipher_algorithm_t algo;
3139 uint8_t *key_data_rw;
3145 assert(params != NULL);
3149 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3150 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3154 tag_size = gnutls_cipher_get_tag_size(algo);
3158 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3159 key.data = key_data_rw;
3169 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3171 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3181 "gnutls_aead_cipher_encrypt");
3182 *max_result_len = result_len;
3185 gnutls_aead_cipher_deinit(ctx);
3186 return ret == 1 ? 1 : 0;
3194 size_t *max_result_len) {
3195 gnutls_aead_cipher_hd_t ctx;
3199 size_t result_len = *max_result_len;
3200 gnutls_cipher_algorithm_t algo;
3202 uint8_t *key_data_rw;
3208 assert(params != NULL);
3213 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3214 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3218 tag_size = gnutls_cipher_get_tag_size(algo);
3222 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3223 key.data = key_data_rw;
3233 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3235 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3245 "gnutls_aead_cipher_decrypt");
3246 *max_result_len = result_len;
3249 gnutls_aead_cipher_deinit(ctx);
3250 return ret == 1 ? 1 : 0;
3258 gnutls_hmac_hd_t ctx;
3261 gnutls_mac_algorithm_t mac_algo;
3267 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3268 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3271 len = gnutls_hmac_get_len(mac_algo);
3278 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3279 "gnutls_hmac_init");
3280 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3281 gnutls_hmac_output(ctx,
dummy->s);
3287 gnutls_hmac_deinit(ctx, NULL);
3288 return ret == 1 ? 1 : 0;
3299#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_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
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::@242136240037311335022001367112102231100333222137 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.
union coap_dtls_key_t::@067104111220140054257241314117327310146344167156 key
coap_pki_key_define_t define
for definable type keys
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