/src/openssl/crypto/x509/x509_vfy.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 "internal/deprecated.h"  | 
11  |  |  | 
12  |  | #include <stdio.h>  | 
13  |  | #include <time.h>  | 
14  |  | #include <errno.h>  | 
15  |  | #include <limits.h>  | 
16  |  |  | 
17  |  | #include "crypto/ctype.h"  | 
18  |  | #include "internal/cryptlib.h"  | 
19  |  | #include <openssl/crypto.h>  | 
20  |  | #include <openssl/buffer.h>  | 
21  |  | #include <openssl/evp.h>  | 
22  |  | #include <openssl/asn1.h>  | 
23  |  | #include <openssl/x509.h>  | 
24  |  | #include <openssl/x509v3.h>  | 
25  |  | #include <openssl/ocsp.h>  | 
26  |  | #include <openssl/objects.h>  | 
27  |  | #include <openssl/core_names.h>  | 
28  |  | #include "internal/dane.h"  | 
29  |  | #include "crypto/x509.h"  | 
30  |  | #include "x509_local.h"  | 
31  |  |  | 
32  |  | /* CRL score values */  | 
33  |  |  | 
34  | 0  | #define CRL_SCORE_NOCRITICAL    0x100 /* No unhandled critical extensions */  | 
35  | 0  | #define CRL_SCORE_SCOPE         0x080 /* certificate is within CRL scope */  | 
36  | 0  | #define CRL_SCORE_TIME          0x040 /* CRL times valid */  | 
37  | 0  | #define CRL_SCORE_ISSUER_NAME   0x020 /* Issuer name matches certificate */  | 
38  |  | #define CRL_SCORE_VALID /* If this score or above CRL is probably valid */ \  | 
39  | 0  |     (CRL_SCORE_NOCRITICAL | CRL_SCORE_TIME | CRL_SCORE_SCOPE)  | 
40  | 0  | #define CRL_SCORE_ISSUER_CERT   0x018 /* CRL issuer is certificate issuer */  | 
41  | 0  | #define CRL_SCORE_SAME_PATH     0x008 /* CRL issuer is on certificate path */  | 
42  | 0  | #define CRL_SCORE_AKID          0x004 /* CRL issuer matches CRL AKID */  | 
43  | 0  | #define CRL_SCORE_TIME_DELTA    0x002 /* Have a delta CRL with valid times */  | 
44  |  |  | 
45  |  | static int x509_verify_x509(X509_STORE_CTX *ctx);  | 
46  |  | static int x509_verify_rpk(X509_STORE_CTX *ctx);  | 
47  |  | static int build_chain(X509_STORE_CTX *ctx);  | 
48  |  | static int verify_chain(X509_STORE_CTX *ctx);  | 
49  |  | static int verify_rpk(X509_STORE_CTX *ctx);  | 
50  |  | static int dane_verify(X509_STORE_CTX *ctx);  | 
51  |  | static int dane_verify_rpk(X509_STORE_CTX *ctx);  | 
52  |  | static int null_callback(int ok, X509_STORE_CTX *e);  | 
53  |  | static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);  | 
54  |  | static int check_extensions(X509_STORE_CTX *ctx);  | 
55  |  | static int check_name_constraints(X509_STORE_CTX *ctx);  | 
56  |  | static int check_id(X509_STORE_CTX *ctx);  | 
57  |  | static int check_trust(X509_STORE_CTX *ctx, int num_untrusted);  | 
58  |  | static int check_revocation(X509_STORE_CTX *ctx);  | 
59  |  | #ifndef OPENSSL_NO_OCSP  | 
60  |  | static int check_cert_ocsp_resp(X509_STORE_CTX *ctx);  | 
61  |  | #endif  | 
62  |  | static int check_cert_crl(X509_STORE_CTX *ctx);  | 
63  |  | static int check_policy(X509_STORE_CTX *ctx);  | 
64  |  | static int check_dane_issuer(X509_STORE_CTX *ctx, int depth);  | 
65  |  | static int check_cert_key_level(X509_STORE_CTX *ctx, X509 *cert);  | 
66  |  | static int check_key_level(X509_STORE_CTX *ctx, EVP_PKEY *pkey);  | 
67  |  | static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert);  | 
68  |  | static int check_curve(X509 *cert);  | 
69  |  |  | 
70  |  | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,  | 
71  |  |                          unsigned int *preasons, X509_CRL *crl, X509 *x);  | 
72  |  | static int get_crl_delta(X509_STORE_CTX *ctx,  | 
73  |  |                          X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);  | 
74  |  | static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,  | 
75  |  |                          int *pcrl_score, X509_CRL *base,  | 
76  |  |                          STACK_OF(X509_CRL) *crls);  | 
77  |  | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,  | 
78  |  |                            int *pcrl_score);  | 
79  |  | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,  | 
80  |  |                            unsigned int *preasons);  | 
81  |  | static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);  | 
82  |  | static int check_crl_chain(X509_STORE_CTX *ctx,  | 
83  |  |                            STACK_OF(X509) *cert_path,  | 
84  |  |                            STACK_OF(X509) *crl_path);  | 
85  |  |  | 
86  |  | static int internal_verify(X509_STORE_CTX *ctx);  | 
87  |  |  | 
88  |  | static int null_callback(int ok, X509_STORE_CTX *e)  | 
89  | 0  | { | 
90  | 0  |     return ok;  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | /*-  | 
94  |  |  * Return 1 if given cert is considered self-signed, 0 if not, or -1 on error.  | 
95  |  |  * This actually verifies self-signedness only if requested.  | 
96  |  |  * It calls ossl_x509v3_cache_extensions()  | 
97  |  |  * to match issuer and subject names (i.e., the cert being self-issued) and any  | 
98  |  |  * present authority key identifier to match the subject key identifier, etc.  | 
99  |  |  */  | 
100  |  | int X509_self_signed(X509 *cert, int verify_signature)  | 
101  | 0  | { | 
102  | 0  |     EVP_PKEY *pkey;  | 
103  |  | 
  | 
104  | 0  |     if ((pkey = X509_get0_pubkey(cert)) == NULL) { /* handles cert == NULL */ | 
105  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);  | 
106  | 0  |         return -1;  | 
107  | 0  |     }  | 
108  | 0  |     if (!ossl_x509v3_cache_extensions(cert))  | 
109  | 0  |         return -1;  | 
110  | 0  |     if ((cert->ex_flags & EXFLAG_SS) == 0)  | 
111  | 0  |         return 0;  | 
112  | 0  |     if (!verify_signature)  | 
113  | 0  |         return 1;  | 
114  | 0  |     return X509_verify(cert, pkey);  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | /*  | 
118  |  |  * Given a certificate, try and find an exact match in the store.  | 
119  |  |  * Returns 1 on success, 0 on not found, -1 on internal error.  | 
120  |  |  */  | 
121  |  | static int lookup_cert_match(X509 **result, X509_STORE_CTX *ctx, X509 *x)  | 
122  | 0  | { | 
123  | 0  |     STACK_OF(X509) *certs;  | 
124  | 0  |     X509 *xtmp = NULL;  | 
125  | 0  |     int i, ret;  | 
126  |  | 
  | 
127  | 0  |     *result = NULL;  | 
128  |  |     /* Lookup all certs with matching subject name */  | 
129  | 0  |     ERR_set_mark();  | 
130  | 0  |     certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));  | 
131  | 0  |     ERR_pop_to_mark();  | 
132  | 0  |     if (certs == NULL)  | 
133  | 0  |         return -1;  | 
134  |  |  | 
135  |  |     /* Look for exact match */  | 
136  | 0  |     for (i = 0; i < sk_X509_num(certs); i++) { | 
137  | 0  |         xtmp = sk_X509_value(certs, i);  | 
138  | 0  |         if (X509_cmp(xtmp, x) == 0)  | 
139  | 0  |             break;  | 
140  | 0  |         xtmp = NULL;  | 
141  | 0  |     }  | 
142  | 0  |     ret = xtmp != NULL;  | 
143  | 0  |     if (ret) { | 
144  | 0  |         if (!X509_up_ref(xtmp))  | 
145  | 0  |             ret = -1;  | 
146  | 0  |         else  | 
147  | 0  |             *result = xtmp;  | 
148  | 0  |     }  | 
149  | 0  |     OSSL_STACK_OF_X509_free(certs);  | 
150  | 0  |     return ret;  | 
151  | 0  | }  | 
152  |  |  | 
153  |  | /*-  | 
154  |  |  * Inform the verify callback of an error.  | 
155  |  |  * The error code is set to |err| if |err| is not X509_V_OK, else  | 
156  |  |  * |ctx->error| is left unchanged (under the assumption it is set elsewhere).  | 
157  |  |  * The error depth is |depth| if >= 0, else it defaults to |ctx->error_depth|.  | 
158  |  |  * The error cert is |x| if not NULL, else the cert in |ctx->chain| at |depth|.  | 
159  |  |  *  | 
160  |  |  * Returns 0 to abort verification with an error, non-zero to continue.  | 
161  |  |  */  | 
162  |  | static int verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err)  | 
163  | 0  | { | 
164  | 0  |     if (depth < 0)  | 
165  | 0  |         depth = ctx->error_depth;  | 
166  | 0  |     else  | 
167  | 0  |         ctx->error_depth = depth;  | 
168  | 0  |     ctx->current_cert = x != NULL ? x : sk_X509_value(ctx->chain, depth);  | 
169  | 0  |     if (err != X509_V_OK)  | 
170  | 0  |         ctx->error = err;  | 
171  | 0  |     return ctx->verify_cb(0, ctx);  | 
172  | 0  | }  | 
173  |  |  | 
174  |  | #define CB_FAIL_IF(cond, ctx, cert, depth, err) \  | 
175  | 0  |     if ((cond) && verify_cb_cert(ctx, cert, depth, err) == 0) \  | 
176  | 0  |         return 0  | 
177  |  |  | 
178  |  | /*-  | 
179  |  |  * Inform the verify callback of an error, CRL-specific variant.  Here, the  | 
180  |  |  * error depth and certificate are already set, we just specify the error  | 
181  |  |  * number.  | 
182  |  |  *  | 
183  |  |  * Returns 0 to abort verification with an error, non-zero to continue.  | 
184  |  |  */  | 
185  |  | static int verify_cb_crl(X509_STORE_CTX *ctx, int err)  | 
186  | 0  | { | 
187  | 0  |     ctx->error = err;  | 
188  | 0  |     return ctx->verify_cb(0, ctx);  | 
189  | 0  | }  | 
190  |  |  | 
191  |  | #ifndef OPENSSL_NO_OCSP  | 
192  |  | /*  | 
193  |  |  * Inform the verify callback of an error, OCSP-specific variant.  | 
194  |  |  * It is called also on OCSP response errors, if the  | 
195  |  |  * X509_V_FLAG_OCSP_RESP_CHECK or X509_V_FLAG_OCSP_RESP_CHECK_ALL flag  | 
196  |  |  * is set.  | 
197  |  |  * Here, the error depth and certificate are already set, we just specify  | 
198  |  |  * the error number.  | 
199  |  |  *  | 
200  |  |  * Returns 0 to abort verification with an error, non-zero to continue.  | 
201  |  |  */  | 
202  |  | static int verify_cb_ocsp(X509_STORE_CTX *ctx, int err)  | 
203  | 0  | { | 
204  | 0  |     ctx->error = err;  | 
205  | 0  |     return ctx->verify_cb(0, ctx);  | 
206  | 0  | }  | 
207  |  | #endif  | 
208  |  |  | 
209  |  | /* Sadly, returns 0 also on internal error in ctx->verify_cb(). */  | 
210  |  | static int check_auth_level(X509_STORE_CTX *ctx)  | 
211  | 0  | { | 
212  | 0  |     int i;  | 
213  | 0  |     int num = sk_X509_num(ctx->chain);  | 
214  |  | 
  | 
215  | 0  |     if (ctx->param->auth_level <= 0)  | 
216  | 0  |         return 1;  | 
217  |  |  | 
218  | 0  |     for (i = 0; i < num; ++i) { | 
219  | 0  |         X509 *cert = sk_X509_value(ctx->chain, i);  | 
220  |  |  | 
221  |  |         /*  | 
222  |  |          * We've already checked the security of the leaf key, so here we only  | 
223  |  |          * check the security of issuer keys.  | 
224  |  |          */  | 
225  | 0  |         CB_FAIL_IF(i > 0 && !check_cert_key_level(ctx, cert),  | 
226  | 0  |                    ctx, cert, i, X509_V_ERR_CA_KEY_TOO_SMALL);  | 
227  |  |         /*  | 
228  |  |          * We also check the signature algorithm security of all certificates  | 
229  |  |          * except those of the trust anchor at index num-1.  | 
230  |  |          */  | 
231  | 0  |         CB_FAIL_IF(i < num - 1 && !check_sig_level(ctx, cert),  | 
232  | 0  |                    ctx, cert, i, X509_V_ERR_CA_MD_TOO_WEAK);  | 
233  | 0  |     }  | 
234  | 0  |     return 1;  | 
235  | 0  | }  | 
236  |  |  | 
237  |  | /*-  | 
238  |  |  * Returns -1 on internal error.  | 
239  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
240  |  |  */  | 
241  |  | static int verify_rpk(X509_STORE_CTX *ctx)  | 
242  | 0  | { | 
243  |  |     /* Not much to verify on a RPK */  | 
244  | 0  |     if (ctx->verify != NULL)  | 
245  | 0  |         return ctx->verify(ctx);  | 
246  |  |  | 
247  | 0  |     return !!ctx->verify_cb(ctx->error == X509_V_OK, ctx);  | 
248  | 0  | }  | 
249  |  |  | 
250  |  | /*-  | 
251  |  |  * Returns -1 on internal error.  | 
252  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
253  |  |  */  | 
254  |  | static int verify_chain(X509_STORE_CTX *ctx)  | 
255  | 0  | { | 
256  | 0  |     int err;  | 
257  | 0  |     int ok;  | 
258  |  | 
  | 
259  | 0  |     if ((ok = build_chain(ctx)) <= 0  | 
260  | 0  |         || (ok = check_extensions(ctx)) <= 0  | 
261  | 0  |         || (ok = check_auth_level(ctx)) <= 0  | 
262  | 0  |         || (ok = check_id(ctx)) <= 0  | 
263  | 0  |         || (ok = X509_get_pubkey_parameters(NULL, ctx->chain) ? 1 : -1) <= 0  | 
264  | 0  |         || (ok = ctx->check_revocation(ctx)) <= 0)  | 
265  | 0  |         return ok;  | 
266  |  |  | 
267  | 0  |     err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,  | 
268  | 0  |                                   ctx->param->flags);  | 
269  | 0  |     CB_FAIL_IF(err != X509_V_OK, ctx, NULL, ctx->error_depth, err);  | 
270  |  |  | 
271  |  |     /* Verify chain signatures and expiration times */  | 
272  | 0  |     ok = ctx->verify != NULL ? ctx->verify(ctx) : internal_verify(ctx);  | 
273  | 0  |     if (ok <= 0)  | 
274  | 0  |         return ok;  | 
275  |  |  | 
276  | 0  |     if ((ok = check_name_constraints(ctx)) <= 0)  | 
277  | 0  |         return ok;  | 
278  |  |  | 
279  | 0  | #ifndef OPENSSL_NO_RFC3779  | 
280  |  |     /* RFC 3779 path validation, now that CRL check has been done */  | 
281  | 0  |     if ((ok = X509v3_asid_validate_path(ctx)) <= 0)  | 
282  | 0  |         return ok;  | 
283  | 0  |     if ((ok = X509v3_addr_validate_path(ctx)) <= 0)  | 
284  | 0  |         return ok;  | 
285  | 0  | #endif  | 
286  |  |  | 
287  |  |     /* If we get this far evaluate policies */  | 
288  | 0  |     if ((ctx->param->flags & X509_V_FLAG_POLICY_CHECK) != 0)  | 
289  | 0  |         ok = ctx->check_policy(ctx);  | 
290  | 0  |     return ok;  | 
291  | 0  | }  | 
292  |  |  | 
293  |  | int X509_STORE_CTX_verify(X509_STORE_CTX *ctx)  | 
294  | 0  | { | 
295  | 0  |     if (ctx == NULL) { | 
296  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
297  | 0  |         return -1;  | 
298  | 0  |     }  | 
299  | 0  |     if (ctx->rpk != NULL)  | 
300  | 0  |         return x509_verify_rpk(ctx);  | 
301  | 0  |     if (ctx->cert == NULL && sk_X509_num(ctx->untrusted) >= 1)  | 
302  | 0  |         ctx->cert = sk_X509_value(ctx->untrusted, 0);  | 
303  | 0  |     return x509_verify_x509(ctx);  | 
304  | 0  | }  | 
305  |  |  | 
306  |  | int X509_verify_cert(X509_STORE_CTX *ctx)  | 
307  | 0  | { | 
308  | 0  |     if (ctx == NULL) { | 
309  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
310  | 0  |         return -1;  | 
311  | 0  |     }  | 
312  | 0  |     return (ctx->rpk != NULL) ? x509_verify_rpk(ctx) : x509_verify_x509(ctx);  | 
313  | 0  | }  | 
314  |  |  | 
315  |  | /*-  | 
316  |  |  * Returns -1 on internal error.  | 
317  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
318  |  |  */  | 
319  |  | static int x509_verify_rpk(X509_STORE_CTX *ctx)  | 
320  | 0  | { | 
321  | 0  |     int ret;  | 
322  |  |  | 
323  |  |     /* If the peer's public key is too weak, we can stop early. */  | 
324  | 0  |     if (!check_key_level(ctx, ctx->rpk)  | 
325  | 0  |         && verify_cb_cert(ctx, NULL, 0, X509_V_ERR_EE_KEY_TOO_SMALL) == 0)  | 
326  | 0  |         return 0;  | 
327  |  |  | 
328  |  |     /* Barring any data to verify the RPK, simply report it as untrusted */  | 
329  | 0  |     ctx->error = X509_V_ERR_RPK_UNTRUSTED;  | 
330  |  | 
  | 
331  | 0  |     ret = DANETLS_ENABLED(ctx->dane) ? dane_verify_rpk(ctx) : verify_rpk(ctx);  | 
332  |  |  | 
333  |  |     /*  | 
334  |  |      * Safety-net.  If we are returning an error, we must also set ctx->error,  | 
335  |  |      * so that the chain is not considered verified should the error be ignored  | 
336  |  |      * (e.g. TLS with SSL_VERIFY_NONE).  | 
337  |  |      */  | 
338  | 0  |     if (ret <= 0 && ctx->error == X509_V_OK)  | 
339  | 0  |         ctx->error = X509_V_ERR_UNSPECIFIED;  | 
340  | 0  |     return ret;  | 
341  | 0  | }  | 
342  |  |  | 
343  |  | /*-  | 
344  |  |  * Returns -1 on internal error.  | 
345  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
346  |  |  */  | 
347  |  | static int x509_verify_x509(X509_STORE_CTX *ctx)  | 
348  | 0  | { | 
349  | 0  |     int ret;  | 
350  |  | 
  | 
351  | 0  |     if (ctx->cert == NULL) { | 
352  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);  | 
353  | 0  |         ctx->error = X509_V_ERR_INVALID_CALL;  | 
354  | 0  |         return -1;  | 
355  | 0  |     }  | 
356  |  |  | 
357  | 0  |     if (ctx->chain != NULL) { | 
358  |  |         /*  | 
359  |  |          * This X509_STORE_CTX has already been used to verify a cert. We  | 
360  |  |          * cannot do another one.  | 
361  |  |          */  | 
362  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
363  | 0  |         ctx->error = X509_V_ERR_INVALID_CALL;  | 
364  | 0  |         return -1;  | 
365  | 0  |     }  | 
366  |  |  | 
367  | 0  |     if (!ossl_x509_add_cert_new(&ctx->chain, ctx->cert, X509_ADD_FLAG_UP_REF)) { | 
368  | 0  |         ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
369  | 0  |         return -1;  | 
370  | 0  |     }  | 
371  | 0  |     ctx->num_untrusted = 1;  | 
372  |  |  | 
373  |  |     /* If the peer's public key is too weak, we can stop early. */  | 
374  | 0  |     CB_FAIL_IF(!check_cert_key_level(ctx, ctx->cert),  | 
375  | 0  |                ctx, ctx->cert, 0, X509_V_ERR_EE_KEY_TOO_SMALL);  | 
376  |  |  | 
377  | 0  |     ret = DANETLS_ENABLED(ctx->dane) ? dane_verify(ctx) : verify_chain(ctx);  | 
378  |  |  | 
379  |  |     /*  | 
380  |  |      * Safety-net.  If we are returning an error, we must also set ctx->error,  | 
381  |  |      * so that the chain is not considered verified should the error be ignored  | 
382  |  |      * (e.g. TLS with SSL_VERIFY_NONE).  | 
383  |  |      */  | 
384  | 0  |     if (ret <= 0 && ctx->error == X509_V_OK)  | 
385  | 0  |         ctx->error = X509_V_ERR_UNSPECIFIED;  | 
386  | 0  |     return ret;  | 
387  | 0  | }  | 
388  |  |  | 
389  |  | static int sk_X509_contains(STACK_OF(X509) *sk, X509 *cert)  | 
390  | 0  | { | 
391  | 0  |     int i, n = sk_X509_num(sk);  | 
392  |  | 
  | 
393  | 0  |     for (i = 0; i < n; i++)  | 
394  | 0  |         if (X509_cmp(sk_X509_value(sk, i), cert) == 0)  | 
395  | 0  |             return 1;  | 
396  | 0  |     return 0;  | 
397  | 0  | }  | 
398  |  |  | 
399  |  | /*-  | 
400  |  |  * Find in |sk| an issuer cert of cert |x| accepted by |ctx->check_issued|.  | 
401  |  |  * If no_dup, the issuer must not yet be in |ctx->chain|, yet allowing the  | 
402  |  |  *     exception that |x| is self-issued and |ctx->chain| has just one element.  | 
403  |  |  * Prefer the first match with suitable validity period or latest expiration.  | 
404  |  |  */  | 
405  |  | /*  | 
406  |  |  * Note: so far, we do not check during chain building  | 
407  |  |  * whether any key usage extension stands against a candidate issuer cert.  | 
408  |  |  * Likely it would be good if build_chain() sets |check_signing_allowed|.  | 
409  |  |  * Yet if |sk| is a list of trusted certs, as with X509_STORE_CTX_set0_trusted_stack(),  | 
410  |  |  * better not set |check_signing_allowed|.  | 
411  |  |  * Maybe not touch X509_STORE_CTX_get1_issuer(), for API backward compatibility.  | 
412  |  |  */  | 
413  |  | static X509 *get0_best_issuer_sk(X509_STORE_CTX *ctx, int check_signing_allowed,  | 
414  |  |                                  int no_dup, STACK_OF(X509) *sk, X509 *x)  | 
415  | 0  | { | 
416  | 0  |     int i;  | 
417  | 0  |     X509 *candidate, *issuer = NULL;  | 
418  |  | 
  | 
419  | 0  |     for (i = 0; i < sk_X509_num(sk); i++) { | 
420  | 0  |         candidate = sk_X509_value(sk, i);  | 
421  | 0  |         if (no_dup  | 
422  | 0  |             && !((x->ex_flags & EXFLAG_SI) != 0 && sk_X509_num(ctx->chain) == 1)  | 
423  | 0  |             && sk_X509_contains(ctx->chain, candidate))  | 
424  | 0  |             continue;  | 
425  | 0  |         if (ctx->check_issued(ctx, x, candidate)) { | 
426  | 0  |             if (check_signing_allowed  | 
427  |  |                 /* yet better not check key usage for trust anchors */  | 
428  | 0  |                 && ossl_x509_signing_allowed(candidate, x) != X509_V_OK)  | 
429  | 0  |                 continue;  | 
430  | 0  |             if (ossl_x509_check_cert_time(ctx, candidate, -1))  | 
431  | 0  |                 return candidate;  | 
432  |  |             /*  | 
433  |  |              * Leave in *issuer the first match that has the latest expiration  | 
434  |  |              * date so we return nearest match if no certificate time is OK.  | 
435  |  |              */  | 
436  | 0  |             if (issuer == NULL  | 
437  | 0  |                     || ASN1_TIME_compare(X509_get0_notAfter(candidate),  | 
438  | 0  |                                          X509_get0_notAfter(issuer)) > 0)  | 
439  | 0  |                 issuer = candidate;  | 
440  | 0  |         }  | 
441  | 0  |     }  | 
442  | 0  |     return issuer;  | 
443  | 0  | }  | 
444  |  |  | 
445  |  | /*-  | 
446  |  |  * Try to get issuer cert from |ctx->store| accepted by |ctx->check_issued|.  | 
447  |  |  * Prefer the first match with suitable validity period or latest expiration.  | 
448  |  |  *  | 
449  |  |  * Return values are:  | 
450  |  |  *  1 lookup successful.  | 
451  |  |  *  0 certificate not found.  | 
452  |  |  * -1 some other error.  | 
453  |  |  */  | 
454  |  | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)  | 
455  | 0  | { | 
456  | 0  |     const X509_NAME *xn = X509_get_issuer_name(x);  | 
457  | 0  |     X509_OBJECT *obj = X509_OBJECT_new();  | 
458  | 0  |     STACK_OF(X509) *certs;  | 
459  | 0  |     int ret;  | 
460  |  | 
  | 
461  | 0  |     *issuer = NULL;  | 
462  | 0  |     if (obj == NULL)  | 
463  | 0  |         return -1;  | 
464  | 0  |     ret = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_X509, xn, obj);  | 
465  | 0  |     if (ret != 1)  | 
466  | 0  |         goto end;  | 
467  |  |  | 
468  |  |     /* quick happy path: certificate matches and is currently valid */  | 
469  | 0  |     if (ctx->check_issued(ctx, x, obj->data.x509)) { | 
470  | 0  |         if (ossl_x509_check_cert_time(ctx, obj->data.x509, -1)) { | 
471  | 0  |             *issuer = obj->data.x509;  | 
472  |  |             /* |*issuer| has taken over the cert reference from |obj| */  | 
473  | 0  |             obj->type = X509_LU_NONE;  | 
474  | 0  |             goto end;  | 
475  | 0  |         }  | 
476  | 0  |     }  | 
477  |  |  | 
478  | 0  |     ret = -1;  | 
479  | 0  |     if ((certs = X509_STORE_CTX_get1_certs(ctx, xn)) == NULL)  | 
480  | 0  |         goto end;  | 
481  | 0  |     *issuer = get0_best_issuer_sk(ctx, 0, 0 /* allow duplicates */, certs, x);  | 
482  | 0  |     ret = 0;  | 
483  | 0  |     if (*issuer != NULL)  | 
484  | 0  |         ret = X509_up_ref(*issuer) ? 1 : -1;  | 
485  | 0  |     OSSL_STACK_OF_X509_free(certs);  | 
486  | 0  |  end:  | 
487  | 0  |     X509_OBJECT_free(obj);  | 
488  | 0  |     return ret;  | 
489  | 0  | }  | 
490  |  |  | 
491  |  | /* Check that the given certificate |x| is issued by the certificate |issuer| */  | 
492  |  | static int check_issued(ossl_unused X509_STORE_CTX *ctx, X509 *x, X509 *issuer)  | 
493  | 0  | { | 
494  | 0  |     int err = ossl_x509_likely_issued(issuer, x);  | 
495  |  | 
  | 
496  | 0  |     if (err == X509_V_OK)  | 
497  | 0  |         return 1;  | 
498  |  |     /*  | 
499  |  |      * SUBJECT_ISSUER_MISMATCH just means 'x' is clearly not issued by 'issuer'.  | 
500  |  |      * Every other error code likely indicates a real error.  | 
501  |  |      */  | 
502  | 0  |     return 0;  | 
503  | 0  | }  | 
504  |  |  | 
505  |  | /*-  | 
506  |  |  * Alternative get_issuer method: look up from a STACK_OF(X509) in other_ctx.  | 
507  |  |  * Returns -1 on internal error.  | 
508  |  |  */  | 
509  |  | static int get1_best_issuer_other_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)  | 
510  | 0  | { | 
511  | 0  |     *issuer = get0_best_issuer_sk(ctx, 0, 1 /* no_dup */, ctx->other_ctx, x);  | 
512  | 0  |     if (*issuer == NULL)  | 
513  | 0  |         return 0;  | 
514  | 0  |     return X509_up_ref(*issuer) ? 1 : -1;  | 
515  | 0  | }  | 
516  |  |  | 
517  |  | /*-  | 
518  |  |  * Alternative lookup method: look from a STACK stored in other_ctx.  | 
519  |  |  * Returns NULL on internal/fatal error, empty stack if not found.  | 
520  |  |  */  | 
521  |  | static STACK_OF(X509) *lookup_certs_sk(X509_STORE_CTX *ctx, const X509_NAME *nm)  | 
522  | 0  | { | 
523  | 0  |     STACK_OF(X509) *sk = sk_X509_new_null();  | 
524  | 0  |     X509 *x;  | 
525  | 0  |     int i;  | 
526  |  | 
  | 
527  | 0  |     if (sk == NULL)  | 
528  | 0  |         return NULL;  | 
529  | 0  |     for (i = 0; i < sk_X509_num(ctx->other_ctx); i++) { | 
530  | 0  |         x = sk_X509_value(ctx->other_ctx, i);  | 
531  | 0  |         if (X509_NAME_cmp(nm, X509_get_subject_name(x)) == 0) { | 
532  | 0  |             if (!X509_add_cert(sk, x, X509_ADD_FLAG_UP_REF)) { | 
533  | 0  |                 OSSL_STACK_OF_X509_free(sk);  | 
534  | 0  |                 ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
535  | 0  |                 return NULL;  | 
536  | 0  |             }  | 
537  | 0  |         }  | 
538  | 0  |     }  | 
539  | 0  |     return sk;  | 
540  | 0  | }  | 
541  |  |  | 
542  |  | /*  | 
543  |  |  * Check EE or CA certificate purpose.  For trusted certificates explicit local  | 
544  |  |  * auxiliary trust can be used to override EKU-restrictions.  | 
545  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
546  |  |  */  | 
547  |  | static int check_purpose(X509_STORE_CTX *ctx, X509 *x, int purpose, int depth,  | 
548  |  |                          int must_be_ca)  | 
549  | 0  | { | 
550  | 0  |     int tr_ok = X509_TRUST_UNTRUSTED;  | 
551  |  |  | 
552  |  |     /*  | 
553  |  |      * For trusted certificates we want to see whether any auxiliary trust  | 
554  |  |      * settings trump the purpose constraints.  | 
555  |  |      *  | 
556  |  |      * This is complicated by the fact that the trust ordinals in  | 
557  |  |      * ctx->param->trust are entirely independent of the purpose ordinals in  | 
558  |  |      * ctx->param->purpose!  | 
559  |  |      *  | 
560  |  |      * What connects them is their mutual initialization via calls from  | 
561  |  |      * X509_STORE_CTX_set_default() into X509_VERIFY_PARAM_lookup() which sets  | 
562  |  |      * related values of both param->trust and param->purpose.  It is however  | 
563  |  |      * typically possible to infer associated trust values from a purpose value  | 
564  |  |      * via the X509_PURPOSE API.  | 
565  |  |      *  | 
566  |  |      * Therefore, we can only check for trust overrides when the purpose we're  | 
567  |  |      * checking is the same as ctx->param->purpose and ctx->param->trust is  | 
568  |  |      * also set.  | 
569  |  |      */  | 
570  | 0  |     if (depth >= ctx->num_untrusted && purpose == ctx->param->purpose)  | 
571  | 0  |         tr_ok = X509_check_trust(x, ctx->param->trust, X509_TRUST_NO_SS_COMPAT);  | 
572  |  | 
  | 
573  | 0  |     switch (tr_ok) { | 
574  | 0  |     case X509_TRUST_TRUSTED:  | 
575  | 0  |         return 1;  | 
576  | 0  |     case X509_TRUST_REJECTED:  | 
577  | 0  |         break;  | 
578  | 0  |     default: /* can only be X509_TRUST_UNTRUSTED */  | 
579  | 0  |         switch (X509_check_purpose(x, purpose, must_be_ca > 0)) { | 
580  | 0  |         case 1:  | 
581  | 0  |             return 1;  | 
582  | 0  |         case 0:  | 
583  | 0  |             break;  | 
584  | 0  |         default:  | 
585  | 0  |             if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) == 0)  | 
586  | 0  |                 return 1;  | 
587  | 0  |         }  | 
588  | 0  |         break;  | 
589  | 0  |     }  | 
590  |  |  | 
591  | 0  |     return verify_cb_cert(ctx, x, depth, X509_V_ERR_INVALID_PURPOSE);  | 
592  | 0  | }  | 
593  |  |  | 
594  |  | /*-  | 
595  |  |  * Check extensions of a cert chain for consistency with the supplied purpose.  | 
596  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
597  |  |  */  | 
598  |  | static int check_extensions(X509_STORE_CTX *ctx)  | 
599  | 0  | { | 
600  | 0  |     int i, must_be_ca, plen = 0;  | 
601  | 0  |     X509 *x;  | 
602  | 0  |     int ret, proxy_path_length = 0;  | 
603  | 0  |     int purpose, allow_proxy_certs, num = sk_X509_num(ctx->chain);  | 
604  |  |  | 
605  |  |     /*-  | 
606  |  |      *  must_be_ca can have 1 of 3 values:  | 
607  |  |      * -1: we accept both CA and non-CA certificates, to allow direct  | 
608  |  |      *     use of self-signed certificates (which are marked as CA).  | 
609  |  |      * 0:  we only accept non-CA certificates.  This is currently not  | 
610  |  |      *     used, but the possibility is present for future extensions.  | 
611  |  |      * 1:  we only accept CA certificates.  This is currently used for  | 
612  |  |      *     all certificates in the chain except the leaf certificate.  | 
613  |  |      */  | 
614  | 0  |     must_be_ca = -1;  | 
615  |  |  | 
616  |  |     /* CRL path validation */  | 
617  | 0  |     if (ctx->parent != NULL) { | 
618  | 0  |         allow_proxy_certs = 0;  | 
619  | 0  |         purpose = X509_PURPOSE_CRL_SIGN;  | 
620  | 0  |     } else { | 
621  | 0  |         allow_proxy_certs =  | 
622  | 0  |             (ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS) != 0;  | 
623  | 0  |         purpose = ctx->param->purpose;  | 
624  | 0  |     }  | 
625  |  | 
  | 
626  | 0  |     for (i = 0; i < num; i++) { | 
627  | 0  |         x = sk_X509_value(ctx->chain, i);  | 
628  | 0  |         CB_FAIL_IF((ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) == 0  | 
629  | 0  |                        && (x->ex_flags & EXFLAG_CRITICAL) != 0,  | 
630  | 0  |                    ctx, x, i, X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION);  | 
631  | 0  |         CB_FAIL_IF(!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY) != 0,  | 
632  | 0  |                    ctx, x, i, X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED);  | 
633  | 0  |         ret = X509_check_ca(x);  | 
634  | 0  |         switch (must_be_ca) { | 
635  | 0  |         case -1:  | 
636  | 0  |             CB_FAIL_IF((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0  | 
637  | 0  |                            && ret != 1 && ret != 0,  | 
638  | 0  |                        ctx, x, i, X509_V_ERR_INVALID_CA);  | 
639  | 0  |             break;  | 
640  | 0  |         case 0:  | 
641  | 0  |             CB_FAIL_IF(ret != 0, ctx, x, i, X509_V_ERR_INVALID_NON_CA);  | 
642  | 0  |             break;  | 
643  | 0  |         default:  | 
644  |  |             /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */  | 
645  | 0  |             CB_FAIL_IF(ret == 0  | 
646  | 0  |                        || ((i + 1 < num  | 
647  | 0  |                             || (ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0)  | 
648  | 0  |                            && ret != 1), ctx, x, i, X509_V_ERR_INVALID_CA);  | 
649  | 0  |             break;  | 
650  | 0  |         }  | 
651  | 0  |         if (num > 1) { | 
652  |  |             /* Check for presence of explicit elliptic curve parameters */  | 
653  | 0  |             ret = check_curve(x);  | 
654  | 0  |             CB_FAIL_IF(ret < 0, ctx, x, i, X509_V_ERR_UNSPECIFIED);  | 
655  | 0  |             CB_FAIL_IF(ret == 0, ctx, x, i, X509_V_ERR_EC_KEY_EXPLICIT_PARAMS);  | 
656  | 0  |         }  | 
657  |  |         /*  | 
658  |  |          * Do the following set of checks only if strict checking is requested  | 
659  |  |          * and not for self-issued (including self-signed) EE (non-CA) certs  | 
660  |  |          * because RFC 5280 does not apply to them according RFC 6818 section 2.  | 
661  |  |          */  | 
662  | 0  |         if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0  | 
663  | 0  |             && num > 1) { /* | 
664  |  |                            * this should imply  | 
665  |  |                            * !(i == 0 && (x->ex_flags & EXFLAG_CA) == 0  | 
666  |  |                            *          && (x->ex_flags & EXFLAG_SI) != 0)  | 
667  |  |                            */  | 
668  |  |             /* Check Basic Constraints according to RFC 5280 section 4.2.1.9 */  | 
669  | 0  |             if (x->ex_pathlen != -1) { | 
670  | 0  |                 CB_FAIL_IF((x->ex_flags & EXFLAG_CA) == 0,  | 
671  | 0  |                            ctx, x, i, X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA);  | 
672  | 0  |                 CB_FAIL_IF((x->ex_kusage & KU_KEY_CERT_SIGN) == 0, ctx,  | 
673  | 0  |                            x, i, X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN);  | 
674  | 0  |             }  | 
675  | 0  |             CB_FAIL_IF((x->ex_flags & EXFLAG_CA) != 0  | 
676  | 0  |                            && (x->ex_flags & EXFLAG_BCONS) != 0  | 
677  | 0  |                            && (x->ex_flags & EXFLAG_BCONS_CRITICAL) == 0,  | 
678  | 0  |                        ctx, x, i, X509_V_ERR_CA_BCONS_NOT_CRITICAL);  | 
679  |  |             /* Check Key Usage according to RFC 5280 section 4.2.1.3 */  | 
680  | 0  |             if ((x->ex_flags & EXFLAG_CA) != 0) { | 
681  | 0  |                 CB_FAIL_IF((x->ex_flags & EXFLAG_KUSAGE) == 0,  | 
682  | 0  |                            ctx, x, i, X509_V_ERR_CA_CERT_MISSING_KEY_USAGE);  | 
683  | 0  |             } else { | 
684  | 0  |                 CB_FAIL_IF((x->ex_kusage & KU_KEY_CERT_SIGN) != 0, ctx, x, i,  | 
685  | 0  |                            X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA);  | 
686  | 0  |             }  | 
687  |  |             /* Check issuer is non-empty acc. to RFC 5280 section 4.1.2.4 */  | 
688  | 0  |             CB_FAIL_IF(X509_NAME_entry_count(X509_get_issuer_name(x)) == 0,  | 
689  | 0  |                        ctx, x, i, X509_V_ERR_ISSUER_NAME_EMPTY);  | 
690  |  |             /* Check subject is non-empty acc. to RFC 5280 section 4.1.2.6 */  | 
691  | 0  |             CB_FAIL_IF(((x->ex_flags & EXFLAG_CA) != 0  | 
692  | 0  |                         || (x->ex_kusage & KU_CRL_SIGN) != 0  | 
693  | 0  |                         || x->altname == NULL)  | 
694  | 0  |                        && X509_NAME_entry_count(X509_get_subject_name(x)) == 0,  | 
695  | 0  |                        ctx, x, i, X509_V_ERR_SUBJECT_NAME_EMPTY);  | 
696  | 0  |             CB_FAIL_IF(X509_NAME_entry_count(X509_get_subject_name(x)) == 0  | 
697  | 0  |                            && x->altname != NULL  | 
698  | 0  |                            && (x->ex_flags & EXFLAG_SAN_CRITICAL) == 0,  | 
699  | 0  |                        ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL);  | 
700  |  |             /* Check SAN is non-empty according to RFC 5280 section 4.2.1.6 */  | 
701  | 0  |             CB_FAIL_IF(x->altname != NULL  | 
702  | 0  |                            && sk_GENERAL_NAME_num(x->altname) <= 0,  | 
703  | 0  |                        ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_ALT_NAME);  | 
704  |  |             /* Check sig alg consistency acc. to RFC 5280 section 4.1.1.2 */  | 
705  | 0  |             CB_FAIL_IF(X509_ALGOR_cmp(&x->sig_alg, &x->cert_info.signature) != 0,  | 
706  | 0  |                        ctx, x, i, X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY);  | 
707  | 0  |             CB_FAIL_IF(x->akid != NULL  | 
708  | 0  |                            && (x->ex_flags & EXFLAG_AKID_CRITICAL) != 0,  | 
709  | 0  |                        ctx, x, i, X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL);  | 
710  | 0  |             CB_FAIL_IF(x->skid != NULL  | 
711  | 0  |                            && (x->ex_flags & EXFLAG_SKID_CRITICAL) != 0,  | 
712  | 0  |                        ctx, x, i, X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL);  | 
713  | 0  |             if (X509_get_version(x) >= X509_VERSION_3) { | 
714  |  |                 /* Check AKID presence acc. to RFC 5280 section 4.2.1.1 */  | 
715  | 0  |                 CB_FAIL_IF(i + 1 < num /*  | 
716  |  |                                         * this means not last cert in chain,  | 
717  |  |                                         * taken as "generated by conforming CAs"  | 
718  |  |                                         */  | 
719  | 0  |                            && (x->akid == NULL || x->akid->keyid == NULL), ctx,  | 
720  | 0  |                            x, i, X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER);  | 
721  |  |                 /* Check SKID presence acc. to RFC 5280 section 4.2.1.2 */  | 
722  | 0  |                 CB_FAIL_IF((x->ex_flags & EXFLAG_CA) != 0 && x->skid == NULL,  | 
723  | 0  |                            ctx, x, i, X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER);  | 
724  | 0  |             } else { | 
725  | 0  |                 CB_FAIL_IF(sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0,  | 
726  | 0  |                            ctx, x, i, X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3);  | 
727  | 0  |             }  | 
728  | 0  |         }  | 
729  |  |  | 
730  |  |         /* check_purpose() makes the callback as needed */  | 
731  | 0  |         if (purpose >= X509_PURPOSE_MIN && !check_purpose(ctx, x, purpose, i, must_be_ca))  | 
732  | 0  |             return 0;  | 
733  |  |         /* Check path length */  | 
734  | 0  |         CB_FAIL_IF(i > 1 && x->ex_pathlen != -1  | 
735  | 0  |                        && plen > x->ex_pathlen + proxy_path_length,  | 
736  | 0  |                    ctx, x, i, X509_V_ERR_PATH_LENGTH_EXCEEDED);  | 
737  |  |         /* Increment path length if not a self-issued intermediate CA */  | 
738  | 0  |         if (i > 0 && (x->ex_flags & EXFLAG_SI) == 0)  | 
739  | 0  |             plen++;  | 
740  |  |         /*  | 
741  |  |          * If this certificate is a proxy certificate, the next certificate  | 
742  |  |          * must be another proxy certificate or a EE certificate.  If not,  | 
743  |  |          * the next certificate must be a CA certificate.  | 
744  |  |          */  | 
745  | 0  |         if (x->ex_flags & EXFLAG_PROXY) { | 
746  |  |             /*  | 
747  |  |              * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint  | 
748  |  |              * is less than max_path_length, the former should be copied to  | 
749  |  |              * the latter, and 4.1.4 (a) stipulates that max_path_length  | 
750  |  |              * should be verified to be larger than zero and decrement it.  | 
751  |  |              *  | 
752  |  |              * Because we're checking the certs in the reverse order, we start  | 
753  |  |              * with verifying that proxy_path_length isn't larger than pcPLC,  | 
754  |  |              * and copy the latter to the former if it is, and finally,  | 
755  |  |              * increment proxy_path_length.  | 
756  |  |              */  | 
757  | 0  |             if (x->ex_pcpathlen != -1) { | 
758  | 0  |                 CB_FAIL_IF(proxy_path_length > x->ex_pcpathlen,  | 
759  | 0  |                            ctx, x, i, X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED);  | 
760  | 0  |                 proxy_path_length = x->ex_pcpathlen;  | 
761  | 0  |             }  | 
762  | 0  |             proxy_path_length++;  | 
763  | 0  |             must_be_ca = 0;  | 
764  | 0  |         } else { | 
765  | 0  |             must_be_ca = 1;  | 
766  | 0  |         }  | 
767  | 0  |     }  | 
768  | 0  |     return 1;  | 
769  | 0  | }  | 
770  |  |  | 
771  |  | static int has_san_id(X509 *x, int gtype)  | 
772  | 0  | { | 
773  | 0  |     int i;  | 
774  | 0  |     int ret = 0;  | 
775  | 0  |     GENERAL_NAMES *gs = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);  | 
776  |  | 
  | 
