/src/openssl30/ssl/ssl_cert.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved  | 
4  |  |  *  | 
5  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
6  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
7  |  |  * in the file LICENSE in the source distribution or at  | 
8  |  |  * https://www.openssl.org/source/license.html  | 
9  |  |  */  | 
10  |  |  | 
11  |  | #include <stdio.h>  | 
12  |  | #include <sys/types.h>  | 
13  |  |  | 
14  |  | #include "internal/nelem.h"  | 
15  |  | #include "internal/o_dir.h"  | 
16  |  | #include <openssl/bio.h>  | 
17  |  | #include <openssl/pem.h>  | 
18  |  | #include <openssl/store.h>  | 
19  |  | #include <openssl/x509v3.h>  | 
20  |  | #include <openssl/dh.h>  | 
21  |  | #include <openssl/bn.h>  | 
22  |  | #include <openssl/crypto.h>  | 
23  |  | #include "internal/refcount.h"  | 
24  |  | #include "ssl_local.h"  | 
25  |  | #include "ssl_cert_table.h"  | 
26  |  | #include "internal/thread_once.h"  | 
27  |  | #ifndef OPENSSL_NO_POSIX_IO  | 
28  |  | # include <sys/stat.h>  | 
29  |  | # ifdef _WIN32  | 
30  |  | #  define stat _stat  | 
31  |  | # endif  | 
32  |  | # ifndef S_ISDIR  | 
33  |  | #  define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)  | 
34  |  | # endif  | 
35  |  | #endif  | 
36  |  |  | 
37  |  |  | 
38  |  | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,  | 
39  |  |                                          int op, int bits, int nid, void *other,  | 
40  |  |                                          void *ex);  | 
41  |  |  | 
42  |  | static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;  | 
43  |  | static volatile int ssl_x509_store_ctx_idx = -1;  | 
44  |  |  | 
45  |  | DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)  | 
46  | 26  | { | 
47  | 26  |     ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,  | 
48  | 26  |                                                              "SSL for verify callback",  | 
49  | 26  |                                                              NULL, NULL, NULL);  | 
50  | 26  |     return ssl_x509_store_ctx_idx >= 0;  | 
51  | 26  | }  | 
52  |  |  | 
53  |  | int SSL_get_ex_data_X509_STORE_CTX_idx(void)  | 
54  | 93.7k  | { | 
55  |  |  | 
56  | 93.7k  |     if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))  | 
57  | 0  |         return -1;  | 
58  | 93.7k  |     return ssl_x509_store_ctx_idx;  | 
59  | 93.7k  | }  | 
60  |  |  | 
61  |  | CERT *ssl_cert_new(void)  | 
62  | 24.4k  | { | 
63  | 24.4k  |     CERT *ret = OPENSSL_zalloc(sizeof(*ret));  | 
64  |  |  | 
65  | 24.4k  |     if (ret == NULL) { | 
66  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
67  | 0  |         return NULL;  | 
68  | 0  |     }  | 
69  |  |  | 
70  | 24.4k  |     ret->key = &(ret->pkeys[SSL_PKEY_RSA]);  | 
71  | 24.4k  |     ret->references = 1;  | 
72  | 24.4k  |     ret->sec_cb = ssl_security_default_callback;  | 
73  | 24.4k  |     ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;  | 
74  | 24.4k  |     ret->sec_ex = NULL;  | 
75  | 24.4k  |     ret->lock = CRYPTO_THREAD_lock_new();  | 
76  | 24.4k  |     if (ret->lock == NULL) { | 
77  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
78  | 0  |         OPENSSL_free(ret);  | 
79  | 0  |         return NULL;  | 
80  | 0  |     }  | 
81  |  |  | 
82  | 24.4k  |     return ret;  | 
83  | 24.4k  | }  | 
84  |  |  | 
85  |  | CERT *ssl_cert_dup(CERT *cert)  | 
86  | 24.4k  | { | 
87  | 24.4k  |     CERT *ret = OPENSSL_zalloc(sizeof(*ret));  | 
88  | 24.4k  |     int i;  | 
89  |  |  | 
90  | 24.4k  |     if (ret == NULL) { | 
91  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
92  | 0  |         return NULL;  | 
93  | 0  |     }  | 
94  |  |  | 
95  | 24.4k  |     ret->references = 1;  | 
96  | 24.4k  |     ret->key = &ret->pkeys[cert->key - cert->pkeys];  | 
97  | 24.4k  |     ret->lock = CRYPTO_THREAD_lock_new();  | 
98  | 24.4k  |     if (ret->lock == NULL) { | 
99  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
100  | 0  |         OPENSSL_free(ret);  | 
101  | 0  |         return NULL;  | 
102  | 0  |     }  | 
103  |  |  | 
104  | 24.4k  |     if (cert->dh_tmp != NULL) { | 
105  | 0  |         ret->dh_tmp = cert->dh_tmp;  | 
106  | 0  |         EVP_PKEY_up_ref(ret->dh_tmp);  | 
107  | 0  |     }  | 
108  |  |  | 
109  | 24.4k  |     ret->dh_tmp_cb = cert->dh_tmp_cb;  | 
110  | 24.4k  |     ret->dh_tmp_auto = cert->dh_tmp_auto;  | 
111  |  |  | 
112  | 244k  |     for (i = 0; i < SSL_PKEY_NUM; i++) { | 
113  | 220k  |         CERT_PKEY *cpk = cert->pkeys + i;  | 
114  | 220k  |         CERT_PKEY *rpk = ret->pkeys + i;  | 
115  | 220k  |         if (cpk->x509 != NULL) { | 
116  | 30.2k  |             rpk->x509 = cpk->x509;  | 
117  | 30.2k  |             X509_up_ref(rpk->x509);  | 
118  | 30.2k  |         }  | 
119  |  |  | 
120  | 220k  |         if (cpk->privatekey != NULL) { | 
121  | 30.2k  |             rpk->privatekey = cpk->privatekey;  | 
122  | 30.2k  |             EVP_PKEY_up_ref(cpk->privatekey);  | 
123  | 30.2k  |         }  | 
124  |  |  | 
125  | 220k  |         if (cpk->chain) { | 
126  | 0  |             rpk->chain = X509_chain_up_ref(cpk->chain);  | 
127  | 0  |             if (!rpk->chain) { | 
128  | 0  |                 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
129  | 0  |                 goto err;  | 
130  | 0  |             }  | 
131  | 0  |         }  | 
132  | 220k  |         if (cert->pkeys[i].serverinfo != NULL) { | 
133  |  |             /* Just copy everything. */  | 
134  | 0  |             ret->pkeys[i].serverinfo =  | 
135  | 0  |                 OPENSSL_malloc(cert->pkeys[i].serverinfo_length);  | 
136  | 0  |             if (ret->pkeys[i].serverinfo == NULL) { | 
137  | 0  |                 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
138  | 0  |                 goto err;  | 
139  | 0  |             }  | 
140  | 0  |             ret->pkeys[i].serverinfo_length = cert->pkeys[i].serverinfo_length;  | 
141  | 0  |             memcpy(ret->pkeys[i].serverinfo,  | 
142  | 0  |                    cert->pkeys[i].serverinfo, cert->pkeys[i].serverinfo_length);  | 
143  | 0  |         }  | 
144  | 220k  |     }  | 
145  |  |  | 
146  |  |     /* Configured sigalgs copied across */  | 
147  | 24.4k  |     if (cert->conf_sigalgs) { | 
148  | 0  |         ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen  | 
149  | 0  |                                            * sizeof(*cert->conf_sigalgs));  | 
150  | 0  |         if (ret->conf_sigalgs == NULL)  | 
151  | 0  |             goto err;  | 
152  | 0  |         memcpy(ret->conf_sigalgs, cert->conf_sigalgs,  | 
153  | 0  |                cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));  | 
154  | 0  |         ret->conf_sigalgslen = cert->conf_sigalgslen;  | 
155  | 0  |     } else  | 
156  | 24.4k  |         ret->conf_sigalgs = NULL;  | 
157  |  |  | 
158  | 24.4k  |     if (cert->client_sigalgs) { | 
159  | 0  |         ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen  | 
160  | 0  |                                              * sizeof(*cert->client_sigalgs));  | 
161  | 0  |         if (ret->client_sigalgs == NULL)  | 
162  | 0  |             goto err;  | 
163  | 0  |         memcpy(ret->client_sigalgs, cert->client_sigalgs,  | 
164  | 0  |                cert->client_sigalgslen * sizeof(*cert->client_sigalgs));  | 
165  | 0  |         ret->client_sigalgslen = cert->client_sigalgslen;  | 
166  | 0  |     } else  | 
167  | 24.4k  |         ret->client_sigalgs = NULL;  | 
168  |  |     /* Copy any custom client certificate types */  | 
169  | 24.4k  |     if (cert->ctype) { | 
170  | 0  |         ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);  | 
171  | 0  |         if (ret->ctype == NULL)  | 
172  | 0  |             goto err;  | 
173  | 0  |         ret->ctype_len = cert->ctype_len;  | 
174  | 0  |     }  | 
175  |  |  | 
176  | 24.4k  |     ret->cert_flags = cert->cert_flags;  | 
177  |  |  | 
178  | 24.4k  |     ret->cert_cb = cert->cert_cb;  | 
179  | 24.4k  |     ret->cert_cb_arg = cert->cert_cb_arg;  | 
180  |  |  | 
181  | 24.4k  |     if (cert->verify_store) { | 
182  | 0  |         X509_STORE_up_ref(cert->verify_store);  | 
183  | 0  |         ret->verify_store = cert->verify_store;  | 
184  | 0  |     }  | 
185  |  |  | 
186  | 24.4k  |     if (cert->chain_store) { | 
187  | 0  |         X509_STORE_up_ref(cert->chain_store);  | 
188  | 0  |         ret->chain_store = cert->chain_store;  | 
189  | 0  |     }  | 
190  |  |  | 
191  | 24.4k  |     ret->sec_cb = cert->sec_cb;  | 
192  | 24.4k  |     ret->sec_level = cert->sec_level;  | 
193  | 24.4k  |     ret->sec_ex = cert->sec_ex;  | 
194  |  |  | 
195  | 24.4k  |     if (!custom_exts_copy(&ret->custext, &cert->custext))  | 
196  | 0  |         goto err;  | 
197  | 24.4k  | #ifndef OPENSSL_NO_PSK  | 
198  | 24.4k  |     if (cert->psk_identity_hint) { | 
199  | 0  |         ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);  | 
200  | 0  |         if (ret->psk_identity_hint == NULL)  | 
201  | 0  |             goto err;  | 
202  | 0  |     }  | 
203  | 24.4k  | #endif  | 
204  | 24.4k  |     return ret;  | 
205  |  |  | 
206  | 0  |  err:  | 
207  | 0  |     ssl_cert_free(ret);  | 
208  |  | 
  | 
