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