/src/openssl/ssl/t1_lib.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include <stdio.h>  | 
11  |  | #include <stdlib.h>  | 
12  |  | #include <ctype.h>  | 
13  |  | #include <openssl/objects.h>  | 
14  |  | #include <openssl/evp.h>  | 
15  |  | #include <openssl/hmac.h>  | 
16  |  | #include <openssl/core_names.h>  | 
17  |  | #include <openssl/ocsp.h>  | 
18  |  | #include <openssl/conf.h>  | 
19  |  | #include <openssl/x509v3.h>  | 
20  |  | #include <openssl/dh.h>  | 
21  |  | #include <openssl/bn.h>  | 
22  |  | #include <openssl/provider.h>  | 
23  |  | #include <openssl/param_build.h>  | 
24  |  | #include "internal/nelem.h"  | 
25  |  | #include "internal/sizes.h"  | 
26  |  | #include "internal/tlsgroups.h"  | 
27  |  | #include "internal/ssl_unwrap.h"  | 
28  |  | #include "ssl_local.h"  | 
29  |  | #include "quic/quic_local.h"  | 
30  |  | #include <openssl/ct.h>  | 
31  |  |  | 
32  |  | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pkey);  | 
33  |  | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, const SIGALG_LOOKUP *lu);  | 
34  |  |  | 
35  |  | SSL3_ENC_METHOD const TLSv1_enc_data = { | 
36  |  |     tls1_setup_key_block,  | 
37  |  |     tls1_generate_master_secret,  | 
38  |  |     tls1_change_cipher_state,  | 
39  |  |     tls1_final_finish_mac,  | 
40  |  |     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,  | 
41  |  |     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,  | 
42  |  |     tls1_alert_code,  | 
43  |  |     tls1_export_keying_material,  | 
44  |  |     0,  | 
45  |  |     ssl3_set_handshake_header,  | 
46  |  |     tls_close_construct_packet,  | 
47  |  |     ssl3_handshake_write  | 
48  |  | };  | 
49  |  |  | 
50  |  | SSL3_ENC_METHOD const TLSv1_1_enc_data = { | 
51  |  |     tls1_setup_key_block,  | 
52  |  |     tls1_generate_master_secret,  | 
53  |  |     tls1_change_cipher_state,  | 
54  |  |     tls1_final_finish_mac,  | 
55  |  |     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,  | 
56  |  |     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,  | 
57  |  |     tls1_alert_code,  | 
58  |  |     tls1_export_keying_material,  | 
59  |  |     0,  | 
60  |  |     ssl3_set_handshake_header,  | 
61  |  |     tls_close_construct_packet,  | 
62  |  |     ssl3_handshake_write  | 
63  |  | };  | 
64  |  |  | 
65  |  | SSL3_ENC_METHOD const TLSv1_2_enc_data = { | 
66  |  |     tls1_setup_key_block,  | 
67  |  |     tls1_generate_master_secret,  | 
68  |  |     tls1_change_cipher_state,  | 
69  |  |     tls1_final_finish_mac,  | 
70  |  |     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,  | 
71  |  |     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,  | 
72  |  |     tls1_alert_code,  | 
73  |  |     tls1_export_keying_material,  | 
74  |  |     SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF  | 
75  |  |         | SSL_ENC_FLAG_TLS1_2_CIPHERS,  | 
76  |  |     ssl3_set_handshake_header,  | 
77  |  |     tls_close_construct_packet,  | 
78  |  |     ssl3_handshake_write  | 
79  |  | };  | 
80  |  |  | 
81  |  | SSL3_ENC_METHOD const TLSv1_3_enc_data = { | 
82  |  |     tls13_setup_key_block,  | 
83  |  |     tls13_generate_master_secret,  | 
84  |  |     tls13_change_cipher_state,  | 
85  |  |     tls13_final_finish_mac,  | 
86  |  |     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,  | 
87  |  |     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,  | 
88  |  |     tls13_alert_code,  | 
89  |  |     tls13_export_keying_material,  | 
90  |  |     SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,  | 
91  |  |     ssl3_set_handshake_header,  | 
92  |  |     tls_close_construct_packet,  | 
93  |  |     ssl3_handshake_write  | 
94  |  | };  | 
95  |  |  | 
96  |  | OSSL_TIME tls1_default_timeout(void)  | 
97  | 0  | { | 
98  |  |     /*  | 
99  |  |      * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for  | 
100  |  |      * http, the cache would over fill  | 
101  |  |      */  | 
102  | 0  |     return ossl_seconds2time(60 * 60 * 2);  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | int tls1_new(SSL *s)  | 
106  | 0  | { | 
107  | 0  |     if (!ssl3_new(s))  | 
108  | 0  |         return 0;  | 
109  | 0  |     if (!s->method->ssl_clear(s))  | 
110  | 0  |         return 0;  | 
111  |  |  | 
112  | 0  |     return 1;  | 
113  | 0  | }  | 
114  |  |  | 
115  |  | void tls1_free(SSL *s)  | 
116  | 0  | { | 
117  | 0  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);  | 
118  |  | 
  | 
119  | 0  |     if (sc == NULL)  | 
120  | 0  |         return;  | 
121  |  |  | 
122  | 0  |     OPENSSL_free(sc->ext.session_ticket);  | 
123  | 0  |     ssl3_free(s);  | 
124  | 0  | }  | 
125  |  |  | 
126  |  | int tls1_clear(SSL *s)  | 
127  | 0  | { | 
128  | 0  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);  | 
129  |  | 
  | 
130  | 0  |     if (sc == NULL)  | 
131  | 0  |         return 0;  | 
132  |  |  | 
133  | 0  |     if (!ssl3_clear(s))  | 
134  | 0  |         return 0;  | 
135  |  |  | 
136  | 0  |     if (s->method->version == TLS_ANY_VERSION)  | 
137  | 0  |         sc->version = TLS_MAX_VERSION_INTERNAL;  | 
138  | 0  |     else  | 
139  | 0  |         sc->version = s->method->version;  | 
140  |  | 
  | 
141  | 0  |     return 1;  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | /* Legacy NID to group_id mapping. Only works for groups we know about */  | 
145  |  | static const struct { | 
146  |  |     int nid;  | 
147  |  |     uint16_t group_id;  | 
148  |  | } nid_to_group[] = { | 
149  |  |     {NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1}, | 
150  |  |     {NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1}, | 
151  |  |     {NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2}, | 
152  |  |     {NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1}, | 
153  |  |     {NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2}, | 
154  |  |     {NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1}, | 
155  |  |     {NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1}, | 
156  |  |     {NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1}, | 
157  |  |     {NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1}, | 
158  |  |     {NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1}, | 
159  |  |     {NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1}, | 
160  |  |     {NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1}, | 
161  |  |     {NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1}, | 
162  |  |     {NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1}, | 
163  |  |     {NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1}, | 
164  |  |     {NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1}, | 
165  |  |     {NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2}, | 
166  |  |     {NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1}, | 
167  |  |     {NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1}, | 
168  |  |     {NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1}, | 
169  |  |     {NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1}, | 
170  |  |     {NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1}, | 
171  |  |     {NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1}, | 
172  |  |     {NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1}, | 
173  |  |     {NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1}, | 
174  |  |     {NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1}, | 
175  |  |     {NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1}, | 
176  |  |     {NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1}, | 
177  |  |     {EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519}, | 
178  |  |     {EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448}, | 
179  |  |     {NID_brainpoolP256r1tls13, OSSL_TLS_GROUP_ID_brainpoolP256r1_tls13}, | 
180  |  |     {NID_brainpoolP384r1tls13, OSSL_TLS_GROUP_ID_brainpoolP384r1_tls13}, | 
181  |  |     {NID_brainpoolP512r1tls13, OSSL_TLS_GROUP_ID_brainpoolP512r1_tls13}, | 
182  |  |     {NID_id_tc26_gost_3410_2012_256_paramSetA, OSSL_TLS_GROUP_ID_gc256A}, | 
183  |  |     {NID_id_tc26_gost_3410_2012_256_paramSetB, OSSL_TLS_GROUP_ID_gc256B}, | 
184  |  |     {NID_id_tc26_gost_3410_2012_256_paramSetC, OSSL_TLS_GROUP_ID_gc256C}, | 
185  |  |     {NID_id_tc26_gost_3410_2012_256_paramSetD, OSSL_TLS_GROUP_ID_gc256D}, | 
186  |  |     {NID_id_tc26_gost_3410_2012_512_paramSetA, OSSL_TLS_GROUP_ID_gc512A}, | 
187  |  |     {NID_id_tc26_gost_3410_2012_512_paramSetB, OSSL_TLS_GROUP_ID_gc512B}, | 
188  |  |     {NID_id_tc26_gost_3410_2012_512_paramSetC, OSSL_TLS_GROUP_ID_gc512C}, | 
189  |  |     {NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048}, | 
190  |  |     {NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072}, | 
191  |  |     {NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096}, | 
192  |  |     {NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144}, | 
193  |  |     {NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192} | 
194  |  | };  | 
195  |  |  | 
196  |  | static const unsigned char ecformats_default[] = { | 
197  |  |     TLSEXT_ECPOINTFORMAT_uncompressed  | 
198  |  | };  | 
199  |  |  | 
200  |  | static const unsigned char ecformats_all[] = { | 
201  |  |     TLSEXT_ECPOINTFORMAT_uncompressed,  | 
202  |  |     TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,  | 
203  |  |     TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2  | 
204  |  | };  | 
205  |  |  | 
206  |  | /* Group list string of the built-in pseudo group DEFAULT */  | 
207  |  | #define DEFAULT_GROUP_NAME "DEFAULT"  | 
208  |  | #define TLS_DEFAULT_GROUP_LIST \  | 
209  |  |     "?*X25519MLKEM768 / ?*X25519:?secp256r1 / ?X448:?secp384r1:?secp521r1 / ?ffdhe2048:?ffdhe3072"  | 
210  |  |  | 
211  |  | static const uint16_t suiteb_curves[] = { | 
212  |  |     OSSL_TLS_GROUP_ID_secp256r1,  | 
213  |  |     OSSL_TLS_GROUP_ID_secp384r1,  | 
214  |  | };  | 
215  |  |  | 
216  |  | /* Group list string of the built-in pseudo group DEFAULT_SUITE_B */  | 
217  |  | #define SUITE_B_GROUP_NAME "DEFAULT_SUITE_B"  | 
218  |  | #define SUITE_B_GROUP_LIST "secp256r1:secp384r1",  | 
219  |  |  | 
220  |  | struct provider_ctx_data_st { | 
221  |  |     SSL_CTX *ctx;  | 
222  |  |     OSSL_PROVIDER *provider;  | 
223  |  | };  | 
224  |  |  | 
225  | 0  | #define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE        10  | 
226  |  | static OSSL_CALLBACK add_provider_groups;  | 
227  |  | static int add_provider_groups(const OSSL_PARAM params[], void *data)  | 
228  | 0  | { | 
229  | 0  |     struct provider_ctx_data_st *pgd = data;  | 
230  | 0  |     SSL_CTX *ctx = pgd->ctx;  | 
231  | 0  |     const OSSL_PARAM *p;  | 
232  | 0  |     TLS_GROUP_INFO *ginf = NULL;  | 
233  | 0  |     EVP_KEYMGMT *keymgmt;  | 
234  | 0  |     unsigned int gid;  | 
235  | 0  |     unsigned int is_kem = 0;  | 
236  | 0  |     int ret = 0;  | 
237  |  | 
  | 
238  | 0  |     if (ctx->group_list_max_len == ctx->group_list_len) { | 
239  | 0  |         TLS_GROUP_INFO *tmp = NULL;  | 
240  |  | 
  | 
241  | 0  |         if (ctx->group_list_max_len == 0)  | 
242  | 0  |             tmp = OPENSSL_malloc_array(TLS_GROUP_LIST_MALLOC_BLOCK_SIZE,  | 
243  | 0  |                                        sizeof(TLS_GROUP_INFO));  | 
244  | 0  |         else  | 
245  | 0  |             tmp = OPENSSL_realloc_array(ctx->group_list,  | 
246  | 0  |                                         ctx->group_list_max_len  | 
247  | 0  |                                         + TLS_GROUP_LIST_MALLOC_BLOCK_SIZE,  | 
248  | 0  |                                         sizeof(TLS_GROUP_INFO));  | 
249  | 0  |         if (tmp == NULL)  | 
250  | 0  |             return 0;  | 
251  | 0  |         ctx->group_list = tmp;  | 
252  | 0  |         memset(tmp + ctx->group_list_max_len,  | 
253  | 0  |                0,  | 
254  | 0  |                sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);  | 
255  | 0  |         ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE;  | 
256  | 0  |     }  | 
257  |  |  | 
258  | 0  |     ginf = &ctx->group_list[ctx->group_list_len];  | 
259  |  | 
  | 
260  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME);  | 
261  | 0  |     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | 
262  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
263  | 0  |         goto err;  | 
264  | 0  |     }  | 
265  | 0  |     ginf->tlsname = OPENSSL_strdup(p->data);  | 
266  | 0  |     if (ginf->tlsname == NULL)  | 
267  | 0  |         goto err;  | 
268  |  |  | 
269  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL);  | 
270  | 0  |     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | 
271  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
272  | 0  |         goto err;  | 
273  | 0  |     }  | 
274  | 0  |     ginf->realname = OPENSSL_strdup(p->data);  | 
275  | 0  |     if (ginf->realname == NULL)  | 
276  | 0  |         goto err;  | 
277  |  |  | 
278  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID);  | 
279  | 0  |     if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) { | 
280  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
281  | 0  |         goto err;  | 
282  | 0  |     }  | 
283  | 0  |     ginf->group_id = (uint16_t)gid;  | 
284  |  | 
  | 
285  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG);  | 
286  | 0  |     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | 
287  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
288  | 0  |         goto err;  | 
289  | 0  |     }  | 
290  | 0  |     ginf->algorithm = OPENSSL_strdup(p->data);  | 
291  | 0  |     if (ginf->algorithm == NULL)  | 
292  | 0  |         goto err;  | 
293  |  |  | 
294  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS);  | 
295  | 0  |     if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) { | 
296  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
297  | 0  |         goto err;  | 
298  | 0  |     }  | 
299  |  |  | 
300  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM);  | 
301  | 0  |     if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) { | 
302  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
303  | 0  |         goto err;  | 
304  | 0  |     }  | 
305  | 0  |     ginf->is_kem = 1 & is_kem;  | 
306  |  | 
  | 
307  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS);  | 
308  | 0  |     if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) { | 
309  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
310  | 0  |         goto err;  | 
311  | 0  |     }  | 
312  |  |  | 
313  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS);  | 
314  | 0  |     if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) { | 
315  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
316  | 0  |         goto err;  | 
317  | 0  |     }  | 
318  |  |  | 
319  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS);  | 
320  | 0  |     if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) { | 
321  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
322  | 0  |         goto err;  | 
323  | 0  |     }  | 
324  |  |  | 
325  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS);  | 
326  | 0  |     if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) { | 
327  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
328  | 0  |         goto err;  | 
329  | 0  |     }  | 
330  |  |     /*  | 
331  |  |      * Now check that the algorithm is actually usable for our property query  | 
332  |  |      * string. Regardless of the result we still return success because we have  | 
333  |  |      * successfully processed this group, even though we may decide not to use  | 
334  |  |      * it.  | 
335  |  |      */  | 
336  | 0  |     ret = 1;  | 
337  | 0  |     ERR_set_mark();  | 
338  | 0  |     keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq);  | 
339  | 0  |     if (keymgmt != NULL) { | 
340  |  |         /* We have successfully fetched the algorithm, we can use the group. */  | 
341  | 0  |         ctx->group_list_len++;  | 
342  | 0  |         ginf = NULL;  | 
343  | 0  |         EVP_KEYMGMT_free(keymgmt);  | 
344  | 0  |     }  | 
345  | 0  |     ERR_pop_to_mark();  | 
346  | 0  |  err:  | 
347  | 0  |     if (ginf != NULL) { | 
348  | 0  |         OPENSSL_free(ginf->tlsname);  | 
349  | 0  |         OPENSSL_free(ginf->realname);  | 
350  | 0  |         OPENSSL_free(ginf->algorithm);  | 
351  | 0  |         ginf->algorithm = ginf->tlsname = ginf->realname = NULL;  | 
352  | 0  |     }  | 
353  | 0  |     return ret;  | 
354  | 0  | }  | 
355  |  |  | 
356  |  | static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx)  | 
357  | 0  | { | 
358  | 0  |     struct provider_ctx_data_st pgd;  | 
359  |  | 
  | 
360  | 0  |     pgd.ctx = vctx;  | 
361  | 0  |     pgd.provider = provider;  | 
362  | 0  |     return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP",  | 
363  | 0  |                                           add_provider_groups, &pgd);  | 
364  | 0  | }  | 
365  |  |  | 
366  |  | int ssl_load_groups(SSL_CTX *ctx)  | 
367  | 0  | { | 
368  | 0  |     if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx))  | 
369  | 0  |         return 0;  | 
370  |  |  | 
371  | 0  |     return SSL_CTX_set1_groups_list(ctx, TLS_DEFAULT_GROUP_LIST);  | 
372  | 0  | }  | 
373  |  |  | 
374  | 0  | #define TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE        10  | 
375  |  | static OSSL_CALLBACK add_provider_sigalgs;  | 
376  |  | static int add_provider_sigalgs(const OSSL_PARAM params[], void *data)  | 
377  | 0  | { | 
378  | 0  |     struct provider_ctx_data_st *pgd = data;  | 
379  | 0  |     SSL_CTX *ctx = pgd->ctx;  | 
380  | 0  |     OSSL_PROVIDER *provider = pgd->provider;  | 
381  | 0  |     const OSSL_PARAM *p;  | 
382  | 0  |     TLS_SIGALG_INFO *sinf = NULL;  | 
383  | 0  |     EVP_KEYMGMT *keymgmt;  | 
384  | 0  |     const char *keytype;  | 
385  | 0  |     unsigned int code_point = 0;  | 
386  | 0  |     int ret = 0;  | 
387  |  | 
  | 
388  | 0  |     if (ctx->sigalg_list_max_len == ctx->sigalg_list_len) { | 
389  | 0  |         TLS_SIGALG_INFO *tmp = NULL;  | 
390  |  | 
  | 
391  | 0  |         if (ctx->sigalg_list_max_len == 0)  | 
392  | 0  |             tmp = OPENSSL_malloc_array(TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE,  | 
393  | 0  |                                        sizeof(TLS_SIGALG_INFO));  | 
394  | 0  |         else  | 
395  | 0  |             tmp = OPENSSL_realloc_array(ctx->sigalg_list,  | 
396  | 0  |                                         ctx->sigalg_list_max_len  | 
397  | 0  |                                         + TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE,  | 
398  | 0  |                                         sizeof(TLS_SIGALG_INFO));  | 
399  | 0  |         if (tmp == NULL)  | 
400  | 0  |             return 0;  | 
401  | 0  |         ctx->sigalg_list = tmp;  | 
402  | 0  |         memset(tmp + ctx->sigalg_list_max_len, 0,  | 
403  | 0  |                sizeof(TLS_SIGALG_INFO) * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE);  | 
404  | 0  |         ctx->sigalg_list_max_len += TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE;  | 
405  | 0  |     }  | 
406  |  |  | 
407  | 0  |     sinf = &ctx->sigalg_list[ctx->sigalg_list_len];  | 
408  |  |  | 
409  |  |     /* First, mandatory parameters */  | 
410  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_NAME);  | 
411  | 0  |     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | 
412  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
413  | 0  |         goto err;  | 
414  | 0  |     }  | 
415  | 0  |     OPENSSL_free(sinf->sigalg_name);  | 
416  | 0  |     sinf->sigalg_name = OPENSSL_strdup(p->data);  | 
417  | 0  |     if (sinf->sigalg_name == NULL)  | 
418  | 0  |         goto err;  | 
419  |  |  | 
420  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME);  | 
421  | 0  |     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | 
422  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
423  | 0  |         goto err;  | 
424  | 0  |     }  | 
425  | 0  |     OPENSSL_free(sinf->name);  | 
426  | 0  |     sinf->name = OPENSSL_strdup(p->data);  | 
427  | 0  |     if (sinf->name == NULL)  | 
428  | 0  |         goto err;  | 
429  |  |  | 
430  | 0  |     p = OSSL_PARAM_locate_const(params,  | 
431  | 0  |                                 OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT);  | 
432  | 0  |     if (p == NULL  | 
433  | 0  |         || !OSSL_PARAM_get_uint(p, &code_point)  | 
434  | 0  |         || code_point > UINT16_MAX) { | 
435  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
436  | 0  |         goto err;  | 
437  | 0  |     }  | 
438  | 0  |     sinf->code_point = (uint16_t)code_point;  | 
439  |  | 
  | 
440  | 0  |     p = OSSL_PARAM_locate_const(params,  | 
441  | 0  |                                 OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS);  | 
442  | 0  |     if (p == NULL || !OSSL_PARAM_get_uint(p, &sinf->secbits)) { | 
443  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
444  | 0  |         goto err;  | 
445  | 0  |     }  | 
446  |  |  | 
447  |  |     /* Now, optional parameters */  | 
448  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_OID);  | 
449  | 0  |     if (p == NULL) { | 
450  | 0  |         sinf->sigalg_oid = NULL;  | 
451  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
452  | 0  |         goto err;  | 
453  | 0  |     } else { | 
454  | 0  |         OPENSSL_free(sinf->sigalg_oid);  | 
455  | 0  |         sinf->sigalg_oid = OPENSSL_strdup(p->data);  | 
456  | 0  |         if (sinf->sigalg_oid == NULL)  | 
457  | 0  |             goto err;  | 
458  | 0  |     }  | 
459  |  |  | 
460  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME);  | 
461  | 0  |     if (p == NULL) { | 
462  | 0  |         sinf->sig_name = NULL;  | 
463  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
464  | 0  |         goto err;  | 
465  | 0  |     } else { | 
466  | 0  |         OPENSSL_free(sinf->sig_name);  | 
467  | 0  |         sinf->sig_name = OPENSSL_strdup(p->data);  | 
468  | 0  |         if (sinf->sig_name == NULL)  | 
469  | 0  |             goto err;  | 
470  | 0  |     }  | 
471  |  |  | 
472  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_OID);  | 
473  | 0  |     if (p == NULL) { | 
474  | 0  |         sinf->sig_oid = NULL;  | 
475  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
476  | 0  |         goto err;  | 
477  | 0  |     } else { | 
478  | 0  |         OPENSSL_free(sinf->sig_oid);  | 
479  | 0  |         sinf->sig_oid = OPENSSL_strdup(p->data);  | 
480  | 0  |         if (sinf->sig_oid == NULL)  | 
481  | 0  |             goto err;  | 
482  | 0  |     }  | 
483  |  |  | 
484  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME);  | 
485  | 0  |     if (p == NULL) { | 
486  | 0  |         sinf->hash_name = NULL;  | 
487  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
488  | 0  |         goto err;  | 
489  | 0  |     } else { | 
490  | 0  |         OPENSSL_free(sinf->hash_name);  | 
491  | 0  |         sinf->hash_name = OPENSSL_strdup(p->data);  | 
492  | 0  |         if (sinf->hash_name == NULL)  | 
493  | 0  |             goto err;  | 
494  | 0  |     }  | 
495  |  |  | 
496  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_OID);  | 
497  | 0  |     if (p == NULL) { | 
498  | 0  |         sinf->hash_oid = NULL;  | 
499  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
500  | 0  |         goto err;  | 
501  | 0  |     } else { | 
502  | 0  |         OPENSSL_free(sinf->hash_oid);  | 
503  | 0  |         sinf->hash_oid = OPENSSL_strdup(p->data);  | 
504  | 0  |         if (sinf->hash_oid == NULL)  | 
505  | 0  |             goto err;  | 
506  | 0  |     }  | 
507  |  |  | 
508  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE);  | 
509  | 0  |     if (p == NULL) { | 
510  | 0  |         sinf->keytype = NULL;  | 
511  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
512  | 0  |         goto err;  | 
513  | 0  |     } else { | 
514  | 0  |         OPENSSL_free(sinf->keytype);  | 
515  | 0  |         sinf->keytype = OPENSSL_strdup(p->data);  | 
516  | 0  |         if (sinf->keytype == NULL)  | 
517  | 0  |             goto err;  | 
518  | 0  |     }  | 
519  |  |  | 
520  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID);  | 
521  | 0  |     if (p == NULL) { | 
522  | 0  |         sinf->keytype_oid = NULL;  | 
523  | 0  |     } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { | 
524  | 0  |         goto err;  | 
525  | 0  |     } else { | 
526  | 0  |         OPENSSL_free(sinf->keytype_oid);  | 
527  | 0  |         sinf->keytype_oid = OPENSSL_strdup(p->data);  | 
528  | 0  |         if (sinf->keytype_oid == NULL)  | 
529  | 0  |             goto err;  | 
530  | 0  |     }  | 
531  |  |  | 
532  |  |     /* Optional, not documented prior to 3.5 */  | 
533  | 0  |     sinf->mindtls = sinf->maxdtls = -1;  | 
534  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_DTLS);  | 
535  | 0  |     if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->mindtls)) { | 
536  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
537  | 0  |         goto err;  | 
538  | 0  |     }  | 
539  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_DTLS);  | 
540  | 0  |     if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->maxdtls)) { | 
541  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
542  | 0  |         goto err;  | 
543  | 0  |     }  | 
544  |  |     /* DTLS version numbers grow downward */  | 
545  | 0  |     if ((sinf->maxdtls != 0) && (sinf->maxdtls != -1) &&  | 
546  | 0  |         ((sinf->maxdtls > sinf->mindtls))) { | 
547  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
548  | 0  |         goto err;  | 
549  | 0  |     }  | 
550  |  |     /* No provider sigalgs are supported in DTLS, reset after checking. */  | 
551  | 0  |     sinf->mindtls = sinf->maxdtls = -1;  | 
552  |  |  | 
553  |  |     /* The remaining parameters below are mandatory again */  | 
554  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS);  | 
555  | 0  |     if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->mintls)) { | 
556  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
557  | 0  |         goto err;  | 
558  | 0  |     }  | 
559  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS);  | 
560  | 0  |     if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->maxtls)) { | 
561  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
562  | 0  |         goto err;  | 
563  | 0  |     }  | 
564  | 0  |     if ((sinf->maxtls != 0) && (sinf->maxtls != -1) &&  | 
565  | 0  |         ((sinf->maxtls < sinf->mintls))) { | 
566  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
567  | 0  |         goto err;  | 
568  | 0  |     }  | 
569  | 0  |     if ((sinf->mintls != 0) && (sinf->mintls != -1) &&  | 
570  | 0  |         ((sinf->mintls > TLS1_3_VERSION)))  | 
571  | 0  |         sinf->mintls = sinf->maxtls = -1;  | 
572  | 0  |     if ((sinf->maxtls != 0) && (sinf->maxtls != -1) &&  | 
573  | 0  |         ((sinf->maxtls < TLS1_3_VERSION)))  | 
574  | 0  |         sinf->mintls = sinf->maxtls = -1;  | 
575  |  |  | 
576  |  |     /* Ignore unusable sigalgs */  | 
577  | 0  |     if (sinf->mintls == -1 && sinf->mindtls == -1) { | 
578  | 0  |         ret = 1;  | 
579  | 0  |         goto err;  | 
580  | 0  |     }  | 
581  |  |  | 
582  |  |     /*  | 
583  |  |      * Now check that the algorithm is actually usable for our property query  | 
584  |  |      * string. Regardless of the result we still return success because we have  | 
585  |  |      * successfully processed this signature, even though we may decide not to  | 
586  |  |      * use it.  | 
587  |  |      */  | 
588  | 0  |     ret = 1;  | 
589  | 0  |     ERR_set_mark();  | 
590  | 0  |     keytype = (sinf->keytype != NULL  | 
591  | 0  |                ? sinf->keytype  | 
592  | 0  |                : (sinf->sig_name != NULL  | 
593  | 0  |                   ? sinf->sig_name  | 
594  | 0  |                   : sinf->sigalg_name));  | 
595  | 0  |     keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, keytype, ctx->propq);  | 
596  | 0  |     if (keymgmt != NULL) { | 
597  |  |         /*  | 
598  |  |          * We have successfully fetched the algorithm - however if the provider  | 
599  |  |          * doesn't match this one then we ignore it.  | 
600  |  |          *  | 
601  |  |          * Note: We're cheating a little here. Technically if the same algorithm  | 
602  |  |          * is available from more than one provider then it is undefined which  | 
603  |  |          * implementation you will get back. Theoretically this could be  | 
604  |  |          * different every time...we assume here that you'll always get the  | 
605  |  |          * same one back if you repeat the exact same fetch. Is this a reasonable  | 
606  |  |          * assumption to make (in which case perhaps we should document this  | 
607  |  |          * behaviour)?  | 
608  |  |          */  | 
609  | 0  |         if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) { | 
610  |  |             /*  | 
611  |  |              * We have a match - so we could use this signature;  | 
612  |  |              * Check proper object registration first, though.  | 
613  |  |              * Don't care about return value as this may have been  | 
614  |  |              * done within providers or previous calls to  | 
615  |  |              * add_provider_sigalgs.  | 
616  |  |              */  | 
617  | 0  |             OBJ_create(sinf->sigalg_oid, sinf->sigalg_name, NULL);  | 
618  |  |             /* sanity check: Without successful registration don't use alg */  | 
619  | 0  |             if ((OBJ_txt2nid(sinf->sigalg_name) == NID_undef) ||  | 
620  | 0  |                 (OBJ_nid2obj(OBJ_txt2nid(sinf->sigalg_name)) == NULL)) { | 
621  | 0  |                     ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);  | 
622  | 0  |                     goto err;  | 
623  | 0  |             }  | 
624  | 0  |             if (sinf->sig_name != NULL)  | 
625  | 0  |                 OBJ_create(sinf->sig_oid, sinf->sig_name, NULL);  | 
626  | 0  |             if (sinf->keytype != NULL)  | 
627  | 0  |                 OBJ_create(sinf->keytype_oid, sinf->keytype, NULL);  | 
628  | 0  |             if (sinf->hash_name != NULL)  | 
629  | 0  |                 OBJ_create(sinf->hash_oid, sinf->hash_name, NULL);  | 
630  | 0  |             OBJ_add_sigid(OBJ_txt2nid(sinf->sigalg_name),  | 
631  | 0  |                           (sinf->hash_name != NULL  | 
632  | 0  |                            ? OBJ_txt2nid(sinf->hash_name)  | 
633  | 0  |                            : NID_undef),  | 
634  | 0  |                           OBJ_txt2nid(keytype));  | 
635  | 0  |             ctx->sigalg_list_len++;  | 
636  | 0  |             sinf = NULL;  | 
637  | 0  |         }  | 
638  | 0  |         EVP_KEYMGMT_free(keymgmt);  | 
639  | 0  |     }  | 
640  | 0  |     ERR_pop_to_mark();  | 
641  | 0  |  err:  | 
642  | 0  |     if (sinf != NULL) { | 
643  | 0  |         OPENSSL_free(sinf->name);  | 
644  | 0  |         sinf->name = NULL;  | 
645  | 0  |         OPENSSL_free(sinf->sigalg_name);  | 
646  | 0  |         sinf->sigalg_name = NULL;  | 
647  | 0  |         OPENSSL_free(sinf->sigalg_oid);  | 
648  | 0  |         sinf->sigalg_oid = NULL;  | 
649  | 0  |         OPENSSL_free(sinf->sig_name);  | 
650  | 0  |         sinf->sig_name = NULL;  | 
651  | 0  |         OPENSSL_free(sinf->sig_oid);  | 
652  | 0  |         sinf->sig_oid = NULL;  | 
653  | 0  |         OPENSSL_free(sinf->hash_name);  | 
654  | 0  |         sinf->hash_name = NULL;  | 
655  | 0  |         OPENSSL_free(sinf->hash_oid);  | 
656  | 0  |         sinf->hash_oid = NULL;  | 
657  | 0  |         OPENSSL_free(sinf->keytype);  | 
658  | 0  |         sinf->keytype = NULL;  | 
659  | 0  |         OPENSSL_free(sinf->keytype_oid);  | 
660  | 0  |         sinf->keytype_oid = NULL;  | 
661  | 0  |     }  | 
662  | 0  |     return ret;  | 
663  | 0  | }  | 
664  |  |  | 
665  |  | static int discover_provider_sigalgs(OSSL_PROVIDER *provider, void *vctx)  | 
666  | 0  | { | 
667  | 0  |     struct provider_ctx_data_st pgd;  | 
668  |  | 
  | 
669  | 0  |     pgd.ctx = vctx;  | 
670  | 0  |     pgd.provider = provider;  | 
671  | 0  |     OSSL_PROVIDER_get_capabilities(provider, "TLS-SIGALG",  | 
672  | 0  |                                    add_provider_sigalgs, &pgd);  | 
673  |  |     /*  | 
674  |  |      * Always OK, even if provider doesn't support the capability:  | 
675  |  |      * Reconsider testing retval when legacy sigalgs are also loaded this way.  | 
676  |  |      */  | 
677  | 0  |     return 1;  | 
678  | 0  | }  | 
679  |  |  | 
680  |  | int ssl_load_sigalgs(SSL_CTX *ctx)  | 
681  | 0  | { | 
682  | 0  |     size_t i;  | 
683  | 0  |     SSL_CERT_LOOKUP lu;  | 
684  |  | 
  | 
685  | 0  |     if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_sigalgs, ctx))  | 
686  | 0  |         return 0;  | 
687  |  |  | 
688  |  |     /* now populate ctx->ssl_cert_info */  | 
689  | 0  |     if (ctx->sigalg_list_len > 0) { | 
690  | 0  |         OPENSSL_free(ctx->ssl_cert_info);  | 
691  | 0  |         ctx->ssl_cert_info = OPENSSL_calloc(ctx->sigalg_list_len, sizeof(lu));  | 
692  | 0  |         if (ctx->ssl_cert_info == NULL)  | 
693  | 0  |             return 0;  | 
694  | 0  |         for(i = 0; i < ctx->sigalg_list_len; i++) { | 
695  | 0  |             ctx->ssl_cert_info[i].nid = OBJ_txt2nid(ctx->sigalg_list[i].sigalg_name);  | 
696  | 0  |             ctx->ssl_cert_info[i].amask = SSL_aANY;  | 
697  | 0  |         }  | 
698  | 0  |     }  | 
699  |  |  | 
700  |  |     /*  | 
701  |  |      * For now, leave it at this: legacy sigalgs stay in their own  | 
702  |  |      * data structures until "legacy cleanup" occurs.  | 
703  |  |      */  | 
704  |  |  | 
705  | 0  |     return 1;  | 
706  | 0  | }  | 
707  |  |  | 
708  |  | static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name)  | 
709  | 0  | { | 
710  | 0  |     size_t i;  | 
711  |  | 
  | 
712  | 0  |     for (i = 0; i < ctx->group_list_len; i++) { | 
713  | 0  |         if (OPENSSL_strcasecmp(ctx->group_list[i].tlsname, name) == 0  | 
714  | 0  |                 || OPENSSL_strcasecmp(ctx->group_list[i].realname, name) == 0)  | 
715  | 0  |             return ctx->group_list[i].group_id;  | 
716  | 0  |     }  | 
717  |  |  | 
718  | 0  |     return 0;  | 
719  | 0  | }  | 
720  |  |  | 
721  |  | const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id)  | 
722  | 0  | { | 
723  | 0  |     size_t i;  | 
724  |  | 
  | 
725  | 0  |     for (i = 0; i < ctx->group_list_len; i++) { | 
726  | 0  |         if (ctx->group_list[i].group_id == group_id)  | 
727  | 0  |             return &ctx->group_list[i];  | 
728  | 0  |     }  | 
729  |  |  | 
730  | 0  |     return NULL;  | 
731  | 0  | }  | 
732  |  |  | 
733  |  | const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id)  | 
734  | 0  | { | 
735  | 0  |     const TLS_GROUP_INFO *tls_group_info = tls1_group_id_lookup(ctx, group_id);  | 
736  |  | 
  | 
737  | 0  |     if (tls_group_info == NULL)  | 
738  | 0  |         return NULL;  | 
739  |  |  | 
740  | 0  |     return tls_group_info->tlsname;  | 
741  | 0  | }  | 
742  |  |  | 
743  |  | int tls1_group_id2nid(uint16_t group_id, int include_unknown)  | 
744  | 0  | { | 
745  | 0  |     size_t i;  | 
746  |  | 
  | 
747  | 0  |     if (group_id == 0)  | 
748  | 0  |         return NID_undef;  | 
749  |  |  | 
750  |  |     /*  | 
751  |  |      * Return well known Group NIDs - for backwards compatibility. This won't  | 
752  |  |      * work for groups we don't know about.  | 
753  |  |      */  | 
754  | 0  |     for (i = 0; i < OSSL_NELEM(nid_to_group); i++)  | 
755  | 0  |     { | 
756  | 0  |         if (nid_to_group[i].group_id == group_id)  | 
757  | 0  |             return nid_to_group[i].nid;  | 
758  | 0  |     }  | 
759  | 0  |     if (!include_unknown)  | 
760  | 0  |         return NID_undef;  | 
761  | 0  |     return TLSEXT_nid_unknown | (int)group_id;  | 
762  | 0  | }  | 
763  |  |  | 
764  |  | uint16_t tls1_nid2group_id(int nid)  | 
765  | 0  | { | 
766  | 0  |     size_t i;  | 
767  |  |  | 
768  |  |     /*  | 
769  |  |      * Return well known Group ids - for backwards compatibility. This won't  | 
770  |  |      * work for groups we don't know about.  | 
771  |  |      */  | 
772  | 0  |     for (i = 0; i < OSSL_NELEM(nid_to_group); i++)  | 
773  | 0  |     { | 
774  | 0  |         if (nid_to_group[i].nid == nid)  | 
775  | 0  |             return nid_to_group[i].group_id;  | 
776  | 0  |     }  | 
777  |  |  | 
778  | 0  |     return 0;  | 
779  | 0  | }  | 
780  |  |  | 
781  |  | /*  | 
782  |  |  * Set *pgroups to the supported groups list and *pgroupslen to  | 
783  |  |  * the number of groups supported.  | 
784  |  |  */  | 
785  |  | void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups,  | 
786  |  |                                size_t *pgroupslen)  | 
787  | 0  | { | 
788  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
789  |  |  | 
790  |  |     /* For Suite B mode only include P-256, P-384 */  | 
791  | 0  |     switch (tls1_suiteb(s)) { | 
792  | 0  |     case SSL_CERT_FLAG_SUITEB_128_LOS:  | 
793  | 0  |         *pgroups = suiteb_curves;  | 
794  | 0  |         *pgroupslen = OSSL_NELEM(suiteb_curves);  | 
795  | 0  |         break;  | 
796  |  |  | 
797  | 0  |     case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:  | 
798  | 0  |         *pgroups = suiteb_curves;  | 
799  | 0  |         *pgroupslen = 1;  | 
800  | 0  |         break;  | 
801  |  |  | 
802  | 0  |     case SSL_CERT_FLAG_SUITEB_192_LOS:  | 
803  | 0  |         *pgroups = suiteb_curves + 1;  | 
804  | 0  |         *pgroupslen = 1;  | 
805  | 0  |         break;  | 
806  |  |  | 
807  | 0  |     default:  | 
808  | 0  |         if (s->ext.supportedgroups == NULL) { | 
809  | 0  |             *pgroups = sctx->ext.supportedgroups;  | 
810  | 0  |             *pgroupslen = sctx->ext.supportedgroups_len;  | 
811  | 0  |         } else { | 
812  | 0  |             *pgroups = s->ext.supportedgroups;  | 
813  | 0  |             *pgroupslen = s->ext.supportedgroups_len;  | 
814  | 0  |         }  | 
815  | 0  |         break;  | 
816  | 0  |     }  | 
817  | 0  | }  | 
818  |  |  | 
819  |  | /*  | 
820  |  |  * Some comments for the function below:  | 
821  |  |  * s->ext.supportedgroups == NULL means legacy syntax (no [*,/,-]) from built-in group array.  | 
822  |  |  * In this case, we need to send exactly one key share, which MUST be the first (leftmost)  | 
823  |  |  * eligible group from the legacy list. Therefore, we provide the entire list of supported  | 
824  |  |  * groups in this case.  | 
825  |  |  *  | 
826  |  |  * A 'flag' to indicate legacy syntax is created by setting the number of key shares to 1,  | 
827  |  |  * but the groupID to 0.  | 
828  |  |  * The 'flag' is checked right at the beginning in tls_construct_ctos_key_share and either  | 
829  |  |  * the "list of requested key share groups" is used, or the "list of supported groups" in  | 
830  |  |  * combination with setting add_only_one = 1 is applied.  | 
831  |  |  */  | 
832  |  | void tls1_get_requested_keyshare_groups(SSL_CONNECTION *s, const uint16_t **pgroups,  | 
833  |  |                                         size_t *pgroupslen)  | 
834  | 0  | { | 
835  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
836  |  | 
  | 
837  | 0  |     if (s->ext.supportedgroups == NULL) { | 
838  | 0  |         *pgroups = sctx->ext.supportedgroups;  | 
839  | 0  |         *pgroupslen = sctx->ext.supportedgroups_len;  | 
840  | 0  |     } else { | 
841  | 0  |         *pgroups = s->ext.keyshares;  | 
842  | 0  |         *pgroupslen = s->ext.keyshares_len;  | 
843  | 0  |     }  | 
844  | 0  | }  | 
845  |  |  | 
846  |  | void tls1_get_group_tuples(SSL_CONNECTION *s, const size_t **ptuples,  | 
847  |  |                            size_t *ptupleslen)  | 
848  | 0  | { | 
849  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
850  |  | 
  | 
851  | 0  |     if (s->ext.supportedgroups == NULL) { | 
852  | 0  |         *ptuples = sctx->ext.tuples;  | 
853  | 0  |         *ptupleslen = sctx->ext.tuples_len;  | 
854  | 0  |     } else { | 
855  | 0  |         *ptuples = s->ext.tuples;  | 
856  | 0  |         *ptupleslen = s->ext.tuples_len;  | 
857  | 0  |     }  | 
858  | 0  | }  | 
859  |  |  | 
860  |  | int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id,  | 
861  |  |                     int minversion, int maxversion,  | 
862  |  |                     int isec, int *okfortls13)  | 
863  | 0  | { | 
864  | 0  |     const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),  | 
865  | 0  |                                                        group_id);  | 
866  | 0  |     int ret;  | 
867  | 0  |     int group_minversion, group_maxversion;  | 
868  |  | 
  | 