209  | 0  |     return NULL;  | 
210  | 24.4k  | }  | 
211  |  |  | 
212  |  | /* Free up and clear all certificates and chains */  | 
213  |  |  | 
214  |  | void ssl_cert_clear_certs(CERT *c)  | 
215  | 158k  | { | 
216  | 158k  |     int i;  | 
217  | 158k  |     if (c == NULL)  | 
218  | 0  |         return;  | 
219  | 1.77M  |     for (i = 0; i < SSL_PKEY_NUM; i++) { | 
220  | 1.61M  |         CERT_PKEY *cpk = c->pkeys + i;  | 
221  | 1.61M  |         X509_free(cpk->x509);  | 
222  | 1.61M  |         cpk->x509 = NULL;  | 
223  | 1.61M  |         EVP_PKEY_free(cpk->privatekey);  | 
224  | 1.61M  |         cpk->privatekey = NULL;  | 
225  | 1.61M  |         sk_X509_pop_free(cpk->chain, X509_free);  | 
226  | 1.61M  |         cpk->chain = NULL;  | 
227  | 1.61M  |         OPENSSL_free(cpk->serverinfo);  | 
228  | 1.61M  |         cpk->serverinfo = NULL;  | 
229  | 1.61M  |         cpk->serverinfo_length = 0;  | 
230  | 1.61M  |     }  | 
231  | 158k  | }  | 
232  |  |  | 
233  |  | void ssl_cert_free(CERT *c)  | 
234  | 158k  | { | 
235  | 158k  |     int i;  | 
236  |  |  | 
237  | 158k  |     if (c == NULL)  | 
238  | 0  |         return;  | 
239  | 158k  |     CRYPTO_DOWN_REF(&c->references, &i, c->lock);  | 
240  | 158k  |     REF_PRINT_COUNT("CERT", c); | 
241  | 158k  |     if (i > 0)  | 
242  | 0  |         return;  | 
243  | 158k  |     REF_ASSERT_ISNT(i < 0);  | 
244  |  |  | 
245  | 158k  |     EVP_PKEY_free(c->dh_tmp);  | 
246  |  |  | 
247  | 158k  |     ssl_cert_clear_certs(c);  | 
248  | 158k  |     OPENSSL_free(c->conf_sigalgs);  | 
249  | 158k  |     OPENSSL_free(c->client_sigalgs);  | 
250  | 158k  |     OPENSSL_free(c->ctype);  | 
251  | 158k  |     X509_STORE_free(c->verify_store);  | 
252  | 158k  |     X509_STORE_free(c->chain_store);  | 
253  | 158k  |     custom_exts_free(&c->custext);  | 
254  | 158k  | #ifndef OPENSSL_NO_PSK  | 
255  | 158k  |     OPENSSL_free(c->psk_identity_hint);  | 
256  | 158k  | #endif  | 
257  | 158k  |     CRYPTO_THREAD_lock_free(c->lock);  | 
258  | 158k  |     OPENSSL_free(c);  | 
259  | 158k  | }  | 
260  |  |  | 
261  |  | int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)  | 
262  | 0  | { | 
263  | 0  |     int i, r;  | 
264  | 0  |     CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;  | 
265  |  | 
  | 
266  | 0  |     if (!cpk)  | 
267  | 0  |         return 0;  | 
268  | 0  |     for (i = 0; i < sk_X509_num(chain); i++) { | 
269  | 0  |         X509 *x = sk_X509_value(chain, i);  | 
270  |  | 
  | 
271  | 0  |         r = ssl_security_cert(s, ctx, x, 0, 0);  | 
272  | 0  |         if (r != 1) { | 
273  | 0  |             ERR_raise(ERR_LIB_SSL, r);  | 
274  | 0  |             return 0;  | 
275  | 0  |         }  | 
276  | 0  |     }  | 
277  | 0  |     sk_X509_pop_free(cpk->chain, X509_free);  | 
278  | 0  |     cpk->chain = chain;  | 
279  | 0  |     return 1;  | 
280  | 0  | }  | 
281  |  |  | 
282  |  | int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)  | 
283  | 0  | { | 
284  | 0  |     STACK_OF(X509) *dchain;  | 
285  | 0  |     if (!chain)  | 
286  | 0  |         return ssl_cert_set0_chain(s, ctx, NULL);  | 
287  | 0  |     dchain = X509_chain_up_ref(chain);  | 
288  | 0  |     if (!dchain)  | 
289  | 0  |         return 0;  | 
290  | 0  |     if (!ssl_cert_set0_chain(s, ctx, dchain)) { | 
291  | 0  |         sk_X509_pop_free(dchain, X509_free);  | 
292  | 0  |         return 0;  | 
293  | 0  |     }  | 
294  | 0  |     return 1;  | 
295  | 0  | }  | 
296  |  |  | 
297  |  | int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)  | 
298  | 0  | { | 
299  | 0  |     int r;  | 
300  | 0  |     CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;  | 
301  | 0  |     if (!cpk)  | 
302  | 0  |         return 0;  | 
303  | 0  |     r = ssl_security_cert(s, ctx, x, 0, 0);  | 
304  | 0  |     if (r != 1) { | 
305  | 0  |         ERR_raise(ERR_LIB_SSL, r);  | 
306  | 0  |         return 0;  | 
307  | 0  |     }  | 
308  | 0  |     if (!cpk->chain)  | 
309  | 0  |         cpk->chain = sk_X509_new_null();  | 
310  | 0  |     if (!cpk->chain || !sk_X509_push(cpk->chain, x))  | 
311  | 0  |         return 0;  | 
312  | 0  |     return 1;  | 
313  | 0  | }  | 
314  |  |  | 
315  |  | int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)  | 
316  | 0  | { | 
317  | 0  |     if (!ssl_cert_add0_chain_cert(s, ctx, x))  | 
318  | 0  |         return 0;  | 
319  | 0  |     X509_up_ref(x);  | 
320  | 0  |     return 1;  | 
321  | 0  | }  | 
322  |  |  | 
323  |  | int ssl_cert_select_current(CERT *c, X509 *x)  | 
324  | 0  | { | 
325  | 0  |     int i;  | 
326  | 0  |     if (x == NULL)  | 
327  | 0  |         return 0;  | 
328  | 0  |     for (i = 0; i < SSL_PKEY_NUM; i++) { | 
329  | 0  |         CERT_PKEY *cpk = c->pkeys + i;  | 
330  | 0  |         if (cpk->x509 == x && cpk->privatekey) { | 
331  | 0  |             c->key = cpk;  | 
332  | 0  |             return 1;  | 
333  | 0  |         }  | 
334  | 0  |     }  | 
335  |  |  | 
336  | 0  |     for (i = 0; i < SSL_PKEY_NUM; i++) { | 
337  | 0  |         CERT_PKEY *cpk = c->pkeys + i;  | 
338  | 0  |         if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { | 
339  | 0  |             c->key = cpk;  | 
340  | 0  |             return 1;  | 
341  | 0  |         }  | 
342  | 0  |     }  | 
343  | 0  |     return 0;  | 
344  | 0  | }  | 
345  |  |  | 
346  |  | int ssl_cert_set_current(CERT *c, long op)  | 
347  | 0  | { | 
348  | 0  |     int i, idx;  | 
349  | 0  |     if (!c)  | 
350  | 0  |         return 0;  | 
351  | 0  |     if (op == SSL_CERT_SET_FIRST)  | 
352  | 0  |         idx = 0;  | 
353  | 0  |     else if (op == SSL_CERT_SET_NEXT) { | 
354  | 0  |         idx = (int)(c->key - c->pkeys + 1);  | 
355  | 0  |         if (idx >= SSL_PKEY_NUM)  | 
356  | 0  |             return 0;  | 
357  | 0  |     } else  | 
358  | 0  |         return 0;  | 
359  | 0  |     for (i = idx; i < SSL_PKEY_NUM; i++) { | 
360  | 0  |         CERT_PKEY *cpk = c->pkeys + i;  | 
361  | 0  |         if (cpk->x509 && cpk->privatekey) { | 
362  | 0  |             c->key = cpk;  | 
363  | 0  |             return 1;  | 
364  | 0  |         }  | 
365  | 0  |     }  | 
366  | 0  |     return 0;  | 
367  | 0  | }  | 
368  |  |  | 
369  |  | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)  | 
370  | 0  | { | 
371  | 0  |     c->cert_cb = cb;  | 
372  | 0  |     c->cert_cb_arg = arg;  | 
373  | 0  | }  | 
374  |  |  | 
375  |  | /*  | 
376  |  |  * Verify a certificate chain  | 
377  |  |  * Return codes:  | 
378  |  |  *  1: Verify success  | 
379  |  |  *  0: Verify failure or error  | 
380  |  |  * -1: Retry required  | 
381  |  |  */  | 
382  |  | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)  | 
383  | 3.51k  | { | 
384  | 3.51k  |     X509 *x;  | 
385  | 3.51k  |     int i = 0;  | 
386  | 3.51k  |     X509_STORE *verify_store;  | 
387  | 3.51k  |     X509_STORE_CTX *ctx = NULL;  | 
388  | 3.51k  |     X509_VERIFY_PARAM *param;  | 
389  |  |  | 
390  | 3.51k  |     if ((sk == NULL) || (sk_X509_num(sk) == 0))  | 
391  | 0  |         return 0;  | 
392  |  |  | 
393  | 3.51k  |     if (s->cert->verify_store)  | 
394  | 0  |         verify_store = s->cert->verify_store;  | 
395  | 3.51k  |     else  | 
396  | 3.51k  |         verify_store = s->ctx->cert_store;  | 
397  |  |  | 
398  | 3.51k  |     ctx = X509_STORE_CTX_new_ex(s->ctx->libctx, s->ctx->propq);  | 
399  | 3.51k  |     if (ctx == NULL) { | 
400  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
401  | 0  |         return 0;  | 
402  | 0  |     }  | 
403  |  |  | 
404  | 3.51k  |     x = sk_X509_value(sk, 0);  | 
405  | 3.51k  |     if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) { | 
406  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);  | 
407  | 0  |         goto end;  | 
408  | 0  |     }  | 
409  | 3.51k  |     param = X509_STORE_CTX_get0_param(ctx);  | 
410  |  |     /*  | 
411  |  |      * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some  | 
412  |  |      * point, for now a single @SECLEVEL sets the same policy for TLS crypto  | 
413  |  |      * and PKI authentication.  | 
414  |  |      */  | 
415  | 3.51k  |     X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s));  | 
416  |  |  | 
417  |  |     /* Set suite B flags if needed */  | 
418  | 3.51k  |     X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));  | 
419  | 3.51k  |     if (!X509_STORE_CTX_set_ex_data  | 
420  | 3.51k  |         (ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) { | 
421  | 0  |         goto end;  | 
422  | 0  |     }  | 
423  |  |  | 
424  |  |     /* Verify via DANE if enabled */  | 
425  | 3.51k  |     if (DANETLS_ENABLED(&s->dane))  | 
426  | 0  |         X509_STORE_CTX_set0_dane(ctx, &s->dane);  | 
427  |  |  | 
428  |  |     /*  | 
429  |  |      * We need to inherit the verify parameters. These can be determined by  | 
430  |  |      * the context: if its a server it will verify SSL client certificates or  | 
431  |  |      * vice versa.  | 
432  |  |      */  | 
433  |  |  | 
434  | 3.51k  |     X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");  | 
435  |  |     /*  | 
436  |  |      * Anything non-default in "s->param" should overwrite anything in the ctx.  | 
437  |  |      */  | 
438  | 3.51k  |     X509_VERIFY_PARAM_set1(param, s->param);  | 
439  |  |  | 
440  | 3.51k  |     if (s->verify_callback)  | 
441  | 0  |         X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);  | 
442  |  |  | 
443  | 3.51k  |     if (s->ctx->app_verify_callback != NULL) { | 
444  | 0  |         i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg);  | 
445  | 3.51k  |     } else { | 
446  | 3.51k  |         i = X509_verify_cert(ctx);  | 
447  |  |         /* We treat an error in the same way as a failure to verify */  | 
448  | 3.51k  |         if (i < 0)  | 
449  | 0  |             i = 0;  | 
450  | 3.51k  |     }  | 
451  |  |  | 
452  | 3.51k  |     s->verify_result = X509_STORE_CTX_get_error(ctx);  | 
453  | 3.51k  |     sk_X509_pop_free(s->verified_chain, X509_free);  | 
454  | 3.51k  |     s->verified_chain = NULL;  | 
455  | 3.51k  |     if (X509_STORE_CTX_get0_chain(ctx) != NULL) { | 
456  | 3.51k  |         s->verified_chain = X509_STORE_CTX_get1_chain(ctx);  | 
457  | 3.51k  |         if (s->verified_chain == NULL) { | 
458  | 0  |             ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
459  | 0  |             i = 0;  | 
460  | 0  |         }  | 
461  | 3.51k  |     }  | 
462  |  |  | 
463  |  |     /* Move peername from the store context params to the SSL handle's */  | 
464  | 3.51k  |     X509_VERIFY_PARAM_move_peername(s->param, param);  | 
465  |  |  | 
466  | 3.51k  |  end:  | 
467  | 3.51k  |     X509_STORE_CTX_free(ctx);  | 
468  | 3.51k  |     return i;  | 
469  | 3.51k  | }  | 
470  |  |  | 
471  |  | static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,  | 
472  |  |                         STACK_OF(X509_NAME) *name_list)  | 
473  | 0  | { | 
474  | 0  |     sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);  | 
475  | 0  |     *ca_list = name_list;  | 
476  | 0  | }  | 
477  |  |  | 
478  |  | STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)  | 
479  | 0  | { | 
480  | 0  |     int i;  | 
481  | 0  |     const int num = sk_X509_NAME_num(sk);  | 
482  | 0  |     STACK_OF(X509_NAME) *ret;  | 
483  | 0  |     X509_NAME *name;  | 
484  |  | 
  | 
485  | 0  |     ret = sk_X509_NAME_new_reserve(NULL, num);  | 
486  | 0  |     if (ret == NULL) { | 
487  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
488  | 0  |         return NULL;  | 
489  | 0  |     }  | 
490  | 0  |     for (i = 0; i < num; i++) { | 
491  | 0  |         name = X509_NAME_dup(sk_X509_NAME_value(sk, i));  | 
492  | 0  |         if (name == NULL) { | 
493  | 0  |             ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
494  | 0  |             sk_X509_NAME_pop_free(ret, X509_NAME_free);  | 
495  | 0  |             return NULL;  | 
496  | 0  |         }  | 
497  | 0  |         sk_X509_NAME_push(ret, name);   /* Cannot fail after reserve call */  | 
498  | 0  |     }  | 
499  | 0  |     return ret;  | 
500  | 0  | }  | 
501  |  |  | 
502  |  | void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)  | 
503  | 0  | { | 
504  | 0  |     set0_CA_list(&s->ca_names, name_list);  | 
505  | 0  | }  | 
506  |  |  | 
507  |  | void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)  | 
508  | 0  | { | 
509  | 0  |     set0_CA_list(&ctx->ca_names, name_list);  | 
510  | 0  | }  | 
511  |  |  | 
512  |  | const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)  | 
513  | 0  | { | 
514  | 0  |     return ctx->ca_names;  | 
515  | 0  | }  | 
516  |  |  | 
517  |  | const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)  | 
518  | 14.5k  | { | 
519  | 14.5k  |     return s->ca_names != NULL ? s->ca_names : s->ctx->ca_names;  | 
520  | 14.5k  | }  | 
521  |  |  | 
522  |  | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)  | 
523  | 0  | { | 
524  | 0  |     set0_CA_list(&ctx->client_ca_names, name_list);  | 
525  | 0  | }  | 
526  |  |  | 
527  |  | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)  | 
528  | 0  | { | 
529  | 0  |     return ctx->client_ca_names;  | 
530  | 0  | }  | 
531  |  |  | 
532  |  | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)  | 
533  | 0  | { | 
534  | 0  |     set0_CA_list(&s->client_ca_names, name_list);  | 
535  | 0  | }  | 
536  |  |  | 
537  |  | const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)  | 
538  | 0  | { | 
539  | 0  |     return s->s3.tmp.peer_ca_names;  | 
540  | 0  | }  | 
541  |  |  | 
542  |  | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)  | 
543  | 0  | { | 
544  | 0  |     if (!s->server)  | 
545  | 0  |         return s->s3.tmp.peer_ca_names;  | 
546  | 0  |     return s->client_ca_names != NULL ?  s->client_ca_names  | 
547  | 0  |                                       : s->ctx->client_ca_names;  | 
548  | 0  | }  | 
549  |  |  | 
550  |  | static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)  | 
551  | 0  | { | 
552  | 0  |     X509_NAME *name;  | 
553  |  | 
  | 
554  | 0  |     if (x == NULL)  | 
555  | 0  |         return 0;  | 
556  | 0  |     if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))  | 
557  | 0  |         return 0;  | 
558  |  |  | 
559  | 0  |     if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)  | 
560  | 0  |         return 0;  | 
561  |  |  | 
562  | 0  |     if (!sk_X509_NAME_push(*sk, name)) { | 
563  | 0  |         X509_NAME_free(name);  | 
564  | 0  |         return 0;  | 
565  | 0  |     }  | 
566  | 0  |     return 1;  | 
567  | 0  | }  | 
568  |  |  | 
569  |  | int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)  | 
570  | 0  | { | 
571  | 0  |     return add_ca_name(&ssl->ca_names, x);  | 
572  | 0  | }  | 
573  |  |  | 
574  |  | int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)  | 
575  | 0  | { | 
576  | 0  |     return add_ca_name(&ctx->ca_names, x);  | 
577  | 0  | }  | 
578  |  |  | 
579  |  | /*  | 
580  |  |  * The following two are older names are to be replaced with  | 
581  |  |  * SSL(_CTX)_add1_to_CA_list  | 
582  |  |  */  | 
583  |  | int SSL_add_client_CA(SSL *ssl, X509 *x)  | 
584  | 0  | { | 
585  | 0  |     return add_ca_name(&ssl->client_ca_names, x);  | 
586  | 0  | }  | 
587  |  |  | 
588  |  | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)  | 
589  | 0  | { | 
590  | 0  |     return add_ca_name(&ctx->client_ca_names, x);  | 
591  | 0  | }  | 
592  |  |  | 
593  |  | static int xname_cmp(const X509_NAME *a, const X509_NAME *b)  | 
594  | 0  | { | 
595  | 0  |     unsigned char *abuf = NULL, *bbuf = NULL;  | 
596  | 0  |     int alen, blen, ret;  | 
597  |  |  | 
598  |  |     /* X509_NAME_cmp() itself casts away constness in this way, so  | 
599  |  |      * assume it's safe:  | 
600  |  |      */  | 
601  | 0  |     alen = i2d_X509_NAME((X509_NAME *)a, &abuf);  | 
602  | 0  |     blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);  | 
603  |  | 
  | 
