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);
508 if (c_session && c_session->
tls) {
509 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
511 return g_env->g_session;
536 const char *err =
"Unknown Error";
538 coap_gnutls_context_t *g_context =
539 (coap_gnutls_context_t *)
540 gnutls_malloc(
sizeof(coap_gnutls_context_t));
544 const char *priority;
546 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
547 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
548 g_context->alpn_proto.data = gnutls_malloc(4);
549 if (g_context->alpn_proto.data) {
550 memcpy(g_context->alpn_proto.data,
"coap", 4);
551 g_context->alpn_proto.size = 4;
554 if (tls_version->
version >= 0x030606) {
555 priority = VARIANTS_3_6_6;
556 }
else if (tls_version->
version >= 0x030505) {
557 priority = VARIANTS_3_5_5;
559 priority = VARIANTS_BASE;
561 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
562 if (ret != GNUTLS_E_SUCCESS) {
563 if (ret == GNUTLS_E_INVALID_REQUEST)
564 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
566 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
581 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
583 gnutls_free(g_context->alpn_proto.data);
584 gnutls_free(g_context->root_ca_file);
585 gnutls_free(g_context->root_ca_path);
586 for (i = 0; i < g_context->pki_sni_count; i++) {
587 gnutls_free(g_context->pki_sni_entry_list[i].sni);
588 gnutls_certificate_free_credentials(
589 g_context->pki_sni_entry_list[i].pki_credentials);
591 if (g_context->pki_sni_entry_list)
592 gnutls_free(g_context->pki_sni_entry_list);
594 for (i = 0; i < g_context->psk_sni_count; i++) {
595 gnutls_free(g_context->psk_sni_entry_list[i].sni);
597 gnutls_psk_free_server_credentials(
598 g_context->psk_sni_entry_list[i].psk_credentials);
600 if (g_context->psk_sni_entry_list)
601 gnutls_free(g_context->psk_sni_entry_list);
603 gnutls_priority_deinit(g_context->priority_cache);
605 gnutls_global_deinit();
606 gnutls_free(g_context);
609#if COAP_CLIENT_SUPPORT
618psk_client_callback(gnutls_session_t g_session,
619 char **username, gnutls_datum_t *key) {
622 coap_gnutls_context_t *g_context;
624 const char *hint = gnutls_psk_client_get_hint(g_session);
634 if (c_session ==
NULL)
638 if (g_context ==
NULL)
643 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
644 temp.
length = strlen((
const char *)temp.
s);
648 (
const char *)temp.
s);
660 if (cpsk_info ==
NULL)
665 psk_identity = &cpsk_info->
identity;
666 psk_key = &cpsk_info->
key;
672 if (psk_identity ==
NULL || psk_key ==
NULL) {
677 *username = gnutls_malloc(psk_identity->
length+1);
678 if (*username ==
NULL)
680 memcpy(*username, psk_identity->
s, psk_identity->
length);
681 (*username)[psk_identity->
length] =
'\000';
683 key->data = gnutls_malloc(psk_key->
length);
684 if (key->data ==
NULL) {
685 gnutls_free(*username);
689 memcpy(key->data, psk_key->
s, psk_key->
length);
690 key->size = psk_key->
length;
696 gnutls_certificate_type_t certificate_type;
698 const gnutls_datum_t *cert_list;
699 unsigned int cert_list_size;
701} coap_gnutls_certificate_info_t;
707static gnutls_certificate_type_t
708get_san_or_cn(gnutls_session_t g_session,
709 coap_gnutls_certificate_info_t *cert_info) {
710 gnutls_x509_crt_t cert;
717#if (GNUTLS_VERSION_NUMBER >= 0x030606)
718 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
721 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
724 cert_info->san_or_cn =
NULL;
726 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
727 &cert_info->cert_list_size);
728 if (cert_info->cert_list_size == 0) {
729 return GNUTLS_CRT_UNKNOWN;
732 if (cert_info->certificate_type != GNUTLS_CRT_X509)
733 return cert_info->certificate_type;
735 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
738 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
739 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
741 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
743 size =
sizeof(dn) -1;
745 ret = gnutls_x509_crt_get_subject_alt_name(cert, 0, dn, &size,
NULL);
748 gnutls_x509_crt_deinit(cert);
749 cert_info->san_or_cn = gnutls_strdup(dn);
750 return cert_info->certificate_type;
754 G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size),
"gnutls_x509_crt_get_dn");
756 gnutls_x509_crt_deinit(cert);
762 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
763 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
772 char *ecn = strchr(cn,
',');
776 cert_info->san_or_cn = gnutls_strdup(cn);
777 return cert_info->certificate_type;
779 return GNUTLS_CRT_UNKNOWN;
782 return GNUTLS_CRT_UNKNOWN;
785#if (GNUTLS_VERSION_NUMBER >= 0x030606)
786#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
787 cert_info.san_or_cn : \
788 cert_type == GNUTLS_CRT_RAW ? \
789 COAP_DTLS_RPK_CERT_CN : "?")
791#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
792 cert_info.san_or_cn : "?")
795#if (GNUTLS_VERSION_NUMBER >= 0x030606)
797check_rpk_cert(coap_gnutls_context_t *g_context,
798 coap_gnutls_certificate_info_t *cert_info,
802 if (g_context->setup_data.validate_cn_call_back) {
803 gnutls_pcert_st pcert;
807 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
808 GNUTLS_X509_FMT_DER, 0, 0),
809 "gnutls_pcert_import_rawpk_raw");
812 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
813 "gnutls_pubkey_export");
814 gnutls_pcert_deinit(&pcert);
822 g_context->setup_data.cn_call_back_arg));
838cert_verify_gnutls(gnutls_session_t g_session) {
839 unsigned int status = 0;
840 unsigned int fail = 0;
843 coap_gnutls_context_t *g_context =
845 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
846 int alert = GNUTLS_A_BAD_CERTIFICATE;
848 coap_gnutls_certificate_info_t cert_info;
849 gnutls_certificate_type_t cert_type;
851 memset(&cert_info, 0,
sizeof(cert_info));
852 cert_type = get_san_or_cn(g_session, &cert_info);
853#if (GNUTLS_VERSION_NUMBER >= 0x030606)
854 if (cert_type == GNUTLS_CRT_RAW) {
855 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
856 alert = GNUTLS_A_ACCESS_DENIED;
863 if (cert_info.cert_list_size == 0) {
864 if (!g_context->setup_data.verify_peer_cert)
870 G_CHECK(gnutls_certificate_verify_peers(g_session,
NULL, 0, &status),
871 "gnutls_certificate_verify_peers");
874 status, cert_info.san_or_cn);
876 status &= ~(GNUTLS_CERT_INVALID);
877 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
878 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
879 if (g_context->setup_data.allow_expired_certs) {
882 "The certificate has an invalid usage date",
888 "The certificate has an invalid usage date",
892 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
893 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
894 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
895 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
896 if (g_context->setup_data.allow_expired_crl) {
899 "The certificate's CRL entry has an invalid usage date",
905 "The certificate's CRL entry has an invalid usage date",
909 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
910 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
911 if (cert_info.self_signed) {
912 if (g_context->setup_data.allow_self_signed &&
913 !g_context->setup_data.check_common_ca) {
920 alert = GNUTLS_A_UNKNOWN_CA;
927 if (!g_context->setup_data.verify_peer_cert) {
930 "The peer certificate's CA is unknown",
934 alert = GNUTLS_A_UNKNOWN_CA;
937 "The peer certificate's CA is unknown",
942 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
943 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
947 "The certificate uses an insecure algorithm",
953 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
955 status, OUTPUT_CERT_NAME);
962 if (g_context->setup_data.validate_cn_call_back) {
963 gnutls_x509_crt_t cert;
968 const int cert_is_trusted = !status;
970 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
973 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
974 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
977 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
978 "gnutls_x509_crt_export");
979 gnutls_x509_crt_deinit(cert);
981 g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
987 g_context->setup_data.cn_call_back_arg));
989 alert = GNUTLS_A_ACCESS_DENIED;
994 if (g_context->setup_data.additional_tls_setup_call_back) {
996 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
997 &g_context->setup_data)) {
1003 if (cert_info.san_or_cn)
1004 gnutls_free(cert_info.san_or_cn);
1009 if (cert_info.san_or_cn)
1010 gnutls_free(cert_info.san_or_cn);
1012 if (!g_env->sent_alert) {
1013 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
1014 g_env->sent_alert = 1;
1028cert_verify_callback_gnutls(gnutls_session_t g_session) {
1029 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
1030 if (cert_verify_gnutls(g_session) == 0) {
1038#define min(a,b) ((a) < (b) ? (a) : (b))
1042pin_callback(
void *user_data,
int attempt,
1062check_null_memory(gnutls_datum_t *datum,
1063 const uint8_t *buf,
size_t len,
int *alloced) {
1066 if (buf[len-1] !=
'\000') {
1069 datum->data = gnutls_malloc(len + 1);
1072 return GNUTLS_E_MEMORY_ERROR;
1074 memcpy(datum->data, buf, len);
1075 datum->data[len] =
'\000';
1079 memcpy(&datum->data,
1080 &buf,
sizeof(datum->data));
1090setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1091 gnutls_session_t g_session,
1092 coap_gnutls_context_t *g_context,
1096 gnutls_datum_t cert;
1097 gnutls_datum_t pkey;
1099 int alloced_cert_memory = 0;
1100 int alloced_pkey_memory = 0;
1101 int alloced_ca_memory = 0;
1102 int have_done_key = 0;
1109 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1110 "gnutls_certificate_allocate_credentials");
1127#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1131 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1134 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1140 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1147 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1161 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1165 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1168 GNUTLS_X509_FMT_PEM)) < 0) {
1175 if ((ret = check_null_memory(&cert,
1178 &alloced_cert_memory)) < 0) {
1183 if ((ret = check_null_memory(&pkey,
1186 &alloced_pkey_memory)) < 0) {
1187 if (alloced_cert_memory)
1188 gnutls_free(cert.data);
1193 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1196 GNUTLS_X509_FMT_PEM)) < 0) {
1197 if (alloced_cert_memory)
1198 gnutls_free(cert.data);
1199 if (alloced_pkey_memory)
1200 gnutls_free(pkey.data);
1205 if (alloced_cert_memory)
1206 gnutls_free(cert.data);
1207 if (alloced_pkey_memory)
1208 gnutls_free(pkey.data);
1211#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1212 if ((ret = check_null_memory(&cert,
1215 &alloced_cert_memory)) < 0) {
1220 if ((ret = check_null_memory(&pkey,
1223 &alloced_pkey_memory)) < 0) {
1224 if (alloced_cert_memory)
1225 gnutls_free(cert.data);
1230 if (strstr((
char *)pkey.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1231 gnutls_datum_t der_private;
1233 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &pkey,
1234 &der_private) == 0) {
1239 gnutls_datum_t tspki;
1241 tspki.data = spki->
s;
1242 tspki.size = spki->
length;
1243 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1246 GNUTLS_X509_FMT_DER,
NULL,
1247 COAP_GNUTLS_KEY_RPK,
1254 gnutls_free(der_private.data);
1257 if (!have_done_key) {
1258 if ((ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1261 GNUTLS_X509_FMT_PEM,
NULL,
1262 COAP_GNUTLS_KEY_RPK,
1264 if (alloced_cert_memory)
1265 gnutls_free(cert.data);
1266 if (alloced_pkey_memory)
1267 gnutls_free(pkey.data);
1270 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1273 if (alloced_cert_memory)
1274 gnutls_free(cert.data);
1275 if (alloced_pkey_memory)
1276 gnutls_free(pkey.data);
1279 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1282 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1285 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1288 GNUTLS_X509_FMT_DER) < 0)) {
1295 if ((ret = check_null_memory(&cert,
1298 &alloced_cert_memory)) < 0) {
1303 if ((ret = check_null_memory(&pkey,
1306 &alloced_pkey_memory)) < 0) {
1307 if (alloced_cert_memory)
1308 gnutls_free(cert.data);
1313 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1316 GNUTLS_X509_FMT_DER)) < 0) {
1317 if (alloced_cert_memory)
1318 gnutls_free(cert.data);
1319 if (alloced_pkey_memory)
1320 gnutls_free(pkey.data);
1325 if (alloced_cert_memory)
1326 gnutls_free(cert.data);
1327 if (alloced_pkey_memory)
1328 gnutls_free(pkey.data);
1331 gnutls_pkcs11_set_pin_function(pin_callback, &setup_data->
pki_key);
1332 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1335 GNUTLS_X509_FMT_DER)) < 0) {
1342#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1343 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1344 if ((ret = gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1347 GNUTLS_X509_FMT_PEM,
NULL,
1348 COAP_GNUTLS_KEY_RPK,
1349 NULL, 0, GNUTLS_PKCS_PLAIN, 0))) {
1355 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1356 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1363 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1374 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1376 GNUTLS_X509_FMT_PEM) < 0)) {
1383 if ((ret = check_null_memory(&ca,
1386 &alloced_ca_memory)) < 0) {
1391 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1393 GNUTLS_X509_FMT_PEM)) < 0) {
1394 if (alloced_ca_memory)
1395 gnutls_free(ca.data);
1400 if (alloced_ca_memory)
1401 gnutls_free(ca.data);
1407 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1409 GNUTLS_X509_FMT_DER) < 0)) {
1416 if ((ret = check_null_memory(&ca,
1419 &alloced_ca_memory)) < 0) {
1424 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1426 GNUTLS_X509_FMT_DER)) <= 0) {
1427 if (alloced_ca_memory)
1428 gnutls_free(ca.data);
1433 if (alloced_ca_memory)
1434 gnutls_free(ca.data);
1437 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1439 GNUTLS_X509_FMT_DER)) <= 0) {
1452 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1456#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1457 if (g_context->trust_store_defined) {
1458 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1459 "gnutls_certificate_set_x509_system_trust");
1462 if (g_context->root_ca_file) {
1463 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1464 g_context->root_ca_file,
1465 GNUTLS_X509_FMT_PEM);
1467 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1470 if (g_context->root_ca_path) {
1471#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1472 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1473 g_context->root_ca_path,
1474 GNUTLS_X509_FMT_PEM),
1475 "gnutls_certificate_set_x509_trust_dir");
1478 gnutls_certificate_send_x509_rdn_sequence(g_session,
1480#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1481 if (!(g_context->psk_pki_enabled & IS_PKI) && !g_context->trust_store_defined) {
1483 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1484 "gnutls_certificate_set_x509_system_trust");
1489 gnutls_certificate_set_verify_function(*pki_credentials,
1490 cert_verify_callback_gnutls);
1494 gnutls_certificate_set_verify_limits(*pki_credentials,
1503 gnutls_certificate_set_verify_flags(*pki_credentials,
1505 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1508 return GNUTLS_E_SUCCESS;
1514#if COAP_SERVER_SUPPORT
1520setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1526 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1527 "gnutls_psk_allocate_server_credentials");
1528 gnutls_psk_set_server_credentials_function(*psk_credentials,
1529 psk_server_callback);
1533 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1534 "gnutls_psk_set_server_credentials_hint");
1537 return GNUTLS_E_SUCCESS;
1548post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1551 coap_gnutls_context_t *g_context =
1553 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1554 int ret = GNUTLS_E_SUCCESS;
1557 if (c_session->
context->spsk_setup_data.validate_sni_call_back) {
1564 name = gnutls_malloc(len);
1566 return GNUTLS_E_MEMORY_ERROR;
1569 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1570 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1572 new_name = gnutls_realloc(name, len);
1573 if (new_name ==
NULL) {
1574 ret = GNUTLS_E_MEMORY_ERROR;
1582 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1585 if (ret != GNUTLS_E_SUCCESS)
1588 if (type != GNUTLS_NAME_DNS)
1599 for (i = 0; i < g_context->psk_sni_count; i++) {
1600 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1604 if (i == g_context->psk_sni_count) {
1611 c_session->
context->spsk_setup_data.validate_sni_call_back(name,
1613 c_session->
context->spsk_setup_data.sni_call_back_arg));
1615 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1616 GNUTLS_A_UNRECOGNIZED_NAME));
1617 g_env->sent_alert = 1;
1618 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1622 g_context->psk_sni_entry_list =
1623 gnutls_realloc(g_context->psk_sni_entry_list,
1624 (i+1)*
sizeof(psk_sni_entry));
1625 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1626 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1627 sni_setup_data = c_session->
context->spsk_setup_data;
1628 sni_setup_data.
psk_info = *new_entry;
1629 if ((ret = setup_psk_credentials(
1630 &g_context->psk_sni_entry_list[i].psk_credentials,
1632 &sni_setup_data)) < 0) {
1634 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1635 GNUTLS_A_BAD_CERTIFICATE));
1636 g_env->sent_alert = 1;
1640 g_context->psk_sni_count++;
1642 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1643 g_context->psk_sni_entry_list[i].psk_credentials),
1644 "gnutls_credentials_set");
1646 &g_context->psk_sni_entry_list[i].psk_info.hint);
1648 &g_context->psk_sni_entry_list[i].psk_info.key);
1664post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1667 coap_gnutls_context_t *g_context =
1669 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1670 int ret = GNUTLS_E_SUCCESS;
1673 if (g_context->setup_data.validate_sni_call_back) {
1680 name = gnutls_malloc(len);
1682 return GNUTLS_E_MEMORY_ERROR;
1685 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1686 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1688 new_name = gnutls_realloc(name, len);
1689 if (new_name ==
NULL) {
1690 ret = GNUTLS_E_MEMORY_ERROR;
1698 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1701 if (ret != GNUTLS_E_SUCCESS)
1704 if (type != GNUTLS_NAME_DNS)
1715 for (i = 0; i < g_context->pki_sni_count; i++) {
1716 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1720 if (i == g_context->pki_sni_count) {
1727 g_context->setup_data.validate_sni_call_back(name,
1728 g_context->setup_data.sni_call_back_arg));
1730 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1731 GNUTLS_A_UNRECOGNIZED_NAME));
1732 g_env->sent_alert = 1;
1733 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1737 g_context->pki_sni_entry_list = gnutls_realloc(
1738 g_context->pki_sni_entry_list,
1739 (i+1)*
sizeof(pki_sni_entry));
1740 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1741 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1742 sni_setup_data = g_context->setup_data;
1743 sni_setup_data.
pki_key = *new_entry;
1744 if ((ret = setup_pki_credentials(&g_context->pki_sni_entry_list[i].pki_credentials,
1749 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1750 GNUTLS_A_BAD_CERTIFICATE));
1751 g_env->sent_alert = 1;
1755 g_context->pki_sni_count++;
1757 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1758 g_context->pki_sni_entry_list[i].pki_credentials),
1759 "gnutls_credentials_set");
1771#if COAP_CLIENT_SUPPORT
1777setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1778 coap_gnutls_context_t *g_context =
1782 g_context->psk_pki_enabled |= IS_CLIENT;
1783 if (g_context->psk_pki_enabled & IS_PSK) {
1785 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1786 "gnutls_psk_allocate_client_credentials");
1787 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1788 psk_client_callback);
1789 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1790 g_env->psk_cl_credentials),
1791 "gnutls_credentials_set");
1794 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1797 "gnutls_server_name_set");
1803 if (tls_version->
version >= 0x030604) {
1805 const char *priority;
1807 if (tls_version->
version >= 0x030606) {
1808 priority = VARIANTS_NO_TLS13_3_6_6;
1810 priority = VARIANTS_NO_TLS13_3_6_4;
1812 ret = gnutls_priority_set_direct(g_env->g_session,
1815 if (ret == GNUTLS_E_INVALID_REQUEST)
1816 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1818 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1825 if ((g_context->psk_pki_enabled & IS_PKI) ||
1826 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1833 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1847 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1848 g_context, setup_data,
1850 "setup_pki_credentials");
1852 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1853 g_env->pki_credentials),
1854 "gnutls_credentials_set");
1857 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1858 &g_context->alpn_proto, 1, 0),
1859 "gnutls_alpn_set_protocols");
1863 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1866 "gnutls_server_name_set");
1869 return GNUTLS_E_SUCCESS;
1876#if COAP_SERVER_SUPPORT
1885psk_server_callback(gnutls_session_t g_session,
1886 const char *identity,
1887 gnutls_datum_t *key) {
1890 coap_gnutls_context_t *g_context;
1895 if (c_session ==
NULL)
1899 if (g_context ==
NULL)
1901 setup_data = &c_session->
context->spsk_setup_data;
1905 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
1906 lidentity.
length = strlen((
const char *)lidentity.
s);
1910 (
int)lidentity.
length, (
const char *)lidentity.
s);
1922 if (psk_key ==
NULL)
1925 key->data = gnutls_malloc(psk_key->
length);
1926 if (key->data ==
NULL)
1928 memcpy(key->data, psk_key->
s, psk_key->
length);
1929 key->size = psk_key->
length;
1938setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1939 coap_gnutls_context_t *g_context =
1941 int ret = GNUTLS_E_SUCCESS;
1943 g_context->psk_pki_enabled |= IS_SERVER;
1944 if (g_context->psk_pki_enabled & IS_PSK) {
1945 G_CHECK(setup_psk_credentials(
1946 &g_env->psk_sv_credentials,
1948 &c_session->
context->spsk_setup_data),
1949 "setup_psk_credentials\n");
1950 G_CHECK(gnutls_credentials_set(g_env->g_session,
1952 g_env->psk_sv_credentials),
1953 "gnutls_credentials_set\n");
1954 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1955 post_client_hello_gnutls_psk);
1958 if (g_context->psk_pki_enabled & IS_PKI) {
1960 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1961 g_context, setup_data,
1963 "setup_pki_credentials");
1966 gnutls_certificate_server_set_request(g_env->g_session,
1967 GNUTLS_CERT_REQUIRE);
1969 gnutls_certificate_server_set_request(g_env->g_session,
1970 GNUTLS_CERT_REQUEST);
1972 gnutls_certificate_server_set_request(g_env->g_session,
1973 GNUTLS_CERT_IGNORE);
1976 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1977 post_client_hello_gnutls_pki);
1979 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1980 g_env->pki_credentials),
1981 "gnutls_credentials_set\n");
1983 return GNUTLS_E_SUCCESS;
1996coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2001 if (!c_session->
tls) {
2005 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
2008 if (data !=
NULL && data->pdu_len > 0) {
2009 if (outl < data->pdu_len) {
2010 memcpy(out, data->pdu, outl);
2012 if (!data->peekmode) {
2014 data->pdu_len -= outl;
2017 memcpy(out, data->pdu, data->pdu_len);
2018 ret = data->pdu_len;
2019 if (!data->peekmode) {
2039coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
2040 size_t send_buffer_length) {
2041 ssize_t result = -1;
2046#if COAP_SERVER_SUPPORT
2047 && c_session->endpoint ==
NULL
2055 send_buffer, send_buffer_length);
2056 if (result != (
int)send_buffer_length) {
2057 int keep_errno = errno;
2060 result, send_buffer_length);
2063 if (errno == ENOTCONN || errno == ECONNREFUSED)
2082receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
2086 fd_set readfds, writefds, exceptfds;
2088 int nfds = c_session->
sock.
fd +1;
2089 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2093 g_env->coap_ssl_data.pdu_len > 0) {
2099 FD_ZERO(&exceptfds);
2100 FD_SET(c_session->
sock.
fd, &readfds);
2101 if (!(g_env && g_env->doing_dtls_timeout)) {
2102 FD_SET(c_session->
sock.
fd, &writefds);
2103 FD_SET(c_session->
sock.
fd, &exceptfds);
2109 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2114static coap_gnutls_env_t *
2116 coap_gnutls_context_t *g_context =
2118 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2119#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2120 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2122 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2129 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2133 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2135 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2137 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2138 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2139 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2141 gnutls_transport_set_ptr(g_env->g_session, c_session);
2143 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2144 "gnutls_priority_set");
2146 if (type == GNUTLS_SERVER) {
2147#if COAP_SERVER_SUPPORT
2148 G_CHECK(setup_server_ssl_session(c_session, g_env),
2149 "setup_server_ssl_session");
2154#if COAP_CLIENT_SUPPORT
2155 G_CHECK(setup_client_ssl_session(c_session, g_env),
2156 "setup_client_ssl_session");
2162 gnutls_handshake_set_timeout(g_env->g_session,
2163 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2176coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2177 coap_gnutls_env_t *g_env,
2178 coap_free_bye_t free_bye) {
2183 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2185 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2186 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2188 gnutls_deinit(g_env->g_session);
2189 g_env->g_session =
NULL;
2190 if (g_context->psk_pki_enabled & IS_PSK) {
2191 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2192 g_env->psk_cl_credentials !=
NULL) {
2193 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2194 g_env->psk_cl_credentials =
NULL;
2197 if (g_env->psk_sv_credentials !=
NULL)
2198 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2199 g_env->psk_sv_credentials =
NULL;
2202 if ((g_context->psk_pki_enabled & IS_PKI) ||
2203 (g_context->psk_pki_enabled &
2204 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2205 gnutls_certificate_free_credentials(g_env->pki_credentials);
2206 g_env->pki_credentials =
NULL;
2208 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2213#if COAP_SERVER_SUPPORT
2216 coap_gnutls_env_t *g_env =
2217 (coap_gnutls_env_t *)c_session->
tls;
2219 gnutls_transport_set_ptr(g_env->g_session, c_session);
2227 gnutls_session_t g_session) {
2228#if COAP_MAX_LOGGING_LEVEL > 0
2229 int last_alert = gnutls_alert_get(g_session);
2231 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2234 last_alert, gnutls_alert_get_name(last_alert));
2238 last_alert, gnutls_alert_get_name(last_alert));
2251do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2254 ret = gnutls_handshake(g_env->g_session);
2256 case GNUTLS_E_SUCCESS:
2257 g_env->established = 1;
2262 case GNUTLS_E_INTERRUPTED:
2266 case GNUTLS_E_AGAIN:
2270 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2274 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2276 g_env->sent_alert = 1;
2277 log_last_alert(c_session, g_env->g_session);
2279 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2280 case GNUTLS_E_UNEXPECTED_PACKET:
2284 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2285 log_last_alert(c_session, g_env->g_session);
2289 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2290#if (GNUTLS_VERSION_NUMBER > 0x030606)
2291 case GNUTLS_E_CERTIFICATE_REQUIRED:
2293 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2295 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2296 GNUTLS_A_BAD_CERTIFICATE));
2297 g_env->sent_alert = 1;
2301 case GNUTLS_E_DECRYPTION_FAILED:
2304 gnutls_strerror(ret));
2305 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2306 GNUTLS_A_DECRYPT_ERROR));
2307 g_env->sent_alert = 1;
2311 case GNUTLS_E_CERTIFICATE_ERROR:
2312 if (g_env->sent_alert) {
2318 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2319 case GNUTLS_E_NO_CIPHER_SUITES:
2320 case GNUTLS_E_INVALID_SESSION:
2323 gnutls_strerror(ret));
2324 if (!g_env->sent_alert) {
2325 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2326 GNUTLS_A_HANDSHAKE_FAILURE));
2327 g_env->sent_alert = 1;
2332 case GNUTLS_E_SESSION_EOF:
2333 case GNUTLS_E_PREMATURE_TERMINATION:
2334 case GNUTLS_E_TIMEDOUT:
2335 case GNUTLS_E_PULL_ERROR:
2336 case GNUTLS_E_PUSH_ERROR:
2342 "returned %d: '%s'\n",
2343 ret, gnutls_strerror(ret));
2350#if COAP_CLIENT_SUPPORT
2353 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2357 ret = do_gnutls_handshake(c_session, g_env);
2362 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2372 if (c_session && c_session->
context && c_session->
tls) {
2376 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2384 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2388 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2389 (
unsigned int)c_session->
mtu),
2390 "gnutls_dtls_set_data_mtu");
2402 const uint8_t *data,
size_t data_len) {
2404 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2406 assert(g_env !=
NULL);
2411 if (g_env->established) {
2412 ret = gnutls_record_send(g_env->g_session, data, data_len);
2416 case GNUTLS_E_AGAIN:
2419 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2421 g_env->sent_alert = 1;
2422 log_last_alert(c_session, g_env->g_session);
2428 "returned %d: '%s'\n",
2429 ret, gnutls_strerror(ret));
2438 ret = do_gnutls_handshake(c_session, g_env);
2470 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2473 if (g_env && g_env->g_session) {
2474 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2487 g_env->last_timeout = now;
2488 return now + rem_ms;
2500 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2503 g_env->doing_dtls_timeout = 1;
2505 (do_gnutls_handshake(c_session, g_env) < 0)) {
2507 g_env->doing_dtls_timeout = 0;
2511 g_env->doing_dtls_timeout = 0;
2524 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2526 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2530 assert(g_env !=
NULL);
2532 if (ssl_data->pdu_len)
2533 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2535 ssl_data->pdu = data;
2536 ssl_data->pdu_len = data_len;
2539 if (g_env->established) {
2543 gnutls_transport_set_ptr(g_env->g_session, c_session);
2546 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2551 }
else if (ret == 0) {
2555 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2557 g_env->sent_alert = 1;
2558 log_last_alert(c_session, g_env->g_session);
2562 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2563 log_last_alert(c_session, g_env->g_session);
2568 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2574 ret = do_gnutls_handshake(c_session, g_env);
2579 if (ssl_data->pdu_len && !g_env->sent_alert) {
2581 ret = do_gnutls_handshake(c_session, g_env);
2601 if (ssl_data && ssl_data->pdu_len) {
2603 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2604 ssl_data->pdu_len = 0;
2605 ssl_data->pdu =
NULL;
2610#if COAP_SERVER_SUPPORT
2618 const uint8_t *data,
2621 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2622 coap_ssl_t *ssl_data;
2626 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2628 c_session->
tls = g_env;
2629 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2630 GNUTLS_COOKIE_KEY_SIZE);
2637 gnutls_dtls_prestate_st prestate;
2640 memset(&prestate, 0,
sizeof(prestate));
2642 memcpy(&data_rw, &data,
sizeof(data_rw));
2643 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2650 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2658 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2661 ssl_data = &g_env->coap_ssl_data;
2662 ssl_data->pdu = data;
2663 ssl_data->pdu_len = data_len;
2665 ret = do_gnutls_handshake(c_session, g_env);
2672 g_env, COAP_FREE_BYE_NONE);
2681 if (ssl_data && ssl_data->pdu_len) {
2683 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2684 ssl_data->pdu_len = 0;
2685 ssl_data->pdu =
NULL;
2696#if !COAP_DISABLE_TCP
2704coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2726coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2735 (errno == EPIPE || errno == ECONNRESET)) {
2761#if COAP_CLIENT_SUPPORT
2764 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2765 coap_gnutls_context_t *g_context =
2767#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2768 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2770 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2777 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2779 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2781 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2782 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2783 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2785 gnutls_transport_set_ptr(g_env->g_session, c_session);
2787 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2788 setup_client_ssl_session(c_session, g_env);
2790 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2792 c_session->
tls = g_env;
2793 ret = do_gnutls_handshake(c_session, g_env);
2807#if COAP_SERVER_SUPPORT
2810 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2811 coap_gnutls_context_t *g_context =
2813#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2814 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2816 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2822 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2824 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2826 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2827 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2828 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2830 gnutls_transport_set_ptr(g_env->g_session, c_session);
2832 setup_server_ssl_session(c_session, g_env);
2834 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2835 gnutls_handshake_set_timeout(g_env->g_session,
2836 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2838 c_session->
tls = g_env;
2839 ret = do_gnutls_handshake(c_session, g_env);
2866 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2868 assert(g_env !=
NULL);
2871 if (g_env->established) {
2872 ret = gnutls_record_send(g_env->g_session, data, data_len);
2876 case GNUTLS_E_AGAIN:
2879 case GNUTLS_E_PUSH_ERROR:
2880 case GNUTLS_E_PULL_ERROR:
2881 case GNUTLS_E_PREMATURE_TERMINATION:
2884 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2886 g_env->sent_alert = 1;
2887 log_last_alert(c_session, g_env->g_session);
2892 "returned %d: '%s'\n",
2893 ret, gnutls_strerror(ret));
2902 ret = do_gnutls_handshake(c_session, g_env);
2925 if (ret == (ssize_t)data_len)
2942 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2951 if (!g_env->established && !g_env->sent_alert) {
2952 ret = do_gnutls_handshake(c_session, g_env);
2961 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2967 case GNUTLS_E_AGAIN:
2971 case GNUTLS_E_PULL_ERROR:
2974 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2976 g_env->sent_alert = 1;
2977 log_last_alert(c_session, g_env->g_session);
2980 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2981 log_last_alert(c_session, g_env->g_session);
2986 "returned %d: '%s'\n",
2987 ret, gnutls_strerror(ret));
3012#if COAP_SERVER_SUPPORT
3014coap_digest_setup(
void) {
3015 gnutls_hash_hd_t digest_ctx;
3017 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
3024coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3026 gnutls_hash_deinit(digest_ctx,
NULL);
3030coap_digest_update(coap_digest_ctx_t *digest_ctx,
3031 const uint8_t *data,
3033 int ret = gnutls_hash(digest_ctx, data, data_len);
3039coap_digest_final(coap_digest_ctx_t *digest_ctx,
3040 coap_digest_t *digest_buffer) {
3041 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
3043 coap_digest_free(digest_ctx);
3053static struct hash_algs {
3055 gnutls_digest_algorithm_t dig_type;
3063static gnutls_digest_algorithm_t
3064get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3067 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3068 if (hashs[idx].alg == alg) {
3069 *hash_len = hashs[idx].dig_size;
3070 return hashs[idx].dig_type;
3073 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3074 return GNUTLS_DIG_UNKNOWN;
3082 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3083 gnutls_hash_hd_t digest_ctx;
3087 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3088 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3092 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3095 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
3102 gnutls_hash_output(digest_ctx,
dummy->s);
3105 gnutls_hash_deinit(digest_ctx,
NULL);
3110 gnutls_hash_deinit(digest_ctx,
NULL);
3115#if COAP_OSCORE_SUPPORT
3126static struct cipher_algs {
3128 gnutls_cipher_algorithm_t cipher_type;
3133static gnutls_cipher_algorithm_t
3137 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3138 if (ciphers[idx].alg == alg)
3139 return ciphers[idx].cipher_type;
3141 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3150static struct hmac_algs {
3152 gnutls_mac_algorithm_t hmac_type;
3158static gnutls_mac_algorithm_t
3162 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3163 if (hmacs[idx].hmac_alg == hmac_alg)
3164 return hmacs[idx].hmac_type;
3166 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3172 return get_cipher_alg(alg);
3181 return get_hmac_alg(hmac_alg);
3189 size_t *max_result_len) {
3190 gnutls_aead_cipher_hd_t ctx;
3194 size_t result_len = *max_result_len;
3195 gnutls_cipher_algorithm_t algo;
3197 uint8_t *key_data_rw;
3203 assert(params !=
NULL);
3207 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3208 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3212 tag_size = gnutls_cipher_get_tag_size(algo);
3216 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3217 key.data = key_data_rw;
3227 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3229 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3239 "gnutls_aead_cipher_encrypt");
3240 *max_result_len = result_len;
3243 gnutls_aead_cipher_deinit(ctx);
3244 return ret == 1 ? 1 : 0;
3252 size_t *max_result_len) {
3253 gnutls_aead_cipher_hd_t ctx;
3257 size_t result_len = *max_result_len;
3258 gnutls_cipher_algorithm_t algo;
3260 uint8_t *key_data_rw;
3266 assert(params !=
NULL);
3271 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3272 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3276 tag_size = gnutls_cipher_get_tag_size(algo);
3280 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3281 key.data = key_data_rw;
3291 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3293 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3303 "gnutls_aead_cipher_decrypt");
3304 *max_result_len = result_len;
3307 gnutls_aead_cipher_deinit(ctx);
3308 return ret == 1 ? 1 : 0;
3316 gnutls_hmac_hd_t ctx;
3319 gnutls_mac_algorithm_t mac_algo;
3325 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3326 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3329 len = gnutls_hmac_get_len(mac_algo);
3336 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3337 "gnutls_hmac_init");
3338 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3339 gnutls_hmac_output(ctx,
dummy->s);
3345 gnutls_hmac_deinit(ctx,
NULL);
3346 return ret == 1 ? 1 : 0;
3357#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.
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.
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.
#define COAP_DTLS_RETRANSMIT_MS
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_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
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.
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.
#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.
#define COAP_DTLS_RETRANSMIT_TOTAL_MS
@ 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.
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_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.
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_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