869  | 0  |     if (okfortls13 != NULL)  | 
870  | 0  |         *okfortls13 = 0;  | 
871  |  | 
  | 
872  | 0  |     if (ginfo == NULL)  | 
873  | 0  |         return 0;  | 
874  |  |  | 
875  | 0  |     group_minversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->mindtls : ginfo->mintls;  | 
876  | 0  |     group_maxversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->maxdtls : ginfo->maxtls;  | 
877  |  | 
  | 
878  | 0  |     if (group_minversion < 0 || group_maxversion < 0)  | 
879  | 0  |         return 0;  | 
880  | 0  |     if (group_maxversion == 0)  | 
881  | 0  |         ret = 1;  | 
882  | 0  |     else  | 
883  | 0  |         ret = (ssl_version_cmp(s, minversion, group_maxversion) <= 0);  | 
884  | 0  |     if (group_minversion > 0)  | 
885  | 0  |         ret &= (ssl_version_cmp(s, maxversion, group_minversion) >= 0);  | 
886  |  | 
  | 
887  | 0  |     if (!SSL_CONNECTION_IS_DTLS(s)) { | 
888  | 0  |         if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION)  | 
889  | 0  |             *okfortls13 = (group_maxversion == 0)  | 
890  | 0  |                           || (group_maxversion >= TLS1_3_VERSION);  | 
891  | 0  |     }  | 
892  | 0  |     ret &= !isec  | 
893  | 0  |            || strcmp(ginfo->algorithm, "EC") == 0  | 
894  | 0  |            || strcmp(ginfo->algorithm, "X25519") == 0  | 
895  | 0  |            || strcmp(ginfo->algorithm, "X448") == 0;  | 
896  |  | 
  | 
897  | 0  |     return ret;  | 
898  | 0  | }  | 
899  |  |  | 
900  |  | /* See if group is allowed by security callback */  | 
901  |  | int tls_group_allowed(SSL_CONNECTION *s, uint16_t group, int op)  | 
902  | 0  | { | 
903  | 0  |     const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),  | 
904  | 0  |                                                        group);  | 
905  | 0  |     unsigned char gtmp[2];  | 
906  |  | 
  | 
907  | 0  |     if (ginfo == NULL)  | 
908  | 0  |         return 0;  | 
909  |  |  | 
910  | 0  |     gtmp[0] = group >> 8;  | 
911  | 0  |     gtmp[1] = group & 0xff;  | 
912  | 0  |     return ssl_security(s, op, ginfo->secbits,  | 
913  | 0  |                         tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp);  | 
914  | 0  | }  | 
915  |  |  | 
916  |  | /* Return 1 if "id" is in "list" */  | 
917  |  | static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)  | 
918  | 0  | { | 
919  | 0  |     size_t i;  | 
920  | 0  |     for (i = 0; i < listlen; i++)  | 
921  | 0  |         if (list[i] == id)  | 
922  | 0  |             return 1;  | 
923  | 0  |     return 0;  | 
924  | 0  | }  | 
925  |  |  | 
926  |  | typedef struct { | 
927  |  |     TLS_GROUP_INFO *grp;  | 
928  |  |     size_t ix;  | 
929  |  | } TLS_GROUP_IX;  | 
930  |  |  | 
931  |  | DEFINE_STACK_OF(TLS_GROUP_IX)  | 
932  |  |  | 
933  |  | static void free_wrapper(TLS_GROUP_IX *a)  | 
934  | 0  | { | 
935  | 0  |     OPENSSL_free(a);  | 
936  | 0  | }  | 
937  |  |  | 
938  |  | static int tls_group_ix_cmp(const TLS_GROUP_IX *const *a,  | 
939  |  |                             const TLS_GROUP_IX *const *b)  | 
940  | 0  | { | 
941  | 0  |     int idcmpab = (*a)->grp->group_id < (*b)->grp->group_id;  | 
942  | 0  |     int idcmpba = (*b)->grp->group_id < (*a)->grp->group_id;  | 
943  | 0  |     int ixcmpab = (*a)->ix < (*b)->ix;  | 
944  | 0  |     int ixcmpba = (*b)->ix < (*a)->ix;  | 
945  |  |  | 
946  |  |     /* Ascending by group id */  | 
947  | 0  |     if (idcmpab != idcmpba)  | 
948  | 0  |         return (idcmpba - idcmpab);  | 
949  |  |     /* Ascending by original appearance index */  | 
950  | 0  |     return ixcmpba - ixcmpab;  | 
951  | 0  | }  | 
952  |  |  | 
953  |  | int tls1_get0_implemented_groups(int min_proto_version, int max_proto_version,  | 
954  |  |                                  TLS_GROUP_INFO *grps, size_t num, long all,  | 
955  |  |                                  STACK_OF(OPENSSL_CSTRING) *out)  | 
956  | 0  | { | 
957  | 0  |     STACK_OF(TLS_GROUP_IX) *collect = NULL;  | 
958  | 0  |     TLS_GROUP_IX *gix;  | 
959  | 0  |     uint16_t id = 0;  | 
960  | 0  |     int ret = 0;  | 
961  | 0  |     int ix;  | 
962  |  | 
  | 
963  | 0  |     if (grps == NULL || out == NULL || num > INT_MAX)  | 
964  | 0  |         return 0;  | 
965  | 0  |     if ((collect = sk_TLS_GROUP_IX_new(tls_group_ix_cmp)) == NULL)  | 
966  | 0  |         return 0;  | 
967  | 0  |     for (ix = 0; ix < (int)num; ++ix, ++grps) { | 
968  | 0  |         if (grps->mintls > 0 && max_proto_version > 0  | 
969  | 0  |              && grps->mintls > max_proto_version)  | 
970  | 0  |             continue;  | 
971  | 0  |         if (grps->maxtls > 0 && min_proto_version > 0  | 
972  | 0  |             && grps->maxtls < min_proto_version)  | 
973  | 0  |             continue;  | 
974  |  |  | 
975  | 0  |         if ((gix = OPENSSL_malloc(sizeof(*gix))) == NULL)  | 
976  | 0  |             goto end;  | 
977  | 0  |         gix->grp = grps;  | 
978  | 0  |         gix->ix = ix;  | 
979  | 0  |         if (sk_TLS_GROUP_IX_push(collect, gix) <= 0) { | 
980  | 0  |             OPENSSL_free(gix);  | 
981  | 0  |             goto end;  | 
982  | 0  |         }  | 
983  | 0  |     }  | 
984  |  |  | 
985  | 0  |     sk_TLS_GROUP_IX_sort(collect);  | 
986  | 0  |     num = sk_TLS_GROUP_IX_num(collect);  | 
987  | 0  |     for (ix = 0; ix < (int)num; ++ix) { | 
988  | 0  |         gix = sk_TLS_GROUP_IX_value(collect, ix);  | 
989  | 0  |         if (!all && gix->grp->group_id == id)  | 
990  | 0  |             continue;  | 
991  | 0  |         id = gix->grp->group_id;  | 
992  | 0  |         if (sk_OPENSSL_CSTRING_push(out, gix->grp->tlsname) <= 0)  | 
993  | 0  |             goto end;  | 
994  | 0  |     }  | 
995  | 0  |     ret = 1;  | 
996  |  | 
  | 
997  | 0  |  end:  | 
998  | 0  |     sk_TLS_GROUP_IX_pop_free(collect, free_wrapper);  | 
999  | 0  |     return ret;  | 
1000  | 0  | }  | 
1001  |  |  | 
1002  |  | /*-  | 
1003  |  |  * For nmatch >= 0, return the id of the |nmatch|th shared group or 0  | 
1004  |  |  * if there is no match.  | 
1005  |  |  * For nmatch == -1, return number of matches  | 
1006  |  |  * For nmatch == -2, return the id of the group to use for  | 
1007  |  |  * a tmp key, or 0 if there is no match.  | 
1008  |  |  */  | 
1009  |  | uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch)  | 
1010  | 0  | { | 
1011  | 0  |     const uint16_t *pref, *supp;  | 
1012  | 0  |     size_t num_pref, num_supp, i;  | 
1013  | 0  |     int k;  | 
1014  | 0  |     SSL_CTX *ctx = SSL_CONNECTION_GET_CTX(s);  | 
1015  |  |  | 
1016  |  |     /* Can't do anything on client side */  | 
1017  | 0  |     if (s->server == 0)  | 
1018  | 0  |         return 0;  | 
1019  | 0  |     if (nmatch == -2) { | 
1020  | 0  |         if (tls1_suiteb(s)) { | 
1021  |  |             /*  | 
1022  |  |              * For Suite B ciphersuite determines curve: we already know  | 
1023  |  |              * these are acceptable due to previous checks.  | 
1024  |  |              */  | 
1025  | 0  |             unsigned long cid = s->s3.tmp.new_cipher->id;  | 
1026  |  | 
  | 
1027  | 0  |             if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)  | 
1028  | 0  |                 return OSSL_TLS_GROUP_ID_secp256r1;  | 
1029  | 0  |             if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)  | 
1030  | 0  |                 return OSSL_TLS_GROUP_ID_secp384r1;  | 
1031  |  |             /* Should never happen */  | 
1032  | 0  |             return 0;  | 
1033  | 0  |         }  | 
1034  |  |         /* If not Suite B just return first preference shared curve */  | 
1035  | 0  |         nmatch = 0;  | 
1036  | 0  |     }  | 
1037  |  |     /*  | 
1038  |  |      * If server preference set, our groups are the preference order  | 
1039  |  |      * otherwise peer decides.  | 
1040  |  |      */  | 
1041  | 0  |     if (s->options & SSL_OP_SERVER_PREFERENCE) { | 
1042  | 0  |         tls1_get_supported_groups(s, &pref, &num_pref);  | 
1043  | 0  |         tls1_get_peer_groups(s, &supp, &num_supp);  | 
1044  | 0  |     } else { | 
1045  | 0  |         tls1_get_peer_groups(s, &pref, &num_pref);  | 
1046  | 0  |         tls1_get_supported_groups(s, &supp, &num_supp);  | 
1047  | 0  |     }  | 
1048  |  | 
  | 
