Use more pre-allocation
[openssl.git] / crypto / x509v3 / v3_pmaps.c
1 /*
2  * Copyright 2003-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <openssl/asn1t.h>
13 #include <openssl/conf.h>
14 #include <openssl/x509v3.h>
15 #include "ext_dat.h"
16
17 static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
18                                  X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
19 static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD
20                                                  *method, void *pmps, STACK_OF(CONF_VALUE)
21                                                  *extlist);
22
23 const X509V3_EXT_METHOD v3_policy_mappings = {
24     NID_policy_mappings, 0,
25     ASN1_ITEM_ref(POLICY_MAPPINGS),
26     0, 0, 0, 0,
27     0, 0,
28     i2v_POLICY_MAPPINGS,
29     v2i_POLICY_MAPPINGS,
30     0, 0,
31     NULL
32 };
33
34 ASN1_SEQUENCE(POLICY_MAPPING) = {
35         ASN1_SIMPLE(POLICY_MAPPING, issuerDomainPolicy, ASN1_OBJECT),
36         ASN1_SIMPLE(POLICY_MAPPING, subjectDomainPolicy, ASN1_OBJECT)
37 } ASN1_SEQUENCE_END(POLICY_MAPPING)
38
39 ASN1_ITEM_TEMPLATE(POLICY_MAPPINGS) =
40         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, POLICY_MAPPINGS,
41                                                                 POLICY_MAPPING)
42 ASN1_ITEM_TEMPLATE_END(POLICY_MAPPINGS)
43
44 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING)
45
46 static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD
47                                                  *method, void *a, STACK_OF(CONF_VALUE)
48                                                  *ext_list)
49 {
50     POLICY_MAPPINGS *pmaps = a;
51     POLICY_MAPPING *pmap;
52     int i;
53     char obj_tmp1[80];
54     char obj_tmp2[80];
55
56     for (i = 0; i < sk_POLICY_MAPPING_num(pmaps); i++) {
57         pmap = sk_POLICY_MAPPING_value(pmaps, i);
58         i2t_ASN1_OBJECT(obj_tmp1, 80, pmap->issuerDomainPolicy);
59         i2t_ASN1_OBJECT(obj_tmp2, 80, pmap->subjectDomainPolicy);
60         X509V3_add_value(obj_tmp1, obj_tmp2, &ext_list);
61     }
62     return ext_list;
63 }
64
65 static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
66                                  X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
67 {
68     POLICY_MAPPING *pmap = NULL;
69     ASN1_OBJECT *obj1 = NULL, *obj2 = NULL;
70     CONF_VALUE *val;
71     POLICY_MAPPINGS *pmaps;
72     const int num = sk_CONF_VALUE_num(nval);
73     int i;
74
75     if ((pmaps = sk_POLICY_MAPPING_new_null()) == NULL) {
76         X509V3err(X509V3_F_V2I_POLICY_MAPPINGS, ERR_R_MALLOC_FAILURE);
77         return NULL;
78     }
79     if (!sk_POLICY_MAPPING_reserve(pmaps, num))
80         goto err;
81
82     for (i = 0; i < num; i++) {
83         val = sk_CONF_VALUE_value(nval, i);
84         if (!val->value || !val->name) {
85             X509V3err(X509V3_F_V2I_POLICY_MAPPINGS,
86                       X509V3_R_INVALID_OBJECT_IDENTIFIER);
87             X509V3_conf_err(val);
88             goto err;
89         }
90         obj1 = OBJ_txt2obj(val->name, 0);
91         obj2 = OBJ_txt2obj(val->value, 0);
92         if (!obj1 || !obj2) {
93             X509V3err(X509V3_F_V2I_POLICY_MAPPINGS,
94                       X509V3_R_INVALID_OBJECT_IDENTIFIER);
95             X509V3_conf_err(val);
96             goto err;
97         }
98         pmap = POLICY_MAPPING_new();
99         if (pmap == NULL) {
100             X509V3err(X509V3_F_V2I_POLICY_MAPPINGS, ERR_R_MALLOC_FAILURE);
101             goto err;
102         }
103         pmap->issuerDomainPolicy = obj1;
104         pmap->subjectDomainPolicy = obj2;
105         obj1 = obj2 = NULL;
106         sk_POLICY_MAPPING_push(pmaps, pmap); /* no failure as it was reserved */
107     }
108     return pmaps;
109  err:
110     ASN1_OBJECT_free(obj1);
111     ASN1_OBJECT_free(obj2);
112     sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free);
113     return NULL;
114 }