diff options
author | beck <> | 2021-10-26 09:09:53 +0000 |
---|---|---|
committer | beck <> | 2021-10-26 09:09:53 +0000 |
commit | fa1f14e289e900bc95217fa67f0aba88712ea0ae (patch) | |
tree | 1ffab474bea99fad4d76253c4288a7854a26df0c | |
parent | 9ecdb7b0d743875dc4c14dc28389438c08c73c7d (diff) | |
download | openbsd-fa1f14e289e900bc95217fa67f0aba88712ea0ae.tar.gz openbsd-fa1f14e289e900bc95217fa67f0aba88712ea0ae.tar.bz2 openbsd-fa1f14e289e900bc95217fa67f0aba88712ea0ae.zip |
Validate Subject Alternate Names when they are being added to certificates.
With this change we will reject adding SAN DNS, EMAIL, and IP addresses
that are malformed at certificate creation time.
ok jsing@ tb@
-rw-r--r-- | src/lib/libcrypto/x509/x509_alt.c | 50 | ||||
-rw-r--r-- | src/lib/libcrypto/x509/x509_constraints.c | 16 | ||||
-rw-r--r-- | src/lib/libcrypto/x509/x509_internal.h | 4 |
3 files changed, 61 insertions, 9 deletions
diff --git a/src/lib/libcrypto/x509/x509_alt.c b/src/lib/libcrypto/x509/x509_alt.c index 5b9f490bae..02a4a3a377 100644 --- a/src/lib/libcrypto/x509/x509_alt.c +++ b/src/lib/libcrypto/x509/x509_alt.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_alt.c,v 1.2 2021/08/24 15:23:03 tb Exp $ */ | 1 | /* $OpenBSD: x509_alt.c,v 1.3 2021/10/26 09:09:53 beck Exp $ */ |
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | * project. | 3 | * project. |
4 | */ | 4 | */ |
@@ -63,6 +63,8 @@ | |||
63 | #include <openssl/err.h> | 63 | #include <openssl/err.h> |
64 | #include <openssl/x509v3.h> | 64 | #include <openssl/x509v3.h> |
65 | 65 | ||
66 | #include "x509_internal.h" | ||
67 | |||
66 | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, | 68 | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, |
67 | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); | 69 | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); |
68 | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, | 70 | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, |
@@ -612,8 +614,11 @@ GENERAL_NAME * | |||
612 | v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, | 614 | v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, |
613 | X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) | 615 | X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) |
614 | { | 616 | { |
615 | int type; | 617 | uint8_t *bytes = NULL; |
616 | char *name, *value; | 618 | char *name, *value; |
619 | GENERAL_NAME *ret; | ||
620 | size_t len = 0; | ||
621 | int type; | ||
617 | 622 | ||
618 | name = cnf->name; | 623 | name = cnf->name; |
619 | value = cnf->value; | 624 | value = cnf->value; |
@@ -643,7 +648,46 @@ v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, | |||
643 | return NULL; | 648 | return NULL; |
644 | } | 649 | } |
645 | 650 | ||
646 | return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); | 651 | ret = a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); |
652 | |||
653 | /* Validate what we have for sanity */ | ||
654 | type = x509_constraints_general_to_bytes(ret, &bytes, &len); | ||
655 | switch(type) { | ||
656 | case GEN_DNS: | ||
657 | if (!x509_constraints_valid_sandns(bytes, len)) { | ||
658 | X509V3error(X509V3_R_BAD_OBJECT); | ||
659 | ERR_asprintf_error_data("name=%s value='%s'", name, bytes); | ||
660 | goto err; | ||
661 | } | ||
662 | break; | ||
663 | case GEN_URI: | ||
664 | if (!x509_constraints_uri_host(bytes, len, NULL)) { | ||
665 | X509V3error(X509V3_R_BAD_OBJECT); | ||
666 | ERR_asprintf_error_data("name=%s value='%s'", name, bytes); | ||
667 | goto err; | ||
668 | } | ||
669 | break; | ||
670 | case GEN_EMAIL: | ||
671 | if (!x509_constraints_parse_mailbox(bytes, len, NULL)) { | ||
672 | X509V3error(X509V3_R_BAD_OBJECT); | ||
673 | ERR_asprintf_error_data("name=%s value='%s'", name, bytes); | ||
674 | goto err; | ||
675 | } | ||
676 | break; | ||
677 | case GEN_IPADD: | ||
678 | if (len != 4 && len != 16) { | ||
679 | X509V3error(X509V3_R_BAD_IP_ADDRESS); | ||
680 | ERR_asprintf_error_data("name=%s len=%zu", name, len); | ||
681 | goto err; | ||
682 | } | ||
683 | break; | ||
684 | default: | ||
685 | break; | ||
686 | } | ||
687 | return ret; | ||
688 | err: | ||
689 | GENERAL_NAME_free(ret); | ||
690 | return NULL; | ||
647 | } | 691 | } |
648 | 692 | ||
649 | static int | 693 | static int |
diff --git a/src/lib/libcrypto/x509/x509_constraints.c b/src/lib/libcrypto/x509/x509_constraints.c index db33bf1aa4..f5e1050bb1 100644 --- a/src/lib/libcrypto/x509/x509_constraints.c +++ b/src/lib/libcrypto/x509/x509_constraints.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_constraints.c,v 1.17 2021/09/23 15:49:48 jsing Exp $ */ | 1 | /* $OpenBSD: x509_constraints.c,v 1.18 2021/10/26 09:09:53 beck Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> | 3 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> |
4 | * | 4 | * |
@@ -424,9 +424,14 @@ x509_constraints_parse_mailbox(uint8_t *candidate, size_t len, | |||
424 | strlen(candidate_domain))) | 424 | strlen(candidate_domain))) |
425 | goto bad; | 425 | goto bad; |
426 | 426 | ||
427 | name->local = candidate_local; | 427 | if (name != NULL) { |
428 | name->name = candidate_domain; | 428 | name->local = candidate_local; |
429 | name->type = GEN_EMAIL; | 429 | name->name = candidate_domain; |
430 | name->type = GEN_EMAIL; | ||
431 | } else { | ||
432 | free(candidate_local); | ||
433 | free(candidate_domain); | ||
434 | } | ||
430 | return 1; | 435 | return 1; |
431 | bad: | 436 | bad: |
432 | free(candidate_local); | 437 | free(candidate_local); |
@@ -511,7 +516,8 @@ x509_constraints_uri_host(uint8_t *uri, size_t len, char **hostpart) | |||
511 | host = authority; | 516 | host = authority; |
512 | if (!x509_constraints_valid_host(host, hostlen)) | 517 | if (!x509_constraints_valid_host(host, hostlen)) |
513 | return 0; | 518 | return 0; |
514 | *hostpart = strndup(host, hostlen); | 519 | if (hostpart != NULL) |
520 | *hostpart = strndup(host, hostlen); | ||
515 | return 1; | 521 | return 1; |
516 | } | 522 | } |
517 | 523 | ||
diff --git a/src/lib/libcrypto/x509/x509_internal.h b/src/lib/libcrypto/x509/x509_internal.h index 8891aecb13..90fafccae0 100644 --- a/src/lib/libcrypto/x509/x509_internal.h +++ b/src/lib/libcrypto/x509/x509_internal.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_internal.h,v 1.12 2021/09/03 08:58:53 beck Exp $ */ | 1 | /* $OpenBSD: x509_internal.h,v 1.13 2021/10/26 09:09:53 beck Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> | 3 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> |
4 | * | 4 | * |
@@ -106,6 +106,8 @@ struct x509_constraints_names *x509_constraints_names_dup( | |||
106 | struct x509_constraints_names *names); | 106 | struct x509_constraints_names *names); |
107 | void x509_constraints_names_clear(struct x509_constraints_names *names); | 107 | void x509_constraints_names_clear(struct x509_constraints_names *names); |
108 | struct x509_constraints_names *x509_constraints_names_new(size_t names_max); | 108 | struct x509_constraints_names *x509_constraints_names_new(size_t names_max); |
109 | int x509_constraints_general_to_bytes(GENERAL_NAME *name, uint8_t **bytes, | ||
110 | size_t *len); | ||
109 | void x509_constraints_names_free(struct x509_constraints_names *names); | 111 | void x509_constraints_names_free(struct x509_constraints_names *names); |
110 | int x509_constraints_valid_host(uint8_t *name, size_t len); | 112 | int x509_constraints_valid_host(uint8_t *name, size_t len); |
111 | int x509_constraints_valid_sandns(uint8_t *name, size_t len); | 113 | int x509_constraints_valid_sandns(uint8_t *name, size_t len); |