libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
coap_gnutls.c
Go to the documentation of this file.
1/*
2 * coap_gnutls.c -- GnuTLS Datagram Transport Layer Support for libcoap
3 *
4 * Copyright (C) 2017 Dag Bjorklund <dag.bjorklund@comsel.fi>
5 * Copyright (C) 2018-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
17
18/*
19 * Naming used to prevent confusion between coap sessions, gnutls sessions etc.
20 * when reading the code.
21 *
22 * c_context A coap_context_t *
23 * c_session A coap_session_t *
24 * g_context A coap_gnutls_context_t * (held in c_context->dtls_context)
25 * g_session A gnutls_session_t (which has the * in the typedef)
26 * g_env A coap_gnutls_env_t * (held in c_session->tls)
27 */
28
29/*
30 * Notes
31 *
32 * There is a memory leak in GnuTLS prior to 3.3.26 when hint is not freed off
33 * when server psk credentials are freed off.
34 *
35 * ca_path in coap_dtls_context_set_pki_root_cas() is not supported until 3.3.6
36 *
37 * Identity Hint is not provided if using DH and versions prior to 3.4.4
38 *
39 * 3.5.5 or later is required to interoperate with TinyDTLS as CCM algorithm
40 * support is required.
41 *
42 * TLS 1.3 is properly supported from 3.6.5 onwards
43 * (but is not enabled by default in 3.6.4)
44 *
45 * Starting with 3.6.3, fixed in 3.6.13, Client Hellos may fail with some
46 * server implementations (e.g. Californium) as random value is all zeros
47 * - CVE-2020-11501 - a security weakness.
48 * 3.6.6 or later is required to support Raw Public Key(RPK)
49 */
50
52
53#if COAP_WITH_LIBGNUTLS
54
55#define MIN_GNUTLS_VERSION "3.3.0"
56
57#include <stdio.h>
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>
64#include <unistd.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
72#endif /* GNUTLS_VERSION_NUMBER >= 0x030606 */
73
74#ifndef GNUTLS_CRT_RAW
75#define GNUTLS_CRT_RAW GNUTLS_CRT_RAWPK
76#endif /* GNUTLS_CRT_RAW */
77
78#ifdef _WIN32
79#define strcasecmp _stricmp
80#endif
81
82typedef struct coap_ssl_t {
83 const uint8_t *pdu;
84 unsigned pdu_len;
85 unsigned peekmode;
86 gnutls_datum_t cookie_key;
87} coap_ssl_t;
88
89/*
90 * This structure encapsulates the GnuTLS session object.
91 * It handles both TLS and DTLS.
92 * c_session->tls points to this.
93 */
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;
100 /* If not set, need to do gnutls_handshake */
101 int established;
102 int doing_dtls_timeout;
103 coap_tick_t last_timeout;
104 int sent_alert;
105} coap_gnutls_env_t;
106
107#define IS_PSK (1 << 0)
108#define IS_PKI (1 << 1)
109#define IS_CLIENT (1 << 6)
110#define IS_SERVER (1 << 7)
111
112typedef struct pki_sni_entry {
113 char *sni;
114 coap_dtls_key_t pki_key;
115 gnutls_certificate_credentials_t pki_credentials;
116} pki_sni_entry;
117
118typedef struct psk_sni_entry {
119 char *sni;
120 coap_dtls_spsk_info_t psk_info;
121 gnutls_psk_server_credentials_t psk_credentials;
122} psk_sni_entry;
123
124typedef struct coap_gnutls_context_t {
125 coap_dtls_pki_t setup_data;
126 int psk_pki_enabled;
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; /* Will be "coap", but that is a const */
132 char *root_ca_file;
133 char *root_ca_path;
134 int trust_store_defined;
135 gnutls_priority_t priority_cache;
136} coap_gnutls_context_t;
137
138typedef enum coap_free_bye_t {
139 COAP_FREE_BYE_AS_TCP,
140 COAP_FREE_BYE_AS_UDP,
141 COAP_FREE_BYE_NONE
142} coap_free_bye_t;
143
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"
147
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"
150
151#define G_ACTION(xx) do { \
152 ret = (xx); \
153 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
154
155#define G_CHECK(xx,func) do { \
156 if ((ret = (xx)) < 0) { \
157 coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \
158 goto fail; \
159 } \
160 } while (0)
161
162#define G_ACTION_CHECK(xx,func) do { \
163 G_ACTION(xx); \
164 G_CHECK(xx, func); \
165 } while 0
166
168
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);
175#endif /* COAP_SERVER_SUPPORT */
176
177/*
178 * return 0 failed
179 * 1 passed
180 */
181int
183 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
184 coap_log_err("GnuTLS " MIN_GNUTLS_VERSION " or later is required\n");
185 return 0;
186 }
187 return 1;
188}
189
190/*
191 * return 0 failed
192 * 1 passed
193 */
194int
196#if !COAP_DISABLE_TCP
197 if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
198 coap_log_err("GnuTLS " MIN_GNUTLS_VERSION " or later is required\n");
199 return 0;
200 }
201 return 1;
202#else /* COAP_DISABLE_TCP */
203 return 0;
204#endif /* COAP_DISABLE_TCP */
205}
206
207/*
208 * return 0 failed
209 * 1 passed
210 */
211int
213 return 1;
214}
215
216/*
217 * return 0 failed
218 * 1 passed
219 */
220int
222 return 1;
223}
224
225/*
226 * return 0 failed
227 * 1 passed
228 */
229int
231 return 1;
232}
233
234/*
235 * return 0 failed
236 * 1 passed
237 */
238int
240#if (GNUTLS_VERSION_NUMBER >= 0x030606)
241 return 1;
242#else /* GNUTLS_VERSION_NUMBER < 0x030606 */
243 return 0;
244#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
245}
246
247/*
248 * return 0 failed
249 * 1 passed
250 */
251int
253 return 0;
254}
255
256#if COAP_CLIENT_SUPPORT
257int
258coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
259 (void)c_context;
260 (void)every;
261 return 0;
262}
263#endif /* COAP_CLIENT_SUPPORT */
264
267 static coap_tls_version_t version;
268 const char *vers = gnutls_check_version(NULL);
269
270 version.version = 0;
271 if (vers) {
272 int p1, p2, p3;
273
274 sscanf(vers, "%d.%d.%d", &p1, &p2, &p3);
275 version.version = (p1 << 16) | (p2 << 8) | p3;
276 }
277 version.built_version = GNUTLS_VERSION_NUMBER;
279 return &version;
280}
281
282static void
283coap_gnutls_audit_log_func(gnutls_session_t g_session, const char *text) {
284#if COAP_MAX_LOGGING_LEVEL > 0
285 if (g_session) {
286 coap_session_t *c_session =
287 (coap_session_t *)gnutls_transport_get_ptr(g_session);
288 coap_log_warn("** %s: %s",
289 coap_session_str(c_session), text);
290 } else {
291 coap_log_warn("** (null): %s", text);
292 }
293#else /* COAP_MAX_LOGGING_LEVEL == 0 */
294 (void)g_session;
295 (void)text;
296#endif /* COAP_MAX_LOGGING_LEVEL == 0 */
297}
298
299static void
300coap_gnutls_log_func(int level, const char *text) {
301 /* Things get noisy, even at level 1 */
302 if (level > 0)
303 level += COAP_LOG_WARN;
304 if (level > COAP_LOG_DEBUG)
305 level = COAP_LOG_DEBUG;
306 coap_dtls_log(level, "%s", text);
307}
308
309/*
310 * return 0 failed
311 * 1 passed
312 */
313int
315 const coap_dtls_pki_t *setup_data,
316 const coap_dtls_role_t role COAP_UNUSED) {
317 coap_dtls_key_t key;
318 coap_gnutls_context_t *g_context =
319 ((coap_gnutls_context_t *)c_context->dtls_context);
320
321 if (!g_context || !setup_data)
322 return 0;
323
324 g_context->setup_data = *setup_data;
325 if (!g_context->setup_data.verify_peer_cert) {
326 /* Needs to be clear so that no CA DNs are transmitted */
327 g_context->setup_data.check_common_ca = 0;
328 if (g_context->setup_data.is_rpk_not_cert) {
329 /* Disable all of these as they cannot be checked */
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;
339 } else {
340 /* Allow all of these but warn if issue */
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;
350 }
351 }
352 /* Map over to the new define format to save code duplication */
353 coap_dtls_map_key_type_to_define(&g_context->setup_data, &key);
354 g_context->setup_data.pki_key = key;
355 g_context->psk_pki_enabled |= IS_PKI;
356 if (setup_data->use_cid) {
357 coap_log_warn("GnuTLS has no Connection-ID support\n");
358 }
359 return 1;
360}
361
362/*
363 * return 0 failed
364 * 1 passed
365 */
366int
368 const char *ca_file,
369 const char *ca_path) {
370 coap_gnutls_context_t *g_context =
371 ((coap_gnutls_context_t *)c_context->dtls_context);
372 if (!g_context) {
373 coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
374 "not set up\n");
375 return 0;
376 }
377
378 if (ca_file == NULL && ca_path == NULL) {
379 coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path "
380 "not defined\n");
381 return 0;
382 }
383 if (g_context->root_ca_file) {
384 gnutls_free(g_context->root_ca_file);
385 g_context->root_ca_file = NULL;
386 }
387 if (ca_file) {
388 g_context->root_ca_file = gnutls_strdup(ca_file);
389 }
390 if (g_context->root_ca_path) {
391 gnutls_free(g_context->root_ca_path);
392 g_context->root_ca_path = NULL;
393 }
394 if (ca_path) {
395#if (GNUTLS_VERSION_NUMBER >= 0x030306)
396 g_context->root_ca_path = gnutls_strdup(ca_path);
397#else
398 coap_log_err("ca_path not supported in GnuTLS < 3.3.6\n");
399#endif
400 }
401 return 1;
402}
403
404/*
405 * return 0 failed
406 * 1 passed
407 */
408int
410 coap_gnutls_context_t *g_context =
411 ((coap_gnutls_context_t *)c_context->dtls_context);
412 if (!g_context) {
413 coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment "
414 "not set up\n");
415 return 0;
416 }
417
418#if (GNUTLS_VERSION_NUMBER >= 0x030020)
419 g_context->trust_store_defined = 1;
420 return 1;
421#else
422 coap_log_warn("coap_context_set_pki_trust_store(): (D)TLS environment "
423 "not supported for GnuTLS < v3.0.20\n");
424 return 0;
425#endif
426}
427
428#if COAP_SERVER_SUPPORT
429/*
430 * return 0 failed
431 * 1 passed
432 */
433int
434coap_dtls_context_set_spsk(coap_context_t *c_context,
435 coap_dtls_spsk_t *setup_data
436 ) {
437 coap_gnutls_context_t *g_context =
438 ((coap_gnutls_context_t *)c_context->dtls_context);
439
440 if (!g_context || !setup_data)
441 return 0;
442
443 if (setup_data->ec_jpake) {
444 coap_log_warn("GnuTLS has no EC-JPAKE support\n");
445 }
446 g_context->psk_pki_enabled |= IS_PSK;
447 return 1;
448}
449#endif /* COAP_SERVER_SUPPORT */
450
451#if COAP_CLIENT_SUPPORT
452/*
453 * return 0 failed
454 * 1 passed
455 */
456int
457coap_dtls_context_set_cpsk(coap_context_t *c_context,
458 coap_dtls_cpsk_t *setup_data
459 ) {
460 coap_gnutls_context_t *g_context =
461 ((coap_gnutls_context_t *)c_context->dtls_context);
462
463 if (!g_context || !setup_data)
464 return 0;
465
466 if (setup_data->ec_jpake) {
467 coap_log_warn("GnuTLS has no EC-JPAKE support\n");
468 }
469 if (setup_data->use_cid) {
470 coap_log_warn("GnuTLS has no Connection-ID support\n");
471 }
472 g_context->psk_pki_enabled |= IS_PSK;
473 return 1;
474}
475#endif /* COAP_CLIENT_SUPPORT */
476
477/*
478 * return 0 failed
479 * 1 passed
480 */
481int
483 coap_gnutls_context_t *g_context =
484 ((coap_gnutls_context_t *)c_context->dtls_context);
485 return g_context->psk_pki_enabled ? 1 : 0;
486}
487
488void
489coap_dtls_startup(void) {
490 gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func);
491 gnutls_global_set_log_function(coap_gnutls_log_func);
492}
493
494void
495coap_dtls_shutdown(void) {
497}
498
499void
501}
502
503void *
504coap_dtls_get_tls(const coap_session_t *c_session,
505 coap_tls_library_t *tls_lib) {
506 if (tls_lib)
507 *tls_lib = COAP_TLS_LIBRARY_GNUTLS;
508 if (c_session && c_session->tls) {
509 const coap_gnutls_env_t *g_env = (const coap_gnutls_env_t *)c_session->tls;
510
511 return g_env->g_session;
512 }
513 return NULL;
514}
515
516void
518 dtls_log_level = level;
519 gnutls_global_set_log_level(dtls_log_level);
520}
521
522/*
523 * return current logging level
524 */
527 return dtls_log_level;
528}
529
530/*
531 * return +ve new g_context
532 * NULL failure
533 */
534void *
536 const char *err = "Unknown Error";
537 int ret;
538 coap_gnutls_context_t *g_context =
539 (coap_gnutls_context_t *)
540 gnutls_malloc(sizeof(coap_gnutls_context_t));
541
542 if (g_context) {
544 const char *priority;
545
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;
552 }
553
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;
558 } else {
559 priority = VARIANTS_BASE;
560 }
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);
565 else
566 coap_log_warn("gnutls_priority_init: %s\n", gnutls_strerror(ret));
567 goto fail;
568 }
569 }
570 return g_context;
571
572fail:
573 if (g_context)
574 coap_dtls_free_context(g_context);
575 return NULL;
576}
577
578void
579coap_dtls_free_context(void *handle) {
580 size_t i;
581 coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
582
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);
590 }
591 if (g_context->pki_sni_entry_list)
592 gnutls_free(g_context->pki_sni_entry_list);
593
594 for (i = 0; i < g_context->psk_sni_count; i++) {
595 gnutls_free(g_context->psk_sni_entry_list[i].sni);
596 /* YUK - A memory leak in 3.3.0 (fixed by 3.3.26) of hint */
597 gnutls_psk_free_server_credentials(
598 g_context->psk_sni_entry_list[i].psk_credentials);
599 }
600 if (g_context->psk_sni_entry_list)
601 gnutls_free(g_context->psk_sni_entry_list);
602
603 gnutls_priority_deinit(g_context->priority_cache);
604
605 gnutls_global_deinit();
606 gnutls_free(g_context);
607}
608
609#if COAP_CLIENT_SUPPORT
610/*
611 * gnutls_psk_client_credentials_function return values
612 * (see gnutls_psk_set_client_credentials_function())
613 *
614 * return -1 failed
615 * 0 passed
616 */
617static int
618psk_client_callback(gnutls_session_t g_session,
619 char **username, gnutls_datum_t *key) {
620 coap_session_t *c_session =
621 (coap_session_t *)gnutls_transport_get_ptr(g_session);
622 coap_gnutls_context_t *g_context;
623 coap_dtls_cpsk_t *setup_data;
624 const char *hint = gnutls_psk_client_get_hint(g_session);
625 coap_bin_const_t temp;
626 const coap_bin_const_t *psk_key;
627 const coap_bin_const_t *psk_identity;
628 const coap_dtls_cpsk_info_t *cpsk_info;
629
630 /* Initialize result parameters. */
631 *username = NULL;
632 key->data = NULL;
633
634 if (c_session == NULL)
635 return -1;
636
637 g_context = (coap_gnutls_context_t *)c_session->context->dtls_context;
638 if (g_context == NULL)
639 return -1;
640
641 setup_data = &c_session->cpsk_setup_data;
642
643 temp.s = hint ? (const uint8_t *)hint : (const uint8_t *)"";
644 temp.length = strlen((const char *)temp.s);
645 coap_session_refresh_psk_hint(c_session, &temp);
646
647 coap_log_debug("got psk_identity_hint: '%.*s'\n", (int)temp.length,
648 (const char *)temp.s);
649
650 if (setup_data->validate_ih_call_back) {
651 coap_str_const_t lhint;
652
653 lhint.length = temp.length;
654 lhint.s = temp.s;
655 coap_lock_callback_ret(cpsk_info,
656 setup_data->validate_ih_call_back(&lhint,
657 c_session,
658 setup_data->ih_call_back_arg));
659
660 if (cpsk_info == NULL)
661 return -1;
662
663 coap_session_refresh_psk_identity(c_session, &cpsk_info->identity);
664 coap_session_refresh_psk_key(c_session, &cpsk_info->key);
665 psk_identity = &cpsk_info->identity;
666 psk_key = &cpsk_info->key;
667 } else {
668 psk_identity = coap_get_session_client_psk_identity(c_session);
669 psk_key = coap_get_session_client_psk_key(c_session);
670 }
671
672 if (psk_identity == NULL || psk_key == NULL) {
673 coap_log_warn("no PSK available\n");
674 return -1;
675 }
676
677 *username = gnutls_malloc(psk_identity->length+1);
678 if (*username == NULL)
679 return -1;
680 memcpy(*username, psk_identity->s, psk_identity->length);
681 (*username)[psk_identity->length] = '\000';
682
683 key->data = gnutls_malloc(psk_key->length);
684 if (key->data == NULL) {
685 gnutls_free(*username);
686 *username = NULL;
687 return -1;
688 }
689 memcpy(key->data, psk_key->s, psk_key->length);
690 key->size = psk_key->length;
691 return 0;
692}
693#endif /* COAP_CLIENT_SUPPORT */
694
695typedef struct {
696 gnutls_certificate_type_t certificate_type;
697 char *san_or_cn;
698 const gnutls_datum_t *cert_list;
699 unsigned int cert_list_size;
700 int self_signed; /* 1 if cert self-signed, 0 otherwise */
701} coap_gnutls_certificate_info_t;
702
703/*
704 * return Type of certificate and SAN or CN if appropriate derived from
705 * certificate. GNUTLS_CRT_UNKNOWN if failure.
706 */
707static gnutls_certificate_type_t
708get_san_or_cn(coap_session_t *session, gnutls_session_t g_session,
709 coap_gnutls_certificate_info_t *cert_info, const char *sni_match) {
710 gnutls_x509_crt_t cert;
711 char dn[256];
712 size_t size;
713 int n;
714 char *cn;
715 int ret;
716 unsigned int seq;
717
718#if (GNUTLS_VERSION_NUMBER >= 0x030606)
719 cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
720 GNUTLS_CTYPE_PEERS);
721#else /* < 3.6.6 */
722 cert_info->certificate_type = gnutls_certificate_type_get(g_session);
723#endif /* < 3.6.6 */
724
725 cert_info->san_or_cn = NULL;
726
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;
731 }
732
733 if (cert_info->certificate_type != GNUTLS_CRT_X509)
734 return cert_info->certificate_type;
735
736 G_CHECK(gnutls_x509_crt_init(&cert), "gnutls_x509_crt_init");
737
738 /* Interested only in first cert in chain */
739 G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
740 GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import");
741
742 cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
743
744 /* See if there is a Subject Alt Name first */
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) {
749 /* Reached the end of the SAN list */
750 break;
751 }
752 if (ret >= 0) {
753 dn[size] = '\000';
754 if (sni_match) {
755 if (strlen(sni_match) != size ||
756 memcmp(dn, sni_match, size)) {
757 continue;
758 }
759 }
760 gnutls_x509_crt_deinit(cert);
761 cert_info->san_or_cn = gnutls_strdup(dn);
762 return cert_info->certificate_type;
763 }
764 }
765
766 /* Otherwise use the CN attribute of the Subject */
767 size = sizeof(dn);
768 ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0,
769 dn, &size);
770 if (ret == 0) {
771 cn = dn;
772 if (sni_match) {
773 if (!coap_pki_name_match_sni(cn, strlen(cn), sni_match)) {
774 coap_log_debug(" %s: got CN '%s'\n",
775 coap_session_str(session), cn);
776 goto no_match;
777 }
778 }
779
780 gnutls_x509_crt_deinit(cert);
781 cert_info->san_or_cn = gnutls_strdup(cn);
782 return cert_info->certificate_type;
783 }
784no_match:
785 n = 0;
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) {
790 /* Reached the end of the SAN list */
791 break;
792 }
793 if (ret >= 0) {
794 dn[size] = '\000';
795 coap_log_debug(" %s: got DNS.%d '%s'\n",
796 coap_session_str(session), n + 1, dn);
797
798 }
799 n++;
800 continue;
801 }
802 gnutls_x509_crt_deinit(cert);
803 return GNUTLS_CRT_UNKNOWN;
804
805fail:
806 return GNUTLS_CRT_UNKNOWN;
807}
808
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 : "?")
814#else /* GNUTLS_VERSION_NUMBER < 0x030606 */
815#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
816 cert_info.san_or_cn : "?")
817#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
818
819#if (GNUTLS_VERSION_NUMBER >= 0x030606)
820static int
821check_rpk_cert(coap_gnutls_context_t *g_context,
822 coap_gnutls_certificate_info_t *cert_info,
823 coap_session_t *c_session) {
824 int ret;
825
826 if (g_context->setup_data.validate_cn_call_back) {
827 gnutls_pcert_st pcert;
828 uint8_t der[2048];
829 size_t size;
830
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");
834
835 size = sizeof(der);
836 G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
837 "gnutls_pubkey_export");
838 gnutls_pcert_deinit(&pcert);
840 g_context->setup_data.validate_cn_call_back(COAP_DTLS_RPK_CERT_CN,
841 der,
842 size,
843 c_session,
844 0,
845 1,
846 g_context->setup_data.cn_call_back_arg));
847 if (!ret) {
848 return 0;
849 }
850 }
851 return 1;
852fail:
853 return 0;
854}
855#endif /* >= 3.6.6 */
856
857/*
858 * return 0 failed
859 * 1 passed
860 */
861static int
862cert_verify_gnutls(gnutls_session_t g_session) {
863 unsigned int status = 0;
864 unsigned int fail = 0;
865 coap_session_t *c_session =
866 (coap_session_t *)gnutls_transport_get_ptr(g_session);
867 coap_gnutls_context_t *g_context =
868 (coap_gnutls_context_t *)c_session->context->dtls_context;
869 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
870 int alert = GNUTLS_A_BAD_CERTIFICATE;
871 int ret;
872 coap_gnutls_certificate_info_t cert_info;
873 gnutls_certificate_type_t cert_type;
874
875 memset(&cert_info, 0, sizeof(cert_info));
876 if (!g_context->setup_data.allow_sni_cn_mismatch &&
877 c_session->type == COAP_SESSION_TYPE_CLIENT &&
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) {
881 coap_log_info(" %s: SNI '%s' not returned in certificate\n",
882 coap_session_str(c_session), g_context->setup_data.client_sni);
883 alert = GNUTLS_A_ACCESS_DENIED;
884 goto fail;
885 }
886 }
887
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;
894 goto fail;
895 }
896 goto ok;
897 }
898#endif /* >= 3.6.6 */
899
900 if (cert_info.cert_list_size == 0) {
901 if (!g_context->setup_data.verify_peer_cert)
902 goto ok;
903 else
904 goto fail;
905 }
906
907 G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
908 "gnutls_certificate_verify_peers");
909
910 coap_dtls_log(COAP_LOG_DEBUG, "error %x cert '%s'\n",
911 status, cert_info.san_or_cn);
912
913 if (g_context->setup_data.validate_cn_call_back) {
914 gnutls_x509_crt_t cert;
915 uint8_t der[2048];
916 size_t size;
917 /* status == 0 indicates that the certificate passed to
918 * setup_data.validate_cn_call_back has been validated. */
919 const int cert_is_trusted = !status;
920
921 G_CHECK(gnutls_x509_crt_init(&cert), "gnutls_x509_crt_init");
922
923 /* Interested only in first cert in chain */
924 G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
925 GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import");
926
927 size = sizeof(der);
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,
933 der,
934 size,
935 c_session,
936 0,
937 cert_is_trusted,
938 g_context->setup_data.cn_call_back_arg));
939 if (!ret) {
940 alert = GNUTLS_A_ACCESS_DENIED;
941 goto fail;
942 }
943 }
944
945 if (status) {
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) {
950 coap_log_info(" %s: %s: overridden: '%s'\n",
951 coap_session_str(c_session),
952 "The certificate has an invalid usage date",
953 OUTPUT_CERT_NAME);
954 } else {
955 fail = 1;
956 coap_log_warn(" %s: %s: '%s'\n",
957 coap_session_str(c_session),
958 "The certificate has an invalid usage date",
959 OUTPUT_CERT_NAME);
960 }
961 }
962 if (status & (GNUTLS_CERT_UNEXPECTED_OWNER)) {
963 status &= ~(GNUTLS_CERT_UNEXPECTED_OWNER);
964 if (g_context->setup_data.allow_sni_cn_mismatch) {
965 coap_log_info(" %s: %s: overridden: '%s' v '%s'\n",
966 coap_session_str(c_session),
967 "The SNI does not match the returned CN",
968 g_context->setup_data.client_sni,
969 OUTPUT_CERT_NAME);
970 } else {
971 fail = 1;
972 coap_log_warn(" %s: %s: '%s' v '%s'\n",
973 coap_session_str(c_session),
974 "The SNI does not match the returned CN",
975 g_context->setup_data.client_sni,
976 OUTPUT_CERT_NAME);
977 }
978 }
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) {
984 coap_log_info(" %s: %s: overridden: '%s'\n",
985 coap_session_str(c_session),
986 "The certificate's CRL entry has an invalid usage date",
987 OUTPUT_CERT_NAME);
988 } else {
989 fail = 1;
990 coap_log_warn(" %s: %s: '%s'\n",
991 coap_session_str(c_session),
992 "The certificate's CRL entry has an invalid usage date",
993 OUTPUT_CERT_NAME);
994 }
995 }
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) {
1001 coap_log_info(" %s: %s: overridden: '%s'\n",
1002 coap_session_str(c_session),
1003 "Self-signed",
1004 OUTPUT_CERT_NAME);
1005 } else {
1006 fail = 1;
1007 alert = GNUTLS_A_UNKNOWN_CA;
1008 coap_log_warn(" %s: %s: '%s'\n",
1009 coap_session_str(c_session),
1010 "Self-signed",
1011 OUTPUT_CERT_NAME);
1012 }
1013 } else {
1014 if (!g_context->setup_data.verify_peer_cert) {
1015 coap_log_info(" %s: %s: overridden: '%s'\n",
1016 coap_session_str(c_session),
1017 "The peer certificate's CA is unknown",
1018 OUTPUT_CERT_NAME);
1019 } else {
1020 fail = 1;
1021 alert = GNUTLS_A_UNKNOWN_CA;
1022 coap_log_warn(" %s: %s: '%s'\n",
1023 coap_session_str(c_session),
1024 "The peer certificate's CA is unknown",
1025 OUTPUT_CERT_NAME);
1026 }
1027 }
1028 }
1029 if (status & (GNUTLS_CERT_INSECURE_ALGORITHM)) {
1030 status &= ~(GNUTLS_CERT_INSECURE_ALGORITHM);
1031 fail = 1;
1032 coap_log_warn(" %s: %s: '%s'\n",
1033 coap_session_str(c_session),
1034 "The certificate uses an insecure algorithm",
1035 OUTPUT_CERT_NAME);
1036 }
1037
1038 if (status) {
1039 fail = 1;
1040 coap_log_warn(" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n",
1041 coap_session_str(c_session),
1042 status, OUTPUT_CERT_NAME);
1043 }
1044 }
1045
1046 if (fail)
1047 goto fail;
1048
1049 if (g_context->setup_data.additional_tls_setup_call_back) {
1050 /* Additional application setup wanted */
1051 if (!g_context->setup_data.additional_tls_setup_call_back(g_session,
1052 &g_context->setup_data)) {
1053 goto fail;
1054 }
1055 }
1056
1057ok:
1058 if (cert_info.san_or_cn)
1059 gnutls_free(cert_info.san_or_cn);
1060
1061 return 1;
1062
1063fail:
1064 if (cert_info.san_or_cn)
1065 gnutls_free(cert_info.san_or_cn);
1066
1067 if (!g_env->sent_alert) {
1068 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, alert));
1069 g_env->sent_alert = 1;
1070 }
1072 return 0;
1073}
1074
1075/*
1076 * gnutls_certificate_verify_function return values
1077 * (see gnutls_certificate_set_verify_function())
1078 *
1079 * return -1 failed
1080 * 0 passed
1081 */
1082static int
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) {
1086 return -1;
1087 }
1088 }
1089 return 0;
1090}
1091
1092#ifndef min
1093#define min(a,b) ((a) < (b) ? (a) : (b))
1094#endif
1095
1096static int
1097pin_callback(void *user_data, int attempt,
1098 const char *token_url COAP_UNUSED,
1099 const char *token_label COAP_UNUSED,
1100 unsigned int flags COAP_UNUSED,
1101 char *pin,
1102 size_t pin_max) {
1103 coap_dtls_key_t *key = (coap_dtls_key_t *)user_data;
1104
1105 /* Only do this on first attempt to prevent token lockout */
1106 if (attempt == 0 && key && key->key.define.user_pin) {
1107 int len = min(pin_max - 1, strlen(key->key.define.user_pin));
1108
1109 memcpy(pin, key->key.define.user_pin, len);
1110 pin[len] = 0;
1111 return 0;
1112 }
1113 return -1;
1114}
1115
1116static int
1117check_null_memory(gnutls_datum_t *datum,
1118 const uint8_t *buf, size_t len, int *alloced) {
1119 datum->size = len;
1120 *alloced = 0;
1121 if (buf[len-1] != '\000') {
1122 /* Need to allocate memory, rather than just copying pointers across */
1123 *alloced = 1;
1124 datum->data = gnutls_malloc(len + 1);
1125 if (!datum->data) {
1126 coap_log_err("gnutls_malloc failure\n");
1127 return GNUTLS_E_MEMORY_ERROR;
1128 }
1129 memcpy(datum->data, buf, len);
1130 datum->data[len] = '\000';
1131 datum->size++;
1132 } else {
1133 /* To get around const issue */
1134 memcpy(&datum->data,
1135 &buf, sizeof(datum->data));
1136 }
1137 return 0;
1138}
1139
1140#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1141static int
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 };
1147 int ret;
1148
1149 /* A single PEM private key can supply both halves of the RPK pair. */
1150 if (!strstr((const char *)cert->data, "-----BEGIN PUBLIC KEY-----")) {
1151 if ((ret = gnutls_privkey_init(&privkey)) < 0)
1152 return ret;
1153 if ((ret = gnutls_privkey_import_x509_raw(privkey, cert, GNUTLS_X509_FMT_PEM,
1154 NULL, 0)) < 0)
1155 goto finish;
1156 if ((ret = gnutls_pubkey_init(&pubkey)) < 0)
1157 goto finish;
1158 if ((ret = gnutls_pubkey_import_privkey(pubkey, privkey, 0, 0)) < 0)
1159 goto finish;
1160 if ((ret = gnutls_pubkey_export2(pubkey, GNUTLS_X509_FMT_PEM, &spki)) < 0)
1161 goto finish;
1162 cert = &spki;
1163 }
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);
1167finish:
1168 gnutls_free(spki.data);
1169 if (pubkey)
1170 gnutls_pubkey_deinit(pubkey);
1171 if (privkey)
1172 gnutls_privkey_deinit(privkey);
1173 return ret;
1174}
1175#endif /* GNUTLS_VERSION_NUMBER >= 0x030606 */
1176
1177/*
1178 * return 0 Success (GNUTLS_E_SUCCESS)
1179 * neg GNUTLS_E_* error code
1180 */
1181static int
1182setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
1183 gnutls_session_t g_session,
1184 coap_gnutls_context_t *g_context,
1185 coap_dtls_pki_t *setup_data, coap_dtls_role_t role) {
1186 coap_dtls_key_t key;
1187 int ret;
1188 gnutls_datum_t cert;
1189 gnutls_datum_t pkey;
1190 gnutls_datum_t ca;
1191 int alloced_cert_memory = 0;
1192 int alloced_pkey_memory = 0;
1193 int alloced_ca_memory = 0;
1194
1195 /* Map over to the new define format to save code duplication */
1196 coap_dtls_map_key_type_to_define(setup_data, &key);
1197
1198 assert(key.key_type == COAP_PKI_KEY_DEFINE);
1199
1200 G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
1201 "gnutls_certificate_allocate_credentials");
1202
1203 /*
1204 * Configure the Private Key
1205 */
1206 if (key.key.define.private_key.u_byte &&
1207 key.key.define.private_key.u_byte[0]) {
1208 switch (key.key.define.private_key_def) {
1209 case COAP_PKI_KEY_DEF_PEM: /* define private key */
1210 case COAP_PKI_KEY_DEF_PEM_BUF: /* define private key */
1211 case COAP_PKI_KEY_DEF_DER: /* define private key */
1212 case COAP_PKI_KEY_DEF_DER_BUF: /* define private key */
1213 case COAP_PKI_KEY_DEF_PKCS11: /* define private key */
1214 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define private key */
1215 /* Handled under public key */
1216 break;
1217 case COAP_PKI_KEY_DEF_RPK_BUF: /* define private key */
1218#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1219 /* Handled under public key */
1220 break;
1221#else /* 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);
1226#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
1227 case COAP_PKI_KEY_DEF_ENGINE: /* define private key */
1228 default:
1231 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1232 }
1233 } else if (role == COAP_DTLS_ROLE_SERVER ||
1235 key.key.define.public_cert.u_byte[0])) {
1238 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1239 }
1240
1241 /*
1242 * Configure the Public Certificate / Key
1243 */
1244 if (key.key.define.public_cert.u_byte &&
1245 key.key.define.public_cert.u_byte[0]) {
1246 /* Both Public and Private keys are handled here and MUST be the same type */
1247 if (!(key.key.define.private_key.s_byte &&
1248 key.key.define.private_key.s_byte[0] &&
1252 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1253 }
1254 switch (key.key.define.public_cert_def) {
1255 case COAP_PKI_KEY_DEF_PEM: /* define public cert */
1256 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1259 GNUTLS_X509_FMT_PEM)) < 0) {
1262 &key, role, ret);
1263 }
1264 break;
1265 case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */
1266 if ((ret = check_null_memory(&cert,
1269 &alloced_cert_memory)) < 0) {
1272 &key, role, ret);
1273 }
1274 if ((ret = check_null_memory(&pkey,
1277 &alloced_pkey_memory)) < 0) {
1278 if (alloced_cert_memory)
1279 gnutls_free(cert.data);
1282 &key, role, ret);
1283 }
1284 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1285 &cert,
1286 &pkey,
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);
1294 &key, role, ret);
1295 }
1296 if (alloced_cert_memory)
1297 gnutls_free(cert.data);
1298 if (alloced_pkey_memory)
1299 gnutls_free(pkey.data);
1300 break;
1301 case COAP_PKI_KEY_DEF_RPK_BUF: /* define public cert */
1302#if (GNUTLS_VERSION_NUMBER >= 0x030606)
1303 if ((ret = check_null_memory(&cert,
1306 &alloced_cert_memory)) < 0) {
1309 &key, role, ret);
1310 }
1311 if ((ret = check_null_memory(&pkey,
1314 &alloced_pkey_memory)) < 0) {
1315 if (alloced_cert_memory)
1316 gnutls_free(cert.data);
1319 &key, role, ret);
1320 }
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);
1326 if (ret < 0)
1328 COAP_DEFINE_FAIL_BAD, &key, role, ret);
1329 break;
1330#else /* GNUTLS_VERSION_NUMBER < 0x030606 */
1331 coap_log_err("RPK Support not available (needs gnutls 3.6.6 or later)\n");
1334 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1335#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
1336 case COAP_PKI_KEY_DEF_DER: /* define public cert */
1337 if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials,
1340 GNUTLS_X509_FMT_DER) < 0)) {
1343 &key, role, ret);
1344 }
1345 break;
1346 case COAP_PKI_KEY_DEF_DER_BUF: /* define public cert */
1347 if ((ret = check_null_memory(&cert,
1350 &alloced_cert_memory)) < 0) {
1353 &key, role, ret);
1354 }
1355 if ((ret = check_null_memory(&pkey,
1358 &alloced_pkey_memory)) < 0) {
1359 if (alloced_cert_memory)
1360 gnutls_free(cert.data);
1363 &key, role, ret);
1364 }
1365 if ((ret = gnutls_certificate_set_x509_key_mem(*pki_credentials,
1366 &cert,
1367 &pkey,
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);
1375 &key, role, ret);
1376 }
1377 if (alloced_cert_memory)
1378 gnutls_free(cert.data);
1379 if (alloced_pkey_memory)
1380 gnutls_free(pkey.data);
1381 break;
1382 case COAP_PKI_KEY_DEF_PKCS11: /* define public cert */
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) {
1390 &key, role, ret);
1391 }
1392 break;
1393 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define public cert */
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))) {
1404 &key, role, ret);
1405 }
1406#else /* GNUTLS_VERSION_NUMBER < 0x030606 */
1407 coap_log_err("RPK Support not available (needs gnutls 3.6.6 or later)\n");
1408 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1409#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
1410 break;
1411 case COAP_PKI_KEY_DEF_ENGINE: /* define public cert */
1412 default:
1415 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1416 }
1417 }
1418
1419 /*
1420 * Configure the CA
1421 */
1422 if (key.key.define.ca.u_byte &&
1423 key.key.define.ca.u_byte[0]) {
1424 switch (key.key.define.ca_def) {
1426 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1427 key.key.define.ca.s_byte,
1428 GNUTLS_X509_FMT_PEM) < 0)) {
1431 &key, role, ret);
1432 }
1433 break;
1434 case COAP_PKI_KEY_DEF_PEM_BUF: /* define ca */
1435 if ((ret = check_null_memory(&ca,
1436 key.key.define.ca.u_byte,
1437 key.key.define.ca_len,
1438 &alloced_ca_memory)) < 0) {
1441 &key, role, ret);
1442 }
1443 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1444 &ca,
1445 GNUTLS_X509_FMT_PEM)) < 0) {
1446 if (alloced_ca_memory)
1447 gnutls_free(ca.data);
1450 &key, role, ret);
1451 }
1452 if (alloced_ca_memory)
1453 gnutls_free(ca.data);
1454 break;
1455 case COAP_PKI_KEY_DEF_RPK_BUF: /* define ca */
1456 /* Ignore if set */
1457 break;
1458 case COAP_PKI_KEY_DEF_DER: /* define ca */
1459 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1460 key.key.define.ca.s_byte,
1461 GNUTLS_X509_FMT_DER) < 0)) {
1464 &key, role, ret);
1465 }
1466 break;
1467 case COAP_PKI_KEY_DEF_DER_BUF: /* define ca */
1468 if ((ret = check_null_memory(&ca,
1469 key.key.define.ca.u_byte,
1470 key.key.define.ca_len,
1471 &alloced_ca_memory)) < 0) {
1474 &key, role, ret);
1475 }
1476 if ((ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials,
1477 &ca,
1478 GNUTLS_X509_FMT_DER)) <= 0) {
1479 if (alloced_ca_memory)
1480 gnutls_free(ca.data);
1483 &key, role, ret);
1484 }
1485 if (alloced_ca_memory)
1486 gnutls_free(ca.data);
1487 break;
1488 case COAP_PKI_KEY_DEF_PKCS11: /* define ca */
1489 if ((ret = gnutls_certificate_set_x509_trust_file(*pki_credentials,
1490 key.key.define.ca.s_byte,
1491 GNUTLS_X509_FMT_DER)) <= 0) {
1494 &key, role, ret);
1495 }
1496 break;
1497 case COAP_PKI_KEY_DEF_PKCS11_RPK: /* define ca */
1498 /* Ignore if set */
1499 break;
1500 case COAP_PKI_KEY_DEF_ENGINE: /* define ca */
1501 default:
1504 &key, role, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
1505 }
1506 }
1507
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");
1512 }
1513#endif
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);
1518 if (ret == 0) {
1519 coap_log_warn("gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n");
1520 }
1521 }
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");
1528#endif
1529 }
1530 gnutls_certificate_send_x509_rdn_sequence(g_session,
1531 setup_data->check_common_ca ? 0 : 1);
1532#if (GNUTLS_VERSION_NUMBER >= 0x030020)
1533 if (!(g_context->psk_pki_enabled & IS_PKI) && !g_context->trust_store_defined) {
1534 /* No PKI defined at all - still need a trust set up for 3.6.0 or later */
1535 G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials),
1536 "gnutls_certificate_set_x509_system_trust");
1537 }
1538#endif
1539
1540 /* Verify Peer */
1541 gnutls_certificate_set_verify_function(*pki_credentials,
1542 cert_verify_callback_gnutls);
1543
1544 /* Cert chain checking (can raise GNUTLS_E_CONSTRAINT_ERROR) */
1545 if (setup_data->cert_chain_validation) {
1546 gnutls_certificate_set_verify_limits(*pki_credentials,
1547 0,
1548 setup_data->cert_chain_verify_depth + 2);
1549 }
1550
1551 /*
1552 * Check for self signed
1553 * CRL checking (can raise GNUTLS_CERT_MISSING_OCSP_STATUS)
1554 */
1555 gnutls_certificate_set_verify_flags(*pki_credentials,
1556 (setup_data->check_cert_revocation == 0 ?
1557 GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0)
1558 );
1559
1560 return GNUTLS_E_SUCCESS;
1561
1562fail:
1563 return ret;
1564}
1565
1566#if COAP_SERVER_SUPPORT
1567/*
1568 * return 0 Success (GNUTLS_E_SUCCESS)
1569 * neg GNUTLS_E_* error code
1570 */
1571static int
1572setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials,
1573 coap_gnutls_context_t *g_context COAP_UNUSED,
1574 coap_dtls_spsk_t *setup_data) {
1575 int ret;
1576 char hint[COAP_DTLS_HINT_LENGTH];
1577
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);
1582 if (setup_data->psk_info.hint.s) {
1583 snprintf(hint, sizeof(hint), "%.*s", (int)setup_data->psk_info.hint.length,
1584 setup_data->psk_info.hint.s);
1585 G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint),
1586 "gnutls_psk_set_server_credentials_hint");
1587 }
1588
1589 return GNUTLS_E_SUCCESS;
1590
1591fail:
1592 return ret;
1593}
1594
1595/*
1596 * return 0 Success (GNUTLS_E_SUCCESS)
1597 * neg GNUTLS_E_* error code
1598 */
1599static int
1600post_client_hello_gnutls_psk(gnutls_session_t g_session) {
1601 coap_session_t *c_session =
1602 (coap_session_t *)gnutls_transport_get_ptr(g_session);
1603 coap_gnutls_context_t *g_context =
1604 (coap_gnutls_context_t *)c_session->context->dtls_context;
1605 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
1606 int ret = GNUTLS_E_SUCCESS;
1607 char *name = NULL;
1608
1609 if (c_session->context->spsk_setup_data.validate_sni_call_back) {
1610 coap_dtls_spsk_t sni_setup_data;
1611 /* DNS names (only type supported) may be at most 256 byte long */
1612 size_t len = 256;
1613 unsigned int type;
1614 unsigned int i;
1615
1616 name = gnutls_malloc(len);
1617 if (name == NULL)
1618 return GNUTLS_E_MEMORY_ERROR;
1619
1620 for (i=0; ;) {
1621 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1622 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1623 char *new_name;
1624 new_name = gnutls_realloc(name, len);
1625 if (new_name == NULL) {
1626 ret = GNUTLS_E_MEMORY_ERROR;
1627 goto end;
1628 }
1629 name = new_name;
1630 continue; /* retry call with same index */
1631 }
1632
1633 /* check if it is the last entry in list */
1634 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1635 break;
1636 i++;
1637 if (ret != GNUTLS_E_SUCCESS)
1638 goto end;
1639 /* unknown types need to be ignored */
1640 if (type != GNUTLS_NAME_DNS)
1641 continue;
1642
1643 }
1644 /* If no extension provided, make it a dummy entry */
1645 if (i == 0) {
1646 name[0] = '\000';
1647 len = 0;
1648 }
1649
1650 /* Is this a cached entry? */
1651 for (i = 0; i < g_context->psk_sni_count; i++) {
1652 if (strcasecmp(name, g_context->psk_sni_entry_list[i].sni) == 0) {
1653 break;
1654 }
1655 }
1656 if (i == g_context->psk_sni_count) {
1657 /*
1658 * New SNI request
1659 */
1660 const coap_dtls_spsk_info_t *new_entry;
1661
1662 coap_lock_callback_ret(new_entry,
1663 c_session->context->spsk_setup_data.validate_sni_call_back(name,
1664 c_session,
1665 c_session->context->spsk_setup_data.sni_call_back_arg));
1666 if (!new_entry) {
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;
1671 goto end;
1672 }
1673
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,
1683 g_context,
1684 &sni_setup_data)) < 0) {
1685 int keep_ret = ret;
1686 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1687 GNUTLS_A_BAD_CERTIFICATE));
1688 g_env->sent_alert = 1;
1689 ret = keep_ret;
1690 goto end;
1691 }
1692 g_context->psk_sni_count++;
1693 }
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);
1701 }
1702
1703end:
1704 free(name);
1705 return ret;
1706
1707fail:
1708 return ret;
1709}
1710
1711/*
1712 * return 0 Success (GNUTLS_E_SUCCESS)
1713 * neg GNUTLS_E_* error code
1714 */
1715static int
1716post_client_hello_gnutls_pki(gnutls_session_t g_session) {
1717 coap_session_t *c_session =
1718 (coap_session_t *)gnutls_transport_get_ptr(g_session);
1719 coap_gnutls_context_t *g_context =
1720 (coap_gnutls_context_t *)c_session->context->dtls_context;
1721 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
1722 int ret = GNUTLS_E_SUCCESS;
1723 char *name = NULL;
1724
1725 if (g_context->setup_data.validate_sni_call_back) {
1726 /* DNS names (only type supported) may be at most 256 byte long */
1727 size_t len = 256;
1728 unsigned int type;
1729 unsigned int i;
1730 coap_dtls_pki_t sni_setup_data;
1731
1732 name = gnutls_malloc(len);
1733 if (name == NULL)
1734 return GNUTLS_E_MEMORY_ERROR;
1735
1736 for (i=0; ;) {
1737 ret = gnutls_server_name_get(g_session, name, &len, &type, i);
1738 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1739 char *new_name;
1740 new_name = gnutls_realloc(name, len);
1741 if (new_name == NULL) {
1742 ret = GNUTLS_E_MEMORY_ERROR;
1743 goto end;
1744 }
1745 name = new_name;
1746 continue; /* retry call with same index */
1747 }
1748
1749 /* check if it is the last entry in list */
1750 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1751 break;
1752 i++;
1753 if (ret != GNUTLS_E_SUCCESS)
1754 goto end;
1755 /* unknown types need to be ignored */
1756 if (type != GNUTLS_NAME_DNS)
1757 continue;
1758
1759 }
1760 /* If no extension provided, make it a dummy entry */
1761 if (i == 0) {
1762 name[0] = '\000';
1763 len = 0;
1764 }
1765
1766 /* Is this a cached entry? */
1767 for (i = 0; i < g_context->pki_sni_count; i++) {
1768 if (strcasecmp(name, g_context->pki_sni_entry_list[i].sni) == 0) {
1769 break;
1770 }
1771 }
1772 if (i == g_context->pki_sni_count) {
1773 /*
1774 * New SNI request
1775 */
1776 coap_dtls_key_t *new_entry;
1777
1778 coap_lock_callback_ret(new_entry,
1779 g_context->setup_data.validate_sni_call_back(name,
1780 g_context->setup_data.sni_call_back_arg));
1781 if (!new_entry) {
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;
1786 goto end;
1787 }
1788
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,
1797 g_session,
1798 g_context,
1799 &sni_setup_data, COAP_DTLS_ROLE_SERVER)) < 0) {
1800 int keep_ret = ret;
1801 G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
1802 GNUTLS_A_BAD_CERTIFICATE));
1803 g_env->sent_alert = 1;
1804 ret = keep_ret;
1805 goto end;
1806 }
1807 g_context->pki_sni_count++;
1808 }
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");
1812 }
1813
1814end:
1815 free(name);
1816 return ret;
1817
1818fail:
1819 return ret;
1820}
1821#endif /* COAP_SERVER_SUPPORT */
1822
1823#if COAP_CLIENT_SUPPORT
1824/*
1825 * return 0 Success (GNUTLS_E_SUCCESS)
1826 * neg GNUTLS_E_* error code
1827 */
1828static int
1829setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1830 coap_gnutls_context_t *g_context =
1831 (coap_gnutls_context_t *)c_session->context->dtls_context;
1832 int ret;
1833
1834 g_context->psk_pki_enabled |= IS_CLIENT;
1835 if (g_context->psk_pki_enabled & IS_PSK) {
1836 coap_dtls_cpsk_t *setup_data = &c_session->cpsk_setup_data;
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");
1844 /* Issue SNI if requested */
1845 if (setup_data->client_sni) {
1846 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1847 setup_data->client_sni,
1848 strlen(setup_data->client_sni)),
1849 "gnutls_server_name_set");
1850 }
1851 if (setup_data->validate_ih_call_back) {
1852 const char *err;
1854
1855 if (tls_version->version >= 0x030604) {
1856 /* Disable TLS1.3 if Identity Hint Callback set */
1857 const char *priority;
1858
1859 if (tls_version->version >= 0x030606) {
1860 priority = VARIANTS_NO_TLS13_3_6_6;
1861 } else {
1862 priority = VARIANTS_NO_TLS13_3_6_4;
1863 }
1864 ret = gnutls_priority_set_direct(g_env->g_session,
1865 priority, &err);
1866 if (ret < 0) {
1867 if (ret == GNUTLS_E_INVALID_REQUEST)
1868 coap_log_warn("gnutls_priority_set_direct: Syntax error at: %s\n", err);
1869 else
1870 coap_log_warn("gnutls_priority_set_direct: %s\n", gnutls_strerror(ret));
1871 goto fail;
1872 }
1873 }
1874 }
1875 }
1876
1877 if ((g_context->psk_pki_enabled & IS_PKI) ||
1878 (g_context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
1879 /*
1880 * If neither PSK or PKI have been set up, use PKI basics.
1881 * This works providing COAP_PKI_KEY_PEM has a value of 0.
1882 */
1883 coap_dtls_pki_t *setup_data = &g_context->setup_data;
1884
1885 if (!(g_context->psk_pki_enabled & IS_PKI)) {
1886 /* PKI not defined - set up some defaults */
1887 setup_data->verify_peer_cert = 1;
1888 setup_data->check_common_ca = 0;
1889 setup_data->allow_self_signed = 1;
1890 setup_data->allow_expired_certs = 1;
1891 setup_data->cert_chain_validation = 1;
1892 setup_data->cert_chain_verify_depth = 2;
1893 setup_data->check_cert_revocation = 1;
1894 setup_data->allow_no_crl = 1;
1895 setup_data->allow_expired_crl = 1;
1896 setup_data->is_rpk_not_cert = 0;
1897 setup_data->use_cid = 0;
1898 }
1899 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
1900 g_context, setup_data,
1902 "setup_pki_credentials");
1903
1904 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
1905 g_env->pki_credentials),
1906 "gnutls_credentials_set");
1907
1908 if (c_session->proto == COAP_PROTO_TLS)
1909 G_CHECK(gnutls_alpn_set_protocols(g_env->g_session,
1910 &g_context->alpn_proto, 1, 0),
1911 "gnutls_alpn_set_protocols");
1912
1913 /* Issue SNI if requested (only happens if PKI defined) */
1914 if (setup_data->client_sni) {
1915 G_CHECK(gnutls_server_name_set(g_env->g_session, GNUTLS_NAME_DNS,
1916 setup_data->client_sni,
1917 strlen(setup_data->client_sni)),
1918 "gnutls_server_name_set");
1919 }
1920 }
1921 return GNUTLS_E_SUCCESS;
1922
1923fail:
1924 return ret;
1925}
1926#endif /* COAP_CLIENT_SUPPORT */
1927
1928#if COAP_SERVER_SUPPORT
1929/*
1930 * gnutls_psk_server_credentials_function return values
1931 * (see gnutls_psk_set_server_credentials_function())
1932 *
1933 * return -1 failed
1934 * 0 passed
1935 */
1936static int
1937psk_server_callback(gnutls_session_t g_session,
1938 const char *identity,
1939 gnutls_datum_t *key) {
1940 coap_session_t *c_session =
1941 (coap_session_t *)gnutls_transport_get_ptr(g_session);
1942 coap_gnutls_context_t *g_context;
1943 coap_dtls_spsk_t *setup_data;
1944 coap_bin_const_t lidentity;
1945 const coap_bin_const_t *psk_key;
1946
1947 if (c_session == NULL)
1948 return -1;
1949
1950 g_context = (coap_gnutls_context_t *)c_session->context->dtls_context;
1951 if (g_context == NULL)
1952 return -1;
1953 setup_data = &c_session->context->spsk_setup_data;
1954
1955
1956 /* Track the Identity being used */
1957 lidentity.s = identity ? (const uint8_t *)identity : (const uint8_t *)"";
1958 lidentity.length = strlen((const char *)lidentity.s);
1959 coap_session_refresh_psk_identity(c_session, &lidentity);
1960
1961 coap_log_debug("got psk_identity: '%.*s'\n",
1962 (int)lidentity.length, (const char *)lidentity.s);
1963
1964 if (setup_data->validate_id_call_back) {
1965 psk_key = setup_data->validate_id_call_back(&lidentity,
1966 c_session,
1967 setup_data->id_call_back_arg);
1968
1969 coap_session_refresh_psk_key(c_session, psk_key);
1970 } else {
1971 psk_key = coap_get_session_server_psk_key(c_session);
1972 }
1973
1974 if (psk_key == NULL)
1975 return -1;
1976
1977 key->data = gnutls_malloc(psk_key->length);
1978 if (key->data == NULL)
1979 return -1;
1980 memcpy(key->data, psk_key->s, psk_key->length);
1981 key->size = psk_key->length;
1982 return 0;
1983}
1984
1985/*
1986 * return 0 Success (GNUTLS_E_SUCCESS)
1987 * neg GNUTLS_E_* error code
1988 */
1989static int
1990setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) {
1991 coap_gnutls_context_t *g_context =
1992 (coap_gnutls_context_t *)c_session->context->dtls_context;
1993 int ret = GNUTLS_E_SUCCESS;
1994
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,
1999 g_context,
2000 &c_session->context->spsk_setup_data),
2001 "setup_psk_credentials\n");
2002 G_CHECK(gnutls_credentials_set(g_env->g_session,
2003 GNUTLS_CRD_PSK,
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);
2008 }
2009
2010 if (g_context->psk_pki_enabled & IS_PKI) {
2011 coap_dtls_pki_t *setup_data = &g_context->setup_data;
2012 G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session,
2013 g_context, setup_data,
2015 "setup_pki_credentials");
2016
2017 if (setup_data->verify_peer_cert) {
2018 gnutls_certificate_server_set_request(g_env->g_session,
2019 GNUTLS_CERT_REQUIRE);
2020 } else if (setup_data->is_rpk_not_cert) {
2021 gnutls_certificate_server_set_request(g_env->g_session,
2022 GNUTLS_CERT_REQUEST);
2023 } else {
2024 gnutls_certificate_server_set_request(g_env->g_session,
2025 GNUTLS_CERT_IGNORE);
2026 }
2027
2028 gnutls_handshake_set_post_client_hello_function(g_env->g_session,
2029 post_client_hello_gnutls_pki);
2030
2031 G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
2032 g_env->pki_credentials),
2033 "gnutls_credentials_set\n");
2034 }
2035 return GNUTLS_E_SUCCESS;
2036
2037fail:
2038 return ret;
2039}
2040#endif /* COAP_SERVER_SUPPORT */
2041
2042/*
2043 * return +ve data amount
2044 * 0 no more
2045 * -1 error (error in errno)
2046 */
2047static ssize_t
2048coap_dgram_read(gnutls_transport_ptr_t context, void *out, size_t outl) {
2049 ssize_t ret = 0;
2050 coap_session_t *c_session = (coap_session_t *)context;
2051 coap_ssl_t *data;
2052
2053 if (!c_session->tls) {
2054 errno = EAGAIN;
2055 return -1;
2056 }
2057 data = &((coap_gnutls_env_t *)c_session->tls)->coap_ssl_data;
2058
2059 if (out != NULL) {
2060 if (data != NULL && data->pdu_len > 0) {
2061 if (outl < data->pdu_len) {
2062 memcpy(out, data->pdu, outl);
2063 ret = outl;
2064 if (!data->peekmode) {
2065 data->pdu += outl;
2066 data->pdu_len -= outl;
2067 }
2068 } else {
2069 memcpy(out, data->pdu, data->pdu_len);
2070 ret = data->pdu_len;
2071 if (!data->peekmode) {
2072 data->pdu_len = 0;
2073 data->pdu = NULL;
2074 }
2075 }
2076 } else {
2077 errno = EAGAIN;
2078 ret = -1;
2079 }
2080 }
2081 return ret;
2082}
2083
2084/*
2085 * return +ve data amount
2086 * 0 no more
2087 * -1 error (error in errno)
2088 */
2089/* callback function given to gnutls for sending data over socket */
2090static ssize_t
2091coap_dgram_write(gnutls_transport_ptr_t context, const void *send_buffer,
2092 size_t send_buffer_length) {
2093 ssize_t result = -1;
2094 coap_session_t *c_session = (coap_session_t *)context;
2095
2096 if (c_session) {
2097 if (!coap_netif_available(c_session)
2099 && c_session->endpoint == NULL
2100#endif /* COAP_SERVER_SUPPORT */
2101 ) {
2102 /* socket was closed on client due to error */
2103 errno = ECONNRESET;
2104 return -1;
2105 }
2106 result = c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session,
2107 send_buffer, send_buffer_length);
2108 if (result != (int)send_buffer_length) {
2109 int keep_errno = errno;
2110
2111 coap_log_warn("coap_netif_dgrm_write failed (%" PRIdS " != %" PRIuS ")\n",
2112 result, send_buffer_length);
2113 errno = keep_errno;
2114 if (result < 0) {
2115 if (errno == ENOTCONN || errno == ECONNREFUSED)
2116 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
2117 return -1;
2118 } else {
2119 result = 0;
2120 }
2121 }
2122 } else {
2123 result = 0;
2124 }
2125 return result;
2126}
2127
2128/*
2129 * return 1 fd has activity
2130 * 0 timeout
2131 * -1 error (error in errno)
2132 */
2133static int
2134receive_timeout(gnutls_transport_ptr_t context, unsigned int ms COAP_UNUSED) {
2135 coap_session_t *c_session = (coap_session_t *)context;
2136
2137 if (c_session) {
2138 fd_set readfds, writefds, exceptfds;
2139 struct timeval tv;
2140#if COAP_SERVER_SUPPORT
2141 coap_fd_t fd = COAP_PROTO_NOT_RELIABLE(c_session->proto) ? c_session->type !=
2142 COAP_SESSION_TYPE_CLIENT ? c_session->endpoint->sock.fd : c_session->sock.fd : c_session->sock.fd;
2143#else /* ! COAP_SERVER_SUPPORT */
2144 coap_fd_t fd = c_session->sock.fd;
2145#endif /* ! COAP_SERVER_SUPPORT */
2146 int nfds = fd + 1;
2147 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2148
2149 /* If data has been read in by libcoap ahead of GnuTLS, say it is there */
2150 if (c_session->proto == COAP_PROTO_DTLS && g_env &&
2151 g_env->coap_ssl_data.pdu_len > 0) {
2152 return 1;
2153 }
2154
2155 FD_ZERO(&readfds);
2156 FD_ZERO(&writefds);
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);
2162 }
2163 /* Polling */
2164 tv.tv_sec = 0;
2165 tv.tv_usec = 0;
2166
2167 return select(nfds, &readfds, &writefds, &exceptfds, &tv);
2168 }
2169 return 1;
2170}
2171
2172static coap_gnutls_env_t *
2173coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) {
2174 coap_gnutls_context_t *g_context =
2175 ((coap_gnutls_context_t *)c_session->context->dtls_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;
2179#else /* < 3.6.6 */
2180 int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK;
2181#endif /* < 3.6.6 */
2182 int ret;
2183
2184 if (g_env)
2185 return g_env;
2186
2187 g_env = gnutls_malloc(sizeof(coap_gnutls_env_t));
2188 if (!g_env)
2189 return NULL;
2190
2191 memset(g_env, 0, sizeof(coap_gnutls_env_t));
2192
2193 G_CHECK(gnutls_init(&g_env->g_session, flags), "gnutls_init");
2194
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);
2198 /* So we can track the coap_session_t in callbacks */
2199 gnutls_transport_set_ptr(g_env->g_session, c_session);
2200
2201 G_CHECK(gnutls_priority_set(g_env->g_session, g_context->priority_cache),
2202 "gnutls_priority_set");
2203
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");
2208#else /* ! COAP_SERVER_SUPPORT */
2209 goto fail;
2210#endif /* ! COAP_SERVER_SUPPORT */
2211 } else {
2212#if COAP_CLIENT_SUPPORT
2213 G_CHECK(setup_client_ssl_session(c_session, g_env),
2214 "setup_client_ssl_session");
2215#else /* COAP_CLIENT_SUPPORT */
2216 goto fail;
2217#endif /* COAP_CLIENT_SUPPORT */
2218 }
2219
2220 gnutls_handshake_set_timeout(g_env->g_session,
2221 GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2222 gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS,
2224
2225 return g_env;
2226
2227fail:
2228 if (g_env)
2229 gnutls_free(g_env);
2230 return NULL;
2231}
2232
2233static void
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) {
2237 if (g_env) {
2238 /* It is suggested not to use GNUTLS_SHUT_RDWR in DTLS
2239 * connections because the peer's closure message might
2240 * be lost */
2241 if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) {
2242 /* Only do this if appropriate */
2243 gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ?
2244 GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR);
2245 }
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;
2253 } else {
2254 /* YUK - A memory leak in 3.3.0 (fixed by 3.3.26) of hint */
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;
2258 }
2259 }
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;
2265 }
2266 gnutls_free(g_env->coap_ssl_data.cookie_key.data);
2267 gnutls_free(g_env);
2268 }
2269}
2270
2271#if COAP_SERVER_SUPPORT
2272void *
2273coap_dtls_new_server_session(coap_session_t *c_session) {
2274 coap_gnutls_env_t *g_env =
2275 (coap_gnutls_env_t *)c_session->tls;
2276
2277 gnutls_transport_set_ptr(g_env->g_session, c_session);
2278
2279 return g_env;
2280}
2281#endif /* COAP_SERVER_SUPPORT */
2282
2283static void
2284log_last_alert(coap_session_t *c_session,
2285 gnutls_session_t g_session) {
2286#if COAP_MAX_LOGGING_LEVEL > 0
2287 int last_alert = gnutls_alert_get(g_session);
2288
2289 if (last_alert == GNUTLS_A_CLOSE_NOTIFY)
2290 coap_log_debug("***%s: Alert '%d': %s\n",
2291 coap_session_str(c_session),
2292 last_alert, gnutls_alert_get_name(last_alert));
2293 else
2294 coap_log_warn("***%s: Alert '%d': %s\n",
2295 coap_session_str(c_session),
2296 last_alert, gnutls_alert_get_name(last_alert));
2297#else /* COAP_MAX_LOGGING_LEVEL == 0 */
2298 (void)c_session;
2299 (void)g_session;
2300#endif /* COAP_MAX_LOGGING_LEVEL == 0 */
2301}
2302
2303/*
2304 * return -1 failure
2305 * 0 not completed
2306 * 1 established
2307 */
2308static int
2309do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) {
2310 int ret;
2311
2312 ret = gnutls_handshake(g_env->g_session);
2313 switch (ret) {
2314 case GNUTLS_E_SUCCESS:
2315 g_env->established = 1;
2316 coap_log_debug("* %s: GnuTLS established\n",
2317 coap_session_str(c_session));
2319 c_session);
2320 gnutls_transport_set_ptr(g_env->g_session, c_session);
2321 c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session);
2322 ret = 1;
2323 break;
2324 case GNUTLS_E_INTERRUPTED:
2325 errno = EINTR;
2326 ret = 0;
2327 break;
2328 case GNUTLS_E_AGAIN:
2329 errno = EAGAIN;
2330 ret = 0;
2331 break;
2332 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
2333 coap_log_warn("Insufficient credentials provided.\n");
2334 ret = -1;
2335 break;
2336 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2337 /* Stop the sending of an alert on closedown */
2338 g_env->sent_alert = 1;
2339 log_last_alert(c_session, g_env->g_session);
2340 /* Fall through */
2341 case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
2342 case GNUTLS_E_UNEXPECTED_PACKET:
2344 ret = -1;
2345 break;
2346 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2347 log_last_alert(c_session, g_env->g_session);
2348 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
2349 ret = 0;
2350 break;
2351 case GNUTLS_E_NO_CERTIFICATE_FOUND:
2352#if (GNUTLS_VERSION_NUMBER > 0x030606)
2353 case GNUTLS_E_CERTIFICATE_REQUIRED:
2354#endif /* GNUTLS_VERSION_NUMBER > 0x030606 */
2355 coap_log_warn("Client Certificate requested and required, but not provided\n"
2356 );
2357 G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
2358 GNUTLS_A_BAD_CERTIFICATE));
2359 g_env->sent_alert = 1;
2361 ret = -1;
2362 break;
2363 case GNUTLS_E_DECRYPTION_FAILED:
2364 coap_log_warn("do_gnutls_handshake: session establish "
2365 "returned '%s'\n",
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;
2371 ret = -1;
2372 break;
2373 case GNUTLS_E_CERTIFICATE_ERROR:
2374 if (g_env->sent_alert) {
2376 ret = -1;
2377 break;
2378 }
2379 /* Fall through */
2380 case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
2381 case GNUTLS_E_NO_CIPHER_SUITES:
2382 case GNUTLS_E_INVALID_SESSION:
2383 coap_log_warn("do_gnutls_handshake: session establish "
2384 "returned '%s'\n",
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;
2390 }
2392 ret = -1;
2393 break;
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:
2400 ret = -1;
2401 break;
2402 default:
2403 coap_log_warn("do_gnutls_handshake: session establish "
2404 "returned %d: '%s'\n",
2405 ret, gnutls_strerror(ret));
2406 ret = -1;
2407 break;
2408 }
2409 return ret;
2410}
2411
2412#if COAP_CLIENT_SUPPORT
2413void *
2414coap_dtls_new_client_session(coap_session_t *c_session) {
2415 coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT);
2416 int ret;
2417
2418 if (g_env) {
2419 ret = do_gnutls_handshake(c_session, g_env);
2420 if (ret == -1) {
2421 coap_dtls_free_gnutls_env(c_session->context->dtls_context,
2422 g_env,
2423 COAP_PROTO_NOT_RELIABLE(c_session->proto) ?
2424 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2425 return NULL;
2426 }
2427 }
2428 return g_env;
2429}
2430#endif /* COAP_CLIENT_SUPPORT */
2431
2432void
2434 if (c_session && c_session->context && c_session->tls) {
2435 coap_dtls_free_gnutls_env(c_session->context->dtls_context,
2436 c_session->tls,
2437 COAP_PROTO_NOT_RELIABLE(c_session->proto) ?
2438 COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP);
2439 c_session->tls = NULL;
2441 }
2442}
2443
2444void
2446 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2447 int ret;
2448
2449 if (g_env)
2450 G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session,
2451 (unsigned int)c_session->mtu),
2452 "gnutls_dtls_set_data_mtu");
2453fail:
2454 ;;
2455}
2456
2457/*
2458 * return +ve data amount
2459 * 0 no more
2460 * -1 error
2461 */
2462ssize_t
2464 const uint8_t *data, size_t data_len) {
2465 int ret;
2466 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2467
2468 if (g_env == NULL) {
2469 return -1;
2470 }
2471
2472 c_session->dtls_event = -1;
2473 coap_log_debug("* %s: dtls: sent %4d bytes\n",
2474 coap_session_str(c_session), (int)data_len);
2475 if (g_env->established) {
2476 ret = gnutls_record_send(g_env->g_session, data, data_len);
2477
2478 if (ret <= 0) {
2479 switch (ret) {
2480 case GNUTLS_E_AGAIN:
2481 ret = 0;
2482 break;
2483 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2484 /* Stop the sending of an alert on closedown */
2485 g_env->sent_alert = 1;
2486 log_last_alert(c_session, g_env->g_session);
2488 ret = -1;
2489 break;
2490 default:
2491 coap_log_debug("coap_dtls_send: gnutls_record_send "
2492 "returned %d: '%s'\n",
2493 ret, gnutls_strerror(ret));
2494 ret = -1;
2495 break;
2496 }
2497 if (ret == -1) {
2498 coap_log_warn("coap_dtls_send: cannot send PDU\n");
2499 }
2500 }
2501 } else {
2502 ret = do_gnutls_handshake(c_session, g_env);
2503 if (ret == 1) {
2504 /* Just connected, so send the data */
2505 return coap_dtls_send(c_session, data, data_len);
2506 }
2507 ret = -1;
2508 }
2509
2510 if (c_session->dtls_event >= 0) {
2511 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2512 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2513 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2514 ret = -1;
2515 }
2516 }
2517
2518 return ret;
2519}
2520
2521int
2523 return 0;
2524}
2525
2527coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
2528 return 0;
2529}
2530
2533 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2534
2535 assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2536 if (g_env && g_env->g_session) {
2537 unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session);
2538
2539 if (rem_ms == 0) {
2540 /*
2541 * Need to make sure that we do not do this too frequently as some
2542 * versions of gnutls reset retransmit if a spurious packet is received
2543 * (e.g. duplicate Client Hello), but last_transmit does not get updated
2544 * when gnutls_handshake() is called and there is 'nothing' to resend.
2545 */
2546 if (g_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS > now)
2547 return g_env->last_timeout + COAP_DTLS_RETRANSMIT_COAP_TICKS;
2548 }
2549 /* Reset for the next time */
2550 g_env->last_timeout = now - rem_ms;
2551 return now + rem_ms;
2552 }
2553
2554 return 0;
2555}
2556
2557/*
2558 * return 1 timed out
2559 * 0 still timing out
2560 */
2561int
2563 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2564
2565 assert(g_env != NULL && c_session->state == COAP_SESSION_STATE_HANDSHAKE);
2566 g_env->doing_dtls_timeout = 1;
2567 if ((++c_session->dtls_timeout_count > c_session->max_retransmit) ||
2568 (do_gnutls_handshake(c_session, g_env) < 0)) {
2569 /* Too many retries */
2570 g_env->doing_dtls_timeout = 0;
2572 return 1;
2573 } else {
2574 g_env->doing_dtls_timeout = 0;
2575 return 0;
2576 }
2577}
2578
2579/*
2580 * return +ve data amount
2581 * 0 no more
2582 * -1 error
2583 */
2584int
2585coap_dtls_receive(coap_session_t *c_session, const uint8_t *data,
2586 size_t data_len) {
2587 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2588 int ret = 0;
2589 coap_ssl_t *ssl_data = &g_env->coap_ssl_data;
2590
2591 uint8_t pdu[COAP_RXBUFFER_SIZE];
2592
2593 assert(g_env != NULL);
2594
2595 if (ssl_data->pdu_len)
2596 coap_log_err("** %s: Previous data not read %u bytes\n",
2597 coap_session_str(c_session), ssl_data->pdu_len);
2598 ssl_data->pdu = data;
2599 ssl_data->pdu_len = data_len;
2600
2601 c_session->dtls_event = -1;
2602 if (g_env->established) {
2603 ret = gnutls_record_recv(g_env->g_session, pdu, (int)sizeof(pdu));
2604 if (ret > 0) {
2605 coap_log_debug("* %s: dtls: recv %4d bytes\n",
2606 coap_session_str(c_session), ret);
2607 return coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret);
2608 } else if (ret == 0) {
2610 } else {
2611 switch (ret) {
2612 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2613 /* Stop the sending of an alert on closedown */
2614 g_env->sent_alert = 1;
2615 log_last_alert(c_session, g_env->g_session);
2617 ret = -1;
2618 break;
2619 case GNUTLS_E_WARNING_ALERT_RECEIVED:
2620 log_last_alert(c_session, g_env->g_session);
2621 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
2622 ret = 0;
2623 break;
2624 default:
2625 coap_log_warn("coap_dtls_receive: gnutls_record_recv returned %d\n", ret);
2626 ret = -1;
2627 break;
2628 }
2629 }
2630 } else {
2631 ret = do_gnutls_handshake(c_session, g_env);
2632 if (ret == 1) {
2633 coap_session_connected(c_session);
2634 } else {
2635 ret = -1;
2636 if (ssl_data->pdu_len && !g_env->sent_alert) {
2637 /* Do the handshake again in case of internal timeout */
2638 ret = do_gnutls_handshake(c_session, g_env);
2639 if (ret == 1) {
2640 /* Just connected, so send the data */
2641 coap_session_connected(c_session);
2642 }
2643 }
2644 }
2645 }
2646
2647 if (c_session->dtls_event >= 0) {
2648 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2649 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2650 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2652 ssl_data = NULL;
2653 ret = -1;
2654 }
2655 }
2656 if (ssl_data && ssl_data->pdu_len) {
2657 /* pdu data is held on stack which will not stay there */
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;
2661 }
2662 return ret;
2663}
2664
2665#if COAP_SERVER_SUPPORT
2666/*
2667 * return -1 failure
2668 * 0 not completed
2669 * 1 client hello seen
2670 */
2671int
2672coap_dtls_hello(coap_session_t *c_session,
2673 const uint8_t *data,
2674 size_t data_len
2675 ) {
2676 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2677 coap_ssl_t *ssl_data;
2678 int ret;
2679
2680 if (!g_env) {
2681 g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_SERVER);
2682 if (g_env) {
2683 c_session->tls = g_env;
2684 gnutls_key_generate(&g_env->coap_ssl_data.cookie_key,
2685 GNUTLS_COOKIE_KEY_SIZE);
2686 } else {
2687 /* error should have already been reported */
2688 return -1;
2689 }
2690 }
2691 if (data_len > 0) {
2692 gnutls_dtls_prestate_st prestate;
2693 uint8_t *data_rw;
2694
2695 memset(&prestate, 0, sizeof(prestate));
2696 /* Need to do this to not get a compiler warning about const parameters */
2697 memcpy(&data_rw, &data, sizeof(data_rw));
2698 ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key,
2699 &c_session->addr_info,
2700 sizeof(c_session->addr_info),
2701 data_rw, data_len,
2702 &prestate);
2703 if (ret < 0) { /* cookie not valid */
2704 coap_log_debug("Invalid Cookie - sending Hello Verify\n");
2705 gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key,
2706 &c_session->addr_info,
2707 sizeof(c_session->addr_info),
2708 &prestate,
2709 c_session,
2710 coap_dgram_write);
2711 return 0;
2712 }
2713 gnutls_dtls_prestate_set(g_env->g_session, &prestate);
2714 }
2715
2716 ssl_data = &g_env->coap_ssl_data;
2717 ssl_data->pdu = data;
2718 ssl_data->pdu_len = data_len;
2719
2720 ret = do_gnutls_handshake(c_session, g_env);
2721 if (ret < 0) {
2722 /*
2723 * as the above failed, need to remove g_env to clean up any
2724 * pollution of the information
2725 */
2726 coap_dtls_free_gnutls_env(((coap_gnutls_context_t *)c_session->context->dtls_context),
2727 g_env, COAP_FREE_BYE_NONE);
2728 c_session->tls = NULL;
2729 ssl_data = NULL;
2730 ret = -1;
2731 } else {
2732 /* Client Hello has been seen */
2733 ret = 1;
2734 }
2735
2736 if (ssl_data && ssl_data->pdu_len) {
2737 /* pdu data is held on stack which will not stay there */
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;
2741 }
2742 return ret;
2743}
2744#endif /* COAP_SERVER_SUPPORT */
2745
2746unsigned int
2748 return 37;
2749}
2750
2751#if !COAP_DISABLE_TCP
2752/*
2753 * strm
2754 * return +ve data amount
2755 * 0 connection closed
2756 * -1 error (error in errno)
2757 */
2758static ssize_t
2759coap_sock_read(gnutls_transport_ptr_t context, void *out, size_t outl) {
2760 int ret = 0;
2761 coap_session_t *c_session = (coap_session_t *)context;
2762
2763 if (out != NULL) {
2764 ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_read(c_session, out, outl);
2765 /* Translate layer returns into what GnuTLS expects */
2766 if (ret == 0) {
2767 errno = EAGAIN;
2768 ret = -1;
2769 }
2770 }
2771 return ret;
2772}
2773
2774/*
2775 * strm
2776 * return +ve data amount
2777 * 0 no more
2778 * -1 error (error in errno)
2779 */
2780static ssize_t
2781coap_sock_write(gnutls_transport_ptr_t context, const void *in, size_t inl) {
2782 int ret = 0;
2783 coap_session_t *c_session = (coap_session_t *)context;
2784
2785 ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].l_write(c_session, in, inl);
2786 /* Translate layer what returns into what GnuTLS expects */
2787 if (ret < 0) {
2788 if ((c_session->state == COAP_SESSION_STATE_CSM ||
2789 c_session->state == COAP_SESSION_STATE_HANDSHAKE) &&
2790 (errno == EPIPE || errno == ECONNRESET)) {
2791 /*
2792 * Need to handle a TCP timing window where an agent continues with
2793 * the sending of the next handshake or a CSM.
2794 * However, the peer does not like a certificate and so sends a
2795 * fatal alert and closes the TCP session.
2796 * The sending of the next handshake or CSM may get terminated because
2797 * of the closed TCP session, but there is still an outstanding alert
2798 * to be read in and reported on.
2799 * In this case, pretend that sending the info was fine so that the
2800 * alert can be read (which effectively is what happens with DTLS).
2801 */
2802 ret = inl;
2803 } else {
2804 coap_log_debug("* %s: failed to send %" PRIdS " bytes (%s) state %d\n",
2805 coap_session_str(c_session), inl, coap_socket_strerror(),
2806 c_session->state);
2807 }
2808 }
2809 if (ret == 0) {
2810 errno = EAGAIN;
2811 ret = -1;
2812 }
2813 return ret;
2814}
2815
2816#if COAP_CLIENT_SUPPORT
2817void *
2818coap_tls_new_client_session(coap_session_t *c_session) {
2819 coap_gnutls_env_t *g_env = gnutls_malloc(sizeof(coap_gnutls_env_t));
2820 coap_gnutls_context_t *g_context =
2821 ((coap_gnutls_context_t *)c_session->context->dtls_context);
2822#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2823 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2824#else /* < 3.6.6 */
2825 int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK;
2826#endif /* < 3.6.6 */
2827 int ret;
2828
2829 if (!g_env) {
2830 return NULL;
2831 }
2832 memset(g_env, 0, sizeof(coap_gnutls_env_t));
2833
2834 G_CHECK(gnutls_init(&g_env->g_session, flags), "gnutls_init");
2835
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);
2839 /* So we can track the coap_session_t in callbacks */
2840 gnutls_transport_set_ptr(g_env->g_session, c_session);
2841
2842 gnutls_priority_set(g_env->g_session, g_context->priority_cache);
2843 setup_client_ssl_session(c_session, g_env);
2844
2845 gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
2846
2847 c_session->tls = g_env;
2848 do_gnutls_handshake(c_session, g_env);
2849 return g_env;
2850
2851fail:
2852 if (g_env)
2853 gnutls_free(g_env);
2854 return NULL;
2855}
2856#endif /* COAP_CLIENT_SUPPORT */
2857
2858#if COAP_SERVER_SUPPORT
2859void *
2860coap_tls_new_server_session(coap_session_t *c_session) {
2861 coap_gnutls_env_t *g_env = gnutls_malloc(sizeof(coap_gnutls_env_t));
2862 coap_gnutls_context_t *g_context =
2863 ((coap_gnutls_context_t *)c_session->context->dtls_context);
2864#if (GNUTLS_VERSION_NUMBER >= 0x030606)
2865 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK;
2866#else /* < 3.6.6 */
2867 int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK;
2868#endif /* < 3.6.6 */
2869 int ret;
2870
2871 if (!g_env)
2872 return NULL;
2873 memset(g_env, 0, sizeof(coap_gnutls_env_t));
2874
2875 G_CHECK(gnutls_init(&g_env->g_session, flags), "gnutls_init");
2876
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);
2880 /* So we can track the coap_session_t in callbacks */
2881 gnutls_transport_set_ptr(g_env->g_session, c_session);
2882
2883 setup_server_ssl_session(c_session, g_env);
2884
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);
2888
2889 c_session->tls = g_env;
2890 do_gnutls_handshake(c_session, g_env);
2891 return g_env;
2892
2893fail:
2894 return NULL;
2895}
2896#endif /* COAP_SERVER_SUPPORT */
2897
2898void
2900 coap_dtls_free_session(c_session);
2901 return;
2902}
2903
2904/*
2905 * strm
2906 * return +ve Number of bytes written.
2907 * -1 Error (error in errno).
2908 */
2909ssize_t
2910coap_tls_write(coap_session_t *c_session, const uint8_t *data,
2911 size_t data_len) {
2912 int ret;
2913 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2914
2915 assert(g_env != NULL);
2916
2917 c_session->dtls_event = -1;
2918 if (g_env->established) {
2919 ret = gnutls_record_send(g_env->g_session, data, data_len);
2920
2921 if (ret <= 0) {
2922 switch (ret) {
2923 case GNUTLS_E_AGAIN:
2924 ret = 0;
2925 break;
2926 case GNUTLS_E_PUSH_ERROR:
2927 case GNUTLS_E_PULL_ERROR:
2928 case GNUTLS_E_PREMATURE_TERMINATION:
2930 break;
2931 case GNUTLS_E_FATAL_ALERT_RECEIVED:
2932 /* Stop the sending of an alert on closedown */
2933 g_env->sent_alert = 1;
2934 log_last_alert(c_session, g_env->g_session);
2936 break;
2937 default:
2938 coap_log_warn("coap_tls_write: gnutls_record_send "
2939 "returned %d: '%s'\n",
2940 ret, gnutls_strerror(ret));
2941 ret = -1;
2942 break;
2943 }
2944 if (ret == -1) {
2945 coap_log_info("coap_tls_write: cannot send PDU\n");
2946 }
2947 }
2948 } else {
2949 ret = do_gnutls_handshake(c_session, g_env);
2950 if (ret == 1) {
2951 ret = 0;
2952 } else {
2953 ret = -1;
2954 }
2955 }
2956
2957 if (c_session->dtls_event >= 0) {
2958 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
2959 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
2960 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
2961 ret = -1;
2962 }
2963 }
2964
2965 if (ret > 0) {
2966 if (ret == (ssize_t)data_len)
2967 coap_log_debug("* %s: tls: sent %4d bytes\n",
2968 coap_session_str(c_session), ret);
2969 else
2970 coap_log_debug("* %s: tls: sent %4d of %4" PRIdS " bytes\n",
2971 coap_session_str(c_session), ret, data_len);
2972 }
2973 return ret;
2974}
2975
2976/*
2977 * strm
2978 * return >=0 Number of bytes read.
2979 * -1 Error (error in errno).
2980 */
2981ssize_t
2982coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) {
2983 coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
2984 int ret = -1;
2985
2986 if (!g_env) {
2987 errno = ENOTCONN;
2988 return -1;
2989 }
2990
2991 c_session->dtls_event = -1;
2992 if (!g_env->established && !g_env->sent_alert) {
2993 ret = do_gnutls_handshake(c_session, g_env);
2994 if (ret == 1) {
2995 ret = 0;
2996 }
2997 }
2998 if (c_session->state != COAP_SESSION_STATE_NONE && g_env->established) {
2999 ret = gnutls_record_recv(g_env->g_session, data, (int)data_len);
3000 if (ret <= 0) {
3001 switch (ret) {
3002 case 0:
3004 break;
3005 case GNUTLS_E_AGAIN:
3006 errno = EAGAIN;
3007 ret = 0;
3008 break;
3009 case GNUTLS_E_PULL_ERROR:
3010 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
3011 break;
3012 case GNUTLS_E_FATAL_ALERT_RECEIVED:
3013 /* Stop the sending of an alert on closedown */
3014 g_env->sent_alert = 1;
3015 log_last_alert(c_session, g_env->g_session);
3017 break;
3018 case GNUTLS_E_WARNING_ALERT_RECEIVED:
3019 log_last_alert(c_session, g_env->g_session);
3020 c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
3021 break;
3022 default:
3023 coap_log_warn("coap_tls_read: gnutls_record_recv "
3024 "returned %d: '%s'\n",
3025 ret, gnutls_strerror(ret));
3026 ret = -1;
3027 break;
3028 }
3029 }
3030 }
3031
3032 if (c_session->dtls_event >= 0) {
3033 coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session);
3034 if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR ||
3035 c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) {
3037 ret = -1;
3038 }
3039 }
3040 if (ret > 0) {
3041 coap_log_debug("* %s: tls: recv %4d bytes\n",
3042 coap_session_str(c_session), ret);
3043 }
3044 return ret;
3045}
3046#endif /* !COAP_DISABLE_TCP */
3047
3048#if COAP_SERVER_SUPPORT
3049coap_digest_ctx_t *
3050coap_digest_setup(void) {
3051 gnutls_hash_hd_t digest_ctx;
3052
3053 if (gnutls_hash_init(&digest_ctx, GNUTLS_DIG_SHA256)) {
3054 return NULL;
3055 }
3056 return digest_ctx;
3057}
3058
3059void
3060coap_digest_free(coap_digest_ctx_t *digest_ctx) {
3061 if (digest_ctx)
3062 gnutls_hash_deinit(digest_ctx, NULL);
3063}
3064
3065int
3066coap_digest_update(coap_digest_ctx_t *digest_ctx,
3067 const uint8_t *data,
3068 size_t data_len) {
3069 int ret = gnutls_hash(digest_ctx, data, data_len);
3070
3071 return ret == 0;
3072}
3073
3074int
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);
3078
3079 coap_digest_free(digest_ctx);
3080 return 1;
3081}
3082#endif /* COAP_SERVER_SUPPORT */
3083
3084#if COAP_WS_SUPPORT || COAP_OSCORE_GROUP_SUPPORT
3085/*
3086 * The struct hash_algs and the function get_hash_alg() are used to
3087 * determine which hash type to use for creating the required hash object.
3088 */
3089static struct hash_algs {
3090 cose_alg_t alg;
3091 gnutls_digest_algorithm_t dig_type;
3092 size_t dig_size;
3093} hashes[] = {
3094 {COSE_ALGORITHM_SHA_1, GNUTLS_DIG_SHA1, 20},
3095 {COSE_ALGORITHM_SHA_256_256, GNUTLS_DIG_SHA256, 32},
3096 {COSE_ALGORITHM_SHA_512, GNUTLS_DIG_SHA512, 64},
3097};
3098
3099static gnutls_digest_algorithm_t
3100get_hash_alg(cose_alg_t alg, size_t *hash_len) {
3101 size_t idx;
3102
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;
3107 }
3108 }
3109 coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg);
3110 return GNUTLS_DIG_UNKNOWN;
3111}
3112
3113int
3115 const coap_bin_const_t *data,
3116 coap_bin_const_t **hash) {
3117 size_t hash_length;
3118 gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length);
3119 gnutls_hash_hd_t digest_ctx;
3121 int ret;
3122
3123 if (dig_type == GNUTLS_DIG_UNKNOWN) {
3124 coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg);
3125 return 0;
3126 }
3127
3128 if (gnutls_hash_init(&digest_ctx, dig_type)) {
3129 return 0;
3130 }
3131 ret = gnutls_hash(digest_ctx, data->s, data->length);
3132 if (ret != 0)
3133 goto error;
3134
3135 dummy = coap_new_binary(hash_length);
3136 if (!dummy)
3137 goto error;
3138 gnutls_hash_output(digest_ctx, dummy->s);
3139
3140 *hash = (coap_bin_const_t *)(dummy);
3141 gnutls_hash_deinit(digest_ctx, NULL);
3142 return 1;
3143
3144error:
3146 gnutls_hash_deinit(digest_ctx, NULL);
3147 return 0;
3148}
3149#endif /* COAP_WS_SUPPORT || COAP_OSCORE_GROUP_SUPPORT */
3150
3151#if COAP_OSCORE_SUPPORT
3152int
3154 return 1;
3155}
3156
3157int
3159#if COAP_OSCORE_GROUP_SUPPORT
3160 return 1;
3161#else /* !COAP_OSCORE_GROUP_SUPPORT */
3162 return 0;
3163#endif /* !COAP_OSCORE_GROUP_SUPPORT */
3164}
3165
3166int
3168#if COAP_OSCORE_GROUP_SUPPORT
3169 return 0;
3170#else /* !COAP_OSCORE_GROUP_SUPPORT */
3171 return 0;
3172#endif /* !COAP_OSCORE_GROUP_SUPPORT */
3173}
3174
3175/*
3176 * The struct cipher_algs and the function get_cipher_alg() are used to
3177 * determine which cipher type to use for creating the required cipher
3178 * suite object.
3179 */
3180static struct cipher_algs {
3181 cose_alg_t alg;
3182 gnutls_cipher_algorithm_t cipher_type;
3183} ciphers[] = {{COSE_ALGORITHM_AES_CCM_16_64_128, GNUTLS_CIPHER_AES_128_CCM_8},
3184 {COSE_ALGORITHM_AES_CCM_16_64_256, GNUTLS_CIPHER_AES_256_CCM_8}
3185};
3186
3187static gnutls_cipher_algorithm_t
3188get_cipher_alg(cose_alg_t alg) {
3189 size_t idx;
3190
3191 for (idx = 0; idx < sizeof(ciphers) / sizeof(struct cipher_algs); idx++) {
3192 if (ciphers[idx].alg == alg)
3193 return ciphers[idx].cipher_type;
3194 }
3195 coap_log_debug("get_cipher_alg: COSE cipher %d not supported\n", alg);
3196 return 0;
3197}
3198
3199/*
3200 * The struct hmac_algs and the function get_hmac_alg() are used to
3201 * determine which hmac type to use for creating the required hmac
3202 * suite object.
3203 */
3204static struct hmac_algs {
3205 cose_hmac_alg_t hmac_alg;
3206 gnutls_mac_algorithm_t hmac_type;
3207} hmacs[] = {
3208 {COSE_HMAC_ALG_HMAC256_256, GNUTLS_MAC_SHA256},
3209 {COSE_HMAC_ALG_HMAC512_512, GNUTLS_MAC_SHA512},
3210};
3211
3212static gnutls_mac_algorithm_t
3213get_hmac_alg(cose_hmac_alg_t hmac_alg) {
3214 size_t idx;
3215
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;
3219 }
3220 coap_log_debug("get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3221 return 0;
3222}
3223
3224#if COAP_OSCORE_GROUP_SUPPORT
3225
3226/*
3227 * The struct curve_algs and the function get_curve_alg() are used to
3228 * determine which curve type to use for creating the required curve
3229 * output object.
3230 */
3231static struct curve_algs {
3232 cose_curve_t alg;
3233 gnutls_ecc_curve_t curve;
3234} curves[] = {
3235 {COSE_CURVE_P_256, GNUTLS_ECC_CURVE_SECP256R1},
3236 {COSE_CURVE_X25519, GNUTLS_ECC_CURVE_X25519},
3237 {COSE_CURVE_X448, GNUTLS_ECC_CURVE_X448},
3238 {COSE_CURVE_ED25519, GNUTLS_ECC_CURVE_ED25519},
3239 {COSE_CURVE_ED448, GNUTLS_ECC_CURVE_ED448},
3240};
3241
3242static gnutls_ecc_curve_t
3243get_curve_alg(cose_curve_t alg) {
3244 size_t idx;
3245
3246 for (idx = 0; idx < sizeof(curves) / sizeof(struct curve_algs); idx++) {
3247 if (curves[idx].alg == alg)
3248 return curves[idx].curve;
3249 }
3250 coap_log_debug("get_curve_alg: COSE curve %d not supported\n", alg);
3251 return 0;
3252}
3253
3254static cose_curve_t
3255get_alg_curve(gnutls_ecc_curve_t curve) {
3256 size_t idx;
3257
3258 for (idx = 0; idx < sizeof(curves) / sizeof(struct curve_algs); idx++) {
3259 if (curves[idx].curve == curve)
3260 return curves[idx].alg;
3261 }
3262 coap_log_debug("get_alg_curve: GnuTLS curve %d not supported\n", curve);
3263 return 0;
3264}
3265
3266static cose_alg_t
3267get_alg_hash(gnutls_digest_algorithm_t dig_type) {
3268 size_t idx;
3269
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;
3273 }
3274 coap_log_debug("get_alg_hash: GnuTLS hash %d not supported\n", dig_type);
3275 return 0;
3276}
3277
3278int
3279coap_crypto_check_curve_alg(cose_curve_t alg) {
3280 return get_curve_alg(alg) != 0;
3281}
3282
3283int
3284coap_crypto_check_hash_alg(cose_alg_t alg) {
3285 size_t hash_length;
3286
3287 return get_hash_alg(alg, &hash_length) != 0;
3288}
3289#endif /* COAP_OSCORE_GROUP_SUPPORT */
3290
3291int
3293 return get_cipher_alg(alg);
3294}
3295
3296int
3298 cose_hmac_alg_t hmac_alg;
3299
3300 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
3301 return 0;
3302 return get_hmac_alg(hmac_alg);
3303}
3304
3305int
3307 coap_bin_const_t *data,
3308 coap_bin_const_t *aad,
3309 uint8_t *result,
3310 size_t *max_result_len) {
3311 gnutls_aead_cipher_hd_t ctx;
3312 gnutls_datum_t key;
3313 const coap_crypto_aes_ccm_t *ccm;
3314 int ret = 0;
3315 size_t result_len = *max_result_len;
3316 gnutls_cipher_algorithm_t algo;
3317 unsigned tag_size;
3318 uint8_t *key_data_rw;
3319 coap_bin_const_t laad;
3320
3321 if (data == NULL)
3322 return 0;
3323
3324 assert(params != NULL);
3325 if (!params) {
3326 return 0;
3327 }
3328 if ((algo = get_cipher_alg(params->alg)) == 0) {
3329 coap_log_debug("coap_crypto_encrypt: algorithm %d not supported\n",
3330 params->alg);
3331 return 0;
3332 }
3333 tag_size = gnutls_cipher_get_tag_size(algo);
3334 ccm = &params->params.aes;
3335
3336 /* Get a RW copy of data */
3337 memcpy(&key_data_rw, &ccm->key.s, sizeof(key_data_rw));
3338 key.data = key_data_rw;
3339 key.size = ccm->key.length;
3340
3341 if (aad) {
3342 laad = *aad;
3343 } else {
3344 laad.s = NULL;
3345 laad.length = 0;
3346 }
3347
3348 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key), "gnutls_aead_cipher_init");
3349
3350 G_CHECK(gnutls_aead_cipher_encrypt(ctx,
3351 ccm->nonce,
3352 15 - ccm->l, /* iv */
3353 laad.s,
3354 laad.length, /* ad */
3355 tag_size,
3356 data->s,
3357 data->length, /* input */
3358 result,
3359 &result_len), /* output */
3360 "gnutls_aead_cipher_encrypt");
3361 *max_result_len = result_len;
3362 ret = 1;
3363fail:
3364 gnutls_aead_cipher_deinit(ctx);
3365 return ret == 1 ? 1 : 0;
3366}
3367
3368int
3370 coap_bin_const_t *data,
3371 coap_bin_const_t *aad,
3372 uint8_t *result,
3373 size_t *max_result_len) {
3374 gnutls_aead_cipher_hd_t ctx;
3375 gnutls_datum_t key;
3376 const coap_crypto_aes_ccm_t *ccm;
3377 int ret = 0;
3378 size_t result_len = *max_result_len;
3379 gnutls_cipher_algorithm_t algo;
3380 unsigned tag_size;
3381 uint8_t *key_data_rw;
3382 coap_bin_const_t laad;
3383
3384 if (data == NULL)
3385 return 0;
3386
3387 assert(params != NULL);
3388
3389 if (!params) {
3390 return 0;
3391 }
3392 if ((algo = get_cipher_alg(params->alg)) == 0) {
3393 coap_log_debug("coap_crypto_decrypt: algorithm %d not supported\n",
3394 params->alg);
3395 return 0;
3396 }
3397 tag_size = gnutls_cipher_get_tag_size(algo);
3398 ccm = &params->params.aes;
3399
3400 /* Get a RW copy of data */
3401 memcpy(&key_data_rw, &ccm->key.s, sizeof(key_data_rw));
3402 key.data = key_data_rw;
3403 key.size = ccm->key.length;
3404
3405 if (aad) {
3406 laad = *aad;
3407 } else {
3408 laad.s = NULL;
3409 laad.length = 0;
3410 }
3411
3412 G_CHECK(gnutls_aead_cipher_init(&ctx, algo, &key), "gnutls_aead_cipher_init");
3413
3414 G_CHECK(gnutls_aead_cipher_decrypt(ctx,
3415 ccm->nonce,
3416 15 - ccm->l, /* iv */
3417 laad.s,
3418 laad.length, /* ad */
3419 tag_size,
3420 data->s,
3421 data->length, /* input */
3422 result,
3423 &result_len), /* output */
3424 "gnutls_aead_cipher_decrypt");
3425 *max_result_len = result_len;
3426 ret = 1;
3427fail:
3428 gnutls_aead_cipher_deinit(ctx);
3429 return ret == 1 ? 1 : 0;
3430}
3431
3432int
3434 coap_bin_const_t *key,
3435 coap_bin_const_t *data,
3436 coap_bin_const_t **hmac) {
3437 gnutls_hmac_hd_t ctx;
3438 int ret = 0;
3439 unsigned len;
3440 gnutls_mac_algorithm_t mac_algo;
3442
3443 if (data == NULL)
3444 return 0;
3445
3446 if ((mac_algo = get_hmac_alg(hmac_alg)) == 0) {
3447 coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3448 return 0;
3449 }
3450 len = gnutls_hmac_get_len(mac_algo);
3451 if (len == 0)
3452 return 0;
3453
3454 dummy = coap_new_binary(len);
3455 if (dummy == NULL)
3456 return 0;
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);
3461 *hmac = (coap_bin_const_t *)dummy;
3462 dummy = NULL;
3463 ret = 1;
3464fail:
3466 gnutls_hmac_deinit(ctx, NULL);
3467 return ret == 1 ? 1 : 0;
3468}
3469
3470#if COAP_OSCORE_GROUP_SUPPORT
3471int
3472coap_crypto_read_pem_private_key(const char *filename,
3473 coap_crypto_pri_key_t **private) {
3474 gnutls_privkey_t priv_key = NULL;
3475 gnutls_x509_privkey_t priv_x509 = NULL;
3476 coap_crypto_pri_key_t *local = gnutls_malloc(sizeof(coap_crypto_pri_key_t));
3477 int ret = 0;
3478 gnutls_datum_t data = {NULL, 0};
3479 gnutls_datum_t key = {NULL, 0};
3480
3481 if (local == NULL)
3482 goto fail;
3483
3484 memset(local, 0, sizeof(coap_crypto_pri_key_t));
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,
3488 &data,
3489 GNUTLS_X509_FMT_PEM,
3490 NULL,
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,
3499 NULL,
3500 GNUTLS_PKCS_PLAIN,
3501 &key),
3502 "gnutls_x509_privkey_export2_pkcs8");
3503 local->pri_der = coap_new_bin_const(key.data, key.size);
3504 if (local->pri_der == NULL)
3505 goto fail;
3506 local->key_tls = priv_key;
3507 priv_key = NULL;
3508 *private = local;
3509 local = NULL;
3510 ret = 1;
3511
3512fail:
3513 if (priv_key)
3514 gnutls_privkey_deinit(priv_key);
3515 if (priv_x509)
3516 gnutls_x509_privkey_deinit(priv_x509);
3517 gnutls_free(local);
3518 gnutls_free(data.data);
3519 gnutls_free(key.data);
3520 return ret == 1 ? 1 : 0;
3521}
3522
3523int
3524coap_crypto_read_asn1_private_key(coap_bin_const_t *binary,
3525 coap_crypto_pri_key_t **private) {
3526 gnutls_privkey_t priv_key = NULL;
3527 coap_crypto_pri_key_t *local;
3528 int ret = 0;
3529 gnutls_datum_t data = {NULL, 0};
3530
3531 local = gnutls_malloc(sizeof(coap_crypto_pri_key_t));
3532 if (local == NULL)
3533 goto fail;
3534
3535 memset(local, 0, sizeof(coap_crypto_pri_key_t));
3536 G_CHECK(gnutls_privkey_init(&priv_key), "gnutls_privkey_init");
3537 data.size = binary->length;
3538 /* Need to do this to not get a compiler warning about const parameters */
3539 memcpy(&data.data, &binary->s, sizeof(data.data));
3540 G_CHECK(gnutls_privkey_import_x509_raw(priv_key,
3541 &data,
3542 GNUTLS_X509_FMT_DER,
3543 NULL,
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");
3548 local->pri_der = (coap_bin_const_t *)binary;
3549 binary = NULL;
3550 local->key_tls = priv_key;
3551 priv_key = NULL;
3552 *private = local;
3553 local = NULL;
3554 ret = 1;
3555
3556fail:
3557 if (priv_key)
3558 gnutls_privkey_deinit(priv_key);
3559 coap_delete_bin_const(binary);
3560 gnutls_free(local);
3561 return ret == 1 ? 1 : 0;
3562}
3563
3564int
3565coap_crypto_read_raw_private_key(cose_curve_t curve,
3566 coap_bin_const_t *binary,
3567 coap_crypto_pri_key_t **private) {
3568 (void)curve;
3569 (void)private;
3570
3571 coap_delete_bin_const(binary);
3572 return 0;
3573}
3574
3576coap_crypto_duplicate_private_key(coap_crypto_pri_key_t *key) {
3577 if (key) {
3578 gnutls_x509_privkey_t priv_x509 = NULL;
3579 int ret = 0;
3580 coap_crypto_pri_key_t *new_key =
3581 gnutls_malloc(sizeof(coap_crypto_pri_key_t));
3582 if (new_key == NULL)
3583 goto fail;
3584
3585 memcpy(new_key, key, sizeof(coap_crypto_pri_key_t));
3586 new_key->pri_der =
3588 if (new_key->pri_der == NULL)
3589 goto fail;
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,
3593 priv_x509,
3594 GNUTLS_PRIVKEY_IMPORT_COPY),
3595 "gnutls_privkey_import_x509");
3596 return new_key;
3597
3598fail:
3599 if (priv_x509)
3600 gnutls_x509_privkey_deinit(priv_x509);
3601 gnutls_free(new_key);
3602 }
3603 return NULL;
3604}
3605
3606void
3607coap_crypto_delete_private_key(coap_crypto_pri_key_t *key) {
3608 if (key) {
3609 gnutls_privkey_deinit(key->key_tls);
3611 gnutls_free(key);
3612 }
3613}
3614
3615int
3616coap_crypto_read_pem_public_cred(const char *filename,
3617 coap_crypto_cred_t **public) {
3618 gnutls_pubkey_t pub_key = NULL;
3619 gnutls_privkey_t priv_key = NULL;
3620 coap_crypto_cred_t *local = gnutls_malloc(sizeof(coap_crypto_cred_t));
3621 int ret = 0;
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;
3627
3628 if (local == NULL)
3629 goto fail;
3630
3631 memset(local, 0, sizeof(coap_crypto_cred_t));
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,
3637 &data,
3638 GNUTLS_X509_FMT_PEM,
3639 NULL,
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");
3646 } else {
3647 G_CHECK(
3648 gnutls_pubkey_import_x509_raw(pub_key, &data, GNUTLS_X509_FMT_PEM, 0),
3649 "gnutls_pubkey_import_x509_raw");
3650 }
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");
3654 local->pub_der = coap_new_bin_const(key.data, key.size);
3655 if (local->pub_der == NULL)
3656 goto fail;
3657 G_CHECK(gnutls_pubkey_get_preferred_hash_algorithm(pub_key, &hash, &mand),
3658 "gnutls_pubkey_get_preferred_hash_algorithm");
3659 local->sign_hash = get_alg_hash(hash);
3660 local->wire_sign_size = cose_hash_len(local->sign_hash);
3661 switch (gnutls_pubkey_get_pk_algorithm(pub_key, NULL)) {
3662 case GNUTLS_PK_EDDSA_ED25519:
3663 break;
3664 case GNUTLS_PK_ECDSA:
3665 local->wire_sign_size *= 2;
3666 break;
3667 default:
3668 break;
3669 }
3670 G_CHECK(gnutls_pubkey_export_ecc_raw2(pub_key, &curve, NULL, NULL, 0),
3671 "gnutls_pubkey_export_ecc_raw2");
3672 local->sign_curve = get_alg_curve(curve);
3673 local->key_tls = pub_key;
3674 pub_key = NULL;
3675 *public = local;
3676 local = NULL;
3677
3678 ret = 1;
3679fail:
3680 if (priv_key)
3681 gnutls_privkey_deinit(priv_key);
3682 if (pub_key)
3683 gnutls_pubkey_deinit(pub_key);
3684 gnutls_free(local);
3685 gnutls_free(data.data);
3686 gnutls_free(key.data);
3687 return ret == 1 ? 1 : 0;
3688}
3689
3690int
3691coap_crypto_read_asn1_public_cred(coap_bin_const_t *binary,
3692 coap_crypto_cred_t **public) {
3693 gnutls_pubkey_t pub_key = NULL;
3694 gnutls_privkey_t priv_key = NULL;
3695 coap_crypto_cred_t *local = gnutls_malloc(sizeof(coap_crypto_cred_t));
3696 int ret = 0;
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;
3702
3703 if (local == NULL)
3704 goto fail;
3705
3706 memset(local, 0, sizeof(coap_crypto_cred_t));
3707 G_CHECK(gnutls_pubkey_init(&pub_key), "gnutls_pubkey_init");
3708 data.size = binary->length;
3709 /* Need to do this to not get a compiler warning about const parameters */
3710 memcpy(&data.data, &binary->s, sizeof(data.data));
3711 if (data.data[2] == 0x02) {
3712 /* Is an Integer, not a Sequence, hence private key */
3713 G_CHECK(gnutls_privkey_init(&priv_key), "gnutls_privkey_init");
3714 G_CHECK(gnutls_privkey_import_x509_raw(priv_key,
3715 &data,
3716 GNUTLS_X509_FMT_DER,
3717 NULL,
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");
3724 } else {
3725 G_CHECK(gnutls_pubkey_import(pub_key, &data, GNUTLS_X509_FMT_DER),
3726 "gnutls_pubkey_import");
3727 }
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");
3731 local->pub_der = coap_new_bin_const(key.data, key.size);
3732 if (local->pub_der == NULL)
3733 goto fail;
3734 G_CHECK(gnutls_pubkey_get_preferred_hash_algorithm(pub_key, &hash, &mand),
3735 "gnutls_pubkey_get_preferred_hash_algorithm");
3736 local->sign_hash = get_alg_hash(hash);
3737 local->wire_sign_size = cose_hash_len(local->sign_hash);
3738 switch (gnutls_pubkey_get_pk_algorithm(pub_key, NULL)) {
3739 case GNUTLS_PK_EDDSA_ED25519:
3740 break;
3741 case GNUTLS_PK_ECDSA:
3742 local->wire_sign_size *= 2;
3743 break;
3744 default:
3745 break;
3746 }
3747 G_CHECK(gnutls_pubkey_export_ecc_raw2(pub_key, &curve, NULL, NULL, 0),
3748 "gnutls_pubkey_export_ecc_raw2");
3749 local->sign_curve = get_alg_curve(curve);
3750 local->key_tls = pub_key;
3751 pub_key = NULL;
3752 *public = local;
3753 local = NULL;
3754
3755 ret = 1;
3756fail:
3757 if (priv_key)
3758 gnutls_privkey_deinit(priv_key);
3759 if (pub_key)
3760 gnutls_pubkey_deinit(pub_key);
3761 gnutls_free(binary);
3762 gnutls_free(key.data);
3763 gnutls_free(local);
3764 return ret == 1 ? 1 : 0;
3765}
3766
3767int
3768coap_crypto_read_raw_public_cred(cose_curve_t curve,
3769 coap_bin_const_t *binary,
3770 coap_crypto_cred_t **public) {
3771 (void)curve;
3772 (void)public;
3773
3774 coap_delete_bin_const(binary);
3775 return 0;
3776}
3777
3779coap_crypto_duplicate_public_cred(coap_crypto_cred_t *key) {
3780 if (key) {
3781 gnutls_pubkey_t pub_key = NULL;
3782 int ret = 0;
3783 gnutls_datum_t data = {NULL, 0};
3784 coap_crypto_cred_t *new_key =
3785 gnutls_malloc(sizeof(coap_crypto_cred_t));
3786
3787 if (new_key == NULL)
3788 goto fail;
3789
3790 memcpy(new_key, key, sizeof(coap_crypto_cred_t));
3791 new_key->pub_der =
3793 if (new_key->pub_der == NULL)
3794 goto fail;
3795 G_CHECK(gnutls_pubkey_init(&pub_key), "gnutls_pubkey_init");
3796 data.size = new_key->pub_der->length;
3797 /* Need to do this to not get a compiler warning about const parameters */
3798 memcpy(&data.data, &new_key->pub_der->s, sizeof(data.data));
3799 G_CHECK(
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");
3804 new_key->key_tls = pub_key;
3805 return new_key;
3806
3807fail:
3808 if (pub_key)
3809 gnutls_pubkey_deinit(pub_key);
3810 gnutls_free(new_key);
3811 }
3812 return NULL;
3813}
3814
3815void
3816coap_crypto_delete_public_cred(coap_crypto_cred_t *key) {
3817 if (key) {
3818 gnutls_pubkey_deinit(key->key_tls);
3821 gnutls_free(key);
3822 }
3823}
3824
3825int
3826coap_crypto_hash_sign(cose_alg_t hash,
3827 coap_binary_t *signature,
3828 coap_bin_const_t *text,
3829 coap_crypto_pri_key_t *private_key) {
3830 size_t hash_length;
3831 gnutls_digest_algorithm_t gnutls_dig = get_hash_alg(hash, &hash_length);
3832 int ret = 0;
3833 gnutls_privkey_t priv_key = private_key->key_tls;
3834 gnutls_datum_t data;
3835 gnutls_datum_t sign;
3836 coap_binary_t sign_b;
3837 coap_binary_t *sign_bp = NULL;
3838
3839 if (gnutls_dig == 0)
3840 goto fail;
3841
3842 data.size = text->length;
3843 /* Need to do this to not get a compiler warning about const parameters */
3844 memcpy(&data.data, &text->s, sizeof(data.data));
3845 sign.size = signature->length;
3846 sign.data = signature->s;
3847 G_CHECK(
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;
3855 "ASN.1 Sign",
3856 (coap_bin_const_t *)&sign_b);
3857 /* Need to convert asn.1 to 64 bytes */
3858 sign_bp = coap_asn1_split_r_s(&sign_b, 64);
3859 gnutls_free(sign.data);
3860 if (sign_bp) {
3861 memcpy(signature->s, sign_bp->s, sign_bp->length);
3862 signature->length = sign_bp->length;
3863 }
3864 break;
3865 default:
3866 coap_log_debug("check id %d\n",
3867 gnutls_privkey_get_pk_algorithm(priv_key, NULL));
3868 /* Fall Through */
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);
3874 break;
3875 }
3876 ret = signature->length;
3877
3878fail:
3879 coap_delete_binary(sign_bp);
3880 return ret;
3881}
3882
3883int
3884coap_crypto_hash_verify(cose_alg_t hash,
3885 coap_binary_t *signature,
3886 coap_bin_const_t *text,
3887 coap_crypto_cred_t *public_cred) {
3888 size_t hash_length;
3889 gnutls_digest_algorithm_t gnutls_dig = get_hash_alg(hash, &hash_length);
3890 int ret = 0;
3891 gnutls_pubkey_t pub_key = public_cred->key_tls;
3892 gnutls_datum_t data;
3893 gnutls_datum_t sign;
3894 coap_binary_t *sign_bp = NULL;
3895
3896 if (gnutls_dig == 0)
3897 goto fail;
3898
3899 (void)signature;
3900 (void)text;
3901 (void)public_cred;
3902 data.size = text->length;
3903 /* Need to do this to not get a compiler warning about const parameters */
3904 memcpy(&data.data, &text->s, sizeof(data.data));
3905 switch (gnutls_pubkey_get_pk_algorithm(pub_key, NULL)) {
3906 case GNUTLS_PK_ECDSA:
3907 /* Need to convert r + s to ASN.1 */
3908 sign_bp = coap_asn1_r_s_join(signature);
3909 if (sign_bp == NULL) {
3910 coap_log_warn("Unable to create ASN.1 for r + s\n");
3911 goto fail;
3912 }
3914 "ASN.1 Sign",
3915 (coap_bin_const_t *)sign_bp);
3916 sign.size = sign_bp->length;
3917 sign.data = sign_bp->s;
3918 break;
3919 default:
3920 coap_log_debug("check id %d\n",
3921 gnutls_pubkey_get_pk_algorithm(pub_key, NULL));
3922 /* Fall Through */
3923 case GNUTLS_PK_EDDSA_ED25519:
3924 sign.size = signature->length;
3925 sign.data = signature->s;
3926 break;
3927 }
3928#if 1
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");
3934#else
3935 G_CHECK(gnutls_pubkey_verify_data2(pub_key,
3936 GNUTLS_SIGN_EDDSA_ED25519,
3937 0,
3938 &data,
3939 &sign),
3940 "gnutls_pubkey_verify_data2");
3941#endif
3942 ret = 1;
3943
3944fail:
3945 coap_delete_binary(sign_bp);
3946 return ret == 1 ? 1 : 0;
3947}
3948
3949int
3950coap_crypto_gen_pkey(cose_curve_t curve,
3951 coap_bin_const_t **private,
3952 coap_bin_const_t **public) {
3953 (void)curve;
3954 (void)private;
3955 (void)public;
3956 return 0;
3957}
3958
3959int
3960coap_crypto_derive_shared_secret(cose_alg_t alg_pairwise,
3961 coap_bin_const_t *raw_local_private,
3962 coap_bin_const_t *raw_peer_public,
3963 coap_bin_const_t **shared_secret) {
3964 (void)alg_pairwise;
3965 (void)raw_local_private;
3966 (void)raw_peer_public;
3967 (void)shared_secret;
3968 return 0;
3969}
3970
3971#endif /* COAP_OSCORE_GROUP_SUPPORT */
3972
3973#endif /* COAP_OSCORE_SUPPORT */
3974
3975#else /* ! COAP_WITH_LIBGNUTLS */
3976
3977#ifdef __clang__
3978/* Make compilers happy that do not like empty modules. As this function is
3979 * never used, we ignore -Wunused-function at the end of compiling this file
3980 */
3981#pragma GCC diagnostic ignored "-Wunused-function"
3982#endif
3983static inline void
3984dummy(void) {
3985}
3986
3987#endif /* ! COAP_WITH_LIBGNUTLS */
#define min(a, b)
Definition coap_block.c:21
static void dummy(void)
static void dummy(void)
#define COAP_SERVER_SUPPORT
#define PRIuS
#define PRIdS
const char * coap_socket_strerror(void)
Definition coap_io.c:971
#define COAP_RXBUFFER_SIZE
Definition coap_io.h:31
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:68
int coap_fd_t
Definition coap_io.h:49
@ COAP_LAYER_TLS
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)
Definition coap_notls.c:395
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
Definition coap_notls.c:507
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:579
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:502
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:521
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:441
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:539
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:411
static coap_log_t dtls_log_level
Definition coap_notls.c:438
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:434
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:490
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition coap_notls.c:567
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:486
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)
Definition coap_notls.c:403
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:516
void coap_dtls_free_context(void *handle COAP_UNUSED)
Definition coap_notls.c:464
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:482
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
Definition coap_notls.c:459
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition coap_notls.c:558
#define NULL
Definition coap_option.h:30
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.
Definition coap_asn1.c:192
coap_binary_t * coap_asn1_r_s_join(coap_binary_t *r_s)
Join the r and s components into ASN1.
Definition coap_asn1.c:272
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
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.
Definition coap_net.c:5563
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.
Definition coap_net.c:3214
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.
Definition coap_notls.c:109
#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.
Definition coap_dtls.c:159
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.
Definition coap_dtls.c:288
void coap_dtls_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c:118
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.
Definition coap_notls.c:497
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c:113
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.
Definition coap_dtls.c:26
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_KEY_CA
@ COAP_DEFINE_KEY_PUBLIC
@ COAP_DEFINE_FAIL_NONE
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
@ COAP_DEFINE_FAIL_BAD
#define COAP_DTLS_HINT_LENGTH
Definition coap_dtls.h:39
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
Definition coap_notls.c:101
struct coap_dtls_key_t coap_dtls_key_t
The structure that holds the PKI key information.
coap_dtls_role_t
Definition coap_dtls.h:48
#define COAP_DTLS_RPK_CERT_CN
Definition coap_dtls.h:53
struct coap_dtls_spsk_info_t coap_dtls_spsk_info_t
The structure that holds the Server Pre-Shared Key and Identity Hint information.
coap_tls_library_t
Definition coap_dtls.h:74
struct coap_dtls_pki_t coap_dtls_pki_t
Definition coap_dtls.h:36
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
Definition coap_dtls.h:250
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
Definition coap_dtls.h:247
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
Definition coap_dtls.h:241
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
Definition coap_dtls.h:239
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
Definition coap_dtls.h:256
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
Definition coap_dtls.h:243
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
Definition coap_dtls.h:245
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
Definition coap_dtls.h:253
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
Definition coap_dtls.h:50
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition coap_dtls.h:49
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
Definition coap_dtls.h:177
@ COAP_TLS_LIBRARY_GNUTLS
Using GnuTLS library.
Definition coap_dtls.h:78
@ COAP_EVENT_DTLS_CLOSED
Triggerrd when (D)TLS session closed.
Definition coap_event.h:41
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
Definition coap_event.h:43
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
Definition coap_event.h:47
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
coap_log_t
Logging type.
Definition coap_debug.h:56
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition coap_notls.c:454
#define coap_dtls_log(level,...)
Logging function.
Definition coap_debug.h:306
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition coap_notls.c:449
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
@ COAP_LOG_OSCORE
Definition coap_debug.h:65
@ COAP_LOG_EMERG
Definition coap_debug.h:57
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
@ COAP_LOG_WARN
Definition coap_debug.h:61
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
Definition coap_netif.c:25
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
cose_hkdf_alg_t
size_t cose_hash_len(cose_alg_t cose_alg)
cose_hmac_alg_t
cose_curve_t
Definition oscore_cose.h:62
cose_alg_t
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_CURVE_X25519
Definition oscore_cose.h:64
@ COSE_CURVE_ED448
Definition oscore_cose.h:67
@ COSE_CURVE_P_256
Definition oscore_cose.h:63
@ COSE_CURVE_ED25519
Definition oscore_cose.h:66
@ COSE_CURVE_X448
Definition oscore_cose.h:65
@ COSE_ALGORITHM_SHA_256_256
@ COSE_ALGORITHM_SHA_1
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_SHA_512
@ COSE_ALGORITHM_AES_CCM_16_64_256
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
@ COAP_PROTO_DTLS
Definition coap_pdu.h:248
@ COAP_PROTO_TLS
Definition coap_pdu.h:250
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_CSM
@ 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.
Definition coap_str.c:130
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:81
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...
Definition coap_str.c:119
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
Definition coap_notls.c:86
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
Definition coap_notls.c:50
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:41
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.
Definition coap_notls.c:36
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
Definition coap_notls.c:59
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
Definition coap_notls.c:77
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
Definition coap_notls.c:68
#define COAP_UNUSED
Definition libcoap.h:74
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
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.
Definition coap_dtls.h:387
coap_bin_const_t key
Definition coap_dtls.h:389
coap_bin_const_t identity
Definition coap_dtls.h:388
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:418
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
Definition coap_dtls.h:425
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
Definition coap_dtls.h:442
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:445
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
Definition coap_dtls.h:441
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:423
The structure that holds the PKI key information.
Definition coap_dtls.h:284
coap_pki_key_define_t define
for definable type keys
Definition coap_dtls.h:291
union coap_dtls_key_t::@165145277033177166324225120075242371261276053256 key
coap_pki_key_t key_type
key format type
Definition coap_dtls.h:285
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:317
uint8_t allow_no_crl
1 ignore if CRL not there
Definition coap_dtls.h:331
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
Definition coap_dtls.h:328
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
Definition coap_dtls.h:338
uint8_t check_cert_revocation
1 if revocation checks wanted
Definition coap_dtls.h:330
uint8_t cert_chain_verify_depth
recommended depth is 3
Definition coap_dtls.h:329
uint8_t allow_expired_certs
1 if expired certs are allowed
Definition coap_dtls.h:327
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:322
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:376
uint8_t allow_self_signed
1 if self-signed certs are allowed.
Definition coap_dtls.h:325
uint8_t allow_expired_crl
1 if expired crl is allowed
Definition coap_dtls.h:332
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
Definition coap_dtls.h:335
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
Definition coap_dtls.h:323
coap_dtls_key_t pki_key
PKI key definition.
Definition coap_dtls.h:381
The structure that holds the Server Pre-Shared Key and Identity Hint information.
Definition coap_dtls.h:458
coap_bin_const_t hint
Definition coap_dtls.h:459
The structure used for defining the Server PSK setup data to be used.
Definition coap_dtls.h:509
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
Definition coap_dtls.h:530
void * id_call_back_arg
Passed in to the Identity callback function.
Definition coap_dtls.h:531
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
Definition coap_dtls.h:514
coap_dtls_spsk_info_t psk_info
Server PSK definition.
Definition coap_dtls.h:541
coap_layer_read_t l_read
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
Definition coap_dtls.h:266
const char * user_pin
define: User pin to access type PKCS11.
Definition coap_dtls.h:276
coap_const_char_ptr_t private_key
define: Private Key
Definition coap_dtls.h:267
coap_const_char_ptr_t ca
define: Common CA Certificate
Definition coap_dtls.h:265
size_t public_cert_len
define Public Cert length (if needed)
Definition coap_dtls.h:269
size_t ca_len
define CA Cert length (if needed)
Definition coap_dtls.h:268
coap_pki_define_t private_key_def
define: Private Key type definition
Definition coap_dtls.h:273
size_t private_key_len
define Private Key length (if needed)
Definition coap_dtls.h:270
coap_pki_define_t ca_def
define: Common CA type definition
Definition coap_dtls.h:271
coap_pki_define_t public_cert_def
define: Public Cert type definition
Definition coap_dtls.h:272
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.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
The structure used for returning the underlying (D)TLS library information.
Definition coap_dtls.h:88
uint64_t built_version
(D)TLS Built against Library Version
Definition coap_dtls.h:91
coap_tls_library_t type
Library type.
Definition coap_dtls.h:90
uint64_t version
(D)TLS runtime Library Version
Definition coap_dtls.h:89
const char * s_byte
signed char ptr
Definition coap_str.h:84
const uint8_t * u_byte
unsigned char ptr
Definition coap_str.h:85