From 36a31cca2d5f7024288e5202f71a4a33c90f01e1 Mon Sep 17 00:00:00 2001 From: "Steven.YGui" Date: Sat, 25 Dec 2021 18:00:50 +0800 Subject: [PATCH] backport upstream patches --- ...fined-value-in-generate_stateless_co.patch | 31 +++ ...I_print-function-to-not-assume-NUL-t.patch | 31 +++ ...rinting-to-not-assume-NUL-terminated.patch | 51 +++++ ...unction-to-not-assume-NUL-terminated.patch | 53 +++++ ...NAME-to-not-assume-NUL-terminated-st.patch | 147 ++++++++++++++ backport-Fix-potential-double-free.patch | 42 ++++ ...PROXY_CERT_INFO_EXTENSION-to-not-ass.patch | 32 +++ ...straints-code-to-not-assume-NUL-term.patch | 189 ++++++++++++++++++ ...-zero-length-digest-to-avoid-divisio.patch | 30 +++ openssl.spec | 14 +- 10 files changed, 619 insertions(+), 1 deletion(-) create mode 100644 backport-Avoid-using-undefined-value-in-generate_stateless_co.patch create mode 100644 backport-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch create mode 100644 backport-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch create mode 100644 backport-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch create mode 100644 backport-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch create mode 100644 backport-Fix-potential-double-free.patch create mode 100644 backport-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch create mode 100644 backport-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch create mode 100644 backport-pkcs12-check-for-zero-length-digest-to-avoid-divisio.patch diff --git a/backport-Avoid-using-undefined-value-in-generate_stateless_co.patch b/backport-Avoid-using-undefined-value-in-generate_stateless_co.patch new file mode 100644 index 0000000..fef3cda --- /dev/null +++ b/backport-Avoid-using-undefined-value-in-generate_stateless_co.patch @@ -0,0 +1,31 @@ +From cf2b1d6f11aa7ec4aa909ff1ecb9bee6892285d9 Mon Sep 17 00:00:00 2001 +From: Bernd Edlinger +Date: Mon, 23 Aug 2021 11:11:29 +0200 +Subject: [PATCH] Avoid using undefined value in + generate_stateless_cookie_callback + +Reviewed-by: Paul Yang +Reviewed-by: Paul Dale +Reviewed-by: Tomas Mraz +(Merged from https://github.com/openssl/openssl/pull/16381) +--- + apps/s_cb.c | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/apps/s_cb.c b/apps/s_cb.c +index dee1b2e5b4..d066a423de 100644 +--- a/apps/s_cb.c ++++ b/apps/s_cb.c +@@ -819,7 +819,9 @@ int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie, + { + unsigned int temp; + int res = generate_cookie_callback(ssl, cookie, &temp); +- *cookie_len = temp; ++ ++ if (res != 0) ++ *cookie_len = temp; + return res; + } + +-- + diff --git a/backport-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch b/backport-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch new file mode 100644 index 0000000..d1a78b3 --- /dev/null +++ b/backport-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch @@ -0,0 +1,31 @@ +From 2d0e5d4a4a5d4332325b5e5cea492fad2be633e1 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Thu, 19 Aug 2021 12:23:38 +0100 +Subject: [PATCH] Fix NETSCAPE_SPKI_print function to not assume NUL terminated + strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + crypto/asn1/t_spki.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/crypto/asn1/t_spki.c b/crypto/asn1/t_spki.c +index 51b56d0aa9..64ee77eeec 100644 +--- a/crypto/asn1/t_spki.c ++++ b/crypto/asn1/t_spki.c +@@ -38,7 +38,7 @@ int NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki) + } + chal = spki->spkac->challenge; + if (chal->length) +- BIO_printf(out, " Challenge String: %s\n", chal->data); ++ BIO_printf(out, " Challenge String: %.*s\n", chal->length, chal->data); + i = OBJ_obj2nid(spki->sig_algor.algorithm); + BIO_printf(out, " Signature Algorithm: %s", + (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i)); +-- + diff --git a/backport-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch b/backport-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch new file mode 100644 index 0000000..7424c11 --- /dev/null +++ b/backport-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch @@ -0,0 +1,51 @@ +From 5f54e57406ca17731b9ade3afd561d3c652e07f2 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 12:31:38 +0100 +Subject: [PATCH] Fix POLICYINFO printing to not assume NUL terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + crypto/x509v3/v3_cpols.c | 9 ++++++--- + 1 file changed, 6 insertions(+), 3 deletions(-) + +diff --git a/crypto/x509v3/v3_cpols.c b/crypto/x509v3/v3_cpols.c +index 1d12c89912..861e8455dd 100644 +--- a/crypto/x509v3/v3_cpols.c ++++ b/crypto/x509v3/v3_cpols.c +@@ -422,7 +422,8 @@ static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, + qualinfo = sk_POLICYQUALINFO_value(quals, i); + switch (OBJ_obj2nid(qualinfo->pqualid)) { + case NID_id_qt_cps: +- BIO_printf(out, "%*sCPS: %s\n", indent, "", ++ BIO_printf(out, "%*sCPS: %.*s\n", indent, "", ++ qualinfo->d.cpsuri->length, + qualinfo->d.cpsuri->data); + break; + +@@ -447,7 +448,8 @@ static void print_notice(BIO *out, USERNOTICE *notice, int indent) + if (notice->noticeref) { + NOTICEREF *ref; + ref = notice->noticeref; +- BIO_printf(out, "%*sOrganization: %s\n", indent, "", ++ BIO_printf(out, "%*sOrganization: %.*s\n", indent, "", ++ ref->organization->length, + ref->organization->data); + BIO_printf(out, "%*sNumber%s: ", indent, "", + sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : ""); +@@ -470,7 +472,8 @@ static void print_notice(BIO *out, USERNOTICE *notice, int indent) + BIO_puts(out, "\n"); + } + if (notice->exptext) +- BIO_printf(out, "%*sExplicit Text: %s\n", indent, "", ++ BIO_printf(out, "%*sExplicit Text: %.*s\n", indent, "", ++ notice->exptext->length, + notice->exptext->data); + } + +-- + diff --git a/backport-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch b/backport-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch new file mode 100644 index 0000000..ac58d33 --- /dev/null +++ b/backport-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch @@ -0,0 +1,53 @@ +From bb4d2ed4091408404e18b3326e3df67848ef63d0 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 17:58:23 +0100 +Subject: [PATCH] Fix append_ia5 function to not assume NUL terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + crypto/x509v3/v3_utl.c | 18 +++++++++++++----- + 1 file changed, 13 insertions(+), 5 deletions(-) + +diff --git a/crypto/x509v3/v3_utl.c b/crypto/x509v3/v3_utl.c +index 004ef55df9..513dc68b08 100644 +--- a/crypto/x509v3/v3_utl.c ++++ b/crypto/x509v3/v3_utl.c +@@ -528,18 +528,26 @@ static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email + /* First some sanity checks */ + if (email->type != V_ASN1_IA5STRING) + return 1; +- if (!email->data || !email->length) ++ if (email->data == NULL || email->length == 0) ++ return 1; ++ if (memchr(email->data, 0, email->length) != NULL) + return 1; + if (*sk == NULL) + *sk = sk_OPENSSL_STRING_new(sk_strcmp); + if (*sk == NULL) + return 0; ++ ++ emtmp = OPENSSL_strndup((char *)email->data, email->length); ++ if (emtmp == NULL) ++ return 0; ++ + /* Don't add duplicates */ +- if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1) ++ if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) { ++ OPENSSL_free(emtmp); + return 1; +- emtmp = OPENSSL_strdup((char *)email->data); +- if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) { +- OPENSSL_free(emtmp); /* free on push failure */ ++ } ++ if (!sk_OPENSSL_STRING_push(*sk, emtmp)) { ++ OPENSSL_free(emtmp); /* free on push failure */ + X509_email_free(*sk); + *sk = NULL; + return 0; +-- + diff --git a/backport-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch b/backport-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch new file mode 100644 index 0000000..3953f5e --- /dev/null +++ b/backport-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch @@ -0,0 +1,147 @@ +From 174ba8048a7f2f5e1fca31cfb93b1730d9db8300 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 12:24:22 +0100 +Subject: [PATCH] Fix i2v_GENERAL_NAME to not assume NUL terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + crypto/x509v3/v3_alt.c | 10 +++++++--- + crypto/x509v3/v3_utl.c | 38 ++++++++++++++++++++++++++++++++------ + include/crypto/x509.h | 5 +++++ + 3 files changed, 44 insertions(+), 9 deletions(-) + +diff --git a/crypto/x509v3/v3_alt.c b/crypto/x509v3/v3_alt.c +index 4dce004101..6e5f9f8b0e 100644 +--- a/crypto/x509v3/v3_alt.c ++++ b/crypto/x509v3/v3_alt.c +@@ -9,6 +9,7 @@ + + #include + #include "internal/cryptlib.h" ++#include "crypto/x509.h" + #include + #include + #include "ext_dat.h" +@@ -99,17 +100,20 @@ STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, + break; + + case GEN_EMAIL: +- if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret)) ++ if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data, ++ gen->d.ia5->length, &ret)) + return NULL; + break; + + case GEN_DNS: +- if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret)) ++ if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data, ++ gen->d.ia5->length, &ret)) + return NULL; + break; + + case GEN_URI: +- if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret)) ++ if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data, ++ gen->d.ia5->length, &ret)) + return NULL; + break; + +diff --git a/crypto/x509v3/v3_utl.c b/crypto/x509v3/v3_utl.c +index 7281a7b917..004ef55df9 100644 +--- a/crypto/x509v3/v3_utl.c ++++ b/crypto/x509v3/v3_utl.c +@@ -12,6 +12,7 @@ + #include "e_os.h" + #include "internal/cryptlib.h" + #include ++#include + #include "crypto/ctype.h" + #include + #include +@@ -34,17 +35,26 @@ static int ipv6_hex(unsigned char *out, const char *in, int inlen); + + /* Add a CONF_VALUE name value pair to stack */ + +-int X509V3_add_value(const char *name, const char *value, +- STACK_OF(CONF_VALUE) **extlist) ++static int x509v3_add_len_value(const char *name, const char *value, ++ size_t vallen, STACK_OF(CONF_VALUE) **extlist) + { + CONF_VALUE *vtmp = NULL; + char *tname = NULL, *tvalue = NULL; + int sk_allocated = (*extlist == NULL); + +- if (name && (tname = OPENSSL_strdup(name)) == NULL) +- goto err; +- if (value && (tvalue = OPENSSL_strdup(value)) == NULL) ++ if (name != NULL && (tname = OPENSSL_strdup(name)) == NULL) + goto err; ++ if (value != NULL && vallen > 0) { ++ /* ++ * We tolerate a single trailing NUL character, but otherwise no ++ * embedded NULs ++ */ ++ if (memchr(value, 0, vallen - 1) != NULL) ++ goto err; ++ tvalue = OPENSSL_strndup(value, vallen); ++ if (tvalue == NULL) ++ goto err; ++ } + if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL) + goto err; + if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL) +@@ -67,10 +77,26 @@ int X509V3_add_value(const char *name, const char *value, + return 0; + } + ++int X509V3_add_value(const char *name, const char *value, ++ STACK_OF(CONF_VALUE) **extlist) ++{ ++ return x509v3_add_len_value(name, value, ++ value != NULL ? strlen((const char *)value) : 0, ++ extlist); ++} ++ + int X509V3_add_value_uchar(const char *name, const unsigned char *value, + STACK_OF(CONF_VALUE) **extlist) + { +- return X509V3_add_value(name, (const char *)value, extlist); ++ return x509v3_add_len_value(name, (const char *)value, ++ value != NULL ? strlen((const char *)value) : 0, ++ extlist); ++} ++ ++int x509v3_add_len_value_uchar(const char *name, const unsigned char *value, ++ size_t vallen, STACK_OF(CONF_VALUE) **extlist) ++{ ++ return x509v3_add_len_value(name, (const char *)value, vallen, extlist); + } + + /* Free function for STACK_OF(CONF_VALUE) */ +diff --git a/include/crypto/x509.h b/include/crypto/x509.h +index b53c2b03c3..7ffb8abfe7 100644 +--- a/include/crypto/x509.h ++++ b/include/crypto/x509.h +@@ -8,6 +8,8 @@ + */ + + #include "internal/refcount.h" ++#include ++#include + + /* Internal X509 structures and functions: not for application use */ + +@@ -284,3 +286,6 @@ int a2i_ipadd(unsigned char *ipout, const char *ipasc); + int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm); + + void x509_init_sig_info(X509 *x); ++ ++int x509v3_add_len_value_uchar(const char *name, const unsigned char *value, ++ size_t vallen, STACK_OF(CONF_VALUE) **extlist); +-- + diff --git a/backport-Fix-potential-double-free.patch b/backport-Fix-potential-double-free.patch new file mode 100644 index 0000000..d1977f3 --- /dev/null +++ b/backport-Fix-potential-double-free.patch @@ -0,0 +1,42 @@ +From 75a4f263ba9d3ec1e9d55ca5024aee62aec70475 Mon Sep 17 00:00:00 2001 +From: Todd Short +Date: Fri, 13 Aug 2021 09:59:59 -0400 +Subject: [PATCH] Fix potential double-free + +The `sk` variable is assigned to `s->session->peer_chain`. +If `ssl3_digest_cached_records()` were to fail, then `sk` would still be +non-NULL, and subsequently freed on the error return. When the session +is freed, it will then attempt to free `s->session->peer_chain`, +resulting in a double-free (of `sk`). + +Reviewed-by: Matt Caswell +Reviewed-by: Tomas Mraz +(Merged from https://github.com/openssl/openssl/pull/16309) + +(cherry picked from commit 0449702abc95a3af24c049cb02c01ca6a8015cef) +--- + ssl/statem/statem_srvr.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/ssl/statem/statem_srvr.c b/ssl/statem/statem_srvr.c +index 30d20f1297..d701c46b43 100644 +--- a/ssl/statem/statem_srvr.c ++++ b/ssl/statem/statem_srvr.c +@@ -3753,6 +3753,7 @@ MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt) + + sk_X509_pop_free(s->session->peer_chain, X509_free); + s->session->peer_chain = sk; ++ sk = NULL; + + /* + * Freeze the handshake buffer. For +Date: Wed, 18 Aug 2021 14:02:40 +0100 +Subject: [PATCH] Fix printing of PROXY_CERT_INFO_EXTENSION to not assume NUL + terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + crypto/x509v3/v3_pci.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/crypto/x509v3/v3_pci.c b/crypto/x509v3/v3_pci.c +index 3d124fa6d9..98b6ef25e2 100644 +--- a/crypto/x509v3/v3_pci.c ++++ b/crypto/x509v3/v3_pci.c +@@ -77,7 +77,8 @@ static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci, + i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage); + BIO_puts(out, "\n"); + if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data) +- BIO_printf(out, "%*sPolicy Text: %s\n", indent, "", ++ BIO_printf(out, "%*sPolicy Text: %.*s\n", indent, "", ++ pci->proxyPolicy->policy->length, + pci->proxyPolicy->policy->data); + return 1; + } +-- + diff --git a/backport-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch b/backport-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch new file mode 100644 index 0000000..becd2c5 --- /dev/null +++ b/backport-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch @@ -0,0 +1,189 @@ +From 8393de42498f8be75cf0353f5c9f906a43a748d2 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 17:08:58 +0100 +Subject: [PATCH] Fix the name constraints code to not assume NUL terminated + strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + crypto/x509v3/v3_ncons.c | 77 +++++++++++++++++++++++++++------------- + 1 file changed, 52 insertions(+), 25 deletions(-) + +diff --git a/crypto/x509v3/v3_ncons.c b/crypto/x509v3/v3_ncons.c +index 2a7b4f0992..cb701c4d84 100644 +--- a/crypto/x509v3/v3_ncons.c ++++ b/crypto/x509v3/v3_ncons.c +@@ -63,8 +63,31 @@ ASN1_SEQUENCE(NAME_CONSTRAINTS) = { + IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE) + IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS) + ++ ++#define IA5_OFFSET_LEN(ia5base, offset) \ ++ ((ia5base)->length - ((unsigned char *)(offset) - (ia5base)->data)) ++ ++/* Like memchr but for ASN1_IA5STRING. Additionally you can specify the ++ * starting point to search from ++ */ ++# define ia5memchr(str, start, c) memchr(start, c, IA5_OFFSET_LEN(str, start)) ++ ++/* Like memrrchr but for ASN1_IA5STRING */ ++static char *ia5memrchr(ASN1_IA5STRING *str, int c) ++{ ++ int i; ++ ++ for (i = str->length; i > 0 && str->data[i - 1] != c; i--); ++ ++ if (i == 0) ++ return NULL; ++ ++ return (char *)&str->data[i - 1]; ++} ++ + /* +- * We cannot use strncasecmp here because that applies locale specific rules. ++ * We cannot use strncasecmp here because that applies locale specific rules. It ++ * also doesn't work with ASN1_STRINGs that may have embedded NUL characters. + * For example in Turkish 'I' is not the uppercase character for 'i'. We need to + * do a simple ASCII case comparison ignoring the locale (that is why we use + * numeric constants below). +@@ -89,20 +112,12 @@ static int ia5ncasecmp(const char *s1, const char *s2, size_t n) + + /* c1 > c2 */ + return 1; +- } else if (*s1 == 0) { +- /* If we get here we know that *s2 == 0 too */ +- return 0; + } + } + + return 0; + } + +-static int ia5casecmp(const char *s1, const char *s2) +-{ +- return ia5ncasecmp(s1, s2, SIZE_MAX); +-} +- + static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) + { +@@ -337,7 +352,7 @@ static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen) + --utf8_length; + + /* Reject *embedded* NULs */ +- if ((size_t)utf8_length != strlen((char *)utf8_value)) { ++ if (memchr(utf8_value, 0, utf8_length) != NULL) { + OPENSSL_free(utf8_value); + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + } +@@ -536,9 +551,14 @@ static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base) + { + char *baseptr = (char *)base->data; + char *dnsptr = (char *)dns->data; ++ + /* Empty matches everything */ +- if (!*baseptr) ++ if (base->length == 0) + return X509_V_OK; ++ ++ if (dns->length < base->length) ++ return X509_V_ERR_PERMITTED_VIOLATION; ++ + /* + * Otherwise can add zero or more components on the left so compare RHS + * and if dns is longer and expect '.' as preceding character. +@@ -549,7 +569,7 @@ static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base) + return X509_V_ERR_PERMITTED_VIOLATION; + } + +- if (ia5casecmp(baseptr, dnsptr)) ++ if (ia5ncasecmp(baseptr, dnsptr, base->length)) + return X509_V_ERR_PERMITTED_VIOLATION; + + return X509_V_OK; +@@ -560,16 +580,17 @@ static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base) + { + const char *baseptr = (char *)base->data; + const char *emlptr = (char *)eml->data; ++ const char *baseat = ia5memrchr(base, '@'); ++ const char *emlat = ia5memrchr(eml, '@'); ++ size_t basehostlen, emlhostlen; + +- const char *baseat = strchr(baseptr, '@'); +- const char *emlat = strchr(emlptr, '@'); + if (!emlat) + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + /* Special case: initial '.' is RHS match */ +- if (!baseat && (*baseptr == '.')) { ++ if (!baseat && base->length > 0 && (*baseptr == '.')) { + if (eml->length > base->length) { + emlptr += eml->length - base->length; +- if (ia5casecmp(baseptr, emlptr) == 0) ++ if (ia5ncasecmp(baseptr, emlptr, base->length) == 0) + return X509_V_OK; + } + return X509_V_ERR_PERMITTED_VIOLATION; +@@ -589,8 +610,10 @@ static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base) + baseptr = baseat + 1; + } + emlptr = emlat + 1; ++ basehostlen = IA5_OFFSET_LEN(base, baseptr); ++ emlhostlen = IA5_OFFSET_LEN(eml, emlptr); + /* Just have hostname left to match: case insensitive */ +- if (ia5casecmp(baseptr, emlptr)) ++ if (basehostlen != emlhostlen || ia5ncasecmp(baseptr, emlptr, emlhostlen)) + return X509_V_ERR_PERMITTED_VIOLATION; + + return X509_V_OK; +@@ -601,10 +624,14 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) + { + const char *baseptr = (char *)base->data; + const char *hostptr = (char *)uri->data; +- const char *p = strchr(hostptr, ':'); ++ const char *p = ia5memchr(uri, (char *)uri->data, ':'); + int hostlen; ++ + /* Check for foo:// and skip past it */ +- if (!p || (p[1] != '/') || (p[2] != '/')) ++ if (p == NULL ++ || IA5_OFFSET_LEN(uri, p) < 3 ++ || p[1] != '/' ++ || p[2] != '/') + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + hostptr = p + 3; + +@@ -612,13 +639,13 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) + + /* Look for a port indicator as end of hostname first */ + +- p = strchr(hostptr, ':'); ++ p = ia5memchr(uri, hostptr, ':'); + /* Otherwise look for trailing slash */ +- if (!p) +- p = strchr(hostptr, '/'); ++ if (p == NULL) ++ p = ia5memchr(uri, hostptr, '/'); + +- if (!p) +- hostlen = strlen(hostptr); ++ if (p == NULL) ++ hostlen = IA5_OFFSET_LEN(uri, hostptr); + else + hostlen = p - hostptr; + +@@ -626,7 +653,7 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + + /* Special case: initial '.' is RHS match */ +- if (*baseptr == '.') { ++ if (base->length > 0 && *baseptr == '.') { + if (hostlen > base->length) { + p = hostptr + hostlen - base->length; + if (ia5ncasecmp(p, baseptr, base->length) == 0) +-- + diff --git a/backport-pkcs12-check-for-zero-length-digest-to-avoid-divisio.patch b/backport-pkcs12-check-for-zero-length-digest-to-avoid-divisio.patch new file mode 100644 index 0000000..d470ea8 --- /dev/null +++ b/backport-pkcs12-check-for-zero-length-digest-to-avoid-divisio.patch @@ -0,0 +1,30 @@ +From 9d868840b821fddf895e3bf6b589ecf6be7b1b13 Mon Sep 17 00:00:00 2001 +From: Pauli +Date: Tue, 17 Aug 2021 13:19:32 +1000 +Subject: [PATCH] pkcs12: check for zero length digest to avoid division by + zero + +Fixes #16331 + +Reviewed-by: Dmitry Belyavskiy +Reviewed-by: Kurt Roeckx +(Merged from https://github.com/openssl/openssl/pull/16333) +--- + crypto/pkcs12/p12_key.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/crypto/pkcs12/p12_key.c b/crypto/pkcs12/p12_key.c +index ab31a61295..b814f79216 100644 +--- a/crypto/pkcs12/p12_key.c ++++ b/crypto/pkcs12/p12_key.c +@@ -101,7 +101,7 @@ int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt, + #endif + v = EVP_MD_block_size(md_type); + u = EVP_MD_size(md_type); +- if (u < 0 || v <= 0) ++ if (u <= 0 || v <= 0) + goto err; + D = OPENSSL_malloc(v); + Ai = OPENSSL_malloc(u); +-- + diff --git a/openssl.spec b/openssl.spec index 171bc53..5e0b257 100644 --- a/openssl.spec +++ b/openssl.spec @@ -2,7 +2,7 @@ Name: openssl Epoch: 1 Version: 1.1.1f -Release: 8 +Release: 9 Summary: Cryptography and SSL/TLS Toolkit License: OpenSSL and SSLeay URL: https://www.openssl.org/ @@ -26,6 +26,15 @@ Patch15: CVE-2021-3711-0003-Extend-tests-for-SM2-decryption.patch Patch16: CVE-2021-3712-0001-Fix-a-read-buffer-overrun-in-X509_aux_print.patch Patch17: CVE-2021-3712-0002-Fix-EC_GROUP_new_from_ecparameters-to-check-the-base.patch Patch18: bugfix-Don-t-Overflow-when-printing-Thawte-Strong-Extranet-.patch +Patch19: backport-Avoid-using-undefined-value-in-generate_stateless_co.patch +Patch20: backport-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch +Patch21: backport-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch +Patch22: backport-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch +Patch23: backport-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch +Patch24: backport-Fix-potential-double-free.patch +Patch25: backport-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch +Patch26: backport-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch +Patch27: backport-pkcs12-check-for-zero-length-digest-to-avoid-divisio.patch BuildRequires: gcc perl make lksctp-tools-devel coreutils util-linux zlib-devel @@ -202,6 +211,9 @@ make test || : %{_pkgdocdir}/html/ %changelog +* Sat Dec 25 2021 steven_ygui - 1:1.1.1f-9 +- backport upstream patches + * Fri Sep 24 2021 openEuler Buildteam - 1:1.1.1f-8 - bugfix Overflow when printing Thawte Strong Extranet -- Gitee