604  | 0  |     if (alen < 0 || blen < 0)  | 
605  | 0  |         ret = -2;  | 
606  | 0  |     else if (alen != blen)  | 
607  | 0  |         ret = alen - blen;  | 
608  | 0  |     else /* alen == blen */  | 
609  | 0  |         ret = memcmp(abuf, bbuf, alen);  | 
610  |  | 
  | 
611  | 0  |     OPENSSL_free(abuf);  | 
612  | 0  |     OPENSSL_free(bbuf);  | 
613  |  | 
  | 
614  | 0  |     return ret;  | 
615  | 0  | }  | 
616  |  |  | 
617  |  | static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)  | 
618  | 0  | { | 
619  | 0  |     return xname_cmp(*a, *b);  | 
620  | 0  | }  | 
621  |  |  | 
622  |  | static unsigned long xname_hash(const X509_NAME *a)  | 
623  | 0  | { | 
624  |  |     /* This returns 0 also if SHA1 is not available */  | 
625  | 0  |     return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);  | 
626  | 0  | }  | 
627  |  |  | 
628  |  | STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,  | 
629  |  |                                                 OSSL_LIB_CTX *libctx,  | 
630  |  |                                                 const char *propq)  | 
631  | 0  | { | 
632  | 0  |     BIO *in = BIO_new(BIO_s_file());  | 
633  | 0  |     X509 *x = NULL;  | 
634  | 0  |     X509_NAME *xn = NULL;  | 
635  | 0  |     STACK_OF(X509_NAME) *ret = NULL;  | 
636  | 0  |     LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);  | 
637  | 0  |     OSSL_LIB_CTX *prev_libctx = NULL;  | 
638  |  | 
  | 
