53#ifdef COAP_WITH_LIBGNUTLS
55#define MIN_GNUTLS_VERSION "3.3.0"
58#include <gnutls/gnutls.h>
59#include <gnutls/x509.h>
60#include <gnutls/dtls.h>
61#include <gnutls/pkcs11.h>
62#include <gnutls/crypto.h>
63#include <gnutls/abstract.h>
65#if (GNUTLS_VERSION_NUMBER >= 0x030606)
66#define COAP_GNUTLS_KEY_RPK GNUTLS_KEY_DIGITAL_SIGNATURE | \
67 GNUTLS_KEY_NON_REPUDIATION | \
68 GNUTLS_KEY_KEY_ENCIPHERMENT | \
69 GNUTLS_KEY_DATA_ENCIPHERMENT | \
70 GNUTLS_KEY_KEY_AGREEMENT | \
71 GNUTLS_KEY_KEY_CERT_SIGN
75#define GNUTLS_CRT_RAW GNUTLS_CRT_RAWPK
79#define strcasecmp _stricmp
82typedef struct coap_ssl_t {
86 gnutls_datum_t cookie_key;
94typedef struct coap_gnutls_env_t {
95 gnutls_session_t g_session;
96 gnutls_psk_client_credentials_t psk_cl_credentials;
97 gnutls_psk_server_credentials_t psk_sv_credentials;
98 gnutls_certificate_credentials_t pki_credentials;
99 coap_ssl_t coap_ssl_data;
102 int doing_dtls_timeout;
107#define IS_PSK (1 << 0)
108#define IS_PKI (1 << 1)
109#define IS_CLIENT (1 << 6)
110#define IS_SERVER (1 << 7)
112typedef struct pki_sni_entry {
115 gnutls_certificate_credentials_t pki_credentials;
118typedef struct psk_sni_entry {
121 gnutls_psk_server_credentials_t psk_credentials;
124typedef struct coap_gnutls_context_t {
127 size_t pki_sni_count;
128 pki_sni_entry *pki_sni_entry_list;
129 size_t psk_sni_count;
130 psk_sni_entry *psk_sni_entry_list;
131 gnutls_datum_t alpn_proto;
134 gnutls_priority_t priority_cache;
135} coap_gnutls_context_t;
137typedef enum coap_free_bye_t {
138 COAP_FREE_BYE_AS_TCP,
139 COAP_FREE_BYE_AS_UDP,
143#define VARIANTS_3_6_6 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL:+SHA256"
144#define VARIANTS_3_5_5 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8"
145#define VARIANTS_BASE "NORMAL:+ECDHE-PSK:+PSK"
147#define VARIANTS_NO_TLS13_3_6_6 VARIANTS_3_6_6 ":-VERS-TLS1.3"
148#define VARIANTS_NO_TLS13_3_6_4 VARIANTS_3_5_5 ":-VERS-TLS1.3"
150#define G_ACTION(xx) do { \
152 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
154#define G_CHECK(xx,func) do { \
155 if ((ret = (xx)) < 0) { \
156 coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \
161#define G_ACTION_CHECK(xx,func) do { \
168#if COAP_SERVER_SUPPORT
169static int post_client_hello_gnutls_pki(gnutls_session_t g_session);
170static int post_client_hello_gnutls_psk(gnutls_session_t g_session);
171static int psk_server_callback(gnutls_session_t g_session,
172 const char *identity,
173 gnutls_datum_t *key);
182 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
183 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
196 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
197 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
239#if (GNUTLS_VERSION_NUMBER >= 0x030606)
249 const char *vers = gnutls_check_version(NULL);
255 sscanf(vers,
"%d.%d.%d", &p1, &p2, &p3);
256 version.
version = (p1 << 16) | (p2 << 8) | p3;
264coap_gnutls_audit_log_func(gnutls_session_t g_session,
const char *text) {
265#if COAP_MAX_LOGGING_LEVEL > 0
281coap_gnutls_log_func(
int level,
const char *text) {
298 coap_gnutls_context_t *g_context =
301 if (!g_context || !setup_data)
304 g_context->setup_data = *setup_data;
305 if (!g_context->setup_data.verify_peer_cert) {
307 g_context->setup_data.check_common_ca = 0;
308 if (g_context->setup_data.is_rpk_not_cert) {
310 g_context->setup_data.allow_self_signed = 0;
311 g_context->setup_data.allow_expired_certs = 0;
312 g_context->setup_data.cert_chain_validation = 0;
313 g_context->setup_data.cert_chain_verify_depth = 0;
314 g_context->setup_data.check_cert_revocation = 0;
315 g_context->setup_data.allow_no_crl = 0;
316 g_context->setup_data.allow_expired_crl = 0;
317 g_context->setup_data.allow_bad_md_hash = 0;
318 g_context->setup_data.allow_short_rsa_length = 0;
321 g_context->setup_data.allow_self_signed = 1;
322 g_context->setup_data.allow_expired_certs = 1;
323 g_context->setup_data.cert_chain_validation = 1;
324 g_context->setup_data.cert_chain_verify_depth = 10;
325 g_context->setup_data.check_cert_revocation = 1;
326 g_context->setup_data.allow_no_crl = 1;
327 g_context->setup_data.allow_expired_crl = 1;
328 g_context->setup_data.allow_bad_md_hash = 1;
329 g_context->setup_data.allow_short_rsa_length = 1;
332 g_context->psk_pki_enabled |= IS_PKI;
343 const char *ca_path) {
344 coap_gnutls_context_t *g_context =
347 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
352 if (ca_file == NULL && ca_path == NULL) {
353 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
357 if (g_context->root_ca_file) {
358 gnutls_free(g_context->root_ca_file);
359 g_context->root_ca_file = NULL;
362 g_context->root_ca_file = gnutls_strdup(ca_file);
364 if (g_context->root_ca_path) {
365 gnutls_free(g_context->root_ca_path);
366 g_context->root_ca_path = NULL;
369#if (GNUTLS_VERSION_NUMBER >= 0x030306)
370 g_context->root_ca_path = gnutls_strdup(ca_path);
372 coap_log_err(
"ca_path not supported in GnuTLS < 3.3.6\n");
378#if COAP_SERVER_SUPPORT
387 coap_gnutls_context_t *g_context =
390 if (!g_context || !setup_data)
393 g_context->psk_pki_enabled |= IS_PSK;
398#if COAP_CLIENT_SUPPORT
407 coap_gnutls_context_t *g_context =
410 if (!g_context || !setup_data)
413 g_context->psk_pki_enabled |= IS_PSK;
424 coap_gnutls_context_t *g_context =
426 return g_context->psk_pki_enabled ? 1 : 0;
431 gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func);
432 gnutls_global_set_log_function(coap_gnutls_log_func);
445 if (c_session && c_session->
tls) {
446 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
448 return g_env->g_session;
475 coap_gnutls_context_t *g_context =
476 (coap_gnutls_context_t *)
477 gnutls_malloc(
sizeof(coap_gnutls_context_t));
481 const char *priority;
483 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
484 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
485 g_context->alpn_proto.data = gnutls_malloc(4);
486 if (g_context->alpn_proto.data) {
487 memcpy(g_context->alpn_proto.data,
"coap", 4);
488 g_context->alpn_proto.size = 4;
491 if (tls_version->
version >= 0x030606) {
492 priority = VARIANTS_3_6_6;
493 }
else if (tls_version->
version >= 0x030505) {
494 priority = VARIANTS_3_5_5;
496 priority = VARIANTS_BASE;
498 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
499 if (ret != GNUTLS_E_SUCCESS) {
500 if (ret == GNUTLS_E_INVALID_REQUEST)
501 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
503 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
518 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
520 gnutls_free(g_context->alpn_proto.data);
521 gnutls_free(g_context->root_ca_file);
522 gnutls_free(g_context->root_ca_path);
523 for (i = 0; i < g_context->pki_sni_count; i++) {
524 gnutls_free(g_context->pki_sni_entry_list[i].sni);
525 gnutls_certificate_free_credentials(
526 g_context->pki_sni_entry_list[i].pki_credentials);
528 if (g_context->pki_sni_entry_list)
529 gnutls_free(g_context->pki_sni_entry_list);
531 for (i = 0; i < g_context->psk_sni_count; i++) {
532 gnutls_free(g_context->psk_sni_entry_list[i].sni);
534 gnutls_psk_free_server_credentials(
535 g_context->psk_sni_entry_list[i].psk_credentials);
537 if (g_context->psk_sni_entry_list)
538 gnutls_free(g_context->psk_sni_entry_list);
540 gnutls_priority_deinit(g_context->priority_cache);
542 gnutls_global_deinit();
543 gnutls_free(g_context);
546#if COAP_CLIENT_SUPPORT
555psk_client_callback(gnutls_session_t g_session,
556 char **username, gnutls_datum_t *key) {
559 coap_gnutls_context_t *g_context;
561 const char *hint = gnutls_psk_client_get_hint(g_session);
571 if (c_session == NULL)
575 if (g_context == NULL)
580 temp.
s = hint ? (
const uint8_t *)hint : (
const uint8_t *)
"";
581 temp.
length = strlen((
const char *)temp.
s);
585 (
const char *)temp.
s);
597 if (cpsk_info == NULL)
602 psk_identity = &cpsk_info->
identity;
603 psk_key = &cpsk_info->
key;
609 if (psk_identity == NULL || psk_key == NULL) {
614 *username = gnutls_malloc(psk_identity->
length+1);
615 if (*username == NULL)
617 memcpy(*username, psk_identity->
s, psk_identity->
length);
618 (*username)[psk_identity->
length] =
'\000';
620 key->data = gnutls_malloc(psk_key->
length);
621 if (key->data == NULL) {
622 gnutls_free(*username);
626 memcpy(key->data, psk_key->
s, psk_key->
length);
627 key->size = psk_key->
length;
633 gnutls_certificate_type_t certificate_type;
635 const gnutls_datum_t *cert_list;
636 unsigned int cert_list_size;
638} coap_gnutls_certificate_info_t;
644static gnutls_certificate_type_t
645get_san_or_cn(gnutls_session_t g_session,
646 coap_gnutls_certificate_info_t *cert_info) {
647 gnutls_x509_crt_t cert;
654#if (GNUTLS_VERSION_NUMBER >= 0x030606)
655 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
658 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
661 cert_info->san_or_cn = NULL;
663 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
664 &cert_info->cert_list_size);
665 if (cert_info->cert_list_size == 0) {
666 return GNUTLS_CRT_UNKNOWN;
669 if (cert_info->certificate_type != GNUTLS_CRT_X509)
670 return cert_info->certificate_type;
672 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
675 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
676 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
678 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
680 size =
sizeof(dn) -1;
682 ret = gnutls_x509_crt_get_subject_alt_name(cert, 0, dn, &size, NULL);
685 gnutls_x509_crt_deinit(cert);
686 cert_info->san_or_cn = gnutls_strdup(dn);
687 return cert_info->certificate_type;
691 G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size),
"gnutls_x509_crt_get_dn");
693 gnutls_x509_crt_deinit(cert);
699 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
700 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
709 char *ecn = strchr(cn,
',');
713 cert_info->san_or_cn = gnutls_strdup(cn);
714 return cert_info->certificate_type;
716 return GNUTLS_CRT_UNKNOWN;
719 return GNUTLS_CRT_UNKNOWN;
722#if (GNUTLS_VERSION_NUMBER >= 0x030606)
723#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
724 cert_info.san_or_cn : \
725 cert_type == GNUTLS_CRT_RAW ? \
726 COAP_DTLS_RPK_CERT_CN : "?")
728#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
729 cert_info.san_or_cn : "?")
732#if (GNUTLS_VERSION_NUMBER >= 0x030606)
734check_rpk_cert(coap_gnutls_context_t *g_context,
735 coap_gnutls_certificate_info_t *cert_info,
739 if (g_context->setup_data.validate_cn_call_back) {
740 gnutls_pcert_st pcert;
744 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
745 GNUTLS_X509_FMT_DER, 0, 0),
746 "gnutls_pcert_import_rawpk_raw");
749 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
750 "gnutls_pubkey_export");
751 gnutls_pcert_deinit(&pcert);
758 g_context->setup_data.cn_call_back_arg)) {
773cert_verify_gnutls(gnutls_session_t g_session) {
774 unsigned int status = 0;
775 unsigned int fail = 0;
778 coap_gnutls_context_t *g_context =
780 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
781 int alert = GNUTLS_A_BAD_CERTIFICATE;
783 coap_gnutls_certificate_info_t cert_info;
784 gnutls_certificate_type_t cert_type;
786 memset(&cert_info, 0,
sizeof(cert_info));
787 cert_type = get_san_or_cn(g_session, &cert_info);
788#if (GNUTLS_VERSION_NUMBER >= 0x030606)
789 if (cert_type == GNUTLS_CRT_RAW) {
790 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
791 alert = GNUTLS_A_ACCESS_DENIED;
798 if (cert_info.cert_list_size == 0 && !g_context->setup_data.verify_peer_cert)
801 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
802 "gnutls_certificate_verify_peers");
805 status &= ~(GNUTLS_CERT_INVALID);
806 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
807 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
808 if (g_context->setup_data.allow_expired_certs) {
811 "The certificate has an invalid usage date",
817 "The certificate has an invalid usage date",
821 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
822 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
823 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
824 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
825 if (g_context->setup_data.allow_expired_crl) {
828 "The certificate's CRL entry has an invalid usage date",
834 "The certificate's CRL entry has an invalid usage date",
838 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
839 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
840 if (cert_info.self_signed) {
841 if (g_context->setup_data.allow_self_signed &&
842 !g_context->setup_data.check_common_ca) {
849 alert = GNUTLS_A_UNKNOWN_CA;
856 if (!g_context->setup_data.verify_peer_cert) {
859 "The peer certificate's CA is unknown",
863 alert = GNUTLS_A_UNKNOWN_CA;
866 "The peer certificate's CA is unknown",
874 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
876 status, OUTPUT_CERT_NAME);
883 if (g_context->setup_data.validate_cn_call_back) {
884 gnutls_x509_crt_t cert;
889 const int cert_is_trusted = !status;
891 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
894 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
895 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
898 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
899 "gnutls_x509_crt_export");
900 gnutls_x509_crt_deinit(cert);
901 if (!g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
907 g_context->setup_data.cn_call_back_arg)) {
908 alert = GNUTLS_A_ACCESS_DENIED;
913 if (g_context->setup_data.additional_tls_setup_call_back) {
915 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
916 &g_context->setup_data)) {
922 if (cert_info.san_or_cn)
923 gnutls_free(cert_info.san_or_cn);
928 if (cert_info.san_or_cn)
929 gnutls_free(cert_info.san_or_cn);
931 if (!g_env->sent_alert) {
932 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
933 g_env->sent_alert = 1;
947cert_verify_callback_gnutls(gnutls_session_t g_session) {
948 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
949 if (cert_verify_gnutls(g_session) == 0) {
957#define min(a,b) ((a) < (b) ? (a) : (b))
961pin_callback(
void *user_data,
int attempt,
978#if (GNUTLS_VERSION_NUMBER >= 0x030606)
980static const unsigned char cert_asn1_header1[] = {
984 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
989 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07,
991static const unsigned char cert_asn1_header2[] = {
996static gnutls_datum_t *
997get_asn1_spki(
const uint8_t *data,
size_t size) {
1000 gnutls_datum_t *spki = NULL;
1002 if (pub_key && prime) {
1003 size_t header_size =
sizeof(cert_asn1_header1) +
1006 sizeof(cert_asn1_header2);
1007 uint8_t *tmp = gnutls_malloc(
sizeof(gnutls_datum_t) +
1012 spki = (gnutls_datum_t *)tmp;
1013 spki->data = &tmp[
sizeof(gnutls_datum_t)];
1014 memcpy(&spki->data[header_size], pub_key->
s, pub_key->
length);
1015 memcpy(spki->data, cert_asn1_header1,
sizeof(cert_asn1_header1));
1017 spki->data[
sizeof(cert_asn1_header1)+1] = prime->
length;
1018 memcpy(&spki->data[
sizeof(cert_asn1_header1)+2],
1020 memcpy(&spki->data[
sizeof(cert_asn1_header1)+2+prime->
length],
1021 cert_asn1_header2,
sizeof(cert_asn1_header2));
1022 spki->size = header_size + pub_key->
length;
1038setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1039 gnutls_session_t g_session,
1040 coap_gnutls_context_t *g_context,
1044 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1045 "gnutls_certificate_allocate_credentials");
1054 coap_log_warn(
"RPK keys cannot be in COAP_PKI_KEY_PEM format\n");
1055 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1057 G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials,
1060 GNUTLS_X509_FMT_PEM),
1061 "gnutls_certificate_set_x509_key_file");
1064 coap_log_err(
"***setup_pki: (D)TLS: No %s Certificate + Private "
1067 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1071 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1073 GNUTLS_X509_FMT_PEM);
1075 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: No certificates found\n");
1076 }
else if (ret < 0) {
1078 "gnutls_certificate_set_x509_trust_file",
1079 gnutls_strerror(ret));
1090 gnutls_datum_t cert;
1092 int alloced_cert_memory = 0;
1093 int alloced_key_memory = 0;
1098 alloced_cert_memory = 1;
1099 cert.data = gnutls_malloc(cert.size + 1);
1102 return GNUTLS_E_MEMORY_ERROR;
1106 cert.data[cert.size] =
'\000';
1116 alloced_key_memory = 1;
1117 key.data = gnutls_malloc(key.size + 1);
1120 if (alloced_cert_memory)
1121 gnutls_free(cert.data);
1122 return GNUTLS_E_MEMORY_ERROR;
1125 key.data[key.size] =
'\000';
1133#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1134 int have_done_key = 0;
1135 if (strstr((
char *)key.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1136 gnutls_datum_t der_private;
1138 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &key,
1139 &der_private) == 0) {
1140 gnutls_datum_t *spki = get_asn1_spki(der_private.data,
1144 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1147 GNUTLS_X509_FMT_DER, NULL,
1148 COAP_GNUTLS_KEY_RPK,
1155 gnutls_free(der_private.data);
1158 if (!have_done_key) {
1159 G_CHECK(gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1162 GNUTLS_X509_FMT_PEM, NULL,
1163 COAP_GNUTLS_KEY_RPK,
1165 "gnutls_certificate_set_rawpk_key_mem");
1168 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1169 if (alloced_cert_memory)
1170 gnutls_free(cert.data);
1171 if (alloced_key_memory)
1172 gnutls_free(key.data);
1173 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1176 G_CHECK(gnutls_certificate_set_x509_key_mem(*pki_credentials,
1179 GNUTLS_X509_FMT_PEM),
1180 "gnutls_certificate_set_x509_key_mem");
1182 if (alloced_cert_memory)
1183 gnutls_free(cert.data);
1184 if (alloced_key_memory)
1185 gnutls_free(key.data);
1187 coap_log_err(
"***setup_pki: (D)TLS: No Server Certificate + Private "
1189 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1194 int alloced_ca_memory = 0;
1199 alloced_ca_memory = 1;
1200 ca.data = gnutls_malloc(ca.size + 1);
1203 return GNUTLS_E_MEMORY_ERROR;
1206 ca.data[ca.size] =
'\000';
1213 ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1215 GNUTLS_X509_FMT_PEM);
1217 coap_log_warn(
"gnutls_certificate_set_x509_trust_mem: No certificates found\n");
1218 }
else if (ret < 0) {
1220 "gnutls_certificate_set_x509_trust_mem",
1221 gnutls_strerror(ret));
1222 if (alloced_ca_memory)
1223 gnutls_free(ca.data);
1226 if (alloced_ca_memory)
1227 gnutls_free(ca.data);
1236 gnutls_datum_t cert;
1247#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1248 int have_done_key = 0;
1251 gnutls_datum_t *spki = get_asn1_spki(key.data,
1255 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1258 GNUTLS_X509_FMT_DER, NULL,
1259 COAP_GNUTLS_KEY_RPK,
1267 if (!have_done_key) {
1268 G_CHECK(gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1271 GNUTLS_X509_FMT_DER, NULL,
1272 COAP_GNUTLS_KEY_RPK,
1274 "gnutls_certificate_set_rawpk_key_mem");
1277 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1278 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1281 G_CHECK(gnutls_certificate_set_x509_key_mem(*pki_credentials,
1284 GNUTLS_X509_FMT_DER),
1285 "gnutls_certificate_set_x509_key_mem");
1288 coap_log_err(
"***setup_pki: (D)TLS: No %s Certificate + Private "
1291 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1295 gnutls_datum_t ca_cert;
1299 sizeof(ca_cert.data));
1301 ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1303 GNUTLS_X509_FMT_DER);
1305 coap_log_warn(
"gnutls_certificate_set_x509_trust_mem: No certificates found\n");
1306 }
else if (ret < 0) {
1308 "gnutls_certificate_set_x509_trust_mem",
1309 gnutls_strerror(ret));
1321 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1323#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1324 G_CHECK(gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1327 GNUTLS_X509_FMT_PEM, NULL,
1328 COAP_GNUTLS_KEY_RPK,
1329 NULL, 0, GNUTLS_PKCS_PLAIN, 0),
1330 "gnutls_certificate_set_rawpk_key_file");
1332 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1333 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1336 G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials,
1339 GNUTLS_X509_FMT_DER),
1340 "gnutls_certificate_set_x509_key_file");
1343 coap_log_err(
"***setup_pki: (D)TLS: No %s Certificate + Private "
1346 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1350 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1352 GNUTLS_X509_FMT_DER);
1354 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: No certificates found\n");
1355 }
else if (ret < 0) {
1357 "gnutls_certificate_set_x509_trust_file",
1358 gnutls_strerror(ret));
1365 coap_log_err(
"***setup_pki: (D)TLS: Unknown key type %d\n",
1367 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1370 if (g_context->root_ca_file) {
1371 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1372 g_context->root_ca_file,
1373 GNUTLS_X509_FMT_PEM);
1375 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1378 if (g_context->root_ca_path) {
1379#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1380 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1381 g_context->root_ca_path,
1382 GNUTLS_X509_FMT_PEM),
1383 "gnutls_certificate_set_x509_trust_dir");
1386 gnutls_certificate_send_x509_rdn_sequence(g_session,
1388 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1390 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1391 "gnutls_certificate_set_x509_system_trust");
1395 gnutls_certificate_set_verify_function(*pki_credentials,
1396 cert_verify_callback_gnutls);
1400 gnutls_certificate_set_verify_limits(*pki_credentials,
1409 gnutls_certificate_set_verify_flags(*pki_credentials,
1411 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1414 return GNUTLS_E_SUCCESS;
1420#if COAP_SERVER_SUPPORT
1426setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1432 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1433 "gnutls_psk_allocate_server_credentials");
1434 gnutls_psk_set_server_credentials_function(*psk_credentials,
1435 psk_server_callback);
1439 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1440 "gnutls_psk_set_server_credentials_hint");
1443 return GNUTLS_E_SUCCESS;
1454post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1457 coap_gnutls_context_t *g_context =
1459 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1460 int ret = GNUTLS_E_SUCCESS;
1470 name = gnutls_malloc(len);
1472 return GNUTLS_E_MEMORY_ERROR;
1475 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1476 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1478 new_name = gnutls_realloc(name, len);
1479 if (new_name == NULL) {
1480 ret = GNUTLS_E_MEMORY_ERROR;
1488 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1491 if (ret != GNUTLS_E_SUCCESS)
1494 if (type != GNUTLS_NAME_DNS)
1505 for (i = 0; i < g_context->psk_sni_count; i++) {
1506 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1510 if (i == g_context->psk_sni_count) {
1519 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1520 GNUTLS_A_UNRECOGNIZED_NAME));
1521 g_env->sent_alert = 1;
1522 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1526 g_context->psk_sni_entry_list =
1527 gnutls_realloc(g_context->psk_sni_entry_list,
1528 (i+1)*
sizeof(psk_sni_entry));
1529 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1530 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1532 sni_setup_data.
psk_info = *new_entry;
1533 if ((ret = setup_psk_credentials(
1534 &g_context->psk_sni_entry_list[i].psk_credentials,
1536 &sni_setup_data)) < 0) {
1538 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1539 GNUTLS_A_BAD_CERTIFICATE));
1540 g_env->sent_alert = 1;
1544 g_context->psk_sni_count++;
1546 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1547 g_context->psk_sni_entry_list[i].psk_credentials),
1548 "gnutls_credentials_set");
1550 &g_context->psk_sni_entry_list[i].psk_info.hint);
1552 &g_context->psk_sni_entry_list[i].psk_info.key);
1568post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1571 coap_gnutls_context_t *g_context =
1573 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1574 int ret = GNUTLS_E_SUCCESS;
1577 if (g_context->setup_data.validate_sni_call_back) {
1584 name = gnutls_malloc(len);
1586 return GNUTLS_E_MEMORY_ERROR;
1589 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1590 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1592 new_name = gnutls_realloc(name, len);
1593 if (new_name == NULL) {
1594 ret = GNUTLS_E_MEMORY_ERROR;
1602 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1605 if (ret != GNUTLS_E_SUCCESS)
1608 if (type != GNUTLS_NAME_DNS)
1619 for (i = 0; i < g_context->pki_sni_count; i++) {
1620 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1624 if (i == g_context->pki_sni_count) {
1629 g_context->setup_data.validate_sni_call_back(name,
1630 g_context->setup_data.sni_call_back_arg);
1632 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1633 GNUTLS_A_UNRECOGNIZED_NAME));
1634 g_env->sent_alert = 1;
1635 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1639 g_context->pki_sni_entry_list = gnutls_realloc(
1640 g_context->pki_sni_entry_list,
1641 (i+1)*
sizeof(pki_sni_entry));
1642 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1643 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1644 sni_setup_data = g_context->setup_data;
1645 sni_setup_data.
pki_key = *new_entry;
1646 if ((ret = setup_pki_credentials(
1647 &g_context->pki_sni_entry_list[i].pki_credentials,
1652 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1653 GNUTLS_A_BAD_CERTIFICATE));
1654 g_env->sent_alert = 1;
1658 g_context->pki_sni_count++;
1660 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1661 g_context->pki_sni_entry_list[i].pki_credentials),
1662 "gnutls_credentials_set");
1674#if COAP_CLIENT_SUPPORT
1680setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1681 coap_gnutls_context_t *g_context =
1685 g_context->psk_pki_enabled |= IS_CLIENT;
1686 if (g_context->psk_pki_enabled & IS_PSK) {
1688 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1689 "gnutls_psk_allocate_client_credentials");
1690 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1691 psk_client_callback);
1692 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1693 g_env->psk_cl_credentials),
1694 "gnutls_credentials_set");
1697 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1700 "gnutls_server_name_set");
1706 if (tls_version->
version >= 0x030604) {
1708 const char *priority;
1710 if (tls_version->
version >= 0x030606) {
1711 priority = VARIANTS_NO_TLS13_3_6_6;
1713 priority = VARIANTS_NO_TLS13_3_6_4;
1715 ret = gnutls_priority_set_direct(g_env->g_session,
1718 if (ret == GNUTLS_E_INVALID_REQUEST)
1719 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1721 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1728 if ((g_context->psk_pki_enabled & IS_PKI) ||
1729 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1735 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1736 g_context, setup_data,
1738 "setup_pki_credentials");
1740 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1741 g_env->pki_credentials),
1742 "gnutls_credentials_set");
1745 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1746 &g_context->alpn_proto, 1, 0),
1747 "gnutls_alpn_set_protocols");
1751 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1754 "gnutls_server_name_set");
1757 return GNUTLS_E_SUCCESS;
1764#if COAP_SERVER_SUPPORT
1773psk_server_callback(gnutls_session_t g_session,
1774 const char *identity,
1775 gnutls_datum_t *key) {
1778 coap_gnutls_context_t *g_context;
1783 if (c_session == NULL)
1787 if (g_context == NULL)
1793 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
1794 lidentity.
length = strlen((
const char *)lidentity.
s);
1798 (
int)lidentity.
length, (
const char *)lidentity.
s);
1810 if (psk_key == NULL)
1813 key->data = gnutls_malloc(psk_key->
length);
1814 if (key->data == NULL)
1816 memcpy(key->data, psk_key->
s, psk_key->
length);
1817 key->size = psk_key->
length;
1826setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1827 coap_gnutls_context_t *g_context =
1829 int ret = GNUTLS_E_SUCCESS;
1831 g_context->psk_pki_enabled |= IS_SERVER;
1832 if (g_context->psk_pki_enabled & IS_PSK) {
1833 G_CHECK(setup_psk_credentials(
1834 &g_env->psk_sv_credentials,
1837 "setup_psk_credentials\n");
1838 G_CHECK(gnutls_credentials_set(g_env->g_session,
1840 g_env->psk_sv_credentials),
1841 "gnutls_credentials_set\n");
1842 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1843 post_client_hello_gnutls_psk);
1846 if (g_context->psk_pki_enabled & IS_PKI) {
1848 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1849 g_context, setup_data,
1851 "setup_pki_credentials");
1854 gnutls_certificate_server_set_request(g_env->g_session,
1855 GNUTLS_CERT_REQUIRE);
1857 gnutls_certificate_server_set_request(g_env->g_session,
1858 GNUTLS_CERT_REQUEST);
1860 gnutls_certificate_server_set_request(g_env->g_session,
1861 GNUTLS_CERT_IGNORE);
1864 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1865 post_client_hello_gnutls_pki);
1867 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1868 g_env->pki_credentials),
1869 "gnutls_credentials_set\n");
1871 return GNUTLS_E_SUCCESS;
1884coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
1889 if (!c_session->
tls) {
1893 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
1896 if (data != NULL && data->pdu_len > 0) {
1897 if (outl < data->pdu_len) {
1898 memcpy(out, data->pdu, outl);
1900 if (!data->peekmode) {
1902 data->pdu_len -= outl;
1905 memcpy(out, data->pdu, data->pdu_len);
1906 ret = data->pdu_len;
1907 if (!data->peekmode) {
1927coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
1928 size_t send_buffer_length) {
1929 ssize_t result = -1;
1934#if COAP_SERVER_SUPPORT
1943 send_buffer, send_buffer_length);
1944 if (result != (
int)send_buffer_length) {
1945 coap_log_warn(
"coap_netif_dgrm_write failed (%zd != %zu)\n",
1946 result, send_buffer_length);
1961receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
1965 fd_set readfds, writefds, exceptfds;
1967 int nfds = c_session->
sock.
fd +1;
1968 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1972 g_env->coap_ssl_data.pdu_len > 0) {
1978 FD_ZERO(&exceptfds);
1979 FD_SET(c_session->
sock.
fd, &readfds);
1980 if (!(g_env && g_env->doing_dtls_timeout)) {
1981 FD_SET(c_session->
sock.
fd, &writefds);
1982 FD_SET(c_session->
sock.
fd, &exceptfds);
1988 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
1993static coap_gnutls_env_t *
1995 coap_gnutls_context_t *g_context =
1997 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1998#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1999 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2001 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2008 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2012 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2014 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2016 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2017 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2018 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2020 gnutls_transport_set_ptr(g_env->g_session, c_session);
2022 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2023 "gnutls_priority_set");
2025 if (type == GNUTLS_SERVER) {
2026#if COAP_SERVER_SUPPORT
2027 G_CHECK(setup_server_ssl_session(c_session, g_env),
2028 "setup_server_ssl_session");
2033#if COAP_CLIENT_SUPPORT
2034 G_CHECK(setup_client_ssl_session(c_session, g_env),
2035 "setup_client_ssl_session");
2041 gnutls_handshake_set_timeout(g_env->g_session,
2042 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2043 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2044 COAP_DTLS_RETRANSMIT_TOTAL_MS);
2055coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2056 coap_gnutls_env_t *g_env,
2057 coap_free_bye_t free_bye) {
2062 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2064 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2065 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2067 gnutls_deinit(g_env->g_session);
2068 g_env->g_session = NULL;
2069 if (g_context->psk_pki_enabled & IS_PSK) {
2070 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2071 g_env->psk_cl_credentials != NULL) {
2072 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2073 g_env->psk_cl_credentials = NULL;
2076 if (g_env->psk_sv_credentials != NULL)
2077 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2078 g_env->psk_sv_credentials = NULL;
2081 if ((g_context->psk_pki_enabled & IS_PKI) ||
2082 (g_context->psk_pki_enabled &
2083 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2084 gnutls_certificate_free_credentials(g_env->pki_credentials);
2085 g_env->pki_credentials = NULL;
2087 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2092#if COAP_SERVER_SUPPORT
2095 coap_gnutls_env_t *g_env =
2096 (coap_gnutls_env_t *)c_session->
tls;
2098 gnutls_transport_set_ptr(g_env->g_session, c_session);
2106 gnutls_session_t g_session) {
2107#if COAP_MAX_LOGGING_LEVEL > 0
2108 int last_alert = gnutls_alert_get(g_session);
2110 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2113 last_alert, gnutls_alert_get_name(last_alert));
2117 last_alert, gnutls_alert_get_name(last_alert));
2130do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2133 ret = gnutls_handshake(g_env->g_session);
2135 case GNUTLS_E_SUCCESS:
2136 g_env->established = 1;
2141 case GNUTLS_E_INTERRUPTED:
2145 case GNUTLS_E_AGAIN:
2149 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2153 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2155 g_env->sent_alert = 1;
2156 log_last_alert(c_session, g_env->g_session);
2158 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2159 case GNUTLS_E_UNEXPECTED_PACKET:
2163 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2164 log_last_alert(c_session, g_env->g_session);
2168 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2169#if (GNUTLS_VERSION_NUMBER > 0x030606)
2170 case GNUTLS_E_CERTIFICATE_REQUIRED:
2172 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2174 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2175 GNUTLS_A_BAD_CERTIFICATE));
2176 g_env->sent_alert = 1;
2180 case GNUTLS_E_DECRYPTION_FAILED:
2183 gnutls_strerror(ret));
2184 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2185 GNUTLS_A_DECRYPT_ERROR));
2186 g_env->sent_alert = 1;
2190 case GNUTLS_E_CERTIFICATE_ERROR:
2191 if (g_env->sent_alert) {
2197 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2198 case GNUTLS_E_NO_CIPHER_SUITES:
2199 case GNUTLS_E_INVALID_SESSION:
2202 gnutls_strerror(ret));
2203 if (!g_env->sent_alert) {
2204 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2205 GNUTLS_A_HANDSHAKE_FAILURE));
2206 g_env->sent_alert = 1;
2211 case GNUTLS_E_SESSION_EOF:
2212 case GNUTLS_E_PREMATURE_TERMINATION:
2213 case GNUTLS_E_TIMEDOUT:
2214 case GNUTLS_E_PULL_ERROR:
2215 case GNUTLS_E_PUSH_ERROR:
2221 "returned %d: '%s'\n",
2222 ret, gnutls_strerror(ret));
2229#if COAP_CLIENT_SUPPORT
2232 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2236 ret = do_gnutls_handshake(c_session, g_env);
2241 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2251 if (c_session && c_session->
context && c_session->
tls) {
2255 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2256 c_session->
tls = NULL;
2263 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2267 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2268 (
unsigned int)c_session->
mtu),
2269 "gnutls_dtls_set_data_mtu");
2281 const uint8_t *data,
size_t data_len) {
2283 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2285 assert(g_env != NULL);
2288 if (g_env->established) {
2289 ret = gnutls_record_send(g_env->g_session, data, data_len);
2293 case GNUTLS_E_AGAIN:
2296 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2298 g_env->sent_alert = 1;
2299 log_last_alert(c_session, g_env->g_session);
2305 "returned %d: '%s'\n",
2306 ret, gnutls_strerror(ret));
2315 ret = do_gnutls_handshake(c_session, g_env);
2333 if (ret == (ssize_t)data_len)
2355 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2358 if (g_env && g_env->g_session) {
2359 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2372 g_env->last_timeout = now;
2373 return now + rem_ms;
2385 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2388 g_env->doing_dtls_timeout = 1;
2390 (do_gnutls_handshake(c_session, g_env) < 0)) {
2392 g_env->doing_dtls_timeout = 0;
2396 g_env->doing_dtls_timeout = 0;
2409 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2411 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2415 assert(g_env != NULL);
2417 if (ssl_data->pdu_len)
2418 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2420 ssl_data->pdu = data;
2421 ssl_data->pdu_len = data_len;
2424 if (g_env->established) {
2428 gnutls_transport_set_ptr(g_env->g_session, c_session);
2431 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2434 }
else if (ret == 0) {
2438 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2440 g_env->sent_alert = 1;
2441 log_last_alert(c_session, g_env->g_session);
2445 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2446 log_last_alert(c_session, g_env->g_session);
2451 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2457 ret = do_gnutls_handshake(c_session, g_env);
2462 if (ssl_data->pdu_len && !g_env->sent_alert) {
2464 ret = do_gnutls_handshake(c_session, g_env);
2484 if (ssl_data && ssl_data->pdu_len) {
2486 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2487 ssl_data->pdu_len = 0;
2488 ssl_data->pdu = NULL;
2497#if COAP_SERVER_SUPPORT
2505 const uint8_t *data,
2508 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2509 coap_ssl_t *ssl_data;
2513 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2515 c_session->
tls = g_env;
2516 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2517 GNUTLS_COOKIE_KEY_SIZE);
2524 gnutls_dtls_prestate_st prestate;
2527 memset(&prestate, 0,
sizeof(prestate));
2529 memcpy(&data_rw, &data,
sizeof(data_rw));
2530 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2537 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2545 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2548 ssl_data = &g_env->coap_ssl_data;
2549 ssl_data->pdu = data;
2550 ssl_data->pdu_len = data_len;
2552 ret = do_gnutls_handshake(c_session, g_env);
2559 g_env, COAP_FREE_BYE_NONE);
2560 c_session->
tls = NULL;
2568 if (ssl_data && ssl_data->pdu_len) {
2570 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2571 ssl_data->pdu_len = 0;
2572 ssl_data->pdu = NULL;
2583#if !COAP_DISABLE_TCP
2591coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2613coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2622 (errno == EPIPE || errno == ECONNRESET)) {
2648#if COAP_CLIENT_SUPPORT
2651 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2652 coap_gnutls_context_t *g_context =
2654#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2655 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2657 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2664 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2666 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2668 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2669 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2670 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2672 gnutls_transport_set_ptr(g_env->g_session, c_session);
2674 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2675 setup_client_ssl_session(c_session, g_env);
2677 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2679 c_session->
tls = g_env;
2680 ret = do_gnutls_handshake(c_session, g_env);
2694#if COAP_SERVER_SUPPORT
2697 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2698 coap_gnutls_context_t *g_context =
2700#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2701 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2703 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2709 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2711 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2713 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2714 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2715 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2717 gnutls_transport_set_ptr(g_env->g_session, c_session);
2719 setup_server_ssl_session(c_session, g_env);
2721 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2722 gnutls_handshake_set_timeout(g_env->g_session,
2723 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2725 c_session->
tls = g_env;
2726 ret = do_gnutls_handshake(c_session, g_env);
2753 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2755 assert(g_env != NULL);
2758 if (g_env->established) {
2759 ret = gnutls_record_send(g_env->g_session, data, data_len);
2763 case GNUTLS_E_AGAIN:
2766 case GNUTLS_E_PUSH_ERROR:
2767 case GNUTLS_E_PULL_ERROR:
2768 case GNUTLS_E_PREMATURE_TERMINATION:
2771 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2773 g_env->sent_alert = 1;
2774 log_last_alert(c_session, g_env->g_session);
2779 "returned %d: '%s'\n",
2780 ret, gnutls_strerror(ret));
2789 ret = do_gnutls_handshake(c_session, g_env);
2812 if (ret == (ssize_t)data_len)
2829 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2838 if (!g_env->established && !g_env->sent_alert) {
2839 ret = do_gnutls_handshake(c_session, g_env);
2848 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2854 case GNUTLS_E_AGAIN:
2858 case GNUTLS_E_PULL_ERROR:
2861 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2863 g_env->sent_alert = 1;
2864 log_last_alert(c_session, g_env->g_session);
2867 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2868 log_last_alert(c_session, g_env->g_session);
2873 "returned %d: '%s'\n",
2874 ret, gnutls_strerror(ret));
2899#if COAP_SERVER_SUPPORT
2902 gnutls_hash_hd_t digest_ctx;
2904 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
2912 gnutls_hash_deinit(digest_ctx, NULL);
2917 const uint8_t *data,
2919 int ret = gnutls_hash(digest_ctx, data, data_len);
2927 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
2939static struct hash_algs {
2941 gnutls_digest_algorithm_t dig_type;
2949static gnutls_digest_algorithm_t
2950get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
2953 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2954 if (hashs[idx].alg == alg) {
2955 *hash_len = hashs[idx].dig_size;
2956 return hashs[idx].dig_type;
2959 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2960 return GNUTLS_DIG_UNKNOWN;
2968 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
2969 gnutls_hash_hd_t digest_ctx;
2973 if (dig_type == GNUTLS_DIG_UNKNOWN) {
2974 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2978 if (gnutls_hash_init(&digest_ctx, dig_type)) {
2981 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
2988 gnutls_hash_output(digest_ctx,
dummy->s);
2991 gnutls_hash_deinit(digest_ctx, NULL);
2996 gnutls_hash_deinit(digest_ctx, NULL);
3001#if COAP_OSCORE_SUPPORT
3012static struct cipher_algs {
3014 gnutls_cipher_algorithm_t cipher_type;
3019static gnutls_cipher_algorithm_t
3023 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3024 if (ciphers[idx].alg == alg)
3025 return ciphers[idx].cipher_type;
3027 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3036static struct hmac_algs {
3038 gnutls_mac_algorithm_t hmac_type;
3044static gnutls_mac_algorithm_t
3048 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3049 if (hmacs[idx].hmac_alg == hmac_alg)
3050 return hmacs[idx].hmac_type;
3052 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3058 return get_cipher_alg(alg);
3067 return get_hmac_alg(hmac_alg);
3075 size_t *max_result_len) {
3076 gnutls_aead_cipher_hd_t ctx;
3080 size_t result_len = *max_result_len;
3081 gnutls_cipher_algorithm_t algo;
3083 uint8_t *key_data_rw;
3089 assert(params != NULL);
3093 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3094 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3098 tag_size = gnutls_cipher_get_tag_size(algo);
3102 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3103 key.data = key_data_rw;
3113 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3115 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3125 "gnutls_aead_cipher_encrypt");
3126 *max_result_len = result_len;
3129 gnutls_aead_cipher_deinit(ctx);
3130 return ret == 1 ? 1 : 0;
3138 size_t *max_result_len) {
3139 gnutls_aead_cipher_hd_t ctx;
3143 size_t result_len = *max_result_len;
3144 gnutls_cipher_algorithm_t algo;
3146 uint8_t *key_data_rw;
3152 assert(params != NULL);
3157 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3158 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3162 tag_size = gnutls_cipher_get_tag_size(algo);
3166 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3167 key.data = key_data_rw;
3177 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3179 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3189 "gnutls_aead_cipher_decrypt");
3190 *max_result_len = result_len;
3193 gnutls_aead_cipher_deinit(ctx);
3194 return ret == 1 ? 1 : 0;
3202 gnutls_hmac_hd_t ctx;
3205 gnutls_mac_algorithm_t mac_algo;
3211 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3212 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3215 len = gnutls_hmac_get_len(mac_algo);
3222 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3223 "gnutls_hmac_init");
3224 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3225 gnutls_hmac_output(ctx,
dummy->s);
3231 gnutls_hmac_deinit(ctx, NULL);
3232 return ret == 1 ? 1 : 0;
3243#pragma GCC diagnostic ignored "-Wunused-function"
Pulls together all the internal only header files.
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
static coap_log_t dtls_log_level
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
coap_binary_t * get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, asn1_validate validate)
Get the asn1 tag and data from the current ptr.
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_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_handle_event(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_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 *session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
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_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
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_HINT_LENGTH
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
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.
#define COAP_DTLS_RPK_CERT_CN
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_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_PKCS11
The PKI key type is PKCS11 (DER)
@ COAP_PKI_KEY_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_ASN1
The PKI key type is ASN.1 (DER) buffer.
@ COAP_ASN1_PKEY_EC
EC type.
@ 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_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_oscore_is_supported(void)
Check whether OSCORE is available.
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).
#define COAP_PROTO_NOT_RELIABLE(p)
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ 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.
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.
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.
The structure that holds the PKI key information.
coap_pki_key_pem_t pem
for PEM file keys
coap_pki_key_pkcs11_t pkcs11
for PKCS11 keys
union coap_dtls_key_t::@3 key
coap_pki_key_pem_buf_t pem_buf
for PEM memory keys
coap_pki_key_t key_type
key format type
coap_pki_key_asn1_t asn1
for ASN.1 (DER) memory keys
The structure used for defining the PKI setup data to be used.
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
uint8_t check_cert_revocation
1 if revocation checks wanted
uint8_t cert_chain_verify_depth
recommended depth is 3
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
char * client_sni
If not NULL, SNI to use in client TLS setup.
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
coap_dtls_key_t pki_key
PKI key definition.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
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
const uint8_t * private_key
ASN1 (DER) Private Key.
coap_asn1_privatekey_type_t private_key_type
Private Key Type.
size_t public_cert_len
ASN1 Public Cert length.
size_t private_key_len
ASN1 Private Key length.
const uint8_t * ca_cert
ASN1 (DER) Common CA Cert.
size_t ca_cert_len
ASN1 CA Cert length.
const uint8_t * public_cert
ASN1 (DER) Public Cert, or Public Key if RPK.
size_t ca_cert_len
PEM buffer CA Cert length.
const uint8_t * ca_cert
PEM buffer Common CA Cert.
size_t private_key_len
PEM buffer Private Key length.
const uint8_t * private_key
PEM buffer Private Key If RPK and 'EC PRIVATE KEY' this can be used for both the public_cert and priv...
size_t public_cert_len
PEM buffer Public Cert length.
const uint8_t * public_cert
PEM buffer Public Cert, or Public Key if RPK.
const char * ca_file
File location of Common CA in PEM format.
const char * public_cert
File location of Public Cert.
const char * private_key
File location of Private Key in PEM format.
const char * private_key
pkcs11: URI for Private Key
const char * ca
pkcs11: URI for Common CA Certificate
const char * user_pin
User pin to access PKCS11.
const char * public_cert
pkcs11: URI for Public Cert
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