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