777  | 0  |     if (gs == NULL)  | 
778  | 0  |         return 0;  | 
779  |  |  | 
780  | 0  |     for (i = 0; i < sk_GENERAL_NAME_num(gs); i++) { | 
781  | 0  |         GENERAL_NAME *g = sk_GENERAL_NAME_value(gs, i);  | 
782  |  | 
  | 
783  | 0  |         if (g->type == gtype) { | 
784  | 0  |             ret = 1;  | 
785  | 0  |             break;  | 
786  | 0  |         }  | 
787  | 0  |     }  | 
788  | 0  |     GENERAL_NAMES_free(gs);  | 
789  | 0  |     return ret;  | 
790  | 0  | }  | 
791  |  |  | 
792  |  | /*-  | 
793  |  |  * Returns -1 on internal error.  | 
794  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
795  |  |  */  | 
796  |  | static int check_name_constraints(X509_STORE_CTX *ctx)  | 
797  | 0  | { | 
798  | 0  |     int i;  | 
799  |  |  | 
800  |  |     /* Check name constraints for all certificates */  | 
801  | 0  |     for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) { | 
802  | 0  |         X509 *x = sk_X509_value(ctx->chain, i);  | 
803  | 0  |         int j;  | 
804  |  |  | 
805  |  |         /* Ignore self-issued certs unless last in chain */  | 
806  | 0  |         if (i != 0 && (x->ex_flags & EXFLAG_SI) != 0)  | 
807  | 0  |             continue;  | 
808  |  |  | 
809  |  |         /*  | 
810  |  |          * Proxy certificates policy has an extra constraint, where the  | 
811  |  |          * certificate subject MUST be the issuer with a single CN entry  | 
812  |  |          * added.  | 
813  |  |          * (RFC 3820: 3.4, 4.1.3 (a)(4))  | 
814  |  |          */  | 
815  | 0  |         if ((x->ex_flags & EXFLAG_PROXY) != 0) { | 
816  | 0  |             X509_NAME *tmpsubject = X509_get_subject_name(x);  | 
817  | 0  |             X509_NAME *tmpissuer = X509_get_issuer_name(x);  | 
818  | 0  |             X509_NAME_ENTRY *tmpentry = NULL;  | 
819  | 0  |             int last_nid = 0;  | 
820  | 0  |             int err = X509_V_OK;  | 
821  | 0  |             int last_loc = X509_NAME_entry_count(tmpsubject) - 1;  | 
822  |  |  | 
823  |  |             /* Check that there are at least two RDNs */  | 
824  | 0  |             if (last_loc < 1) { | 
825  | 0  |                 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;  | 
826  | 0  |                 goto proxy_name_done;  | 
827  | 0  |             }  | 
828  |  |  | 
829  |  |             /*  | 
830  |  |              * Check that there is exactly one more RDN in subject as  | 
831  |  |              * there is in issuer.  | 
832  |  |              */  | 
833  | 0  |             if (X509_NAME_entry_count(tmpsubject)  | 
834  | 0  |                 != X509_NAME_entry_count(tmpissuer) + 1) { | 
835  | 0  |                 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;  | 
836  | 0  |                 goto proxy_name_done;  | 
837  | 0  |             }  | 
838  |  |  | 
839  |  |             /*  | 
840  |  |              * Check that the last subject component isn't part of a  | 
841  |  |              * multi-valued RDN  | 
842  |  |              */  | 
843  | 0  |             if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject, last_loc))  | 
844  | 0  |                 == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject,  | 
845  | 0  |                                                            last_loc - 1))) { | 
846  | 0  |                 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;  | 
847  | 0  |                 goto proxy_name_done;  | 
848  | 0  |             }  | 
849  |  |  | 
850  |  |             /*  | 
851  |  |              * Check that the last subject RDN is a commonName, and that  | 
852  |  |              * all the previous RDNs match the issuer exactly  | 
853  |  |              */  | 
854  | 0  |             tmpsubject = X509_NAME_dup(tmpsubject);  | 
855  | 0  |             if (tmpsubject == NULL) { | 
856  | 0  |                 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
857  | 0  |                 ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
858  | 0  |                 return -1;  | 
859  | 0  |             }  | 
860  |  |  | 
861  | 0  |             tmpentry = X509_NAME_delete_entry(tmpsubject, last_loc);  | 
862  | 0  |             last_nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry));  | 
863  |  | 
  | 
864  | 0  |             if (last_nid != NID_commonName  | 
865  | 0  |                 || X509_NAME_cmp(tmpsubject, tmpissuer) != 0) { | 
866  | 0  |                 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;  | 
867  | 0  |             }  | 
868  |  | 
  | 
869  | 0  |             X509_NAME_ENTRY_free(tmpentry);  | 
870  | 0  |             X509_NAME_free(tmpsubject);  | 
871  |  | 
  | 
