Fix declarations and constification for inline stack.
[openssl.git] / crypto / x509v3 / v3_pci.c
1 /* v3_pci.c */
2 /*
3  * Contributed to the OpenSSL Project 2004 by Richard Levitte
4  * (richard@levitte.org)
5  */
6 /* Copyright (c) 2004 Kungliga Tekniska Högskolan
7  * (Royal Institute of Technology, Stockholm, Sweden).
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * 3. Neither the name of the Institute nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #include <stdio.h>
39 #include "internal/cryptlib.h"
40 #include <openssl/conf.h>
41 #include <openssl/x509v3.h>
42 #include "ext_dat.h"
43
44 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
45                    BIO *out, int indent);
46 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
47                                           X509V3_CTX *ctx, char *str);
48
49 const X509V3_EXT_METHOD v3_pci =
50     { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
51     0, 0, 0, 0,
52     0, 0,
53     NULL, NULL,
54     (X509V3_EXT_I2R)i2r_pci,
55     (X509V3_EXT_R2I)r2i_pci,
56     NULL,
57 };
58
59 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
60                    BIO *out, int indent)
61 {
62     BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
63     if (pci->pcPathLengthConstraint)
64         i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
65     else
66         BIO_printf(out, "infinite");
67     BIO_puts(out, "\n");
68     BIO_printf(out, "%*sPolicy Language: ", indent, "");
69     i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
70     BIO_puts(out, "\n");
71     if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
72         BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
73                    pci->proxyPolicy->policy->data);
74     return 1;
75 }
76
77 static int process_pci_value(CONF_VALUE *val,
78                              ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
79                              ASN1_OCTET_STRING **policy)
80 {
81     int free_policy = 0;
82
83     if (strcmp(val->name, "language") == 0) {
84         if (*language) {
85             X509V3err(X509V3_F_PROCESS_PCI_VALUE,
86                       X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
87             X509V3_conf_err(val);
88             return 0;
89         }
90         if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
91             X509V3err(X509V3_F_PROCESS_PCI_VALUE,
92                       X509V3_R_INVALID_OBJECT_IDENTIFIER);
93             X509V3_conf_err(val);
94             return 0;
95         }
96     } else if (strcmp(val->name, "pathlen") == 0) {
97         if (*pathlen) {
98             X509V3err(X509V3_F_PROCESS_PCI_VALUE,
99                       X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
100             X509V3_conf_err(val);
101             return 0;
102         }
103         if (!X509V3_get_value_int(val, pathlen)) {
104             X509V3err(X509V3_F_PROCESS_PCI_VALUE,
105                       X509V3_R_POLICY_PATH_LENGTH);
106             X509V3_conf_err(val);
107             return 0;
108         }
109     } else if (strcmp(val->name, "policy") == 0) {
110         unsigned char *tmp_data = NULL;
111         long val_len;
112         if (!*policy) {
113             *policy = ASN1_OCTET_STRING_new();
114             if (*policy == NULL) {
115                 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
116                 X509V3_conf_err(val);
117                 return 0;
118             }
119             free_policy = 1;
120         }
121         if (strncmp(val->value, "hex:", 4) == 0) {
122             unsigned char *tmp_data2 =
123                 string_to_hex(val->value + 4, &val_len);
124
125             if (!tmp_data2) {
126                 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
127                           X509V3_R_ILLEGAL_HEX_DIGIT);
128                 X509V3_conf_err(val);
129                 goto err;
130             }
131
132             tmp_data = OPENSSL_realloc((*policy)->data,
133                                        (*policy)->length + val_len + 1);
134             if (tmp_data) {
135                 (*policy)->data = tmp_data;
136                 memcpy(&(*policy)->data[(*policy)->length],
137                        tmp_data2, val_len);
138                 (*policy)->length += val_len;
139                 (*policy)->data[(*policy)->length] = '\0';
140             } else {
141                 OPENSSL_free(tmp_data2);
142                 /*
143                  * realloc failure implies the original data space is b0rked
144                  * too!
145                  */
146                 OPENSSL_free((*policy)->data);
147                 (*policy)->data = NULL;
148                 (*policy)->length = 0;
149                 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
150                 X509V3_conf_err(val);
151                 goto err;
152             }
153             OPENSSL_free(tmp_data2);
154         } else if (strncmp(val->value, "file:", 5) == 0) {
155             unsigned char buf[2048];
156             int n;
157             BIO *b = BIO_new_file(val->value + 5, "r");
158             if (!b) {
159                 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
160                 X509V3_conf_err(val);
161                 goto err;
162             }
163             while ((n = BIO_read(b, buf, sizeof(buf))) > 0
164                    || (n == 0 && BIO_should_retry(b))) {
165                 if (!n)
166                     continue;
167
168                 tmp_data = OPENSSL_realloc((*policy)->data,
169                                            (*policy)->length + n + 1);
170
171                 if (!tmp_data) {
172                     OPENSSL_free((*policy)->data);
173                     (*policy)->data = NULL;
174                     (*policy)->length = 0;
175                     X509V3err(X509V3_F_PROCESS_PCI_VALUE,
176                               ERR_R_MALLOC_FAILURE);
177                     X509V3_conf_err(val);
178                     BIO_free_all(b);
179                     goto err;
180                 }
181
182                 (*policy)->data = tmp_data;
183                 memcpy(&(*policy)->data[(*policy)->length], buf, n);
184                 (*policy)->length += n;
185                 (*policy)->data[(*policy)->length] = '\0';
186             }
187             BIO_free_all(b);
188
189             if (n < 0) {
190                 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
191                 X509V3_conf_err(val);
192                 goto err;
193             }
194         } else if (strncmp(val->value, "text:", 5) == 0) {
195             val_len = strlen(val->value + 5);
196             tmp_data = OPENSSL_realloc((*policy)->data,
197                                        (*policy)->length + val_len + 1);
198             if (tmp_data) {
199                 (*policy)->data = tmp_data;
200                 memcpy(&(*policy)->data[(*policy)->length],
201                        val->value + 5, val_len);
202                 (*policy)->length += val_len;
203                 (*policy)->data[(*policy)->length] = '\0';
204             } else {
205                 /*
206                  * realloc failure implies the original data space is b0rked
207                  * too!
208                  */
209                 OPENSSL_free((*policy)->data);
210                 (*policy)->data = NULL;
211                 (*policy)->length = 0;
212                 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
213                 X509V3_conf_err(val);
214                 goto err;
215             }
216         } else {
217             X509V3err(X509V3_F_PROCESS_PCI_VALUE,
218                       X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
219             X509V3_conf_err(val);
220             goto err;
221         }
222         if (!tmp_data) {
223             X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
224             X509V3_conf_err(val);
225             goto err;
226         }
227     }
228     return 1;
229  err:
230     if (free_policy) {
231         ASN1_OCTET_STRING_free(*policy);
232         *policy = NULL;
233     }
234     return 0;
235 }
236
237 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
238                                           X509V3_CTX *ctx, char *value)
239 {
240     PROXY_CERT_INFO_EXTENSION *pci = NULL;
241     STACK_OF(CONF_VALUE) *vals;
242     ASN1_OBJECT *language = NULL;
243     ASN1_INTEGER *pathlen = NULL;
244     ASN1_OCTET_STRING *policy = NULL;
245     int i, j;
246
247     vals = X509V3_parse_list(value);
248     for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
249         CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
250         if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
251             X509V3err(X509V3_F_R2I_PCI,
252                       X509V3_R_INVALID_PROXY_POLICY_SETTING);
253             X509V3_conf_err(cnf);
254             goto err;
255         }
256         if (*cnf->name == '@') {
257             STACK_OF(CONF_VALUE) *sect;
258             int success_p = 1;
259
260             sect = X509V3_get_section(ctx, cnf->name + 1);
261             if (!sect) {
262                 X509V3err(X509V3_F_R2I_PCI, X509V3_R_INVALID_SECTION);
263                 X509V3_conf_err(cnf);
264                 goto err;
265             }
266             for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
267                 success_p =
268                     process_pci_value(sk_CONF_VALUE_value(sect, j),
269                                       &language, &pathlen, &policy);
270             }
271             X509V3_section_free(ctx, sect);
272             if (!success_p)
273                 goto err;
274         } else {
275             if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
276                 X509V3_conf_err(cnf);
277                 goto err;
278             }
279         }
280     }
281
282     /* Language is mandatory */
283     if (!language) {
284         X509V3err(X509V3_F_R2I_PCI,
285                   X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
286         goto err;
287     }
288     i = OBJ_obj2nid(language);
289     if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
290         X509V3err(X509V3_F_R2I_PCI,
291                   X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
292         goto err;
293     }
294
295     pci = PROXY_CERT_INFO_EXTENSION_new();
296     if (pci == NULL) {
297         X509V3err(X509V3_F_R2I_PCI, ERR_R_MALLOC_FAILURE);
298         goto err;
299     }
300
301     pci->proxyPolicy->policyLanguage = language;
302     language = NULL;
303     pci->proxyPolicy->policy = policy;
304     policy = NULL;
305     pci->pcPathLengthConstraint = pathlen;
306     pathlen = NULL;
307     goto end;
308  err:
309     ASN1_OBJECT_free(language);
310     ASN1_INTEGER_free(pathlen);
311     pathlen = NULL;
312     ASN1_OCTET_STRING_free(policy);
313     policy = NULL;
314     PROXY_CERT_INFO_EXTENSION_free(pci);
315     pci = NULL;
316  end:
317     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
318     return pci;
319 }