2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 /* Copyright (c) 2004 Kungliga Tekniska Högskolan
11 * (Royal Institute of Technology, Stockholm, Sweden).
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the Institute nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include "internal/cryptlib.h"
44 #include <openssl/conf.h>
45 #include <openssl/x509v3.h>
48 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
49 BIO *out, int indent);
50 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
51 X509V3_CTX *ctx, char *str);
53 const X509V3_EXT_METHOD v3_pci =
54 { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
58 (X509V3_EXT_I2R)i2r_pci,
59 (X509V3_EXT_R2I)r2i_pci,
63 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
66 BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
67 if (pci->pcPathLengthConstraint)
68 i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
70 BIO_printf(out, "infinite");
72 BIO_printf(out, "%*sPolicy Language: ", indent, "");
73 i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
75 if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
76 BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
77 pci->proxyPolicy->policy->data);
81 static int process_pci_value(CONF_VALUE *val,
82 ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
83 ASN1_OCTET_STRING **policy)
87 if (strcmp(val->name, "language") == 0) {
89 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
90 X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
94 if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
95 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
96 X509V3_R_INVALID_OBJECT_IDENTIFIER);
100 } else if (strcmp(val->name, "pathlen") == 0) {
102 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
103 X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
104 X509V3_conf_err(val);
107 if (!X509V3_get_value_int(val, pathlen)) {
108 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
109 X509V3_R_POLICY_PATH_LENGTH);
110 X509V3_conf_err(val);
113 } else if (strcmp(val->name, "policy") == 0) {
114 unsigned char *tmp_data = NULL;
117 *policy = ASN1_OCTET_STRING_new();
118 if (*policy == NULL) {
119 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
120 X509V3_conf_err(val);
125 if (strncmp(val->value, "hex:", 4) == 0) {
126 unsigned char *tmp_data2 =
127 OPENSSL_hexstr2buf(val->value + 4, &val_len);
130 X509V3_conf_err(val);
134 tmp_data = OPENSSL_realloc((*policy)->data,
135 (*policy)->length + val_len + 1);
137 (*policy)->data = tmp_data;
138 memcpy(&(*policy)->data[(*policy)->length],
140 (*policy)->length += val_len;
141 (*policy)->data[(*policy)->length] = '\0';
143 OPENSSL_free(tmp_data2);
145 * realloc failure implies the original data space is b0rked
148 OPENSSL_free((*policy)->data);
149 (*policy)->data = NULL;
150 (*policy)->length = 0;
151 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
152 X509V3_conf_err(val);
155 OPENSSL_free(tmp_data2);
156 } else if (strncmp(val->value, "file:", 5) == 0) {
157 unsigned char buf[2048];
159 BIO *b = BIO_new_file(val->value + 5, "r");
161 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
162 X509V3_conf_err(val);
165 while ((n = BIO_read(b, buf, sizeof(buf))) > 0
166 || (n == 0 && BIO_should_retry(b))) {
170 tmp_data = OPENSSL_realloc((*policy)->data,
171 (*policy)->length + n + 1);
174 OPENSSL_free((*policy)->data);
175 (*policy)->data = NULL;
176 (*policy)->length = 0;
177 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
178 ERR_R_MALLOC_FAILURE);
179 X509V3_conf_err(val);
184 (*policy)->data = tmp_data;
185 memcpy(&(*policy)->data[(*policy)->length], buf, n);
186 (*policy)->length += n;
187 (*policy)->data[(*policy)->length] = '\0';
192 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
193 X509V3_conf_err(val);
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);
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';
208 * realloc failure implies the original data space is b0rked
211 OPENSSL_free((*policy)->data);
212 (*policy)->data = NULL;
213 (*policy)->length = 0;
214 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
215 X509V3_conf_err(val);
219 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
220 X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
221 X509V3_conf_err(val);
225 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
226 X509V3_conf_err(val);
233 ASN1_OCTET_STRING_free(*policy);
239 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
240 X509V3_CTX *ctx, char *value)
242 PROXY_CERT_INFO_EXTENSION *pci = NULL;
243 STACK_OF(CONF_VALUE) *vals;
244 ASN1_OBJECT *language = NULL;
245 ASN1_INTEGER *pathlen = NULL;
246 ASN1_OCTET_STRING *policy = NULL;
249 vals = X509V3_parse_list(value);
250 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
251 CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
252 if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
253 X509V3err(X509V3_F_R2I_PCI,
254 X509V3_R_INVALID_PROXY_POLICY_SETTING);
255 X509V3_conf_err(cnf);
258 if (*cnf->name == '@') {
259 STACK_OF(CONF_VALUE) *sect;
262 sect = X509V3_get_section(ctx, cnf->name + 1);
264 X509V3err(X509V3_F_R2I_PCI, X509V3_R_INVALID_SECTION);
265 X509V3_conf_err(cnf);
268 for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
270 process_pci_value(sk_CONF_VALUE_value(sect, j),
271 &language, &pathlen, &policy);
273 X509V3_section_free(ctx, sect);
277 if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
278 X509V3_conf_err(cnf);
284 /* Language is mandatory */
286 X509V3err(X509V3_F_R2I_PCI,
287 X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
290 i = OBJ_obj2nid(language);
291 if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
292 X509V3err(X509V3_F_R2I_PCI,
293 X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
297 pci = PROXY_CERT_INFO_EXTENSION_new();
299 X509V3err(X509V3_F_R2I_PCI, ERR_R_MALLOC_FAILURE);
303 pci->proxyPolicy->policyLanguage = language;
305 pci->proxyPolicy->policy = policy;
307 pci->pcPathLengthConstraint = pathlen;
311 ASN1_OBJECT_free(language);
312 ASN1_INTEGER_free(pathlen);
314 ASN1_OCTET_STRING_free(policy);
316 PROXY_CERT_INFO_EXTENSION_free(pci);
319 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);