639  | 0  |     if ((name_hash == NULL) || (in == NULL)) { | 
640  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
641  | 0  |         goto err;  | 
642  | 0  |     }  | 
643  |  |  | 
644  | 0  |     x = X509_new_ex(libctx, propq);  | 
645  | 0  |     if (x == NULL) { | 
646  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
647  | 0  |         goto err;  | 
648  | 0  |     }  | 
649  | 0  |     if (BIO_read_filename(in, file) <= 0)  | 
650  | 0  |         goto err;  | 
651  |  |  | 
652  |  |     /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */  | 
653  | 0  |     prev_libctx = OSSL_LIB_CTX_set0_default(libctx);  | 
654  | 0  |     for (;;) { | 
655  | 0  |         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)  | 
656  | 0  |             break;  | 
657  | 0  |         if (ret == NULL) { | 
658  | 0  |             ret = sk_X509_NAME_new_null();  | 
659  | 0  |             if (ret == NULL) { | 
660  | 0  |                 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
661  | 0  |                 goto err;  | 
662  | 0  |             }  | 
663  | 0  |         }  | 
664  | 0  |         if ((xn = X509_get_subject_name(x)) == NULL)  | 
665  | 0  |             goto err;  | 
666  |  |         /* check for duplicates */  | 
667  | 0  |         xn = X509_NAME_dup(xn);  | 
668  | 0  |         if (xn == NULL)  | 
669  | 0  |             goto err;  | 
670  | 0  |         if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) { | 
671  |  |             /* Duplicate. */  | 
672  | 0  |             X509_NAME_free(xn);  | 
673  | 0  |             xn = NULL;  | 
674  | 0  |         } else { | 
675  | 0  |             lh_X509_NAME_insert(name_hash, xn);  | 
676  | 0  |             if (!sk_X509_NAME_push(ret, xn))  | 
677  | 0  |                 goto err;  | 
678  | 0  |         }  | 
679  | 0  |     }  | 
680  | 0  |     goto done;  | 
681  |  |  | 
682  | 0  |  err:  | 
683  | 0  |     X509_NAME_free(xn);  | 
684  | 0  |     sk_X509_NAME_pop_free(ret, X509_NAME_free);  | 
685  | 0  |     ret = NULL;  | 
686  | 0  |  done:  | 
687  |  |     /* restore the old libctx */  | 
688  | 0  |     OSSL_LIB_CTX_set0_default(prev_libctx);  | 
689  | 0  |     BIO_free(in);  | 
690  | 0  |     X509_free(x);  | 
691  | 0  |     lh_X509_NAME_free(name_hash);  | 
692  | 0  |     if (ret != NULL)  | 
693  | 0  |         ERR_clear_error();  | 
694  | 0  |     return ret;  | 
695  | 0  | }  | 
696  |  |  | 
697  |  | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)  | 
698  | 0  | { | 
699  | 0  |     return SSL_load_client_CA_file_ex(file, NULL, NULL);  | 
700  | 0  | }  | 
701  |  |  | 
702  |  | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,  | 
703  |  |                                         const char *file)  | 
704  | 0  | { | 
705  | 0  |     BIO *in;  | 
706  | 0  |     X509 *x = NULL;  | 
707  | 0  |     X509_NAME *xn = NULL;  | 
708  | 0  |     int ret = 1;  | 
709  | 0  |     int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);  | 
710  |  | 
  | 
