/src/openssl36/crypto/x509/v3_purp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include "internal/numbers.h" |
13 | | #include <openssl/x509v3.h> |
14 | | #include <openssl/x509_vfy.h> |
15 | | #include "crypto/x509.h" |
16 | | #include "internal/tsan_assist.h" |
17 | | #include "x509_local.h" |
18 | | |
19 | | static int check_ssl_ca(const X509 *x); |
20 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
21 | | int non_leaf); |
22 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
23 | | int non_leaf); |
24 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
25 | | int non_leaf); |
26 | | static int purpose_smime(const X509 *x, int non_leaf); |
27 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
28 | | int non_leaf); |
29 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
30 | | int non_leaf); |
31 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
32 | | int non_leaf); |
33 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
34 | | int non_leaf); |
35 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
36 | | int non_leaf); |
37 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
38 | | int non_leaf); |
39 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
40 | | int non_leaf); |
41 | | |
42 | | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b); |
43 | | static void xptable_free(X509_PURPOSE *p); |
44 | | |
45 | | /* note that the id must be unique and for the standard entries == idx + 1 */ |
46 | | static X509_PURPOSE xstandard[] = { |
47 | | { X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, |
48 | | check_purpose_ssl_client, "SSL client", "sslclient", NULL }, |
49 | | { X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
50 | | check_purpose_ssl_server, "SSL server", "sslserver", NULL }, |
51 | | { X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
52 | | check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL }, |
53 | | { X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, |
54 | | "S/MIME signing", "smimesign", NULL }, |
55 | | { X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, |
56 | | check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL }, |
57 | | { X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, |
58 | | "CRL signing", "crlsign", NULL }, |
59 | | { X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check_purpose, |
60 | | "Any Purpose", "any", |
61 | | NULL }, |
62 | | { X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, check_purpose_ocsp_helper, |
63 | | "OCSP helper", "ocsphelper", NULL }, |
64 | | { X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, |
65 | | check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign", |
66 | | NULL }, |
67 | | { X509_PURPOSE_CODE_SIGN, X509_TRUST_OBJECT_SIGN, 0, |
68 | | check_purpose_code_sign, "Code signing", "codesign", |
69 | | NULL }, |
70 | | }; |
71 | | |
72 | 823 | #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard) |
73 | | |
74 | | /* the id must be unique, but there may be gaps and maybe table is not sorted */ |
75 | | static STACK_OF(X509_PURPOSE) *xptable = NULL; |
76 | | |
77 | | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b) |
78 | 0 | { |
79 | 0 | return (*a)->purpose - (*b)->purpose; |
80 | 0 | } |
81 | | |
82 | | /* |
83 | | * As much as I'd like to make X509_check_purpose use a "const" X509* I really |
84 | | * can't because it does recalculate hashes and do other non-const things. |
85 | | * If id == -1 it just calls x509v3_cache_extensions() for its side-effect. |
86 | | * Returns 1 on success, 0 if x does not allow purpose, -1 on (internal) error. |
87 | | */ |
88 | | int X509_check_purpose(X509 *x, int id, int non_leaf) |
89 | 181k | { |
90 | 181k | int idx; |
91 | 181k | const X509_PURPOSE *pt; |
92 | | |
93 | 181k | if (!ossl_x509v3_cache_extensions(x)) |
94 | 45.3k | return -1; |
95 | 136k | if (id == -1) |
96 | 136k | return 1; |
97 | | |
98 | 499 | idx = X509_PURPOSE_get_by_id(id); |
99 | 499 | if (idx == -1) |
100 | 0 | return -1; |
101 | 499 | pt = X509_PURPOSE_get0(idx); |
102 | 499 | return pt->check_purpose(pt, x, non_leaf); |
103 | 499 | } |
104 | | |
105 | | /* resets to default (any) purpose if purpose == X509_PURPOSE_DEFAULT_ANY (0) */ |
106 | | int X509_PURPOSE_set(int *p, int purpose) |
107 | 0 | { |
108 | 0 | if (purpose != X509_PURPOSE_DEFAULT_ANY && X509_PURPOSE_get_by_id(purpose) == -1) { |
109 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE); |
110 | 0 | return 0; |
111 | 0 | } |
112 | 0 | *p = purpose; |
113 | 0 | return 1; |
114 | 0 | } |
115 | | |
116 | | int X509_PURPOSE_get_count(void) |
117 | 0 | { |
118 | 0 | if (!xptable) |
119 | 0 | return X509_PURPOSE_COUNT; |
120 | 0 | return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT; |
121 | 0 | } |
122 | | |
123 | | /* find smallest identifier not yet taken - note there might be gaps */ |
124 | | int X509_PURPOSE_get_unused_id(ossl_unused OSSL_LIB_CTX *libctx) |
125 | 0 | { |
126 | 0 | int id = X509_PURPOSE_MAX + 1; |
127 | |
|
128 | 0 | while (X509_PURPOSE_get_by_id(id) != -1) |
129 | 0 | id++; |
130 | 0 | return id; /* is guaranteed to be unique and > X509_PURPOSE_MAX and != 0 */ |
131 | 0 | } |
132 | | |
133 | | X509_PURPOSE *X509_PURPOSE_get0(int idx) |
134 | 71.5k | { |
135 | 71.5k | if (idx < 0) |
136 | 70.6k | return NULL; |
137 | 823 | if (idx < (int)X509_PURPOSE_COUNT) |
138 | 823 | return xstandard + idx; |
139 | 0 | return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT); |
140 | 823 | } |
141 | | |
142 | | int X509_PURPOSE_get_by_sname(const char *sname) |
143 | 0 | { |
144 | 0 | int i; |
145 | 0 | X509_PURPOSE *xptmp; |
146 | |
|
147 | 0 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { |
148 | 0 | xptmp = X509_PURPOSE_get0(i); |
149 | 0 | if (strcmp(xptmp->sname, sname) == 0) |
150 | 0 | return i; |
151 | 0 | } |
152 | 0 | return -1; |
153 | 0 | } |
154 | | |
155 | | /* Returns -1 on error, else an index => 0 in standard/extended purpose table */ |
156 | | int X509_PURPOSE_get_by_id(int purpose) |
157 | 71.5k | { |
158 | 71.5k | X509_PURPOSE tmp; |
159 | 71.5k | int idx; |
160 | | |
161 | 71.5k | if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX) |
162 | 823 | return purpose - X509_PURPOSE_MIN; |
163 | 70.6k | if (xptable == NULL) |
164 | 70.6k | return -1; |
165 | 0 | tmp.purpose = purpose; |
166 | 0 | idx = sk_X509_PURPOSE_find(xptable, &tmp); |
167 | 0 | if (idx < 0) |
168 | 0 | return -1; |
169 | 0 | return idx + X509_PURPOSE_COUNT; |
170 | 0 | } |
171 | | |
172 | | /* |
173 | | * Add purpose entry identified by |sname|. |id| must be >= X509_PURPOSE_MIN. |
174 | | * May also be used to modify existing entry, including changing its id. |
175 | | */ |
176 | | int X509_PURPOSE_add(int id, int trust, int flags, |
177 | | int (*ck)(const X509_PURPOSE *, const X509 *, int), |
178 | | const char *name, const char *sname, void *arg) |
179 | 0 | { |
180 | 0 | int old_id = 0; |
181 | 0 | int idx; |
182 | 0 | X509_PURPOSE *ptmp; |
183 | |
|
184 | 0 | if (id < X509_PURPOSE_MIN) { |
185 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | if (trust < X509_TRUST_DEFAULT || name == NULL || sname == NULL || ck == NULL) { |
189 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_INVALID_ARGUMENT); |
190 | 0 | return 0; |
191 | 0 | } |
192 | | |
193 | | /* This is set according to what we change: application can't set it */ |
194 | 0 | flags &= ~X509_PURPOSE_DYNAMIC; |
195 | | /* This will always be set for application modified trust entries */ |
196 | 0 | flags |= X509_PURPOSE_DYNAMIC_NAME; |
197 | | |
198 | | /* Get existing entry if any */ |
199 | 0 | idx = X509_PURPOSE_get_by_sname(sname); |
200 | 0 | if (idx == -1) { /* Need a new entry */ |
201 | 0 | if (X509_PURPOSE_get_by_id(id) != -1) { |
202 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE); |
203 | 0 | return 0; |
204 | 0 | } |
205 | 0 | if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) |
206 | 0 | return 0; |
207 | 0 | ptmp->flags = X509_PURPOSE_DYNAMIC; |
208 | 0 | } else { |
209 | 0 | ptmp = X509_PURPOSE_get0(idx); |
210 | 0 | old_id = ptmp->purpose; |
211 | 0 | if (id != old_id && X509_PURPOSE_get_by_id(id) != -1) { |
212 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE); |
213 | 0 | return 0; |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | | /* OPENSSL_free existing name if dynamic */ |
218 | 0 | if ((ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { |
219 | 0 | OPENSSL_free(ptmp->name); |
220 | 0 | OPENSSL_free(ptmp->sname); |
221 | 0 | } |
222 | | /* Dup supplied name */ |
223 | 0 | ptmp->name = OPENSSL_strdup(name); |
224 | 0 | ptmp->sname = OPENSSL_strdup(sname); |
225 | 0 | if (ptmp->name == NULL || ptmp->sname == NULL) |
226 | 0 | goto err; |
227 | | /* Keep the dynamic flag of existing entry */ |
228 | 0 | ptmp->flags &= X509_PURPOSE_DYNAMIC; |
229 | | /* Set all other flags */ |
230 | 0 | ptmp->flags |= flags; |
231 | |
|
232 | 0 | ptmp->purpose = id; |
233 | 0 | ptmp->trust = trust; |
234 | 0 | ptmp->check_purpose = ck; |
235 | 0 | ptmp->usr_data = arg; |
236 | | |
237 | | /* If its a new entry manage the dynamic table */ |
238 | 0 | if (idx == -1) { |
239 | 0 | if (xptable == NULL |
240 | 0 | && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) { |
241 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
242 | 0 | goto err; |
243 | 0 | } |
244 | 0 | if (!sk_X509_PURPOSE_push(xptable, ptmp)) { |
245 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
246 | 0 | goto err; |
247 | 0 | } |
248 | 0 | } else if (id != old_id) { |
249 | | /* on changing existing entry id, make sure to reset 'sorted' */ |
250 | 0 | (void)sk_X509_PURPOSE_set(xptable, idx, ptmp); |
251 | 0 | } |
252 | 0 | return 1; |
253 | 0 | err: |
254 | 0 | if (idx == -1) { |
255 | 0 | OPENSSL_free(ptmp->name); |
256 | 0 | OPENSSL_free(ptmp->sname); |
257 | 0 | OPENSSL_free(ptmp); |
258 | 0 | } |
259 | 0 | return 0; |
260 | 0 | } |
261 | | |
262 | | static void xptable_free(X509_PURPOSE *p) |
263 | 0 | { |
264 | 0 | if (p == NULL) |
265 | 0 | return; |
266 | 0 | if ((p->flags & X509_PURPOSE_DYNAMIC) != 0) { |
267 | 0 | if ((p->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { |
268 | 0 | OPENSSL_free(p->name); |
269 | 0 | OPENSSL_free(p->sname); |
270 | 0 | } |
271 | 0 | OPENSSL_free(p); |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | | void X509_PURPOSE_cleanup(void) |
276 | 0 | { |
277 | 0 | sk_X509_PURPOSE_pop_free(xptable, xptable_free); |
278 | 0 | xptable = NULL; |
279 | 0 | } |
280 | | |
281 | | int X509_PURPOSE_get_id(const X509_PURPOSE *xp) |
282 | 0 | { |
283 | 0 | return xp->purpose; |
284 | 0 | } |
285 | | |
286 | | char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp) |
287 | 0 | { |
288 | 0 | return xp->name; |
289 | 0 | } |
290 | | |
291 | | char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) |
292 | 0 | { |
293 | 0 | return xp->sname; |
294 | 0 | } |
295 | | |
296 | | int X509_PURPOSE_get_trust(const X509_PURPOSE *xp) |
297 | 0 | { |
298 | 0 | return xp->trust; |
299 | 0 | } |
300 | | |
301 | | static int nid_cmp(const int *a, const int *b) |
302 | 132k | { |
303 | 132k | return *a - *b; |
304 | 132k | } |
305 | | |
306 | | DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid); |
307 | | IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid); |
308 | | |
309 | | int X509_supported_extension(X509_EXTENSION *ex) |
310 | 58.2k | { |
311 | | /* |
312 | | * This table is a list of the NIDs of supported extensions: that is |
313 | | * those which are used by the verify process. If an extension is |
314 | | * critical and doesn't appear in this list then the verify process will |
315 | | * normally reject the certificate. The list must be kept in numerical |
316 | | * order because it will be searched using bsearch. |
317 | | */ |
318 | 58.2k | static const int supported_nids[] = { |
319 | 58.2k | NID_netscape_cert_type, /* 71 */ |
320 | 58.2k | NID_key_usage, /* 83 */ |
321 | 58.2k | NID_subject_alt_name, /* 85 */ |
322 | 58.2k | NID_basic_constraints, /* 87 */ |
323 | 58.2k | NID_certificate_policies, /* 89 */ |
324 | 58.2k | NID_crl_distribution_points, /* 103 */ |
325 | 58.2k | NID_ext_key_usage, /* 126 */ |
326 | 58.2k | #ifndef OPENSSL_NO_RFC3779 |
327 | 58.2k | NID_sbgp_ipAddrBlock, /* 290 */ |
328 | 58.2k | NID_sbgp_autonomousSysNum, /* 291 */ |
329 | 58.2k | #endif |
330 | 58.2k | NID_id_pkix_OCSP_noCheck, /* 369 */ |
331 | 58.2k | NID_policy_constraints, /* 401 */ |
332 | 58.2k | NID_proxyCertInfo, /* 663 */ |
333 | 58.2k | NID_name_constraints, /* 666 */ |
334 | 58.2k | NID_policy_mappings, /* 747 */ |
335 | 58.2k | NID_inhibit_any_policy /* 748 */ |
336 | 58.2k | }; |
337 | | |
338 | 58.2k | int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
339 | | |
340 | 58.2k | if (ex_nid == NID_undef) |
341 | 5.20k | return 0; |
342 | | |
343 | 53.0k | if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids))) |
344 | 51.6k | return 1; |
345 | 1.37k | return 0; |
346 | 53.0k | } |
347 | | |
348 | | /* |
349 | | * The full name of a nameRelativeToCRLIssuer distribution point is not |
350 | | * computed here. Doing so for every parsed certificate cost an |
351 | | * X509_NAME_dup() of the issuer name per relative distribution point, |
352 | | * which a certificate with many such entries could turn into hundreds of |
353 | | * megabytes of heap on a plain TLS handshake, while the result is only |
354 | | * needed when a CRL is actually being matched against the certificate. |
355 | | * That name is now built on demand in the CRL checking code instead. |
356 | | */ |
357 | | static int setup_dp(DIST_POINT *dp) |
358 | 2.50k | { |
359 | 2.50k | if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) { |
360 | 50 | ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT); |
361 | 50 | return 0; |
362 | 50 | } |
363 | 2.45k | if (dp->reasons != NULL) { |
364 | 36 | if (dp->reasons->length > 0) |
365 | 34 | dp->dp_reasons = dp->reasons->data[0]; |
366 | 36 | if (dp->reasons->length > 1) |
367 | 12 | dp->dp_reasons |= (dp->reasons->data[1] << 8); |
368 | 36 | dp->dp_reasons &= CRLDP_ALL_REASONS; |
369 | 2.41k | } else { |
370 | 2.41k | dp->dp_reasons = CRLDP_ALL_REASONS; |
371 | 2.41k | } |
372 | 2.45k | return 1; |
373 | 2.50k | } |
374 | | |
375 | | /* Return 1 on success, 0 on error. */ |
376 | | static int setup_crldp(X509 *x) |
377 | 126k | { |
378 | 126k | int i; |
379 | | |
380 | 126k | x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL); |
381 | 126k | if (x->crldp == NULL && i != -1) |
382 | 2.06k | return 0; |
383 | | |
384 | 127k | for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { |
385 | 2.50k | if (!setup_dp(sk_DIST_POINT_value(x->crldp, i))) |
386 | 50 | return 0; |
387 | 2.50k | } |
388 | 124k | return 1; |
389 | 124k | } |
390 | | |
391 | | /* Check that issuer public key algorithm matches subject signature algorithm */ |
392 | | static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject) |
393 | 88.5k | { |
394 | 88.5k | int subj_sig_nid; |
395 | | |
396 | 88.5k | if (issuer_key == NULL) |
397 | 6.97k | return X509_V_ERR_NO_ISSUER_PUBLIC_KEY; |
398 | 81.6k | if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm), |
399 | 81.6k | NULL, &subj_sig_nid) |
400 | 81.6k | == 0) |
401 | 7.82k | return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
402 | 73.8k | if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid)) |
403 | 1.86k | || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss)) |
404 | 73.2k | return X509_V_OK; |
405 | 578 | return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH; |
406 | 73.8k | } |
407 | | |
408 | 14.2k | #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS) |
409 | | #define ku_reject(x, usage) \ |
410 | 17.6k | (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0) |
411 | | #define xku_reject(x, usage) \ |
412 | 0 | (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0) |
413 | | #define ns_reject(x, usage) \ |
414 | 0 | (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0) |
415 | | |
416 | | /* |
417 | | * Cache info on various X.509v3 extensions and further derived information, |
418 | | * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields. |
419 | | * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags. |
420 | | * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully. |
421 | | * Set EXFLAG_INVALID and return 0 in case the certificate is invalid. |
422 | | */ |
423 | | int ossl_x509v3_cache_extensions(X509 *x) |
424 | 200k | { |
425 | 200k | BASIC_CONSTRAINTS *bs; |
426 | 200k | PROXY_CERT_INFO_EXTENSION *pci; |
427 | 200k | ASN1_BIT_STRING *usage; |
428 | 200k | ASN1_BIT_STRING *ns; |
429 | 200k | EXTENDED_KEY_USAGE *extusage; |
430 | 200k | int i; |
431 | 200k | int res; |
432 | | |
433 | 200k | #ifdef tsan_ld_acq |
434 | | /* Fast lock-free check, see end of the function for details. */ |
435 | 200k | if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached)) |
436 | 123k | return (x->ex_flags & EXFLAG_INVALID) == 0; |
437 | 77.2k | #endif |
438 | | |
439 | 77.2k | if (!CRYPTO_THREAD_write_lock(x->lock)) |
440 | 0 | return 0; |
441 | 77.2k | if ((x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */ |
442 | 0 | CRYPTO_THREAD_unlock(x->lock); |
443 | 0 | return (x->ex_flags & EXFLAG_INVALID) == 0; |
444 | 0 | } |
445 | | |
446 | 77.2k | ERR_set_mark(); |
447 | | |
448 | | /* Cache the SHA1 digest of the cert */ |
449 | 77.2k | if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL)) |
450 | 0 | x->ex_flags |= EXFLAG_NO_FINGERPRINT; |
451 | | |
452 | | /* V1 should mean no extensions ... */ |
453 | 77.2k | if (X509_get_version(x) == X509_VERSION_1) |
454 | 1.80k | x->ex_flags |= EXFLAG_V1; |
455 | | |
456 | | /* Handle basic constraints */ |
457 | 77.2k | x->ex_pathlen = -1; |
458 | 77.2k | if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) { |
459 | 56.7k | if (bs->ca) |
460 | 11.6k | x->ex_flags |= EXFLAG_CA; |
461 | 56.7k | if (bs->pathlen != NULL) { |
462 | | /* |
463 | | * The error case !bs->ca is checked by check_chain() |
464 | | * in case ctx->param->flags & X509_V_FLAG_X509_STRICT |
465 | | */ |
466 | 1.65k | if (bs->pathlen->type == V_ASN1_NEG_INTEGER) { |
467 | 74 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN); |
468 | 74 | x->ex_flags |= EXFLAG_INVALID; |
469 | 1.57k | } else { |
470 | 1.57k | x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen); |
471 | 1.57k | } |
472 | 1.65k | } |
473 | 56.7k | BASIC_CONSTRAINTS_free(bs); |
474 | 56.7k | x->ex_flags |= EXFLAG_BCONS; |
475 | 56.7k | } else if (i != -1) { |
476 | 5.68k | x->ex_flags |= EXFLAG_INVALID; |
477 | 5.68k | } |
478 | | |
479 | | /* Handle proxy certificates */ |
480 | 77.2k | if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) { |
481 | 258 | if ((x->ex_flags & EXFLAG_CA) != 0 |
482 | 253 | || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0 |
483 | 192 | || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) { |
484 | 104 | x->ex_flags |= EXFLAG_INVALID; |
485 | 104 | } |
486 | 258 | if (pci->pcPathLengthConstraint != NULL) |
487 | 127 | x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint); |
488 | 131 | else |
489 | 131 | x->ex_pcpathlen = -1; |
490 | 258 | PROXY_CERT_INFO_EXTENSION_free(pci); |
491 | 258 | x->ex_flags |= EXFLAG_PROXY; |
492 | 76.9k | } else if (i != -1) { |
493 | 118 | x->ex_flags |= EXFLAG_INVALID; |
494 | 118 | } |
495 | | |
496 | | /* Handle (basic) key usage */ |
497 | 77.2k | if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) { |
498 | 41.9k | x->ex_kusage = 0; |
499 | 41.9k | if (usage->length > 0) { |
500 | 41.8k | x->ex_kusage = usage->data[0]; |
501 | 41.8k | if (usage->length > 1) |
502 | 51 | x->ex_kusage |= usage->data[1] << 8; |
503 | 41.8k | } |
504 | 41.9k | x->ex_flags |= EXFLAG_KUSAGE; |
505 | 41.9k | ASN1_BIT_STRING_free(usage); |
506 | | /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */ |
507 | 41.9k | if (x->ex_kusage == 0) { |
508 | 626 | ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE); |
509 | 626 | x->ex_flags |= EXFLAG_INVALID; |
510 | 626 | } |
511 | 41.9k | } else if (i != -1) { |
512 | 1.18k | x->ex_flags |= EXFLAG_INVALID; |
513 | 1.18k | } |
514 | | |
515 | | /* Handle extended key usage */ |
516 | 77.2k | x->ex_xkusage = 0; |
517 | 77.2k | if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) { |
518 | 41.4k | x->ex_flags |= EXFLAG_XKUSAGE; |
519 | 83.1k | for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { |
520 | 41.6k | switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { |
521 | 39.7k | case NID_server_auth: |
522 | 39.7k | x->ex_xkusage |= XKU_SSL_SERVER; |
523 | 39.7k | break; |
524 | 114 | case NID_client_auth: |
525 | 114 | x->ex_xkusage |= XKU_SSL_CLIENT; |
526 | 114 | break; |
527 | 60 | case NID_email_protect: |
528 | 60 | x->ex_xkusage |= XKU_SMIME; |
529 | 60 | break; |
530 | 40 | case NID_code_sign: |
531 | 40 | x->ex_xkusage |= XKU_CODE_SIGN; |
532 | 40 | break; |
533 | 3 | case NID_ms_sgc: |
534 | 7 | case NID_ns_sgc: |
535 | 7 | x->ex_xkusage |= XKU_SGC; |
536 | 7 | break; |
537 | 36 | case NID_OCSP_sign: |
538 | 36 | x->ex_xkusage |= XKU_OCSP_SIGN; |
539 | 36 | break; |
540 | 50 | case NID_time_stamp: |
541 | 50 | x->ex_xkusage |= XKU_TIMESTAMP; |
542 | 50 | break; |
543 | 37 | case NID_dvcs: |
544 | 37 | x->ex_xkusage |= XKU_DVCS; |
545 | 37 | break; |
546 | 2 | case NID_anyExtendedKeyUsage: |
547 | 2 | x->ex_xkusage |= XKU_ANYEKU; |
548 | 2 | break; |
549 | 1.55k | default: |
550 | | /* Ignore unknown extended key usage */ |
551 | 1.55k | break; |
552 | 41.6k | } |
553 | 41.6k | } |
554 | 41.4k | sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); |
555 | 41.4k | } else if (i != -1) { |
556 | 2.90k | x->ex_flags |= EXFLAG_INVALID; |
557 | 2.90k | } |
558 | | |
559 | | /* Handle legacy Netscape extension */ |
560 | 77.2k | if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) { |
561 | 32 | if (ns->length > 0) |
562 | 22 | x->ex_nscert = ns->data[0]; |
563 | 10 | else |
564 | 10 | x->ex_nscert = 0; |
565 | 32 | x->ex_flags |= EXFLAG_NSCERT; |
566 | 32 | ASN1_BIT_STRING_free(ns); |
567 | 77.1k | } else if (i != -1) { |
568 | 18 | x->ex_flags |= EXFLAG_INVALID; |
569 | 18 | } |
570 | | |
571 | | /* Handle subject key identifier and issuer/authority key identifier */ |
572 | 77.2k | x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL); |
573 | 77.2k | if (x->skid == NULL && i != -1) |
574 | 1.21k | x->ex_flags |= EXFLAG_INVALID; |
575 | | |
576 | 77.2k | x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL); |
577 | 77.2k | if (x->akid == NULL && i != -1) |
578 | 3.66k | x->ex_flags |= EXFLAG_INVALID; |
579 | | |
580 | | /* Check if subject name matches issuer */ |
581 | 77.2k | if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) { |
582 | 49.9k | x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */ |
583 | 49.9k | if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */ |
584 | 49.2k | /* .. and the signature alg matches the PUBKEY alg: */ |
585 | 49.2k | && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK) |
586 | 45.3k | x->ex_flags |= EXFLAG_SS; /* indicate self-signed */ |
587 | | /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */ |
588 | 49.9k | } |
589 | | |
590 | | /* Handle subject alternative names and various other extensions */ |
591 | 77.2k | x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL); |
592 | 77.2k | if (x->altname == NULL && i != -1) |
593 | 2.19k | x->ex_flags |= EXFLAG_INVALID; |
594 | 77.2k | x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL); |
595 | 77.2k | if (x->nc == NULL && i != -1) |
596 | 550 | x->ex_flags |= EXFLAG_INVALID; |
597 | | |
598 | | /* Handle CRL distribution point entries */ |
599 | 77.2k | res = setup_crldp(x); |
600 | 77.2k | if (res == 0) |
601 | 1.09k | x->ex_flags |= EXFLAG_INVALID; |
602 | | |
603 | 77.2k | #ifndef OPENSSL_NO_RFC3779 |
604 | 77.2k | x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL); |
605 | 77.2k | if (x->rfc3779_addr == NULL && i != -1) |
606 | 139 | x->ex_flags |= EXFLAG_INVALID; |
607 | 77.2k | x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL); |
608 | 77.2k | if (x->rfc3779_asid == NULL && i != -1) |
609 | 58 | x->ex_flags |= EXFLAG_INVALID; |
610 | 77.2k | #endif |
611 | 378k | for (i = 0; i < X509_get_ext_count(x); i++) { |
612 | 304k | X509_EXTENSION *ex = X509_get_ext(x, i); |
613 | 304k | int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
614 | | |
615 | 304k | if (nid == NID_freshest_crl) |
616 | 2.29k | x->ex_flags |= EXFLAG_FRESHEST; |
617 | 304k | if (!X509_EXTENSION_get_critical(ex)) |
618 | 269k | continue; |
619 | 35.7k | if (!X509_supported_extension(ex)) { |
620 | 3.92k | x->ex_flags |= EXFLAG_CRITICAL; |
621 | 3.92k | break; |
622 | 3.92k | } |
623 | 31.8k | switch (nid) { |
624 | 19.2k | case NID_basic_constraints: |
625 | 19.2k | x->ex_flags |= EXFLAG_BCONS_CRITICAL; |
626 | 19.2k | break; |
627 | 0 | case NID_authority_key_identifier: |
628 | 0 | x->ex_flags |= EXFLAG_AKID_CRITICAL; |
629 | 0 | break; |
630 | 0 | case NID_subject_key_identifier: |
631 | 0 | x->ex_flags |= EXFLAG_SKID_CRITICAL; |
632 | 0 | break; |
633 | 848 | case NID_subject_alt_name: |
634 | 848 | x->ex_flags |= EXFLAG_SAN_CRITICAL; |
635 | 848 | break; |
636 | 11.7k | default: |
637 | 11.7k | break; |
638 | 31.8k | } |
639 | 31.8k | } |
640 | | |
641 | | /* Set x->siginf, ignoring errors due to unsupported algos */ |
642 | 77.2k | (void)ossl_x509_init_sig_info(x); |
643 | | |
644 | 77.2k | x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */ |
645 | 77.2k | #ifdef tsan_st_rel |
646 | 77.2k | tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1); |
647 | | /* |
648 | | * Above store triggers fast lock-free check in the beginning of the |
649 | | * function. But one has to ensure that the structure is "stable", i.e. |
650 | | * all stores are visible on all processors. Hence the release fence. |
651 | | */ |
652 | 77.2k | #endif |
653 | 77.2k | ERR_pop_to_mark(); |
654 | | |
655 | 77.2k | if ((x->ex_flags & EXFLAG_INVALID) == 0) { |
656 | 62.8k | CRYPTO_THREAD_unlock(x->lock); |
657 | 62.8k | return 1; |
658 | 62.8k | } |
659 | 14.3k | CRYPTO_THREAD_unlock(x->lock); |
660 | 14.3k | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE); |
661 | 14.3k | return 0; |
662 | 77.2k | } |
663 | | |
664 | | /*- |
665 | | * CA checks common to all purposes |
666 | | * return codes: |
667 | | * 0 not a CA |
668 | | * 1 is a CA |
669 | | * 2 Only possible in older versions of openSSL when basicConstraints are absent |
670 | | * new versions will not return this value. May be a CA |
671 | | * 3 basicConstraints absent but self-signed V1. |
672 | | * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. |
673 | | * 5 Netscape specific CA Flags present |
674 | | */ |
675 | | |
676 | | static int check_ca(const X509 *x) |
677 | 9.64k | { |
678 | | /* keyUsage if present should allow cert signing */ |
679 | 9.64k | if (ku_reject(x, KU_KEY_CERT_SIGN)) |
680 | 76 | return 0; |
681 | 9.56k | if ((x->ex_flags & EXFLAG_BCONS) != 0) { |
682 | | /* If basicConstraints says not a CA then say so */ |
683 | 2.44k | return (x->ex_flags & EXFLAG_CA) != 0; |
684 | 7.12k | } else { |
685 | | /* We support V1 roots for... uh, I don't really know why. */ |
686 | 7.12k | if ((x->ex_flags & V1_ROOT) == V1_ROOT) |
687 | 94 | return 3; |
688 | | /* |
689 | | * If key usage present it must have certSign so tolerate it |
690 | | */ |
691 | 7.03k | else if ((x->ex_flags & EXFLAG_KUSAGE) != 0) |
692 | 35 | return 4; |
693 | | /* Older certificates could have Netscape-specific CA types */ |
694 | 6.99k | else if ((x->ex_flags & EXFLAG_NSCERT) != 0 |
695 | 14 | && (x->ex_nscert & NS_ANY_CA) != 0) |
696 | 6 | return 5; |
697 | | /* Can this still be regarded a CA certificate? I doubt it. */ |
698 | 6.99k | return 0; |
699 | 7.12k | } |
700 | 9.56k | } |
701 | | |
702 | | void X509_set_proxy_flag(X509 *x) |
703 | 0 | { |
704 | 0 | if (CRYPTO_THREAD_write_lock(x->lock)) { |
705 | 0 | x->ex_flags |= EXFLAG_PROXY; |
706 | 0 | CRYPTO_THREAD_unlock(x->lock); |
707 | 0 | } |
708 | 0 | } |
709 | | |
710 | | void X509_set_proxy_pathlen(X509 *x, long l) |
711 | 0 | { |
712 | 0 | x->ex_pcpathlen = l; |
713 | 0 | } |
714 | | |
715 | | int X509_check_ca(X509 *x) |
716 | 10.7k | { |
717 | | /* Note 0 normally means "not a CA" - but in this case means error. */ |
718 | 10.7k | if (!ossl_x509v3_cache_extensions(x)) |
719 | 1.32k | return 0; |
720 | | |
721 | 9.42k | return check_ca(x); |
722 | 10.7k | } |
723 | | |
724 | | /* Check SSL CA: common checks for SSL client and server. */ |
725 | | static int check_ssl_ca(const X509 *x) |
726 | 0 | { |
727 | 0 | int ca_ret = check_ca(x); |
728 | |
|
729 | 0 | if (ca_ret == 0) |
730 | 0 | return 0; |
731 | | /* Check nsCertType if present */ |
732 | 0 | return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0; |
733 | 0 | } |
734 | | |
735 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
736 | | int non_leaf) |
737 | 0 | { |
738 | 0 | if (xku_reject(x, XKU_SSL_CLIENT)) |
739 | 0 | return 0; |
740 | 0 | if (non_leaf) |
741 | 0 | return check_ssl_ca(x); |
742 | | /* We need to do digital signatures or key agreement */ |
743 | 0 | if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) |
744 | 0 | return 0; |
745 | | /* nsCertType if present should allow SSL client use */ |
746 | 0 | if (ns_reject(x, NS_SSL_CLIENT)) |
747 | 0 | return 0; |
748 | 0 | return 1; |
749 | 0 | } |
750 | | |
751 | | /* |
752 | | * Key usage needed for TLS/SSL server: digital signature, encipherment or |
753 | | * key agreement. The ssl code can check this more thoroughly for individual |
754 | | * key types. |
755 | | */ |
756 | | #define KU_TLS \ |
757 | | KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT |
758 | | |
759 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
760 | | int non_leaf) |
761 | 0 | { |
762 | 0 | if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC)) |
763 | 0 | return 0; |
764 | 0 | if (non_leaf) |
765 | 0 | return check_ssl_ca(x); |
766 | | |
767 | 0 | if (ns_reject(x, NS_SSL_SERVER)) |
768 | 0 | return 0; |
769 | 0 | if (ku_reject(x, KU_TLS)) |
770 | 0 | return 0; |
771 | | |
772 | 0 | return 1; |
773 | 0 | } |
774 | | |
775 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
776 | | int non_leaf) |
777 | 0 | { |
778 | 0 | int ret = check_purpose_ssl_server(xp, x, non_leaf); |
779 | |
|
780 | 0 | if (!ret || non_leaf) |
781 | 0 | return ret; |
782 | | /* We need to encipher or Netscape complains */ |
783 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
784 | 0 | } |
785 | | |
786 | | /* common S/MIME checks */ |
787 | | static int purpose_smime(const X509 *x, int non_leaf) |
788 | 0 | { |
789 | 0 | if (xku_reject(x, XKU_SMIME)) |
790 | 0 | return 0; |
791 | 0 | if (non_leaf) { |
792 | 0 | int ca_ret = check_ca(x); |
793 | |
|
794 | 0 | if (ca_ret == 0) |
795 | 0 | return 0; |
796 | | /* Check nsCertType if present */ |
797 | 0 | if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0) |
798 | 0 | return ca_ret; |
799 | 0 | else |
800 | 0 | return 0; |
801 | 0 | } |
802 | 0 | if ((x->ex_flags & EXFLAG_NSCERT) != 0) { |
803 | 0 | if ((x->ex_nscert & NS_SMIME) != 0) |
804 | 0 | return 1; |
805 | | /* Workaround for some buggy certificates */ |
806 | 0 | return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0; |
807 | 0 | } |
808 | 0 | return 1; |
809 | 0 | } |
810 | | |
811 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
812 | | int non_leaf) |
813 | 0 | { |
814 | 0 | int ret = purpose_smime(x, non_leaf); |
815 | |
|
816 | 0 | if (!ret || non_leaf) |
817 | 0 | return ret; |
818 | 0 | return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret; |
819 | 0 | } |
820 | | |
821 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
822 | | int non_leaf) |
823 | 0 | { |
824 | 0 | int ret = purpose_smime(x, non_leaf); |
825 | |
|
826 | 0 | if (!ret || non_leaf) |
827 | 0 | return ret; |
828 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
829 | 0 | } |
830 | | |
831 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
832 | | int non_leaf) |
833 | 0 | { |
834 | 0 | if (non_leaf) { |
835 | 0 | int ca_ret = check_ca(x); |
836 | |
|
837 | 0 | return ca_ret == 2 ? 0 : ca_ret; |
838 | 0 | } |
839 | 0 | return !ku_reject(x, KU_CRL_SIGN); |
840 | 0 | } |
841 | | |
842 | | /* |
843 | | * OCSP helper: this is *not* a full OCSP check. It just checks that each CA |
844 | | * is valid. Additional checks must be made on the chain. |
845 | | */ |
846 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
847 | | int non_leaf) |
848 | 499 | { |
849 | | /* |
850 | | * Must be a valid CA. Should we really support the "I don't know" value |
851 | | * (2)? |
852 | | */ |
853 | 499 | if (non_leaf) |
854 | 219 | return check_ca(x); |
855 | | /* Leaf certificate is checked in OCSP_verify() */ |
856 | 280 | return 1; |
857 | 499 | } |
858 | | |
859 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
860 | | int non_leaf) |
861 | 0 | { |
862 | 0 | int i_ext; |
863 | | |
864 | | /* |
865 | | * If non_leaf is true we must check if this is a valid CA certificate. |
866 | | * The extra requirements by the CA/Browser Forum are not checked. |
867 | | */ |
868 | 0 | if (non_leaf) |
869 | 0 | return check_ca(x); |
870 | | |
871 | | /* |
872 | | * Key Usage is checked according to RFC 5280 and |
873 | | * Extended Key Usage attributes is checked according to RFC 3161. |
874 | | * The extra (and somewhat conflicting) CA/Browser Forum |
875 | | * Baseline Requirements for the Issuance and Management of |
876 | | * Publicly‐Trusted Code Signing Certificates, Version 3.0.0, |
877 | | * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked. |
878 | | */ |
879 | | /* |
880 | | * Check the optional key usage field: |
881 | | * if Key Usage is present, it must be one of digitalSignature |
882 | | * and/or nonRepudiation (other values are not consistent and shall |
883 | | * be rejected). |
884 | | */ |
885 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) != 0 |
886 | 0 | && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) |
887 | 0 | return 0; |
888 | | |
889 | | /* Only timestamp key usage is permitted and it's required. */ |
890 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP) |
891 | 0 | return 0; |
892 | | |
893 | | /* Extended Key Usage MUST be critical */ |
894 | 0 | i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); |
895 | 0 | if (i_ext >= 0 |
896 | 0 | && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext))) |
897 | 0 | return 0; |
898 | 0 | return 1; |
899 | 0 | } |
900 | | |
901 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
902 | | int non_leaf) |
903 | 0 | { |
904 | 0 | int i_ext; |
905 | | |
906 | | /* |
907 | | * If non_leaf is true we must check if this is a valid CA certificate. |
908 | | * The extra requirements by the CA/Browser Forum are not checked. |
909 | | */ |
910 | 0 | if (non_leaf) |
911 | 0 | return check_ca(x); |
912 | | |
913 | | /* |
914 | | * Check the key usage and extended key usage fields: |
915 | | * |
916 | | * Reference: CA/Browser Forum, |
917 | | * Baseline Requirements for the Issuance and Management of |
918 | | * Publicly‐Trusted Code Signing Certificates, Version 3.0.0, |
919 | | * Section 7.1.2.3: Code signing and Timestamp Certificate |
920 | | * |
921 | | * Checking covers Key Usage and Extended Key Usage attributes. |
922 | | * The certificatePolicies, cRLDistributionPoints (CDP), and |
923 | | * authorityInformationAccess (AIA) extensions are so far not checked. |
924 | | */ |
925 | | /* Key Usage */ |
926 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) == 0) |
927 | 0 | return 0; |
928 | 0 | if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0) |
929 | 0 | return 0; |
930 | 0 | if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0) |
931 | 0 | return 0; |
932 | | |
933 | | /* Key Usage MUST be critical */ |
934 | 0 | i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1); |
935 | 0 | if (i_ext < 0) |
936 | 0 | return 0; |
937 | 0 | if (i_ext >= 0) { |
938 | 0 | X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext); |
939 | 0 | if (!X509_EXTENSION_get_critical(ext)) |
940 | 0 | return 0; |
941 | 0 | } |
942 | | |
943 | | /* Extended Key Usage */ |
944 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0) |
945 | 0 | return 0; |
946 | 0 | if ((x->ex_xkusage & XKU_CODE_SIGN) == 0) |
947 | 0 | return 0; |
948 | 0 | if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0) |
949 | 0 | return 0; |
950 | | |
951 | 0 | return 1; |
952 | 0 | } |
953 | | |
954 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
955 | | int non_leaf) |
956 | 0 | { |
957 | 0 | return 1; |
958 | 0 | } |
959 | | |
960 | | /*- |
961 | | * Various checks to see if one certificate potentially issued the second. |
962 | | * This can be used to prune a set of possible issuer certificates which |
963 | | * have been looked up using some simple method such as by subject name. |
964 | | * These are: |
965 | | * 1. issuer_name(subject) == subject_name(issuer) |
966 | | * 2. If akid(subject) exists, it matches the respective issuer fields. |
967 | | * 3. subject signature algorithm == issuer public key algorithm |
968 | | * 4. If key_usage(issuer) exists, it allows for signing subject. |
969 | | * Note that this does not include actually checking the signature. |
970 | | * Returns 0 for OK, or positive for reason for mismatch |
971 | | * where reason codes match those for X509_verify_cert(). |
972 | | */ |
973 | | int X509_check_issued(X509 *issuer, X509 *subject) |
974 | 39.0k | { |
975 | 39.0k | int ret; |
976 | | |
977 | 39.0k | if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK) |
978 | 34.5k | return ret; |
979 | 4.51k | return ossl_x509_signing_allowed(issuer, subject); |
980 | 39.0k | } |
981 | | |
982 | | /* do the checks 1., 2., and 3. as described above for X509_check_issued() */ |
983 | | int ossl_x509_likely_issued(X509 *issuer, X509 *subject) |
984 | 40.0k | { |
985 | 40.0k | int ret; |
986 | | |
987 | 40.0k | if (X509_NAME_cmp(X509_get_subject_name(issuer), |
988 | 40.0k | X509_get_issuer_name(subject)) |
989 | 40.0k | != 0) |
990 | 25.4k | return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; |
991 | | |
992 | | /* set issuer->skid and subject->akid */ |
993 | 14.6k | if (!ossl_x509v3_cache_extensions(issuer) |
994 | 11.2k | || !ossl_x509v3_cache_extensions(subject)) |
995 | 3.83k | return X509_V_ERR_UNSPECIFIED; |
996 | | |
997 | 10.7k | ret = X509_check_akid(issuer, subject->akid); |
998 | 10.7k | if (ret != X509_V_OK) |
999 | 1.20k | return ret; |
1000 | | |
1001 | | /* Check if the subject signature alg matches the issuer's PUBKEY alg */ |
1002 | 9.57k | return check_sig_alg_match(X509_get0_pubkey(issuer), subject); |
1003 | 10.7k | } |
1004 | | |
1005 | | /*- |
1006 | | * Check if certificate I<issuer> is allowed to issue certificate I<subject> |
1007 | | * according to the B<keyUsage> field of I<issuer> if present |
1008 | | * depending on any proxyCertInfo extension of I<subject>. |
1009 | | * Returns 0 for OK, or positive for reason for rejection |
1010 | | * where reason codes match those for X509_verify_cert(). |
1011 | | */ |
1012 | | int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject) |
1013 | 8.00k | { |
1014 | 8.00k | if ((subject->ex_flags & EXFLAG_PROXY) != 0) { |
1015 | 4 | if (ku_reject(issuer, KU_DIGITAL_SIGNATURE)) |
1016 | 0 | return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE; |
1017 | 7.99k | } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) { |
1018 | 199 | return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; |
1019 | 199 | } |
1020 | 7.80k | return X509_V_OK; |
1021 | 8.00k | } |
1022 | | |
1023 | | int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid) |
1024 | 94.9k | { |
1025 | 94.9k | if (akid == NULL) |
1026 | 73.8k | return X509_V_OK; |
1027 | | |
1028 | | /* Check key ids (if present) */ |
1029 | 21.0k | if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) |
1030 | 1.47k | return X509_V_ERR_AKID_SKID_MISMATCH; |
1031 | | /* Check serial number */ |
1032 | 19.5k | if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial)) |
1033 | 1.14k | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1034 | | /* Check issuer name */ |
1035 | 18.4k | if (akid->issuer) { |
1036 | | /* |
1037 | | * Ugh, for some peculiar reason AKID includes SEQUENCE OF |
1038 | | * GeneralName. So look for a DirName. There may be more than one but |
1039 | | * we only take any notice of the first. |
1040 | | */ |
1041 | 358 | GENERAL_NAMES *gens = akid->issuer; |
1042 | 358 | GENERAL_NAME *gen; |
1043 | 358 | X509_NAME *nm = NULL; |
1044 | 358 | int i; |
1045 | | |
1046 | 650 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
1047 | 447 | gen = sk_GENERAL_NAME_value(gens, i); |
1048 | 447 | if (gen->type == GEN_DIRNAME) { |
1049 | 155 | nm = gen->d.dirn; |
1050 | 155 | break; |
1051 | 155 | } |
1052 | 447 | } |
1053 | 358 | if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0) |
1054 | 106 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1055 | 358 | } |
1056 | 18.3k | return X509_V_OK; |
1057 | 18.4k | } |
1058 | | |
1059 | | uint32_t X509_get_extension_flags(X509 *x) |
1060 | 32.9k | { |
1061 | | /* Call for side-effect of computing hash and caching extensions */ |
1062 | 32.9k | X509_check_purpose(x, -1, 0); |
1063 | 32.9k | return x->ex_flags; |
1064 | 32.9k | } |
1065 | | |
1066 | | uint32_t X509_get_key_usage(X509 *x) |
1067 | 24.0k | { |
1068 | | /* Call for side-effect of computing hash and caching extensions */ |
1069 | 24.0k | if (X509_check_purpose(x, -1, 0) != 1) |
1070 | 0 | return 0; |
1071 | 24.0k | return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX; |
1072 | 24.0k | } |
1073 | | |
1074 | | uint32_t X509_get_extended_key_usage(X509 *x) |
1075 | 8 | { |
1076 | | /* Call for side-effect of computing hash and caching extensions */ |
1077 | 8 | if (X509_check_purpose(x, -1, 0) != 1) |
1078 | 0 | return 0; |
1079 | 8 | return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX; |
1080 | 8 | } |
1081 | | |
1082 | | const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x) |
1083 | 6.90k | { |
1084 | | /* Call for side-effect of computing hash and caching extensions */ |
1085 | 6.90k | if (X509_check_purpose(x, -1, 0) != 1) |
1086 | 808 | return NULL; |
1087 | 6.09k | return x->skid; |
1088 | 6.90k | } |
1089 | | |
1090 | | const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x) |
1091 | 0 | { |
1092 | | /* Call for side-effect of computing hash and caching extensions */ |
1093 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1094 | 0 | return NULL; |
1095 | 0 | return (x->akid != NULL ? x->akid->keyid : NULL); |
1096 | 0 | } |
1097 | | |
1098 | | const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x) |
1099 | 0 | { |
1100 | | /* Call for side-effect of computing hash and caching extensions */ |
1101 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1102 | 0 | return NULL; |
1103 | 0 | return (x->akid != NULL ? x->akid->issuer : NULL); |
1104 | 0 | } |
1105 | | |
1106 | | const ASN1_INTEGER *X509_get0_authority_serial(X509 *x) |
1107 | 0 | { |
1108 | | /* Call for side-effect of computing hash and caching extensions */ |
1109 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1110 | 0 | return NULL; |
1111 | 0 | return (x->akid != NULL ? x->akid->serial : NULL); |
1112 | 0 | } |
1113 | | |
1114 | | long X509_get_pathlen(X509 *x) |
1115 | 0 | { |
1116 | | /* Called for side effect of caching extensions */ |
1117 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1118 | 0 | || (x->ex_flags & EXFLAG_BCONS) == 0) |
1119 | 0 | return -1; |
1120 | 0 | return x->ex_pathlen; |
1121 | 0 | } |
1122 | | |
1123 | | long X509_get_proxy_pathlen(X509 *x) |
1124 | 0 | { |
1125 | | /* Called for side effect of caching extensions */ |
1126 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1127 | 0 | || (x->ex_flags & EXFLAG_PROXY) == 0) |
1128 | 0 | return -1; |
1129 | 0 | return x->ex_pcpathlen; |
1130 | 0 | } |