]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
4e5d3a7f | 3 | * project. |
9aeaf1b4 DSH |
4 | */ |
5 | /* ==================================================================== | |
4e5d3a7f | 6 | * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved. |
9aeaf1b4 DSH |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
9aeaf1b4 DSH |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | /* X509 v3 extension utilities */ | |
59 | ||
e527ba09 | 60 | #include <stdio.h> |
9aeaf1b4 | 61 | #include <ctype.h> |
b39fc560 | 62 | #include "internal/cryptlib.h" |
ec577822 BM |
63 | #include <openssl/conf.h> |
64 | #include <openssl/x509v3.h> | |
124055a9 | 65 | #include "internal/x509_int.h" |
0f814687 | 66 | #include <openssl/bn.h> |
9021a5df | 67 | #include "ext_dat.h" |
9aeaf1b4 | 68 | |
9aeaf1b4 | 69 | static char *strip_spaces(char *name); |
0f113f3e MC |
70 | static int sk_strcmp(const char *const *a, const char *const *b); |
71 | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, | |
72 | GENERAL_NAMES *gens); | |
c869da88 DSH |
73 | static void str_free(OPENSSL_STRING str); |
74 | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email); | |
9aeaf1b4 | 75 | |
4e5d3a7f DSH |
76 | static int ipv4_from_asc(unsigned char *v4, const char *in); |
77 | static int ipv6_from_asc(unsigned char *v6, const char *in); | |
78 | static int ipv6_cb(const char *elem, int len, void *usr); | |
79 | static int ipv6_hex(unsigned char *out, const char *in, int inlen); | |
80 | ||
9aeaf1b4 DSH |
81 | /* Add a CONF_VALUE name value pair to stack */ |
82 | ||
ba404b5e | 83 | int X509V3_add_value(const char *name, const char *value, |
0f113f3e | 84 | STACK_OF(CONF_VALUE) **extlist) |
9aeaf1b4 | 85 | { |
0f113f3e MC |
86 | CONF_VALUE *vtmp = NULL; |
87 | char *tname = NULL, *tvalue = NULL; | |
75ebbd9a | 88 | |
7644a9ae | 89 | if (name && (tname = OPENSSL_strdup(name)) == NULL) |
0f113f3e | 90 | goto err; |
7644a9ae | 91 | if (value && (tvalue = OPENSSL_strdup(value)) == NULL) |
0f113f3e | 92 | goto err; |
75ebbd9a | 93 | if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL) |
0f113f3e | 94 | goto err; |
75ebbd9a | 95 | if (*extlist == NULL && (*extlist = sk_CONF_VALUE_new_null()) == NULL) |
0f113f3e MC |
96 | goto err; |
97 | vtmp->section = NULL; | |
98 | vtmp->name = tname; | |
99 | vtmp->value = tvalue; | |
100 | if (!sk_CONF_VALUE_push(*extlist, vtmp)) | |
101 | goto err; | |
102 | return 1; | |
103 | err: | |
104 | X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE); | |
b548a1f1 RS |
105 | OPENSSL_free(vtmp); |
106 | OPENSSL_free(tname); | |
107 | OPENSSL_free(tvalue); | |
0f113f3e | 108 | return 0; |
9aeaf1b4 DSH |
109 | } |
110 | ||
61f5b6f3 | 111 | int X509V3_add_value_uchar(const char *name, const unsigned char *value, |
0f113f3e MC |
112 | STACK_OF(CONF_VALUE) **extlist) |
113 | { | |
114 | return X509V3_add_value(name, (const char *)value, extlist); | |
115 | } | |
61f5b6f3 | 116 | |
66ab08b1 | 117 | /* Free function for STACK_OF(CONF_VALUE) */ |
9aeaf1b4 | 118 | |
6b691a5c | 119 | void X509V3_conf_free(CONF_VALUE *conf) |
9aeaf1b4 | 120 | { |
0f113f3e MC |
121 | if (!conf) |
122 | return; | |
b548a1f1 RS |
123 | OPENSSL_free(conf->name); |
124 | OPENSSL_free(conf->value); | |
125 | OPENSSL_free(conf->section); | |
0f113f3e | 126 | OPENSSL_free(conf); |
9aeaf1b4 DSH |
127 | } |
128 | ||
ba404b5e | 129 | int X509V3_add_value_bool(const char *name, int asn1_bool, |
0f113f3e | 130 | STACK_OF(CONF_VALUE) **extlist) |
9aeaf1b4 | 131 | { |
0f113f3e MC |
132 | if (asn1_bool) |
133 | return X509V3_add_value(name, "TRUE", extlist); | |
134 | return X509V3_add_value(name, "FALSE", extlist); | |
9aeaf1b4 DSH |
135 | } |
136 | ||
ba404b5e | 137 | int X509V3_add_value_bool_nf(char *name, int asn1_bool, |
0f113f3e | 138 | STACK_OF(CONF_VALUE) **extlist) |
9aeaf1b4 | 139 | { |
0f113f3e MC |
140 | if (asn1_bool) |
141 | return X509V3_add_value(name, "TRUE", extlist); | |
142 | return 1; | |
9aeaf1b4 DSH |
143 | } |
144 | ||
6b691a5c | 145 | char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a) |
c74f1eb9 | 146 | { |
0f113f3e MC |
147 | BIGNUM *bntmp = NULL; |
148 | char *strtmp = NULL; | |
75ebbd9a | 149 | |
0f113f3e MC |
150 | if (!a) |
151 | return NULL; | |
75ebbd9a RS |
152 | if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL |
153 | || (strtmp = BN_bn2dec(bntmp)) == NULL) | |
0f113f3e MC |
154 | X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE); |
155 | BN_free(bntmp); | |
156 | return strtmp; | |
c74f1eb9 DSH |
157 | } |
158 | ||
6b691a5c | 159 | char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a) |
0ca5f8b1 | 160 | { |
0f113f3e MC |
161 | BIGNUM *bntmp = NULL; |
162 | char *strtmp = NULL; | |
75ebbd9a | 163 | |
0f113f3e MC |
164 | if (!a) |
165 | return NULL; | |
75ebbd9a RS |
166 | if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL |
167 | || (strtmp = BN_bn2dec(bntmp)) == NULL) | |
0f113f3e MC |
168 | X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); |
169 | BN_free(bntmp); | |
170 | return strtmp; | |
0ca5f8b1 DSH |
171 | } |
172 | ||
6b691a5c | 173 | ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value) |
28a98809 | 174 | { |
0f113f3e MC |
175 | BIGNUM *bn = NULL; |
176 | ASN1_INTEGER *aint; | |
177 | int isneg, ishex; | |
178 | int ret; | |
90945fa3 | 179 | if (value == NULL) { |
0f113f3e | 180 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE); |
90945fa3 | 181 | return NULL; |
0f113f3e MC |
182 | } |
183 | bn = BN_new(); | |
90945fa3 MC |
184 | if (bn == NULL) { |
185 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); | |
186 | return NULL; | |
187 | } | |
0f113f3e MC |
188 | if (value[0] == '-') { |
189 | value++; | |
190 | isneg = 1; | |
191 | } else | |
192 | isneg = 0; | |
193 | ||
194 | if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { | |
195 | value += 2; | |
196 | ishex = 1; | |
197 | } else | |
198 | ishex = 0; | |
199 | ||
200 | if (ishex) | |
201 | ret = BN_hex2bn(&bn, value); | |
202 | else | |
203 | ret = BN_dec2bn(&bn, value); | |
204 | ||
205 | if (!ret || value[ret]) { | |
206 | BN_free(bn); | |
207 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR); | |
90945fa3 | 208 | return NULL; |
0f113f3e MC |
209 | } |
210 | ||
211 | if (isneg && BN_is_zero(bn)) | |
212 | isneg = 0; | |
213 | ||
214 | aint = BN_to_ASN1_INTEGER(bn, NULL); | |
215 | BN_free(bn); | |
216 | if (!aint) { | |
217 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, | |
218 | X509V3_R_BN_TO_ASN1_INTEGER_ERROR); | |
90945fa3 | 219 | return NULL; |
0f113f3e MC |
220 | } |
221 | if (isneg) | |
222 | aint->type |= V_ASN1_NEG; | |
223 | return aint; | |
28a98809 DSH |
224 | } |
225 | ||
6b691a5c | 226 | int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint, |
0f113f3e | 227 | STACK_OF(CONF_VALUE) **extlist) |
9aeaf1b4 | 228 | { |
0f113f3e MC |
229 | char *strtmp; |
230 | int ret; | |
75ebbd9a | 231 | |
0f113f3e MC |
232 | if (!aint) |
233 | return 1; | |
75ebbd9a | 234 | if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL) |
0f113f3e MC |
235 | return 0; |
236 | ret = X509V3_add_value(name, strtmp, extlist); | |
237 | OPENSSL_free(strtmp); | |
238 | return ret; | |
9aeaf1b4 DSH |
239 | } |
240 | ||
6b691a5c | 241 | int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool) |
9aeaf1b4 | 242 | { |
0f113f3e | 243 | char *btmp; |
75ebbd9a RS |
244 | |
245 | if ((btmp = value->value) == NULL) | |
0f113f3e | 246 | goto err; |
86885c28 RS |
247 | if (strcmp(btmp, "TRUE") == 0 |
248 | || strcmp(btmp, "true") == 0 | |
249 | || strcmp(btmp, "Y") == 0 | |
250 | || strcmp(btmp, "y") == 0 | |
251 | || strcmp(btmp, "YES") == 0 | |
252 | || strcmp(btmp, "yes") == 0) { | |
0f113f3e MC |
253 | *asn1_bool = 0xff; |
254 | return 1; | |
86885c28 RS |
255 | } |
256 | if (strcmp(btmp, "FALSE") == 0 | |
257 | || strcmp(btmp, "false") == 0 | |
258 | || strcmp(btmp, "N") == 0 | |
259 | || strcmp(btmp, "n") == 0 | |
260 | || strcmp(btmp, "NO") == 0 | |
261 | || strcmp(btmp, "no") == 0) { | |
0f113f3e MC |
262 | *asn1_bool = 0; |
263 | return 1; | |
264 | } | |
265 | err: | |
266 | X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL, | |
267 | X509V3_R_INVALID_BOOLEAN_STRING); | |
268 | X509V3_conf_err(value); | |
269 | return 0; | |
9aeaf1b4 DSH |
270 | } |
271 | ||
6b691a5c | 272 | int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint) |
9aeaf1b4 | 273 | { |
0f113f3e | 274 | ASN1_INTEGER *itmp; |
75ebbd9a RS |
275 | |
276 | if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) { | |
0f113f3e MC |
277 | X509V3_conf_err(value); |
278 | return 0; | |
279 | } | |
280 | *aint = itmp; | |
281 | return 1; | |
9aeaf1b4 DSH |
282 | } |
283 | ||
0f113f3e MC |
284 | #define HDR_NAME 1 |
285 | #define HDR_VALUE 2 | |
9aeaf1b4 | 286 | |
0f113f3e MC |
287 | /* |
288 | * #define DEBUG | |
289 | */ | |
9aeaf1b4 | 290 | |
535d79da | 291 | STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line) |
9aeaf1b4 | 292 | { |
0f113f3e MC |
293 | char *p, *q, c; |
294 | char *ntmp, *vtmp; | |
295 | STACK_OF(CONF_VALUE) *values = NULL; | |
296 | char *linebuf; | |
297 | int state; | |
298 | /* We are going to modify the line so copy it first */ | |
7644a9ae | 299 | linebuf = OPENSSL_strdup(line); |
344c271e KC |
300 | if (linebuf == NULL) { |
301 | X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE); | |
302 | goto err; | |
303 | } | |
0f113f3e MC |
304 | state = HDR_NAME; |
305 | ntmp = NULL; | |
306 | /* Go through all characters */ | |
307 | for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); | |
308 | p++) { | |
309 | ||
310 | switch (state) { | |
311 | case HDR_NAME: | |
312 | if (c == ':') { | |
313 | state = HDR_VALUE; | |
314 | *p = 0; | |
315 | ntmp = strip_spaces(q); | |
316 | if (!ntmp) { | |
317 | X509V3err(X509V3_F_X509V3_PARSE_LIST, | |
318 | X509V3_R_INVALID_NULL_NAME); | |
319 | goto err; | |
320 | } | |
321 | q = p + 1; | |
322 | } else if (c == ',') { | |
323 | *p = 0; | |
324 | ntmp = strip_spaces(q); | |
325 | q = p + 1; | |
0f113f3e MC |
326 | if (!ntmp) { |
327 | X509V3err(X509V3_F_X509V3_PARSE_LIST, | |
328 | X509V3_R_INVALID_NULL_NAME); | |
329 | goto err; | |
330 | } | |
331 | X509V3_add_value(ntmp, NULL, &values); | |
332 | } | |
333 | break; | |
334 | ||
335 | case HDR_VALUE: | |
336 | if (c == ',') { | |
337 | state = HDR_NAME; | |
338 | *p = 0; | |
339 | vtmp = strip_spaces(q); | |
0f113f3e MC |
340 | if (!vtmp) { |
341 | X509V3err(X509V3_F_X509V3_PARSE_LIST, | |
342 | X509V3_R_INVALID_NULL_VALUE); | |
343 | goto err; | |
344 | } | |
345 | X509V3_add_value(ntmp, vtmp, &values); | |
346 | ntmp = NULL; | |
347 | q = p + 1; | |
348 | } | |
349 | ||
350 | } | |
351 | } | |
352 | ||
353 | if (state == HDR_VALUE) { | |
354 | vtmp = strip_spaces(q); | |
0f113f3e MC |
355 | if (!vtmp) { |
356 | X509V3err(X509V3_F_X509V3_PARSE_LIST, | |
357 | X509V3_R_INVALID_NULL_VALUE); | |
358 | goto err; | |
359 | } | |
360 | X509V3_add_value(ntmp, vtmp, &values); | |
361 | } else { | |
362 | ntmp = strip_spaces(q); | |
0f113f3e MC |
363 | if (!ntmp) { |
364 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME); | |
365 | goto err; | |
366 | } | |
367 | X509V3_add_value(ntmp, NULL, &values); | |
368 | } | |
369 | OPENSSL_free(linebuf); | |
370 | return values; | |
371 | ||
372 | err: | |
373 | OPENSSL_free(linebuf); | |
374 | sk_CONF_VALUE_pop_free(values, X509V3_conf_free); | |
375 | return NULL; | |
9aeaf1b4 DSH |
376 | |
377 | } | |
378 | ||
379 | /* Delete leading and trailing spaces from a string */ | |
6b691a5c | 380 | static char *strip_spaces(char *name) |
9aeaf1b4 | 381 | { |
0f113f3e MC |
382 | char *p, *q; |
383 | /* Skip over leading spaces */ | |
384 | p = name; | |
385 | while (*p && isspace((unsigned char)*p)) | |
386 | p++; | |
387 | if (!*p) | |
388 | return NULL; | |
389 | q = p + strlen(p) - 1; | |
390 | while ((q != p) && isspace((unsigned char)*q)) | |
391 | q--; | |
392 | if (p != q) | |
393 | q[1] = 0; | |
394 | if (!*p) | |
395 | return NULL; | |
396 | return p; | |
9aeaf1b4 | 397 | } |
175b0942 DSH |
398 | |
399 | /* hex string utilities */ | |
400 | ||
0f113f3e MC |
401 | /* |
402 | * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its | |
403 | * hex representation @@@ (Contents of buffer are always kept in ASCII, also | |
404 | * on EBCDIC machines) | |
175b0942 DSH |
405 | */ |
406 | ||
376bf1d4 | 407 | char *hex_to_string(const unsigned char *buffer, long len) |
175b0942 | 408 | { |
0f113f3e MC |
409 | char *tmp, *q; |
410 | const unsigned char *p; | |
411 | int i; | |
412 | const static char hexdig[] = "0123456789ABCDEF"; | |
413 | if (!buffer || !len) | |
414 | return NULL; | |
75ebbd9a | 415 | if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) { |
0f113f3e MC |
416 | X509V3err(X509V3_F_HEX_TO_STRING, ERR_R_MALLOC_FAILURE); |
417 | return NULL; | |
418 | } | |
419 | q = tmp; | |
420 | for (i = 0, p = buffer; i < len; i++, p++) { | |
421 | *q++ = hexdig[(*p >> 4) & 0xf]; | |
422 | *q++ = hexdig[*p & 0xf]; | |
423 | *q++ = ':'; | |
424 | } | |
425 | q[-1] = 0; | |
97d8e82c | 426 | #ifdef CHARSET_EBCDIC |
0f113f3e | 427 | ebcdic2ascii(tmp, tmp, q - tmp - 1); |
97d8e82c RL |
428 | #endif |
429 | ||
0f113f3e | 430 | return tmp; |
175b0942 DSH |
431 | } |
432 | ||
0f113f3e MC |
433 | /* |
434 | * Give a string of hex digits convert to a buffer | |
175b0942 DSH |
435 | */ |
436 | ||
376bf1d4 | 437 | unsigned char *string_to_hex(const char *str, long *len) |
175b0942 | 438 | { |
0f113f3e MC |
439 | unsigned char *hexbuf, *q; |
440 | unsigned char ch, cl, *p; | |
441 | if (!str) { | |
442 | X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_INVALID_NULL_ARGUMENT); | |
443 | return NULL; | |
444 | } | |
75ebbd9a | 445 | if ((hexbuf = OPENSSL_malloc(strlen(str) >> 1)) == NULL) |
0f113f3e MC |
446 | goto err; |
447 | for (p = (unsigned char *)str, q = hexbuf; *p;) { | |
448 | ch = *p++; | |
97d8e82c | 449 | #ifdef CHARSET_EBCDIC |
0f113f3e | 450 | ch = os_toebcdic[ch]; |
97d8e82c | 451 | #endif |
0f113f3e MC |
452 | if (ch == ':') |
453 | continue; | |
454 | cl = *p++; | |
97d8e82c | 455 | #ifdef CHARSET_EBCDIC |
0f113f3e | 456 | cl = os_toebcdic[cl]; |
97d8e82c | 457 | #endif |
0f113f3e MC |
458 | if (!cl) { |
459 | X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ODD_NUMBER_OF_DIGITS); | |
460 | OPENSSL_free(hexbuf); | |
461 | return NULL; | |
462 | } | |
463 | if (isupper(ch)) | |
464 | ch = tolower(ch); | |
465 | if (isupper(cl)) | |
466 | cl = tolower(cl); | |
467 | ||
468 | if ((ch >= '0') && (ch <= '9')) | |
469 | ch -= '0'; | |
470 | else if ((ch >= 'a') && (ch <= 'f')) | |
471 | ch -= 'a' - 10; | |
472 | else | |
473 | goto badhex; | |
474 | ||
475 | if ((cl >= '0') && (cl <= '9')) | |
476 | cl -= '0'; | |
477 | else if ((cl >= 'a') && (cl <= 'f')) | |
478 | cl -= 'a' - 10; | |
479 | else | |
480 | goto badhex; | |
481 | ||
482 | *q++ = (ch << 4) | cl; | |
483 | } | |
175b0942 | 484 | |
0f113f3e MC |
485 | if (len) |
486 | *len = q - hexbuf; | |
175b0942 | 487 | |
0f113f3e | 488 | return hexbuf; |
175b0942 | 489 | |
0f113f3e | 490 | err: |
b548a1f1 | 491 | OPENSSL_free(hexbuf); |
0f113f3e MC |
492 | X509V3err(X509V3_F_STRING_TO_HEX, ERR_R_MALLOC_FAILURE); |
493 | return NULL; | |
175b0942 | 494 | |
0f113f3e MC |
495 | badhex: |
496 | OPENSSL_free(hexbuf); | |
497 | X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ILLEGAL_HEX_DIGIT); | |
498 | return NULL; | |
175b0942 DSH |
499 | |
500 | } | |
d08d8da4 | 501 | |
0f113f3e MC |
502 | /* |
503 | * V2I name comparison function: returns zero if 'name' matches cmp or cmp.* | |
d08d8da4 DSH |
504 | */ |
505 | ||
6b691a5c | 506 | int name_cmp(const char *name, const char *cmp) |
d08d8da4 | 507 | { |
0f113f3e MC |
508 | int len, ret; |
509 | char c; | |
510 | len = strlen(cmp); | |
511 | if ((ret = strncmp(name, cmp, len))) | |
512 | return ret; | |
513 | c = name[len]; | |
514 | if (!c || (c == '.')) | |
515 | return 0; | |
516 | return 1; | |
d08d8da4 | 517 | } |
a91dedca | 518 | |
0f113f3e | 519 | static int sk_strcmp(const char *const *a, const char *const *b) |
a91dedca | 520 | { |
0f113f3e | 521 | return strcmp(*a, *b); |
a91dedca DSH |
522 | } |
523 | ||
c869da88 | 524 | STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) |
a91dedca | 525 | { |
0f113f3e MC |
526 | GENERAL_NAMES *gens; |
527 | STACK_OF(OPENSSL_STRING) *ret; | |
5ce278a7 | 528 | |
0f113f3e MC |
529 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
530 | ret = get_email(X509_get_subject_name(x), gens); | |
531 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | |
532 | return ret; | |
a91dedca DSH |
533 | } |
534 | ||
c869da88 | 535 | STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) |
67c8e7f4 | 536 | { |
0f113f3e MC |
537 | AUTHORITY_INFO_ACCESS *info; |
538 | STACK_OF(OPENSSL_STRING) *ret = NULL; | |
539 | int i; | |
540 | ||
541 | info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL); | |
542 | if (!info) | |
543 | return NULL; | |
544 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { | |
545 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); | |
546 | if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { | |
547 | if (ad->location->type == GEN_URI) { | |
548 | if (!append_ia5 | |
549 | (&ret, ad->location->d.uniformResourceIdentifier)) | |
550 | break; | |
551 | } | |
552 | } | |
553 | } | |
554 | AUTHORITY_INFO_ACCESS_free(info); | |
555 | return ret; | |
67c8e7f4 DSH |
556 | } |
557 | ||
c869da88 | 558 | STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x) |
a91dedca | 559 | { |
0f113f3e MC |
560 | GENERAL_NAMES *gens; |
561 | STACK_OF(X509_EXTENSION) *exts; | |
562 | STACK_OF(OPENSSL_STRING) *ret; | |
563 | ||
564 | exts = X509_REQ_get_extensions(x); | |
565 | gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL); | |
566 | ret = get_email(X509_REQ_get_subject_name(x), gens); | |
567 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | |
568 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); | |
569 | return ret; | |
a91dedca DSH |
570 | } |
571 | ||
0f113f3e MC |
572 | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, |
573 | GENERAL_NAMES *gens) | |
a91dedca | 574 | { |
0f113f3e MC |
575 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
576 | X509_NAME_ENTRY *ne; | |
577 | ASN1_IA5STRING *email; | |
578 | GENERAL_NAME *gen; | |
579 | int i; | |
580 | /* Now add any email address(es) to STACK */ | |
581 | i = -1; | |
582 | /* First supplied X509_NAME */ | |
583 | while ((i = X509_NAME_get_index_by_NID(name, | |
584 | NID_pkcs9_emailAddress, i)) >= 0) { | |
585 | ne = X509_NAME_get_entry(name, i); | |
586 | email = X509_NAME_ENTRY_get_data(ne); | |
587 | if (!append_ia5(&ret, email)) | |
588 | return NULL; | |
589 | } | |
590 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
591 | gen = sk_GENERAL_NAME_value(gens, i); | |
592 | if (gen->type != GEN_EMAIL) | |
593 | continue; | |
594 | if (!append_ia5(&ret, gen->d.ia5)) | |
595 | return NULL; | |
596 | } | |
597 | return ret; | |
a91dedca DSH |
598 | } |
599 | ||
c869da88 | 600 | static void str_free(OPENSSL_STRING str) |
a91dedca | 601 | { |
0f113f3e | 602 | OPENSSL_free(str); |
a91dedca DSH |
603 | } |
604 | ||
c869da88 | 605 | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email) |
a91dedca | 606 | { |
0f113f3e MC |
607 | char *emtmp; |
608 | /* First some sanity checks */ | |
609 | if (email->type != V_ASN1_IA5STRING) | |
610 | return 1; | |
611 | if (!email->data || !email->length) | |
612 | return 1; | |
90945fa3 | 613 | if (*sk == NULL) |
0f113f3e | 614 | *sk = sk_OPENSSL_STRING_new(sk_strcmp); |
90945fa3 | 615 | if (*sk == NULL) |
0f113f3e MC |
616 | return 0; |
617 | /* Don't add duplicates */ | |
618 | if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1) | |
619 | return 1; | |
7644a9ae | 620 | emtmp = OPENSSL_strdup((char *)email->data); |
90945fa3 | 621 | if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) { |
0f113f3e MC |
622 | X509_email_free(*sk); |
623 | *sk = NULL; | |
624 | return 0; | |
625 | } | |
626 | return 1; | |
a91dedca DSH |
627 | } |
628 | ||
c869da88 | 629 | void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) |
a91dedca | 630 | { |
0f113f3e | 631 | sk_OPENSSL_STRING_pop_free(sk, str_free); |
a91dedca | 632 | } |
4e5d3a7f | 633 | |
0f113f3e MC |
634 | typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len, |
635 | const unsigned char *subject, size_t subject_len, | |
636 | unsigned int flags); | |
d88926f1 | 637 | |
a09e4d24 VD |
638 | /* Skip pattern prefix to match "wildcard" subject */ |
639 | static void skip_prefix(const unsigned char **p, size_t *plen, | |
a773b52a | 640 | size_t subject_len, |
0f113f3e MC |
641 | unsigned int flags) |
642 | { | |
643 | const unsigned char *pattern = *p; | |
644 | size_t pattern_len = *plen; | |
645 | ||
646 | /* | |
647 | * If subject starts with a leading '.' followed by more octets, and | |
648 | * pattern is longer, compare just an equal-length suffix with the | |
649 | * full subject (starting at the '.'), provided the prefix contains | |
650 | * no NULs. | |
651 | */ | |
652 | if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0) | |
653 | return; | |
654 | ||
655 | while (pattern_len > subject_len && *pattern) { | |
656 | if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) && | |
657 | *pattern == '.') | |
658 | break; | |
659 | ++pattern; | |
660 | --pattern_len; | |
661 | } | |
662 | ||
663 | /* Skip if entire prefix acceptable */ | |
664 | if (pattern_len == subject_len) { | |
665 | *p = pattern; | |
666 | *plen = pattern_len; | |
667 | } | |
668 | } | |
a09e4d24 | 669 | |
d88926f1 DSH |
670 | /* Compare while ASCII ignoring case. */ |
671 | static int equal_nocase(const unsigned char *pattern, size_t pattern_len, | |
0f113f3e MC |
672 | const unsigned char *subject, size_t subject_len, |
673 | unsigned int flags) | |
674 | { | |
a773b52a | 675 | skip_prefix(&pattern, &pattern_len, subject_len, flags); |
0f113f3e MC |
676 | if (pattern_len != subject_len) |
677 | return 0; | |
678 | while (pattern_len) { | |
679 | unsigned char l = *pattern; | |
680 | unsigned char r = *subject; | |
681 | /* The pattern must not contain NUL characters. */ | |
682 | if (l == 0) | |
683 | return 0; | |
684 | if (l != r) { | |
685 | if ('A' <= l && l <= 'Z') | |
686 | l = (l - 'A') + 'a'; | |
687 | if ('A' <= r && r <= 'Z') | |
688 | r = (r - 'A') + 'a'; | |
689 | if (l != r) | |
690 | return 0; | |
691 | } | |
692 | ++pattern; | |
693 | ++subject; | |
694 | --pattern_len; | |
695 | } | |
696 | return 1; | |
697 | } | |
d88926f1 DSH |
698 | |
699 | /* Compare using memcmp. */ | |
700 | static int equal_case(const unsigned char *pattern, size_t pattern_len, | |
0f113f3e MC |
701 | const unsigned char *subject, size_t subject_len, |
702 | unsigned int flags) | |
d88926f1 | 703 | { |
a773b52a | 704 | skip_prefix(&pattern, &pattern_len, subject_len, flags); |
0f113f3e MC |
705 | if (pattern_len != subject_len) |
706 | return 0; | |
707 | return !memcmp(pattern, subject, pattern_len); | |
d88926f1 DSH |
708 | } |
709 | ||
0f113f3e MC |
710 | /* |
711 | * RFC 5280, section 7.5, requires that only the domain is compared in a | |
712 | * case-insensitive manner. | |
713 | */ | |
d88926f1 | 714 | static int equal_email(const unsigned char *a, size_t a_len, |
0f113f3e MC |
715 | const unsigned char *b, size_t b_len, |
716 | unsigned int unused_flags) | |
717 | { | |
718 | size_t i = a_len; | |
719 | if (a_len != b_len) | |
720 | return 0; | |
721 | /* | |
722 | * We search backwards for the '@' character, so that we do not have to | |
723 | * deal with quoted local-parts. The domain part is compared in a | |
724 | * case-insensitive manner. | |
725 | */ | |
726 | while (i > 0) { | |
727 | --i; | |
728 | if (a[i] == '@' || b[i] == '@') { | |
729 | if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) | |
730 | return 0; | |
731 | break; | |
732 | } | |
733 | } | |
734 | if (i == 0) | |
735 | i = a_len; | |
736 | return equal_case(a, i, b, i, 0); | |
737 | } | |
738 | ||
739 | /* | |
740 | * Compare the prefix and suffix with the subject, and check that the | |
741 | * characters in-between are valid. | |
742 | */ | |
d88926f1 | 743 | static int wildcard_match(const unsigned char *prefix, size_t prefix_len, |
0f113f3e MC |
744 | const unsigned char *suffix, size_t suffix_len, |
745 | const unsigned char *subject, size_t subject_len, | |
746 | unsigned int flags) | |
747 | { | |
748 | const unsigned char *wildcard_start; | |
749 | const unsigned char *wildcard_end; | |
750 | const unsigned char *p; | |
751 | int allow_multi = 0; | |
752 | int allow_idna = 0; | |
753 | ||
754 | if (subject_len < prefix_len + suffix_len) | |
755 | return 0; | |
756 | if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) | |
757 | return 0; | |
758 | wildcard_start = subject + prefix_len; | |
759 | wildcard_end = subject + (subject_len - suffix_len); | |
760 | if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) | |
761 | return 0; | |
762 | /* | |
763 | * If the wildcard makes up the entire first label, it must match at | |
764 | * least one character. | |
765 | */ | |
766 | if (prefix_len == 0 && *suffix == '.') { | |
767 | if (wildcard_start == wildcard_end) | |
768 | return 0; | |
769 | allow_idna = 1; | |
770 | if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS) | |
771 | allow_multi = 1; | |
772 | } | |
773 | /* IDNA labels cannot match partial wildcards */ | |
774 | if (!allow_idna && | |
775 | subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0) | |
776 | return 0; | |
777 | /* The wildcard may match a literal '*' */ | |
778 | if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') | |
779 | return 1; | |
780 | /* | |
781 | * Check that the part matched by the wildcard contains only | |
782 | * permitted characters and only matches a single label unless | |
783 | * allow_multi is set. | |
784 | */ | |
785 | for (p = wildcard_start; p != wildcard_end; ++p) | |
786 | if (!(('0' <= *p && *p <= '9') || | |
787 | ('A' <= *p && *p <= 'Z') || | |
788 | ('a' <= *p && *p <= 'z') || | |
789 | *p == '-' || (allow_multi && *p == '.'))) | |
790 | return 0; | |
791 | return 1; | |
792 | } | |
793 | ||
794 | #define LABEL_START (1 << 0) | |
795 | #define LABEL_END (1 << 1) | |
796 | #define LABEL_HYPHEN (1 << 2) | |
797 | #define LABEL_IDNA (1 << 3) | |
d88926f1 | 798 | |
397a8e74 | 799 | static const unsigned char *valid_star(const unsigned char *p, size_t len, |
0f113f3e MC |
800 | unsigned int flags) |
801 | { | |
802 | const unsigned char *star = 0; | |
803 | size_t i; | |
804 | int state = LABEL_START; | |
805 | int dots = 0; | |
806 | for (i = 0; i < len; ++i) { | |
807 | /* | |
808 | * Locate first and only legal wildcard, either at the start | |
809 | * or end of a non-IDNA first and not final label. | |
810 | */ | |
811 | if (p[i] == '*') { | |
812 | int atstart = (state & LABEL_START); | |
9a3bf973 | 813 | int atend = (i == len - 1 || p[i + 1] == '.'); |
35a1cc90 MC |
814 | /*- |
815 | * At most one wildcard per pattern. | |
816 | * No wildcards in IDNA labels. | |
817 | * No wildcards after the first label. | |
818 | */ | |
0f113f3e MC |
819 | if (star != NULL || (state & LABEL_IDNA) != 0 || dots) |
820 | return NULL; | |
821 | /* Only full-label '*.example.com' wildcards? */ | |
822 | if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS) | |
823 | && (!atstart || !atend)) | |
824 | return NULL; | |
825 | /* No 'foo*bar' wildcards */ | |
826 | if (!atstart && !atend) | |
827 | return NULL; | |
828 | star = &p[i]; | |
829 | state &= ~LABEL_START; | |
830 | } else if (('a' <= p[i] && p[i] <= 'z') | |
831 | || ('A' <= p[i] && p[i] <= 'Z') | |
832 | || ('0' <= p[i] && p[i] <= '9')) { | |
833 | if ((state & LABEL_START) != 0 | |
834 | && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0) | |
835 | state |= LABEL_IDNA; | |
836 | state &= ~(LABEL_HYPHEN | LABEL_START); | |
837 | } else if (p[i] == '.') { | |
838 | if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) | |
839 | return NULL; | |
840 | state = LABEL_START; | |
841 | ++dots; | |
842 | } else if (p[i] == '-') { | |
9f9a3926 ZL |
843 | /* no domain/subdomain starts with '-' */ |
844 | if ((state & LABEL_START) != 0) | |
0f113f3e MC |
845 | return NULL; |
846 | state |= LABEL_HYPHEN; | |
847 | } else | |
848 | return NULL; | |
849 | } | |
850 | ||
851 | /* | |
852 | * The final label must not end in a hyphen or ".", and | |
853 | * there must be at least two dots after the star. | |
854 | */ | |
855 | if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) | |
856 | return NULL; | |
857 | return star; | |
858 | } | |
d88926f1 DSH |
859 | |
860 | /* Compare using wildcards. */ | |
861 | static int equal_wildcard(const unsigned char *pattern, size_t pattern_len, | |
0f113f3e MC |
862 | const unsigned char *subject, size_t subject_len, |
863 | unsigned int flags) | |
864 | { | |
865 | const unsigned char *star = NULL; | |
866 | ||
867 | /* | |
868 | * Subject names starting with '.' can only match a wildcard pattern | |
869 | * via a subject sub-domain pattern suffix match. | |
870 | */ | |
871 | if (!(subject_len > 1 && subject[0] == '.')) | |
872 | star = valid_star(pattern, pattern_len, flags); | |
873 | if (star == NULL) | |
874 | return equal_nocase(pattern, pattern_len, | |
875 | subject, subject_len, flags); | |
876 | return wildcard_match(pattern, star - pattern, | |
877 | star + 1, (pattern + pattern_len) - star - 1, | |
878 | subject, subject_len, flags); | |
879 | } | |
880 | ||
881 | /* | |
882 | * Compare an ASN1_STRING to a supplied string. If they match return 1. If | |
883 | * cmp_type > 0 only compare if string matches the type, otherwise convert it | |
884 | * to UTF8. | |
a70da5b3 DSH |
885 | */ |
886 | ||
d88926f1 | 887 | static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal, |
0f113f3e MC |
888 | unsigned int flags, const char *b, size_t blen, |
889 | char **peername) | |
890 | { | |
891 | int rv = 0; | |
892 | ||
893 | if (!a->data || !a->length) | |
894 | return 0; | |
895 | if (cmp_type > 0) { | |
896 | if (cmp_type != a->type) | |
897 | return 0; | |
898 | if (cmp_type == V_ASN1_IA5STRING) | |
899 | rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); | |
900 | else if (a->length == (int)blen && !memcmp(a->data, b, blen)) | |
901 | rv = 1; | |
902 | if (rv > 0 && peername) | |
7644a9ae | 903 | *peername = OPENSSL_strndup((char *)a->data, a->length); |
0f113f3e MC |
904 | } else { |
905 | int astrlen; | |
906 | unsigned char *astr; | |
907 | astrlen = ASN1_STRING_to_UTF8(&astr, a); | |
0923e7df EK |
908 | if (astrlen < 0) { |
909 | /* | |
910 | * -1 could be an internal malloc failure or a decoding error from | |
911 | * malformed input; we can't distinguish. | |
912 | */ | |
0f113f3e | 913 | return -1; |
0923e7df | 914 | } |
0f113f3e MC |
915 | rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); |
916 | if (rv > 0 && peername) | |
7644a9ae | 917 | *peername = OPENSSL_strndup((char *)astr, astrlen); |
0f113f3e MC |
918 | OPENSSL_free(astr); |
919 | } | |
920 | return rv; | |
921 | } | |
a70da5b3 | 922 | |
297c67fc | 923 | static int do_x509_check(X509 *x, const char *chk, size_t chklen, |
0f113f3e MC |
924 | unsigned int flags, int check_type, char **peername) |
925 | { | |
926 | GENERAL_NAMES *gens = NULL; | |
927 | X509_NAME *name = NULL; | |
928 | int i; | |
fffc2fae | 929 | int cnid = NID_undef; |
0f113f3e MC |
930 | int alt_type; |
931 | int san_present = 0; | |
932 | int rv = 0; | |
933 | equal_fn equal; | |
934 | ||
935 | /* See below, this flag is internal-only */ | |
936 | flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS; | |
937 | if (check_type == GEN_EMAIL) { | |
938 | cnid = NID_pkcs9_emailAddress; | |
939 | alt_type = V_ASN1_IA5STRING; | |
940 | equal = equal_email; | |
941 | } else if (check_type == GEN_DNS) { | |
942 | cnid = NID_commonName; | |
943 | /* Implicit client-side DNS sub-domain pattern */ | |
944 | if (chklen > 1 && chk[0] == '.') | |
945 | flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS; | |
946 | alt_type = V_ASN1_IA5STRING; | |
947 | if (flags & X509_CHECK_FLAG_NO_WILDCARDS) | |
948 | equal = equal_nocase; | |
949 | else | |
950 | equal = equal_wildcard; | |
951 | } else { | |
0f113f3e MC |
952 | alt_type = V_ASN1_OCTET_STRING; |
953 | equal = equal_case; | |
954 | } | |
955 | ||
956 | if (chklen == 0) | |
957 | chklen = strlen(chk); | |
958 | ||
959 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); | |
960 | if (gens) { | |
961 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
962 | GENERAL_NAME *gen; | |
963 | ASN1_STRING *cstr; | |
964 | gen = sk_GENERAL_NAME_value(gens, i); | |
965 | if (gen->type != check_type) | |
966 | continue; | |
967 | san_present = 1; | |
968 | if (check_type == GEN_EMAIL) | |
969 | cstr = gen->d.rfc822Name; | |
970 | else if (check_type == GEN_DNS) | |
971 | cstr = gen->d.dNSName; | |
972 | else | |
973 | cstr = gen->d.iPAddress; | |
974 | /* Positive on success, negative on error! */ | |
975 | if ((rv = do_check_string(cstr, alt_type, equal, flags, | |
976 | chk, chklen, peername)) != 0) | |
977 | break; | |
978 | } | |
979 | GENERAL_NAMES_free(gens); | |
980 | if (rv != 0) | |
981 | return rv; | |
dd60efea | 982 | if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)) |
0f113f3e MC |
983 | return 0; |
984 | } | |
fffc2fae VD |
985 | |
986 | /* We're done if CN-ID is not pertinent */ | |
dd60efea | 987 | if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT)) |
fffc2fae VD |
988 | return 0; |
989 | ||
0f113f3e MC |
990 | i = -1; |
991 | name = X509_get_subject_name(x); | |
992 | while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) { | |
993 | X509_NAME_ENTRY *ne; | |
994 | ASN1_STRING *str; | |
995 | ne = X509_NAME_get_entry(name, i); | |
996 | str = X509_NAME_ENTRY_get_data(ne); | |
997 | /* Positive on success, negative on error! */ | |
998 | if ((rv = do_check_string(str, -1, equal, flags, | |
999 | chk, chklen, peername)) != 0) | |
1000 | return rv; | |
1001 | } | |
1002 | return 0; | |
1003 | } | |
a70da5b3 | 1004 | |
297c67fc | 1005 | int X509_check_host(X509 *x, const char *chk, size_t chklen, |
0f113f3e MC |
1006 | unsigned int flags, char **peername) |
1007 | { | |
1008 | if (chk == NULL) | |
1009 | return -2; | |
1010 | /* | |
1011 | * Embedded NULs are disallowed, except as the last character of a | |
1012 | * string of length 2 or more (tolerate caller including terminating | |
1013 | * NUL in string length). | |
1014 | */ | |
1015 | if (chklen == 0) | |
1016 | chklen = strlen(chk); | |
1017 | else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) | |
1018 | return -2; | |
1019 | if (chklen > 1 && chk[chklen - 1] == '\0') | |
1020 | --chklen; | |
1021 | return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername); | |
1022 | } | |
a70da5b3 | 1023 | |
297c67fc | 1024 | int X509_check_email(X509 *x, const char *chk, size_t chklen, |
0f113f3e MC |
1025 | unsigned int flags) |
1026 | { | |
1027 | if (chk == NULL) | |
1028 | return -2; | |
1029 | /* | |
1030 | * Embedded NULs are disallowed, except as the last character of a | |
1031 | * string of length 2 or more (tolerate caller including terminating | |
1032 | * NUL in string length). | |
1033 | */ | |
1034 | if (chklen == 0) | |
1035 | chklen = strlen((char *)chk); | |
1036 | else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) | |
1037 | return -2; | |
1038 | if (chklen > 1 && chk[chklen - 1] == '\0') | |
1039 | --chklen; | |
1040 | return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL); | |
1041 | } | |
a70da5b3 DSH |
1042 | |
1043 | int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, | |
0f113f3e MC |
1044 | unsigned int flags) |
1045 | { | |
1046 | if (chk == NULL) | |
1047 | return -2; | |
1048 | return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL); | |
1049 | } | |
a70da5b3 DSH |
1050 | |
1051 | int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags) | |
0f113f3e MC |
1052 | { |
1053 | unsigned char ipout[16]; | |
1054 | size_t iplen; | |
1055 | ||
1056 | if (ipasc == NULL) | |
1057 | return -2; | |
1058 | iplen = (size_t)a2i_ipadd(ipout, ipasc); | |
1059 | if (iplen == 0) | |
1060 | return -2; | |
1061 | return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL); | |
1062 | } | |
1063 | ||
1064 | /* | |
1065 | * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible | |
1066 | * with RFC3280. | |
4e5d3a7f DSH |
1067 | */ |
1068 | ||
1069 | ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc) | |
0f113f3e MC |
1070 | { |
1071 | unsigned char ipout[16]; | |
1072 | ASN1_OCTET_STRING *ret; | |
1073 | int iplen; | |
4e5d3a7f | 1074 | |
0f113f3e | 1075 | /* If string contains a ':' assume IPv6 */ |
4e5d3a7f | 1076 | |
0f113f3e | 1077 | iplen = a2i_ipadd(ipout, ipasc); |
520b76ff | 1078 | |
0f113f3e MC |
1079 | if (!iplen) |
1080 | return NULL; | |
4e5d3a7f | 1081 | |
0f113f3e | 1082 | ret = ASN1_OCTET_STRING_new(); |
90945fa3 | 1083 | if (ret == NULL) |
0f113f3e MC |
1084 | return NULL; |
1085 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) { | |
1086 | ASN1_OCTET_STRING_free(ret); | |
1087 | return NULL; | |
1088 | } | |
1089 | return ret; | |
1090 | } | |
4e5d3a7f | 1091 | |
520b76ff | 1092 | ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) |
0f113f3e MC |
1093 | { |
1094 | ASN1_OCTET_STRING *ret = NULL; | |
1095 | unsigned char ipout[32]; | |
1096 | char *iptmp = NULL, *p; | |
1097 | int iplen1, iplen2; | |
1098 | p = strchr(ipasc, '/'); | |
1099 | if (!p) | |
1100 | return NULL; | |
7644a9ae | 1101 | iptmp = OPENSSL_strdup(ipasc); |
0f113f3e MC |
1102 | if (!iptmp) |
1103 | return NULL; | |
1104 | p = iptmp + (p - ipasc); | |
1105 | *p++ = 0; | |
1106 | ||
1107 | iplen1 = a2i_ipadd(ipout, iptmp); | |
1108 | ||
1109 | if (!iplen1) | |
1110 | goto err; | |
1111 | ||
1112 | iplen2 = a2i_ipadd(ipout + iplen1, p); | |
1113 | ||
1114 | OPENSSL_free(iptmp); | |
1115 | iptmp = NULL; | |
1116 | ||
1117 | if (!iplen2 || (iplen1 != iplen2)) | |
1118 | goto err; | |
1119 | ||
1120 | ret = ASN1_OCTET_STRING_new(); | |
90945fa3 | 1121 | if (ret == NULL) |
0f113f3e MC |
1122 | goto err; |
1123 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2)) | |
1124 | goto err; | |
1125 | ||
1126 | return ret; | |
1127 | ||
1128 | err: | |
b548a1f1 | 1129 | OPENSSL_free(iptmp); |
2ace7450 | 1130 | ASN1_OCTET_STRING_free(ret); |
0f113f3e MC |
1131 | return NULL; |
1132 | } | |
520b76ff | 1133 | |
96ea4ae9 | 1134 | int a2i_ipadd(unsigned char *ipout, const char *ipasc) |
0f113f3e MC |
1135 | { |
1136 | /* If string contains a ':' assume IPv6 */ | |
1137 | ||
1138 | if (strchr(ipasc, ':')) { | |
1139 | if (!ipv6_from_asc(ipout, ipasc)) | |
1140 | return 0; | |
1141 | return 16; | |
1142 | } else { | |
1143 | if (!ipv4_from_asc(ipout, ipasc)) | |
1144 | return 0; | |
1145 | return 4; | |
1146 | } | |
1147 | } | |
520b76ff | 1148 | |
4e5d3a7f | 1149 | static int ipv4_from_asc(unsigned char *v4, const char *in) |
0f113f3e MC |
1150 | { |
1151 | int a0, a1, a2, a3; | |
1152 | if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4) | |
1153 | return 0; | |
1154 | if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255) | |
1155 | || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255)) | |
1156 | return 0; | |
1157 | v4[0] = a0; | |
1158 | v4[1] = a1; | |
1159 | v4[2] = a2; | |
1160 | v4[3] = a3; | |
1161 | return 1; | |
1162 | } | |
4e5d3a7f DSH |
1163 | |
1164 | typedef struct { | |
0f113f3e MC |
1165 | /* Temporary store for IPV6 output */ |
1166 | unsigned char tmp[16]; | |
1167 | /* Total number of bytes in tmp */ | |
1168 | int total; | |
1169 | /* The position of a zero (corresponding to '::') */ | |
1170 | int zero_pos; | |
1171 | /* Number of zeroes */ | |
1172 | int zero_cnt; | |
1173 | } IPV6_STAT; | |
4e5d3a7f DSH |
1174 | |
1175 | static int ipv6_from_asc(unsigned char *v6, const char *in) | |
0f113f3e MC |
1176 | { |
1177 | IPV6_STAT v6stat; | |
1178 | v6stat.total = 0; | |
1179 | v6stat.zero_pos = -1; | |
1180 | v6stat.zero_cnt = 0; | |
1181 | /* | |
1182 | * Treat the IPv6 representation as a list of values separated by ':'. | |
1183 | * The presence of a '::' will parse as one, two or three zero length | |
1184 | * elements. | |
1185 | */ | |
1186 | if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) | |
1187 | return 0; | |
1188 | ||
1189 | /* Now for some sanity checks */ | |
1190 | ||
1191 | if (v6stat.zero_pos == -1) { | |
1192 | /* If no '::' must have exactly 16 bytes */ | |
1193 | if (v6stat.total != 16) | |
1194 | return 0; | |
1195 | } else { | |
1196 | /* If '::' must have less than 16 bytes */ | |
1197 | if (v6stat.total == 16) | |
1198 | return 0; | |
1199 | /* More than three zeroes is an error */ | |
1200 | if (v6stat.zero_cnt > 3) | |
1201 | return 0; | |
1202 | /* Can only have three zeroes if nothing else present */ | |
1203 | else if (v6stat.zero_cnt == 3) { | |
1204 | if (v6stat.total > 0) | |
1205 | return 0; | |
1206 | } | |
1207 | /* Can only have two zeroes if at start or end */ | |
1208 | else if (v6stat.zero_cnt == 2) { | |
1209 | if ((v6stat.zero_pos != 0) | |
1210 | && (v6stat.zero_pos != v6stat.total)) | |
1211 | return 0; | |
1212 | } else | |
1213 | /* Can only have one zero if *not* start or end */ | |
1214 | { | |
1215 | if ((v6stat.zero_pos == 0) | |
1216 | || (v6stat.zero_pos == v6stat.total)) | |
1217 | return 0; | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | /* Format result */ | |
1222 | ||
1223 | if (v6stat.zero_pos >= 0) { | |
1224 | /* Copy initial part */ | |
1225 | memcpy(v6, v6stat.tmp, v6stat.zero_pos); | |
1226 | /* Zero middle */ | |
1227 | memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); | |
1228 | /* Copy final part */ | |
1229 | if (v6stat.total != v6stat.zero_pos) | |
1230 | memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, | |
1231 | v6stat.tmp + v6stat.zero_pos, | |
1232 | v6stat.total - v6stat.zero_pos); | |
1233 | } else | |
1234 | memcpy(v6, v6stat.tmp, 16); | |
1235 | ||
1236 | return 1; | |
1237 | } | |
4e5d3a7f DSH |
1238 | |
1239 | static int ipv6_cb(const char *elem, int len, void *usr) | |
0f113f3e MC |
1240 | { |
1241 | IPV6_STAT *s = usr; | |
1242 | /* Error if 16 bytes written */ | |
1243 | if (s->total == 16) | |
1244 | return 0; | |
1245 | if (len == 0) { | |
1246 | /* Zero length element, corresponds to '::' */ | |
1247 | if (s->zero_pos == -1) | |
1248 | s->zero_pos = s->total; | |
1249 | /* If we've already got a :: its an error */ | |
1250 | else if (s->zero_pos != s->total) | |
1251 | return 0; | |
1252 | s->zero_cnt++; | |
1253 | } else { | |
1254 | /* If more than 4 characters could be final a.b.c.d form */ | |
1255 | if (len > 4) { | |
1256 | /* Need at least 4 bytes left */ | |
1257 | if (s->total > 12) | |
1258 | return 0; | |
1259 | /* Must be end of string */ | |
1260 | if (elem[len]) | |
1261 | return 0; | |
1262 | if (!ipv4_from_asc(s->tmp + s->total, elem)) | |
1263 | return 0; | |
1264 | s->total += 4; | |
1265 | } else { | |
1266 | if (!ipv6_hex(s->tmp + s->total, elem, len)) | |
1267 | return 0; | |
1268 | s->total += 2; | |
1269 | } | |
1270 | } | |
1271 | return 1; | |
1272 | } | |
1273 | ||
1274 | /* | |
1275 | * Convert a string of up to 4 hex digits into the corresponding IPv6 form. | |
4e5d3a7f DSH |
1276 | */ |
1277 | ||
1278 | static int ipv6_hex(unsigned char *out, const char *in, int inlen) | |
0f113f3e MC |
1279 | { |
1280 | unsigned char c; | |
1281 | unsigned int num = 0; | |
1282 | if (inlen > 4) | |
1283 | return 0; | |
1284 | while (inlen--) { | |
1285 | c = *in++; | |
1286 | num <<= 4; | |
1287 | if ((c >= '0') && (c <= '9')) | |
1288 | num |= c - '0'; | |
1289 | else if ((c >= 'A') && (c <= 'F')) | |
1290 | num |= c - 'A' + 10; | |
1291 | else if ((c >= 'a') && (c <= 'f')) | |
1292 | num |= c - 'a' + 10; | |
1293 | else | |
1294 | return 0; | |
1295 | } | |
1296 | out[0] = num >> 8; | |
1297 | out[1] = num & 0xff; | |
1298 | return 1; | |
1299 | } | |
f0dc08e6 | 1300 | |
a7b1eed5 | 1301 | int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk, |
0f113f3e MC |
1302 | unsigned long chtype) |
1303 | { | |
1304 | CONF_VALUE *v; | |
b5292f7b | 1305 | int i, mval, spec_char, plus_char; |
0f113f3e MC |
1306 | char *p, *type; |
1307 | if (!nm) | |
1308 | return 0; | |
1309 | ||
1310 | for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { | |
1311 | v = sk_CONF_VALUE_value(dn_sk, i); | |
1312 | type = v->name; | |
1313 | /* | |
1314 | * Skip past any leading X. X: X, etc to allow for multiple instances | |
1315 | */ | |
b5292f7b | 1316 | for (p = type; *p; p++) { |
f0dc08e6 | 1317 | #ifndef CHARSET_EBCDIC |
b5292f7b | 1318 | spec_char = ((*p == ':') || (*p == ',') || (*p == '.')); |
f0dc08e6 | 1319 | #else |
b5292f7b FM |
1320 | spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[',']) |
1321 | || (*p == os_toascii['.'])); | |
f0dc08e6 | 1322 | #endif |
b5292f7b | 1323 | if (spec_char) { |
0f113f3e MC |
1324 | p++; |
1325 | if (*p) | |
1326 | type = p; | |
1327 | break; | |
1328 | } | |
b5292f7b | 1329 | } |
1a15c899 | 1330 | #ifndef CHARSET_EBCDIC |
b5292f7b | 1331 | plus_char = (*type == '+'); |
1a15c899 | 1332 | #else |
b5292f7b | 1333 | plus_char = (*type == os_toascii['+']); |
1a15c899 | 1334 | #endif |
b5292f7b | 1335 | if (plus_char) { |
0f113f3e MC |
1336 | mval = -1; |
1337 | type++; | |
1338 | } else | |
1339 | mval = 0; | |
1340 | if (!X509_NAME_add_entry_by_txt(nm, type, chtype, | |
1341 | (unsigned char *)v->value, -1, -1, | |
1342 | mval)) | |
1343 | return 0; | |
1344 | ||
1345 | } | |
1346 | return 1; | |
1347 | } |