1049  | 0  |     for (k = 0, i = 0; i < num_pref; i++) { | 
1050  | 0  |         uint16_t id = pref[i];  | 
1051  | 0  |         const TLS_GROUP_INFO *inf;  | 
1052  | 0  |         int minversion, maxversion;  | 
1053  |  | 
  | 
1054  | 0  |         if (!tls1_in_list(id, supp, num_supp)  | 
1055  | 0  |                 || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED))  | 
1056  | 0  |             continue;  | 
1057  | 0  |         inf = tls1_group_id_lookup(ctx, id);  | 
1058  | 0  |         if (!ossl_assert(inf != NULL))  | 
1059  | 0  |             return 0;  | 
1060  |  |  | 
1061  | 0  |         minversion = SSL_CONNECTION_IS_DTLS(s)  | 
1062  | 0  |                          ? inf->mindtls : inf->mintls;  | 
1063  | 0  |         maxversion = SSL_CONNECTION_IS_DTLS(s)  | 
1064  | 0  |                          ? inf->maxdtls : inf->maxtls;  | 
1065  | 0  |         if (maxversion == -1)  | 
1066  | 0  |             continue;  | 
1067  | 0  |         if ((minversion != 0 && ssl_version_cmp(s, s->version, minversion) < 0)  | 
1068  | 0  |             || (maxversion != 0  | 
1069  | 0  |                 && ssl_version_cmp(s, s->version, maxversion) > 0))  | 
1070  | 0  |             continue;  | 
1071  |  |  | 
1072  | 0  |         if (nmatch == k)  | 
1073  | 0  |             return id;  | 
1074  | 0  |          k++;  | 
1075  | 0  |     }  | 
1076  | 0  |     if (nmatch == -1)  | 
1077  | 0  |         return k;  | 
1078  |  |     /* Out of range (nmatch > k). */  | 
1079  | 0  |     return 0;  | 
1080  | 0  | }  | 
1081  |  |  | 
1082  |  | int tls1_set_groups(uint16_t **grpext, size_t *grpextlen,  | 
1083  |  |                     uint16_t **ksext, size_t *ksextlen,  | 
1084  |  |                     size_t **tplext, size_t *tplextlen,  | 
1085  |  |                     int *groups, size_t ngroups)  | 
1086  | 0  | { | 
1087  | 0  |     uint16_t *glist = NULL, *kslist = NULL;  | 
1088  | 0  |     size_t *tpllist = NULL;  | 
1089  | 0  |     size_t i;  | 
1090  |  |     /*  | 
1091  |  |      * Bitmap of groups included to detect duplicates: two variables are added  | 
1092  |  |      * to detect duplicates as some values are more than 32.  | 
1093  |  |      */  | 
1094  | 0  |     unsigned long *dup_list = NULL;  | 
1095  | 0  |     unsigned long dup_list_egrp = 0;  | 
1096  | 0  |     unsigned long dup_list_dhgrp = 0;  | 
1097  |  | 
  | 
1098  | 0  |     if (ngroups == 0) { | 
1099  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);  | 
1100  | 0  |         return 0;  | 
1101  | 0  |     }  | 
1102  | 0  |     if ((glist = OPENSSL_malloc_array(ngroups, sizeof(*glist))) == NULL)  | 
1103  | 0  |         goto err;  | 
1104  | 0  |     if ((kslist = OPENSSL_malloc_array(1, sizeof(*kslist))) == NULL)  | 
1105  | 0  |         goto err;  | 
1106  | 0  |     if ((tpllist = OPENSSL_malloc_array(1, sizeof(*tpllist))) == NULL)  | 
1107  | 0  |         goto err;  | 
1108  | 0  |     for (i = 0; i < ngroups; i++) { | 
1109  | 0  |         unsigned long idmask;  | 
1110  | 0  |         uint16_t id;  | 
1111  | 0  |         id = tls1_nid2group_id(groups[i]);  | 
1112  | 0  |         if ((id & 0x00FF) >= (sizeof(unsigned long) * 8))  | 
1113  | 0  |             goto err;  | 
1114  | 0  |         idmask = 1L << (id & 0x00FF);  | 
1115  | 0  |         dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp;  | 
1116  | 0  |         if (!id || ((*dup_list) & idmask))  | 
1117  | 0  |             goto err;  | 
1118  | 0  |         *dup_list |= idmask;  | 
1119  | 0  |         glist[i] = id;  | 
1120  | 0  |     }  | 
1121  | 0  |     OPENSSL_free(*grpext);  | 
1122  | 0  |     OPENSSL_free(*ksext);  | 
1123  | 0  |     OPENSSL_free(*tplext);  | 
1124  | 0  |     *grpext = glist;  | 
1125  | 0  |     *grpextlen = ngroups;  | 
1126  | 0  |     kslist[0] = glist[0];  | 
1127  | 0  |     *ksext = kslist;  | 
1128  | 0  |     *ksextlen = 1;  | 
1129  | 0  |     tpllist[0] = ngroups;  | 
1130  | 0  |     *tplext = tpllist;  | 
1131  | 0  |     *tplextlen = 1;  | 
1132  | 0  |     return 1;  | 
1133  | 0  | err:  | 
1134  | 0  |     OPENSSL_free(glist);  | 
1135  | 0  |     OPENSSL_free(kslist);  | 
1136  | 0  |     OPENSSL_free(tpllist);  | 
1137  | 0  |     return 0;  | 
1138  | 0  | }  | 
1139  |  |  | 
1140  |  | /*  | 
1141  |  |  * Definition of DEFAULT[_XYZ] pseudo group names.  | 
1142  |  |  * A pseudo group name is actually a full list of groups, including prefixes  | 
1143  |  |  * and or tuple delimiters. It can be hierarchically defined (for potential future use).  | 
1144  |  |  * IMPORTANT REMARK: For ease of use, in the built-in lists of groups, unknown groups or  | 
1145  |  |  * groups not backed by a provider will always silently be ignored, even without '?' prefix  | 
1146  |  |  */  | 
1147  |  | typedef struct { | 
1148  |  |     const char *list_name; /* The name of this pseudo group */  | 
1149  |  |     const char *group_string; /* The group string of this pseudo group */  | 
1150  |  | } default_group_string_st;    /* (can include '?', '*'. '-', '/' as needed) */  | 
1151  |  |  | 
1152  |  | /* Built-in pseudo group-names must start with a (D or d) */  | 
1153  |  | static const char *DEFAULT_GROUPNAME_FIRST_CHARACTER = "D";  | 
1154  |  |  | 
1155  |  | /* The list of all built-in pseudo-group-name structures */  | 
1156  |  | static const default_group_string_st default_group_strings[] = { | 
1157  |  |     {DEFAULT_GROUP_NAME, TLS_DEFAULT_GROUP_LIST}, | 
1158  |  |     {SUITE_B_GROUP_NAME, SUITE_B_GROUP_LIST} | 
1159  |  | };  | 
1160  |  |  | 
1161  |  | /*  | 
1162  |  |  * Some GOST names are not resolved by tls1_group_name2id,  | 
1163  |  |  * hence we'll check for those manually  | 
1164  |  |  */  | 
1165  |  | typedef struct { | 
1166  |  |     const char *group_name;  | 
1167  |  |     uint16_t groupID;  | 
1168  |  | } name2id_st;  | 
1169  |  | static const name2id_st name2id_arr[] = { | 
1170  |  |     {"GC256A", OSSL_TLS_GROUP_ID_gc256A }, | 
1171  |  |     {"GC256B", OSSL_TLS_GROUP_ID_gc256B }, | 
1172  |  |     {"GC256C", OSSL_TLS_GROUP_ID_gc256C }, | 
1173  |  |     {"GC256D", OSSL_TLS_GROUP_ID_gc256D }, | 
1174  |  |     {"GC512A", OSSL_TLS_GROUP_ID_gc512A }, | 
1175  |  |     {"GC512B", OSSL_TLS_GROUP_ID_gc512B }, | 
1176  |  |     {"GC512C", OSSL_TLS_GROUP_ID_gc512C }, | 
1177  |  | };  | 
1178  |  |  | 
1179  |  | /*  | 
1180  |  |  * Group list management:  | 
1181  |  |  * We establish three lists along with their related size counters:  | 
1182  |  |  * 1) List of (unique) groups  | 
1183  |  |  * 2) List of number of groups per group-priority-tuple  | 
1184  |  |  * 3) List of (unique) key share groups  | 
1185  |  |  */  | 
1186  | 0  | #define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (64 Bytes chunks ~= cache line) */  | 
1187  |  | #define GROUP_NAME_BUFFER_LENGTH 64 /* Max length of a group name */  | 
1188  |  |  | 
1189  |  | /*  | 
1190  |  |  * Preparation of the prefix used to indicate the desire to send a key share,  | 
1191  |  |  * the characters used as separators between groups or tuples of groups, the  | 
1192  |  |  * character to indicate that an unknown group should be ignored, and the  | 
1193  |  |  * character to indicate that a group should be deleted from a list  | 
1194  |  |  */  | 
1195  |  | #ifndef TUPLE_DELIMITER_CHARACTER  | 
1196  |  | /* The prefix characters to indicate group tuple boundaries */  | 
1197  | 0  | # define TUPLE_DELIMITER_CHARACTER '/'  | 
1198  |  | #endif  | 
1199  |  | #ifndef GROUP_DELIMITER_CHARACTER  | 
1200  |  | /* The prefix characters to indicate group tuple boundaries */  | 
1201  | 0  | # define GROUP_DELIMITER_CHARACTER ':'  | 
1202  |  | #endif  | 
1203  |  | #ifndef IGNORE_UNKNOWN_GROUP_CHARACTER  | 
1204  |  | /* The prefix character to ignore unknown groups */  | 
1205  | 0  | # define IGNORE_UNKNOWN_GROUP_CHARACTER '?'  | 
1206  |  | #endif  | 
1207  |  | #ifndef KEY_SHARE_INDICATOR_CHARACTER  | 
1208  |  | /* The prefix character to trigger a key share addition */  | 
1209  | 0  | # define KEY_SHARE_INDICATOR_CHARACTER '*'  | 
1210  |  | #endif  | 
1211  |  | #ifndef REMOVE_GROUP_INDICATOR_CHARACTER  | 
1212  |  | /* The prefix character to trigger a key share removal */  | 
1213  | 0  | # define REMOVE_GROUP_INDICATOR_CHARACTER '-'  | 
1214  |  | #endif  | 
1215  |  | static const char prefixes[] = {TUPLE_DELIMITER_CHARACTER, | 
1216  |  |                                 GROUP_DELIMITER_CHARACTER,  | 
1217  |  |                                 IGNORE_UNKNOWN_GROUP_CHARACTER,  | 
1218  |  |                                 KEY_SHARE_INDICATOR_CHARACTER,  | 
1219  |  |                                 REMOVE_GROUP_INDICATOR_CHARACTER,  | 
1220  |  |                                 '\0'};  | 
1221  |  |  | 
1222  |  | /*  | 
1223  |  |  * High-level description of how group strings are analyzed:  | 
1224  |  |  * A first call back function (tuple_cb) is used to process group tuples, and a  | 
1225  |  |  * second callback function (gid_cb) is used to process the groups inside a tuple.  | 
1226  |  |  * Those callback functions are (indirectly) called by CONF_parse_list with  | 
1227  |  |  * different separators (nominally ':' or '/'), a variable based on gid_cb_st  | 
1228  |  |  * is used to keep track of the parsing results between the various calls  | 
1229  |  |  */  | 
1230  |  |  | 
1231  |  | typedef struct { | 
1232  |  |     SSL_CTX *ctx;  | 
1233  |  |     /* Variables to hold the three lists (groups, requested keyshares, tuple structure) */  | 
1234  |  |     size_t gidmax; /* The memory allocation chunk size for the group IDs */  | 
1235  |  |     size_t gidcnt; /* Number of groups */  | 
1236  |  |     uint16_t *gid_arr; /* The IDs of the supported groups (flat list) */  | 
1237  |  |     size_t tplmax; /* The memory allocation chunk size for the tuple counters */  | 
1238  |  |     size_t tplcnt; /* Number of tuples */  | 
1239  |  |     size_t *tuplcnt_arr; /* The number of groups inside a tuple */  | 
1240  |  |     size_t ksidmax; /* The memory allocation chunk size */  | 
1241  |  |     size_t ksidcnt; /* Number of key shares */  | 
1242  |  |     uint16_t *ksid_arr; /* The IDs of the key share groups (flat list) */  | 
1243  |  |     /* Variable to keep state between execution of callback or helper functions */  | 
1244  |  |     size_t tuple_mode; /* Keeps track whether tuple_cb called from 'the top' or from gid_cb */  | 
1245  |  |     int ignore_unknown_default; /* Flag such that unknown groups for DEFAULT[_XYZ] are ignored */  | 
1246  |  | } gid_cb_st;  | 
1247  |  |  | 
1248  |  | /* Forward declaration of tuple callback function */  | 
1249  |  | static int tuple_cb(const char *tuple, int len, void *arg);  | 
1250  |  |  | 
1251  |  | /*  | 
1252  |  |  * Extract and process the individual groups (and their prefixes if present)  | 
1253  |  |  * present in a tuple. Note: The argument 'elem' is a NON-\0-terminated string  | 
1254  |  |  * and must be appended by a \0 if used as \0-terminated string  | 
1255  |  |  */  | 
1256  |  | static int gid_cb(const char *elem, int len, void *arg)  | 
1257  | 0  | { | 
1258  | 0  |     gid_cb_st *garg = arg;  | 
1259  | 0  |     size_t i, j, k;  | 
1260  | 0  |     uint16_t gid = 0;  | 
1261  | 0  |     int found_group = 0;  | 
1262  | 0  |     char etmp[GROUP_NAME_BUFFER_LENGTH];  | 
1263  | 0  |     int retval = 1; /* We assume success */  | 
1264  | 0  |     char *current_prefix;  | 
1265  | 0  |     int ignore_unknown = 0;  | 
1266  | 0  |     int add_keyshare = 0;  | 
1267  | 0  |     int remove_group = 0;  | 
1268  | 0  |     size_t restored_prefix_index = 0;  | 
1269  | 0  |     char *restored_default_group_string;  | 
1270  | 0  |     int continue_while_loop = 1;  | 
1271  |  |  | 
1272  |  |     /* Sanity checks */  | 
1273  | 0  |     if (garg == NULL || elem == NULL || len <= 0) { | 
1274  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE);  | 
1275  | 0  |         return 0;  | 
1276  | 0  |     }  | 
1277  |  |  | 
1278  |  |     /* Check the possible prefixes (remark: Leading and trailing spaces already cleared) */  | 
1279  | 0  |     while (continue_while_loop && len > 0  | 
1280  | 0  |            && ((current_prefix = strchr(prefixes, elem[0])) != NULL  | 
1281  | 0  |                || OPENSSL_strncasecmp(current_prefix = (char *)DEFAULT_GROUPNAME_FIRST_CHARACTER, elem, 1) == 0)) { | 
1282  |  | 
  | 
1283  | 0  |         switch (*current_prefix) { | 
1284  | 0  |         case TUPLE_DELIMITER_CHARACTER:  | 
1285  |  |             /* tuple delimiter not allowed here -> syntax error */  | 
1286  | 0  |             return -1;  | 
1287  | 0  |             break;  | 
1288  | 0  |         case GROUP_DELIMITER_CHARACTER:  | 
1289  | 0  |             return -1; /* Not a valid prefix for a single group name-> syntax error */  | 
1290  | 0  |             break;  | 
1291  | 0  |         case KEY_SHARE_INDICATOR_CHARACTER:  | 
1292  | 0  |             if (add_keyshare)  | 
1293  | 0  |                 return -1; /* Only single key share prefix allowed -> syntax error */  | 
1294  | 0  |             add_keyshare = 1;  | 
1295  | 0  |             ++elem;  | 
1296  | 0  |             --len;  | 
1297  | 0  |             break;  | 
1298  | 0  |         case REMOVE_GROUP_INDICATOR_CHARACTER:  | 
1299  | 0  |             if (remove_group)  | 
1300  | 0  |                 return -1; /* Only single remove group prefix allowed -> syntax error */  | 
1301  | 0  |             remove_group = 1;  | 
1302  | 0  |             ++elem;  | 
1303  | 0  |             --len;  | 
1304  | 0  |             break;  | 
1305  | 0  |         case IGNORE_UNKNOWN_GROUP_CHARACTER:  | 
1306  | 0  |             if (ignore_unknown)  | 
1307  | 0  |                 return -1; /* Only single ? allowed -> syntax error */  | 
1308  | 0  |             ignore_unknown = 1;  | 
1309  | 0  |             ++elem;  | 
1310  | 0  |             --len;  | 
1311  | 0  |             break;  | 
1312  | 0  |         default:  | 
1313  |  |             /*  | 
1314  |  |              * Check whether a DEFAULT[_XYZ] 'pseudo group' (= a built-in  | 
1315  |  |              * list of groups) should be added  | 
1316  |  |              */  | 
1317  | 0  |             for (i = 0; i < OSSL_NELEM(default_group_strings); i++) { | 
1318  | 0  |                 if ((size_t)len == (strlen(default_group_strings[i].list_name))  | 
1319  | 0  |                     && OPENSSL_strncasecmp(default_group_strings[i].list_name, elem, len) == 0) { | 
1320  |  |                     /*  | 
1321  |  |                      * We're asked to insert an entire list of groups from a  | 
1322  |  |                      * DEFAULT[_XYZ] 'pseudo group' which we do by  | 
1323  |  |                      * recursively calling this function (indirectly via  | 
1324  |  |                      * CONF_parse_list and tuple_cb); essentially, we treat a DEFAULT  | 
1325  |  |                      * group string like a tuple which is appended to the current tuple  | 
1326  |  |                      * rather then starting a new tuple. Variable tuple_mode is the flag which  | 
1327  |  |                      * controls append tuple vs start new tuple.  | 
1328  |  |                      */  | 
1329  |  | 
  | 
1330  | 0  |                     if (ignore_unknown || remove_group)  | 
1331  | 0  |                         return -1; /* removal or ignore not allowed here -> syntax error */  | 
1332  |  |  | 
1333  |  |                     /*  | 
1334  |  |                      * First, we restore any keyshare prefix in a new zero-terminated string  | 
1335  |  |                      * (if not already present)  | 
1336  |  |                      */  | 
1337  | 0  |                     restored_default_group_string =  | 
1338  | 0  |                         OPENSSL_malloc(1 /* max prefix length */ +  | 
1339  | 0  |                                        strlen(default_group_strings[i].group_string) +  | 
1340  | 0  |                                        1 /* \0 */);  | 
1341  | 0  |                     if (restored_default_group_string == NULL)  | 
1342  | 0  |                         return 0;  | 
1343  | 0  |                     if (add_keyshare  | 
1344  |  |                         /* Remark: we tolerate a duplicated keyshare indicator here */  | 
1345  | 0  |                         && default_group_strings[i].group_string[0]  | 
1346  | 0  |                         != KEY_SHARE_INDICATOR_CHARACTER)  | 
1347  | 0  |                         restored_default_group_string[restored_prefix_index++] =  | 
1348  | 0  |                             KEY_SHARE_INDICATOR_CHARACTER;  | 
1349  |  | 
  | 
1350  | 0  |                     memcpy(restored_default_group_string + restored_prefix_index,  | 
1351  | 0  |                            default_group_strings[i].group_string,  | 
1352  | 0  |                            strlen(default_group_strings[i].group_string));  | 
1353  | 0  |                     restored_default_group_string[strlen(default_group_strings[i].group_string) +  | 
1354  | 0  |                                                   restored_prefix_index] = '\0';  | 
1355  |  |                     /* We execute the recursive call */  | 
1356  | 0  |                     garg->ignore_unknown_default = 1; /* We ignore unknown groups for DEFAULT_XYZ */  | 
1357  |  |                     /* we enforce group mode (= append tuple) for DEFAULT_XYZ group lists */  | 
1358  | 0  |                     garg->tuple_mode = 0;  | 
1359  |  |                     /* We use the tuple_cb callback to process the pseudo group tuple */  | 
1360  | 0  |                     retval = CONF_parse_list(restored_default_group_string,  | 
1361  | 0  |                                              TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, garg);  | 
1362  | 0  |                     garg->tuple_mode = 1; /* next call to tuple_cb will again start new tuple */  | 
1363  | 0  |                     garg->ignore_unknown_default = 0; /* reset to original value */  | 
1364  |  |                     /* We don't need the \0-terminated string anymore */  | 
1365  | 0  |                     OPENSSL_free(restored_default_group_string);  | 
1366  |  | 
  | 
1367  | 0  |                     return retval;  | 
1368  | 0  |                 }  | 
1369  | 0  |             }  | 
1370  |  |             /*  | 
1371  |  |              * If we reached this point, a group name started with a 'd' or 'D', but no request  | 
1372  |  |              * for a DEFAULT[_XYZ] 'pseudo group' was detected, hence processing of the group  | 
1373  |  |              * name can continue as usual (= the while loop checking prefixes can end)  | 
1374  |  |              */  | 
1375  | 0  |             continue_while_loop = 0;  | 
1376  | 0  |             break;  | 
1377  | 0  |         }  | 
1378  | 0  |     }  | 
1379  |  |  | 
1380  | 0  |     if (len == 0)  | 
1381  | 0  |         return -1; /* Seems we have prefxes without a group name -> syntax error */  | 
1382  |  |  | 
1383  | 0  |     if (garg->ignore_unknown_default == 1) /* Always ignore unknown groups for DEFAULT[_XYZ] */  | 
1384  | 0  |         ignore_unknown = 1;  | 
1385  |  |  | 
1386  |  |     /* Memory management in case more groups are present compared to initial allocation */  | 
1387  | 0  |     if (garg->gidcnt == garg->gidmax) { | 
1388  | 0  |         uint16_t *tmp =  | 
1389  | 0  |             OPENSSL_realloc_array(garg->gid_arr,  | 
1390  | 0  |                                   garg->gidmax + GROUPLIST_INCREMENT,  | 
1391  | 0  |                                   sizeof(*garg->gid_arr));  | 
1392  |  | 
  | 
1393  | 0  |         if (tmp == NULL)  | 
1394  | 0  |             return 0;  | 
1395  |  |  | 
1396  | 0  |         garg->gidmax += GROUPLIST_INCREMENT;  | 
1397  | 0  |         garg->gid_arr = tmp;  | 
1398  | 0  |     }  | 
1399  |  |     /* Memory management for key share groups */  | 
1400  | 0  |     if (garg->ksidcnt == garg->ksidmax) { | 
1401  | 0  |         uint16_t *tmp =  | 
1402  | 0  |             OPENSSL_realloc_array(garg->ksid_arr,  | 
1403  | 0  |                                   garg->ksidmax + GROUPLIST_INCREMENT,  | 
1404  | 0  |                                   sizeof(*garg->ksid_arr));  | 
1405  |  | 
  | 
1406  | 0  |         if (tmp == NULL)  | 
1407  | 0  |             return 0;  | 
1408  | 0  |         garg->ksidmax += GROUPLIST_INCREMENT;  | 
1409  | 0  |         garg->ksid_arr = tmp;  | 
1410  | 0  |     }  | 
1411  |  |  | 
1412  | 0  |     if (len > (int)(sizeof(etmp) - 1))  | 
1413  | 0  |         return -1; /* group name to long  -> syntax error */  | 
1414  |  |  | 
1415  |  |     /*  | 
1416  |  |      * Prepare addition or removal of a single group by converting  | 
1417  |  |      * a group name into its groupID equivalent  | 
1418  |  |      */  | 
1419  |  |  | 
1420  |  |     /* Create a \0-terminated string and get the gid for this group if possible */  | 
1421  | 0  |     memcpy(etmp, elem, len);  | 
1422  | 0  |     etmp[len] = 0;  | 
1423  |  |  | 
1424  |  |     /* Get the groupID */  | 
1425  | 0  |     gid = tls1_group_name2id(garg->ctx, etmp);  | 
1426  |  |     /*  | 
1427  |  |      * Handle the case where no valid groupID was returned  | 
1428  |  |      * e.g. for an unknown group, which we'd ignore (only) if relevant prefix was set  | 
1429  |  |      */  | 
1430  | 0  |     if (gid == 0) { | 
1431  |  |         /* Is it one of the GOST groups ? */  | 
1432  | 0  |         for (i = 0; i < OSSL_NELEM(name2id_arr); i++) { | 
1433  | 0  |             if (OPENSSL_strcasecmp(etmp, name2id_arr[i].group_name) == 0) { | 
1434  | 0  |                 gid = name2id_arr[i].groupID;  | 
1435  | 0  |                 break;  | 
1436  | 0  |             }  | 
1437  | 0  |         }  | 
1438  | 0  |         if (gid == 0) { /* still not found */ | 
1439  |  |             /* Unknown group - ignore if ignore_unknown; trigger error otherwise */  | 
1440  | 0  |             retval = ignore_unknown;  | 
1441  | 0  |             goto done;  | 
1442  | 0  |         }  | 
1443  | 0  |     }  | 
1444  |  |  | 
1445  |  |     /* Make sure that at least one provider is supporting this groupID */  | 
1446  | 0  |     found_group = 0;  | 
1447  | 0  |     for (j = 0; j < garg->ctx->group_list_len; j++)  | 
1448  | 0  |         if (garg->ctx->group_list[j].group_id == gid) { | 
1449  | 0  |             found_group = 1;  | 
1450  | 0  |             break;  | 
1451  | 0  |         }  | 
1452  |  |  | 
1453  |  |     /*  | 
1454  |  |      * No provider supports this group - ignore if  | 
1455  |  |      * ignore_unknown; trigger error otherwise  | 
1456  |  |      */  | 
1457  | 0  |     if (found_group == 0) { | 
1458  | 0  |         retval = ignore_unknown;  | 
1459  | 0  |         goto done;  | 
1460  | 0  |     }  | 
1461  |  |     /* Remove group (and keyshare) from anywhere in the list if present, ignore if not present */  | 
1462  | 0  |     if (remove_group) { | 
1463  |  |         /* Is the current group specified anywhere in the entire list so far? */  | 
1464  | 0  |         found_group = 0;  | 
1465  | 0  |         for (i = 0; i < garg->gidcnt; i++)  | 
1466  | 0  |             if (garg->gid_arr[i] == gid) { | 
1467  | 0  |                 found_group = 1;  | 
1468  | 0  |                 break;  | 
1469  | 0  |             }  | 
1470  |  |         /* The group to remove is at position i in the list of (zero indexed) groups */  | 
1471  | 0  |         if (found_group) { | 
1472  |  |             /* We remove that group from its position (which is at i)... */  | 
1473  | 0  |             for (j = i; j < (garg->gidcnt - 1); j++)  | 
1474  | 0  |                 garg->gid_arr[j] = garg->gid_arr[j + 1]; /* ...shift remaining groups left ... */  | 
1475  | 0  |             garg->gidcnt--; /* ..and update the book keeping for the number of groups */  | 
1476  |  |  | 
1477  |  |             /*  | 
1478  |  |              * We also must update the number of groups either in a previous tuple (which we  | 
1479  |  |              * must identify and check whether it becomes empty due to the deletion) or in  | 
1480  |  |              * the current tuple, pending where the deleted group resides  | 
1481  |  |              */  | 
1482  | 0  |             k = 0;  | 
1483  | 0  |             for (j = 0; j < garg->tplcnt; j++) { | 
1484  | 0  |                 k += garg->tuplcnt_arr[j];  | 
1485  |  |                 /* Remark: i is zero-indexed, k is one-indexed */  | 
1486  | 0  |                 if (k > i) { /* remove from one of the previous tuples */ | 
1487  | 0  |                     garg->tuplcnt_arr[j]--;  | 
1488  | 0  |                     break; /* We took care not to have group duplicates, hence we can stop here */  | 
1489  | 0  |                 }  | 
1490  | 0  |             }  | 
1491  | 0  |             if (k <= i) /* remove from current tuple */  | 
1492  | 0  |                 garg->tuplcnt_arr[j]--;  | 
1493  |  |  | 
1494  |  |             /* We also remove the group from the list of keyshares (if present) */  | 
1495  | 0  |             found_group = 0;  | 
1496  | 0  |             for (i = 0; i < garg->ksidcnt; i++)  | 
1497  | 0  |                 if (garg->ksid_arr[i] == gid) { | 
1498  | 0  |                     found_group = 1;  | 
1499  | 0  |                     break;  | 
1500  | 0  |                 }  | 
1501  | 0  |             if (found_group) { | 
1502  |  |                 /* Found, hence we remove that keyshare from its position (which is at i)... */  | 
1503  | 0  |                 for (j = i; j < (garg->ksidcnt - 1); j++)  | 
1504  | 0  |                     garg->ksid_arr[j] = garg->ksid_arr[j + 1]; /* shift remaining key shares */  | 
1505  |  |                 /* ... and update the book keeping */  | 
1506  | 0  |                 garg->ksidcnt--;  | 
1507  | 0  |             }  | 
1508  | 0  |         }  | 
1509  | 0  |     } else { /* Processing addition of a single new group */ | 
1510  |  |  | 
1511  |  |         /* Check for duplicates */  | 
1512  | 0  |         for (i = 0; i < garg->gidcnt; i++)  | 
1513  | 0  |             if (garg->gid_arr[i] == gid) { | 
1514  |  |                 /* Duplicate group anywhere in the list of groups - ignore */  | 
1515  | 0  |                 goto done;  | 
1516  | 0  |             }  | 
1517  |  |  | 
1518  |  |         /* Add the current group to the 'flat' list of groups */  | 
1519  | 0  |         garg->gid_arr[garg->gidcnt++] = gid;  | 
1520  |  |         /* and update the book keeping for the number of groups in current tuple */  | 
1521  | 0  |         garg->tuplcnt_arr[garg->tplcnt]++;  | 
1522  |  |  | 
1523  |  |         /* We memorize if needed that we want to add a key share for the current group */  | 
1524  | 0  |         if (add_keyshare)  | 
1525  | 0  |             garg->ksid_arr[garg->ksidcnt++] = gid;  | 
1526  | 0  |     }  | 
1527  |  |  | 
1528  | 0  | done:  | 
1529  | 0  |     return retval;  | 
1530  | 0  | }  | 
1531  |  |  | 
1532  |  | /* Extract and process a tuple of groups */  | 
1533  |  | static int tuple_cb(const char *tuple, int len, void *arg)  | 
1534  | 0  | { | 
1535  | 0  |     gid_cb_st *garg = arg;  | 
1536  | 0  |     int retval = 1; /* We assume success */  | 
1537  | 0  |     char *restored_tuple_string;  | 
1538  |  |  | 
1539  |  |     /* Sanity checks */  | 
1540  | 0  |     if (garg == NULL || tuple == NULL || len <= 0) { | 
1541  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE);  | 
1542  | 0  |         return 0;  | 
1543  | 0  |     }  | 
1544  |  |  | 
1545  |  |     /* Memory management for tuples */  | 
1546  | 0  |     if (garg->tplcnt == garg->tplmax) { | 
1547  | 0  |         size_t *tmp =  | 
1548  | 0  |             OPENSSL_realloc_array(garg->tuplcnt_arr,  | 
1549  | 0  |                                   garg->tplmax + GROUPLIST_INCREMENT,  | 
1550  | 0  |                                   sizeof(*garg->tuplcnt_arr));  | 
1551  |  | 
  | 
1552  | 0  |         if (tmp == NULL)  | 
1553  | 0  |             return 0;  | 
1554  | 0  |         garg->tplmax += GROUPLIST_INCREMENT;  | 
1555  | 0  |         garg->tuplcnt_arr = tmp;  | 
1556  | 0  |     }  | 
1557  |  |  | 
1558  |  |     /* Convert to \0-terminated string */  | 
1559  | 0  |     restored_tuple_string = OPENSSL_malloc(len + 1 /* \0 */);  | 
1560  | 0  |     if (restored_tuple_string == NULL)  | 
1561  | 0  |         return 0;  | 
1562  | 0  |     memcpy(restored_tuple_string, tuple, len);  | 
1563  | 0  |     restored_tuple_string[len] = '\0';  | 
1564  |  |  | 
1565  |  |     /* Analyze group list of this tuple */  | 
1566  | 0  |     retval = CONF_parse_list(restored_tuple_string, GROUP_DELIMITER_CHARACTER, 1, gid_cb, arg);  | 
1567  |  |  | 
1568  |  |     /* We don't need the \o-terminated string anymore */  | 
1569  | 0  |     OPENSSL_free(restored_tuple_string);  | 
1570  |  | 
  | 
1571  | 0  |     if (garg->tuplcnt_arr[garg->tplcnt] > 0) { /* Some valid groups are present in current tuple... */ | 
1572  | 0  |         if (garg->tuple_mode) { | 
1573  |  |             /* We 'close' the tuple */  | 
1574  | 0  |             garg->tplcnt++;  | 
1575  | 0  |             garg->tuplcnt_arr[garg->tplcnt] = 0; /* Next tuple is initialized to be empty */  | 
1576  | 0  |             garg->tuple_mode = 1; /* next call will start a tuple (unless overridden in gid_cb) */  | 
1577  | 0  |         }  | 
1578  | 0  |     }  | 
1579  |  | 
  | 
1580  | 0  |     return retval;  | 
1581  | 0  | }  | 
1582  |  |  | 
1583  |  | /*  | 
1584  |  |  * Set groups and prepare generation of keyshares based on a string of groupnames,  | 
1585  |  |  * names separated by the group or the tuple delimiter, with per-group prefixes to  | 
1586  |  |  * (1) add a key share for this group, (2) ignore the group if unknown to the current  | 
1587  |  |  * context, (3) delete a previous occurrence of the group in the current tuple.  | 
1588  |  |  *  | 
1589  |  |  * The list parsing is done in two hierarchical steps: The top-level step extracts the  | 
1590  |  |  * string of a tuple using tuple_cb, while the next lower step uses gid_cb to  | 
1591  |  |  * parse and process the groups inside a tuple  | 
1592  |  |  */  | 
1593  |  | int tls1_set_groups_list(SSL_CTX *ctx,  | 
1594  |  |                          uint16_t **grpext, size_t *grpextlen,  | 
1595  |  |                          uint16_t **ksext, size_t *ksextlen,  | 
1596  |  |                          size_t **tplext, size_t *tplextlen,  | 
1597  |  |                          const char *str)  | 
1598  | 0  | { | 
1599  | 0  |     size_t i = 0, j;  | 
1600  | 0  |     int ret = 0, parse_ret = 0;  | 
1601  | 0  |     gid_cb_st gcb;  | 
1602  |  |  | 
1603  |  |     /* Sanity check */  | 
1604  | 0  |     if (ctx == NULL) { | 
1605  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);  | 
1606  | 0  |         return 0;  | 
1607  | 0  |     }  | 
1608  |  |  | 
1609  | 0  |     memset(&gcb, 0, sizeof(gcb));  | 
1610  | 0  |     gcb.tuple_mode = 1; /* We prepare to collect the first tuple */  | 
1611  | 0  |     gcb.ignore_unknown_default = 0;  | 
1612  | 0  |     gcb.gidmax = GROUPLIST_INCREMENT;  | 
1613  | 0  |     gcb.tplmax = GROUPLIST_INCREMENT;  | 
1614  | 0  |     gcb.ksidmax = GROUPLIST_INCREMENT;  | 
1615  | 0  |     gcb.ctx = ctx;  | 
1616  |  |  | 
1617  |  |     /* Prepare initial chunks of memory for groups, tuples and keyshares groupIDs */  | 
1618  | 0  |     gcb.gid_arr = OPENSSL_malloc_array(gcb.gidmax, sizeof(*gcb.gid_arr));  | 
1619  | 0  |     if (gcb.gid_arr == NULL)  | 
1620  | 0  |         goto end;  | 
1621  | 0  |     gcb.tuplcnt_arr = OPENSSL_malloc_array(gcb.tplmax, sizeof(*gcb.tuplcnt_arr));  | 
1622  | 0  |     if (gcb.tuplcnt_arr == NULL)  | 
1623  | 0  |         goto end;  | 
1624  | 0  |     gcb.tuplcnt_arr[0] = 0;  | 
1625  | 0  |     gcb.ksid_arr = OPENSSL_malloc_array(gcb.ksidmax, sizeof(*gcb.ksid_arr));  | 
1626  | 0  |     if (gcb.ksid_arr == NULL)  | 
1627  | 0  |         goto end;  | 
1628  |  |  | 
1629  | 0  |     while (str[0] != '\0' && isspace((unsigned char)*str))  | 
1630  | 0  |         str++;  | 
1631  | 0  |     if (str[0] == '\0')  | 
1632  | 0  |         goto empty_list;  | 
1633  |  |  | 
1634  |  |     /*  | 
1635  |  |      * Start the (potentially recursive) tuple processing by calling CONF_parse_list  | 
1636  |  |      * with the TUPLE_DELIMITER_CHARACTER (which will call tuple_cb after cleaning spaces)  | 
1637  |  |      */  | 
1638  | 0  |     parse_ret = CONF_parse_list(str, TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, &gcb);  | 
1639  |  | 
  | 
1640  | 0  |     if (parse_ret == 0)  | 
1641  | 0  |         goto end;  | 
1642  | 0  |     if (parse_ret == -1) { | 
1643  | 0  |         ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,  | 
1644  | 0  |                        "Syntax error in '%s'", str);  | 
1645  | 0  |         goto end;  | 
1646  | 0  |     }  | 
1647  |  |  | 
1648  |  |     /*  | 
1649  |  |      * We check whether a tuple was completely emptied by using "-" prefix  | 
1650  |  |      * excessively, in which case we remove the tuple  | 
1651  |  |      */  | 
1652  | 0  |     for (i = j = 0; j < gcb.tplcnt; j++) { | 
1653  | 0  |         if (gcb.tuplcnt_arr[j] == 0)  | 
1654  | 0  |             continue;  | 
1655  |  |         /* If there's a gap, move to first unfilled slot */  | 
1656  | 0  |         if (j == i)  | 
1657  | 0  |             ++i;  | 
1658  | 0  |         else  | 
1659  | 0  |             gcb.tuplcnt_arr[i++] = gcb.tuplcnt_arr[j];  | 
1660  | 0  |     }  | 
1661  | 0  |     gcb.tplcnt = i;  | 
1662  |  | 
  | 
1663  | 0  |     if (gcb.ksidcnt > OPENSSL_CLIENT_MAX_KEY_SHARES) { | 
1664  | 0  |         ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,  | 
1665  | 0  |                        "To many keyshares requested in '%s' (max = %d)",  | 
1666  | 0  |                        str, OPENSSL_CLIENT_MAX_KEY_SHARES);  | 
1667  | 0  |         goto end;  | 
1668  | 0  |     }  | 
1669  |  |  | 
1670  |  |     /*  | 
1671  |  |      * For backward compatibility we let the rest of the code know that a key share  | 
1672  |  |      * for the first valid group should be added if no "*" prefix was used anywhere  | 
1673  |  |      */  | 
1674  | 0  |     if (gcb.gidcnt > 0 && gcb.ksidcnt == 0) { | 
1675  |  |         /*  | 
1676  |  |          * No key share group prefix character was used, hence we indicate that a single  | 
1677  |  |          * key share should be sent and flag that it should come from the supported_groups list  | 
1678  |  |          */  | 
1679  | 0  |         gcb.ksidcnt = 1;  | 
1680  | 0  |         gcb.ksid_arr[0] = 0;  | 
1681  | 0  |     }  | 
1682  |  | 
  | 
1683  | 0  |  empty_list:  | 
1684  |  |     /*  | 
1685  |  |      * A call to tls1_set_groups_list with any of the args (other than ctx) set  | 
1686  |  |      * to NULL only does a syntax check, hence we're done here and report success  | 
1687  |  |      */  | 
1688  | 0  |     if (grpext == NULL || ksext == NULL || tplext == NULL ||  | 
1689  | 0  |         grpextlen == NULL || ksextlen == NULL || tplextlen == NULL) { | 
1690  | 0  |         ret = 1;  | 
1691  | 0  |         goto end;  | 
1692  | 0  |     }  | 
1693  |  |  | 
1694  |  |     /*  | 
1695  |  |      * tuple_cb and gid_cb combo ensures there are no duplicates or unknown groups so we  | 
1696  |  |      * can just go ahead and set the results (after disposing the existing)  | 
1697  |  |      */  | 
1698  | 0  |     OPENSSL_free(*grpext);  | 
1699  | 0  |     *grpext = gcb.gid_arr;  | 
1700  | 0  |     *grpextlen = gcb.gidcnt;  | 
1701  | 0  |     OPENSSL_free(*ksext);  | 
1702  | 0  |     *ksext = gcb.ksid_arr;  | 
1703  | 0  |     *ksextlen = gcb.ksidcnt;  | 
1704  | 0  |     OPENSSL_free(*tplext);  | 
1705  | 0  |     *tplext = gcb.tuplcnt_arr;  | 
1706  | 0  |     *tplextlen = gcb.tplcnt;  | 
1707  |  | 
  | 
