]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Contributed to the OpenSSL Project 2004 by Richard Levitte | |
3 | * (richard@levitte.org) | |
6951c23a | 4 | */ |
053fa39a | 5 | /* Copyright (c) 2004 Kungliga Tekniska Högskolan |
6951c23a RL |
6 | * (Royal Institute of Technology, Stockholm, Sweden). |
7 | * All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * | |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in the | |
18 | * documentation and/or other materials provided with the distribution. | |
19 | * | |
20 | * 3. Neither the name of the Institute nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | */ | |
36 | ||
37 | #include <stdio.h> | |
b39fc560 | 38 | #include "internal/cryptlib.h" |
6951c23a RL |
39 | #include <openssl/conf.h> |
40 | #include <openssl/x509v3.h> | |
df2ee0e2 | 41 | #include "ext_dat.h" |
6951c23a RL |
42 | |
43 | static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext, | |
0f113f3e | 44 | BIO *out, int indent); |
6951c23a | 45 | static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method, |
0f113f3e | 46 | X509V3_CTX *ctx, char *str); |
6951c23a | 47 | |
560b79cb | 48 | const X509V3_EXT_METHOD v3_pci = |
0f113f3e MC |
49 | { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION), |
50 | 0, 0, 0, 0, | |
51 | 0, 0, | |
52 | NULL, NULL, | |
53 | (X509V3_EXT_I2R)i2r_pci, | |
54 | (X509V3_EXT_R2I)r2i_pci, | |
55 | NULL, | |
56 | }; | |
6951c23a RL |
57 | |
58 | static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci, | |
0f113f3e MC |
59 | BIO *out, int indent) |
60 | { | |
61 | BIO_printf(out, "%*sPath Length Constraint: ", indent, ""); | |
62 | if (pci->pcPathLengthConstraint) | |
63 | i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint); | |
64 | else | |
65 | BIO_printf(out, "infinite"); | |
66 | BIO_puts(out, "\n"); | |
67 | BIO_printf(out, "%*sPolicy Language: ", indent, ""); | |
68 | i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage); | |
69 | BIO_puts(out, "\n"); | |
70 | if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data) | |
71 | BIO_printf(out, "%*sPolicy Text: %s\n", indent, "", | |
72 | pci->proxyPolicy->policy->data); | |
73 | return 1; | |
74 | } | |
6951c23a RL |
75 | |
76 | static int process_pci_value(CONF_VALUE *val, | |
0f113f3e MC |
77 | ASN1_OBJECT **language, ASN1_INTEGER **pathlen, |
78 | ASN1_OCTET_STRING **policy) | |
79 | { | |
80 | int free_policy = 0; | |
6951c23a | 81 | |
0f113f3e MC |
82 | if (strcmp(val->name, "language") == 0) { |
83 | if (*language) { | |
84 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, | |
85 | X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED); | |
86 | X509V3_conf_err(val); | |
87 | return 0; | |
88 | } | |
75ebbd9a | 89 | if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) { |
0f113f3e MC |
90 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, |
91 | X509V3_R_INVALID_OBJECT_IDENTIFIER); | |
92 | X509V3_conf_err(val); | |
93 | return 0; | |
94 | } | |
95 | } else if (strcmp(val->name, "pathlen") == 0) { | |
96 | if (*pathlen) { | |
97 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, | |
98 | X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED); | |
99 | X509V3_conf_err(val); | |
100 | return 0; | |
101 | } | |
102 | if (!X509V3_get_value_int(val, pathlen)) { | |
103 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, | |
104 | X509V3_R_POLICY_PATH_LENGTH); | |
105 | X509V3_conf_err(val); | |
106 | return 0; | |
107 | } | |
108 | } else if (strcmp(val->name, "policy") == 0) { | |
109 | unsigned char *tmp_data = NULL; | |
110 | long val_len; | |
111 | if (!*policy) { | |
112 | *policy = ASN1_OCTET_STRING_new(); | |
90945fa3 | 113 | if (*policy == NULL) { |
0f113f3e MC |
114 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE); |
115 | X509V3_conf_err(val); | |
116 | return 0; | |
117 | } | |
118 | free_policy = 1; | |
119 | } | |
120 | if (strncmp(val->value, "hex:", 4) == 0) { | |
121 | unsigned char *tmp_data2 = | |
122 | string_to_hex(val->value + 4, &val_len); | |
6951c23a | 123 | |
0f113f3e MC |
124 | if (!tmp_data2) { |
125 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, | |
126 | X509V3_R_ILLEGAL_HEX_DIGIT); | |
127 | X509V3_conf_err(val); | |
128 | goto err; | |
129 | } | |
6951c23a | 130 | |
0f113f3e MC |
131 | tmp_data = OPENSSL_realloc((*policy)->data, |
132 | (*policy)->length + val_len + 1); | |
133 | if (tmp_data) { | |
134 | (*policy)->data = tmp_data; | |
135 | memcpy(&(*policy)->data[(*policy)->length], | |
136 | tmp_data2, val_len); | |
137 | (*policy)->length += val_len; | |
138 | (*policy)->data[(*policy)->length] = '\0'; | |
139 | } else { | |
140 | OPENSSL_free(tmp_data2); | |
141 | /* | |
142 | * realloc failure implies the original data space is b0rked | |
143 | * too! | |
144 | */ | |
145 | OPENSSL_free((*policy)->data); | |
146 | (*policy)->data = NULL; | |
147 | (*policy)->length = 0; | |
148 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE); | |
149 | X509V3_conf_err(val); | |
150 | goto err; | |
151 | } | |
152 | OPENSSL_free(tmp_data2); | |
153 | } else if (strncmp(val->value, "file:", 5) == 0) { | |
154 | unsigned char buf[2048]; | |
155 | int n; | |
156 | BIO *b = BIO_new_file(val->value + 5, "r"); | |
157 | if (!b) { | |
158 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB); | |
159 | X509V3_conf_err(val); | |
160 | goto err; | |
161 | } | |
162 | while ((n = BIO_read(b, buf, sizeof(buf))) > 0 | |
163 | || (n == 0 && BIO_should_retry(b))) { | |
164 | if (!n) | |
165 | continue; | |
6951c23a | 166 | |
0f113f3e MC |
167 | tmp_data = OPENSSL_realloc((*policy)->data, |
168 | (*policy)->length + n + 1); | |
6951c23a | 169 | |
0f113f3e MC |
170 | if (!tmp_data) { |
171 | OPENSSL_free((*policy)->data); | |
172 | (*policy)->data = NULL; | |
173 | (*policy)->length = 0; | |
174 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, | |
175 | ERR_R_MALLOC_FAILURE); | |
176 | X509V3_conf_err(val); | |
177 | BIO_free_all(b); | |
178 | goto err; | |
179 | } | |
6951c23a | 180 | |
0f113f3e MC |
181 | (*policy)->data = tmp_data; |
182 | memcpy(&(*policy)->data[(*policy)->length], buf, n); | |
183 | (*policy)->length += n; | |
184 | (*policy)->data[(*policy)->length] = '\0'; | |
185 | } | |
186 | BIO_free_all(b); | |
6951c23a | 187 | |
0f113f3e MC |
188 | if (n < 0) { |
189 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB); | |
190 | X509V3_conf_err(val); | |
191 | goto err; | |
192 | } | |
193 | } else if (strncmp(val->value, "text:", 5) == 0) { | |
194 | val_len = strlen(val->value + 5); | |
195 | tmp_data = OPENSSL_realloc((*policy)->data, | |
196 | (*policy)->length + val_len + 1); | |
197 | if (tmp_data) { | |
198 | (*policy)->data = tmp_data; | |
199 | memcpy(&(*policy)->data[(*policy)->length], | |
200 | val->value + 5, val_len); | |
201 | (*policy)->length += val_len; | |
202 | (*policy)->data[(*policy)->length] = '\0'; | |
203 | } else { | |
204 | /* | |
205 | * realloc failure implies the original data space is b0rked | |
206 | * too! | |
207 | */ | |
208 | OPENSSL_free((*policy)->data); | |
209 | (*policy)->data = NULL; | |
210 | (*policy)->length = 0; | |
211 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE); | |
212 | X509V3_conf_err(val); | |
213 | goto err; | |
214 | } | |
215 | } else { | |
216 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, | |
217 | X509V3_R_INCORRECT_POLICY_SYNTAX_TAG); | |
218 | X509V3_conf_err(val); | |
219 | goto err; | |
220 | } | |
221 | if (!tmp_data) { | |
222 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE); | |
223 | X509V3_conf_err(val); | |
224 | goto err; | |
225 | } | |
226 | } | |
227 | return 1; | |
228 | err: | |
229 | if (free_policy) { | |
230 | ASN1_OCTET_STRING_free(*policy); | |
231 | *policy = NULL; | |
232 | } | |
233 | return 0; | |
234 | } | |
6951c23a RL |
235 | |
236 | static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method, | |
0f113f3e MC |
237 | X509V3_CTX *ctx, char *value) |
238 | { | |
239 | PROXY_CERT_INFO_EXTENSION *pci = NULL; | |
240 | STACK_OF(CONF_VALUE) *vals; | |
241 | ASN1_OBJECT *language = NULL; | |
242 | ASN1_INTEGER *pathlen = NULL; | |
243 | ASN1_OCTET_STRING *policy = NULL; | |
244 | int i, j; | |
6951c23a | 245 | |
0f113f3e MC |
246 | vals = X509V3_parse_list(value); |
247 | for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { | |
248 | CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i); | |
249 | if (!cnf->name || (*cnf->name != '@' && !cnf->value)) { | |
250 | X509V3err(X509V3_F_R2I_PCI, | |
251 | X509V3_R_INVALID_PROXY_POLICY_SETTING); | |
252 | X509V3_conf_err(cnf); | |
253 | goto err; | |
254 | } | |
255 | if (*cnf->name == '@') { | |
256 | STACK_OF(CONF_VALUE) *sect; | |
257 | int success_p = 1; | |
6951c23a | 258 | |
0f113f3e MC |
259 | sect = X509V3_get_section(ctx, cnf->name + 1); |
260 | if (!sect) { | |
261 | X509V3err(X509V3_F_R2I_PCI, X509V3_R_INVALID_SECTION); | |
262 | X509V3_conf_err(cnf); | |
263 | goto err; | |
264 | } | |
265 | for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) { | |
266 | success_p = | |
267 | process_pci_value(sk_CONF_VALUE_value(sect, j), | |
268 | &language, &pathlen, &policy); | |
269 | } | |
270 | X509V3_section_free(ctx, sect); | |
271 | if (!success_p) | |
272 | goto err; | |
273 | } else { | |
274 | if (!process_pci_value(cnf, &language, &pathlen, &policy)) { | |
275 | X509V3_conf_err(cnf); | |
276 | goto err; | |
277 | } | |
278 | } | |
279 | } | |
6951c23a | 280 | |
0f113f3e MC |
281 | /* Language is mandatory */ |
282 | if (!language) { | |
283 | X509V3err(X509V3_F_R2I_PCI, | |
284 | X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED); | |
285 | goto err; | |
286 | } | |
287 | i = OBJ_obj2nid(language); | |
288 | if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) { | |
289 | X509V3err(X509V3_F_R2I_PCI, | |
290 | X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY); | |
291 | goto err; | |
292 | } | |
6951c23a | 293 | |
0f113f3e | 294 | pci = PROXY_CERT_INFO_EXTENSION_new(); |
90945fa3 | 295 | if (pci == NULL) { |
0f113f3e MC |
296 | X509V3err(X509V3_F_R2I_PCI, ERR_R_MALLOC_FAILURE); |
297 | goto err; | |
298 | } | |
6951c23a | 299 | |
0f113f3e MC |
300 | pci->proxyPolicy->policyLanguage = language; |
301 | language = NULL; | |
302 | pci->proxyPolicy->policy = policy; | |
303 | policy = NULL; | |
304 | pci->pcPathLengthConstraint = pathlen; | |
305 | pathlen = NULL; | |
306 | goto end; | |
307 | err: | |
0dfb9398 | 308 | ASN1_OBJECT_free(language); |
2ace7450 RS |
309 | ASN1_INTEGER_free(pathlen); |
310 | pathlen = NULL; | |
311 | ASN1_OCTET_STRING_free(policy); | |
312 | policy = NULL; | |
313 | PROXY_CERT_INFO_EXTENSION_free(pci); | |
314 | pci = NULL; | |
0f113f3e MC |
315 | end: |
316 | sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); | |
317 | return pci; | |
318 | } |