]>
Commit | Line | Data |
---|---|---|
9aeaf1b4 | 1 | /* v3_utl.c */ |
2e597528 | 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 | |
13 | * notice, this list of conditions and the following disclaimer. | |
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 DSH |
60 | |
61 | #include <stdio.h> | |
9aeaf1b4 | 62 | #include <ctype.h> |
e527ba09 | 63 | #include "cryptlib.h" |
ec577822 BM |
64 | #include <openssl/conf.h> |
65 | #include <openssl/x509v3.h> | |
0f814687 | 66 | #include <openssl/bn.h> |
9aeaf1b4 | 67 | |
9aeaf1b4 | 68 | static char *strip_spaces(char *name); |
a91dedca | 69 | static int sk_strcmp(const char * const *a, const char * const *b); |
c869da88 DSH |
70 | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, GENERAL_NAMES *gens); |
71 | static void str_free(OPENSSL_STRING str); | |
72 | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email); | |
9aeaf1b4 | 73 | |
4e5d3a7f DSH |
74 | static int ipv4_from_asc(unsigned char *v4, const char *in); |
75 | static int ipv6_from_asc(unsigned char *v6, const char *in); | |
76 | static int ipv6_cb(const char *elem, int len, void *usr); | |
77 | static int ipv6_hex(unsigned char *out, const char *in, int inlen); | |
78 | ||
9aeaf1b4 DSH |
79 | /* Add a CONF_VALUE name value pair to stack */ |
80 | ||
ba404b5e DSH |
81 | int X509V3_add_value(const char *name, const char *value, |
82 | STACK_OF(CONF_VALUE) **extlist) | |
9aeaf1b4 DSH |
83 | { |
84 | CONF_VALUE *vtmp = NULL; | |
85 | char *tname = NULL, *tvalue = NULL; | |
9985bed3 | 86 | if(name && !(tname = BUF_strdup(name))) goto err; |
477fd459 | 87 | if(value && !(tvalue = BUF_strdup(value))) goto err; |
26a3a48d | 88 | if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err; |
62324627 | 89 | if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err; |
9aeaf1b4 DSH |
90 | vtmp->section = NULL; |
91 | vtmp->name = tname; | |
92 | vtmp->value = tvalue; | |
ba404b5e | 93 | if(!sk_CONF_VALUE_push(*extlist, vtmp)) goto err; |
9aeaf1b4 DSH |
94 | return 1; |
95 | err: | |
96 | X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE); | |
26a3a48d RL |
97 | if(vtmp) OPENSSL_free(vtmp); |
98 | if(tname) OPENSSL_free(tname); | |
99 | if(tvalue) OPENSSL_free(tvalue); | |
9aeaf1b4 DSH |
100 | return 0; |
101 | } | |
102 | ||
61f5b6f3 | 103 | int X509V3_add_value_uchar(const char *name, const unsigned char *value, |
ba404b5e | 104 | STACK_OF(CONF_VALUE) **extlist) |
61f5b6f3 BL |
105 | { |
106 | return X509V3_add_value(name,(const char *)value,extlist); | |
107 | } | |
108 | ||
66ab08b1 | 109 | /* Free function for STACK_OF(CONF_VALUE) */ |
9aeaf1b4 | 110 | |
6b691a5c | 111 | void X509V3_conf_free(CONF_VALUE *conf) |
9aeaf1b4 DSH |
112 | { |
113 | if(!conf) return; | |
26a3a48d RL |
114 | if(conf->name) OPENSSL_free(conf->name); |
115 | if(conf->value) OPENSSL_free(conf->value); | |
116 | if(conf->section) OPENSSL_free(conf->section); | |
117 | OPENSSL_free(conf); | |
9aeaf1b4 DSH |
118 | } |
119 | ||
ba404b5e DSH |
120 | int X509V3_add_value_bool(const char *name, int asn1_bool, |
121 | STACK_OF(CONF_VALUE) **extlist) | |
9aeaf1b4 DSH |
122 | { |
123 | if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist); | |
124 | return X509V3_add_value(name, "FALSE", extlist); | |
125 | } | |
126 | ||
ba404b5e DSH |
127 | int X509V3_add_value_bool_nf(char *name, int asn1_bool, |
128 | STACK_OF(CONF_VALUE) **extlist) | |
9aeaf1b4 DSH |
129 | { |
130 | if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist); | |
131 | return 1; | |
132 | } | |
133 | ||
c74f1eb9 | 134 | |
6b691a5c | 135 | char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a) |
c74f1eb9 DSH |
136 | { |
137 | BIGNUM *bntmp = NULL; | |
138 | char *strtmp = NULL; | |
139 | if(!a) return NULL; | |
140 | if(!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) || | |
141 | !(strtmp = BN_bn2dec(bntmp)) ) | |
142 | X509V3err(X509V3_F_I2S_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE); | |
143 | BN_free(bntmp); | |
144 | return strtmp; | |
145 | } | |
146 | ||
6b691a5c | 147 | char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a) |
0ca5f8b1 DSH |
148 | { |
149 | BIGNUM *bntmp = NULL; | |
150 | char *strtmp = NULL; | |
151 | if(!a) return NULL; | |
152 | if(!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) || | |
153 | !(strtmp = BN_bn2dec(bntmp)) ) | |
154 | X509V3err(X509V3_F_I2S_ASN1_INTEGER,ERR_R_MALLOC_FAILURE); | |
155 | BN_free(bntmp); | |
156 | return strtmp; | |
157 | } | |
158 | ||
6b691a5c | 159 | ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value) |
28a98809 DSH |
160 | { |
161 | BIGNUM *bn = NULL; | |
162 | ASN1_INTEGER *aint; | |
8e8972bb DSH |
163 | int isneg, ishex; |
164 | int ret; | |
8e8972bb | 165 | if (!value) { |
28a98809 DSH |
166 | X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_INVALID_NULL_VALUE); |
167 | return 0; | |
168 | } | |
2232e262 | 169 | bn = BN_new(); |
8e8972bb DSH |
170 | if (value[0] == '-') { |
171 | value++; | |
172 | isneg = 1; | |
173 | } else isneg = 0; | |
174 | ||
175 | if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { | |
176 | value += 2; | |
177 | ishex = 1; | |
178 | } else ishex = 0; | |
179 | ||
180 | if (ishex) ret = BN_hex2bn(&bn, value); | |
181 | else ret = BN_dec2bn(&bn, value); | |
182 | ||
2232e262 DSH |
183 | if (!ret || value[ret]) { |
184 | BN_free(bn); | |
28a98809 DSH |
185 | X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_DEC2BN_ERROR); |
186 | return 0; | |
187 | } | |
188 | ||
8e8972bb DSH |
189 | if (isneg && BN_is_zero(bn)) isneg = 0; |
190 | ||
191 | aint = BN_to_ASN1_INTEGER(bn, NULL); | |
192 | BN_free(bn); | |
193 | if (!aint) { | |
28a98809 DSH |
194 | X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_TO_ASN1_INTEGER_ERROR); |
195 | return 0; | |
196 | } | |
8e8972bb | 197 | if (isneg) aint->type |= V_ASN1_NEG; |
28a98809 DSH |
198 | return aint; |
199 | } | |
200 | ||
6b691a5c | 201 | int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint, |
ba404b5e | 202 | STACK_OF(CONF_VALUE) **extlist) |
9aeaf1b4 | 203 | { |
9aeaf1b4 DSH |
204 | char *strtmp; |
205 | int ret; | |
206 | if(!aint) return 1; | |
0ca5f8b1 | 207 | if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0; |
9aeaf1b4 | 208 | ret = X509V3_add_value(name, strtmp, extlist); |
26a3a48d | 209 | OPENSSL_free(strtmp); |
9aeaf1b4 DSH |
210 | return ret; |
211 | } | |
212 | ||
6b691a5c | 213 | int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool) |
9aeaf1b4 DSH |
214 | { |
215 | char *btmp; | |
216 | if(!(btmp = value->value)) goto err; | |
217 | if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true") | |
218 | || !strcmp(btmp, "Y") || !strcmp(btmp, "y") | |
219 | || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) { | |
220 | *asn1_bool = 0xff; | |
221 | return 1; | |
222 | } else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false") | |
223 | || !strcmp(btmp, "N") || !strcmp(btmp, "n") | |
224 | || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) { | |
225 | *asn1_bool = 0; | |
226 | return 1; | |
227 | } | |
228 | err: | |
28a98809 | 229 | X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,X509V3_R_INVALID_BOOLEAN_STRING); |
9aeaf1b4 DSH |
230 | X509V3_conf_err(value); |
231 | return 0; | |
232 | } | |
233 | ||
6b691a5c | 234 | int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint) |
9aeaf1b4 | 235 | { |
28a98809 DSH |
236 | ASN1_INTEGER *itmp; |
237 | if(!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) { | |
9aeaf1b4 DSH |
238 | X509V3_conf_err(value); |
239 | return 0; | |
240 | } | |
28a98809 | 241 | *aint = itmp; |
9aeaf1b4 DSH |
242 | return 1; |
243 | } | |
244 | ||
245 | #define HDR_NAME 1 | |
246 | #define HDR_VALUE 2 | |
247 | ||
248 | /*#define DEBUG*/ | |
249 | ||
535d79da | 250 | STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line) |
9aeaf1b4 DSH |
251 | { |
252 | char *p, *q, c; | |
253 | char *ntmp, *vtmp; | |
ba404b5e | 254 | STACK_OF(CONF_VALUE) *values = NULL; |
9aeaf1b4 DSH |
255 | char *linebuf; |
256 | int state; | |
257 | /* We are going to modify the line so copy it first */ | |
9985bed3 | 258 | linebuf = BUF_strdup(line); |
9aeaf1b4 DSH |
259 | state = HDR_NAME; |
260 | ntmp = NULL; | |
261 | /* Go through all characters */ | |
262 | for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { | |
263 | ||
264 | switch(state) { | |
265 | case HDR_NAME: | |
266 | if(c == ':') { | |
267 | state = HDR_VALUE; | |
268 | *p = 0; | |
269 | ntmp = strip_spaces(q); | |
270 | if(!ntmp) { | |
271 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME); | |
272 | goto err; | |
273 | } | |
274 | q = p + 1; | |
275 | } else if(c == ',') { | |
276 | *p = 0; | |
277 | ntmp = strip_spaces(q); | |
278 | q = p + 1; | |
59dbdb51 | 279 | #if 0 |
9aeaf1b4 DSH |
280 | printf("%s\n", ntmp); |
281 | #endif | |
282 | if(!ntmp) { | |
283 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME); | |
284 | goto err; | |
285 | } | |
286 | X509V3_add_value(ntmp, NULL, &values); | |
287 | } | |
288 | break ; | |
289 | ||
290 | case HDR_VALUE: | |
291 | if(c == ',') { | |
292 | state = HDR_NAME; | |
293 | *p = 0; | |
294 | vtmp = strip_spaces(q); | |
59dbdb51 | 295 | #if 0 |
9aeaf1b4 DSH |
296 | printf("%s\n", ntmp); |
297 | #endif | |
298 | if(!vtmp) { | |
299 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE); | |
300 | goto err; | |
301 | } | |
302 | X509V3_add_value(ntmp, vtmp, &values); | |
303 | ntmp = NULL; | |
304 | q = p + 1; | |
305 | } | |
306 | ||
307 | } | |
308 | } | |
309 | ||
310 | if(state == HDR_VALUE) { | |
311 | vtmp = strip_spaces(q); | |
59dbdb51 | 312 | #if 0 |
9aeaf1b4 DSH |
313 | printf("%s=%s\n", ntmp, vtmp); |
314 | #endif | |
315 | if(!vtmp) { | |
316 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE); | |
317 | goto err; | |
318 | } | |
319 | X509V3_add_value(ntmp, vtmp, &values); | |
320 | } else { | |
321 | ntmp = strip_spaces(q); | |
59dbdb51 | 322 | #if 0 |
9aeaf1b4 DSH |
323 | printf("%s\n", ntmp); |
324 | #endif | |
325 | if(!ntmp) { | |
326 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME); | |
327 | goto err; | |
328 | } | |
329 | X509V3_add_value(ntmp, NULL, &values); | |
330 | } | |
26a3a48d | 331 | OPENSSL_free(linebuf); |
9aeaf1b4 DSH |
332 | return values; |
333 | ||
334 | err: | |
26a3a48d | 335 | OPENSSL_free(linebuf); |
ba404b5e | 336 | sk_CONF_VALUE_pop_free(values, X509V3_conf_free); |
9aeaf1b4 DSH |
337 | return NULL; |
338 | ||
339 | } | |
340 | ||
341 | /* Delete leading and trailing spaces from a string */ | |
6b691a5c | 342 | static char *strip_spaces(char *name) |
9aeaf1b4 DSH |
343 | { |
344 | char *p, *q; | |
345 | /* Skip over leading spaces */ | |
346 | p = name; | |
84a370a4 | 347 | while(*p && isspace((unsigned char)*p)) p++; |
9aeaf1b4 DSH |
348 | if(!*p) return NULL; |
349 | q = p + strlen(p) - 1; | |
84a370a4 | 350 | while((q != p) && isspace((unsigned char)*q)) q--; |
9aeaf1b4 DSH |
351 | if(p != q) q[1] = 0; |
352 | if(!*p) return NULL; | |
353 | return p; | |
354 | } | |
175b0942 DSH |
355 | |
356 | /* hex string utilities */ | |
357 | ||
26a3a48d | 358 | /* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its |
175b0942 | 359 | * hex representation |
97d8e82c | 360 | * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines) |
175b0942 DSH |
361 | */ |
362 | ||
376bf1d4 | 363 | char *hex_to_string(const unsigned char *buffer, long len) |
175b0942 DSH |
364 | { |
365 | char *tmp, *q; | |
376bf1d4 | 366 | const unsigned char *p; |
175b0942 | 367 | int i; |
af32f9fd | 368 | const static char hexdig[] = "0123456789ABCDEF"; |
175b0942 | 369 | if(!buffer || !len) return NULL; |
26a3a48d | 370 | if(!(tmp = OPENSSL_malloc(len * 3 + 1))) { |
175b0942 DSH |
371 | X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE); |
372 | return NULL; | |
373 | } | |
374 | q = tmp; | |
375 | for(i = 0, p = buffer; i < len; i++,p++) { | |
376 | *q++ = hexdig[(*p >> 4) & 0xf]; | |
377 | *q++ = hexdig[*p & 0xf]; | |
378 | *q++ = ':'; | |
379 | } | |
380 | q[-1] = 0; | |
97d8e82c RL |
381 | #ifdef CHARSET_EBCDIC |
382 | ebcdic2ascii(tmp, tmp, q - tmp - 1); | |
383 | #endif | |
384 | ||
175b0942 DSH |
385 | return tmp; |
386 | } | |
387 | ||
388 | /* Give a string of hex digits convert to | |
389 | * a buffer | |
390 | */ | |
391 | ||
376bf1d4 | 392 | unsigned char *string_to_hex(const char *str, long *len) |
175b0942 DSH |
393 | { |
394 | unsigned char *hexbuf, *q; | |
395 | unsigned char ch, cl, *p; | |
396 | if(!str) { | |
397 | X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT); | |
398 | return NULL; | |
399 | } | |
26a3a48d | 400 | if(!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) goto err; |
175b0942 DSH |
401 | for(p = (unsigned char *)str, q = hexbuf; *p;) { |
402 | ch = *p++; | |
97d8e82c RL |
403 | #ifdef CHARSET_EBCDIC |
404 | ch = os_toebcdic[ch]; | |
405 | #endif | |
175b0942 DSH |
406 | if(ch == ':') continue; |
407 | cl = *p++; | |
97d8e82c RL |
408 | #ifdef CHARSET_EBCDIC |
409 | cl = os_toebcdic[cl]; | |
410 | #endif | |
175b0942 DSH |
411 | if(!cl) { |
412 | X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS); | |
26a3a48d | 413 | OPENSSL_free(hexbuf); |
175b0942 DSH |
414 | return NULL; |
415 | } | |
416 | if(isupper(ch)) ch = tolower(ch); | |
417 | if(isupper(cl)) cl = tolower(cl); | |
418 | ||
419 | if((ch >= '0') && (ch <= '9')) ch -= '0'; | |
420 | else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10; | |
421 | else goto badhex; | |
422 | ||
423 | if((cl >= '0') && (cl <= '9')) cl -= '0'; | |
424 | else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10; | |
425 | else goto badhex; | |
426 | ||
427 | *q++ = (ch << 4) | cl; | |
428 | } | |
429 | ||
430 | if(len) *len = q - hexbuf; | |
431 | ||
432 | return hexbuf; | |
433 | ||
434 | err: | |
26a3a48d | 435 | if(hexbuf) OPENSSL_free(hexbuf); |
175b0942 DSH |
436 | X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE); |
437 | return NULL; | |
438 | ||
439 | badhex: | |
26a3a48d | 440 | OPENSSL_free(hexbuf); |
175b0942 DSH |
441 | X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT); |
442 | return NULL; | |
443 | ||
444 | } | |
d08d8da4 DSH |
445 | |
446 | /* V2I name comparison function: returns zero if 'name' matches | |
447 | * cmp or cmp.* | |
448 | */ | |
449 | ||
6b691a5c | 450 | int name_cmp(const char *name, const char *cmp) |
d08d8da4 DSH |
451 | { |
452 | int len, ret; | |
453 | char c; | |
454 | len = strlen(cmp); | |
455 | if((ret = strncmp(name, cmp, len))) return ret; | |
456 | c = name[len]; | |
457 | if(!c || (c=='.')) return 0; | |
458 | return 1; | |
459 | } | |
a91dedca DSH |
460 | |
461 | static int sk_strcmp(const char * const *a, const char * const *b) | |
462 | { | |
463 | return strcmp(*a, *b); | |
464 | } | |
465 | ||
c869da88 | 466 | STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) |
a91dedca | 467 | { |
9d6b1ce6 | 468 | GENERAL_NAMES *gens; |
c869da88 | 469 | STACK_OF(OPENSSL_STRING) *ret; |
5ce278a7 | 470 | |
a91dedca DSH |
471 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
472 | ret = get_email(X509_get_subject_name(x), gens); | |
473 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | |
474 | return ret; | |
475 | } | |
476 | ||
c869da88 | 477 | STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) |
67c8e7f4 DSH |
478 | { |
479 | AUTHORITY_INFO_ACCESS *info; | |
c869da88 | 480 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
67c8e7f4 | 481 | int i; |
5ce278a7 | 482 | |
67c8e7f4 DSH |
483 | info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL); |
484 | if (!info) | |
485 | return NULL; | |
486 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) | |
487 | { | |
488 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); | |
489 | if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) | |
490 | { | |
491 | if (ad->location->type == GEN_URI) | |
492 | { | |
493 | if (!append_ia5(&ret, ad->location->d.uniformResourceIdentifier)) | |
494 | break; | |
495 | } | |
496 | } | |
497 | } | |
498 | AUTHORITY_INFO_ACCESS_free(info); | |
499 | return ret; | |
500 | } | |
501 | ||
c869da88 | 502 | STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x) |
a91dedca | 503 | { |
9d6b1ce6 | 504 | GENERAL_NAMES *gens; |
a91dedca | 505 | STACK_OF(X509_EXTENSION) *exts; |
c869da88 | 506 | STACK_OF(OPENSSL_STRING) *ret; |
5ce278a7 | 507 | |
a91dedca DSH |
508 | exts = X509_REQ_get_extensions(x); |
509 | gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL); | |
510 | ret = get_email(X509_REQ_get_subject_name(x), gens); | |
511 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | |
512 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); | |
513 | return ret; | |
514 | } | |
515 | ||
516 | ||
c869da88 | 517 | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, GENERAL_NAMES *gens) |
a91dedca | 518 | { |
c869da88 | 519 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
a91dedca DSH |
520 | X509_NAME_ENTRY *ne; |
521 | ASN1_IA5STRING *email; | |
522 | GENERAL_NAME *gen; | |
523 | int i; | |
524 | /* Now add any email address(es) to STACK */ | |
525 | i = -1; | |
526 | /* First supplied X509_NAME */ | |
527 | while((i = X509_NAME_get_index_by_NID(name, | |
65caee44 | 528 | NID_pkcs9_emailAddress, i)) >= 0) { |
a91dedca DSH |
529 | ne = X509_NAME_get_entry(name, i); |
530 | email = X509_NAME_ENTRY_get_data(ne); | |
531 | if(!append_ia5(&ret, email)) return NULL; | |
532 | } | |
533 | for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) | |
534 | { | |
535 | gen = sk_GENERAL_NAME_value(gens, i); | |
536 | if(gen->type != GEN_EMAIL) continue; | |
537 | if(!append_ia5(&ret, gen->d.ia5)) return NULL; | |
538 | } | |
539 | return ret; | |
540 | } | |
541 | ||
c869da88 | 542 | static void str_free(OPENSSL_STRING str) |
a91dedca DSH |
543 | { |
544 | OPENSSL_free(str); | |
545 | } | |
546 | ||
c869da88 | 547 | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email) |
a91dedca DSH |
548 | { |
549 | char *emtmp; | |
550 | /* First some sanity checks */ | |
551 | if(email->type != V_ASN1_IA5STRING) return 1; | |
552 | if(!email->data || !email->length) return 1; | |
c869da88 | 553 | if(!*sk) *sk = sk_OPENSSL_STRING_new(sk_strcmp); |
a91dedca DSH |
554 | if(!*sk) return 0; |
555 | /* Don't add duplicates */ | |
c869da88 | 556 | if(sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1) return 1; |
d4cdbab9 | 557 | emtmp = BUF_strdup((char *)email->data); |
c869da88 | 558 | if(!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) { |
a91dedca DSH |
559 | X509_email_free(*sk); |
560 | *sk = NULL; | |
561 | return 0; | |
562 | } | |
563 | return 1; | |
564 | } | |
565 | ||
c869da88 | 566 | void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) |
a91dedca | 567 | { |
c869da88 | 568 | sk_OPENSSL_STRING_pop_free(sk, str_free); |
a91dedca | 569 | } |
4e5d3a7f | 570 | |
a70da5b3 DSH |
571 | /* Compare an ASN1_STRING to a supplied string. If they match |
572 | * return 1. If cmp_type > 0 only compare if string matches the | |
573 | * type, otherwise convert it to UTF8. | |
574 | */ | |
575 | ||
576 | static int do_check_string(ASN1_STRING *a, int cmp_type, | |
577 | const unsigned char *b, size_t blen) | |
578 | { | |
579 | if (!a->data || !a->length) | |
580 | return 0; | |
581 | if (cmp_type > 0) | |
582 | { | |
583 | if (cmp_type != a->type) | |
584 | return 0; | |
585 | if (a->length == (int)blen && !memcmp(a->data, b, blen)) | |
586 | return 1; | |
587 | else | |
588 | return 0; | |
589 | } | |
590 | else | |
591 | { | |
592 | int astrlen, rv; | |
593 | unsigned char *astr; | |
594 | astrlen = ASN1_STRING_to_UTF8(&astr, a); | |
595 | if (astrlen < 0) | |
596 | return 0; | |
597 | if (astrlen == (int)blen && !memcmp(astr, b, blen)) | |
598 | rv = 1; | |
599 | else | |
600 | rv = 0; | |
601 | OPENSSL_free(astr); | |
602 | return rv; | |
603 | } | |
604 | } | |
605 | ||
606 | static int do_x509_check(X509 *x, const unsigned char *chk, size_t chklen, | |
607 | unsigned int flags, int check_type) | |
608 | { | |
609 | GENERAL_NAMES *gens = NULL; | |
610 | X509_NAME *name = NULL; | |
611 | int i; | |
612 | int cnid; | |
613 | if (check_type == GEN_EMAIL) | |
614 | cnid = NID_pkcs9_emailAddress; | |
615 | else if (check_type == GEN_DNS) | |
616 | cnid = NID_commonName; | |
617 | else | |
618 | cnid = 0; | |
619 | ||
620 | if (chklen == 0) | |
621 | chklen = strlen((const char *)chk); | |
622 | ||
623 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); | |
624 | if (gens) | |
625 | { | |
626 | int rv = 0; | |
627 | int alt_type; | |
628 | if (cnid) | |
629 | alt_type = V_ASN1_IA5STRING; | |
630 | else | |
631 | alt_type = V_ASN1_OCTET_STRING; | |
632 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) | |
633 | { | |
634 | GENERAL_NAME *gen; | |
635 | ASN1_STRING *cstr; | |
636 | gen = sk_GENERAL_NAME_value(gens, i); | |
637 | if(gen->type != check_type) | |
638 | continue; | |
639 | if (check_type == GEN_EMAIL) | |
640 | cstr = gen->d.rfc822Name; | |
641 | else if (check_type == GEN_DNS) | |
642 | cstr = gen->d.dNSName; | |
643 | else | |
644 | cstr = gen->d.iPAddress; | |
645 | if (do_check_string(cstr, alt_type, chk, chklen)) | |
646 | { | |
647 | rv = 1; | |
648 | break; | |
649 | } | |
650 | } | |
651 | GENERAL_NAMES_free(gens); | |
652 | if (rv) | |
653 | return 1; | |
654 | if (!(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) || !cnid) | |
655 | return 0; | |
656 | } | |
657 | i = -1; | |
658 | name = X509_get_subject_name(x); | |
659 | while((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) | |
660 | { | |
661 | X509_NAME_ENTRY *ne; | |
662 | ASN1_STRING *str; | |
663 | ne = X509_NAME_get_entry(name, i); | |
664 | str = X509_NAME_ENTRY_get_data(ne); | |
665 | if (do_check_string(str, -1, chk, chklen)) | |
666 | return 1; | |
667 | } | |
668 | return 0; | |
669 | } | |
670 | ||
671 | int X509_check_host(X509 *x, const unsigned char *chk, size_t chklen, | |
672 | unsigned int flags) | |
673 | { | |
674 | return do_x509_check(x, chk, chklen, flags, GEN_DNS); | |
675 | } | |
676 | ||
677 | int X509_check_email(X509 *x, const unsigned char *chk, size_t chklen, | |
678 | unsigned int flags) | |
679 | { | |
680 | return do_x509_check(x, chk, chklen, flags, GEN_EMAIL); | |
681 | } | |
682 | ||
683 | int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, | |
684 | unsigned int flags) | |
685 | { | |
686 | return do_x509_check(x, chk, chklen, flags, GEN_IPADD); | |
687 | } | |
688 | ||
689 | int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags) | |
690 | { | |
691 | unsigned char ipout[16]; | |
692 | int iplen; | |
693 | iplen = a2i_ipadd(ipout, ipasc); | |
694 | if (iplen == 0) | |
695 | return 0; | |
696 | return do_x509_check(x, ipout, (size_t)iplen, flags, GEN_IPADD); | |
697 | } | |
698 | ||
4e5d3a7f DSH |
699 | /* Convert IP addresses both IPv4 and IPv6 into an |
700 | * OCTET STRING compatible with RFC3280. | |
701 | */ | |
702 | ||
703 | ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc) | |
704 | { | |
705 | unsigned char ipout[16]; | |
706 | ASN1_OCTET_STRING *ret; | |
707 | int iplen; | |
708 | ||
709 | /* If string contains a ':' assume IPv6 */ | |
710 | ||
520b76ff DSH |
711 | iplen = a2i_ipadd(ipout, ipasc); |
712 | ||
713 | if (!iplen) | |
714 | return NULL; | |
4e5d3a7f DSH |
715 | |
716 | ret = ASN1_OCTET_STRING_new(); | |
717 | if (!ret) | |
718 | return NULL; | |
719 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) | |
720 | { | |
721 | ASN1_OCTET_STRING_free(ret); | |
722 | return NULL; | |
723 | } | |
724 | return ret; | |
725 | } | |
726 | ||
520b76ff DSH |
727 | ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) |
728 | { | |
729 | ASN1_OCTET_STRING *ret = NULL; | |
730 | unsigned char ipout[32]; | |
731 | char *iptmp = NULL, *p; | |
732 | int iplen1, iplen2; | |
733 | p = strchr(ipasc,'/'); | |
734 | if (!p) | |
735 | return NULL; | |
736 | iptmp = BUF_strdup(ipasc); | |
737 | if (!iptmp) | |
738 | return NULL; | |
739 | p = iptmp + (p - ipasc); | |
740 | *p++ = 0; | |
741 | ||
742 | iplen1 = a2i_ipadd(ipout, iptmp); | |
743 | ||
744 | if (!iplen1) | |
745 | goto err; | |
746 | ||
747 | iplen2 = a2i_ipadd(ipout + iplen1, p); | |
748 | ||
749 | OPENSSL_free(iptmp); | |
750 | iptmp = NULL; | |
751 | ||
752 | if (!iplen2 || (iplen1 != iplen2)) | |
753 | goto err; | |
754 | ||
755 | ret = ASN1_OCTET_STRING_new(); | |
756 | if (!ret) | |
757 | goto err; | |
758 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2)) | |
759 | goto err; | |
760 | ||
761 | return ret; | |
762 | ||
763 | err: | |
764 | if (iptmp) | |
765 | OPENSSL_free(iptmp); | |
766 | if (ret) | |
767 | ASN1_OCTET_STRING_free(ret); | |
768 | return NULL; | |
769 | } | |
770 | ||
771 | ||
96ea4ae9 | 772 | int a2i_ipadd(unsigned char *ipout, const char *ipasc) |
520b76ff DSH |
773 | { |
774 | /* If string contains a ':' assume IPv6 */ | |
775 | ||
776 | if (strchr(ipasc, ':')) | |
777 | { | |
778 | if (!ipv6_from_asc(ipout, ipasc)) | |
779 | return 0; | |
780 | return 16; | |
781 | } | |
782 | else | |
783 | { | |
784 | if (!ipv4_from_asc(ipout, ipasc)) | |
785 | return 0; | |
786 | return 4; | |
787 | } | |
788 | } | |
789 | ||
4e5d3a7f DSH |
790 | static int ipv4_from_asc(unsigned char *v4, const char *in) |
791 | { | |
792 | int a0, a1, a2, a3; | |
793 | if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4) | |
794 | return 0; | |
795 | if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255) | |
796 | || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255)) | |
797 | return 0; | |
798 | v4[0] = a0; | |
799 | v4[1] = a1; | |
800 | v4[2] = a2; | |
801 | v4[3] = a3; | |
802 | return 1; | |
803 | } | |
804 | ||
805 | typedef struct { | |
806 | /* Temporary store for IPV6 output */ | |
807 | unsigned char tmp[16]; | |
808 | /* Total number of bytes in tmp */ | |
809 | int total; | |
810 | /* The position of a zero (corresponding to '::') */ | |
811 | int zero_pos; | |
812 | /* Number of zeroes */ | |
813 | int zero_cnt; | |
814 | } IPV6_STAT; | |
815 | ||
816 | ||
817 | static int ipv6_from_asc(unsigned char *v6, const char *in) | |
818 | { | |
819 | IPV6_STAT v6stat; | |
820 | v6stat.total = 0; | |
821 | v6stat.zero_pos = -1; | |
822 | v6stat.zero_cnt = 0; | |
823 | /* Treat the IPv6 representation as a list of values | |
824 | * separated by ':'. The presence of a '::' will parse | |
825 | * as one, two or three zero length elements. | |
826 | */ | |
827 | if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) | |
828 | return 0; | |
829 | ||
830 | /* Now for some sanity checks */ | |
831 | ||
832 | if (v6stat.zero_pos == -1) | |
833 | { | |
834 | /* If no '::' must have exactly 16 bytes */ | |
835 | if (v6stat.total != 16) | |
836 | return 0; | |
837 | } | |
838 | else | |
839 | { | |
840 | /* If '::' must have less than 16 bytes */ | |
841 | if (v6stat.total == 16) | |
842 | return 0; | |
843 | /* More than three zeroes is an error */ | |
844 | if (v6stat.zero_cnt > 3) | |
845 | return 0; | |
846 | /* Can only have three zeroes if nothing else present */ | |
847 | else if (v6stat.zero_cnt == 3) | |
848 | { | |
849 | if (v6stat.total > 0) | |
850 | return 0; | |
851 | } | |
852 | /* Can only have two zeroes if at start or end */ | |
853 | else if (v6stat.zero_cnt == 2) | |
854 | { | |
855 | if ((v6stat.zero_pos != 0) | |
856 | && (v6stat.zero_pos != v6stat.total)) | |
857 | return 0; | |
858 | } | |
859 | else | |
860 | /* Can only have one zero if *not* start or end */ | |
861 | { | |
862 | if ((v6stat.zero_pos == 0) | |
863 | || (v6stat.zero_pos == v6stat.total)) | |
864 | return 0; | |
865 | } | |
866 | } | |
867 | ||
868 | /* Format result */ | |
869 | ||
9d44cd16 | 870 | if (v6stat.zero_pos >= 0) |
1d4e8791 DSH |
871 | { |
872 | /* Copy initial part */ | |
4e5d3a7f | 873 | memcpy(v6, v6stat.tmp, v6stat.zero_pos); |
1d4e8791 | 874 | /* Zero middle */ |
4e5d3a7f | 875 | memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); |
1d4e8791 DSH |
876 | /* Copy final part */ |
877 | if (v6stat.total != v6stat.zero_pos) | |
878 | memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, | |
879 | v6stat.tmp + v6stat.zero_pos, | |
880 | v6stat.total - v6stat.zero_pos); | |
881 | } | |
882 | else | |
883 | memcpy(v6, v6stat.tmp, 16); | |
4e5d3a7f DSH |
884 | |
885 | return 1; | |
886 | } | |
887 | ||
888 | static int ipv6_cb(const char *elem, int len, void *usr) | |
889 | { | |
890 | IPV6_STAT *s = usr; | |
891 | /* Error if 16 bytes written */ | |
892 | if (s->total == 16) | |
893 | return 0; | |
894 | if (len == 0) | |
895 | { | |
896 | /* Zero length element, corresponds to '::' */ | |
897 | if (s->zero_pos == -1) | |
898 | s->zero_pos = s->total; | |
899 | /* If we've already got a :: its an error */ | |
900 | else if (s->zero_pos != s->total) | |
901 | return 0; | |
902 | s->zero_cnt++; | |
903 | } | |
904 | else | |
905 | { | |
906 | /* If more than 4 characters could be final a.b.c.d form */ | |
907 | if (len > 4) | |
908 | { | |
909 | /* Need at least 4 bytes left */ | |
910 | if (s->total > 12) | |
911 | return 0; | |
912 | /* Must be end of string */ | |
913 | if (elem[len]) | |
914 | return 0; | |
915 | if (!ipv4_from_asc(s->tmp + s->total, elem)) | |
916 | return 0; | |
917 | s->total += 4; | |
918 | } | |
919 | else | |
920 | { | |
921 | if (!ipv6_hex(s->tmp + s->total, elem, len)) | |
922 | return 0; | |
923 | s->total += 2; | |
924 | } | |
925 | } | |
926 | return 1; | |
927 | } | |
928 | ||
929 | /* Convert a string of up to 4 hex digits into the corresponding | |
930 | * IPv6 form. | |
931 | */ | |
932 | ||
933 | static int ipv6_hex(unsigned char *out, const char *in, int inlen) | |
934 | { | |
935 | unsigned char c; | |
936 | unsigned int num = 0; | |
937 | if (inlen > 4) | |
938 | return 0; | |
939 | while(inlen--) | |
940 | { | |
941 | c = *in++; | |
942 | num <<= 4; | |
943 | if ((c >= '0') && (c <= '9')) | |
944 | num |= c - '0'; | |
945 | else if ((c >= 'A') && (c <= 'F')) | |
946 | num |= c - 'A' + 10; | |
947 | else if ((c >= 'a') && (c <= 'f')) | |
948 | num |= c - 'a' + 10; | |
949 | else | |
950 | return 0; | |
951 | } | |
952 | out[0] = num >> 8; | |
953 | out[1] = num & 0xff; | |
954 | return 1; | |
955 | } | |
956 | ||
f0dc08e6 DSH |
957 | |
958 | int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE)*dn_sk, | |
959 | unsigned long chtype) | |
960 | { | |
961 | CONF_VALUE *v; | |
1a15c899 | 962 | int i, mval; |
f0dc08e6 DSH |
963 | char *p, *type; |
964 | if (!nm) | |
965 | return 0; | |
966 | ||
967 | for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) | |
968 | { | |
969 | v=sk_CONF_VALUE_value(dn_sk,i); | |
970 | type=v->name; | |
971 | /* Skip past any leading X. X: X, etc to allow for | |
972 | * multiple instances | |
973 | */ | |
974 | for(p = type; *p ; p++) | |
975 | #ifndef CHARSET_EBCDIC | |
976 | if ((*p == ':') || (*p == ',') || (*p == '.')) | |
977 | #else | |
978 | if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) | |
979 | #endif | |
980 | { | |
981 | p++; | |
982 | if(*p) type = p; | |
983 | break; | |
984 | } | |
1a15c899 | 985 | #ifndef CHARSET_EBCDIC |
5e64f8c4 | 986 | if (*type == '+') |
1a15c899 | 987 | #else |
5e64f8c4 | 988 | if (*type == os_toascii['+']) |
1a15c899 DSH |
989 | #endif |
990 | { | |
991 | mval = -1; | |
5e64f8c4 | 992 | type++; |
1a15c899 DSH |
993 | } |
994 | else | |
995 | mval = 0; | |
f0dc08e6 | 996 | if (!X509_NAME_add_entry_by_txt(nm,type, chtype, |
1a15c899 | 997 | (unsigned char *) v->value,-1,-1,mval)) |
f0dc08e6 DSH |
998 | return 0; |
999 | ||
1000 | } | |
1001 | return 1; | |
1002 | } |