53#if COAP_WITH_LIBGNUTLS
55#define MIN_GNUTLS_VERSION "3.3.0"
58#include <gnutls/gnutls.h>
59#include <gnutls/x509.h>
60#include <gnutls/dtls.h>
61#include <gnutls/pkcs11.h>
62#include <gnutls/crypto.h>
63#include <gnutls/abstract.h>
65#if (GNUTLS_VERSION_NUMBER >= 0x030606)
66#define COAP_GNUTLS_KEY_RPK GNUTLS_KEY_DIGITAL_SIGNATURE | \
67 GNUTLS_KEY_NON_REPUDIATION | \
68 GNUTLS_KEY_KEY_ENCIPHERMENT | \
69 GNUTLS_KEY_DATA_ENCIPHERMENT | \
70 GNUTLS_KEY_KEY_AGREEMENT | \
71 GNUTLS_KEY_KEY_CERT_SIGN
75#define GNUTLS_CRT_RAW GNUTLS_CRT_RAWPK
79#define strcasecmp _stricmp
82typedef struct coap_ssl_t {
86 gnutls_datum_t cookie_key;
94typedef struct coap_gnutls_env_t {
95 gnutls_session_t g_session;
96 gnutls_psk_client_credentials_t psk_cl_credentials;
97 gnutls_psk_server_credentials_t psk_sv_credentials;
98 gnutls_certificate_credentials_t pki_credentials;
99 coap_ssl_t coap_ssl_data;
102 int doing_dtls_timeout;
107#define IS_PSK (1 << 0)
108#define IS_PKI (1 << 1)
109#define IS_CLIENT (1 << 6)
110#define IS_SERVER (1 << 7)
112typedef struct pki_sni_entry {
115 gnutls_certificate_credentials_t pki_credentials;
118typedef struct psk_sni_entry {
121 gnutls_psk_server_credentials_t psk_credentials;
124typedef struct coap_gnutls_context_t {
127 size_t pki_sni_count;
128 pki_sni_entry *pki_sni_entry_list;
129 size_t psk_sni_count;
130 psk_sni_entry *psk_sni_entry_list;
131 gnutls_datum_t alpn_proto;
134 int trust_store_defined;
135 gnutls_priority_t priority_cache;
136} coap_gnutls_context_t;
138typedef enum coap_free_bye_t {
139 COAP_FREE_BYE_AS_TCP,
140 COAP_FREE_BYE_AS_UDP,
144#define VARIANTS_3_6_6 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL:+SHA256"
145#define VARIANTS_3_5_5 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8"
146#define VARIANTS_BASE "NORMAL:+ECDHE-PSK:+PSK"
148#define VARIANTS_NO_TLS13_3_6_6 VARIANTS_3_6_6 ":-VERS-TLS1.3"
149#define VARIANTS_NO_TLS13_3_6_4 VARIANTS_3_5_5 ":-VERS-TLS1.3"
151#define G_ACTION(xx) do { \
153 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
155#define G_CHECK(xx,func) do { \
156 if ((ret = (xx)) < 0) { \
157 coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \
162#define G_ACTION_CHECK(xx,func) do { \
169#if COAP_SERVER_SUPPORT
170static int post_client_hello_gnutls_pki(gnutls_session_t g_session);
171static int post_client_hello_gnutls_psk(gnutls_session_t g_session);
172static int psk_server_callback(gnutls_session_t g_session,
173 const char *identity,
174 gnutls_datum_t *key);
183 if (gnutls_check_version(MIN_GNUTLS_VERSION) ==
NULL) {
184 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
197 if (gnutls_check_version(MIN_GNUTLS_VERSION) ==
NULL) {
198 coap_log_err(
"GnuTLS " MIN_GNUTLS_VERSION
" or later is required\n");
240#if (GNUTLS_VERSION_NUMBER >= 0x030606)
256#if COAP_CLIENT_SUPPORT
268 const char *vers = gnutls_check_version(
NULL);
274 sscanf(vers,
"%d.%d.%d", &p1, &p2, &p3);
275 version.
version = (p1 << 16) | (p2 << 8) | p3;
283coap_gnutls_audit_log_func(gnutls_session_t g_session,
const char *text) {
284#if COAP_MAX_LOGGING_LEVEL > 0
300coap_gnutls_log_func(
int level,
const char *text) {
318 coap_gnutls_context_t *g_context =
321 if (!g_context || !setup_data)
324 g_context->setup_data = *setup_data;
325 if (!g_context->setup_data.verify_peer_cert) {
327 g_context->setup_data.check_common_ca = 0;
328 if (g_context->setup_data.is_rpk_not_cert) {
330 g_context->setup_data.allow_self_signed = 0;
331 g_context->setup_data.allow_expired_certs = 0;
332 g_context->setup_data.cert_chain_validation = 0;
333 g_context->setup_data.cert_chain_verify_depth = 0;
334 g_context->setup_data.check_cert_revocation = 0;
335 g_context->setup_data.allow_no_crl = 0;
336 g_context->setup_data.allow_expired_crl = 0;
337 g_context->setup_data.allow_bad_md_hash = 0;
338 g_context->setup_data.allow_short_rsa_length = 0;
341 g_context->setup_data.allow_self_signed = 1;
342 g_context->setup_data.allow_expired_certs = 1;
343 g_context->setup_data.cert_chain_validation = 1;
344 g_context->setup_data.cert_chain_verify_depth = 10;
345 g_context->setup_data.check_cert_revocation = 1;
346 g_context->setup_data.allow_no_crl = 1;
347 g_context->setup_data.allow_expired_crl = 1;
348 g_context->setup_data.allow_bad_md_hash = 1;
349 g_context->setup_data.allow_short_rsa_length = 1;
354 g_context->setup_data.pki_key = key;
355 g_context->psk_pki_enabled |= IS_PKI;
369 const char *ca_path) {
370 coap_gnutls_context_t *g_context =
373 coap_log_warn(
"coap_context_set_pki_root_cas: (D)TLS environment "
378 if (ca_file ==
NULL && ca_path ==
NULL) {
379 coap_log_warn(
"coap_context_set_pki_root_cas: ca_file and/or ca_path "
383 if (g_context->root_ca_file) {
384 gnutls_free(g_context->root_ca_file);
385 g_context->root_ca_file =
NULL;
388 g_context->root_ca_file = gnutls_strdup(ca_file);
390 if (g_context->root_ca_path) {
391 gnutls_free(g_context->root_ca_path);
392 g_context->root_ca_path =
NULL;
395#if (GNUTLS_VERSION_NUMBER >= 0x030306)
396 g_context->root_ca_path = gnutls_strdup(ca_path);
398 coap_log_err(
"ca_path not supported in GnuTLS < 3.3.6\n");
410 coap_gnutls_context_t *g_context =
413 coap_log_warn(
"coap_context_set_pki_trust_store: (D)TLS environment "
418#if (GNUTLS_VERSION_NUMBER >= 0x030020)
419 g_context->trust_store_defined = 1;
422 coap_log_warn(
"coap_context_set_pki_trust_store(): (D)TLS environment "
423 "not supported for GnuTLS < v3.0.20\n");
428#if COAP_SERVER_SUPPORT
437 coap_gnutls_context_t *g_context =
440 if (!g_context || !setup_data)
446 g_context->psk_pki_enabled |= IS_PSK;
451#if COAP_CLIENT_SUPPORT
460 coap_gnutls_context_t *g_context =
463 if (!g_context || !setup_data)
472 g_context->psk_pki_enabled |= IS_PSK;
483 coap_gnutls_context_t *g_context =
485 return g_context->psk_pki_enabled ? 1 : 0;
490 gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func);
491 gnutls_global_set_log_function(coap_gnutls_log_func);
508 if (c_session && c_session->
tls) {
509 const coap_gnutls_env_t *g_env = (
const coap_gnutls_env_t *)c_session->
tls;
511 return g_env->g_session;
536 const char *err =
"Unknown Error";
538 coap_gnutls_context_t *g_context =
539 (coap_gnutls_context_t *)
540 gnutls_malloc(
sizeof(coap_gnutls_context_t));
544 const char *priority;
546 memset(g_context, 0,
sizeof(coap_gnutls_context_t));
547 G_CHECK(gnutls_global_init(),
"gnutls_global_init");
548 g_context->alpn_proto.data = gnutls_malloc(4);
549 if (g_context->alpn_proto.data) {
550 memcpy(g_context->alpn_proto.data,
"coap", 4);
551 g_context->alpn_proto.size = 4;
554 if (tls_version->
version >= 0x030606) {
555 priority = VARIANTS_3_6_6;
556 }
else if (tls_version->
version >= 0x030505) {
557 priority = VARIANTS_3_5_5;
559 priority = VARIANTS_BASE;
561 ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
562 if (ret != GNUTLS_E_SUCCESS) {
563 if (ret == GNUTLS_E_INVALID_REQUEST)
564 coap_log_warn(
"gnutls_priority_init: Syntax error at: %s\n", err);
566 coap_log_warn(
"gnutls_priority_init: %s\n", gnutls_strerror(ret));
581 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
583 gnutls_free(g_context->alpn_proto.data);
584 gnutls_free(g_context->root_ca_file);
585 gnutls_free(g_context->root_ca_path);
586 for (i = 0; i < g_context->pki_sni_count; i++) {
587 gnutls_free(g_context->pki_sni_entry_list[i].sni);
588 gnutls_certificate_free_credentials(
589 g_context->pki_sni_entry_list[i].pki_credentials);
591 if (g_context->pki_sni_entry_list)
592 gnutls_free(g_context->pki_sni_entry_list);
594 for (i = 0; i < g_context->psk_sni_count; i++) {
595 gnutls_free(g_context->psk_sni_entry_list[i].sni);
597 gnutls_psk_free_server_credentials(
598 g_context->psk_sni_entry_list[i].psk_credentials);
600 if (g_context->psk_sni_entry_list)
601 gnutls_free(g_context->psk_sni_entry_list);
603 gnutls_priority_deinit(g_context->priority_cache);
605 gnutls_global_deinit();
606 gnutls_free(g_context);
609#if COAP_CLIENT_SUPPORT
618psk_client_callback(gnutls_session_t g_session,
619 char **username, gnutls_datum_t *key) {
622 coap_gnutls_context_t *g_context;
624 const char *hint = gnutls_psk_client_get_hint(g_session);
634 if (c_session ==
NULL)
638 if (g_context ==
NULL)
643 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
644 temp.
length = strlen((
const char *)temp.
s);
648 (
const char *)temp.
s);
660 if (cpsk_info ==
NULL)
665 psk_identity = &cpsk_info->
identity;
666 psk_key = &cpsk_info->
key;
672 if (psk_identity ==
NULL || psk_key ==
NULL) {
677 *username = gnutls_malloc(psk_identity->
length+1);
678 if (*username ==
NULL)
680 memcpy(*username, psk_identity->
s, psk_identity->
length);
681 (*username)[psk_identity->
length] =
'\000';
683 key->data = gnutls_malloc(psk_key->
length);
684 if (key->data ==
NULL) {
685 gnutls_free(*username);
689 memcpy(key->data, psk_key->
s, psk_key->
length);
690 key->size = psk_key->
length;
696 gnutls_certificate_type_t certificate_type;
698 const gnutls_datum_t *cert_list;
699 unsigned int cert_list_size;
701} coap_gnutls_certificate_info_t;
707static gnutls_certificate_type_t
709 coap_gnutls_certificate_info_t *cert_info,
const char *sni_match) {
710 gnutls_x509_crt_t cert;
718#if (GNUTLS_VERSION_NUMBER >= 0x030606)
719 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
722 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
725 cert_info->san_or_cn =
NULL;
727 cert_info->cert_list = gnutls_certificate_get_peers(g_session,
728 &cert_info->cert_list_size);
729 if (cert_info->cert_list_size == 0) {
730 return GNUTLS_CRT_UNKNOWN;
733 if (cert_info->certificate_type != GNUTLS_CRT_X509)
734 return cert_info->certificate_type;
736 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
739 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
740 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
742 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
745 for (seq = 0; seq < 100; seq++) {
746 size =
sizeof(dn) -1;
747 ret = gnutls_x509_crt_get_subject_alt_name(cert, seq, dn, &size,
NULL);
748 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
755 if (strlen(sni_match) != size ||
756 memcmp(dn, sni_match, size)) {
760 gnutls_x509_crt_deinit(cert);
761 cert_info->san_or_cn = gnutls_strdup(dn);
762 return cert_info->certificate_type;
768 ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0,
780 gnutls_x509_crt_deinit(cert);
781 cert_info->san_or_cn = gnutls_strdup(cn);
782 return cert_info->certificate_type;
786 for (seq = 0; seq < 100; seq++) {
787 size =
sizeof(dn) -1;
788 ret = gnutls_x509_crt_get_subject_alt_name(cert, seq, dn, &size,
NULL);
789 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
802 gnutls_x509_crt_deinit(cert);
803 return GNUTLS_CRT_UNKNOWN;
806 return GNUTLS_CRT_UNKNOWN;
809#if (GNUTLS_VERSION_NUMBER >= 0x030606)
810#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
811 cert_info.san_or_cn : \
812 cert_type == GNUTLS_CRT_RAW ? \
813 COAP_DTLS_RPK_CERT_CN : "?")
815#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
816 cert_info.san_or_cn : "?")
819#if (GNUTLS_VERSION_NUMBER >= 0x030606)
821check_rpk_cert(coap_gnutls_context_t *g_context,
822 coap_gnutls_certificate_info_t *cert_info,
826 if (g_context->setup_data.validate_cn_call_back) {
827 gnutls_pcert_st pcert;
831 G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
832 GNUTLS_X509_FMT_DER, 0, 0),
833 "gnutls_pcert_import_rawpk_raw");
836 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
837 "gnutls_pubkey_export");
838 gnutls_pcert_deinit(&pcert);
846 g_context->setup_data.cn_call_back_arg));
862cert_verify_gnutls(gnutls_session_t g_session) {
863 unsigned int status = 0;
864 unsigned int fail = 0;
867 coap_gnutls_context_t *g_context =
869 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
870 int alert = GNUTLS_A_BAD_CERTIFICATE;
872 coap_gnutls_certificate_info_t cert_info;
873 gnutls_certificate_type_t cert_type;
875 memset(&cert_info, 0,
sizeof(cert_info));
876 if (!g_context->setup_data.allow_sni_cn_mismatch &&
878 g_context->setup_data.client_sni) {
879 cert_type = get_san_or_cn(c_session, g_session, &cert_info, g_context->setup_data.client_sni);
880 if (cert_type == GNUTLS_CRT_UNKNOWN) {
883 alert = GNUTLS_A_ACCESS_DENIED;
888 if (!cert_info.san_or_cn)
889 cert_type = get_san_or_cn(c_session, g_session, &cert_info,
NULL);
890#if (GNUTLS_VERSION_NUMBER >= 0x030606)
891 if (cert_type == GNUTLS_CRT_RAW) {
892 if (!check_rpk_cert(g_context, &cert_info, c_session)) {
893 alert = GNUTLS_A_ACCESS_DENIED;
900 if (cert_info.cert_list_size == 0) {
901 if (!g_context->setup_data.verify_peer_cert)
907 G_CHECK(gnutls_certificate_verify_peers(g_session,
NULL, 0, &status),
908 "gnutls_certificate_verify_peers");
911 status, cert_info.san_or_cn);
913 if (g_context->setup_data.validate_cn_call_back) {
914 gnutls_x509_crt_t cert;
919 const int cert_is_trusted = !status;
921 G_CHECK(gnutls_x509_crt_init(&cert),
"gnutls_x509_crt_init");
924 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
925 GNUTLS_X509_FMT_DER),
"gnutls_x509_crt_import");
928 G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
929 "gnutls_x509_crt_export");
930 gnutls_x509_crt_deinit(cert);
932 g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
938 g_context->setup_data.cn_call_back_arg));
940 alert = GNUTLS_A_ACCESS_DENIED;
946 status &= ~(GNUTLS_CERT_INVALID);
947 if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
948 status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
949 if (g_context->setup_data.allow_expired_certs) {
952 "The certificate has an invalid usage date",
958 "The certificate has an invalid usage date",
962 if (status & (GNUTLS_CERT_UNEXPECTED_OWNER)) {
963 status &= ~(GNUTLS_CERT_UNEXPECTED_OWNER);
964 if (g_context->setup_data.allow_sni_cn_mismatch) {
967 "The SNI does not match the returned CN",
968 g_context->setup_data.client_sni,
974 "The SNI does not match the returned CN",
975 g_context->setup_data.client_sni,
979 if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
980 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
981 status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
982 GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
983 if (g_context->setup_data.allow_expired_crl) {
986 "The certificate's CRL entry has an invalid usage date",
992 "The certificate's CRL entry has an invalid usage date",
996 if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) {
997 status &= ~(GNUTLS_CERT_SIGNER_NOT_FOUND);
998 if (cert_info.self_signed) {
999 if (g_context->setup_data.allow_self_signed &&
1000 !g_context->setup_data.check_common_ca) {
1007 alert = GNUTLS_A_UNKNOWN_CA;
1014 if (!g_context->setup_data.verify_peer_cert) {
1017 "The peer certificate's CA is unknown",
1021 alert = GNUTLS_A_UNKNOWN_CA;
1024 "The peer certificate's CA is unknown",
1029 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
1030 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
1034 "The certificate uses an insecure algorithm",
1040 coap_log_warn(
" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
1042 status, OUTPUT_CERT_NAME);
1049 if (g_context->setup_data.additional_tls_setup_call_back) {
1051 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
1052 &g_context->setup_data)) {
1058 if (cert_info.san_or_cn)
1059 gnutls_free(cert_info.san_or_cn);
1064 if (cert_info.san_or_cn)
1065 gnutls_free(cert_info.san_or_cn);
1067 if (!g_env->sent_alert) {
1068 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
1069 g_env->sent_alert = 1;
1083cert_verify_callback_gnutls(gnutls_session_t g_session) {
1084 if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) {
1085 if (cert_verify_gnutls(g_session) == 0) {
1093#define min(a,b) ((a) < (b) ? (a) : (b))
1097pin_callback(
void *user_data,
int attempt,
1117check_null_memory(gnutls_datum_t *datum,
1118 const uint8_t *buf,
size_t len,
int *alloced) {
1121 if (buf[len-1] !=
'\000') {
1124 datum->data = gnutls_malloc(len + 1);
1127 return GNUTLS_E_MEMORY_ERROR;
1129 memcpy(datum->data, buf, len);
1130 datum->data[len] =
'\000';
1134 memcpy(&datum->data,
1135 &buf,
sizeof(datum->data));
1140#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1142set_rawpk_key_mem(gnutls_certificate_credentials_t credentials,
1143 const gnutls_datum_t *cert,
const gnutls_datum_t *pkey) {
1144 gnutls_privkey_t privkey =
NULL;
1145 gnutls_pubkey_t pubkey =
NULL;
1146 gnutls_datum_t spki = {
NULL, 0 };
1150 if (!strstr((
const char *)cert->data,
"-----BEGIN PUBLIC KEY-----")) {
1151 if ((ret = gnutls_privkey_init(&privkey)) < 0)
1153 if ((ret = gnutls_privkey_import_x509_raw(privkey, cert, GNUTLS_X509_FMT_PEM,
1156 if ((ret = gnutls_pubkey_init(&pubkey)) < 0)
1158 if ((ret = gnutls_pubkey_import_privkey(pubkey, privkey, 0, 0)) < 0)
1160 if ((ret = gnutls_pubkey_export2(pubkey, GNUTLS_X509_FMT_PEM, &spki)) < 0)
1164 ret = gnutls_certificate_set_rawpk_key_mem(credentials, cert, pkey,
1165 GNUTLS_X509_FMT_PEM,
NULL,
1166 COAP_GNUTLS_KEY_RPK,
NULL, 0, 0);
1168 gnutls_free(spki.data);
1170 gnutls_pubkey_deinit(pubkey);
1172 gnutls_privkey_deinit(privkey);
1182setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1183 gnutls_session_t g_session,
1184 coap_gnutls_context_t *g_context,
1188 gnutls_datum_t cert;
1189 gnutls_datum_t pkey;
1191 int alloced_cert_memory = 0;
1192 int alloced_pkey_memory = 0;
1193 int alloced_ca_memory = 0;
1200 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1201 "gnutls_certificate_allocate_credentials");
1218#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1222 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1225 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1231 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1238 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1252 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1256 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1259 GNUTLS_X509_FMT_PEM)) < 0) {
1266 if ((ret = check_null_memory(&cert,
1269 &alloced_cert_memory)) < 0) {
1274 if ((ret = check_null_memory(&pkey,
1277 &alloced_pkey_memory)) < 0) {
1278 if (alloced_cert_memory)
1279 gnutls_free(cert.data);
1284 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1287 GNUTLS_X509_FMT_PEM)) < 0) {
1288 if (alloced_cert_memory)
1289 gnutls_free(cert.data);
1290 if (alloced_pkey_memory)
1291 gnutls_free(pkey.data);
1296 if (alloced_cert_memory)
1297 gnutls_free(cert.data);
1298 if (alloced_pkey_memory)
1299 gnutls_free(pkey.data);
1302#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1303 if ((ret = check_null_memory(&cert,
1306 &alloced_cert_memory)) < 0) {
1311 if ((ret = check_null_memory(&pkey,
1314 &alloced_pkey_memory)) < 0) {
1315 if (alloced_cert_memory)
1316 gnutls_free(cert.data);
1321 ret = set_rawpk_key_mem(*pki_credentials, &cert, &pkey);
1322 if (alloced_cert_memory)
1323 gnutls_free(cert.data);
1324 if (alloced_pkey_memory)
1325 gnutls_free(pkey.data);
1331 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1334 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1337 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1340 GNUTLS_X509_FMT_DER) < 0)) {
1347 if ((ret = check_null_memory(&cert,
1350 &alloced_cert_memory)) < 0) {
1355 if ((ret = check_null_memory(&pkey,
1358 &alloced_pkey_memory)) < 0) {
1359 if (alloced_cert_memory)
1360 gnutls_free(cert.data);
1365 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1368 GNUTLS_X509_FMT_DER)) < 0) {
1369 if (alloced_cert_memory)
1370 gnutls_free(cert.data);
1371 if (alloced_pkey_memory)
1372 gnutls_free(pkey.data);
1377 if (alloced_cert_memory)
1378 gnutls_free(cert.data);
1379 if (alloced_pkey_memory)
1380 gnutls_free(pkey.data);
1383 gnutls_pkcs11_set_pin_function(pin_callback, &setup_data->
pki_key);
1384 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1387 GNUTLS_X509_FMT_DER)) < 0) {
1394#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1395 gnutls_pkcs11_set_pin_function(pin_callback, setup_data);
1396 if ((ret = gnutls_certificate_set_rawpk_key_file(*pki_credentials,
1399 GNUTLS_X509_FMT_PEM,
NULL,
1400 COAP_GNUTLS_KEY_RPK,
1401 NULL, 0, GNUTLS_PKCS_PLAIN, 0))) {
1407 coap_log_err(
"RPK Support not available (needs gnutls 3.6.6 or later)\n");
1408 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1415 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1426 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1428 GNUTLS_X509_FMT_PEM) < 0)) {
1435 if ((ret = check_null_memory(&ca,
1438 &alloced_ca_memory)) < 0) {
1443 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1445 GNUTLS_X509_FMT_PEM)) < 0) {
1446 if (alloced_ca_memory)
1447 gnutls_free(ca.data);
1452 if (alloced_ca_memory)
1453 gnutls_free(ca.data);
1459 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1461 GNUTLS_X509_FMT_DER) < 0)) {
1468 if ((ret = check_null_memory(&ca,
1471 &alloced_ca_memory)) < 0) {
1476 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1478 GNUTLS_X509_FMT_DER)) <= 0) {
1479 if (alloced_ca_memory)
1480 gnutls_free(ca.data);
1485 if (alloced_ca_memory)
1486 gnutls_free(ca.data);
1489 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1491 GNUTLS_X509_FMT_DER)) <= 0) {
1504 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1508#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1509 if (g_context->trust_store_defined) {
1510 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1511 "gnutls_certificate_set_x509_system_trust");
1514 if (g_context->root_ca_file) {
1515 ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1516 g_context->root_ca_file,
1517 GNUTLS_X509_FMT_PEM);
1519 coap_log_warn(
"gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1522 if (g_context->root_ca_path) {
1523#if (GNUTLS_VERSION_NUMBER >= 0x030306)
1524 G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
1525 g_context->root_ca_path,
1526 GNUTLS_X509_FMT_PEM),
1527 "gnutls_certificate_set_x509_trust_dir");
1530 gnutls_certificate_send_x509_rdn_sequence(g_session,
1532#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1533 if (!(g_context->psk_pki_enabled & IS_PKI) && !g_context->trust_store_defined) {
1535 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1536 "gnutls_certificate_set_x509_system_trust");
1541 gnutls_certificate_set_verify_function(*pki_credentials,
1542 cert_verify_callback_gnutls);
1546 gnutls_certificate_set_verify_limits(*pki_credentials,
1555 gnutls_certificate_set_verify_flags(*pki_credentials,
1557 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1560 return GNUTLS_E_SUCCESS;
1566#if COAP_SERVER_SUPPORT
1572setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1578 G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials),
1579 "gnutls_psk_allocate_server_credentials");
1580 gnutls_psk_set_server_credentials_function(*psk_credentials,
1581 psk_server_callback);
1585 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1586 "gnutls_psk_set_server_credentials_hint");
1589 return GNUTLS_E_SUCCESS;
1600post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1603 coap_gnutls_context_t *g_context =
1605 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1606 int ret = GNUTLS_E_SUCCESS;
1609 if (c_session->
context->spsk_setup_data.validate_sni_call_back) {
1616 name = gnutls_malloc(len);
1618 return GNUTLS_E_MEMORY_ERROR;
1621 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1622 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1624 new_name = gnutls_realloc(name, len);
1625 if (new_name ==
NULL) {
1626 ret = GNUTLS_E_MEMORY_ERROR;
1634 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1637 if (ret != GNUTLS_E_SUCCESS)
1640 if (type != GNUTLS_NAME_DNS)
1651 for (i = 0; i < g_context->psk_sni_count; i++) {
1652 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1656 if (i == g_context->psk_sni_count) {
1663 c_session->
context->spsk_setup_data.validate_sni_call_back(name,
1665 c_session->
context->spsk_setup_data.sni_call_back_arg));
1667 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1668 GNUTLS_A_UNRECOGNIZED_NAME));
1669 g_env->sent_alert = 1;
1670 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1674 g_context->psk_sni_entry_list =
1675 gnutls_realloc(g_context->psk_sni_entry_list,
1676 (i+1)*
sizeof(psk_sni_entry));
1677 g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name);
1678 g_context->psk_sni_entry_list[i].psk_info = *new_entry;
1679 sni_setup_data = c_session->
context->spsk_setup_data;
1680 sni_setup_data.
psk_info = *new_entry;
1681 if ((ret = setup_psk_credentials(
1682 &g_context->psk_sni_entry_list[i].psk_credentials,
1684 &sni_setup_data)) < 0) {
1686 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1687 GNUTLS_A_BAD_CERTIFICATE));
1688 g_env->sent_alert = 1;
1692 g_context->psk_sni_count++;
1694 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1695 g_context->psk_sni_entry_list[i].psk_credentials),
1696 "gnutls_credentials_set");
1698 &g_context->psk_sni_entry_list[i].psk_info.hint);
1700 &g_context->psk_sni_entry_list[i].psk_info.key);
1716post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1719 coap_gnutls_context_t *g_context =
1721 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
1722 int ret = GNUTLS_E_SUCCESS;
1725 if (g_context->setup_data.validate_sni_call_back) {
1732 name = gnutls_malloc(len);
1734 return GNUTLS_E_MEMORY_ERROR;
1737 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1738 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1740 new_name = gnutls_realloc(name, len);
1741 if (new_name ==
NULL) {
1742 ret = GNUTLS_E_MEMORY_ERROR;
1750 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1753 if (ret != GNUTLS_E_SUCCESS)
1756 if (type != GNUTLS_NAME_DNS)
1767 for (i = 0; i < g_context->pki_sni_count; i++) {
1768 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1772 if (i == g_context->pki_sni_count) {
1779 g_context->setup_data.validate_sni_call_back(name,
1780 g_context->setup_data.sni_call_back_arg));
1782 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1783 GNUTLS_A_UNRECOGNIZED_NAME));
1784 g_env->sent_alert = 1;
1785 ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
1789 g_context->pki_sni_entry_list = gnutls_realloc(
1790 g_context->pki_sni_entry_list,
1791 (i+1)*
sizeof(pki_sni_entry));
1792 g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name);
1793 g_context->pki_sni_entry_list[i].pki_key = *new_entry;
1794 sni_setup_data = g_context->setup_data;
1795 sni_setup_data.
pki_key = *new_entry;
1796 if ((ret = setup_pki_credentials(&g_context->pki_sni_entry_list[i].pki_credentials,
1801 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1802 GNUTLS_A_BAD_CERTIFICATE));
1803 g_env->sent_alert = 1;
1807 g_context->pki_sni_count++;
1809 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1810 g_context->pki_sni_entry_list[i].pki_credentials),
1811 "gnutls_credentials_set");
1823#if COAP_CLIENT_SUPPORT
1829setup_client_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1830 coap_gnutls_context_t *g_context =
1834 g_context->psk_pki_enabled |= IS_CLIENT;
1835 if (g_context->psk_pki_enabled & IS_PSK) {
1837 G_CHECK(gnutls_psk_allocate_client_credentials(&g_env->psk_cl_credentials),
1838 "gnutls_psk_allocate_client_credentials");
1839 gnutls_psk_set_client_credentials_function(g_env->psk_cl_credentials,
1840 psk_client_callback);
1841 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK,
1842 g_env->psk_cl_credentials),
1843 "gnutls_credentials_set");
1846 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1849 "gnutls_server_name_set");
1855 if (tls_version->
version >= 0x030604) {
1857 const char *priority;
1859 if (tls_version->
version >= 0x030606) {
1860 priority = VARIANTS_NO_TLS13_3_6_6;
1862 priority = VARIANTS_NO_TLS13_3_6_4;
1864 ret = gnutls_priority_set_direct(g_env->g_session,
1867 if (ret == GNUTLS_E_INVALID_REQUEST)
1868 coap_log_warn(
"gnutls_priority_set_direct: Syntax error at: %s\n", err);
1870 coap_log_warn(
"gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1877 if ((g_context->psk_pki_enabled & IS_PKI) ||
1878 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1885 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1899 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1900 g_context, setup_data,
1902 "setup_pki_credentials");
1904 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1905 g_env->pki_credentials),
1906 "gnutls_credentials_set");
1909 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1910 &g_context->alpn_proto, 1, 0),
1911 "gnutls_alpn_set_protocols");
1915 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1918 "gnutls_server_name_set");
1921 return GNUTLS_E_SUCCESS;
1928#if COAP_SERVER_SUPPORT
1937psk_server_callback(gnutls_session_t g_session,
1938 const char *identity,
1939 gnutls_datum_t *key) {
1942 coap_gnutls_context_t *g_context;
1947 if (c_session ==
NULL)
1951 if (g_context ==
NULL)
1953 setup_data = &c_session->
context->spsk_setup_data;
1957 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
1958 lidentity.
length = strlen((
const char *)lidentity.
s);
1962 (
int)lidentity.
length, (
const char *)lidentity.
s);
1974 if (psk_key ==
NULL)
1977 key->data = gnutls_malloc(psk_key->
length);
1978 if (key->data ==
NULL)
1980 memcpy(key->data, psk_key->
s, psk_key->
length);
1981 key->size = psk_key->
length;
1990setup_server_ssl_session(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1991 coap_gnutls_context_t *g_context =
1993 int ret = GNUTLS_E_SUCCESS;
1995 g_context->psk_pki_enabled |= IS_SERVER;
1996 if (g_context->psk_pki_enabled & IS_PSK) {
1997 G_CHECK(setup_psk_credentials(
1998 &g_env->psk_sv_credentials,
2000 &c_session->
context->spsk_setup_data),
2001 "setup_psk_credentials\n");
2002 G_CHECK(gnutls_credentials_set(g_env->g_session,
2004 g_env->psk_sv_credentials),
2005 "gnutls_credentials_set\n");
2006 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
2007 post_client_hello_gnutls_psk);
2010 if (g_context->psk_pki_enabled & IS_PKI) {
2012 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
2013 g_context, setup_data,
2015 "setup_pki_credentials");
2018 gnutls_certificate_server_set_request(g_env->g_session,
2019 GNUTLS_CERT_REQUIRE);
2021 gnutls_certificate_server_set_request(g_env->g_session,
2022 GNUTLS_CERT_REQUEST);
2024 gnutls_certificate_server_set_request(g_env->g_session,
2025 GNUTLS_CERT_IGNORE);
2028 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
2029 post_client_hello_gnutls_pki);
2031 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
2032 g_env->pki_credentials),
2033 "gnutls_credentials_set\n");
2035 return GNUTLS_E_SUCCESS;
2048coap_dgram_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2053 if (!c_session->
tls) {
2057 data = &((coap_gnutls_env_t *)c_session->
tls)->coap_ssl_data;
2060 if (data !=
NULL && data->pdu_len > 0) {
2061 if (outl < data->pdu_len) {
2062 memcpy(out, data->pdu, outl);
2064 if (!data->peekmode) {
2066 data->pdu_len -= outl;
2069 memcpy(out, data->pdu, data->pdu_len);
2070 ret = data->pdu_len;
2071 if (!data->peekmode) {
2091coap_dgram_write(gnutls_transport_ptr_t context,
const void *send_buffer,
2092 size_t send_buffer_length) {
2093 ssize_t result = -1;
2099 && c_session->endpoint ==
NULL
2107 send_buffer, send_buffer_length);
2108 if (result != (
int)send_buffer_length) {
2109 int keep_errno = errno;
2112 result, send_buffer_length);
2115 if (errno == ENOTCONN || errno == ECONNREFUSED)
2134receive_timeout(gnutls_transport_ptr_t context,
unsigned int ms
COAP_UNUSED) {
2138 fd_set readfds, writefds, exceptfds;
2140#if COAP_SERVER_SUPPORT
2147 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2151 g_env->coap_ssl_data.pdu_len > 0) {
2157 FD_ZERO(&exceptfds);
2158 FD_SET(fd, &readfds);
2159 if (!(g_env && g_env->doing_dtls_timeout)) {
2160 FD_SET(fd, &writefds);
2161 FD_SET(fd, &exceptfds);
2167 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2172static coap_gnutls_env_t *
2174 coap_gnutls_context_t *g_context =
2176 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2177#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2178 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2180 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2187 g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2191 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2193 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2195 gnutls_transport_set_pull_function(g_env->g_session, coap_dgram_read);
2196 gnutls_transport_set_push_function(g_env->g_session, coap_dgram_write);
2197 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2199 gnutls_transport_set_ptr(g_env->g_session, c_session);
2201 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2202 "gnutls_priority_set");
2204 if (type == GNUTLS_SERVER) {
2205#if COAP_SERVER_SUPPORT
2206 G_CHECK(setup_server_ssl_session(c_session, g_env),
2207 "setup_server_ssl_session");
2212#if COAP_CLIENT_SUPPORT
2213 G_CHECK(setup_client_ssl_session(c_session, g_env),
2214 "setup_client_ssl_session");
2220 gnutls_handshake_set_timeout(g_env->g_session,
2221 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2234coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context,
2235 coap_gnutls_env_t *g_env,
2236 coap_free_bye_t free_bye) {
2241 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2243 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2244 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2246 gnutls_deinit(g_env->g_session);
2247 g_env->g_session =
NULL;
2248 if (g_context->psk_pki_enabled & IS_PSK) {
2249 if ((g_context->psk_pki_enabled & IS_CLIENT) &&
2250 g_env->psk_cl_credentials !=
NULL) {
2251 gnutls_psk_free_client_credentials(g_env->psk_cl_credentials);
2252 g_env->psk_cl_credentials =
NULL;
2255 if (g_env->psk_sv_credentials !=
NULL)
2256 gnutls_psk_free_server_credentials(g_env->psk_sv_credentials);
2257 g_env->psk_sv_credentials =
NULL;
2260 if ((g_context->psk_pki_enabled & IS_PKI) ||
2261 (g_context->psk_pki_enabled &
2262 (IS_PSK | IS_PKI | IS_CLIENT)) == IS_CLIENT) {
2263 gnutls_certificate_free_credentials(g_env->pki_credentials);
2264 g_env->pki_credentials =
NULL;
2266 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2271#if COAP_SERVER_SUPPORT
2274 coap_gnutls_env_t *g_env =
2275 (coap_gnutls_env_t *)c_session->
tls;
2277 gnutls_transport_set_ptr(g_env->g_session, c_session);
2285 gnutls_session_t g_session) {
2286#if COAP_MAX_LOGGING_LEVEL > 0
2287 int last_alert = gnutls_alert_get(g_session);
2289 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2292 last_alert, gnutls_alert_get_name(last_alert));
2296 last_alert, gnutls_alert_get_name(last_alert));
2309do_gnutls_handshake(
coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2312 ret = gnutls_handshake(g_env->g_session);
2314 case GNUTLS_E_SUCCESS:
2315 g_env->established = 1;
2320 gnutls_transport_set_ptr(g_env->g_session, c_session);
2324 case GNUTLS_E_INTERRUPTED:
2328 case GNUTLS_E_AGAIN:
2332 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2336 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2338 g_env->sent_alert = 1;
2339 log_last_alert(c_session, g_env->g_session);
2341 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2342 case GNUTLS_E_UNEXPECTED_PACKET:
2346 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2347 log_last_alert(c_session, g_env->g_session);
2351 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2352#if (GNUTLS_VERSION_NUMBER > 0x030606)
2353 case GNUTLS_E_CERTIFICATE_REQUIRED:
2355 coap_log_warn(
"Client Certificate requested and required, but not provided\n"
2357 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2358 GNUTLS_A_BAD_CERTIFICATE));
2359 g_env->sent_alert = 1;
2363 case GNUTLS_E_DECRYPTION_FAILED:
2366 gnutls_strerror(ret));
2367 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2368 GNUTLS_A_DECRYPT_ERROR));
2369 g_env->sent_alert = 1;
2373 case GNUTLS_E_CERTIFICATE_ERROR:
2374 if (g_env->sent_alert) {
2380 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2381 case GNUTLS_E_NO_CIPHER_SUITES:
2382 case GNUTLS_E_INVALID_SESSION:
2385 gnutls_strerror(ret));
2386 if (!g_env->sent_alert) {
2387 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2388 GNUTLS_A_HANDSHAKE_FAILURE));
2389 g_env->sent_alert = 1;
2394 case GNUTLS_E_SESSION_EOF:
2395 case GNUTLS_E_PREMATURE_TERMINATION:
2396 case GNUTLS_E_TIMEDOUT:
2397 case GNUTLS_E_PULL_ERROR:
2398 case GNUTLS_E_PUSH_ERROR:
2404 "returned %d: '%s'\n",
2405 ret, gnutls_strerror(ret));
2412#if COAP_CLIENT_SUPPORT
2415 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2419 ret = do_gnutls_handshake(c_session, g_env);
2424 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2434 if (c_session && c_session->
context && c_session->
tls) {
2438 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2446 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2450 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2451 (
unsigned int)c_session->
mtu),
2452 "gnutls_dtls_set_data_mtu");
2464 const uint8_t *data,
size_t data_len) {
2466 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2468 if (g_env ==
NULL) {
2475 if (g_env->established) {
2476 ret = gnutls_record_send(g_env->g_session, data, data_len);
2480 case GNUTLS_E_AGAIN:
2483 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2485 g_env->sent_alert = 1;
2486 log_last_alert(c_session, g_env->g_session);
2492 "returned %d: '%s'\n",
2493 ret, gnutls_strerror(ret));
2502 ret = do_gnutls_handshake(c_session, g_env);
2533 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2536 if (g_env && g_env->g_session) {
2537 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2550 g_env->last_timeout = now - rem_ms;
2551 return now + rem_ms;
2563 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2566 g_env->doing_dtls_timeout = 1;
2568 (do_gnutls_handshake(c_session, g_env) < 0)) {
2570 g_env->doing_dtls_timeout = 0;
2574 g_env->doing_dtls_timeout = 0;
2587 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2589 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2593 assert(g_env !=
NULL);
2595 if (ssl_data->pdu_len)
2596 coap_log_err(
"** %s: Previous data not read %u bytes\n",
2598 ssl_data->pdu = data;
2599 ssl_data->pdu_len = data_len;
2602 if (g_env->established) {
2603 ret = gnutls_record_recv(g_env->g_session, pdu, (
int)
sizeof(pdu));
2608 }
else if (ret == 0) {
2612 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2614 g_env->sent_alert = 1;
2615 log_last_alert(c_session, g_env->g_session);
2619 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2620 log_last_alert(c_session, g_env->g_session);
2625 coap_log_warn(
"coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2631 ret = do_gnutls_handshake(c_session, g_env);
2636 if (ssl_data->pdu_len && !g_env->sent_alert) {
2638 ret = do_gnutls_handshake(c_session, g_env);
2656 if (ssl_data && ssl_data->pdu_len) {
2658 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2659 ssl_data->pdu_len = 0;
2660 ssl_data->pdu =
NULL;
2665#if COAP_SERVER_SUPPORT
2673 const uint8_t *data,
2676 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2677 coap_ssl_t *ssl_data;
2681 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2683 c_session->
tls = g_env;
2684 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2685 GNUTLS_COOKIE_KEY_SIZE);
2692 gnutls_dtls_prestate_st prestate;
2695 memset(&prestate, 0,
sizeof(prestate));
2697 memcpy(&data_rw, &data,
sizeof(data_rw));
2698 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2705 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2713 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2716 ssl_data = &g_env->coap_ssl_data;
2717 ssl_data->pdu = data;
2718 ssl_data->pdu_len = data_len;
2720 ret = do_gnutls_handshake(c_session, g_env);
2727 g_env, COAP_FREE_BYE_NONE);
2736 if (ssl_data && ssl_data->pdu_len) {
2738 coap_log_debug(
"coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len);
2739 ssl_data->pdu_len = 0;
2740 ssl_data->pdu =
NULL;
2751#if !COAP_DISABLE_TCP
2759coap_sock_read(gnutls_transport_ptr_t context,
void *out,
size_t outl) {
2781coap_sock_write(gnutls_transport_ptr_t context,
const void *in,
size_t inl) {
2790 (errno == EPIPE || errno == ECONNRESET)) {
2816#if COAP_CLIENT_SUPPORT
2819 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2820 coap_gnutls_context_t *g_context =
2822#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2823 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2825 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2832 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2834 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2836 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2837 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2838 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2840 gnutls_transport_set_ptr(g_env->g_session, c_session);
2842 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2843 setup_client_ssl_session(c_session, g_env);
2845 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2847 c_session->
tls = g_env;
2848 do_gnutls_handshake(c_session, g_env);
2858#if COAP_SERVER_SUPPORT
2861 coap_gnutls_env_t *g_env = gnutls_malloc(
sizeof(coap_gnutls_env_t));
2862 coap_gnutls_context_t *g_context =
2864#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2865 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2867 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2873 memset(g_env, 0,
sizeof(coap_gnutls_env_t));
2875 G_CHECK(gnutls_init(&g_env->g_session, flags),
"gnutls_init");
2877 gnutls_transport_set_pull_function(g_env->g_session, coap_sock_read);
2878 gnutls_transport_set_push_function(g_env->g_session, coap_sock_write);
2879 gnutls_transport_set_pull_timeout_function(g_env->g_session, receive_timeout);
2881 gnutls_transport_set_ptr(g_env->g_session, c_session);
2883 setup_server_ssl_session(c_session, g_env);
2885 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2886 gnutls_handshake_set_timeout(g_env->g_session,
2887 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2889 c_session->
tls = g_env;
2890 do_gnutls_handshake(c_session, g_env);
2913 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2915 assert(g_env !=
NULL);
2918 if (g_env->established) {
2919 ret = gnutls_record_send(g_env->g_session, data, data_len);
2923 case GNUTLS_E_AGAIN:
2926 case GNUTLS_E_PUSH_ERROR:
2927 case GNUTLS_E_PULL_ERROR:
2928 case GNUTLS_E_PREMATURE_TERMINATION:
2931 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2933 g_env->sent_alert = 1;
2934 log_last_alert(c_session, g_env->g_session);
2939 "returned %d: '%s'\n",
2940 ret, gnutls_strerror(ret));
2949 ret = do_gnutls_handshake(c_session, g_env);
2966 if (ret == (ssize_t)data_len)
2983 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->
tls;
2992 if (!g_env->established && !g_env->sent_alert) {
2993 ret = do_gnutls_handshake(c_session, g_env);
2999 ret = gnutls_record_recv(g_env->g_session, data, (
int)data_len);
3005 case GNUTLS_E_AGAIN:
3009 case GNUTLS_E_PULL_ERROR:
3012 case GNUTLS_E_FATAL_ALERT_RECEIVED:
3014 g_env->sent_alert = 1;
3015 log_last_alert(c_session, g_env->g_session);
3018 case GNUTLS_E_WARNING_ALERT_RECEIVED:
3019 log_last_alert(c_session, g_env->g_session);
3024 "returned %d: '%s'\n",
3025 ret, gnutls_strerror(ret));
3048#if COAP_SERVER_SUPPORT
3050coap_digest_setup(
void) {
3051 gnutls_hash_hd_t digest_ctx;
3053 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
3060coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3062 gnutls_hash_deinit(digest_ctx,
NULL);
3066coap_digest_update(coap_digest_ctx_t *digest_ctx,
3067 const uint8_t *data,
3069 int ret = gnutls_hash(digest_ctx, data, data_len);
3075coap_digest_final(coap_digest_ctx_t *digest_ctx,
3076 coap_digest_t *digest_buffer) {
3077 gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer);
3079 coap_digest_free(digest_ctx);
3084#if COAP_WS_SUPPORT || COAP_OSCORE_GROUP_SUPPORT
3089static struct hash_algs {
3091 gnutls_digest_algorithm_t dig_type;
3099static gnutls_digest_algorithm_t
3100get_hash_alg(
cose_alg_t alg,
size_t *hash_len) {
3103 for (idx = 0; idx <
sizeof(hashes) /
sizeof(
struct hash_algs); idx++) {
3104 if (hashes[idx].alg == alg) {
3105 *hash_len = hashes[idx].dig_size;
3106 return hashes[idx].dig_type;
3109 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3110 return GNUTLS_DIG_UNKNOWN;
3118 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3119 gnutls_hash_hd_t digest_ctx;
3123 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3124 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3128 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3131 ret = gnutls_hash(digest_ctx, data->
s, data->
length);
3138 gnutls_hash_output(digest_ctx,
dummy->s);
3141 gnutls_hash_deinit(digest_ctx,
NULL);
3146 gnutls_hash_deinit(digest_ctx,
NULL);
3151#if COAP_OSCORE_SUPPORT
3159#if COAP_OSCORE_GROUP_SUPPORT
3168#if COAP_OSCORE_GROUP_SUPPORT
3180static struct cipher_algs {
3182 gnutls_cipher_algorithm_t cipher_type;
3187static gnutls_cipher_algorithm_t
3191 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3192 if (ciphers[idx].alg == alg)
3193 return ciphers[idx].cipher_type;
3195 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3204static struct hmac_algs {
3206 gnutls_mac_algorithm_t hmac_type;
3212static gnutls_mac_algorithm_t
3216 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3217 if (hmacs[idx].hmac_alg == hmac_alg)
3218 return hmacs[idx].hmac_type;
3220 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3224#if COAP_OSCORE_GROUP_SUPPORT
3231static struct curve_algs {
3233 gnutls_ecc_curve_t curve;
3242static gnutls_ecc_curve_t
3246 for (idx = 0; idx <
sizeof(curves) /
sizeof(
struct curve_algs); idx++) {
3247 if (curves[idx].alg == alg)
3248 return curves[idx].curve;
3250 coap_log_debug(
"get_curve_alg: COSE curve %d not supported\n", alg);
3255get_alg_curve(gnutls_ecc_curve_t curve) {
3258 for (idx = 0; idx <
sizeof(curves) /
sizeof(
struct curve_algs); idx++) {
3259 if (curves[idx].curve == curve)
3260 return curves[idx].alg;
3262 coap_log_debug(
"get_alg_curve: GnuTLS curve %d not supported\n", curve);
3267get_alg_hash(gnutls_digest_algorithm_t dig_type) {
3270 for (idx = 0; idx <
sizeof(hashes) /
sizeof(
struct hash_algs); idx++) {
3271 if (hashes[idx].dig_type == dig_type)
3272 return hashes[idx].alg;
3274 coap_log_debug(
"get_alg_hash: GnuTLS hash %d not supported\n", dig_type);
3280 return get_curve_alg(alg) != 0;
3287 return get_hash_alg(alg, &hash_length) != 0;
3293 return get_cipher_alg(alg);
3302 return get_hmac_alg(hmac_alg);
3310 size_t *max_result_len) {
3311 gnutls_aead_cipher_hd_t ctx;
3315 size_t result_len = *max_result_len;
3316 gnutls_cipher_algorithm_t algo;
3318 uint8_t *key_data_rw;
3324 assert(params !=
NULL);
3328 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3329 coap_log_debug(
"coap_crypto_encrypt: algorithm %d not supported\n",
3333 tag_size = gnutls_cipher_get_tag_size(algo);
3337 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3338 key.data = key_data_rw;
3348 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3350 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3360 "gnutls_aead_cipher_encrypt");
3361 *max_result_len = result_len;
3364 gnutls_aead_cipher_deinit(ctx);
3365 return ret == 1 ? 1 : 0;
3373 size_t *max_result_len) {
3374 gnutls_aead_cipher_hd_t ctx;
3378 size_t result_len = *max_result_len;
3379 gnutls_cipher_algorithm_t algo;
3381 uint8_t *key_data_rw;
3387 assert(params !=
NULL);
3392 if ((algo = get_cipher_alg(params->
alg)) == 0) {
3393 coap_log_debug(
"coap_crypto_decrypt: algorithm %d not supported\n",
3397 tag_size = gnutls_cipher_get_tag_size(algo);
3401 memcpy(&key_data_rw, &ccm->
key.
s,
sizeof(key_data_rw));
3402 key.data = key_data_rw;
3412 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key),
"gnutls_aead_cipher_init");
3414 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3424 "gnutls_aead_cipher_decrypt");
3425 *max_result_len = result_len;
3428 gnutls_aead_cipher_deinit(ctx);
3429 return ret == 1 ? 1 : 0;
3437 gnutls_hmac_hd_t ctx;
3440 gnutls_mac_algorithm_t mac_algo;
3446 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3447 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3450 len = gnutls_hmac_get_len(mac_algo);
3457 G_CHECK(gnutls_hmac_init(&ctx, mac_algo, key->
s, key->
length),
3458 "gnutls_hmac_init");
3459 G_CHECK(gnutls_hmac(ctx, data->
s, data->
length),
"gnutls_hmac");
3460 gnutls_hmac_output(ctx,
dummy->s);
3466 gnutls_hmac_deinit(ctx,
NULL);
3467 return ret == 1 ? 1 : 0;
3470#if COAP_OSCORE_GROUP_SUPPORT
3472coap_crypto_read_pem_private_key(
const char *filename,
3474 gnutls_privkey_t priv_key =
NULL;
3475 gnutls_x509_privkey_t priv_x509 =
NULL;
3478 gnutls_datum_t data = {
NULL, 0};
3479 gnutls_datum_t key = {
NULL, 0};
3485 G_CHECK(gnutls_privkey_init(&priv_key),
"gnutls_privkey_init");
3486 G_CHECK(gnutls_load_file(filename, &data),
"gnutls_load_file");
3487 G_CHECK(gnutls_privkey_import_x509_raw(priv_key,
3489 GNUTLS_X509_FMT_PEM,
3491 GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE),
3492 "gnutls_privkey_import_x509_raw");
3493 G_CHECK(gnutls_privkey_verify_params(priv_key),
3494 "gnutls_privkey_verify_params");
3495 G_CHECK(gnutls_privkey_export_x509(priv_key, &priv_x509),
3496 "gnutls_privkey_export_x509");
3497 G_CHECK(gnutls_x509_privkey_export2_pkcs8(priv_x509,
3498 GNUTLS_X509_FMT_DER,
3502 "gnutls_x509_privkey_export2_pkcs8");
3514 gnutls_privkey_deinit(priv_key);
3516 gnutls_x509_privkey_deinit(priv_x509);
3518 gnutls_free(data.data);
3519 gnutls_free(key.data);
3520 return ret == 1 ? 1 : 0;
3526 gnutls_privkey_t priv_key =
NULL;
3529 gnutls_datum_t data = {
NULL, 0};
3536 G_CHECK(gnutls_privkey_init(&priv_key),
"gnutls_privkey_init");
3537 data.size = binary->
length;
3539 memcpy(&data.data, &binary->
s,
sizeof(data.data));
3540 G_CHECK(gnutls_privkey_import_x509_raw(priv_key,
3542 GNUTLS_X509_FMT_DER,
3544 GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE),
3545 "gnutls_privkey_import_x509_raw");
3546 G_CHECK(gnutls_privkey_verify_params(priv_key),
3547 "gnutls_privkey_verify_params");
3558 gnutls_privkey_deinit(priv_key);
3561 return ret == 1 ? 1 : 0;
3578 gnutls_x509_privkey_t priv_x509 =
NULL;
3582 if (new_key ==
NULL)
3590 G_CHECK(gnutls_privkey_export_x509(new_key->
key_tls, &priv_x509),
3591 "gnutls_privkey_export_x509");
3592 G_CHECK(gnutls_privkey_import_x509(key->
key_tls,
3594 GNUTLS_PRIVKEY_IMPORT_COPY),
3595 "gnutls_privkey_import_x509");
3600 gnutls_x509_privkey_deinit(priv_x509);
3601 gnutls_free(new_key);
3609 gnutls_privkey_deinit(key->
key_tls);
3616coap_crypto_read_pem_public_cred(
const char *filename,
3618 gnutls_pubkey_t pub_key =
NULL;
3619 gnutls_privkey_t priv_key =
NULL;
3622 gnutls_datum_t data = {
NULL, 0};
3623 gnutls_datum_t key = {
NULL, 0};
3624 gnutls_digest_algorithm_t hash;
3625 unsigned int mand = 0;
3626 gnutls_ecc_curve_t curve = 0;
3632 G_CHECK(gnutls_pubkey_init(&pub_key),
"gnutls_pubkey_init");
3633 G_CHECK(gnutls_load_file(filename, &data),
"gnutls_load_file");
3634 if (strstr((
char *)data.data,
"PUBLIC KEY") ==
NULL) {
3635 G_CHECK(gnutls_privkey_init(&priv_key),
"gnutls_privkey_init");
3636 G_CHECK(gnutls_privkey_import_x509_raw(priv_key,
3638 GNUTLS_X509_FMT_PEM,
3640 GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE),
3641 "gnutls_privkey_import_x509_raw");
3642 G_CHECK(gnutls_privkey_verify_params(priv_key),
3643 "gnutls_privkey_verify_params");
3644 G_CHECK(gnutls_pubkey_import_privkey(pub_key, priv_key, 0, 0),
3645 "gnutls_pubkey_import_privkey");
3648 gnutls_pubkey_import_x509_raw(pub_key, &data, GNUTLS_X509_FMT_PEM, 0),
3649 "gnutls_pubkey_import_x509_raw");
3651 G_CHECK(gnutls_pubkey_verify_params(pub_key),
"gnutls_pubkey_verify_params");
3652 G_CHECK(gnutls_pubkey_export2(pub_key, GNUTLS_X509_FMT_DER, &key),
3653 "gnutls_pubkey_export2");
3657 G_CHECK(gnutls_pubkey_get_preferred_hash_algorithm(pub_key, &hash, &mand),
3658 "gnutls_pubkey_get_preferred_hash_algorithm");
3661 switch (gnutls_pubkey_get_pk_algorithm(pub_key,
NULL)) {
3662 case GNUTLS_PK_EDDSA_ED25519:
3664 case GNUTLS_PK_ECDSA:
3670 G_CHECK(gnutls_pubkey_export_ecc_raw2(pub_key, &curve,
NULL,
NULL, 0),
3671 "gnutls_pubkey_export_ecc_raw2");
3681 gnutls_privkey_deinit(priv_key);
3683 gnutls_pubkey_deinit(pub_key);
3685 gnutls_free(data.data);
3686 gnutls_free(key.data);
3687 return ret == 1 ? 1 : 0;
3693 gnutls_pubkey_t pub_key =
NULL;
3694 gnutls_privkey_t priv_key =
NULL;
3697 gnutls_datum_t data = {
NULL, 0};
3698 gnutls_datum_t key = {
NULL, 0};
3699 gnutls_digest_algorithm_t hash;
3700 unsigned int mand = 0;
3701 gnutls_ecc_curve_t curve = 0;
3707 G_CHECK(gnutls_pubkey_init(&pub_key),
"gnutls_pubkey_init");
3708 data.size = binary->
length;
3710 memcpy(&data.data, &binary->
s,
sizeof(data.data));
3711 if (data.data[2] == 0x02) {
3713 G_CHECK(gnutls_privkey_init(&priv_key),
"gnutls_privkey_init");
3714 G_CHECK(gnutls_privkey_import_x509_raw(priv_key,
3716 GNUTLS_X509_FMT_DER,
3718 GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE),
3719 "gnutls_privkey_import_x509_raw");
3720 G_CHECK(gnutls_privkey_verify_params(priv_key),
3721 "gnutls_privkey_verify_params");
3722 G_CHECK(gnutls_pubkey_import_privkey(pub_key, priv_key, 0, 0),
3723 "gnutls_pubkey_import_privkey");
3725 G_CHECK(gnutls_pubkey_import(pub_key, &data, GNUTLS_X509_FMT_DER),
3726 "gnutls_pubkey_import");
3728 G_CHECK(gnutls_pubkey_verify_params(pub_key),
"gnutls_pubkey_verify_params");
3729 G_CHECK(gnutls_pubkey_export2(pub_key, GNUTLS_X509_FMT_DER, &key),
3730 "gnutls_pubkey_export2");
3734 G_CHECK(gnutls_pubkey_get_preferred_hash_algorithm(pub_key, &hash, &mand),
3735 "gnutls_pubkey_get_preferred_hash_algorithm");
3738 switch (gnutls_pubkey_get_pk_algorithm(pub_key,
NULL)) {
3739 case GNUTLS_PK_EDDSA_ED25519:
3741 case GNUTLS_PK_ECDSA:
3747 G_CHECK(gnutls_pubkey_export_ecc_raw2(pub_key, &curve,
NULL,
NULL, 0),
3748 "gnutls_pubkey_export_ecc_raw2");
3758 gnutls_privkey_deinit(priv_key);
3760 gnutls_pubkey_deinit(pub_key);
3761 gnutls_free(binary);
3762 gnutls_free(key.data);
3764 return ret == 1 ? 1 : 0;
3781 gnutls_pubkey_t pub_key =
NULL;
3783 gnutls_datum_t data = {
NULL, 0};
3787 if (new_key ==
NULL)
3795 G_CHECK(gnutls_pubkey_init(&pub_key),
"gnutls_pubkey_init");
3798 memcpy(&data.data, &new_key->
pub_der->
s,
sizeof(data.data));
3800 gnutls_pubkey_import_x509_raw(pub_key, &data, GNUTLS_X509_FMT_DER, 0),
3801 "gnutls_pubkey_import_x509_raw");
3802 G_CHECK(gnutls_pubkey_verify_params(pub_key),
3803 "gnutls_pubkey_verify_params");
3809 gnutls_pubkey_deinit(pub_key);
3810 gnutls_free(new_key);
3818 gnutls_pubkey_deinit(key->
key_tls);
3831 gnutls_digest_algorithm_t gnutls_dig = get_hash_alg(hash, &hash_length);
3833 gnutls_privkey_t priv_key = private_key->
key_tls;
3834 gnutls_datum_t data;
3835 gnutls_datum_t sign;
3839 if (gnutls_dig == 0)
3842 data.size = text->
length;
3844 memcpy(&data.data, &text->
s,
sizeof(data.data));
3845 sign.size = signature->
length;
3846 sign.data = signature->
s;
3848 gnutls_privkey_sign_data(priv_key, gnutls_dig, 0, &data, &sign),
3849 "gnutls_privkey_sign_data");
3850 switch (gnutls_privkey_get_pk_algorithm(priv_key,
NULL)) {
3851 case GNUTLS_PK_ECDSA:
3852 sign_b.
s = sign.data;
3853 sign_b.
length = sign.size;
3859 gnutls_free(sign.data);
3861 memcpy(signature->
s, sign_bp->
s, sign_bp->
length);
3867 gnutls_privkey_get_pk_algorithm(priv_key,
NULL));
3869 case GNUTLS_PK_EDDSA_ED25519:
3870 assert(signature->
length >= sign.size);
3871 memcpy(signature->
s, sign.data, sign.size);
3872 signature->
length = sign.size;
3873 gnutls_free(sign.data);
3889 gnutls_digest_algorithm_t gnutls_dig = get_hash_alg(hash, &hash_length);
3891 gnutls_pubkey_t pub_key = public_cred->
key_tls;
3892 gnutls_datum_t data;
3893 gnutls_datum_t sign;
3896 if (gnutls_dig == 0)
3902 data.size = text->
length;
3904 memcpy(&data.data, &text->
s,
sizeof(data.data));
3905 switch (gnutls_pubkey_get_pk_algorithm(pub_key,
NULL)) {
3906 case GNUTLS_PK_ECDSA:
3909 if (sign_bp ==
NULL) {
3916 sign.size = sign_bp->
length;
3917 sign.data = sign_bp->
s;
3921 gnutls_pubkey_get_pk_algorithm(pub_key,
NULL));
3923 case GNUTLS_PK_EDDSA_ED25519:
3924 sign.size = signature->
length;
3925 sign.data = signature->
s;
3929 G_CHECK(gnutls_pubkey_get_pk_algorithm(pub_key,
NULL),
3930 "gnutls_pubkey_get_pk_algorithm");
3931 G_CHECK(gnutls_pubkey_verify_data2(pub_key, gnutls_pk_to_sign(ret,
3932 gnutls_dig), 0, &data, &sign),
3933 "gnutls_pubkey_verify_data2");
3935 G_CHECK(gnutls_pubkey_verify_data2(pub_key,
3936 GNUTLS_SIGN_EDDSA_ED25519,
3940 "gnutls_pubkey_verify_data2");
3946 return ret == 1 ? 1 : 0;
3960coap_crypto_derive_shared_secret(
cose_alg_t alg_pairwise,
3965 (void)raw_local_private;
3966 (void)raw_peer_public;
3967 (void)shared_secret;
3981#pragma GCC diagnostic ignored "-Wunused-function"
#define COAP_SERVER_SUPPORT
const char * coap_socket_strerror(void)
#define COAP_RXBUFFER_SIZE
Library specific build wrapper for coap_internal.h.
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED)
static coap_log_t dtls_log_level
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
coap_binary_t * coap_asn1_split_r_s(coap_binary_t *asn1, size_t size)
Split the ASN.1 signature into the r and s components.
coap_binary_t * coap_asn1_r_s_join(coap_binary_t *r_s)
Join the r and s components into ASN1.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
#define COAP_DTLS_RETRANSMIT_MS
int coap_dtls_define_issue(coap_define_issue_key_t type, coap_define_issue_fail_t fail, coap_dtls_key_t *key, const coap_dtls_role_t role, int ret)
Report PKI DEFINE type issue.
int coap_pki_name_match_sni(const char *name_entry, size_t name_length, const char *sni_match)
Check if the PKI name entry is a (wildcard) match for the requested SNI.
void coap_dtls_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
#define COAP_DTLS_RETRANSMIT_COAP_TICKS
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
#define COAP_DTLS_RETRANSMIT_TOTAL_MS
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
#define COAP_DTLS_HINT_LENGTH
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
struct coap_dtls_key_t coap_dtls_key_t
The structure that holds the PKI key information.
#define COAP_DTLS_RPK_CERT_CN
struct coap_dtls_spsk_info_t coap_dtls_spsk_info_t
The structure that holds the Server Pre-Shared Key and Identity Hint information.
struct coap_dtls_pki_t coap_dtls_pki_t
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
@ COAP_TLS_LIBRARY_GNUTLS
Using GnuTLS library.
@ COAP_EVENT_DTLS_CLOSED
Triggerrd when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_dtls_log(level,...)
Logging function.
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
size_t cose_hash_len(cose_alg_t cose_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
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
The session is to be disconnected (a send on it has failed, the peer or the (D)TLS layer has closed i...
#define COAP_PROTO_NOT_RELIABLE(p)
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_NONE
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_oscore_group_is_supported(void)
Check whether OSCORE Group support is available.
int coap_oscore_pairwise_is_supported(void)
Check whether OSCORE Pairwise support is available.
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
The CoAP stack's global state is stored in a coap_context_t object.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
Holds credential definitions.
size_t wire_sign_size
Size of signature on wire.
coap_bin_const_t * auth_cred
Authentication Credentials of type type.
cose_curve_t sign_curve
Sign Curve.
coap_bin_const_t * pub_der
DER Value of the public key abstracted from key_tls.
void * key_tls
Public Key in the used TLS format.
cose_alg_t sign_hash
Sign Hash Algorithm.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@250042334257003036060227022037266106077265366262 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorithm to use.
Holds private key definitions.
coap_bin_const_t * pri_der
DER Value of the private key abstracted from key_tls.
void * key_tls
Private Key in the used TLS format.
The structure that holds the Client PSK information.
coap_bin_const_t identity
The structure used for defining the Client PSK setup data to be used.
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
The structure that holds the PKI key information.
coap_pki_key_define_t define
for definable type keys
union coap_dtls_key_t::@165145277033177166324225120075242371261276053256 key
coap_pki_key_t key_type
key format type
The structure used for defining the PKI setup data to be used.
uint8_t allow_no_crl
1 ignore if CRL not there
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
uint8_t check_cert_revocation
1 if revocation checks wanted
uint8_t cert_chain_verify_depth
recommended depth is 3
uint8_t allow_expired_certs
1 if expired certs are allowed
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
char * client_sni
If not NULL, SNI to use in client TLS setup.
uint8_t allow_self_signed
1 if self-signed certs are allowed.
uint8_t allow_expired_crl
1 if expired crl is allowed
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
coap_dtls_key_t pki_key
PKI key definition.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
coap_dtls_spsk_info_t psk_info
Server PSK definition.
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
const char * user_pin
define: User pin to access type PKCS11.
coap_const_char_ptr_t private_key
define: Private Key
coap_const_char_ptr_t ca
define: Common CA Certificate
size_t public_cert_len
define Public Cert length (if needed)
size_t ca_len
define CA Cert length (if needed)
coap_pki_define_t private_key_def
define: Private Key type definition
size_t private_key_len
define Private Key length (if needed)
coap_pki_define_t ca_def
define: Common CA type definition
coap_pki_define_t public_cert_def
define: Public Cert type definition
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
The structure used for returning the underlying (D)TLS library information.
uint64_t built_version
(D)TLS Built against Library Version
coap_tls_library_t type
Library type.
uint64_t version
(D)TLS runtime Library Version
const char * s_byte
signed char ptr
const uint8_t * u_byte
unsigned char ptr