/src/openssl40/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 | 670 | #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 | | int X509_check_purpose(const X509 *x, int id, int non_leaf) |
83 | 149k | { |
84 | 149k | int idx; |
85 | 149k | const X509_PURPOSE *pt; |
86 | | |
87 | | /* |
88 | | * TODO: This cast can be dropped when https://github.com/openssl/openssl/pull/30067 |
89 | | * gets merged |
90 | | */ |
91 | 149k | if (!ossl_x509v3_cache_extensions((X509 *)x)) |
92 | 35.4k | return -1; |
93 | 114k | if (id == -1) |
94 | 113k | return 1; |
95 | | |
96 | 407 | idx = X509_PURPOSE_get_by_id(id); |
97 | 407 | if (idx == -1) |
98 | 0 | return -1; |
99 | 407 | pt = X509_PURPOSE_get0(idx); |
100 | 407 | return pt->check_purpose(pt, x, non_leaf); |
101 | 407 | } |
102 | | |
103 | | /* resets to default (any) purpose if purpose == X509_PURPOSE_DEFAULT_ANY (0) */ |
104 | | int X509_PURPOSE_set(int *p, int purpose) |
105 | 0 | { |
106 | 0 | if (purpose != X509_PURPOSE_DEFAULT_ANY && X509_PURPOSE_get_by_id(purpose) == -1) { |
107 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE); |
108 | 0 | return 0; |
109 | 0 | } |
110 | 0 | *p = purpose; |
111 | 0 | return 1; |
112 | 0 | } |
113 | | |
114 | | int X509_PURPOSE_get_count(void) |
115 | 0 | { |
116 | 0 | if (!xptable) |
117 | 0 | return X509_PURPOSE_COUNT; |
118 | 0 | return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT; |
119 | 0 | } |
120 | | |
121 | | /* find smallest identifier not yet taken - note there might be gaps */ |
122 | | int X509_PURPOSE_get_unused_id(ossl_unused OSSL_LIB_CTX *libctx) |
123 | 0 | { |
124 | 0 | int id = X509_PURPOSE_MAX + 1; |
125 | |
|
126 | 0 | while (X509_PURPOSE_get_by_id(id) != -1) |
127 | 0 | id++; |
128 | 0 | return id; /* is guaranteed to be unique and > X509_PURPOSE_MAX and != 0 */ |
129 | 0 | } |
130 | | |
131 | | X509_PURPOSE *X509_PURPOSE_get0(int idx) |
132 | 56.5k | { |
133 | 56.5k | if (idx < 0) |
134 | 55.8k | return NULL; |
135 | 670 | if (idx < (int)X509_PURPOSE_COUNT) |
136 | 670 | return xstandard + idx; |
137 | 0 | return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT); |
138 | 670 | } |
139 | | |
140 | | int X509_PURPOSE_get_by_sname(const char *sname) |
141 | 0 | { |
142 | 0 | int i; |
143 | 0 | X509_PURPOSE *xptmp; |
144 | |
|
145 | 0 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { |
146 | 0 | xptmp = X509_PURPOSE_get0(i); |
147 | 0 | if (strcmp(xptmp->sname, sname) == 0) |
148 | 0 | return i; |
149 | 0 | } |
150 | 0 | return -1; |
151 | 0 | } |
152 | | |
153 | | /* Returns -1 on error, else an index => 0 in standard/extended purpose table */ |
154 | | int X509_PURPOSE_get_by_id(int purpose) |
155 | 56.5k | { |
156 | 56.5k | X509_PURPOSE tmp; |
157 | 56.5k | int idx; |
158 | | |
159 | 56.5k | if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX) |
160 | 670 | return purpose - X509_PURPOSE_MIN; |
161 | 55.8k | if (xptable == NULL) |
162 | 55.8k | return -1; |
163 | 0 | tmp.purpose = purpose; |
164 | 0 | idx = sk_X509_PURPOSE_find(xptable, &tmp); |
165 | 0 | if (idx < 0) |
166 | 0 | return -1; |
167 | 0 | return idx + X509_PURPOSE_COUNT; |
168 | 0 | } |
169 | | |
170 | | /* |
171 | | * Add purpose entry identified by |sname|. |id| must be >= X509_PURPOSE_MIN. |
172 | | * May also be used to modify existing entry, including changing its id. |
173 | | */ |
174 | | int X509_PURPOSE_add(int id, int trust, int flags, |
175 | | int (*ck)(const X509_PURPOSE *, const X509 *, int), |
176 | | const char *name, const char *sname, void *arg) |
177 | 0 | { |
178 | 0 | int old_id = 0; |
179 | 0 | int idx; |
180 | 0 | X509_PURPOSE *ptmp; |
181 | |
|
182 | 0 | if (id < X509_PURPOSE_MIN) { |
183 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE); |
184 | 0 | return 0; |
185 | 0 | } |
186 | 0 | if (trust < X509_TRUST_DEFAULT || name == NULL || sname == NULL || ck == NULL) { |
187 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT); |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | | /* This is set according to what we change: application can't set it */ |
192 | 0 | flags &= ~X509_PURPOSE_DYNAMIC; |
193 | | /* This will always be set for application modified trust entries */ |
194 | 0 | flags |= X509_PURPOSE_DYNAMIC_NAME; |
195 | | |
196 | | /* Get existing entry if any */ |
197 | 0 | idx = X509_PURPOSE_get_by_sname(sname); |
198 | 0 | if (idx == -1) { /* Need a new entry */ |
199 | 0 | if (X509_PURPOSE_get_by_id(id) != -1) { |
200 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE); |
201 | 0 | return 0; |
202 | 0 | } |
203 | 0 | if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) |
204 | 0 | return 0; |
205 | 0 | ptmp->flags = X509_PURPOSE_DYNAMIC; |
206 | 0 | } else { |
207 | 0 | ptmp = X509_PURPOSE_get0(idx); |
208 | 0 | old_id = ptmp->purpose; |
209 | 0 | if (id != old_id && X509_PURPOSE_get_by_id(id) != -1) { |
210 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | /* OPENSSL_free existing name if dynamic */ |
216 | 0 | if ((ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { |
217 | 0 | OPENSSL_free(ptmp->name); |
218 | 0 | OPENSSL_free(ptmp->sname); |
219 | 0 | } |
220 | | /* Dup supplied name */ |
221 | 0 | ptmp->name = OPENSSL_strdup(name); |
222 | 0 | ptmp->sname = OPENSSL_strdup(sname); |
223 | 0 | if (ptmp->name == NULL || ptmp->sname == NULL) |
224 | 0 | goto err; |
225 | | /* Keep the dynamic flag of existing entry */ |
226 | 0 | ptmp->flags &= X509_PURPOSE_DYNAMIC; |
227 | | /* Set all other flags */ |
228 | 0 | ptmp->flags |= flags; |
229 | |
|
230 | 0 | ptmp->purpose = id; |
231 | 0 | ptmp->trust = trust; |
232 | 0 | ptmp->check_purpose = ck; |
233 | 0 | ptmp->usr_data = arg; |
234 | | |
235 | | /* If its a new entry manage the dynamic table */ |
236 | 0 | if (idx == -1) { |
237 | 0 | if (xptable == NULL |
238 | 0 | && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) { |
239 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
240 | 0 | goto err; |
241 | 0 | } |
242 | 0 | if (!sk_X509_PURPOSE_push(xptable, ptmp)) { |
243 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
244 | 0 | goto err; |
245 | 0 | } |
246 | 0 | } else if (id != old_id) { |
247 | | /* on changing existing entry id, make sure to reset 'sorted' */ |
248 | 0 | (void)sk_X509_PURPOSE_set(xptable, idx, ptmp); |
249 | 0 | } |
250 | 0 | return 1; |
251 | 0 | err: |
252 | 0 | if (idx == -1) { |
253 | 0 | OPENSSL_free(ptmp->name); |
254 | 0 | OPENSSL_free(ptmp->sname); |
255 | 0 | OPENSSL_free(ptmp); |
256 | 0 | } |
257 | 0 | return 0; |
258 | 0 | } |
259 | | |
260 | | static void xptable_free(X509_PURPOSE *p) |
261 | 0 | { |
262 | 0 | if (p == NULL) |
263 | 0 | return; |
264 | 0 | if ((p->flags & X509_PURPOSE_DYNAMIC) != 0) { |
265 | 0 | if ((p->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { |
266 | 0 | OPENSSL_free(p->name); |
267 | 0 | OPENSSL_free(p->sname); |
268 | 0 | } |
269 | 0 | OPENSSL_free(p); |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | | void X509_PURPOSE_cleanup(void) |
274 | 0 | { |
275 | 0 | sk_X509_PURPOSE_pop_free(xptable, xptable_free); |
276 | 0 | xptable = NULL; |
277 | 0 | } |
278 | | |
279 | | int X509_PURPOSE_get_id(const X509_PURPOSE *xp) |
280 | 0 | { |
281 | 0 | return xp->purpose; |
282 | 0 | } |
283 | | |
284 | | char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp) |
285 | 0 | { |
286 | 0 | return xp->name; |
287 | 0 | } |
288 | | |
289 | | char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) |
290 | 0 | { |
291 | 0 | return xp->sname; |
292 | 0 | } |
293 | | |
294 | | int X509_PURPOSE_get_trust(const X509_PURPOSE *xp) |
295 | 0 | { |
296 | 0 | return xp->trust; |
297 | 0 | } |
298 | | |
299 | | static int nid_cmp(const int *a, const int *b) |
300 | 108k | { |
301 | 108k | return *a - *b; |
302 | 108k | } |
303 | | |
304 | | DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid); |
305 | | IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid); |
306 | | |
307 | | int X509_supported_extension(const X509_EXTENSION *ex) |
308 | 47.7k | { |
309 | | /* |
310 | | * This table is a list of the NIDs of supported extensions: that is |
311 | | * those which are used by the verify process. If an extension is |
312 | | * critical and doesn't appear in this list then the verify process will |
313 | | * normally reject the certificate. The list must be kept in numerical |
314 | | * order because it will be searched using bsearch. |
315 | | */ |
316 | 47.7k | static const int supported_nids[] = { |
317 | 47.7k | NID_netscape_cert_type, /* 71 */ |
318 | 47.7k | NID_key_usage, /* 83 */ |
319 | 47.7k | NID_subject_alt_name, /* 85 */ |
320 | 47.7k | NID_basic_constraints, /* 87 */ |
321 | 47.7k | NID_certificate_policies, /* 89 */ |
322 | 47.7k | NID_crl_distribution_points, /* 103 */ |
323 | 47.7k | NID_ext_key_usage, /* 126 */ |
324 | 47.7k | #ifndef OPENSSL_NO_RFC3779 |
325 | 47.7k | NID_sbgp_ipAddrBlock, /* 290 */ |
326 | 47.7k | NID_sbgp_autonomousSysNum, /* 291 */ |
327 | 47.7k | #endif |
328 | 47.7k | NID_id_pkix_OCSP_noCheck, /* 369 */ |
329 | 47.7k | NID_policy_constraints, /* 401 */ |
330 | 47.7k | NID_proxyCertInfo, /* 663 */ |
331 | 47.7k | NID_name_constraints, /* 666 */ |
332 | 47.7k | NID_policy_mappings, /* 747 */ |
333 | 47.7k | NID_inhibit_any_policy /* 748 */ |
334 | 47.7k | }; |
335 | | |
336 | 47.7k | int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
337 | | |
338 | 47.7k | if (ex_nid == NID_undef) |
339 | 4.00k | return 0; |
340 | | |
341 | 43.6k | if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids))) |
342 | 42.5k | return 1; |
343 | 1.12k | return 0; |
344 | 43.6k | } |
345 | | |
346 | | /* Returns 1 on success, 0 if x is invalid, -1 on (internal) error. */ |
347 | | static int setup_dp(const X509 *x, DIST_POINT *dp) |
348 | 2.07k | { |
349 | 2.07k | const X509_NAME *iname = NULL; |
350 | 2.07k | int i; |
351 | | |
352 | 2.07k | if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) { |
353 | 47 | ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT); |
354 | 47 | return 0; |
355 | 47 | } |
356 | 2.02k | if (dp->reasons != NULL) { |
357 | 31 | if (dp->reasons->length > 0) |
358 | 30 | dp->dp_reasons = dp->reasons->data[0]; |
359 | 31 | if (dp->reasons->length > 1) |
360 | 11 | dp->dp_reasons |= (dp->reasons->data[1] << 8); |
361 | 31 | dp->dp_reasons &= CRLDP_ALL_REASONS; |
362 | 1.99k | } else { |
363 | 1.99k | dp->dp_reasons = CRLDP_ALL_REASONS; |
364 | 1.99k | } |
365 | 2.02k | if (dp->distpoint == NULL || dp->distpoint->type != 1) |
366 | 1.98k | return 1; |
367 | | |
368 | | /* Handle name fragment given by nameRelativeToCRLIssuer */ |
369 | | /* |
370 | | * Note that the below way of determining iname is not really compliant |
371 | | * with https://tools.ietf.org/html/rfc5280#section-4.2.1.13 |
372 | | * According to it, sk_GENERAL_NAME_num(dp->CRLissuer) MUST be <= 1 |
373 | | * and any CRLissuer could be of type different to GEN_DIRNAME. |
374 | | */ |
375 | 70 | for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { |
376 | 35 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); |
377 | | |
378 | 35 | if (gen->type == GEN_DIRNAME) { |
379 | 0 | iname = gen->d.directoryName; |
380 | 0 | break; |
381 | 0 | } |
382 | 35 | } |
383 | 35 | if (iname == NULL) |
384 | 35 | iname = X509_get_issuer_name(x); |
385 | 35 | return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1; |
386 | 2.02k | } |
387 | | |
388 | | /* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */ |
389 | | static int setup_crldp(const X509 *x, STACK_OF(DIST_POINT) **tmp_crldp) |
390 | 103k | { |
391 | 103k | int i; |
392 | | |
393 | 103k | *tmp_crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL); |
394 | 103k | if (*tmp_crldp == NULL && i != -1) |
395 | 1.67k | return 0; |
396 | | |
397 | 103k | for (i = 0; i < sk_DIST_POINT_num(*tmp_crldp); i++) { |
398 | 2.07k | int res = setup_dp(x, sk_DIST_POINT_value(*tmp_crldp, i)); |
399 | | |
400 | 2.07k | if (res < 1) |
401 | 61 | return res; |
402 | 2.07k | } |
403 | 101k | return 1; |
404 | 101k | } |
405 | | |
406 | | /* Check that issuer public key algorithm matches subject signature algorithm */ |
407 | | static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject) |
408 | 75.3k | { |
409 | 75.3k | int subj_sig_nid; |
410 | | |
411 | 75.3k | if (issuer_key == NULL) |
412 | 6.08k | return X509_V_ERR_NO_ISSUER_PUBLIC_KEY; |
413 | 69.2k | if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm), |
414 | 69.2k | NULL, &subj_sig_nid) |
415 | 69.2k | == 0) |
416 | 5.89k | return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
417 | 63.3k | if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid)) |
418 | 1.65k | || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss)) |
419 | 62.7k | return X509_V_OK; |
420 | 631 | return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH; |
421 | 63.3k | } |
422 | | |
423 | 11.0k | #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS) |
424 | | #define ku_reject(x, usage) \ |
425 | 13.7k | (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0) |
426 | | #define xku_reject(x, usage) \ |
427 | 0 | (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0) |
428 | | #define ns_reject(x, usage) \ |
429 | 0 | (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0) |
430 | | |
431 | | /* |
432 | | * Cache info on various X.509v3 extensions and further derived information, |
433 | | * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields. |
434 | | * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags. |
435 | | * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully. |
436 | | * Set EXFLAG_INVALID and return 0 in case the certificate is invalid. |
437 | | * |
438 | | * This is usually called by side-effect on objects, and forces us to keep |
439 | | * mutable X509 objects around. We should really make this go away. |
440 | | * In the interest of being able to do so, this function explicitly takes |
441 | | * a const argument and casts away const. |
442 | | */ |
443 | | int ossl_x509v3_cache_extensions(const X509 *const_x) |
444 | 42.0k | { |
445 | 42.0k | BASIC_CONSTRAINTS *bs; |
446 | 42.0k | PROXY_CERT_INFO_EXTENSION *pci; |
447 | 42.0k | ASN1_BIT_STRING *usage; |
448 | 42.0k | ASN1_BIT_STRING *ns; |
449 | 42.0k | EXTENDED_KEY_USAGE *extusage; |
450 | 42.0k | int i; |
451 | 42.0k | int res; |
452 | 42.0k | uint32_t tmp_ex_flags; |
453 | 42.0k | unsigned char tmp_sha1_hash[SHA_DIGEST_LENGTH]; |
454 | 42.0k | long tmp_ex_pathlen; |
455 | 42.0k | long tmp_ex_pcpathlen; |
456 | 42.0k | uint32_t tmp_ex_kusage; |
457 | 42.0k | uint32_t tmp_ex_xkusage; |
458 | 42.0k | uint32_t tmp_ex_nscert; |
459 | 42.0k | ASN1_OCTET_STRING *tmp_skid; |
460 | 42.0k | AUTHORITY_KEYID *tmp_akid; |
461 | 42.0k | STACK_OF(GENERAL_NAME) *tmp_altname; |
462 | 42.0k | NAME_CONSTRAINTS *tmp_nc; |
463 | 42.0k | STACK_OF(DIST_POINT) *tmp_crldp = NULL; |
464 | 42.0k | X509_SIG_INFO tmp_siginf; |
465 | | |
466 | 42.0k | #ifdef tsan_ld_acq |
467 | | /* Fast lock-free check, see end of the function for details. */ |
468 | 42.0k | if (tsan_ld_acq((TSAN_QUALIFIER int *)&const_x->ex_cached)) |
469 | 30.4k | return (const_x->ex_flags & EXFLAG_INVALID) == 0; |
470 | 11.6k | #endif |
471 | | |
472 | 11.6k | if (!CRYPTO_THREAD_read_lock(const_x->lock)) |
473 | 0 | return 0; |
474 | 11.6k | tmp_ex_flags = const_x->ex_flags; |
475 | 11.6k | tmp_ex_pcpathlen = const_x->ex_pcpathlen; |
476 | 11.6k | tmp_ex_kusage = const_x->ex_kusage; |
477 | 11.6k | tmp_ex_nscert = const_x->ex_nscert; |
478 | | |
479 | 11.6k | if ((tmp_ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */ |
480 | 0 | CRYPTO_THREAD_unlock(const_x->lock); |
481 | 0 | return (tmp_ex_flags & EXFLAG_INVALID) == 0; |
482 | 0 | } |
483 | | |
484 | 11.6k | ERR_set_mark(); |
485 | | |
486 | | /* Cache the SHA1 digest of the cert */ |
487 | 11.6k | if (!X509_digest(const_x, EVP_sha1(), tmp_sha1_hash, NULL)) |
488 | 0 | tmp_ex_flags |= EXFLAG_NO_FINGERPRINT; |
489 | | |
490 | | /* V1 should mean no extensions ... */ |
491 | 11.6k | if (X509_get_version(const_x) == X509_VERSION_1) |
492 | 154 | tmp_ex_flags |= EXFLAG_V1; |
493 | | |
494 | | /* Handle basic constraints */ |
495 | 11.6k | tmp_ex_pathlen = -1; |
496 | 11.6k | if ((bs = X509_get_ext_d2i(const_x, NID_basic_constraints, &i, NULL)) != NULL) { |
497 | 8.35k | if (bs->ca) |
498 | 1.98k | tmp_ex_flags |= EXFLAG_CA; |
499 | 8.35k | if (bs->pathlen != NULL) { |
500 | | /* |
501 | | * The error case !bs->ca is checked by check_chain() |
502 | | * in case ctx->param->flags & X509_V_FLAG_X509_STRICT |
503 | | */ |
504 | 626 | if (bs->pathlen->type == V_ASN1_NEG_INTEGER) { |
505 | 34 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN); |
506 | 34 | tmp_ex_flags |= EXFLAG_INVALID; |
507 | 592 | } else { |
508 | 592 | tmp_ex_pathlen = ASN1_INTEGER_get(bs->pathlen); |
509 | 592 | } |
510 | 626 | } |
511 | 8.35k | BASIC_CONSTRAINTS_free(bs); |
512 | 8.35k | tmp_ex_flags |= EXFLAG_BCONS; |
513 | 8.35k | } else if (i != -1) { |
514 | 561 | tmp_ex_flags |= EXFLAG_INVALID; |
515 | 561 | } |
516 | | |
517 | | /* Handle proxy certificates */ |
518 | 11.6k | if ((pci = X509_get_ext_d2i(const_x, NID_proxyCertInfo, &i, NULL)) != NULL) { |
519 | 38 | if ((tmp_ex_flags & EXFLAG_CA) != 0 |
520 | 32 | || X509_get_ext_by_NID(const_x, NID_subject_alt_name, -1) >= 0 |
521 | 23 | || X509_get_ext_by_NID(const_x, NID_issuer_alt_name, -1) >= 0) { |
522 | 20 | tmp_ex_flags |= EXFLAG_INVALID; |
523 | 20 | } |
524 | 38 | if (pci->pcPathLengthConstraint != NULL) |
525 | 0 | tmp_ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint); |
526 | 38 | else |
527 | 38 | tmp_ex_pcpathlen = -1; |
528 | 38 | PROXY_CERT_INFO_EXTENSION_free(pci); |
529 | 38 | tmp_ex_flags |= EXFLAG_PROXY; |
530 | 11.6k | } else if (i != -1) { |
531 | 47 | tmp_ex_flags |= EXFLAG_INVALID; |
532 | 47 | } |
533 | | |
534 | | /* Handle (basic) key usage */ |
535 | 11.6k | if ((usage = X509_get_ext_d2i(const_x, NID_key_usage, &i, NULL)) != NULL) { |
536 | 4.71k | tmp_ex_kusage = 0; |
537 | 4.71k | if (usage->length > 0) { |
538 | 4.68k | tmp_ex_kusage = usage->data[0]; |
539 | 4.68k | if (usage->length > 1) |
540 | 3 | tmp_ex_kusage |= usage->data[1] << 8; |
541 | 4.68k | } |
542 | 4.71k | tmp_ex_flags |= EXFLAG_KUSAGE; |
543 | 4.71k | ASN1_BIT_STRING_free(usage); |
544 | | /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */ |
545 | 4.71k | if (tmp_ex_kusage == 0) { |
546 | 314 | ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE); |
547 | 314 | tmp_ex_flags |= EXFLAG_INVALID; |
548 | 314 | } |
549 | 6.94k | } else if (i != -1) { |
550 | 286 | tmp_ex_flags |= EXFLAG_INVALID; |
551 | 286 | } |
552 | | |
553 | | /* Handle extended key usage */ |
554 | 11.6k | tmp_ex_xkusage = 0; |
555 | 11.6k | if ((extusage = X509_get_ext_d2i(const_x, NID_ext_key_usage, &i, NULL)) != NULL) { |
556 | 5.36k | tmp_ex_flags |= EXFLAG_XKUSAGE; |
557 | 10.7k | for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { |
558 | 5.39k | switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { |
559 | 5.01k | case NID_server_auth: |
560 | 5.01k | tmp_ex_xkusage |= XKU_SSL_SERVER; |
561 | 5.01k | break; |
562 | 17 | case NID_client_auth: |
563 | 17 | tmp_ex_xkusage |= XKU_SSL_CLIENT; |
564 | 17 | break; |
565 | 16 | case NID_email_protect: |
566 | 16 | tmp_ex_xkusage |= XKU_SMIME; |
567 | 16 | break; |
568 | 5 | case NID_code_sign: |
569 | 5 | tmp_ex_xkusage |= XKU_CODE_SIGN; |
570 | 5 | break; |
571 | 0 | case NID_ms_sgc: |
572 | 0 | case NID_ns_sgc: |
573 | 0 | tmp_ex_xkusage |= XKU_SGC; |
574 | 0 | break; |
575 | 5 | case NID_OCSP_sign: |
576 | 5 | tmp_ex_xkusage |= XKU_OCSP_SIGN; |
577 | 5 | break; |
578 | 6 | case NID_time_stamp: |
579 | 6 | tmp_ex_xkusage |= XKU_TIMESTAMP; |
580 | 6 | break; |
581 | 3 | case NID_dvcs: |
582 | 3 | tmp_ex_xkusage |= XKU_DVCS; |
583 | 3 | break; |
584 | 0 | case NID_anyExtendedKeyUsage: |
585 | 0 | tmp_ex_xkusage |= XKU_ANYEKU; |
586 | 0 | break; |
587 | 319 | default: |
588 | | /* Ignore unknown extended key usage */ |
589 | 319 | break; |
590 | 5.39k | } |
591 | 5.39k | } |
592 | 5.36k | sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); |
593 | 6.29k | } else if (i != -1) { |
594 | 665 | tmp_ex_flags |= EXFLAG_INVALID; |
595 | 665 | } |
596 | | |
597 | | /* Handle legacy Netscape extension */ |
598 | 11.6k | if ((ns = X509_get_ext_d2i(const_x, NID_netscape_cert_type, &i, NULL)) != NULL) { |
599 | 4 | if (ns->length > 0) |
600 | 3 | tmp_ex_nscert = ns->data[0]; |
601 | 1 | else |
602 | 1 | tmp_ex_nscert = 0; |
603 | 4 | tmp_ex_flags |= EXFLAG_NSCERT; |
604 | 4 | ASN1_BIT_STRING_free(ns); |
605 | 11.6k | } else if (i != -1) { |
606 | 4 | tmp_ex_flags |= EXFLAG_INVALID; |
607 | 4 | } |
608 | | |
609 | | /* Handle subject key identifier and issuer/authority key identifier */ |
610 | 11.6k | tmp_skid = X509_get_ext_d2i(const_x, NID_subject_key_identifier, &i, NULL); |
611 | 11.6k | if (tmp_skid == NULL && i != -1) |
612 | 192 | tmp_ex_flags |= EXFLAG_INVALID; |
613 | | |
614 | 11.6k | tmp_akid = X509_get_ext_d2i(const_x, NID_authority_key_identifier, &i, NULL); |
615 | 11.6k | if (tmp_akid == NULL && i != -1) |
616 | 1.12k | tmp_ex_flags |= EXFLAG_INVALID; |
617 | | |
618 | | /* Setting EXFLAG_SS is equivalent to ossl_x509_likely_issued(const_x, const_x) == X509_V_OK */ |
619 | 11.6k | if (X509_NAME_cmp(X509_get_subject_name(const_x), X509_get_issuer_name(const_x)) == 0) { |
620 | 5.62k | tmp_ex_flags |= EXFLAG_SI; /* Certificate is self-issued: subject == issuer */ |
621 | | /* |
622 | | * When the SKID is missing, which is rare for self-issued certs, |
623 | | * we could afford doing the (accurate) actual self-signature check, but |
624 | | * decided against it for efficiency reasons and according to RFC 5280, |
625 | | * CA certs MUST have an SKID and non-root certs MUST have an AKID. |
626 | | */ |
627 | 5.62k | if (X509_check_akid(const_x, tmp_akid) == X509_V_OK |
628 | 5.53k | && check_sig_alg_match(X509_get0_pubkey(const_x), const_x) == X509_V_OK) { |
629 | | /* |
630 | | * Assume self-signed if the signature alg matches the pkey alg and |
631 | | * AKID is missing or matches respective fields in the same cert |
632 | | * Not checking if any given key usage extension allows signing. |
633 | | */ |
634 | 4.83k | tmp_ex_flags |= EXFLAG_SS; |
635 | 4.83k | } |
636 | 5.62k | } |
637 | | |
638 | | /* Handle subject alternative names and various other extensions */ |
639 | 11.6k | tmp_altname = X509_get_ext_d2i(const_x, NID_subject_alt_name, &i, NULL); |
640 | 11.6k | if (tmp_altname == NULL && i != -1) |
641 | 350 | tmp_ex_flags |= EXFLAG_INVALID; |
642 | 11.6k | tmp_nc = X509_get_ext_d2i(const_x, NID_name_constraints, &i, NULL); |
643 | 11.6k | if (tmp_nc == NULL && i != -1) |
644 | 176 | tmp_ex_flags |= EXFLAG_INVALID; |
645 | | |
646 | | /* Handle CRL distribution point entries */ |
647 | 11.6k | res = setup_crldp(const_x, &tmp_crldp); |
648 | 11.6k | if (res == 0) |
649 | 314 | tmp_ex_flags |= EXFLAG_INVALID; |
650 | | |
651 | 11.6k | #ifndef OPENSSL_NO_RFC3779 |
652 | 11.6k | STACK_OF(IPAddressFamily) *tmp_rfc3779_addr |
653 | 11.6k | = X509_get_ext_d2i(const_x, NID_sbgp_ipAddrBlock, &i, NULL); |
654 | 11.6k | if (tmp_rfc3779_addr == NULL && i != -1) |
655 | 64 | tmp_ex_flags |= EXFLAG_INVALID; |
656 | | |
657 | 11.6k | struct ASIdentifiers_st *tmp_rfc3779_asid |
658 | 11.6k | = X509_get_ext_d2i(const_x, NID_sbgp_autonomousSysNum, &i, NULL); |
659 | 11.6k | if (tmp_rfc3779_asid == NULL && i != -1) |
660 | 13 | tmp_ex_flags |= EXFLAG_INVALID; |
661 | 11.6k | #endif |
662 | | |
663 | 56.5k | for (i = 0; i < X509_get_ext_count(const_x); i++) { |
664 | 45.5k | const X509_EXTENSION *ex = X509_get_ext(const_x, i); |
665 | 45.5k | int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
666 | | |
667 | 45.5k | if (nid == NID_freshest_crl) |
668 | 482 | tmp_ex_flags |= EXFLAG_FRESHEST; |
669 | 45.5k | if (!X509_EXTENSION_get_critical(ex)) |
670 | 39.9k | continue; |
671 | 5.65k | if (!X509_supported_extension(ex)) { |
672 | 695 | tmp_ex_flags |= EXFLAG_CRITICAL; |
673 | 695 | break; |
674 | 695 | } |
675 | 5.65k | } |
676 | | |
677 | | /* Set x->siginf, ignoring errors due to unsupported algos */ |
678 | 11.6k | (void)ossl_x509_init_sig_info(const_x, &tmp_siginf); |
679 | | |
680 | 11.6k | tmp_ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */ |
681 | 11.6k | ERR_pop_to_mark(); |
682 | | |
683 | 11.6k | CRYPTO_THREAD_unlock(const_x->lock); |
684 | | /* |
685 | | * Now that we've done all the compute intensive work under read lock |
686 | | * do all the updating under a write lock |
687 | | */ |
688 | 11.6k | if (!CRYPTO_THREAD_write_lock(const_x->lock)) |
689 | 0 | return 0; |
690 | 11.6k | ((X509 *)const_x)->ex_flags = tmp_ex_flags; |
691 | 11.6k | ((X509 *)const_x)->ex_pathlen = tmp_ex_pathlen; |
692 | 11.6k | ((X509 *)const_x)->ex_pcpathlen = tmp_ex_pcpathlen; |
693 | 11.6k | if (!(tmp_ex_flags & EXFLAG_NO_FINGERPRINT)) |
694 | 11.6k | memcpy(((X509 *)const_x)->sha1_hash, tmp_sha1_hash, SHA_DIGEST_LENGTH); |
695 | 11.6k | if (tmp_ex_flags & EXFLAG_KUSAGE) |
696 | 4.71k | ((X509 *)const_x)->ex_kusage = tmp_ex_kusage; |
697 | 11.6k | ((X509 *)const_x)->ex_xkusage = tmp_ex_xkusage; |
698 | 11.6k | if (tmp_ex_flags & EXFLAG_NSCERT) |
699 | 4 | ((X509 *)const_x)->ex_nscert = tmp_ex_nscert; |
700 | 11.6k | ASN1_OCTET_STRING_free(((X509 *)const_x)->skid); |
701 | 11.6k | ((X509 *)const_x)->skid = tmp_skid; |
702 | 11.6k | AUTHORITY_KEYID_free(((X509 *)const_x)->akid); |
703 | 11.6k | ((X509 *)const_x)->akid = tmp_akid; |
704 | 11.6k | sk_GENERAL_NAME_pop_free(((X509 *)const_x)->altname, GENERAL_NAME_free); |
705 | 11.6k | ((X509 *)const_x)->altname = tmp_altname; |
706 | 11.6k | NAME_CONSTRAINTS_free(((X509 *)const_x)->nc); |
707 | 11.6k | ((X509 *)const_x)->nc = tmp_nc; |
708 | 11.6k | sk_DIST_POINT_pop_free(((X509 *)const_x)->crldp, DIST_POINT_free); |
709 | 11.6k | ((X509 *)const_x)->crldp = tmp_crldp; |
710 | 11.6k | #ifndef OPENSSL_NO_RFC3779 |
711 | 11.6k | sk_IPAddressFamily_pop_free(((X509 *)const_x)->rfc3779_addr, IPAddressFamily_free); |
712 | 11.6k | ((X509 *)const_x)->rfc3779_addr = tmp_rfc3779_addr; |
713 | 11.6k | ASIdentifiers_free(((X509 *)const_x)->rfc3779_asid); |
714 | 11.6k | ((X509 *)const_x)->rfc3779_asid = tmp_rfc3779_asid; |
715 | 11.6k | #endif |
716 | 11.6k | ((X509 *)const_x)->siginf = tmp_siginf; |
717 | | |
718 | 11.6k | #ifdef tsan_st_rel |
719 | 11.6k | tsan_st_rel((TSAN_QUALIFIER int *)&const_x->ex_cached, 1); |
720 | | /* |
721 | | * Above store triggers fast lock-free check in the beginning of the |
722 | | * function. But one has to ensure that the structure is "stable", i.e. |
723 | | * all stores are visible on all processors. Hence the release fence. |
724 | | */ |
725 | 11.6k | #endif |
726 | 11.6k | CRYPTO_THREAD_unlock(const_x->lock); |
727 | 11.6k | if (tmp_ex_flags & EXFLAG_INVALID) { |
728 | 2.95k | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE); |
729 | 2.95k | return 0; |
730 | 2.95k | } |
731 | 8.70k | return 1; |
732 | 11.6k | } |
733 | | |
734 | | /*- |
735 | | * CA checks common to all purposes |
736 | | * return codes: |
737 | | * 0 not a CA |
738 | | * 1 is a CA |
739 | | * 2 Only possible in older versions of openSSL when basicConstraints are absent |
740 | | * new versions will not return this value. May be a CA |
741 | | * 3 basicConstraints absent but self-signed V1. |
742 | | * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. |
743 | | * 5 Netscape specific CA Flags present |
744 | | */ |
745 | | |
746 | | static int check_ca(const X509 *x) |
747 | 7.34k | { |
748 | | /* keyUsage if present should allow cert signing */ |
749 | 7.34k | if (ku_reject(x, KU_KEY_CERT_SIGN)) |
750 | 22 | return 0; |
751 | 7.32k | if ((x->ex_flags & EXFLAG_BCONS) != 0) { |
752 | | /* If basicConstraints says not a CA then say so */ |
753 | 1.78k | return (x->ex_flags & EXFLAG_CA) != 0; |
754 | 5.54k | } else { |
755 | | /* We support V1 roots for... uh, I don't really know why. */ |
756 | 5.54k | if ((x->ex_flags & V1_ROOT) == V1_ROOT) |
757 | 77 | return 3; |
758 | | /* |
759 | | * If key usage present it must have certSign so tolerate it |
760 | | */ |
761 | 5.46k | else if ((x->ex_flags & EXFLAG_KUSAGE) != 0) |
762 | 15 | return 4; |
763 | | /* Older certificates could have Netscape-specific CA types */ |
764 | 5.44k | else if ((x->ex_flags & EXFLAG_NSCERT) != 0 |
765 | 12 | && (x->ex_nscert & NS_ANY_CA) != 0) |
766 | 5 | return 5; |
767 | | /* Can this still be regarded a CA certificate? I doubt it. */ |
768 | 5.44k | return 0; |
769 | 5.54k | } |
770 | 7.32k | } |
771 | | |
772 | | void X509_set_proxy_flag(X509 *x) |
773 | 0 | { |
774 | 0 | if (CRYPTO_THREAD_write_lock(x->lock)) { |
775 | 0 | x->ex_flags |= EXFLAG_PROXY; |
776 | 0 | CRYPTO_THREAD_unlock(x->lock); |
777 | 0 | } |
778 | 0 | } |
779 | | |
780 | | void X509_set_proxy_pathlen(X509 *x, long l) |
781 | 0 | { |
782 | 0 | x->ex_pcpathlen = l; |
783 | 0 | } |
784 | | |
785 | | int X509_check_ca(const X509 *x) |
786 | 8.31k | { |
787 | | /* Note 0 normally means "not a CA" - but in this case means error. */ |
788 | 8.31k | if (!ossl_x509v3_cache_extensions(x)) |
789 | 1.14k | return 0; |
790 | | |
791 | 7.17k | return check_ca(x); |
792 | 8.31k | } |
793 | | |
794 | | /* Check SSL CA: common checks for SSL client and server. */ |
795 | | static int check_ssl_ca(const X509 *x) |
796 | 0 | { |
797 | 0 | int ca_ret = check_ca(x); |
798 | |
|
799 | 0 | if (ca_ret == 0) |
800 | 0 | return 0; |
801 | | /* Check nsCertType if present */ |
802 | 0 | return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0; |
803 | 0 | } |
804 | | |
805 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
806 | | int non_leaf) |
807 | 0 | { |
808 | 0 | if (xku_reject(x, XKU_SSL_CLIENT)) |
809 | 0 | return 0; |
810 | 0 | if (non_leaf) |
811 | 0 | return check_ssl_ca(x); |
812 | | /* We need to do digital signatures or key agreement */ |
813 | 0 | if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) |
814 | 0 | return 0; |
815 | | /* nsCertType if present should allow SSL client use */ |
816 | 0 | if (ns_reject(x, NS_SSL_CLIENT)) |
817 | 0 | return 0; |
818 | 0 | return 1; |
819 | 0 | } |
820 | | |
821 | | /* |
822 | | * Key usage needed for TLS/SSL server: digital signature, encipherment or |
823 | | * key agreement. The ssl code can check this more thoroughly for individual |
824 | | * key types. |
825 | | */ |
826 | | #define KU_TLS \ |
827 | | KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT |
828 | | |
829 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
830 | | int non_leaf) |
831 | 0 | { |
832 | 0 | if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC)) |
833 | 0 | return 0; |
834 | 0 | if (non_leaf) |
835 | 0 | return check_ssl_ca(x); |
836 | | |
837 | 0 | if (ns_reject(x, NS_SSL_SERVER)) |
838 | 0 | return 0; |
839 | 0 | if (ku_reject(x, KU_TLS)) |
840 | 0 | return 0; |
841 | | |
842 | 0 | return 1; |
843 | 0 | } |
844 | | |
845 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
846 | | int non_leaf) |
847 | 0 | { |
848 | 0 | int ret = check_purpose_ssl_server(xp, x, non_leaf); |
849 | |
|
850 | 0 | if (!ret || non_leaf) |
851 | 0 | return ret; |
852 | | /* We need to encipher or Netscape complains */ |
853 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
854 | 0 | } |
855 | | |
856 | | /* common S/MIME client checks */ |
857 | | static int purpose_smime(const X509 *x, int non_leaf) |
858 | 0 | { |
859 | 0 | if (xku_reject(x, XKU_SMIME)) |
860 | 0 | return 0; |
861 | 0 | if (non_leaf) { |
862 | 0 | int ca_ret = check_ca(x); |
863 | |
|
864 | 0 | if (ca_ret == 0) |
865 | 0 | return 0; |
866 | | /* Check nsCertType if present */ |
867 | 0 | if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0) |
868 | 0 | return ca_ret; |
869 | 0 | else |
870 | 0 | return 0; |
871 | 0 | } |
872 | 0 | if ((x->ex_flags & EXFLAG_NSCERT) != 0) { |
873 | 0 | if ((x->ex_nscert & NS_SMIME) != 0) |
874 | 0 | return 1; |
875 | | /* Workaround for some buggy certificates */ |
876 | 0 | return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0; |
877 | 0 | } |
878 | 0 | return 1; |
879 | 0 | } |
880 | | |
881 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
882 | | int non_leaf) |
883 | 0 | { |
884 | 0 | int ret = purpose_smime(x, non_leaf); |
885 | |
|
886 | 0 | if (!ret || non_leaf) |
887 | 0 | return ret; |
888 | 0 | return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret; |
889 | 0 | } |
890 | | |
891 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
892 | | int non_leaf) |
893 | 0 | { |
894 | 0 | int ret = purpose_smime(x, non_leaf); |
895 | |
|
896 | 0 | if (!ret || non_leaf) |
897 | 0 | return ret; |
898 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
899 | 0 | } |
900 | | |
901 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
902 | | int non_leaf) |
903 | 0 | { |
904 | 0 | if (non_leaf) { |
905 | 0 | int ca_ret = check_ca(x); |
906 | |
|
907 | 0 | return ca_ret == 2 ? 0 : ca_ret; |
908 | 0 | } |
909 | 0 | return !ku_reject(x, KU_CRL_SIGN); |
910 | 0 | } |
911 | | |
912 | | /* |
913 | | * OCSP helper: this is *not* a full OCSP check. It just checks that each CA |
914 | | * is valid. Additional checks must be made on the chain. |
915 | | */ |
916 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
917 | | int non_leaf) |
918 | 407 | { |
919 | | /* |
920 | | * Must be a valid CA. Should we really support the "I don't know" value |
921 | | * (2)? |
922 | | */ |
923 | 407 | if (non_leaf) |
924 | 178 | return check_ca(x); |
925 | | /* Leaf certificate is checked in OCSP_basic_verify() */ |
926 | 229 | return 1; |
927 | 407 | } |
928 | | |
929 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
930 | | int non_leaf) |
931 | 0 | { |
932 | 0 | int i_ext; |
933 | | |
934 | | /* |
935 | | * If non_leaf is true we must check if this is a valid CA certificate. |
936 | | * The extra requirements by the CA/Browser Forum are not checked. |
937 | | */ |
938 | 0 | if (non_leaf) |
939 | 0 | return check_ca(x); |
940 | | |
941 | | /* |
942 | | * Key Usage is checked according to RFC 5280 and |
943 | | * Extended Key Usage attributes is checked according to RFC 3161. |
944 | | * The extra (and somewhat conflicting) CA/Browser Forum |
945 | | * Baseline Requirements for the Issuance and Management of |
946 | | * Publicly‐Trusted Code Signing Certificates, Version 3.0.0, |
947 | | * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked. |
948 | | */ |
949 | | /* |
950 | | * Check the optional key usage field: |
951 | | * if Key Usage is present, it must be one of digitalSignature |
952 | | * and/or nonRepudiation (other values are not consistent and shall |
953 | | * be rejected). |
954 | | */ |
955 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) != 0 |
956 | 0 | && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) |
957 | 0 | return 0; |
958 | | |
959 | | /* Only timestamp key usage is permitted and it's required. */ |
960 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP) |
961 | 0 | return 0; |
962 | | |
963 | | /* Extended Key Usage MUST be critical */ |
964 | 0 | i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); |
965 | 0 | if (i_ext >= 0 |
966 | 0 | && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext))) |
967 | 0 | return 0; |
968 | 0 | return 1; |
969 | 0 | } |
970 | | |
971 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
972 | | int non_leaf) |
973 | 0 | { |
974 | 0 | int i_ext; |
975 | | |
976 | | /* |
977 | | * If non_leaf is true we must check if this is a valid CA certificate. |
978 | | * The extra requirements by the CA/Browser Forum are not checked. |
979 | | */ |
980 | 0 | if (non_leaf) |
981 | 0 | return check_ca(x); |
982 | | |
983 | | /* |
984 | | * Check the key usage and extended key usage fields: |
985 | | * |
986 | | * Reference: CA/Browser Forum, |
987 | | * Baseline Requirements for the Issuance and Management of |
988 | | * Publicly‐Trusted Code Signing Certificates, Version 3.0.0, |
989 | | * Section 7.1.2.3: Code signing and Timestamp Certificate |
990 | | * |
991 | | * Checking covers Key Usage and Extended Key Usage attributes. |
992 | | * The certificatePolicies, cRLDistributionPoints (CDP), and |
993 | | * authorityInformationAccess (AIA) extensions are so far not checked. |
994 | | */ |
995 | | /* Key Usage */ |
996 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) == 0) |
997 | 0 | return 0; |
998 | 0 | if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0) |
999 | 0 | return 0; |
1000 | 0 | if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0) |
1001 | 0 | return 0; |
1002 | | |
1003 | | /* Key Usage MUST be critical */ |
1004 | 0 | i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1); |
1005 | 0 | if (i_ext < 0) |
1006 | 0 | return 0; |
1007 | 0 | if (i_ext >= 0) { |
1008 | 0 | const X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext); |
1009 | 0 | if (!X509_EXTENSION_get_critical(ext)) |
1010 | 0 | return 0; |
1011 | 0 | } |
1012 | | |
1013 | | /* Extended Key Usage */ |
1014 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0) |
1015 | 0 | return 0; |
1016 | 0 | if ((x->ex_xkusage & XKU_CODE_SIGN) == 0) |
1017 | 0 | return 0; |
1018 | 0 | if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0) |
1019 | 0 | return 0; |
1020 | | |
1021 | 0 | return 1; |
1022 | 0 | } |
1023 | | |
1024 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
1025 | | int non_leaf) |
1026 | 0 | { |
1027 | 0 | return 1; |
1028 | 0 | } |
1029 | | |
1030 | | /*- |
1031 | | * Various checks to see if one certificate potentially issued the second. |
1032 | | * This can be used to prune a set of possible issuer certificates which |
1033 | | * have been looked up using some simple method such as by subject name. |
1034 | | * These are: |
1035 | | * 1. issuer_name(subject) == subject_name(issuer) |
1036 | | * 2. If akid(subject) exists, it matches the respective issuer fields. |
1037 | | * 3. subject signature algorithm == issuer public key algorithm |
1038 | | * 4. If key_usage(issuer) exists, it allows for signing subject. |
1039 | | * Note that this does not include actually checking the signature. |
1040 | | * Returns 0 for OK, or positive for reason for mismatch |
1041 | | * where reason codes match those for X509_verify_cert(). |
1042 | | */ |
1043 | | int X509_check_issued(const X509 *issuer, const X509 *subject) |
1044 | 32.0k | { |
1045 | 32.0k | int ret; |
1046 | | |
1047 | 32.0k | if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK) |
1048 | 28.2k | return ret; |
1049 | 3.85k | return ossl_x509_signing_allowed(issuer, subject); |
1050 | 32.0k | } |
1051 | | |
1052 | | /* |
1053 | | * Do the checks 1., 2., and 3. as described above for X509_check_issued(). |
1054 | | * These are very similar to a section of ossl_x509v3_cache_extensions(). |
1055 | | * If |issuer| equals |subject| (such that self-signature should be checked), |
1056 | | * use the EXFLAG_SS result of ossl_x509v3_cache_extensions(). |
1057 | | */ |
1058 | | int ossl_x509_likely_issued(const X509 *issuer, const X509 *subject) |
1059 | 20.1k | { |
1060 | 20.1k | int ret; |
1061 | | |
1062 | 20.1k | if (X509_NAME_cmp(X509_get_subject_name(issuer), |
1063 | 20.1k | X509_get_issuer_name(subject)) |
1064 | 20.1k | != 0) |
1065 | 10.4k | return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; |
1066 | | |
1067 | | /* set issuer->skid, subject->akid, and subject->ex_flags */ |
1068 | 9.70k | if (!ossl_x509v3_cache_extensions(issuer) |
1069 | 7.09k | || !ossl_x509v3_cache_extensions(subject)) |
1070 | 3.08k | return X509_V_ERR_UNSPECIFIED; |
1071 | | |
1072 | 6.61k | if (issuer == subject |
1073 | 2.72k | || (X509_NAME_cmp(X509_get_issuer_name(issuer), X509_get_issuer_name(subject)) == 0 |
1074 | 1.69k | && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), X509_get0_serialNumber(subject)) == 0)) |
1075 | | /* |
1076 | | * At this point, we can assume that issuer and subject |
1077 | | * are semantically the same cert because they are identical |
1078 | | * or at least have the same issuer and serial number, |
1079 | | * which (for any sane cert issuer) implies equality of the two certs. |
1080 | | * In this case, for consistency with chain building and validation, |
1081 | | * we make our issuance judgment depend on the presence of EXFLAG_SS. |
1082 | | * This is used for corrected chain building in the corner case of |
1083 | | * a self-issued but not actually self-signed trust anchor cert |
1084 | | * without subject and issuer key identifiers (i.e., no SKID and AKID). |
1085 | | */ |
1086 | 3.92k | return (issuer->ex_flags & EXFLAG_SS) != 0 |
1087 | 3.92k | ? X509_V_OK |
1088 | 3.92k | : X509_V_ERR_CERT_SIGNATURE_FAILURE; |
1089 | | |
1090 | 2.68k | ret = X509_check_akid(issuer, subject->akid); |
1091 | 2.68k | if (ret != X509_V_OK) |
1092 | 306 | return ret; |
1093 | | |
1094 | | /* Check if the subject signature alg matches the issuer's PUBKEY alg */ |
1095 | 2.37k | return check_sig_alg_match(X509_get0_pubkey(issuer), subject); |
1096 | 2.68k | } |
1097 | | |
1098 | | /*- |
1099 | | * Check if certificate I<issuer> is allowed to issue certificate I<subject> |
1100 | | * according to the B<keyUsage> field of I<issuer> if present |
1101 | | * depending on any proxyCertInfo extension of I<subject>. |
1102 | | * Returns 0 for OK, or positive for reason for rejection |
1103 | | * where reason codes match those for X509_verify_cert(). |
1104 | | */ |
1105 | | int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject) |
1106 | 6.35k | { |
1107 | 6.35k | if ((subject->ex_flags & EXFLAG_PROXY) != 0) { |
1108 | 4 | if (ku_reject(issuer, KU_DIGITAL_SIGNATURE)) |
1109 | 0 | return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE; |
1110 | 6.35k | } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) { |
1111 | 125 | return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; |
1112 | 125 | } |
1113 | 6.23k | return X509_V_OK; |
1114 | 6.35k | } |
1115 | | |
1116 | | /* |
1117 | | * check if all sub-fields of the authority key identifier information akid, |
1118 | | * as far as present, match the respective subjectKeyIdentifier extension (if |
1119 | | * present in issuer), serialNumber field, and issuer fields of issuer. |
1120 | | * returns X509_V_OK also if akid is NULL because this means no restriction. |
1121 | | */ |
1122 | | int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid) |
1123 | 80.5k | { |
1124 | 80.5k | if (akid == NULL) |
1125 | 62.8k | return X509_V_OK; |
1126 | | |
1127 | | /* Check key ids (if present) */ |
1128 | 17.7k | if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) |
1129 | 1.28k | return X509_V_ERR_AKID_SKID_MISMATCH; |
1130 | | /* Check serial number */ |
1131 | 16.4k | if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial)) |
1132 | 967 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1133 | | /* Check issuer name */ |
1134 | 15.5k | if (akid->issuer) { |
1135 | | /* |
1136 | | * Ugh, for some peculiar reason AKID includes SEQUENCE OF |
1137 | | * GeneralName. So look for a DirName. There may be more than one but |
1138 | | * we only take any notice of the first. |
1139 | | */ |
1140 | 317 | GENERAL_NAMES *gens = akid->issuer; |
1141 | 317 | GENERAL_NAME *gen; |
1142 | 317 | X509_NAME *nm = NULL; |
1143 | 317 | int i; |
1144 | | |
1145 | 585 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
1146 | 409 | gen = sk_GENERAL_NAME_value(gens, i); |
1147 | 409 | if (gen->type == GEN_DIRNAME) { |
1148 | 141 | nm = gen->d.dirn; |
1149 | 141 | break; |
1150 | 141 | } |
1151 | 409 | } |
1152 | 317 | if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0) |
1153 | 83 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1154 | 317 | } |
1155 | 15.4k | return X509_V_OK; |
1156 | 15.5k | } |
1157 | | |
1158 | | uint32_t X509_get_extension_flags(const X509 *x) |
1159 | 30.9k | { |
1160 | | /* Call for side-effect of computing hash and caching extensions */ |
1161 | 30.9k | X509_check_purpose(x, -1, 0); |
1162 | 30.9k | return x->ex_flags; |
1163 | 30.9k | } |
1164 | | |
1165 | | uint32_t X509_get_key_usage(const X509 *x) |
1166 | 19.6k | { |
1167 | | /* Call for side-effect of computing hash and caching extensions */ |
1168 | 19.6k | if (X509_check_purpose(x, -1, 0) != 1) |
1169 | 0 | return 0; |
1170 | 19.6k | return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX; |
1171 | 19.6k | } |
1172 | | |
1173 | | uint32_t X509_get_extended_key_usage(const X509 *x) |
1174 | 8 | { |
1175 | | /* Call for side-effect of computing hash and caching extensions */ |
1176 | 8 | if (X509_check_purpose(x, -1, 0) != 1) |
1177 | 0 | return 0; |
1178 | 8 | return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX; |
1179 | 8 | } |
1180 | | |
1181 | | const ASN1_OCTET_STRING *X509_get0_subject_key_id(const X509 *x) |
1182 | 4.21k | { |
1183 | | /* Call for side-effect of computing hash and caching extensions */ |
1184 | 4.21k | if (X509_check_purpose(x, -1, 0) != 1) |
1185 | 362 | return NULL; |
1186 | 3.85k | return x->skid; |
1187 | 4.21k | } |
1188 | | |
1189 | | const ASN1_OCTET_STRING *X509_get0_authority_key_id(const X509 *x) |
1190 | 0 | { |
1191 | | /* Call for side-effect of computing hash and caching extensions */ |
1192 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1193 | 0 | return NULL; |
1194 | 0 | return (x->akid != NULL ? x->akid->keyid : NULL); |
1195 | 0 | } |
1196 | | |
1197 | | const GENERAL_NAMES *X509_get0_authority_issuer(const X509 *x) |
1198 | 0 | { |
1199 | | /* Call for side-effect of computing hash and caching extensions */ |
1200 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1201 | 0 | return NULL; |
1202 | 0 | return (x->akid != NULL ? x->akid->issuer : NULL); |
1203 | 0 | } |
1204 | | |
1205 | | const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x) |
1206 | 0 | { |
1207 | | /* Call for side-effect of computing hash and caching extensions */ |
1208 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1209 | 0 | return NULL; |
1210 | 0 | return (x->akid != NULL ? x->akid->serial : NULL); |
1211 | 0 | } |
1212 | | |
1213 | | long X509_get_pathlen(const X509 *x) |
1214 | 0 | { |
1215 | | /* Called for side effect of caching extensions */ |
1216 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1217 | 0 | || (x->ex_flags & EXFLAG_BCONS) == 0) |
1218 | 0 | return -1; |
1219 | 0 | return x->ex_pathlen; |
1220 | 0 | } |
1221 | | |
1222 | | long X509_get_proxy_pathlen(const X509 *x) |
1223 | 0 | { |
1224 | | /* Called for side effect of caching extensions */ |
1225 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1226 | 0 | || (x->ex_flags & EXFLAG_PROXY) == 0) |
1227 | 0 | return -1; |
1228 | 0 | return x->ex_pcpathlen; |
1229 | 0 | } |