711  | 0  |     oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);  | 
712  |  | 
  | 
713  | 0  |     in = BIO_new(BIO_s_file());  | 
714  |  | 
  | 
715  | 0  |     if (in == NULL) { | 
716  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
717  | 0  |         goto err;  | 
718  | 0  |     }  | 
719  |  |  | 
720  | 0  |     if (BIO_read_filename(in, file) <= 0)  | 
721  | 0  |         goto err;  | 
722  |  |  | 
723  | 0  |     for (;;) { | 
724  | 0  |         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)  | 
725  | 0  |             break;  | 
726  | 0  |         if ((xn = X509_get_subject_name(x)) == NULL)  | 
727  | 0  |             goto err;  | 
728  | 0  |         xn = X509_NAME_dup(xn);  | 
729  | 0  |         if (xn == NULL)  | 
730  | 0  |             goto err;  | 
731  | 0  |         if (sk_X509_NAME_find(stack, xn) >= 0) { | 
732  |  |             /* Duplicate. */  | 
733  | 0  |             X509_NAME_free(xn);  | 
734  | 0  |         } else if (!sk_X509_NAME_push(stack, xn)) { | 
735  | 0  |             X509_NAME_free(xn);  | 
736  | 0  |             goto err;  | 
737  | 0  |         }  | 
738  | 0  |     }  | 
739  |  |  | 
740  | 0  |     ERR_clear_error();  | 
741  | 0  |     goto done;  | 
742  |  |  | 
743  | 0  |  err:  | 
744  | 0  |     ret = 0;  | 
745  | 0  |  done:  | 
746  | 0  |     BIO_free(in);  | 
747  | 0  |     X509_free(x);  | 
748  | 0  |     (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);  | 
749  | 0  |     return ret;  | 
750  | 0  | }  | 
751  |  |  | 
752  |  | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,  | 
753  |  |                                        const char *dir)  | 
754  | 0  | { | 
755  | 0  |     OPENSSL_DIR_CTX *d = NULL;  | 
756  | 0  |     const char *filename;  | 
757  | 0  |     int ret = 0;  | 
758  |  |  | 
759  |  |     /* Note that a side effect is that the CAs will be sorted by name */  | 
760  |  | 
  | 
761  | 0  |     while ((filename = OPENSSL_DIR_read(&d, dir))) { | 
762  | 0  |         char buf[1024];  | 
763  | 0  |         int r;  | 
764  | 0  | #ifndef OPENSSL_NO_POSIX_IO  | 
765  | 0  |         struct stat st;  | 
766  |  | 
  | 
767  |  | #else  | 
768  |  |         /* Cannot use stat so just skip current and parent directories */  | 
769  |  |         if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)  | 
770  |  |             continue;  | 
771  |  | #endif  | 
772  | 0  |         if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) { | 
773  | 0  |             ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);  | 
774  | 0  |             goto err;  | 
775  | 0  |         }  | 
776  |  | #ifdef OPENSSL_SYS_VMS  | 
777  |  |         r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);  | 
778  |  | #else  | 
779  | 0  |         r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);  | 
780  | 0  | #endif  | 
781  | 0  | #ifndef OPENSSL_NO_POSIX_IO  | 
782  |  |         /* Skip subdirectories */  | 
783  | 0  |         if (!stat(buf, &st) && S_ISDIR(st.st_mode))  | 
784  | 0  |             continue;  | 
785  | 0  | #endif  | 
786  | 0  |         if (r <= 0 || r >= (int)sizeof(buf))  | 
787  | 0  |             goto err;  | 
788  | 0  |         if (!SSL_add_file_cert_subjects_to_stack(stack, buf))  | 
789  | 0  |             goto err;  | 
790  | 0  |     }  | 
791  |  |  | 
792  | 0  |     if (errno) { | 
793  | 0  |         ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),  | 
794  | 0  |                        "calling OPENSSL_dir_read(%s)", dir);  | 
795  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);  | 
796  | 0  |         goto err;  | 
797  | 0  |     }  | 
798  |  |  | 
799  | 0  |     ret = 1;  | 
800  |  | 
  | 