1708  | 0  |     return 1;  | 
1709  |  |  | 
1710  | 0  |  end:  | 
1711  | 0  |     OPENSSL_free(gcb.gid_arr);  | 
1712  | 0  |     OPENSSL_free(gcb.tuplcnt_arr);  | 
1713  | 0  |     OPENSSL_free(gcb.ksid_arr);  | 
1714  | 0  |     return ret;  | 
1715  | 0  | }  | 
1716  |  |  | 
1717  |  | /* Check a group id matches preferences */  | 
1718  |  | int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id,  | 
1719  |  |                         int check_own_groups)  | 
1720  | 0  |     { | 
1721  | 0  |     const uint16_t *groups;  | 
1722  | 0  |     size_t groups_len;  | 
1723  |  | 
  | 
1724  | 0  |     if (group_id == 0)  | 
1725  | 0  |         return 0;  | 
1726  |  |  | 
1727  |  |     /* Check for Suite B compliance */  | 
1728  | 0  |     if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) { | 
1729  | 0  |         unsigned long cid = s->s3.tmp.new_cipher->id;  | 
1730  |  | 
  | 
1731  | 0  |         if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { | 
1732  | 0  |             if (group_id != OSSL_TLS_GROUP_ID_secp256r1)  | 
1733  | 0  |                 return 0;  | 
1734  | 0  |         } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { | 
1735  | 0  |             if (group_id != OSSL_TLS_GROUP_ID_secp384r1)  | 
1736  | 0  |                 return 0;  | 
1737  | 0  |         } else { | 
1738  |  |             /* Should never happen */  | 
1739  | 0  |             return 0;  | 
1740  | 0  |         }  | 
1741  | 0  |     }  | 
1742  |  |  | 
1743  | 0  |     if (check_own_groups) { | 
1744  |  |         /* Check group is one of our preferences */  | 
1745  | 0  |         tls1_get_supported_groups(s, &groups, &groups_len);  | 
1746  | 0  |         if (!tls1_in_list(group_id, groups, groups_len))  | 
1747  | 0  |             return 0;  | 
1748  | 0  |     }  | 
1749  |  |  | 
1750  | 0  |     if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))  | 
1751  | 0  |         return 0;  | 
1752  |  |  | 
1753  |  |     /* For clients, nothing more to check */  | 
1754  | 0  |     if (!s->server)  | 
1755  | 0  |         return 1;  | 
1756  |  |  | 
1757  |  |     /* Check group is one of peers preferences */  | 
1758  | 0  |     tls1_get_peer_groups(s, &groups, &groups_len);  | 
1759  |  |  | 
1760  |  |     /*  | 
1761  |  |      * RFC 4492 does not require the supported elliptic curves extension  | 
1762  |  |      * so if it is not sent we can just choose any curve.  | 
1763  |  |      * It is invalid to send an empty list in the supported groups  | 
1764  |  |      * extension, so groups_len == 0 always means no extension.  | 
1765  |  |      */  | 
1766  | 0  |     if (groups_len == 0)  | 
1767  | 0  |             return 1;  | 
1768  | 0  |     return tls1_in_list(group_id, groups, groups_len);  | 
1769  | 0  | }  | 
1770  |  |  | 
1771  |  | void tls1_get_formatlist(SSL_CONNECTION *s, const unsigned char **pformats,  | 
1772  |  |                          size_t *num_formats)  | 
1773  | 0  | { | 
1774  |  |     /*  | 
1775  |  |      * If we have a custom point format list use it otherwise use default  | 
1776  |  |      */  | 
1777  | 0  |     if (s->ext.ecpointformats) { | 
1778  | 0  |         *pformats = s->ext.ecpointformats;  | 
1779  | 0  |         *num_formats = s->ext.ecpointformats_len;  | 
1780  | 0  |     } else if ((s->options & SSL_OP_LEGACY_EC_POINT_FORMATS) != 0) { | 
1781  | 0  |         *pformats = ecformats_all;  | 
1782  |  |         /* For Suite B we don't support char2 fields */  | 
1783  | 0  |         if (tls1_suiteb(s))  | 
1784  | 0  |             *num_formats = sizeof(ecformats_all) - 1;  | 
1785  | 0  |         else  | 
1786  | 0  |             *num_formats = sizeof(ecformats_all);  | 
1787  | 0  |     } else { | 
1788  | 0  |         *pformats = ecformats_default;  | 
1789  | 0  |         *num_formats = sizeof(ecformats_default);  | 
1790  | 0  |     }  | 
1791  | 0  | }  | 
1792  |  |  | 
1793  |  | /* Check a key is compatible with compression extension */  | 
1794  |  | static int tls1_check_pkey_comp(SSL_CONNECTION *s, EVP_PKEY *pkey)  | 
1795  | 0  | { | 
1796  | 0  |     unsigned char comp_id;  | 
1797  | 0  |     size_t i;  | 
1798  | 0  |     int point_conv;  | 
1799  |  |  | 
1800  |  |     /* If not an EC key nothing to check */  | 
1801  | 0  |     if (!EVP_PKEY_is_a(pkey, "EC"))  | 
1802  | 0  |         return 1;  | 
1803  |  |  | 
1804  |  |  | 
1805  |  |     /* Get required compression id */  | 
1806  | 0  |     point_conv = EVP_PKEY_get_ec_point_conv_form(pkey);  | 
1807  | 0  |     if (point_conv == 0)  | 
1808  | 0  |         return 0;  | 
1809  | 0  |     if (point_conv == POINT_CONVERSION_UNCOMPRESSED) { | 
1810  | 0  |             comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;  | 
1811  | 0  |     } else if (SSL_CONNECTION_IS_TLS13(s)) { | 
1812  |  |         /*  | 
1813  |  |          * ec_point_formats extension is not used in TLSv1.3 so we ignore  | 
1814  |  |          * this check.  | 
1815  |  |          */  | 
1816  | 0  |         return 1;  | 
1817  | 0  |     } else { | 
1818  | 0  |         int field_type = EVP_PKEY_get_field_type(pkey);  | 
1819  |  | 
  | 
1820  | 0  |         if (field_type == NID_X9_62_prime_field)  | 
1821  | 0  |             comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;  | 
1822  | 0  |         else if (field_type == NID_X9_62_characteristic_two_field)  | 
1823  | 0  |             comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;  | 
1824  | 0  |         else  | 
1825  | 0  |             return 0;  | 
1826  | 0  |     }  | 
1827  |  |     /*  | 
1828  |  |      * If point formats extension present check it, otherwise everything is  | 
1829  |  |      * supported (see RFC4492).  | 
1830  |  |      */  | 
1831  | 0  |     if (s->ext.peer_ecpointformats == NULL)  | 
1832  | 0  |         return 1;  | 
1833  |  |  | 
1834  | 0  |     for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { | 
1835  | 0  |         if (s->ext.peer_ecpointformats[i] == comp_id)  | 
1836  | 0  |             return 1;  | 
1837  | 0  |     }  | 
1838  | 0  |     return 0;  | 
1839  | 0  | }  | 
1840  |  |  | 
1841  |  | /* Return group id of a key */  | 
1842  |  | static uint16_t tls1_get_group_id(EVP_PKEY *pkey)  | 
1843  | 0  | { | 
1844  | 0  |     int curve_nid = ssl_get_EC_curve_nid(pkey);  | 
1845  |  | 
  | 
1846  | 0  |     if (curve_nid == NID_undef)  | 
1847  | 0  |         return 0;  | 
1848  | 0  |     return tls1_nid2group_id(curve_nid);  | 
1849  | 0  | }  | 
1850  |  |  | 
1851  |  | /*  | 
1852  |  |  * Check cert parameters compatible with extensions: currently just checks EC  | 
1853  |  |  * certificates have compatible curves and compression.  | 
1854  |  |  */  | 
1855  |  | static int tls1_check_cert_param(SSL_CONNECTION *s, X509 *x, int check_ee_md)  | 
1856  | 0  | { | 
1857  | 0  |     uint16_t group_id;  | 
1858  | 0  |     EVP_PKEY *pkey;  | 
1859  | 0  |     pkey = X509_get0_pubkey(x);  | 
1860  | 0  |     if (pkey == NULL)  | 
1861  | 0  |         return 0;  | 
1862  |  |     /* If not EC nothing to do */  | 
1863  | 0  |     if (!EVP_PKEY_is_a(pkey, "EC"))  | 
1864  | 0  |         return 1;  | 
1865  |  |     /* Check compression */  | 
1866  | 0  |     if (!tls1_check_pkey_comp(s, pkey))  | 
1867  | 0  |         return 0;  | 
1868  | 0  |     group_id = tls1_get_group_id(pkey);  | 
1869  |  |     /*  | 
1870  |  |      * For a server we allow the certificate to not be in our list of supported  | 
1871  |  |      * groups.  | 
1872  |  |      */  | 
1873  | 0  |     if (!tls1_check_group_id(s, group_id, !s->server))  | 
1874  | 0  |         return 0;  | 
1875  |  |     /*  | 
1876  |  |      * Special case for suite B. We *MUST* sign using SHA256+P-256 or  | 
1877  |  |      * SHA384+P-384.  | 
1878  |  |      */  | 
1879  | 0  |     if (check_ee_md && tls1_suiteb(s)) { | 
1880  | 0  |         int check_md;  | 
1881  | 0  |         size_t i;  | 
1882  |  |  | 
1883  |  |         /* Check to see we have necessary signing algorithm */  | 
1884  | 0  |         if (group_id == OSSL_TLS_GROUP_ID_secp256r1)  | 
1885  | 0  |             check_md = NID_ecdsa_with_SHA256;  | 
1886  | 0  |         else if (group_id == OSSL_TLS_GROUP_ID_secp384r1)  | 
1887  | 0  |             check_md = NID_ecdsa_with_SHA384;  | 
1888  | 0  |         else  | 
1889  | 0  |             return 0;           /* Should never happen */  | 
1890  | 0  |         for (i = 0; i < s->shared_sigalgslen; i++) { | 
1891  | 0  |             if (check_md == s->shared_sigalgs[i]->sigandhash)  | 
1892  | 0  |                 return 1;  | 
1893  | 0  |         }  | 
1894  | 0  |         return 0;  | 
1895  | 0  |     }  | 
1896  | 0  |     return 1;  | 
1897  | 0  | }  | 
1898  |  |  | 
1899  |  | /*  | 
1900  |  |  * tls1_check_ec_tmp_key - Check EC temporary key compatibility  | 
1901  |  |  * @s: SSL connection  | 
1902  |  |  * @cid: Cipher ID we're considering using  | 
1903  |  |  *  | 
1904  |  |  * Checks that the kECDHE cipher suite we're considering using  | 
1905  |  |  * is compatible with the client extensions.  | 
1906  |  |  *  | 
1907  |  |  * Returns 0 when the cipher can't be used or 1 when it can.  | 
1908  |  |  */  | 
1909  |  | int tls1_check_ec_tmp_key(SSL_CONNECTION *s, unsigned long cid)  | 
1910  | 0  | { | 
1911  |  |     /* If not Suite B just need a shared group */  | 
1912  | 0  |     if (!tls1_suiteb(s))  | 
1913  | 0  |         return tls1_shared_group(s, 0) != 0;  | 
1914  |  |     /*  | 
1915  |  |      * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other  | 
1916  |  |      * curves permitted.  | 
1917  |  |      */  | 
1918  | 0  |     if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)  | 
1919  | 0  |         return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp256r1, 1);  | 
1920  | 0  |     if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)  | 
1921  | 0  |         return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp384r1, 1);  | 
1922  |  |  | 
1923  | 0  |     return 0;  | 
1924  | 0  | }  | 
1925  |  |  | 
1926  |  | /* Default sigalg schemes */  | 
1927  |  | static const uint16_t tls12_sigalgs[] = { | 
1928  |  |     TLSEXT_SIGALG_mldsa65,  | 
1929  |  |     TLSEXT_SIGALG_mldsa87,  | 
1930  |  |     TLSEXT_SIGALG_mldsa44,  | 
1931  |  |     TLSEXT_SIGALG_ecdsa_secp256r1_sha256,  | 
1932  |  |     TLSEXT_SIGALG_ecdsa_secp384r1_sha384,  | 
1933  |  |     TLSEXT_SIGALG_ecdsa_secp521r1_sha512,  | 
1934  |  |     TLSEXT_SIGALG_ed25519,  | 
1935  |  |     TLSEXT_SIGALG_ed448,  | 
1936  |  |     TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256,  | 
1937  |  |     TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384,  | 
1938  |  |     TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512,  | 
1939  |  |  | 
1940  |  |     TLSEXT_SIGALG_rsa_pss_pss_sha256,  | 
1941  |  |     TLSEXT_SIGALG_rsa_pss_pss_sha384,  | 
1942  |  |     TLSEXT_SIGALG_rsa_pss_pss_sha512,  | 
1943  |  |     TLSEXT_SIGALG_rsa_pss_rsae_sha256,  | 
1944  |  |     TLSEXT_SIGALG_rsa_pss_rsae_sha384,  | 
1945  |  |     TLSEXT_SIGALG_rsa_pss_rsae_sha512,  | 
1946  |  |  | 
1947  |  |     TLSEXT_SIGALG_rsa_pkcs1_sha256,  | 
1948  |  |     TLSEXT_SIGALG_rsa_pkcs1_sha384,  | 
1949  |  |     TLSEXT_SIGALG_rsa_pkcs1_sha512,  | 
1950  |  |  | 
1951  |  |     TLSEXT_SIGALG_ecdsa_sha224,  | 
1952  |  |     TLSEXT_SIGALG_ecdsa_sha1,  | 
1953  |  |  | 
1954  |  |     TLSEXT_SIGALG_rsa_pkcs1_sha224,  | 
1955  |  |     TLSEXT_SIGALG_rsa_pkcs1_sha1,  | 
1956  |  |  | 
1957  |  |     TLSEXT_SIGALG_dsa_sha224,  | 
1958  |  |     TLSEXT_SIGALG_dsa_sha1,  | 
1959  |  |  | 
1960  |  |     TLSEXT_SIGALG_dsa_sha256,  | 
1961  |  |     TLSEXT_SIGALG_dsa_sha384,  | 
1962  |  |     TLSEXT_SIGALG_dsa_sha512,  | 
1963  |  |  | 
1964  |  | #ifndef OPENSSL_NO_GOST  | 
1965  |  |     TLSEXT_SIGALG_gostr34102012_256_intrinsic,  | 
1966  |  |     TLSEXT_SIGALG_gostr34102012_512_intrinsic,  | 
1967  |  |     TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,  | 
1968  |  |     TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,  | 
1969  |  |     TLSEXT_SIGALG_gostr34102001_gostr3411,  | 
1970  |  | #endif  | 
1971  |  | };  | 
1972  |  |  | 
1973  |  |  | 
1974  |  | static const uint16_t suiteb_sigalgs[] = { | 
1975  |  |     TLSEXT_SIGALG_ecdsa_secp256r1_sha256,  | 
1976  |  |     TLSEXT_SIGALG_ecdsa_secp384r1_sha384  | 
1977  |  | };  | 
1978  |  |  | 
1979  |  | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { | 
1980  |  |     {TLSEXT_SIGALG_ecdsa_secp256r1_sha256_name, | 
1981  |  |      "ECDSA+SHA256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,  | 
1982  |  |      NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
1983  |  |      NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1, 0,  | 
1984  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
1985  |  |     {TLSEXT_SIGALG_ecdsa_secp384r1_sha384_name, | 
1986  |  |      "ECDSA+SHA384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,  | 
1987  |  |      NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
1988  |  |      NID_ecdsa_with_SHA384, NID_secp384r1, 1, 0,  | 
1989  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
1990  |  |     {TLSEXT_SIGALG_ecdsa_secp521r1_sha512_name, | 
1991  |  |      "ECDSA+SHA512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,  | 
1992  |  |      NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
1993  |  |      NID_ecdsa_with_SHA512, NID_secp521r1, 1, 0,  | 
1994  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
1995  |  |  | 
1996  |  |     {TLSEXT_SIGALG_ed25519_name, | 
1997  |  |      NULL, TLSEXT_SIGALG_ed25519,  | 
1998  |  |      NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,  | 
1999  |  |      NID_undef, NID_undef, 1, 0,  | 
2000  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2001  |  |     {TLSEXT_SIGALG_ed448_name, | 
2002  |  |      NULL, TLSEXT_SIGALG_ed448,  | 
2003  |  |      NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448,  | 
2004  |  |      NID_undef, NID_undef, 1, 0,  | 
2005  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2006  |  |  | 
2007  |  |     {TLSEXT_SIGALG_ecdsa_sha224_name, | 
2008  |  |      "ECDSA+SHA224", TLSEXT_SIGALG_ecdsa_sha224,  | 
2009  |  |      NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
2010  |  |      NID_ecdsa_with_SHA224, NID_undef, 1, 0,  | 
2011  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2012  |  |     {TLSEXT_SIGALG_ecdsa_sha1_name, | 
2013  |  |      "ECDSA+SHA1", TLSEXT_SIGALG_ecdsa_sha1,  | 
2014  |  |      NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
2015  |  |      NID_ecdsa_with_SHA1, NID_undef, 1, 0,  | 
2016  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2017  |  |  | 
2018  |  |     {TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_name, | 
2019  |  |      TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_alias,  | 
2020  |  |      TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256,  | 
2021  |  |      NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
2022  |  |      NID_ecdsa_with_SHA256, NID_brainpoolP256r1, 1, 0,  | 
2023  |  |      TLS1_3_VERSION, 0, -1, -1},  | 
2024  |  |     {TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_name, | 
2025  |  |      TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_alias,  | 
2026  |  |      TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384,  | 
2027  |  |      NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
2028  |  |      NID_ecdsa_with_SHA384, NID_brainpoolP384r1, 1, 0,  | 
2029  |  |      TLS1_3_VERSION, 0, -1, -1},  | 
2030  |  |     {TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_name, | 
2031  |  |      TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_alias,  | 
2032  |  |      TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512,  | 
2033  |  |      NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,  | 
2034  |  |      NID_ecdsa_with_SHA512, NID_brainpoolP512r1, 1, 0,  | 
2035  |  |      TLS1_3_VERSION, 0, -1, -1},  | 
2036  |  |  | 
2037  |  |     {TLSEXT_SIGALG_rsa_pss_rsae_sha256_name, | 
2038  |  |      "PSS+SHA256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,  | 
2039  |  |      NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,  | 
2040  |  |      NID_undef, NID_undef, 1, 0,  | 
2041  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2042  |  |     {TLSEXT_SIGALG_rsa_pss_rsae_sha384_name, | 
2043  |  |      "PSS+SHA384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,  | 
2044  |  |      NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,  | 
2045  |  |      NID_undef, NID_undef, 1, 0,  | 
2046  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2047  |  |     {TLSEXT_SIGALG_rsa_pss_rsae_sha512_name, | 
2048  |  |      "PSS+SHA512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,  | 
2049  |  |      NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,  | 
2050  |  |      NID_undef, NID_undef, 1, 0,  | 
2051  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2052  |  |  | 
2053  |  |     {TLSEXT_SIGALG_rsa_pss_pss_sha256_name, | 
2054  |  |      NULL, TLSEXT_SIGALG_rsa_pss_pss_sha256,  | 
2055  |  |      NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,  | 
2056  |  |      NID_undef, NID_undef, 1, 0,  | 
2057  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2058  |  |     {TLSEXT_SIGALG_rsa_pss_pss_sha384_name, | 
2059  |  |      NULL, TLSEXT_SIGALG_rsa_pss_pss_sha384,  | 
2060  |  |      NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,  | 
2061  |  |      NID_undef, NID_undef, 1, 0,  | 
2062  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2063  |  |     {TLSEXT_SIGALG_rsa_pss_pss_sha512_name, | 
2064  |  |      NULL, TLSEXT_SIGALG_rsa_pss_pss_sha512,  | 
2065  |  |      NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,  | 
2066  |  |      NID_undef, NID_undef, 1, 0,  | 
2067  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2068  |  |  | 
2069  |  |     {TLSEXT_SIGALG_rsa_pkcs1_sha256_name, | 
2070  |  |      "RSA+SHA256", TLSEXT_SIGALG_rsa_pkcs1_sha256,  | 
2071  |  |      NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,  | 
2072  |  |      NID_sha256WithRSAEncryption, NID_undef, 1, 0,  | 
2073  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2074  |  |     {TLSEXT_SIGALG_rsa_pkcs1_sha384_name, | 
2075  |  |      "RSA+SHA384", TLSEXT_SIGALG_rsa_pkcs1_sha384,  | 
2076  |  |      NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,  | 
2077  |  |      NID_sha384WithRSAEncryption, NID_undef, 1, 0,  | 
2078  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2079  |  |     {TLSEXT_SIGALG_rsa_pkcs1_sha512_name, | 
2080  |  |      "RSA+SHA512", TLSEXT_SIGALG_rsa_pkcs1_sha512,  | 
2081  |  |      NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,  | 
2082  |  |      NID_sha512WithRSAEncryption, NID_undef, 1, 0,  | 
2083  |  |      TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0},  | 
2084  |  |  | 
2085  |  |     {TLSEXT_SIGALG_rsa_pkcs1_sha224_name, | 
2086  |  |      "RSA+SHA224", TLSEXT_SIGALG_rsa_pkcs1_sha224,  | 
2087  |  |      NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,  | 
2088  |  |      NID_sha224WithRSAEncryption, NID_undef, 1, 0,  | 
2089  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2090  |  |     {TLSEXT_SIGALG_rsa_pkcs1_sha1_name, | 
2091  |  |      "RSA+SHA1", TLSEXT_SIGALG_rsa_pkcs1_sha1,  | 
2092  |  |      NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,  | 
2093  |  |      NID_sha1WithRSAEncryption, NID_undef, 1, 0,  | 
2094  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2095  |  |  | 
2096  |  |     {TLSEXT_SIGALG_dsa_sha256_name, | 
2097  |  |      "DSA+SHA256", TLSEXT_SIGALG_dsa_sha256,  | 
2098  |  |      NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,  | 
2099  |  |      NID_dsa_with_SHA256, NID_undef, 1, 0,  | 
2100  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2101  |  |     {TLSEXT_SIGALG_dsa_sha384_name, | 
2102  |  |      "DSA+SHA384", TLSEXT_SIGALG_dsa_sha384,  | 
2103  |  |      NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,  | 
2104  |  |      NID_undef, NID_undef, 1, 0,  | 
2105  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2106  |  |     {TLSEXT_SIGALG_dsa_sha512_name, | 
2107  |  |      "DSA+SHA512", TLSEXT_SIGALG_dsa_sha512,  | 
2108  |  |      NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,  | 
2109  |  |      NID_undef, NID_undef, 1, 0,  | 
2110  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2111  |  |     {TLSEXT_SIGALG_dsa_sha224_name, | 
2112  |  |      "DSA+SHA224", TLSEXT_SIGALG_dsa_sha224,  | 
2113  |  |      NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,  | 
2114  |  |      NID_undef, NID_undef, 1, 0,  | 
2115  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2116  |  |     {TLSEXT_SIGALG_dsa_sha1_name, | 
2117  |  |      "DSA+SHA1", TLSEXT_SIGALG_dsa_sha1,  | 
2118  |  |      NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,  | 
2119  |  |      NID_dsaWithSHA1, NID_undef, 1, 0,  | 
2120  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2121  |  |  | 
2122  |  | #ifndef OPENSSL_NO_GOST  | 
2123  |  |     {TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ | 
2124  |  |      TLSEXT_SIGALG_gostr34102012_256_intrinsic_name,  | 
2125  |  |      TLSEXT_SIGALG_gostr34102012_256_intrinsic,  | 
2126  |  |      NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,  | 
2127  |  |      NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,  | 
2128  |  |      NID_undef, NID_undef, 1, 0,  | 
2129  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2130  |  |     {TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ | 
2131  |  |      TLSEXT_SIGALG_gostr34102012_256_intrinsic_name,  | 
2132  |  |      TLSEXT_SIGALG_gostr34102012_512_intrinsic,  | 
2133  |  |      NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,  | 
2134  |  |      NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,  | 
2135  |  |      NID_undef, NID_undef, 1, 0,  | 
2136  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2137  |  |  | 
2138  |  |     {TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256_name, | 
2139  |  |      NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,  | 
2140  |  |      NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,  | 
2141  |  |      NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,  | 
2142  |  |      NID_undef, NID_undef, 1, 0,  | 
2143  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2144  |  |     {TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512_name, | 
2145  |  |      NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,  | 
2146  |  |      NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,  | 
2147  |  |      NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,  | 
2148  |  |      NID_undef, NID_undef, 1, 0,  | 
2149  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2150  |  |     {TLSEXT_SIGALG_gostr34102001_gostr3411_name, | 
2151  |  |      NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,  | 
2152  |  |      NID_id_GostR3411_94, SSL_MD_GOST94_IDX,  | 
2153  |  |      NID_id_GostR3410_2001, SSL_PKEY_GOST01,  | 
2154  |  |      NID_undef, NID_undef, 1, 0,  | 
2155  |  |      TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION},  | 
2156  |  | #endif  | 
2157  |  | };  | 
2158  |  | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */  | 
2159  |  | static const SIGALG_LOOKUP legacy_rsa_sigalg = { | 
2160  |  |     "rsa_pkcs1_md5_sha1", NULL, 0,  | 
2161  |  |      NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,  | 
2162  |  |      EVP_PKEY_RSA, SSL_PKEY_RSA,  | 
2163  |  |      NID_undef, NID_undef, 1, 0,  | 
2164  |  |      TLS1_VERSION, TLS1_2_VERSION, DTLS1_VERSION, DTLS1_2_VERSION  | 
2165  |  | };  | 
2166  |  |  | 
2167  |  | /*  | 
2168  |  |  * Default signature algorithm values used if signature algorithms not present.  | 
2169  |  |  * From RFC5246. Note: order must match certificate index order.  | 
2170  |  |  */  | 
2171  |  | static const uint16_t tls_default_sigalg[] = { | 
2172  |  |     TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */  | 
2173  |  |     0, /* SSL_PKEY_RSA_PSS_SIGN */  | 
2174  |  |     TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */  | 
2175  |  |     TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */  | 
2176  |  |     TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */  | 
2177  |  |     TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */  | 
2178  |  |     TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */  | 
2179  |  |     0, /* SSL_PKEY_ED25519 */  | 
2180  |  |     0, /* SSL_PKEY_ED448 */  | 
2181  |  | };  | 
2182  |  |  | 
2183  |  | int ssl_setup_sigalgs(SSL_CTX *ctx)  | 
2184  | 0  | { | 
2185  | 0  |     size_t i, cache_idx, sigalgs_len, enabled;  | 
2186  | 0  |     const SIGALG_LOOKUP *lu;  | 
2187  | 0  |     SIGALG_LOOKUP *cache = NULL;  | 
2188  | 0  |     uint16_t *tls12_sigalgs_list = NULL;  | 
2189  | 0  |     EVP_PKEY *tmpkey = EVP_PKEY_new();  | 
2190  | 0  |     int istls;  | 
2191  | 0  |     int ret = 0;  | 
2192  |  | 
  | 
2193  | 0  |     if (ctx == NULL)  | 
2194  | 0  |         goto err;  | 
2195  |  |  | 
2196  | 0  |     istls = !SSL_CTX_IS_DTLS(ctx);  | 
2197  |  | 
  | 
2198  | 0  |     sigalgs_len = OSSL_NELEM(sigalg_lookup_tbl) + ctx->sigalg_list_len;  | 
2199  |  | 
  | 
2200  | 0  |     cache = OPENSSL_calloc(sigalgs_len, sizeof(const SIGALG_LOOKUP));  | 
2201  | 0  |     if (cache == NULL || tmpkey == NULL)  | 
2202  | 0  |         goto err;  | 
2203  |  |  | 
2204  | 0  |     tls12_sigalgs_list = OPENSSL_calloc(sigalgs_len, sizeof(uint16_t));  | 
2205  | 0  |     if (tls12_sigalgs_list == NULL)  | 
2206  | 0  |         goto err;  | 
2207  |  |  | 
2208  | 0  |     ERR_set_mark();  | 
2209  |  |     /* First fill cache and tls12_sigalgs list from legacy algorithm list */  | 
2210  | 0  |     for (i = 0, lu = sigalg_lookup_tbl;  | 
2211  | 0  |          i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { | 
2212  | 0  |         EVP_PKEY_CTX *pctx;  | 
2213  |  | 
  | 
2214  | 0  |         cache[i] = *lu;  | 
2215  |  |  | 
2216  |  |         /*  | 
2217  |  |          * Check hash is available.  | 
2218  |  |          * This test is not perfect. A provider could have support  | 
2219  |  |          * for a signature scheme, but not a particular hash. However the hash  | 
2220  |  |          * could be available from some other loaded provider. In that case it  | 
2221  |  |          * could be that the signature is available, and the hash is available  | 
2222  |  |          * independently - but not as a combination. We ignore this for now.  | 
2223  |  |          */  | 
2224  | 0  |         if (lu->hash != NID_undef  | 
2225  | 0  |                 && ctx->ssl_digest_methods[lu->hash_idx] == NULL) { | 
2226  | 0  |             cache[i].available = 0;  | 
2227  | 0  |             continue;  | 
2228  | 0  |         }  | 
2229  |  |  | 
2230  | 0  |         if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { | 
2231  | 0  |             cache[i].available = 0;  | 
2232  | 0  |             continue;  | 
2233  | 0  |         }  | 
2234  | 0  |         pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq);  | 
2235  |  |         /* If unable to create pctx we assume the sig algorithm is unavailable */  | 
2236  | 0  |         if (pctx == NULL)  | 
2237  | 0  |             cache[i].available = 0;  | 
2238  | 0  |         EVP_PKEY_CTX_free(pctx);  | 
2239  | 0  |     }  | 
2240  |  |  | 
2241  |  |     /* Now complete cache and tls12_sigalgs list with provider sig information */  | 
2242  | 0  |     cache_idx = OSSL_NELEM(sigalg_lookup_tbl);  | 
2243  | 0  |     for (i = 0; i < ctx->sigalg_list_len; i++) { | 
2244  | 0  |         TLS_SIGALG_INFO si = ctx->sigalg_list[i];  | 
2245  | 0  |         cache[cache_idx].name = si.name;  | 
2246  | 0  |         cache[cache_idx].name12 = si.sigalg_name;  | 
2247  | 0  |         cache[cache_idx].sigalg = si.code_point;  | 
2248  | 0  |         tls12_sigalgs_list[cache_idx] = si.code_point;  | 
2249  | 0  |         cache[cache_idx].hash = si.hash_name?OBJ_txt2nid(si.hash_name):NID_undef;  | 
2250  | 0  |         cache[cache_idx].hash_idx = ssl_get_md_idx(cache[cache_idx].hash);  | 
2251  | 0  |         cache[cache_idx].sig = OBJ_txt2nid(si.sigalg_name);  | 
2252  | 0  |         cache[cache_idx].sig_idx = (int)(i + SSL_PKEY_NUM);  | 
2253  | 0  |         cache[cache_idx].sigandhash = OBJ_txt2nid(si.sigalg_name);  | 
2254  | 0  |         cache[cache_idx].curve = NID_undef;  | 
2255  | 0  |         cache[cache_idx].mintls = TLS1_3_VERSION;  | 
2256  | 0  |         cache[cache_idx].maxtls = TLS1_3_VERSION;  | 
2257  | 0  |         cache[cache_idx].mindtls = -1;  | 
2258  | 0  |         cache[cache_idx].maxdtls = -1;  | 
2259  |  |         /* Compatibility with TLS 1.3 is checked on load */  | 
2260  | 0  |         cache[cache_idx].available = istls;  | 
2261  | 0  |         cache[cache_idx].advertise = 0;  | 
2262  | 0  |         cache_idx++;  | 
2263  | 0  |     }  | 
2264  | 0  |     ERR_pop_to_mark();  | 
2265  |  | 
  | 
2266  | 0  |     enabled = 0;  | 
2267  | 0  |     for (i = 0; i < OSSL_NELEM(tls12_sigalgs); ++i) { | 
2268  | 0  |         SIGALG_LOOKUP *ent = cache;  | 
2269  | 0  |         size_t j;  | 
2270  |  | 
  | 
2271  | 0  |         for (j = 0; j < sigalgs_len; ent++, j++) { | 
2272  | 0  |             if (ent->sigalg != tls12_sigalgs[i])  | 
2273  | 0  |                 continue;  | 
2274  |  |             /* Dedup by marking cache entry as default enabled. */  | 
2275  | 0  |             if (ent->available && !ent->advertise) { | 
2276  | 0  |                 ent->advertise = 1;  | 
2277  | 0  |                 tls12_sigalgs_list[enabled++] = tls12_sigalgs[i];  | 
2278  | 0  |             }  | 
2279  | 0  |             break;  | 
2280  | 0  |         }  | 
2281  | 0  |     }  | 
2282  |  |  | 
2283  |  |     /* Append any provider sigalgs not yet handled */  | 
2284  | 0  |     for (i = OSSL_NELEM(sigalg_lookup_tbl); i < sigalgs_len; ++i) { | 
2285  | 0  |         SIGALG_LOOKUP *ent = &cache[i];  | 
2286  |  | 
  | 
2287  | 0  |         if (ent->available && !ent->advertise)  | 
2288  | 0  |             tls12_sigalgs_list[enabled++] = ent->sigalg;  | 
2289  | 0  |     }  | 
2290  |  | 
  | 
2291  | 0  |     ctx->sigalg_lookup_cache = cache;  | 
2292  | 0  |     ctx->sigalg_lookup_cache_len = sigalgs_len;  | 
2293  | 0  |     ctx->tls12_sigalgs = tls12_sigalgs_list;  | 
2294  | 0  |     ctx->tls12_sigalgs_len = enabled;  | 
2295  | 0  |     cache = NULL;  | 
2296  | 0  |     tls12_sigalgs_list = NULL;  | 
2297  |  | 
  | 
2298  | 0  |     ret = 1;  | 
2299  | 0  |  err:  | 
2300  | 0  |     OPENSSL_free(cache);  | 
2301  | 0  |     OPENSSL_free(tls12_sigalgs_list);  | 
2302  | 0  |     EVP_PKEY_free(tmpkey);  | 
2303  | 0  |     return ret;  | 
2304  | 0  | }  | 
2305  |  |  | 
2306  | 0  | #define SIGLEN_BUF_INCREMENT 100  | 
2307  |  |  | 
2308  |  | char *SSL_get1_builtin_sigalgs(OSSL_LIB_CTX *libctx)  | 
2309  | 0  | { | 
2310  | 0  |     size_t i, maxretlen = SIGLEN_BUF_INCREMENT;  | 
2311  | 0  |     const SIGALG_LOOKUP *lu;  | 
2312  | 0  |     EVP_PKEY *tmpkey = EVP_PKEY_new();  | 
2313  | 0  |     char *retval = OPENSSL_malloc(maxretlen);  | 
2314  |  | 
  | 
2315  | 0  |     if (retval == NULL)  | 
2316  | 0  |         return NULL;  | 
2317  |  |  | 
2318  |  |     /* ensure retval string is NUL terminated */  | 
2319  | 0  |     retval[0] = (char)0;  | 
2320  |  | 
  | 
2321  | 0  |     for (i = 0, lu = sigalg_lookup_tbl;  | 
2322  | 0  |          i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { | 
2323  | 0  |         EVP_PKEY_CTX *pctx;  | 
2324  | 0  |         int enabled = 1;  | 
2325  |  | 
  | 
2326  | 0  |         ERR_set_mark();  | 
2327  |  |         /* Check hash is available in some provider. */  | 
2328  | 0  |         if (lu->hash != NID_undef) { | 
2329  | 0  |             EVP_MD *hash = EVP_MD_fetch(libctx, OBJ_nid2ln(lu->hash), NULL);  | 
2330  |  |  | 
2331  |  |             /* If unable to create we assume the hash algorithm is unavailable */  | 
2332  | 0  |             if (hash == NULL) { | 
2333  | 0  |                 enabled = 0;  | 
2334  | 0  |                 ERR_pop_to_mark();  | 
2335  | 0  |                 continue;  | 
2336  | 0  |             }  | 
2337  | 0  |             EVP_MD_free(hash);  | 
2338  | 0  |         }  | 
2339  |  |  | 
2340  | 0  |         if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { | 
2341  | 0  |             enabled = 0;  | 
2342  | 0  |             ERR_pop_to_mark();  | 
2343  | 0  |             continue;  | 
2344  | 0  |         }  | 
2345  | 0  |         pctx = EVP_PKEY_CTX_new_from_pkey(libctx, tmpkey, NULL);  | 
2346  |  |         /* If unable to create pctx we assume the sig algorithm is unavailable */  | 
2347  | 0  |         if (pctx == NULL)  | 
2348  | 0  |             enabled = 0;  | 
2349  | 0  |         ERR_pop_to_mark();  | 
2350  | 0  |         EVP_PKEY_CTX_free(pctx);  | 
2351  |  | 
  | 
2352  | 0  |         if (enabled) { | 
2353  | 0  |             const char *sa = lu->name;  | 
2354  |  | 
  | 
2355  | 0  |             if (sa != NULL) { | 
2356  | 0  |                 if (strlen(sa) + strlen(retval) + 1 >= maxretlen) { | 
2357  | 0  |                     char *tmp;  | 
2358  |  | 
  | 
2359  | 0  |                     maxretlen += SIGLEN_BUF_INCREMENT;  | 
2360  | 0  |                     tmp = OPENSSL_realloc(retval, maxretlen);  | 
2361  | 0  |                     if (tmp == NULL) { | 
2362  | 0  |                         OPENSSL_free(retval);  | 
2363  | 0  |                         return NULL;  | 
2364  | 0  |                     }  | 
2365  | 0  |                     retval = tmp;  | 
2366  | 0  |                 }  | 
2367  | 0  |                 if (strlen(retval) > 0)  | 
2368  | 0  |                     OPENSSL_strlcat(retval, ":", maxretlen);  | 
2369  | 0  |                 OPENSSL_strlcat(retval, sa, maxretlen);  | 
2370  | 0  |             } else { | 
2371  |  |                 /* lu->name must not be NULL */  | 
2372  | 0  |                 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);  | 
2373  | 0  |             }  | 
2374  | 0  |         }  | 
2375  | 0  |     }  | 
2376  |  |  | 
2377  | 0  |     EVP_PKEY_free(tmpkey);  | 
2378  | 0  |     return retval;  | 
2379  | 0  | }  | 
2380  |  |  | 
2381  |  | /* Lookup TLS signature algorithm */  | 
2382  |  | static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL_CTX *ctx,  | 
2383  |  |                                                uint16_t sigalg)  | 
2384  | 0  | { | 
2385  | 0  |     size_t i;  | 
2386  | 0  |     const SIGALG_LOOKUP *lu = ctx->sigalg_lookup_cache;  | 
2387  |  | 
  | 
2388  | 0  |     for (i = 0; i < ctx->sigalg_lookup_cache_len; lu++, i++) { | 
2389  | 0  |         if (lu->sigalg == sigalg) { | 
2390  | 0  |             if (!lu->available)  | 
2391  | 0  |                 return NULL;  | 
2392  | 0  |             return lu;  | 
2393  | 0  |         }  | 
2394  | 0  |     }  | 
2395  | 0  |     return NULL;  | 
2396  | 0  | }  | 
2397  |  |  | 
2398  |  | /* Lookup hash: return 0 if invalid or not enabled */  | 
2399  |  | int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd)  | 
2400  | 0  | { | 
2401  | 0  |     const EVP_MD *md;  | 
2402  |  | 
  | 
2403  | 0  |     if (lu == NULL)  | 
2404  | 0  |         return 0;  | 
2405  |  |     /* lu->hash == NID_undef means no associated digest */  | 
2406  | 0  |     if (lu->hash == NID_undef) { | 
2407  | 0  |         md = NULL;  | 
2408  | 0  |     } else { | 
2409  | 0  |         md = ssl_md(ctx, lu->hash_idx);  | 
2410  | 0  |         if (md == NULL)  | 
2411  | 0  |             return 0;  | 
2412  | 0  |     }  | 
2413  | 0  |     if (pmd)  | 
2414  | 0  |         *pmd = md;  | 
2415  | 0  |     return 1;  | 
2416  | 0  | }  | 
2417  |  |  | 
2418  |  | /*  | 
2419  |  |  * Check if key is large enough to generate RSA-PSS signature.  | 
2420  |  |  *  | 
2421  |  |  * The key must greater than or equal to 2 * hash length + 2.  | 
2422  |  |  * SHA512 has a hash length of 64 bytes, which is incompatible  | 
2423  |  |  * with a 128 byte (1024 bit) key.  | 
2424  |  |  */  | 
2425  | 0  | #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_get_size(md) + 2)  | 
2426  |  | static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey,  | 
2427  |  |                                       const SIGALG_LOOKUP *lu)  | 
2428  | 0  | { | 
2429  | 0  |     const EVP_MD *md;  | 
2430  |  | 
  | 
2431  | 0  |     if (pkey == NULL)  | 
2432  | 0  |         return 0;  | 
2433  | 0  |     if (!tls1_lookup_md(ctx, lu, &md) || md == NULL)  | 
2434  | 0  |         return 0;  | 
2435  | 0  |     if (EVP_MD_get_size(md) <= 0)  | 
2436  | 0  |         return 0;  | 
2437  | 0  |     if (EVP_PKEY_get_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md))  | 
2438  | 0  |         return 0;  | 
2439  | 0  |     return 1;  | 
2440  | 0  | }  | 
2441  |  |  | 
2442  |  | /*  | 
2443  |  |  * Returns a signature algorithm when the peer did not send a list of supported  | 
2444  |  |  * signature algorithms. The signature algorithm is fixed for the certificate  | 
2445  |  |  * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the  | 
2446  |  |  * certificate type from |s| will be used.  | 
2447  |  |  * Returns the signature algorithm to use, or NULL on error.  | 
2448  |  |  */  | 
2449  |  | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s,  | 
2450  |  |                                                    int idx)  | 
2451  | 0  | { | 
2452  | 0  |     if (idx == -1) { | 
2453  | 0  |         if (s->server) { | 
2454  | 0  |             size_t i;  | 
2455  |  |  | 
2456  |  |             /* Work out index corresponding to ciphersuite */  | 
2457  | 0  |             for (i = 0; i < s->ssl_pkey_num; i++) { | 
2458  | 0  |                 const SSL_CERT_LOOKUP *clu  | 
2459  | 0  |                     = ssl_cert_lookup_by_idx(i, SSL_CONNECTION_GET_CTX(s));  | 
2460  |  | 
  | 
2461  | 0  |                 if (clu == NULL)  | 
2462  | 0  |                     continue;  | 
2463  | 0  |                 if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) { | 
2464  | 0  |                     idx = (int)i;  | 
2465  | 0  |                     break;  | 
2466  | 0  |                 }  | 
2467  | 0  |             }  | 
2468  |  |  | 
2469  |  |             /*  | 
2470  |  |              * Some GOST ciphersuites allow more than one signature algorithms  | 
2471  |  |              * */  | 
2472  | 0  |             if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) { | 
2473  | 0  |                 int real_idx;  | 
2474  |  | 
  | 
2475  | 0  |                 for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01;  | 
2476  | 0  |                      real_idx--) { | 
2477  | 0  |                     if (s->cert->pkeys[real_idx].privatekey != NULL) { | 
2478  | 0  |                         idx = real_idx;  | 
2479  | 0  |                         break;  | 
2480  | 0  |                     }  | 
2481  | 0  |                 }  | 
2482  | 0  |             }  | 
2483  |  |             /*  | 
2484  |  |              * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used  | 
2485  |  |              * with new (aGOST12-only) ciphersuites, we should find out which one is available really.  | 
2486  |  |              */  | 
2487  | 0  |             else if (idx == SSL_PKEY_GOST12_256) { | 
2488  | 0  |                 int real_idx;  | 
2489  |  | 
  | 
2490  | 0  |                 for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256;  | 
2491  | 0  |                      real_idx--) { | 
2492  | 0  |                      if (s->cert->pkeys[real_idx].privatekey != NULL) { | 
2493  | 0  |                          idx = real_idx;  | 
2494  | 0  |                          break;  | 
2495  | 0  |                      }  | 
2496  | 0  |                 }  | 
2497  | 0  |             }  | 
2498  | 0  |         } else { | 
2499  | 0  |             idx = (int)(s->cert->key - s->cert->pkeys);  | 
2500  | 0  |         }  | 
2501  | 0  |     }  | 
2502  | 0  |     if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))  | 
2503  | 0  |         return NULL;  | 
2504  |  |  | 
2505  | 0  |     if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { | 
2506  | 0  |         const SIGALG_LOOKUP *lu =  | 
2507  | 0  |             tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s),  | 
2508  | 0  |                                tls_default_sigalg[idx]);  | 
2509  |  | 
  | 
2510  | 0  |         if (lu == NULL)  | 
2511  | 0  |             return NULL;  | 
2512  | 0  |         if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, NULL))  | 
2513  | 0  |             return NULL;  | 
2514  | 0  |         if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))  | 
2515  | 0  |             return NULL;  | 
2516  | 0  |         return lu;  | 
2517  | 0  |     }  | 
2518  | 0  |     if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg))  | 
2519  | 0  |         return NULL;  | 
2520  | 0  |     return &legacy_rsa_sigalg;  | 
2521  | 0  | }  | 
2522  |  | /* Set peer sigalg based key type */  | 
2523  |  | int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey)  | 
2524  | 0  | { | 
2525  | 0  |     size_t idx;  | 
2526  | 0  |     const SIGALG_LOOKUP *lu;  | 
2527  |  | 
  | 
2528  | 0  |     if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL)  | 
2529  | 0  |         return 0;  | 
2530  | 0  |     lu = tls1_get_legacy_sigalg(s, (int)idx);  | 
2531  | 0  |     if (lu == NULL)  | 
2532  | 0  |         return 0;  | 
2533  | 0  |     s->s3.tmp.peer_sigalg = lu;  | 
2534  | 0  |     return 1;  | 
2535  | 0  | }  | 
2536  |  |  | 
2537  |  | size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent, const uint16_t **psigs)  | 
2538  | 0  | { | 
2539  |  |     /*  | 
2540  |  |      * If Suite B mode use Suite B sigalgs only, ignore any other  | 
2541  |  |      * preferences.  | 
2542  |  |      */  | 
2543  | 0  |     switch (tls1_suiteb(s)) { | 
2544  | 0  |     case SSL_CERT_FLAG_SUITEB_128_LOS:  | 
2545  | 0  |         *psigs = suiteb_sigalgs;  | 
2546  | 0  |         return OSSL_NELEM(suiteb_sigalgs);  | 
2547  |  |  | 
2548  | 0  |     case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:  | 
2549  | 0  |         *psigs = suiteb_sigalgs;  | 
2550  | 0  |         return 1;  | 
2551  |  |  | 
2552  | 0  |     case SSL_CERT_FLAG_SUITEB_192_LOS:  | 
2553  | 0  |         *psigs = suiteb_sigalgs + 1;  | 
2554  | 0  |         return 1;  | 
2555  | 0  |     }  | 
2556  |  |     /*  | 
2557  |  |      *  We use client_sigalgs (if not NULL) if we're a server  | 
2558  |  |      *  and sending a certificate request or if we're a client and  | 
2559  |  |      *  determining which shared algorithm to use.  | 
2560  |  |      */  | 
2561  | 0  |     if ((s->server == sent) && s->cert->client_sigalgs != NULL) { | 
2562  | 0  |         *psigs = s->cert->client_sigalgs;  | 
2563  | 0  |         return s->cert->client_sigalgslen;  | 
2564  | 0  |     } else if (s->cert->conf_sigalgs) { | 
2565  | 0  |         *psigs = s->cert->conf_sigalgs;  | 
2566  | 0  |         return s->cert->conf_sigalgslen;  | 
2567  | 0  |     } else { | 
2568  | 0  |         *psigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs;  | 
2569  | 0  |         return SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len;  | 
2570  | 0  |     }  | 
2571  | 0  | }  | 
2572  |  |  | 
2573  |  | /*  | 
2574  |  |  * Called by servers only. Checks that we have a sig alg that supports the  | 
2575  |  |  * specified EC curve.  | 
2576  |  |  */  | 
2577  |  | int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve)  | 
2578  | 0  | { | 
2579  | 0  |    const uint16_t *sigs;  | 
2580  | 0  |    size_t siglen, i;  | 
2581  |  | 
  | 
2582  | 0  |     if (s->cert->conf_sigalgs) { | 
2583  | 0  |         sigs = s->cert->conf_sigalgs;  | 
2584  | 0  |         siglen = s->cert->conf_sigalgslen;  | 
2585  | 0  |     } else { | 
2586  | 0  |         sigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs;  | 
2587  | 0  |         siglen = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len;  | 
2588  | 0  |     }  | 
2589  |  | 
  | 
2590  | 0  |     for (i = 0; i < siglen; i++) { | 
2591  | 0  |         const SIGALG_LOOKUP *lu =  | 
2592  | 0  |             tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sigs[i]);  | 
2593  |  | 
  | 
2594  | 0  |         if (lu == NULL)  | 
2595  | 0  |             continue;  | 
2596  | 0  |         if (lu->sig == EVP_PKEY_EC  | 
2597  | 0  |                 && lu->curve != NID_undef  | 
2598  | 0  |                 && curve == lu->curve)  | 
2599  | 0  |             return 1;  | 
2600  | 0  |     }  | 
2601  |  |  | 
2602  | 0  |     return 0;  | 
2603  | 0  | }  | 
2604  |  |  | 
2605  |  | /*  | 
2606  |  |  * Return the number of security bits for the signature algorithm, or 0 on  | 
2607  |  |  * error.  | 
2608  |  |  */  | 
2609  |  | static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu)  | 
2610  | 0  | { | 
2611  | 0  |     const EVP_MD *md = NULL;  | 
2612  | 0  |     int secbits = 0;  | 
2613  |  | 
  | 
2614  | 0  |     if (!tls1_lookup_md(ctx, lu, &md))  | 
2615  | 0  |         return 0;  | 
2616  | 0  |     if (md != NULL)  | 
2617  | 0  |     { | 
2618  | 0  |         int md_type = EVP_MD_get_type(md);  | 
2619  |  |  | 
2620  |  |         /* Security bits: half digest bits */  | 
2621  | 0  |         secbits = EVP_MD_get_size(md) * 4;  | 
2622  | 0  |         if (secbits <= 0)  | 
2623  | 0  |             return 0;  | 
2624  |  |         /*  | 
2625  |  |          * SHA1 and MD5 are known to be broken. Reduce security bits so that  | 
2626  |  |          * they're no longer accepted at security level 1. The real values don't  | 
2627  |  |          * really matter as long as they're lower than 80, which is our  | 
2628  |  |          * security level 1.  | 
2629  |  |          * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for  | 
2630  |  |          * SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2  | 
2631  |  |          * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf  | 
2632  |  |          * puts a chosen-prefix attack for MD5 at 2^39.  | 
2633  |  |          */  | 
2634  | 0  |         if (md_type == NID_sha1)  | 
2635  | 0  |             secbits = 64;  | 
2636  | 0  |         else if (md_type == NID_md5_sha1)  | 
2637  | 0  |             secbits = 67;  | 
2638  | 0  |         else if (md_type == NID_md5)  | 
2639  | 0  |             secbits = 39;  | 
2640  | 0  |     } else { | 
2641  |  |         /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */  | 
2642  | 0  |         if (lu->sigalg == TLSEXT_SIGALG_ed25519)  | 
2643  | 0  |             secbits = 128;  | 
2644  | 0  |         else if (lu->sigalg == TLSEXT_SIGALG_ed448)  | 
2645  | 0  |             secbits = 224;  | 
2646  | 0  |     }  | 
2647  |  |     /*  | 
2648  |  |      * For provider-based sigalgs we have secbits information available  | 
2649  |  |      * in the (provider-loaded) sigalg_list structure  | 
2650  |  |      */  | 
2651  | 0  |     if ((secbits == 0) && (lu->sig_idx >= SSL_PKEY_NUM)  | 
2652  | 0  |                && ((lu->sig_idx - SSL_PKEY_NUM) < (int)ctx->sigalg_list_len)) { | 
2653  | 0  |         secbits = ctx->sigalg_list[lu->sig_idx - SSL_PKEY_NUM].secbits;  | 
2654  | 0  |     }  | 
2655  | 0  |     return secbits;  | 
2656  | 0  | }  | 
2657  |  |  | 
2658  |  | static int tls_sigalg_compat(SSL_CONNECTION *sc, const SIGALG_LOOKUP *lu)  | 
2659  | 0  | { | 
2660  | 0  |     int minversion, maxversion;  | 
2661  | 0  |     int minproto, maxproto;  | 
2662  |  | 
  | 
2663  | 0  |     if (!lu->available)  | 
2664  | 0  |         return 0;  | 
2665  |  |  | 
2666  | 0  |     if (SSL_CONNECTION_IS_DTLS(sc)) { | 
2667  | 0  |         if (sc->ssl.method->version == DTLS_ANY_VERSION) { | 
2668  | 0  |             minproto = sc->min_proto_version;  | 
2669  | 0  |             maxproto = sc->max_proto_version;  | 
2670  | 0  |         } else { | 
2671  | 0  |             maxproto = minproto = sc->version;  | 
2672  | 0  |         }  | 
2673  | 0  |         minversion = lu->mindtls;  | 
2674  | 0  |         maxversion = lu->maxdtls;  | 
2675  | 0  |     } else { | 
2676  | 0  |         if (sc->ssl.method->version == TLS_ANY_VERSION) { | 
2677  | 0  |             minproto = sc->min_proto_version;  | 
2678  | 0  |             maxproto = sc->max_proto_version;  | 
2679  | 0  |         } else { | 
2680  | 0  |             maxproto = minproto = sc->version;  | 
2681  | 0  |         }  | 
2682  | 0  |         minversion = lu->mintls;  | 
2683  | 0  |         maxversion = lu->maxtls;  | 
2684  | 0  |     }  | 
2685  | 0  |     if (minversion == -1 || maxversion == -1  | 
2686  | 0  |         || (minversion != 0 && maxproto != 0  | 
2687  | 0  |             && ssl_version_cmp(sc, minversion, maxproto) > 0)  | 
2688  | 0  |         || (maxversion != 0 && minproto != 0  | 
2689  | 0  |             && ssl_version_cmp(sc, maxversion, minproto) < 0)  | 
2690  | 0  |         || !tls12_sigalg_allowed(sc, SSL_SECOP_SIGALG_SUPPORTED, lu))  | 
2691  | 0  |         return 0;  | 
2692  | 0  |     return 1;  | 
2693  | 0  | }  | 
2694  |  |  | 
2695  |  | /*  | 
2696  |  |  * Check signature algorithm is consistent with sent supported signature  | 
2697  |  |  * algorithms and if so set relevant digest and signature scheme in  | 
2698  |  |  * s.  | 
2699  |  |  */  | 
2700  |  | int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey)  | 
2701  | 0  | { | 
2702  | 0  |     const uint16_t *sent_sigs;  | 
2703  | 0  |     const EVP_MD *md = NULL;  | 
2704  | 0  |     char sigalgstr[2];  | 
2705  | 0  |     size_t sent_sigslen, i, cidx;  | 
2706  | 0  |     int pkeyid = -1;  | 
2707  | 0  |     const SIGALG_LOOKUP *lu;  | 
2708  | 0  |     int secbits = 0;  | 
2709  |  | 
  | 
2710  | 0  |     pkeyid = EVP_PKEY_get_id(pkey);  | 
2711  |  | 
  | 
2712  | 0  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
2713  |  |         /* Disallow DSA for TLS 1.3 */  | 
2714  | 0  |         if (pkeyid == EVP_PKEY_DSA) { | 
2715  | 0  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2716  | 0  |             return 0;  | 
2717  | 0  |         }  | 
2718  |  |         /* Only allow PSS for TLS 1.3 */  | 
2719  | 0  |         if (pkeyid == EVP_PKEY_RSA)  | 
2720  | 0  |             pkeyid = EVP_PKEY_RSA_PSS;  | 
2721  | 0  |     }  | 
2722  |  |  | 
2723  |  |     /* Is this code point available and compatible with the protocol */  | 
2724  | 0  |     lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sig);  | 
2725  | 0  |     if (lu == NULL || !tls_sigalg_compat(s, lu)) { | 
2726  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2727  | 0  |         return 0;  | 
2728  | 0  |     }  | 
2729  |  |  | 
2730  |  |     /* if this sigalg is loaded, set so far unknown pkeyid to its sig NID */  | 
2731  | 0  |     if (pkeyid == EVP_PKEY_KEYMGMT)  | 
2732  | 0  |         pkeyid = lu->sig;  | 
2733  |  |  | 
2734  |  |     /* Should never happen */  | 
2735  | 0  |     if (pkeyid == -1) { | 
2736  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2737  | 0  |         return -1;  | 
2738  | 0  |     }  | 
2739  |  |  | 
2740  |  |     /*  | 
2741  |  |      * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type  | 
2742  |  |      * is consistent with signature: RSA keys can be used for RSA-PSS  | 
2743  |  |      */  | 
2744  | 0  |     if ((SSL_CONNECTION_IS_TLS13(s)  | 
2745  | 0  |             && (lu->hash == NID_sha1 || lu->hash == NID_sha224))  | 
2746  | 0  |         || (pkeyid != lu->sig  | 
2747  | 0  |         && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { | 
2748  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2749  | 0  |         return 0;  | 
2750  | 0  |     }  | 
2751  |  |     /* Check the sigalg is consistent with the key OID */  | 
2752  | 0  |     if (!ssl_cert_lookup_by_nid(  | 
2753  | 0  |                  (pkeyid == EVP_PKEY_RSA_PSS) ? EVP_PKEY_get_id(pkey) : pkeyid,  | 
2754  | 0  |                  &cidx, SSL_CONNECTION_GET_CTX(s))  | 
2755  | 0  |             || lu->sig_idx != (int)cidx) { | 
2756  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2757  | 0  |         return 0;  | 
2758  | 0  |     }  | 
2759  |  |  | 
2760  | 0  |     if (pkeyid == EVP_PKEY_EC) { | 
2761  |  |  | 
2762  |  |         /* Check point compression is permitted */  | 
2763  | 0  |         if (!tls1_check_pkey_comp(s, pkey)) { | 
2764  | 0  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
2765  | 0  |                      SSL_R_ILLEGAL_POINT_COMPRESSION);  | 
2766  | 0  |             return 0;  | 
2767  | 0  |         }  | 
2768  |  |  | 
2769  |  |         /* For TLS 1.3 or Suite B check curve matches signature algorithm */  | 
2770  | 0  |         if (SSL_CONNECTION_IS_TLS13(s) || tls1_suiteb(s)) { | 
2771  | 0  |             int curve = ssl_get_EC_curve_nid(pkey);  | 
2772  |  | 
  | 
2773  | 0  |             if (lu->curve != NID_undef && curve != lu->curve) { | 
2774  | 0  |                 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);  | 
2775  | 0  |                 return 0;  | 
2776  | 0  |             }  | 
2777  | 0  |         }  | 
2778  | 0  |         if (!SSL_CONNECTION_IS_TLS13(s)) { | 
2779  |  |             /* Check curve matches extensions */  | 
2780  | 0  |             if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { | 
2781  | 0  |                 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);  | 
2782  | 0  |                 return 0;  | 
2783  | 0  |             }  | 
2784  | 0  |             if (tls1_suiteb(s)) { | 
2785  |  |                 /* Check sigalg matches a permissible Suite B value */  | 
2786  | 0  |                 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256  | 
2787  | 0  |                     && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { | 
2788  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
2789  | 0  |                              SSL_R_WRONG_SIGNATURE_TYPE);  | 
2790  | 0  |                     return 0;  | 
2791  | 0  |                 }  | 
2792  | 0  |             }  | 
2793  | 0  |         }  | 
2794  | 0  |     } else if (tls1_suiteb(s)) { | 
2795  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2796  | 0  |         return 0;  | 
2797  | 0  |     }  | 
2798  |  |  | 
2799  |  |     /* Check signature matches a type we sent */  | 
2800  | 0  |     sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);  | 
2801  | 0  |     for (i = 0; i < sent_sigslen; i++, sent_sigs++) { | 
2802  | 0  |         if (sig == *sent_sigs)  | 
2803  | 0  |             break;  | 
2804  | 0  |     }  | 
2805  |  |     /* Allow fallback to SHA1 if not strict mode */  | 
2806  | 0  |     if (i == sent_sigslen && (lu->hash != NID_sha1  | 
2807  | 0  |         || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | 
2808  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2809  | 0  |         return 0;  | 
2810  | 0  |     }  | 
2811  | 0  |     if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, &md)) { | 
2812  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST);  | 
2813  | 0  |         return 0;  | 
2814  | 0  |     }  | 
2815  |  |     /*  | 
2816  |  |      * Make sure security callback allows algorithm. For historical  | 
2817  |  |      * reasons we have to pass the sigalg as a two byte char array.  | 
2818  |  |      */  | 
2819  | 0  |     sigalgstr[0] = (sig >> 8) & 0xff;  | 
2820  | 0  |     sigalgstr[1] = sig & 0xff;  | 
2821  | 0  |     secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu);  | 
2822  | 0  |     if (secbits == 0 ||  | 
2823  | 0  |         !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits,  | 
2824  | 0  |                       md != NULL ? EVP_MD_get_type(md) : NID_undef,  | 
2825  | 0  |                       (void *)sigalgstr)) { | 
2826  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);  | 
2827  | 0  |         return 0;  | 
2828  | 0  |     }  | 
2829  |  |     /* Store the sigalg the peer uses */  | 
2830  | 0  |     s->s3.tmp.peer_sigalg = lu;  | 
2831  | 0  |     return 1;  | 
2832  | 0  | }  | 
2833  |  |  | 
2834  |  | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)  | 
2835  | 0  | { | 
2836  | 0  |     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);  | 
2837  |  | 
  | 
