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