-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathcoap_gnutls.c
More file actions
3417 lines (3081 loc) · 107 KB
/
Copy pathcoap_gnutls.c
File metadata and controls
3417 lines (3081 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* coap_gnutls.c -- GnuTLS Datagram Transport Layer Support for libcoap
*
* Copyright (C) 2017 Dag Bjorklund <dag.bjorklund@comsel.fi>
* Copyright (C) 2018-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_gnutls.c
* @brief GnuTLS interafe functions
*/
/*
* Naming used to prevent confusion between coap sessions, gnutls sessions etc.
* when reading the code.
*
* c_context A coap_context_t *
* c_session A coap_session_t *
* g_context A coap_gnutls_context_t * (held in c_context->dtls_context)
* g_session A gnutls_session_t (which has the * in the typedef)
* g_env A coap_gnutls_env_t * (held in c_session->tls)
*/
/*
* Notes
*
* There is a memory leak in GnuTLS prior to 3.3.26 when hint is not freed off
* when server psk credentials are freed off.
*
* ca_path in coap_dtls_context_set_pki_root_cas() is not supported until 3.3.6
*
* Identity Hint is not provided if using DH and versions prior to 3.4.4
*
* 3.5.5 or later is required to interoperate with TinyDTLS as CCM algorithm
* support is required.
*
* TLS 1.3 is properly supported from 3.6.5 onwards
* (but is not enabled by default in 3.6.4)
*
* Starting with 3.6.3, fixed in 3.6.13, Client Hellos may fail with some
* server implementations (e.g. Californium) as random value is all zeros
* - CVE-2020-11501 - a security weakness.
* 3.6.6 or later is required to support Raw Public Key(RPK)
*/
#include "coap3/coap_libcoap_build.h"
#if COAP_WITH_LIBGNUTLS
#define MIN_GNUTLS_VERSION "3.3.0"
#include <stdio.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/dtls.h>
#include <gnutls/pkcs11.h>
#include <gnutls/crypto.h>
#include <gnutls/abstract.h>
#include <unistd.h>
#if (GNUTLS_VERSION_NUMBER >= 0x030606)
#define COAP_GNUTLS_KEY_RPK GNUTLS_KEY_DIGITAL_SIGNATURE | \
GNUTLS_KEY_NON_REPUDIATION | \
GNUTLS_KEY_KEY_ENCIPHERMENT | \
GNUTLS_KEY_DATA_ENCIPHERMENT | \
GNUTLS_KEY_KEY_AGREEMENT | \
GNUTLS_KEY_KEY_CERT_SIGN
#endif /* GNUTLS_VERSION_NUMBER >= 0x030606 */
#ifndef GNUTLS_CRT_RAW
#define GNUTLS_CRT_RAW GNUTLS_CRT_RAWPK
#endif /* GNUTLS_CRT_RAW */
#ifdef _WIN32
#define strcasecmp _stricmp
#endif
typedef struct coap_ssl_t {
const uint8_t *pdu;
unsigned pdu_len;
unsigned peekmode;
gnutls_datum_t cookie_key;
} coap_ssl_t;
/*
* This structure encapsulates the GnuTLS session object.
* It handles both TLS and DTLS.
* c_session->tls points to this.
*/
typedef struct coap_gnutls_env_t {
gnutls_session_t g_session;
gnutls_psk_client_credentials_t psk_cl_credentials;
gnutls_psk_server_credentials_t psk_sv_credentials;
gnutls_certificate_credentials_t pki_credentials;
coap_ssl_t coap_ssl_data;
/* If not set, need to do gnutls_handshake */
int established;
int doing_dtls_timeout;
coap_tick_t last_timeout;
int sent_alert;
} coap_gnutls_env_t;
#define IS_PSK (1 << 0)
#define IS_PKI (1 << 1)
#define IS_CLIENT (1 << 6)
#define IS_SERVER (1 << 7)
typedef struct pki_sni_entry {
char *sni;
coap_dtls_key_t pki_key;
gnutls_certificate_credentials_t pki_credentials;
} pki_sni_entry;
typedef struct psk_sni_entry {
char *sni;
coap_dtls_spsk_info_t psk_info;
gnutls_psk_server_credentials_t psk_credentials;
} psk_sni_entry;
typedef struct coap_gnutls_context_t {
coap_dtls_pki_t setup_data;
int psk_pki_enabled;
size_t pki_sni_count;
pki_sni_entry *pki_sni_entry_list;
size_t psk_sni_count;
psk_sni_entry *psk_sni_entry_list;
gnutls_datum_t alpn_proto; /* Will be "coap", but that is a const */
char *root_ca_file;
char *root_ca_path;
int trust_store_defined;
gnutls_priority_t priority_cache;
} coap_gnutls_context_t;
typedef enum coap_free_bye_t {
COAP_FREE_BYE_AS_TCP, /**< call gnutls_bye() with GNUTLS_SHUT_RDWR */
COAP_FREE_BYE_AS_UDP, /**< call gnutls_bye() with GNUTLS_SHUT_WR */
COAP_FREE_BYE_NONE /**< do not call gnutls_bye() */
} coap_free_bye_t;
#define VARIANTS_3_6_6 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL:+SHA256"
#define VARIANTS_3_5_5 "NORMAL:+ECDHE-PSK:+PSK:+ECDHE-ECDSA:+AES-128-CCM-8"
#define VARIANTS_BASE "NORMAL:+ECDHE-PSK:+PSK"
#define VARIANTS_NO_TLS13_3_6_6 VARIANTS_3_6_6 ":-VERS-TLS1.3"
#define VARIANTS_NO_TLS13_3_6_4 VARIANTS_3_5_5 ":-VERS-TLS1.3"
#define G_ACTION(xx) do { \
ret = (xx); \
} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
#define G_CHECK(xx,func) do { \
if ((ret = (xx)) < 0) { \
coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \
goto fail; \
} \
} while (0)
#define G_ACTION_CHECK(xx,func) do { \
G_ACTION(xx); \
G_CHECK(xx, func); \
} while 0
static coap_log_t dtls_log_level = COAP_LOG_EMERG;
#if COAP_SERVER_SUPPORT
static int post_client_hello_gnutls_pki(gnutls_session_t g_session);
static int post_client_hello_gnutls_psk(gnutls_session_t g_session);
static int psk_server_callback(gnutls_session_t g_session,
const char *identity,
gnutls_datum_t *key);
#endif /* COAP_SERVER_SUPPORT */
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_is_supported(void) {
if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
coap_log_err("GnuTLS " MIN_GNUTLS_VERSION " or later is required\n");
return 0;
}
return 1;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_tls_is_supported(void) {
#if !COAP_DISABLE_TCP
if (gnutls_check_version(MIN_GNUTLS_VERSION) == NULL) {
coap_log_err("GnuTLS " MIN_GNUTLS_VERSION " or later is required\n");
return 0;
}
return 1;
#else /* COAP_DISABLE_TCP */
return 0;
#endif /* COAP_DISABLE_TCP */
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_psk_is_supported(void) {
return 1;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_pki_is_supported(void) {
return 1;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_pkcs11_is_supported(void) {
return 1;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_rpk_is_supported(void) {
#if (GNUTLS_VERSION_NUMBER >= 0x030606)
return 1;
#else /* GNUTLS_VERSION_NUMBER < 0x030606 */
return 0;
#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_cid_is_supported(void) {
return 0;
}
#if COAP_CLIENT_SUPPORT
int
coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
(void)c_context;
(void)every;
return 0;
}
#endif /* COAP_CLIENT_SUPPORT */
coap_tls_version_t *
coap_get_tls_library_version(void) {
static coap_tls_version_t version;
const char *vers = gnutls_check_version(NULL);
version.version = 0;
if (vers) {
int p1, p2, p3;
sscanf(vers, "%d.%d.%d", &p1, &p2, &p3);
version.version = (p1 << 16) | (p2 << 8) | p3;
}
version.built_version = GNUTLS_VERSION_NUMBER;
version.type = COAP_TLS_LIBRARY_GNUTLS;
return &version;
}
static void
coap_gnutls_audit_log_func(gnutls_session_t g_session, const char *text) {
#if COAP_MAX_LOGGING_LEVEL > 0
if (g_session) {
coap_session_t *c_session =
(coap_session_t *)gnutls_transport_get_ptr(g_session);
coap_log_warn("** %s: %s",
coap_session_str(c_session), text);
} else {
coap_log_warn("** (null): %s", text);
}
#else /* COAP_MAX_LOGGING_LEVEL == 0 */
(void)g_session;
(void)text;
#endif /* COAP_MAX_LOGGING_LEVEL == 0 */
}
static void
coap_gnutls_log_func(int level, const char *text) {
/* Things get noisy, even at level 1 */
if (level > 0)
level += COAP_LOG_WARN;
if (level > COAP_LOG_DEBUG)
level = COAP_LOG_DEBUG;
coap_dtls_log(level, "%s", text);
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_context_set_pki(coap_context_t *c_context,
const coap_dtls_pki_t *setup_data,
const coap_dtls_role_t role COAP_UNUSED) {
coap_dtls_key_t key;
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
if (!g_context || !setup_data)
return 0;
g_context->setup_data = *setup_data;
if (!g_context->setup_data.verify_peer_cert) {
/* Needs to be clear so that no CA DNs are transmitted */
g_context->setup_data.check_common_ca = 0;
if (g_context->setup_data.is_rpk_not_cert) {
/* Disable all of these as they cannot be checked */
g_context->setup_data.allow_self_signed = 0;
g_context->setup_data.allow_expired_certs = 0;
g_context->setup_data.cert_chain_validation = 0;
g_context->setup_data.cert_chain_verify_depth = 0;
g_context->setup_data.check_cert_revocation = 0;
g_context->setup_data.allow_no_crl = 0;
g_context->setup_data.allow_expired_crl = 0;
g_context->setup_data.allow_bad_md_hash = 0;
g_context->setup_data.allow_short_rsa_length = 0;
} else {
/* Allow all of these but warn if issue */
g_context->setup_data.allow_self_signed = 1;
g_context->setup_data.allow_expired_certs = 1;
g_context->setup_data.cert_chain_validation = 1;
g_context->setup_data.cert_chain_verify_depth = 10;
g_context->setup_data.check_cert_revocation = 1;
g_context->setup_data.allow_no_crl = 1;
g_context->setup_data.allow_expired_crl = 1;
g_context->setup_data.allow_bad_md_hash = 1;
g_context->setup_data.allow_short_rsa_length = 1;
}
}
/* Map over to the new define format to save code duplication */
coap_dtls_map_key_type_to_define(&g_context->setup_data, &key);
g_context->setup_data.pki_key = key;
g_context->psk_pki_enabled |= IS_PKI;
if (setup_data->use_cid) {
coap_log_warn("GnuTLS has no Connection-ID support\n");
}
return 1;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_context_set_pki_root_cas(coap_context_t *c_context,
const char *ca_file,
const char *ca_path) {
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
if (!g_context) {
coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment "
"not set up\n");
return 0;
}
if (ca_file == NULL && ca_path == NULL) {
coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path "
"not defined\n");
return 0;
}
if (g_context->root_ca_file) {
gnutls_free(g_context->root_ca_file);
g_context->root_ca_file = NULL;
}
if (ca_file) {
g_context->root_ca_file = gnutls_strdup(ca_file);
}
if (g_context->root_ca_path) {
gnutls_free(g_context->root_ca_path);
g_context->root_ca_path = NULL;
}
if (ca_path) {
#if (GNUTLS_VERSION_NUMBER >= 0x030306)
g_context->root_ca_path = gnutls_strdup(ca_path);
#else
coap_log_err("ca_path not supported in GnuTLS < 3.3.6\n");
#endif
}
return 1;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_context_load_pki_trust_store(coap_context_t *c_context) {
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
if (!g_context) {
coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment "
"not set up\n");
return 0;
}
#if (GNUTLS_VERSION_NUMBER >= 0x030020)
g_context->trust_store_defined = 1;
return 1;
#else
coap_log_warn("coap_context_set_pki_trust_store(): (D)TLS environment "
"not supported for GnuTLS < v3.0.20\n");
return 0;
#endif
}
#if COAP_SERVER_SUPPORT
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_context_set_spsk(coap_context_t *c_context,
coap_dtls_spsk_t *setup_data
) {
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
if (!g_context || !setup_data)
return 0;
if (setup_data->ec_jpake) {
coap_log_warn("GnuTLS has no EC-JPAKE support\n");
}
g_context->psk_pki_enabled |= IS_PSK;
return 1;
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_context_set_cpsk(coap_context_t *c_context,
coap_dtls_cpsk_t *setup_data
) {
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
if (!g_context || !setup_data)
return 0;
if (setup_data->ec_jpake) {
coap_log_warn("GnuTLS has no EC-JPAKE support\n");
}
if (setup_data->use_cid) {
coap_log_warn("GnuTLS has no Connection-ID support\n");
}
g_context->psk_pki_enabled |= IS_PSK;
return 1;
}
#endif /* COAP_CLIENT_SUPPORT */
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_context_check_keys_enabled(coap_context_t *c_context) {
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
return g_context->psk_pki_enabled ? 1 : 0;
}
void
coap_dtls_startup(void) {
gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func);
gnutls_global_set_log_function(coap_gnutls_log_func);
}
void
coap_dtls_shutdown(void) {
coap_dtls_set_log_level(COAP_LOG_EMERG);
}
void
coap_dtls_thread_shutdown(void) {
}
void *
coap_dtls_get_tls(const coap_session_t *c_session,
coap_tls_library_t *tls_lib) {
if (tls_lib)
*tls_lib = COAP_TLS_LIBRARY_GNUTLS;
if (c_session && c_session->tls) {
const coap_gnutls_env_t *g_env = (const coap_gnutls_env_t *)c_session->tls;
return g_env->g_session;
}
return NULL;
}
void
coap_dtls_set_log_level(coap_log_t level) {
dtls_log_level = level;
gnutls_global_set_log_level(dtls_log_level);
}
/*
* return current logging level
*/
coap_log_t
coap_dtls_get_log_level(void) {
return dtls_log_level;
}
/*
* return +ve new g_context
* NULL failure
*/
void *
coap_dtls_new_context(coap_context_t *c_context COAP_UNUSED) {
const char *err = "Unknown Error";
int ret;
coap_gnutls_context_t *g_context =
(coap_gnutls_context_t *)
gnutls_malloc(sizeof(coap_gnutls_context_t));
if (g_context) {
coap_tls_version_t *tls_version = coap_get_tls_library_version();
const char *priority;
memset(g_context, 0, sizeof(coap_gnutls_context_t));
G_CHECK(gnutls_global_init(), "gnutls_global_init");
g_context->alpn_proto.data = gnutls_malloc(4);
if (g_context->alpn_proto.data) {
memcpy(g_context->alpn_proto.data, "coap", 4);
g_context->alpn_proto.size = 4;
}
if (tls_version->version >= 0x030606) {
priority = VARIANTS_3_6_6;
} else if (tls_version->version >= 0x030505) {
priority = VARIANTS_3_5_5;
} else {
priority = VARIANTS_BASE;
}
ret = gnutls_priority_init(&g_context->priority_cache, priority, &err);
if (ret != GNUTLS_E_SUCCESS) {
if (ret == GNUTLS_E_INVALID_REQUEST)
coap_log_warn("gnutls_priority_init: Syntax error at: %s\n", err);
else
coap_log_warn("gnutls_priority_init: %s\n", gnutls_strerror(ret));
goto fail;
}
}
return g_context;
fail:
if (g_context)
coap_dtls_free_context(g_context);
return NULL;
}
void
coap_dtls_free_context(void *handle) {
size_t i;
coap_gnutls_context_t *g_context = (coap_gnutls_context_t *)handle;
gnutls_free(g_context->alpn_proto.data);
gnutls_free(g_context->root_ca_file);
gnutls_free(g_context->root_ca_path);
for (i = 0; i < g_context->pki_sni_count; i++) {
gnutls_free(g_context->pki_sni_entry_list[i].sni);
gnutls_certificate_free_credentials(
g_context->pki_sni_entry_list[i].pki_credentials);
}
if (g_context->pki_sni_entry_list)
gnutls_free(g_context->pki_sni_entry_list);
for (i = 0; i < g_context->psk_sni_count; i++) {
gnutls_free(g_context->psk_sni_entry_list[i].sni);
/* YUK - A memory leak in 3.3.0 (fixed by 3.3.26) of hint */
gnutls_psk_free_server_credentials(
g_context->psk_sni_entry_list[i].psk_credentials);
}
if (g_context->psk_sni_entry_list)
gnutls_free(g_context->psk_sni_entry_list);
gnutls_priority_deinit(g_context->priority_cache);
gnutls_global_deinit();
gnutls_free(g_context);
}
#if COAP_CLIENT_SUPPORT
/*
* gnutls_psk_client_credentials_function return values
* (see gnutls_psk_set_client_credentials_function())
*
* return -1 failed
* 0 passed
*/
static int
psk_client_callback(gnutls_session_t g_session,
char **username, gnutls_datum_t *key) {
coap_session_t *c_session =
(coap_session_t *)gnutls_transport_get_ptr(g_session);
coap_gnutls_context_t *g_context;
coap_dtls_cpsk_t *setup_data;
const char *hint = gnutls_psk_client_get_hint(g_session);
coap_bin_const_t temp;
const coap_bin_const_t *psk_key;
const coap_bin_const_t *psk_identity;
const coap_dtls_cpsk_info_t *cpsk_info;
/* Initialize result parameters. */
*username = NULL;
key->data = NULL;
if (c_session == NULL)
return -1;
g_context = (coap_gnutls_context_t *)c_session->context->dtls_context;
if (g_context == NULL)
return -1;
setup_data = &c_session->cpsk_setup_data;
temp.s = hint ? (const uint8_t *)hint : (const uint8_t *)"";
temp.length = strlen((const char *)temp.s);
coap_session_refresh_psk_hint(c_session, &temp);
coap_log_debug("got psk_identity_hint: '%.*s'\n", (int)temp.length,
(const char *)temp.s);
if (setup_data->validate_ih_call_back) {
coap_str_const_t lhint;
lhint.length = temp.length;
lhint.s = temp.s;
coap_lock_callback_ret(cpsk_info,
setup_data->validate_ih_call_back(&lhint,
c_session,
setup_data->ih_call_back_arg));
if (cpsk_info == NULL)
return -1;
coap_session_refresh_psk_identity(c_session, &cpsk_info->identity);
coap_session_refresh_psk_key(c_session, &cpsk_info->key);
psk_identity = &cpsk_info->identity;
psk_key = &cpsk_info->key;
} else {
psk_identity = coap_get_session_client_psk_identity(c_session);
psk_key = coap_get_session_client_psk_key(c_session);
}
if (psk_identity == NULL || psk_key == NULL) {
coap_log_warn("no PSK available\n");
return -1;
}
*username = gnutls_malloc(psk_identity->length+1);
if (*username == NULL)
return -1;
memcpy(*username, psk_identity->s, psk_identity->length);
(*username)[psk_identity->length] = '\000';
key->data = gnutls_malloc(psk_key->length);
if (key->data == NULL) {
gnutls_free(*username);
*username = NULL;
return -1;
}
memcpy(key->data, psk_key->s, psk_key->length);
key->size = psk_key->length;
return 0;
}
#endif /* COAP_CLIENT_SUPPORT */
typedef struct {
gnutls_certificate_type_t certificate_type;
char *san_or_cn;
const gnutls_datum_t *cert_list;
unsigned int cert_list_size;
int self_signed; /* 1 if cert self-signed, 0 otherwise */
} coap_gnutls_certificate_info_t;
/*
* return Type of certificate and SAN or CN if appropriate derived from
* certificate. GNUTLS_CRT_UNKNOWN if failure.
*/
static gnutls_certificate_type_t
get_san_or_cn(coap_session_t *session, gnutls_session_t g_session,
coap_gnutls_certificate_info_t *cert_info, const char *sni_match) {
gnutls_x509_crt_t cert;
char dn[256];
size_t size;
int n;
char *cn;
int ret;
unsigned int seq;
#if (GNUTLS_VERSION_NUMBER >= 0x030606)
cert_info->certificate_type = gnutls_certificate_type_get2(g_session,
GNUTLS_CTYPE_PEERS);
#else /* < 3.6.6 */
cert_info->certificate_type = gnutls_certificate_type_get(g_session);
#endif /* < 3.6.6 */
cert_info->san_or_cn = NULL;
cert_info->cert_list = gnutls_certificate_get_peers(g_session,
&cert_info->cert_list_size);
if (cert_info->cert_list_size == 0) {
return GNUTLS_CRT_UNKNOWN;
}
if (cert_info->certificate_type != GNUTLS_CRT_X509)
return cert_info->certificate_type;
G_CHECK(gnutls_x509_crt_init(&cert), "gnutls_x509_crt_init");
/* Interested only in first cert in chain */
G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0],
GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import");
cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert);
/* See if there is a Subject Alt Name first */
for (seq = 0; seq < 100; seq++) {
size = sizeof(dn) -1;
ret = gnutls_x509_crt_get_subject_alt_name(cert, seq, dn, &size, NULL);
if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
/* Reached the end of the SAN list */
break;
}
if (ret >= 0) {
dn[size] = '\000';
if (sni_match) {
if (strlen(sni_match) != size ||
memcmp(dn, sni_match, size)) {
continue;
}
}
gnutls_x509_crt_deinit(cert);
cert_info->san_or_cn = gnutls_strdup(dn);
return cert_info->certificate_type;
}
}
size = sizeof(dn);
G_CHECK(gnutls_x509_crt_get_dn(cert, dn, &size), "gnutls_x509_crt_get_dn");
/* Need to emulate strcasestr() here. Looking for CN= */
n = strlen(dn) - 3;
cn = dn;
while (n > 0) {
if (((cn[0] == 'C') || (cn[0] == 'c')) &&
((cn[1] == 'N') || (cn[1] == 'n')) &&
(cn[2] == '=')) {
cn += 3;
break;
}
cn++;
n--;
}
if (n > 0) {
char *ecn = strchr(cn, ',');
if (ecn) {
cn[ecn-cn] = '\000';
}
if (sni_match) {
if (!coap_pki_name_match_sni(cn, strlen(cn), sni_match)) {
coap_log_debug(" %s: got CN '%s'\n",
coap_session_str(session), cn);
goto no_match;
}
}
gnutls_x509_crt_deinit(cert);
cert_info->san_or_cn = gnutls_strdup(cn);
return cert_info->certificate_type;
}
no_match:
n = 0;
for (seq = 0; seq < 100; seq++) {
size = sizeof(dn) -1;
ret = gnutls_x509_crt_get_subject_alt_name(cert, seq, dn, &size, NULL);
if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
/* Reached the end of the SAN list */
break;
}
if (ret >= 0) {
dn[size] = '\000';
coap_log_debug(" %s: got DNS.%d '%s'\n",
coap_session_str(session), n + 1, dn);
}
n++;
continue;
}
gnutls_x509_crt_deinit(cert);
return GNUTLS_CRT_UNKNOWN;
fail:
return GNUTLS_CRT_UNKNOWN;
}
#if (GNUTLS_VERSION_NUMBER >= 0x030606)
#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
cert_info.san_or_cn : \
cert_type == GNUTLS_CRT_RAW ? \
COAP_DTLS_RPK_CERT_CN : "?")
#else /* GNUTLS_VERSION_NUMBER < 0x030606 */
#define OUTPUT_CERT_NAME (cert_type == GNUTLS_CRT_X509 ? \
cert_info.san_or_cn : "?")
#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */
#if (GNUTLS_VERSION_NUMBER >= 0x030606)
static int
check_rpk_cert(coap_gnutls_context_t *g_context,
coap_gnutls_certificate_info_t *cert_info,
coap_session_t *c_session) {
int ret;
if (g_context->setup_data.validate_cn_call_back) {
gnutls_pcert_st pcert;
uint8_t der[2048];
size_t size;
G_CHECK(gnutls_pcert_import_rawpk_raw(&pcert, &cert_info->cert_list[0],
GNUTLS_X509_FMT_DER, 0, 0),
"gnutls_pcert_import_rawpk_raw");
size = sizeof(der);
G_CHECK(gnutls_pubkey_export(pcert.pubkey, GNUTLS_X509_FMT_DER, der, &size),
"gnutls_pubkey_export");
gnutls_pcert_deinit(&pcert);
coap_lock_callback_ret(ret,
g_context->setup_data.validate_cn_call_back(COAP_DTLS_RPK_CERT_CN,
der,
size,
c_session,
0,
1,
g_context->setup_data.cn_call_back_arg));
if (!ret) {
return 0;
}
}
return 1;
fail:
return 0;
}
#endif /* >= 3.6.6 */
/*
* return 0 failed
* 1 passed
*/
static int
cert_verify_gnutls(gnutls_session_t g_session) {
unsigned int status = 0;
unsigned int fail = 0;
coap_session_t *c_session =
(coap_session_t *)gnutls_transport_get_ptr(g_session);
coap_gnutls_context_t *g_context =
(coap_gnutls_context_t *)c_session->context->dtls_context;
coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls;
int alert = GNUTLS_A_BAD_CERTIFICATE;
int ret;
coap_gnutls_certificate_info_t cert_info;
gnutls_certificate_type_t cert_type;
memset(&cert_info, 0, sizeof(cert_info));
if (!g_context->setup_data.allow_sni_cn_mismatch &&
c_session->type == COAP_SESSION_TYPE_CLIENT &&
g_context->setup_data.client_sni) {
cert_type = get_san_or_cn(c_session, g_session, &cert_info, g_context->setup_data.client_sni);
if (cert_type == GNUTLS_CRT_UNKNOWN) {
coap_log_info(" %s: SNI '%s' not returned in certificate\n",
coap_session_str(c_session), g_context->setup_data.client_sni);
alert = GNUTLS_A_ACCESS_DENIED;
goto fail;
}
}
if (!cert_info.san_or_cn)
cert_type = get_san_or_cn(c_session, g_session, &cert_info, NULL);
#if (GNUTLS_VERSION_NUMBER >= 0x030606)
if (cert_type == GNUTLS_CRT_RAW) {
if (!check_rpk_cert(g_context, &cert_info, c_session)) {
alert = GNUTLS_A_ACCESS_DENIED;
goto fail;
}
goto ok;
}
#endif /* >= 3.6.6 */
if (cert_info.cert_list_size == 0) {
if (!g_context->setup_data.verify_peer_cert)
goto ok;
else
goto fail;
}
G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status),
"gnutls_certificate_verify_peers");
coap_dtls_log(COAP_LOG_DEBUG, "error %x cert '%s'\n",
status, cert_info.san_or_cn);
if (g_context->setup_data.validate_cn_call_back) {
gnutls_x509_crt_t cert;
uint8_t der[2048];
size_t size;
/* status == 0 indicates that the certificate passed to
* setup_data.validate_cn_call_back has been validated. */
const int cert_is_trusted = !status;
G_CHECK(gnutls_x509_crt_init(&cert), "gnutls_x509_crt_init");
/* Interested only in first cert in chain */
G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0],
GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import");
size = sizeof(der);
G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size),
"gnutls_x509_crt_export");
gnutls_x509_crt_deinit(cert);
coap_lock_callback_ret(ret,
g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME,
der,
size,
c_session,
0,
cert_is_trusted,
g_context->setup_data.cn_call_back_arg));
if (!ret) {
alert = GNUTLS_A_ACCESS_DENIED;
goto fail;
}
}
if (status) {
status &= ~(GNUTLS_CERT_INVALID);
if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) {
status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED);
if (g_context->setup_data.allow_expired_certs) {
coap_log_info(" %s: %s: overridden: '%s'\n",
coap_session_str(c_session),
"The certificate has an invalid usage date",
OUTPUT_CERT_NAME);
} else {
fail = 1;
coap_log_warn(" %s: %s: '%s'\n",
coap_session_str(c_session),
"The certificate has an invalid usage date",
OUTPUT_CERT_NAME);
}
}
if (status & (GNUTLS_CERT_UNEXPECTED_OWNER)) {
status &= ~(GNUTLS_CERT_UNEXPECTED_OWNER);
if (g_context->setup_data.allow_sni_cn_mismatch) {
coap_log_info(" %s: %s: overridden: '%s' v '%s'\n",
coap_session_str(c_session),
"The SNI does not match the returned CN",
g_context->setup_data.client_sni,
OUTPUT_CERT_NAME);
} else {
fail = 1;
coap_log_warn(" %s: %s: '%s' v '%s'\n",
coap_session_str(c_session),
"The SNI does not match the returned CN",
g_context->setup_data.client_sni,
OUTPUT_CERT_NAME);
}
}
if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)) {
status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED|
GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE);
if (g_context->setup_data.allow_expired_crl) {
coap_log_info(" %s: %s: overridden: '%s'\n",
coap_session_str(c_session),