2838  | 0  |     if (sc == NULL)  | 
2839  | 0  |         return 0;  | 
2840  |  |  | 
2841  | 0  |     if (sc->s3.tmp.peer_sigalg == NULL)  | 
2842  | 0  |         return 0;  | 
2843  | 0  |     *pnid = sc->s3.tmp.peer_sigalg->sig;  | 
2844  | 0  |     return 1;  | 
2845  | 0  | }  | 
2846  |  |  | 
2847  |  | int SSL_get_signature_type_nid(const SSL *s, int *pnid)  | 
2848  | 0  | { | 
2849  | 0  |     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);  | 
2850  |  | 
  | 
2851  | 0  |     if (sc == NULL)  | 
2852  | 0  |         return 0;  | 
2853  |  |  | 
2854  | 0  |     if (sc->s3.tmp.sigalg == NULL)  | 
2855  | 0  |         return 0;  | 
2856  | 0  |     *pnid = sc->s3.tmp.sigalg->sig;  | 
2857  | 0  |     return 1;  | 
2858  | 0  | }  | 
2859  |  |  | 
2860  |  | /*  | 
2861  |  |  * Set a mask of disabled algorithms: an algorithm is disabled if it isn't  | 
2862  |  |  * supported, doesn't appear in supported signature algorithms, isn't supported  | 
2863  |  |  * by the enabled protocol versions or by the security level.  | 
2864  |  |  *  | 
2865  |  |  * This function should only be used for checking which ciphers are supported  | 
2866  |  |  * by the client.  | 
2867  |  |  *  | 
2868  |  |  * Call ssl_cipher_disabled() to check that it's enabled or not.  | 
2869  |  |  */  | 
2870  |  | int ssl_set_client_disabled(SSL_CONNECTION *s)  | 
2871  | 0  | { | 
2872  | 0  |     s->s3.tmp.mask_a = 0;  | 
2873  | 0  |     s->s3.tmp.mask_k = 0;  | 
2874  | 0  |     ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);  | 
2875  | 0  |     if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver,  | 
2876  | 0  |                                 &s->s3.tmp.max_ver, NULL) != 0)  | 
2877  | 0  |         return 0;  | 
2878  | 0  | #ifndef OPENSSL_NO_PSK  | 
2879  |  |     /* with PSK there must be client callback set */  | 
2880  | 0  |     if (!s->psk_client_callback) { | 
2881  | 0  |         s->s3.tmp.mask_a |= SSL_aPSK;  | 
2882  | 0  |         s->s3.tmp.mask_k |= SSL_PSK;  | 
2883  | 0  |     }  | 
2884  | 0  | #endif                          /* OPENSSL_NO_PSK */  | 
2885  | 0  | #ifndef OPENSSL_NO_SRP  | 
2886  | 0  |     if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { | 
2887  | 0  |         s->s3.tmp.mask_a |= SSL_aSRP;  | 
2888  | 0  |         s->s3.tmp.mask_k |= SSL_kSRP;  | 
2889  | 0  |     }  | 
2890  | 0  | #endif  | 
2891  | 0  |     return 1;  | 
2892  | 0  | }  | 
2893  |  |  | 
2894  |  | /*  | 
2895  |  |  * ssl_cipher_disabled - check that a cipher is disabled or not  | 
2896  |  |  * @s: SSL connection that you want to use the cipher on  | 
2897  |  |  * @c: cipher to check  | 
2898  |  |  * @op: Security check that you want to do  | 
2899  |  |  * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3  | 
2900  |  |  *  | 
2901  |  |  * Returns 1 when it's disabled, 0 when enabled.  | 
2902  |  |  */  | 
2903  |  | int ssl_cipher_disabled(const SSL_CONNECTION *s, const SSL_CIPHER *c,  | 
2904  |  |                         int op, int ecdhe)  | 
2905  | 0  | { | 
2906  | 0  |     int minversion = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls;  | 
2907  | 0  |     int maxversion = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls;  | 
2908  |  | 
  | 
2909  | 0  |     if (c->algorithm_mkey & s->s3.tmp.mask_k  | 
2910  | 0  |         || c->algorithm_auth & s->s3.tmp.mask_a)  | 
2911  | 0  |         return 1;  | 
2912  | 0  |     if (s->s3.tmp.max_ver == 0)  | 
2913  | 0  |         return 1;  | 
2914  |  |  | 
2915  | 0  |     if (SSL_IS_QUIC_INT_HANDSHAKE(s))  | 
2916  |  |         /* For QUIC, only allow these ciphersuites. */  | 
2917  | 0  |         switch (SSL_CIPHER_get_id(c)) { | 
2918  | 0  |         case TLS1_3_CK_AES_128_GCM_SHA256:  | 
2919  | 0  |         case TLS1_3_CK_AES_256_GCM_SHA384:  | 
2920  | 0  |         case TLS1_3_CK_CHACHA20_POLY1305_SHA256:  | 
2921  | 0  |             break;  | 
2922  | 0  |         default:  | 
2923  | 0  |             return 1;  | 
2924  | 0  |         }  | 
2925  |  |  | 
2926  |  |     /*  | 
2927  |  |      * For historical reasons we will allow ECHDE to be selected by a server  | 
2928  |  |      * in SSLv3 if we are a client  | 
2929  |  |      */  | 
2930  | 0  |     if (minversion == TLS1_VERSION  | 
2931  | 0  |             && ecdhe  | 
2932  | 0  |             && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)  | 
2933  | 0  |         minversion = SSL3_VERSION;  | 
2934  |  | 
  | 
2935  | 0  |     if (ssl_version_cmp(s, minversion, s->s3.tmp.max_ver) > 0  | 
2936  | 0  |         || ssl_version_cmp(s, maxversion, s->s3.tmp.min_ver) < 0)  | 
2937  | 0  |         return 1;  | 
2938  |  |  | 
2939  | 0  |     return !ssl_security(s, op, c->strength_bits, 0, (void *)c);  | 
2940  | 0  | }  | 
2941  |  |  | 
2942  |  | int tls_use_ticket(SSL_CONNECTION *s)  | 
2943  | 0  | { | 
2944  | 0  |     if ((s->options & SSL_OP_NO_TICKET))  | 
2945  | 0  |         return 0;  | 
2946  | 0  |     return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);  | 
2947  | 0  | }  | 
2948  |  |  | 
2949  |  | int tls1_set_server_sigalgs(SSL_CONNECTION *s)  | 
2950  | 0  | { | 
2951  | 0  |     size_t i;  | 
2952  |  |  | 
2953  |  |     /* Clear any shared signature algorithms */  | 
2954  | 0  |     OPENSSL_free(s->shared_sigalgs);  | 
2955  | 0  |     s->shared_sigalgs = NULL;  | 
2956  | 0  |     s->shared_sigalgslen = 0;  | 
2957  |  |  | 
2958  |  |     /* Clear certificate validity flags */  | 
2959  | 0  |     if (s->s3.tmp.valid_flags)  | 
2960  | 0  |         memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t));  | 
2961  | 0  |     else  | 
2962  | 0  |         s->s3.tmp.valid_flags = OPENSSL_calloc(s->ssl_pkey_num, sizeof(uint32_t));  | 
2963  | 0  |     if (s->s3.tmp.valid_flags == NULL) { | 
2964  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2965  | 0  |         return 0;  | 
2966  | 0  |     }  | 
2967  |  |     /*  | 
2968  |  |      * If peer sent no signature algorithms check to see if we support  | 
2969  |  |      * the default algorithm for each certificate type  | 
2970  |  |      */  | 
2971  | 0  |     if (s->s3.tmp.peer_cert_sigalgs == NULL  | 
2972  | 0  |             && s->s3.tmp.peer_sigalgs == NULL) { | 
2973  | 0  |         const uint16_t *sent_sigs;  | 
2974  | 0  |         size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);  | 
2975  |  | 
  | 
2976  | 0  |         for (i = 0; i < s->ssl_pkey_num; i++) { | 
2977  | 0  |             const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, (int)i);  | 
2978  | 0  |             size_t j;  | 
2979  |  | 
  | 
2980  | 0  |             if (lu == NULL)  | 
2981  | 0  |                 continue;  | 
2982  |  |             /* Check default matches a type we sent */  | 
2983  | 0  |             for (j = 0; j < sent_sigslen; j++) { | 
2984  | 0  |                 if (lu->sigalg == sent_sigs[j]) { | 
2985  | 0  |                         s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN;  | 
2986  | 0  |                         break;  | 
2987  | 0  |                 }  | 
2988  | 0  |             }  | 
2989  | 0  |         }  | 
2990  | 0  |         return 1;  | 
2991  | 0  |     }  | 
2992  |  |  | 
2993  | 0  |     if (!tls1_process_sigalgs(s)) { | 
2994  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2995  | 0  |         return 0;  | 
2996  | 0  |     }  | 
2997  | 0  |     if (s->shared_sigalgs != NULL)  | 
2998  | 0  |         return 1;  | 
2999  |  |  | 
3000  |  |     /* Fatal error if no shared signature algorithms */  | 
3001  | 0  |     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
3002  | 0  |              SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);  | 
3003  | 0  |     return 0;  | 
3004  | 0  | }  | 
3005  |  |  | 
3006  |  | /*-  | 
3007  |  |  * Gets the ticket information supplied by the client if any.  | 
3008  |  |  *  | 
3009  |  |  *   hello: The parsed ClientHello data  | 
3010  |  |  *   ret: (output) on return, if a ticket was decrypted, then this is set to  | 
3011  |  |  *       point to the resulting session.  | 
3012  |  |  */  | 
3013  |  | SSL_TICKET_STATUS tls_get_ticket_from_client(SSL_CONNECTION *s,  | 
3014  |  |                                              CLIENTHELLO_MSG *hello,  | 
3015  |  |                                              SSL_SESSION **ret)  | 
3016  | 0  | { | 
3017  | 0  |     size_t size;  | 
3018  | 0  |     RAW_EXTENSION *ticketext;  | 
3019  |  | 
  | 
3020  | 0  |     *ret = NULL;  | 
3021  | 0  |     s->ext.ticket_expected = 0;  | 
3022  |  |  | 
3023  |  |     /*  | 
3024  |  |      * If tickets disabled or not supported by the protocol version  | 
3025  |  |      * (e.g. TLSv1.3) behave as if no ticket present to permit stateful  | 
3026  |  |      * resumption.  | 
3027  |  |      */  | 
3028  | 0  |     if (s->version <= SSL3_VERSION || !tls_use_ticket(s))  | 
3029  | 0  |         return SSL_TICKET_NONE;  | 
3030  |  |  | 
3031  | 0  |     ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];  | 
3032  | 0  |     if (!ticketext->present)  | 
3033  | 0  |         return SSL_TICKET_NONE;  | 
3034  |  |  | 
3035  | 0  |     size = PACKET_remaining(&ticketext->data);  | 
3036  |  | 
  | 