872  | 0  |         proxy_name_done:  | 
873  | 0  |             CB_FAIL_IF(err != X509_V_OK, ctx, x, i, err);  | 
874  | 0  |         }  | 
875  |  |  | 
876  |  |         /*  | 
877  |  |          * Check against constraints for all certificates higher in chain  | 
878  |  |          * including trust anchor. Trust anchor not strictly speaking needed  | 
879  |  |          * but if it includes constraints it is to be assumed it expects them  | 
880  |  |          * to be obeyed.  | 
881  |  |          */  | 
882  | 0  |         for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) { | 
883  | 0  |             NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;  | 
884  |  | 
  | 
885  | 0  |             if (nc) { | 
886  | 0  |                 int rv = NAME_CONSTRAINTS_check(x, nc);  | 
887  | 0  |                 int ret = 1;  | 
888  |  |  | 
889  |  |                 /* If EE certificate check commonName too */  | 
890  | 0  |                 if (rv == X509_V_OK && i == 0  | 
891  | 0  |                     && (ctx->param->hostflags  | 
892  | 0  |                         & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT) == 0  | 
893  | 0  |                     && ((ctx->param->hostflags  | 
894  | 0  |                          & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) != 0  | 
895  | 0  |                         || (ret = has_san_id(x, GEN_DNS)) == 0))  | 
896  | 0  |                     rv = NAME_CONSTRAINTS_check_CN(x, nc);  | 
897  | 0  |                 if (ret < 0)  | 
898  | 0  |                     return ret;  | 
899  |  |  | 
900  | 0  |                 switch (rv) { | 
901  | 0  |                 case X509_V_OK:  | 
902  | 0  |                     break;  | 
903  | 0  |                 case X509_V_ERR_OUT_OF_MEM:  | 
904  | 0  |                     return -1;  | 
905  | 0  |                 default:  | 
906  | 0  |                     CB_FAIL_IF(1, ctx, x, i, rv);  | 
907  | 0  |                     break;  | 
908  | 0  |                 }  | 
909  | 0  |             }  | 
910  | 0  |         }  | 
911  | 0  |     }  | 
912  | 0  |     return 1;  | 
913  | 0  | }  | 
914  |  |  | 
915  |  | static int check_id_error(X509_STORE_CTX *ctx, int errcode)  | 
916  | 0  | { | 
917  | 0  |     return verify_cb_cert(ctx, ctx->cert, 0, errcode);  | 
918  | 0  | }  | 
919  |  |  | 
920  |  | static int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm)  | 
921  | 0  | { | 
922  | 0  |     int i;  | 
923  | 0  |     int n = sk_OPENSSL_STRING_num(vpm->hosts);  | 
924  | 0  |     char *name;  | 
925  |  | 
  | 
926  | 0  |     if (vpm->peername != NULL) { | 
927  | 0  |         OPENSSL_free(vpm->peername);  | 
928  | 0  |         vpm->peername = NULL;  | 
929  | 0  |     }  | 
930  | 0  |     for (i = 0; i < n; ++i) { | 
931  | 0  |         name = sk_OPENSSL_STRING_value(vpm->hosts, i);  | 
932  | 0  |         if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0)  | 
933  | 0  |             return 1;  | 
934  | 0  |     }  | 
935  | 0  |     return n == 0;  | 
936  | 0  | }  | 
937  |  |  | 
938  |  | static int check_id(X509_STORE_CTX *ctx)  | 
939  | 0  | { | 
940  | 0  |     X509_VERIFY_PARAM *vpm = ctx->param;  | 
941  | 0  |     X509 *x = ctx->cert;  | 
942  |  | 
  | 
943  | 0  |     if (vpm->hosts != NULL && check_hosts(x, vpm) <= 0) { | 
944  | 0  |         if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))  | 
945  | 0  |             return 0;  | 
946  | 0  |     }  | 
947  | 0  |     if (vpm->email != NULL  | 
948  | 0  |             && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) { | 
949  | 0  |         if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))  | 
950  | 0  |             return 0;  | 
951  | 0  |     }  | 
952  | 0  |     if (vpm->ip != NULL && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) { | 
953  | 0  |         if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))  | 
954  | 0  |             return 0;  | 
955  | 0  |     }  | 
956  | 0  |     return 1;  | 
957  | 0  | }  | 
958  |  |  | 
959  |  | /*  | 
960  |  |  * Returns 1 is an ASN1 time is valid for an RFC5280 certificate, 0 otherwise  | 
961  |  |  */  | 
962  |  | static int validate_certifiate_time(const ASN1_TIME *ctm)  | 
963  | 0  | { | 
964  | 0  |     static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1; | 
965  | 0  |     static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1; | 
966  | 0  |     int i;  | 
967  |  | #ifdef CHARSET_EBCDIC  | 
968  |  |     const char upper_z = 0x5A;  | 
969  |  | #else  | 
970  | 0  |     const char upper_z = 'Z';  | 
971  | 0  | #endif  | 
972  |  |  | 
973  |  |     /*-  | 
974  |  |      * Note that ASN.1 allows much more slack in the time format than RFC5280.  | 
975  |  |      * In RFC5280, the representation is fixed:  | 
976  |  |      * UTCTime: YYMMDDHHMMSSZ  | 
977  |  |      * GeneralizedTime: YYYYMMDDHHMMSSZ  | 
978  |  |      *  | 
979  |  |      * We do NOT currently enforce the following RFC 5280 requirement:  | 
980  |  |      * "CAs conforming to this profile MUST always encode certificate  | 
981  |  |      *  validity dates through the year 2049 as UTCTime; certificate validity  | 
982  |  |      *  dates in 2050 or later MUST be encoded as GeneralizedTime."  | 
983  |  |      */  | 
984  | 0  |     switch (ctm->type) { | 
985  | 0  |     case V_ASN1_UTCTIME:  | 
986  | 0  |         if (ctm->length != (int)(utctime_length))  | 
987  | 0  |             return 0;  | 
988  | 0  |         break;  | 
989  | 0  |     case V_ASN1_GENERALIZEDTIME:  | 
990  | 0  |         if (ctm->length != (int)(generalizedtime_length))  | 
991  | 0  |             return 0;  | 
992  | 0  |         break;  | 
993  | 0  |     default:  | 
994  | 0  |         return 0;  | 
995  | 0  |     }  | 
996  |  |  | 
997  |  |     /**  | 
998  |  |      * Verify the format: the ASN.1 functions we use below allow a more  | 
999  |  |      * flexible format than what's mandated by RFC 5280.  | 
1000  |  |      * Digit and date ranges will be verified in the conversion methods.  | 
1001  |  |      */  | 
1002  | 0  |     for (i = 0; i < ctm->length - 1; i++) { | 
1003  | 0  |         if (!ossl_ascii_isdigit(ctm->data[i]))  | 
1004  | 0  |             return 0;  | 
1005  | 0  |     }  | 
1006  | 0  |     if (ctm->data[ctm->length - 1] != upper_z)  | 
1007  | 0  |         return 0;  | 
1008  |  |  | 
1009  | 0  |     return 1;  | 
1010  | 0  | }  | 
1011  |  |  | 
1012  |  | /*  | 
1013  |  |  *  Compare a certificate time to a time_t.  | 
1014  |  |  *  returns 0 if either the certificate time or time_t were invalid on not  | 
1015  |  |  *  representable. Otherwise returns 1 and stores the comparison result  | 
1016  |  |  *  (-1, 0, or 1) in *out_comparison.  | 
1017  |  |  */  | 
1018  |  | static int x509_cmp_time_internal(const ASN1_TIME *ctm, const time_t *cmp_time,  | 
1019  |  |                                   int *out_comparison)  | 
1020  | 0  | { | 
1021  | 0  |     time_t t = cmp_time == NULL ? time(NULL) : *cmp_time;  | 
1022  | 0  |     int comparison;  | 
1023  |  | 
  | 
1024  | 0  |     if (!validate_certifiate_time(ctm))  | 
1025  | 0  |         return 0;  | 
1026  |  |  | 
1027  | 0  |     if ((comparison = ASN1_TIME_cmp_time_t(ctm, t)) == -2)  | 
1028  | 0  |         return 0;  | 
1029  |  |  | 
1030  | 0  |     *out_comparison = comparison;  | 
1031  | 0  |     return 1;  | 
1032  | 0  | }  | 
1033  |  |  | 
1034  |  | /* Returns -1 on internal error */  | 
1035  |  | static int check_trust(X509_STORE_CTX *ctx, int num_untrusted)  | 
1036  | 0  | { | 
1037  | 0  |     int i, res;  | 
1038  | 0  |     X509 *x = NULL;  | 
1039  | 0  |     X509 *mx;  | 
1040  | 0  |     SSL_DANE *dane = ctx->dane;  | 
1041  | 0  |     int num = sk_X509_num(ctx->chain);  | 
1042  | 0  |     int trust;  | 
1043  |  |  | 
1044  |  |     /*  | 
1045  |  |      * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2)  | 
1046  |  |      * match, we're done, otherwise we'll merely record the match depth.  | 
1047  |  |      */  | 
1048  | 0  |     if (DANETLS_HAS_TA(dane) && num_untrusted > 0 && num_untrusted < num) { | 
1049  | 0  |         trust = check_dane_issuer(ctx, num_untrusted);  | 
1050  | 0  |         if (trust != X509_TRUST_UNTRUSTED)  | 
1051  | 0  |             return trust;  | 
1052  | 0  |     }  | 
1053  |  |  | 
1054  |  |     /*  | 
1055  |  |      * Check trusted certificates in chain at depth num_untrusted and up.  | 
1056  |  |      * Note, that depths 0..num_untrusted-1 may also contain trusted  | 
1057  |  |      * certificates, but the caller is expected to have already checked those,  | 
1058  |  |      * and wants to incrementally check just any added since.  | 
1059  |  |      */  | 
1060  | 0  |     for (i = num_untrusted; i < num; i++) { | 
1061  | 0  |         x = sk_X509_value(ctx->chain, i);  | 
1062  | 0  |         trust = X509_check_trust(x, ctx->param->trust, 0);  | 
1063  |  |         /* If explicitly trusted (so not neutral nor rejected) return trusted */  | 
1064  | 0  |         if (trust == X509_TRUST_TRUSTED)  | 
1065  | 0  |             goto trusted;  | 
1066  | 0  |         if (trust == X509_TRUST_REJECTED)  | 
1067  | 0  |             goto rejected;  | 
1068  | 0  |     }  | 
1069  |  |  | 
1070  |  |     /*  | 
1071  |  |      * If we are looking at a trusted certificate, and accept partial chains,  | 
1072  |  |      * the chain is PKIX trusted.  | 
1073  |  |      */  | 
1074  | 0  |     if (num_untrusted < num) { | 
1075  | 0  |         if ((ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) != 0)  | 
1076  | 0  |             goto trusted;  | 
1077  | 0  |         return X509_TRUST_UNTRUSTED;  | 
1078  | 0  |     }  | 
1079  |  |  | 
1080  | 0  |     if (num_untrusted == num  | 
1081  | 0  |             && (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) != 0) { | 
1082  |  |         /*  | 
1083  |  |          * Last-resort call with no new trusted certificates, check the leaf  | 
1084  |  |          * for a direct trust store match.  | 
1085  |  |          */  | 
1086  | 0  |         i = 0;  | 
1087  | 0  |         x = sk_X509_value(ctx->chain, i);  | 
1088  | 0  |         res = lookup_cert_match(&mx, ctx, x);  | 
1089  | 0  |         if (res < 0)  | 
1090  | 0  |             return res;  | 
1091  | 0  |         if (res == 0)  | 
1092  | 0  |             return X509_TRUST_UNTRUSTED;  | 
1093  |  |  | 
1094  |  |         /*  | 
1095  |  |          * Check explicit auxiliary trust/reject settings.  If none are set,  | 
1096  |  |          * we'll accept X509_TRUST_UNTRUSTED when not self-signed.  | 
1097  |  |          */  | 
1098  | 0  |         trust = X509_check_trust(mx, ctx->param->trust, 0);  | 
1099  | 0  |         if (trust == X509_TRUST_REJECTED) { | 
1100  | 0  |             X509_free(mx);  | 
1101  | 0  |             goto rejected;  | 
1102  | 0  |         }  | 
1103  |  |  | 
1104  |  |         /* Replace leaf with trusted match */  | 
1105  | 0  |         (void)sk_X509_set(ctx->chain, 0, mx);  | 
1106  | 0  |         X509_free(x);  | 
1107  | 0  |         ctx->num_untrusted = 0;  | 
1108  | 0  |         goto trusted;  | 
1109  | 0  |     }  | 
1110  |  |  | 
1111  |  |     /*  | 
1112  |  |      * If no trusted certs in chain at all return untrusted and allow  | 
1113  |  |      * standard (no issuer cert) etc errors to be indicated.  | 
1114  |  |      */  | 
1115  | 0  |     return X509_TRUST_UNTRUSTED;  | 
1116  |  |  | 
1117  | 0  |  rejected:  | 
1118  | 0  |     return verify_cb_cert(ctx, x, i, X509_V_ERR_CERT_REJECTED) == 0  | 
1119  | 0  |         ? X509_TRUST_REJECTED : X509_TRUST_UNTRUSTED;  | 
1120  |  |  | 
1121  | 0  |  trusted:  | 
1122  | 0  |     if (!DANETLS_ENABLED(dane))  | 
1123  | 0  |         return X509_TRUST_TRUSTED;  | 
1124  | 0  |     if (dane->pdpth < 0)  | 
1125  | 0  |         dane->pdpth = num_untrusted;  | 
1126  |  |     /* With DANE, PKIX alone is not trusted until we have both */  | 
1127  | 0  |     if (dane->mdpth >= 0)  | 
1128  | 0  |         return X509_TRUST_TRUSTED;  | 
1129  | 0  |     return X509_TRUST_UNTRUSTED;  | 
1130  | 0  | }  | 
1131  |  |  | 
1132  |  | /* Sadly, returns 0 also on internal error. */  | 
1133  |  | static int check_revocation(X509_STORE_CTX *ctx)  | 
1134  | 0  | { | 
1135  | 0  |     int i = 0, last = 0, ok = 0;  | 
1136  | 0  |     int crl_check_enabled =  | 
1137  | 0  |         (ctx->param->flags &  | 
1138  | 0  |          (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) != 0;  | 
1139  | 0  |     int crl_check_all_enabled =  | 
1140  | 0  |         (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) != 0;  | 
1141  | 0  |     int ocsp_check_enabled =  | 
1142  | 0  |         (ctx->param->flags &  | 
1143  | 0  |          (X509_V_FLAG_OCSP_RESP_CHECK | X509_V_FLAG_OCSP_RESP_CHECK_ALL)) != 0;  | 
1144  | 0  |     int ocsp_check_all_enabled =  | 
1145  | 0  |         (ctx->param->flags & X509_V_FLAG_OCSP_RESP_CHECK_ALL) != 0;  | 
1146  |  | 
  | 
1147  | 0  |     if (!crl_check_enabled && !ocsp_check_enabled)  | 
1148  | 0  |         return 1;  | 
1149  |  |  | 
1150  | 0  |     if (ocsp_check_enabled) { | 
1151  | 0  | #ifndef OPENSSL_NO_OCSP  | 
1152  |  |         /*  | 
1153  |  |          * certificate status checking with OCSP  | 
1154  |  |          */  | 
1155  | 0  |         if (ocsp_check_all_enabled)  | 
1156  | 0  |             last = sk_X509_num(ctx->chain) - 1;  | 
1157  | 0  |         else if (!crl_check_all_enabled && ctx->parent != NULL)  | 
1158  | 0  |             return 1; /* If checking CRL paths this isn't the EE certificate */  | 
1159  |  |  | 
1160  | 0  |         for (i = 0; i <= last; i++) { | 
1161  | 0  |             ctx->error_depth = i;  | 
1162  | 0  |             ctx->current_cert = sk_X509_value(ctx->chain, i);  | 
1163  |  |  | 
1164  |  |             /* skip if cert is apparently self-signed */  | 
1165  | 0  |             if (ctx->current_cert->ex_flags & EXFLAG_SS)  | 
1166  | 0  |                 continue;  | 
1167  |  |  | 
1168  |  |             /* the issuer certificate is the next in the chain */  | 
1169  | 0  |             ctx->current_issuer = sk_X509_value(ctx->chain, i + 1);  | 
1170  |  | 
  | 
1171  | 0  |             ok = check_cert_ocsp_resp(ctx);  | 
1172  |  |  | 
1173  |  |             /*  | 
1174  |  |              * In the case the certificate status is REVOKED, the verification  | 
1175  |  |              * can stop here.  | 
1176  |  |              */  | 
1177  | 0  |             if (ok == V_OCSP_CERTSTATUS_REVOKED) { | 
1178  | 0  |                 return verify_cb_ocsp(ctx, ctx->error != 0  | 
1179  | 0  |                                       ? ctx->error  | 
1180  | 0  |                                       : X509_V_ERR_OCSP_VERIFY_FAILED);  | 
1181  | 0  |             }  | 
1182  |  |  | 
1183  |  |             /*  | 
1184  |  |              * In the case the certificate status is GOOD, continue with the next  | 
1185  |  |              * certificate.  | 
1186  |  |              */  | 
1187  | 0  |             if (ok == V_OCSP_CERTSTATUS_GOOD)  | 
1188  | 0  |                 continue;  | 
1189  |  |  | 
1190  |  |             /*  | 
1191  |  |              * As stated in RFC 6961 section 2.2:  | 
1192  |  |              * If OCSP is not enabled or the client receives a "ocsp_response_list"  | 
1193  |  |              * that does not contain a response for one or more of the certificates  | 
1194  |  |              * in the completed certificate chain, the client SHOULD attempt to  | 
1195  |  |              * validate the certificate using an alternative retrieval method,  | 
1196  |  |              * such as downloading the relevant CRL;  | 
1197  |  |              */  | 
1198  | 0  |             if (crl_check_all_enabled || (crl_check_enabled && i == 0)) { | 
1199  | 0  |                 ok = check_cert_crl(ctx);  | 
1200  | 0  |                 if (!ok)  | 
1201  | 0  |                     return ok;  | 
1202  | 0  |             } else { | 
1203  | 0  |                 ok = verify_cb_ocsp(ctx, X509_V_ERR_OCSP_VERIFY_FAILED);  | 
1204  | 0  |                 if (!ok)  | 
1205  | 0  |                     return ok;  | 
1206  | 0  |             }  | 
1207  | 0  |         }  | 
1208  | 0  | #endif  | 
1209  | 0  |     }  | 
1210  |  |  | 
1211  | 0  |     if (crl_check_enabled && !ocsp_check_all_enabled) { | 
1212  |  |         /* certificate status check with CRLs */  | 
1213  | 0  |         if (crl_check_all_enabled) { | 
1214  | 0  |             last = sk_X509_num(ctx->chain) - 1;  | 
1215  | 0  |         } else { | 
1216  |  |             /* If checking CRL paths this isn't the EE certificate */  | 
1217  | 0  |             if (ctx->parent != NULL)  | 
1218  | 0  |                 return 1;  | 
1219  | 0  |             last = 0;  | 
1220  | 0  |         }  | 
1221  |  |  | 
1222  |  |         /*  | 
1223  |  |          * in the case that OCSP is only enabled for the server certificate  | 
1224  |  |          * and CRL for the complete chain, the rest of the chain has to be  | 
1225  |  |          * checked here  | 
1226  |  |          */  | 
1227  | 0  |         if (ocsp_check_enabled && crl_check_all_enabled)  | 
1228  | 0  |             i = 1;  | 
1229  | 0  |         else  | 
1230  | 0  |             i = 0;  | 
1231  | 0  |         for (; i <= last; i++) { | 
1232  | 0  |             ctx->error_depth = i;  | 
1233  | 0  |             ok = check_cert_crl(ctx);  | 
1234  | 0  |             if (!ok)  | 
1235  | 0  |                 return ok;  | 
1236  | 0  |         }  | 
1237  | 0  |     }  | 
1238  |  |  | 
1239  | 0  |     return 1;  | 
1240  | 0  | }  | 
1241  |  |  | 
1242  |  | #ifndef OPENSSL_NO_OCSP  | 
1243  |  | static int check_cert_ocsp_resp(X509_STORE_CTX *ctx)  | 
1244  | 0  | { | 
1245  | 0  |     int cert_status, crl_reason;  | 
1246  | 0  |     int i;  | 
1247  | 0  |     OCSP_RESPONSE *resp = NULL;  | 
1248  | 0  |     OCSP_BASICRESP *bs = NULL;  | 
1249  | 0  |     OCSP_SINGLERESP *sr = NULL;  | 
1250  | 0  |     OCSP_CERTID *sr_cert_id = NULL;  | 
1251  | 0  |     ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;  | 
1252  | 0  |     ASN1_OBJECT *cert_id_md_oid;  | 
1253  | 0  |     EVP_MD *cert_id_md;  | 
1254  | 0  |     OCSP_CERTID *cert_id = NULL;  | 
1255  | 0  |     int ret = V_OCSP_CERTSTATUS_UNKNOWN;  | 
1256  | 0  |     int num;  | 
1257  |  | 
  | 
1258  | 0  |     num = sk_OCSP_RESPONSE_num(ctx->ocsp_resp);  | 
1259  |  | 
  | 
1260  | 0  |     if (num < 0 || num <= ctx->error_depth)  | 
1261  | 0  |         return X509_V_ERR_OCSP_NO_RESPONSE;  | 
1262  |  |  | 
1263  | 0  |     if ((resp = sk_OCSP_RESPONSE_value(ctx->ocsp_resp, ctx->error_depth)) == NULL  | 
1264  | 0  |         || (bs = OCSP_response_get1_basic(resp)) == NULL  | 
1265  | 0  |         || (num = OCSP_resp_count(bs)) < 1)  | 
1266  | 0  |         return X509_V_ERR_OCSP_NO_RESPONSE;  | 
1267  |  |  | 
1268  | 0  |     if (OCSP_response_status(resp) != OCSP_RESPONSE_STATUS_SUCCESSFUL) { | 
1269  | 0  |         OCSP_BASICRESP_free(bs);  | 
1270  | 0  |         ret = X509_V_ERR_OCSP_RESP_INVALID;  | 
1271  | 0  |         goto end;  | 
1272  | 0  |     }  | 
1273  |  |  | 
1274  | 0  |     if (OCSP_basic_verify(bs, ctx->chain, ctx->store, OCSP_TRUSTOTHER) <= 0) { | 
1275  | 0  |         ret = X509_V_ERR_OCSP_SIGNATURE_FAILURE;  | 
1276  | 0  |         goto end;  | 
1277  | 0  |     }  | 
1278  |  |  | 
1279  |  |     /* find the right single response in the OCSP response */  | 
1280  | 0  |     for (i = 0; i < num; i++) { | 
1281  | 0  |         sr = OCSP_resp_get0(bs, i);  | 
1282  |  |  | 
1283  |  |         /* determine the md algorithm which was used to create cert id */  | 
1284  | 0  |         sr_cert_id = (OCSP_CERTID *)OCSP_SINGLERESP_get0_id(sr);  | 
1285  | 0  |         OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, sr_cert_id);  | 
1286  | 0  |         if (cert_id_md_oid != NULL)  | 
1287  | 0  |             cert_id_md = (EVP_MD *)EVP_get_digestbyobj(cert_id_md_oid);  | 
1288  | 0  |         else  | 
1289  | 0  |             cert_id_md = NULL;  | 
1290  |  |  | 
1291  |  |         /* search the stack for the requested OCSP response */  | 
1292  | 0  |         cert_id = OCSP_cert_to_id(cert_id_md, ctx->current_cert, ctx->current_issuer);  | 
1293  | 0  |         if (cert_id == NULL) { | 
1294  | 0  |             ret = X509_V_ERR_OCSP_RESP_INVALID;  | 
1295  | 0  |             goto end;  | 
1296  | 0  |         }  | 
1297  |  |  | 
1298  | 0  |         if (!OCSP_id_cmp(cert_id, sr_cert_id))  | 
1299  | 0  |             break;  | 
1300  |  |  | 
1301  | 0  |         OCSP_CERTID_free(cert_id);  | 
1302  | 0  |         cert_id = NULL;  | 
1303  | 0  |     }  | 
1304  |  |  | 
1305  | 0  |     if (cert_id == NULL) { | 
1306  | 0  |         ret = X509_V_ERR_OCSP_NO_RESPONSE;  | 
1307  | 0  |         goto end;  | 
1308  | 0  |     }  | 
1309  |  |  | 
1310  | 0  |     if (OCSP_resp_find_status(bs, cert_id, &cert_status, &crl_reason, &rev,  | 
1311  | 0  |                               &thisupd, &nextupd) <= 0) { | 
1312  | 0  |         ret = X509_V_ERR_OCSP_RESP_INVALID;  | 
1313  | 0  |         goto end;  | 
1314  | 0  |     }  | 
1315  |  |  | 
1316  | 0  |     if (cert_status == V_OCSP_CERTSTATUS_GOOD) { | 
1317  |  |         /*  | 
1318  |  |          * Note:  | 
1319  |  |          * A OCSP stapling result will be accepted up to 5 minutes  | 
1320  |  |          * after it expired!  | 
1321  |  |          */  | 
1322  | 0  |         if (!OCSP_check_validity(thisupd, nextupd, 300L, -1L))  | 
1323  | 0  |             ret = X509_V_ERR_OCSP_HAS_EXPIRED;  | 
1324  | 0  |         else  | 
1325  | 0  |             ret = V_OCSP_CERTSTATUS_GOOD;  | 
1326  | 0  |     } else { | 
1327  | 0  |         ret = cert_status;  | 
1328  | 0  |     }  | 
1329  |  | 
  | 
1330  | 0  | end:  | 
1331  | 0  |     OCSP_CERTID_free(cert_id);  | 
1332  | 0  |     OCSP_BASICRESP_free(bs);  | 
1333  | 0  |     return ret;  | 
1334  | 0  | }  | 
1335  |  | #endif  | 
1336  |  |  | 
1337  |  | /* Sadly, returns 0 also on internal error. */  | 
1338  |  | static int check_cert_crl(X509_STORE_CTX *ctx)  | 
1339  | 0  | { | 
1340  | 0  |     X509_CRL *crl = NULL, *dcrl = NULL;  | 
1341  | 0  |     int ok = 0;  | 
1342  | 0  |     int cnum = ctx->error_depth;  | 
1343  | 0  |     X509 *x = sk_X509_value(ctx->chain, cnum);  | 
1344  |  | 
  | 
1345  | 0  |     ctx->current_cert = x;  | 
1346  | 0  |     ctx->current_issuer = NULL;  | 
1347  | 0  |     ctx->current_crl_score = 0;  | 
1348  | 0  |     ctx->current_reasons = 0;  | 
1349  |  |  | 
1350  |  |     /* skip if cert is apparently self-signed */  | 
1351  | 0  |     if (ctx->current_cert->ex_flags & EXFLAG_SS)  | 
1352  | 0  |         return 1;  | 
1353  | 0  |     if ((x->ex_flags & EXFLAG_PROXY) != 0)  | 
1354  | 0  |         return 1;  | 
1355  |  |  | 
1356  | 0  |     while (ctx->current_reasons != CRLDP_ALL_REASONS) { | 
1357  | 0  |         unsigned int last_reasons = ctx->current_reasons;  | 
1358  |  |  | 
1359  |  |         /* Try to retrieve relevant CRL */  | 
1360  | 0  |         if (ctx->get_crl != NULL)  | 
1361  | 0  |             ok = ctx->get_crl(ctx, &crl, x);  | 
1362  | 0  |         else  | 
1363  | 0  |             ok = get_crl_delta(ctx, &crl, &dcrl, x);  | 
1364  |  |         /* If error looking up CRL, nothing we can do except notify callback */  | 
1365  | 0  |         if (!ok) { | 
1366  | 0  |             ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL);  | 
1367  | 0  |             goto done;  | 
1368  | 0  |         }  | 
1369  | 0  |         ctx->current_crl = crl;  | 
1370  | 0  |         ok = ctx->check_crl(ctx, crl);  | 
1371  | 0  |         if (!ok)  | 
1372  | 0  |             goto done;  | 
1373  |  |  | 
1374  | 0  |         if (dcrl != NULL) { | 
1375  | 0  |             ok = ctx->check_crl(ctx, dcrl);  | 
1376  | 0  |             if (!ok)  | 
1377  | 0  |                 goto done;  | 
1378  | 0  |             ok = ctx->cert_crl(ctx, dcrl, x);  | 
1379  | 0  |             if (!ok)  | 
1380  | 0  |                 goto done;  | 
1381  | 0  |         } else { | 
1382  | 0  |             ok = 1;  | 
1383  | 0  |         }  | 
1384  |  |  | 
1385  |  |         /* Don't look in full CRL if delta reason is removefromCRL */  | 
1386  | 0  |         if (ok != 2) { | 
1387  | 0  |             ok = ctx->cert_crl(ctx, crl, x);  | 
1388  | 0  |             if (!ok)  | 
1389  | 0  |                 goto done;  | 
1390  | 0  |         }  | 
1391  |  |  | 
1392  | 0  |         X509_CRL_free(crl);  | 
1393  | 0  |         X509_CRL_free(dcrl);  | 
1394  | 0  |         crl = NULL;  | 
1395  | 0  |         dcrl = NULL;  | 
1396  |  |         /*  | 
1397  |  |          * If reasons not updated we won't get anywhere by another iteration,  | 
1398  |  |          * so exit loop.  | 
1399  |  |          */  | 
1400  | 0  |         if (last_reasons == ctx->current_reasons) { | 
1401  | 0  |             ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL);  | 
1402  | 0  |             goto done;  | 
1403  | 0  |         }  | 
1404  | 0  |     }  | 
1405  | 0  |  done:  | 
1406  | 0  |     X509_CRL_free(crl);  | 
1407  | 0  |     X509_CRL_free(dcrl);  | 
1408  |  | 
  | 
1409  | 0  |     ctx->current_crl = NULL;  | 
1410  | 0  |     return ok;  | 
1411  | 0  | }  | 
1412  |  |  | 
1413  |  | /* Check CRL times against values in X509_STORE_CTX */  | 
1414  |  | static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)  | 
1415  | 0  | { | 
1416  | 0  |     time_t *ptime;  | 
1417  | 0  |     int i, comparison;  | 
1418  |  | 
  | 
1419  | 0  |     if ((ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) != 0)  | 
1420  | 0  |         ptime = &ctx->param->check_time;  | 
1421  | 0  |     else if ((ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) != 0)  | 
1422  | 0  |         return 1;  | 
1423  | 0  |     else  | 
1424  | 0  |         ptime = NULL;  | 
1425  | 0  |     if (notify)  | 
1426  | 0  |         ctx->current_crl = crl;  | 
1427  |  | 
  | 
1428  | 0  |     i = x509_cmp_time_internal(X509_CRL_get0_lastUpdate(crl), ptime, &comparison);  | 
1429  | 0  |     if (i == 0) { | 
1430  | 0  |         if (!notify)  | 
1431  | 0  |             return 0;  | 
1432  | 0  |         if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD))  | 
1433  | 0  |             return 0;  | 
1434  | 0  |     }  | 
1435  |  |  | 
1436  | 0  |     if (comparison > 0) { | 
1437  | 0  |         if (!notify)  | 
1438  | 0  |             return 0;  | 
1439  | 0  |         if (!verify_cb_crl(ctx, X509_V_ERR_CRL_NOT_YET_VALID))  | 
1440  | 0  |             return 0;  | 
1441  | 0  |     }  | 
1442  |  |  | 
1443  | 0  |     if (X509_CRL_get0_nextUpdate(crl)) { | 
1444  | 0  |         i = x509_cmp_time_internal(X509_CRL_get0_nextUpdate(crl), ptime, &comparison);  | 
1445  |  | 
  | 
1446  | 0  |         if (i == 0) { | 
1447  | 0  |             if (!notify)  | 
1448  | 0  |                 return 0;  | 
1449  | 0  |             if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD))  | 
1450  | 0  |                 return 0;  | 
1451  | 0  |         }  | 
1452  |  |         /* Ignore expiration of base CRL is delta is valid */  | 
1453  | 0  |         if (comparison < 0 && (ctx->current_crl_score & CRL_SCORE_TIME_DELTA) == 0) { | 
1454  | 0  |             if (!notify || !verify_cb_crl(ctx, X509_V_ERR_CRL_HAS_EXPIRED))  | 
1455  | 0  |                 return 0;  | 
1456  | 0  |         }  | 
1457  | 0  |     }  | 
1458  |  |  | 
1459  | 0  |     if (notify)  | 
1460  | 0  |         ctx->current_crl = NULL;  | 
1461  |  | 
  | 
1462  | 0  |     return 1;  | 
1463  | 0  | }  | 
1464  |  |  | 
1465  |  | static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,  | 
1466  |  |                       X509 **pissuer, int *pscore, unsigned int *preasons,  | 
1467  |  |                       STACK_OF(X509_CRL) *crls)  | 
1468  | 0  | { | 
1469  | 0  |     int i, crl_score, best_score = *pscore;  | 
1470  | 0  |     unsigned int reasons, best_reasons = 0;  | 
1471  | 0  |     X509 *x = ctx->current_cert;  | 
1472  | 0  |     X509_CRL *crl, *best_crl = NULL;  | 
1473  | 0  |     X509 *crl_issuer = NULL, *best_crl_issuer = NULL;  | 
1474  |  | 
  | 
1475  | 0  |     for (i = 0; i < sk_X509_CRL_num(crls); i++) { | 
1476  | 0  |         crl = sk_X509_CRL_value(crls, i);  | 
1477  | 0  |         reasons = *preasons;  | 
1478  | 0  |         crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);  | 
1479  | 0  |         if (crl_score < best_score || crl_score == 0)  | 
1480  | 0  |             continue;  | 
1481  |  |         /* If current CRL is equivalent use it if it is newer */  | 
1482  | 0  |         if (crl_score == best_score && best_crl != NULL) { | 
1483  | 0  |             int day, sec;  | 
1484  |  | 
  | 
1485  | 0  |             if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),  | 
1486  | 0  |                                X509_CRL_get0_lastUpdate(crl)) == 0)  | 
1487  | 0  |                 continue;  | 
1488  |  |             /*  | 
1489  |  |              * ASN1_TIME_diff never returns inconsistent signs for |day|  | 
1490  |  |              * and |sec|.  | 
1491  |  |              */  | 
1492  | 0  |             if (day <= 0 && sec <= 0)  | 
1493  | 0  |                 continue;  | 
1494  | 0  |         }  | 
1495  | 0  |         best_crl = crl;  | 
1496  | 0  |         best_crl_issuer = crl_issuer;  | 
1497  | 0  |         best_score = crl_score;  | 
1498  | 0  |         best_reasons = reasons;  | 
1499  | 0  |     }  | 
1500  |  | 
  | 
