53#if 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 int trust_store_defined;
135 gnutls_priority_t priority_cache;
136} coap_gnutls_context_t;
138typedef enum coap_free_bye_t {
139 COAP_FREE_BYE_AS_TCP,
140 COAP_FREE_BYE_AS_UDP,
144#define VARIANTS_3_6_6 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL:+SHA256"
145#define VARIANTS_3_5_5 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8"
146#define VARIANTS_BASE "NORMAL:+ECDHE-PSK:+PSK"
148#define VARIANTS_NO_TLS13_3_6_6 VARIANTS_3_6_6 ":-VERS-TLS1.3"
149#define VARIANTS_NO_TLS13_3_6_4 VARIANTS_3_5_5 ":-VERS-TLS1.3"
151#define G_ACTION(xx) do { \
153 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
155#define G_CHECK(xx,func) do { \
156 if ((ret = (xx)) < 0) { \
157 coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \
162#define G_ACTION_CHECK(xx,func) do { \
169#if COAP_SERVER_SUPPORT
170static int post_client_hello_gnutls_pki(gnutls_session_t g_session);
171static int post_client_hello_gnutls_psk(gnutls_session_t g_session);
172static int psk_server_callback(gnutls_session_t g_session,
173 const char *identity,
174 gnutls_datum_t *key);
183 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
184 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
197 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
198 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
240#if (GNUTLS_VERSION_NUMBER >= 0x030606)
256#if COAP_CLIENT_SUPPORT
268 const char *vers = gnutls_check_version(NULL);
274 sscanf(vers,
"%d.%d.%d", &p1, &p2, &p3);
275 version.
version = (p1 << 16) | (p2 << 8) | p3;
283coap_gnutls_audit_log_func(gnutls_session_t g_session,
const char *text) {
284#if COAP_MAX_LOGGING_LEVEL > 0
300coap_gnutls_log_func(
int level,
const char *text) {
318 coap_gnutls_context_t *g_context =
321 if (!g_context || !setup_data)
324 g_context->setup_data = *setup_data;
325 if (!g_context->setup_data.verify_peer_cert) {
327 g_context->setup_data.check_common_ca = 0;
328 if (g_context->setup_data.is_rpk_not_cert) {
330 g_context->setup_data.allow_self_signed = 0;
331 g_context->setup_data.allow_expired_certs = 0;
332 g_context->setup_data.cert_chain_validation = 0;
333 g_context->setup_data.cert_chain_verify_depth = 0;
334 g_context->setup_data.check_cert_revocation = 0;
335 g_context->setup_data.allow_no_crl = 0;
336 g_context->setup_data.allow_expired_crl = 0;
337 g_context->setup_data.allow_bad_md_hash = 0;
338 g_context->setup_data.allow_short_rsa_length = 0;
341 g_context->setup_data.allow_self_signed = 1;
342 g_context->setup_data.allow_expired_certs = 1;
343 g_context->setup_data.cert_chain_validation = 1;
344 g_context->setup_data.cert_chain_verify_depth = 10;
345 g_context->setup_data.check_cert_revocation = 1;
346 g_context->setup_data.allow_no_crl = 1;
347 g_context->setup_data.allow_expired_crl = 1;
348 g_context->setup_data.allow_bad_md_hash = 1;
349 g_context->setup_data.allow_short_rsa_length = 1;
354 g_context->setup_data.pki_key = key;
355 g_context->psk_pki_enabled |= IS_PKI;
369 const char *ca_path) {
370 coap_gnutls_context_t *g_context =
373 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
378 if (ca_file == NULL && ca_path == NULL) {
379 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
383 if (g_context->root_ca_file) {
384 gnutls_free(g_context->root_ca_file);
385 g_context->root_ca_file = NULL;
388 g_context->root_ca_file = gnutls_strdup(ca_file);
390 if (g_context->root_ca_path) {
391 gnutls_free(g_context->root_ca_path);
392 g_context->root_ca_path = NULL;
395#if (GNUTLS_VERSION_NUMBER >= 0x030306)
396 g_context->root_ca_path = gnutls_strdup(ca_path);
398 coap_log_err(
"ca_path not supported in GnuTLS < 3.3.6\n");
410 coap_gnutls_context_t *g_context =
413 coap_log_warn(
"coap_context_set_pki_trust_store: (D)TLS environment "
418#if (GNUTLS_VERSION_NUMBER >= 0x030020)
419 g_context->trust_store_defined = 1;
422 coap_log_warn(
"coap_context_set_pki_trust_store(): (D)TLS environment "
423 "not supported for GnuTLS < v3.0.20\n");
428#if COAP_SERVER_SUPPORT
437 coap_gnutls_context_t *g_context =
440 if (!g_context || !setup_data)
446 g_context->psk_pki_enabled |= IS_PSK;
451#if COAP_CLIENT_SUPPORT
460 coap_gnutls_context_t *g_context =
463 if (!g_context || !setup_data)
472 g_context->psk_pki_enabled |= IS_PSK;
483 coap_gnutls_context_t *g_context =
485 return g_context->psk_pki_enabled ? 1 : 0;
490 gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func);
491 gnutls_global_set_log_function(coap_gnutls_log_func);
504 if (c_session && c_session->
tls) {
505 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
507 return g_env->g_session;
532 const char *err =
"Unknown Error";
534 coap_gnutls_context_t *g_context =
535 (coap_gnutls_context_t *)
536 gnutls_malloc(
sizeof(coap_gnutls_context_t));
540 const char *priority;
542 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
543 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
544 g_context->alpn_proto.data = gnutls_malloc(4);
545 if (g_context->alpn_proto.data) {
546 memcpy(g_context->alpn_proto.data,
"coap", 4);
547 g_context->alpn_proto.size = 4;
550 if (tls_version->
version >= 0x030606) {
551 priority = VARIANTS_3_6_6;
552 }
else if (tls_version->
version >= 0x030505) {
553 priority = VARIANTS_3_5_5;
555 priority = VARIANTS_BASE;
557 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
558 if (ret != GNUTLS_E_SUCCESS) {
559 if (ret == GNUTLS_E_INVALID_REQUEST)
560 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
562 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
577 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
579 gnutls_free(g_context->alpn_proto.data);
580 gnutls_free(g_context->root_ca_file);
581 gnutls_free(g_context->root_ca_path);
582 for (i = 0; i < g_context->pki_sni_count; i++) {
583 gnutls_free(g_context->pki_sni_entry_list[i].sni);
584 gnutls_certificate_free_credentials(
585 g_context->pki_sni_entry_list[i].pki_credentials);
587 if (g_context->pki_sni_entry_list)
588 gnutls_free(g_context->pki_sni_entry_list);
590 for (i = 0; i < g_context->psk_sni_count; i++) {
591 gnutls_free(g_context->psk_sni_entry_list[i].sni);
593 gnutls_psk_free_server_credentials(
594 g_context->psk_sni_entry_list[i].psk_credentials);
596 if (g_context->psk_sni_entry_list)
597 gnutls_free(g_context->psk_sni_entry_list);
599 gnutls_priority_deinit(g_context->priority_cache);
601 gnutls_global_deinit();
602 gnutls_free(g_context);
605#if COAP_CLIENT_SUPPORT
614psk_client_callback(gnutls_session_t g_session,
615 char **username, gnutls_datum_t *key) {
618 coap_gnutls_context_t *g_context;
620 const char *hint = gnutls_psk_client_get_hint(g_session);
630 if (c_session == NULL)
634 if (g_context == NULL)
639 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
640 temp.
length = strlen((
const char *)temp.
s);
644 (
const char *)temp.
s);
656 if (cpsk_info == NULL)
661 psk_identity = &cpsk_info->
identity;
662 psk_key = &cpsk_info->
key;
668 if (psk_identity == NULL || psk_key == NULL) {
673 *username = gnutls_malloc(psk_identity->
length+1);
674 if (*username == NULL)
676 memcpy(*username, psk_identity->
s, psk_identity->
length);
677 (*username)[psk_identity->
length] =
'\000';
679 key->data = gnutls_malloc(psk_key->
length);
680 if (key->data == NULL) {
681 gnutls_free(*username);
685 memcpy(key->data, psk_key->
s, psk_key->
length);
686 key->size = psk_key->
length;
692 gnutls_certificate_type_t certificate_type;
694 const gnutls_datum_t *cert_list;
695 unsigned int cert_list_size;
697} coap_gnutls_certificate_info_t;
703static gnutls_certificate_type_t
704get_san_or_cn(gnutls_session_t g_session,
705 coap_gnutls_certificate_info_t *cert_info) {
706 gnutls_x509_crt_t cert;
713#if (GNUTLS_VERSION_NUMBER >= 0x030606)
714 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
717 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
720 cert_info->san_or_cn = NULL;
722 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
723 &cert_info->cert_list_size);
724 if (cert_info->cert_list_size == 0) {
725 return GNUTLS_CRT_UNKNOWN;
728 if (cert_info->certificate_type != GNUTLS_CRT_X509)
729 return cert_info->certificate_type;
731 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
734 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
735 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
737 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
739 size =
sizeof(dn) -1;
741 ret = gnutls_x509_crt_get_subject_alt_name(cert, 0, dn, &size, NULL);
744 gnutls_x509_crt_deinit(cert);
745 cert_info->san_or_cn = gnutls_strdup(dn);
746 return cert_info->certificate_type;
750 G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size),
"gnutls_x509_crt_get_dn");
752 gnutls_x509_crt_deinit(cert);
758 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
759 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
768 char *ecn = strchr(cn,
',');
772 cert_info->san_or_cn = gnutls_strdup(cn);
773 return cert_info->certificate_type;
775 return GNUTLS_CRT_UNKNOWN;
778 return GNUTLS_CRT_UNKNOWN;
781#if (GNUTLS_VERSION_NUMBER >= 0x030606)
782#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
783 cert_info.san_or_cn : \
784 cert_type == GNUTLS_CRT_RAW ? \
785 COAP_DTLS_RPK_CERT_CN : "?")
787#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
788 cert_info.san_or_cn : "?")
791#if (GNUTLS_VERSION_NUMBER >= 0x030606)
793check_rpk_cert(coap_gnutls_context_t *g_context,
794 coap_gnutls_certificate_info_t *cert_info,
798 if (g_context->setup_data.validate_cn_call_back) {
799 gnutls_pcert_st pcert;
803 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
804 GNUTLS_X509_FMT_DER, 0, 0),
805 "gnutls_pcert_import_rawpk_raw");
808 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
809 "gnutls_pubkey_export");
810 gnutls_pcert_deinit(&pcert);
818 g_context->setup_data.cn_call_back_arg));
834cert_verify_gnutls(gnutls_session_t g_session) {
835 unsigned int status = 0;
836 unsigned int fail = 0;
839 coap_gnutls_context_t *g_context =
841 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
842 int alert = GNUTLS_A_BAD_CERTIFICATE;
844 coap_gnutls_certificate_info_t cert_info;
845 gnutls_certificate_type_t cert_type;
847 memset(&cert_info, 0,
sizeof(cert_info));
848 cert_type = get_san_or_cn(g_session, &cert_info);
849#if (GNUTLS_VERSION_NUMBER >= 0x030606)
850 if (cert_type == GNUTLS_CRT_RAW) {
851 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
852 alert = GNUTLS_A_ACCESS_DENIED;
859 if (cert_info.cert_list_size == 0) {
860 if (!g_context->setup_data.verify_peer_cert)
866 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
867 "gnutls_certificate_verify_peers");
870 status, cert_info.san_or_cn);
872 status &= ~(GNUTLS_CERT_INVALID);
873 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
874 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
875 if (g_context->setup_data.allow_expired_certs) {
878 "The certificate has an invalid usage date",
884 "The certificate has an invalid usage date",
888 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
889 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
890 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
891 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
892 if (g_context->setup_data.allow_expired_crl) {
895 "The certificate's CRL entry has an invalid usage date",
901 "The certificate's CRL entry has an invalid usage date",
905 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
906 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
907 if (cert_info.self_signed) {
908 if (g_context->setup_data.allow_self_signed &&
909 !g_context->setup_data.check_common_ca) {
916 alert = GNUTLS_A_UNKNOWN_CA;
923 if (!g_context->setup_data.verify_peer_cert) {
926 "The peer certificate's CA is unknown",
930 alert = GNUTLS_A_UNKNOWN_CA;
933 "The peer certificate's CA is unknown",
938 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
939 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
943 "The certificate uses an insecure algorithm",
949 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
951 status, OUTPUT_CERT_NAME);
958 if (g_context->setup_data.validate_cn_call_back) {
959 gnutls_x509_crt_t cert;
964 const int cert_is_trusted = !status;
966 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
969 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
970 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
973 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
974 "gnutls_x509_crt_export");
975 gnutls_x509_crt_deinit(cert);
977 g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
983 g_context->setup_data.cn_call_back_arg));
985 alert = GNUTLS_A_ACCESS_DENIED;
990 if (g_context->setup_data.additional_tls_setup_call_back) {
992 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
993 &g_context->setup_data)) {
999 if (cert_info.san_or_cn)
1000 gnutls_free(cert_info.san_or_cn);
1005 if (cert_info.san_or_cn)
1006 gnutls_free(cert_info.san_or_cn);
1008 if (!g_env->sent_alert) {
1009 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
1010 g_env->sent_alert = 1;
1024cert_verify_callback_gnutls(gnutls_session_t g_session) {
1025 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
1026 if (cert_verify_gnutls(g_session) == 0) {
1034#define min(a,b) ((a) < (b) ? (a) : (b))
1038pin_callback(
void *user_data,
int attempt,
1058check_null_memory(gnutls_datum_t *datum,
1059 const uint8_t *buf,
size_t len,
int *alloced) {
1062 if (buf[len-1] !=
'\000') {
1065 datum->data = gnutls_malloc(len + 1);
1068 return GNUTLS_E_MEMORY_ERROR;
1070 memcpy(datum->data, buf, len);
1071 datum->data[len] =
'\000';
1075 memcpy(&datum->data,
1076 &buf,
sizeof(datum->data));
1086setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1087 gnutls_session_t g_session,
1088 coap_gnutls_context_t *g_context,
1092 gnutls_datum_t cert;
1093 gnutls_datum_t pkey;
1095 int alloced_cert_memory = 0;
1096 int alloced_pkey_memory = 0;
1097 int alloced_ca_memory = 0;
1098 int have_done_key = 0;
1105 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1106 "gnutls_certificate_allocate_credentials");
1123#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1127 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1130 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1136 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1143 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1157 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1161 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1164 GNUTLS_X509_FMT_PEM)) < 0) {
1171 if ((ret = check_null_memory(&cert,
1174 &alloced_cert_memory)) < 0) {
1179 if ((ret = check_null_memory(&pkey,
1182 &alloced_pkey_memory)) < 0) {
1183 if (alloced_cert_memory)
1184 gnutls_free(cert.data);
1189 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1192 GNUTLS_X509_FMT_PEM)) < 0) {
1193 if (alloced_cert_memory)
1194 gnutls_free(cert.data);
1195 if (alloced_pkey_memory)
1196 gnutls_free(pkey.data);
1201 if (alloced_cert_memory)
1202 gnutls_free(cert.data);
1203 if (alloced_pkey_memory)
1204 gnutls_free(pkey.data);
1207#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1208 if ((ret = check_null_memory(&cert,
1211 &alloced_cert_memory)) < 0) {
1216 if ((ret = check_null_memory(&pkey,
1219 &alloced_pkey_memory)) < 0) {
1220 if (alloced_cert_memory)
1221 gnutls_free(cert.data);
1226 if (strstr((
char *)pkey.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1227 gnutls_datum_t der_private;
1229 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &pkey,
1230 &der_private) == 0) {
1235 gnutls_datum_t tspki;
1237 tspki.data = spki->
s;
1238 tspki.size = spki->
length;
1239 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1242 GNUTLS_X509_FMT_DER, NULL,
1243 COAP_GNUTLS_KEY_RPK,
1250 gnutls_free(der_private.data);
1253 if (!have_done_key) {
1254 if ((ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1257 GNUTLS_X509_FMT_PEM, NULL,
1258 COAP_GNUTLS_KEY_RPK,
1260 if (alloced_cert_memory)
1261 gnutls_free(cert.data);
1262 if (alloced_pkey_memory)
1263 gnutls_free(pkey.data);
1266 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1269 if (alloced_cert_memory)
1270 gnutls_free(cert.data);
1271 if (alloced_pkey_memory)
1272 gnutls_free(pkey.data);
1275 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1278 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1281 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1284 GNUTLS_X509_FMT_DER) < 0)) {
1291 if ((ret = check_null_memory(&cert,
1294 &alloced_cert_memory)) < 0) {
1299 if ((ret = check_null_memory(&pkey,
1302 &alloced_pkey_memory)) < 0) {
1303 if (alloced_cert_memory)
1304 gnutls_free(cert.data);
1309 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1312 GNUTLS_X509_FMT_DER)) < 0) {
1313 if (alloced_cert_memory)
1314 gnutls_free(cert.data);
1315 if (alloced_pkey_memory)
1316 gnutls_free(pkey.data);
1321 if (alloced_cert_memory)
1322 gnutls_free(cert.data);
1323 if (alloced_pkey_memory)
1324 gnutls_free(pkey.data);
1327 gnutls_pkcs11_set_pin_function(pin_callback, &setup_data->
pki_key);
1328 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1331 GNUTLS_X509_FMT_DER)) < 0) {
1338#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1339 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1340 if ((ret = gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1343 GNUTLS_X509_FMT_PEM, NULL,
1344 COAP_GNUTLS_KEY_RPK,
1345 NULL, 0, GNUTLS_PKCS_PLAIN, 0))) {
1351 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1352 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1359 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1370 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1372 GNUTLS_X509_FMT_PEM) < 0)) {
1379 if ((ret = check_null_memory(&ca,
1382 &alloced_ca_memory)) < 0) {
1387 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1389 GNUTLS_X509_FMT_PEM)) < 0) {
1390 if (alloced_ca_memory)
1391 gnutls_free(ca.data);
1396 if (alloced_ca_memory)
1397 gnutls_free(ca.data);
1403 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1405 GNUTLS_X509_FMT_DER) < 0)) {
1412 if ((ret = check_null_memory(&ca,
1415 &alloced_ca_memory)) < 0) {
1420 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1422 GNUTLS_X509_FMT_DER)) <= 0) {
1423 if (alloced_ca_memory)
1424 gnutls_free(ca.data);
1429 if (alloced_ca_memory)
1430 gnutls_free(ca.data);
1433 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1435 GNUTLS_X509_FMT_DER)) <= 0) {
1448 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1452#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1453 if (g_context->trust_store_defined) {
1454 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1455 "gnutls_certificate_set_x509_system_trust");
1458 if (g_context->root_ca_file) {
1459 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1460 g_context->root_ca_file,
1461 GNUTLS_X509_FMT_PEM);
1463 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1466 if (g_context->root_ca_path) {
1467#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1468 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1469 g_context->root_ca_path,
1470 GNUTLS_X509_FMT_PEM),
1471 "gnutls_certificate_set_x509_trust_dir");
1474 gnutls_certificate_send_x509_rdn_sequence(g_session,
1476#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1477 if (!(g_context->psk_pki_enabled & IS_PKI) && !g_context->trust_store_defined) {
1479 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1480 "gnutls_certificate_set_x509_system_trust");
1485 gnutls_certificate_set_verify_function(*pki_credentials,
1486 cert_verify_callback_gnutls);
1490 gnutls_certificate_set_verify_limits(*pki_credentials,
1499 gnutls_certificate_set_verify_flags(*pki_credentials,
1501 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1504 return GNUTLS_E_SUCCESS;
1510#if COAP_SERVER_SUPPORT
1516setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1522 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1523 "gnutls_psk_allocate_server_credentials");
1524 gnutls_psk_set_server_credentials_function(*psk_credentials,
1525 psk_server_callback);
1529 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1530 "gnutls_psk_set_server_credentials_hint");
1533 return GNUTLS_E_SUCCESS;
1544post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1547 coap_gnutls_context_t *g_context =
1549 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1550 int ret = GNUTLS_E_SUCCESS;
1560 name = gnutls_malloc(len);
1562 return GNUTLS_E_MEMORY_ERROR;
1565 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1566 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1568 new_name = gnutls_realloc(name, len);
1569 if (new_name == NULL) {
1570 ret = GNUTLS_E_MEMORY_ERROR;
1578 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1581 if (ret != GNUTLS_E_SUCCESS)
1584 if (type != GNUTLS_NAME_DNS)
1595 for (i = 0; i < g_context->psk_sni_count; i++) {
1596 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1600 if (i == g_context->psk_sni_count) {
1611 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1612 GNUTLS_A_UNRECOGNIZED_NAME));
1613 g_env->sent_alert = 1;
1614 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1618 g_context->psk_sni_entry_list =
1619 gnutls_realloc(g_context->psk_sni_entry_list,
1620 (i+1)*
sizeof(psk_sni_entry));
1621 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1622 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1624 sni_setup_data.
psk_info = *new_entry;
1625 if ((ret = setup_psk_credentials(
1626 &g_context->psk_sni_entry_list[i].psk_credentials,
1628 &sni_setup_data)) < 0) {
1630 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1631 GNUTLS_A_BAD_CERTIFICATE));
1632 g_env->sent_alert = 1;
1636 g_context->psk_sni_count++;
1638 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1639 g_context->psk_sni_entry_list[i].psk_credentials),
1640 "gnutls_credentials_set");
1642 &g_context->psk_sni_entry_list[i].psk_info.hint);
1644 &g_context->psk_sni_entry_list[i].psk_info.key);
1660post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1663 coap_gnutls_context_t *g_context =
1665 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1666 int ret = GNUTLS_E_SUCCESS;
1669 if (g_context->setup_data.validate_sni_call_back) {
1676 name = gnutls_malloc(len);
1678 return GNUTLS_E_MEMORY_ERROR;
1681 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1682 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1684 new_name = gnutls_realloc(name, len);
1685 if (new_name == NULL) {
1686 ret = GNUTLS_E_MEMORY_ERROR;
1694 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1697 if (ret != GNUTLS_E_SUCCESS)
1700 if (type != GNUTLS_NAME_DNS)
1711 for (i = 0; i < g_context->pki_sni_count; i++) {
1712 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1716 if (i == g_context->pki_sni_count) {
1723 g_context->setup_data.validate_sni_call_back(name,
1724 g_context->setup_data.sni_call_back_arg));
1726 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1727 GNUTLS_A_UNRECOGNIZED_NAME));
1728 g_env->sent_alert = 1;
1729 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1733 g_context->pki_sni_entry_list = gnutls_realloc(
1734 g_context->pki_sni_entry_list,
1735 (i+1)*
sizeof(pki_sni_entry));
1736 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1737 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1738 sni_setup_data = g_context->setup_data;
1739 sni_setup_data.
pki_key = *new_entry;
1740 if ((ret = setup_pki_credentials(&g_context->pki_sni_entry_list[i].pki_credentials,
1745 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1746 GNUTLS_A_BAD_CERTIFICATE));
1747 g_env->sent_alert = 1;
1751 g_context->pki_sni_count++;
1753 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1754 g_context->pki_sni_entry_list[i].pki_credentials),
1755 "gnutls_credentials_set");
1767#if COAP_CLIENT_SUPPORT
1773setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1774 coap_gnutls_context_t *g_context =
1778 g_context->psk_pki_enabled |= IS_CLIENT;
1779 if (g_context->psk_pki_enabled & IS_PSK) {
1781 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1782 "gnutls_psk_allocate_client_credentials");
1783 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1784 psk_client_callback);
1785 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1786 g_env->psk_cl_credentials),
1787 "gnutls_credentials_set");
1790 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1793 "gnutls_server_name_set");
1799 if (tls_version->
version >= 0x030604) {
1801 const char *priority;
1803 if (tls_version->
version >= 0x030606) {
1804 priority = VARIANTS_NO_TLS13_3_6_6;
1806 priority = VARIANTS_NO_TLS13_3_6_4;
1808 ret = gnutls_priority_set_direct(g_env->g_session,
1811 if (ret == GNUTLS_E_INVALID_REQUEST)
1812 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1814 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1821 if ((g_context->psk_pki_enabled & IS_PKI) ||
1822 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1829 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1843 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1844 g_context, setup_data,
1846 "setup_pki_credentials");
1848 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1849 g_env->pki_credentials),
1850 "gnutls_credentials_set");
1853 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1854 &g_context->alpn_proto, 1, 0),
1855 "gnutls_alpn_set_protocols");
1859 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1862 "gnutls_server_name_set");
1865 return GNUTLS_E_SUCCESS;
1872#if COAP_SERVER_SUPPORT
1881psk_server_callback(gnutls_session_t g_session,
1882 const char *identity,
1883 gnutls_datum_t *key) {
1886 coap_gnutls_context_t *g_context;
1891 if (c_session == NULL)
1895 if (g_context == NULL)
1901 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
1902 lidentity.
length = strlen((
const char *)lidentity.
s);
1906 (
int)lidentity.
length, (
const char *)lidentity.
s);
1918 if (psk_key == NULL)
1921 key->data = gnutls_malloc(psk_key->
length);
1922 if (key->data == NULL)
1924 memcpy(key->data, psk_key->
s, psk_key->
length);
1925 key->size = psk_key->
length;
1934setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1935 coap_gnutls_context_t *g_context =
1937 int ret = GNUTLS_E_SUCCESS;
1939 g_context->psk_pki_enabled |= IS_SERVER;
1940 if (g_context->psk_pki_enabled & IS_PSK) {
1941 G_CHECK(setup_psk_credentials(
1942 &g_env->psk_sv_credentials,
1945 "setup_psk_credentials\n");
1946 G_CHECK(gnutls_credentials_set(g_env->g_session,
1948 g_env->psk_sv_credentials),
1949 "gnutls_credentials_set\n");
1950 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1951 post_client_hello_gnutls_psk);
1954 if (g_context->psk_pki_enabled & IS_PKI) {
1956 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1957 g_context, setup_data,
1959 "setup_pki_credentials");
1962 gnutls_certificate_server_set_request(g_env->g_session,
1963 GNUTLS_CERT_REQUIRE);
1965 gnutls_certificate_server_set_request(g_env->g_session,
1966 GNUTLS_CERT_REQUEST);
1968 gnutls_certificate_server_set_request(g_env->g_session,
1969 GNUTLS_CERT_IGNORE);
1972 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1973 post_client_hello_gnutls_pki);
1975 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1976 g_env->pki_credentials),
1977 "gnutls_credentials_set\n");
1979 return GNUTLS_E_SUCCESS;
1992coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
1997 if (!c_session->
tls) {
2001 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
2004 if (data != NULL && data->pdu_len > 0) {
2005 if (outl < data->pdu_len) {
2006 memcpy(out, data->pdu, outl);
2008 if (!data->peekmode) {
2010 data->pdu_len -= outl;
2013 memcpy(out, data->pdu, data->pdu_len);
2014 ret = data->pdu_len;
2015 if (!data->peekmode) {
2035coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
2036 size_t send_buffer_length) {
2037 ssize_t result = -1;
2042#if COAP_SERVER_SUPPORT
2051 send_buffer, send_buffer_length);
2052 if (result != (
int)send_buffer_length) {
2053 int keep_errno = errno;
2055 coap_log_warn(
"coap_netif_dgrm_write failed (%zd != %zu)\n",
2056 result, send_buffer_length);
2059 if (errno == ENOTCONN || errno == ECONNREFUSED)
2078receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
2082 fd_set readfds, writefds, exceptfds;
2084 int nfds = c_session->
sock.
fd +1;
2085 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2089 g_env->coap_ssl_data.pdu_len > 0) {
2095 FD_ZERO(&exceptfds);
2096 FD_SET(c_session->
sock.
fd, &readfds);
2097 if (!(g_env && g_env->doing_dtls_timeout)) {
2098 FD_SET(c_session->
sock.
fd, &writefds);
2099 FD_SET(c_session->
sock.
fd, &exceptfds);
2105 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2110static coap_gnutls_env_t *
2112 coap_gnutls_context_t *g_context =
2114 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2115#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2116 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2118 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2125 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2129 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2131 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2133 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2134 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2135 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2137 gnutls_transport_set_ptr(g_env->g_session, c_session);
2139 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2140 "gnutls_priority_set");
2142 if (type == GNUTLS_SERVER) {
2143#if COAP_SERVER_SUPPORT
2144 G_CHECK(setup_server_ssl_session(c_session, g_env),
2145 "setup_server_ssl_session");
2150#if COAP_CLIENT_SUPPORT
2151 G_CHECK(setup_client_ssl_session(c_session, g_env),
2152 "setup_client_ssl_session");
2158 gnutls_handshake_set_timeout(g_env->g_session,
2159 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2160 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2161 COAP_DTLS_RETRANSMIT_TOTAL_MS);
2172coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2173 coap_gnutls_env_t *g_env,
2174 coap_free_bye_t free_bye) {
2179 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2181 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2182 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2184 gnutls_deinit(g_env->g_session);
2185 g_env->g_session = NULL;
2186 if (g_context->psk_pki_enabled & IS_PSK) {
2187 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2188 g_env->psk_cl_credentials != NULL) {
2189 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2190 g_env->psk_cl_credentials = NULL;
2193 if (g_env->psk_sv_credentials != NULL)
2194 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2195 g_env->psk_sv_credentials = NULL;
2198 if ((g_context->psk_pki_enabled & IS_PKI) ||
2199 (g_context->psk_pki_enabled &
2200 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2201 gnutls_certificate_free_credentials(g_env->pki_credentials);
2202 g_env->pki_credentials = NULL;
2204 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2209#if COAP_SERVER_SUPPORT
2212 coap_gnutls_env_t *g_env =
2213 (coap_gnutls_env_t *)c_session->
tls;
2215 gnutls_transport_set_ptr(g_env->g_session, c_session);
2223 gnutls_session_t g_session) {
2224#if COAP_MAX_LOGGING_LEVEL > 0
2225 int last_alert = gnutls_alert_get(g_session);
2227 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2230 last_alert, gnutls_alert_get_name(last_alert));
2234 last_alert, gnutls_alert_get_name(last_alert));
2247do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2250 ret = gnutls_handshake(g_env->g_session);
2252 case GNUTLS_E_SUCCESS:
2253 g_env->established = 1;
2258 case GNUTLS_E_INTERRUPTED:
2262 case GNUTLS_E_AGAIN:
2266 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2270 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2272 g_env->sent_alert = 1;
2273 log_last_alert(c_session, g_env->g_session);
2275 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2276 case GNUTLS_E_UNEXPECTED_PACKET:
2280 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2281 log_last_alert(c_session, g_env->g_session);
2285 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2286#if (GNUTLS_VERSION_NUMBER > 0x030606)
2287 case GNUTLS_E_CERTIFICATE_REQUIRED:
2289 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2291 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2292 GNUTLS_A_BAD_CERTIFICATE));
2293 g_env->sent_alert = 1;
2297 case GNUTLS_E_DECRYPTION_FAILED:
2300 gnutls_strerror(ret));
2301 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2302 GNUTLS_A_DECRYPT_ERROR));
2303 g_env->sent_alert = 1;
2307 case GNUTLS_E_CERTIFICATE_ERROR:
2308 if (g_env->sent_alert) {
2314 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2315 case GNUTLS_E_NO_CIPHER_SUITES:
2316 case GNUTLS_E_INVALID_SESSION:
2319 gnutls_strerror(ret));
2320 if (!g_env->sent_alert) {
2321 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2322 GNUTLS_A_HANDSHAKE_FAILURE));
2323 g_env->sent_alert = 1;
2328 case GNUTLS_E_SESSION_EOF:
2329 case GNUTLS_E_PREMATURE_TERMINATION:
2330 case GNUTLS_E_TIMEDOUT:
2331 case GNUTLS_E_PULL_ERROR:
2332 case GNUTLS_E_PUSH_ERROR:
2338 "returned %d: '%s'\n",
2339 ret, gnutls_strerror(ret));
2346#if COAP_CLIENT_SUPPORT
2349 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2353 ret = do_gnutls_handshake(c_session, g_env);
2358 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2368 if (c_session && c_session->
context && c_session->
tls) {
2372 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2373 c_session->
tls = NULL;
2380 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2384 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2385 (
unsigned int)c_session->
mtu),
2386 "gnutls_dtls_set_data_mtu");
2398 const uint8_t *data,
size_t data_len) {
2400 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2402 assert(g_env != NULL);
2407 if (g_env->established) {
2408 ret = gnutls_record_send(g_env->g_session, data, data_len);
2412 case GNUTLS_E_AGAIN:
2415 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2417 g_env->sent_alert = 1;
2418 log_last_alert(c_session, g_env->g_session);
2424 "returned %d: '%s'\n",
2425 ret, gnutls_strerror(ret));
2434 ret = do_gnutls_handshake(c_session, g_env);
2466 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2469 if (g_env && g_env->g_session) {
2470 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2483 g_env->last_timeout = now;
2484 return now + rem_ms;
2496 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2499 g_env->doing_dtls_timeout = 1;
2501 (do_gnutls_handshake(c_session, g_env) < 0)) {
2503 g_env->doing_dtls_timeout = 0;
2507 g_env->doing_dtls_timeout = 0;
2520 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2522 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2526 assert(g_env != NULL);
2528 if (ssl_data->pdu_len)
2529 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2531 ssl_data->pdu = data;
2532 ssl_data->pdu_len = data_len;
2535 if (g_env->established) {
2539 gnutls_transport_set_ptr(g_env->g_session, c_session);
2542 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2547 }
else if (ret == 0) {
2551 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2553 g_env->sent_alert = 1;
2554 log_last_alert(c_session, g_env->g_session);
2558 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2559 log_last_alert(c_session, g_env->g_session);
2564 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2570 ret = do_gnutls_handshake(c_session, g_env);
2575 if (ssl_data->pdu_len && !g_env->sent_alert) {
2577 ret = do_gnutls_handshake(c_session, g_env);
2597 if (ssl_data && ssl_data->pdu_len) {
2599 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2600 ssl_data->pdu_len = 0;
2601 ssl_data->pdu = NULL;
2606#if COAP_SERVER_SUPPORT
2614 const uint8_t *data,
2617 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2618 coap_ssl_t *ssl_data;
2622 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2624 c_session->
tls = g_env;
2625 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2626 GNUTLS_COOKIE_KEY_SIZE);
2633 gnutls_dtls_prestate_st prestate;
2636 memset(&prestate, 0,
sizeof(prestate));
2638 memcpy(&data_rw, &data,
sizeof(data_rw));
2639 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2646 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2654 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2657 ssl_data = &g_env->coap_ssl_data;
2658 ssl_data->pdu = data;
2659 ssl_data->pdu_len = data_len;
2661 ret = do_gnutls_handshake(c_session, g_env);
2668 g_env, COAP_FREE_BYE_NONE);
2669 c_session->
tls = NULL;
2677 if (ssl_data && ssl_data->pdu_len) {
2679 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2680 ssl_data->pdu_len = 0;
2681 ssl_data->pdu = NULL;
2692#if !COAP_DISABLE_TCP
2700coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2722coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2731 (errno == EPIPE || errno == ECONNRESET)) {
2757#if COAP_CLIENT_SUPPORT
2760 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2761 coap_gnutls_context_t *g_context =
2763#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2764 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2766 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2773 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2775 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2777 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2778 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2779 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2781 gnutls_transport_set_ptr(g_env->g_session, c_session);
2783 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2784 setup_client_ssl_session(c_session, g_env);
2786 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2788 c_session->
tls = g_env;
2789 ret = do_gnutls_handshake(c_session, g_env);
2803#if COAP_SERVER_SUPPORT
2806 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2807 coap_gnutls_context_t *g_context =
2809#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2810 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2812 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2818 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2820 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2822 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2823 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2824 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2826 gnutls_transport_set_ptr(g_env->g_session, c_session);
2828 setup_server_ssl_session(c_session, g_env);
2830 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2831 gnutls_handshake_set_timeout(g_env->g_session,
2832 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2834 c_session->
tls = g_env;
2835 ret = do_gnutls_handshake(c_session, g_env);
2862 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2864 assert(g_env != NULL);
2867 if (g_env->established) {
2868 ret = gnutls_record_send(g_env->g_session, data, data_len);
2872 case GNUTLS_E_AGAIN:
2875 case GNUTLS_E_PUSH_ERROR:
2876 case GNUTLS_E_PULL_ERROR:
2877 case GNUTLS_E_PREMATURE_TERMINATION:
2880 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2882 g_env->sent_alert = 1;
2883 log_last_alert(c_session, g_env->g_session);
2888 "returned %d: '%s'\n",
2889 ret, gnutls_strerror(ret));
2898 ret = do_gnutls_handshake(c_session, g_env);
2921 if (ret == (ssize_t)data_len)
2938 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2947 if (!g_env->established && !g_env->sent_alert) {
2948 ret = do_gnutls_handshake(c_session, g_env);
2957 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2963 case GNUTLS_E_AGAIN:
2967 case GNUTLS_E_PULL_ERROR:
2970 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2972 g_env->sent_alert = 1;
2973 log_last_alert(c_session, g_env->g_session);
2976 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2977 log_last_alert(c_session, g_env->g_session);
2982 "returned %d: '%s'\n",
2983 ret, gnutls_strerror(ret));
3008#if COAP_SERVER_SUPPORT
3011 gnutls_hash_hd_t digest_ctx;
3013 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
3022 gnutls_hash_deinit(digest_ctx, NULL);
3027 const uint8_t *data,
3029 int ret = gnutls_hash(digest_ctx, data, data_len);
3037 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
3049static struct hash_algs {
3051 gnutls_digest_algorithm_t dig_type;
3059static gnutls_digest_algorithm_t
3060get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3063 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3064 if (hashs[idx].alg == alg) {
3065 *hash_len = hashs[idx].dig_size;
3066 return hashs[idx].dig_type;
3069 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3070 return GNUTLS_DIG_UNKNOWN;
3078 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3079 gnutls_hash_hd_t digest_ctx;
3083 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3084 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3088 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3091 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
3098 gnutls_hash_output(digest_ctx,
dummy->s);
3101 gnutls_hash_deinit(digest_ctx, NULL);
3106 gnutls_hash_deinit(digest_ctx, NULL);
3111#if COAP_OSCORE_SUPPORT
3122static struct cipher_algs {
3124 gnutls_cipher_algorithm_t cipher_type;
3129static gnutls_cipher_algorithm_t
3133 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3134 if (ciphers[idx].alg == alg)
3135 return ciphers[idx].cipher_type;
3137 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3146static struct hmac_algs {
3148 gnutls_mac_algorithm_t hmac_type;
3154static gnutls_mac_algorithm_t
3158 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3159 if (hmacs[idx].hmac_alg == hmac_alg)
3160 return hmacs[idx].hmac_type;
3162 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3168 return get_cipher_alg(alg);
3177 return get_hmac_alg(hmac_alg);
3185 size_t *max_result_len) {
3186 gnutls_aead_cipher_hd_t ctx;
3190 size_t result_len = *max_result_len;
3191 gnutls_cipher_algorithm_t algo;
3193 uint8_t *key_data_rw;
3199 assert(params != NULL);
3203 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3204 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3208 tag_size = gnutls_cipher_get_tag_size(algo);
3212 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3213 key.data = key_data_rw;
3223 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3225 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3235 "gnutls_aead_cipher_encrypt");
3236 *max_result_len = result_len;
3239 gnutls_aead_cipher_deinit(ctx);
3240 return ret == 1 ? 1 : 0;
3248 size_t *max_result_len) {
3249 gnutls_aead_cipher_hd_t ctx;
3253 size_t result_len = *max_result_len;
3254 gnutls_cipher_algorithm_t algo;
3256 uint8_t *key_data_rw;
3262 assert(params != NULL);
3267 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3268 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3272 tag_size = gnutls_cipher_get_tag_size(algo);
3276 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3277 key.data = key_data_rw;
3287 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3289 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3299 "gnutls_aead_cipher_decrypt");
3300 *max_result_len = result_len;
3303 gnutls_aead_cipher_deinit(ctx);
3304 return ret == 1 ? 1 : 0;
3312 gnutls_hmac_hd_t ctx;
3315 gnutls_mac_algorithm_t mac_algo;
3321 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3322 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3325 len = gnutls_hmac_get_len(mac_algo);
3332 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3333 "gnutls_hmac_init");
3334 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3335 gnutls_hmac_output(ctx,
dummy->s);
3341 gnutls_hmac_deinit(ctx, NULL);
3342 return ret == 1 ? 1 : 0;
3353#pragma GCC diagnostic ignored "-Wunused-function"
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
Library specific build wrapper for coap_internal.h.
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED)
static coap_log_t dtls_log_level
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
coap_binary_t * get_asn1_spki(const uint8_t *data, size_t size)
Abstract SPKI public key from the ASN1.
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
int coap_dtls_define_issue(coap_define_issue_key_t type, coap_define_issue_fail_t fail, coap_dtls_key_t *key, const coap_dtls_role_t role, int ret)
Report PKI DEFINE type issue.
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
#define COAP_DTLS_HINT_LENGTH
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
#define COAP_DTLS_RPK_CERT_CN
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
@ COAP_TLS_LIBRARY_GNUTLS
Using GnuTLS library.
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_dtls_log(level,...)
Logging function.
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_ALGORITHM_SHA_256_256
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
#define COAP_PROTO_NOT_RELIABLE(p)
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_NONE
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
The CoAP stack's global state is stored in a coap_context_t object.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@2 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorith to use.
The structure that holds the Client PSK information.
coap_bin_const_t identity
The structure used for defining the Client PSK setup data to be used.
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
The structure that holds the PKI key information.
coap_pki_key_define_t define
for definable type keys
union coap_dtls_key_t::@3 key
coap_pki_key_t key_type
key format type
The structure used for defining the PKI setup data to be used.
uint8_t allow_no_crl
1 ignore if CRL not there
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 allow_expired_certs
1 if expired certs are allowed
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 allow_self_signed
1 if self-signed certs are allowed.
uint8_t allow_expired_crl
1 if expired crl is allowed
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