3037  | 0  |     return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,  | 
3038  | 0  |                               hello->session_id, hello->session_id_len, ret);  | 
3039  | 0  | }  | 
3040  |  |  | 
3041  |  | /*-  | 
3042  |  |  * tls_decrypt_ticket attempts to decrypt a session ticket.  | 
3043  |  |  *  | 
3044  |  |  * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are  | 
3045  |  |  * expecting a pre-shared key ciphersuite, in which case we have no use for  | 
3046  |  |  * session tickets and one will never be decrypted, nor will  | 
3047  |  |  * s->ext.ticket_expected be set to 1.  | 
3048  |  |  *  | 
3049  |  |  * Side effects:  | 
3050  |  |  *   Sets s->ext.ticket_expected to 1 if the server will have to issue  | 
3051  |  |  *   a new session ticket to the client because the client indicated support  | 
3052  |  |  *   (and s->tls_session_secret_cb is NULL) but the client either doesn't have  | 
3053  |  |  *   a session ticket or we couldn't use the one it gave us, or if  | 
3054  |  |  *   s->ctx->ext.ticket_key_cb asked to renew the client's ticket.  | 
3055  |  |  *   Otherwise, s->ext.ticket_expected is set to 0.  | 
3056  |  |  *  | 
3057  |  |  *   etick: points to the body of the session ticket extension.  | 
3058  |  |  *   eticklen: the length of the session tickets extension.  | 
3059  |  |  *   sess_id: points at the session ID.  | 
3060  |  |  *   sesslen: the length of the session ID.  | 
3061  |  |  *   psess: (output) on return, if a ticket was decrypted, then this is set to  | 
3062  |  |  *       point to the resulting session.  | 
3063  |  |  */  | 
3064  |  | SSL_TICKET_STATUS tls_decrypt_ticket(SSL_CONNECTION *s,  | 
3065  |  |                                      const unsigned char *etick,  | 
3066  |  |                                      size_t eticklen,  | 
3067  |  |                                      const unsigned char *sess_id,  | 
3068  |  |                                      size_t sesslen, SSL_SESSION **psess)  | 
3069  | 0  | { | 
3070  | 0  |     SSL_SESSION *sess = NULL;  | 
3071  | 0  |     unsigned char *sdec;  | 
3072  | 0  |     const unsigned char *p;  | 
3073  | 0  |     int slen, ivlen, renew_ticket = 0, declen;  | 
3074  | 0  |     SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3075  | 0  |     size_t mlen;  | 
3076  | 0  |     unsigned char tick_hmac[EVP_MAX_MD_SIZE];  | 
3077  | 0  |     SSL_HMAC *hctx = NULL;  | 
3078  | 0  |     EVP_CIPHER_CTX *ctx = NULL;  | 
3079  | 0  |     SSL_CTX *tctx = s->session_ctx;  | 
3080  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
3081  |  | 
  | 
3082  | 0  |     if (eticklen == 0) { | 
3083  |  |         /*  | 
3084  |  |          * The client will accept a ticket but doesn't currently have  | 
3085  |  |          * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3  | 
3086  |  |          */  | 
3087  | 0  |         ret = SSL_TICKET_EMPTY;  | 
3088  | 0  |         goto end;  | 
3089  | 0  |     }  | 
3090  | 0  |     if (!SSL_CONNECTION_IS_TLS13(s) && s->ext.session_secret_cb) { | 
3091  |  |         /*  | 
3092  |  |          * Indicate that the ticket couldn't be decrypted rather than  | 
3093  |  |          * generating the session from ticket now, trigger  | 
3094  |  |          * abbreviated handshake based on external mechanism to  | 
3095  |  |          * calculate the master secret later.  | 
3096  |  |          */  | 
3097  | 0  |         ret = SSL_TICKET_NO_DECRYPT;  | 
3098  | 0  |         goto end;  | 
3099  | 0  |     }  | 
3100  |  |  | 
3101  |  |     /* Need at least keyname + iv */  | 
3102  | 0  |     if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { | 
3103  | 0  |         ret = SSL_TICKET_NO_DECRYPT;  | 
3104  | 0  |         goto end;  | 
3105  | 0  |     }  | 
3106  |  |  | 
3107  |  |     /* Initialize session ticket encryption and HMAC contexts */  | 
3108  | 0  |     hctx = ssl_hmac_new(tctx);  | 
3109  | 0  |     if (hctx == NULL) { | 
3110  | 0  |         ret = SSL_TICKET_FATAL_ERR_MALLOC;  | 
3111  | 0  |         goto end;  | 
3112  | 0  |     }  | 
3113  | 0  |     ctx = EVP_CIPHER_CTX_new();  | 
3114  | 0  |     if (ctx == NULL) { | 
3115  | 0  |         ret = SSL_TICKET_FATAL_ERR_MALLOC;  | 
3116  | 0  |         goto end;  | 
3117  | 0  |     }  | 
3118  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
3119  | 0  |     if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)  | 
3120  |  | #else  | 
3121  |  |     if (tctx->ext.ticket_key_evp_cb != NULL)  | 
3122  |  | #endif  | 
3123  | 0  |     { | 
3124  | 0  |         unsigned char *nctick = (unsigned char *)etick;  | 
3125  | 0  |         int rv = 0;  | 
3126  |  | 
  | 
3127  | 0  |         if (tctx->ext.ticket_key_evp_cb != NULL)  | 
3128  | 0  |             rv = tctx->ext.ticket_key_evp_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
3129  | 0  |                                              nctick,  | 
3130  | 0  |                                              nctick + TLSEXT_KEYNAME_LENGTH,  | 
3131  | 0  |                                              ctx,  | 
3132  | 0  |                                              ssl_hmac_get0_EVP_MAC_CTX(hctx),  | 
3133  | 0  |                                              0);  | 
3134  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
3135  | 0  |         else if (tctx->ext.ticket_key_cb != NULL)  | 
3136  |  |             /* if 0 is returned, write an empty ticket */  | 
3137  | 0  |             rv = tctx->ext.ticket_key_cb(SSL_CONNECTION_GET_USER_SSL(s), nctick,  | 
3138  | 0  |                                          nctick + TLSEXT_KEYNAME_LENGTH,  | 
3139  | 0  |                                          ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0);  | 
3140  | 0  | #endif  | 
3141  | 0  |         if (rv < 0) { | 
3142  | 0  |             ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3143  | 0  |             goto end;  | 
3144  | 0  |         }  | 
3145  | 0  |         if (rv == 0) { | 
3146  | 0  |             ret = SSL_TICKET_NO_DECRYPT;  | 
3147  | 0  |             goto end;  | 
3148  | 0  |         }  | 
3149  | 0  |         if (rv == 2)  | 
3150  | 0  |             renew_ticket = 1;  | 
3151  | 0  |     } else { | 
3152  | 0  |         EVP_CIPHER *aes256cbc = NULL;  | 
3153  |  |  | 
3154  |  |         /* Check key name matches */  | 
3155  | 0  |         if (memcmp(etick, tctx->ext.tick_key_name,  | 
3156  | 0  |                    TLSEXT_KEYNAME_LENGTH) != 0) { | 
3157  | 0  |             ret = SSL_TICKET_NO_DECRYPT;  | 
3158  | 0  |             goto end;  | 
3159  | 0  |         }  | 
3160  |  |  | 
3161  | 0  |         aes256cbc = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC",  | 
3162  | 0  |                                      sctx->propq);  | 
3163  | 0  |         if (aes256cbc == NULL  | 
3164  | 0  |             || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,  | 
3165  | 0  |                              sizeof(tctx->ext.secure->tick_hmac_key),  | 
3166  | 0  |                              "SHA256") <= 0  | 
3167  | 0  |             || EVP_DecryptInit_ex(ctx, aes256cbc, NULL,  | 
3168  | 0  |                                   tctx->ext.secure->tick_aes_key,  | 
3169  | 0  |                                   etick + TLSEXT_KEYNAME_LENGTH) <= 0) { | 
3170  | 0  |             EVP_CIPHER_free(aes256cbc);  | 
3171  | 0  |             ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3172  | 0  |             goto end;  | 
3173  | 0  |         }  | 
3174  | 0  |         EVP_CIPHER_free(aes256cbc);  | 
3175  | 0  |         if (SSL_CONNECTION_IS_TLS13(s))  | 
3176  | 0  |             renew_ticket = 1;  | 
3177  | 0  |     }  | 
3178  |  |     /*  | 
3179  |  |      * Attempt to process session ticket, first conduct sanity and integrity  | 
3180  |  |      * checks on ticket.  | 
3181  |  |      */  | 
3182  | 0  |     mlen = ssl_hmac_size(hctx);  | 
3183  | 0  |     if (mlen == 0) { | 
3184  | 0  |         ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3185  | 0  |         goto end;  | 
3186  | 0  |     }  | 
3187  |  |  | 
3188  | 0  |     ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);  | 
3189  | 0  |     if (ivlen < 0) { | 
3190  | 0  |         ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3191  | 0  |         goto end;  | 
3192  | 0  |     }  | 
3193  |  |  | 
3194  |  |     /* Sanity check ticket length: must exceed keyname + IV + HMAC */  | 
3195  | 0  |     if (eticklen <= TLSEXT_KEYNAME_LENGTH + ivlen + mlen) { | 
3196  | 0  |         ret = SSL_TICKET_NO_DECRYPT;  | 
3197  | 0  |         goto end;  | 
3198  | 0  |     }  | 
3199  | 0  |     eticklen -= mlen;  | 
3200  |  |     /* Check HMAC of encrypted ticket */  | 
3201  | 0  |     if (ssl_hmac_update(hctx, etick, eticklen) <= 0  | 
3202  | 0  |         || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) { | 
3203  | 0  |         ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3204  | 0  |         goto end;  | 
3205  | 0  |     }  | 
3206  |  |  | 
3207  | 0  |     if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { | 
3208  | 0  |         ret = SSL_TICKET_NO_DECRYPT;  | 
3209  | 0  |         goto end;  | 
3210  | 0  |     }  | 
3211  |  |     /* Attempt to decrypt session data */  | 
3212  |  |     /* Move p after IV to start of encrypted ticket, update length */  | 
3213  | 0  |     p = etick + TLSEXT_KEYNAME_LENGTH + ivlen;  | 
3214  | 0  |     eticklen -= TLSEXT_KEYNAME_LENGTH + ivlen;  | 
3215  | 0  |     sdec = OPENSSL_malloc(eticklen);  | 
3216  | 0  |     if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,  | 
3217  | 0  |                                           (int)eticklen) <= 0) { | 
3218  | 0  |         OPENSSL_free(sdec);  | 
3219  | 0  |         ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3220  | 0  |         goto end;  | 
3221  | 0  |     }  | 
3222  | 0  |     if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { | 
3223  | 0  |         OPENSSL_free(sdec);  | 
3224  | 0  |         ret = SSL_TICKET_NO_DECRYPT;  | 
3225  | 0  |         goto end;  | 
3226  | 0  |     }  | 
3227  | 0  |     slen += declen;  | 
3228  | 0  |     p = sdec;  | 
3229  |  | 
  | 
3230  | 0  |     sess = d2i_SSL_SESSION_ex(NULL, &p, slen, sctx->libctx, sctx->propq);  | 
3231  | 0  |     slen -= (int)(p - sdec);  | 
3232  | 0  |     OPENSSL_free(sdec);  | 
3233  | 0  |     if (sess) { | 
3234  |  |         /* Some additional consistency checks */  | 
3235  | 0  |         if (slen != 0) { | 
3236  | 0  |             SSL_SESSION_free(sess);  | 
3237  | 0  |             sess = NULL;  | 
3238  | 0  |             ret = SSL_TICKET_NO_DECRYPT;  | 
3239  | 0  |             goto end;  | 
3240  | 0  |         }  | 
3241  |  |         /*  | 
3242  |  |          * The session ID, if non-empty, is used by some clients to detect  | 
3243  |  |          * that the ticket has been accepted. So we copy it to the session  | 
3244  |  |          * structure. If it is empty set length to zero as required by  | 
3245  |  |          * standard.  | 
3246  |  |          */  | 
3247  | 0  |         if (sesslen) { | 
3248  | 0  |             memcpy(sess->session_id, sess_id, sesslen);  | 
3249  | 0  |             sess->session_id_length = sesslen;  | 
3250  | 0  |         }  | 
3251  | 0  |         if (renew_ticket)  | 
3252  | 0  |             ret = SSL_TICKET_SUCCESS_RENEW;  | 
3253  | 0  |         else  | 
3254  | 0  |             ret = SSL_TICKET_SUCCESS;  | 
3255  | 0  |         goto end;  | 
3256  | 0  |     }  | 
3257  | 0  |     ERR_clear_error();  | 
3258  |  |     /*  | 
3259  |  |      * For session parse failure, indicate that we need to send a new ticket.  | 
3260  |  |      */  | 
3261  | 0  |     ret = SSL_TICKET_NO_DECRYPT;  | 
3262  |  | 
  | 
3263  | 0  |  end:  | 
3264  | 0  |     EVP_CIPHER_CTX_free(ctx);  | 
3265  | 0  |     ssl_hmac_free(hctx);  | 
3266  |  |  | 
3267  |  |     /*  | 
3268  |  |      * If set, the decrypt_ticket_cb() is called unless a fatal error was  | 
3269  |  |      * detected above. The callback is responsible for checking |ret| before it  | 
3270  |  |      * performs any action  | 
3271  |  |      */  | 
3272  | 0  |     if (s->session_ctx->decrypt_ticket_cb != NULL  | 
3273  | 0  |             && (ret == SSL_TICKET_EMPTY  | 
3274  | 0  |                 || ret == SSL_TICKET_NO_DECRYPT  | 
3275  | 0  |                 || ret == SSL_TICKET_SUCCESS  | 
3276  | 0  |                 || ret == SSL_TICKET_SUCCESS_RENEW)) { | 
3277  | 0  |         size_t keyname_len = eticklen;  | 
3278  | 0  |         int retcb;  | 
3279  |  | 
  | 
3280  | 0  |         if (keyname_len > TLSEXT_KEYNAME_LENGTH)  | 
3281  | 0  |             keyname_len = TLSEXT_KEYNAME_LENGTH;  | 
3282  | 0  |         retcb = s->session_ctx->decrypt_ticket_cb(SSL_CONNECTION_GET_SSL(s),  | 
3283  | 0  |                                                   sess, etick, keyname_len,  | 
3284  | 0  |                                                   ret,  | 
3285  | 0  |                                                   s->session_ctx->ticket_cb_data);  | 
3286  | 0  |         switch (retcb) { | 
3287  | 0  |         case SSL_TICKET_RETURN_ABORT:  | 
3288  | 0  |             ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3289  | 0  |             break;  | 
3290  |  |  | 
3291  | 0  |         case SSL_TICKET_RETURN_IGNORE:  | 
3292  | 0  |             ret = SSL_TICKET_NONE;  | 
3293  | 0  |             SSL_SESSION_free(sess);  | 
3294  | 0  |             sess = NULL;  | 
3295  | 0  |             break;  | 
3296  |  |  | 
3297  | 0  |         case SSL_TICKET_RETURN_IGNORE_RENEW:  | 
3298  | 0  |             if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT)  | 
3299  | 0  |                 ret = SSL_TICKET_NO_DECRYPT;  | 
3300  |  |             /* else the value of |ret| will already do the right thing */  | 
3301  | 0  |             SSL_SESSION_free(sess);  | 
3302  | 0  |             sess = NULL;  | 
3303  | 0  |             break;  | 
3304  |  |  | 
3305  | 0  |         case SSL_TICKET_RETURN_USE:  | 
3306  | 0  |         case SSL_TICKET_RETURN_USE_RENEW:  | 
3307  | 0  |             if (ret != SSL_TICKET_SUCCESS  | 
3308  | 0  |                     && ret != SSL_TICKET_SUCCESS_RENEW)  | 
3309  | 0  |                 ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3310  | 0  |             else if (retcb == SSL_TICKET_RETURN_USE)  | 
3311  | 0  |                 ret = SSL_TICKET_SUCCESS;  | 
3312  | 0  |             else  | 
3313  | 0  |                 ret = SSL_TICKET_SUCCESS_RENEW;  | 
3314  | 0  |             break;  | 
3315  |  |  | 
3316  | 0  |         default:  | 
3317  | 0  |             ret = SSL_TICKET_FATAL_ERR_OTHER;  | 
3318  | 0  |         }  | 
3319  | 0  |     }  | 
3320  |  |  | 
3321  | 0  |     if (s->ext.session_secret_cb == NULL || SSL_CONNECTION_IS_TLS13(s)) { | 
3322  | 0  |         switch (ret) { | 
3323  | 0  |         case SSL_TICKET_NO_DECRYPT:  | 
3324  | 0  |         case SSL_TICKET_SUCCESS_RENEW:  | 
3325  | 0  |         case SSL_TICKET_EMPTY:  | 
3326  | 0  |             s->ext.ticket_expected = 1;  | 
3327  | 0  |         }  | 
3328  | 0  |     }  | 
3329  |  | 
  | 
3330  | 0  |     *psess = sess;  | 
3331  |  | 
  | 
3332  | 0  |     return ret;  | 
3333  | 0  | }  | 
3334  |  |  | 
3335  |  | /* Check to see if a signature algorithm is allowed */  | 
3336  |  | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op,  | 
3337  |  |                                 const SIGALG_LOOKUP *lu)  | 
3338  | 0  | { | 
3339  | 0  |     unsigned char sigalgstr[2];  | 
3340  | 0  |     int secbits;  | 
3341  |  | 
  | 
3342  | 0  |     if (lu == NULL || !lu->available)  | 
3343  | 0  |         return 0;  | 
3344  |  |     /* DSA is not allowed in TLS 1.3 */  | 
3345  | 0  |     if (SSL_CONNECTION_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)  | 
3346  | 0  |         return 0;  | 
3347  |  |     /*  | 
3348  |  |      * At some point we should fully axe DSA/etc. in ClientHello as per TLS 1.3  | 
3349  |  |      * spec  | 
3350  |  |      */  | 
3351  | 0  |     if (!s->server && !SSL_CONNECTION_IS_DTLS(s)  | 
3352  | 0  |         && s->s3.tmp.min_ver >= TLS1_3_VERSION  | 
3353  | 0  |         && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX  | 
3354  | 0  |             || lu->hash_idx == SSL_MD_MD5_IDX  | 
3355  | 0  |             || lu->hash_idx == SSL_MD_SHA224_IDX))  | 
3356  | 0  |         return 0;  | 
3357  |  |  | 
3358  |  |     /* See if public key algorithm allowed */  | 
3359  | 0  |     if (ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), lu->sig_idx))  | 
3360  | 0  |         return 0;  | 
3361  |  |  | 
3362  | 0  |     if (lu->sig == NID_id_GostR3410_2012_256  | 
3363  | 0  |             || lu->sig == NID_id_GostR3410_2012_512  | 
3364  | 0  |             || lu->sig == NID_id_GostR3410_2001) { | 
3365  |  |         /* We never allow GOST sig algs on the server with TLSv1.3 */  | 
3366  | 0  |         if (s->server && SSL_CONNECTION_IS_TLS13(s))  | 
3367  | 0  |             return 0;  | 
3368  | 0  |         if (!s->server  | 
3369  | 0  |                 && SSL_CONNECTION_GET_SSL(s)->method->version == TLS_ANY_VERSION  | 
3370  | 0  |                 && s->s3.tmp.max_ver >= TLS1_3_VERSION) { | 
3371  | 0  |             int i, num;  | 
3372  | 0  |             STACK_OF(SSL_CIPHER) *sk;  | 
3373  |  |  | 
3374  |  |             /*  | 
3375  |  |              * We're a client that could negotiate TLSv1.3. We only allow GOST  | 
3376  |  |              * sig algs if we could negotiate TLSv1.2 or below and we have GOST  | 
3377  |  |              * ciphersuites enabled.  | 
3378  |  |              */  | 
3379  |  | 
  | 
3380  | 0  |             if (s->s3.tmp.min_ver >= TLS1_3_VERSION)  | 
3381  | 0  |                 return 0;  | 
3382  |  |  | 
3383  | 0  |             sk = SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s));  | 
3384  | 0  |             num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0;  | 
3385  | 0  |             for (i = 0; i < num; i++) { | 
3386  | 0  |                 const SSL_CIPHER *c;  | 
3387  |  | 
  | 
3388  | 0  |                 c = sk_SSL_CIPHER_value(sk, i);  | 
3389  |  |                 /* Skip disabled ciphers */  | 
3390  | 0  |                 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))  | 
3391  | 0  |                     continue;  | 
3392  |  |  | 
3393  | 0  |                 if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0)  | 
3394  | 0  |                     break;  | 
3395  | 0  |             }  | 
3396  | 0  |             if (i == num)  | 
3397  | 0  |                 return 0;  | 
3398  | 0  |         }  | 
3399  | 0  |     }  | 
3400  |  |  | 
3401  |  |     /* Finally see if security callback allows it */  | 
3402  | 0  |     secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu);  | 
3403  | 0  |     sigalgstr[0] = (lu->sigalg >> 8) & 0xff;  | 
3404  | 0  |     sigalgstr[1] = lu->sigalg & 0xff;  | 
3405  | 0  |     return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);  | 
3406  | 0  | }  | 
3407  |  |  | 
3408  |  | /*  | 
3409  |  |  * Get a mask of disabled public key algorithms based on supported signature  | 
3410  |  |  * algorithms. For example if no signature algorithm supports RSA then RSA is  | 
3411  |  |  * disabled.  | 
3412  |  |  */  | 
3413  |  |  | 
3414  |  | void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op)  | 
3415  | 0  | { | 
3416  | 0  |     const uint16_t *sigalgs;  | 
3417  | 0  |     size_t i, sigalgslen;  | 
3418  | 0  |     uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;  | 
3419  |  |     /*  | 
3420  |  |      * Go through all signature algorithms seeing if we support any  | 
3421  |  |      * in disabled_mask.  | 
3422  |  |      */  | 
3423  | 0  |     sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);  | 
3424  | 0  |     for (i = 0; i < sigalgslen; i++, sigalgs++) { | 
3425  | 0  |         const SIGALG_LOOKUP *lu =  | 
3426  | 0  |             tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *sigalgs);  | 
3427  | 0  |         const SSL_CERT_LOOKUP *clu;  | 
3428  |  | 
  | 
3429  | 0  |         if (lu == NULL)  | 
3430  | 0  |             continue;  | 
3431  |  |  | 
3432  | 0  |         clu = ssl_cert_lookup_by_idx(lu->sig_idx,  | 
3433  | 0  |                                      SSL_CONNECTION_GET_CTX(s));  | 
3434  | 0  |         if (clu == NULL)  | 
3435  | 0  |                 continue;  | 
3436  |  |  | 
3437  |  |         /* If algorithm is disabled see if we can enable it */  | 
3438  | 0  |         if ((clu->amask & disabled_mask) != 0  | 
3439  | 0  |                 && tls12_sigalg_allowed(s, op, lu))  | 
3440  | 0  |             disabled_mask &= ~clu->amask;  | 
3441  | 0  |     }  | 
3442  | 0  |     *pmask_a |= disabled_mask;  | 
3443  | 0  | }  | 
3444  |  |  | 
3445  |  | int tls12_copy_sigalgs(SSL_CONNECTION *s, WPACKET *pkt,  | 
3446  |  |                        const uint16_t *psig, size_t psiglen)  | 
3447  | 0  | { | 
3448  | 0  |     size_t i;  | 
3449  | 0  |     int rv = 0;  | 
3450  |  | 
  | 
3451  | 0  |     for (i = 0; i < psiglen; i++, psig++) { | 
3452  | 0  |         const SIGALG_LOOKUP *lu =  | 
3453  | 0  |             tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *psig);  | 
3454  |  | 
  | 
3455  | 0  |         if (lu == NULL || !tls_sigalg_compat(s, lu))  | 
3456  | 0  |             continue;  | 
3457  | 0  |         if (!WPACKET_put_bytes_u16(pkt, *psig))  | 
3458  | 0  |             return 0;  | 
3459  |  |         /*  | 
3460  |  |          * If TLS 1.3 must have at least one valid TLS 1.3 message  | 
3461  |  |          * signing algorithm: i.e. neither RSA nor SHA1/SHA224  | 
3462  |  |          */  | 
3463  | 0  |         if (rv == 0 && (!SSL_CONNECTION_IS_TLS13(s)  | 
3464  | 0  |             || (lu->sig != EVP_PKEY_RSA  | 
3465  | 0  |                 && lu->hash != NID_sha1  | 
3466  | 0  |                 && lu->hash != NID_sha224)))  | 
3467  | 0  |             rv = 1;  | 
3468  | 0  |     }  | 
3469  | 0  |     if (rv == 0)  | 
3470  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);  | 
3471  | 0  |     return rv;  | 
3472  | 0  | }  | 
3473  |  |  | 
3474  |  | /* Given preference and allowed sigalgs set shared sigalgs */  | 
3475  |  | static size_t tls12_shared_sigalgs(SSL_CONNECTION *s,  | 
3476  |  |                                    const SIGALG_LOOKUP **shsig,  | 
3477  |  |                                    const uint16_t *pref, size_t preflen,  | 
3478  |  |                                    const uint16_t *allow, size_t allowlen)  | 
3479  | 0  | { | 
3480  | 0  |     const uint16_t *ptmp, *atmp;  | 
3481  | 0  |     size_t i, j, nmatch = 0;  | 
3482  | 0  |     for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { | 
3483  | 0  |         const SIGALG_LOOKUP *lu =  | 
3484  | 0  |             tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *ptmp);  | 
3485  |  |  | 
3486  |  |         /* Skip disabled hashes or signature algorithms */  | 
3487  | 0  |         if (lu == NULL  | 
3488  | 0  |                 || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))  | 
3489  | 0  |             continue;  | 
3490  | 0  |         for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { | 
3491  | 0  |             if (*ptmp == *atmp) { | 
3492  | 0  |                 nmatch++;  | 
3493  | 0  |                 if (shsig)  | 
3494  | 0  |                     *shsig++ = lu;  | 
3495  | 0  |                 break;  | 
3496  | 0  |             }  | 
3497  | 0  |         }  | 
3498  | 0  |     }  | 
3499  | 0  |     return nmatch;  | 
3500  | 0  | }  | 
3501  |  |  | 
3502  |  | /* Set shared signature algorithms for SSL structures */  | 
3503  |  | static int tls1_set_shared_sigalgs(SSL_CONNECTION *s)  | 
3504  | 0  | { | 
3505  | 0  |     const uint16_t *pref, *allow, *conf;  | 
3506  | 0  |     size_t preflen, allowlen, conflen;  | 
3507  | 0  |     size_t nmatch;  | 
3508  | 0  |     const SIGALG_LOOKUP **salgs = NULL;  | 
3509  | 0  |     CERT *c = s->cert;  | 
3510  | 0  |     unsigned int is_suiteb = tls1_suiteb(s);  | 
3511  |  | 
  | 
3512  | 0  |     OPENSSL_free(s->shared_sigalgs);  | 
3513  | 0  |     s->shared_sigalgs = NULL;  | 
3514  | 0  |     s->shared_sigalgslen = 0;  | 
3515  |  |     /* If client use client signature algorithms if not NULL */  | 
3516  | 0  |     if (!s->server && c->client_sigalgs && !is_suiteb) { | 
3517  | 0  |         conf = c->client_sigalgs;  | 
3518  | 0  |         conflen = c->client_sigalgslen;  | 
3519  | 0  |     } else if (c->conf_sigalgs && !is_suiteb) { | 
3520  | 0  |         conf = c->conf_sigalgs;  | 
3521  | 0  |         conflen = c->conf_sigalgslen;  | 
3522  | 0  |     } else  | 
3523  | 0  |         conflen = tls12_get_psigalgs(s, 0, &conf);  | 
3524  | 0  |     if (s->options & SSL_OP_SERVER_PREFERENCE || is_suiteb) { | 
3525  | 0  |         pref = conf;  | 
3526  | 0  |         preflen = conflen;  | 
3527  | 0  |         allow = s->s3.tmp.peer_sigalgs;  | 
3528  | 0  |         allowlen = s->s3.tmp.peer_sigalgslen;  | 
3529  | 0  |     } else { | 
3530  | 0  |         allow = conf;  | 
3531  | 0  |         allowlen = conflen;  | 
3532  | 0  |         pref = s->s3.tmp.peer_sigalgs;  | 
3533  | 0  |         preflen = s->s3.tmp.peer_sigalgslen;  | 
3534  | 0  |     }  | 
3535  | 0  |     nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);  | 
3536  | 0  |     if (nmatch) { | 
3537  | 0  |         if ((salgs = OPENSSL_malloc_array(nmatch, sizeof(*salgs))) == NULL)  | 
3538  | 0  |             return 0;  | 
3539  | 0  |         nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);  | 
3540  | 0  |     } else { | 
3541  | 0  |         salgs = NULL;  | 
3542  | 0  |     }  | 
3543  | 0  |     s->shared_sigalgs = salgs;  | 
3544  | 0  |     s->shared_sigalgslen = nmatch;  | 
3545  | 0  |     return 1;  | 
3546  | 0  | }  | 
3547  |  |  | 
3548  |  | int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)  | 
3549  | 0  | { | 
3550  | 0  |     unsigned int stmp;  | 
3551  | 0  |     size_t size, i;  | 
3552  | 0  |     uint16_t *buf;  | 
3553  |  | 
  | 
3554  | 0  |     size = PACKET_remaining(pkt);  | 
3555  |  |  | 
3556  |  |     /* Invalid data length */  | 
3557  | 0  |     if (size == 0 || (size & 1) != 0)  | 
3558  | 0  |         return 0;  | 
3559  |  |  | 
3560  | 0  |     size >>= 1;  | 
3561  |  | 
  | 
3562  | 0  |     if ((buf = OPENSSL_malloc_array(size, sizeof(*buf))) == NULL)  | 
3563  | 0  |         return 0;  | 
3564  | 0  |     for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)  | 
3565  | 0  |         buf[i] = stmp;  | 
3566  |  | 
  | 
3567  | 0  |     if (i != size) { | 
3568  | 0  |         OPENSSL_free(buf);  | 
3569  | 0  |         return 0;  | 
3570  | 0  |     }  | 
3571  |  |  | 
3572  | 0  |     OPENSSL_free(*pdest);  | 
3573  | 0  |     *pdest = buf;  | 
3574  | 0  |     *pdestlen = size;  | 
3575  |  | 
  | 
3576  | 0  |     return 1;  | 
3577  | 0  | }  | 
3578  |  |  | 
3579  |  | int tls1_save_sigalgs(SSL_CONNECTION *s, PACKET *pkt, int cert)  | 
3580  | 0  | { | 
3581  |  |     /* Extension ignored for inappropriate versions */  | 
3582  | 0  |     if (!SSL_USE_SIGALGS(s))  | 
3583  | 0  |         return 1;  | 
3584  |  |     /* Should never happen */  | 
3585  | 0  |     if (s->cert == NULL)  | 
3586  | 0  |         return 0;  | 
3587  |  |  | 
3588  | 0  |     if (cert)  | 
3589  | 0  |         return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs,  | 
3590  | 0  |                              &s->s3.tmp.peer_cert_sigalgslen);  | 
3591  | 0  |     else  | 
3592  | 0  |         return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs,  | 
3593  | 0  |                              &s->s3.tmp.peer_sigalgslen);  | 
3594  |  | 
  | 
3595  | 0  | }  | 
3596  |  |  | 
3597  |  | /* Set preferred digest for each key type */  | 
3598  |  |  | 
3599  |  | int tls1_process_sigalgs(SSL_CONNECTION *s)  | 
3600  | 0  | { | 
3601  | 0  |     size_t i;  | 
3602  | 0  |     uint32_t *pvalid = s->s3.tmp.valid_flags;  | 
3603  |  | 
  | 
3604  | 0  |     if (!tls1_set_shared_sigalgs(s))  | 
3605  | 0  |         return 0;  | 
3606  |  |  | 
3607  | 0  |     for (i = 0; i < s->ssl_pkey_num; i++)  | 
3608  | 0  |         pvalid[i] = 0;  | 
3609  |  | 
  | 
3610  | 0  |     for (i = 0; i < s->shared_sigalgslen; i++) { | 
3611  | 0  |         const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i];  | 
3612  | 0  |         int idx = sigptr->sig_idx;  | 
3613  |  |  | 
3614  |  |         /* Ignore PKCS1 based sig algs in TLSv1.3 */  | 
3615  | 0  |         if (SSL_CONNECTION_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)  | 
3616  | 0  |             continue;  | 
3617  |  |         /* If not disabled indicate we can explicitly sign */  | 
3618  | 0  |         if (pvalid[idx] == 0  | 
3619  | 0  |             && !ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), idx))  | 
3620  | 0  |             pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;  | 
3621  | 0  |     }  | 
3622  | 0  |     return 1;  | 
3623  | 0  | }  | 
3624  |  |  | 
3625  |  | int SSL_get_sigalgs(SSL *s, int idx,  | 
3626  |  |                     int *psign, int *phash, int *psignhash,  | 
3627  |  |                     unsigned char *rsig, unsigned char *rhash)  | 
3628  | 0  | { | 
3629  | 0  |     uint16_t *psig;  | 
3630  | 0  |     size_t numsigalgs;  | 
3631  | 0  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);  | 
3632  |  | 
  | 
3633  | 0  |     if (sc == NULL)  | 
3634  | 0  |         return 0;  | 
3635  |  |  | 
3636  | 0  |     psig = sc->s3.tmp.peer_sigalgs;  | 
3637  | 0  |     numsigalgs = sc->s3.tmp.peer_sigalgslen;  | 
3638  |  | 
  | 
3639  | 0  |     if (psig == NULL || numsigalgs > INT_MAX)  | 
3640  | 0  |         return 0;  | 
3641  | 0  |     if (idx >= 0) { | 
3642  | 0  |         const SIGALG_LOOKUP *lu;  | 
3643  |  | 
  | 
3644  | 0  |         if (idx >= (int)numsigalgs)  | 
3645  | 0  |             return 0;  | 
3646  | 0  |         psig += idx;  | 
3647  | 0  |         if (rhash != NULL)  | 
3648  | 0  |             *rhash = (unsigned char)((*psig >> 8) & 0xff);  | 
3649  | 0  |         if (rsig != NULL)  | 
3650  | 0  |             *rsig = (unsigned char)(*psig & 0xff);  | 
3651  | 0  |         lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(sc), *psig);  | 
3652  | 0  |         if (psign != NULL)  | 
3653  | 0  |             *psign = lu != NULL ? lu->sig : NID_undef;  | 
3654  | 0  |         if (phash != NULL)  | 
3655  | 0  |             *phash = lu != NULL ? lu->hash : NID_undef;  | 
3656  | 0  |         if (psignhash != NULL)  | 
3657  | 0  |             *psignhash = lu != NULL ? lu->sigandhash : NID_undef;  | 
3658  | 0  |     }  | 
3659  | 0  |     return (int)numsigalgs;  | 
3660  | 0  | }  | 
3661  |  |  | 
3662  |  | int SSL_get_shared_sigalgs(SSL *s, int idx,  | 
3663  |  |                            int *psign, int *phash, int *psignhash,  | 
3664  |  |                            unsigned char *rsig, unsigned char *rhash)  | 
3665  | 0  | { | 
3666  | 0  |     const SIGALG_LOOKUP *shsigalgs;  | 
3667  | 0  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);  | 
3668  |  | 
  | 