1501  | 0  |     if (best_crl != NULL) { | 
1502  | 0  |         if (!X509_CRL_up_ref(best_crl))  | 
1503  | 0  |             return 0;  | 
1504  | 0  |         X509_CRL_free(*pcrl);  | 
1505  | 0  |         *pcrl = best_crl;  | 
1506  | 0  |         *pissuer = best_crl_issuer;  | 
1507  | 0  |         *pscore = best_score;  | 
1508  | 0  |         *preasons = best_reasons;  | 
1509  | 0  |         X509_CRL_free(*pdcrl);  | 
1510  | 0  |         *pdcrl = NULL;  | 
1511  | 0  |         get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);  | 
1512  | 0  |     }  | 
1513  |  |  | 
1514  | 0  |     if (best_score >= CRL_SCORE_VALID)  | 
1515  | 0  |         return 1;  | 
1516  |  |  | 
1517  | 0  |     return 0;  | 
1518  | 0  | }  | 
1519  |  |  | 
1520  |  | /*  | 
1521  |  |  * Compare two CRL extensions for delta checking purposes. They should be  | 
1522  |  |  * both present or both absent. If both present all fields must be identical.  | 
1523  |  |  */  | 
1524  |  | static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)  | 
1525  | 0  | { | 
1526  | 0  |     ASN1_OCTET_STRING *exta = NULL, *extb = NULL;  | 
1527  | 0  |     int i = X509_CRL_get_ext_by_NID(a, nid, -1);  | 
1528  |  | 
  | 
1529  | 0  |     if (i >= 0) { | 
1530  |  |         /* Can't have multiple occurrences */  | 
1531  | 0  |         if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)  | 
1532  | 0  |             return 0;  | 
1533  | 0  |         exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));  | 
1534  | 0  |     }  | 
1535  |  |  | 
1536  | 0  |     i = X509_CRL_get_ext_by_NID(b, nid, -1);  | 
1537  | 0  |     if (i >= 0) { | 
1538  | 0  |         if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)  | 
1539  | 0  |             return 0;  | 
1540  | 0  |         extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));  | 
1541  | 0  |     }  | 
1542  |  |  | 
1543  | 0  |     if (exta == NULL && extb == NULL)  | 
1544  | 0  |         return 1;  | 
1545  |  |  | 
1546  | 0  |     if (exta == NULL || extb == NULL)  | 
1547  | 0  |         return 0;  | 
1548  |  |  | 
1549  | 0  |     return ASN1_OCTET_STRING_cmp(exta, extb) == 0;  | 
1550  | 0  | }  | 
1551  |  |  | 
1552  |  | /* See if a base and delta are compatible */  | 
1553  |  | static int check_delta_base(X509_CRL *delta, X509_CRL *base)  | 
1554  | 0  | { | 
1555  |  |     /* Delta CRL must be a delta */  | 
1556  | 0  |     if (delta->base_crl_number == NULL)  | 
1557  | 0  |         return 0;  | 
1558  |  |     /* Base must have a CRL number */  | 
1559  | 0  |     if (base->crl_number == NULL)  | 
1560  | 0  |         return 0;  | 
1561  |  |     /* Issuer names must match */  | 
1562  | 0  |     if (X509_NAME_cmp(X509_CRL_get_issuer(base),  | 
1563  | 0  |                       X509_CRL_get_issuer(delta)) != 0)  | 
1564  | 0  |         return 0;  | 
1565  |  |     /* AKID and IDP must match */  | 
1566  | 0  |     if (!crl_extension_match(delta, base, NID_authority_key_identifier))  | 
1567  | 0  |         return 0;  | 
1568  | 0  |     if (!crl_extension_match(delta, base, NID_issuing_distribution_point))  | 
1569  | 0  |         return 0;  | 
1570  |  |     /* Delta CRL base number must not exceed Full CRL number. */  | 
1571  | 0  |     if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)  | 
1572  | 0  |         return 0;  | 
1573  |  |     /* Delta CRL number must exceed full CRL number */  | 
1574  | 0  |     return ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0;  | 
1575  | 0  | }  | 
1576  |  |  | 
1577  |  | /*  | 
1578  |  |  * For a given base CRL find a delta... maybe extend to delta scoring or  | 
1579  |  |  * retrieve a chain of deltas...  | 
1580  |  |  */  | 
1581  |  | static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,  | 
1582  |  |                          X509_CRL *base, STACK_OF(X509_CRL) *crls)  | 
1583  | 0  | { | 
1584  | 0  |     X509_CRL *delta;  | 
1585  | 0  |     int i;  | 
1586  |  | 
  | 
1587  | 0  |     if ((ctx->param->flags & X509_V_FLAG_USE_DELTAS) == 0)  | 
1588  | 0  |         return;  | 
1589  | 0  |     if (((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST) == 0)  | 
1590  | 0  |         return;  | 
1591  | 0  |     for (i = 0; i < sk_X509_CRL_num(crls); i++) { | 
1592  | 0  |         delta = sk_X509_CRL_value(crls, i);  | 
1593  | 0  |         if (check_delta_base(delta, base)) { | 
1594  | 0  |             if (!X509_CRL_up_ref(delta)) { | 
1595  | 0  |                 *dcrl = NULL;  | 
1596  | 0  |                 return;  | 
1597  | 0  |             }  | 
1598  |  |  | 
1599  | 0  |             *dcrl = delta;  | 
1600  |  | 
  | 
1601  | 0  |             if (check_crl_time(ctx, delta, 0))  | 
1602  | 0  |                 *pscore |= CRL_SCORE_TIME_DELTA;  | 
1603  |  | 
  | 
1604  | 0  |             return;  | 
1605  | 0  |         }  | 
1606  | 0  |     }  | 
1607  | 0  |     *dcrl = NULL;  | 
1608  | 0  | }  | 
1609  |  |  | 
1610  |  | /*  | 
1611  |  |  * For a given CRL return how suitable it is for the supplied certificate  | 
1612  |  |  * 'x'. The return value is a mask of several criteria. If the issuer is not  | 
1613  |  |  * the certificate issuer this is returned in *pissuer. The reasons mask is  | 
1614  |  |  * also used to determine if the CRL is suitable: if no new reasons the CRL  | 
1615  |  |  * is rejected, otherwise reasons is updated.  | 
1616  |  |  */  | 
1617  |  | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,  | 
1618  |  |                          unsigned int *preasons, X509_CRL *crl, X509 *x)  | 
1619  | 0  | { | 
1620  | 0  |     int crl_score = 0;  | 
1621  | 0  |     unsigned int tmp_reasons = *preasons, crl_reasons;  | 
1622  |  |  | 
1623  |  |     /* First see if we can reject CRL straight away */  | 
1624  |  |  | 
1625  |  |     /* Invalid IDP cannot be processed */  | 
1626  | 0  |     if ((crl->idp_flags & IDP_INVALID) != 0)  | 
1627  | 0  |         return 0;  | 
1628  |  |     /* Reason codes or indirect CRLs need extended CRL support */  | 
1629  | 0  |     if ((ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT) == 0) { | 
1630  | 0  |         if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))  | 
1631  | 0  |             return 0;  | 
1632  | 0  |     } else if ((crl->idp_flags & IDP_REASONS) != 0) { | 
1633  |  |         /* If no new reasons reject */  | 
1634  | 0  |         if ((crl->idp_reasons & ~tmp_reasons) == 0)  | 
1635  | 0  |             return 0;  | 
1636  | 0  |     }  | 
1637  |  |     /* Don't process deltas at this stage */  | 
1638  | 0  |     else if (crl->base_crl_number != NULL)  | 
1639  | 0  |         return 0;  | 
1640  |  |     /* If issuer name doesn't match certificate need indirect CRL */  | 
1641  | 0  |     if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl)) != 0) { | 
1642  | 0  |         if ((crl->idp_flags & IDP_INDIRECT) == 0)  | 
1643  | 0  |             return 0;  | 
1644  | 0  |     } else { | 
1645  | 0  |         crl_score |= CRL_SCORE_ISSUER_NAME;  | 
1646  | 0  |     }  | 
1647  |  |  | 
1648  | 0  |     if ((crl->flags & EXFLAG_CRITICAL) == 0)  | 
1649  | 0  |         crl_score |= CRL_SCORE_NOCRITICAL;  | 
1650  |  |  | 
1651  |  |     /* Check expiration */  | 
1652  | 0  |     if (check_crl_time(ctx, crl, 0))  | 
1653  | 0  |         crl_score |= CRL_SCORE_TIME;  | 
1654  |  |  | 
1655  |  |     /* Check authority key ID and locate certificate issuer */  | 
1656  | 0  |     crl_akid_check(ctx, crl, pissuer, &crl_score);  | 
1657  |  |  | 
1658  |  |     /* If we can't locate certificate issuer at this point forget it */  | 
1659  | 0  |     if ((crl_score & CRL_SCORE_AKID) == 0)  | 
1660  | 0  |         return 0;  | 
1661  |  |  | 
1662  |  |     /* Check cert for matching CRL distribution points */  | 
1663  | 0  |     if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) { | 
1664  |  |         /* If no new reasons reject */  | 
1665  | 0  |         if ((crl_reasons & ~tmp_reasons) == 0)  | 
1666  | 0  |             return 0;  | 
1667  | 0  |         tmp_reasons |= crl_reasons;  | 
1668  | 0  |         crl_score |= CRL_SCORE_SCOPE;  | 
1669  | 0  |     }  | 
1670  |  |  | 
1671  | 0  |     *preasons = tmp_reasons;  | 
1672  |  | 
  | 
1673  | 0  |     return crl_score;  | 
1674  |  | 
  | 
1675  | 0  | }  | 
1676  |  |  | 
1677  |  | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,  | 
1678  |  |                            X509 **pissuer, int *pcrl_score)  | 
1679  | 0  | { | 
1680  | 0  |     X509 *crl_issuer = NULL;  | 
1681  | 0  |     const X509_NAME *cnm = X509_CRL_get_issuer(crl);  | 
1682  | 0  |     int cidx = ctx->error_depth;  | 
1683  | 0  |     int i;  | 
1684  |  | 
  | 
1685  | 0  |     if (cidx != sk_X509_num(ctx->chain) - 1)  | 
1686  | 0  |         cidx++;  | 
1687  |  | 
  | 
1688  | 0  |     crl_issuer = sk_X509_value(ctx->chain, cidx);  | 
1689  |  | 
  | 
1690  | 0  |     if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { | 
1691  | 0  |         if (*pcrl_score & CRL_SCORE_ISSUER_NAME) { | 
1692  | 0  |             *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;  | 
1693  | 0  |             *pissuer = crl_issuer;  | 
1694  | 0  |             return;  | 
1695  | 0  |         }  | 
1696  | 0  |     }  | 
1697  |  |  | 
1698  | 0  |     for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) { | 
1699  | 0  |         crl_issuer = sk_X509_value(ctx->chain, cidx);  | 
1700  | 0  |         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))  | 
1701  | 0  |             continue;  | 
1702  | 0  |         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { | 
1703  | 0  |             *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;  | 
1704  | 0  |             *pissuer = crl_issuer;  | 
1705  | 0  |             return;  | 
1706  | 0  |         }  | 
1707  | 0  |     }  | 
1708  |  |  | 
1709  |  |     /* Anything else needs extended CRL support */  | 
1710  | 0  |     if ((ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT) == 0)  | 
1711  | 0  |         return;  | 
1712  |  |  | 
1713  |  |     /*  | 
1714  |  |      * Otherwise the CRL issuer is not on the path. Look for it in the set of  | 
1715  |  |      * untrusted certificates.  | 
1716  |  |      */  | 
1717  | 0  |     for (i = 0; i < sk_X509_num(ctx->untrusted); i++) { | 
1718  | 0  |         crl_issuer = sk_X509_value(ctx->untrusted, i);  | 
1719  | 0  |         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm) != 0)  | 
1720  | 0  |             continue;  | 
1721  | 0  |         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { | 
1722  | 0  |             *pissuer = crl_issuer;  | 
1723  | 0  |             *pcrl_score |= CRL_SCORE_AKID;  | 
1724  | 0  |             return;  | 
1725  | 0  |         }  | 
1726  | 0  |     }  | 
1727  | 0  | }  | 
1728  |  |  | 
1729  |  | /*  | 
1730  |  |  * Check the path of a CRL issuer certificate. This creates a new  | 
1731  |  |  * X509_STORE_CTX and populates it with most of the parameters from the  | 
1732  |  |  * parent. This could be optimised somewhat since a lot of path checking will  | 
1733  |  |  * be duplicated by the parent, but this will rarely be used in practice.  | 
1734  |  |  */  | 
1735  |  | static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)  | 
1736  | 0  | { | 
1737  | 0  |     X509_STORE_CTX crl_ctx = {0}; | 
1738  | 0  |     int ret;  | 
1739  |  |  | 
1740  |  |     /* Don't allow recursive CRL path validation */  | 
1741  | 0  |     if (ctx->parent != NULL)  | 
1742  | 0  |         return 0;  | 
1743  | 0  |     if (!X509_STORE_CTX_init(&crl_ctx, ctx->store, x, ctx->untrusted))  | 
1744  | 0  |         return -1;  | 
1745  |  |  | 
1746  | 0  |     crl_ctx.crls = ctx->crls;  | 
1747  |  |     /* Copy verify params across */  | 
1748  | 0  |     X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);  | 
1749  |  | 
  | 
1750  | 0  |     crl_ctx.parent = ctx;  | 
1751  | 0  |     crl_ctx.verify_cb = ctx->verify_cb;  | 
1752  |  |  | 
1753  |  |     /* Verify CRL issuer */  | 
1754  | 0  |     ret = X509_verify_cert(&crl_ctx);  | 
1755  | 0  |     if (ret <= 0)  | 
1756  | 0  |         goto err;  | 
1757  |  |  | 
1758  |  |     /* Check chain is acceptable */  | 
1759  | 0  |     ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);  | 
1760  | 0  |  err:  | 
1761  | 0  |     X509_STORE_CTX_cleanup(&crl_ctx);  | 
1762  | 0  |     return ret;  | 
1763  | 0  | }  | 
1764  |  |  | 
1765  |  | /*  | 
1766  |  |  * RFC3280 says nothing about the relationship between CRL path and  | 
1767  |  |  * certificate path, which could lead to situations where a certificate could  | 
1768  |  |  * be revoked or validated by a CA not authorized to do so. RFC5280 is more  | 
1769  |  |  * strict and states that the two paths must end in the same trust anchor,  | 
1770  |  |  * though some discussions remain... until this is resolved we use the  | 
1771  |  |  * RFC5280 version  | 
1772  |  |  */  | 
1773  |  | static int check_crl_chain(X509_STORE_CTX *ctx,  | 
1774  |  |                            STACK_OF(X509) *cert_path,  | 
1775  |  |                            STACK_OF(X509) *crl_path)  | 
1776  | 0  | { | 
1777  | 0  |     X509 *cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);  | 
1778  | 0  |     X509 *crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);  | 
1779  |  | 
  | 
1780  | 0  |     return X509_cmp(cert_ta, crl_ta) == 0;  | 
1781  | 0  | }  | 
1782  |  |  | 
1783  |  | /*-  | 
1784  |  |  * Check for match between two dist point names: three separate cases.  | 
1785  |  |  * 1. Both are relative names and compare X509_NAME types.  | 
1786  |  |  * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.  | 
1787  |  |  * 3. Both are full names and compare two GENERAL_NAMES.  | 
1788  |  |  * 4. One is NULL: automatic match.  | 
1789  |  |  */  | 
1790  |  | static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)  | 
1791  | 0  | { | 
1792  | 0  |     X509_NAME *nm = NULL;  | 
1793  | 0  |     GENERAL_NAMES *gens = NULL;  | 
1794  | 0  |     GENERAL_NAME *gena, *genb;  | 
1795  | 0  |     int i, j;  | 
1796  |  | 
  | 
1797  | 0  |     if (a == NULL || b == NULL)  | 
1798  | 0  |         return 1;  | 
1799  | 0  |     if (a->type == 1) { | 
1800  | 0  |         if (a->dpname == NULL)  | 
1801  | 0  |             return 0;  | 
1802  |  |         /* Case 1: two X509_NAME */  | 
1803  | 0  |         if (b->type == 1) { | 
1804  | 0  |             if (b->dpname == NULL)  | 
1805  | 0  |                 return 0;  | 
1806  | 0  |             return X509_NAME_cmp(a->dpname, b->dpname) == 0;  | 
1807  | 0  |         }  | 
1808  |  |         /* Case 2: set name and GENERAL_NAMES appropriately */  | 
1809  | 0  |         nm = a->dpname;  | 
1810  | 0  |         gens = b->name.fullname;  | 
1811  | 0  |     } else if (b->type == 1) { | 
1812  | 0  |         if (b->dpname == NULL)  | 
1813  | 0  |             return 0;  | 
1814  |  |         /* Case 2: set name and GENERAL_NAMES appropriately */  | 
1815  | 0  |         gens = a->name.fullname;  | 
1816  | 0  |         nm = b->dpname;  | 
1817  | 0  |     }  | 
1818  |  |  | 
1819  |  |     /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */  | 
1820  | 0  |     if (nm != NULL) { | 
1821  | 0  |         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | 
1822  | 0  |             gena = sk_GENERAL_NAME_value(gens, i);  | 
1823  | 0  |             if (gena->type != GEN_DIRNAME)  | 
1824  | 0  |                 continue;  | 
1825  | 0  |             if (X509_NAME_cmp(nm, gena->d.directoryName) == 0)  | 
1826  | 0  |                 return 1;  | 
1827  | 0  |         }  | 
1828  | 0  |         return 0;  | 
1829  | 0  |     }  | 
1830  |  |  | 
1831  |  |     /* Else case 3: two GENERAL_NAMES */  | 
1832  |  |  | 
1833  | 0  |     for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) { | 
1834  | 0  |         gena = sk_GENERAL_NAME_value(a->name.fullname, i);  | 
1835  | 0  |         for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) { | 
1836  | 0  |             genb = sk_GENERAL_NAME_value(b->name.fullname, j);  | 
1837  | 0  |             if (GENERAL_NAME_cmp(gena, genb) == 0)  | 
1838  | 0  |                 return 1;  | 
1839  | 0  |         }  | 
1840  | 0  |     }  | 
1841  |  |  | 
1842  | 0  |     return 0;  | 
1843  |  | 
  | 
1844  | 0  | }  | 
1845  |  |  | 
1846  |  | static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)  | 
1847  | 0  | { | 
1848  | 0  |     int i;  | 
1849  | 0  |     const X509_NAME *nm = X509_CRL_get_issuer(crl);  | 
1850  |  |  | 
1851  |  |     /* If no CRLissuer return is successful iff don't need a match */  | 
1852  | 0  |     if (dp->CRLissuer == NULL)  | 
1853  | 0  |         return (crl_score & CRL_SCORE_ISSUER_NAME) != 0;  | 
1854  | 0  |     for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { | 
1855  | 0  |         GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);  | 
1856  |  | 
  | 
1857  | 0  |         if (gen->type != GEN_DIRNAME)  | 
1858  | 0  |             continue;  | 
1859  | 0  |         if (X509_NAME_cmp(gen->d.directoryName, nm) == 0)  | 
1860  | 0  |             return 1;  | 
1861  | 0  |     }  | 
1862  | 0  |     return 0;  | 
1863  | 0  | }  | 
1864  |  |  | 
1865  |  | /* Check CRLDP and IDP */  | 
1866  |  | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,  | 
1867  |  |                            unsigned int *preasons)  | 
1868  | 0  | { | 
1869  | 0  |     int i;  | 
1870  |  | 
  | 
1871  | 0  |     if ((crl->idp_flags & IDP_ONLYATTR) != 0)  | 
1872  | 0  |         return 0;  | 
1873  | 0  |     if ((x->ex_flags & EXFLAG_CA) != 0) { | 
1874  | 0  |         if ((crl->idp_flags & IDP_ONLYUSER) != 0)  | 
1875  | 0  |             return 0;  | 
1876  | 0  |     } else { | 
1877  | 0  |         if ((crl->idp_flags & IDP_ONLYCA) != 0)  | 
1878  | 0  |             return 0;  | 
1879  | 0  |     }  | 
1880  | 0  |     *preasons = crl->idp_reasons;  | 
1881  | 0  |     for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { | 
1882  | 0  |         DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);  | 
1883  |  | 
  | 
1884  | 0  |         if (crldp_check_crlissuer(dp, crl, crl_score)) { | 
1885  | 0  |             if (crl->idp == NULL  | 
1886  | 0  |                     || idp_check_dp(dp->distpoint, crl->idp->distpoint)) { | 
1887  | 0  |                 *preasons &= dp->dp_reasons;  | 
1888  | 0  |                 return 1;  | 
1889  | 0  |             }  | 
1890  | 0  |         }  | 
1891  | 0  |     }  | 
1892  | 0  |     return (crl->idp == NULL || crl->idp->distpoint == NULL)  | 
1893  | 0  |             && (crl_score & CRL_SCORE_ISSUER_NAME) != 0;  | 
1894  | 0  | }  | 
1895  |  |  | 
1896  |  | /*  | 
1897  |  |  * Retrieve CRL corresponding to current certificate. If deltas enabled try  | 
1898  |  |  * to find a delta CRL too  | 
1899  |  |  */  | 
1900  |  | static int get_crl_delta(X509_STORE_CTX *ctx,  | 
1901  |  |                          X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)  | 
1902  | 0  | { | 
1903  | 0  |     int ok;  | 
1904  | 0  |     X509 *issuer = NULL;  | 
1905  | 0  |     int crl_score = 0;  | 
1906  | 0  |     unsigned int reasons;  | 
1907  | 0  |     X509_CRL *crl = NULL, *dcrl = NULL;  | 
1908  | 0  |     STACK_OF(X509_CRL) *skcrl;  | 
1909  | 0  |     const X509_NAME *nm = X509_get_issuer_name(x);  | 
1910  |  | 
  | 
1911  | 0  |     reasons = ctx->current_reasons;  | 
1912  | 0  |     ok = get_crl_sk(ctx, &crl, &dcrl,  | 
1913  | 0  |                     &issuer, &crl_score, &reasons, ctx->crls);  | 
1914  | 0  |     if (ok)  | 
1915  | 0  |         goto done;  | 
1916  |  |  | 
1917  |  |     /* Lookup CRLs from store */  | 
1918  | 0  |     skcrl = ctx->lookup_crls(ctx, nm);  | 
1919  |  |  | 
1920  |  |     /* If no CRLs found and a near match from get_crl_sk use that */  | 
1921  | 0  |     if (skcrl == NULL && crl != NULL)  | 
1922  | 0  |         goto done;  | 
1923  |  |  | 
1924  | 0  |     get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);  | 
1925  |  | 
  | 
1926  | 0  |     sk_X509_CRL_pop_free(skcrl, X509_CRL_free);  | 
1927  |  | 
  | 
1928  | 0  | done:  | 
1929  |  |     /* If we got any kind of CRL use it and return success */  | 
1930  | 0  |     if (crl != NULL) { | 
1931  | 0  |         ctx->current_issuer = issuer;  | 
1932  | 0  |         ctx->current_crl_score = crl_score;  | 
1933  | 0  |         ctx->current_reasons = reasons;  | 
1934  | 0  |         *pcrl = crl;  | 
1935  | 0  |         *pdcrl = dcrl;  | 
1936  | 0  |         return 1;  | 
1937  | 0  |     }  | 
1938  | 0  |     return 0;  | 
1939  | 0  | }  | 
1940  |  |  | 
1941  |  | /* Check CRL validity */  | 
1942  |  | static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)  | 
1943  | 0  | { | 
1944  | 0  |     X509 *issuer = NULL;  | 
1945  | 0  |     EVP_PKEY *ikey = NULL;  | 
1946  | 0  |     int cnum = ctx->error_depth;  | 
1947  | 0  |     int chnum = sk_X509_num(ctx->chain) - 1;  | 
1948  |  |  | 
1949  |  |     /* If we have an alternative CRL issuer cert use that */  | 
1950  | 0  |     if (ctx->current_issuer != NULL) { | 
1951  | 0  |         issuer = ctx->current_issuer;  | 
1952  |  |     /*  | 
1953  |  |      * Else find CRL issuer: if not last certificate then issuer is next  | 
1954  |  |      * certificate in chain.  | 
1955  |  |      */  | 
1956  | 0  |     } else if (cnum < chnum) { | 
1957  | 0  |         issuer = sk_X509_value(ctx->chain, cnum + 1);  | 
1958  | 0  |     } else { | 
1959  | 0  |         issuer = sk_X509_value(ctx->chain, chnum);  | 
1960  | 0  |         if (!ossl_assert(issuer != NULL))  | 
1961  | 0  |             return 0;  | 
1962  |  |         /* If not self-issued, can't check signature */  | 
1963  | 0  |         if (!ctx->check_issued(ctx, issuer, issuer) &&  | 
1964  | 0  |             !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER))  | 
1965  | 0  |             return 0;  | 
1966  | 0  |     }  | 
1967  |  |  | 
1968  | 0  |     if (issuer == NULL)  | 
1969  | 0  |         return 1;  | 
1970  |  |  | 
1971  |  |     /*  | 
1972  |  |      * Skip most tests for deltas because they have already been done  | 
1973  |  |      */  | 
1974  | 0  |     if (crl->base_crl_number == NULL) { | 
1975  |  |         /* Check for cRLSign bit if keyUsage present */  | 
1976  | 0  |         if ((issuer->ex_flags & EXFLAG_KUSAGE) != 0 &&  | 
1977  | 0  |             (issuer->ex_kusage & KU_CRL_SIGN) == 0 &&  | 
1978  | 0  |             !verify_cb_crl(ctx, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN))  | 
1979  | 0  |             return 0;  | 
1980  |  |  | 
1981  | 0  |         if ((ctx->current_crl_score & CRL_SCORE_SCOPE) == 0 &&  | 
1982  | 0  |             !verify_cb_crl(ctx, X509_V_ERR_DIFFERENT_CRL_SCOPE))  | 
1983  | 0  |             return 0;  | 
1984  |  |  | 
1985  | 0  |         if ((ctx->current_crl_score & CRL_SCORE_SAME_PATH) == 0 &&  | 
1986  | 0  |             check_crl_path(ctx, ctx->current_issuer) <= 0 &&  | 
1987  | 0  |             !verify_cb_crl(ctx, X509_V_ERR_CRL_PATH_VALIDATION_ERROR))  | 
1988  | 0  |             return 0;  | 
1989  |  |  | 
1990  | 0  |         if ((crl->idp_flags & IDP_INVALID) != 0 &&  | 
1991  | 0  |             !verify_cb_crl(ctx, X509_V_ERR_INVALID_EXTENSION))  | 
1992  | 0  |             return 0;  | 
1993  | 0  |     }  | 
1994  |  |  | 
1995  | 0  |     if ((ctx->current_crl_score & CRL_SCORE_TIME) == 0 &&  | 
1996  | 0  |         !check_crl_time(ctx, crl, 1))  | 
1997  | 0  |         return 0;  | 
1998  |  |  | 
1999  |  |     /* Attempt to get issuer certificate public key */  | 
2000  | 0  |     ikey = X509_get0_pubkey(issuer);  | 
2001  | 0  |     if (ikey == NULL &&  | 
2002  | 0  |         !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY))  | 
2003  | 0  |         return 0;  | 
2004  |  |  | 
2005  | 0  |     if (ikey != NULL) { | 
2006  | 0  |         int rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);  | 
2007  |  | 
  | 
