1 /* v3_pci.c -*- mode:C; c-file-style: "eay" -*- */
2 /*
3 * Contributed to the OpenSSL Project 2004 by Richard Levitte
4 * (richard@levitte.org)
5 */
6 /* Copyright (c) 2004 Kungliga Tekniska Högskolan
7 * (Royal Institute of Technology, Stockholm, Sweden).
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
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.
20 *
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.
24 *
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
35 * SUCH DAMAGE.
36 */
37
38 #include <string.h>
39
40 #include <openssl/conf.h>
41 #include <openssl/err.h>
42 #include <openssl/mem.h>
43 #include <openssl/obj.h>
44 #include <openssl/x509v3.h>
45
46 #include "../internal.h"
47 #include "internal.h"
48
49
50 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
51 BIO *out, int indent);
52 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
53 X509V3_CTX *ctx, char *str);
54
55 const X509V3_EXT_METHOD v3_pci =
56 { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
57 0, 0, 0, 0,
58 0, 0,
59 NULL, NULL,
60 (X509V3_EXT_I2R)i2r_pci,
61 (X509V3_EXT_R2I)r2i_pci,
62 NULL,
63 };
64
i2r_pci(X509V3_EXT_METHOD * method,PROXY_CERT_INFO_EXTENSION * pci,BIO * out,int indent)65 static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
66 BIO *out, int indent)
67 {
68 BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
69 if (pci->pcPathLengthConstraint)
70 i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
71 else
72 BIO_printf(out, "infinite");
73 BIO_puts(out, "\n");
74 BIO_printf(out, "%*sPolicy Language: ", indent, "");
75 i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
76 BIO_puts(out, "\n");
77 if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
78 BIO_printf(out, "%*sPolicy Text: %.*s\n", indent, "",
79 pci->proxyPolicy->policy->length,
80 pci->proxyPolicy->policy->data);
81 return 1;
82 }
83
process_pci_value(CONF_VALUE * val,ASN1_OBJECT ** language,ASN1_INTEGER ** pathlen,ASN1_OCTET_STRING ** policy)84 static int process_pci_value(CONF_VALUE *val,
85 ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
86 ASN1_OCTET_STRING **policy)
87 {
88 int free_policy = 0;
89
90 if (strcmp(val->name, "language") == 0) {
91 if (*language) {
92 OPENSSL_PUT_ERROR(X509V3,
93 X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
94 X509V3_conf_err(val);
95 return 0;
96 }
97 if (!(*language = OBJ_txt2obj(val->value, 0))) {
98 OPENSSL_PUT_ERROR(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 OPENSSL_PUT_ERROR(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 OPENSSL_PUT_ERROR(X509V3, X509V3_R_POLICY_PATH_LENGTH);
111 X509V3_conf_err(val);
112 return 0;
113 }
114 } else if (strcmp(val->name, "policy") == 0) {
115 unsigned char *tmp_data = NULL;
116 long val_len;
117 if (!*policy) {
118 *policy = ASN1_OCTET_STRING_new();
119 if (!*policy) {
120 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
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 =
128 x509v3_hex_to_bytes(val->value + 4, &val_len);
129
130 if (!tmp_data2) {
131 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT);
132 X509V3_conf_err(val);
133 goto err;
134 }
135
136 tmp_data = OPENSSL_realloc((*policy)->data,
137 (*policy)->length + val_len + 1);
138 if (tmp_data) {
139 (*policy)->data = tmp_data;
140 OPENSSL_memcpy(&(*policy)->data[(*policy)->length],
141 tmp_data2, val_len);
142 (*policy)->length += val_len;
143 (*policy)->data[(*policy)->length] = '\0';
144 } else {
145 OPENSSL_free(tmp_data2);
146 /*
147 * realloc failure implies the original data space is b0rked
148 * too!
149 */
150 (*policy)->data = NULL;
151 (*policy)->length = 0;
152 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
153 X509V3_conf_err(val);
154 goto err;
155 }
156 OPENSSL_free(tmp_data2);
157 } else if (strncmp(val->value, "text:", 5) == 0) {
158 val_len = strlen(val->value + 5);
159 tmp_data = OPENSSL_realloc((*policy)->data,
160 (*policy)->length + val_len + 1);
161 if (tmp_data) {
162 (*policy)->data = tmp_data;
163 OPENSSL_memcpy(&(*policy)->data[(*policy)->length],
164 val->value + 5, val_len);
165 (*policy)->length += val_len;
166 (*policy)->data[(*policy)->length] = '\0';
167 } else {
168 /*
169 * realloc failure implies the original data space is b0rked
170 * too!
171 */
172 (*policy)->data = NULL;
173 (*policy)->length = 0;
174 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
175 X509V3_conf_err(val);
176 goto err;
177 }
178 } else {
179 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
180 X509V3_conf_err(val);
181 goto err;
182 }
183 if (!tmp_data) {
184 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
185 X509V3_conf_err(val);
186 goto err;
187 }
188 }
189 return 1;
190 err:
191 if (free_policy) {
192 ASN1_OCTET_STRING_free(*policy);
193 *policy = NULL;
194 }
195 return 0;
196 }
197
r2i_pci(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,char * value)198 static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
199 X509V3_CTX *ctx, char *value)
200 {
201 PROXY_CERT_INFO_EXTENSION *pci = NULL;
202 STACK_OF(CONF_VALUE) *vals;
203 ASN1_OBJECT *language = NULL;
204 ASN1_INTEGER *pathlen = NULL;
205 ASN1_OCTET_STRING *policy = NULL;
206 size_t i, j;
207 int nid;
208
209 vals = X509V3_parse_list(value);
210 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
211 CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
212 if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
213 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_PROXY_POLICY_SETTING);
214 X509V3_conf_err(cnf);
215 goto err;
216 }
217 if (*cnf->name == '@') {
218 STACK_OF(CONF_VALUE) *sect;
219 int success_p = 1;
220
221 sect = X509V3_get_section(ctx, cnf->name + 1);
222 if (!sect) {
223 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION);
224 X509V3_conf_err(cnf);
225 goto err;
226 }
227 for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
228 success_p =
229 process_pci_value(sk_CONF_VALUE_value(sect, j),
230 &language, &pathlen, &policy);
231 }
232 X509V3_section_free(ctx, sect);
233 if (!success_p)
234 goto err;
235 } else {
236 if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
237 X509V3_conf_err(cnf);
238 goto err;
239 }
240 }
241 }
242
243 /* Language is mandatory */
244 if (!language) {
245 OPENSSL_PUT_ERROR(X509V3,
246 X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
247 goto err;
248 }
249 nid = OBJ_obj2nid(language);
250 if ((nid == NID_Independent || nid == NID_id_ppl_inheritAll) && policy) {
251 OPENSSL_PUT_ERROR(X509V3,
252 X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
253 goto err;
254 }
255
256 pci = PROXY_CERT_INFO_EXTENSION_new();
257 if (!pci) {
258 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
259 goto err;
260 }
261
262 pci->proxyPolicy->policyLanguage = language;
263 language = NULL;
264 pci->proxyPolicy->policy = policy;
265 policy = NULL;
266 pci->pcPathLengthConstraint = pathlen;
267 pathlen = NULL;
268 goto end;
269 err:
270 if (language) {
271 ASN1_OBJECT_free(language);
272 language = NULL;
273 }
274 if (pathlen) {
275 ASN1_INTEGER_free(pathlen);
276 pathlen = NULL;
277 }
278 if (policy) {
279 ASN1_OCTET_STRING_free(policy);
280 policy = NULL;
281 }
282 if (pci) {
283 PROXY_CERT_INFO_EXTENSION_free(pci);
284 pci = NULL;
285 }
286 end:
287 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
288 return pci;
289 }
290