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