2008  | 0  |         if (rv != X509_V_OK && !verify_cb_crl(ctx, rv))  | 
2009  | 0  |             return 0;  | 
2010  |  |         /* Verify CRL signature */  | 
2011  | 0  |         if (X509_CRL_verify(crl, ikey) <= 0 &&  | 
2012  | 0  |             !verify_cb_crl(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE))  | 
2013  | 0  |             return 0;  | 
2014  | 0  |     }  | 
2015  | 0  |     return 1;  | 
2016  | 0  | }  | 
2017  |  |  | 
2018  |  | /* Check certificate against CRL */  | 
2019  |  | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)  | 
2020  | 0  | { | 
2021  | 0  |     X509_REVOKED *rev;  | 
2022  |  |  | 
2023  |  |     /*  | 
2024  |  |      * The rules changed for this... previously if a CRL contained unhandled  | 
2025  |  |      * critical extensions it could still be used to indicate a certificate  | 
2026  |  |      * was revoked. This has since been changed since critical extensions can  | 
2027  |  |      * change the meaning of CRL entries.  | 
2028  |  |      */  | 
2029  | 0  |     if ((ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) == 0  | 
2030  | 0  |         && (crl->flags & EXFLAG_CRITICAL) != 0 &&  | 
2031  | 0  |         !verify_cb_crl(ctx, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION))  | 
2032  | 0  |         return 0;  | 
2033  |  |     /*  | 
2034  |  |      * Look for serial number of certificate in CRL.  If found, make sure  | 
2035  |  |      * reason is not removeFromCRL.  | 
2036  |  |      */  | 
2037  | 0  |     if (X509_CRL_get0_by_cert(crl, &rev, x)) { | 
2038  | 0  |         if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)  | 
2039  | 0  |             return 2;  | 
2040  | 0  |         if (!verify_cb_crl(ctx, X509_V_ERR_CERT_REVOKED))  | 
2041  | 0  |             return 0;  | 
2042  | 0  |     }  | 
2043  |  |  | 
2044  | 0  |     return 1;  | 
2045  | 0  | }  | 
2046  |  |  | 
2047  |  | /* Sadly, returns 0 also on internal error in ctx->verify_cb(). */  | 
2048  |  | static int check_policy(X509_STORE_CTX *ctx)  | 
2049  | 0  | { | 
2050  | 0  |     int ret;  | 
2051  |  | 
  | 
2052  | 0  |     if (ctx->parent)  | 
2053  | 0  |         return 1;  | 
2054  |  |     /*  | 
2055  |  |      * With DANE, the trust anchor might be a bare public key, not a  | 
2056  |  |      * certificate!  In that case our chain does not have the trust anchor  | 
2057  |  |      * certificate as a top-most element.  This comports well with RFC5280  | 
2058  |  |      * chain verification, since there too, the trust anchor is not part of the  | 
2059  |  |      * chain to be verified.  In particular, X509_policy_check() does not look  | 
2060  |  |      * at the TA cert, but assumes that it is present as the top-most chain  | 
2061  |  |      * element.  We therefore temporarily push a NULL cert onto the chain if it  | 
2062  |  |      * was verified via a bare public key, and pop it off right after the  | 
2063  |  |      * X509_policy_check() call.  | 
2064  |  |      */  | 
2065  | 0  |     if (ctx->bare_ta_signed && !sk_X509_push(ctx->chain, NULL)) { | 
2066  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);  | 
2067  | 0  |         goto memerr;  | 
2068  | 0  |     }  | 
2069  | 0  |     ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,  | 
2070  | 0  |                             ctx->param->policies, ctx->param->flags);  | 
2071  | 0  |     if (ctx->bare_ta_signed)  | 
2072  | 0  |         (void)sk_X509_pop(ctx->chain);  | 
2073  |  | 
  | 
2074  | 0  |     if (ret == X509_PCY_TREE_INTERNAL) { | 
2075  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2076  | 0  |         goto memerr;  | 
2077  | 0  |     }  | 
2078  |  |     /* Invalid or inconsistent extensions */  | 
2079  | 0  |     if (ret == X509_PCY_TREE_INVALID) { | 
2080  | 0  |         int i, cbcalled = 0;  | 
2081  |  |  | 
2082  |  |         /* Locate certificates with bad extensions and notify callback. */  | 
2083  | 0  |         for (i = 0; i < sk_X509_num(ctx->chain); i++) { | 
2084  | 0  |             X509 *x = sk_X509_value(ctx->chain, i);  | 
2085  |  | 
  | 
2086  | 0  |             if ((x->ex_flags & EXFLAG_INVALID_POLICY) != 0)  | 
2087  | 0  |                 cbcalled = 1;  | 
2088  | 0  |             CB_FAIL_IF((x->ex_flags & EXFLAG_INVALID_POLICY) != 0,  | 
2089  | 0  |                        ctx, x, i, X509_V_ERR_INVALID_POLICY_EXTENSION);  | 
2090  | 0  |         }  | 
2091  | 0  |         if (!cbcalled) { | 
2092  |  |             /* Should not be able to get here */  | 
2093  | 0  |             ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);  | 
2094  | 0  |             return 0;  | 
2095  | 0  |         }  | 
2096  |  |         /* The callback ignored the error so we return success */  | 
2097  | 0  |         return 1;  | 
2098  | 0  |     }  | 
2099  | 0  |     if (ret == X509_PCY_TREE_FAILURE) { | 
2100  | 0  |         ctx->current_cert = NULL;  | 
2101  | 0  |         ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;  | 
2102  | 0  |         return ctx->verify_cb(0, ctx);  | 
2103  | 0  |     }  | 
2104  | 0  |     if (ret != X509_PCY_TREE_VALID) { | 
2105  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);  | 
2106  | 0  |         return 0;  | 
2107  | 0  |     }  | 
2108  |  |  | 
2109  | 0  |     if ((ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) != 0) { | 
2110  | 0  |         ctx->current_cert = NULL;  | 
2111  |  |         /*  | 
2112  |  |          * Verification errors need to be "sticky", a callback may have allowed  | 
2113  |  |          * an SSL handshake to continue despite an error, and we must then  | 
2114  |  |          * remain in an error state.  Therefore, we MUST NOT clear earlier  | 
2115  |  |          * verification errors by setting the error to X509_V_OK.  | 
2116  |  |          */  | 
2117  | 0  |         if (!ctx->verify_cb(2, ctx))  | 
2118  | 0  |             return 0;  | 
2119  | 0  |     }  | 
2120  |  |  | 
2121  | 0  |     return 1;  | 
2122  |  |  | 
2123  | 0  |  memerr:  | 
2124  | 0  |     ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
2125  | 0  |     return -1;  | 
2126  | 0  | }  | 
2127  |  |  | 
2128  |  | /*-  | 
2129  |  |  * Check an ASN1_time against X509 verify parameter time.  | 
2130  |  |  *  | 
2131  |  |  * Return 1 on success, 0 otherwise.  | 
2132  |  |  */  | 
2133  |  | int ossl_x509_compare_asn1_time(const X509_VERIFY_PARAM *vpm,  | 
2134  |  |                                 const ASN1_TIME *asn1_time, int *comparison)  | 
2135  | 0  | { | 
2136  | 0  |     const time_t now = time(NULL);  | 
2137  | 0  |     const time_t *check_time = NULL;  | 
2138  |  | 
  | 
2139  | 0  |     if (vpm == NULL) { | 
2140  | 0  |         check_time = &now;  | 
2141  | 0  |     } else if ((vpm->flags & X509_V_FLAG_USE_CHECK_TIME) != 0) { | 
2142  | 0  |         check_time = &vpm->check_time;  | 
2143  | 0  |     } else if ((vpm->flags & X509_V_FLAG_NO_CHECK_TIME) != 0) { | 
2144  | 0  |         *comparison = 0;  | 
2145  | 0  |         return 1;  | 
2146  | 0  |     }  | 
2147  | 0  |     return x509_cmp_time_internal(asn1_time, check_time, comparison);  | 
2148  | 0  | }  | 
2149  |  |  | 
2150  |  | /*-  | 
2151  |  |  * Check certificate validity times.  | 
2152  |  |  *  | 
2153  |  |  * Return 1 on success, 0 otherwise.  | 
2154  |  |  */  | 
2155  |  | int ossl_x509_check_certificate_times(const X509_VERIFY_PARAM *vpm, X509 *x,  | 
2156  |  |                                       int *error)  | 
2157  | 0  | { | 
2158  | 0  |     int err = 0, ret = 0;  | 
2159  | 0  |     int comparison;  | 
2160  | 0  |     const ASN1_TIME *notafter;  | 
2161  |  | 
  | 
2162  | 0  |     if (!ossl_x509_compare_asn1_time(vpm, X509_get0_notBefore(x), &comparison)) { | 
2163  | 0  |         err = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;  | 
2164  | 0  |         goto done;  | 
2165  | 0  |     }  | 
2166  | 0  |     if (comparison > 0) { | 
2167  | 0  |         err = X509_V_ERR_CERT_NOT_YET_VALID;  | 
2168  | 0  |         goto done;  | 
2169  | 0  |     }  | 
2170  |  |     /*  | 
2171  |  |      * RFC 5280 4.1.2.5:  | 
2172  |  |      * To indicate that a certificate has no well-defined expiration date,  | 
2173  |  |      * the notAfter SHOULD be assigned the GeneralizedTime value of  | 
2174  |  |      * 99991231235959Z.  | 
2175  |  |      */  | 
2176  | 0  |     notafter = X509_get0_notAfter(x);  | 
2177  | 0  |     if (notafter->length == 15  | 
2178  | 0  |         && memcmp(ASN1_STRING_get0_data(notafter), "99991231235959Z", 15) == 0)  | 
2179  | 0  |         return 1;  | 
2180  |  |  | 
2181  | 0  |     if (!ossl_x509_compare_asn1_time(vpm, notafter, &comparison)) { | 
2182  | 0  |         err = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;  | 
2183  | 0  |         goto done;  | 
2184  | 0  |     }  | 
2185  | 0  |     if (comparison < 0) { | 
2186  | 0  |         err = X509_V_ERR_CERT_HAS_EXPIRED;  | 
2187  | 0  |         goto done;  | 
2188  | 0  |     }  | 
2189  |  |  | 
2190  | 0  |     ret = 1;  | 
2191  |  | 
  | 
2192  | 0  | done:  | 
2193  | 0  |     if (error != NULL)  | 
2194  | 0  |         *error = err;  | 
2195  |  | 
  | 
2196  | 0  |     return ret;  | 
2197  | 0  | }  | 
2198  |  |  | 
2199  |  | /*-  | 
2200  |  |  * Check certificate validity times.  | 
2201  |  |  * If depth >= 0, invoke verification callbacks on error, otherwise just return  | 
2202  |  |  * the validation status.  | 
2203  |  |  *  | 
2204  |  |  * Return 1 on success, 0 otherwise.  | 
2205  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
2206  |  |  */  | 
2207  |  | int ossl_x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth)  | 
2208  | 0  | { | 
2209  | 0  |     const X509_VERIFY_PARAM *vpm = ctx->param;  | 
2210  | 0  |     int i, comparison;  | 
2211  | 0  |     const ASN1_TIME *notafter;  | 
2212  |  | 
  | 
2213  | 0  |     i = ossl_x509_compare_asn1_time(vpm, X509_get0_notBefore(x), &comparison);  | 
2214  | 0  |     if (i == 0 && depth < 0)  | 
2215  | 0  |         return 0;  | 
2216  | 0  |     if (comparison > 0 && depth < 0)  | 
2217  | 0  |         return 0;  | 
2218  | 0  |     CB_FAIL_IF(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD);  | 
2219  | 0  |     CB_FAIL_IF(comparison > 0, ctx, x, depth, X509_V_ERR_CERT_NOT_YET_VALID);  | 
2220  |  |  | 
2221  |  |     /*  | 
2222  |  |      * RFC 5280 4.1.2.5:  | 
2223  |  |      * To indicate that a certificate has no well-defined expiration date,  | 
2224  |  |      * the notAfter SHOULD be assigned the GeneralizedTime value of  | 
2225  |  |      * 99991231235959Z.  | 
2226  |  |      */  | 
2227  | 0  |     notafter = X509_get0_notAfter(x);  | 
2228  | 0  |     if (notafter->length == 15  | 
2229  | 0  |         && memcmp(ASN1_STRING_get0_data(notafter), "99991231235959Z", 15) == 0)  | 
2230  | 0  |         return 1;  | 
2231  |  |  | 
2232  | 0  |     i = ossl_x509_compare_asn1_time(vpm, notafter, &comparison);  | 
2233  | 0  |     if (i == 0 && depth < 0)  | 
2234  | 0  |         return 0;  | 
2235  | 0  |     if (comparison < 0 && depth < 0)  | 
2236  | 0  |         return 0;  | 
2237  | 0  |     CB_FAIL_IF(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD);  | 
2238  | 0  |     CB_FAIL_IF(comparison < 0, ctx, x, depth, X509_V_ERR_CERT_HAS_EXPIRED);  | 
2239  |  |  | 
2240  | 0  |     return 1;  | 
2241  | 0  | }  | 
2242  |  |  | 
2243  |  | /*  | 
2244  |  |  * Verify the issuer signatures and cert times of ctx->chain.  | 
2245  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
2246  |  |  */  | 
2247  |  | static int internal_verify(X509_STORE_CTX *ctx)  | 
2248  | 0  | { | 
2249  | 0  |     int n;  | 
2250  | 0  |     X509 *xi;  | 
2251  | 0  |     X509 *xs;  | 
2252  |  |  | 
2253  |  |     /* For RPK: just do the verify callback */  | 
2254  | 0  |     if (ctx->rpk != NULL) { | 
2255  | 0  |         if (!ctx->verify_cb(ctx->error == X509_V_OK, ctx))  | 
2256  | 0  |             return 0;  | 
2257  | 0  |         return 1;  | 
2258  | 0  |     }  | 
2259  | 0  |     n = sk_X509_num(ctx->chain) - 1;  | 
2260  | 0  |     xi = sk_X509_value(ctx->chain, n);  | 
2261  | 0  |     xs = xi;  | 
2262  |  | 
  | 
2263  | 0  |     ctx->error_depth = n;  | 
2264  | 0  |     if (ctx->bare_ta_signed) { | 
2265  |  |         /*  | 
2266  |  |          * With DANE-verified bare public key TA signatures,  | 
2267  |  |          * on the top certificate we check only the timestamps.  | 
2268  |  |          * We report the issuer as NULL because all we have is a bare key.  | 
2269  |  |          */  | 
2270  | 0  |         xi = NULL;  | 
2271  | 0  |     } else if (ossl_x509_likely_issued(xi, xi) != X509_V_OK  | 
2272  |  |                /* exceptional case: last cert in the chain is not self-issued */  | 
2273  | 0  |                && ((ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) == 0)) { | 
2274  | 0  |         if (n > 0) { | 
2275  | 0  |             n--;  | 
2276  | 0  |             ctx->error_depth = n;  | 
2277  | 0  |             xs = sk_X509_value(ctx->chain, n);  | 
2278  | 0  |         } else { | 
2279  | 0  |             CB_FAIL_IF(1, ctx, xi, 0,  | 
2280  | 0  |                        X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);  | 
2281  | 0  |         }  | 
2282  |  |         /*  | 
2283  |  |          * The below code will certainly not do a  | 
2284  |  |          * self-signature check on xi because it is not self-issued.  | 
2285  |  |          */  | 
2286  | 0  |     }  | 
2287  |  |  | 
2288  |  |     /*  | 
2289  |  |      * Do not clear error (by ctx->error = X509_V_OK), it must be "sticky",  | 
2290  |  |      * only the user's callback is allowed to reset errors (at its own peril).  | 
2291  |  |      */  | 
2292  | 0  |     while (n >= 0) { | 
2293  |  |         /*-  | 
2294  |  |          * For each iteration of this loop:  | 
2295  |  |          * n is the subject depth  | 
2296  |  |          * xs is the subject cert, for which the signature is to be checked  | 
2297  |  |          * xi is NULL for DANE-verified bare public key TA signatures  | 
2298  |  |          *       else the supposed issuer cert containing the public key to use  | 
2299  |  |          * Initially xs == xi if the last cert in the chain is self-issued.  | 
2300  |  |          */  | 
2301  |  |         /*  | 
2302  |  |          * Do signature check for self-signed certificates only if explicitly  | 
2303  |  |          * asked for because it does not add any security and just wastes time.  | 
2304  |  |          */  | 
2305  | 0  |         if (xi != NULL  | 
2306  | 0  |             && (xs != xi  | 
2307  | 0  |                 || ((ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE) != 0  | 
2308  | 0  |                     && (xi->ex_flags & EXFLAG_SS) != 0))) { | 
2309  | 0  |             EVP_PKEY *pkey;  | 
2310  |  |             /*  | 
2311  |  |              * If the issuer's public key is not available or its key usage  | 
2312  |  |              * does not support issuing the subject cert, report the issuer  | 
2313  |  |              * cert and its depth (rather than n, the depth of the subject).  | 
2314  |  |              */  | 
2315  | 0  |             int issuer_depth = n + (xs == xi ? 0 : 1);  | 
2316  |  |             /*  | 
2317  |  |              * According to https://tools.ietf.org/html/rfc5280#section-6.1.4  | 
2318  |  |              * step (n) we must check any given key usage extension in a CA cert  | 
2319  |  |              * when preparing the verification of a certificate issued by it.  | 
2320  |  |              * According to https://tools.ietf.org/html/rfc5280#section-4.2.1.3  | 
2321  |  |              * we must not verify a certificate signature if the key usage of  | 
2322  |  |              * the CA certificate that issued the certificate prohibits signing.  | 
2323  |  |              * In case the 'issuing' certificate is the last in the chain and is  | 
2324  |  |              * not a CA certificate but a 'self-issued' end-entity cert (i.e.,  | 
2325  |  |              * xs == xi && !(xi->ex_flags & EXFLAG_CA)) RFC 5280 does not apply  | 
2326  |  |              * (see https://tools.ietf.org/html/rfc6818#section-2) and thus  | 
2327  |  |              * we are free to ignore any key usage restrictions on such certs.  | 
2328  |  |              */  | 
2329  | 0  |             int ret = xs == xi && (xi->ex_flags & EXFLAG_CA) == 0  | 
2330  | 0  |                 ? X509_V_OK : ossl_x509_signing_allowed(xi, xs);  | 
2331  |  | 
  | 
2332  | 0  |             CB_FAIL_IF(ret != X509_V_OK, ctx, xi, issuer_depth, ret);  | 
2333  | 0  |             if ((pkey = X509_get0_pubkey(xi)) == NULL) { | 
2334  | 0  |                 CB_FAIL_IF(1, ctx, xi, issuer_depth,  | 
2335  | 0  |                            X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY);  | 
2336  | 0  |             } else { | 
2337  | 0  |                 CB_FAIL_IF(X509_verify(xs, pkey) <= 0,  | 
2338  | 0  |                            ctx, xs, n, X509_V_ERR_CERT_SIGNATURE_FAILURE);  | 
2339  | 0  |             }  | 
2340  | 0  |         }  | 
2341  |  |  | 
2342  |  |         /* In addition to RFC 5280 requirements do also for trust anchor cert */  | 
2343  |  |         /* Calls verify callback as needed */  | 
2344  | 0  |         if (!ossl_x509_check_cert_time(ctx, xs, n))  | 
2345  | 0  |             return 0;  | 
2346  |  |  | 
2347  |  |         /*  | 
2348  |  |          * Signal success at this depth.  However, the previous error (if any)  | 
2349  |  |          * is retained.  | 
2350  |  |          */  | 
2351  | 0  |         ctx->current_issuer = xi;  | 
2352  | 0  |         ctx->current_cert = xs;  | 
2353  | 0  |         ctx->error_depth = n;  | 
2354  | 0  |         if (!ctx->verify_cb(1, ctx))  | 
2355  | 0  |             return 0;  | 
2356  |  |  | 
2357  | 0  |         if (--n >= 0) { | 
2358  | 0  |             xi = xs;  | 
2359  | 0  |             xs = sk_X509_value(ctx->chain, n);  | 
2360  | 0  |         }  | 
2361  | 0  |     }  | 
2362  | 0  |     return 1;  | 
2363  | 0  | }  | 
2364  |  |  | 
2365  |  | int X509_cmp_current_time(const ASN1_TIME *ctm)  | 
2366  | 0  | { | 
2367  | 0  |     return X509_cmp_time(ctm, NULL);  | 
2368  | 0  | }  | 
2369  |  |  | 
2370  |  | /* returns 0 on error, otherwise 1 if ctm > cmp_time, else -1 */  | 
2371  |  | int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)  | 
2372  | 0  | { | 
2373  | 0  |     int comparison;  | 
2374  |  | 
  | 
2375  | 0  |     if (!x509_cmp_time_internal(ctm, cmp_time, &comparison))  | 
2376  | 0  |         return 0;  | 
2377  |  |  | 
2378  |  |     /* It's tradition, that makes it OK. Hyrum's law bites forever */  | 
2379  | 0  |     if (comparison == 0)  | 
2380  | 0  |         comparison = -1;  | 
2381  |  | 
  | 
2382  | 0  |     return comparison;  | 
2383  | 0  | }  | 
2384  |  |  | 
2385  |  | /*  | 
2386  |  |  * Return 0 if time should not be checked or reference time is in range,  | 
2387  |  |  * or else 1 if it is past the end, or -1 if it is before the start  | 
2388  |  |  * treats invalid start and end as times infinitely in the past or  | 
2389  |  |  * future, respectively. Do not use on untrusted input (meaning  | 
2390  |  |  * do not use this when validating certificates for actual use)  | 
2391  |  |  */  | 
2392  |  | int X509_cmp_timeframe(const X509_VERIFY_PARAM *vpm,  | 
2393  |  |                        const ASN1_TIME *start, const ASN1_TIME *end)  | 
2394  | 0  | { | 
2395  | 0  |     unsigned long flags = vpm == NULL ? 0 : X509_VERIFY_PARAM_get_flags(vpm);  | 
2396  | 0  |     time_t ref_time;  | 
2397  | 0  |     time_t *time = NULL;  | 
2398  |  | 
  | 
2399  | 0  |     if ((flags & X509_V_FLAG_USE_CHECK_TIME) != 0) { | 
2400  | 0  |         ref_time = X509_VERIFY_PARAM_get_time(vpm);  | 
2401  | 0  |         time = &ref_time;  | 
2402  | 0  |     } else if ((flags & X509_V_FLAG_NO_CHECK_TIME) != 0) { | 
2403  | 0  |         return 0; /* this means ok */  | 
2404  | 0  |     } /* else reference time is the current time */  | 
2405  |  |  | 
2406  |  |     /*  | 
2407  |  |      * XXX this is public API so we have the entertaining property  | 
2408  |  |      * that invalid asn1 times for |start| or |end| are effectively  | 
2409  |  |      * treated as infinitely in the past or future, due to the use  | 
2410  |  |      * X509_cmp_time, and the 0 return for an invalid time.  | 
2411  |  |      *  | 
2412  |  |      * Treating NULL as infinite a bit off but probably mostly harmless  | 
2413  |  |      * in practice because X509_get0_notBefore and friends do not  | 
2414  |  |      * return NULL. However, if you can end up using a cert with an  | 
2415  |  |      * invalid time that whatever signed it did not validate it in a  | 
2416  |  |      * compatible way with us, You can end up with infinite validity  | 
2417  |  |      * when you did not expect it. Depending on how you got the  | 
2418  |  |      * certificate and what you are doing based upon this decision  | 
2419  |  |      * this could have undesirable consequences.  | 
2420  |  |      *  | 
2421  |  |      * (invalid) (invalid) -> 0;  | 
2422  |  |      * start (invalid) -> returns 0 if start if after time  | 
2423  |  |      * (invalid) end -> returns 0 if end is before time  | 
2424  |  |      *  | 
2425  |  |      * So for better or worse we keep this the way it is and update  | 
2426  |  |      * the documentation accordingly.  | 
2427  |  |      */  | 
2428  | 0  |     if (end != NULL && X509_cmp_time(end, time) < 0)  | 
2429  | 0  |         return 1;  | 
2430  | 0  |     if (start != NULL && X509_cmp_time(start, time) > 0)  | 
2431  | 0  |         return -1;  | 
2432  | 0  |     return 0;  | 
2433  | 0  | }  | 
2434  |  |  | 
2435  |  | ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)  | 
2436  | 0  | { | 
2437  | 0  |     return X509_time_adj(s, adj, NULL);  | 
2438  | 0  | }  | 
2439  |  |  | 
2440  |  | ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)  | 
2441  | 0  | { | 
2442  | 0  |     return X509_time_adj_ex(s, 0, offset_sec, in_tm);  | 
2443  | 0  | }  | 
2444  |  |  | 
2445  |  | ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,  | 
2446  |  |                             int offset_day, long offset_sec, time_t *in_tm)  | 
2447  | 0  | { | 
2448  | 0  |     time_t t;  | 
2449  |  | 
  | 
2450  | 0  |     if (in_tm)  | 
2451  | 0  |         t = *in_tm;  | 
2452  | 0  |     else  | 
2453  | 0  |         time(&t);  | 
2454  |  | 
  | 
2455  | 0  |     if (s != NULL && (s->flags & ASN1_STRING_FLAG_MSTRING) == 0) { | 
2456  | 0  |         if (s->type == V_ASN1_UTCTIME)  | 
2457  | 0  |             return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);  | 
2458  | 0  |         if (s->type == V_ASN1_GENERALIZEDTIME)  | 
2459  | 0  |             return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);  | 
2460  | 0  |     }  | 
2461  | 0  |     return ASN1_TIME_adj(s, t, offset_day, offset_sec);  | 
2462  | 0  | }  | 
2463  |  |  | 
2464  |  | /* Copy any missing public key parameters up the chain towards pkey */  | 
2465  |  | int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)  | 
2466  | 0  | { | 
2467  | 0  |     EVP_PKEY *ktmp = NULL, *ktmp2;  | 
2468  | 0  |     int i, j;  | 
2469  |  | 
  | 
2470  | 0  |     if (pkey != NULL && !EVP_PKEY_missing_parameters(pkey))  | 
2471  | 0  |         return 1;  | 
2472  |  |  | 
2473  | 0  |     for (i = 0; i < sk_X509_num(chain); i++) { | 
2474  | 0  |         ktmp = X509_get0_pubkey(sk_X509_value(chain, i));  | 
2475  | 0  |         if (ktmp == NULL) { | 
2476  | 0  |             ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);  | 
2477  | 0  |             return 0;  | 
2478  | 0  |         }  | 
2479  | 0  |         if (!EVP_PKEY_missing_parameters(ktmp))  | 
2480  | 0  |             break;  | 
2481  | 0  |         ktmp = NULL;  | 
2482  | 0  |     }  | 
2483  | 0  |     if (ktmp == NULL) { | 
2484  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);  | 
2485  | 0  |         return 0;  | 
2486  | 0  |     }  | 
2487  |  |  | 
2488  |  |     /* first, populate the other certs */  | 
2489  | 0  |     for (j = i - 1; j >= 0; j--) { | 
2490  | 0  |         ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j));  | 
2491  | 0  |         if (!EVP_PKEY_copy_parameters(ktmp2, ktmp))  | 
2492  | 0  |             return 0;  | 
2493  | 0  |     }  | 
2494  |  |  | 
2495  | 0  |     if (pkey != NULL)  | 
2496  | 0  |         return EVP_PKEY_copy_parameters(pkey, ktmp);  | 
2497  | 0  |     return 1;  | 
2498  | 0  | }  | 
2499  |  |  | 
2500  |  | /*  | 
2501  |  |  * Make a delta CRL as the difference between two full CRLs.  | 
2502  |  |  * Sadly, returns NULL also on internal error.  | 
2503  |  |  */  | 
2504  |  | X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,  | 
2505  |  |                         EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)  | 
2506  | 0  | { | 
2507  | 0  |     X509_CRL *crl = NULL;  | 
2508  | 0  |     int i;  | 
2509  | 0  |     STACK_OF(X509_REVOKED) *revs = NULL;  | 
2510  |  |  | 
2511  |  |     /* CRLs can't be delta already */  | 
2512  | 0  |     if (base->base_crl_number != NULL || newer->base_crl_number != NULL) { | 
2513  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_CRL_ALREADY_DELTA);  | 
2514  | 0  |         return NULL;  | 
2515  | 0  |     }  | 
2516  |  |     /* Base and new CRL must have a CRL number */  | 
2517  | 0  |     if (base->crl_number == NULL || newer->crl_number == NULL) { | 
2518  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_NUMBER);  | 
2519  | 0  |         return NULL;  | 
2520  | 0  |     }  | 
2521  |  |     /* Issuer names must match */  | 
2522  | 0  |     if (X509_NAME_cmp(X509_CRL_get_issuer(base),  | 
2523  | 0  |                       X509_CRL_get_issuer(newer)) != 0) { | 
2524  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_ISSUER_MISMATCH);  | 
2525  | 0  |         return NULL;  | 
2526  | 0  |     }  | 
2527  |  |     /* AKID and IDP must match */  | 
2528  | 0  |     if (!crl_extension_match(base, newer, NID_authority_key_identifier)) { | 
2529  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_AKID_MISMATCH);  | 
2530  | 0  |         return NULL;  | 
2531  | 0  |     }  | 
2532  | 0  |     if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) { | 
2533  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_IDP_MISMATCH);  | 
2534  | 0  |         return NULL;  | 
2535  | 0  |     }  | 
2536  |  |     /* Newer CRL number must exceed full CRL number */  | 
2537  | 0  |     if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) { | 
2538  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_NEWER_CRL_NOT_NEWER);  | 
2539  | 0  |         return NULL;  | 
2540  | 0  |     }  | 
2541  |  |     /* CRLs must verify */  | 
2542  | 0  |     if (skey != NULL && (X509_CRL_verify(base, skey) <= 0 ||  | 
2543  | 0  |                          X509_CRL_verify(newer, skey) <= 0)) { | 
2544  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_CRL_VERIFY_FAILURE);  | 
2545  | 0  |         return NULL;  | 
2546  | 0  |     }  | 
2547  |  |     /* Create new CRL */  | 
2548  | 0  |     crl = X509_CRL_new_ex(base->libctx, base->propq);  | 
2549  | 0  |     if (crl == NULL || !X509_CRL_set_version(crl, X509_CRL_VERSION_2)) { | 
2550  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2551  | 0  |         goto err;  | 
2552  | 0  |     }  | 
2553  |  |     /* Set issuer name */  | 
2554  | 0  |     if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer))) { | 
2555  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2556  | 0  |         goto err;  | 
2557  | 0  |     }  | 
2558  |  |  | 
2559  | 0  |     if (!X509_CRL_set1_lastUpdate(crl, X509_CRL_get0_lastUpdate(newer))) { | 
2560  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2561  | 0  |         goto err;  | 
2562  | 0  |     }  | 
2563  | 0  |     if (!X509_CRL_set1_nextUpdate(crl, X509_CRL_get0_nextUpdate(newer))) { | 
2564  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2565  | 0  |         goto err;  | 
2566  | 0  |     }  | 
2567  |  |  | 
2568  |  |     /* Set base CRL number: must be critical */  | 
2569  | 0  |     if (X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0) <= 0) { | 
2570  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2571  | 0  |         goto err;  | 
2572  | 0  |     }  | 
2573  |  |  | 
2574  |  |     /*  | 
2575  |  |      * Copy extensions across from newest CRL to delta: this will set CRL  | 
2576  |  |      * number to correct value too.  | 
2577  |  |      */  | 
2578  | 0  |     for (i = 0; i < X509_CRL_get_ext_count(newer); i++) { | 
2579  | 0  |         X509_EXTENSION *ext = X509_CRL_get_ext(newer, i);  | 
2580  |  | 
  | 
2581  | 0  |         if (!X509_CRL_add_ext(crl, ext, -1)) { | 
2582  | 0  |             ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2583  | 0  |             goto err;  | 
2584  | 0  |         }  | 
2585  | 0  |     }  | 
2586  |  |  | 
2587  |  |     /* Go through revoked entries, copying as needed */  | 
2588  | 0  |     revs = X509_CRL_get_REVOKED(newer);  | 
2589  |  | 
  | 
