-
Notifications
You must be signed in to change notification settings - Fork 14
/
tls_mbedtls_alt.c
3293 lines (2972 loc) · 118 KB
/
tls_mbedtls_alt.c
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
/*
* SSL/TLS interface functions for mbed TLS
*
* SPDX-FileCopyrightText: 2022 Glenn Strauss <gstrauss@gluelogic.com>
* SPDX-License-Identifier: BSD-3-Clause
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*
* template: src/crypto/tls_none.c
* reference: src/crypto/tls_*.c
*
* Known Limitations:
* - no TLSv1.3 (not available in mbedtls 2.x; experimental in mbedtls 3.x)
* - no OCSP (not yet available in mbedtls)
* - mbedtls does not support all certificate encodings used by hwsim tests
* PCKS#5 v1.5
* PCKS#12
* DH DSA
* - EAP-FAST, EAP-TEAP session ticket support not implemented in tls_mbedtls.c
* - mbedtls does not currently provide way to set an attribute in a CSR
* https://github.com/Mbed-TLS/mbedtls/issues/4886
* so tests/hwsim dpp_enterprise tests fail
* - DPP2 not supported
* PKCS#7 parsing is not supported in mbedtls
* See crypto_mbedtls.c:crypto_pkcs7_get_certificates() comments
* - DPP3 not supported
* hpke_base_seal() and hpke_base_seal() not implemented in crypto_mbedtls.c
*
* Status:
* - code written to be compatible with mbedtls 2.x and mbedtls 3.x
* (currently requires mbedtls >= 2.27.0 for mbedtls_mpi_random())
* (currently requires mbedtls >= 2.18.0 for mbedtls_ssl_tls_prf())
* - builds with tests/build/build-wpa_supplicant-mbedtls.config
* - passes all tests/ crypto module tests (incomplete coverage)
* ($ cd tests; make clean; make -j 4 run-tests CONFIG_TLS=mbedtls)
* - passes almost all tests/hwsim tests
* (hwsim tests skipped for missing features)
*
* RFE:
* - EAP-FAST, EAP-TEAP session ticket support not implemented in tls_mbedtls.c
* - client/server session resumption, and/or save client session ticket
*/
#include "includes.h"
#include "utils/common.h"
#ifndef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_NONE
#include <mbedtls/version.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/error.h>
#include <mbedtls/oid.h>
#include <mbedtls/pem.h>
#include <mbedtls/platform.h> /* mbedtls_calloc() mbedtls_free() */
#include <mbedtls/platform_util.h> /* mbedtls_platform_zeroize() */
#include <mbedtls/ssl.h>
#include <mbedtls/ssl_ticket.h>
#include <mbedtls/x509.h>
#include <mbedtls/x509_crt.h>
#ifdef MBEDTLS_DEBUG_C
#define DEBUG_THRESHOLD 4
#include <mbedtls/debug.h>
#ifdef __ZEPHYR__
#define PRINTF printk
#endif
#define tls_mbedtls_d(...) PRINTF("tls_mbedtls", ##__VA_ARGS__)
#else
#define tls_mbedtls_d(...)
#endif /* MBEDTLS_DEBUG_C */
#if MBEDTLS_VERSION_NUMBER >= 0x02040000 /* mbedtls 2.4.0 */
#include <mbedtls/net_sockets.h>
#else
#include <mbedtls/net.h>
#endif
#if MBEDTLS_VERSION_NUMBER < 0x03020000 /* mbedtls 3.2.0 */
#define mbedtls_ssl_get_ciphersuite_id_from_ssl(ssl) \
((ssl)->MBEDTLS_PRIVATE(session) ? (ssl)->MBEDTLS_PRIVATE(session)->MBEDTLS_PRIVATE(ciphersuite) : 0)
#define mbedtls_ssl_ciphersuite_get_name(info) (info)->MBEDTLS_PRIVATE(name)
#endif
#include "crypto.h" /* sha256_vector() */
#include "tls.h"
#ifndef SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH 32
#endif
#ifndef MBEDTLS_EXPKEY_FIXED_SECRET_LEN
#define MBEDTLS_EXPKEY_FIXED_SECRET_LEN 48
#endif
#ifndef MBEDTLS_EXPKEY_RAND_LEN
#define MBEDTLS_EXPKEY_RAND_LEN 32
#endif
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
static mbedtls_ssl_export_keys_t tls_connection_export_keys_cb;
#elif MBEDTLS_VERSION_NUMBER >= 0x02120000 /* mbedtls 2.18.0 */
static mbedtls_ssl_export_keys_ext_t tls_connection_export_keys_cb;
#else /*(not implemented; return error)*/
#define mbedtls_ssl_tls_prf(a, b, c, d, e, f, g, h) (-1)
typedef mbedtls_tls_prf_types int;
#endif
/* hostapd/wpa_supplicant provides forced_memzero(),
* but prefer mbedtls_platform_zeroize() */
#define forced_memzero(ptr, sz) mbedtls_platform_zeroize(ptr, sz)
/* much of fine-grained certificate subject matching (300+ LoC) could probably
* be replaced by sending a DN hint with ServerHello Certificate Request, and
* then checking the client cert against the DN hint in certificate verify_cb.
* Comment out to disable feature and remove ~3k from binary .text segment */
#define TLS_MBEDTLS_CERT_VERIFY_EXTMATCH
#define TLS_MBEDTLS_CERT_DISABLE_KEY_USAGE_CHECK
#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) || defined(EAP_TEAP) || \
defined(EAP_SERVER_TEAP)
#ifdef MBEDTLS_SSL_SESSION_TICKETS
#ifdef MBEDTLS_SSL_TICKET_C
#define TLS_MBEDTLS_SESSION_TICKETS
#if defined(EAP_TEAP) || defined(EAP_SERVER_TEAP)
#define TLS_MBEDTLS_EAP_TEAP
#endif
#if !defined(CONFIG_FIPS) /* EAP-FAST keys cannot be exported in FIPS mode */
#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
#define TLS_MBEDTLS_EAP_FAST
#endif
#endif
#endif
#endif
#endif
struct tls_conf
{
mbedtls_ssl_config conf;
unsigned int verify_peer : 1;
unsigned int verify_depth0_only : 1;
unsigned int check_crl : 2; /*(needs :2 bits for 0, 1, 2)*/
unsigned int check_crl_strict : 1; /*(needs :1 bit for 0, 1)*/
unsigned int ca_cert_probe : 1;
unsigned int has_ca_cert : 1;
unsigned int has_client_cert : 1;
unsigned int has_private_key : 1;
unsigned int suiteb128 : 1;
unsigned int suiteb192 : 1;
mbedtls_x509_crl *crl;
mbedtls_x509_crt ca_cert;
mbedtls_x509_crt client_cert;
mbedtls_pk_context private_key;
uint32_t refcnt;
unsigned int flags;
#ifdef TLS_MBEDTLS_CERT_VERIFY_EXTMATCH
char *subject_match;
char *altsubject_match;
char *suffix_match;
char *domain_match;
char *check_cert_subject;
#endif
u8 ca_cert_hash[SHA256_DIGEST_LENGTH];
int *ciphersuites; /* list of ciphersuite ids for mbedtls_ssl_config */
#if MBEDTLS_VERSION_NUMBER < 0x03010000 /* mbedtls 3.1.0 */
mbedtls_ecp_group_id *curves;
#else
uint16_t *curves; /* list of curve ids for mbedtls_ssl_config */
#endif
};
struct tls_global
{
struct tls_conf *tls_conf;
char *ocsp_stapling_response;
mbedtls_ctr_drbg_context *ctr_drbg; /*(see crypto_mbedtls.c)*/
#ifdef MBEDTLS_SSL_SESSION_TICKETS
mbedtls_ssl_ticket_context ticket_ctx;
#endif
char *ca_cert_file;
struct os_reltime crl_reload_previous;
unsigned int crl_reload_interval;
uint32_t refcnt;
struct tls_config init_conf;
};
static struct tls_global tls_ctx_global;
struct tls_connection
{
struct tls_conf *tls_conf;
struct wpabuf *push_buf;
struct wpabuf *pull_buf;
size_t pull_buf_offset;
unsigned int established : 1;
unsigned int resumed : 1;
unsigned int verify_peer : 1;
unsigned int is_server : 1;
mbedtls_ssl_context ssl;
mbedtls_tls_prf_types tls_prf_type;
size_t expkey_keyblock_size;
size_t expkey_secret_len;
#if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.0.0 */
unsigned char expkey_secret[MBEDTLS_EXPKEY_FIXED_SECRET_LEN];
#else
unsigned char expkey_secret[48];
#endif
unsigned char expkey_randbytes[MBEDTLS_EXPKEY_RAND_LEN * 2];
int read_alerts, write_alerts, failed;
#ifdef TLS_MBEDTLS_SESSION_TICKETS
tls_session_ticket_cb session_ticket_cb;
void *session_ticket_cb_ctx;
unsigned char *clienthello_session_ticket;
size_t clienthello_session_ticket_len;
#endif
char *peer_subject; /* peer subject info for authenticated peer */
struct wpabuf *success_data;
};
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
#ifndef __GNUC_PREREQ
#define __GNUC_PREREQ(maj, min) 0
#endif
#ifndef __attribute_cold__
#if __has_attribute(cold) || __GNUC_PREREQ(4, 3)
#define __attribute_cold__ __attribute__((__cold__))
#else
#define __attribute_cold__
#endif
#endif
#ifndef __attribute_noinline__
#if __has_attribute(noinline) || __GNUC_PREREQ(3, 1)
#define __attribute_noinline__ __attribute__((__noinline__))
#else
#define __attribute_noinline__
#endif
#endif
__attribute_cold__ __attribute_noinline__ static void emsg(int level, const char *const msg)
{
wpa_printf(level, "MTLS: %s", msg);
}
__attribute_cold__ __attribute_noinline__ static void emsgrc(int level, const char *const msg, int rc)
{
#ifdef MBEDTLS_ERROR_C
/* error logging convenience function that decodes mbedtls result codes */
char buf[256];
mbedtls_strerror(rc, buf, sizeof(buf));
wpa_printf(level, "MTLS: %s: %s (-0x%04x)", msg, buf, -rc);
#else
wpa_printf(level, "MTLS: %s: (-0x%04x)", msg, -rc);
#endif
}
#define elog(rc, msg) emsgrc(MSG_ERROR, (msg), (rc))
#define ilog(rc, msg) emsgrc(MSG_INFO, (msg), (rc))
#ifdef MBEDTLS_DEBUG_C
static void (*g_f_dbg)(void *, int, const char *, int, const char *);
#ifdef __ICCARM__
#define PATH_SEPARATOR '\\'
#else
#define PATH_SEPARATOR '/'
#endif /* __ICCARM__ */
/*
* This function will return basename of filename i.e.
* it will return a pointer to basename in the filename after ignoring '/'.
*
* e.g. for filename "abc/xyx/some_name.c", this function will return
* pointer to basename i.e. "some_name.c"
*/
static char *get_basename(char *file_name)
{
char *p = strrchr(file_name, PATH_SEPARATOR);
return p ? ++p : file_name;
}
static void tls_mbedtls_f_dbg(void *ctx, int level, const char *file, int line, const char *str)
{
(void)PRINTF("%s:%04d: |%d| %s\r\n", get_basename((char *)file), line, level, str);
}
void tls_mbedtls_set_debug_cb(mbedtls_ssl_config *conf,
int threshold,
void (*f_dbg)(void *, int, const char *, int, const char *))
{
if (!conf)
{
elog(-1, "Invalid SSL configuration context");
return;
}
if (!f_dbg && !g_f_dbg)
{
/**
* If 'NULL' dbg function and 'g_f_dbg' has not been set yet,
* then set 'g_f_dbg' pointing to global dbg function,
* and set it in conf.
*/
g_f_dbg = tls_mbedtls_f_dbg;
mbedtls_ssl_conf_dbg(conf, g_f_dbg, NULL);
}
else if (!f_dbg && g_f_dbg)
{
/**
* If 'NULL' dbg function and already set 'g_f_dbg',
* we will not override 'g_f_dbg',
* but set it in conf.
*/
mbedtls_ssl_conf_dbg(conf, g_f_dbg, NULL);
}
else if (f_dbg)
{
/**
* If valid dbg function then set it in conf.
*/
mbedtls_ssl_conf_dbg(conf, f_dbg, NULL);
}
mbedtls_debug_set_threshold(threshold);
}
#endif /* MBEDTLS_DEBUG_C */
struct tls_conf *tls_conf_init(void *tls_ctx)
{
struct tls_conf *tls_conf = os_zalloc(sizeof(*tls_conf));
if (tls_conf == NULL)
return NULL;
tls_conf->refcnt = 1;
mbedtls_ssl_config_init(&tls_conf->conf);
mbedtls_ssl_conf_rng(&tls_conf->conf, mbedtls_ctr_drbg_random, tls_ctx_global.ctr_drbg);
mbedtls_x509_crt_init(&tls_conf->ca_cert);
mbedtls_x509_crt_init(&tls_conf->client_cert);
mbedtls_pk_init(&tls_conf->private_key);
#ifdef MBEDTLS_DEBUG_C
tls_mbedtls_set_debug_cb(&tls_conf->conf, DEBUG_THRESHOLD, NULL);
#endif
return tls_conf;
}
struct tls_conf *tls_conf_deinit(struct tls_conf *tls_conf)
{
if (tls_conf == NULL || --tls_conf->refcnt != 0)
return tls_conf;
mbedtls_x509_crt_free(&tls_conf->ca_cert);
mbedtls_x509_crt_free(&tls_conf->client_cert);
if (tls_conf->crl)
{
mbedtls_x509_crl_free(tls_conf->crl);
os_free(tls_conf->crl);
}
mbedtls_pk_free(&tls_conf->private_key);
mbedtls_ssl_config_free(&tls_conf->conf);
os_free(tls_conf->curves);
os_free(tls_conf->ciphersuites);
#ifdef TLS_MBEDTLS_CERT_VERIFY_EXTMATCH
os_free(tls_conf->subject_match);
os_free(tls_conf->altsubject_match);
os_free(tls_conf->suffix_match);
os_free(tls_conf->domain_match);
os_free(tls_conf->check_cert_subject);
#endif
os_free(tls_conf);
return NULL;
}
mbedtls_ctr_drbg_context *crypto_mbedtls_ctr_drbg(void); /*(not in header)*/
__attribute_cold__ void *tls_init(const struct tls_config *conf)
{
/* RFE: review struct tls_config *conf (different from tls_conf) */
if (++tls_ctx_global.refcnt > 1)
return &tls_ctx_global;
tls_ctx_global.ctr_drbg = crypto_mbedtls_ctr_drbg();
#ifdef MBEDTLS_SSL_SESSION_TICKETS
mbedtls_ssl_ticket_init(&tls_ctx_global.ticket_ctx);
mbedtls_ssl_ticket_setup(&tls_ctx_global.ticket_ctx, mbedtls_ctr_drbg_random, tls_ctx_global.ctr_drbg,
MBEDTLS_CIPHER_AES_256_GCM, 43200); /* ticket timeout: 12 hours */
#endif
/* copy struct for future use */
tls_ctx_global.init_conf = *conf;
if (conf->openssl_ciphers)
tls_ctx_global.init_conf.openssl_ciphers = os_strdup(conf->openssl_ciphers);
tls_ctx_global.crl_reload_interval = conf->crl_reload_interval;
os_get_reltime(&tls_ctx_global.crl_reload_previous);
return &tls_ctx_global;
}
__attribute_cold__ void tls_deinit(void *tls_ctx)
{
if (tls_ctx == NULL)
return;
tls_ctx_global.tls_conf = tls_conf_deinit(tls_ctx_global.tls_conf);
if (--tls_ctx_global.refcnt != 0)
return;
os_free(tls_ctx_global.ca_cert_file);
os_free(tls_ctx_global.ocsp_stapling_response);
char *openssl_ciphers; /*(allocated in tls_init())*/
*(const char **)&openssl_ciphers = tls_ctx_global.init_conf.openssl_ciphers;
os_free(openssl_ciphers);
#ifdef MBEDTLS_SSL_SESSION_TICKETS
mbedtls_ssl_ticket_free(&tls_ctx_global.ticket_ctx);
#endif
os_memset(&tls_ctx_global, 0, sizeof(tls_ctx_global));
}
int tls_get_errors(void *tls_ctx)
{
return 0;
}
static void tls_connection_deinit_expkey(struct tls_connection *conn)
{
conn->tls_prf_type = 0; /* MBEDTLS_SSL_TLS_PRF_NONE; */
conn->expkey_keyblock_size = 0;
conn->expkey_secret_len = 0;
forced_memzero(conn->expkey_secret, sizeof(conn->expkey_secret));
forced_memzero(conn->expkey_randbytes, sizeof(conn->expkey_randbytes));
}
#ifdef TLS_MBEDTLS_SESSION_TICKETS
void tls_connection_deinit_clienthello_session_ticket(struct tls_connection *conn)
{
if (conn->clienthello_session_ticket)
{
mbedtls_platform_zeroize(conn->clienthello_session_ticket, conn->clienthello_session_ticket_len);
mbedtls_free(conn->clienthello_session_ticket);
conn->clienthello_session_ticket = NULL;
conn->clienthello_session_ticket_len = 0;
}
}
#endif
void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
{
if (conn == NULL)
return;
if (conn->tls_prf_type)
tls_connection_deinit_expkey(conn);
#ifdef TLS_MBEDTLS_SESSION_TICKETS
if (conn->clienthello_session_ticket)
tls_connection_deinit_clienthello_session_ticket(conn);
#endif
os_free(conn->peer_subject);
wpabuf_free(conn->success_data);
wpabuf_free(conn->push_buf);
wpabuf_free(conn->pull_buf);
mbedtls_ssl_free(&conn->ssl);
tls_conf_deinit(conn->tls_conf);
os_free(conn);
}
static void tls_mbedtls_refresh_crl(void);
static int tls_mbedtls_ssl_setup(struct tls_connection *conn);
struct tls_connection *tls_connection_init(void *tls_ctx)
{
struct tls_connection *conn = os_zalloc(sizeof(*conn));
if (conn == NULL)
return NULL;
mbedtls_ssl_init(&conn->ssl);
conn->tls_conf = tls_ctx_global.tls_conf; /*(inherit global conf, if set)*/
if (conn->tls_conf)
{
++conn->tls_conf->refcnt;
/* check for CRL refresh if inheriting from global config */
tls_mbedtls_refresh_crl();
conn->verify_peer = conn->tls_conf->verify_peer;
if (tls_mbedtls_ssl_setup(conn) != 0)
{
tls_connection_deinit(&tls_ctx_global, conn);
return NULL;
}
}
return conn;
}
int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
{
return conn ? conn->established : 0;
}
__attribute_noinline__ char *tls_mbedtls_peer_serial_num(const mbedtls_x509_crt *crt, char *serial_num, size_t len)
{
/* mbedtls_x509_serial_gets() inefficiently formats to hex separated by
* colons, so generate the hex serial number here. The func
* wpa_snprintf_hex_uppercase() is similarly inefficient. */
size_t i = 0; /* skip leading 0's per Distinguished Encoding Rules (DER) */
while (i < crt->serial.len && crt->serial.p[i] == 0)
++i;
if (i == crt->serial.len)
--i;
const unsigned char *s = crt->serial.p + i;
const size_t e = (crt->serial.len - i) * 2;
if (e >= len)
return NULL;
for (i = 0; i < e; i += 2, ++s)
{
serial_num[i + 0] = "0123456789ABCDEF"[(*s >> 4)];
serial_num[i + 1] = "0123456789ABCDEF"[(*s & 0xF)];
}
serial_num[e] = '\0';
return serial_num;
}
char *tls_connection_peer_serial_num(void *tls_ctx, struct tls_connection *conn)
{
const mbedtls_x509_crt *crt = mbedtls_ssl_get_peer_cert(&conn->ssl);
if (crt == NULL)
return NULL;
size_t len = crt->serial.len * 2 + 1;
char *serial_num = os_malloc(len);
if (!serial_num)
return NULL;
return tls_mbedtls_peer_serial_num(crt, serial_num, len);
}
static void tls_pull_buf_reset(struct tls_connection *conn);
int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
{
/* Note: this function called from eap_peer_tls_reauth_init()
* for session resumption, not for connection shutdown */
if (conn == NULL)
return -1;
tls_pull_buf_reset(conn);
wpabuf_free(conn->push_buf);
conn->push_buf = NULL;
conn->established = 0;
conn->resumed = 0;
if (conn->tls_prf_type)
tls_connection_deinit_expkey(conn);
/* RFE: prepare for session resumption? (see doc in crypto/tls.h) */
return mbedtls_ssl_session_reset(&conn->ssl);
}
static int tls_wpabuf_resize_put_data(struct wpabuf **buf, const unsigned char *data, size_t dlen)
{
if (wpabuf_resize(buf, dlen) < 0)
return 0;
wpabuf_put_data(*buf, data, dlen);
return 1;
}
static int tls_pull_buf_append(struct tls_connection *conn, const struct wpabuf *in_data)
{
/*(interface does not lend itself to move semantics)*/
return tls_wpabuf_resize_put_data(&conn->pull_buf, wpabuf_head(in_data), wpabuf_len(in_data));
}
static void tls_pull_buf_reset(struct tls_connection *conn)
{
/*(future: might consider reusing conn->pull_buf)*/
wpabuf_free(conn->pull_buf);
conn->pull_buf = NULL;
conn->pull_buf_offset = 0;
}
__attribute_cold__ static void tls_pull_buf_discard(struct tls_connection *conn, const char *func)
{
size_t discard = wpabuf_len(conn->pull_buf) - conn->pull_buf_offset;
if (discard)
wpa_printf(MSG_DEBUG, "%s - %zu bytes remaining in pull_buf; discarding", func, discard);
tls_pull_buf_reset(conn);
}
static int tls_pull_func(void *ptr, unsigned char *buf, size_t len)
{
struct tls_connection *conn = (struct tls_connection *)ptr;
if (conn->pull_buf == NULL)
return MBEDTLS_ERR_SSL_WANT_READ;
const size_t dlen = wpabuf_len(conn->pull_buf) - conn->pull_buf_offset;
if (dlen == 0)
return MBEDTLS_ERR_SSL_WANT_READ;
if (len > dlen)
len = dlen;
os_memcpy(buf, (u8 *)wpabuf_head(conn->pull_buf) + conn->pull_buf_offset, len);
if (len == dlen)
{
tls_pull_buf_reset(conn);
/*wpa_printf(MSG_DEBUG, "%s - emptied pull_buf", __func__);*/
}
else
{
conn->pull_buf_offset += len;
/*wpa_printf(MSG_DEBUG, "%s - %zu bytes remaining in pull_buf",
__func__, dlen - len);*/
}
return (int)len;
}
static int tls_push_func(void *ptr, const unsigned char *buf, size_t len)
{
struct tls_connection *conn = (struct tls_connection *)ptr;
return tls_wpabuf_resize_put_data(&conn->push_buf, buf, len) ? (int)len : MBEDTLS_ERR_SSL_ALLOC_FAILED;
}
static int tls_mbedtls_verify_cb(void *arg, mbedtls_x509_crt *crt, int depth, uint32_t *flags);
static int tls_mbedtls_ssl_setup(struct tls_connection *conn)
{
int ret = mbedtls_ssl_setup(&conn->ssl, &conn->tls_conf->conf);
if (ret != 0)
{
elog(ret, "mbedtls_ssl_setup");
return -1;
}
mbedtls_ssl_set_bio(&conn->ssl, conn, tls_push_func, tls_pull_func, NULL);
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
mbedtls_ssl_set_export_keys_cb(&conn->ssl, tls_connection_export_keys_cb, conn);
#elif MBEDTLS_VERSION_NUMBER >= 0x02120000 /* mbedtls 2.18.0 */
mbedtls_ssl_conf_export_keys_ext_cb(&conn->tls_conf->conf, tls_connection_export_keys_cb, conn);
#endif
if (conn->verify_peer)
mbedtls_ssl_set_verify(&conn->ssl, tls_mbedtls_verify_cb, conn);
return 0;
}
static int tls_mbedtls_data_is_pem(const u8 *data)
{
return (NULL != os_strstr((char *)data, "-----"));
}
static void tls_mbedtls_set_allowed_tls_vers(struct tls_conf *tls_conf, mbedtls_ssl_config *conf)
{
#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
tls_conf->flags |= TLS_CONN_DISABLE_TLSv1_3;
#endif
/* unconditionally require TLSv1.2+ for TLS_CONN_SUITEB */
if (tls_conf->flags & TLS_CONN_SUITEB)
{
tls_conf->flags |= TLS_CONN_DISABLE_TLSv1_0;
tls_conf->flags |= TLS_CONN_DISABLE_TLSv1_1;
}
const unsigned int flags = tls_conf->flags;
/* attempt to map flags to min and max TLS protocol version */
int min = (flags & TLS_CONN_DISABLE_TLSv1_0) ?
(flags & TLS_CONN_DISABLE_TLSv1_1) ?
(flags & TLS_CONN_DISABLE_TLSv1_2) ? (flags & TLS_CONN_DISABLE_TLSv1_3) ? 4 : 3 : 2 :
1 :
0;
int max = (flags & TLS_CONN_DISABLE_TLSv1_3) ?
(flags & TLS_CONN_DISABLE_TLSv1_2) ?
(flags & TLS_CONN_DISABLE_TLSv1_1) ? (flags & TLS_CONN_DISABLE_TLSv1_0) ? -1 : 0 : 1 :
2 :
3;
if ((flags & TLS_CONN_ENABLE_TLSv1_2) && min > 2)
min = 2;
if ((flags & TLS_CONN_ENABLE_TLSv1_1) && min > 1)
min = 1;
if ((flags & TLS_CONN_ENABLE_TLSv1_0) && min > 0)
min = 0;
if (max < min)
{
emsg(MSG_ERROR, "invalid tls_disable_tlsv* params; ignoring");
return;
}
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
/* mbed TLS 3.0.0 removes support for protocols < TLSv1.2 */
if (min < 2 || max < 2)
{
emsg(MSG_ERROR, "invalid tls_disable_tlsv* params; ignoring");
if (min < 2)
min = 2;
if (max < 2)
max = 2;
}
#endif
#if MBEDTLS_VERSION_NUMBER >= 0x03020000 /* mbedtls 3.2.0 */
/* MBEDTLS_SSL_VERSION_TLS1_2 = 0x0303 */ /*!< (D)TLS 1.2 */
/* MBEDTLS_SSL_VERSION_TLS1_3 = 0x0304 */ /*!< (D)TLS 1.3 */
min = (min == 2) ? MBEDTLS_SSL_VERSION_TLS1_2 : MBEDTLS_SSL_VERSION_TLS1_3;
max = (max == 2) ? MBEDTLS_SSL_VERSION_TLS1_2 : MBEDTLS_SSL_VERSION_TLS1_3;
mbedtls_ssl_conf_min_tls_version(conf, min);
mbedtls_ssl_conf_max_tls_version(conf, max);
#else
#ifndef MBEDTLS_SSL_MINOR_VERSION_4
if (min == 3)
min = 2;
if (max == 3)
max = 2;
#endif
/* MBEDTLS_SSL_MINOR_VERSION_0 0 */ /*!< SSL v3.0 */
/* MBEDTLS_SSL_MINOR_VERSION_1 1 */ /*!< TLS v1.0 */
/* MBEDTLS_SSL_MINOR_VERSION_2 2 */ /*!< TLS v1.1 */
/* MBEDTLS_SSL_MINOR_VERSION_3 3 */ /*!< TLS v1.2 */
/* MBEDTLS_SSL_MINOR_VERSION_4 4 */ /*!< TLS v1.3 */
mbedtls_ssl_conf_min_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3, min + 1);
mbedtls_ssl_conf_max_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3, max + 1);
#endif
}
__attribute_noinline__ static int tls_mbedtls_readfile(const char *path, u8 **buf, size_t *n);
#ifdef MBEDTLS_DHM_C
static int tls_mbedtls_set_dhparams(struct tls_conf *tls_conf, const struct tls_connection_params *params)
{
size_t len;
const u8 *data;
// if (tls_mbedtls_readfile(dh_file, &data, &len))
// return 0;
data = params->dh_blob;
len = params->dh_blob_len;
/* parse only if DH parameters if in PEM format */
if (tls_mbedtls_data_is_pem(data) && NULL == os_strstr((char *)data, "-----BEGIN DH PARAMETERS-----"))
{
if (os_strstr((char *)data, "-----BEGIN DSA PARAMETERS-----"))
wpa_printf(MSG_WARNING, "DSA parameters not handled (%s)", "dh_file");
else
wpa_printf(MSG_WARNING, "unexpected DH param content (%s)", "dh_file");
//forced_memzero(data, len);
// os_free(data);
return 0;
}
/* mbedtls_dhm_parse_dhm() expects "-----BEGIN DH PARAMETERS-----" if PEM */
mbedtls_dhm_context dhm;
mbedtls_dhm_init(&dhm);
int rc = mbedtls_dhm_parse_dhm(&dhm, data, len);
if (0 == rc)
rc = mbedtls_ssl_conf_dh_param_ctx(&tls_conf->conf, &dhm);
if (0 != rc)
elog(rc, "dh_file");
mbedtls_dhm_free(&dhm);
// forced_memzero(data, len);
// os_free(data);
return (0 == rc);
}
#endif
/* reference: lighttpd src/mod_mbedtls.c:mod_mbedtls_ssl_append_curve()
* (same author: gstrauss@gluelogic.com; same license: BSD-3-Clause) */
#if MBEDTLS_VERSION_NUMBER < 0x03010000 /* mbedtls 3.1.0 */
static int tls_mbedtls_append_curve(mbedtls_ecp_group_id *ids, int nids, int idsz, const mbedtls_ecp_group_id id)
{
if (1 >= idsz - (nids + 1))
{
emsg(MSG_ERROR, "error: too many curves during list expand");
return -1;
}
ids[++nids] = id;
return nids;
}
static int tls_mbedtls_set_curves(struct tls_conf *tls_conf, const char *curvelist)
{
mbedtls_ecp_group_id ids[512];
int nids = -1;
const int idsz = (int)(sizeof(ids) / sizeof(*ids) - 1);
const mbedtls_ecp_curve_info *const curve_info = mbedtls_ecp_curve_list();
for (const char *e = curvelist - 1; e;)
{
const char *const n = e + 1;
e = os_strchr(n, ':');
size_t len = e ? (size_t)(e - n) : os_strlen(n);
mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
switch (len)
{
case 5:
if (0 == os_memcmp("P-521", n, 5))
grp_id = MBEDTLS_ECP_DP_SECP521R1;
else if (0 == os_memcmp("P-384", n, 5))
grp_id = MBEDTLS_ECP_DP_SECP384R1;
else if (0 == os_memcmp("P-256", n, 5))
grp_id = MBEDTLS_ECP_DP_SECP256R1;
break;
case 6:
if (0 == os_memcmp("BP-521", n, 6))
grp_id = MBEDTLS_ECP_DP_BP512R1;
else if (0 == os_memcmp("BP-384", n, 6))
grp_id = MBEDTLS_ECP_DP_BP384R1;
else if (0 == os_memcmp("BP-256", n, 6))
grp_id = MBEDTLS_ECP_DP_BP256R1;
break;
default:
break;
}
if (grp_id != MBEDTLS_ECP_DP_NONE)
{
nids = tls_mbedtls_append_curve(ids, nids, idsz, grp_id);
if (-1 == nids)
return 0;
continue;
}
/* similar to mbedtls_ecp_curve_info_from_name() */
const mbedtls_ecp_curve_info *info;
for (info = curve_info; info->grp_id != MBEDTLS_ECP_DP_NONE; ++info)
{
if (0 == os_strncmp(info->name, n, len) && info->name[len] == '\0')
break;
}
if (info->grp_id == MBEDTLS_ECP_DP_NONE)
{
wpa_printf(MSG_ERROR, "MTLS: unrecognized curve: %.*s", (int)len, n);
return 0;
}
nids = tls_mbedtls_append_curve(ids, nids, idsz, info->grp_id);
if (-1 == nids)
return 0;
}
/* mod_openssl configures "prime256v1" if curve list not specified,
* but mbedtls provides a list of supported curves if not explicitly set */
if (-1 == nids)
return 1; /* empty list; no-op */
ids[++nids] = MBEDTLS_ECP_DP_NONE; /* terminate list */
++nids;
/* curves list must be persistent for lifetime of mbedtls_ssl_config */
tls_conf->curves = os_malloc(nids * sizeof(mbedtls_ecp_group_id));
if (tls_conf->curves == NULL)
return 0;
os_memcpy(tls_conf->curves, ids, nids * sizeof(mbedtls_ecp_group_id));
mbedtls_ssl_conf_curves(&tls_conf->conf, tls_conf->curves);
return 1;
}
#else
static int tls_mbedtls_append_curve(uint16_t *ids, int nids, int idsz, const uint16_t id)
{
if (1 >= idsz - (nids + 1))
{
emsg(MSG_ERROR, "error: too many curves during list expand");
return -1;
}
ids[++nids] = id;
return nids;
}
static int tls_mbedtls_set_curves(struct tls_conf *tls_conf, const char *curvelist)
{
/* TLS Supported Groups (renamed from "EC Named Curve Registry")
* https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
*/
uint16_t ids[512];
int nids = -1;
const int idsz = (int)(sizeof(ids) / sizeof(*ids) - 1);
const mbedtls_ecp_curve_info *const curve_info = mbedtls_ecp_curve_list();
for (const char *e = curvelist - 1; e;)
{
const char *const n = e + 1;
e = os_strchr(n, ':');
size_t len = e ? (size_t)(e - n) : os_strlen(n);
uint16_t tls_id = 0;
switch (len)
{
case 5:
if (0 == os_memcmp("P-521", n, 5))
tls_id = 25; /* mbedtls_ecp_group_id MBEDTLS_ECP_DP_SECP521R1 */
else if (0 == os_memcmp("P-384", n, 5))
tls_id = 24; /* mbedtls_ecp_group_id MBEDTLS_ECP_DP_SECP384R1 */
else if (0 == os_memcmp("P-256", n, 5))
tls_id = 23; /* mbedtls_ecp_group_id MBEDTLS_ECP_DP_SECP256R1 */
break;
case 6:
if (0 == os_memcmp("BP-521", n, 6))
tls_id = 28; /* mbedtls_ecp_group_id MBEDTLS_ECP_DP_BP512R1 */
else if (0 == os_memcmp("BP-384", n, 6))
tls_id = 27; /* mbedtls_ecp_group_id MBEDTLS_ECP_DP_BP384R1 */
else if (0 == os_memcmp("BP-256", n, 6))
tls_id = 26; /* mbedtls_ecp_group_id MBEDTLS_ECP_DP_BP256R1 */
break;
default:
break;
}
if (tls_id != 0)
{
nids = tls_mbedtls_append_curve(ids, nids, idsz, tls_id);
if (-1 == nids)
return 0;
continue;
}
/* similar to mbedtls_ecp_curve_info_from_name() */
const mbedtls_ecp_curve_info *info;
for (info = curve_info; info->tls_id != 0; ++info)
{
if (0 == os_strncmp(info->name, n, len) && info->name[len] == '\0')
break;
}
if (info->tls_id == 0)
{
wpa_printf(MSG_ERROR, "MTLS: unrecognized curve: %.*s", (int)len, n);
return 0;
}
nids = tls_mbedtls_append_curve(ids, nids, idsz, info->tls_id);
if (-1 == nids)
return 0;
}
/* mod_openssl configures "prime256v1" if curve list not specified,
* but mbedtls provides a list of supported curves if not explicitly set */
if (-1 == nids)
return 1; /* empty list; no-op */
ids[++nids] = 0; /* terminate list */
++nids;
/* curves list must be persistent for lifetime of mbedtls_ssl_config */
tls_conf->curves = os_malloc(nids * sizeof(uint16_t));
if (tls_conf->curves == NULL)
return 0;
os_memcpy(tls_conf->curves, ids, nids * sizeof(uint16_t));
mbedtls_ssl_conf_groups(&tls_conf->conf, tls_conf->curves);
return 1;
}
#endif /* MBEDTLS_VERSION_NUMBER >= 0x03010000 */ /* mbedtls 3.1.0 */
/* data copied from lighttpd src/mod_mbedtls.c (BSD-3-Clause) */
static const int suite_AES_256_ephemeral[] = {
/* All AES-256 ephemeral suites */
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8};
/* data copied from lighttpd src/mod_mbedtls.c (BSD-3-Clause) */
static const int suite_AES_128_ephemeral[] = {
/* All AES-128 ephemeral suites */
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8};