forked from openssl/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht1_lib.c
4199 lines (3746 loc) · 135 KB
/
t1_lib.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
/*
* Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/objects.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/core_names.h>
#include <openssl/ocsp.h>
#include <openssl/conf.h>
#include <openssl/x509v3.h>
#include <openssl/dh.h>
#include <openssl/bn.h>
#include <openssl/provider.h>
#include <openssl/param_build.h>
#include "internal/nelem.h"
#include "internal/sizes.h"
#include "internal/tlsgroups.h"
#include "ssl_local.h"
#include "quic/quic_local.h"
#include <openssl/ct.h>
static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pkey);
static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, const SIGALG_LOOKUP *lu);
SSL3_ENC_METHOD const TLSv1_enc_data = {
tls1_setup_key_block,
tls1_generate_master_secret,
tls1_change_cipher_state,
tls1_final_finish_mac,
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
tls1_alert_code,
tls1_export_keying_material,
0,
ssl3_set_handshake_header,
tls_close_construct_packet,
ssl3_handshake_write
};
SSL3_ENC_METHOD const TLSv1_1_enc_data = {
tls1_setup_key_block,
tls1_generate_master_secret,
tls1_change_cipher_state,
tls1_final_finish_mac,
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
tls1_alert_code,
tls1_export_keying_material,
0,
ssl3_set_handshake_header,
tls_close_construct_packet,
ssl3_handshake_write
};
SSL3_ENC_METHOD const TLSv1_2_enc_data = {
tls1_setup_key_block,
tls1_generate_master_secret,
tls1_change_cipher_state,
tls1_final_finish_mac,
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
tls1_alert_code,
tls1_export_keying_material,
SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
| SSL_ENC_FLAG_TLS1_2_CIPHERS,
ssl3_set_handshake_header,
tls_close_construct_packet,
ssl3_handshake_write
};
SSL3_ENC_METHOD const TLSv1_3_enc_data = {
tls13_setup_key_block,
tls13_generate_master_secret,
tls13_change_cipher_state,
tls13_final_finish_mac,
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
tls13_alert_code,
tls13_export_keying_material,
SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
ssl3_set_handshake_header,
tls_close_construct_packet,
ssl3_handshake_write
};
OSSL_TIME tls1_default_timeout(void)
{
/*
* 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
* http, the cache would over fill
*/
return ossl_seconds2time(60 * 60 * 2);
}
int tls1_new(SSL *s)
{
if (!ssl3_new(s))
return 0;
if (!s->method->ssl_clear(s))
return 0;
return 1;
}
void tls1_free(SSL *s)
{
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
if (sc == NULL)
return;
OPENSSL_free(sc->ext.session_ticket);
ssl3_free(s);
}
int tls1_clear(SSL *s)
{
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
if (sc == NULL)
return 0;
if (!ssl3_clear(s))
return 0;
if (s->method->version == TLS_ANY_VERSION)
sc->version = TLS_MAX_VERSION_INTERNAL;
else
sc->version = s->method->version;
return 1;
}
/* Legacy NID to group_id mapping. Only works for groups we know about */
static const struct {
int nid;
uint16_t group_id;
} nid_to_group[] = {
{NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1},
{NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1},
{NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2},
{NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1},
{NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2},
{NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1},
{NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1},
{NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1},
{NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1},
{NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1},
{NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1},
{NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1},
{NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1},
{NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1},
{NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1},
{NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1},
{NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2},
{NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1},
{NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1},
{NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1},
{NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1},
{NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1},
{NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1},
{NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1},
{NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1},
{NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1},
{NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1},
{NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1},
{EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519},
{EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448},
{NID_brainpoolP256r1tls13, OSSL_TLS_GROUP_ID_brainpoolP256r1_tls13},
{NID_brainpoolP384r1tls13, OSSL_TLS_GROUP_ID_brainpoolP384r1_tls13},
{NID_brainpoolP512r1tls13, OSSL_TLS_GROUP_ID_brainpoolP512r1_tls13},
{NID_id_tc26_gost_3410_2012_256_paramSetA, OSSL_TLS_GROUP_ID_gc256A},
{NID_id_tc26_gost_3410_2012_256_paramSetB, OSSL_TLS_GROUP_ID_gc256B},
{NID_id_tc26_gost_3410_2012_256_paramSetC, OSSL_TLS_GROUP_ID_gc256C},
{NID_id_tc26_gost_3410_2012_256_paramSetD, OSSL_TLS_GROUP_ID_gc256D},
{NID_id_tc26_gost_3410_2012_512_paramSetA, OSSL_TLS_GROUP_ID_gc512A},
{NID_id_tc26_gost_3410_2012_512_paramSetB, OSSL_TLS_GROUP_ID_gc512B},
{NID_id_tc26_gost_3410_2012_512_paramSetC, OSSL_TLS_GROUP_ID_gc512C},
{NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048},
{NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072},
{NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096},
{NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144},
{NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192}
};
static const unsigned char ecformats_default[] = {
TLSEXT_ECPOINTFORMAT_uncompressed,
TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
};
/* The default curves */
static const uint16_t supported_groups_default[] = {
OSSL_TLS_GROUP_ID_x25519, /* X25519 (29) */
OSSL_TLS_GROUP_ID_secp256r1, /* secp256r1 (23) */
OSSL_TLS_GROUP_ID_x448, /* X448 (30) */
OSSL_TLS_GROUP_ID_secp521r1, /* secp521r1 (25) */
OSSL_TLS_GROUP_ID_secp384r1, /* secp384r1 (24) */
OSSL_TLS_GROUP_ID_gc256A, /* GC256A (34) */
OSSL_TLS_GROUP_ID_gc256B, /* GC256B (35) */
OSSL_TLS_GROUP_ID_gc256C, /* GC256C (36) */
OSSL_TLS_GROUP_ID_gc256D, /* GC256D (37) */
OSSL_TLS_GROUP_ID_gc512A, /* GC512A (38) */
OSSL_TLS_GROUP_ID_gc512B, /* GC512B (39) */
OSSL_TLS_GROUP_ID_gc512C, /* GC512C (40) */
OSSL_TLS_GROUP_ID_ffdhe2048, /* ffdhe2048 (0x100) */
OSSL_TLS_GROUP_ID_ffdhe3072, /* ffdhe3072 (0x101) */
OSSL_TLS_GROUP_ID_ffdhe4096, /* ffdhe4096 (0x102) */
OSSL_TLS_GROUP_ID_ffdhe6144, /* ffdhe6144 (0x103) */
OSSL_TLS_GROUP_ID_ffdhe8192, /* ffdhe8192 (0x104) */
};
static const uint16_t suiteb_curves[] = {
OSSL_TLS_GROUP_ID_secp256r1,
OSSL_TLS_GROUP_ID_secp384r1,
};
struct provider_ctx_data_st {
SSL_CTX *ctx;
OSSL_PROVIDER *provider;
};
#define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10
static OSSL_CALLBACK add_provider_groups;
static int add_provider_groups(const OSSL_PARAM params[], void *data)
{
struct provider_ctx_data_st *pgd = data;
SSL_CTX *ctx = pgd->ctx;
OSSL_PROVIDER *provider = pgd->provider;
const OSSL_PARAM *p;
TLS_GROUP_INFO *ginf = NULL;
EVP_KEYMGMT *keymgmt;
unsigned int gid;
unsigned int is_kem = 0;
int ret = 0;
if (ctx->group_list_max_len == ctx->group_list_len) {
TLS_GROUP_INFO *tmp = NULL;
if (ctx->group_list_max_len == 0)
tmp = OPENSSL_malloc(sizeof(TLS_GROUP_INFO)
* TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);
else
tmp = OPENSSL_realloc(ctx->group_list,
(ctx->group_list_max_len
+ TLS_GROUP_LIST_MALLOC_BLOCK_SIZE)
* sizeof(TLS_GROUP_INFO));
if (tmp == NULL)
return 0;
ctx->group_list = tmp;
memset(tmp + ctx->group_list_max_len,
0,
sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);
ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE;
}
ginf = &ctx->group_list[ctx->group_list_len];
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME);
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
ginf->tlsname = OPENSSL_strdup(p->data);
if (ginf->tlsname == NULL)
goto err;
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL);
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
ginf->realname = OPENSSL_strdup(p->data);
if (ginf->realname == NULL)
goto err;
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID);
if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
ginf->group_id = (uint16_t)gid;
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG);
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
ginf->algorithm = OPENSSL_strdup(p->data);
if (ginf->algorithm == NULL)
goto err;
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS);
if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM);
if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
ginf->is_kem = 1 & is_kem;
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS);
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS);
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS);
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS);
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
/*
* Now check that the algorithm is actually usable for our property query
* string. Regardless of the result we still return success because we have
* successfully processed this group, even though we may decide not to use
* it.
*/
ret = 1;
ERR_set_mark();
keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq);
if (keymgmt != NULL) {
/*
* We have successfully fetched the algorithm - however if the provider
* doesn't match this one then we ignore it.
*
* Note: We're cheating a little here. Technically if the same algorithm
* is available from more than one provider then it is undefined which
* implementation you will get back. Theoretically this could be
* different every time...we assume here that you'll always get the
* same one back if you repeat the exact same fetch. Is this a reasonable
* assumption to make (in which case perhaps we should document this
* behaviour)?
*/
if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) {
/* We have a match - so we will use this group */
ctx->group_list_len++;
ginf = NULL;
}
EVP_KEYMGMT_free(keymgmt);
}
ERR_pop_to_mark();
err:
if (ginf != NULL) {
OPENSSL_free(ginf->tlsname);
OPENSSL_free(ginf->realname);
OPENSSL_free(ginf->algorithm);
ginf->algorithm = ginf->tlsname = ginf->realname = NULL;
}
return ret;
}
static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx)
{
struct provider_ctx_data_st pgd;
pgd.ctx = vctx;
pgd.provider = provider;
return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP",
add_provider_groups, &pgd);
}
int ssl_load_groups(SSL_CTX *ctx)
{
size_t i, j, num_deflt_grps = 0;
uint16_t tmp_supp_groups[OSSL_NELEM(supported_groups_default)];
if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx))
return 0;
for (i = 0; i < OSSL_NELEM(supported_groups_default); i++) {
for (j = 0; j < ctx->group_list_len; j++) {
if (ctx->group_list[j].group_id == supported_groups_default[i]) {
tmp_supp_groups[num_deflt_grps++] = ctx->group_list[j].group_id;
break;
}
}
}
if (num_deflt_grps == 0)
return 1;
ctx->ext.supported_groups_default
= OPENSSL_malloc(sizeof(uint16_t) * num_deflt_grps);
if (ctx->ext.supported_groups_default == NULL)
return 0;
memcpy(ctx->ext.supported_groups_default,
tmp_supp_groups,
num_deflt_grps * sizeof(tmp_supp_groups[0]));
ctx->ext.supported_groups_default_len = num_deflt_grps;
return 1;
}
#define TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE 10
static OSSL_CALLBACK add_provider_sigalgs;
static int add_provider_sigalgs(const OSSL_PARAM params[], void *data)
{
struct provider_ctx_data_st *pgd = data;
SSL_CTX *ctx = pgd->ctx;
OSSL_PROVIDER *provider = pgd->provider;
const OSSL_PARAM *p;
TLS_SIGALG_INFO *sinf = NULL;
EVP_KEYMGMT *keymgmt;
const char *keytype;
unsigned int code_point = 0;
int ret = 0;
if (ctx->sigalg_list_max_len == ctx->sigalg_list_len) {
TLS_SIGALG_INFO *tmp = NULL;
if (ctx->sigalg_list_max_len == 0)
tmp = OPENSSL_malloc(sizeof(TLS_SIGALG_INFO)
* TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE);
else
tmp = OPENSSL_realloc(ctx->sigalg_list,
(ctx->sigalg_list_max_len
+ TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE)
* sizeof(TLS_SIGALG_INFO));
if (tmp == NULL)
return 0;
ctx->sigalg_list = tmp;
memset(tmp + ctx->sigalg_list_max_len, 0,
sizeof(TLS_SIGALG_INFO) * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE);
ctx->sigalg_list_max_len += TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE;
}
sinf = &ctx->sigalg_list[ctx->sigalg_list_len];
/* First, mandatory parameters */
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_NAME);
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
OPENSSL_free(sinf->sigalg_name);
sinf->sigalg_name = OPENSSL_strdup(p->data);
if (sinf->sigalg_name == NULL)
goto err;
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME);
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
OPENSSL_free(sinf->name);
sinf->name = OPENSSL_strdup(p->data);
if (sinf->name == NULL)
goto err;
p = OSSL_PARAM_locate_const(params,
OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT);
if (p == NULL
|| !OSSL_PARAM_get_uint(p, &code_point)
|| code_point > UINT16_MAX) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
sinf->code_point = (uint16_t)code_point;
p = OSSL_PARAM_locate_const(params,
OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS);
if (p == NULL || !OSSL_PARAM_get_uint(p, &sinf->secbits)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
/* Now, optional parameters */
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_OID);
if (p == NULL) {
sinf->sigalg_oid = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->sigalg_oid);
sinf->sigalg_oid = OPENSSL_strdup(p->data);
if (sinf->sigalg_oid == NULL)
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME);
if (p == NULL) {
sinf->sig_name = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->sig_name);
sinf->sig_name = OPENSSL_strdup(p->data);
if (sinf->sig_name == NULL)
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_OID);
if (p == NULL) {
sinf->sig_oid = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->sig_oid);
sinf->sig_oid = OPENSSL_strdup(p->data);
if (sinf->sig_oid == NULL)
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME);
if (p == NULL) {
sinf->hash_name = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->hash_name);
sinf->hash_name = OPENSSL_strdup(p->data);
if (sinf->hash_name == NULL)
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_OID);
if (p == NULL) {
sinf->hash_oid = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->hash_oid);
sinf->hash_oid = OPENSSL_strdup(p->data);
if (sinf->hash_oid == NULL)
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE);
if (p == NULL) {
sinf->keytype = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->keytype);
sinf->keytype = OPENSSL_strdup(p->data);
if (sinf->keytype == NULL)
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID);
if (p == NULL) {
sinf->keytype_oid = NULL;
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) {
goto err;
} else {
OPENSSL_free(sinf->keytype_oid);
sinf->keytype_oid = OPENSSL_strdup(p->data);
if (sinf->keytype_oid == NULL)
goto err;
}
/* The remaining parameters below are mandatory again */
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS);
if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->mintls)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
if ((sinf->mintls != 0) && (sinf->mintls != -1) &&
((sinf->mintls < TLS1_3_VERSION))) {
/* ignore this sigalg as this OpenSSL doesn't know how to handle it */
ret = 1;
goto err;
}
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS);
if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->maxtls)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
if ((sinf->maxtls != 0) && (sinf->maxtls != -1) &&
((sinf->maxtls < sinf->mintls))) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
if ((sinf->maxtls != 0) && (sinf->maxtls != -1) &&
((sinf->maxtls < TLS1_3_VERSION))) {
/* ignore this sigalg as this OpenSSL doesn't know how to handle it */
ret = 1;
goto err;
}
/*
* Now check that the algorithm is actually usable for our property query
* string. Regardless of the result we still return success because we have
* successfully processed this signature, even though we may decide not to
* use it.
*/
ret = 1;
ERR_set_mark();
keytype = (sinf->keytype != NULL
? sinf->keytype
: (sinf->sig_name != NULL
? sinf->sig_name
: sinf->sigalg_name));
keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, keytype, ctx->propq);
if (keymgmt != NULL) {
/*
* We have successfully fetched the algorithm - however if the provider
* doesn't match this one then we ignore it.
*
* Note: We're cheating a little here. Technically if the same algorithm
* is available from more than one provider then it is undefined which
* implementation you will get back. Theoretically this could be
* different every time...we assume here that you'll always get the
* same one back if you repeat the exact same fetch. Is this a reasonable
* assumption to make (in which case perhaps we should document this
* behaviour)?
*/
if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) {
/*
* We have a match - so we could use this signature;
* Check proper object registration first, though.
* Don't care about return value as this may have been
* done within providers or previous calls to
* add_provider_sigalgs.
*/
OBJ_create(sinf->sigalg_oid, sinf->sigalg_name, NULL);
/* sanity check: Without successful registration don't use alg */
if ((OBJ_txt2nid(sinf->sigalg_name) == NID_undef) ||
(OBJ_nid2obj(OBJ_txt2nid(sinf->sigalg_name)) == NULL)) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
if (sinf->sig_name != NULL)
OBJ_create(sinf->sig_oid, sinf->sig_name, NULL);
if (sinf->keytype != NULL)
OBJ_create(sinf->keytype_oid, sinf->keytype, NULL);
if (sinf->hash_name != NULL)
OBJ_create(sinf->hash_oid, sinf->hash_name, NULL);
OBJ_add_sigid(OBJ_txt2nid(sinf->sigalg_name),
(sinf->hash_name != NULL
? OBJ_txt2nid(sinf->hash_name)
: NID_undef),
OBJ_txt2nid(keytype));
ctx->sigalg_list_len++;
sinf = NULL;
}
EVP_KEYMGMT_free(keymgmt);
}
ERR_pop_to_mark();
err:
if (sinf != NULL) {
OPENSSL_free(sinf->name);
sinf->name = NULL;
OPENSSL_free(sinf->sigalg_name);
sinf->sigalg_name = NULL;
OPENSSL_free(sinf->sigalg_oid);
sinf->sigalg_oid = NULL;
OPENSSL_free(sinf->sig_name);
sinf->sig_name = NULL;
OPENSSL_free(sinf->sig_oid);
sinf->sig_oid = NULL;
OPENSSL_free(sinf->hash_name);
sinf->hash_name = NULL;
OPENSSL_free(sinf->hash_oid);
sinf->hash_oid = NULL;
OPENSSL_free(sinf->keytype);
sinf->keytype = NULL;
OPENSSL_free(sinf->keytype_oid);
sinf->keytype_oid = NULL;
}
return ret;
}
static int discover_provider_sigalgs(OSSL_PROVIDER *provider, void *vctx)
{
struct provider_ctx_data_st pgd;
pgd.ctx = vctx;
pgd.provider = provider;
OSSL_PROVIDER_get_capabilities(provider, "TLS-SIGALG",
add_provider_sigalgs, &pgd);
/*
* Always OK, even if provider doesn't support the capability:
* Reconsider testing retval when legacy sigalgs are also loaded this way.
*/
return 1;
}
int ssl_load_sigalgs(SSL_CTX *ctx)
{
size_t i;
SSL_CERT_LOOKUP lu;
if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_sigalgs, ctx))
return 0;
/* now populate ctx->ssl_cert_info */
if (ctx->sigalg_list_len > 0) {
OPENSSL_free(ctx->ssl_cert_info);
ctx->ssl_cert_info = OPENSSL_zalloc(sizeof(lu) * ctx->sigalg_list_len);
if (ctx->ssl_cert_info == NULL)
return 0;
for(i = 0; i < ctx->sigalg_list_len; i++) {
ctx->ssl_cert_info[i].nid = OBJ_txt2nid(ctx->sigalg_list[i].sigalg_name);
ctx->ssl_cert_info[i].amask = SSL_aANY;
}
}
/*
* For now, leave it at this: legacy sigalgs stay in their own
* data structures until "legacy cleanup" occurs.
*/
return 1;
}
static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name)
{
size_t i;
for (i = 0; i < ctx->group_list_len; i++) {
if (strcmp(ctx->group_list[i].tlsname, name) == 0
|| strcmp(ctx->group_list[i].realname, name) == 0)
return ctx->group_list[i].group_id;
}
return 0;
}
const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id)
{
size_t i;
for (i = 0; i < ctx->group_list_len; i++) {
if (ctx->group_list[i].group_id == group_id)
return &ctx->group_list[i];
}
return NULL;
}
const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id)
{
const TLS_GROUP_INFO *tls_group_info = tls1_group_id_lookup(ctx, group_id);
if (tls_group_info == NULL)
return NULL;
return tls_group_info->tlsname;
}
int tls1_group_id2nid(uint16_t group_id, int include_unknown)
{
size_t i;
if (group_id == 0)
return NID_undef;
/*
* Return well known Group NIDs - for backwards compatibility. This won't
* work for groups we don't know about.
*/
for (i = 0; i < OSSL_NELEM(nid_to_group); i++)
{
if (nid_to_group[i].group_id == group_id)
return nid_to_group[i].nid;
}
if (!include_unknown)
return NID_undef;
return TLSEXT_nid_unknown | (int)group_id;
}
uint16_t tls1_nid2group_id(int nid)
{
size_t i;
/*
* Return well known Group ids - for backwards compatibility. This won't
* work for groups we don't know about.
*/
for (i = 0; i < OSSL_NELEM(nid_to_group); i++)
{
if (nid_to_group[i].nid == nid)
return nid_to_group[i].group_id;
}
return 0;
}
/*
* Set *pgroups to the supported groups list and *pgroupslen to
* the number of groups supported.
*/
void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups,
size_t *pgroupslen)
{
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
/* For Suite B mode only include P-256, P-384 */
switch (tls1_suiteb(s)) {
case SSL_CERT_FLAG_SUITEB_128_LOS:
*pgroups = suiteb_curves;
*pgroupslen = OSSL_NELEM(suiteb_curves);
break;
case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
*pgroups = suiteb_curves;
*pgroupslen = 1;
break;
case SSL_CERT_FLAG_SUITEB_192_LOS:
*pgroups = suiteb_curves + 1;
*pgroupslen = 1;
break;
default:
if (s->ext.supportedgroups == NULL) {
*pgroups = sctx->ext.supported_groups_default;
*pgroupslen = sctx->ext.supported_groups_default_len;
} else {
*pgroups = s->ext.supportedgroups;
*pgroupslen = s->ext.supportedgroups_len;
}
break;
}
}
int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id,
int minversion, int maxversion,
int isec, int *okfortls13)
{
const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
group_id);
int ret;
int group_minversion, group_maxversion;
if (okfortls13 != NULL)
*okfortls13 = 0;
if (ginfo == NULL)
return 0;
group_minversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->mindtls : ginfo->mintls;
group_maxversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->maxdtls : ginfo->maxtls;
if (group_minversion < 0 || group_maxversion < 0)
return 0;
if (group_maxversion == 0)
ret = 1;
else
ret = (ssl_version_cmp(s, minversion, group_maxversion) <= 0);
if (group_minversion > 0)
ret &= (ssl_version_cmp(s, maxversion, group_minversion) >= 0);
if (!SSL_CONNECTION_IS_DTLS(s)) {
if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION)
*okfortls13 = (group_maxversion == 0)
|| (group_maxversion >= TLS1_3_VERSION);
}
ret &= !isec
|| strcmp(ginfo->algorithm, "EC") == 0
|| strcmp(ginfo->algorithm, "X25519") == 0
|| strcmp(ginfo->algorithm, "X448") == 0;
return ret;
}
/* See if group is allowed by security callback */
int tls_group_allowed(SSL_CONNECTION *s, uint16_t group, int op)
{
const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
group);
unsigned char gtmp[2];
if (ginfo == NULL)
return 0;
gtmp[0] = group >> 8;
gtmp[1] = group & 0xff;
return ssl_security(s, op, ginfo->secbits,
tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp);
}
/* Return 1 if "id" is in "list" */
static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
{
size_t i;
for (i = 0; i < listlen; i++)
if (list[i] == id)
return 1;
return 0;
}
/*-
* For nmatch >= 0, return the id of the |nmatch|th shared group or 0
* if there is no match.
* For nmatch == -1, return number of matches
* For nmatch == -2, return the id of the group to use for
* a tmp key, or 0 if there is no match.
*/
uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch)
{
const uint16_t *pref, *supp;
size_t num_pref, num_supp, i;
int k;
SSL_CTX *ctx = SSL_CONNECTION_GET_CTX(s);
/* Can't do anything on client side */
if (s->server == 0)
return 0;
if (nmatch == -2) {
if (tls1_suiteb(s)) {
/*
* For Suite B ciphersuite determines curve: we already know
* these are acceptable due to previous checks.
*/
unsigned long cid = s->s3.tmp.new_cipher->id;
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
return OSSL_TLS_GROUP_ID_secp256r1;
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
return OSSL_TLS_GROUP_ID_secp384r1;
/* Should never happen */
return 0;
}
/* If not Suite B just return first preference shared curve */
nmatch = 0;
}
/*
* If server preference set, our groups are the preference order
* otherwise peer decides.
*/
if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
tls1_get_supported_groups(s, &pref, &num_pref);
tls1_get_peer_groups(s, &supp, &num_supp);
} else {
tls1_get_peer_groups(s, &pref, &num_pref);
tls1_get_supported_groups(s, &supp, &num_supp);
}
for (k = 0, i = 0; i < num_pref; i++) {
uint16_t id = pref[i];
const TLS_GROUP_INFO *inf;
int minversion, maxversion;
if (!tls1_in_list(id, supp, num_supp)
|| !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED))
continue;
inf = tls1_group_id_lookup(ctx, id);
if (!ossl_assert(inf != NULL))
return 0;
minversion = SSL_CONNECTION_IS_DTLS(s)
? inf->mindtls : inf->mintls;
maxversion = SSL_CONNECTION_IS_DTLS(s)
? inf->maxdtls : inf->maxtls;
if (maxversion == -1)
continue;
if ((minversion != 0 && ssl_version_cmp(s, s->version, minversion) < 0)
|| (maxversion != 0
&& ssl_version_cmp(s, s->version, maxversion) > 0))
continue;
if (nmatch == k)
return id;
k++;
}
if (nmatch == -1)
return k;
/* Out of range (nmatch > k). */
return 0;
}
int tls1_set_groups(uint16_t **pext, size_t *pextlen,
int *groups, size_t ngroups)
{
uint16_t *glist;
size_t i;
/*
* Bitmap of groups included to detect duplicates: two variables are added
* to detect duplicates as some values are more than 32.
*/