2590  | 0  |     for (i = 0; i < sk_X509_REVOKED_num(revs); i++) { | 
2591  | 0  |         X509_REVOKED *rvn, *rvtmp;  | 
2592  |  | 
  | 
2593  | 0  |         rvn = sk_X509_REVOKED_value(revs, i);  | 
2594  |  |         /*  | 
2595  |  |          * Add only if not also in base.  | 
2596  |  |          * Need something cleverer here for some more complex CRLs covering  | 
2597  |  |          * multiple CAs.  | 
2598  |  |          */  | 
2599  | 0  |         if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) { | 
2600  | 0  |             rvtmp = X509_REVOKED_dup(rvn);  | 
2601  | 0  |             if (rvtmp == NULL) { | 
2602  | 0  |                 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
2603  | 0  |                 goto err;  | 
2604  | 0  |             }  | 
2605  | 0  |             if (!X509_CRL_add0_revoked(crl, rvtmp)) { | 
2606  | 0  |                 X509_REVOKED_free(rvtmp);  | 
2607  | 0  |                 ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2608  | 0  |                 goto err;  | 
2609  | 0  |             }  | 
2610  | 0  |         }  | 
2611  | 0  |     }  | 
2612  |  |  | 
2613  | 0  |     if (skey != NULL && md != NULL && !X509_CRL_sign(crl, skey, md)) { | 
2614  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
2615  | 0  |         goto err;  | 
2616  | 0  |     }  | 
2617  |  |  | 
2618  | 0  |     return crl;  | 
2619  |  |  | 
2620  | 0  |  err:  | 
2621  | 0  |     X509_CRL_free(crl);  | 
2622  | 0  |     return NULL;  | 
2623  | 0  | }  | 
2624  |  |  | 
2625  |  | int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)  | 
2626  | 0  | { | 
2627  | 0  |     return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);  | 
2628  | 0  | }  | 
2629  |  |  | 
2630  |  | void *X509_STORE_CTX_get_ex_data(const X509_STORE_CTX *ctx, int idx)  | 
2631  | 0  | { | 
2632  | 0  |     return CRYPTO_get_ex_data(&ctx->ex_data, idx);  | 
2633  | 0  | }  | 
2634  |  |  | 
2635  |  | int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx)  | 
2636  | 0  | { | 
2637  | 0  |     return ctx->error;  | 
2638  | 0  | }  | 
2639  |  |  | 
2640  |  | void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)  | 
2641  | 0  | { | 
2642  | 0  |     ctx->error = err;  | 
2643  | 0  | }  | 
2644  |  |  | 
2645  |  | int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx)  | 
2646  | 0  | { | 
2647  | 0  |     return ctx->error_depth;  | 
2648  | 0  | }  | 
2649  |  |  | 
2650  |  | void X509_STORE_CTX_set_error_depth(X509_STORE_CTX *ctx, int depth)  | 
2651  | 0  | { | 
2652  | 0  |     ctx->error_depth = depth;  | 
2653  | 0  | }  | 
2654  |  |  | 
2655  |  | X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx)  | 
2656  | 0  | { | 
2657  | 0  |     return ctx->current_cert;  | 
2658  | 0  | }  | 
2659  |  |  | 
2660  |  | void X509_STORE_CTX_set_current_cert(X509_STORE_CTX *ctx, X509 *x)  | 
2661  | 0  | { | 
2662  | 0  |     ctx->current_cert = x;  | 
2663  | 0  | }  | 
2664  |  |  | 
2665  |  | STACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx)  | 
2666  | 0  | { | 
2667  | 0  |     return ctx->chain;  | 
2668  | 0  | }  | 
2669  |  |  | 
2670  |  | STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx)  | 
2671  | 0  | { | 
2672  | 0  |     if (ctx->chain == NULL)  | 
2673  | 0  |         return NULL;  | 
2674  | 0  |     return X509_chain_up_ref(ctx->chain);  | 
2675  | 0  | }  | 
2676  |  |  | 
2677  |  | X509 *X509_STORE_CTX_get0_current_issuer(const X509_STORE_CTX *ctx)  | 
2678  | 0  | { | 
2679  | 0  |     return ctx->current_issuer;  | 
2680  | 0  | }  | 
2681  |  |  | 
2682  |  | X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx)  | 
2683  | 0  | { | 
2684  | 0  |     return ctx->current_crl;  | 
2685  | 0  | }  | 
2686  |  |  | 
2687  |  | X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx)  | 
2688  | 0  | { | 
2689  | 0  |     return ctx->parent;  | 
2690  | 0  | }  | 
2691  |  |  | 
2692  |  | void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)  | 
2693  | 0  | { | 
2694  | 0  |     ctx->cert = x;  | 
2695  | 0  | }  | 
2696  |  |  | 
2697  |  | void X509_STORE_CTX_set0_rpk(X509_STORE_CTX *ctx, EVP_PKEY *rpk)  | 
2698  | 0  | { | 
2699  | 0  |     ctx->rpk = rpk;  | 
2700  | 0  | }  | 
2701  |  |  | 
2702  |  | void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)  | 
2703  | 0  | { | 
2704  | 0  |     ctx->crls = sk;  | 
2705  | 0  | }  | 
2706  |  |  | 
2707  |  | #ifndef OPENSSL_NO_OCSP  | 
2708  |  | void X509_STORE_CTX_set_ocsp_resp(X509_STORE_CTX *ctx, STACK_OF(OCSP_RESPONSE) *sk)  | 
2709  | 0  | { | 
2710  | 0  |     ctx->ocsp_resp = sk;  | 
2711  | 0  | }  | 
2712  |  | #endif  | 
2713  |  |  | 
2714  |  | int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)  | 
2715  | 0  | { | 
2716  |  |     /*  | 
2717  |  |      * XXX: Why isn't this function always used to set the associated trust?  | 
2718  |  |      * Should there even be a VPM->trust field at all?  Or should the trust  | 
2719  |  |      * always be inferred from the purpose by X509_STORE_CTX_init().  | 
2720  |  |      */  | 
2721  | 0  |     return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);  | 
2722  | 0  | }  | 
2723  |  |  | 
2724  |  | int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)  | 
2725  | 0  | { | 
2726  |  |     /*  | 
2727  |  |      * XXX: See above, this function would only be needed when the default  | 
2728  |  |      * trust for the purpose needs an override in a corner case.  | 
2729  |  |      */  | 
2730  | 0  |     return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);  | 
2731  | 0  | }  | 
2732  |  |  | 
2733  |  | /*  | 
2734  |  |  * Use this function to set the X509_STORE_CTX purpose and/or trust id values.  | 
2735  |  |  * The |def_purpose| argument is used if the given purpose value is 0.  | 
2736  |  |  * The |purpose| is unchanged if also the def_purpose argument is 0.  | 
2737  |  |  * The |trust| is unchanged if the given trust value is X509_TRUST_DEFAULT.  | 
2738  |  |  * This is intended to be used when another structure has its own trust and  | 
2739  |  |  * purpose values, which (if set) will be inherited by the |ctx|. If they aren't  | 
2740  |  |  * set then we will usually have a default purpose in mind, which should then  | 
2741  |  |  * be used to set the trust id. An example of this is SSL use: an SSL  | 
2742  |  |  * structure will have its own purpose and trust settings, which the  | 
2743  |  |  * application can set: if they aren't set then we use the default of SSL  | 
2744  |  |  * client/server.  | 
2745  |  |  */  | 
2746  |  | int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,  | 
2747  |  |                                    int purpose, int trust)  | 
2748  | 0  | { | 
2749  | 0  |     int idx;  | 
2750  |  |  | 
2751  |  |     /* If purpose not set use default */  | 
2752  | 0  |     if (purpose == 0)  | 
2753  | 0  |         purpose = def_purpose;  | 
2754  |  |     /*  | 
2755  |  |      * If purpose is set but we don't have a default then set the default to  | 
2756  |  |      * the current purpose  | 
2757  |  |      */  | 
2758  | 0  |     else if (def_purpose == 0)  | 
2759  | 0  |         def_purpose = purpose;  | 
2760  |  |     /* If we have a purpose then check it is valid */  | 
2761  | 0  |     if (purpose != 0) { | 
2762  | 0  |         X509_PURPOSE *ptmp;  | 
2763  |  | 
  | 
2764  | 0  |         idx = X509_PURPOSE_get_by_id(purpose);  | 
2765  | 0  |         if (idx == -1) { | 
2766  | 0  |             ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID);  | 
2767  | 0  |             return 0;  | 
2768  | 0  |         }  | 
2769  | 0  |         ptmp = X509_PURPOSE_get0(idx);  | 
2770  | 0  |         if (ptmp->trust == X509_TRUST_DEFAULT) { | 
2771  | 0  |             idx = X509_PURPOSE_get_by_id(def_purpose);  | 
2772  | 0  |             if (idx == -1) { | 
2773  | 0  |                 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID);  | 
2774  | 0  |                 return 0;  | 
2775  | 0  |             }  | 
2776  | 0  |             ptmp = X509_PURPOSE_get0(idx);  | 
2777  | 0  |         }  | 
2778  |  |         /* If trust not set then get from purpose default */  | 
2779  | 0  |         if (trust == X509_TRUST_DEFAULT)  | 
2780  | 0  |             trust = ptmp->trust;  | 
2781  | 0  |     }  | 
2782  | 0  |     if (trust != X509_TRUST_DEFAULT) { | 
2783  | 0  |         idx = X509_TRUST_get_by_id(trust);  | 
2784  | 0  |         if (idx == -1) { | 
2785  | 0  |             ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_TRUST_ID);  | 
2786  | 0  |             return 0;  | 
2787  | 0  |         }  | 
2788  | 0  |     }  | 
2789  |  |  | 
2790  | 0  |     if (ctx->param->purpose == 0 && purpose != 0)  | 
2791  | 0  |         ctx->param->purpose = purpose;  | 
2792  | 0  |     if (ctx->param->trust == X509_TRUST_DEFAULT && trust != X509_TRUST_DEFAULT)  | 
2793  | 0  |         ctx->param->trust = trust;  | 
2794  | 0  |     return 1;  | 
2795  | 0  | }  | 
2796  |  |  | 
2797  |  | X509_STORE_CTX *X509_STORE_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq)  | 
2798  | 0  | { | 
2799  | 0  |     X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));  | 
2800  |  | 
  | 
2801  | 0  |     if (ctx == NULL)  | 
2802  | 0  |         return NULL;  | 
2803  |  |  | 
2804  | 0  |     ctx->libctx = libctx;  | 
2805  | 0  |     if (propq != NULL) { | 
2806  | 0  |         ctx->propq = OPENSSL_strdup(propq);  | 
2807  | 0  |         if (ctx->propq == NULL) { | 
2808  | 0  |             OPENSSL_free(ctx);  | 
2809  | 0  |             return NULL;  | 
2810  | 0  |         }  | 
2811  | 0  |     }  | 
2812  |  |  | 
2813  | 0  |     return ctx;  | 
2814  | 0  | }  | 
2815  |  |  | 
2816  |  | X509_STORE_CTX *X509_STORE_CTX_new(void)  | 
2817  | 0  | { | 
2818  | 0  |     return X509_STORE_CTX_new_ex(NULL, NULL);  | 
2819  | 0  | }  | 
2820  |  |  | 
2821  |  | void X509_STORE_CTX_free(X509_STORE_CTX *ctx)  | 
2822  | 0  | { | 
2823  | 0  |     if (ctx == NULL)  | 
2824  | 0  |         return;  | 
2825  |  |  | 
2826  | 0  |     X509_STORE_CTX_cleanup(ctx);  | 
2827  |  |  | 
2828  |  |     /* libctx and propq survive X509_STORE_CTX_cleanup() */  | 
2829  | 0  |     OPENSSL_free(ctx->propq);  | 
2830  | 0  |     OPENSSL_free(ctx);  | 
2831  | 0  | }  | 
2832  |  |  | 
2833  |  | int X509_STORE_CTX_init_rpk(X509_STORE_CTX *ctx, X509_STORE *store, EVP_PKEY *rpk)  | 
2834  | 0  | { | 
2835  | 0  |     if (!X509_STORE_CTX_init(ctx, store, NULL, NULL))  | 
2836  | 0  |         return 0;  | 
2837  | 0  |     ctx->rpk = rpk;  | 
2838  | 0  |     return 1;  | 
2839  | 0  | }  | 
2840  |  |  | 
2841  |  | int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,  | 
2842  |  |                         STACK_OF(X509) *chain)  | 
2843  | 0  | { | 
2844  | 0  |     if (ctx == NULL) { | 
2845  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
2846  | 0  |         return 0;  | 
2847  | 0  |     }  | 
2848  | 0  |     X509_STORE_CTX_cleanup(ctx);  | 
2849  |  | 
  | 
2850  | 0  |     ctx->store = store;  | 
2851  | 0  |     ctx->cert = x509;  | 
2852  | 0  |     ctx->untrusted = chain;  | 
2853  | 0  |     ctx->crls = NULL;  | 
2854  | 0  |     ctx->num_untrusted = 0;  | 
2855  | 0  |     ctx->other_ctx = NULL;  | 
2856  | 0  |     ctx->valid = 0;  | 
2857  | 0  |     ctx->chain = NULL;  | 
2858  | 0  |     ctx->error = X509_V_OK;  | 
2859  | 0  |     ctx->explicit_policy = 0;  | 
2860  | 0  |     ctx->error_depth = 0;  | 
2861  | 0  |     ctx->current_cert = NULL;  | 
2862  | 0  |     ctx->current_issuer = NULL;  | 
2863  | 0  |     ctx->current_crl = NULL;  | 
2864  | 0  |     ctx->current_crl_score = 0;  | 
2865  | 0  |     ctx->current_reasons = 0;  | 
2866  | 0  |     ctx->tree = NULL;  | 
2867  | 0  |     ctx->parent = NULL;  | 
2868  | 0  |     ctx->dane = NULL;  | 
2869  | 0  |     ctx->bare_ta_signed = 0;  | 
2870  | 0  |     ctx->rpk = NULL;  | 
2871  |  |     /* Zero ex_data to make sure we're cleanup-safe */  | 
2872  | 0  |     memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));  | 
2873  | 0  |     ctx->ocsp_resp = NULL;  | 
2874  |  |  | 
2875  |  |     /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */  | 
2876  | 0  |     if (store != NULL)  | 
2877  | 0  |         ctx->cleanup = store->cleanup;  | 
2878  | 0  |     else  | 
2879  | 0  |         ctx->cleanup = NULL;  | 
2880  |  | 
  | 
2881  | 0  |     if (store != NULL && store->check_issued != NULL)  | 
2882  | 0  |         ctx->check_issued = store->check_issued;  | 
2883  | 0  |     else  | 
2884  | 0  |         ctx->check_issued = check_issued;  | 
2885  |  | 
  | 
2886  | 0  |     if (store != NULL && store->get_issuer != NULL)  | 
2887  | 0  |         ctx->get_issuer = store->get_issuer;  | 
2888  | 0  |     else  | 
2889  | 0  |         ctx->get_issuer = X509_STORE_CTX_get1_issuer;  | 
2890  |  | 
  | 
2891  | 0  |     if (store != NULL && store->verify_cb != NULL)  | 
2892  | 0  |         ctx->verify_cb = store->verify_cb;  | 
2893  | 0  |     else  | 
2894  | 0  |         ctx->verify_cb = null_callback;  | 
2895  |  | 
  | 
2896  | 0  |     if (store != NULL && store->verify != NULL)  | 
2897  | 0  |         ctx->verify = store->verify;  | 
2898  | 0  |     else  | 
2899  | 0  |         ctx->verify = internal_verify;  | 
2900  |  | 
  | 
2901  | 0  |     if (store != NULL && store->check_revocation != NULL)  | 
2902  | 0  |         ctx->check_revocation = store->check_revocation;  | 
2903  | 0  |     else  | 
2904  | 0  |         ctx->check_revocation = check_revocation;  | 
2905  |  | 
  | 
2906  | 0  |     if (store != NULL && store->get_crl != NULL)  | 
2907  | 0  |         ctx->get_crl = store->get_crl;  | 
2908  | 0  |     else  | 
2909  | 0  |         ctx->get_crl = NULL;  | 
2910  |  | 
  | 
2911  | 0  |     if (store != NULL && store->check_crl != NULL)  | 
2912  | 0  |         ctx->check_crl = store->check_crl;  | 
2913  | 0  |     else  | 
2914  | 0  |         ctx->check_crl = check_crl;  | 
2915  |  | 
  | 
2916  | 0  |     if (store != NULL && store->cert_crl != NULL)  | 
2917  | 0  |         ctx->cert_crl = store->cert_crl;  | 
2918  | 0  |     else  | 
2919  | 0  |         ctx->cert_crl = cert_crl;  | 
2920  |  | 
  | 
2921  | 0  |     if (store != NULL && store->check_policy != NULL)  | 
2922  | 0  |         ctx->check_policy = store->check_policy;  | 
2923  | 0  |     else  | 
2924  | 0  |         ctx->check_policy = check_policy;  | 
2925  |  | 
  | 
2926  | 0  |     if (store != NULL && store->lookup_certs != NULL)  | 
2927  | 0  |         ctx->lookup_certs = store->lookup_certs;  | 
2928  | 0  |     else  | 
2929  | 0  |         ctx->lookup_certs = X509_STORE_CTX_get1_certs;  | 
2930  |  | 
  | 
2931  | 0  |     if (store != NULL && store->lookup_crls != NULL)  | 
2932  | 0  |         ctx->lookup_crls = store->lookup_crls;  | 
2933  | 0  |     else  | 
2934  | 0  |         ctx->lookup_crls = X509_STORE_CTX_get1_crls;  | 
2935  |  | 
  | 
2936  | 0  |     ctx->param = X509_VERIFY_PARAM_new();  | 
2937  | 0  |     if (ctx->param == NULL) { | 
2938  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
2939  | 0  |         goto err;  | 
2940  | 0  |     }  | 
2941  |  |  | 
2942  |  |     /* Inherit callbacks and flags from X509_STORE if not set use defaults. */  | 
2943  | 0  |     if (store == NULL)  | 
2944  | 0  |         ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;  | 
2945  | 0  |     else if (X509_VERIFY_PARAM_inherit(ctx->param, store->param) == 0)  | 
2946  | 0  |         goto err;  | 
2947  |  |  | 
2948  | 0  |     if (!X509_STORE_CTX_set_default(ctx, "default"))  | 
2949  | 0  |         goto err;  | 
2950  |  |  | 
2951  |  |     /*  | 
2952  |  |      * XXX: For now, continue to inherit trust from VPM, but infer from the  | 
2953  |  |      * purpose if this still yields the default value.  | 
2954  |  |      */  | 
2955  | 0  |     if (ctx->param->trust == X509_TRUST_DEFAULT) { | 
2956  | 0  |         int idx = X509_PURPOSE_get_by_id(ctx->param->purpose);  | 
2957  | 0  |         X509_PURPOSE *xp = X509_PURPOSE_get0(idx);  | 
2958  |  | 
  | 
2959  | 0  |         if (xp != NULL)  | 
2960  | 0  |             ctx->param->trust = X509_PURPOSE_get_trust(xp);  | 
2961  | 0  |     }  | 
2962  |  | 
  | 
2963  | 0  |     if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,  | 
2964  | 0  |                            &ctx->ex_data))  | 
2965  | 0  |         return 1;  | 
2966  | 0  |     ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);  | 
2967  |  | 
  | 
