]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/v3_pci.c
Update copyright year
[thirdparty/openssl.git] / crypto / x509 / v3_pci.c
CommitLineData
0f113f3e 1/*
3c2bdd7d 2 * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
d2e9e320 3 *
4286ca47 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
6951c23a 8 */
d2e9e320 9
0c3d0d4a
RS
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
6951c23a
RL
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>
b39fc560 47#include "internal/cryptlib.h"
6951c23a
RL
48#include <openssl/conf.h>
49#include <openssl/x509v3.h>
df2ee0e2 50#include "ext_dat.h"
6951c23a
RL
51
52static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
0f113f3e 53 BIO *out, int indent);
6951c23a 54static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
0f113f3e 55 X509V3_CTX *ctx, char *str);
6951c23a 56
47864aea 57const X509V3_EXT_METHOD ossl_v3_pci =
0f113f3e
MC
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};
6951c23a
RL
66
67static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
0f113f3e
MC
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);
0f113f3e 78 if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
a4c467c9 79 BIO_printf(out, "\n%*sPolicy Text: %s", indent, "",
0f113f3e
MC
80 pci->proxyPolicy->policy->data);
81 return 1;
82}
6951c23a
RL
83
84static int process_pci_value(CONF_VALUE *val,
0f113f3e
MC
85 ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
86 ASN1_OCTET_STRING **policy)
87{
88 int free_policy = 0;
6951c23a 89
0f113f3e
MC
90 if (strcmp(val->name, "language") == 0) {
91 if (*language) {
9311d0c4 92 ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
0f113f3e
MC
93 X509V3_conf_err(val);
94 return 0;
95 }
75ebbd9a 96 if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
9311d0c4 97 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
0f113f3e
MC
98 X509V3_conf_err(val);
99 return 0;
100 }
101 } else if (strcmp(val->name, "pathlen") == 0) {
102 if (*pathlen) {
9311d0c4 103 ERR_raise(ERR_LIB_X509V3,
0f113f3e
MC
104 X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
105 X509V3_conf_err(val);
106 return 0;
107 }
108 if (!X509V3_get_value_int(val, pathlen)) {
9311d0c4 109 ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_PATH_LENGTH);
0f113f3e
MC
110 X509V3_conf_err(val);
111 return 0;
112 }
113 } else if (strcmp(val->name, "policy") == 0) {
114 unsigned char *tmp_data = NULL;
115 long val_len;
12a765a5
RS
116
117 if (*policy == NULL) {
0f113f3e 118 *policy = ASN1_OCTET_STRING_new();
90945fa3 119 if (*policy == NULL) {
9311d0c4 120 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
121 X509V3_conf_err(val);
122 return 0;
123 }
124 free_policy = 1;
125 }
126 if (strncmp(val->value, "hex:", 4) == 0) {
127 unsigned char *tmp_data2 =
14f051a0 128 OPENSSL_hexstr2buf(val->value + 4, &val_len);
6951c23a 129
0f113f3e 130 if (!tmp_data2) {
0f113f3e
MC
131 X509V3_conf_err(val);
132 goto err;
133 }
6951c23a 134
0f113f3e
MC
135 tmp_data = OPENSSL_realloc((*policy)->data,
136 (*policy)->length + val_len + 1);
137 if (tmp_data) {
138 (*policy)->data = tmp_data;
139 memcpy(&(*policy)->data[(*policy)->length],
140 tmp_data2, val_len);
141 (*policy)->length += val_len;
142 (*policy)->data[(*policy)->length] = '\0';
143 } else {
144 OPENSSL_free(tmp_data2);
145 /*
146 * realloc failure implies the original data space is b0rked
147 * too!
148 */
149 OPENSSL_free((*policy)->data);
150 (*policy)->data = NULL;
151 (*policy)->length = 0;
9311d0c4 152 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
153 X509V3_conf_err(val);
154 goto err;
155 }
156 OPENSSL_free(tmp_data2);
157 } else if (strncmp(val->value, "file:", 5) == 0) {
158 unsigned char buf[2048];
159 int n;
160 BIO *b = BIO_new_file(val->value + 5, "r");
161 if (!b) {
9311d0c4 162 ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
0f113f3e
MC
163 X509V3_conf_err(val);
164 goto err;
165 }
166 while ((n = BIO_read(b, buf, sizeof(buf))) > 0
167 || (n == 0 && BIO_should_retry(b))) {
168 if (!n)
169 continue;
6951c23a 170
0f113f3e
MC
171 tmp_data = OPENSSL_realloc((*policy)->data,
172 (*policy)->length + n + 1);
6951c23a 173
0f113f3e
MC
174 if (!tmp_data) {
175 OPENSSL_free((*policy)->data);
176 (*policy)->data = NULL;
177 (*policy)->length = 0;
9311d0c4 178 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
179 X509V3_conf_err(val);
180 BIO_free_all(b);
181 goto err;
182 }
6951c23a 183
0f113f3e
MC
184 (*policy)->data = tmp_data;
185 memcpy(&(*policy)->data[(*policy)->length], buf, n);
186 (*policy)->length += n;
187 (*policy)->data[(*policy)->length] = '\0';
188 }
189 BIO_free_all(b);
6951c23a 190
0f113f3e 191 if (n < 0) {
9311d0c4 192 ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
0f113f3e
MC
193 X509V3_conf_err(val);
194 goto err;
195 }
196 } else if (strncmp(val->value, "text:", 5) == 0) {
197 val_len = strlen(val->value + 5);
198 tmp_data = OPENSSL_realloc((*policy)->data,
199 (*policy)->length + val_len + 1);
200 if (tmp_data) {
201 (*policy)->data = tmp_data;
202 memcpy(&(*policy)->data[(*policy)->length],
203 val->value + 5, val_len);
204 (*policy)->length += val_len;
205 (*policy)->data[(*policy)->length] = '\0';
206 } else {
207 /*
208 * realloc failure implies the original data space is b0rked
209 * too!
210 */
211 OPENSSL_free((*policy)->data);
212 (*policy)->data = NULL;
213 (*policy)->length = 0;
9311d0c4 214 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
215 X509V3_conf_err(val);
216 goto err;
217 }
218 } else {
9311d0c4 219 ERR_raise(ERR_LIB_X509V3, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
0f113f3e
MC
220 X509V3_conf_err(val);
221 goto err;
222 }
223 if (!tmp_data) {
9311d0c4 224 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
225 X509V3_conf_err(val);
226 goto err;
227 }
228 }
229 return 1;
230 err:
231 if (free_policy) {
232 ASN1_OCTET_STRING_free(*policy);
233 *policy = NULL;
234 }
235 return 0;
236}
6951c23a
RL
237
238static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
0f113f3e
MC
239 X509V3_CTX *ctx, char *value)
240{
241 PROXY_CERT_INFO_EXTENSION *pci = NULL;
242 STACK_OF(CONF_VALUE) *vals;
243 ASN1_OBJECT *language = NULL;
244 ASN1_INTEGER *pathlen = NULL;
245 ASN1_OCTET_STRING *policy = NULL;
246 int i, j;
6951c23a 247
0f113f3e
MC
248 vals = X509V3_parse_list(value);
249 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
250 CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
c90c4693 251
0f113f3e 252 if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
9311d0c4 253 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PROXY_POLICY_SETTING);
0f113f3e
MC
254 X509V3_conf_err(cnf);
255 goto err;
256 }
257 if (*cnf->name == '@') {
258 STACK_OF(CONF_VALUE) *sect;
259 int success_p = 1;
6951c23a 260
0f113f3e
MC
261 sect = X509V3_get_section(ctx, cnf->name + 1);
262 if (!sect) {
9311d0c4 263 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
0f113f3e
MC
264 X509V3_conf_err(cnf);
265 goto err;
266 }
267 for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
268 success_p =
269 process_pci_value(sk_CONF_VALUE_value(sect, j),
270 &language, &pathlen, &policy);
271 }
272 X509V3_section_free(ctx, sect);
273 if (!success_p)
274 goto err;
275 } else {
276 if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
277 X509V3_conf_err(cnf);
278 goto err;
279 }
280 }
281 }
6951c23a 282
0f113f3e
MC
283 /* Language is mandatory */
284 if (!language) {
9311d0c4 285 ERR_raise(ERR_LIB_X509V3,
0f113f3e
MC
286 X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
287 goto err;
288 }
289 i = OBJ_obj2nid(language);
290 if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
9311d0c4 291 ERR_raise(ERR_LIB_X509V3,
0f113f3e
MC
292 X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
293 goto err;
294 }
6951c23a 295
0f113f3e 296 pci = PROXY_CERT_INFO_EXTENSION_new();
90945fa3 297 if (pci == NULL) {
9311d0c4 298 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
299 goto err;
300 }
6951c23a 301
0f113f3e
MC
302 pci->proxyPolicy->policyLanguage = language;
303 language = NULL;
304 pci->proxyPolicy->policy = policy;
305 policy = NULL;
306 pci->pcPathLengthConstraint = pathlen;
307 pathlen = NULL;
308 goto end;
309 err:
0dfb9398 310 ASN1_OBJECT_free(language);
2ace7450
RS
311 ASN1_INTEGER_free(pathlen);
312 pathlen = NULL;
313 ASN1_OCTET_STRING_free(policy);
314 policy = NULL;
315 PROXY_CERT_INFO_EXTENSION_free(pci);
316 pci = NULL;
0f113f3e
MC
317 end:
318 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
319 return pci;
320}