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);
444 if (c_session && c_session->
tls) {
445 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
447 return g_env->g_session;
474 coap_gnutls_context_t *g_context =
475 (coap_gnutls_context_t *)
476 gnutls_malloc(
sizeof(coap_gnutls_context_t));
480 const char *priority;
482 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
483 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
484 g_context->alpn_proto.data = gnutls_malloc(4);
485 if (g_context->alpn_proto.data) {
486 memcpy(g_context->alpn_proto.data,
"coap", 4);
487 g_context->alpn_proto.size = 4;
490 if (tls_version->
version >= 0x030606) {
491 priority = VARIANTS_3_6_6;
492 }
else if (tls_version->
version >= 0x030505) {
493 priority = VARIANTS_3_5_5;
495 priority = VARIANTS_BASE;
497 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
498 if (ret != GNUTLS_E_SUCCESS) {
499 if (ret == GNUTLS_E_INVALID_REQUEST)
500 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
502 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
517 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
519 gnutls_free(g_context->alpn_proto.data);
520 gnutls_free(g_context->root_ca_file);
521 gnutls_free(g_context->root_ca_path);
522 for (i = 0; i < g_context->pki_sni_count; i++) {
523 gnutls_free(g_context->pki_sni_entry_list[i].sni);
524 gnutls_certificate_free_credentials(
525 g_context->pki_sni_entry_list[i].pki_credentials);
527 if (g_context->pki_sni_entry_list)
528 gnutls_free(g_context->pki_sni_entry_list);
530 for (i = 0; i < g_context->psk_sni_count; i++) {
531 gnutls_free(g_context->psk_sni_entry_list[i].sni);
533 gnutls_psk_free_server_credentials(
534 g_context->psk_sni_entry_list[i].psk_credentials);
536 if (g_context->psk_sni_entry_list)
537 gnutls_free(g_context->psk_sni_entry_list);
539 gnutls_priority_deinit(g_context->priority_cache);
541 gnutls_global_deinit();
542 gnutls_free(g_context);
545#if COAP_CLIENT_SUPPORT
554psk_client_callback(gnutls_session_t g_session,
555 char **username, gnutls_datum_t *key) {
558 coap_gnutls_context_t *g_context;
560 const char *hint = gnutls_psk_client_get_hint(g_session);
570 if (c_session == NULL)
574 if (g_context == NULL)
579 temp.
s = hint ? (
const uint8_t *)hint : (
const uint8_t *)
"";
580 temp.
length = strlen((
const char *)temp.
s);
584 (
const char *)temp.
s);
596 if (cpsk_info == NULL)
601 psk_identity = &cpsk_info->
identity;
602 psk_key = &cpsk_info->
key;
608 if (psk_identity == NULL || psk_key == NULL) {
613 *username = gnutls_malloc(psk_identity->
length+1);
614 if (*username == NULL)
616 memcpy(*username, psk_identity->
s, psk_identity->
length);
617 (*username)[psk_identity->
length] =
'\000';
619 key->data = gnutls_malloc(psk_key->
length);
620 if (key->data == NULL) {
621 gnutls_free(*username);
625 memcpy(key->data, psk_key->
s, psk_key->
length);
626 key->size = psk_key->
length;
632 gnutls_certificate_type_t certificate_type;
634 const gnutls_datum_t *cert_list;
635 unsigned int cert_list_size;
637} coap_gnutls_certificate_info_t;
643static gnutls_certificate_type_t
644get_san_or_cn(gnutls_session_t g_session,
645 coap_gnutls_certificate_info_t *cert_info) {
646 gnutls_x509_crt_t cert;
653#if (GNUTLS_VERSION_NUMBER >= 0x030606)
654 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
657 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
660 cert_info->san_or_cn = NULL;
662 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
663 &cert_info->cert_list_size);
664 if (cert_info->cert_list_size == 0) {
665 return GNUTLS_CRT_UNKNOWN;
668 if (cert_info->certificate_type != GNUTLS_CRT_X509)
669 return cert_info->certificate_type;
671 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
674 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
675 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
677 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
679 size =
sizeof(dn) -1;
681 ret = gnutls_x509_crt_get_subject_alt_name(cert, 0, dn, &size, NULL);
684 gnutls_x509_crt_deinit(cert);
685 cert_info->san_or_cn = gnutls_strdup(dn);
686 return cert_info->certificate_type;
690 G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size),
"gnutls_x509_crt_get_dn");
692 gnutls_x509_crt_deinit(cert);
698 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
699 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
708 char *ecn = strchr(cn,
',');
712 cert_info->san_or_cn = gnutls_strdup(cn);
713 return cert_info->certificate_type;
715 return GNUTLS_CRT_UNKNOWN;
718 return GNUTLS_CRT_UNKNOWN;
721#if (GNUTLS_VERSION_NUMBER >= 0x030606)
722#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
723 cert_info.san_or_cn : \
724 cert_type == GNUTLS_CRT_RAW ? \
725 COAP_DTLS_RPK_CERT_CN : "?")
727#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
728 cert_info.san_or_cn : "?")
731#if (GNUTLS_VERSION_NUMBER >= 0x030606)
733check_rpk_cert(coap_gnutls_context_t *g_context,
734 coap_gnutls_certificate_info_t *cert_info,
738 if (g_context->setup_data.validate_cn_call_back) {
739 gnutls_pcert_st pcert;
743 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
744 GNUTLS_X509_FMT_DER, 0, 0),
745 "gnutls_pcert_import_rawpk_raw");
748 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
749 "gnutls_pubkey_export");
750 gnutls_pcert_deinit(&pcert);
757 g_context->setup_data.cn_call_back_arg)) {
772cert_verify_gnutls(gnutls_session_t g_session) {
773 unsigned int status = 0;
774 unsigned int fail = 0;
777 coap_gnutls_context_t *g_context =
779 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
780 int alert = GNUTLS_A_BAD_CERTIFICATE;
782 coap_gnutls_certificate_info_t cert_info;
783 gnutls_certificate_type_t cert_type;
785 memset(&cert_info, 0,
sizeof(cert_info));
786 cert_type = get_san_or_cn(g_session, &cert_info);
787#if (GNUTLS_VERSION_NUMBER >= 0x030606)
788 if (cert_type == GNUTLS_CRT_RAW) {
789 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
790 alert = GNUTLS_A_ACCESS_DENIED;
797 if (cert_info.cert_list_size == 0 && !g_context->setup_data.verify_peer_cert)
800 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
801 "gnutls_certificate_verify_peers");
804 status &= ~(GNUTLS_CERT_INVALID);
805 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
806 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
807 if (g_context->setup_data.allow_expired_certs) {
810 "The certificate has an invalid usage date",
816 "The certificate has an invalid usage date",
820 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
821 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
822 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
823 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
824 if (g_context->setup_data.allow_expired_crl) {
827 "The certificate's CRL entry has an invalid usage date",
833 "The certificate's CRL entry has an invalid usage date",
837 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
838 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
839 if (cert_info.self_signed) {
840 if (g_context->setup_data.allow_self_signed &&
841 !g_context->setup_data.check_common_ca) {
848 alert = GNUTLS_A_UNKNOWN_CA;
855 if (!g_context->setup_data.verify_peer_cert) {
858 "The peer certificate's CA is unknown",
862 alert = GNUTLS_A_UNKNOWN_CA;
865 "The peer certificate's CA is unknown",
873 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
875 status, OUTPUT_CERT_NAME);
882 if (g_context->setup_data.validate_cn_call_back) {
883 gnutls_x509_crt_t cert;
888 const int cert_is_trusted = !status;
890 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
893 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
894 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
897 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
898 "gnutls_x509_crt_export");
899 gnutls_x509_crt_deinit(cert);
900 if (!g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
906 g_context->setup_data.cn_call_back_arg)) {
907 alert = GNUTLS_A_ACCESS_DENIED;
912 if (g_context->setup_data.additional_tls_setup_call_back) {
914 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
915 &g_context->setup_data)) {
921 if (cert_info.san_or_cn)
922 gnutls_free(cert_info.san_or_cn);
927 if (cert_info.san_or_cn)
928 gnutls_free(cert_info.san_or_cn);
930 if (!g_env->sent_alert) {
931 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
932 g_env->sent_alert = 1;
946cert_verify_callback_gnutls(gnutls_session_t g_session) {
947 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
948 if (cert_verify_gnutls(g_session) == 0) {
956#define min(a,b) ((a) < (b) ? (a) : (b))
960pin_callback(
void *user_data,
int attempt,
977#if (GNUTLS_VERSION_NUMBER >= 0x030606)
979static const unsigned char cert_asn1_header1[] = {
983 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
988 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07,
990static const unsigned char cert_asn1_header2[] = {
995static gnutls_datum_t *
996get_asn1_spki(
const uint8_t *data,
size_t size) {
999 gnutls_datum_t *spki = NULL;
1001 if (pub_key && prime) {
1002 size_t header_size =
sizeof(cert_asn1_header1) +
1005 sizeof(cert_asn1_header2);
1006 uint8_t *tmp = gnutls_malloc(
sizeof(gnutls_datum_t) +
1011 spki = (gnutls_datum_t *)tmp;
1012 spki->data = &tmp[
sizeof(gnutls_datum_t)];
1013 memcpy(&spki->data[header_size], pub_key->
s, pub_key->
length);
1014 memcpy(spki->data, cert_asn1_header1,
sizeof(cert_asn1_header1));
1016 spki->data[
sizeof(cert_asn1_header1)+1] = prime->
length;
1017 memcpy(&spki->data[
sizeof(cert_asn1_header1)+2],
1019 memcpy(&spki->data[
sizeof(cert_asn1_header1)+2+prime->
length],
1020 cert_asn1_header2,
sizeof(cert_asn1_header2));
1021 spki->size = header_size + pub_key->
length;
1037setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1038 gnutls_session_t g_session,
1039 coap_gnutls_context_t *g_context,
1043 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1044 "gnutls_certificate_allocate_credentials");
1053 coap_log_warn(
"RPK keys cannot be in COAP_PKI_KEY_PEM format\n");
1054 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1056 G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials,
1059 GNUTLS_X509_FMT_PEM),
1060 "gnutls_certificate_set_x509_key_file");
1063 coap_log_err(
"***setup_pki: (D)TLS: No %s Certificate + Private "
1066 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1070 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1072 GNUTLS_X509_FMT_PEM);
1074 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: No certificates found\n");
1075 }
else if (ret < 0) {
1077 "gnutls_certificate_set_x509_trust_file",
1078 gnutls_strerror(ret));
1089 gnutls_datum_t cert;
1091 int alloced_cert_memory = 0;
1092 int alloced_key_memory = 0;
1097 alloced_cert_memory = 1;
1098 cert.data = gnutls_malloc(cert.size + 1);
1101 return GNUTLS_E_MEMORY_ERROR;
1105 cert.data[cert.size] =
'\000';
1115 alloced_key_memory = 1;
1116 key.data = gnutls_malloc(key.size + 1);
1119 if (alloced_cert_memory)
1120 gnutls_free(cert.data);
1121 return GNUTLS_E_MEMORY_ERROR;
1124 key.data[key.size] =
'\000';
1132#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1133 int have_done_key = 0;
1134 if (strstr((
char *)key.data,
"-----BEGIN EC PRIVATE KEY-----")) {
1135 gnutls_datum_t der_private;
1137 if (gnutls_pem_base64_decode2(
"EC PRIVATE KEY", &key,
1138 &der_private) == 0) {
1139 gnutls_datum_t *spki = get_asn1_spki(der_private.data,
1143 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1146 GNUTLS_X509_FMT_DER, NULL,
1147 COAP_GNUTLS_KEY_RPK,
1154 gnutls_free(der_private.data);
1157 if (!have_done_key) {
1158 G_CHECK(gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1161 GNUTLS_X509_FMT_PEM, NULL,
1162 COAP_GNUTLS_KEY_RPK,
1164 "gnutls_certificate_set_rawpk_key_mem");
1167 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1168 if (alloced_cert_memory)
1169 gnutls_free(cert.data);
1170 if (alloced_key_memory)
1171 gnutls_free(key.data);
1172 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1175 G_CHECK(gnutls_certificate_set_x509_key_mem(*pki_credentials,
1178 GNUTLS_X509_FMT_PEM),
1179 "gnutls_certificate_set_x509_key_mem");
1181 if (alloced_cert_memory)
1182 gnutls_free(cert.data);
1183 if (alloced_key_memory)
1184 gnutls_free(key.data);
1186 coap_log_err(
"***setup_pki: (D)TLS: No Server Certificate + Private "
1188 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1193 int alloced_ca_memory = 0;
1198 alloced_ca_memory = 1;
1199 ca.data = gnutls_malloc(ca.size + 1);
1202 return GNUTLS_E_MEMORY_ERROR;
1205 ca.data[ca.size] =
'\000';
1212 ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1214 GNUTLS_X509_FMT_PEM);
1216 coap_log_warn(
"gnutls_certificate_set_x509_trust_mem: No certificates found\n");
1217 }
else if (ret < 0) {
1219 "gnutls_certificate_set_x509_trust_mem",
1220 gnutls_strerror(ret));
1221 if (alloced_ca_memory)
1222 gnutls_free(ca.data);
1225 if (alloced_ca_memory)
1226 gnutls_free(ca.data);
1235 gnutls_datum_t cert;
1246#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1247 int have_done_key = 0;
1250 gnutls_datum_t *spki = get_asn1_spki(key.data,
1254 ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1257 GNUTLS_X509_FMT_DER, NULL,
1258 COAP_GNUTLS_KEY_RPK,
1266 if (!have_done_key) {
1267 G_CHECK(gnutls_certificate_set_rawpk_key_mem(*pki_credentials,
1270 GNUTLS_X509_FMT_DER, NULL,
1271 COAP_GNUTLS_KEY_RPK,
1273 "gnutls_certificate_set_rawpk_key_mem");
1276 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1277 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1280 G_CHECK(gnutls_certificate_set_x509_key_mem(*pki_credentials,
1283 GNUTLS_X509_FMT_DER),
1284 "gnutls_certificate_set_x509_key_mem");
1287 coap_log_err(
"***setup_pki: (D)TLS: No %s Certificate + Private "
1290 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1294 gnutls_datum_t ca_cert;
1298 sizeof(ca_cert.data));
1300 ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1302 GNUTLS_X509_FMT_DER);
1304 coap_log_warn(
"gnutls_certificate_set_x509_trust_mem: No certificates found\n");
1305 }
else if (ret < 0) {
1307 "gnutls_certificate_set_x509_trust_mem",
1308 gnutls_strerror(ret));
1320 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1322#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1323 G_CHECK(gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1326 GNUTLS_X509_FMT_PEM, NULL,
1327 COAP_GNUTLS_KEY_RPK,
1328 NULL, 0, GNUTLS_PKCS_PLAIN, 0),
1329 "gnutls_certificate_set_rawpk_key_file");
1331 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1332 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1335 G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials,
1338 GNUTLS_X509_FMT_DER),
1339 "gnutls_certificate_set_x509_key_file");
1342 coap_log_err(
"***setup_pki: (D)TLS: No %s Certificate + Private "
1345 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1349 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1351 GNUTLS_X509_FMT_DER);
1353 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: No certificates found\n");
1354 }
else if (ret < 0) {
1356 "gnutls_certificate_set_x509_trust_file",
1357 gnutls_strerror(ret));
1364 coap_log_err(
"***setup_pki: (D)TLS: Unknown key type %d\n",
1366 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1369 if (g_context->root_ca_file) {
1370 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1371 g_context->root_ca_file,
1372 GNUTLS_X509_FMT_PEM);
1374 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1377 if (g_context->root_ca_path) {
1378#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1379 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1380 g_context->root_ca_path,
1381 GNUTLS_X509_FMT_PEM),
1382 "gnutls_certificate_set_x509_trust_dir");
1385 gnutls_certificate_send_x509_rdn_sequence(g_session,
1387 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1389 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1390 "gnutls_certificate_set_x509_system_trust");
1394 gnutls_certificate_set_verify_function(*pki_credentials,
1395 cert_verify_callback_gnutls);
1399 gnutls_certificate_set_verify_limits(*pki_credentials,
1408 gnutls_certificate_set_verify_flags(*pki_credentials,
1410 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1413 return GNUTLS_E_SUCCESS;
1419#if COAP_SERVER_SUPPORT
1425setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1431 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1432 "gnutls_psk_allocate_server_credentials");
1433 gnutls_psk_set_server_credentials_function(*psk_credentials,
1434 psk_server_callback);
1438 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1439 "gnutls_psk_set_server_credentials_hint");
1442 return GNUTLS_E_SUCCESS;
1453post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1456 coap_gnutls_context_t *g_context =
1458 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1459 int ret = GNUTLS_E_SUCCESS;
1469 name = gnutls_malloc(len);
1471 return GNUTLS_E_MEMORY_ERROR;
1474 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1475 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1477 new_name = gnutls_realloc(name, len);
1478 if (new_name == NULL) {
1479 ret = GNUTLS_E_MEMORY_ERROR;
1487 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1490 if (ret != GNUTLS_E_SUCCESS)
1493 if (type != GNUTLS_NAME_DNS)
1504 for (i = 0; i < g_context->psk_sni_count; i++) {
1505 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1509 if (i == g_context->psk_sni_count) {
1518 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1519 GNUTLS_A_UNRECOGNIZED_NAME));
1520 g_env->sent_alert = 1;
1521 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1525 g_context->psk_sni_entry_list =
1526 gnutls_realloc(g_context->psk_sni_entry_list,
1527 (i+1)*
sizeof(psk_sni_entry));
1528 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1529 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1531 sni_setup_data.
psk_info = *new_entry;
1532 if ((ret = setup_psk_credentials(
1533 &g_context->psk_sni_entry_list[i].psk_credentials,
1535 &sni_setup_data)) < 0) {
1537 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1538 GNUTLS_A_BAD_CERTIFICATE));
1539 g_env->sent_alert = 1;
1543 g_context->psk_sni_count++;
1545 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1546 g_context->psk_sni_entry_list[i].psk_credentials),
1547 "gnutls_credentials_set");
1549 &g_context->psk_sni_entry_list[i].psk_info.hint);
1551 &g_context->psk_sni_entry_list[i].psk_info.key);
1567post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1570 coap_gnutls_context_t *g_context =
1572 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1573 int ret = GNUTLS_E_SUCCESS;
1576 if (g_context->setup_data.validate_sni_call_back) {
1583 name = gnutls_malloc(len);
1585 return GNUTLS_E_MEMORY_ERROR;
1588 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1589 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1591 new_name = gnutls_realloc(name, len);
1592 if (new_name == NULL) {
1593 ret = GNUTLS_E_MEMORY_ERROR;
1601 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1604 if (ret != GNUTLS_E_SUCCESS)
1607 if (type != GNUTLS_NAME_DNS)
1618 for (i = 0; i < g_context->pki_sni_count; i++) {
1619 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1623 if (i == g_context->pki_sni_count) {
1628 g_context->setup_data.validate_sni_call_back(name,
1629 g_context->setup_data.sni_call_back_arg);
1631 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1632 GNUTLS_A_UNRECOGNIZED_NAME));
1633 g_env->sent_alert = 1;
1634 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1638 g_context->pki_sni_entry_list = gnutls_realloc(
1639 g_context->pki_sni_entry_list,
1640 (i+1)*
sizeof(pki_sni_entry));
1641 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1642 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1643 sni_setup_data = g_context->setup_data;
1644 sni_setup_data.
pki_key = *new_entry;
1645 if ((ret = setup_pki_credentials(
1646 &g_context->pki_sni_entry_list[i].pki_credentials,
1651 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1652 GNUTLS_A_BAD_CERTIFICATE));
1653 g_env->sent_alert = 1;
1657 g_context->pki_sni_count++;
1659 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1660 g_context->pki_sni_entry_list[i].pki_credentials),
1661 "gnutls_credentials_set");
1673#if COAP_CLIENT_SUPPORT
1679setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1680 coap_gnutls_context_t *g_context =
1684 g_context->psk_pki_enabled |= IS_CLIENT;
1685 if (g_context->psk_pki_enabled & IS_PSK) {
1687 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1688 "gnutls_psk_allocate_client_credentials");
1689 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1690 psk_client_callback);
1691 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1692 g_env->psk_cl_credentials),
1693 "gnutls_credentials_set");
1696 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1699 "gnutls_server_name_set");
1705 if (tls_version->
version >= 0x030604) {
1707 const char *priority;
1709 if (tls_version->
version >= 0x030606) {
1710 priority = VARIANTS_NO_TLS13_3_6_6;
1712 priority = VARIANTS_NO_TLS13_3_6_4;
1714 ret = gnutls_priority_set_direct(g_env->g_session,
1717 if (ret == GNUTLS_E_INVALID_REQUEST)
1718 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1720 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1727 if ((g_context->psk_pki_enabled & IS_PKI) ||
1728 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1734 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1735 g_context, setup_data,
1737 "setup_pki_credentials");
1739 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1740 g_env->pki_credentials),
1741 "gnutls_credentials_set");
1744 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1745 &g_context->alpn_proto, 1, 0),
1746 "gnutls_alpn_set_protocols");
1750 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1753 "gnutls_server_name_set");
1756 return GNUTLS_E_SUCCESS;
1763#if COAP_SERVER_SUPPORT
1772psk_server_callback(gnutls_session_t g_session,
1773 const char *identity,
1774 gnutls_datum_t *key) {
1777 coap_gnutls_context_t *g_context;
1782 if (c_session == NULL)
1786 if (g_context == NULL)
1792 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
1793 lidentity.
length = strlen((
const char *)lidentity.
s);
1797 (
int)lidentity.
length, (
const char *)lidentity.
s);
1809 if (psk_key == NULL)
1812 key->data = gnutls_malloc(psk_key->
length);
1813 if (key->data == NULL)
1815 memcpy(key->data, psk_key->
s, psk_key->
length);
1816 key->size = psk_key->
length;
1825setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1826 coap_gnutls_context_t *g_context =
1828 int ret = GNUTLS_E_SUCCESS;
1830 g_context->psk_pki_enabled |= IS_SERVER;
1831 if (g_context->psk_pki_enabled & IS_PSK) {
1832 G_CHECK(setup_psk_credentials(
1833 &g_env->psk_sv_credentials,
1836 "setup_psk_credentials\n");
1837 G_CHECK(gnutls_credentials_set(g_env->g_session,
1839 g_env->psk_sv_credentials),
1840 "gnutls_credentials_set\n");
1841 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1842 post_client_hello_gnutls_psk);
1845 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");
1853 gnutls_certificate_server_set_request(g_env->g_session,
1854 GNUTLS_CERT_REQUIRE);
1856 gnutls_certificate_server_set_request(g_env->g_session,
1857 GNUTLS_CERT_REQUEST);
1859 gnutls_certificate_server_set_request(g_env->g_session,
1860 GNUTLS_CERT_IGNORE);
1863 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
1864 post_client_hello_gnutls_pki);
1866 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1867 g_env->pki_credentials),
1868 "gnutls_credentials_set\n");
1870 return GNUTLS_E_SUCCESS;
1883coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
1888 if (!c_session->
tls) {
1892 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
1895 if (data != NULL && data->pdu_len > 0) {
1896 if (outl < data->pdu_len) {
1897 memcpy(out, data->pdu, outl);
1899 if (!data->peekmode) {
1901 data->pdu_len -= outl;
1904 memcpy(out, data->pdu, data->pdu_len);
1905 ret = data->pdu_len;
1906 if (!data->peekmode) {
1926coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
1927 size_t send_buffer_length) {
1928 ssize_t result = -1;
1933#if COAP_SERVER_SUPPORT
1942 send_buffer, send_buffer_length);
1943 if (result != (
int)send_buffer_length) {
1944 coap_log_warn(
"coap_netif_dgrm_write failed (%zd != %zu)\n",
1945 result, send_buffer_length);
1960receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
1964 fd_set readfds, writefds, exceptfds;
1966 int nfds = c_session->
sock.
fd +1;
1967 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1971 g_env->coap_ssl_data.pdu_len > 0) {
1977 FD_ZERO(&exceptfds);
1978 FD_SET(c_session->
sock.
fd, &readfds);
1979 if (!(g_env && g_env->doing_dtls_timeout)) {
1980 FD_SET(c_session->
sock.
fd, &writefds);
1981 FD_SET(c_session->
sock.
fd, &exceptfds);
1987 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
1992static coap_gnutls_env_t *
1994 coap_gnutls_context_t *g_context =
1996 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1997#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1998 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2000 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2007 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2011 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2013 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2015 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2016 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2017 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2019 gnutls_transport_set_ptr(g_env->g_session, c_session);
2021 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2022 "gnutls_priority_set");
2024 if (type == GNUTLS_SERVER) {
2025#if COAP_SERVER_SUPPORT
2026 G_CHECK(setup_server_ssl_session(c_session, g_env),
2027 "setup_server_ssl_session");
2032#if COAP_CLIENT_SUPPORT
2033 G_CHECK(setup_client_ssl_session(c_session, g_env),
2034 "setup_client_ssl_session");
2040 gnutls_handshake_set_timeout(g_env->g_session,
2041 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2042 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2043 COAP_DTLS_RETRANSMIT_TOTAL_MS);
2054coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2055 coap_gnutls_env_t *g_env,
2056 coap_free_bye_t free_bye) {
2061 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2063 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2064 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2066 gnutls_deinit(g_env->g_session);
2067 g_env->g_session = NULL;
2068 if (g_context->psk_pki_enabled & IS_PSK) {
2069 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2070 g_env->psk_cl_credentials != NULL) {
2071 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2072 g_env->psk_cl_credentials = NULL;
2075 if (g_env->psk_sv_credentials != NULL)
2076 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2077 g_env->psk_sv_credentials = NULL;
2080 if ((g_context->psk_pki_enabled & IS_PKI) ||
2081 (g_context->psk_pki_enabled &
2082 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2083 gnutls_certificate_free_credentials(g_env->pki_credentials);
2084 g_env->pki_credentials = NULL;
2086 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2091#if COAP_SERVER_SUPPORT
2094 coap_gnutls_env_t *g_env =
2095 (coap_gnutls_env_t *)c_session->
tls;
2097 gnutls_transport_set_ptr(g_env->g_session, c_session);
2105 gnutls_session_t g_session) {
2106#if COAP_MAX_LOGGING_LEVEL > 0
2107 int last_alert = gnutls_alert_get(g_session);
2109 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2112 last_alert, gnutls_alert_get_name(last_alert));
2116 last_alert, gnutls_alert_get_name(last_alert));
2129do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2132 ret = gnutls_handshake(g_env->g_session);
2134 case GNUTLS_E_SUCCESS:
2135 g_env->established = 1;
2140 case GNUTLS_E_INTERRUPTED:
2144 case GNUTLS_E_AGAIN:
2148 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2152 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2154 g_env->sent_alert = 1;
2155 log_last_alert(c_session, g_env->g_session);
2157 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2158 case GNUTLS_E_UNEXPECTED_PACKET:
2162 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2163 log_last_alert(c_session, g_env->g_session);
2167 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2168#if (GNUTLS_VERSION_NUMBER > 0x030606)
2169 case GNUTLS_E_CERTIFICATE_REQUIRED:
2171 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2173 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2174 GNUTLS_A_BAD_CERTIFICATE));
2175 g_env->sent_alert = 1;
2179 case GNUTLS_E_DECRYPTION_FAILED:
2182 gnutls_strerror(ret));
2183 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2184 GNUTLS_A_DECRYPT_ERROR));
2185 g_env->sent_alert = 1;
2189 case GNUTLS_E_CERTIFICATE_ERROR:
2190 if (g_env->sent_alert) {
2196 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2197 case GNUTLS_E_NO_CIPHER_SUITES:
2198 case GNUTLS_E_INVALID_SESSION:
2201 gnutls_strerror(ret));
2202 if (!g_env->sent_alert) {
2203 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2204 GNUTLS_A_HANDSHAKE_FAILURE));
2205 g_env->sent_alert = 1;
2210 case GNUTLS_E_SESSION_EOF:
2211 case GNUTLS_E_PREMATURE_TERMINATION:
2212 case GNUTLS_E_TIMEDOUT:
2213 case GNUTLS_E_PULL_ERROR:
2214 case GNUTLS_E_PUSH_ERROR:
2220 "returned %d: '%s'\n",
2221 ret, gnutls_strerror(ret));
2228#if COAP_CLIENT_SUPPORT
2231 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2235 ret = do_gnutls_handshake(c_session, g_env);
2240 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2250 if (c_session && c_session->
context && c_session->
tls) {
2254 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2255 c_session->
tls = NULL;
2262 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2266 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2267 (
unsigned int)c_session->
mtu),
2268 "gnutls_dtls_set_data_mtu");
2280 const uint8_t *data,
size_t data_len) {
2282 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2284 assert(g_env != NULL);
2287 if (g_env->established) {
2288 ret = gnutls_record_send(g_env->g_session, data, data_len);
2292 case GNUTLS_E_AGAIN:
2295 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2297 g_env->sent_alert = 1;
2298 log_last_alert(c_session, g_env->g_session);
2304 "returned %d: '%s'\n",
2305 ret, gnutls_strerror(ret));
2314 ret = do_gnutls_handshake(c_session, g_env);
2332 if (ret == (ssize_t)data_len)
2354 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2357 if (g_env && g_env->g_session) {
2358 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2371 g_env->last_timeout = now;
2372 return now + rem_ms;
2384 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2387 g_env->doing_dtls_timeout = 1;
2389 (do_gnutls_handshake(c_session, g_env) < 0)) {
2391 g_env->doing_dtls_timeout = 0;
2395 g_env->doing_dtls_timeout = 0;
2408 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2410 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2414 assert(g_env != NULL);
2416 if (ssl_data->pdu_len)
2417 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2419 ssl_data->pdu = data;
2420 ssl_data->pdu_len = data_len;
2423 if (g_env->established) {
2427 gnutls_transport_set_ptr(g_env->g_session, c_session);
2430 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2433 }
else if (ret == 0) {
2437 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2439 g_env->sent_alert = 1;
2440 log_last_alert(c_session, g_env->g_session);
2444 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2445 log_last_alert(c_session, g_env->g_session);
2450 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2456 ret = do_gnutls_handshake(c_session, g_env);
2461 if (ssl_data->pdu_len && !g_env->sent_alert) {
2463 ret = do_gnutls_handshake(c_session, g_env);
2483 if (ssl_data && ssl_data->pdu_len) {
2485 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2486 ssl_data->pdu_len = 0;
2487 ssl_data->pdu = NULL;
2496#if COAP_SERVER_SUPPORT
2504 const uint8_t *data,
2507 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2508 coap_ssl_t *ssl_data;
2512 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2514 c_session->
tls = g_env;
2515 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2516 GNUTLS_COOKIE_KEY_SIZE);
2523 gnutls_dtls_prestate_st prestate;
2526 memset(&prestate, 0,
sizeof(prestate));
2528 memcpy(&data_rw, &data,
sizeof(data_rw));
2529 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2536 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2544 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2547 ssl_data = &g_env->coap_ssl_data;
2548 ssl_data->pdu = data;
2549 ssl_data->pdu_len = data_len;
2551 ret = do_gnutls_handshake(c_session, g_env);
2558 g_env, COAP_FREE_BYE_NONE);
2559 c_session->
tls = NULL;
2567 if (ssl_data && ssl_data->pdu_len) {
2569 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2570 ssl_data->pdu_len = 0;
2571 ssl_data->pdu = NULL;
2582#if !COAP_DISABLE_TCP
2590coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2612coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2621 (errno == EPIPE || errno == ECONNRESET)) {
2647#if COAP_CLIENT_SUPPORT
2650 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2651 coap_gnutls_context_t *g_context =
2653#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2654 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2656 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2663 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2665 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2667 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2668 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2669 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2671 gnutls_transport_set_ptr(g_env->g_session, c_session);
2673 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2674 setup_client_ssl_session(c_session, g_env);
2676 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2678 c_session->
tls = g_env;
2679 ret = do_gnutls_handshake(c_session, g_env);
2693#if COAP_SERVER_SUPPORT
2696 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2697 coap_gnutls_context_t *g_context =
2699#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2700 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2702 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2708 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2710 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2712 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2713 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2714 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2716 gnutls_transport_set_ptr(g_env->g_session, c_session);
2718 setup_server_ssl_session(c_session, g_env);
2720 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2721 gnutls_handshake_set_timeout(g_env->g_session,
2722 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2724 c_session->
tls = g_env;
2725 ret = do_gnutls_handshake(c_session, g_env);
2752 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2754 assert(g_env != NULL);
2757 if (g_env->established) {
2758 ret = gnutls_record_send(g_env->g_session, data, data_len);
2762 case GNUTLS_E_AGAIN:
2765 case GNUTLS_E_PUSH_ERROR:
2766 case GNUTLS_E_PULL_ERROR:
2767 case GNUTLS_E_PREMATURE_TERMINATION:
2770 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2772 g_env->sent_alert = 1;
2773 log_last_alert(c_session, g_env->g_session);
2778 "returned %d: '%s'\n",
2779 ret, gnutls_strerror(ret));
2788 ret = do_gnutls_handshake(c_session, g_env);
2811 if (ret == (ssize_t)data_len)
2828 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2837 if (!g_env->established && !g_env->sent_alert) {
2838 ret = do_gnutls_handshake(c_session, g_env);
2847 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
2853 case GNUTLS_E_AGAIN:
2857 case GNUTLS_E_PULL_ERROR:
2860 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2862 g_env->sent_alert = 1;
2863 log_last_alert(c_session, g_env->g_session);
2866 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2867 log_last_alert(c_session, g_env->g_session);
2872 "returned %d: '%s'\n",
2873 ret, gnutls_strerror(ret));
2898#if COAP_SERVER_SUPPORT
2901 gnutls_hash_hd_t digest_ctx;
2903 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
2911 gnutls_hash_deinit(digest_ctx, NULL);
2916 const uint8_t *data,
2918 int ret = gnutls_hash(digest_ctx, data, data_len);
2926 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
2938static struct hash_algs {
2940 gnutls_digest_algorithm_t dig_type;
2948static gnutls_digest_algorithm_t
2949get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
2952 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
2953 if (hashs[idx].alg == alg) {
2954 *hash_len = hashs[idx].dig_size;
2955 return hashs[idx].dig_type;
2958 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
2959 return GNUTLS_DIG_UNKNOWN;
2967 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
2968 gnutls_hash_hd_t digest_ctx;
2972 if (dig_type == GNUTLS_DIG_UNKNOWN) {
2973 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
2977 if (gnutls_hash_init(&digest_ctx, dig_type)) {
2980 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
2987 gnutls_hash_output(digest_ctx,
dummy->s);
2990 gnutls_hash_deinit(digest_ctx, NULL);
2995 gnutls_hash_deinit(digest_ctx, NULL);
3000#if COAP_OSCORE_SUPPORT
3011static struct cipher_algs {
3013 gnutls_cipher_algorithm_t cipher_type;
3018static gnutls_cipher_algorithm_t
3022 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3023 if (ciphers[idx].alg == alg)
3024 return ciphers[idx].cipher_type;
3026 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3035static struct hmac_algs {
3037 gnutls_mac_algorithm_t hmac_type;
3043static gnutls_mac_algorithm_t
3047 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3048 if (hmacs[idx].hmac_alg == hmac_alg)
3049 return hmacs[idx].hmac_type;
3051 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3057 return get_cipher_alg(alg);
3066 return get_hmac_alg(hmac_alg);
3074 size_t *max_result_len) {
3075 gnutls_aead_cipher_hd_t ctx;
3079 size_t result_len = *max_result_len;
3080 gnutls_cipher_algorithm_t algo;
3082 uint8_t *key_data_rw;
3088 assert(params != NULL);
3092 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3093 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3097 tag_size = gnutls_cipher_get_tag_size(algo);
3101 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3102 key.data = key_data_rw;
3112 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3114 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3124 "gnutls_aead_cipher_encrypt");
3125 *max_result_len = result_len;
3128 gnutls_aead_cipher_deinit(ctx);
3129 return ret == 1 ? 1 : 0;
3137 size_t *max_result_len) {
3138 gnutls_aead_cipher_hd_t ctx;
3142 size_t result_len = *max_result_len;
3143 gnutls_cipher_algorithm_t algo;
3145 uint8_t *key_data_rw;
3151 assert(params != NULL);
3156 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3157 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3161 tag_size = gnutls_cipher_get_tag_size(algo);
3165 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3166 key.data = key_data_rw;
3176 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3178 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3188 "gnutls_aead_cipher_decrypt");
3189 *max_result_len = result_len;
3192 gnutls_aead_cipher_deinit(ctx);
3193 return ret == 1 ? 1 : 0;
3201 gnutls_hmac_hd_t ctx;
3204 gnutls_mac_algorithm_t mac_algo;
3210 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3211 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3214 len = gnutls_hmac_get_len(mac_algo);
3221 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3222 "gnutls_hmac_init");
3223 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3224 gnutls_hmac_output(ctx,
dummy->s);
3230 gnutls_hmac_deinit(ctx, NULL);
3231 return ret == 1 ? 1 : 0;
3242#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