2968  | 0  |  err:  | 
2969  |  |     /*  | 
2970  |  |      * On error clean up allocated storage, if the store context was not  | 
2971  |  |      * allocated with X509_STORE_CTX_new() this is our last chance to do so.  | 
2972  |  |      */  | 
2973  | 0  |     X509_STORE_CTX_cleanup(ctx);  | 
2974  | 0  |     return 0;  | 
2975  | 0  | }  | 
2976  |  |  | 
2977  |  | /*  | 
2978  |  |  * Set alternative get_issuer method: just from a STACK of trusted certificates.  | 
2979  |  |  * This avoids the complexity of X509_STORE where it is not needed.  | 
2980  |  |  */  | 
2981  |  | void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)  | 
2982  | 0  | { | 
2983  | 0  |     ctx->other_ctx = sk;  | 
2984  | 0  |     ctx->get_issuer = get1_best_issuer_other_sk;  | 
2985  | 0  |     ctx->lookup_certs = lookup_certs_sk;  | 
2986  | 0  | }  | 
2987  |  |  | 
2988  |  | void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)  | 
2989  | 0  | { | 
2990  |  |     /*  | 
2991  |  |      * We need to be idempotent because, unfortunately, free() also calls  | 
2992  |  |      * cleanup(), so the natural call sequence new(), init(), cleanup(), free()  | 
2993  |  |      * calls cleanup() for the same object twice!  Thus we must zero the  | 
2994  |  |      * pointers below after they're freed!  | 
2995  |  |      */  | 
2996  |  |     /* Seems to always be NULL in OpenSSL, do this at most once. */  | 
2997  | 0  |     if (ctx->cleanup != NULL) { | 
2998  | 0  |         ctx->cleanup(ctx);  | 
2999  | 0  |         ctx->cleanup = NULL;  | 
3000  | 0  |     }  | 
3001  | 0  |     if (ctx->param != NULL) { | 
3002  | 0  |         if (ctx->parent == NULL)  | 
3003  | 0  |             X509_VERIFY_PARAM_free(ctx->param);  | 
3004  | 0  |         ctx->param = NULL;  | 
3005  | 0  |     }  | 
3006  | 0  |     X509_policy_tree_free(ctx->tree);  | 
3007  | 0  |     ctx->tree = NULL;  | 
3008  | 0  |     OSSL_STACK_OF_X509_free(ctx->chain);  | 
3009  | 0  |     ctx->chain = NULL;  | 
3010  | 0  |     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));  | 
3011  | 0  |     memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));  | 
3012  | 0  | }  | 
3013  |  |  | 
3014  |  | void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)  | 
3015  | 0  | { | 
3016  | 0  |     X509_VERIFY_PARAM_set_depth(ctx->param, depth);  | 
3017  | 0  | }  | 
3018  |  |  | 
3019  |  | void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)  | 
3020  | 0  | { | 
3021  | 0  |     X509_VERIFY_PARAM_set_flags(ctx->param, flags);  | 
3022  | 0  | }  | 
3023  |  |  | 
3024  |  | void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,  | 
3025  |  |                              time_t t)  | 
3026  | 0  | { | 
3027  | 0  |     X509_VERIFY_PARAM_set_time(ctx->param, t);  | 
3028  | 0  | }  | 
3029  |  |  | 
3030  |  | void X509_STORE_CTX_set_current_reasons(X509_STORE_CTX *ctx,  | 
3031  |  |                                         unsigned int current_reasons)  | 
3032  | 0  | { | 
3033  | 0  |     ctx->current_reasons = current_reasons;  | 
3034  | 0  | }  | 
3035  |  |  | 
3036  |  | X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx)  | 
3037  | 0  | { | 
3038  | 0  |     return ctx->cert;  | 
3039  | 0  | }  | 
3040  |  |  | 
3041  |  | EVP_PKEY *X509_STORE_CTX_get0_rpk(const X509_STORE_CTX *ctx)  | 
3042  | 0  | { | 
3043  | 0  |     return ctx->rpk;  | 
3044  | 0  | }  | 
3045  |  |  | 
3046  |  | STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx)  | 
3047  | 0  | { | 
3048  | 0  |     return ctx->untrusted;  | 
3049  | 0  | }  | 
3050  |  |  | 
3051  |  | void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)  | 
3052  | 0  | { | 
3053  | 0  |     ctx->untrusted = sk;  | 
3054  | 0  | }  | 
3055  |  |  | 
3056  |  | void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)  | 
3057  | 0  | { | 
3058  | 0  |     OSSL_STACK_OF_X509_free(ctx->chain);  | 
3059  | 0  |     ctx->chain = sk;  | 
3060  | 0  | }  | 
3061  |  |  | 
3062  |  | void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,  | 
3063  |  |                                   X509_STORE_CTX_verify_cb verify_cb)  | 
3064  | 0  | { | 
3065  | 0  |     ctx->verify_cb = verify_cb;  | 
3066  | 0  | }  | 
3067  |  |  | 
3068  |  | X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(const X509_STORE_CTX *ctx)  | 
3069  | 0  | { | 
3070  | 0  |     return ctx->verify_cb;  | 
3071  | 0  | }  | 
3072  |  |  | 
3073  |  | void X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx,  | 
3074  |  |                                X509_STORE_CTX_verify_fn verify)  | 
3075  | 0  | { | 
3076  | 0  |     ctx->verify = verify;  | 
3077  | 0  | }  | 
3078  |  |  | 
3079  |  | X509_STORE_CTX_verify_fn X509_STORE_CTX_get_verify(const X509_STORE_CTX *ctx)  | 
3080  | 0  | { | 
3081  | 0  |     return ctx->verify;  | 
3082  | 0  | }  | 
3083  |  |  | 
3084  |  | X509_STORE_CTX_get_issuer_fn  | 
3085  |  | X509_STORE_CTX_get_get_issuer(const X509_STORE_CTX *ctx)  | 
3086  | 0  | { | 
3087  | 0  |     return ctx->get_issuer;  | 
3088  | 0  | }  | 
3089  |  |  | 
3090  |  | X509_STORE_CTX_check_issued_fn  | 
3091  |  | X509_STORE_CTX_get_check_issued(const X509_STORE_CTX *ctx)  | 
3092  | 0  | { | 
3093  | 0  |     return ctx->check_issued;  | 
3094  | 0  | }  | 
3095  |  |  | 
3096  |  | X509_STORE_CTX_check_revocation_fn  | 
3097  |  | X509_STORE_CTX_get_check_revocation(const X509_STORE_CTX *ctx)  | 
3098  | 0  | { | 
3099  | 0  |     return ctx->check_revocation;  | 
3100  | 0  | }  | 
3101  |  |  | 
3102  |  | X509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(const X509_STORE_CTX *ctx)  | 
3103  | 0  | { | 
3104  | 0  |     return ctx->get_crl;  | 
3105  | 0  | }  | 
3106  |  |  | 
3107  |  | void X509_STORE_CTX_set_get_crl(X509_STORE_CTX *ctx,  | 
3108  |  |                                 X509_STORE_CTX_get_crl_fn get_crl)  | 
3109  | 0  | { | 
3110  | 0  |     ctx->get_crl = get_crl;  | 
3111  | 0  | }  | 
3112  |  |  | 
3113  |  | X509_STORE_CTX_check_crl_fn  | 
3114  |  | X509_STORE_CTX_get_check_crl(const X509_STORE_CTX *ctx)  | 
3115  | 0  | { | 
3116  | 0  |     return ctx->check_crl;  | 
3117  | 0  | }  | 
3118  |  |  | 
3119  |  | X509_STORE_CTX_cert_crl_fn  | 
3120  |  | X509_STORE_CTX_get_cert_crl(const X509_STORE_CTX *ctx)  | 
3121  | 0  | { | 
3122  | 0  |     return ctx->cert_crl;  | 
3123  | 0  | }  | 
3124  |  |  | 
3125  |  | X509_STORE_CTX_check_policy_fn  | 
3126  |  | X509_STORE_CTX_get_check_policy(const X509_STORE_CTX *ctx)  | 
3127  | 0  | { | 
3128  | 0  |     return ctx->check_policy;  | 
3129  | 0  | }  | 
3130  |  |  | 
3131  |  | X509_STORE_CTX_lookup_certs_fn  | 
3132  |  | X509_STORE_CTX_get_lookup_certs(const X509_STORE_CTX *ctx)  | 
3133  | 0  | { | 
3134  | 0  |     return ctx->lookup_certs;  | 
3135  | 0  | }  | 
3136  |  |  | 
3137  |  | X509_STORE_CTX_lookup_crls_fn  | 
3138  |  | X509_STORE_CTX_get_lookup_crls(const X509_STORE_CTX *ctx)  | 
3139  | 0  | { | 
3140  | 0  |     return ctx->lookup_crls;  | 
3141  | 0  | }  | 
3142  |  |  | 
3143  |  | X509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(const X509_STORE_CTX *ctx)  | 
3144  | 0  | { | 
3145  | 0  |     return ctx->cleanup;  | 
3146  | 0  | }  | 
3147  |  |  | 
3148  |  | X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(const X509_STORE_CTX *ctx)  | 
3149  | 0  | { | 
3150  | 0  |     return ctx->tree;  | 
3151  | 0  | }  | 
3152  |  |  | 
3153  |  | int X509_STORE_CTX_get_explicit_policy(const X509_STORE_CTX *ctx)  | 
3154  | 0  | { | 
3155  | 0  |     return ctx->explicit_policy;  | 
3156  | 0  | }  | 
3157  |  |  | 
3158  |  | int X509_STORE_CTX_get_num_untrusted(const X509_STORE_CTX *ctx)  | 
3159  | 0  | { | 
3160  | 0  |     return ctx->num_untrusted;  | 
3161  | 0  | }  | 
3162  |  |  | 
3163  |  | int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)  | 
3164  | 0  | { | 
3165  | 0  |     const X509_VERIFY_PARAM *param;  | 
3166  |  | 
  | 
3167  | 0  |     param = X509_VERIFY_PARAM_lookup(name);  | 
3168  | 0  |     if (param == NULL) { | 
3169  | 0  |         ERR_raise_data(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID, "name=%s", name);  | 
3170  | 0  |         return 0;  | 
3171  | 0  |     }  | 
3172  | 0  |     return X509_VERIFY_PARAM_inherit(ctx->param, param);  | 
3173  | 0  | }  | 
3174  |  |  | 
3175  |  | X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(const X509_STORE_CTX *ctx)  | 
3176  | 0  | { | 
3177  | 0  |     return ctx->param;  | 
3178  | 0  | }  | 
3179  |  |  | 
3180  |  | void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)  | 
3181  | 0  | { | 
3182  | 0  |     X509_VERIFY_PARAM_free(ctx->param);  | 
3183  | 0  |     ctx->param = param;  | 
3184  | 0  | }  | 
3185  |  |  | 
3186  |  | void X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, SSL_DANE *dane)  | 
3187  | 0  | { | 
3188  | 0  |     ctx->dane = dane;  | 
3189  | 0  | }  | 
3190  |  |  | 
3191  |  | static unsigned char *dane_i2d(X509 *cert, uint8_t selector,  | 
3192  |  |                                unsigned int *i2dlen)  | 
3193  | 0  | { | 
3194  | 0  |     unsigned char *buf = NULL;  | 
3195  | 0  |     int len;  | 
3196  |  |  | 
3197  |  |     /*  | 
3198  |  |      * Extract ASN.1 DER form of certificate or public key.  | 
3199  |  |      */  | 
3200  | 0  |     switch (selector) { | 
3201  | 0  |     case DANETLS_SELECTOR_CERT:  | 
3202  | 0  |         len = i2d_X509(cert, &buf);  | 
3203  | 0  |         break;  | 
3204  | 0  |     case DANETLS_SELECTOR_SPKI:  | 
3205  | 0  |         len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf);  | 
3206  | 0  |         break;  | 
3207  | 0  |     default:  | 
3208  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_BAD_SELECTOR);  | 
3209  | 0  |         return NULL;  | 
3210  | 0  |     }  | 
3211  |  |  | 
3212  | 0  |     if (len < 0 || buf == NULL) { | 
3213  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
3214  | 0  |         return NULL;  | 
3215  | 0  |     }  | 
3216  |  |  | 
3217  | 0  |     *i2dlen = (unsigned int)len;  | 
3218  | 0  |     return buf;  | 
3219  | 0  | }  | 
3220  |  |  | 
3221  | 0  | #define DANETLS_NONE 256 /* impossible uint8_t */  | 
3222  |  |  | 
3223  |  | /* Returns -1 on internal error */  | 
3224  |  | static int dane_match_cert(X509_STORE_CTX *ctx, X509 *cert, int depth)  | 
3225  | 0  | { | 
3226  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3227  | 0  |     unsigned usage = DANETLS_NONE;  | 
3228  | 0  |     unsigned selector = DANETLS_NONE;  | 
3229  | 0  |     unsigned ordinal = DANETLS_NONE;  | 
3230  | 0  |     unsigned mtype = DANETLS_NONE;  | 
3231  | 0  |     unsigned char *i2dbuf = NULL;  | 
3232  | 0  |     unsigned int i2dlen = 0;  | 
3233  | 0  |     unsigned char mdbuf[EVP_MAX_MD_SIZE];  | 
3234  | 0  |     unsigned char *cmpbuf = NULL;  | 
3235  | 0  |     unsigned int cmplen = 0;  | 
3236  | 0  |     int i;  | 
3237  | 0  |     int recnum;  | 
3238  | 0  |     int matched = 0;  | 
3239  | 0  |     danetls_record *t = NULL;  | 
3240  | 0  |     uint32_t mask;  | 
3241  |  | 
  | 
3242  | 0  |     mask = (depth == 0) ? DANETLS_EE_MASK : DANETLS_TA_MASK;  | 
3243  |  |  | 
3244  |  |     /* The trust store is not applicable with DANE-TA(2) */  | 
3245  | 0  |     if (depth >= ctx->num_untrusted)  | 
3246  | 0  |         mask &= DANETLS_PKIX_MASK;  | 
3247  |  |  | 
3248  |  |     /*  | 
3249  |  |      * If we've previously matched a PKIX-?? record, no need to test any  | 
3250  |  |      * further PKIX-?? records, it remains to just build the PKIX chain.  | 
3251  |  |      * Had the match been a DANE-?? record, we'd be done already.  | 
3252  |  |      */  | 
3253  | 0  |     if (dane->mdpth >= 0)  | 
3254  | 0  |         mask &= ~DANETLS_PKIX_MASK;  | 
3255  |  |  | 
3256  |  |     /*-  | 
3257  |  |      * https://tools.ietf.org/html/rfc7671#section-5.1  | 
3258  |  |      * https://tools.ietf.org/html/rfc7671#section-5.2  | 
3259  |  |      * https://tools.ietf.org/html/rfc7671#section-5.3  | 
3260  |  |      * https://tools.ietf.org/html/rfc7671#section-5.4  | 
3261  |  |      *  | 
3262  |  |      * We handle DANE-EE(3) records first as they require no chain building  | 
3263  |  |      * and no expiration or hostname checks.  We also process digests with  | 
3264  |  |      * higher ordinals first and ignore lower priorities except Full(0) which  | 
3265  |  |      * is always processed (last).  If none match, we then process PKIX-EE(1).  | 
3266  |  |      *  | 
3267  |  |      * NOTE: This relies on DANE usages sorting before the corresponding PKIX  | 
3268  |  |      * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest  | 
3269  |  |      * priorities.  See twin comment in ssl/ssl_lib.c.  | 
3270  |  |      *  | 
3271  |  |      * We expect that most TLSA RRsets will have just a single usage, so we  | 
3272  |  |      * don't go out of our way to cache multiple selector-specific i2d buffers  | 
3273  |  |      * across usages, but if the selector happens to remain the same as switch  | 
3274  |  |      * usages, that's OK.  Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1",  | 
3275  |  |      * records would result in us generating each of the certificate and public  | 
3276  |  |      * key DER forms twice, but more typically we'd just see multiple "3 1 1"  | 
3277  |  |      * or multiple "3 0 1" records.  | 
3278  |  |      *  | 
3279  |  |      * As soon as we find a match at any given depth, we stop, because either  | 
3280  |  |      * we've matched a DANE-?? record and the peer is authenticated, or, after  | 
3281  |  |      * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is  | 
3282  |  |      * sufficient for DANE, and what remains to do is ordinary PKIX validation.  | 
3283  |  |      */  | 
3284  | 0  |     recnum = (dane->umask & mask) != 0 ? sk_danetls_record_num(dane->trecs) : 0;  | 
3285  | 0  |     for (i = 0; matched == 0 && i < recnum; ++i) { | 
3286  | 0  |         t = sk_danetls_record_value(dane->trecs, i);  | 
3287  | 0  |         if ((DANETLS_USAGE_BIT(t->usage) & mask) == 0)  | 
3288  | 0  |             continue;  | 
3289  | 0  |         if (t->usage != usage) { | 
3290  | 0  |             usage = t->usage;  | 
3291  |  |  | 
3292  |  |             /* Reset digest agility for each usage/selector pair */  | 
3293  | 0  |             mtype = DANETLS_NONE;  | 
3294  | 0  |             ordinal = dane->dctx->mdord[t->mtype];  | 
3295  | 0  |         }  | 
3296  | 0  |         if (t->selector != selector) { | 
3297  | 0  |             selector = t->selector;  | 
3298  |  |  | 
3299  |  |             /* Update per-selector state */  | 
3300  | 0  |             OPENSSL_free(i2dbuf);  | 
3301  | 0  |             i2dbuf = dane_i2d(cert, selector, &i2dlen);  | 
3302  | 0  |             if (i2dbuf == NULL)  | 
3303  | 0  |                 return -1;  | 
3304  |  |  | 
3305  |  |             /* Reset digest agility for each usage/selector pair */  | 
3306  | 0  |             mtype = DANETLS_NONE;  | 
3307  | 0  |             ordinal = dane->dctx->mdord[t->mtype];  | 
3308  | 0  |         } else if (t->mtype != DANETLS_MATCHING_FULL) { | 
3309  |  |             /*-  | 
3310  |  |              * Digest agility:  | 
3311  |  |              *  | 
3312  |  |              *     <https://tools.ietf.org/html/rfc7671#section-9>  | 
3313  |  |              *  | 
3314  |  |              * For a fixed selector, after processing all records with the  | 
3315  |  |              * highest mtype ordinal, ignore all mtypes with lower ordinals  | 
3316  |  |              * other than "Full".  | 
3317  |  |              */  | 
3318  | 0  |             if (dane->dctx->mdord[t->mtype] < ordinal)  | 
3319  | 0  |                 continue;  | 
3320  | 0  |         }  | 
3321  |  |  | 
3322  |  |         /*  | 
3323  |  |          * Each time we hit a (new selector or) mtype, re-compute the relevant  | 
3324  |  |          * digest, more complex caching is not worth the code space.  | 
3325  |  |          */  | 
3326  | 0  |         if (t->mtype != mtype) { | 
3327  | 0  |             const EVP_MD *md = dane->dctx->mdevp[mtype = t->mtype];  | 
3328  |  | 
  | 
3329  | 0  |             cmpbuf = i2dbuf;  | 
3330  | 0  |             cmplen = i2dlen;  | 
3331  |  | 
  | 
3332  | 0  |             if (md != NULL) { | 
3333  | 0  |                 cmpbuf = mdbuf;  | 
3334  | 0  |                 if (!EVP_Digest(i2dbuf, i2dlen, cmpbuf, &cmplen, md, 0)) { | 
3335  | 0  |                     matched = -1;  | 
3336  | 0  |                     break;  | 
3337  | 0  |                 }  | 
3338  | 0  |             }  | 
3339  | 0  |         }  | 
3340  |  |  | 
3341  |  |         /*  | 
3342  |  |          * Squirrel away the certificate and depth if we have a match.  Any  | 
3343  |  |          * DANE match is dispositive, but with PKIX we still need to build a  | 
3344  |  |          * full chain.  | 
3345  |  |          */  | 
3346  | 0  |         if (cmplen == t->dlen &&  | 
3347  | 0  |             memcmp(cmpbuf, t->data, cmplen) == 0) { | 
3348  | 0  |             if (DANETLS_USAGE_BIT(usage) & DANETLS_DANE_MASK)  | 
3349  | 0  |                 matched = 1;  | 
3350  | 0  |             if (matched || dane->mdpth < 0) { | 
3351  | 0  |                 if (!X509_up_ref(cert)) { | 
3352  | 0  |                     matched = -1;  | 
3353  | 0  |                     break;  | 
3354  | 0  |                 }  | 
3355  |  |  | 
3356  | 0  |                 OPENSSL_free(dane->mcert);  | 
3357  | 0  |                 dane->mcert = cert;  | 
3358  | 0  |                 dane->mdpth = depth;  | 
3359  | 0  |                 dane->mtlsa = t;  | 
3360  | 0  |             }  | 
3361  | 0  |             break;  | 
3362  | 0  |         }  | 
3363  | 0  |     }  | 
3364  |  |  | 
3365  |  |     /* Clear the one-element DER cache */  | 
3366  | 0  |     OPENSSL_free(i2dbuf);  | 
3367  | 0  |     return matched;  | 
3368  | 0  | }  | 
3369  |  |  | 
3370  |  | /* Returns -1 on internal error */  | 
3371  |  | static int check_dane_issuer(X509_STORE_CTX *ctx, int depth)  | 
3372  | 0  | { | 
3373  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3374  | 0  |     int matched = 0;  | 
3375  | 0  |     X509 *cert;  | 
3376  |  | 
  | 
3377  | 0  |     if (!DANETLS_HAS_TA(dane) || depth == 0)  | 
3378  | 0  |         return X509_TRUST_UNTRUSTED;  | 
3379  |  |  | 
3380  |  |     /*  | 
3381  |  |      * Record any DANE trust anchor matches, for the first depth to test, if  | 
3382  |  |      * there's one at that depth. (This'll be false for length 1 chains looking  | 
3383  |  |      * for an exact match for the leaf certificate).  | 
3384  |  |      */  | 
3385  | 0  |     cert = sk_X509_value(ctx->chain, depth);  | 
3386  | 0  |     if (cert != NULL && (matched = dane_match_cert(ctx, cert, depth)) < 0)  | 
3387  | 0  |         return matched;  | 
3388  | 0  |     if (matched > 0) { | 
3389  | 0  |         ctx->num_untrusted = depth - 1;  | 
3390  | 0  |         return X509_TRUST_TRUSTED;  | 
3391  | 0  |     }  | 
3392  |  |  | 
3393  | 0  |     return X509_TRUST_UNTRUSTED;  | 
3394  | 0  | }  | 
3395  |  |  | 
3396  |  | static int check_dane_pkeys(X509_STORE_CTX *ctx)  | 
3397  | 0  | { | 
3398  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3399  | 0  |     danetls_record *t;  | 
3400  | 0  |     int num = ctx->num_untrusted;  | 
3401  | 0  |     X509 *cert = sk_X509_value(ctx->chain, num - 1);  | 
3402  | 0  |     int recnum = sk_danetls_record_num(dane->trecs);  | 
3403  | 0  |     int i;  | 
3404  |  | 
  | 
3405  | 0  |     for (i = 0; i < recnum; ++i) { | 
3406  | 0  |         t = sk_danetls_record_value(dane->trecs, i);  | 
3407  | 0  |         if (t->usage != DANETLS_USAGE_DANE_TA ||  | 
3408  | 0  |             t->selector != DANETLS_SELECTOR_SPKI ||  | 
3409  | 0  |             t->mtype != DANETLS_MATCHING_FULL ||  | 
3410  | 0  |             X509_verify(cert, t->spki) <= 0)  | 
3411  | 0  |             continue;  | 
3412  |  |  | 
3413  |  |         /* Clear any PKIX-?? matches that failed to extend to a full chain */  | 
3414  | 0  |         X509_free(dane->mcert);  | 
3415  | 0  |         dane->mcert = NULL;  | 
3416  |  |  | 
3417  |  |         /* Record match via a bare TA public key */  | 
3418  | 0  |         ctx->bare_ta_signed = 1;  | 
3419  | 0  |         dane->mdpth = num - 1;  | 
3420  | 0  |         dane->mtlsa = t;  | 
3421  |  |  | 
3422  |  |         /* Prune any excess chain certificates */  | 
3423  | 0  |         num = sk_X509_num(ctx->chain);  | 
3424  | 0  |         for (; num > ctx->num_untrusted; --num)  | 
3425  | 0  |             X509_free(sk_X509_pop(ctx->chain));  | 
3426  |  | 
  | 
3427  | 0  |         return X509_TRUST_TRUSTED;  | 
3428  | 0  |     }  | 
3429  |  |  | 
3430  | 0  |     return X509_TRUST_UNTRUSTED;  | 
3431  | 0  | }  | 
3432  |  |  | 
3433  |  | /*  | 
3434  |  |  * Only DANE-EE and SPKI are supported  | 
3435  |  |  * Returns -1 on internal error  | 
3436  |  |  */  | 
3437  |  | static int dane_match_rpk(X509_STORE_CTX *ctx, EVP_PKEY *rpk)  | 
3438  | 0  | { | 
3439  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3440  | 0  |     danetls_record *t = NULL;  | 
3441  | 0  |     int mtype = DANETLS_MATCHING_FULL;  | 
3442  | 0  |     unsigned char *i2dbuf = NULL;  | 
3443  | 0  |     unsigned int i2dlen = 0;  | 
3444  | 0  |     unsigned char mdbuf[EVP_MAX_MD_SIZE];  | 
3445  | 0  |     unsigned char *cmpbuf;  | 
3446  | 0  |     unsigned int cmplen = 0;  | 
3447  | 0  |     int len;  | 
3448  | 0  |     int recnum = sk_danetls_record_num(dane->trecs);  | 
3449  | 0  |     int i;  | 
3450  | 0  |     int matched = 0;  | 
3451  |  |  | 
3452  |  |     /* Calculate ASN.1 DER of RPK */  | 
3453  | 0  |     if ((len = i2d_PUBKEY(rpk, &i2dbuf)) <= 0)  | 
3454  | 0  |         return -1;  | 
3455  | 0  |     cmplen = i2dlen = (unsigned int)len;  | 
3456  | 0  |     cmpbuf = i2dbuf;  | 
3457  |  | 
  | 
3458  | 0  |     for (i = 0; i < recnum; i++) { | 
3459  | 0  |         t = sk_danetls_record_value(dane->trecs, i);  | 
3460  | 0  |         if (t->usage != DANETLS_USAGE_DANE_EE || t->selector != DANETLS_SELECTOR_SPKI)  | 
3461  | 0  |             continue;  | 
3462  |  |  | 
3463  |  |         /* Calculate hash - keep only one around */  | 
3464  | 0  |         if (t->mtype != mtype) { | 
3465  | 0  |             const EVP_MD *md = dane->dctx->mdevp[mtype = t->mtype];  | 
3466  |  | 
  | 
3467  | 0  |             cmpbuf = i2dbuf;  | 
3468  | 0  |             cmplen = i2dlen;  | 
3469  |  | 
  | 
3470  | 0  |             if (md != NULL) { | 
3471  | 0  |                 cmpbuf = mdbuf;  | 
3472  | 0  |                 if (!EVP_Digest(i2dbuf, i2dlen, cmpbuf, &cmplen, md, 0)) { | 
3473  | 0  |                     matched = -1;  | 
3474  | 0  |                     break;  | 
3475  | 0  |                 }  | 
3476  | 0  |             }  | 
3477  | 0  |         }  | 
3478  | 0  |         if (cmplen == t->dlen && memcmp(cmpbuf, t->data, cmplen) == 0) { | 
3479  | 0  |             matched = 1;  | 
3480  | 0  |             dane->mdpth = 0;  | 
3481  | 0  |             dane->mtlsa = t;  | 
3482  | 0  |             break;  | 
3483  | 0  |         }  | 
3484  | 0  |     }  | 
3485  | 0  |     OPENSSL_free(i2dbuf);  | 
3486  | 0  |     return matched;  | 
3487  | 0  | }  | 
3488  |  |  | 
3489  |  | static void dane_reset(SSL_DANE *dane)  | 
3490  | 0  | { | 
3491  |  |     /* Reset state to verify another chain, or clear after failure. */  | 
3492  | 0  |     X509_free(dane->mcert);  | 
3493  | 0  |     dane->mcert = NULL;  | 
3494  | 0  |     dane->mtlsa = NULL;  | 
3495  | 0  |     dane->mdpth = -1;  | 
3496  | 0  |     dane->pdpth = -1;  | 
3497  | 0  | }  | 
3498  |  |  | 
3499  |  | /* Sadly, returns 0 also on internal error in ctx->verify_cb(). */  | 
3500  |  | static int check_leaf_suiteb(X509_STORE_CTX *ctx, X509 *cert)  | 
3501  | 0  | { | 
3502  | 0  |     int err = X509_chain_check_suiteb(NULL, cert, NULL, ctx->param->flags);  | 
3503  |  | 
  | 
3504  | 0  |     CB_FAIL_IF(err != X509_V_OK, ctx, cert, 0, err);  | 
3505  | 0  |     return 1;  | 
3506  | 0  | }  | 
3507  |  |  | 
3508  |  | /* Returns -1 on internal error */  | 
3509  |  | static int dane_verify_rpk(X509_STORE_CTX *ctx)  | 
3510  | 0  | { | 
3511  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3512  | 0  |     int matched;  | 
3513  |  | 
  | 
3514  | 0  |     dane_reset(dane);  | 
3515  |  |  | 
3516  |  |     /*  | 
3517  |  |      * Look for a DANE record for RPK  | 
3518  |  |      * If error, return -1  | 
3519  |  |      * If found, call ctx->verify_cb(1, ctx)  | 
3520  |  |      * If not found call ctx->verify_cb(0, ctx)  | 
3521  |  |      */  | 
3522  | 0  |     matched = dane_match_rpk(ctx, ctx->rpk);  | 
3523  | 0  |     ctx->error_depth = 0;  | 
3524  |  | 
  | 
3525  | 0  |     if (matched < 0) { | 
3526  | 0  |         ctx->error = X509_V_ERR_UNSPECIFIED;  | 
3527  | 0  |         return -1;  | 
3528  | 0  |     }  | 
3529  |  |  | 
3530  | 0  |     if (matched > 0)  | 
3531  | 0  |         ctx->error = X509_V_OK;  | 
3532  | 0  |     else  | 
3533  | 0  |         ctx->error = X509_V_ERR_DANE_NO_MATCH;  | 
3534  |  | 
  | 
3535  | 0  |     return verify_rpk(ctx);  | 
3536  | 0  | }  | 
3537  |  |  | 
3538  |  | /* Returns -1 on internal error */  | 
3539  |  | static int dane_verify(X509_STORE_CTX *ctx)  | 
3540  | 0  | { | 
3541  | 0  |     X509 *cert = ctx->cert;  | 
3542  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3543  | 0  |     int matched;  | 
3544  | 0  |     int done;  | 
3545  |  | 
  | 
3546  | 0  |     dane_reset(dane);  | 
3547  |  |  | 
3548  |  |     /*-  | 
3549  |  |      * When testing the leaf certificate, if we match a DANE-EE(3) record,  | 
3550  |  |      * dane_match() returns 1 and we're done.  If however we match a PKIX-EE(1)  | 
3551  |  |      * record, the match depth and matching TLSA record are recorded, but the  | 
3552  |  |      * return value is 0, because we still need to find a PKIX trust anchor.  | 
3553  |  |      * Therefore, when DANE authentication is enabled (required), we're done  | 
3554  |  |      * if:  | 
3555  |  |      *   + matched < 0, internal error.  | 
3556  |  |      *   + matched == 1, we matched a DANE-EE(3) record  | 
3557  |  |      *   + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no  | 
3558  |  |      *     DANE-TA(2) or PKIX-TA(0) to test.  | 
3559  |  |      */  | 
3560  | 0  |     matched = dane_match_cert(ctx, ctx->cert, 0);  | 
3561  | 0  |     done = matched != 0 || (!DANETLS_HAS_TA(dane) && dane->mdpth < 0);  | 
3562  |  | 
  | 
3563  | 0  |     if (done && !X509_get_pubkey_parameters(NULL, ctx->chain))  | 
3564  | 0  |         return -1;  | 
3565  |  |  | 
3566  | 0  |     if (matched > 0) { | 
3567  |  |         /* Callback invoked as needed */  | 
3568  | 0  |         if (!check_leaf_suiteb(ctx, cert))  | 
3569  | 0  |             return 0;  | 
3570  |  |         /* Callback invoked as needed */  | 
3571  | 0  |         if ((dane->flags & DANE_FLAG_NO_DANE_EE_NAMECHECKS) == 0 &&  | 
3572  | 0  |             !check_id(ctx))  | 
3573  | 0  |             return 0;  | 
3574  |  |         /* Bypass internal_verify(), issue depth 0 success callback */  | 
3575  | 0  |         ctx->error_depth = 0;  | 
3576  | 0  |         ctx->current_cert = cert;  | 
3577  | 0  |         return ctx->verify_cb(1, ctx);  | 
3578  | 0  |     }  | 
3579  |  |  | 
3580  | 0  |     if (matched < 0) { | 
3581  | 0  |         ctx->error_depth = 0;  | 
3582  | 0  |         ctx->current_cert = cert;  | 
3583  | 0  |         ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
3584  | 0  |         return -1;  | 
3585  | 0  |     }  | 
3586  |  |  | 
3587  | 0  |     if (done) { | 
3588  |  |         /* Fail early, TA-based success is not possible */  | 
3589  | 0  |         if (!check_leaf_suiteb(ctx, cert))  | 
3590  | 0  |             return 0;  | 
3591  | 0  |         return verify_cb_cert(ctx, cert, 0, X509_V_ERR_DANE_NO_MATCH);  | 
3592  | 0  |     }  | 
3593  |  |  | 
3594  |  |     /*  | 
3595  |  |      * Chain verification for usages 0/1/2.  TLSA record matching of depth > 0  | 
3596  |  |      * certificates happens in-line with building the rest of the chain.  | 
3597  |  |      */  | 
3598  | 0  |     return verify_chain(ctx);  | 
3599  | 0  | }  | 
3600  |  |  | 
3601  |  | /*  | 
3602  |  |  * Get trusted issuer, without duplicate suppression  | 
3603  |  |  * Returns -1 on internal error.  | 
3604  |  |  */  | 
3605  |  | static int get1_trusted_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *cert)  | 
3606  | 0  | { | 
3607  | 0  |     STACK_OF(X509) *saved_chain = ctx->chain;  | 
3608  | 0  |     int ok;  | 
3609  |  | 
  | 
3610  | 0  |     ctx->chain = NULL;  | 
3611  | 0  |     ok = ctx->get_issuer(issuer, ctx, cert);  | 
3612  | 0  |     ctx->chain = saved_chain;  | 
3613  |  | 
  | 
3614  | 0  |     return ok;  | 
3615  | 0  | }  | 
3616  |  |  | 
3617  |  | /*-  | 
3618  |  |  * Returns -1 on internal error.  | 
3619  |  |  * Sadly, returns 0 also on internal error in ctx->verify_cb().  | 
3620  |  |  */  | 
3621  |  | static int build_chain(X509_STORE_CTX *ctx)  | 
3622  | 0  | { | 
3623  | 0  |     SSL_DANE *dane = ctx->dane;  | 
3624  | 0  |     int num = sk_X509_num(ctx->chain);  | 
3625  | 0  |     STACK_OF(X509) *sk_untrusted = NULL;  | 
3626  | 0  |     unsigned int search;  | 
3627  | 0  |     int may_trusted = 0;  | 
3628  | 0  |     int may_alternate = 0;  | 
3629  | 0  |     int trust = X509_TRUST_UNTRUSTED;  | 
3630  | 0  |     int alt_untrusted = 0;  | 
3631  | 0  |     int max_depth;  | 
3632  | 0  |     int ok = 0;  | 
3633  | 0  |     int i;  | 
3634  |  |  | 
3635  |  |     /* Our chain starts with a single untrusted element. */  | 
3636  | 0  |     if (!ossl_assert(num == 1 && ctx->num_untrusted == num))  | 
3637  | 0  |         goto int_err;  | 
3638  |  |  | 
3639  | 0  | #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */  | 
3640  | 0  | #define S_DOTRUSTED   (1 << 1) /* Search trusted store */  | 
3641  | 0  | #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */  | 
3642  |  |     /*  | 
3643  |  |      * Set up search policy, untrusted if possible, trusted-first if enabled,  | 
3644  |  |      * which is the default.  | 
3645  |  |      * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the  | 
3646  |  |      * trust_store, otherwise we might look there first.  If not trusted-first,  | 
3647  |  |      * and alternate chains are not disabled, try building an alternate chain  | 
3648  |  |      * if no luck with untrusted first.  | 
3649  |  |      */  | 
3650  | 0  |     search = ctx->untrusted != NULL ? S_DOUNTRUSTED : 0;  | 
3651  | 0  |     if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) { | 
3652  | 0  |         if (search == 0 || (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) != 0)  | 
3653  | 0  |             search |= S_DOTRUSTED;  | 
3654  | 0  |         else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))  | 
3655  | 0  |             may_alternate = 1;  | 
3656  | 0  |         may_trusted = 1;  | 
3657  | 0  |     }  | 
3658  |  |  | 
3659  |  |     /* Initialize empty untrusted stack. */  | 
3660  | 0  |     if ((sk_untrusted = sk_X509_new_null()) == NULL) { | 
3661  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);  | 
3662  | 0  |         goto memerr;  | 
3663  | 0  |     }  | 
3664  |  |  | 
3665  |  |     /*  | 
3666  |  |      * If we got any "Cert(0) Full(0)" trust anchors from DNS, *prepend* them  | 
3667  |  |      * to our working copy of the untrusted certificate stack.  | 
3668  |  |      */  | 
3669  | 0  |     if (DANETLS_ENABLED(dane) && dane->certs != NULL  | 
3670  | 0  |         && !X509_add_certs(sk_untrusted, dane->certs, X509_ADD_FLAG_DEFAULT)) { | 
3671  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
3672  | 0  |         goto memerr;  | 
3673  | 0  |     }  | 
3674  |  |  | 
3675  |  |     /*  | 
3676  |  |      * Shallow-copy the stack of untrusted certificates (with TLS, this is  | 
3677  |  |      * typically the content of the peer's certificate message) so we can make  | 
3678  |  |      * multiple passes over it, while free to remove elements as we go.  | 
3679  |  |      */  | 
3680  | 0  |     if (!X509_add_certs(sk_untrusted, ctx->untrusted, X509_ADD_FLAG_DEFAULT)) { | 
3681  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);  | 
3682  | 0  |         goto memerr;  | 
3683  | 0  |     }  | 
3684  |  |  | 
3685  |  |     /*  | 
3686  |  |      * Still absurdly large, but arithmetically safe, a lower hard upper bound  | 
3687  |  |      * might be reasonable.  | 
3688  |  |      */  | 
3689  | 0  |     if (ctx->param->depth > INT_MAX / 2)  | 
3690  | 0  |         ctx->param->depth = INT_MAX / 2;  | 
3691  |  |  | 
3692  |  |     /*  | 
3693  |  |      * Try to extend the chain until we reach an ultimately trusted issuer.  | 
3694  |  |      * Build chains up to one longer the limit, later fail if we hit the limit,  | 
3695  |  |      * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.  | 
3696  |  |      */  | 
3697  | 0  |     max_depth = ctx->param->depth + 1;  | 
3698  |  | 
  | 
