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