/src/openssl31/crypto/x509/v3_pci.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
67 | | static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci, |
68 | | BIO *out, int indent) |
69 | 6.36k | { |
70 | 6.36k | BIO_printf(out, "%*sPath Length Constraint: ", indent, ""); |
71 | 6.36k | if (pci->pcPathLengthConstraint) |
72 | 4.44k | i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint); |
73 | 1.92k | else |
74 | 1.92k | BIO_printf(out, "infinite"); |
75 | 6.36k | BIO_puts(out, "\n"); |
76 | 6.36k | BIO_printf(out, "%*sPolicy Language: ", indent, ""); |
77 | 6.36k | i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage); |
78 | 6.36k | if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data) |
79 | 1.23k | BIO_printf(out, "\n%*sPolicy Text: %.*s", indent, "", |
80 | 1.23k | pci->proxyPolicy->policy->length, |
81 | 1.23k | pci->proxyPolicy->policy->data); |
82 | 6.36k | return 1; |
83 | 6.36k | } |
84 | | |
85 | | static int process_pci_value(CONF_VALUE *val, |
86 | | ASN1_OBJECT **language, ASN1_INTEGER **pathlen, |
87 | | ASN1_OCTET_STRING **policy) |
88 | 0 | { |
89 | 0 | int free_policy = 0; |
90 | |
|
91 | 0 | if (strcmp(val->name, "language") == 0) { |
92 | 0 | if (*language) { |
93 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED); |
94 | 0 | X509V3_conf_err(val); |
95 | 0 | return 0; |
96 | 0 | } |
97 | 0 | if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) { |
98 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER); |
99 | 0 | X509V3_conf_err(val); |
100 | 0 | return 0; |
101 | 0 | } |
102 | 0 | } else if (strcmp(val->name, "pathlen") == 0) { |
103 | 0 | if (*pathlen) { |
104 | 0 | ERR_raise(ERR_LIB_X509V3, |
105 | 0 | X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED); |
106 | 0 | X509V3_conf_err(val); |
107 | 0 | return 0; |
108 | 0 | } |
109 | 0 | if (!X509V3_get_value_int(val, pathlen)) { |
110 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_PATH_LENGTH); |
111 | 0 | X509V3_conf_err(val); |
112 | 0 | return 0; |
113 | 0 | } |
114 | 0 | } else if (strcmp(val->name, "policy") == 0) { |
115 | 0 | unsigned char *tmp_data = NULL; |
116 | 0 | long val_len; |
117 | |
|
118 | 0 | if (*policy == NULL) { |
119 | 0 | *policy = ASN1_OCTET_STRING_new(); |
120 | 0 | if (*policy == NULL) { |
121 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
122 | 0 | X509V3_conf_err(val); |
123 | 0 | return 0; |
124 | 0 | } |
125 | 0 | free_policy = 1; |
126 | 0 | } |
127 | 0 | if (strncmp(val->value, "hex:", 4) == 0) { |
128 | 0 | unsigned char *tmp_data2 = |
129 | 0 | OPENSSL_hexstr2buf(val->value + 4, &val_len); |
130 | |
|
131 | 0 | if (!tmp_data2) { |
132 | 0 | X509V3_conf_err(val); |
133 | 0 | goto err; |
134 | 0 | } |
135 | | |
136 | 0 | tmp_data = OPENSSL_realloc((*policy)->data, |
137 | 0 | (*policy)->length + val_len + 1); |
138 | 0 | if (tmp_data) { |
139 | 0 | (*policy)->data = tmp_data; |
140 | 0 | memcpy(&(*policy)->data[(*policy)->length], |
141 | 0 | tmp_data2, val_len); |
142 | 0 | (*policy)->length += val_len; |
143 | 0 | (*policy)->data[(*policy)->length] = '\0'; |
144 | 0 | } else { |
145 | 0 | OPENSSL_free(tmp_data2); |
146 | | /* |
147 | | * realloc failure implies the original data space is b0rked |
148 | | * too! |
149 | | */ |
150 | 0 | OPENSSL_free((*policy)->data); |
151 | 0 | (*policy)->data = NULL; |
152 | 0 | (*policy)->length = 0; |
153 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
154 | 0 | X509V3_conf_err(val); |
155 | 0 | goto err; |
156 | 0 | } |
157 | 0 | OPENSSL_free(tmp_data2); |
158 | 0 | } else if (strncmp(val->value, "file:", 5) == 0) { |
159 | 0 | unsigned char buf[2048]; |
160 | 0 | int n; |
161 | 0 | BIO *b = BIO_new_file(val->value + 5, "r"); |
162 | 0 | if (!b) { |
163 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB); |
164 | 0 | X509V3_conf_err(val); |
165 | 0 | goto err; |
166 | 0 | } |
167 | 0 | while ((n = BIO_read(b, buf, sizeof(buf))) > 0 |
168 | 0 | || (n == 0 && BIO_should_retry(b))) { |
169 | 0 | if (!n) |
170 | 0 | continue; |
171 | | |
172 | 0 | tmp_data = OPENSSL_realloc((*policy)->data, |
173 | 0 | (*policy)->length + n + 1); |
174 | |
|
175 | 0 | if (!tmp_data) { |
176 | 0 | OPENSSL_free((*policy)->data); |
177 | 0 | (*policy)->data = NULL; |
178 | 0 | (*policy)->length = 0; |
179 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
180 | 0 | X509V3_conf_err(val); |
181 | 0 | BIO_free_all(b); |
182 | 0 | goto err; |
183 | 0 | } |
184 | | |
185 | 0 | (*policy)->data = tmp_data; |
186 | 0 | memcpy(&(*policy)->data[(*policy)->length], buf, n); |
187 | 0 | (*policy)->length += n; |
188 | 0 | (*policy)->data[(*policy)->length] = '\0'; |
189 | 0 | } |
190 | 0 | BIO_free_all(b); |
191 | |
|
192 | 0 | if (n < 0) { |
193 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB); |
194 | 0 | X509V3_conf_err(val); |
195 | 0 | goto err; |
196 | 0 | } |
197 | 0 | } else if (strncmp(val->value, "text:", 5) == 0) { |
198 | 0 | val_len = strlen(val->value + 5); |
199 | 0 | tmp_data = OPENSSL_realloc((*policy)->data, |
200 | 0 | (*policy)->length + val_len + 1); |
201 | 0 | if (tmp_data) { |
202 | 0 | (*policy)->data = tmp_data; |
203 | 0 | memcpy(&(*policy)->data[(*policy)->length], |
204 | 0 | val->value + 5, val_len); |
205 | 0 | (*policy)->length += val_len; |
206 | 0 | (*policy)->data[(*policy)->length] = '\0'; |
207 | 0 | } else { |
208 | | /* |
209 | | * realloc failure implies the original data space is b0rked |
210 | | * too! |
211 | | */ |
212 | 0 | OPENSSL_free((*policy)->data); |
213 | 0 | (*policy)->data = NULL; |
214 | 0 | (*policy)->length = 0; |
215 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
216 | 0 | X509V3_conf_err(val); |
217 | 0 | goto err; |
218 | 0 | } |
219 | 0 | } else { |
220 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG); |
221 | 0 | X509V3_conf_err(val); |
222 | 0 | goto err; |
223 | 0 | } |
224 | 0 | if (!tmp_data) { |
225 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
226 | 0 | X509V3_conf_err(val); |
227 | 0 | goto err; |
228 | 0 | } |
229 | 0 | } |
230 | 0 | return 1; |
231 | 0 | err: |
232 | 0 | if (free_policy) { |
233 | 0 | ASN1_OCTET_STRING_free(*policy); |
234 | 0 | *policy = NULL; |
235 | 0 | } |
236 | 0 | return 0; |
237 | 0 | } |
238 | | |
239 | | static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method, |
240 | | X509V3_CTX *ctx, char *value) |
241 | 0 | { |
242 | 0 | PROXY_CERT_INFO_EXTENSION *pci = NULL; |
243 | 0 | STACK_OF(CONF_VALUE) *vals; |
244 | 0 | ASN1_OBJECT *language = NULL; |
245 | 0 | ASN1_INTEGER *pathlen = NULL; |
246 | 0 | ASN1_OCTET_STRING *policy = NULL; |
247 | 0 | int i, j; |
248 | |
|
249 | 0 | vals = X509V3_parse_list(value); |
250 | 0 | for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { |
251 | 0 | CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i); |
252 | |
|
253 | 0 | if (!cnf->name || (*cnf->name != '@' && !cnf->value)) { |
254 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PROXY_POLICY_SETTING); |
255 | 0 | X509V3_conf_err(cnf); |
256 | 0 | goto err; |
257 | 0 | } |
258 | 0 | if (*cnf->name == '@') { |
259 | 0 | STACK_OF(CONF_VALUE) *sect; |
260 | 0 | int success_p = 1; |
261 | |
|
262 | 0 | sect = X509V3_get_section(ctx, cnf->name + 1); |
263 | 0 | if (!sect) { |
264 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION); |
265 | 0 | X509V3_conf_err(cnf); |
266 | 0 | goto err; |
267 | 0 | } |
268 | 0 | for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) { |
269 | 0 | success_p = |
270 | 0 | process_pci_value(sk_CONF_VALUE_value(sect, j), |
271 | 0 | &language, &pathlen, &policy); |
272 | 0 | } |
273 | 0 | X509V3_section_free(ctx, sect); |
274 | 0 | if (!success_p) |
275 | 0 | goto err; |
276 | 0 | } else { |
277 | 0 | if (!process_pci_value(cnf, &language, &pathlen, &policy)) { |
278 | 0 | X509V3_conf_err(cnf); |
279 | 0 | goto err; |
280 | 0 | } |
281 | 0 | } |
282 | 0 | } |
283 | | |
284 | | /* Language is mandatory */ |
285 | 0 | if (!language) { |
286 | 0 | ERR_raise(ERR_LIB_X509V3, |
287 | 0 | X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED); |
288 | 0 | goto err; |
289 | 0 | } |
290 | 0 | i = OBJ_obj2nid(language); |
291 | 0 | if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) { |
292 | 0 | ERR_raise(ERR_LIB_X509V3, |
293 | 0 | X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY); |
294 | 0 | goto err; |
295 | 0 | } |
296 | | |
297 | 0 | pci = PROXY_CERT_INFO_EXTENSION_new(); |
298 | 0 | if (pci == NULL) { |
299 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
300 | 0 | goto err; |
301 | 0 | } |
302 | | |
303 | 0 | pci->proxyPolicy->policyLanguage = language; |
304 | 0 | language = NULL; |
305 | 0 | pci->proxyPolicy->policy = policy; |
306 | 0 | policy = NULL; |
307 | 0 | pci->pcPathLengthConstraint = pathlen; |
308 | 0 | pathlen = NULL; |
309 | 0 | goto end; |
310 | 0 | err: |
311 | 0 | ASN1_OBJECT_free(language); |
312 | 0 | ASN1_INTEGER_free(pathlen); |
313 | 0 | pathlen = NULL; |
314 | 0 | ASN1_OCTET_STRING_free(policy); |
315 | 0 | policy = NULL; |
316 | 0 | PROXY_CERT_INFO_EXTENSION_free(pci); |
317 | 0 | pci = NULL; |
318 | 0 | end: |
319 | 0 | sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); |
320 | 0 | return pci; |
321 | 0 | } |