aed0155c2afc2f8fea1120275a98add0032cf7a7
[openssl.git] / crypto / x509v3 / pcy_tree.c
1 /* pcy_tree.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 2004.
4  */
5 /* ====================================================================
6  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include "cryptlib.h"
60 #include <openssl/x509.h>
61 #include <openssl/x509v3.h>
62
63 #include "pcy_int.h"
64
65 /* Initialize policy tree. Return values:
66  *  0 Some internal error occured.
67  * -1 Inconsistent or invalid extensions in certificates.
68  *  1 Tree initialized OK.
69  *  2 Policy tree is empty.
70  *  5 Tree OK and requireExplicitPolicy true.
71  *  6 Tree empty and requireExplicitPolicy true.
72  */
73
74 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
75                         unsigned int flags)
76         {
77         X509_POLICY_TREE *tree;
78         X509_POLICY_LEVEL *level;
79         const X509_POLICY_CACHE *cache;
80         X509_POLICY_DATA *data = NULL;
81         X509 *x;
82         int ret = 1;
83         int i, n;
84         int explicit_policy;
85         int any_skip;
86         int map_skip;
87         *ptree = NULL;
88         n = sk_X509_num(certs);
89
90         /* Disable policy mapping for now... */
91         flags |= X509_V_FLAG_INHIBIT_MAP;
92
93         if (flags & X509_V_FLAG_EXPLICIT_POLICY)
94                 explicit_policy = 0;
95         else
96                 explicit_policy = n + 1;
97
98         if (flags & X509_V_FLAG_INHIBIT_ANY)
99                 any_skip = 0;
100         else
101                 any_skip = n + 1;
102
103         if (flags & X509_V_FLAG_INHIBIT_MAP)
104                 map_skip = 0;
105         else
106                 map_skip = n + 1;
107
108         /* Can't do anything with just a trust anchor */
109         if (n == 1)
110                 return 1;
111         /* First setup policy cache in all certificates apart from the
112          * trust anchor. Note any bad cache results on the way. Also can
113          * calculate explicit_policy value at this point.
114          */
115         for (i = n - 2; i >= 0; i--)
116                 {
117                 x = sk_X509_value(certs, i);
118                 X509_check_purpose(x, -1, -1);
119                 cache = policy_cache_set(x);
120                 /* If cache NULL something bad happened: return immediately */
121                 if (cache == NULL)
122                         return 0;
123                 /* If inconsistent extensions keep a note of it but continue */
124                 if (x->ex_flags & EXFLAG_INVALID_POLICY)
125                         ret = -1;
126                 /* Otherwise if we have no data (hence no CertificatePolicies)
127                  * and haven't already set an inconsistent code note it.
128                  */
129                 else if ((ret == 1) && !cache->data)
130                         ret = 2;
131                 if (explicit_policy > 0)
132                         {
133                         explicit_policy--;
134                         if (!(x->ex_flags & EXFLAG_SI)
135                                 && (cache->explicit_skip != -1)
136                                 && (cache->explicit_skip < explicit_policy))
137                                 explicit_policy = cache->explicit_skip;
138                         }
139                 }
140
141         if (ret != 1)
142                 {
143                 if (ret == 2 && !explicit_policy)
144                         return 6;
145                 return ret;
146                 }
147
148
149         /* If we get this far initialize the tree */
150
151         tree = OPENSSL_malloc(sizeof(X509_POLICY_TREE));
152
153         if (!tree)
154                 return 0;
155
156         tree->flags = 0;
157         tree->levels = OPENSSL_malloc(sizeof(X509_POLICY_LEVEL) * n);
158         tree->nlevel = 0;
159         tree->extra_data = NULL;
160         tree->auth_policies = NULL;
161         tree->user_policies = NULL;
162
163         if (!tree)
164                 {
165                 OPENSSL_free(tree);
166                 return 0;
167                 }
168
169         memset(tree->levels, 0, n * sizeof(X509_POLICY_LEVEL));
170
171         tree->nlevel = n;
172
173         level = tree->levels;
174
175         /* Root data: initialize to anyPolicy */
176
177         data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
178
179         if (!data || !level_add_node(level, data, NULL, tree))
180                 goto bad_tree;
181
182         for (i = n - 2; i >= 0; i--)
183                 {
184                 level++;
185                 x = sk_X509_value(certs, i);
186                 cache = policy_cache_set(x);
187
188                 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
189                 level->cert = x;
190
191                 if (!cache->anyPolicy)
192                                 level->flags |= X509_V_FLAG_INHIBIT_ANY;
193
194                 /* Determine inhibit any and inhibit map flags */
195                 if (any_skip == 0)
196                         {
197                         /* Any matching allowed if certificate is self
198                          * issued and not the last in the chain.
199                          */
200                         if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
201                                 level->flags |= X509_V_FLAG_INHIBIT_ANY;
202                         }
203                 else
204                         {
205                         any_skip--;
206                         if ((cache->any_skip > 0)
207                                 && (cache->any_skip < any_skip))
208                                 any_skip = cache->any_skip;
209                         }
210
211                 if (map_skip == 0)
212                         level->flags |= X509_V_FLAG_INHIBIT_MAP;
213                 else
214                         {
215                         map_skip--;
216                         if ((cache->map_skip > 0)
217                                 && (cache->map_skip < map_skip))
218                                 map_skip = cache->map_skip;
219                         }
220
221
222                 }
223
224         *ptree = tree;
225
226         if (explicit_policy)
227                 return 1;
228         else
229                 return 5;
230
231         bad_tree:
232
233         X509_policy_tree_free(tree);
234
235         return 0;
236
237         }
238
239 /* This corresponds to RFC3280 XXXX XXXXX:
240  * link any data from CertificatePolicies onto matching parent
241  * or anyPolicy if no match.
242  */
243
244 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
245                                 const X509_POLICY_CACHE *cache)
246         {
247         int i;
248         X509_POLICY_LEVEL *last;
249         X509_POLICY_DATA *data;
250         X509_POLICY_NODE *parent;
251         last = curr - 1;
252         for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++)
253                 {
254                 data = sk_X509_POLICY_DATA_value(cache->data, i);
255                 /* If a node is mapped any it doesn't have a corresponding
256                  * CertificatePolicies entry. 
257                  * However such an identical node would be created
258                  * if anyPolicy matching is enabled because there would be
259                  * no match with the parent valid_policy_set. So we create
260                  * link because then it will have the mapping flags
261                  * right and we can prune it later.
262                  */
263                 if ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)
264                         && !(curr->flags & X509_V_FLAG_INHIBIT_ANY))
265                         continue;
266                 /* Look for matching node in parent */
267                 parent = level_find_node(last, data->valid_policy);
268                 /* If no match link to anyPolicy */
269                 if (!parent)
270                         parent = last->anyPolicy;
271                 if (parent && !level_add_node(curr, data, parent, NULL))
272                                 return 0;
273                 }
274         return 1;
275         }
276
277 /* This corresponds to RFC3280 XXXX XXXXX:
278  * Create new data for any unmatched policies in the parent and link
279  * to anyPolicy.
280  */
281
282 static int tree_link_any(X509_POLICY_LEVEL *curr,
283                         const X509_POLICY_CACHE *cache,
284                         X509_POLICY_TREE *tree)
285         {
286         int i;
287         X509_POLICY_DATA *data;
288         X509_POLICY_NODE *node;
289         X509_POLICY_LEVEL *last;
290
291         last = curr - 1;
292
293         for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++)
294                 {
295                 node = sk_X509_POLICY_NODE_value(last->nodes, i);
296
297                 /* Skip any node with any children: we only want unmathced
298                  * nodes.
299                  *
300                  * Note: need something better for policy mapping
301                  * because each node may have multiple children 
302                  */
303                 if (node->nchild)
304                         continue;
305                 /* Create a new node with qualifiers from anyPolicy and
306                  * id from unmatched node.
307                  */
308                 data = policy_data_new(NULL, node->data->valid_policy, 
309                                                 node_critical(node));
310
311                 if (data == NULL)
312                         return 0;
313                 /* Curr may not have anyPolicy */
314                 data->qualifier_set = cache->anyPolicy->qualifier_set;
315                 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
316                 if (!level_add_node(curr, data, node, tree))
317                         {
318                         policy_data_free(data);
319                         return 0;
320                         }
321                 }
322         /* Finally add link to anyPolicy */
323         if (last->anyPolicy)
324                 {
325                 if (!level_add_node(curr, cache->anyPolicy,
326                                                 last->anyPolicy, NULL))
327                         return 0;
328                 }
329         return 1;
330         }
331
332 /* Prune the tree: delete any child mapped child data on the current level
333  * then proceed up the tree deleting any data with no children. If we ever
334  * have no data on a level we can halt because the tree will be empty.
335  */
336
337 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
338         {
339         X509_POLICY_NODE *node;
340         int i;
341         for (i = sk_X509_POLICY_NODE_num(curr->nodes) - 1; i >= 0; i--)
342                 {
343                 node = sk_X509_POLICY_NODE_value(curr->nodes, i);
344                 /* Delete any mapped data: see RFC3280 XXXX */
345                 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK)
346                         {
347                         node->parent->nchild--;
348                         OPENSSL_free(node);
349                         (void)sk_X509_POLICY_NODE_delete(curr->nodes, i);
350                         }
351                 }
352
353         for(;;) {
354                 --curr;
355                 for (i = sk_X509_POLICY_NODE_num(curr->nodes) - 1; i >= 0; i--)
356                         {
357                         node = sk_X509_POLICY_NODE_value(curr->nodes, i);
358                         if (node->nchild == 0)
359                                 {
360                                 node->parent->nchild--;
361                                 OPENSSL_free(node);
362                                 (void)sk_X509_POLICY_NODE_delete(curr->nodes, i);
363                                 }
364                         }
365                 if (curr->anyPolicy && !curr->anyPolicy->nchild)
366                         {
367                         if (curr->anyPolicy->parent)
368                                 curr->anyPolicy->parent->nchild--;
369                         OPENSSL_free(curr->anyPolicy);
370                         curr->anyPolicy = NULL;
371                         }
372                 if (curr == tree->levels)
373                         {
374                         /* If we zapped anyPolicy at top then tree is empty */
375                         if (!curr->anyPolicy)
376                                         return 2;
377                         return 1;
378                         }
379                 }
380
381         return 1;
382
383         }
384
385 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
386                                                  X509_POLICY_NODE *pcy)
387         {
388         if (!*pnodes)
389                 {
390                 *pnodes = policy_node_cmp_new();
391                 if (!*pnodes)
392                         return 0;
393                 }
394         else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
395                 return 1;
396
397         if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
398                 return 0;
399
400         return 1;
401
402         }
403
404 /* Calculate the authority set based on policy tree.
405  * The 'pnodes' parameter is used as a store for the set of policy nodes
406  * used to calculate the user set. If the authority set is not anyPolicy
407  * then pnodes will just point to the authority set. If however the authority
408  * set is anyPolicy then the set of valid policies (other than anyPolicy)
409  * is store in pnodes. The return value of '2' is used in this case to indicate
410  * that pnodes should be freed.
411  */
412
413 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
414                                         STACK_OF(X509_POLICY_NODE) **pnodes)
415         {
416         X509_POLICY_LEVEL *curr;
417         X509_POLICY_NODE *node, *anyptr;
418         STACK_OF(X509_POLICY_NODE) **addnodes;
419         int i, j;
420         curr = tree->levels + tree->nlevel - 1;
421
422         /* If last level contains anyPolicy set is anyPolicy */
423         if (curr->anyPolicy)
424                 {
425                 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
426                         return 0;
427                 addnodes = pnodes;
428                 }
429         else
430                 /* Add policies to authority set */
431                 addnodes = &tree->auth_policies;
432
433         curr = tree->levels;
434         for (i = 1; i < tree->nlevel; i++)
435                 {
436                 /* If no anyPolicy node on this this level it can't
437                  * appear on lower levels so end search.
438                  */
439                 if (!(anyptr = curr->anyPolicy))
440                         break;
441                 curr++;
442                 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++)
443                         {
444                         node = sk_X509_POLICY_NODE_value(curr->nodes, j);
445                         if ((node->parent == anyptr)
446                                 && !tree_add_auth_node(addnodes, node))
447                                         return 0;
448                         }
449                 }
450
451         if (addnodes == pnodes)
452                 return 2;
453
454         *pnodes = tree->auth_policies;
455
456         return 1;
457         }
458
459 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
460                                 STACK_OF(ASN1_OBJECT) *policy_oids,
461                                 STACK_OF(X509_POLICY_NODE) *auth_nodes)
462         {
463         int i;
464         X509_POLICY_NODE *node;
465         ASN1_OBJECT *oid;
466
467         X509_POLICY_NODE *anyPolicy;
468         X509_POLICY_DATA *extra;
469
470         /* Check if anyPolicy present in authority constrained policy set:
471          * this will happen if it is a leaf node.
472          */
473
474         if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
475                 return 1;
476
477         anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
478
479         for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
480                 {
481                 oid = sk_ASN1_OBJECT_value(policy_oids, i);
482                 if (OBJ_obj2nid(oid) == NID_any_policy)
483                         {
484                         tree->flags |= POLICY_FLAG_ANY_POLICY;
485                         return 1;
486                         }
487                 }
488
489         for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
490                 {
491                 oid = sk_ASN1_OBJECT_value(policy_oids, i);
492                 node = tree_find_sk(auth_nodes, oid);
493                 if (!node)
494                         {
495                         if (!anyPolicy)
496                                 continue;
497                         /* Create a new node with policy ID from user set
498                          * and qualifiers from anyPolicy.
499                          */
500                         extra = policy_data_new(NULL, oid,
501                                                 node_critical(anyPolicy));
502                         if (!extra)
503                                 return 0;
504                         extra->qualifier_set = anyPolicy->data->qualifier_set;
505                         extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
506                                                 | POLICY_DATA_FLAG_EXTRA_NODE;
507                         node = level_add_node(NULL, extra, anyPolicy->parent,
508                                                 tree);
509                         }
510                 if (!tree->user_policies)
511                         {
512                         tree->user_policies = sk_X509_POLICY_NODE_new_null();
513                         if (!tree->user_policies)
514                                 return 1;
515                         }
516                 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
517                         return 0;
518                 }
519         return 1;
520
521         }
522
523 static int tree_evaluate(X509_POLICY_TREE *tree)
524         {
525         int ret, i;
526         X509_POLICY_LEVEL *curr = tree->levels + 1;
527         const X509_POLICY_CACHE *cache;
528
529         for(i = 1; i < tree->nlevel; i++, curr++)
530                 {
531                 cache = policy_cache_set(curr->cert);
532                 if (!tree_link_nodes(curr, cache))
533                         return 0;
534
535                 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
536                         && !tree_link_any(curr, cache, tree))
537                         return 0;
538                 ret = tree_prune(tree, curr);
539                 if (ret != 1)
540                         return ret;
541                 }
542
543         return 1;
544
545         }
546
547 static void exnode_free(X509_POLICY_NODE *node)
548         {
549         if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
550                 OPENSSL_free(node);
551         }
552
553
554 void X509_policy_tree_free(X509_POLICY_TREE *tree)
555         {
556         X509_POLICY_LEVEL *curr;
557         int i;
558
559         if (!tree)
560                 return;
561
562         sk_X509_POLICY_NODE_free(tree->auth_policies);
563         sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
564
565         for(i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++)
566                 {
567                 if (curr->cert)
568                         X509_free(curr->cert);
569                 if (curr->nodes)
570                         sk_X509_POLICY_NODE_pop_free(curr->nodes,
571                                                 policy_node_free);
572                 if (curr->anyPolicy)
573                         policy_node_free(curr->anyPolicy);
574                 }
575
576         if (tree->extra_data)
577                 sk_X509_POLICY_DATA_pop_free(tree->extra_data,
578                                                 policy_data_free);
579
580         OPENSSL_free(tree->levels);
581         OPENSSL_free(tree);
582
583         }
584
585 /* Application policy checking function.
586  * Return codes:
587  *  0   Internal Error.
588  *  1   Successful.
589  * -1   One or more certificates contain invalid or inconsistent extensions
590  * -2   User constrained policy set empty and requireExplicit true.
591  */
592
593 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
594                         STACK_OF(X509) *certs,
595                         STACK_OF(ASN1_OBJECT) *policy_oids,
596                         unsigned int flags)
597         {
598         int ret;
599         X509_POLICY_TREE *tree = NULL;
600         STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
601         *ptree = NULL;
602
603         *pexplicit_policy = 0;
604         ret = tree_init(&tree, certs, flags);
605
606
607         switch (ret)
608                 {
609
610                 /* Tree empty requireExplicit False: OK */
611                 case 2:
612                 return 1;
613
614                 /* Some internal error */
615                 case 0:
616                 return 0;
617
618                 /* Tree empty requireExplicit True: Error */
619
620                 case 6:
621                 *pexplicit_policy = 1;
622                 return -2;
623
624                 /* Tree OK requireExplicit True: OK and continue */
625                 case 5:
626                 *pexplicit_policy = 1;
627                 break;
628
629                 /* Tree OK: continue */
630
631                 case 1:
632                 if (!tree)
633                         /*
634                          * tree_init() returns success and a null tree
635                          * if it's just looking at a trust anchor.
636                          * I'm not sure that returning success here is
637                          * correct, but I'm sure that reporting this
638                          * as an internal error which our caller
639                          * interprets as a malloc failure is wrong.
640                          */
641                         return 1;
642                 break;
643                 }
644
645         if (!tree) goto error;
646         ret = tree_evaluate(tree);
647
648         if (ret <= 0)
649                 goto error;
650
651         /* Return value 2 means tree empty */
652         if (ret == 2)
653                 {
654                 X509_policy_tree_free(tree);
655                 if (*pexplicit_policy)
656                         return -2;
657                 else
658                         return 1;
659                 }
660
661         /* Tree is not empty: continue */
662
663         ret = tree_calculate_authority_set(tree, &auth_nodes);
664
665         if (!ret)
666                 goto error;
667
668         if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
669                 goto error;
670         
671         if (ret == 2)
672                 sk_X509_POLICY_NODE_free(auth_nodes);
673
674         if (tree)
675                 *ptree = tree;
676
677         if (*pexplicit_policy)
678                 {
679                 nodes = X509_policy_tree_get0_user_policies(tree);
680                 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
681                         return -2;
682                 }
683
684         return 1;
685
686         error:
687
688         X509_policy_tree_free(tree);
689
690         return 0;
691
692         }
693