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