801  | 0  |  err:  | 
802  | 0  |     if (d)  | 
803  | 0  |         OPENSSL_DIR_end(&d);  | 
804  |  | 
  | 
805  | 0  |     return ret;  | 
806  | 0  | }  | 
807  |  |  | 
808  |  | static int add_uris_recursive(STACK_OF(X509_NAME) *stack,  | 
809  |  |                               const char *uri, int depth)  | 
810  | 0  | { | 
811  | 0  |     int ok = 1;  | 
812  | 0  |     OSSL_STORE_CTX *ctx = NULL;  | 
813  | 0  |     X509 *x = NULL;  | 
814  | 0  |     X509_NAME *xn = NULL;  | 
815  | 0  |     OSSL_STORE_INFO *info = NULL;  | 
816  |  | 
  | 
817  | 0  |     if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)  | 
818  | 0  |         goto err;  | 
819  |  |  | 
820  | 0  |     while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) { | 
821  | 0  |         int infotype;  | 
822  |  | 
  | 
823  | 0  |         if ((info = OSSL_STORE_load(ctx)) == NULL)  | 
824  | 0  |             continue;  | 
825  | 0  |         infotype = OSSL_STORE_INFO_get_type(info);  | 
826  |  | 
  | 
827  | 0  |         if (infotype == OSSL_STORE_INFO_NAME) { | 
828  |  |             /*  | 
829  |  |              * This is an entry in the "directory" represented by the current  | 
830  |  |              * uri.  if |depth| allows, dive into it.  | 
831  |  |              */  | 
832  | 0  |             if (depth > 0)  | 
833  | 0  |                 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),  | 
834  | 0  |                                         depth - 1);  | 
835  | 0  |         } else if (infotype == OSSL_STORE_INFO_CERT) { | 
836  | 0  |             if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL  | 
837  | 0  |                 || (xn = X509_get_subject_name(x)) == NULL  | 
838  | 0  |                 || (xn = X509_NAME_dup(xn)) == NULL)  | 
839  | 0  |                 goto err;  | 
840  | 0  |             if (sk_X509_NAME_find(stack, xn) >= 0) { | 
841  |  |                 /* Duplicate. */  | 
842  | 0  |                 X509_NAME_free(xn);  | 
843  | 0  |             } else if (!sk_X509_NAME_push(stack, xn)) { | 
844  | 0  |                 X509_NAME_free(xn);  | 
845  | 0  |                 goto err;  | 
846  | 0  |             }  | 
847  | 0  |         }  | 
848  |  |  | 
849  | 0  |         OSSL_STORE_INFO_free(info);  | 
850  | 0  |         info = NULL;  | 
851  | 0  |     }  | 
852  |  |  | 
853  | 0  |     ERR_clear_error();  | 
854  | 0  |     goto done;  | 
855  |  |  | 
856  | 0  |  err:  | 
857  | 0  |     ok = 0;  | 
858  | 0  |     OSSL_STORE_INFO_free(info);  | 
859  | 0  |  done:  | 
860  | 0  |     OSSL_STORE_close(ctx);  | 
861  |  | 
  | 
862  | 0  |     return ok;  | 
863  | 0  | }  | 
864  |  |  | 
865  |  | int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,  | 
866  |  |                                          const char *store)  | 
867  | 0  | { | 
868  | 0  |     int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)  | 
869  | 0  |         = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);  | 
870  | 0  |     int ret = add_uris_recursive(stack, store, 1);  | 
871  |  | 
  | 
