]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509v3/v3_utl.c
Fix indent issue with functions using STACK_OF
[thirdparty/openssl.git] / crypto / x509v3 / v3_utl.c
CommitLineData
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 68static char *strip_spaces(char *name);
a91dedca 69static int sk_strcmp(const char * const *a, const char * const *b);
c869da88
DSH
70static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, GENERAL_NAMES *gens);
71static void str_free(OPENSSL_STRING str);
72static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
9aeaf1b4 73
4e5d3a7f
DSH
74static int ipv4_from_asc(unsigned char *v4, const char *in);
75static int ipv6_from_asc(unsigned char *v6, const char *in);
76static int ipv6_cb(const char *elem, int len, void *usr);
77static 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
81int 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 103int 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 111void 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
120int 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
127int 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 135char *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 147char *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 159ASN1_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 201int 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 213int 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 234int 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 250STACK_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 331OPENSSL_free(linebuf);
9aeaf1b4
DSH
332return values;
333
334err:
26a3a48d 335OPENSSL_free(linebuf);
ba404b5e 336sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
9aeaf1b4
DSH
337return NULL;
338
339}
340
341/* Delete leading and trailing spaces from a string */
6b691a5c 342static 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 363char *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 392unsigned 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 450int 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
461static int sk_strcmp(const char * const *a, const char * const *b)
462{
463 return strcmp(*a, *b);
464}
465
c869da88 466STACK_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 477STACK_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 502STACK_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 517static 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 542static void str_free(OPENSSL_STRING str)
a91dedca
DSH
543{
544 OPENSSL_free(str);
545}
546
c869da88 547static 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 566void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
a91dedca 567{
c869da88 568 sk_OPENSSL_STRING_pop_free(sk, str_free);
a91dedca 569}
4e5d3a7f 570
d88926f1 571typedef int (*equal_fn)(const unsigned char *pattern, size_t pattern_len,
397a8e74
VD
572 const unsigned char *subject, size_t subject_len,
573 unsigned int flags);
d88926f1 574
a09e4d24
VD
575/* Skip pattern prefix to match "wildcard" subject */
576static void skip_prefix(const unsigned char **p, size_t *plen,
577 const unsigned char *subject, size_t subject_len,
578 unsigned int flags)
579 {
580 const unsigned char *pattern = *p;
581 size_t pattern_len = *plen;
582
583 /*
584 * If subject starts with a leading '.' followed by more octets, and
585 * pattern is longer, compare just an equal-length suffix with the
586 * full subject (starting at the '.'), provided the prefix contains
7241a4c7 587 * no NULs.
a09e4d24 588 */
7241a4c7 589 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
a09e4d24
VD
590 return;
591
592 while (pattern_len > subject_len && *pattern)
593 {
594 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
595 *pattern == '.')
596 break;
597 ++pattern;
598 --pattern_len;
599 }
600
601 /* Skip if entire prefix acceptable */
602 if (pattern_len == subject_len)
603 {
604 *p = pattern;
605 *plen = pattern_len;
606 }
607 }
608
d88926f1
DSH
609/* Compare while ASCII ignoring case. */
610static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
397a8e74 611 const unsigned char *subject, size_t subject_len,
a09e4d24 612 unsigned int flags)
d88926f1 613 {
a09e4d24 614 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
d88926f1
DSH
615 if (pattern_len != subject_len)
616 return 0;
617 while (pattern_len)
618 {
619 unsigned char l = *pattern;
620 unsigned char r = *subject;
621 /* The pattern must not contain NUL characters. */
622 if (l == 0)
623 return 0;
624 if (l != r)
625 {
626 if ('A' <= l && l <= 'Z')
627 l = (l - 'A') + 'a';
628 if ('A' <= r && r <= 'Z')
629 r = (r - 'A') + 'a';
630 if (l != r)
631 return 0;
632 }
633 ++pattern;
634 ++subject;
635 --pattern_len;
636 }
637 return 1;
638 }
639
640/* Compare using memcmp. */
641static int equal_case(const unsigned char *pattern, size_t pattern_len,
397a8e74 642 const unsigned char *subject, size_t subject_len,
a09e4d24 643 unsigned int flags)
d88926f1 644{
a09e4d24 645 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
d88926f1
DSH
646 if (pattern_len != subject_len)
647 return 0;
648 return !memcmp(pattern, subject, pattern_len);
649}
650
651/* RFC 5280, section 7.5, requires that only the domain is compared in
652 a case-insensitive manner. */
653static int equal_email(const unsigned char *a, size_t a_len,
397a8e74
VD
654 const unsigned char *b, size_t b_len,
655 unsigned int unused_flags)
d88926f1
DSH
656 {
657 size_t i = a_len;
658 if (a_len != b_len)
659 return 0;
660 /* We search backwards for the '@' character, so that we do
661 not have to deal with quoted local-parts. The domain part
662 is compared in a case-insensitive manner. */
663 while (i > 0)
664 {
665 --i;
666 if (a[i] == '@' || b[i] == '@')
667 {
668 if (!equal_nocase(a + i, a_len - i,
397a8e74 669 b + i, a_len - i, 0))
d88926f1
DSH
670 return 0;
671 break;
672 }
673 }
674 if (i == 0)
675 i = a_len;
397a8e74 676 return equal_case(a, i, b, i, 0);
d88926f1
DSH
677 }
678
679/* Compare the prefix and suffix with the subject, and check that the
680 characters in-between are valid. */
681static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
682 const unsigned char *suffix, size_t suffix_len,
397a8e74
VD
683 const unsigned char *subject, size_t subject_len,
684 unsigned int flags)
d88926f1
DSH
685 {
686 const unsigned char *wildcard_start;
687 const unsigned char *wildcard_end;
688 const unsigned char *p;
397a8e74
VD
689 int allow_multi = 0;
690 int allow_idna = 0;
691
d88926f1
DSH
692 if (subject_len < prefix_len + suffix_len)
693 return 0;
397a8e74 694 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
d88926f1
DSH
695 return 0;
696 wildcard_start = subject + prefix_len;
697 wildcard_end = subject + (subject_len - suffix_len);
397a8e74 698 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
d88926f1 699 return 0;
397a8e74
VD
700 /*
701 * If the wildcard makes up the entire first label, it must match at
702 * least one character.
703 */
704 if (prefix_len == 0 && *suffix == '.')
705 {
706 if (wildcard_start == wildcard_end)
707 return 0;
708 allow_idna = 1;
709 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
710 allow_multi = 1;
711 }
712 /* IDNA labels cannot match partial wildcards */
713 if (!allow_idna &&
714 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
d88926f1 715 return 0;
397a8e74
VD
716 /* The wildcard may match a literal '*' */
717 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
718 return 1;
719 /*
720 * Check that the part matched by the wildcard contains only
721 * permitted characters and only matches a single label unless
722 * allow_multi is set.
723 */
d88926f1
DSH
724 for (p = wildcard_start; p != wildcard_end; ++p)
725 if (!(('0' <= *p && *p <= '9') ||
726 ('A' <= *p && *p <= 'Z') ||
727 ('a' <= *p && *p <= 'z') ||
397a8e74 728 *p == '-' || (allow_multi && *p == '.')))
d88926f1
DSH
729 return 0;
730 return 1;
731 }
732
397a8e74
VD
733#define LABEL_START (1 << 0)
734#define LABEL_END (1 << 1)
735#define LABEL_HYPHEN (1 << 2)
736#define LABEL_IDNA (1 << 3)
d88926f1 737
397a8e74
VD
738static const unsigned char *valid_star(const unsigned char *p, size_t len,
739 unsigned int flags)
d88926f1 740 {
397a8e74
VD
741 const unsigned char *star = 0;
742 size_t i;
743 int state = LABEL_START;
744 int dots = 0;
745 for (i = 0; i < len; ++i)
d88926f1 746 {
397a8e74
VD
747 /*
748 * Locate first and only legal wildcard, either at the start
749 * or end of a non-IDNA first and not final label.
750 */
751 if (p[i] == '*')
752 {
753 int atstart = (state & LABEL_START);
754 int atend = (i == len - 1 || p[i+i] == '.');
31d1d374 755 /*-
397a8e74
VD
756 * At most one wildcard per pattern.
757 * No wildcards in IDNA labels.
758 * No wildcards after the first label.
759 */
760 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
761 return NULL;
762 /* Only full-label '*.example.com' wildcards? */
763 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
764 && (!atstart || !atend))
765 return NULL;
766 /* No 'foo*bar' wildcards */
767 if (!atstart && !atend)
768 return NULL;
769 star = &p[i];
770 state &= ~LABEL_START;
771 }
397a8e74
VD
772 else if (('a' <= p[i] && p[i] <= 'z')
773 || ('A' <= p[i] && p[i] <= 'Z')
774 || ('0' <= p[i] && p[i] <= '9'))
775 {
31d1d374
RS
776 if ((state & LABEL_START) != 0
777 && len - i >= 4
778 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
779 state |= LABEL_IDNA;
780 state &= ~(LABEL_HYPHEN|LABEL_START);
397a8e74
VD
781 }
782 else if (p[i] == '.')
783 {
31d1d374 784 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
397a8e74
VD
785 return NULL;
786 state = LABEL_START;
787 ++dots;
788 }
789 else if (p[i] == '-')
790 {
31d1d374 791 if ((state & LABEL_HYPHEN) != 0)
397a8e74
VD
792 return NULL;
793 state |= LABEL_HYPHEN;
794 }
795 else
796 return NULL;
d88926f1 797 }
397a8e74
VD
798
799 /*
800 * The final label must not end in a hyphen or ".", and
801 * there must be at least two dots after the star.
802 */
803 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0
804 || dots < 2)
d88926f1
DSH
805 return NULL;
806 return star;
807 }
808
809/* Compare using wildcards. */
810static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
397a8e74
VD
811 const unsigned char *subject, size_t subject_len,
812 unsigned int flags)
d88926f1 813 {
a09e4d24
VD
814 const unsigned char *star = NULL;
815
816 /*
817 * Subject names starting with '.' can only match a wildcard pattern
818 * via a subject sub-domain pattern suffix match.
819 */
820 if (!(subject_len > 1 && subject[0] == '.'))
821 star = valid_star(pattern, pattern_len, flags);
d88926f1
DSH
822 if (star == NULL)
823 return equal_nocase(pattern, pattern_len,
397a8e74 824 subject, subject_len, flags);
d88926f1
DSH
825 return wildcard_match(pattern, star - pattern,
826 star + 1, (pattern + pattern_len) - star - 1,
397a8e74 827 subject, subject_len, flags);
d88926f1
DSH
828 }
829
a70da5b3
DSH
830/* Compare an ASN1_STRING to a supplied string. If they match
831 * return 1. If cmp_type > 0 only compare if string matches the
832 * type, otherwise convert it to UTF8.
833 */
834
d88926f1 835static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
297c67fc 836 unsigned int flags, const char *b, size_t blen,
ced3d915 837 char **peername)
a70da5b3 838 {
ced3d915
VD
839 int rv = 0;
840
a70da5b3
DSH
841 if (!a->data || !a->length)
842 return 0;
843 if (cmp_type > 0)
844 {
845 if (cmp_type != a->type)
846 return 0;
d88926f1 847 if (cmp_type == V_ASN1_IA5STRING)
297c67fc
VD
848 rv = equal(a->data, a->length,
849 (unsigned char *)b, blen, flags);
ced3d915
VD
850 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
851 rv = 1;
852 if (rv > 0 && peername)
853 *peername = BUF_strndup((char *)a->data, a->length);
a70da5b3
DSH
854 }
855 else
856 {
ced3d915 857 int astrlen;
a70da5b3
DSH
858 unsigned char *astr;
859 astrlen = ASN1_STRING_to_UTF8(&astr, a);
860 if (astrlen < 0)
d88926f1 861 return -1;
297c67fc 862 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
ced3d915
VD
863 if (rv > 0 && peername)
864 *peername = BUF_strndup((char *)astr, astrlen);
86588655 865 OPENSSL_free(astr);
a70da5b3 866 }
ced3d915 867 return rv;
a70da5b3
DSH
868 }
869
297c67fc 870static int do_x509_check(X509 *x, const char *chk, size_t chklen,
ced3d915
VD
871 unsigned int flags, int check_type,
872 char **peername)
a70da5b3
DSH
873 {
874 GENERAL_NAMES *gens = NULL;
875 X509_NAME *name = NULL;
876 int i;
877 int cnid;
d88926f1 878 int alt_type;
397a8e74 879 int san_present = 0;
ced3d915 880 int rv = 0;
d88926f1 881 equal_fn equal;
7241a4c7
VD
882
883 /* See below, this flag is internal-only */
884 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
a70da5b3 885 if (check_type == GEN_EMAIL)
d88926f1 886 {
a70da5b3 887 cnid = NID_pkcs9_emailAddress;
d88926f1
DSH
888 alt_type = V_ASN1_IA5STRING;
889 equal = equal_email;
890 }
a70da5b3 891 else if (check_type == GEN_DNS)
d88926f1 892 {
a70da5b3 893 cnid = NID_commonName;
a09e4d24
VD
894 /* Implicit client-side DNS sub-domain pattern */
895 if (chklen > 1 && chk[0] == '.')
896 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
d88926f1
DSH
897 alt_type = V_ASN1_IA5STRING;
898 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
899 equal = equal_nocase;
900 else
901 equal = equal_wildcard;
902 }
a70da5b3 903 else
d88926f1 904 {
a70da5b3 905 cnid = 0;
d88926f1
DSH
906 alt_type = V_ASN1_OCTET_STRING;
907 equal = equal_case;
908 }
a70da5b3
DSH
909
910 if (chklen == 0)
297c67fc 911 chklen = strlen(chk);
a70da5b3
DSH
912
913 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
914 if (gens)
915 {
a70da5b3
DSH
916 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
917 {
918 GENERAL_NAME *gen;
919 ASN1_STRING *cstr;
920 gen = sk_GENERAL_NAME_value(gens, i);
397a8e74 921 if (gen->type != check_type)
a70da5b3 922 continue;
397a8e74 923 san_present = 1;
a70da5b3
DSH
924 if (check_type == GEN_EMAIL)
925 cstr = gen->d.rfc822Name;
926 else if (check_type == GEN_DNS)
927 cstr = gen->d.dNSName;
928 else
929 cstr = gen->d.iPAddress;
ced3d915
VD
930 /* Positive on success, negative on error! */
931 if ((rv = do_check_string(cstr, alt_type, equal, flags,
932 chk, chklen, peername)) != 0)
a70da5b3 933 break;
a70da5b3
DSH
934 }
935 GENERAL_NAMES_free(gens);
ced3d915
VD
936 if (rv != 0)
937 return rv;
397a8e74
VD
938 if (!cnid
939 || (san_present
940 && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
a70da5b3
DSH
941 return 0;
942 }
943 i = -1;
944 name = X509_get_subject_name(x);
945 while((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0)
946 {
947 X509_NAME_ENTRY *ne;
948 ASN1_STRING *str;
949 ne = X509_NAME_get_entry(name, i);
950 str = X509_NAME_ENTRY_get_data(ne);
ced3d915
VD
951 /* Positive on success, negative on error! */
952 if ((rv = do_check_string(str, -1, equal, flags,
953 chk, chklen, peername)) != 0)
954 return rv;
a70da5b3
DSH
955 }
956 return 0;
957 }
958
297c67fc
VD
959int X509_check_host(X509 *x, const char *chk, size_t chklen,
960 unsigned int flags, char **peername)
a70da5b3 961 {
29edebe9
VD
962 if (chk == NULL)
963 return -2;
964 /*
965 * Embedded NULs are disallowed, except as the last character of a
966 * string of length 2 or more (tolerate caller including terminating
967 * NUL in string length).
968 */
b3012c69 969 if (chklen == 0)
297c67fc 970 chklen = strlen(chk);
29edebe9
VD
971 else if (memchr(chk, '\0', chklen > 1 ? chklen-1 : chklen))
972 return -2;
973 if (chklen > 1 && chk[chklen-1] == '\0')
974 --chklen;
ced3d915 975 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
a70da5b3
DSH
976 }
977
297c67fc
VD
978int X509_check_email(X509 *x, const char *chk, size_t chklen,
979 unsigned int flags)
a70da5b3 980 {
29edebe9
VD
981 if (chk == NULL)
982 return -2;
983 /*
984 * Embedded NULs are disallowed, except as the last character of a
985 * string of length 2 or more (tolerate caller including terminating
986 * NUL in string length).
987 */
988 if (chklen == 0)
989 chklen = strlen((char *)chk);
990 else if (memchr(chk, '\0', chklen > 1 ? chklen-1 : chklen))
991 return -2;
992 if (chklen > 1 && chk[chklen-1] == '\0')
993 --chklen;
ced3d915 994 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
a70da5b3
DSH
995 }
996
997int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
998 unsigned int flags)
999 {
29edebe9
VD
1000 if (chk == NULL)
1001 return -2;
297c67fc 1002 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
a70da5b3
DSH
1003 }
1004
1005int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
1006 {
1007 unsigned char ipout[16];
297c67fc
VD
1008 size_t iplen;
1009
29edebe9
VD
1010 if (ipasc == NULL)
1011 return -2;
297c67fc 1012 iplen = (size_t) a2i_ipadd(ipout, ipasc);
a70da5b3 1013 if (iplen == 0)
d88926f1 1014 return -2;
297c67fc 1015 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
a70da5b3
DSH
1016 }
1017
4e5d3a7f
DSH
1018/* Convert IP addresses both IPv4 and IPv6 into an
1019 * OCTET STRING compatible with RFC3280.
1020 */
1021
1022ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
1023 {
1024 unsigned char ipout[16];
1025 ASN1_OCTET_STRING *ret;
1026 int iplen;
1027
1028 /* If string contains a ':' assume IPv6 */
1029
520b76ff
DSH
1030 iplen = a2i_ipadd(ipout, ipasc);
1031
1032 if (!iplen)
1033 return NULL;
4e5d3a7f
DSH
1034
1035 ret = ASN1_OCTET_STRING_new();
1036 if (!ret)
1037 return NULL;
1038 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen))
1039 {
1040 ASN1_OCTET_STRING_free(ret);
1041 return NULL;
1042 }
1043 return ret;
1044 }
1045
520b76ff
DSH
1046ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
1047 {
1048 ASN1_OCTET_STRING *ret = NULL;
1049 unsigned char ipout[32];
1050 char *iptmp = NULL, *p;
1051 int iplen1, iplen2;
1052 p = strchr(ipasc,'/');
1053 if (!p)
1054 return NULL;
1055 iptmp = BUF_strdup(ipasc);
1056 if (!iptmp)
1057 return NULL;
1058 p = iptmp + (p - ipasc);
1059 *p++ = 0;
1060
1061 iplen1 = a2i_ipadd(ipout, iptmp);
1062
1063 if (!iplen1)
1064 goto err;
1065
1066 iplen2 = a2i_ipadd(ipout + iplen1, p);
1067
1068 OPENSSL_free(iptmp);
1069 iptmp = NULL;
1070
1071 if (!iplen2 || (iplen1 != iplen2))
1072 goto err;
1073
1074 ret = ASN1_OCTET_STRING_new();
1075 if (!ret)
1076 goto err;
1077 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1078 goto err;
1079
1080 return ret;
1081
1082 err:
1083 if (iptmp)
1084 OPENSSL_free(iptmp);
1085 if (ret)
1086 ASN1_OCTET_STRING_free(ret);
1087 return NULL;
1088 }
1089
1090
96ea4ae9 1091int a2i_ipadd(unsigned char *ipout, const char *ipasc)
520b76ff
DSH
1092 {
1093 /* If string contains a ':' assume IPv6 */
1094
1095 if (strchr(ipasc, ':'))
1096 {
1097 if (!ipv6_from_asc(ipout, ipasc))
1098 return 0;
1099 return 16;
1100 }
1101 else
1102 {
1103 if (!ipv4_from_asc(ipout, ipasc))
1104 return 0;
1105 return 4;
1106 }
1107 }
1108
4e5d3a7f
DSH
1109static int ipv4_from_asc(unsigned char *v4, const char *in)
1110 {
1111 int a0, a1, a2, a3;
1112 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1113 return 0;
1114 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1115 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1116 return 0;
1117 v4[0] = a0;
1118 v4[1] = a1;
1119 v4[2] = a2;
1120 v4[3] = a3;
1121 return 1;
1122 }
1123
1124typedef struct {
1125 /* Temporary store for IPV6 output */
1126 unsigned char tmp[16];
1127 /* Total number of bytes in tmp */
1128 int total;
1129 /* The position of a zero (corresponding to '::') */
1130 int zero_pos;
1131 /* Number of zeroes */
1132 int zero_cnt;
1133 } IPV6_STAT;
1134
1135
1136static int ipv6_from_asc(unsigned char *v6, const char *in)
1137 {
1138 IPV6_STAT v6stat;
1139 v6stat.total = 0;
1140 v6stat.zero_pos = -1;
1141 v6stat.zero_cnt = 0;
1142 /* Treat the IPv6 representation as a list of values
1143 * separated by ':'. The presence of a '::' will parse
1144 * as one, two or three zero length elements.
1145 */
1146 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1147 return 0;
1148
1149 /* Now for some sanity checks */
1150
1151 if (v6stat.zero_pos == -1)
1152 {
1153 /* If no '::' must have exactly 16 bytes */
1154 if (v6stat.total != 16)
1155 return 0;
1156 }
1157 else
1158 {
1159 /* If '::' must have less than 16 bytes */
1160 if (v6stat.total == 16)
1161 return 0;
1162 /* More than three zeroes is an error */
1163 if (v6stat.zero_cnt > 3)
1164 return 0;
1165 /* Can only have three zeroes if nothing else present */
1166 else if (v6stat.zero_cnt == 3)
1167 {
1168 if (v6stat.total > 0)
1169 return 0;
1170 }
1171 /* Can only have two zeroes if at start or end */
1172 else if (v6stat.zero_cnt == 2)
1173 {
1174 if ((v6stat.zero_pos != 0)
1175 && (v6stat.zero_pos != v6stat.total))
1176 return 0;
1177 }
1178 else
1179 /* Can only have one zero if *not* start or end */
1180 {
1181 if ((v6stat.zero_pos == 0)
1182 || (v6stat.zero_pos == v6stat.total))
1183 return 0;
1184 }
1185 }
1186
1187 /* Format result */
1188
9d44cd16 1189 if (v6stat.zero_pos >= 0)
1d4e8791
DSH
1190 {
1191 /* Copy initial part */
4e5d3a7f 1192 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1d4e8791 1193 /* Zero middle */
4e5d3a7f 1194 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1d4e8791
DSH
1195 /* Copy final part */
1196 if (v6stat.total != v6stat.zero_pos)
1197 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1198 v6stat.tmp + v6stat.zero_pos,
1199 v6stat.total - v6stat.zero_pos);
1200 }
1201 else
1202 memcpy(v6, v6stat.tmp, 16);
4e5d3a7f
DSH
1203
1204 return 1;
1205 }
1206
1207static int ipv6_cb(const char *elem, int len, void *usr)
1208 {
1209 IPV6_STAT *s = usr;
1210 /* Error if 16 bytes written */
1211 if (s->total == 16)
1212 return 0;
1213 if (len == 0)
1214 {
1215 /* Zero length element, corresponds to '::' */
1216 if (s->zero_pos == -1)
1217 s->zero_pos = s->total;
1218 /* If we've already got a :: its an error */
1219 else if (s->zero_pos != s->total)
1220 return 0;
1221 s->zero_cnt++;
1222 }
1223 else
1224 {
1225 /* If more than 4 characters could be final a.b.c.d form */
1226 if (len > 4)
1227 {
1228 /* Need at least 4 bytes left */
1229 if (s->total > 12)
1230 return 0;
1231 /* Must be end of string */
1232 if (elem[len])
1233 return 0;
1234 if (!ipv4_from_asc(s->tmp + s->total, elem))
1235 return 0;
1236 s->total += 4;
1237 }
1238 else
1239 {
1240 if (!ipv6_hex(s->tmp + s->total, elem, len))
1241 return 0;
1242 s->total += 2;
1243 }
1244 }
1245 return 1;
1246 }
1247
1248/* Convert a string of up to 4 hex digits into the corresponding
1249 * IPv6 form.
1250 */
1251
1252static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1253 {
1254 unsigned char c;
1255 unsigned int num = 0;
1256 if (inlen > 4)
1257 return 0;
1258 while(inlen--)
1259 {
1260 c = *in++;
1261 num <<= 4;
1262 if ((c >= '0') && (c <= '9'))
1263 num |= c - '0';
1264 else if ((c >= 'A') && (c <= 'F'))
1265 num |= c - 'A' + 10;
1266 else if ((c >= 'a') && (c <= 'f'))
1267 num |= c - 'a' + 10;
1268 else
1269 return 0;
1270 }
1271 out[0] = num >> 8;
1272 out[1] = num & 0xff;
1273 return 1;
1274 }
1275
f0dc08e6
DSH
1276
1277int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE)*dn_sk,
1278 unsigned long chtype)
1279 {
1280 CONF_VALUE *v;
1a15c899 1281 int i, mval;
f0dc08e6
DSH
1282 char *p, *type;
1283 if (!nm)
1284 return 0;
1285
1286 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
1287 {
1288 v=sk_CONF_VALUE_value(dn_sk,i);
1289 type=v->name;
1290 /* Skip past any leading X. X: X, etc to allow for
1291 * multiple instances
1292 */
1293 for(p = type; *p ; p++)
1294#ifndef CHARSET_EBCDIC
1295 if ((*p == ':') || (*p == ',') || (*p == '.'))
1296#else
1297 if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.']))
1298#endif
1299 {
1300 p++;
1301 if(*p) type = p;
1302 break;
1303 }
1a15c899 1304#ifndef CHARSET_EBCDIC
5e64f8c4 1305 if (*type == '+')
1a15c899 1306#else
5e64f8c4 1307 if (*type == os_toascii['+'])
1a15c899
DSH
1308#endif
1309 {
1310 mval = -1;
5e64f8c4 1311 type++;
1a15c899
DSH
1312 }
1313 else
1314 mval = 0;
f0dc08e6 1315 if (!X509_NAME_add_entry_by_txt(nm,type, chtype,
1a15c899 1316 (unsigned char *) v->value,-1,-1,mval))
f0dc08e6
DSH
1317 return 0;
1318
1319 }
1320 return 1;
1321 }