1 /* v3_pci.c -*- mode:C; c-file-style: "eay" -*- */
3 * Contributed to the OpenSSL Project 2004 by Richard Levitte
4 * (richard@levitte.org)
6 /* Copyright (c) 2004 Kungliga Tekniska Högskolan
7 * (Royal Institute of Technology, Stockholm, Sweden).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "internal/cryptlib.h"
40 #include <openssl/conf.h>
41 #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 { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
54 (X509V3_EXT_I2R)i2r_pci,
55 (X509V3_EXT_R2I)r2i_pci,
59 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
62 BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
63 if (pci->pcPathLengthConstraint)
64 i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
66 BIO_printf(out, "infinite");
68 BIO_printf(out, "%*sPolicy Language: ", indent, "");
69 i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
71 if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
72 BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
73 pci->proxyPolicy->policy->data);
77 static int process_pci_value(CONF_VALUE *val,
78 ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
79 ASN1_OCTET_STRING **policy)
83 if (strcmp(val->name, "language") == 0) {
85 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
86 X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
90 if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
91 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
92 X509V3_R_INVALID_OBJECT_IDENTIFIER);
96 } else if (strcmp(val->name, "pathlen") == 0) {
98 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
99 X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
100 X509V3_conf_err(val);
103 if (!X509V3_get_value_int(val, pathlen)) {
104 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
105 X509V3_R_POLICY_PATH_LENGTH);
106 X509V3_conf_err(val);
109 } else if (strcmp(val->name, "policy") == 0) {
110 unsigned char *tmp_data = NULL;
113 *policy = ASN1_OCTET_STRING_new();
114 if (*policy == NULL) {
115 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
116 X509V3_conf_err(val);
121 if (strncmp(val->value, "hex:", 4) == 0) {
122 unsigned char *tmp_data2 =
123 string_to_hex(val->value + 4, &val_len);
126 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
127 X509V3_R_ILLEGAL_HEX_DIGIT);
128 X509V3_conf_err(val);
132 tmp_data = OPENSSL_realloc((*policy)->data,
133 (*policy)->length + val_len + 1);
135 (*policy)->data = tmp_data;
136 memcpy(&(*policy)->data[(*policy)->length],
138 (*policy)->length += val_len;
139 (*policy)->data[(*policy)->length] = '\0';
141 OPENSSL_free(tmp_data2);
143 * realloc failure implies the original data space is b0rked
146 OPENSSL_free((*policy)->data);
147 (*policy)->data = NULL;
148 (*policy)->length = 0;
149 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
150 X509V3_conf_err(val);
153 OPENSSL_free(tmp_data2);
154 } else if (strncmp(val->value, "file:", 5) == 0) {
155 unsigned char buf[2048];
157 BIO *b = BIO_new_file(val->value + 5, "r");
159 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
160 X509V3_conf_err(val);
163 while ((n = BIO_read(b, buf, sizeof(buf))) > 0
164 || (n == 0 && BIO_should_retry(b))) {
168 tmp_data = OPENSSL_realloc((*policy)->data,
169 (*policy)->length + n + 1);
172 OPENSSL_free((*policy)->data);
173 (*policy)->data = NULL;
174 (*policy)->length = 0;
175 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
176 ERR_R_MALLOC_FAILURE);
177 X509V3_conf_err(val);
182 (*policy)->data = tmp_data;
183 memcpy(&(*policy)->data[(*policy)->length], buf, n);
184 (*policy)->length += n;
185 (*policy)->data[(*policy)->length] = '\0';
190 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
191 X509V3_conf_err(val);
194 } else if (strncmp(val->value, "text:", 5) == 0) {
195 val_len = strlen(val->value + 5);
196 tmp_data = OPENSSL_realloc((*policy)->data,
197 (*policy)->length + val_len + 1);
199 (*policy)->data = tmp_data;
200 memcpy(&(*policy)->data[(*policy)->length],
201 val->value + 5, val_len);
202 (*policy)->length += val_len;
203 (*policy)->data[(*policy)->length] = '\0';
206 * realloc failure implies the original data space is b0rked
209 OPENSSL_free((*policy)->data);
210 (*policy)->data = NULL;
211 (*policy)->length = 0;
212 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
213 X509V3_conf_err(val);
217 X509V3err(X509V3_F_PROCESS_PCI_VALUE,
218 X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
219 X509V3_conf_err(val);
223 X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
224 X509V3_conf_err(val);
231 ASN1_OCTET_STRING_free(*policy);
237 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
238 X509V3_CTX *ctx, char *value)
240 PROXY_CERT_INFO_EXTENSION *pci = NULL;
241 STACK_OF(CONF_VALUE) *vals;
242 ASN1_OBJECT *language = NULL;
243 ASN1_INTEGER *pathlen = NULL;
244 ASN1_OCTET_STRING *policy = NULL;
247 vals = X509V3_parse_list(value);
248 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
249 CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
250 if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
251 X509V3err(X509V3_F_R2I_PCI,
252 X509V3_R_INVALID_PROXY_POLICY_SETTING);
253 X509V3_conf_err(cnf);
256 if (*cnf->name == '@') {
257 STACK_OF(CONF_VALUE) *sect;
260 sect = X509V3_get_section(ctx, cnf->name + 1);
262 X509V3err(X509V3_F_R2I_PCI, X509V3_R_INVALID_SECTION);
263 X509V3_conf_err(cnf);
266 for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
268 process_pci_value(sk_CONF_VALUE_value(sect, j),
269 &language, &pathlen, &policy);
271 X509V3_section_free(ctx, sect);
275 if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
276 X509V3_conf_err(cnf);
282 /* Language is mandatory */
284 X509V3err(X509V3_F_R2I_PCI,
285 X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
288 i = OBJ_obj2nid(language);
289 if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
290 X509V3err(X509V3_F_R2I_PCI,
291 X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
295 pci = PROXY_CERT_INFO_EXTENSION_new();
297 X509V3err(X509V3_F_R2I_PCI, ERR_R_MALLOC_FAILURE);
301 pci->proxyPolicy->policyLanguage = language;
303 pci->proxyPolicy->policy = policy;
305 pci->pcPathLengthConstraint = pathlen;
309 ASN1_OBJECT_free(language);
310 ASN1_INTEGER_free(pathlen);
312 ASN1_OCTET_STRING_free(policy);
314 PROXY_CERT_INFO_EXTENSION_free(pci);
317 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);