3669  | 0  |     if (sc == NULL)  | 
3670  | 0  |         return 0;  | 
3671  |  |  | 
3672  | 0  |     if (sc->shared_sigalgs == NULL  | 
3673  | 0  |         || idx < 0  | 
3674  | 0  |         || idx >= (int)sc->shared_sigalgslen  | 
3675  | 0  |         || sc->shared_sigalgslen > INT_MAX)  | 
3676  | 0  |         return 0;  | 
3677  | 0  |     shsigalgs = sc->shared_sigalgs[idx];  | 
3678  | 0  |     if (phash != NULL)  | 
3679  | 0  |         *phash = shsigalgs->hash;  | 
3680  | 0  |     if (psign != NULL)  | 
3681  | 0  |         *psign = shsigalgs->sig;  | 
3682  | 0  |     if (psignhash != NULL)  | 
3683  | 0  |         *psignhash = shsigalgs->sigandhash;  | 
3684  | 0  |     if (rsig != NULL)  | 
3685  | 0  |         *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);  | 
3686  | 0  |     if (rhash != NULL)  | 
3687  | 0  |         *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);  | 
3688  | 0  |     return (int)sc->shared_sigalgslen;  | 
3689  | 0  | }  | 
3690  |  |  | 
3691  |  | /* Maximum possible number of unique entries in sigalgs array */  | 
3692  | 0  | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)  | 
3693  |  |  | 
3694  |  | typedef struct { | 
3695  |  |     size_t sigalgcnt;  | 
3696  |  |     /* TLSEXT_SIGALG_XXX values */  | 
3697  |  |     uint16_t sigalgs[TLS_MAX_SIGALGCNT];  | 
3698  |  |     SSL_CTX *ctx;  | 
3699  |  | } sig_cb_st;  | 
3700  |  |  | 
3701  |  | static void get_sigorhash(int *psig, int *phash, const char *str)  | 
3702  | 0  | { | 
3703  | 0  |     if (OPENSSL_strcasecmp(str, "RSA") == 0) { | 
3704  | 0  |         *psig = EVP_PKEY_RSA;  | 
3705  | 0  |     } else if (OPENSSL_strcasecmp(str, "RSA-PSS") == 0  | 
3706  | 0  |                || OPENSSL_strcasecmp(str, "PSS") == 0) { | 
3707  | 0  |         *psig = EVP_PKEY_RSA_PSS;  | 
3708  | 0  |     } else if (OPENSSL_strcasecmp(str, "DSA") == 0) { | 
3709  | 0  |         *psig = EVP_PKEY_DSA;  | 
3710  | 0  |     } else if (OPENSSL_strcasecmp(str, "ECDSA") == 0) { | 
3711  | 0  |         *psig = EVP_PKEY_EC;  | 
3712  | 0  |     } else { | 
3713  | 0  |         *phash = OBJ_sn2nid(str);  | 
3714  | 0  |         if (*phash == NID_undef)  | 
3715  | 0  |             *phash = OBJ_ln2nid(str);  | 
3716  | 0  |     }  | 
3717  | 0  | }  | 
3718  |  | /* Maximum length of a signature algorithm string component */  | 
3719  |  | #define TLS_MAX_SIGSTRING_LEN   40  | 
3720  |  |  | 
3721  |  | static int sig_cb(const char *elem, int len, void *arg)  | 
3722  | 0  | { | 
3723  | 0  |     sig_cb_st *sarg = arg;  | 
3724  | 0  |     size_t i = 0;  | 
3725  | 0  |     const SIGALG_LOOKUP *s;  | 
3726  | 0  |     char etmp[TLS_MAX_SIGSTRING_LEN], *p;  | 
3727  | 0  |     const char *iana, *alias;  | 
3728  | 0  |     int sig_alg = NID_undef, hash_alg = NID_undef;  | 
3729  | 0  |     int ignore_unknown = 0;  | 
3730  |  | 
  | 
3731  | 0  |     if (elem == NULL)  | 
3732  | 0  |         return 0;  | 
3733  | 0  |     if (elem[0] == '?') { | 
3734  | 0  |         ignore_unknown = 1;  | 
3735  | 0  |         ++elem;  | 
3736  | 0  |         --len;  | 
3737  | 0  |     }  | 
3738  | 0  |     if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)  | 
3739  | 0  |         return 0;  | 
3740  | 0  |     if (len > (int)(sizeof(etmp) - 1))  | 
3741  | 0  |         return 0;  | 
3742  | 0  |     memcpy(etmp, elem, len);  | 
3743  | 0  |     etmp[len] = 0;  | 
3744  | 0  |     p = strchr(etmp, '+');  | 
3745  |  |     /*  | 
3746  |  |      * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;  | 
3747  |  |      * if there's no '+' in the provided name, look for the new-style combined  | 
3748  |  |      * name.  If not, match both sig+hash to find the needed SIGALG_LOOKUP.  | 
3749  |  |      * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and  | 
3750  |  |      * rsa_pss_rsae_* that differ only by public key OID; in such cases  | 
3751  |  |      * we will pick the _rsae_ variant, by virtue of them appearing earlier  | 
3752  |  |      * in the table.  | 
3753  |  |      */  | 
3754  | 0  |     if (p == NULL) { | 
3755  | 0  |         if (sarg->ctx != NULL) { | 
3756  | 0  |             for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { | 
3757  | 0  |                 iana = sarg->ctx->sigalg_lookup_cache[i].name;  | 
3758  | 0  |                 alias = sarg->ctx->sigalg_lookup_cache[i].name12;  | 
3759  | 0  |                 if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0)  | 
3760  | 0  |                     || OPENSSL_strcasecmp(etmp, iana) == 0) { | 
3761  |  |                     /* Ignore known, but unavailable sigalgs. */  | 
3762  | 0  |                     if (!sarg->ctx->sigalg_lookup_cache[i].available)  | 
3763  | 0  |                         return 1;  | 
3764  | 0  |                     sarg->sigalgs[sarg->sigalgcnt++] =  | 
3765  | 0  |                         sarg->ctx->sigalg_lookup_cache[i].sigalg;  | 
3766  | 0  |                     goto found;  | 
3767  | 0  |                 }  | 
3768  | 0  |             }  | 
3769  | 0  |         } else { | 
3770  |  |             /* Syntax checks use the built-in sigalgs */  | 
3771  | 0  |             for (i = 0, s = sigalg_lookup_tbl;  | 
3772  | 0  |                  i < OSSL_NELEM(sigalg_lookup_tbl); i++, s++) { | 
3773  | 0  |                 iana = s->name;  | 
3774  | 0  |                 alias = s->name12;  | 
3775  | 0  |                 if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0)  | 
3776  | 0  |                     || OPENSSL_strcasecmp(etmp, iana) == 0) { | 
3777  | 0  |                     sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;  | 
3778  | 0  |                     goto found;  | 
3779  | 0  |                 }  | 
3780  | 0  |             }  | 
3781  | 0  |         }  | 
3782  | 0  |     } else { | 
3783  | 0  |         *p = 0;  | 
3784  | 0  |         p++;  | 
3785  | 0  |         if (*p == 0)  | 
3786  | 0  |             return 0;  | 
3787  | 0  |         get_sigorhash(&sig_alg, &hash_alg, etmp);  | 
3788  | 0  |         get_sigorhash(&sig_alg, &hash_alg, p);  | 
3789  | 0  |         if (sig_alg != NID_undef && hash_alg != NID_undef) { | 
3790  | 0  |             if (sarg->ctx != NULL) { | 
3791  | 0  |                 for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { | 
3792  | 0  |                     s = &sarg->ctx->sigalg_lookup_cache[i];  | 
3793  | 0  |                     if (s->hash == hash_alg && s->sig == sig_alg) { | 
3794  |  |                         /* Ignore known, but unavailable sigalgs. */  | 
3795  | 0  |                         if (!sarg->ctx->sigalg_lookup_cache[i].available)  | 
3796  | 0  |                             return 1;  | 
3797  | 0  |                         sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;  | 
3798  | 0  |                         goto found;  | 
3799  | 0  |                     }  | 
3800  | 0  |                 }  | 
3801  | 0  |             } else { | 
3802  | 0  |                 for (i = 0; i < OSSL_NELEM(sigalg_lookup_tbl); i++) { | 
3803  | 0  |                     s = &sigalg_lookup_tbl[i];  | 
3804  | 0  |                     if (s->hash == hash_alg && s->sig == sig_alg) { | 
3805  | 0  |                         sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;  | 
3806  | 0  |                         goto found;  | 
3807  | 0  |                     }  | 
3808  | 0  |                 }  | 
3809  | 0  |             }  | 
3810  | 0  |         }  | 
3811  | 0  |     }  | 
3812  |  |     /* Ignore unknown algorithms if ignore_unknown */  | 
3813  | 0  |     return ignore_unknown;  | 
3814  |  |  | 
3815  | 0  |  found:  | 
3816  |  |     /* Ignore duplicates */  | 
3817  | 0  |     for (i = 0; i < sarg->sigalgcnt - 1; i++) { | 
3818  | 0  |         if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { | 
3819  | 0  |             sarg->sigalgcnt--;  | 
3820  | 0  |             return 1;  | 
3821  | 0  |         }  | 
3822  | 0  |     }  | 
3823  | 0  |     return 1;  | 
3824  | 0  | }  | 
3825  |  |  | 
3826  |  | /*  | 
3827  |  |  * Set supported signature algorithms based on a colon separated list of the  | 
3828  |  |  * form sig+hash e.g. RSA+SHA512:DSA+SHA512  | 
3829  |  |  */  | 
3830  |  | int tls1_set_sigalgs_list(SSL_CTX *ctx, CERT *c, const char *str, int client)  | 
3831  | 0  | { | 
3832  | 0  |     sig_cb_st sig;  | 
3833  | 0  |     sig.sigalgcnt = 0;  | 
3834  |  | 
  | 
3835  | 0  |     if (ctx != NULL)  | 
3836  | 0  |         sig.ctx = ctx;  | 
3837  | 0  |     if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))  | 
3838  | 0  |         return 0;  | 
3839  | 0  |     if (sig.sigalgcnt == 0) { | 
3840  | 0  |         ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,  | 
3841  | 0  |                        "No valid signature algorithms in '%s'", str);  | 
3842  | 0  |         return 0;  | 
3843  | 0  |     }  | 
3844  | 0  |     if (c == NULL)  | 
3845  | 0  |         return 1;  | 
3846  | 0  |     return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);  | 
3847  | 0  | }  | 
3848  |  |  | 
3849  |  | int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,  | 
3850  |  |                      int client)  | 
3851  | 0  | { | 
3852  | 0  |     uint16_t *sigalgs;  | 
3853  |  | 
  | 
3854  | 0  |     if ((sigalgs = OPENSSL_malloc_array(salglen, sizeof(*sigalgs))) == NULL)  | 
3855  | 0  |         return 0;  | 
3856  | 0  |     memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));  | 
3857  |  | 
  | 
3858  | 0  |     if (client) { | 
3859  | 0  |         OPENSSL_free(c->client_sigalgs);  | 
3860  | 0  |         c->client_sigalgs = sigalgs;  | 
3861  | 0  |         c->client_sigalgslen = salglen;  | 
3862  | 0  |     } else { | 
3863  | 0  |         OPENSSL_free(c->conf_sigalgs);  | 
3864  | 0  |         c->conf_sigalgs = sigalgs;  | 
3865  | 0  |         c->conf_sigalgslen = salglen;  | 
3866  | 0  |     }  | 
3867  |  | 
  | 
3868  | 0  |     return 1;  | 
3869  | 0  | }  | 
3870  |  |  | 
3871  |  | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)  | 
3872  | 0  | { | 
3873  | 0  |     uint16_t *sigalgs, *sptr;  | 
3874  | 0  |     size_t i;  | 
3875  |  | 
  | 
3876  | 0  |     if (salglen & 1)  | 
3877  | 0  |         return 0;  | 
3878  | 0  |     if ((sigalgs = OPENSSL_malloc_array(salglen / 2, sizeof(*sigalgs))) == NULL)  | 
3879  | 0  |         return 0;  | 
3880  | 0  |     for (i = 0, sptr = sigalgs; i < salglen; i += 2) { | 
3881  | 0  |         size_t j;  | 
3882  | 0  |         const SIGALG_LOOKUP *curr;  | 
3883  | 0  |         int md_id = *psig_nids++;  | 
3884  | 0  |         int sig_id = *psig_nids++;  | 
3885  |  | 
  | 
3886  | 0  |         for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);  | 
3887  | 0  |              j++, curr++) { | 
3888  | 0  |             if (curr->hash == md_id && curr->sig == sig_id) { | 
3889  | 0  |                 *sptr++ = curr->sigalg;  | 
3890  | 0  |                 break;  | 
3891  | 0  |             }  | 
3892  | 0  |         }  | 
3893  |  | 
  | 
3894  | 0  |         if (j == OSSL_NELEM(sigalg_lookup_tbl))  | 
3895  | 0  |             goto err;  | 
3896  | 0  |     }  | 
3897  |  |  | 
3898  | 0  |     if (client) { | 
3899  | 0  |         OPENSSL_free(c->client_sigalgs);  | 
3900  | 0  |         c->client_sigalgs = sigalgs;  | 
3901  | 0  |         c->client_sigalgslen = salglen / 2;  | 
3902  | 0  |     } else { | 
3903  | 0  |         OPENSSL_free(c->conf_sigalgs);  | 
3904  | 0  |         c->conf_sigalgs = sigalgs;  | 
3905  | 0  |         c->conf_sigalgslen = salglen / 2;  | 
3906  | 0  |     }  | 
3907  |  | 
  | 
3908  | 0  |     return 1;  | 
3909  |  |  | 
3910  | 0  |  err:  | 
3911  | 0  |     OPENSSL_free(sigalgs);  | 
3912  | 0  |     return 0;  | 
3913  | 0  | }  | 
3914  |  |  | 
3915  |  | static int tls1_check_sig_alg(SSL_CONNECTION *s, X509 *x, int default_nid)  | 
3916  | 0  | { | 
3917  | 0  |     int sig_nid, use_pc_sigalgs = 0;  | 
3918  | 0  |     size_t i;  | 
3919  | 0  |     const SIGALG_LOOKUP *sigalg;  | 
3920  | 0  |     size_t sigalgslen;  | 
3921  |  |  | 
3922  |  |     /*-  | 
3923  |  |      * RFC 8446, section 4.2.3:  | 
3924  |  |      *  | 
3925  |  |      * The signatures on certificates that are self-signed or certificates  | 
3926  |  |      * that are trust anchors are not validated, since they begin a  | 
3927  |  |      * certification path (see [RFC5280], Section 3.2).  A certificate that  | 
3928  |  |      * begins a certification path MAY use a signature algorithm that is not  | 
3929  |  |      * advertised as being supported in the "signature_algorithms"  | 
3930  |  |      * extension.  | 
3931  |  |      */  | 
3932  | 0  |     if (default_nid == -1 || X509_self_signed(x, 0))  | 
3933  | 0  |         return 1;  | 
3934  | 0  |     sig_nid = X509_get_signature_nid(x);  | 
3935  | 0  |     if (default_nid)  | 
3936  | 0  |         return sig_nid == default_nid ? 1 : 0;  | 
3937  |  |  | 
3938  | 0  |     if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) { | 
3939  |  |         /*  | 
3940  |  |          * If we're in TLSv1.3 then we only get here if we're checking the  | 
3941  |  |          * chain. If the peer has specified peer_cert_sigalgs then we use them  | 
3942  |  |          * otherwise we default to normal sigalgs.  | 
3943  |  |          */  | 
3944  | 0  |         sigalgslen = s->s3.tmp.peer_cert_sigalgslen;  | 
3945  | 0  |         use_pc_sigalgs = 1;  | 
3946  | 0  |     } else { | 
3947  | 0  |         sigalgslen = s->shared_sigalgslen;  | 
3948  | 0  |     }  | 
3949  | 0  |     for (i = 0; i < sigalgslen; i++) { | 
3950  | 0  |         int mdnid, pknid;  | 
3951  |  | 
  | 
3952  | 0  |         sigalg = use_pc_sigalgs  | 
3953  | 0  |                  ? tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s),  | 
3954  | 0  |                                       s->s3.tmp.peer_cert_sigalgs[i])  | 
3955  | 0  |                  : s->shared_sigalgs[i];  | 
3956  | 0  |         if (sigalg == NULL)  | 
3957  | 0  |             continue;  | 
3958  | 0  |         if (sig_nid == sigalg->sigandhash)  | 
3959  | 0  |             return 1;  | 
3960  | 0  |         if (sigalg->sig != EVP_PKEY_RSA_PSS)  | 
3961  | 0  |             continue;  | 
3962  |  |         /*  | 
3963  |  |          * Accept RSA PKCS#1 signatures in certificates when the signature  | 
3964  |  |          * algorithms include RSA-PSS with a matching digest algorithm.  | 
3965  |  |          *  | 
3966  |  |          * When a TLS 1.3 peer inadvertently omits the legacy RSA PKCS#1 code  | 
3967  |  |          * points, and we're doing strict checking of the certificate chain (in  | 
3968  |  |          * a cert_cb via SSL_check_chain()) we may then reject RSA signed  | 
3969  |  |          * certificates in the chain, but the TLS requirement on PSS should not  | 
3970  |  |          * extend to certificates.  Though the peer can in fact list the legacy  | 
3971  |  |          * sigalgs for just this purpose, it is not likely that a better chain  | 
3972  |  |          * signed with RSA-PSS is available.  | 
3973  |  |          */  | 
3974  | 0  |         if (!OBJ_find_sigid_algs(sig_nid, &mdnid, &pknid))  | 
3975  | 0  |             continue;  | 
3976  | 0  |         if (pknid == EVP_PKEY_RSA && mdnid == sigalg->hash)  | 
3977  | 0  |             return 1;  | 
3978  | 0  |     }  | 
3979  | 0  |     return 0;  | 
3980  | 0  | }  | 
3981  |  |  | 
3982  |  | /* Check to see if a certificate issuer name matches list of CA names */  | 
3983  |  | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)  | 
3984  | 0  | { | 
3985  | 0  |     const X509_NAME *nm;  | 
3986  | 0  |     int i;  | 
3987  | 0  |     nm = X509_get_issuer_name(x);  | 
3988  | 0  |     for (i = 0; i < sk_X509_NAME_num(names); i++) { | 
3989  | 0  |         if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))  | 
3990  | 0  |             return 1;  | 
3991  | 0  |     }  | 
3992  | 0  |     return 0;  | 
3993  | 0  | }  | 
3994  |  |  | 
3995  |  | /*  | 
3996  |  |  * Check certificate chain is consistent with TLS extensions and is usable by  | 
3997  |  |  * server. This servers two purposes: it allows users to check chains before  | 
3998  |  |  * passing them to the server and it allows the server to check chains before  | 
3999  |  |  * attempting to use them.  | 
4000  |  |  */  | 
4001  |  |  | 
4002  |  | /* Flags which need to be set for a certificate when strict mode not set */  | 
4003  |  |  | 
4004  |  | #define CERT_PKEY_VALID_FLAGS \  | 
4005  | 0  |         (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)  | 
4006  |  | /* Strict mode flags */  | 
4007  |  | #define CERT_PKEY_STRICT_FLAGS \  | 
4008  | 0  |          (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \  | 
4009  | 0  |          | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)  | 
4010  |  |  | 
4011  |  | int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk,  | 
4012  |  |                      STACK_OF(X509) *chain, int idx)  | 
4013  | 0  | { | 
4014  | 0  |     int i;  | 
4015  | 0  |     int rv = 0;  | 
4016  | 0  |     int check_flags = 0, strict_mode;  | 
4017  | 0  |     CERT_PKEY *cpk = NULL;  | 
4018  | 0  |     CERT *c = s->cert;  | 
4019  | 0  |     uint32_t *pvalid;  | 
4020  | 0  |     unsigned int suiteb_flags = tls1_suiteb(s);  | 
4021  |  |  | 
4022  |  |     /*  | 
4023  |  |      * Meaning of idx:  | 
4024  |  |      * idx == -1 means SSL_check_chain() invocation  | 
4025  |  |      * idx == -2 means checking client certificate chains  | 
4026  |  |      * idx >= 0 means checking SSL_PKEY index  | 
4027  |  |      *  | 
4028  |  |      * For RPK, where there may be no cert, we ignore -1  | 
4029  |  |      */  | 
4030  | 0  |     if (idx != -1) { | 
4031  | 0  |         if (idx == -2) { | 
4032  | 0  |             cpk = c->key;  | 
4033  | 0  |             idx = (int)(cpk - c->pkeys);  | 
4034  | 0  |         } else  | 
4035  | 0  |             cpk = c->pkeys + idx;  | 
4036  | 0  |         pvalid = s->s3.tmp.valid_flags + idx;  | 
4037  | 0  |         x = cpk->x509;  | 
4038  | 0  |         pk = cpk->privatekey;  | 
4039  | 0  |         chain = cpk->chain;  | 
4040  | 0  |         strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;  | 
4041  | 0  |         if (tls12_rpk_and_privkey(s, idx)) { | 
4042  | 0  |             if (EVP_PKEY_is_a(pk, "EC") && !tls1_check_pkey_comp(s, pk))  | 
4043  | 0  |                 return 0;  | 
4044  | 0  |             *pvalid = rv = CERT_PKEY_RPK;  | 
4045  | 0  |             return rv;  | 
4046  | 0  |         }  | 
4047  |  |         /* If no cert or key, forget it */  | 
4048  | 0  |         if (x == NULL || pk == NULL)  | 
4049  | 0  |             goto end;  | 
4050  | 0  |     } else { | 
4051  | 0  |         size_t certidx;  | 
4052  |  | 
  | 
4053  | 0  |         if (x == NULL || pk == NULL)  | 
4054  | 0  |             return 0;  | 
4055  |  |  | 
4056  | 0  |         if (ssl_cert_lookup_by_pkey(pk, &certidx,  | 
4057  | 0  |                                     SSL_CONNECTION_GET_CTX(s)) == NULL)  | 
4058  | 0  |             return 0;  | 
4059  | 0  |         idx = (int)certidx;  | 
4060  | 0  |         pvalid = s->s3.tmp.valid_flags + idx;  | 
4061  |  | 
  | 
4062  | 0  |         if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)  | 
4063  | 0  |             check_flags = CERT_PKEY_STRICT_FLAGS;  | 
4064  | 0  |         else  | 
4065  | 0  |             check_flags = CERT_PKEY_VALID_FLAGS;  | 
4066  | 0  |         strict_mode = 1;  | 
4067  | 0  |     }  | 
4068  |  |  | 
4069  | 0  |     if (suiteb_flags) { | 
4070  | 0  |         int ok;  | 
4071  | 0  |         if (check_flags)  | 
4072  | 0  |             check_flags |= CERT_PKEY_SUITEB;  | 
4073  | 0  |         ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);  | 
4074  | 0  |         if (ok == X509_V_OK)  | 
4075  | 0  |             rv |= CERT_PKEY_SUITEB;  | 
4076  | 0  |         else if (!check_flags)  | 
4077  | 0  |             goto end;  | 
4078  | 0  |     }  | 
4079  |  |  | 
4080  |  |     /*  | 
4081  |  |      * Check all signature algorithms are consistent with signature  | 
4082  |  |      * algorithms extension if TLS 1.2 or later and strict mode.  | 
4083  |  |      */  | 
4084  | 0  |     if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION  | 
4085  | 0  |         && strict_mode) { | 
4086  | 0  |         int default_nid;  | 
4087  | 0  |         int rsign = 0;  | 
4088  |  | 
  | 
4089  | 0  |         if (s->s3.tmp.peer_cert_sigalgs != NULL  | 
4090  | 0  |                 || s->s3.tmp.peer_sigalgs != NULL) { | 
4091  | 0  |             default_nid = 0;  | 
4092  |  |         /* If no sigalgs extension use defaults from RFC5246 */  | 
4093  | 0  |         } else { | 
4094  | 0  |             switch (idx) { | 
4095  | 0  |             case SSL_PKEY_RSA:  | 
4096  | 0  |                 rsign = EVP_PKEY_RSA;  | 
4097  | 0  |                 default_nid = NID_sha1WithRSAEncryption;  | 
4098  | 0  |                 break;  | 
4099  |  |  | 
4100  | 0  |             case SSL_PKEY_DSA_SIGN:  | 
4101  | 0  |                 rsign = EVP_PKEY_DSA;  | 
4102  | 0  |                 default_nid = NID_dsaWithSHA1;  | 
4103  | 0  |                 break;  | 
4104  |  |  | 
4105  | 0  |             case SSL_PKEY_ECC:  | 
4106  | 0  |                 rsign = EVP_PKEY_EC;  | 
4107  | 0  |                 default_nid = NID_ecdsa_with_SHA1;  | 
4108  | 0  |                 break;  | 
4109  |  |  | 
4110  | 0  |             case SSL_PKEY_GOST01:  | 
4111  | 0  |                 rsign = NID_id_GostR3410_2001;  | 
4112  | 0  |                 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;  | 
4113  | 0  |                 break;  | 
4114  |  |  | 
4115  | 0  |             case SSL_PKEY_GOST12_256:  | 
4116  | 0  |                 rsign = NID_id_GostR3410_2012_256;  | 
4117  | 0  |                 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;  | 
4118  | 0  |                 break;  | 
4119  |  |  | 
4120  | 0  |             case SSL_PKEY_GOST12_512:  | 
4121  | 0  |                 rsign = NID_id_GostR3410_2012_512;  | 
4122  | 0  |                 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;  | 
4123  | 0  |                 break;  | 
4124  |  |  | 
4125  | 0  |             default:  | 
4126  | 0  |                 default_nid = -1;  | 
4127  | 0  |                 break;  | 
4128  | 0  |             }  | 
4129  | 0  |         }  | 
4130  |  |         /*  | 
4131  |  |          * If peer sent no signature algorithms extension and we have set  | 
4132  |  |          * preferred signature algorithms check we support sha1.  | 
4133  |  |          */  | 
4134  | 0  |         if (default_nid > 0 && c->conf_sigalgs) { | 
4135  | 0  |             size_t j;  | 
4136  | 0  |             const uint16_t *p = c->conf_sigalgs;  | 
4137  | 0  |             for (j = 0; j < c->conf_sigalgslen; j++, p++) { | 
4138  | 0  |                 const SIGALG_LOOKUP *lu =  | 
4139  | 0  |                     tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *p);  | 
4140  |  | 
  | 
4141  | 0  |                 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)  | 
4142  | 0  |                     break;  | 
4143  | 0  |             }  | 
4144  | 0  |             if (j == c->conf_sigalgslen) { | 
4145  | 0  |                 if (check_flags)  | 
4146  | 0  |                     goto skip_sigs;  | 
4147  | 0  |                 else  | 
4148  | 0  |                     goto end;  | 
4149  | 0  |             }  | 
4150  | 0  |         }  | 
4151  |  |         /* Check signature algorithm of each cert in chain */  | 
4152  | 0  |         if (SSL_CONNECTION_IS_TLS13(s)) { | 
4153  |  |             /*  | 
4154  |  |              * We only get here if the application has called SSL_check_chain(),  | 
4155  |  |              * so check_flags is always set.  | 
4156  |  |              */  | 
4157  | 0  |             if (find_sig_alg(s, x, pk) != NULL)  | 
4158  | 0  |                 rv |= CERT_PKEY_EE_SIGNATURE;  | 
4159  | 0  |         } else if (!tls1_check_sig_alg(s, x, default_nid)) { | 
4160  | 0  |             if (!check_flags)  | 
4161  | 0  |                 goto end;  | 
4162  | 0  |         } else  | 
4163  | 0  |             rv |= CERT_PKEY_EE_SIGNATURE;  | 
4164  | 0  |         rv |= CERT_PKEY_CA_SIGNATURE;  | 
4165  | 0  |         for (i = 0; i < sk_X509_num(chain); i++) { | 
4166  | 0  |             if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) { | 
4167  | 0  |                 if (check_flags) { | 
4168  | 0  |                     rv &= ~CERT_PKEY_CA_SIGNATURE;  | 
4169  | 0  |                     break;  | 
4170  | 0  |                 } else  | 
4171  | 0  |                     goto end;  | 
4172  | 0  |             }  | 
4173  | 0  |         }  | 
4174  | 0  |     }  | 
4175  |  |     /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */  | 
4176  | 0  |     else if (check_flags)  | 
4177  | 0  |         rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;  | 
4178  | 0  |  skip_sigs:  | 
4179  |  |     /* Check cert parameters are consistent */  | 
4180  | 0  |     if (tls1_check_cert_param(s, x, 1))  | 
4181  | 0  |         rv |= CERT_PKEY_EE_PARAM;  | 
4182  | 0  |     else if (!check_flags)  | 
4183  | 0  |         goto end;  | 
4184  | 0  |     if (!s->server)  | 
4185  | 0  |         rv |= CERT_PKEY_CA_PARAM;  | 
4186  |  |     /* In strict mode check rest of chain too */  | 
4187  | 0  |     else if (strict_mode) { | 
4188  | 0  |         rv |= CERT_PKEY_CA_PARAM;  | 
4189  | 0  |         for (i = 0; i < sk_X509_num(chain); i++) { | 
4190  | 0  |             X509 *ca = sk_X509_value(chain, i);  | 
4191  | 0  |             if (!tls1_check_cert_param(s, ca, 0)) { | 
4192  | 0  |                 if (check_flags) { | 
4193  | 0  |                     rv &= ~CERT_PKEY_CA_PARAM;  | 
4194  | 0  |                     break;  | 
4195  | 0  |                 } else  | 
4196  | 0  |                     goto end;  | 
4197  | 0  |             }  | 
4198  | 0  |         }  | 
4199  | 0  |     }  | 
4200  | 0  |     if (!s->server && strict_mode) { | 
4201  | 0  |         STACK_OF(X509_NAME) *ca_dn;  | 
4202  | 0  |         int check_type = 0;  | 
4203  |  | 
  | 
4204  | 0  |         if (EVP_PKEY_is_a(pk, "RSA"))  | 
4205  | 0  |             check_type = TLS_CT_RSA_SIGN;  | 
4206  | 0  |         else if (EVP_PKEY_is_a(pk, "DSA"))  | 
4207  | 0  |             check_type = TLS_CT_DSS_SIGN;  | 
4208  | 0  |         else if (EVP_PKEY_is_a(pk, "EC"))  | 
4209  | 0  |             check_type = TLS_CT_ECDSA_SIGN;  | 
4210  |  | 
  | 
4211  | 0  |         if (check_type) { | 
4212  | 0  |             const uint8_t *ctypes = s->s3.tmp.ctype;  | 
4213  | 0  |             size_t j;  | 
4214  |  | 
  | 
4215  | 0  |             for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) { | 
4216  | 0  |                 if (*ctypes == check_type) { | 
4217  | 0  |                     rv |= CERT_PKEY_CERT_TYPE;  | 
4218  | 0  |                     break;  | 
4219  | 0  |                 }  | 
4220  | 0  |             }  | 
4221  | 0  |             if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)  | 
4222  | 0  |                 goto end;  | 
4223  | 0  |         } else { | 
4224  | 0  |             rv |= CERT_PKEY_CERT_TYPE;  | 
4225  | 0  |         }  | 
4226  |  |  | 
4227  | 0  |         ca_dn = s->s3.tmp.peer_ca_names;  | 
4228  |  | 
  | 
4229  | 0  |         if (ca_dn == NULL  | 
4230  | 0  |             || sk_X509_NAME_num(ca_dn) == 0  | 
4231  | 0  |             || ssl_check_ca_name(ca_dn, x))  | 
4232  | 0  |             rv |= CERT_PKEY_ISSUER_NAME;  | 
4233  | 0  |         else  | 
4234  | 0  |             for (i = 0; i < sk_X509_num(chain); i++) { | 
4235  | 0  |                 X509 *xtmp = sk_X509_value(chain, i);  | 
4236  |  | 
  | 
4237  | 0  |                 if (ssl_check_ca_name(ca_dn, xtmp)) { | 
4238  | 0  |                     rv |= CERT_PKEY_ISSUER_NAME;  | 
4239  | 0  |                     break;  | 
4240  | 0  |                 }  | 
4241  | 0  |             }  | 
4242  |  | 
  | 
4243  | 0  |         if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))  | 
4244  | 0  |             goto end;  | 
4245  | 0  |     } else  | 
4246  | 0  |         rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;  | 
4247  |  |  | 
4248  | 0  |     if (!check_flags || (rv & check_flags) == check_flags)  | 
4249  | 0  |         rv |= CERT_PKEY_VALID;  | 
4250  |  | 
  | 
4251  | 0  |  end:  | 
4252  |  | 
  | 
4253  | 0  |     if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION)  | 
4254  | 0  |         rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);  | 
4255  | 0  |     else  | 
4256  | 0  |         rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;  | 
4257  |  |  | 
4258  |  |     /*  | 
4259  |  |      * When checking a CERT_PKEY structure all flags are irrelevant if the  | 
4260  |  |      * chain is invalid.  | 
4261  |  |      */  | 
4262  | 0  |     if (!check_flags) { | 
4263  | 0  |         if (rv & CERT_PKEY_VALID) { | 
4264  | 0  |             *pvalid = rv;  | 
4265  | 0  |         } else { | 
4266  |  |             /* Preserve sign and explicit sign flag, clear rest */  | 
4267  | 0  |             *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;  | 
4268  | 0  |             return 0;  | 
4269  | 0  |         }  | 
4270  | 0  |     }  | 
4271  | 0  |     return rv;  | 
4272  | 0  | }  | 
4273  |  |  | 
4274  |  | /* Set validity of certificates in an SSL structure */  | 
4275  |  | void tls1_set_cert_validity(SSL_CONNECTION *s)  | 
4276  | 0  | { | 
4277  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);  | 
4278  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);  | 
4279  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);  | 
4280  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);  | 
4281  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);  | 
4282  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);  | 
4283  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);  | 
4284  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);  | 
4285  | 0  |     tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448);  | 
4286  | 0  | }  | 
4287  |  |  | 
4288  |  | /* User level utility function to check a chain is suitable */  | 
4289  |  | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)  | 
4290  | 0  | { | 
4291  | 0  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);  | 
4292  |  | 
  | 