872  | 0  |     (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);  | 
873  | 0  |     return ret;  | 
874  | 0  | }  | 
875  |  |  | 
876  |  | /* Build a certificate chain for current certificate */  | 
877  |  | int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)  | 
878  | 0  | { | 
879  | 0  |     CERT *c = s ? s->cert : ctx->cert;  | 
880  | 0  |     CERT_PKEY *cpk = c->key;  | 
881  | 0  |     X509_STORE *chain_store = NULL;  | 
882  | 0  |     X509_STORE_CTX *xs_ctx = NULL;  | 
883  | 0  |     STACK_OF(X509) *chain = NULL, *untrusted = NULL;  | 
884  | 0  |     X509 *x;  | 
885  | 0  |     SSL_CTX *real_ctx = (s == NULL) ? ctx : s->ctx;  | 
886  | 0  |     int i, rv = 0;  | 
887  |  | 
  | 
888  | 0  |     if (!cpk->x509) { | 
889  | 0  |         ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);  | 
890  | 0  |         goto err;  | 
891  | 0  |     }  | 
892  |  |     /* Rearranging and check the chain: add everything to a store */  | 
893  | 0  |     if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { | 
894  | 0  |         chain_store = X509_STORE_new();  | 
895  | 0  |         if (chain_store == NULL)  | 
896  | 0  |             goto err;  | 
897  | 0  |         for (i = 0; i < sk_X509_num(cpk->chain); i++) { | 
898  | 0  |             x = sk_X509_value(cpk->chain, i);  | 
899  | 0  |             if (!X509_STORE_add_cert(chain_store, x))  | 
900  | 0  |                 goto err;  | 
901  | 0  |         }  | 
902  |  |         /* Add EE cert too: it might be self signed */  | 
903  | 0  |         if (!X509_STORE_add_cert(chain_store, cpk->x509))  | 
904  | 0  |             goto err;  | 
905  | 0  |     } else { | 
906  | 0  |         if (c->chain_store)  | 
907  | 0  |             chain_store = c->chain_store;  | 
908  | 0  |         else if (s)  | 
909  | 0  |             chain_store = s->ctx->cert_store;  | 
910  | 0  |         else  | 
911  | 0  |             chain_store = ctx->cert_store;  | 
912  |  | 
  | 
913  | 0  |         if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)  | 
914  | 0  |             untrusted = cpk->chain;  | 
915  | 0  |     }  | 
916  |  |  | 
917  | 0  |     xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);  | 
918  | 0  |     if (xs_ctx == NULL) { | 
919  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
920  | 0  |         goto err;  | 
921  | 0  |     }  | 
922  | 0  |     if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) { | 
923  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);  | 
924  | 0  |         goto err;  | 
925  | 0  |     }  | 
926  |  |     /* Set suite B flags if needed */  | 
927  | 0  |     X509_STORE_CTX_set_flags(xs_ctx,  | 
928  | 0  |                              c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);  | 
929  |  | 
  | 
930  | 0  |     i = X509_verify_cert(xs_ctx);  | 
931  | 0  |     if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { | 
932  | 0  |         if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)  | 
933  | 0  |             ERR_clear_error();  | 
934  | 0  |         i = 1;  | 
935  | 0  |         rv = 2;  | 
936  | 0  |     }  | 
937  | 0  |     if (i > 0)  | 
938  | 0  |         chain = X509_STORE_CTX_get1_chain(xs_ctx);  | 
939  | 0  |     if (i <= 0) { | 
940  | 0  |         i = X509_STORE_CTX_get_error(xs_ctx);  | 
941  | 0  |         ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,  | 
942  | 0  |                        "Verify error:%s", X509_verify_cert_error_string(i));  | 
943  |  | 
  | 
944  | 0  |         goto err;  | 
945  | 0  |     }  | 
946  |  |     /* Remove EE certificate from chain */  | 
947  | 0  |     x = sk_X509_shift(chain);  | 
948  | 0  |     X509_free(x);  | 
949  | 0  |     if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { | 
950  | 0  |         if (sk_X509_num(chain) > 0) { | 
951  |  |             /* See if last cert is self signed */  | 
952  | 0  |             x = sk_X509_value(chain, sk_X509_num(chain) - 1);  | 
953  | 0  |             if (X509_get_extension_flags(x) & EXFLAG_SS) { | 
954  | 0  |                 x = sk_X509_pop(chain);  | 
955  | 0  |                 X509_free(x);  | 
956  | 0  |             }  | 
957  | 0  |         }  | 
958  | 0  |     }  | 
959  |  |     /*  | 
960  |  |      * Check security level of all CA certificates: EE will have been checked  | 
961  |  |      * already.  | 
962  |  |      */  | 
963  | 0  |     for (i = 0; i < sk_X509_num(chain); i++) { | 
964  | 0  |         x = sk_X509_value(chain, i);  | 
965  | 0  |         rv = ssl_security_cert(s, ctx, x, 0, 0);  | 
966  | 0  |         if (rv != 1) { | 
967  | 0  |             ERR_raise(ERR_LIB_SSL, rv);  | 
968  | 0  |             sk_X509_pop_free(chain, X509_free);  | 
969  | 0  |             rv = 0;  | 
970  | 0  |             goto err;  | 
971  | 0  |         }  | 
972  | 0  |     }  | 
973  | 0  |     sk_X509_pop_free(cpk->chain, X509_free);  | 
974  | 0  |     cpk->chain = chain;  | 
975  | 0  |     if (rv == 0)  | 
976  | 0  |         rv = 1;  | 
977  | 0  |  err:  | 
978  | 0  |     if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)  | 
979  | 0  |         X509_STORE_free(chain_store);  | 
980  | 0  |     X509_STORE_CTX_free(xs_ctx);  | 
981  |  | 
  | 
