Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/policy.c

raw
1 // Copyright (c) 2022, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/x509.h>
5
6 #include <assert.h>
7
8 #include <openssl/mem.h>
9 #include <openssl/obj.h>
10 #include <openssl/stack.h>
11
12 #include "../internal.h"
13 #include "internal.h"
14
15
16 // This file computes the X.509 policy tree, as described in RFC 5280, section
17 // 6.1. It differs in that:
18 //
19 // (1) It does not track "qualifier_set". This is not needed as it is not
20 // output by this implementation.
21 //
22 // (2) It builds a directed acyclic graph, rather than a tree. When a given
23 // policy matches multiple parents, RFC 5280 makes a separate node for
24 // each parent. This representation condenses them into one node with
25 // multiple parents. Thus we refer to this structure as a "policy graph",
26 // rather than a "policy tree".
27 //
28 // (3) "expected_policy_set" is not tracked explicitly and built temporarily
29 // as part of building the graph.
30 //
31 // (4) anyPolicy nodes are not tracked explicitly.
32 //
33 // (5) Some pruning steps are deferred to when policies are evaluated, as a
34 // reachability pass.
35
36 // An X509_POLICY_NODE is a node in the policy graph. It corresponds to a node
37 // from RFC 5280, section 6.1.2, step (a), but we store some fields differently.
38 typedef struct x509_policy_node_st {
39 // policy is the "valid_policy" field from RFC 5280.
40 ASN1_OBJECT *policy;
41
42 // parent_policies, if non-empty, is the list of "valid_policy" values for all
43 // nodes which are a parent of this node. In this case, no entry in this list
44 // will be anyPolicy. This list is in no particular order and may contain
45 // duplicates if the corresponding certificate had duplicate mappings.
46 //
47 // If empty, this node has a single parent, anyPolicy. The node is then a root
48 // policies, and is in authorities-constrained-policy-set if it has a path to
49 // a leaf node.
50 //
51 // Note it is not possible for a policy to have both anyPolicy and a
52 // concrete policy as a parent. Section 6.1.3, step (d.1.ii) only runs if
53 // there was no match in step (d.1.i). We do not need to represent a parent
54 // list of, say, {anyPolicy, OID1, OID2}.
55 STACK_OF(ASN1_OBJECT) *parent_policies;
56
57 // mapped is one if this node matches a policy mapping in the certificate and
58 // zero otherwise.
59 int mapped;
60
61 // reachable is one if this node is reachable from some valid policy in the
62 // end-entity certificate. It is computed during |has_explicit_policy|.
63 int reachable;
64 } X509_POLICY_NODE;
65
66 DEFINE_STACK_OF(X509_POLICY_NODE)
67
68 // An X509_POLICY_LEVEL is the collection of nodes at the same depth in the
69 // policy graph. This structure can also be used to represent a level's
70 // "expected_policy_set" values. See |process_policy_mappings|.
71 typedef struct x509_policy_level_st {
72 // nodes is the list of nodes at this depth, except for the anyPolicy node, if
73 // any. This list is sorted by policy OID for efficient lookup.
74 STACK_OF(X509_POLICY_NODE) *nodes;
75
76 // has_any_policy is one if there is an anyPolicy node at this depth, and zero
77 // otherwise.
78 int has_any_policy;
79 } X509_POLICY_LEVEL;
80
81 DEFINE_STACK_OF(X509_POLICY_LEVEL)
82
83 static int is_any_policy(const ASN1_OBJECT *obj) {
84 return OBJ_obj2nid(obj) == NID_any_policy;
85 }
86
87 static void x509_policy_node_free(X509_POLICY_NODE *node) {
88 if (node != NULL) {
89 ASN1_OBJECT_free(node->policy);
90 sk_ASN1_OBJECT_pop_free(node->parent_policies, ASN1_OBJECT_free);
91 OPENSSL_free(node);
92 }
93 }
94
95 static X509_POLICY_NODE *x509_policy_node_new(const ASN1_OBJECT *policy) {
96 assert(!is_any_policy(policy));
97 X509_POLICY_NODE *node = OPENSSL_zalloc(sizeof(X509_POLICY_NODE));
98 if (node == NULL) {
99 return NULL;
100 }
101 node->policy = OBJ_dup(policy);
102 node->parent_policies = sk_ASN1_OBJECT_new_null();
103 if (node->policy == NULL || node->parent_policies == NULL) {
104 x509_policy_node_free(node);
105 return NULL;
106 }
107 return node;
108 }
109
110 static int x509_policy_node_cmp(const X509_POLICY_NODE *const *a,
111 const X509_POLICY_NODE *const *b) {
112 return OBJ_cmp((*a)->policy, (*b)->policy);
113 }
114
115 static void x509_policy_level_free(X509_POLICY_LEVEL *level) {
116 if (level != NULL) {
117 sk_X509_POLICY_NODE_pop_free(level->nodes, x509_policy_node_free);
118 OPENSSL_free(level);
119 }
120 }
121
122 static X509_POLICY_LEVEL *x509_policy_level_new(void) {
123 X509_POLICY_LEVEL *level = OPENSSL_zalloc(sizeof(X509_POLICY_LEVEL));
124 if (level == NULL) {
125 return NULL;
126 }
127 level->nodes = sk_X509_POLICY_NODE_new(x509_policy_node_cmp);
128 if (level->nodes == NULL) {
129 x509_policy_level_free(level);
130 return NULL;
131 }
132 return level;
133 }
134
135 static int x509_policy_level_is_empty(const X509_POLICY_LEVEL *level) {
136 return !level->has_any_policy && sk_X509_POLICY_NODE_num(level->nodes) == 0;
137 }
138
139 static void x509_policy_level_clear(X509_POLICY_LEVEL *level) {
140 level->has_any_policy = 0;
141 for (size_t i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
142 x509_policy_node_free(sk_X509_POLICY_NODE_value(level->nodes, i));
143 }
144 sk_X509_POLICY_NODE_zero(level->nodes);
145 }
146
147 // x509_policy_level_find returns the node in |level| corresponding to |policy|,
148 // or NULL if none exists.
149 static X509_POLICY_NODE *x509_policy_level_find(X509_POLICY_LEVEL *level,
150 const ASN1_OBJECT *policy) {
151 assert(sk_X509_POLICY_NODE_is_sorted(level->nodes));
152 X509_POLICY_NODE node;
153 node.policy = (ASN1_OBJECT *)policy;
154 size_t idx;
155 if (!sk_X509_POLICY_NODE_find_awslc(level->nodes, &idx, &node)) {
156 return NULL;
157 }
158 return sk_X509_POLICY_NODE_value(level->nodes, idx);
159 }
160
161 // x509_policy_level_add_nodes adds the nodes in |nodes| to |level|. It returns
162 // one on success and zero on error. No policy in |nodes| may already be present
163 // in |level|. This function modifies |nodes| to avoid making a copy, but the
164 // caller is still responsible for releasing |nodes| itself.
165 //
166 // This function is used to add nodes to |level| in bulk, and avoid resorting
167 // |level| after each addition.
168 static int x509_policy_level_add_nodes(X509_POLICY_LEVEL *level,
169 STACK_OF(X509_POLICY_NODE) *nodes) {
170 for (size_t i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) {
171 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(nodes, i);
172 if (!sk_X509_POLICY_NODE_push(level->nodes, node)) {
173 return 0;
174 }
175 sk_X509_POLICY_NODE_set(nodes, i, NULL);
176 }
177 sk_X509_POLICY_NODE_sort(level->nodes);
178
179 #if !defined(NDEBUG)
180 // There should be no duplicate nodes.
181 for (size_t i = 1; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
182 assert(OBJ_cmp(sk_X509_POLICY_NODE_value(level->nodes, i - 1)->policy,
183 sk_X509_POLICY_NODE_value(level->nodes, i)->policy) != 0);
184 }
185 #endif
186 return 1;
187 }
188
189 static int policyinfo_cmp(const POLICYINFO *const *a,
190 const POLICYINFO *const *b) {
191 return OBJ_cmp((*a)->policyid, (*b)->policyid);
192 }
193
194 static int delete_if_not_in_policies(X509_POLICY_NODE *node, void *data) {
195 const CERTIFICATEPOLICIES *policies = data;
196 assert(sk_POLICYINFO_is_sorted(policies));
197 POLICYINFO info;
198 info.policyid = node->policy;
199 if (sk_POLICYINFO_find_awslc(policies, NULL, &info)) {
200 return 0;
201 }
202 x509_policy_node_free(node);
203 return 1;
204 }
205
206 // process_certificate_policies updates |level| to incorporate |x509|'s
207 // certificate policies extension. This implements steps (d) and (e) of RFC
208 // 5280, section 6.1.3. |level| must contain the previous level's
209 // "expected_policy_set" information. For all but the top-most level, this is
210 // the output of |process_policy_mappings|. |any_policy_allowed| specifies
211 // whether anyPolicy is allowed or inhibited, taking into account the exception
212 // for self-issued certificates.
213 static int process_certificate_policies(const X509 *x509,
214 X509_POLICY_LEVEL *level,
215 int any_policy_allowed) {
216 int ret = 0;
217 int critical;
218 STACK_OF(X509_POLICY_NODE) *new_nodes = NULL;
219 CERTIFICATEPOLICIES *policies =
220 X509_get_ext_d2i(x509, NID_certificate_policies, &critical, NULL);
221 if (policies == NULL) {
222 if (critical != -1) {
223 return 0; // Syntax error in the extension.
224 }
225
226 // RFC 5280, section 6.1.3, step (e).
227 x509_policy_level_clear(level);
228 return 1;
229 }
230
231 // certificatePolicies may not be empty. See RFC 5280, section 4.2.1.4.
232 // TODO(https://crbug.com/boringssl/443): Move this check into the parser.
233 if (sk_POLICYINFO_num(policies) == 0) {
234 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_POLICY_EXTENSION);
235 goto err;
236 }
237
238 sk_POLICYINFO_set_cmp_func(policies, policyinfo_cmp);
239 sk_POLICYINFO_sort(policies);
240 int cert_has_any_policy = 0;
241 for (size_t i = 0; i < sk_POLICYINFO_num(policies); i++) {
242 const POLICYINFO *policy = sk_POLICYINFO_value(policies, i);
243 if (is_any_policy(policy->policyid)) {
244 cert_has_any_policy = 1;
245 }
246 if (i > 0 && OBJ_cmp(sk_POLICYINFO_value(policies, i - 1)->policyid,
247 policy->policyid) == 0) {
248 // Per RFC 5280, section 4.2.1.4, |policies| may not have duplicates.
249 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_POLICY_EXTENSION);
250 goto err;
251 }
252 }
253
254 // This does the same thing as RFC 5280, section 6.1.3, step (d), though in
255 // a slighty different order. |level| currently contains "expected_policy_set"
256 // values of the previous level. See |process_policy_mappings| for details.
257 const int previous_level_has_any_policy = level->has_any_policy;
258
259 // First, we handle steps (d.1.i) and (d.2). The net effect of these two steps
260 // is to intersect |level| with |policies|, ignoring anyPolicy if it is
261 // inhibited.
262 if (!cert_has_any_policy || !any_policy_allowed) {
263 sk_X509_POLICY_NODE_delete_if(level->nodes, delete_if_not_in_policies,
264 policies);
265 level->has_any_policy = 0;
266 }
267
268 // Step (d.1.ii) may attach new nodes to the previous level's anyPolicy node.
269 if (previous_level_has_any_policy) {
270 new_nodes = sk_X509_POLICY_NODE_new_null();
271 if (new_nodes == NULL) {
272 goto err;
273 }
274 for (size_t i = 0; i < sk_POLICYINFO_num(policies); i++) {
275 const POLICYINFO *policy = sk_POLICYINFO_value(policies, i);
276 // Though we've reordered the steps slightly, |policy| is in |level| if
277 // and only if it would have been a match in step (d.1.ii).
278 if (!is_any_policy(policy->policyid) &&
279 x509_policy_level_find(level, policy->policyid) == NULL) {
280 X509_POLICY_NODE *node = x509_policy_node_new(policy->policyid);
281 if (node == NULL || //
282 !sk_X509_POLICY_NODE_push(new_nodes, node)) {
283 x509_policy_node_free(node);
284 goto err;
285 }
286 }
287 }
288 if (!x509_policy_level_add_nodes(level, new_nodes)) {
289 goto err;
290 }
291 }
292
293 ret = 1;
294
295 err:
296 sk_X509_POLICY_NODE_pop_free(new_nodes, x509_policy_node_free);
297 CERTIFICATEPOLICIES_free(policies);
298 return ret;
299 }
300
301 static int compare_issuer_policy(const POLICY_MAPPING *const *a,
302 const POLICY_MAPPING *const *b) {
303 return OBJ_cmp((*a)->issuerDomainPolicy, (*b)->issuerDomainPolicy);
304 }
305
306 static int compare_subject_policy(const POLICY_MAPPING *const *a,
307 const POLICY_MAPPING *const *b) {
308 return OBJ_cmp((*a)->subjectDomainPolicy, (*b)->subjectDomainPolicy);
309 }
310
311 static int delete_if_mapped(X509_POLICY_NODE *node, void *data) {
312 const POLICY_MAPPINGS *mappings = data;
313 // |mappings| must have been sorted by |compare_issuer_policy|.
314 assert(sk_POLICY_MAPPING_is_sorted(mappings));
315 POLICY_MAPPING mapping;
316 mapping.issuerDomainPolicy = node->policy;
317 if (!sk_POLICY_MAPPING_find_awslc(mappings, NULL, &mapping)) {
318 return 0;
319 }
320 x509_policy_node_free(node);
321 return 1;
322 }
323
324 // process_policy_mappings processes the policy mappings extension of |cert|,
325 // whose corresponding graph level is |level|. |mapping_allowed| specifies
326 // whether policy mapping is inhibited at this point. On success, it returns an
327 // |X509_POLICY_LEVEL| containing the "expected_policy_set" for |level|. On
328 // error, it returns NULL. This implements steps (a) and (b) of RFC 5280,
329 // section 6.1.4.
330 //
331 // We represent the "expected_policy_set" as an |X509_POLICY_LEVEL|.
332 // |has_any_policy| indicates whether there is an anyPolicy node with
333 // "expected_policy_set" of {anyPolicy}. If a node with policy oid P1 contains
334 // P2 in its "expected_policy_set", the level will contain a node of policy P2
335 // with P1 in |parent_policies|.
336 //
337 // This is equivalent to the |X509_POLICY_LEVEL| that would result if the next
338 // certificats contained anyPolicy. |process_certificate_policies| will filter
339 // this result down to compute the actual level.
340 static X509_POLICY_LEVEL *process_policy_mappings(const X509 *cert,
341 X509_POLICY_LEVEL *level,
342 int mapping_allowed) {
343 int ok = 0;
344 STACK_OF(X509_POLICY_NODE) *new_nodes = NULL;
345 X509_POLICY_LEVEL *next = NULL;
346 int critical;
347 POLICY_MAPPINGS *mappings =
348 X509_get_ext_d2i(cert, NID_policy_mappings, &critical, NULL);
349 if (mappings == NULL && critical != -1) {
350 // Syntax error in the policy mappings extension.
351 goto err;
352 }
353
354 if (mappings != NULL) {
355 // PolicyMappings may not be empty. See RFC 5280, section 4.2.1.5.
356 // TODO(https://crbug.com/boringssl/443): Move this check into the parser.
357 if (sk_POLICY_MAPPING_num(mappings) == 0) {
358 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_POLICY_EXTENSION);
359 goto err;
360 }
361
362 // RFC 5280, section 6.1.4, step (a).
363 for (size_t i = 0; i < sk_POLICY_MAPPING_num(mappings); i++) {
364 POLICY_MAPPING *mapping = sk_POLICY_MAPPING_value(mappings, i);
365 if (is_any_policy(mapping->issuerDomainPolicy) ||
366 is_any_policy(mapping->subjectDomainPolicy)) {
367 goto err;
368 }
369 }
370
371 // Sort to group by issuerDomainPolicy.
372 sk_POLICY_MAPPING_set_cmp_func(mappings, compare_issuer_policy);
373 sk_POLICY_MAPPING_sort(mappings);
374
375 if (mapping_allowed) {
376 // Mark nodes as mapped, and add any nodes to |level| which may be needed
377 // as part of RFC 5280, section 6.1.4, step (b.1).
378 new_nodes = sk_X509_POLICY_NODE_new_null();
379 if (new_nodes == NULL) {
380 goto err;
381 }
382 const ASN1_OBJECT *last_policy = NULL;
383 for (size_t i = 0; i < sk_POLICY_MAPPING_num(mappings); i++) {
384 const POLICY_MAPPING *mapping = sk_POLICY_MAPPING_value(mappings, i);
385 // There may be multiple mappings with the same |issuerDomainPolicy|.
386 if (last_policy != NULL &&
387 OBJ_cmp(mapping->issuerDomainPolicy, last_policy) == 0) {
388 continue;
389 }
390 last_policy = mapping->issuerDomainPolicy;
391
392 X509_POLICY_NODE *node =
393 x509_policy_level_find(level, mapping->issuerDomainPolicy);
394 if (node == NULL) {
395 if (!level->has_any_policy) {
396 continue;
397 }
398 node = x509_policy_node_new(mapping->issuerDomainPolicy);
399 if (node == NULL || //
400 !sk_X509_POLICY_NODE_push(new_nodes, node)) {
401 x509_policy_node_free(node);
402 goto err;
403 }
404 }
405 node->mapped = 1;
406 }
407 if (!x509_policy_level_add_nodes(level, new_nodes)) {
408 goto err;
409 }
410 } else {
411 // RFC 5280, section 6.1.4, step (b.2). If mapping is inhibited, delete
412 // all mapped nodes.
413 sk_X509_POLICY_NODE_delete_if(level->nodes, delete_if_mapped, mappings);
414 sk_POLICY_MAPPING_pop_free(mappings, POLICY_MAPPING_free);
415 mappings = NULL;
416 }
417 }
418
419 // If a node was not mapped, it retains the original "explicit_policy_set"
420 // value, itself. Add those to |mappings|.
421 if (mappings == NULL) {
422 mappings = sk_POLICY_MAPPING_new_null();
423 if (mappings == NULL) {
424 goto err;
425 }
426 }
427 for (size_t i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
428 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(level->nodes, i);
429 if (!node->mapped) {
430 POLICY_MAPPING *mapping = POLICY_MAPPING_new();
431 if (mapping == NULL) {
432 goto err;
433 }
434 mapping->issuerDomainPolicy = OBJ_dup(node->policy);
435 mapping->subjectDomainPolicy = OBJ_dup(node->policy);
436 if (mapping->issuerDomainPolicy == NULL ||
437 mapping->subjectDomainPolicy == NULL ||
438 !sk_POLICY_MAPPING_push(mappings, mapping)) {
439 POLICY_MAPPING_free(mapping);
440 goto err;
441 }
442 }
443 }
444
445 // Sort to group by subjectDomainPolicy.
446 sk_POLICY_MAPPING_set_cmp_func(mappings, compare_subject_policy);
447 sk_POLICY_MAPPING_sort(mappings);
448
449 // Convert |mappings| to our "expected_policy_set" representation.
450 next = x509_policy_level_new();
451 if (next == NULL) {
452 goto err;
453 }
454 next->has_any_policy = level->has_any_policy;
455
456 X509_POLICY_NODE *last_node = NULL;
457 for (size_t i = 0; i < sk_POLICY_MAPPING_num(mappings); i++) {
458 POLICY_MAPPING *mapping = sk_POLICY_MAPPING_value(mappings, i);
459 // Skip mappings where |issuerDomainPolicy| does not appear in the graph.
460 if (!level->has_any_policy &&
461 x509_policy_level_find(level, mapping->issuerDomainPolicy) == NULL) {
462 continue;
463 }
464
465 if (last_node == NULL ||
466 OBJ_cmp(last_node->policy, mapping->subjectDomainPolicy) != 0) {
467 last_node = x509_policy_node_new(mapping->subjectDomainPolicy);
468 if (last_node == NULL ||
469 !sk_X509_POLICY_NODE_push(next->nodes, last_node)) {
470 x509_policy_node_free(last_node);
471 goto err;
472 }
473 }
474
475 if (!sk_ASN1_OBJECT_push(last_node->parent_policies,
476 mapping->issuerDomainPolicy)) {
477 goto err;
478 }
479 mapping->issuerDomainPolicy = NULL;
480 }
481
482 sk_X509_POLICY_NODE_sort(next->nodes);
483 ok = 1;
484
485 err:
486 if (!ok) {
487 x509_policy_level_free(next);
488 next = NULL;
489 }
490
491 sk_POLICY_MAPPING_pop_free(mappings, POLICY_MAPPING_free);
492 sk_X509_POLICY_NODE_pop_free(new_nodes, x509_policy_node_free);
493 return next;
494 }
495
496 // apply_skip_certs, if |skip_certs| is non-NULL, sets |*value| to the minimum
497 // of its current value and |skip_certs|. It returns one on success and zero if
498 // |skip_certs| is negative.
499 static int apply_skip_certs(const ASN1_INTEGER *skip_certs, size_t *value) {
500 if (skip_certs == NULL) {
501 return 1;
502 }
503
504 // TODO(https://crbug.com/boringssl/443): Move this check into the parser.
505 if (skip_certs->type & V_ASN1_NEG) {
506 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_POLICY_EXTENSION);
507 return 0;
508 }
509
510 // If |skip_certs| does not fit in |uint64_t|, it must exceed |*value|.
511 uint64_t u64;
512 if (ASN1_INTEGER_get_uint64(&u64, skip_certs) && u64 < *value) {
513 *value = (size_t)u64;
514 }
515 ERR_clear_error();
516 return 1;
517 }
518
519 // process_policy_constraints updates |*explicit_policy|, |*policy_mapping|, and
520 // |*inhibit_any_policy| according to |x509|'s policy constraints and inhibit
521 // anyPolicy extensions. It returns one on success and zero on error. This
522 // implements steps (i) and (j) of RFC 5280, section 6.1.4.
523 static int process_policy_constraints(const X509 *x509, size_t *explicit_policy,
524 size_t *policy_mapping,
525 size_t *inhibit_any_policy) {
526 int critical;
527 POLICY_CONSTRAINTS *constraints =
528 X509_get_ext_d2i(x509, NID_policy_constraints, &critical, NULL);
529 if (constraints == NULL && critical != -1) {
530 return 0;
531 }
532 if (constraints != NULL) {
533 if (constraints->requireExplicitPolicy == NULL &&
534 constraints->inhibitPolicyMapping == NULL) {
535 // Per RFC 5280, section 4.2.1.11, at least one of the fields must be
536 // present.
537 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_POLICY_EXTENSION);
538 POLICY_CONSTRAINTS_free(constraints);
539 return 0;
540 }
541 int ok =
542 apply_skip_certs(constraints->requireExplicitPolicy, explicit_policy) &&
543 apply_skip_certs(constraints->inhibitPolicyMapping, policy_mapping);
544 POLICY_CONSTRAINTS_free(constraints);
545 if (!ok) {
546 return 0;
547 }
548 }
549
550 ASN1_INTEGER *inhibit_any_policy_ext =
551 X509_get_ext_d2i(x509, NID_inhibit_any_policy, &critical, NULL);
552 if (inhibit_any_policy_ext == NULL && critical != -1) {
553 return 0;
554 }
555 int ok = apply_skip_certs(inhibit_any_policy_ext, inhibit_any_policy);
556 ASN1_INTEGER_free(inhibit_any_policy_ext);
557 return ok;
558 }
559
560 // has_explicit_policy returns one if the set of authority-space policy OIDs
561 // |levels| has some non-empty intersection with |user_policies|, and zero
562 // otherwise. This mirrors the logic in RFC 5280, section 6.1.5, step (g). This
563 // function modifies |levels| and should only be called at the end of policy
564 // evaluation.
565 static int has_explicit_policy(STACK_OF(X509_POLICY_LEVEL) *levels,
566 const STACK_OF(ASN1_OBJECT) *user_policies) {
567 assert(sk_ASN1_OBJECT_is_sorted(user_policies));
568
569 // Step (g.i). If the policy graph is empty, the intersection is empty.
570 size_t num_levels = sk_X509_POLICY_LEVEL_num(levels);
571 X509_POLICY_LEVEL *level = sk_X509_POLICY_LEVEL_value(levels, num_levels - 1);
572 if (x509_policy_level_is_empty(level)) {
573 return 0;
574 }
575
576 // If |user_policies| is empty, we interpret it as having a single anyPolicy
577 // value. The caller may also have supplied anyPolicy explicitly.
578 int user_has_any_policy = sk_ASN1_OBJECT_num(user_policies) == 0;
579 for (size_t i = 0; i < sk_ASN1_OBJECT_num(user_policies); i++) {
580 if (is_any_policy(sk_ASN1_OBJECT_value(user_policies, i))) {
581 user_has_any_policy = 1;
582 break;
583 }
584 }
585
586 // Step (g.ii). If the policy graph is not empty and the user set contains
587 // anyPolicy, the intersection is the entire (non-empty) graph.
588 if (user_has_any_policy) {
589 return 1;
590 }
591
592 // Step (g.iii) does not delete anyPolicy nodes, so if the graph has
593 // anyPolicy, some explicit policy will survive. The actual intersection may
594 // synthesize some nodes in step (g.iii.3), but we do not return the policy
595 // list itself, so we skip actually computing this.
596 if (level->has_any_policy) {
597 return 1;
598 }
599
600 // We defer pruning the tree, so as we look for nodes with parent anyPolicy,
601 // step (g.iii.1), we must limit to nodes reachable from the bottommost level.
602 // Start by marking each of those nodes as reachable.
603 for (size_t i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
604 sk_X509_POLICY_NODE_value(level->nodes, i)->reachable = 1;
605 }
606
607 for (size_t i = num_levels - 1; i < num_levels; i--) {
608 level = sk_X509_POLICY_LEVEL_value(levels, i);
609 for (size_t j = 0; j < sk_X509_POLICY_NODE_num(level->nodes); j++) {
610 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(level->nodes, j);
611 if (!node->reachable) {
612 continue;
613 }
614 if (sk_ASN1_OBJECT_num(node->parent_policies) == 0) {
615 // |node|'s parent is anyPolicy and is part of "valid_policy_node_set".
616 // If it exists in |user_policies|, the intersection is non-empty and we
617 // can return immediately.
618 if (sk_ASN1_OBJECT_find_awslc(user_policies, NULL, node->policy)) {
619 return 1;
620 }
621 } else if (i > 0) {
622 // |node|'s parents are concrete policies. Mark the parents reachable,
623 // to be inspected by the next loop iteration.
624 X509_POLICY_LEVEL *prev = sk_X509_POLICY_LEVEL_value(levels, i - 1);
625 for (size_t k = 0; k < sk_ASN1_OBJECT_num(node->parent_policies); k++) {
626 X509_POLICY_NODE *parent = x509_policy_level_find(
627 prev, sk_ASN1_OBJECT_value(node->parent_policies, k));
628 if (parent != NULL) {
629 parent->reachable = 1;
630 }
631 }
632 }
633 }
634 }
635
636 return 0;
637 }
638
639 static int asn1_object_cmp(const ASN1_OBJECT *const *a,
640 const ASN1_OBJECT *const *b) {
641 return OBJ_cmp(*a, *b);
642 }
643
644 int X509_policy_check(const STACK_OF(X509) *certs,
645 const STACK_OF(ASN1_OBJECT) *user_policies,
646 unsigned long flags, X509 **out_current_cert) {
647 *out_current_cert = NULL;
648 int ret = X509_V_ERR_OUT_OF_MEM;
649 X509_POLICY_LEVEL *level = NULL;
650 STACK_OF(X509_POLICY_LEVEL) *levels = NULL;
651 STACK_OF(ASN1_OBJECT) *user_policies_sorted = NULL;
652 size_t num_certs = sk_X509_num(certs);
653
654 // Skip policy checking if the chain is just the trust anchor.
655 if (num_certs <= 1) {
656 return X509_V_OK;
657 }
658
659 // See RFC 5280, section 6.1.2, steps (d) through (f).
660 size_t explicit_policy =
661 (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : num_certs + 1;
662 size_t inhibit_any_policy =
663 (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : num_certs + 1;
664 size_t policy_mapping =
665 (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : num_certs + 1;
666
667 levels = sk_X509_POLICY_LEVEL_new_null();
668 if (levels == NULL) {
669 goto err;
670 }
671
672 for (size_t i = num_certs - 2; i < num_certs; i--) {
673 X509 *cert = sk_X509_value(certs, i);
674 if (!x509v3_cache_extensions(cert)) {
675 goto err;
676 }
677 const int is_self_issued = (cert->ex_flags & EXFLAG_SI) != 0;
678
679 if (level == NULL) {
680 assert(i == num_certs - 2);
681 level = x509_policy_level_new();
682 if (level == NULL) {
683 goto err;
684 }
685 level->has_any_policy = 1;
686 }
687
688 // RFC 5280, section 6.1.3, steps (d) and (e). |any_policy_allowed| is
689 // computed as in step (d.2).
690 const int any_policy_allowed =
691 inhibit_any_policy > 0 || (i > 0 && is_self_issued);
692 if (!process_certificate_policies(cert, level, any_policy_allowed)) {
693 ret = X509_V_ERR_INVALID_POLICY_EXTENSION;
694 *out_current_cert = cert;
695 goto err;
696 }
697
698 // RFC 5280, section 6.1.3, step (f).
699 if (explicit_policy == 0 && x509_policy_level_is_empty(level)) {
700 ret = X509_V_ERR_NO_EXPLICIT_POLICY;
701 goto err;
702 }
703
704 // Insert into the list.
705 if (!sk_X509_POLICY_LEVEL_push(levels, level)) {
706 goto err;
707 }
708 X509_POLICY_LEVEL *current_level = level;
709 level = NULL;
710
711 // If this is not the leaf certificate, we go to section 6.1.4. If it
712 // is the leaf certificate, we go to section 6.1.5 instead.
713 if (i != 0) {
714 // RFC 5280, section 6.1.4, steps (a) and (b).
715 level = process_policy_mappings(cert, current_level, policy_mapping > 0);
716 if (level == NULL) {
717 ret = X509_V_ERR_INVALID_POLICY_EXTENSION;
718 *out_current_cert = cert;
719 goto err;
720 }
721 }
722
723 // RFC 5280, section 6.1.4, step (h-j) for non-leaves, and section 6.1.5,
724 // step (a-b) for leaves. In the leaf case, RFC 5280 says only to update
725 // |explicit_policy|, but |policy_mapping| and |inhibit_any_policy| are no
726 // longer read at this point, so we use the same process.
727 if (i == 0 || !is_self_issued) {
728 if (explicit_policy > 0) {
729 explicit_policy--;
730 }
731 if (policy_mapping > 0) {
732 policy_mapping--;
733 }
734 if (inhibit_any_policy > 0) {
735 inhibit_any_policy--;
736 }
737 }
738 if (!process_policy_constraints(cert, &explicit_policy, &policy_mapping,
739 &inhibit_any_policy)) {
740 ret = X509_V_ERR_INVALID_POLICY_EXTENSION;
741 *out_current_cert = cert;
742 goto err;
743 }
744 }
745
746 // RFC 5280, section 6.1.5, step (g). We do not output the policy set, so it
747 // is only necessary to check if the user-constrained-policy-set is not empty.
748 if (explicit_policy == 0) {
749 // Build a sorted copy of |user_policies| for more efficient lookup.
750 user_policies_sorted = sk_ASN1_OBJECT_dup(user_policies);
751 if (user_policies_sorted == NULL) {
752 goto err;
753 }
754 sk_ASN1_OBJECT_set_cmp_func(user_policies_sorted, asn1_object_cmp);
755 sk_ASN1_OBJECT_sort(user_policies_sorted);
756
757 if (!has_explicit_policy(levels, user_policies_sorted)) {
758 ret = X509_V_ERR_NO_EXPLICIT_POLICY;
759 goto err;
760 }
761 }
762
763 ret = X509_V_OK;
764
765 err:
766 x509_policy_level_free(level);
767 // |user_policies_sorted|'s contents are owned by |user_policies|, so we do
768 // not use |sk_ASN1_OBJECT_pop_free|.
769 sk_ASN1_OBJECT_free(user_policies_sorted);
770 sk_X509_POLICY_LEVEL_pop_free(levels, x509_policy_level_free);
771 return ret;
772 }