isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

Blame SOURCES/openssl-1.1.1-read-buff.patch

9972ad
diff -up openssl-1.1.1k/crypto/asn1/t_spki.c.read-buff openssl-1.1.1k/crypto/asn1/t_spki.c
9972ad
--- openssl-1.1.1k/crypto/asn1/t_spki.c.read-buff	2021-11-11 15:38:39.678509348 +0100
9972ad
+++ openssl-1.1.1k/crypto/asn1/t_spki.c	2021-11-11 15:40:59.647922530 +0100
9972ad
@@ -38,7 +38,7 @@ int NETSCAPE_SPKI_print(BIO *out, NETSCA
9972ad
     }
9972ad
     chal = spki->spkac->challenge;
9972ad
     if (chal->length)
9972ad
-        BIO_printf(out, "  Challenge String: %s\n", chal->data);
9972ad
+        BIO_printf(out, "  Challenge String: %.*s\n", chal->length, chal->data);
9972ad
     i = OBJ_obj2nid(spki->sig_algor.algorithm);
9972ad
     BIO_printf(out, "  Signature Algorithm: %s",
9972ad
                (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
9972ad
diff -up openssl-1.1.1k/crypto/ec/ec_asn1.c.read-buff openssl-1.1.1k/crypto/ec/ec_asn1.c
9972ad
--- openssl-1.1.1k/crypto/ec/ec_asn1.c.read-buff	2021-11-11 15:36:43.782339219 +0100
9972ad
+++ openssl-1.1.1k/crypto/ec/ec_asn1.c	2021-11-11 15:37:43.064937758 +0100
9972ad
@@ -761,7 +761,10 @@ EC_GROUP *EC_GROUP_new_from_ecparameters
9972ad
         ret->seed_len = params->curve->seed->length;
9972ad
     }
9972ad
 
9972ad
-    if (!params->order || !params->base || !params->base->data) {
9972ad
+    if (params->order == NULL
9972ad
+            || params->base == NULL
9972ad
+            || params->base->data == NULL
9972ad
+            || params->base->length == 0) {
9972ad
         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
9972ad
         goto err;
9972ad
     }
9972ad
diff -up openssl-1.1.1k/crypto/x509/t_x509.c.read-buff openssl-1.1.1k/crypto/x509/t_x509.c
9972ad
--- openssl-1.1.1k/crypto/x509/t_x509.c.read-buff	2021-11-12 12:54:15.665091764 +0100
9972ad
+++ openssl-1.1.1k/crypto/x509/t_x509.c	2021-11-12 12:56:10.897782587 +0100
9972ad
@@ -365,9 +365,9 @@ int X509_aux_print(BIO *out, X509 *x, in
9972ad
         BIO_puts(out, "\n");
9972ad
     } else
9972ad
         BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
9972ad
-    alias = X509_alias_get0(x, NULL);
9972ad
+    alias = X509_alias_get0(x, &i);
9972ad
     if (alias)
9972ad
-        BIO_printf(out, "%*sAlias: %s\n", indent, "", alias);
9972ad
+        BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias);
9972ad
     keyid = X509_keyid_get0(x, &keyidlen);
9972ad
     if (keyid) {
9972ad
         BIO_printf(out, "%*sKey Id: ", indent, "");
9972ad
diff -up openssl-1.1.1k/crypto/x509v3/v3_cpols.c.read-buff openssl-1.1.1k/crypto/x509v3/v3_cpols.c
9972ad
--- openssl-1.1.1k/crypto/x509v3/v3_cpols.c.read-buff	2021-11-12 12:40:51.415811428 +0100
9972ad
+++ openssl-1.1.1k/crypto/x509v3/v3_cpols.c	2021-11-12 12:50:06.062808372 +0100
9972ad
@@ -422,7 +422,8 @@ static void print_qualifiers(BIO *out, S
9972ad
         qualinfo = sk_POLICYQUALINFO_value(quals, i);
9972ad
         switch (OBJ_obj2nid(qualinfo->pqualid)) {
9972ad
         case NID_id_qt_cps:
9972ad
-            BIO_printf(out, "%*sCPS: %s\n", indent, "",
9972ad
+            BIO_printf(out, "%*sCPS: %.*s\n", indent, "",
9972ad
+                       qualinfo->d.cpsuri->length,
9972ad
                        qualinfo->d.cpsuri->data);
9972ad
             break;
9972ad
 
9972ad
@@ -447,7 +448,8 @@ static void print_notice(BIO *out, USERN
9972ad
     if (notice->noticeref) {
9972ad
         NOTICEREF *ref;
9972ad
         ref = notice->noticeref;
9972ad
-        BIO_printf(out, "%*sOrganization: %s\n", indent, "",
9972ad
+        BIO_printf(out, "%*sOrganization: %.*s\n", indent, "",
9972ad
+                   ref->organization->length,
9972ad
                    ref->organization->data);
9972ad
         BIO_printf(out, "%*sNumber%s: ", indent, "",
9972ad
                    sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
9972ad
@@ -470,7 +472,8 @@ static void print_notice(BIO *out, USERN
9972ad
         BIO_puts(out, "\n");
9972ad
     }
9972ad
     if (notice->exptext)
9972ad
-        BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
9972ad
+        BIO_printf(out, "%*sExplicit Text: %.*s\n", indent, "",
9972ad
+                   notice->exptext->length,
9972ad
                    notice->exptext->data);
9972ad
 }
9972ad
 
9972ad
diff -up openssl-1.1.1k/crypto/x509v3/v3_ncons.c.read-buff openssl-1.1.1k/crypto/x509v3/v3_ncons.c
9972ad
--- openssl-1.1.1k/crypto/x509v3/v3_ncons.c.read-buff	2021-11-11 15:56:12.675140779 +0100
9972ad
+++ openssl-1.1.1k/crypto/x509v3/v3_ncons.c	2021-11-12 12:38:24.781856836 +0100
9972ad
@@ -63,8 +63,30 @@ ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
9972ad
 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
9972ad
 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
9972ad
 
9972ad
+#define IA5_OFFSET_LEN(ia5base, offset) \
9972ad
+    ((ia5base)->length - ((unsigned char *)(offset) - (ia5base)->data))
9972ad
+
9972ad
+/* Like memchr but for ASN1_IA5STRING. Additionally you can specify the
9972ad
+ * starting point to search from
9972ad
+ */
9972ad
+# define ia5memchr(str, start, c) memchr(start, c, IA5_OFFSET_LEN(str, start))
9972ad
+
9972ad
+/* Like memrrchr but for ASN1_IA5STRING */
9972ad
+static char *ia5memrchr(ASN1_IA5STRING *str, int c)
9972ad
+{
9972ad
+    int i;
9972ad
+
9972ad
+    for (i = str->length; i > 0 && str->data[i - 1] != c; i--);
9972ad
+
9972ad
+    if (i == 0)
9972ad
+        return NULL;
9972ad
+
9972ad
+    return (char *)&str->data[i - 1];
9972ad
+}
9972ad
+
9972ad
 /*
9972ad
- * We cannot use strncasecmp here because that applies locale specific rules.
9972ad
+ * We cannot use strncasecmp here because that applies locale specific rules. It
9972ad
+ * also doesn't work with ASN1_STRINGs that may have embedded NUL characters.
9972ad
  * For example in Turkish 'I' is not the uppercase character for 'i'. We need to
9972ad
  * do a simple ASCII case comparison ignoring the locale (that is why we use
9972ad
  * numeric constants below).
9972ad
@@ -89,20 +111,12 @@ static int ia5ncasecmp(const char *s1, c
9972ad
 
9972ad
             /* c1 > c2 */
9972ad
             return 1;
9972ad
-        } else if (*s1 == 0) {
9972ad
-            /* If we get here we know that *s2 == 0 too */
9972ad
-            return 0;
9972ad
         }
9972ad
     }
9972ad
 
9972ad
     return 0;
9972ad
 }
9972ad
 
9972ad
-static int ia5casecmp(const char *s1, const char *s2)
9972ad
-{
9972ad
-    return ia5ncasecmp(s1, s2, SIZE_MAX);
9972ad
-}
9972ad
-
9972ad
 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
9972ad
                                   X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
9972ad
 {
9972ad
@@ -337,7 +351,7 @@ static int cn2dnsid(ASN1_STRING *cn, uns
9972ad
         --utf8_length;
9972ad
 
9972ad
     /* Reject *embedded* NULs */
9972ad
-    if ((size_t)utf8_length != strlen((char *)utf8_value)) {
9972ad
+    if (memchr(utf8_value, 0, utf8_length) != NULL) {
9972ad
         OPENSSL_free(utf8_value);
9972ad
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
9972ad
     }
9972ad
@@ -537,7 +551,7 @@ static int nc_dns(ASN1_IA5STRING *dns, A
9972ad
     char *baseptr = (char *)base->data;
9972ad
     char *dnsptr = (char *)dns->data;
9972ad
     /* Empty matches everything */
9972ad
-    if (!*baseptr)
9972ad
+    if (base->length == 0)
9972ad
         return X509_V_OK;
9972ad
     /*
9972ad
      * Otherwise can add zero or more components on the left so compare RHS
9972ad
@@ -549,7 +563,7 @@ static int nc_dns(ASN1_IA5STRING *dns, A
9972ad
             return X509_V_ERR_PERMITTED_VIOLATION;
9972ad
     }
9972ad
 
9972ad
-    if (ia5casecmp(baseptr, dnsptr))
9972ad
+    if (ia5ncasecmp(baseptr, dnsptr, base->length))
9972ad
         return X509_V_ERR_PERMITTED_VIOLATION;
9972ad
 
9972ad
     return X509_V_OK;
9972ad
@@ -560,16 +574,17 @@ static int nc_email(ASN1_IA5STRING *eml,
9972ad
 {
9972ad
     const char *baseptr = (char *)base->data;
9972ad
     const char *emlptr = (char *)eml->data;
9972ad
+    const char *baseat = ia5memrchr(base, '@');
9972ad
+    const char *emlat = ia5memrchr(eml, '@');
9972ad
+    size_t basehostlen, emlhostlen;
9972ad
 
9972ad
-    const char *baseat = strchr(baseptr, '@');
9972ad
-    const char *emlat = strchr(emlptr, '@');
9972ad
     if (!emlat)
9972ad
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
9972ad
     /* Special case: initial '.' is RHS match */
9972ad
-    if (!baseat && (*baseptr == '.')) {
9972ad
+    if (!baseat && base->length > 0 && (*baseptr == '.')) {
9972ad
         if (eml->length > base->length) {
9972ad
             emlptr += eml->length - base->length;
9972ad
-            if (ia5casecmp(baseptr, emlptr) == 0)
9972ad
+            if (ia5ncasecmp(baseptr, emlptr, base->length) == 0)
9972ad
                 return X509_V_OK;
9972ad
         }
9972ad
         return X509_V_ERR_PERMITTED_VIOLATION;
9972ad
@@ -589,8 +604,10 @@ static int nc_email(ASN1_IA5STRING *eml,
9972ad
         baseptr = baseat + 1;
9972ad
     }
9972ad
     emlptr = emlat + 1;
9972ad
+    basehostlen = IA5_OFFSET_LEN(base, baseptr);
9972ad
+    emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
9972ad
     /* Just have hostname left to match: case insensitive */
9972ad
-    if (ia5casecmp(baseptr, emlptr))
9972ad
+    if (basehostlen != emlhostlen || ia5ncasecmp(baseptr, emlptr, emlhostlen))
9972ad
         return X509_V_ERR_PERMITTED_VIOLATION;
9972ad
 
9972ad
     return X509_V_OK;
9972ad
@@ -601,10 +618,14 @@ static int nc_uri(ASN1_IA5STRING *uri, A
9972ad
 {
9972ad
     const char *baseptr = (char *)base->data;
9972ad
     const char *hostptr = (char *)uri->data;
9972ad
-    const char *p = strchr(hostptr, ':');
9972ad
+    const char *p = ia5memchr(uri, (char *)uri->data, ':');
9972ad
     int hostlen;
9972ad
+
9972ad
     /* Check for foo:// and skip past it */
9972ad
-    if (!p || (p[1] != '/') || (p[2] != '/'))
9972ad
+    if (p == NULL
9972ad
+            || IA5_OFFSET_LEN(uri, p) < 3
9972ad
+            || p[1] != '/'
9972ad
+            || p[2] != '/')
9972ad
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
9972ad
     hostptr = p + 3;
9972ad
 
9972ad
@@ -612,13 +633,13 @@ static int nc_uri(ASN1_IA5STRING *uri, A
9972ad
 
9972ad
     /* Look for a port indicator as end of hostname first */
9972ad
 
9972ad
-    p = strchr(hostptr, ':');
9972ad
+    p = ia5memchr(uri, hostptr, ':');
9972ad
     /* Otherwise look for trailing slash */
9972ad
-    if (!p)
9972ad
-        p = strchr(hostptr, '/');
9972ad
+    if (p == NULL)
9972ad
+        p = ia5memchr(uri, hostptr, '/');
9972ad
 
9972ad
-    if (!p)
9972ad
-        hostlen = strlen(hostptr);
9972ad
+    if (p == NULL)
9972ad
+        hostlen = IA5_OFFSET_LEN(uri, hostptr);
9972ad
     else
9972ad
         hostlen = p - hostptr;
9972ad
 
9972ad
@@ -626,7 +647,7 @@ static int nc_uri(ASN1_IA5STRING *uri, A
9972ad
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
9972ad
 
9972ad
     /* Special case: initial '.' is RHS match */
9972ad
-    if (*baseptr == '.') {
9972ad
+    if (base->length > 0 && *baseptr == '.') {
9972ad
         if (hostlen > base->length) {
9972ad
             p = hostptr + hostlen - base->length;
9972ad
             if (ia5ncasecmp(p, baseptr, base->length) == 0)
9972ad
diff -up openssl-1.1.1k/crypto/x509v3/v3_pci.c.read-buff openssl-1.1.1k/crypto/x509v3/v3_pci.c
9972ad
--- openssl-1.1.1k/crypto/x509v3/v3_pci.c.read-buff	2021-11-12 12:39:06.649337807 +0100
9972ad
+++ openssl-1.1.1k/crypto/x509v3/v3_pci.c	2021-11-12 12:40:07.955201861 +0100
9972ad
@@ -77,7 +77,8 @@ static int i2r_pci(X509V3_EXT_METHOD *me
9972ad
     i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
9972ad
     BIO_puts(out, "\n");
9972ad
     if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
9972ad
-        BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
9972ad
+        BIO_printf(out, "%*sPolicy Text: %.*s\n", indent, "",
9972ad
+                   pci->proxyPolicy->policy->length,
9972ad
                    pci->proxyPolicy->policy->data);
9972ad
     return 1;
9972ad
 }
9972ad
diff -up openssl-1.1.1k/crypto/x509v3/v3_utl.c.read-buff openssl-1.1.1k/crypto/x509v3/v3_utl.c
9972ad
--- openssl-1.1.1k/crypto/x509v3/v3_utl.c.read-buff	2021-11-11 15:46:16.797124581 +0100
9972ad
+++ openssl-1.1.1k/crypto/x509v3/v3_utl.c	2021-11-11 15:50:36.696748621 +0100
9972ad
@@ -502,18 +502,26 @@ static int append_ia5(STACK_OF(OPENSSL_S
9972ad
     /* First some sanity checks */
9972ad
     if (email->type != V_ASN1_IA5STRING)
9972ad
         return 1;
9972ad
-    if (!email->data || !email->length)
9972ad
+    if (email->data == NULL || email->length == 0)
9972ad
+        return 1;
9972ad
+    if (memchr(email->data, 0, email->length) != NULL)
9972ad
         return 1;
9972ad
     if (*sk == NULL)
9972ad
         *sk = sk_OPENSSL_STRING_new(sk_strcmp);
9972ad
     if (*sk == NULL)
9972ad
         return 0;
9972ad
+
9972ad
+    emtmp = OPENSSL_strndup((char *)email->data, email->length);
9972ad
+    if (emtmp == NULL)
9972ad
+        return 0;
9972ad
+
9972ad
     /* Don't add duplicates */
9972ad
-    if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
9972ad
+    if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) {
9972ad
+        OPENSSL_free(emtmp);
9972ad
         return 1;
9972ad
-    emtmp = OPENSSL_strdup((char *)email->data);
9972ad
-    if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
9972ad
-        OPENSSL_free(emtmp);    /* free on push failure */
9972ad
+    }
9972ad
+    if (!sk_OPENSSL_STRING_push(*sk, emtmp)) {
9972ad
+        OPENSSL_free(emtmp); /* free on push failure */
9972ad
         X509_email_free(*sk);
9972ad
         *sk = NULL;
9972ad
         return 0;
9972ad
diff -up openssl-1.1.1k/test/x509_time_test.c.read-buff openssl-1.1.1k/test/x509_time_test.c
9972ad
--- openssl-1.1.1k/test/x509_time_test.c.read-buff	2021-11-11 15:53:59.112792286 +0100
9972ad
+++ openssl-1.1.1k/test/x509_time_test.c	2021-11-11 15:55:18.148590259 +0100
9972ad
@@ -330,10 +330,12 @@ static int test_x509_time(int idx)
9972ad
 
9972ad
     /* if t is not NULL but expected_string is NULL, it is an 'OK' case too */
9972ad
     if (t != NULL && x509_format_tests[idx].expected_string) {
9972ad
-        if (!TEST_str_eq((const char *)t->data,
9972ad
-                    x509_format_tests[idx].expected_string)) {
9972ad
-            TEST_info("test_x509_time(%d) failed: expected_string %s, got %s\n",
9972ad
-                    idx, x509_format_tests[idx].expected_string, t->data);
9972ad
+        if (!TEST_mem_eq((const char *)t->data, t->length,
9972ad
+                    x509_format_tests[idx].expected_string,
9972ad
+                    strlen(x509_format_tests[idx].expected_string))) {
9972ad
+            TEST_info("test_x509_time(%d) failed: expected_string %s, got %.*s\n",
9972ad
+                    idx, x509_format_tests[idx].expected_string, t->length,
9972ad
+                    t->data);
9972ad
             goto out;
9972ad
         }
9972ad
     }
9972ad
diff -up openssl-1.1.1k/crypto/x509v3/v3_ncons.c.read-buff openssl-1.1.1k/crypto/x509v3/v3_ncons.c
9972ad
--- openssl-1.1.1k/crypto/x509v3/v3_ncons.c.read-buff	2021-11-12 18:19:14.742820536 +0100
9972ad
+++ openssl-1.1.1k/crypto/x509v3/v3_ncons.c	2021-11-12 18:20:09.663327518 +0100
9972ad
@@ -553,6 +553,10 @@ static int nc_dns(ASN1_IA5STRING *dns, A
9972ad
     /* Empty matches everything */
9972ad
     if (base->length == 0)
9972ad
         return X509_V_OK;
9972ad
+
9972ad
+    if (dns->length < base->length)
9972ad
+        return X509_V_ERR_PERMITTED_VIOLATION;
9972ad
+
9972ad
     /*
9972ad
      * Otherwise can add zero or more components on the left so compare RHS
9972ad
      * and if dns is longer and expect '.' as preceding character.