982  | 0  |     return rv;  | 
983  | 0  | }  | 
984  |  |  | 
985  |  | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)  | 
986  | 0  | { | 
987  | 0  |     X509_STORE **pstore;  | 
988  | 0  |     if (chain)  | 
989  | 0  |         pstore = &c->chain_store;  | 
990  | 0  |     else  | 
991  | 0  |         pstore = &c->verify_store;  | 
992  | 0  |     X509_STORE_free(*pstore);  | 
993  | 0  |     *pstore = store;  | 
994  | 0  |     if (ref && store)  | 
995  | 0  |         X509_STORE_up_ref(store);  | 
996  | 0  |     return 1;  | 
997  | 0  | }  | 
998  |  |  | 
999  |  | int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)  | 
1000  | 0  | { | 
1001  | 0  |     *pstore = (chain ? c->chain_store : c->verify_store);  | 
1002  | 0  |     return 1;  | 
1003  | 0  | }  | 
1004  |  |  | 
1005  |  | int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)  | 
1006  | 14.8M  | { | 
1007  | 14.8M  |     int level;  | 
1008  |  |     /*  | 
1009  |  |      * note that there's a corresponding minbits_table  | 
1010  |  |      * in crypto/x509/x509_vfy.c that's used for checking the security level  | 
1011  |  |      * of RSA and DSA keys  | 
1012  |  |      */  | 
1013  | 14.8M  |     static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 }; | 
1014  |  |  | 
1015  | 14.8M  |     if (ctx != NULL)  | 
1016  | 76.3k  |         level = SSL_CTX_get_security_level(ctx);  | 
1017  | 14.7M  |     else  | 
1018  | 14.7M  |         level = SSL_get_security_level(s);  | 
1019  |  |  | 
1020  | 14.8M  |     if (level > 5)  | 
1021  | 0  |         level = 5;  | 
1022  | 14.8M  |     else if (level < 0)  | 
1023  | 0  |         level = 0;  | 
1024  |  |  | 
1025  | 14.8M  |     if (levelp != NULL)  | 
1026  | 14.8M  |         *levelp = level;  | 
1027  |  |  | 
1028  | 14.8M  |     return minbits_table[level];  | 
1029  | 14.8M  | }  | 
1030  |  |  | 
1031  |  | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,  | 
1032  |  |                                          int op, int bits, int nid, void *other,  | 
1033  |  |                                          void *ex)  | 
1034  | 2.82M  | { | 
1035  | 2.82M  |     int level, minbits, pfs_mask;  | 
1036  |  |  | 
1037  | 2.82M  |     minbits = ssl_get_security_level_bits(s, ctx, &level);  | 
1038  |  |  | 
1039  | 2.82M  |     if (level == 0) { | 
1040  |  |         /*  | 
1041  |  |          * No EDH keys weaker than 1024-bits even at level 0, otherwise,  | 
1042  |  |          * anything goes.  | 
1043  |  |          */  | 
1044  | 2.82M  |         if (op == SSL_SECOP_TMP_DH && bits < 80)  | 
1045  | 1  |             return 0;  | 
1046  | 2.82M  |         return 1;  | 
1047  | 2.82M  |     }  | 
1048  | 0  |     switch (op) { | 
1049  | 0  |     case SSL_SECOP_CIPHER_SUPPORTED:  | 
1050  | 0  |     case SSL_SECOP_CIPHER_SHARED:  | 
1051  | 0  |     case SSL_SECOP_CIPHER_CHECK:  | 
1052  | 0  |         { | 
1053  | 0  |             const SSL_CIPHER *c = other;  | 
1054  |  |             /* No ciphers below security level */  | 
1055  | 0  |             if (bits < minbits)  | 
1056  | 0  |                 return 0;  | 
1057  |  |             /* No unauthenticated ciphersuites */  | 
1058  | 0  |             if (c->algorithm_auth & SSL_aNULL)  | 
1059  | 0  |                 return 0;  | 
1060  |  |             /* No MD5 mac ciphersuites */  | 
1061  | 0  |             if (c->algorithm_mac & SSL_MD5)  | 
1062  | 0  |                 return 0;  | 
1063  |  |             /* SHA1 HMAC is 160 bits of security */  | 
1064  | 0  |             if (minbits > 160 && c->algorithm_mac & SSL_SHA1)  | 
1065  | 0  |                 return 0;  | 
1066  |  |             /* Level 2: no RC4 */  | 
1067  | 0  |             if (level >= 2 && c->algorithm_enc == SSL_RC4)  | 
1068  | 0  |                 return 0;  | 
1069  |  |             /* Level 3: forward secure ciphersuites only */  | 
1070  | 0  |             pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;  | 
1071  | 0  |             if (level >= 3 && c->min_tls != TLS1_3_VERSION &&  | 
1072  | 0  |                                !(c->algorithm_mkey & pfs_mask))  | 
1073  | 0  |                 return 0;  | 
1074  | 0  |             break;  | 
1075  | 0  |         }  | 
1076  | 0  |     case SSL_SECOP_VERSION:  | 
1077  | 0  |         if (!SSL_IS_DTLS(s)) { | 
1078  |  |             /* SSLv3 not allowed at level 2 */  | 
1079  | 0  |             if (nid <= SSL3_VERSION && level >= 2)  | 
1080  | 0  |                 return 0;  | 
1081  |  |             /* TLS v1.1 and above only for level 3 */  | 
1082  | 0  |             if (nid <= TLS1_VERSION && level >= 3)  | 
1083  | 0  |                 return 0;  | 
1084  |  |             /* TLS v1.2 only for level 4 and above */  | 
1085  | 0  |             if (nid <= TLS1_1_VERSION && level >= 4)  | 
1086  | 0  |                 return 0;  | 
1087  | 0  |         } else { | 
1088  |  |             /* DTLS v1.2 only for level 4 and above */  | 
1089  | 0  |             if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)  | 
1090  | 0  |                 return 0;  | 
1091  | 0  |         }  | 
1092  | 0  |         break;  | 
1093  |  |  | 
1094  | 0  |     case SSL_SECOP_COMPRESSION:  | 
1095  | 0  |         if (level >= 2)  | 
1096  | 0  |             return 0;  | 
1097  | 0  |         break;  | 
1098  | 0  |     case SSL_SECOP_TICKET:  | 
1099  | 0  |         if (level >= 3)  | 
1100  | 0  |             return 0;  | 
1101  | 0  |         break;  | 
1102  | 0  |     default:  | 
1103  | 0  |         if (bits < minbits)  | 
1104  | 0  |             return 0;  | 
1105  | 0  |     }  | 
1106  | 0  |     return 1;  | 
1107  | 0  | }  | 
1108  |  |  | 
1109  |  | int ssl_security(const SSL *s, int op, int bits, int nid, void *other)  | 
1110  | 14.7M  | { | 
1111  | 14.7M  |     return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);  | 
1112  | 14.7M  | }  | 
1113  |  |  | 
1114  |  | int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)  | 
1115  | 76.3k  | { | 
1116  | 76.3k  |     return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,  | 
1117  | 76.3k  |                              ctx->cert->sec_ex);  | 
1118  | 76.3k  | }  | 
1119  |  |  | 
1120  |  | int ssl_cert_lookup_by_nid(int nid, size_t *pidx)  | 
1121  | 780  | { | 
1122  | 780  |     size_t i;  | 
1123  |  |  | 
1124  | 2.19k  |     for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) { | 
1125  | 2.19k  |         if (ssl_cert_info[i].nid == nid) { | 
1126  | 780  |             *pidx = i;  | 
1127  | 780  |             return 1;  | 
1128  | 780  |         }  | 
1129  | 2.19k  |     }  | 
1130  |  |  | 
1131  | 0  |     return 0;  | 
1132  | 780  | }  | 
1133  |  |  | 
1134  |  | const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)  | 
1135  | 64.6k  | { | 
1136  | 64.6k  |     size_t i;  | 
1137  |  |  | 
1138  | 170k  |     for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) { | 
1139  | 170k  |         const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];  | 
1140  |  |  | 
1141  | 170k  |         if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))  | 
1142  | 170k  |             || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) { | 
1143  | 64.5k  |             if (pidx != NULL)  | 
1144  | 64.5k  |                 *pidx = i;  | 
1145  | 64.5k  |             return tmp_lu;  | 
1146  | 64.5k  |         }  | 
1147  | 170k  |     }  | 
1148  |  |  | 
1149  | 15  |     return NULL;  | 
1150  | 64.6k  | }  | 
1151  |  |  | 
1152  |  | const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)  | 
1153  | 1.57M  | { | 
1154  | 1.57M  |     if (idx >= OSSL_NELEM(ssl_cert_info))  | 
1155  | 0  |         return NULL;  | 
1156  | 1.57M  |     return &ssl_cert_info[idx];  | 
1157  | 1.57M  | }  |