4293  | 0  |     if (sc == NULL)  | 
4294  | 0  |         return 0;  | 
4295  |  |  | 
4296  | 0  |     return tls1_check_chain(sc, x, pk, chain, -1);  | 
4297  | 0  | }  | 
4298  |  |  | 
4299  |  | EVP_PKEY *ssl_get_auto_dh(SSL_CONNECTION *s)  | 
4300  | 0  | { | 
4301  | 0  |     EVP_PKEY *dhp = NULL;  | 
4302  | 0  |     BIGNUM *p;  | 
4303  | 0  |     int dh_secbits = 80, sec_level_bits;  | 
4304  | 0  |     EVP_PKEY_CTX *pctx = NULL;  | 
4305  | 0  |     OSSL_PARAM_BLD *tmpl = NULL;  | 
4306  | 0  |     OSSL_PARAM *params = NULL;  | 
4307  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
4308  |  | 
  | 
4309  | 0  |     if (s->cert->dh_tmp_auto != 2) { | 
4310  | 0  |         if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { | 
4311  | 0  |             if (s->s3.tmp.new_cipher->strength_bits == 256)  | 
4312  | 0  |                 dh_secbits = 128;  | 
4313  | 0  |             else  | 
4314  | 0  |                 dh_secbits = 80;  | 
4315  | 0  |         } else { | 
4316  | 0  |             if (s->s3.tmp.cert == NULL)  | 
4317  | 0  |                 return NULL;  | 
4318  | 0  |             dh_secbits = EVP_PKEY_get_security_bits(s->s3.tmp.cert->privatekey);  | 
4319  | 0  |         }  | 
4320  | 0  |     }  | 
4321  |  |  | 
4322  |  |     /* Do not pick a prime that is too weak for the current security level */  | 
4323  | 0  |     sec_level_bits = ssl_get_security_level_bits(SSL_CONNECTION_GET_SSL(s),  | 
4324  | 0  |                                                  NULL, NULL);  | 
4325  | 0  |     if (dh_secbits < sec_level_bits)  | 
4326  | 0  |         dh_secbits = sec_level_bits;  | 
4327  |  | 
  | 
4328  | 0  |     if (dh_secbits >= 192)  | 
4329  | 0  |         p = BN_get_rfc3526_prime_8192(NULL);  | 
4330  | 0  |     else if (dh_secbits >= 152)  | 
4331  | 0  |         p = BN_get_rfc3526_prime_4096(NULL);  | 
4332  | 0  |     else if (dh_secbits >= 128)  | 
4333  | 0  |         p = BN_get_rfc3526_prime_3072(NULL);  | 
4334  | 0  |     else if (dh_secbits >= 112)  | 
4335  | 0  |         p = BN_get_rfc3526_prime_2048(NULL);  | 
4336  | 0  |     else  | 
4337  | 0  |         p = BN_get_rfc2409_prime_1024(NULL);  | 
4338  | 0  |     if (p == NULL)  | 
4339  | 0  |         goto err;  | 
4340  |  |  | 
4341  | 0  |     pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq);  | 
4342  | 0  |     if (pctx == NULL  | 
4343  | 0  |             || EVP_PKEY_fromdata_init(pctx) != 1)  | 
4344  | 0  |         goto err;  | 
4345  |  |  | 
4346  | 0  |     tmpl = OSSL_PARAM_BLD_new();  | 
4347  | 0  |     if (tmpl == NULL  | 
4348  | 0  |             || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)  | 
4349  | 0  |             || !OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2))  | 
4350  | 0  |         goto err;  | 
4351  |  |  | 
4352  | 0  |     params = OSSL_PARAM_BLD_to_param(tmpl);  | 
4353  | 0  |     if (params == NULL  | 
4354  | 0  |             || EVP_PKEY_fromdata(pctx, &dhp, EVP_PKEY_KEY_PARAMETERS, params) != 1)  | 
4355  | 0  |         goto err;  | 
4356  |  |  | 
4357  | 0  | err:  | 
4358  | 0  |     OSSL_PARAM_free(params);  | 
4359  | 0  |     OSSL_PARAM_BLD_free(tmpl);  | 
4360  | 0  |     EVP_PKEY_CTX_free(pctx);  | 
4361  | 0  |     BN_free(p);  | 
4362  | 0  |     return dhp;  | 
4363  | 0  | }  | 
4364  |  |  | 
4365  |  | static int ssl_security_cert_key(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x,  | 
4366  |  |                                  int op)  | 
4367  | 0  | { | 
4368  | 0  |     int secbits = -1;  | 
4369  | 0  |     EVP_PKEY *pkey = X509_get0_pubkey(x);  | 
4370  |  | 
  | 
4371  | 0  |     if (pkey) { | 
4372  |  |         /*  | 
4373  |  |          * If no parameters this will return -1 and fail using the default  | 
4374  |  |          * security callback for any non-zero security level. This will  | 
4375  |  |          * reject keys which omit parameters but this only affects DSA and  | 
4376  |  |          * omission of parameters is never (?) done in practice.  | 
4377  |  |          */  | 
4378  | 0  |         secbits = EVP_PKEY_get_security_bits(pkey);  | 
4379  | 0  |     }  | 
4380  | 0  |     if (s != NULL)  | 
4381  | 0  |         return ssl_security(s, op, secbits, 0, x);  | 
4382  | 0  |     else  | 
4383  | 0  |         return ssl_ctx_security(ctx, op, secbits, 0, x);  | 
4384  | 0  | }  | 
4385  |  |  | 
4386  |  | static int ssl_security_cert_sig(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x,  | 
4387  |  |                                  int op)  | 
4388  | 0  | { | 
4389  |  |     /* Lookup signature algorithm digest */  | 
4390  | 0  |     int secbits, nid, pknid;  | 
4391  |  |  | 
4392  |  |     /* Don't check signature if self signed */  | 
4393  | 0  |     if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)  | 
4394  | 0  |         return 1;  | 
4395  | 0  |     if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))  | 
4396  | 0  |         secbits = -1;  | 
4397  |  |     /* If digest NID not defined use signature NID */  | 
4398  | 0  |     if (nid == NID_undef)  | 
4399  | 0  |         nid = pknid;  | 
4400  | 0  |     if (s != NULL)  | 
4401  | 0  |         return ssl_security(s, op, secbits, nid, x);  | 
4402  | 0  |     else  | 
4403  | 0  |         return ssl_ctx_security(ctx, op, secbits, nid, x);  | 
4404  | 0  | }  | 
4405  |  |  | 
4406  |  | int ssl_security_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, int vfy,  | 
4407  |  |                       int is_ee)  | 
4408  | 0  | { | 
4409  | 0  |     if (vfy)  | 
4410  | 0  |         vfy = SSL_SECOP_PEER;  | 
4411  | 0  |     if (is_ee) { | 
4412  | 0  |         if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))  | 
4413  | 0  |             return SSL_R_EE_KEY_TOO_SMALL;  | 
4414  | 0  |     } else { | 
4415  | 0  |         if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))  | 
4416  | 0  |             return SSL_R_CA_KEY_TOO_SMALL;  | 
4417  | 0  |     }  | 
4418  | 0  |     if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))  | 
4419  | 0  |         return SSL_R_CA_MD_TOO_WEAK;  | 
4420  | 0  |     return 1;  | 
4421  | 0  | }  | 
4422  |  |  | 
4423  |  | /*  | 
4424  |  |  * Check security of a chain, if |sk| includes the end entity certificate then  | 
4425  |  |  * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending  | 
4426  |  |  * one to the peer. Return values: 1 if ok otherwise error code to use  | 
4427  |  |  */  | 
4428  |  |  | 
4429  |  | int ssl_security_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk,  | 
4430  |  |                             X509 *x, int vfy)  | 
4431  | 0  | { | 
4432  | 0  |     int rv, start_idx, i;  | 
4433  |  | 
  | 
4434  | 0  |     if (x == NULL) { | 
4435  | 0  |         x = sk_X509_value(sk, 0);  | 
4436  | 0  |         if (x == NULL)  | 
4437  | 0  |             return ERR_R_INTERNAL_ERROR;  | 
4438  | 0  |         start_idx = 1;  | 
4439  | 0  |     } else  | 
4440  | 0  |         start_idx = 0;  | 
4441  |  |  | 
4442  | 0  |     rv = ssl_security_cert(s, NULL, x, vfy, 1);  | 
4443  | 0  |     if (rv != 1)  | 
4444  | 0  |         return rv;  | 
4445  |  |  | 
4446  | 0  |     for (i = start_idx; i < sk_X509_num(sk); i++) { | 
4447  | 0  |         x = sk_X509_value(sk, i);  | 
4448  | 0  |         rv = ssl_security_cert(s, NULL, x, vfy, 0);  | 
4449  | 0  |         if (rv != 1)  | 
4450  | 0  |             return rv;  | 
4451  | 0  |     }  | 
4452  | 0  |     return 1;  | 
4453  | 0  | }  | 
4454  |  |  | 
4455  |  | /*  | 
4456  |  |  * For TLS 1.2 servers check if we have a certificate which can be used  | 
4457  |  |  * with the signature algorithm "lu" and return index of certificate.  | 
4458  |  |  */  | 
4459  |  |  | 
4460  |  | static int tls12_get_cert_sigalg_idx(const SSL_CONNECTION *s,  | 
4461  |  |                                      const SIGALG_LOOKUP *lu)  | 
4462  | 0  | { | 
4463  | 0  |     int sig_idx = lu->sig_idx;  | 
4464  | 0  |     const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx,  | 
4465  | 0  |                                                         SSL_CONNECTION_GET_CTX(s));  | 
4466  |  |  | 
4467  |  |     /* If not recognised or not supported by cipher mask it is not suitable */  | 
4468  | 0  |     if (clu == NULL  | 
4469  | 0  |             || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0  | 
4470  | 0  |             || (clu->nid == EVP_PKEY_RSA_PSS  | 
4471  | 0  |                 && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0))  | 
4472  | 0  |         return -1;  | 
4473  |  |  | 
4474  |  |     /* If doing RPK, the CERT_PKEY won't be "valid" */  | 
4475  | 0  |     if (tls12_rpk_and_privkey(s, sig_idx))  | 
4476  | 0  |         return  s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_RPK ? sig_idx : -1;  | 
4477  |  |  | 
4478  | 0  |     return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;  | 
4479  | 0  | }  | 
4480  |  |  | 
4481  |  | /*  | 
4482  |  |  * Checks the given cert against signature_algorithm_cert restrictions sent by  | 
4483  |  |  * the peer (if any) as well as whether the hash from the sigalg is usable with  | 
4484  |  |  * the key.  | 
4485  |  |  * Returns true if the cert is usable and false otherwise.  | 
4486  |  |  */  | 
4487  |  | static int check_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig,  | 
4488  |  |                              X509 *x, EVP_PKEY *pkey)  | 
4489  | 0  | { | 
4490  | 0  |     const SIGALG_LOOKUP *lu;  | 
4491  | 0  |     int mdnid, pknid, supported;  | 
4492  | 0  |     size_t i;  | 
4493  | 0  |     const char *mdname = NULL;  | 
4494  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
4495  |  |  | 
4496  |  |     /*  | 
4497  |  |      * If the given EVP_PKEY cannot support signing with this digest,  | 
4498  |  |      * the answer is simply 'no'.  | 
4499  |  |      */  | 
4500  | 0  |     if (sig->hash != NID_undef)  | 
4501  | 0  |         mdname = OBJ_nid2sn(sig->hash);  | 
4502  | 0  |     supported = EVP_PKEY_digestsign_supports_digest(pkey, sctx->libctx,  | 
4503  | 0  |                                                     mdname,  | 
4504  | 0  |                                                     sctx->propq);  | 
4505  | 0  |     if (supported <= 0)  | 
4506  | 0  |         return 0;  | 
4507  |  |  | 
4508  |  |     /*  | 
4509  |  |      * The TLS 1.3 signature_algorithms_cert extension places restrictions  | 
4510  |  |      * on the sigalg with which the certificate was signed (by its issuer).  | 
4511  |  |      */  | 
4512  | 0  |     if (s->s3.tmp.peer_cert_sigalgs != NULL) { | 
4513  | 0  |         if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL))  | 
4514  | 0  |             return 0;  | 
4515  | 0  |         for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) { | 
4516  | 0  |             lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s),  | 
4517  | 0  |                                     s->s3.tmp.peer_cert_sigalgs[i]);  | 
4518  | 0  |             if (lu == NULL)  | 
4519  | 0  |                 continue;  | 
4520  |  |  | 
4521  |  |             /*  | 
4522  |  |              * This does not differentiate between the  | 
4523  |  |              * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not  | 
4524  |  |              * have a chain here that lets us look at the key OID in the  | 
4525  |  |              * signing certificate.  | 
4526  |  |              */  | 
4527  | 0  |             if (mdnid == lu->hash && pknid == lu->sig)  | 
4528  | 0  |                 return 1;  | 
4529  | 0  |         }  | 
4530  | 0  |         return 0;  | 
4531  | 0  |     }  | 
4532  |  |  | 
4533  |  |     /*  | 
4534  |  |      * Without signat_algorithms_cert, any certificate for which we have  | 
4535  |  |      * a viable public key is permitted.  | 
4536  |  |      */  | 
4537  | 0  |     return 1;  | 
4538  | 0  | }  | 
4539  |  |  | 
4540  |  | /*  | 
4541  |  |  * Returns true if |s| has a usable certificate configured for use  | 
4542  |  |  * with signature scheme |sig|.  | 
4543  |  |  * "Usable" includes a check for presence as well as applying  | 
4544  |  |  * the signature_algorithm_cert restrictions sent by the peer (if any).  | 
4545  |  |  * Returns false if no usable certificate is found.  | 
4546  |  |  */  | 
4547  |  | static int has_usable_cert(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, int idx)  | 
4548  | 0  | { | 
4549  |  |     /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */  | 
4550  | 0  |     if (idx == -1)  | 
4551  | 0  |         idx = sig->sig_idx;  | 
4552  | 0  |     if (!ssl_has_cert(s, idx))  | 
4553  | 0  |         return 0;  | 
4554  |  |  | 
4555  | 0  |     return check_cert_usable(s, sig, s->cert->pkeys[idx].x509,  | 
4556  | 0  |                              s->cert->pkeys[idx].privatekey);  | 
4557  | 0  | }  | 
4558  |  |  | 
4559  |  | /*  | 
4560  |  |  * Returns true if the supplied cert |x| and key |pkey| is usable with the  | 
4561  |  |  * specified signature scheme |sig|, or false otherwise.  | 
4562  |  |  */  | 
4563  |  | static int is_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, X509 *x,  | 
4564  |  |                           EVP_PKEY *pkey)  | 
4565  | 0  | { | 
4566  | 0  |     size_t idx;  | 
4567  |  | 
  | 
4568  | 0  |     if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL)  | 
4569  | 0  |         return 0;  | 
4570  |  |  | 
4571  |  |     /* Check the key is consistent with the sig alg */  | 
4572  | 0  |     if ((int)idx != sig->sig_idx)  | 
4573  | 0  |         return 0;  | 
4574  |  |  | 
4575  | 0  |     return check_cert_usable(s, sig, x, pkey);  | 
4576  | 0  | }  | 
4577  |  |  | 
4578  |  | /*  | 
4579  |  |  * Find a signature scheme that works with the supplied certificate |x| and key  | 
4580  |  |  * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our  | 
4581  |  |  * available certs/keys to find one that works.  | 
4582  |  |  */  | 
4583  |  | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x,  | 
4584  |  |                                          EVP_PKEY *pkey)  | 
4585  | 0  | { | 
4586  | 0  |     const SIGALG_LOOKUP *lu = NULL;  | 
4587  | 0  |     size_t i;  | 
4588  | 0  |     int curve = -1;  | 
4589  | 0  |     EVP_PKEY *tmppkey;  | 
4590  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
4591  |  |  | 
4592  |  |     /* Look for a shared sigalgs matching possible certificates */  | 
4593  | 0  |     for (i = 0; i < s->shared_sigalgslen; i++) { | 
4594  |  |         /* Skip SHA1, SHA224, DSA and RSA if not PSS */  | 
4595  | 0  |         lu = s->shared_sigalgs[i];  | 
4596  | 0  |         if (lu->hash == NID_sha1  | 
4597  | 0  |             || lu->hash == NID_sha224  | 
4598  | 0  |             || lu->sig == EVP_PKEY_DSA  | 
4599  | 0  |             || lu->sig == EVP_PKEY_RSA  | 
4600  | 0  |             || !tls_sigalg_compat(s, lu))  | 
4601  | 0  |             continue;  | 
4602  |  |  | 
4603  |  |         /* Check that we have a cert, and signature_algorithms_cert */  | 
4604  | 0  |         if (!tls1_lookup_md(sctx, lu, NULL))  | 
4605  | 0  |             continue;  | 
4606  | 0  |         if ((pkey == NULL && !has_usable_cert(s, lu, -1))  | 
4607  | 0  |                 || (pkey != NULL && !is_cert_usable(s, lu, x, pkey)))  | 
4608  | 0  |             continue;  | 
4609  |  |  | 
4610  | 0  |         tmppkey = (pkey != NULL) ? pkey  | 
4611  | 0  |                                  : s->cert->pkeys[lu->sig_idx].privatekey;  | 
4612  |  | 
  | 
4613  | 0  |         if (lu->sig == EVP_PKEY_EC) { | 
4614  | 0  |             if (curve == -1)  | 
4615  | 0  |                 curve = ssl_get_EC_curve_nid(tmppkey);  | 
4616  | 0  |             if (lu->curve != NID_undef && curve != lu->curve)  | 
4617  | 0  |                 continue;  | 
4618  | 0  |         } else if (lu->sig == EVP_PKEY_RSA_PSS) { | 
4619  |  |             /* validate that key is large enough for the signature algorithm */  | 
4620  | 0  |             if (!rsa_pss_check_min_key_size(sctx, tmppkey, lu))  | 
4621  | 0  |                 continue;  | 
4622  | 0  |         }  | 
4623  | 0  |         break;  | 
4624  | 0  |     }  | 
4625  |  | 
  | 
4626  | 0  |     if (i == s->shared_sigalgslen)  | 
4627  | 0  |         return NULL;  | 
4628  |  |  | 
4629  | 0  |     return lu;  | 
4630  | 0  | }  | 
4631  |  |  | 
4632  |  | /*  | 
4633  |  |  * Choose an appropriate signature algorithm based on available certificates  | 
4634  |  |  * Sets chosen certificate and signature algorithm.  | 
4635  |  |  *  | 
4636  |  |  * For servers if we fail to find a required certificate it is a fatal error,  | 
4637  |  |  * an appropriate error code is set and a TLS alert is sent.  | 
4638  |  |  *  | 
4639  |  |  * For clients fatalerrs is set to 0. If a certificate is not suitable it is not  | 
4640  |  |  * a fatal error: we will either try another certificate or not present one  | 
4641  |  |  * to the server. In this case no error is set.  | 
4642  |  |  */  | 
4643  |  | int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs)  | 
4644  | 0  | { | 
4645  | 0  |     const SIGALG_LOOKUP *lu = NULL;  | 
4646  | 0  |     int sig_idx = -1;  | 
4647  |  | 
  | 
4648  | 0  |     s->s3.tmp.cert = NULL;  | 
4649  | 0  |     s->s3.tmp.sigalg = NULL;  | 
4650  |  | 
  | 
4651  | 0  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
4652  | 0  |         lu = find_sig_alg(s, NULL, NULL);  | 
4653  | 0  |         if (lu == NULL) { | 
4654  | 0  |             if (!fatalerrs)  | 
4655  | 0  |                 return 1;  | 
4656  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
4657  | 0  |                      SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);  | 
4658  | 0  |             return 0;  | 
4659  | 0  |         }  | 
4660  | 0  |     } else { | 
4661  |  |         /* If ciphersuite doesn't require a cert nothing to do */  | 
4662  | 0  |         if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT))  | 
4663  | 0  |             return 1;  | 
4664  | 0  |         if (!s->server && !ssl_has_cert(s, (int)(s->cert->key - s->cert->pkeys)))  | 
4665  | 0  |                 return 1;  | 
4666  |  |  | 
4667  | 0  |         if (SSL_USE_SIGALGS(s)) { | 
4668  | 0  |             size_t i;  | 
4669  | 0  |             if (s->s3.tmp.peer_sigalgs != NULL) { | 
4670  | 0  |                 int curve = -1;  | 
4671  | 0  |                 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
4672  |  |  | 
4673  |  |                 /* For Suite B need to match signature algorithm to curve */  | 
4674  | 0  |                 if (tls1_suiteb(s))  | 
4675  | 0  |                     curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC]  | 
4676  | 0  |                                                  .privatekey);  | 
4677  |  |  | 
4678  |  |                 /*  | 
4679  |  |                  * Find highest preference signature algorithm matching  | 
4680  |  |                  * cert type  | 
4681  |  |                  */  | 
4682  | 0  |                 for (i = 0; i < s->shared_sigalgslen; i++) { | 
4683  |  |                     /* Check the sigalg version bounds */  | 
4684  | 0  |                     lu = s->shared_sigalgs[i];  | 
4685  | 0  |                     if (!tls_sigalg_compat(s, lu))  | 
4686  | 0  |                         continue;  | 
4687  | 0  |                     if (s->server) { | 
4688  | 0  |                         if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)  | 
4689  | 0  |                             continue;  | 
4690  | 0  |                     } else { | 
4691  | 0  |                         int cc_idx = (int)(s->cert->key - s->cert->pkeys);  | 
4692  |  | 
  | 
4693  | 0  |                         sig_idx = lu->sig_idx;  | 
4694  | 0  |                         if (cc_idx != sig_idx)  | 
4695  | 0  |                             continue;  | 
4696  | 0  |                     }  | 
4697  |  |                     /* Check that we have a cert, and sig_algs_cert */  | 
4698  | 0  |                     if (!has_usable_cert(s, lu, sig_idx))  | 
4699  | 0  |                         continue;  | 
4700  | 0  |                     if (lu->sig == EVP_PKEY_RSA_PSS) { | 
4701  |  |                         /* validate that key is large enough for the signature algorithm */  | 
4702  | 0  |                         EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;  | 
4703  |  | 
  | 
4704  | 0  |                         if (!rsa_pss_check_min_key_size(sctx, pkey, lu))  | 
4705  | 0  |                             continue;  | 
4706  | 0  |                     }  | 
4707  | 0  |                     if (curve == -1 || lu->curve == curve)  | 
4708  | 0  |                         break;  | 
4709  | 0  |                 }  | 
4710  | 0  | #ifndef OPENSSL_NO_GOST  | 
4711  |  |                 /*  | 
4712  |  |                  * Some Windows-based implementations do not send GOST algorithms indication  | 
4713  |  |                  * in supported_algorithms extension, so when we have GOST-based ciphersuite,  | 
4714  |  |                  * we have to assume GOST support.  | 
4715  |  |                  */  | 
4716  | 0  |                 if (i == s->shared_sigalgslen  | 
4717  | 0  |                     && (s->s3.tmp.new_cipher->algorithm_auth  | 
4718  | 0  |                         & (SSL_aGOST01 | SSL_aGOST12)) != 0) { | 
4719  | 0  |                   if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { | 
4720  | 0  |                     if (!fatalerrs)  | 
4721  | 0  |                       return 1;  | 
4722  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
4723  | 0  |                              SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);  | 
4724  | 0  |                     return 0;  | 
4725  | 0  |                   } else { | 
4726  | 0  |                     i = 0;  | 
4727  | 0  |                     sig_idx = lu->sig_idx;  | 
4728  | 0  |                   }  | 
4729  | 0  |                 }  | 
4730  | 0  | #endif  | 
4731  | 0  |                 if (i == s->shared_sigalgslen) { | 
4732  | 0  |                     if (!fatalerrs)  | 
4733  | 0  |                         return 1;  | 
4734  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
4735  | 0  |                              SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);  | 
4736  | 0  |                     return 0;  | 
4737  | 0  |                 }  | 
4738  | 0  |             } else { | 
4739  |  |                 /*  | 
4740  |  |                  * If we have no sigalg use defaults  | 
4741  |  |                  */  | 
4742  | 0  |                 const uint16_t *sent_sigs;  | 
4743  | 0  |                 size_t sent_sigslen;  | 
4744  |  | 
  | 
4745  | 0  |                 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { | 
4746  | 0  |                     if (!fatalerrs)  | 
4747  | 0  |                         return 1;  | 
4748  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
4749  | 0  |                              SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);  | 
4750  | 0  |                     return 0;  | 
4751  | 0  |                 }  | 
4752  |  |  | 
4753  |  |                 /* Check signature matches a type we sent */  | 
4754  | 0  |                 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);  | 
4755  | 0  |                 for (i = 0; i < sent_sigslen; i++, sent_sigs++) { | 
4756  | 0  |                     if (lu->sigalg == *sent_sigs  | 
4757  | 0  |                             && has_usable_cert(s, lu, lu->sig_idx))  | 
4758  | 0  |                         break;  | 
4759  | 0  |                 }  | 
4760  | 0  |                 if (i == sent_sigslen) { | 
4761  | 0  |                     if (!fatalerrs)  | 
4762  | 0  |                         return 1;  | 
4763  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
4764  | 0  |                              SSL_R_WRONG_SIGNATURE_TYPE);  | 
4765  | 0  |                     return 0;  | 
4766  | 0  |                 }  | 
4767  | 0  |             }  | 
4768  | 0  |         } else { | 
4769  | 0  |             if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { | 
4770  | 0  |                 if (!fatalerrs)  | 
4771  | 0  |                     return 1;  | 
4772  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
4773  | 0  |                          SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);  | 
4774  | 0  |                 return 0;  | 
4775  | 0  |             }  | 
4776  | 0  |         }  | 
4777  | 0  |     }  | 
4778  | 0  |     if (sig_idx == -1)  | 
4779  | 0  |         sig_idx = lu->sig_idx;  | 
4780  | 0  |     s->s3.tmp.cert = &s->cert->pkeys[sig_idx];  | 
4781  | 0  |     s->cert->key = s->s3.tmp.cert;  | 
4782  | 0  |     s->s3.tmp.sigalg = lu;  | 
4783  | 0  |     return 1;  | 
4784  | 0  | }  | 
4785  |  |  | 
4786  |  | int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)  | 
4787  | 0  | { | 
4788  | 0  |     if (mode != TLSEXT_max_fragment_length_DISABLED  | 
4789  | 0  |             && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { | 
4790  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);  | 
4791  | 0  |         return 0;  | 
4792  | 0  |     }  | 
4793  |  |  | 
4794  | 0  |     ctx->ext.max_fragment_len_mode = mode;  | 
4795  | 0  |     return 1;  | 
4796  | 0  | }  | 
4797  |  |  | 
4798  |  | int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)  | 
4799  | 0  | { | 
4800  | 0  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);  | 
4801  |  | 
  | 
4802  | 0  |     if (sc == NULL  | 
4803  | 0  |         || (IS_QUIC(ssl) && mode != TLSEXT_max_fragment_length_DISABLED))  | 
4804  | 0  |         return 0;  | 
4805  |  |  | 
4806  | 0  |     if (mode != TLSEXT_max_fragment_length_DISABLED  | 
4807  | 0  |             && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { | 
4808  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);  | 
4809  | 0  |         return 0;  | 
4810  | 0  |     }  | 
4811  |  |  | 
4812  | 0  |     sc->ext.max_fragment_len_mode = mode;  | 
4813  | 0  |     return 1;  | 
4814  | 0  | }  | 
4815  |  |  | 
4816  |  | uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)  | 
4817  | 0  | { | 
4818  | 0  |     if (session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)  | 
4819  | 0  |         return TLSEXT_max_fragment_length_DISABLED;  | 
4820  | 0  |     return session->ext.max_fragment_len_mode;  | 
4821  | 0  | }  | 
4822  |  |  | 
4823  |  | /*  | 
4824  |  |  * Helper functions for HMAC access with legacy support included.  | 
4825  |  |  */  | 
4826  |  | SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx)  | 
4827  | 0  | { | 
4828  | 0  |     SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret));  | 
4829  | 0  |     EVP_MAC *mac = NULL;  | 
4830  |  | 
  | 
4831  | 0  |     if (ret == NULL)  | 
4832  | 0  |         return NULL;  | 
4833  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4834  | 0  |     if (ctx->ext.ticket_key_evp_cb == NULL  | 
4835  | 0  |             && ctx->ext.ticket_key_cb != NULL) { | 
4836  | 0  |         if (!ssl_hmac_old_new(ret))  | 
4837  | 0  |             goto err;  | 
4838  | 0  |         return ret;  | 
4839  | 0  |     }  | 
4840  | 0  | #endif  | 
4841  | 0  |     mac = EVP_MAC_fetch(ctx->libctx, "HMAC", ctx->propq);  | 
4842  | 0  |     if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL)  | 
4843  | 0  |         goto err;  | 
4844  | 0  |     EVP_MAC_free(mac);  | 
4845  | 0  |     return ret;  | 
4846  | 0  |  err:  | 
4847  | 0  |     EVP_MAC_CTX_free(ret->ctx);  | 
4848  | 0  |     EVP_MAC_free(mac);  | 
4849  | 0  |     OPENSSL_free(ret);  | 
4850  | 0  |     return NULL;  | 
4851  | 0  | }  | 
4852  |  |  | 
4853  |  | void ssl_hmac_free(SSL_HMAC *ctx)  | 
4854  | 0  | { | 
4855  | 0  |     if (ctx != NULL) { | 
4856  | 0  |         EVP_MAC_CTX_free(ctx->ctx);  | 
4857  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4858  | 0  |         ssl_hmac_old_free(ctx);  | 
4859  | 0  | #endif  | 
4860  | 0  |         OPENSSL_free(ctx);  | 
4861  | 0  |     }  | 
4862  | 0  | }  | 
4863  |  |  | 
4864  |  | EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx)  | 
4865  | 0  | { | 
4866  | 0  |     return ctx->ctx;  | 
4867  | 0  | }  | 
4868  |  |  | 
4869  |  | int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md)  | 
4870  | 0  | { | 
4871  | 0  |     OSSL_PARAM params[2], *p = params;  | 
4872  |  | 
  | 
4873  | 0  |     if (ctx->ctx != NULL) { | 
4874  | 0  |         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0);  | 
4875  | 0  |         *p = OSSL_PARAM_construct_end();  | 
4876  | 0  |         if (EVP_MAC_init(ctx->ctx, key, len, params))  | 
4877  | 0  |             return 1;  | 
4878  | 0  |     }  | 
4879  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4880  | 0  |     if (ctx->old_ctx != NULL)  | 
4881  | 0  |         return ssl_hmac_old_init(ctx, key, len, md);  | 
4882  | 0  | #endif  | 
4883  | 0  |     return 0;  | 
4884  | 0  | }  | 
4885  |  |  | 
4886  |  | int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len)  | 
4887  | 0  | { | 
4888  | 0  |     if (ctx->ctx != NULL)  | 
4889  | 0  |         return EVP_MAC_update(ctx->ctx, data, len);  | 
4890  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4891  | 0  |     if (ctx->old_ctx != NULL)  | 
4892  | 0  |         return ssl_hmac_old_update(ctx, data, len);  | 
4893  | 0  | #endif  | 
4894  | 0  |     return 0;  | 
4895  | 0  | }  | 
4896  |  |  | 
4897  |  | int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,  | 
4898  |  |                    size_t max_size)  | 
4899  | 0  | { | 
4900  | 0  |     if (ctx->ctx != NULL)  | 
4901  | 0  |         return EVP_MAC_final(ctx->ctx, md, len, max_size);  | 
4902  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4903  | 0  |     if (ctx->old_ctx != NULL)  | 
4904  | 0  |         return ssl_hmac_old_final(ctx, md, len);  | 
4905  | 0  | #endif  | 
4906  | 0  |     return 0;  | 
4907  | 0  | }  | 
4908  |  |  | 
4909  |  | size_t ssl_hmac_size(const SSL_HMAC *ctx)  | 
4910  | 0  | { | 
4911  | 0  |     if (ctx->ctx != NULL)  | 
4912  | 0  |         return EVP_MAC_CTX_get_mac_size(ctx->ctx);  | 
4913  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4914  | 0  |     if (ctx->old_ctx != NULL)  | 
4915  | 0  |         return ssl_hmac_old_size(ctx);  | 
4916  | 0  | #endif  | 
4917  | 0  |     return 0;  | 
4918  | 0  | }  | 
4919  |  |  | 
4920  |  | int ssl_get_EC_curve_nid(const EVP_PKEY *pkey)  | 
4921  | 0  | { | 
4922  | 0  |     char gname[OSSL_MAX_NAME_SIZE];  | 
4923  |  | 
  | 
4924  | 0  |     if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) > 0)  | 
4925  | 0  |         return OBJ_txt2nid(gname);  | 
4926  |  |  | 
4927  | 0  |     return NID_undef;  | 
4928  | 0  | }  | 
4929  |  |  | 
4930  |  | __owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey,  | 
4931  |  |                                      const unsigned char *enckey,  | 
4932  |  |                                      size_t enckeylen)  | 
4933  | 0  | { | 
4934  | 0  |     if (EVP_PKEY_is_a(pkey, "DH")) { | 
4935  | 0  |         int bits = EVP_PKEY_get_bits(pkey);  | 
4936  |  | 
  | 
4937  | 0  |         if (bits <= 0 || enckeylen != (size_t)bits / 8)  | 
4938  |  |             /* the encoded key must be padded to the length of the p */  | 
4939  | 0  |             return 0;  | 
4940  | 0  |     } else if (EVP_PKEY_is_a(pkey, "EC")) { | 
4941  | 0  |         if (enckeylen < 3 /* point format and at least 1 byte for x and y */  | 
4942  | 0  |             || enckey[0] != 0x04)  | 
4943  | 0  |             return 0;  | 
4944  | 0  |     }  | 
4945  |  |  | 
4946  | 0  |     return EVP_PKEY_set1_encoded_public_key(pkey, enckey, enckeylen);  | 
4947  | 0  | }  |