3699  | 0  |     while (search != 0) { | 
3700  | 0  |         X509 *curr, *issuer = NULL;  | 
3701  |  | 
  | 
3702  | 0  |         num = sk_X509_num(ctx->chain);  | 
3703  | 0  |         ctx->error_depth = num - 1;  | 
3704  |  |         /*  | 
3705  |  |          * Look in the trust store if enabled for first lookup, or we've run  | 
3706  |  |          * out of untrusted issuers and search here is not disabled.  When we  | 
3707  |  |          * reach the depth limit, we stop extending the chain, if by that point  | 
3708  |  |          * we've not found a trust anchor, any trusted chain would be too long.  | 
3709  |  |          *  | 
3710  |  |          * The error reported to the application verify callback is at the  | 
3711  |  |          * maximal valid depth with the current certificate equal to the last  | 
3712  |  |          * not ultimately-trusted issuer.  For example, with verify_depth = 0,  | 
3713  |  |          * the callback will report errors at depth=1 when the immediate issuer  | 
3714  |  |          * of the leaf certificate is not a trust anchor.  No attempt will be  | 
3715  |  |          * made to locate an issuer for that certificate, since such a chain  | 
3716  |  |          * would be a-priori too long.  | 
3717  |  |          */  | 
3718  | 0  |         if ((search & S_DOTRUSTED) != 0) { | 
3719  | 0  |             i = num;  | 
3720  | 0  |             if ((search & S_DOALTERNATE) != 0) { | 
3721  |  |                 /*  | 
3722  |  |                  * As high up the chain as we can, look for an alternative  | 
3723  |  |                  * trusted issuer of an untrusted certificate that currently  | 
3724  |  |                  * has an untrusted issuer.  We use the alt_untrusted variable  | 
3725  |  |                  * to track how far up the chain we find the first match.  It  | 
3726  |  |                  * is only if and when we find a match, that we prune the chain  | 
3727  |  |                  * and reset ctx->num_untrusted to the reduced count of  | 
3728  |  |                  * untrusted certificates.  While we're searching for such a  | 
3729  |  |                  * match (which may never be found), it is neither safe nor  | 
3730  |  |                  * wise to preemptively modify either the chain or  | 
3731  |  |                  * ctx->num_untrusted.  | 
3732  |  |                  *  | 
3733  |  |                  * Note, like ctx->num_untrusted, alt_untrusted is a count of  | 
3734  |  |                  * untrusted certificates, not a "depth".  | 
3735  |  |                  */  | 
3736  | 0  |                 i = alt_untrusted;  | 
3737  | 0  |             }  | 
3738  | 0  |             curr = sk_X509_value(ctx->chain, i - 1);  | 
3739  |  |  | 
3740  |  |             /* Note: get1_trusted_issuer() must be used even if self-signed. */  | 
3741  | 0  |             ok = num > max_depth ? 0 : get1_trusted_issuer(&issuer, ctx, curr);  | 
3742  |  | 
  | 
3743  | 0  |             if (ok < 0) { | 
3744  | 0  |                 trust = -1;  | 
3745  | 0  |                 ctx->error = X509_V_ERR_STORE_LOOKUP;  | 
3746  | 0  |                 break;  | 
3747  | 0  |             }  | 
3748  |  |  | 
3749  | 0  |             if (ok > 0) { | 
3750  | 0  |                 int self_signed = X509_self_signed(curr, 0);  | 
3751  |  | 
  | 
3752  | 0  |                 if (self_signed < 0) { | 
3753  | 0  |                     X509_free(issuer);  | 
3754  | 0  |                     goto int_err;  | 
3755  | 0  |                 }  | 
3756  |  |                 /*  | 
3757  |  |                  * Alternative trusted issuer for a mid-chain untrusted cert?  | 
3758  |  |                  * Pop the untrusted cert's successors and retry.  We might now  | 
3759  |  |                  * be able to complete a valid chain via the trust store.  Note  | 
3760  |  |                  * that despite the current trust store match we might still  | 
3761  |  |                  * fail complete the chain to a suitable trust anchor, in which  | 
3762  |  |                  * case we may prune some more untrusted certificates and try  | 
3763  |  |                  * again.  Thus the S_DOALTERNATE bit may yet be turned on  | 
3764  |  |                  * again with an even shorter untrusted chain!  | 
3765  |  |                  *  | 
3766  |  |                  * If in the process we threw away our matching PKIX-TA trust  | 
3767  |  |                  * anchor, reset DANE trust.  We might find a suitable trusted  | 
3768  |  |                  * certificate among the ones from the trust store.  | 
3769  |  |                  */  | 
3770  | 0  |                 if ((search & S_DOALTERNATE) != 0) { | 
3771  | 0  |                     if (!ossl_assert(num > i && i > 0 && !self_signed)) { | 
3772  | 0  |                         X509_free(issuer);  | 
3773  | 0  |                         goto int_err;  | 
3774  | 0  |                     }  | 
3775  | 0  |                     search &= ~S_DOALTERNATE;  | 
3776  | 0  |                     for (; num > i; --num)  | 
3777  | 0  |                         X509_free(sk_X509_pop(ctx->chain));  | 
3778  | 0  |                     ctx->num_untrusted = num;  | 
3779  |  | 
  | 
3780  | 0  |                     if (DANETLS_ENABLED(dane) &&  | 
3781  | 0  |                         dane->mdpth >= ctx->num_untrusted) { | 
3782  | 0  |                         dane->mdpth = -1;  | 
3783  | 0  |                         X509_free(dane->mcert);  | 
3784  | 0  |                         dane->mcert = NULL;  | 
3785  | 0  |                     }  | 
3786  | 0  |                     if (DANETLS_ENABLED(dane) &&  | 
3787  | 0  |                         dane->pdpth >= ctx->num_untrusted)  | 
3788  | 0  |                         dane->pdpth = -1;  | 
3789  | 0  |                 }  | 
3790  |  |  | 
3791  | 0  |                 if (!self_signed) { /* untrusted not self-signed certificate */ | 
3792  |  |                     /* Grow the chain by trusted issuer */  | 
3793  | 0  |                     if (!sk_X509_push(ctx->chain, issuer)) { | 
3794  | 0  |                         X509_free(issuer);  | 
3795  | 0  |                         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);  | 
3796  | 0  |                         goto memerr;  | 
3797  | 0  |                     }  | 
3798  | 0  |                     if ((self_signed = X509_self_signed(issuer, 0)) < 0)  | 
3799  | 0  |                         goto int_err;  | 
3800  | 0  |                 } else { | 
3801  |  |                     /*  | 
3802  |  |                      * We have a self-signed untrusted cert that has the same  | 
3803  |  |                      * subject name (and perhaps keyid and/or serial number) as  | 
3804  |  |                      * a trust anchor.  We must have an exact match to avoid  | 
3805  |  |                      * possible impersonation via key substitution etc.  | 
3806  |  |                      */  | 
3807  | 0  |                     if (X509_cmp(curr, issuer) != 0) { | 
3808  |  |                         /* Self-signed untrusted mimic. */  | 
3809  | 0  |                         X509_free(issuer);  | 
3810  | 0  |                         ok = 0;  | 
3811  | 0  |                     } else { /* curr "==" issuer */ | 
3812  |  |                         /*  | 
3813  |  |                          * Replace self-signed untrusted certificate  | 
3814  |  |                          * by its trusted matching issuer.  | 
3815  |  |                          */  | 
3816  | 0  |                         X509_free(curr);  | 
3817  | 0  |                         ctx->num_untrusted = --num;  | 
3818  | 0  |                         (void)sk_X509_set(ctx->chain, num, issuer);  | 
3819  | 0  |                     }  | 
3820  | 0  |                 }  | 
3821  |  |  | 
3822  |  |                 /*  | 
3823  |  |                  * We've added a new trusted certificate to the chain, re-check  | 
3824  |  |                  * trust.  If not done, and not self-signed look deeper.  | 
3825  |  |                  * Whether or not we're doing "trusted first", we no longer  | 
3826  |  |                  * look for untrusted certificates from the peer's chain.  | 
3827  |  |                  *  | 
3828  |  |                  * At this point ctx->num_trusted and num must reflect the  | 
3829  |  |                  * correct number of untrusted certificates, since the DANE  | 
3830  |  |                  * logic in check_trust() depends on distinguishing CAs from  | 
3831  |  |                  * "the wire" from CAs from the trust store.  In particular, the  | 
3832  |  |                  * certificate at depth "num" should be the new trusted  | 
3833  |  |                  * certificate with ctx->num_untrusted <= num.  | 
3834  |  |                  */  | 
3835  | 0  |                 if (ok) { | 
3836  | 0  |                     if (!ossl_assert(ctx->num_untrusted <= num))  | 
3837  | 0  |                         goto int_err;  | 
3838  | 0  |                     search &= ~S_DOUNTRUSTED;  | 
3839  | 0  |                     trust = check_trust(ctx, num);  | 
3840  | 0  |                     if (trust != X509_TRUST_UNTRUSTED)  | 
3841  | 0  |                         break;  | 
3842  | 0  |                     if (!self_signed)  | 
3843  | 0  |                         continue;  | 
3844  | 0  |                 }  | 
3845  | 0  |             }  | 
3846  |  |  | 
3847  |  |             /*  | 
3848  |  |              * No dispositive decision, and either self-signed or no match, if  | 
3849  |  |              * we were doing untrusted-first, and alt-chains are not disabled,  | 
3850  |  |              * do that, by repeatedly losing one untrusted element at a time,  | 
3851  |  |              * and trying to extend the shorted chain.  | 
3852  |  |              */  | 
3853  | 0  |             if ((search & S_DOUNTRUSTED) == 0) { | 
3854  |  |                 /* Continue search for a trusted issuer of a shorter chain? */  | 
3855  | 0  |                 if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)  | 
3856  | 0  |                     continue;  | 
3857  |  |                 /* Still no luck and no fallbacks left? */  | 
3858  | 0  |                 if (!may_alternate || (search & S_DOALTERNATE) != 0 ||  | 
3859  | 0  |                     ctx->num_untrusted < 2)  | 
3860  | 0  |                     break;  | 
3861  |  |                 /* Search for a trusted issuer of a shorter chain */  | 
3862  | 0  |                 search |= S_DOALTERNATE;  | 
3863  | 0  |                 alt_untrusted = ctx->num_untrusted - 1;  | 
3864  | 0  |             }  | 
3865  | 0  |         }  | 
3866  |  |  | 
3867  |  |         /*  | 
3868  |  |          * Try to extend chain with peer-provided untrusted certificate  | 
3869  |  |          */  | 
3870  | 0  |         if ((search & S_DOUNTRUSTED) != 0) { | 
3871  | 0  |             num = sk_X509_num(ctx->chain);  | 
3872  | 0  |             if (!ossl_assert(num == ctx->num_untrusted))  | 
3873  | 0  |                 goto int_err;  | 
3874  | 0  |             curr = sk_X509_value(ctx->chain, num - 1);  | 
3875  | 0  |             issuer = (X509_self_signed(curr, 0) > 0 || num > max_depth) ?  | 
3876  | 0  |                 NULL : get0_best_issuer_sk(ctx, 0, 1 /* no_dup */, sk_untrusted, curr);  | 
3877  | 0  |             if (issuer == NULL) { | 
3878  |  |                 /*  | 
3879  |  |                  * Once we have reached a self-signed cert or num > max_depth  | 
3880  |  |                  * or can't find an issuer in the untrusted list we stop looking  | 
3881  |  |                  * there and start looking only in the trust store if enabled.  | 
3882  |  |                  */  | 
3883  | 0  |                 search &= ~S_DOUNTRUSTED;  | 
3884  | 0  |                 if (may_trusted)  | 
3885  | 0  |                     search |= S_DOTRUSTED;  | 
3886  | 0  |                 continue;  | 
3887  | 0  |             }  | 
3888  |  |  | 
3889  |  |             /* Drop this issuer from future consideration */  | 
3890  | 0  |             (void)sk_X509_delete_ptr(sk_untrusted, issuer);  | 
3891  |  |  | 
3892  |  |             /* Grow the chain by untrusted issuer */  | 
3893  | 0  |             if (!X509_add_cert(ctx->chain, issuer, X509_ADD_FLAG_UP_REF))  | 
3894  | 0  |                 goto int_err;  | 
3895  |  |  | 
3896  | 0  |             ++ctx->num_untrusted;  | 
3897  |  |  | 
3898  |  |             /* Check for DANE-TA trust of the topmost untrusted certificate. */  | 
3899  | 0  |             trust = check_dane_issuer(ctx, ctx->num_untrusted - 1);  | 
3900  | 0  |             if (trust == X509_TRUST_TRUSTED || trust == X509_TRUST_REJECTED)  | 
3901  | 0  |                 break;  | 
3902  | 0  |         }  | 
3903  | 0  |     }  | 
3904  | 0  |     sk_X509_free(sk_untrusted);  | 
3905  |  | 
  | 
3906  | 0  |     if (trust < 0) /* internal error */  | 
3907  | 0  |         return trust;  | 
3908  |  |  | 
3909  |  |     /*  | 
3910  |  |      * Last chance to make a trusted chain, either bare DANE-TA public-key  | 
3911  |  |      * signers, or else direct leaf PKIX trust.  | 
3912  |  |      */  | 
3913  | 0  |     num = sk_X509_num(ctx->chain);  | 
3914  | 0  |     if (num <= max_depth) { | 
3915  | 0  |         if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane))  | 
3916  | 0  |             trust = check_dane_pkeys(ctx);  | 
3917  | 0  |         if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted)  | 
3918  | 0  |             trust = check_trust(ctx, num);  | 
3919  | 0  |     }  | 
3920  |  | 
  | 
3921  | 0  |     switch (trust) { | 
3922  | 0  |     case X509_TRUST_TRUSTED:  | 
3923  | 0  |         return 1;  | 
3924  | 0  |     case X509_TRUST_REJECTED:  | 
3925  |  |         /* Callback already issued */  | 
3926  | 0  |         return 0;  | 
3927  | 0  |     case X509_TRUST_UNTRUSTED:  | 
3928  | 0  |     default:  | 
3929  | 0  |         switch (ctx->error) { | 
3930  | 0  |         case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:  | 
3931  | 0  |         case X509_V_ERR_CERT_NOT_YET_VALID:  | 
3932  | 0  |         case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:  | 
3933  | 0  |         case X509_V_ERR_CERT_HAS_EXPIRED:  | 
3934  | 0  |             return 0; /* Callback already done by ossl_x509_check_cert_time() */  | 
3935  | 0  |         default: /* A preliminary error has become final */  | 
3936  | 0  |             return verify_cb_cert(ctx, NULL, num - 1, ctx->error);  | 
3937  | 0  |         case X509_V_OK:  | 
3938  | 0  |             break;  | 
3939  | 0  |         }  | 
3940  | 0  |         CB_FAIL_IF(num > max_depth,  | 
3941  | 0  |                    ctx, NULL, num - 1, X509_V_ERR_CERT_CHAIN_TOO_LONG);  | 
3942  | 0  |         CB_FAIL_IF(DANETLS_ENABLED(dane)  | 
3943  | 0  |                        && (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0),  | 
3944  | 0  |                    ctx, NULL, num - 1, X509_V_ERR_DANE_NO_MATCH);  | 
3945  | 0  |         if (X509_self_signed(sk_X509_value(ctx->chain, num - 1), 0) > 0)  | 
3946  | 0  |             return verify_cb_cert(ctx, NULL, num - 1,  | 
3947  | 0  |                                   num == 1  | 
3948  | 0  |                                   ? X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT  | 
3949  | 0  |                                   : X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN);  | 
3950  | 0  |         return verify_cb_cert(ctx, NULL, num - 1,  | 
3951  | 0  |                               ctx->num_untrusted < num  | 
3952  | 0  |                               ? X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT  | 
3953  | 0  |                               : X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);  | 
3954  | 0  |     }  | 
3955  |  |  | 
3956  | 0  |  int_err:  | 
3957  | 0  |     ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);  | 
3958  | 0  |     ctx->error = X509_V_ERR_UNSPECIFIED;  | 
3959  | 0  |     sk_X509_free(sk_untrusted);  | 
3960  | 0  |     return -1;  | 
3961  |  |  | 
3962  | 0  |  memerr:  | 
3963  | 0  |     ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
3964  | 0  |     sk_X509_free(sk_untrusted);  | 
3965  | 0  |     return -1;  | 
3966  | 0  | }  | 
3967  |  |  | 
3968  |  | STACK_OF(X509) *X509_build_chain(X509 *target, STACK_OF(X509) *certs,  | 
3969  |  |                                  X509_STORE *store, int with_self_signed,  | 
3970  |  |                                  OSSL_LIB_CTX *libctx, const char *propq)  | 
3971  | 0  | { | 
3972  | 0  |     int finish_chain = store != NULL;  | 
3973  | 0  |     X509_STORE_CTX *ctx;  | 
3974  | 0  |     int flags = X509_ADD_FLAG_UP_REF;  | 
3975  | 0  |     STACK_OF(X509) *result = NULL;  | 
3976  |  | 
  | 
3977  | 0  |     if (target == NULL) { | 
3978  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
3979  | 0  |         return NULL;  | 
3980  | 0  |     }  | 
3981  |  |  | 
3982  | 0  |     if ((ctx = X509_STORE_CTX_new_ex(libctx, propq)) == NULL)  | 
3983  | 0  |         return NULL;  | 
3984  | 0  |     if (!X509_STORE_CTX_init(ctx, store, target, finish_chain ? certs : NULL))  | 
3985  | 0  |         goto err;  | 
3986  | 0  |     if (!finish_chain)  | 
3987  | 0  |         X509_STORE_CTX_set0_trusted_stack(ctx, certs);  | 
3988  | 0  |     if (!ossl_x509_add_cert_new(&ctx->chain, target, X509_ADD_FLAG_UP_REF)) { | 
3989  | 0  |         ctx->error = X509_V_ERR_OUT_OF_MEM;  | 
3990  | 0  |         goto err;  | 
3991  | 0  |     }  | 
3992  | 0  |     ctx->num_untrusted = 1;  | 
3993  |  | 
  | 
3994  | 0  |     if (!build_chain(ctx) && finish_chain)  | 
3995  | 0  |         goto err;  | 
3996  |  |  | 
3997  |  |     /* result list to store the up_ref'ed certificates */  | 
3998  | 0  |     if (sk_X509_num(ctx->chain) > 1 && !with_self_signed)  | 
3999  | 0  |         flags |= X509_ADD_FLAG_NO_SS;  | 
4000  | 0  |     if (!ossl_x509_add_certs_new(&result, ctx->chain, flags)) { | 
4001  | 0  |         sk_X509_free(result);  | 
4002  | 0  |         result = NULL;  | 
4003  | 0  |     }  | 
4004  |  | 
  | 
4005  | 0  |  err:  | 
4006  | 0  |     X509_STORE_CTX_free(ctx);  | 
4007  | 0  |     return result;  | 
4008  | 0  | }  | 
4009  |  |  | 
4010  |  | /*  | 
4011  |  |  * note that there's a corresponding minbits_table in ssl/ssl_cert.c  | 
4012  |  |  * in ssl_get_security_level_bits that's used for selection of DH parameters  | 
4013  |  |  */  | 
4014  |  | static const int minbits_table[] = { 80, 112, 128, 192, 256 }; | 
4015  |  | static const int NUM_AUTH_LEVELS = OSSL_NELEM(minbits_table);  | 
4016  |  |  | 
4017  |  | /*-  | 
4018  |  |  * Check whether the given public key meets the security level of `ctx`.  | 
4019  |  |  * Returns 1 on success, 0 otherwise.  | 
4020  |  |  */  | 
4021  |  | static int check_key_level(X509_STORE_CTX *ctx, EVP_PKEY *pkey)  | 
4022  | 0  | { | 
4023  | 0  |     int level = ctx->param->auth_level;  | 
4024  |  |  | 
4025  |  |     /*  | 
4026  |  |      * At security level zero, return without checking for a supported public  | 
4027  |  |      * key type.  Some engines support key types not understood outside the  | 
4028  |  |      * engine, and we only need to understand the key when enforcing a security  | 
4029  |  |      * floor.  | 
4030  |  |      */  | 
4031  | 0  |     if (level <= 0)  | 
4032  | 0  |         return 1;  | 
4033  |  |  | 
4034  |  |     /* Unsupported or malformed keys are not secure */  | 
4035  | 0  |     if (pkey == NULL)  | 
4036  | 0  |         return 0;  | 
4037  |  |  | 
4038  | 0  |     if (level > NUM_AUTH_LEVELS)  | 
4039  | 0  |         level = NUM_AUTH_LEVELS;  | 
4040  |  | 
  | 
4041  | 0  |     return EVP_PKEY_get_security_bits(pkey) >= minbits_table[level - 1];  | 
4042  | 0  | }  | 
4043  |  |  | 
4044  |  | /*-  | 
4045  |  |  * Check whether the public key of `cert` meets the security level of `ctx`.  | 
4046  |  |  * Returns 1 on success, 0 otherwise.  | 
4047  |  |  */  | 
4048  |  | static int check_cert_key_level(X509_STORE_CTX *ctx, X509 *cert)  | 
4049  | 0  | { | 
4050  | 0  |     return check_key_level(ctx, X509_get0_pubkey(cert));  | 
4051  | 0  | }  | 
4052  |  |  | 
4053  |  | /*-  | 
4054  |  |  * Check whether the public key of ``cert`` does not use explicit params  | 
4055  |  |  * for an elliptic curve.  | 
4056  |  |  *  | 
4057  |  |  * Returns 1 on success, 0 if check fails, -1 for other errors.  | 
4058  |  |  */  | 
4059  |  | static int check_curve(X509 *cert)  | 
4060  | 0  | { | 
4061  | 0  |     EVP_PKEY *pkey = X509_get0_pubkey(cert);  | 
4062  | 0  |     int ret, val;  | 
4063  |  |  | 
4064  |  |     /* Unsupported or malformed key */  | 
4065  | 0  |     if (pkey == NULL)  | 
4066  | 0  |         return -1;  | 
4067  | 0  |     if (EVP_PKEY_get_id(pkey) != EVP_PKEY_EC)  | 
4068  | 0  |         return 1;  | 
4069  |  |  | 
4070  | 0  |     ret =  | 
4071  | 0  |         EVP_PKEY_get_int_param(pkey,  | 
4072  | 0  |                                OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,  | 
4073  | 0  |                                &val);  | 
4074  | 0  |     return ret == 1 ? !val : -1;  | 
4075  | 0  | }  | 
4076  |  |  | 
4077  |  | /*-  | 
4078  |  |  * Check whether the signature digest algorithm of ``cert`` meets the security  | 
4079  |  |  * level of ``ctx``.  Should not be checked for trust anchors (whether  | 
4080  |  |  * self-signed or otherwise).  | 
4081  |  |  *  | 
4082  |  |  * Returns 1 on success, 0 otherwise.  | 
4083  |  |  */  | 
4084  |  | static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert)  | 
4085  | 0  | { | 
4086  | 0  |     int secbits = -1;  | 
4087  | 0  |     int level = ctx->param->auth_level;  | 
4088  |  | 
  | 
4089  | 0  |     if (level <= 0)  | 
4090  | 0  |         return 1;  | 
4091  | 0  |     if (level > NUM_AUTH_LEVELS)  | 
4092  | 0  |         level = NUM_AUTH_LEVELS;  | 
4093  |  | 
  | 
4094  | 0  |     if (!X509_get_signature_info(cert, NULL, NULL, &secbits, NULL))  | 
4095  | 0  |         return 0;  | 
4096  |  |  | 
4097  | 0  |     return secbits >= minbits_table[level - 1];  | 
4098  | 0  | }  |