/src/openssl41/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 "crypto/siphash.h" |
20 | | #include <openssl/rand.h> |
21 | | |
22 | | static int check_ssl_ca(const X509 *x); |
23 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
24 | | int non_leaf); |
25 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
26 | | int non_leaf); |
27 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
28 | | int non_leaf); |
29 | | static int purpose_smime(const X509 *x, int non_leaf); |
30 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
31 | | int non_leaf); |
32 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
33 | | int non_leaf); |
34 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
35 | | int non_leaf); |
36 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
37 | | int non_leaf); |
38 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
39 | | int non_leaf); |
40 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
41 | | int non_leaf); |
42 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
43 | | int non_leaf); |
44 | | static int check_name_constraints(const NAME_CONSTRAINTS *nc); |
45 | | |
46 | | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b); |
47 | | static void xptable_free(X509_PURPOSE *p); |
48 | | |
49 | | /* note that the id must be unique and for the standard entries == idx + 1 */ |
50 | | static X509_PURPOSE xstandard[] = { |
51 | | { X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, |
52 | | check_purpose_ssl_client, "SSL client", "sslclient", NULL }, |
53 | | { X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
54 | | check_purpose_ssl_server, "SSL server", "sslserver", NULL }, |
55 | | { X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
56 | | check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL }, |
57 | | { X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, |
58 | | "S/MIME signing", "smimesign", NULL }, |
59 | | { X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, |
60 | | check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL }, |
61 | | { X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, |
62 | | "CRL signing", "crlsign", NULL }, |
63 | | { X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check_purpose, |
64 | | "Any Purpose", "any", |
65 | | NULL }, |
66 | | { X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, check_purpose_ocsp_helper, |
67 | | "OCSP helper", "ocsphelper", NULL }, |
68 | | { X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, |
69 | | check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign", |
70 | | NULL }, |
71 | | { X509_PURPOSE_CODE_SIGN, X509_TRUST_OBJECT_SIGN, 0, |
72 | | check_purpose_code_sign, "Code signing", "codesign", |
73 | | NULL }, |
74 | | }; |
75 | | |
76 | 823 | #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard) |
77 | | |
78 | | /* the id must be unique, but there may be gaps and maybe table is not sorted */ |
79 | | static STACK_OF(X509_PURPOSE) *xptable = NULL; |
80 | | |
81 | | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b) |
82 | 0 | { |
83 | 0 | return (*a)->purpose - (*b)->purpose; |
84 | 0 | } |
85 | | |
86 | | int X509_check_purpose(const X509 *x, int id, int non_leaf) |
87 | 181k | { |
88 | 181k | int idx; |
89 | 181k | const X509_PURPOSE *pt; |
90 | | |
91 | | /* |
92 | | * TODO: This cast can be dropped when https://github.com/openssl/openssl/pull/30067 |
93 | | * gets merged |
94 | | */ |
95 | 181k | if (!ossl_x509v3_cache_extensions((X509 *)x)) |
96 | 45.3k | return -1; |
97 | 136k | if (id == -1) |
98 | 136k | return 1; |
99 | | |
100 | 499 | idx = X509_PURPOSE_get_by_id(id); |
101 | 499 | if (idx == -1) |
102 | 0 | return -1; |
103 | 499 | pt = X509_PURPOSE_get0(idx); |
104 | 499 | return pt->check_purpose(pt, x, non_leaf); |
105 | 499 | } |
106 | | |
107 | | /* resets to default (any) purpose if purpose == X509_PURPOSE_DEFAULT_ANY (0) */ |
108 | | int X509_PURPOSE_set(int *p, int purpose) |
109 | 0 | { |
110 | 0 | if (purpose != X509_PURPOSE_DEFAULT_ANY && X509_PURPOSE_get_by_id(purpose) == -1) { |
111 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE); |
112 | 0 | return 0; |
113 | 0 | } |
114 | 0 | *p = purpose; |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | | int X509_PURPOSE_get_count(void) |
119 | 0 | { |
120 | 0 | if (!xptable) |
121 | 0 | return X509_PURPOSE_COUNT; |
122 | 0 | return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT; |
123 | 0 | } |
124 | | |
125 | | /* find smallest identifier not yet taken - note there might be gaps */ |
126 | | int X509_PURPOSE_get_unused_id(ossl_unused OSSL_LIB_CTX *libctx) |
127 | 0 | { |
128 | 0 | int id = X509_PURPOSE_MAX + 1; |
129 | |
|
130 | 0 | while (X509_PURPOSE_get_by_id(id) != -1) |
131 | 0 | id++; |
132 | 0 | return id; /* is guaranteed to be unique and > X509_PURPOSE_MAX and != 0 */ |
133 | 0 | } |
134 | | |
135 | | X509_PURPOSE *X509_PURPOSE_get0(int idx) |
136 | 71.5k | { |
137 | 71.5k | if (idx < 0) |
138 | 70.6k | return NULL; |
139 | 823 | if (idx < (int)X509_PURPOSE_COUNT) |
140 | 823 | return xstandard + idx; |
141 | 0 | return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT); |
142 | 823 | } |
143 | | |
144 | | int X509_PURPOSE_get_by_sname(const char *sname) |
145 | 0 | { |
146 | 0 | int i; |
147 | 0 | X509_PURPOSE *xptmp; |
148 | |
|
149 | 0 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { |
150 | 0 | xptmp = X509_PURPOSE_get0(i); |
151 | 0 | if (strcmp(xptmp->sname, sname) == 0) |
152 | 0 | return i; |
153 | 0 | } |
154 | 0 | return -1; |
155 | 0 | } |
156 | | |
157 | | /* Returns -1 on error, else an index => 0 in standard/extended purpose table */ |
158 | | int X509_PURPOSE_get_by_id(int purpose) |
159 | 71.5k | { |
160 | 71.5k | X509_PURPOSE tmp; |
161 | 71.5k | int idx; |
162 | | |
163 | 71.5k | if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX) |
164 | 823 | return purpose - X509_PURPOSE_MIN; |
165 | 70.6k | if (xptable == NULL) |
166 | 70.6k | return -1; |
167 | 0 | tmp.purpose = purpose; |
168 | 0 | idx = sk_X509_PURPOSE_find(xptable, &tmp); |
169 | 0 | if (idx < 0) |
170 | 0 | return -1; |
171 | 0 | return idx + X509_PURPOSE_COUNT; |
172 | 0 | } |
173 | | |
174 | | /* |
175 | | * Add purpose entry identified by |sname|. |id| must be >= X509_PURPOSE_MIN. |
176 | | * May also be used to modify existing entry, including changing its id. |
177 | | */ |
178 | | int X509_PURPOSE_add(int id, int trust, int flags, |
179 | | int (*ck)(const X509_PURPOSE *, const X509 *, int), |
180 | | const char *name, const char *sname, void *arg) |
181 | 0 | { |
182 | 0 | int old_id = 0; |
183 | 0 | int idx; |
184 | 0 | X509_PURPOSE *ptmp; |
185 | |
|
186 | 0 | if (id < X509_PURPOSE_MIN) { |
187 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE); |
188 | 0 | return 0; |
189 | 0 | } |
190 | 0 | if (trust < X509_TRUST_DEFAULT || name == NULL || sname == NULL || ck == NULL) { |
191 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT); |
192 | 0 | return 0; |
193 | 0 | } |
194 | | |
195 | | /* This is set according to what we change: application can't set it */ |
196 | 0 | flags &= ~X509_PURPOSE_DYNAMIC; |
197 | | /* This will always be set for application modified trust entries */ |
198 | 0 | flags |= X509_PURPOSE_DYNAMIC_NAME; |
199 | | |
200 | | /* Get existing entry if any */ |
201 | 0 | idx = X509_PURPOSE_get_by_sname(sname); |
202 | 0 | if (idx == -1) { /* Need a new entry */ |
203 | 0 | if (X509_PURPOSE_get_by_id(id) != -1) { |
204 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE); |
205 | 0 | return 0; |
206 | 0 | } |
207 | 0 | if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) |
208 | 0 | return 0; |
209 | 0 | ptmp->flags = X509_PURPOSE_DYNAMIC; |
210 | 0 | } else { |
211 | 0 | ptmp = X509_PURPOSE_get0(idx); |
212 | 0 | old_id = ptmp->purpose; |
213 | 0 | if (id != old_id && X509_PURPOSE_get_by_id(id) != -1) { |
214 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE); |
215 | 0 | return 0; |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | /* OPENSSL_free existing name if dynamic */ |
220 | 0 | if ((ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { |
221 | 0 | OPENSSL_free(ptmp->name); |
222 | 0 | OPENSSL_free(ptmp->sname); |
223 | 0 | } |
224 | | /* Dup supplied name */ |
225 | 0 | ptmp->name = OPENSSL_strdup(name); |
226 | 0 | ptmp->sname = OPENSSL_strdup(sname); |
227 | 0 | if (ptmp->name == NULL || ptmp->sname == NULL) |
228 | 0 | goto err; |
229 | | /* Keep the dynamic flag of existing entry */ |
230 | 0 | ptmp->flags &= X509_PURPOSE_DYNAMIC; |
231 | | /* Set all other flags */ |
232 | 0 | ptmp->flags |= flags; |
233 | |
|
234 | 0 | ptmp->purpose = id; |
235 | 0 | ptmp->trust = trust; |
236 | 0 | ptmp->check_purpose = ck; |
237 | 0 | ptmp->usr_data = arg; |
238 | | |
239 | | /* If its a new entry manage the dynamic table */ |
240 | 0 | if (idx == -1) { |
241 | 0 | if (xptable == NULL |
242 | 0 | && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) { |
243 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
244 | 0 | goto err; |
245 | 0 | } |
246 | 0 | if (!sk_X509_PURPOSE_push(xptable, ptmp)) { |
247 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
248 | 0 | goto err; |
249 | 0 | } |
250 | 0 | } else if (id != old_id) { |
251 | | /* on changing existing entry id, make sure to reset 'sorted' */ |
252 | 0 | (void)sk_X509_PURPOSE_set(xptable, idx, ptmp); |
253 | 0 | } |
254 | 0 | return 1; |
255 | 0 | err: |
256 | 0 | if (idx == -1) { |
257 | 0 | OPENSSL_free(ptmp->name); |
258 | 0 | OPENSSL_free(ptmp->sname); |
259 | 0 | OPENSSL_free(ptmp); |
260 | 0 | } |
261 | 0 | return 0; |
262 | 0 | } |
263 | | |
264 | | static void xptable_free(X509_PURPOSE *p) |
265 | 0 | { |
266 | 0 | if (p == NULL) |
267 | 0 | return; |
268 | 0 | if ((p->flags & X509_PURPOSE_DYNAMIC) != 0) { |
269 | 0 | if ((p->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { |
270 | 0 | OPENSSL_free(p->name); |
271 | 0 | OPENSSL_free(p->sname); |
272 | 0 | } |
273 | 0 | OPENSSL_free(p); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | void X509_PURPOSE_cleanup(void) |
278 | 0 | { |
279 | 0 | sk_X509_PURPOSE_pop_free(xptable, xptable_free); |
280 | 0 | xptable = NULL; |
281 | 0 | } |
282 | | |
283 | | int X509_PURPOSE_get_id(const X509_PURPOSE *xp) |
284 | 0 | { |
285 | 0 | return xp->purpose; |
286 | 0 | } |
287 | | |
288 | | char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp) |
289 | 0 | { |
290 | 0 | return xp->name; |
291 | 0 | } |
292 | | |
293 | | char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) |
294 | 0 | { |
295 | 0 | return xp->sname; |
296 | 0 | } |
297 | | |
298 | | int X509_PURPOSE_get_trust(const X509_PURPOSE *xp) |
299 | 0 | { |
300 | 0 | return xp->trust; |
301 | 0 | } |
302 | | |
303 | | static int nid_cmp(const int *a, const int *b) |
304 | 132k | { |
305 | 132k | return *a - *b; |
306 | 132k | } |
307 | | |
308 | | DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid); |
309 | | IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid); |
310 | | |
311 | | int X509_supported_extension(const X509_EXTENSION *ex) |
312 | 58.2k | { |
313 | | /* |
314 | | * This table is a list of the NIDs of supported extensions: that is |
315 | | * those which are used by the verify process. If an extension is |
316 | | * critical and doesn't appear in this list then the verify process will |
317 | | * normally reject the certificate. The list must be kept in numerical |
318 | | * order because it will be searched using bsearch. |
319 | | */ |
320 | 58.2k | static const int supported_nids[] = { |
321 | 58.2k | NID_netscape_cert_type, /* 71 */ |
322 | 58.2k | NID_key_usage, /* 83 */ |
323 | 58.2k | NID_subject_alt_name, /* 85 */ |
324 | 58.2k | NID_basic_constraints, /* 87 */ |
325 | 58.2k | NID_certificate_policies, /* 89 */ |
326 | 58.2k | NID_crl_distribution_points, /* 103 */ |
327 | 58.2k | NID_ext_key_usage, /* 126 */ |
328 | 58.2k | #ifndef OPENSSL_NO_RFC3779 |
329 | 58.2k | NID_sbgp_ipAddrBlock, /* 290 */ |
330 | 58.2k | NID_sbgp_autonomousSysNum, /* 291 */ |
331 | 58.2k | #endif |
332 | 58.2k | NID_id_pkix_OCSP_noCheck, /* 369 */ |
333 | 58.2k | NID_policy_constraints, /* 401 */ |
334 | 58.2k | NID_proxyCertInfo, /* 663 */ |
335 | 58.2k | NID_name_constraints, /* 666 */ |
336 | 58.2k | NID_policy_mappings, /* 747 */ |
337 | 58.2k | NID_inhibit_any_policy /* 748 */ |
338 | 58.2k | }; |
339 | | |
340 | 58.2k | int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
341 | | |
342 | 58.2k | if (ex_nid == NID_undef) |
343 | 5.20k | return 0; |
344 | | |
345 | 53.0k | if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids))) |
346 | 51.6k | return 1; |
347 | 1.37k | return 0; |
348 | 53.0k | } |
349 | | |
350 | | /* |
351 | | * Returns 1 on success, 0 if x is invalid. |
352 | | * |
353 | | * The full name of a nameRelativeToCRLIssuer distribution point is not |
354 | | * computed here. Doing so for every parsed certificate cost an |
355 | | * X509_NAME_dup() of the issuer name per relative distribution point, |
356 | | * which a certificate with many such entries could turn into hundreds of |
357 | | * megabytes of heap on a plain TLS handshake, while the result is only |
358 | | * needed when a CRL is actually being matched against the certificate. |
359 | | * That name is now built on demand in the CRL checking code instead. |
360 | | */ |
361 | | static int setup_dp(DIST_POINT *dp) |
362 | 2.50k | { |
363 | 2.50k | if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) { |
364 | 50 | ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT); |
365 | 50 | return 0; |
366 | 50 | } |
367 | 2.45k | if (dp->reasons != NULL) { |
368 | 36 | if (dp->reasons->length > 0) |
369 | 34 | dp->dp_reasons = dp->reasons->data[0]; |
370 | 36 | if (dp->reasons->length > 1) |
371 | 12 | dp->dp_reasons |= (dp->reasons->data[1] << 8); |
372 | 36 | dp->dp_reasons &= CRLDP_ALL_REASONS; |
373 | 2.41k | } else { |
374 | 2.41k | dp->dp_reasons = CRLDP_ALL_REASONS; |
375 | 2.41k | } |
376 | 2.45k | return 1; |
377 | 2.50k | } |
378 | | |
379 | | /* Return 1 on success, 0 if x is invalid. */ |
380 | | static int setup_crldp(const X509 *x, STACK_OF(DIST_POINT) **tmp_crldp) |
381 | 126k | { |
382 | 126k | int i; |
383 | | |
384 | 126k | *tmp_crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL); |
385 | 126k | if (*tmp_crldp == NULL && i != -1) |
386 | 2.06k | return 0; |
387 | | |
388 | 127k | for (i = 0; i < sk_DIST_POINT_num(*tmp_crldp); i++) { |
389 | 2.50k | if (!setup_dp(sk_DIST_POINT_value(*tmp_crldp, i))) |
390 | 50 | return 0; |
391 | 2.50k | } |
392 | 124k | return 1; |
393 | 124k | } |
394 | | |
395 | | /* Check that issuer public key algorithm matches subject signature algorithm */ |
396 | | static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject) |
397 | 88.5k | { |
398 | 88.5k | int subj_sig_nid; |
399 | | |
400 | 88.5k | if (issuer_key == NULL) |
401 | 6.97k | return X509_V_ERR_NO_ISSUER_PUBLIC_KEY; |
402 | 81.6k | if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm), |
403 | 81.6k | NULL, &subj_sig_nid) |
404 | 81.6k | == 0) |
405 | 7.82k | return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
406 | 73.8k | if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid)) |
407 | 1.86k | || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss)) |
408 | 73.2k | return X509_V_OK; |
409 | 578 | return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH; |
410 | 73.8k | } |
411 | | |
412 | | /* |
413 | | * Per-scan SipHash keying for the duplicate-extension fall-back table. |
414 | | * |
415 | | * Standard extensions have an assigned NID and are deduplicated with the O(1) |
416 | | * bitset in scan_ext_flags(). Only unknown or dynamically registered OIDs, |
417 | | * whose bytes are fully attacker-controlled on an untrusted certificate, reach |
418 | | * the hash table. An unkeyed hash would let an attacker precompute colliding |
419 | | * OIDs and drive the table into O(n^2) chain walks (hash flooding). Hash the |
420 | | * OID with SipHash under a random key drawn once per scan, mirroring the |
421 | | * defense in ssl/quic/quic_lcidm.c. |
422 | | */ |
423 | | typedef struct { |
424 | | const ASN1_OBJECT *obj; |
425 | | const uint64_t *key; /* 128-bit SipHash key owned by scan_ext_flags */ |
426 | | } oid_hte; |
427 | | |
428 | | static unsigned long oid_hash(const void *p) |
429 | 14.9k | { |
430 | 14.9k | const oid_hte *e = (const oid_hte *)p; |
431 | 14.9k | SIPHASH sip = { 0 }; |
432 | 14.9k | unsigned char digest[SIPHASH_MIN_DIGEST_SIZE]; |
433 | 14.9k | unsigned long v = 0; |
434 | | |
435 | 14.9k | if (!SipHash_set_hash_size(&sip, SIPHASH_MIN_DIGEST_SIZE)) |
436 | 0 | return 0; |
437 | 14.9k | if (!SipHash_Init(&sip, (const unsigned char *)e->key, 0, 0)) |
438 | 0 | return 0; |
439 | 14.9k | SipHash_Update(&sip, (const unsigned char *)e->obj->data, e->obj->length); |
440 | | |
441 | 14.9k | if (!SipHash_Final(&sip, digest, sizeof(digest))) |
442 | 0 | return 0; |
443 | 14.9k | memcpy(&v, digest, sizeof(v) < sizeof(digest) ? sizeof(v) : sizeof(digest)); |
444 | 14.9k | return v; |
445 | 14.9k | } |
446 | | |
447 | | static int oid_cmp(const void *a, const void *b) |
448 | 34 | { |
449 | 34 | return OBJ_cmp(((const oid_hte *)a)->obj, ((const oid_hte *)b)->obj); |
450 | 34 | } |
451 | | |
452 | | /* |
453 | | * Scan all extensions of a certificate to collect extension-related flags. |
454 | | * Detects duplicate extensions (RFC 5280 section 4.2), the presence of a |
455 | | * freshest CRL extension and unsupported critical extensions. |
456 | | * |
457 | | * In the future, if needed, this scanning function could return the index |
458 | | * of the offending extension on error, allowing the caller to identify which |
459 | | * extension caused the problem and report it via ERR_raise_data(). |
460 | | */ |
461 | | static void scan_ext_flags(const X509 *x509, uint32_t *flags) |
462 | 37.1k | { |
463 | 37.1k | OPENSSL_LHASH *h = NULL; |
464 | 37.1k | oid_hte *arr = NULL; |
465 | 37.1k | size_t num_entries = 0; |
466 | 37.1k | uint64_t k[2]; /* random SipHash key for the fall-back table */ |
467 | 37.1k | uint8_t ex_bitset[(NUM_NID + 7) / 8]; |
468 | 37.1k | int count = X509_get_ext_count(x509); |
469 | | |
470 | 37.1k | memset(ex_bitset, 0, sizeof(ex_bitset)); |
471 | | /* A certificate MUST NOT include more than one instance of an extension. */ |
472 | 164k | for (int i = 0; i < count; i++) { |
473 | 129k | const X509_EXTENSION *ex = X509_get_ext(x509, i); |
474 | 129k | const ASN1_OBJECT *a = X509_EXTENSION_get_object(ex); |
475 | 129k | int nid = OBJ_obj2nid(a); |
476 | | |
477 | | /* |
478 | | * Known NIDs within the build-time bitset limit are checked for |
479 | | * duplicates in constant time. Unknown OIDs and dynamically registered |
480 | | * NIDs that exceed the limit fall back to duplicate detection via a |
481 | | * hash table. |
482 | | */ |
483 | 129k | if (nid > NID_undef && nid < NUM_NID) { |
484 | 114k | unsigned int ex_bit = nid; |
485 | | |
486 | 114k | if ((ex_bitset[ex_bit >> 3] & (1u << (ex_bit & 7))) != 0) { |
487 | 519 | *flags |= EXFLAG_DUPLICATE; |
488 | 519 | break; |
489 | 519 | } |
490 | 114k | ex_bitset[ex_bit >> 3] |= (1u << (ex_bit & 7)); |
491 | 114k | } else { |
492 | | /* |
493 | | * Extensions with unknown NID (NID_undef) and dynamically |
494 | | * registered NIDs are handled here by hashing the OID (data/length). |
495 | | * A zero-length OID should not reach this point, but we check for |
496 | | * it anyway and assign the EXFLAG_INVALID flag if it does. |
497 | | */ |
498 | 14.9k | if (a->length < 1) { |
499 | 0 | *flags |= EXFLAG_INVALID; |
500 | 0 | break; |
501 | 0 | } |
502 | | /* |
503 | | * Create the table and draw a fresh random SipHash key the first |
504 | | * time an attacker-controlled OID is seen, so unkeyed hashing |
505 | | * cannot be used to force O(n^2) collisions (hash flooding). |
506 | | * |
507 | | * The key and the entry wrappers remain valid for the duration of |
508 | | * the scan; the table never outlives this function. |
509 | | * |
510 | | * If the table cannot be set up or an insert fails, fail closed |
511 | | * with EXFLAG_INVALID, as ossl_x509v3_cache_extensions() does for |
512 | | * any other extension it cannot process. Leaving the loop |
513 | | * silently would skip not only the duplicate check but also the |
514 | | * unsupported-critical check for this and every later extension. |
515 | | */ |
516 | 14.9k | if (h == NULL) { |
517 | 9.83k | if (RAND_bytes_ex(x509->libctx, (unsigned char *)k, sizeof(k), 0) <= 0 |
518 | 9.83k | || (arr = OPENSSL_malloc_array(count, sizeof(*arr))) == NULL |
519 | 9.83k | || (h = OPENSSL_LH_new(oid_hash, oid_cmp)) == NULL) { |
520 | 0 | *flags |= EXFLAG_INVALID; |
521 | 0 | break; |
522 | 0 | } |
523 | 9.83k | } |
524 | 14.9k | arr[num_entries].obj = a; |
525 | 14.9k | arr[num_entries].key = k; |
526 | 14.9k | if (OPENSSL_LH_insert(h, &arr[num_entries]) != NULL) { |
527 | 34 | *flags |= EXFLAG_DUPLICATE; |
528 | 34 | break; |
529 | 34 | } |
530 | 14.9k | if (OPENSSL_LH_error(h) != 0) { |
531 | 0 | *flags |= EXFLAG_INVALID; |
532 | 0 | break; |
533 | 0 | } |
534 | 14.9k | num_entries++; |
535 | 14.9k | } |
536 | 129k | if (nid == NID_freshest_crl) |
537 | 337 | *flags |= EXFLAG_FRESHEST; |
538 | 129k | if (!X509_EXTENSION_get_critical(ex)) |
539 | 113k | continue; |
540 | 16.3k | if (!X509_supported_extension(ex)) { |
541 | 1.89k | *flags |= EXFLAG_CRITICAL; |
542 | 1.89k | break; |
543 | 1.89k | } |
544 | 16.3k | } |
545 | 37.1k | OPENSSL_LH_free(h); |
546 | 37.1k | OPENSSL_free(arr); |
547 | 37.1k | } |
548 | | |
549 | 14.2k | #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS) |
550 | | #define ku_reject(x, usage) \ |
551 | 17.6k | (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0) |
552 | | #define xku_reject(x, usage) \ |
553 | 0 | (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0) |
554 | | #define ns_reject(x, usage) \ |
555 | 0 | (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0) |
556 | | |
557 | | /* |
558 | | * Cache info on various X.509v3 extensions and further derived information, |
559 | | * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields. |
560 | | * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags. |
561 | | * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully. |
562 | | * Set EXFLAG_INVALID and return 0 in case the certificate is invalid. |
563 | | * |
564 | | * This is usually called by side-effect on objects, and forces us to keep |
565 | | * mutable X509 objects around. We should really make this go away. |
566 | | * In the interest of being able to do so, this function explicitly takes |
567 | | * a const argument and casts away const. |
568 | | */ |
569 | | int ossl_x509v3_cache_extensions(const X509 *const_x) |
570 | 53.2k | { |
571 | 53.2k | BASIC_CONSTRAINTS *bs; |
572 | 53.2k | PROXY_CERT_INFO_EXTENSION *pci; |
573 | 53.2k | ASN1_BIT_STRING *usage; |
574 | 53.2k | ASN1_BIT_STRING *ns; |
575 | 53.2k | EXTENDED_KEY_USAGE *extusage; |
576 | 53.2k | int i; |
577 | 53.2k | int res; |
578 | 53.2k | uint32_t tmp_ex_flags; |
579 | 53.2k | unsigned char tmp_sha1_hash[SHA_DIGEST_LENGTH]; |
580 | 53.2k | long tmp_ex_pathlen; |
581 | 53.2k | long tmp_ex_pcpathlen; |
582 | 53.2k | uint32_t tmp_ex_kusage; |
583 | 53.2k | uint32_t tmp_ex_xkusage; |
584 | 53.2k | uint32_t tmp_ex_nscert; |
585 | 53.2k | ASN1_OCTET_STRING *tmp_skid = NULL; |
586 | 53.2k | AUTHORITY_KEYID *tmp_akid = NULL; |
587 | 53.2k | STACK_OF(GENERAL_NAME) *tmp_altname = NULL; |
588 | 53.2k | NAME_CONSTRAINTS *tmp_nc = NULL; |
589 | 53.2k | STACK_OF(DIST_POINT) *tmp_crldp = NULL; |
590 | 53.2k | X509_SIG_INFO tmp_siginf; |
591 | 53.2k | #ifndef OPENSSL_NO_RFC3779 |
592 | 53.2k | STACK_OF(IPAddressFamily) *tmp_rfc3779_addr = NULL; |
593 | 53.2k | struct ASIdentifiers_st *tmp_rfc3779_asid = NULL; |
594 | 53.2k | #endif |
595 | 53.2k | int ret = 0; |
596 | | |
597 | 53.2k | #ifdef tsan_ld_acq |
598 | | /* Fast lock-free check, see end of the function for details. */ |
599 | 53.2k | if (tsan_ld_acq((TSAN_QUALIFIER int *)&const_x->ex_cached)) { |
600 | 35.7k | ret = (const_x->ex_flags & EXFLAG_INVALID) == 0; |
601 | 35.7k | goto done; |
602 | 35.7k | } |
603 | 17.4k | #endif |
604 | | |
605 | 17.4k | if (!CRYPTO_THREAD_read_lock(const_x->lock)) |
606 | 0 | goto done; |
607 | 17.4k | tmp_ex_flags = const_x->ex_flags; |
608 | 17.4k | tmp_ex_pcpathlen = const_x->ex_pcpathlen; |
609 | 17.4k | tmp_ex_kusage = const_x->ex_kusage; |
610 | 17.4k | tmp_ex_nscert = const_x->ex_nscert; |
611 | | |
612 | 17.4k | if ((tmp_ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */ |
613 | 0 | CRYPTO_THREAD_unlock(const_x->lock); |
614 | 0 | ret = (tmp_ex_flags & EXFLAG_INVALID) == 0; |
615 | 0 | goto done; |
616 | 0 | } |
617 | | |
618 | 17.4k | ERR_set_mark(); |
619 | | |
620 | | /* Cache the SHA1 digest of the cert */ |
621 | 17.4k | if (!X509_digest(const_x, EVP_sha1(), tmp_sha1_hash, NULL)) |
622 | 0 | tmp_ex_flags |= EXFLAG_NO_FINGERPRINT; |
623 | | |
624 | | /* V1 should mean no extensions ... */ |
625 | 17.4k | if (X509_get_version(const_x) == X509_VERSION_1) |
626 | 414 | tmp_ex_flags |= EXFLAG_V1; |
627 | | |
628 | | /* Handle basic constraints */ |
629 | 17.4k | tmp_ex_pathlen = -1; |
630 | 17.4k | if ((bs = X509_get_ext_d2i(const_x, NID_basic_constraints, &i, NULL)) != NULL) { |
631 | 11.5k | if (bs->ca) |
632 | 3.49k | tmp_ex_flags |= EXFLAG_CA; |
633 | 11.5k | if (bs->pathlen != NULL) { |
634 | | /* |
635 | | * The error case !bs->ca is checked by check_chain() |
636 | | * in case ctx->param->flags & X509_V_FLAG_X509_STRICT |
637 | | */ |
638 | 665 | if (bs->pathlen->type == V_ASN1_NEG_INTEGER) { |
639 | 21 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN); |
640 | 21 | tmp_ex_flags |= EXFLAG_INVALID; |
641 | 644 | } else { |
642 | 644 | tmp_ex_pathlen = ASN1_INTEGER_get(bs->pathlen); |
643 | 644 | } |
644 | 665 | } |
645 | 11.5k | BASIC_CONSTRAINTS_free(bs); |
646 | 11.5k | tmp_ex_flags |= EXFLAG_BCONS; |
647 | 11.5k | } else if (i != -1) { |
648 | 478 | tmp_ex_flags |= EXFLAG_INVALID; |
649 | 478 | } |
650 | | |
651 | | /* Handle proxy certificates */ |
652 | 17.4k | if ((pci = X509_get_ext_d2i(const_x, NID_proxyCertInfo, &i, NULL)) != NULL) { |
653 | 26 | if ((tmp_ex_flags & EXFLAG_CA) != 0 |
654 | 19 | || X509_get_ext_by_NID(const_x, NID_subject_alt_name, -1) >= 0 |
655 | 20 | || X509_get_ext_by_NID(const_x, NID_issuer_alt_name, -1) >= 0) { |
656 | 20 | tmp_ex_flags |= EXFLAG_INVALID; |
657 | 20 | } |
658 | 26 | if (pci->pcPathLengthConstraint != NULL) |
659 | 0 | tmp_ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint); |
660 | 26 | else |
661 | 26 | tmp_ex_pcpathlen = -1; |
662 | 26 | PROXY_CERT_INFO_EXTENSION_free(pci); |
663 | 26 | tmp_ex_flags |= EXFLAG_PROXY; |
664 | 17.4k | } else if (i != -1) { |
665 | 24 | tmp_ex_flags |= EXFLAG_INVALID; |
666 | 24 | } |
667 | | |
668 | | /* Handle (basic) key usage */ |
669 | 17.4k | if ((usage = X509_get_ext_d2i(const_x, NID_key_usage, &i, NULL)) != NULL) { |
670 | 7.92k | tmp_ex_kusage = 0; |
671 | 7.92k | if (usage->length > 0) { |
672 | 7.88k | tmp_ex_kusage = usage->data[0]; |
673 | 7.88k | if (usage->length > 1) |
674 | 74 | tmp_ex_kusage |= usage->data[1] << 8; |
675 | 7.88k | } |
676 | 7.92k | tmp_ex_flags |= EXFLAG_KUSAGE; |
677 | 7.92k | ASN1_BIT_STRING_free(usage); |
678 | | /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */ |
679 | 7.92k | if (tmp_ex_kusage == 0) { |
680 | 317 | ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE); |
681 | 317 | tmp_ex_flags |= EXFLAG_INVALID; |
682 | 317 | } |
683 | 9.56k | } else if (i != -1) { |
684 | 549 | tmp_ex_flags |= EXFLAG_INVALID; |
685 | 549 | } |
686 | | |
687 | | /* Handle extended key usage */ |
688 | 17.4k | tmp_ex_xkusage = 0; |
689 | 17.4k | if ((extusage = X509_get_ext_d2i(const_x, NID_ext_key_usage, &i, NULL)) != NULL) { |
690 | 7.46k | tmp_ex_flags |= EXFLAG_XKUSAGE; |
691 | 14.9k | for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { |
692 | 7.46k | switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { |
693 | 7.04k | case NID_server_auth: |
694 | 7.04k | tmp_ex_xkusage |= XKU_SSL_SERVER; |
695 | 7.04k | break; |
696 | 17 | case NID_client_auth: |
697 | 17 | tmp_ex_xkusage |= XKU_SSL_CLIENT; |
698 | 17 | break; |
699 | 28 | case NID_email_protect: |
700 | 28 | tmp_ex_xkusage |= XKU_SMIME; |
701 | 28 | break; |
702 | 12 | case NID_code_sign: |
703 | 12 | tmp_ex_xkusage |= XKU_CODE_SIGN; |
704 | 12 | break; |
705 | 0 | case NID_ms_sgc: |
706 | 0 | case NID_ns_sgc: |
707 | 0 | tmp_ex_xkusage |= XKU_SGC; |
708 | 0 | break; |
709 | 3 | case NID_OCSP_sign: |
710 | 3 | tmp_ex_xkusage |= XKU_OCSP_SIGN; |
711 | 3 | break; |
712 | 3 | case NID_time_stamp: |
713 | 3 | tmp_ex_xkusage |= XKU_TIMESTAMP; |
714 | 3 | break; |
715 | 6 | case NID_dvcs: |
716 | 6 | tmp_ex_xkusage |= XKU_DVCS; |
717 | 6 | break; |
718 | 0 | case NID_anyExtendedKeyUsage: |
719 | 0 | tmp_ex_xkusage |= XKU_ANYEKU; |
720 | 0 | break; |
721 | 345 | default: |
722 | | /* Ignore unknown extended key usage */ |
723 | 345 | break; |
724 | 7.46k | } |
725 | 7.46k | } |
726 | 7.46k | sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); |
727 | 10.0k | } else if (i != -1) { |
728 | 1.76k | tmp_ex_flags |= EXFLAG_INVALID; |
729 | 1.76k | } |
730 | | |
731 | | /* Handle legacy Netscape extension */ |
732 | 17.4k | if ((ns = X509_get_ext_d2i(const_x, NID_netscape_cert_type, &i, NULL)) != NULL) { |
733 | 2 | if (ns->length > 0) |
734 | 1 | tmp_ex_nscert = ns->data[0]; |
735 | 1 | else |
736 | 1 | tmp_ex_nscert = 0; |
737 | 2 | tmp_ex_flags |= EXFLAG_NSCERT; |
738 | 2 | ASN1_BIT_STRING_free(ns); |
739 | 17.4k | } else if (i != -1) { |
740 | 8 | tmp_ex_flags |= EXFLAG_INVALID; |
741 | 8 | } |
742 | | |
743 | | /* Handle subject key identifier and issuer/authority key identifier */ |
744 | 17.4k | tmp_skid = X509_get_ext_d2i(const_x, NID_subject_key_identifier, &i, NULL); |
745 | 17.4k | if (tmp_skid == NULL && i != -1) |
746 | 311 | tmp_ex_flags |= EXFLAG_INVALID; |
747 | | |
748 | 17.4k | tmp_akid = X509_get_ext_d2i(const_x, NID_authority_key_identifier, &i, NULL); |
749 | 17.4k | if (tmp_akid == NULL && i != -1) |
750 | 899 | tmp_ex_flags |= EXFLAG_INVALID; |
751 | | |
752 | | /* Setting EXFLAG_SS is equivalent to ossl_x509_likely_issued(const_x, const_x) == X509_V_OK */ |
753 | 17.4k | if (X509_NAME_cmp(X509_get_subject_name(const_x), X509_get_issuer_name(const_x)) == 0) { |
754 | 10.5k | tmp_ex_flags |= EXFLAG_SI; /* Certificate is self-issued: subject == issuer */ |
755 | | /* |
756 | | * When the SKID is missing, which is rare for self-issued certs, |
757 | | * we could afford doing the (accurate) actual self-signature check, but |
758 | | * decided against it for efficiency reasons and according to RFC 5280, |
759 | | * CA certs MUST have an SKID and non-root certs MUST have an AKID. |
760 | | * |
761 | | * The cached const_x->skid is not populated until the write-lock |
762 | | * publication below, so the keyid comparison is done directly |
763 | | * against tmp_skid. X509_check_akid() is retained for its serial |
764 | | * number and issuer name checks. |
765 | | */ |
766 | 10.5k | if ((tmp_akid == NULL || tmp_akid->keyid == NULL || tmp_skid == NULL |
767 | 2.39k | || ASN1_OCTET_STRING_cmp(tmp_akid->keyid, tmp_skid) == 0) |
768 | 10.4k | && X509_check_akid(const_x, tmp_akid) == X509_V_OK |
769 | 10.3k | && check_sig_alg_match(X509_get0_pubkey(const_x), const_x) == X509_V_OK) { |
770 | | /* |
771 | | * Assume self-signed if the signature alg matches the pkey alg and |
772 | | * AKID is missing or matches respective fields in the same cert |
773 | | * Not checking if any given key usage extension allows signing. |
774 | | */ |
775 | 7.99k | tmp_ex_flags |= EXFLAG_SS; |
776 | 7.99k | } |
777 | 10.5k | } |
778 | | |
779 | | /* Handle subject alternative names and various other extensions */ |
780 | 17.4k | tmp_altname = X509_get_ext_d2i(const_x, NID_subject_alt_name, &i, NULL); |
781 | 17.4k | if (tmp_altname == NULL && i != -1) |
782 | 370 | tmp_ex_flags |= EXFLAG_INVALID; |
783 | 17.4k | tmp_nc = X509_get_ext_d2i(const_x, NID_name_constraints, &i, NULL); |
784 | 17.4k | if (tmp_nc == NULL && i != -1) |
785 | 258 | tmp_ex_flags |= EXFLAG_INVALID; |
786 | 17.4k | if (!check_name_constraints(tmp_nc)) |
787 | 0 | tmp_ex_flags |= EXFLAG_INVALID; |
788 | | |
789 | | /* Handle CRL distribution point entries */ |
790 | 17.4k | res = setup_crldp(const_x, &tmp_crldp); |
791 | 17.4k | if (res == 0) |
792 | 382 | tmp_ex_flags |= EXFLAG_INVALID; |
793 | | |
794 | 17.4k | #ifndef OPENSSL_NO_RFC3779 |
795 | 17.4k | tmp_rfc3779_addr = X509_get_ext_d2i(const_x, NID_sbgp_ipAddrBlock, &i, NULL); |
796 | 17.4k | if (tmp_rfc3779_addr == NULL && i != -1) |
797 | 74 | tmp_ex_flags |= EXFLAG_INVALID; |
798 | | |
799 | 17.4k | tmp_rfc3779_asid = X509_get_ext_d2i(const_x, NID_sbgp_autonomousSysNum, &i, NULL); |
800 | 17.4k | if (tmp_rfc3779_asid == NULL && i != -1) |
801 | 24 | tmp_ex_flags |= EXFLAG_INVALID; |
802 | 17.4k | #endif |
803 | | |
804 | 17.4k | scan_ext_flags(const_x, &tmp_ex_flags); |
805 | | |
806 | | /* Set x->siginf, ignoring errors due to unsupported algos */ |
807 | 17.4k | (void)ossl_x509_init_sig_info(const_x, &tmp_siginf); |
808 | | |
809 | 17.4k | tmp_ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */ |
810 | 17.4k | ERR_pop_to_mark(); |
811 | | |
812 | 17.4k | CRYPTO_THREAD_unlock(const_x->lock); |
813 | | /* |
814 | | * Now that we've done all the compute intensive work under read lock |
815 | | * do all the updating under a write lock |
816 | | */ |
817 | 17.4k | if (!CRYPTO_THREAD_write_lock(const_x->lock)) |
818 | 0 | goto done; |
819 | | |
820 | | /* See if another thread updated this certificate before we got the write lock. */ |
821 | 17.4k | if ((const_x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */ |
822 | 0 | CRYPTO_THREAD_unlock(const_x->lock); |
823 | 0 | ret = (const_x->ex_flags & EXFLAG_INVALID) == 0; |
824 | 0 | goto done; |
825 | 0 | } |
826 | | |
827 | | /* Otherwise, we have the lock, set the cached fields in the cert. */ |
828 | 17.4k | ((X509 *)const_x)->ex_flags = tmp_ex_flags; |
829 | 17.4k | ((X509 *)const_x)->ex_pathlen = tmp_ex_pathlen; |
830 | 17.4k | ((X509 *)const_x)->ex_pcpathlen = tmp_ex_pcpathlen; |
831 | 17.4k | if (!(tmp_ex_flags & EXFLAG_NO_FINGERPRINT)) |
832 | 17.4k | memcpy(((X509 *)const_x)->sha1_hash, tmp_sha1_hash, SHA_DIGEST_LENGTH); |
833 | 17.4k | if (tmp_ex_flags & EXFLAG_KUSAGE) |
834 | 7.92k | ((X509 *)const_x)->ex_kusage = tmp_ex_kusage; |
835 | 17.4k | ((X509 *)const_x)->ex_xkusage = tmp_ex_xkusage; |
836 | 17.4k | if (tmp_ex_flags & EXFLAG_NSCERT) |
837 | 2 | ((X509 *)const_x)->ex_nscert = tmp_ex_nscert; |
838 | 17.4k | ASN1_OCTET_STRING_free(((X509 *)const_x)->skid); |
839 | 17.4k | ((X509 *)const_x)->skid = tmp_skid; |
840 | 17.4k | tmp_skid = NULL; |
841 | 17.4k | AUTHORITY_KEYID_free(((X509 *)const_x)->akid); |
842 | 17.4k | ((X509 *)const_x)->akid = tmp_akid; |
843 | 17.4k | tmp_akid = NULL; |
844 | 17.4k | sk_GENERAL_NAME_pop_free(((X509 *)const_x)->altname, GENERAL_NAME_free); |
845 | 17.4k | ((X509 *)const_x)->altname = tmp_altname; |
846 | 17.4k | tmp_altname = NULL; |
847 | 17.4k | NAME_CONSTRAINTS_free(((X509 *)const_x)->nc); |
848 | 17.4k | ((X509 *)const_x)->nc = tmp_nc; |
849 | 17.4k | tmp_nc = NULL; |
850 | 17.4k | sk_DIST_POINT_pop_free(((X509 *)const_x)->crldp, DIST_POINT_free); |
851 | 17.4k | ((X509 *)const_x)->crldp = tmp_crldp; |
852 | 17.4k | tmp_crldp = NULL; |
853 | 17.4k | #ifndef OPENSSL_NO_RFC3779 |
854 | 17.4k | sk_IPAddressFamily_pop_free(((X509 *)const_x)->rfc3779_addr, IPAddressFamily_free); |
855 | 17.4k | ((X509 *)const_x)->rfc3779_addr = tmp_rfc3779_addr; |
856 | 17.4k | tmp_rfc3779_addr = NULL; |
857 | 17.4k | ASIdentifiers_free(((X509 *)const_x)->rfc3779_asid); |
858 | 17.4k | ((X509 *)const_x)->rfc3779_asid = tmp_rfc3779_asid; |
859 | 17.4k | tmp_rfc3779_asid = NULL; |
860 | 17.4k | #endif |
861 | 17.4k | ((X509 *)const_x)->siginf = tmp_siginf; |
862 | | |
863 | 17.4k | #ifdef tsan_st_rel |
864 | 17.4k | tsan_st_rel((TSAN_QUALIFIER int *)&const_x->ex_cached, 1); |
865 | | /* |
866 | | * Above store triggers fast lock-free check in the beginning of the |
867 | | * function. But one has to ensure that the structure is "stable", i.e. |
868 | | * all stores are visible on all processors. Hence the release fence. |
869 | | */ |
870 | 17.4k | #endif |
871 | 17.4k | CRYPTO_THREAD_unlock(const_x->lock); |
872 | 17.4k | if (tmp_ex_flags & EXFLAG_INVALID) { |
873 | 4.23k | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE); |
874 | 4.23k | goto done; |
875 | 4.23k | } |
876 | | |
877 | 13.2k | ret = 1; |
878 | | |
879 | 53.2k | done: |
880 | 53.2k | ASN1_OCTET_STRING_free(tmp_skid); |
881 | 53.2k | AUTHORITY_KEYID_free(tmp_akid); |
882 | 53.2k | sk_GENERAL_NAME_pop_free(tmp_altname, GENERAL_NAME_free); |
883 | 53.2k | NAME_CONSTRAINTS_free(tmp_nc); |
884 | 53.2k | sk_DIST_POINT_pop_free(tmp_crldp, DIST_POINT_free); |
885 | 53.2k | #ifndef OPENSSL_NO_RFC3779 |
886 | 53.2k | sk_IPAddressFamily_pop_free(tmp_rfc3779_addr, IPAddressFamily_free); |
887 | 53.2k | ASIdentifiers_free(tmp_rfc3779_asid); |
888 | 53.2k | #endif |
889 | 53.2k | return ret; |
890 | 13.2k | } |
891 | | |
892 | | /*- |
893 | | * CA checks common to all purposes |
894 | | * return codes: |
895 | | * 0 not a CA |
896 | | * 1 is a CA |
897 | | * 2 Only possible in older versions of openSSL when basicConstraints are absent |
898 | | * new versions will not return this value. May be a CA |
899 | | * 3 basicConstraints absent but self-signed V1. |
900 | | * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. |
901 | | * 5 Netscape specific CA Flags present |
902 | | */ |
903 | | |
904 | | static int check_ca(const X509 *x) |
905 | 9.64k | { |
906 | | /* keyUsage if present should allow cert signing */ |
907 | 9.64k | if (ku_reject(x, KU_KEY_CERT_SIGN)) |
908 | 76 | return 0; |
909 | 9.56k | if ((x->ex_flags & EXFLAG_BCONS) != 0) { |
910 | | /* If basicConstraints says not a CA then say so */ |
911 | 2.44k | return (x->ex_flags & EXFLAG_CA) != 0; |
912 | 7.12k | } else { |
913 | | /* We support V1 roots for... uh, I don't really know why. */ |
914 | 7.12k | if ((x->ex_flags & V1_ROOT) == V1_ROOT) |
915 | 94 | return 3; |
916 | | /* |
917 | | * If key usage present it must have certSign so tolerate it |
918 | | */ |
919 | 7.03k | else if ((x->ex_flags & EXFLAG_KUSAGE) != 0) |
920 | 35 | return 4; |
921 | | /* Older certificates could have Netscape-specific CA types */ |
922 | 6.99k | else if ((x->ex_flags & EXFLAG_NSCERT) != 0 |
923 | 14 | && (x->ex_nscert & NS_ANY_CA) != 0) |
924 | 6 | return 5; |
925 | | /* Can this still be regarded a CA certificate? I doubt it. */ |
926 | 6.99k | return 0; |
927 | 7.12k | } |
928 | 9.56k | } |
929 | | |
930 | | void X509_set_proxy_flag(X509 *x) |
931 | 0 | { |
932 | 0 | if (CRYPTO_THREAD_write_lock(x->lock)) { |
933 | 0 | x->ex_flags |= EXFLAG_PROXY; |
934 | 0 | CRYPTO_THREAD_unlock(x->lock); |
935 | 0 | } |
936 | 0 | } |
937 | | |
938 | | void X509_set_proxy_pathlen(X509 *x, long l) |
939 | 0 | { |
940 | 0 | x->ex_pcpathlen = l; |
941 | 0 | } |
942 | | |
943 | | int X509_check_ca(const X509 *x) |
944 | 10.7k | { |
945 | | /* Note 0 normally means "not a CA" - but in this case means error. */ |
946 | 10.7k | if (!ossl_x509v3_cache_extensions(x)) |
947 | 1.32k | return 0; |
948 | | |
949 | 9.42k | return check_ca(x); |
950 | 10.7k | } |
951 | | |
952 | | /* Check SSL CA: common checks for SSL client and server. */ |
953 | | static int check_ssl_ca(const X509 *x) |
954 | 0 | { |
955 | 0 | int ca_ret = check_ca(x); |
956 | |
|
957 | 0 | if (ca_ret == 0) |
958 | 0 | return 0; |
959 | | /* Check nsCertType if present */ |
960 | 0 | return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0; |
961 | 0 | } |
962 | | |
963 | | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
964 | | int non_leaf) |
965 | 0 | { |
966 | 0 | if (xku_reject(x, XKU_SSL_CLIENT)) |
967 | 0 | return 0; |
968 | 0 | if (non_leaf) |
969 | 0 | return check_ssl_ca(x); |
970 | | /* We need to do digital signatures or key agreement */ |
971 | 0 | if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) |
972 | 0 | return 0; |
973 | | /* nsCertType if present should allow SSL client use */ |
974 | 0 | if (ns_reject(x, NS_SSL_CLIENT)) |
975 | 0 | return 0; |
976 | 0 | return 1; |
977 | 0 | } |
978 | | |
979 | | /* |
980 | | * Key usage needed for TLS/SSL server: digital signature, encipherment or |
981 | | * key agreement. The ssl code can check this more thoroughly for individual |
982 | | * key types. |
983 | | */ |
984 | | #define KU_TLS \ |
985 | | KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT |
986 | | |
987 | | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
988 | | int non_leaf) |
989 | 0 | { |
990 | 0 | if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC)) |
991 | 0 | return 0; |
992 | 0 | if (non_leaf) |
993 | 0 | return check_ssl_ca(x); |
994 | | |
995 | 0 | if (ns_reject(x, NS_SSL_SERVER)) |
996 | 0 | return 0; |
997 | 0 | if (ku_reject(x, KU_TLS)) |
998 | 0 | return 0; |
999 | | |
1000 | 0 | return 1; |
1001 | 0 | } |
1002 | | |
1003 | | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
1004 | | int non_leaf) |
1005 | 0 | { |
1006 | 0 | int ret = check_purpose_ssl_server(xp, x, non_leaf); |
1007 | |
|
1008 | 0 | if (!ret || non_leaf) |
1009 | 0 | return ret; |
1010 | | /* We need to encipher or Netscape complains */ |
1011 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
1012 | 0 | } |
1013 | | |
1014 | | /* common S/MIME client checks */ |
1015 | | static int purpose_smime(const X509 *x, int non_leaf) |
1016 | 0 | { |
1017 | 0 | if (xku_reject(x, XKU_SMIME)) |
1018 | 0 | return 0; |
1019 | 0 | if (non_leaf) { |
1020 | 0 | int ca_ret = check_ca(x); |
1021 | |
|
1022 | 0 | if (ca_ret == 0) |
1023 | 0 | return 0; |
1024 | | /* Check nsCertType if present */ |
1025 | 0 | if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0) |
1026 | 0 | return ca_ret; |
1027 | 0 | else |
1028 | 0 | return 0; |
1029 | 0 | } |
1030 | 0 | if ((x->ex_flags & EXFLAG_NSCERT) != 0) { |
1031 | 0 | if ((x->ex_nscert & NS_SMIME) != 0) |
1032 | 0 | return 1; |
1033 | | /* Workaround for some buggy certificates */ |
1034 | 0 | return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0; |
1035 | 0 | } |
1036 | 0 | return 1; |
1037 | 0 | } |
1038 | | |
1039 | | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
1040 | | int non_leaf) |
1041 | 0 | { |
1042 | 0 | int ret = purpose_smime(x, non_leaf); |
1043 | |
|
1044 | 0 | if (!ret || non_leaf) |
1045 | 0 | return ret; |
1046 | 0 | return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret; |
1047 | 0 | } |
1048 | | |
1049 | | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
1050 | | int non_leaf) |
1051 | 0 | { |
1052 | 0 | int ret = purpose_smime(x, non_leaf); |
1053 | |
|
1054 | 0 | if (!ret || non_leaf) |
1055 | 0 | return ret; |
1056 | 0 | return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret; |
1057 | 0 | } |
1058 | | |
1059 | | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
1060 | | int non_leaf) |
1061 | 0 | { |
1062 | 0 | if (non_leaf) { |
1063 | 0 | int ca_ret = check_ca(x); |
1064 | |
|
1065 | 0 | return ca_ret == 2 ? 0 : ca_ret; |
1066 | 0 | } |
1067 | 0 | return !ku_reject(x, KU_CRL_SIGN); |
1068 | 0 | } |
1069 | | |
1070 | | /* |
1071 | | * OCSP helper: this is *not* a full OCSP check. It just checks that each CA |
1072 | | * is valid. Additional checks must be made on the chain. |
1073 | | */ |
1074 | | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x, |
1075 | | int non_leaf) |
1076 | 499 | { |
1077 | | /* |
1078 | | * Must be a valid CA. Should we really support the "I don't know" value |
1079 | | * (2)? |
1080 | | */ |
1081 | 499 | if (non_leaf) |
1082 | 219 | return check_ca(x); |
1083 | | /* Leaf certificate is checked in OCSP_basic_verify() */ |
1084 | 280 | return 1; |
1085 | 499 | } |
1086 | | |
1087 | | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
1088 | | int non_leaf) |
1089 | 0 | { |
1090 | 0 | int i_ext; |
1091 | | |
1092 | | /* |
1093 | | * If non_leaf is true we must check if this is a valid CA certificate. |
1094 | | * The extra requirements by the CA/Browser Forum are not checked. |
1095 | | */ |
1096 | 0 | if (non_leaf) |
1097 | 0 | return check_ca(x); |
1098 | | |
1099 | | /* |
1100 | | * Key Usage is checked according to RFC 5280 and |
1101 | | * Extended Key Usage attributes is checked according to RFC 3161. |
1102 | | * The extra (and somewhat conflicting) CA/Browser Forum |
1103 | | * Baseline Requirements for the Issuance and Management of |
1104 | | * PubliclyâTrusted Code Signing Certificates, Version 3.0.0, |
1105 | | * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked. |
1106 | | */ |
1107 | | /* |
1108 | | * Check the optional key usage field: |
1109 | | * if Key Usage is present, it must be one of digitalSignature |
1110 | | * and/or nonRepudiation (other values are not consistent and shall |
1111 | | * be rejected). |
1112 | | */ |
1113 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) != 0 |
1114 | 0 | && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) |
1115 | 0 | return 0; |
1116 | | |
1117 | | /* Only timestamp key usage is permitted and it's required. */ |
1118 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP) |
1119 | 0 | return 0; |
1120 | | |
1121 | | /* Extended Key Usage MUST be critical */ |
1122 | 0 | i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); |
1123 | 0 | if (i_ext >= 0 |
1124 | 0 | && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext))) |
1125 | 0 | return 0; |
1126 | 0 | return 1; |
1127 | 0 | } |
1128 | | |
1129 | | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x, |
1130 | | int non_leaf) |
1131 | 0 | { |
1132 | 0 | int i_ext; |
1133 | | |
1134 | | /* |
1135 | | * If non_leaf is true we must check if this is a valid CA certificate. |
1136 | | * The extra requirements by the CA/Browser Forum are not checked. |
1137 | | */ |
1138 | 0 | if (non_leaf) |
1139 | 0 | return check_ca(x); |
1140 | | |
1141 | | /* |
1142 | | * Check the key usage and extended key usage fields: |
1143 | | * |
1144 | | * Reference: CA/Browser Forum, |
1145 | | * Baseline Requirements for the Issuance and Management of |
1146 | | * PubliclyâTrusted Code Signing Certificates, Version 3.0.0, |
1147 | | * Section 7.1.2.3: Code signing and Timestamp Certificate |
1148 | | * |
1149 | | * Checking covers Key Usage and Extended Key Usage attributes. |
1150 | | * The certificatePolicies, cRLDistributionPoints (CDP), and |
1151 | | * authorityInformationAccess (AIA) extensions are so far not checked. |
1152 | | */ |
1153 | | /* Key Usage */ |
1154 | 0 | if ((x->ex_flags & EXFLAG_KUSAGE) == 0) |
1155 | 0 | return 0; |
1156 | 0 | if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0) |
1157 | 0 | return 0; |
1158 | 0 | if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0) |
1159 | 0 | return 0; |
1160 | | |
1161 | | /* Key Usage MUST be critical */ |
1162 | 0 | i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1); |
1163 | 0 | if (i_ext < 0) |
1164 | 0 | return 0; |
1165 | 0 | if (i_ext >= 0) { |
1166 | 0 | const X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext); |
1167 | 0 | if (!X509_EXTENSION_get_critical(ext)) |
1168 | 0 | return 0; |
1169 | 0 | } |
1170 | | |
1171 | | /* Extended Key Usage */ |
1172 | 0 | if ((x->ex_flags & EXFLAG_XKUSAGE) == 0) |
1173 | 0 | return 0; |
1174 | 0 | if ((x->ex_xkusage & XKU_CODE_SIGN) == 0) |
1175 | 0 | return 0; |
1176 | 0 | if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0) |
1177 | 0 | return 0; |
1178 | | |
1179 | 0 | return 1; |
1180 | 0 | } |
1181 | | |
1182 | | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x, |
1183 | | int non_leaf) |
1184 | 0 | { |
1185 | 0 | return 1; |
1186 | 0 | } |
1187 | | |
1188 | | static int check_name_constraints(const NAME_CONSTRAINTS *nc) |
1189 | 37.1k | { |
1190 | 37.1k | GENERAL_SUBTREE *sub; |
1191 | 37.1k | int ret = 1; |
1192 | | |
1193 | 37.1k | if (nc == NULL) |
1194 | 36.9k | goto done; |
1195 | | |
1196 | 159 | for (int i = 0; nc->permittedSubtrees != NULL |
1197 | 27 | && i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); |
1198 | 147 | i++) { |
1199 | 12 | sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i); |
1200 | 12 | if (sub->base->type == GEN_OTHERNAME |
1201 | 0 | && OBJ_obj2nid(sub->base->d.otherName->type_id) |
1202 | 0 | == NID_id_on_SmtpUTF8Mailbox) { |
1203 | | /* RFC 9598 prohibits GEN_OTHERNAME email constraints */ |
1204 | 0 | ERR_raise(ERR_LIB_X509V3, X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE); |
1205 | 0 | ret = 0; |
1206 | 0 | goto done; |
1207 | 0 | } |
1208 | 12 | } |
1209 | 177 | for (int i = 0; nc->excludedSubtrees != NULL |
1210 | 62 | && i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); |
1211 | 147 | i++) { |
1212 | 30 | sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i); |
1213 | 30 | if (sub->base->type == GEN_OTHERNAME |
1214 | 0 | && OBJ_obj2nid(sub->base->d.otherName->type_id) |
1215 | 0 | == NID_id_on_SmtpUTF8Mailbox) { |
1216 | | /* RFC 9598 prohibits GEN_OTHERNAME email constraints */ |
1217 | 0 | ERR_raise(ERR_LIB_X509V3, X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE); |
1218 | 0 | ret = 0; |
1219 | 0 | goto done; |
1220 | 0 | } |
1221 | 30 | } |
1222 | | |
1223 | 37.1k | done: |
1224 | 37.1k | return ret; |
1225 | 147 | } |
1226 | | |
1227 | | /*- |
1228 | | * Various checks to see if one certificate potentially issued the second. |
1229 | | * This can be used to prune a set of possible issuer certificates which |
1230 | | * have been looked up using some simple method such as by subject name. |
1231 | | * These are: |
1232 | | * 1. issuer_name(subject) == subject_name(issuer) |
1233 | | * 2. If akid(subject) exists, it matches the respective issuer fields. |
1234 | | * 3. subject signature algorithm == issuer public key algorithm |
1235 | | * 4. If key_usage(issuer) exists, it allows for signing subject. |
1236 | | * Note that this does not include actually checking the signature. |
1237 | | * Returns 0 for OK, or positive for reason for mismatch |
1238 | | * where reason codes match those for X509_verify_cert(). |
1239 | | */ |
1240 | | int X509_check_issued(const X509 *issuer, const X509 *subject) |
1241 | 39.0k | { |
1242 | 39.0k | int ret; |
1243 | | |
1244 | 39.0k | if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK) |
1245 | 34.5k | return ret; |
1246 | 4.51k | return ossl_x509_signing_allowed(issuer, subject); |
1247 | 39.0k | } |
1248 | | |
1249 | | /* |
1250 | | * Do the checks 1., 2., and 3. as described above for X509_check_issued(). |
1251 | | * These are very similar to a section of ossl_x509v3_cache_extensions(). |
1252 | | * If |issuer| equals |subject| (such that self-signature should be checked), |
1253 | | * use the EXFLAG_SS result of ossl_x509v3_cache_extensions(). |
1254 | | */ |
1255 | | int ossl_x509_likely_issued(const X509 *issuer, const X509 *subject) |
1256 | 31.7k | { |
1257 | 31.7k | int ret; |
1258 | | |
1259 | 31.7k | if (X509_NAME_cmp(X509_get_subject_name(issuer), |
1260 | 31.7k | X509_get_issuer_name(subject)) |
1261 | 31.7k | != 0) |
1262 | 16.0k | return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; |
1263 | | |
1264 | | /* set issuer->skid, subject->akid, and subject->ex_flags */ |
1265 | 15.7k | if (!ossl_x509v3_cache_extensions(issuer) |
1266 | 11.0k | || !ossl_x509v3_cache_extensions(subject)) |
1267 | 5.31k | return X509_V_ERR_UNSPECIFIED; |
1268 | | |
1269 | 10.4k | if (issuer == subject |
1270 | 3.95k | || (X509_NAME_cmp(X509_get_issuer_name(issuer), X509_get_issuer_name(subject)) == 0 |
1271 | 2.37k | && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), X509_get0_serialNumber(subject)) == 0)) |
1272 | | /* |
1273 | | * At this point, we can assume that issuer and subject |
1274 | | * are semantically the same cert because they are identical |
1275 | | * or at least have the same issuer and serial number, |
1276 | | * which (for any sane cert issuer) implies equality of the two certs. |
1277 | | * In this case, for consistency with chain building and validation, |
1278 | | * we make our issuance judgment depend on the presence of EXFLAG_SS. |
1279 | | * This is used for corrected chain building in the corner case of |
1280 | | * a self-issued but not actually self-signed trust anchor cert |
1281 | | * without subject and issuer key identifiers (i.e., no SKID and AKID). |
1282 | | */ |
1283 | 6.56k | return (issuer->ex_flags & EXFLAG_SS) != 0 |
1284 | 6.56k | ? X509_V_OK |
1285 | 6.56k | : X509_V_ERR_CERT_SIGNATURE_FAILURE; |
1286 | | |
1287 | 3.88k | ret = X509_check_akid(issuer, subject->akid); |
1288 | 3.88k | if (ret != X509_V_OK) |
1289 | 529 | return ret; |
1290 | | |
1291 | | /* Check if the subject signature alg matches the issuer's PUBKEY alg */ |
1292 | 3.35k | return check_sig_alg_match(X509_get0_pubkey(issuer), subject); |
1293 | 3.88k | } |
1294 | | |
1295 | | /*- |
1296 | | * Check if certificate I<issuer> is allowed to issue certificate I<subject> |
1297 | | * according to the B<keyUsage> field of I<issuer> if present |
1298 | | * depending on any proxyCertInfo extension of I<subject>. |
1299 | | * Returns 0 for OK, or positive for reason for rejection |
1300 | | * where reason codes match those for X509_verify_cert(). |
1301 | | */ |
1302 | | int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject) |
1303 | 8.00k | { |
1304 | 8.00k | if ((subject->ex_flags & EXFLAG_PROXY) != 0) { |
1305 | 4 | if (ku_reject(issuer, KU_DIGITAL_SIGNATURE)) |
1306 | 0 | return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE; |
1307 | 7.99k | } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) { |
1308 | 199 | return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; |
1309 | 199 | } |
1310 | 7.80k | return X509_V_OK; |
1311 | 8.00k | } |
1312 | | |
1313 | | /* |
1314 | | * check if all sub-fields of the authority key identifier information akid, |
1315 | | * as far as present, match the respective subjectKeyIdentifier extension (if |
1316 | | * present in issuer), serialNumber field, and issuer fields of issuer. |
1317 | | * returns X509_V_OK also if akid is NULL because this means no restriction. |
1318 | | */ |
1319 | | int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid) |
1320 | 94.9k | { |
1321 | 94.9k | if (akid == NULL) |
1322 | 73.8k | return X509_V_OK; |
1323 | | |
1324 | | /* Check key ids (if present) */ |
1325 | 21.0k | if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) |
1326 | 1.47k | return X509_V_ERR_AKID_SKID_MISMATCH; |
1327 | | /* Check serial number */ |
1328 | 19.5k | if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial)) |
1329 | 1.14k | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1330 | | /* Check issuer name */ |
1331 | 18.4k | if (akid->issuer) { |
1332 | | /* |
1333 | | * Ugh, for some peculiar reason AKID includes SEQUENCE OF |
1334 | | * GeneralName. So look for a DirName. There may be more than one but |
1335 | | * we only take any notice of the first. |
1336 | | */ |
1337 | 358 | GENERAL_NAMES *gens = akid->issuer; |
1338 | 358 | GENERAL_NAME *gen; |
1339 | 358 | X509_NAME *nm = NULL; |
1340 | 358 | int i; |
1341 | | |
1342 | 650 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
1343 | 447 | gen = sk_GENERAL_NAME_value(gens, i); |
1344 | 447 | if (gen->type == GEN_DIRNAME) { |
1345 | 155 | nm = gen->d.dirn; |
1346 | 155 | break; |
1347 | 155 | } |
1348 | 447 | } |
1349 | 358 | if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0) |
1350 | 106 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
1351 | 358 | } |
1352 | 18.3k | return X509_V_OK; |
1353 | 18.4k | } |
1354 | | |
1355 | | uint32_t X509_get_extension_flags(const X509 *x) |
1356 | 32.9k | { |
1357 | | /* Call for side-effect of computing hash and caching extensions */ |
1358 | 32.9k | X509_check_purpose(x, -1, 0); |
1359 | 32.9k | return x->ex_flags; |
1360 | 32.9k | } |
1361 | | |
1362 | | uint32_t X509_get_key_usage(const X509 *x) |
1363 | 24.0k | { |
1364 | | /* Call for side-effect of computing hash and caching extensions */ |
1365 | 24.0k | if (X509_check_purpose(x, -1, 0) != 1) |
1366 | 0 | return 0; |
1367 | 24.0k | return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX; |
1368 | 24.0k | } |
1369 | | |
1370 | | uint32_t X509_get_extended_key_usage(const X509 *x) |
1371 | 8 | { |
1372 | | /* Call for side-effect of computing hash and caching extensions */ |
1373 | 8 | if (X509_check_purpose(x, -1, 0) != 1) |
1374 | 0 | return 0; |
1375 | 8 | return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX; |
1376 | 8 | } |
1377 | | |
1378 | | const ASN1_OCTET_STRING *X509_get0_subject_key_id(const X509 *x) |
1379 | 6.90k | { |
1380 | | /* Call for side-effect of computing hash and caching extensions */ |
1381 | 6.90k | if (X509_check_purpose(x, -1, 0) != 1) |
1382 | 808 | return NULL; |
1383 | 6.09k | return x->skid; |
1384 | 6.90k | } |
1385 | | |
1386 | | const ASN1_OCTET_STRING *X509_get0_authority_key_id(const X509 *x) |
1387 | 0 | { |
1388 | | /* Call for side-effect of computing hash and caching extensions */ |
1389 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1390 | 0 | return NULL; |
1391 | 0 | return (x->akid != NULL ? x->akid->keyid : NULL); |
1392 | 0 | } |
1393 | | |
1394 | | const GENERAL_NAMES *X509_get0_authority_issuer(const X509 *x) |
1395 | 0 | { |
1396 | | /* Call for side-effect of computing hash and caching extensions */ |
1397 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1398 | 0 | return NULL; |
1399 | 0 | return (x->akid != NULL ? x->akid->issuer : NULL); |
1400 | 0 | } |
1401 | | |
1402 | | const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x) |
1403 | 0 | { |
1404 | | /* Call for side-effect of computing hash and caching extensions */ |
1405 | 0 | if (X509_check_purpose(x, -1, 0) != 1) |
1406 | 0 | return NULL; |
1407 | 0 | return (x->akid != NULL ? x->akid->serial : NULL); |
1408 | 0 | } |
1409 | | |
1410 | | long X509_get_pathlen(const X509 *x) |
1411 | 0 | { |
1412 | | /* Called for side effect of caching extensions */ |
1413 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1414 | 0 | || (x->ex_flags & EXFLAG_BCONS) == 0) |
1415 | 0 | return -1; |
1416 | 0 | return x->ex_pathlen; |
1417 | 0 | } |
1418 | | |
1419 | | long X509_get_proxy_pathlen(const X509 *x) |
1420 | 0 | { |
1421 | | /* Called for side effect of caching extensions */ |
1422 | 0 | if (X509_check_purpose(x, -1, 0) != 1 |
1423 | 0 | || (x->ex_flags & EXFLAG_PROXY) == 0) |
1424 | 0 | return -1; |
1425 | 0 | return x->ex_pcpathlen; |
1426 | 0 | } |