CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / x509 / pcy_map.c
1 /*
2  * Copyright 2004-2020 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 "internal/cryptlib.h"
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
13 #include "crypto/x509.h"
14
15 #include "pcy_local.h"
16
17 DEFINE_STACK_OF(POLICY_MAPPING)
18 DEFINE_STACK_OF(ASN1_OBJECT)
19
20 /*
21  * Set policy mapping entries in cache. Note: this modifies the passed
22  * POLICY_MAPPINGS structure
23  */
24
25 int policy_cache_set_mapping(X509 *x, POLICY_MAPPINGS *maps)
26 {
27     POLICY_MAPPING *map;
28     X509_POLICY_DATA *data;
29     X509_POLICY_CACHE *cache = x->policy_cache;
30     int i;
31     int ret = 0;
32     if (sk_POLICY_MAPPING_num(maps) == 0) {
33         ret = -1;
34         goto bad_mapping;
35     }
36     for (i = 0; i < sk_POLICY_MAPPING_num(maps); i++) {
37         map = sk_POLICY_MAPPING_value(maps, i);
38         /* Reject if map to or from anyPolicy */
39         if ((OBJ_obj2nid(map->subjectDomainPolicy) == NID_any_policy)
40             || (OBJ_obj2nid(map->issuerDomainPolicy) == NID_any_policy)) {
41             ret = -1;
42             goto bad_mapping;
43         }
44
45         /* Attempt to find matching policy data */
46         data = policy_cache_find_data(cache, map->issuerDomainPolicy);
47         /* If we don't have anyPolicy can't map */
48         if (data == NULL && !cache->anyPolicy)
49             continue;
50
51         /* Create a NODE from anyPolicy */
52         if (data == NULL) {
53             data = policy_data_new(NULL, map->issuerDomainPolicy,
54                                    cache->anyPolicy->flags
55                                    & POLICY_DATA_FLAG_CRITICAL);
56             if (data == NULL)
57                 goto bad_mapping;
58             data->qualifier_set = cache->anyPolicy->qualifier_set;
59             /*
60              * map->issuerDomainPolicy = NULL;
61              */
62             data->flags |= POLICY_DATA_FLAG_MAPPED_ANY;
63             data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
64             if (!sk_X509_POLICY_DATA_push(cache->data, data)) {
65                 policy_data_free(data);
66                 goto bad_mapping;
67             }
68         } else
69             data->flags |= POLICY_DATA_FLAG_MAPPED;
70         if (!sk_ASN1_OBJECT_push(data->expected_policy_set,
71                                  map->subjectDomainPolicy))
72             goto bad_mapping;
73         map->subjectDomainPolicy = NULL;
74
75     }
76
77     ret = 1;
78  bad_mapping:
79     if (ret == -1)
80         x->ex_flags |= EXFLAG_INVALID_POLICY;
81     sk_POLICY_MAPPING_pop_free(maps, POLICY_MAPPING_free);
82     return ret;
83
84 }