/src/openssl/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 | | #include "crypto/objects/obj_dat.h" |
19 | | #include "internal/hashfunc.h" |
20 | | |
21 | | static int check_ssl_ca(const X509 *x); |
22 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
23 | | int non_leaf); |
24 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
25 | | int non_leaf); |
26 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
27 | | int non_leaf); |
28 | | static int purpose_smime(const X509 *x, int non_leaf); |
29 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
30 | | int non_leaf); |
31 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
32 | | int non_leaf); |
33 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
34 | | int non_leaf); |
35 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
36 | | int non_leaf); |
37 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
38 | | int non_leaf); |
39 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
40 | | int non_leaf); |
41 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
42 | | int non_leaf); |
43 | | |
44 | | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b); |
45 | | static void xptable_free(X509_PURPOSE *p); |
46 | | |
47 | | /* note that the id must be unique and for the standard entries == idx + 1 */ |
48 | | static X509_PURPOSE xstandard[] = { |
49 | | { X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, |
50 | | check_purpose_ssl_client, "SSL client", "sslclient", NULL }, |
51 | | { X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
52 | | check_purpose_ssl_server, "SSL server", "sslserver", NULL }, |
53 | | { X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
54 | | check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL }, |
55 | | { X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, |
56 | | "S/MIME signing", "smimesign", NULL }, |
57 | | { X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, |
58 | | check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL }, |
59 | | { X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, |
60 | | "CRL signing", "crlsign", NULL }, |
61 | | { X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check_purpose, |
62 | | "Any Purpose", "any", |
63 | | NULL }, |
64 | | { X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, check_purpose_ocsp_helper, |
65 | | "OCSP helper", "ocsphelper", NULL }, |
66 | | { X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, |
67 | | check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign", |
68 | | NULL }, |
69 | | { X509_PURPOSE_CODE_SIGN, X509_TRUST_OBJECT_SIGN, 0, |
70 | | check_purpose_code_sign, "Code signing", "codesign", |
71 | | NULL }, |
72 | | }; |
73 | | |
74 | 0 | #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard) |
75 | | |
76 | | /* the id must be unique, but there may be gaps and maybe table is not sorted */ |
77 | | static STACK_OF(X509_PURPOSE) *xptable = NULL; |
78 | | |
79 | | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b) |
80 | 0 | { |
81 | 0 | return (*a)->purpose - (*b)->purpose; |
82 | 0 | } |
83 | | |
84 | | int X509_check_purpose(const X509 *x, int id, int non_leaf) |
85 | 0 | { |
86 | 0 | int idx; |
87 | 0 | const X509_PURPOSE *pt; |
88 | | |
89 | | /* |
90 | | * TODO: This cast can be dropped when https://github.com/openssl/openssl/pull/30067 |
91 | | * gets merged |
92 | | */ |
93 | 0 | if (!ossl_x509v3_cache_extensions((X509 *)x)) |
94 | 0 | return -1; |
95 | 0 | if (id == -1) |
96 | 0 | return 1; |
97 | | |
98 | 0 | idx = X509_PURPOSE_get_by_id(id); |
99 | 0 | if (idx == -1) |
100 | 0 | return -1; |
101 | 0 | pt = X509_PURPOSE_get0(idx); |
102 | 0 | return pt->check_purpose(pt, x, non_leaf); |
103 | 0 | } |
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 | 0 | { |
135 | 0 | if (idx < 0) |
136 | 0 | return NULL; |
137 | 0 | if (idx < (int)X509_PURPOSE_COUNT) |
138 | 0 | return xstandard + idx; |
139 | 0 | return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT); |
140 | 0 | } |
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 | 0 | { |
158 | 0 | X509_PURPOSE tmp; |
159 | 0 | int idx; |
160 | |
|
161 | 0 | if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX) |
162 | 0 | return purpose - X509_PURPOSE_MIN; |
163 | 0 | if (xptable == NULL) |
164 | 0 | 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_X509V3, 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 | 0 | { |
303 | 0 | return *a - *b; |
304 | 0 | } |
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(const X509_EXTENSION *ex) |
310 | 0 | { |
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 | 0 | static const int supported_nids[] = { |
319 | 0 | NID_netscape_cert_type, /* 71 */ |
320 | 0 | NID_key_usage, /* 83 */ |
321 | 0 | NID_subject_alt_name, /* 85 */ |
322 | 0 | NID_basic_constraints, /* 87 */ |
323 | 0 | NID_certificate_policies, /* 89 */ |
324 | 0 | NID_crl_distribution_points, /* 103 */ |
325 | 0 | NID_ext_key_usage, /* 126 */ |
326 | 0 | #ifndef OPENSSL_NO_RFC3779 |
327 | 0 | NID_sbgp_ipAddrBlock, /* 290 */ |
328 | 0 | NID_sbgp_autonomousSysNum, /* 291 */ |
329 | 0 | #endif |
330 | 0 | NID_id_pkix_OCSP_noCheck, /* 369 */ |
331 | 0 | NID_policy_constraints, /* 401 */ |
332 | 0 | NID_proxyCertInfo, /* 663 */ |
333 | 0 | NID_name_constraints, /* 666 */ |
334 | 0 | NID_policy_mappings, /* 747 */ |
335 | 0 | NID_inhibit_any_policy /* 748 */ |
336 | 0 | }; |
337 | |
|
338 | 0 | int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
339 | |
|
340 | 0 | if (ex_nid == NID_undef) |
341 | 0 | return 0; |
342 | | |
343 | 0 | if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids))) |
344 | 0 | return 1; |
345 | 0 | return 0; |
346 | 0 | } |
347 | | |
348 | | /* Returns 1 on success, 0 if x is invalid, -1 on (internal) error. */ |
349 | | static int setup_dp(const X509 *x, DIST_POINT *dp) |
350 | 0 | { |
351 | 0 | const X509_NAME *iname = NULL; |
352 | 0 | int i; |
353 | |
|
354 | 0 | if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) { |
355 | 0 | ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT); |
356 | 0 | return 0; |
357 | 0 | } |
358 | 0 | if (dp->reasons != NULL) { |
359 | 0 | if (dp->reasons->length > 0) |
360 | 0 | dp->dp_reasons = dp->reasons->data[0]; |
361 | 0 | if (dp->reasons->length > 1) |
362 | 0 | dp->dp_reasons |= (dp->reasons->data[1] << 8); |
363 | 0 | dp->dp_reasons &= CRLDP_ALL_REASONS; |
364 | 0 | } else { |
365 | 0 | dp->dp_reasons = CRLDP_ALL_REASONS; |
366 | 0 | } |
367 | 0 | if (dp->distpoint == NULL || dp->distpoint->type != 1) |
368 | 0 | return 1; |
369 | | |
370 | | /* Handle name fragment given by nameRelativeToCRLIssuer */ |
371 | | /* |
372 | | * Note that the below way of determining iname is not really compliant |
373 | | * with https://tools.ietf.org/html/rfc5280#section-4.2.1.13 |
374 | | * According to it, sk_GENERAL_NAME_num(dp->CRLissuer) MUST be <= 1 |
375 | | * and any CRLissuer could be of type different to GEN_DIRNAME. |
376 | | */ |
377 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { |
378 | 0 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); |
379 | |
|
380 | 0 | if (gen->type == GEN_DIRNAME) { |
381 | 0 | iname = gen->d.directoryName; |
382 | 0 | break; |
383 | 0 | } |
384 | 0 | } |
385 | 0 | if (iname == NULL) |
386 | 0 | iname = X509_get_issuer_name(x); |
387 | 0 | return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1; |
388 | 0 | } |
389 | | |
390 | | /* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */ |
391 | | static int setup_crldp(const X509 *x, STACK_OF(DIST_POINT) **tmp_crldp) |
392 | 0 | { |
393 | 0 | int i; |
394 | |
|
395 | 0 | *tmp_crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL); |
396 | 0 | if (*tmp_crldp == NULL && i != -1) |
397 | 0 | return 0; |
398 | | |
399 | 0 | for (i = 0; i < sk_DIST_POINT_num(*tmp_crldp); i++) { |
400 | 0 | int res = setup_dp(x, sk_DIST_POINT_value(*tmp_crldp, i)); |
401 | |
|
402 | 0 | if (res < 1) |
403 | 0 | return res; |
404 | 0 | } |
405 | 0 | return 1; |
406 | 0 | } |
407 | | |
408 | | /* Check that issuer public key algorithm matches subject signature algorithm */ |
409 | | static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject) |
410 | 0 | { |
411 | 0 | int subj_sig_nid; |
412 | |
|
413 | 0 | if (issuer_key == NULL) |
414 | 0 | return X509_V_ERR_NO_ISSUER_PUBLIC_KEY; |
415 | 0 | if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm), |
416 | 0 | NULL, &subj_sig_nid) |
417 | 0 | == 0) |
418 | 0 | return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
419 | 0 | if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid)) |
420 | 0 | || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss)) |
421 | 0 | return X509_V_OK; |
422 | 0 | return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH; |
423 | 0 | } |
424 | | |
425 | | static unsigned long oid_hash(const void *p) |
426 | 0 | { |
427 | 0 | const ASN1_OBJECT *a = p; |
428 | |
|
429 | 0 | return (unsigned long)ossl_fnv1a_hash((uint8_t *)a->data, a->length); |
430 | 0 | } |
431 | | |
432 | | static int oid_cmp(const void *a, const void *b) |
433 | 0 | { |
434 | 0 | return OBJ_cmp((const ASN1_OBJECT *)a, (const ASN1_OBJECT *)b); |
435 | 0 | } |
436 | | |
437 | | /* |
438 | | * Scan all extensions of a certificate to collect extension-related flags. |
439 | | * Detects duplicate extensions (RFC 5280 section 4.2), the presence of a |
440 | | * freshest CRL extension and unsupported critical extensions. |
441 | | * |
442 | | * In the future, if needed, this scanning function could return the index |
443 | | * of the offending extension on error, allowing the caller to identify which |
444 | | * extension caused the problem and report it via ERR_raise_data(). |
445 | | */ |
446 | | static void scan_ext_flags(const X509 *x509, uint32_t *flags) |
447 | 0 | { |
448 | 0 | OPENSSL_LHASH *h = NULL; |
449 | 0 | uint8_t ex_bitset[(NUM_NID + 7) / 8]; |
450 | |
|
451 | 0 | memset(ex_bitset, 0, sizeof(ex_bitset)); |
452 | | /* A certificate MUST NOT include more than one instance of an extension. */ |
453 | 0 | for (int i = 0; i < X509_get_ext_count(x509); i++) { |
454 | 0 | const X509_EXTENSION *ex = X509_get_ext(x509, i); |
455 | 0 | const ASN1_OBJECT *a = X509_EXTENSION_get_object(ex); |
456 | 0 | int nid = OBJ_obj2nid(a); |
457 | | |
458 | | /* |
459 | | * Known NIDs within the build-time bitset limit are checked for |
460 | | * duplicates in constant time. Unknown OIDs and dynamically registered |
461 | | * NIDs that exceed the limit fall back to duplicate detection via a |
462 | | * hash table. |
463 | | */ |
464 | 0 | if (nid > NID_undef && nid < NUM_NID) { |
465 | 0 | unsigned int ex_bit = nid; |
466 | |
|
467 | 0 | if ((ex_bitset[ex_bit >> 3] & (1u << (ex_bit & 7))) != 0) { |
468 | 0 | *flags |= EXFLAG_DUPLICATE; |
469 | 0 | break; |
470 | 0 | } |
471 | 0 | ex_bitset[ex_bit >> 3] |= (1u << (ex_bit & 7)); |
472 | 0 | } else { |
473 | | /* |
474 | | * Extensions with unknown NID (NID_undef) and dynamically |
475 | | * registered NIDs are handled here by hashing the OID (data/length). |
476 | | * A zero-length OID should not reach this point, but we check for |
477 | | * it anyway and assign the EXFLAG_INVALID flag if it does. |
478 | | */ |
479 | 0 | if (a->length < 1) { |
480 | 0 | *flags |= EXFLAG_INVALID; |
481 | 0 | break; |
482 | 0 | } |
483 | | /* |
484 | | * Hashing the OID should be manageable more cheaply as well, and |
485 | | * without additional dynamic allocations. In the case of this |
486 | | * corner case, itâs not a problem at all, but the other duplicate |
487 | | * detections also require hashing, so for the sake of consistency |
488 | | * it would make sense to use a cheaper construct here later as well. |
489 | | */ |
490 | 0 | if (h == NULL && (h = OPENSSL_LH_new(oid_hash, oid_cmp)) == NULL) |
491 | 0 | break; |
492 | 0 | if (OPENSSL_LH_insert(h, (void *)a) != NULL) { |
493 | 0 | *flags |= EXFLAG_DUPLICATE; |
494 | 0 | break; |
495 | 0 | } |
496 | 0 | } |
497 | 0 | if (nid == NID_freshest_crl) |
498 | 0 | *flags |= EXFLAG_FRESHEST; |
499 | 0 | if (!X509_EXTENSION_get_critical(ex)) |
500 | 0 | continue; |
501 | 0 | if (!X509_supported_extension(ex)) { |
502 | 0 | *flags |= EXFLAG_CRITICAL; |
503 | 0 | break; |
504 | 0 | } |
505 | 0 | } |
506 | 0 | OPENSSL_LH_free(h); |
507 | 0 | } |
508 | | |
509 | 0 | #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS) |
510 | | #define ku_reject(x, usage) \ |
511 | 0 | (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0) |
512 | | #define xku_reject(x, usage) \ |
513 | 0 | (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0) |
514 | | #define ns_reject(x, usage) \ |
515 | 0 | (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0) |
516 | | |
517 | | /* |
518 | | * Cache info on various X.509v3 extensions and further derived information, |
519 | | * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields. |
520 | | * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags. |
521 | | * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully. |
522 | | * Set EXFLAG_INVALID and return 0 in case the certificate is invalid. |
523 | | * |
524 | | * This is usually called by side-effect on objects, and forces us to keep |
525 | | * mutable X509 objects around. We should really make this go away. |
526 | | * In the interest of being able to do so, this function explicitly takes |
527 | | * a const argument and casts away const. |
528 | | */ |
529 | | int ossl_x509v3_cache_extensions(const X509 *const_x) |
530 | 0 | { |
531 | 0 | BASIC_CONSTRAINTS *bs; |
532 | 0 | PROXY_CERT_INFO_EXTENSION *pci; |
533 | 0 | ASN1_BIT_STRING *usage; |
534 | 0 | ASN1_BIT_STRING *ns; |
535 | 0 | EXTENDED_KEY_USAGE *extusage; |
536 | 0 | int i; |
537 | 0 | int res; |
538 | 0 | uint32_t tmp_ex_flags; |
539 | 0 | unsigned char tmp_sha1_hash[SHA_DIGEST_LENGTH]; |
540 | 0 | long tmp_ex_pathlen; |
541 | 0 | long tmp_ex_pcpathlen; |
542 | 0 | uint32_t tmp_ex_kusage; |
543 | 0 | uint32_t tmp_ex_xkusage; |
544 | 0 | uint32_t tmp_ex_nscert; |
545 | 0 | ASN1_OCTET_STRING *tmp_skid; |
546 | 0 | AUTHORITY_KEYID *tmp_akid; |
547 | 0 | STACK_OF(GENERAL_NAME) *tmp_altname; |
548 | 0 | NAME_CONSTRAINTS *tmp_nc; |
549 | 0 | STACK_OF(DIST_POINT) *tmp_crldp = NULL; |
550 | 0 | X509_SIG_INFO tmp_siginf; |
551 | |
|
552 | 0 | #ifdef tsan_ld_acq |
553 | | /* Fast lock-free check, see end of the function for details. */ |
554 | 0 | if (tsan_ld_acq((TSAN_QUALIFIER int *)&const_x->ex_cached)) |
555 | 0 | return (const_x->ex_flags & EXFLAG_INVALID) == 0; |
556 | 0 | #endif |
557 | | |
558 | 0 | if (!CRYPTO_THREAD_read_lock(const_x->lock)) |
559 | 0 | return 0; |
560 | 0 | tmp_ex_flags = const_x->ex_flags; |
561 | 0 | tmp_ex_pcpathlen = const_x->ex_pcpathlen; |
562 | 0 | tmp_ex_kusage = const_x->ex_kusage; |
563 | 0 | tmp_ex_nscert = const_x->ex_nscert; |
564 | |
|
565 | 0 | if ((tmp_ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */ |
566 | 0 | CRYPTO_THREAD_unlock(const_x->lock); |
567 | 0 | return (tmp_ex_flags & EXFLAG_INVALID) == 0; |
568 | 0 | } |
569 | | |
570 | 0 | ERR_set_mark(); |
571 | | |
572 | | /* Cache the SHA1 digest of the cert */ |
573 | 0 | if (!X509_digest(const_x, EVP_sha1(), tmp_sha1_hash, NULL)) |
574 | 0 | tmp_ex_flags |= EXFLAG_NO_FINGERPRINT; |
575 | | |
576 | | /* V1 should mean no extensions ... */ |
577 | 0 | if (X509_get_version(const_x) == X509_VERSION_1) |
578 | 0 | tmp_ex_flags |= EXFLAG_V1; |
579 | | |
580 | | /* Handle basic constraints */ |
581 | 0 | tmp_ex_pathlen = -1; |
582 | 0 | if ((bs = X509_get_ext_d2i(const_x, NID_basic_constraints, &i, NULL)) != NULL) { |
583 | 0 | if (bs->ca) |
584 | 0 | tmp_ex_flags |= EXFLAG_CA; |
585 | 0 | if (bs->pathlen != NULL) { |
586 | | /* |
587 | | * The error case !bs->ca is checked by check_chain() |
588 | | * in case ctx->param->flags & X509_V_FLAG_X509_STRICT |
589 | | */ |
590 | 0 | if (bs->pathlen->type == V_ASN1_NEG_INTEGER) { |
591 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN); |
592 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
593 | 0 | } else { |
594 | 0 | tmp_ex_pathlen = ASN1_INTEGER_get(bs->pathlen); |
595 | 0 | } |
596 | 0 | } |
597 | 0 | BASIC_CONSTRAINTS_free(bs); |
598 | 0 | tmp_ex_flags |= EXFLAG_BCONS; |
599 | 0 | } else if (i != -1) { |
600 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
601 | 0 | } |
602 | | |
603 | | /* Handle proxy certificates */ |
604 | 0 | if ((pci = X509_get_ext_d2i(const_x, NID_proxyCertInfo, &i, NULL)) != NULL) { |
605 | 0 | if ((tmp_ex_flags & EXFLAG_CA) != 0 |
606 | 0 | || X509_get_ext_by_NID(const_x, NID_subject_alt_name, -1) >= 0 |
607 | 0 | || X509_get_ext_by_NID(const_x, NID_issuer_alt_name, -1) >= 0) { |
608 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
609 | 0 | } |
610 | 0 | if (pci->pcPathLengthConstraint != NULL) |
611 | 0 | tmp_ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint); |
612 | 0 | else |
613 | 0 | tmp_ex_pcpathlen = -1; |
614 | 0 | PROXY_CERT_INFO_EXTENSION_free(pci); |
615 | 0 | tmp_ex_flags |= EXFLAG_PROXY; |
616 | 0 | } else if (i != -1) { |
617 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
618 | 0 | } |
619 | | |
620 | | /* Handle (basic) key usage */ |
621 | 0 | if ((usage = X509_get_ext_d2i(const_x, NID_key_usage, &i, NULL)) != NULL) { |
622 | 0 | tmp_ex_kusage = 0; |
623 | 0 | if (usage->length > 0) { |
624 | 0 | tmp_ex_kusage = usage->data[0]; |
625 | 0 | if (usage->length > 1) |
626 | 0 | tmp_ex_kusage |= usage->data[1] << 8; |
627 | 0 | } |
628 | 0 | tmp_ex_flags |= EXFLAG_KUSAGE; |
629 | 0 | ASN1_BIT_STRING_free(usage); |
630 | | /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */ |
631 | 0 | if (tmp_ex_kusage == 0) { |
632 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE); |
633 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
634 | 0 | } |
635 | 0 | } else if (i != -1) { |
636 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
637 | 0 | } |
638 | | |
639 | | /* Handle extended key usage */ |
640 | 0 | tmp_ex_xkusage = 0; |
641 | 0 | if ((extusage = X509_get_ext_d2i(const_x, NID_ext_key_usage, &i, NULL)) != NULL) { |
642 | 0 | tmp_ex_flags |= EXFLAG_XKUSAGE; |
643 | 0 | for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { |
644 | 0 | switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { |
645 | 0 | case NID_server_auth: |
646 | 0 | tmp_ex_xkusage |= XKU_SSL_SERVER; |
647 | 0 | break; |
648 | 0 | case NID_client_auth: |
649 | 0 | tmp_ex_xkusage |= XKU_SSL_CLIENT; |
650 | 0 | break; |
651 | 0 | case NID_email_protect: |
652 | 0 | tmp_ex_xkusage |= XKU_SMIME; |
653 | 0 | break; |
654 | 0 | case NID_code_sign: |
655 | 0 | tmp_ex_xkusage |= XKU_CODE_SIGN; |
656 | 0 | break; |
657 | 0 | case NID_ms_sgc: |
658 | 0 | case NID_ns_sgc: |
659 | 0 | tmp_ex_xkusage |= XKU_SGC; |
660 | 0 | break; |
661 | 0 | case NID_OCSP_sign: |
662 | 0 | tmp_ex_xkusage |= XKU_OCSP_SIGN; |
663 | 0 | break; |
664 | 0 | case NID_time_stamp: |
665 | 0 | tmp_ex_xkusage |= XKU_TIMESTAMP; |
666 | 0 | break; |
667 | 0 | case NID_dvcs: |
668 | 0 | tmp_ex_xkusage |= XKU_DVCS; |
669 | 0 | break; |
670 | 0 | case NID_anyExtendedKeyUsage: |
671 | 0 | tmp_ex_xkusage |= XKU_ANYEKU; |
672 | 0 | break; |
673 | 0 | default: |
674 | | /* Ignore unknown extended key usage */ |
675 | 0 | break; |
676 | 0 | } |
677 | 0 | } |
678 | 0 | sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); |
679 | 0 | } else if (i != -1) { |
680 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
681 | 0 | } |
682 | | |
683 | | /* Handle legacy Netscape extension */ |
684 | 0 | if ((ns = X509_get_ext_d2i(const_x, NID_netscape_cert_type, &i, NULL)) != NULL) { |
685 | 0 | if (ns->length > 0) |
686 | 0 | tmp_ex_nscert = ns->data[0]; |
687 | 0 | else |
688 | 0 | tmp_ex_nscert = 0; |
689 | 0 | tmp_ex_flags |= EXFLAG_NSCERT; |
690 | 0 | ASN1_BIT_STRING_free(ns); |
691 | 0 | } else if (i != -1) { |
692 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
693 | 0 | } |
694 | | |
695 | | /* Handle subject key identifier and issuer/authority key identifier */ |
696 | 0 | tmp_skid = X509_get_ext_d2i(const_x, NID_subject_key_identifier, &i, NULL); |
697 | 0 | if (tmp_skid == NULL && i != -1) |
698 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
699 | |
|
700 | 0 | tmp_akid = X509_get_ext_d2i(const_x, NID_authority_key_identifier, &i, NULL); |
701 | 0 | if (tmp_akid == NULL && i != -1) |
702 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
703 | | |
704 | | /* Setting EXFLAG_SS is equivalent to ossl_x509_likely_issued(const_x, const_x) == X509_V_OK */ |
705 | 0 | if (X509_NAME_cmp(X509_get_subject_name(const_x), X509_get_issuer_name(const_x)) == 0) { |
706 | 0 | tmp_ex_flags |= EXFLAG_SI; /* Certificate is self-issued: subject == issuer */ |
707 | | /* |
708 | | * When the SKID is missing, which is rare for self-issued certs, |
709 | | * we could afford doing the (accurate) actual self-signature check, but |
710 | | * decided against it for efficiency reasons and according to RFC 5280, |
711 | | * CA certs MUST have an SKID and non-root certs MUST have an AKID. |
712 | | */ |
713 | 0 | if (X509_check_akid(const_x, tmp_akid) == X509_V_OK |
714 | 0 | && check_sig_alg_match(X509_get0_pubkey(const_x), const_x) == X509_V_OK) { |
715 | | /* |
716 | | * Assume self-signed if the signature alg matches the pkey alg and |
717 | | * AKID is missing or matches respective fields in the same cert |
718 | | * Not checking if any given key usage extension allows signing. |
719 | | */ |
720 | 0 | tmp_ex_flags |= EXFLAG_SS; |
721 | 0 | } |
722 | 0 | } |
723 | | |
724 | | /* Handle subject alternative names and various other extensions */ |
725 | 0 | tmp_altname = X509_get_ext_d2i(const_x, NID_subject_alt_name, &i, NULL); |
726 | 0 | if (tmp_altname == NULL && i != -1) |
727 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
728 | 0 | tmp_nc = X509_get_ext_d2i(const_x, NID_name_constraints, &i, NULL); |
729 | 0 | if (tmp_nc == NULL && i != -1) |
730 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
731 | | |
732 | | /* Handle CRL distribution point entries */ |
733 | 0 | res = setup_crldp(const_x, &tmp_crldp); |
734 | 0 | if (res == 0) |
735 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
736 | |
|
737 | 0 | #ifndef OPENSSL_NO_RFC3779 |
738 | 0 | STACK_OF(IPAddressFamily) *tmp_rfc3779_addr |
739 | 0 | = X509_get_ext_d2i(const_x, NID_sbgp_ipAddrBlock, &i, NULL); |
740 | 0 | if (tmp_rfc3779_addr == NULL && i != -1) |
741 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
742 | |
|
743 | 0 | struct ASIdentifiers_st *tmp_rfc3779_asid |
744 | 0 | = X509_get_ext_d2i(const_x, NID_sbgp_autonomousSysNum, &i, NULL); |
745 | 0 | if (tmp_rfc3779_asid == NULL && i != -1) |
746 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
747 | 0 | #endif |
748 | |
|
749 | 0 | scan_ext_flags(const_x, &tmp_ex_flags); |
750 | | |
751 | | /* Set x->siginf, ignoring errors due to unsupported algos */ |
752 | 0 | (void)ossl_x509_init_sig_info(const_x, &tmp_siginf); |
753 | |
|
754 | 0 | tmp_ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */ |
755 | 0 | ERR_pop_to_mark(); |
756 | |
|
757 | 0 | CRYPTO_THREAD_unlock(const_x->lock); |
758 | | /* |
759 | | * Now that we've done all the compute intensive work under read lock |
760 | | * do all the updating under a write lock |
761 | | */ |
762 | 0 | if (!CRYPTO_THREAD_write_lock(const_x->lock)) |
763 | 0 | return 0; |
764 | 0 | ((X509 *)const_x)->ex_flags = tmp_ex_flags; |
765 | 0 | ((X509 *)const_x)->ex_pathlen = tmp_ex_pathlen; |
766 | 0 | ((X509 *)const_x)->ex_pcpathlen = tmp_ex_pcpathlen; |
767 | 0 | if (!(tmp_ex_flags & EXFLAG_NO_FINGERPRINT)) |
768 | 0 | memcpy(((X509 *)const_x)->sha1_hash, tmp_sha1_hash, SHA_DIGEST_LENGTH); |
769 | 0 | if (tmp_ex_flags & EXFLAG_KUSAGE) |
770 | 0 | ((X509 *)const_x)->ex_kusage = tmp_ex_kusage; |
771 | 0 | ((X509 *)const_x)->ex_xkusage = tmp_ex_xkusage; |
772 | 0 | if (tmp_ex_flags & EXFLAG_NSCERT) |
773 | 0 | ((X509 *)const_x)->ex_nscert = tmp_ex_nscert; |
774 | 0 | ASN1_OCTET_STRING_free(((X509 *)const_x)->skid); |
775 | 0 | ((X509 *)const_x)->skid = tmp_skid; |
776 | 0 | AUTHORITY_KEYID_free(((X509 *)const_x)->akid); |
777 | 0 | ((X509 *)const_x)->akid = tmp_akid; |
778 | 0 | sk_GENERAL_NAME_pop_free(((X509 *)const_x)->altname, GENERAL_NAME_free); |
779 | 0 | ((X509 *)const_x)->altname = tmp_altname; |
780 | 0 | NAME_CONSTRAINTS_free(((X509 *)const_x)->nc); |
781 | 0 | ((X509 *)const_x)->nc = tmp_nc; |
782 | 0 | sk_DIST_POINT_pop_free(((X509 *)const_x)->crldp, DIST_POINT_free); |
783 | 0 | ((X509 *)const_x)->crldp = tmp_crldp; |
784 | 0 | #ifndef OPENSSL_NO_RFC3779 |
785 | 0 | sk_IPAddressFamily_pop_free(((X509 *)const_x)->rfc3779_addr, IPAddressFamily_free); |
786 | 0 | ((X509 *)const_x)->rfc3779_addr = tmp_rfc3779_addr; |
787 | 0 | ASIdentifiers_free(((X509 *)const_x)->rfc3779_asid); |
788 | 0 | ((X509 *)const_x)->rfc3779_asid = tmp_rfc3779_asid; |
789 | 0 | #endif |
790 | 0 | ((X509 *)const_x)->siginf = tmp_siginf; |
791 | |
|
792 | 0 | #ifdef tsan_st_rel |
793 | 0 | tsan_st_rel((TSAN_QUALIFIER int *)&const_x->ex_cached, 1); |
794 | | /* |
795 | | * Above store triggers fast lock-free check in the beginning of the |
796 | | * function. But one has to ensure that the structure is "stable", i.e. |
797 | | * all stores are visible on all processors. Hence the release fence. |
798 | | */ |
799 | 0 | #endif |
800 | 0 | CRYPTO_THREAD_unlock(const_x->lock); |
801 | 0 | if (tmp_ex_flags & EXFLAG_INVALID) { |
802 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE); |
803 | 0 | return 0; |
804 | 0 | } |
805 | 0 | return 1; |
806 | 0 | } |
807 | | |
808 | | /*- |
809 | | * CA checks common to all purposes |
810 | | * return codes: |
811 | | * 0 not a CA |
812 | | * 1 is a CA |
813 | | * 2 Only possible in older versions of openSSL when basicConstraints are absent |
814 | | * new versions will not return this value. May be a CA |
815 | | * 3 basicConstraints absent but self-signed V1. |
816 | | * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. |
817 | | * 5 Netscape specific CA Flags present |
818 | | */ |
819 | | |
820 | | static int check_ca(const X509 *x) |
821 | 0 | { |
822 | | /* keyUsage if present should allow cert signing */ |
823 | 0 | if (ku_reject(x, KU_KEY_CERT_SIGN)) |
824 | 0 | return 0; |
825 | 0 | if ((x->ex_flags & EXFLAG_BCONS) != 0) { |
826 | | /* If basicConstraints says not a CA then say so */ |
827 | 0 | return (x->ex_flags & EXFLAG_CA) != 0; |
828 | 0 | } else { |
829 | | /* We support V1 roots for... uh, I don't really know why. */ |
830 | 0 | if ((x->ex_flags & V1_ROOT) == V1_ROOT) |
831 | 0 | return 3; |
832 | | /* |
833 | | * If key usage present it must have certSign so tolerate it |
834 | | */ |
835 | 0 | else if ((x->ex_flags & EXFLAG_KUSAGE) != 0) |
836 | 0 | return 4; |
837 | | /* Older certificates could have Netscape-specific CA types */ |
838 | 0 | else if ((x->ex_flags & EXFLAG_NSCERT) != 0 |
839 | 0 | && (x->ex_nscert & NS_ANY_CA) != 0) |
840 | 0 | return 5; |
841 | | /* Can this still be regarded a CA certificate? I doubt it. */ |
842 | 0 | return 0; |
843 | 0 | } |
844 | 0 | } |
845 | | |
846 | | void X509_set_proxy_flag(X509 *x) |
847 | 0 | { |
848 | 0 | if (CRYPTO_THREAD_write_lock(x->lock)) { |
849 | 0 | x->ex_flags |= EXFLAG_PROXY; |
850 | 0 | CRYPTO_THREAD_unlock(x->lock); |
851 | 0 | } |
852 | 0 | } |
853 | | |
854 | | void X509_set_proxy_pathlen(X509 *x, long l) |
855 | 0 | { |
856 | 0 | x->ex_pcpathlen = l; |
857 | 0 | } |
858 | | |
859 | | int X509_check_ca(const X509 *x) |
860 | 0 | { |
861 | | /* Note 0 normally means "not a CA" - but in this case means error. */ |
862 | 0 | if (!ossl_x509v3_cache_extensions(x)) |
863 | 0 | return 0; |
864 | | |
865 | 0 | return check_ca(x); |
866 | 0 | } |
867 | | |
868 | | /* Check SSL CA: common checks for SSL client and server. */ |
869 | | static int check_ssl_ca(const X509 *x) |
870 | 0 | { |
871 | 0 | int ca_ret = check_ca(x); |
872 | |
|
873 | 0 | if (ca_ret == 0) |
874 | 0 | return 0; |
875 | | /* Check nsCertType if present */ |
876 | 0 | return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0; |
877 | 0 | } |
878 | | |
879 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
880 | | int non_leaf) |
881 | 0 | { |
882 | 0 | if (xku_reject(x, XKU_SSL_CLIENT)) |
883 | 0 | return 0; |
884 | 0 | if (non_leaf) |
885 | 0 | return check_ssl_ca(x); |
886 | | /* We need to do digital signatures or key agreement */ |
887 | 0 | if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) |
888 | 0 | return 0; |
889 | | /* nsCertType if present should allow SSL client use */ |
890 | 0 | if (ns_reject(x, NS_SSL_CLIENT)) |
891 | 0 | return 0; |
892 | 0 | return 1; |
893 | 0 | } |
894 | | |
895 | | /* |
896 | | * Key usage needed for TLS/SSL server: digital signature, encipherment or |
897 | | * key agreement. The ssl code can check this more thoroughly for individual |
898 | | * key types. |
899 | | */ |
900 | | #define KU_TLS \ |
901 | | KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT |
902 | | |
903 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
904 | | int non_leaf) |
905 | 0 | { |
906 | 0 | if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC)) |
907 | 0 | return 0; |
908 | 0 | if (non_leaf) |
909 | 0 | return check_ssl_ca(x); |
910 | | |
911 | 0 | if (ns_reject(x, NS_SSL_SERVER)) |
912 | 0 | return 0; |
913 | 0 | if (ku_reject(x, KU_TLS)) |
914 | 0 | return 0; |
915 | | |
916 | 0 | return 1; |
917 | 0 | } |
918 | | |
919 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
920 | | int non_leaf) |
921 | 0 | { |
922 | 0 | int ret = check_purpose_ssl_server(xp, x, non_leaf); |
923 | |
|
924 | 0 | if (!ret || non_leaf) |
925 | 0 | return ret; |
926 | | /* We need to encipher or Netscape complains */ |
927 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
928 | 0 | } |
929 | | |
930 | | /* common S/MIME client checks */ |
931 | | static int purpose_smime(const X509 *x, int non_leaf) |
932 | 0 | { |
933 | 0 | if (xku_reject(x, XKU_SMIME)) |
934 | 0 | return 0; |
935 | 0 | if (non_leaf) { |
936 | 0 | int ca_ret = check_ca(x); |
937 | |
|
938 | 0 | if (ca_ret == 0) |
939 | 0 | return 0; |
940 | | /* Check nsCertType if present */ |
941 | 0 | if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0) |
942 | 0 | return ca_ret; |
943 | 0 | else |
944 | 0 | return 0; |
945 | 0 | } |
946 | 0 | if ((x->ex_flags & EXFLAG_NSCERT) != 0) { |
947 | 0 | if ((x->ex_nscert & NS_SMIME) != 0) |
948 | 0 | return 1; |
949 | | /* Workaround for some buggy certificates */ |
950 | 0 | return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0; |
951 | 0 | } |
952 | 0 | return 1; |
953 | 0 | } |
954 | | |
955 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
956 | | int non_leaf) |
957 | 0 | { |
958 | 0 | int ret = purpose_smime(x, non_leaf); |
959 | |
|
960 | 0 | if (!ret || non_leaf) |
961 | 0 | return ret; |
962 | 0 | return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret; |
963 | 0 | } |
964 | | |
965 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
966 | | int non_leaf) |
967 | 0 | { |
968 | 0 | int ret = purpose_smime(x, non_leaf); |
969 | |
|
970 | 0 | if (!ret || non_leaf) |
971 | 0 | return ret; |
972 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
973 | 0 | } |
974 | | |
975 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
976 | | int non_leaf) |
977 | 0 | { |
978 | 0 | if (non_leaf) { |
979 | 0 | int ca_ret = check_ca(x); |
980 | |
|
981 | 0 | return ca_ret == 2 ? 0 : ca_ret; |
982 | 0 | } |
983 | 0 | return !ku_reject(x, KU_CRL_SIGN); |
984 | 0 | } |
985 | | |
986 | | /* |
987 | | * OCSP helper: this is *not* a full OCSP check. It just checks that each CA |
988 | | * is valid. Additional checks must be made on the chain. |
989 | | */ |
990 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
991 | | int non_leaf) |
992 | 0 | { |
993 | | /* |
994 | | * Must be a valid CA. Should we really support the "I don't know" value |
995 | | * (2)? |
996 | | */ |
997 | 0 | if (non_leaf) |
998 | 0 | return check_ca(x); |
999 | | /* Leaf certificate is checked in OCSP_basic_verify() */ |
1000 | 0 | return 1; |
1001 | 0 | } |
1002 | | |
1003 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
1004 | | int non_leaf) |
1005 | 0 | { |
1006 | 0 | int i_ext; |
1007 | | |
1008 | | /* |
1009 | | * If non_leaf is true we must check if this is a valid CA certificate. |
1010 | | * The extra requirements by the CA/Browser Forum are not checked. |
1011 | | */ |
1012 | 0 | if (non_leaf) |
1013 | 0 | return check_ca(x); |
1014 | | |
1015 | | /* |
1016 | | * Key Usage is checked according to RFC 5280 and |
1017 | | * Extended Key Usage attributes is checked according to RFC 3161. |
1018 | | * The extra (and somewhat conflicting) CA/Browser Forum |
1019 | | * Baseline Requirements for the Issuance and Management of |
1020 | | * PubliclyâTrusted Code Signing Certificates, Version 3.0.0, |
1021 | | * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked. |
1022 | | */ |
1023 | | /* |
1024 | | * Check the optional key usage field: |
1025 | | * if Key Usage is present, it must be one of digitalSignature |
1026 | | * and/or nonRepudiation (other values are not consistent and shall |
1027 | | * be rejected). |
1028 | | */ |
1029 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) != 0 |
1030 | 0 | && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) |
1031 | 0 | return 0; |
1032 | | |
1033 | | /* Only timestamp key usage is permitted and it's required. */ |
1034 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP) |
1035 | 0 | return 0; |
1036 | | |
1037 | | /* Extended Key Usage MUST be critical */ |
1038 | 0 | i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); |
1039 | 0 | if (i_ext >= 0 |
1040 | 0 | && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext))) |
1041 | 0 | return 0; |
1042 | 0 | return 1; |
1043 | 0 | } |
1044 | | |
1045 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
1046 | | int non_leaf) |
1047 | 0 | { |
1048 | 0 | int i_ext; |
1049 | | |
1050 | | /* |
1051 | | * If non_leaf is true we must check if this is a valid CA certificate. |
1052 | | * The extra requirements by the CA/Browser Forum are not checked. |
1053 | | */ |
1054 | 0 | if (non_leaf) |
1055 | 0 | return check_ca(x); |
1056 | | |
1057 | | /* |
1058 | | * Check the key usage and extended key usage fields: |
1059 | | * |
1060 | | * Reference: CA/Browser Forum, |
1061 | | * Baseline Requirements for the Issuance and Management of |
1062 | | * PubliclyâTrusted Code Signing Certificates, Version 3.0.0, |
1063 | | * Section 7.1.2.3: Code signing and Timestamp Certificate |
1064 | | * |
1065 | | * Checking covers Key Usage and Extended Key Usage attributes. |
1066 | | * The certificatePolicies, cRLDistributionPoints (CDP), and |
1067 | | * authorityInformationAccess (AIA) extensions are so far not checked. |
1068 | | */ |
1069 | | /* Key Usage */ |
1070 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) == 0) |
1071 | 0 | return 0; |
1072 | 0 | if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0) |
1073 | 0 | return 0; |
1074 | 0 | if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0) |
1075 | 0 | return 0; |
1076 | | |
1077 | | /* Key Usage MUST be critical */ |
1078 | 0 | i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1); |
1079 | 0 | if (i_ext < 0) |
1080 | 0 | return 0; |
1081 | 0 | if (i_ext >= 0) { |
1082 | 0 | const X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext); |
1083 | 0 | if (!X509_EXTENSION_get_critical(ext)) |
1084 | 0 | return 0; |
1085 | 0 | } |
1086 | | |
1087 | | /* Extended Key Usage */ |
1088 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0) |
1089 | 0 | return 0; |
1090 | 0 | if ((x->ex_xkusage & XKU_CODE_SIGN) == 0) |
1091 | 0 | return 0; |
1092 | 0 | if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0) |
1093 | 0 | return 0; |
1094 | | |
1095 | 0 | return 1; |
1096 | 0 | } |
1097 | | |
1098 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
1099 | | int non_leaf) |
1100 | 0 | { |
1101 | 0 | return 1; |
1102 | 0 | } |
1103 | | |
1104 | | /*- |
1105 | | * Various checks to see if one certificate potentially issued the second. |
1106 | | * This can be used to prune a set of possible issuer certificates which |
1107 | | * have been looked up using some simple method such as by subject name. |
1108 | | * These are: |
1109 | | * 1. issuer_name(subject) == subject_name(issuer) |
1110 | | * 2. If akid(subject) exists, it matches the respective issuer fields. |
1111 | | * 3. subject signature algorithm == issuer public key algorithm |
1112 | | * 4. If key_usage(issuer) exists, it allows for signing subject. |
1113 | | * Note that this does not include actually checking the signature. |
1114 | | * Returns 0 for OK, or positive for reason for mismatch |
1115 | | * where reason codes match those for X509_verify_cert(). |
1116 | | */ |
1117 | | int X509_check_issued(const X509 *issuer, const X509 *subject) |
1118 | 0 | { |
1119 | 0 | int ret; |
1120 | |
|
1121 | 0 | if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK) |
1122 | 0 | return ret; |
1123 | 0 | return ossl_x509_signing_allowed(issuer, subject); |
1124 | 0 | } |
1125 | | |
1126 | | /* |
1127 | | * Do the checks 1., 2., and 3. as described above for X509_check_issued(). |
1128 | | * These are very similar to a section of ossl_x509v3_cache_extensions(). |
1129 | | * If |issuer| equals |subject| (such that self-signature should be checked), |
1130 | | * use the EXFLAG_SS result of ossl_x509v3_cache_extensions(). |
1131 | | */ |
1132 | | int ossl_x509_likely_issued(const X509 *issuer, const X509 *subject) |
1133 | 0 | { |
1134 | 0 | int ret; |
1135 | |
|
1136 | 0 | if (X509_NAME_cmp(X509_get_subject_name(issuer), |
1137 | 0 | X509_get_issuer_name(subject)) |
1138 | 0 | != 0) |
1139 | 0 | return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; |
1140 | | |
1141 | | /* set issuer->skid, subject->akid, and subject->ex_flags */ |
1142 | 0 | if (!ossl_x509v3_cache_extensions(issuer) |
1143 | 0 | || !ossl_x509v3_cache_extensions(subject)) |
1144 | 0 | return X509_V_ERR_UNSPECIFIED; |
1145 | | |
1146 | 0 | if (issuer == subject |
1147 | 0 | || (X509_NAME_cmp(X509_get_issuer_name(issuer), X509_get_issuer_name(subject)) == 0 |
1148 | 0 | && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), X509_get0_serialNumber(subject)) == 0)) |
1149 | | /* |
1150 | | * At this point, we can assume that issuer and subject |
1151 | | * are semantically the same cert because they are identical |
1152 | | * or at least have the same issuer and serial number, |
1153 | | * which (for any sane cert issuer) implies equality of the two certs. |
1154 | | * In this case, for consistency with chain building and validation, |
1155 | | * we make our issuance judgment depend on the presence of EXFLAG_SS. |
1156 | | * This is used for corrected chain building in the corner case of |
1157 | | * a self-issued but not actually self-signed trust anchor cert |
1158 | | * without subject and issuer key identifiers (i.e., no SKID and AKID). |
1159 | | */ |
1160 | 0 | return (issuer->ex_flags & EXFLAG_SS) != 0 |
1161 | 0 | ? X509_V_OK |
1162 | 0 | : X509_V_ERR_CERT_SIGNATURE_FAILURE; |
1163 | | |
1164 | 0 | ret = X509_check_akid(issuer, subject->akid); |
1165 | 0 | if (ret != X509_V_OK) |
1166 | 0 | return ret; |
1167 | | |
1168 | | /* Check if the subject signature alg matches the issuer's PUBKEY alg */ |
1169 | 0 | return check_sig_alg_match(X509_get0_pubkey(issuer), subject); |
1170 | 0 | } |
1171 | | |
1172 | | /*- |
1173 | | * Check if certificate I<issuer> is allowed to issue certificate I<subject> |
1174 | | * according to the B<keyUsage> field of I<issuer> if present |
1175 | | * depending on any proxyCertInfo extension of I<subject>. |
1176 | | * Returns 0 for OK, or positive for reason for rejection |
1177 | | * where reason codes match those for X509_verify_cert(). |
1178 | | */ |
1179 | | int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject) |
1180 | 0 | { |
1181 | 0 | if ((subject->ex_flags & EXFLAG_PROXY) != 0) { |
1182 | 0 | if (ku_reject(issuer, KU_DIGITAL_SIGNATURE)) |
1183 | 0 | return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE; |
1184 | 0 | } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) { |
1185 | 0 | return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; |
1186 | 0 | } |
1187 | 0 | return X509_V_OK; |
1188 | 0 | } |
1189 | | |
1190 | | /* |
1191 | | * check if all sub-fields of the authority key identifier information akid, |
1192 | | * as far as present, match the respective subjectKeyIdentifier extension (if |
1193 | | * present in issuer), serialNumber field, and issuer fields of issuer. |
1194 | | * returns X509_V_OK also if akid is NULL because this means no restriction. |
1195 | | */ |
1196 | | int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid) |
1197 | 0 | { |
1198 | 0 | if (akid == NULL) |
1199 | 0 | return X509_V_OK; |
1200 | | |
1201 | | /* Check key ids (if present) */ |
1202 | 0 | if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) |
1203 | 0 | return X509_V_ERR_AKID_SKID_MISMATCH; |
1204 | | /* Check serial number */ |
1205 | 0 | if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial)) |
1206 | 0 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1207 | | /* Check issuer name */ |
1208 | 0 | if (akid->issuer) { |
1209 | | /* |
1210 | | * Ugh, for some peculiar reason AKID includes SEQUENCE OF |
1211 | | * GeneralName. So look for a DirName. There may be more than one but |
1212 | | * we only take any notice of the first. |
1213 | | */ |
1214 | 0 | GENERAL_NAMES *gens = akid->issuer; |
1215 | 0 | GENERAL_NAME *gen; |
1216 | 0 | X509_NAME *nm = NULL; |
1217 | 0 | int i; |
1218 | |
|
1219 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
1220 | 0 | gen = sk_GENERAL_NAME_value(gens, i); |
1221 | 0 | if (gen->type == GEN_DIRNAME) { |
1222 | 0 | nm = gen->d.dirn; |
1223 | 0 | break; |
1224 | 0 | } |
1225 | 0 | } |
1226 | 0 | if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0) |
1227 | 0 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1228 | 0 | } |
1229 | 0 | return X509_V_OK; |
1230 | 0 | } |
1231 | | |
1232 | | uint32_t X509_get_extension_flags(const X509 *x) |
1233 | 0 | { |
1234 | | /* Call for side-effect of computing hash and caching extensions */ |
1235 | 0 | X509_check_purpose(x, -1, 0); |
1236 | 0 | return x->ex_flags; |
1237 | 0 | } |
1238 | | |
1239 | | uint32_t X509_get_key_usage(const X509 *x) |
1240 | 0 | { |
1241 | | /* Call for side-effect of computing hash and caching extensions */ |
1242 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1243 | 0 | return 0; |
1244 | 0 | return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX; |
1245 | 0 | } |
1246 | | |
1247 | | uint32_t X509_get_extended_key_usage(const X509 *x) |
1248 | 0 | { |
1249 | | /* Call for side-effect of computing hash and caching extensions */ |
1250 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1251 | 0 | return 0; |
1252 | 0 | return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX; |
1253 | 0 | } |
1254 | | |
1255 | | const ASN1_OCTET_STRING *X509_get0_subject_key_id(const X509 *x) |
1256 | 0 | { |
1257 | | /* Call for side-effect of computing hash and caching extensions */ |
1258 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1259 | 0 | return NULL; |
1260 | 0 | return x->skid; |
1261 | 0 | } |
1262 | | |
1263 | | const ASN1_OCTET_STRING *X509_get0_authority_key_id(const X509 *x) |
1264 | 0 | { |
1265 | | /* Call for side-effect of computing hash and caching extensions */ |
1266 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1267 | 0 | return NULL; |
1268 | 0 | return (x->akid != NULL ? x->akid->keyid : NULL); |
1269 | 0 | } |
1270 | | |
1271 | | const GENERAL_NAMES *X509_get0_authority_issuer(const X509 *x) |
1272 | 0 | { |
1273 | | /* Call for side-effect of computing hash and caching extensions */ |
1274 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1275 | 0 | return NULL; |
1276 | 0 | return (x->akid != NULL ? x->akid->issuer : NULL); |
1277 | 0 | } |
1278 | | |
1279 | | const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x) |
1280 | 0 | { |
1281 | | /* Call for side-effect of computing hash and caching extensions */ |
1282 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1283 | 0 | return NULL; |
1284 | 0 | return (x->akid != NULL ? x->akid->serial : NULL); |
1285 | 0 | } |
1286 | | |
1287 | | long X509_get_pathlen(const X509 *x) |
1288 | 0 | { |
1289 | | /* Called for side effect of caching extensions */ |
1290 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1291 | 0 | || (x->ex_flags & EXFLAG_BCONS) == 0) |
1292 | 0 | return -1; |
1293 | 0 | return x->ex_pathlen; |
1294 | 0 | } |
1295 | | |
1296 | | long X509_get_proxy_pathlen(const X509 *x) |
1297 | 0 | { |
1298 | | /* Called for side effect of caching extensions */ |
1299 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1300 | 0 | || (x->ex_flags & EXFLAG_PROXY) == 0) |
1301 | 0 | return -1; |
1302 | 0 | return x->ex_pcpathlen; |
1303 | 0 | } |