Blame SOURCES/openssl-1.0.2k-cve-2021-3712.patch

2906da
diff -up openssl-1.0.2k/crypto/asn1/t_spki.c.read-buff openssl-1.0.2k/crypto/asn1/t_spki.c
2906da
--- openssl-1.0.2k/crypto/asn1/t_spki.c.read-buff	2017-01-26 14:22:03.000000000 +0100
2906da
+++ openssl-1.0.2k/crypto/asn1/t_spki.c	2021-11-19 12:14:17.572630962 +0100
2906da
@@ -90,7 +90,7 @@ int NETSCAPE_SPKI_print(BIO *out, NETSCA
2906da
     }
2906da
     chal = spki->spkac->challenge;
2906da
     if (chal->length)
2906da
-        BIO_printf(out, "  Challenge String: %s\n", chal->data);
2906da
+        BIO_printf(out, "  Challenge String: %.*s\n", chal->length, chal->data);
2906da
     i = OBJ_obj2nid(spki->sig_algor->algorithm);
2906da
     BIO_printf(out, "  Signature Algorithm: %s",
2906da
                (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
2906da
diff -up openssl-1.0.2k/crypto/crypto.h.read-buff openssl-1.0.2k/crypto/crypto.h
2906da
--- openssl-1.0.2k/crypto/crypto.h.read-buff	2021-11-19 15:10:25.738928196 +0100
2906da
+++ openssl-1.0.2k/crypto/crypto.h	2021-11-19 17:51:47.062694785 +0100
2906da
@@ -380,6 +380,8 @@ int CRYPTO_is_mem_check_on(void);
2906da
 
2906da
 # define OPENSSL_malloc(num)     CRYPTO_malloc((int)num,__FILE__,__LINE__)
2906da
 # define OPENSSL_strdup(str)     CRYPTO_strdup((str),__FILE__,__LINE__)
2906da
+# define OPENSSL_strndup(str, n) \
2906da
+        CRYPTO_strndup((str), n, __FILE__, __LINE__)
2906da
 # define OPENSSL_realloc(addr,num) \
2906da
         CRYPTO_realloc((char *)addr,(int)num,__FILE__,__LINE__)
2906da
 # define OPENSSL_realloc_clean(addr,old_num,num) \
2906da
@@ -393,6 +395,8 @@ int CRYPTO_is_mem_check_on(void);
2906da
         CRYPTO_malloc_locked((int)num,__FILE__,__LINE__)
2906da
 # define OPENSSL_free_locked(addr) CRYPTO_free_locked(addr)
2906da
 
2906da
+size_t OPENSSL_strnlen(const char *str, size_t maxlen);
2906da
+
2906da
 const char *SSLeay_version(int type);
2906da
 unsigned long SSLeay(void);
2906da
 
2906da
@@ -533,6 +537,7 @@ void *CRYPTO_malloc_locked(int num, cons
2906da
 void CRYPTO_free_locked(void *ptr);
2906da
 void *CRYPTO_malloc(int num, const char *file, int line);
2906da
 char *CRYPTO_strdup(const char *str, const char *file, int line);
2906da
+char *CRYPTO_strndup(const char *str, size_t s, const char *file, int line);
2906da
 void CRYPTO_free(void *ptr);
2906da
 void *CRYPTO_realloc(void *addr, int num, const char *file, int line);
2906da
 void *CRYPTO_realloc_clean(void *addr, int old_num, int num, const char *file,
2906da
diff -up openssl-1.0.2k/crypto/ec/ec_asn1.c.read-buff openssl-1.0.2k/crypto/ec/ec_asn1.c
2906da
--- openssl-1.0.2k/crypto/ec/ec_asn1.c.read-buff	2017-01-26 14:22:03.000000000 +0100
2906da
+++ openssl-1.0.2k/crypto/ec/ec_asn1.c	2021-11-19 12:14:17.572630962 +0100
2906da
@@ -860,7 +860,10 @@ static EC_GROUP *ec_asn1_parameters2grou
2906da
         ret->seed_len = params->curve->seed->length;
2906da
     }
2906da
 
2906da
-    if (!params->order || !params->base || !params->base->data) {
2906da
+    if (params->order == NULL
2906da
+            || params->base == NULL
2906da
+            || params->base->data == NULL
2906da
+            || params->base->length == 0) {
2906da
         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
2906da
         goto err;
2906da
     }
2906da
diff -up openssl-1.0.2k/crypto/mem.c.read-buff openssl-1.0.2k/crypto/mem.c
2906da
--- openssl-1.0.2k/crypto/mem.c.read-buff	2021-11-19 15:54:38.937427447 +0100
2906da
+++ openssl-1.0.2k/crypto/mem.c	2021-11-19 15:55:21.234906752 +0100
2906da
@@ -364,6 +364,24 @@ char *CRYPTO_strdup(const char *str, con
2906da
     return ret;
2906da
 }
2906da
 
2906da
+char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
2906da
+{
2906da
+    size_t maxlen;
2906da
+    char *ret;
2906da
+
2906da
+    if (str == NULL)
2906da
+        return NULL;
2906da
+
2906da
+    maxlen = OPENSSL_strnlen(str, s);
2906da
+
2906da
+    ret = CRYPTO_malloc(maxlen + 1, file, line);
2906da
+    if (ret) {
2906da
+        memcpy(ret, str, maxlen);
2906da
+        ret[maxlen] = '\0';
2906da
+    }
2906da
+    return ret;
2906da
+}
2906da
+
2906da
 void *CRYPTO_realloc(void *str, int num, const char *file, int line)
2906da
 {
2906da
     void *ret = NULL;
2906da
diff -up openssl-1.0.2k/crypto/o_str.c.read-buff openssl-1.0.2k/crypto/o_str.c
2906da
--- openssl-1.0.2k/crypto/o_str.c.read-buff	2021-11-19 15:57:18.812239108 +0100
2906da
+++ openssl-1.0.2k/crypto/o_str.c	2021-11-19 15:58:22.704963124 +0100
2906da
@@ -114,3 +114,12 @@ int OPENSSL_memcmp(const void *v1, const
2906da
 
2906da
     return ret;
2906da
 }
2906da
+
2906da
+size_t OPENSSL_strnlen(const char *str, size_t maxlen)
2906da
+{
2906da
+    const char *p;
2906da
+
2906da
+    for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
2906da
+
2906da
+    return p - str;
2906da
+}
2906da
diff -up openssl-1.0.2k/crypto/x509v3/v3_cpols.c.read-buff openssl-1.0.2k/crypto/x509v3/v3_cpols.c
2906da
--- openssl-1.0.2k/crypto/x509v3/v3_cpols.c.read-buff	2017-01-26 14:22:04.000000000 +0100
2906da
+++ openssl-1.0.2k/crypto/x509v3/v3_cpols.c	2021-11-19 12:14:17.572630962 +0100
2906da
@@ -423,7 +423,8 @@ static void print_qualifiers(BIO *out, S
2906da
         qualinfo = sk_POLICYQUALINFO_value(quals, i);
2906da
         switch (OBJ_obj2nid(qualinfo->pqualid)) {
2906da
         case NID_id_qt_cps:
2906da
-            BIO_printf(out, "%*sCPS: %s\n", indent, "",
2906da
+            BIO_printf(out, "%*sCPS: %.*s\n", indent, "",
2906da
+                       qualinfo->d.cpsuri->length,
2906da
                        qualinfo->d.cpsuri->data);
2906da
             break;
2906da
 
2906da
@@ -448,7 +449,8 @@ static void print_notice(BIO *out, USERN
2906da
     if (notice->noticeref) {
2906da
         NOTICEREF *ref;
2906da
         ref = notice->noticeref;
2906da
-        BIO_printf(out, "%*sOrganization: %s\n", indent, "",
2906da
+        BIO_printf(out, "%*sOrganization: %.*s\n", indent, "",
2906da
+                   ref->organization->length,
2906da
                    ref->organization->data);
2906da
         BIO_printf(out, "%*sNumber%s: ", indent, "",
2906da
                    sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
2906da
@@ -465,7 +467,8 @@ static void print_notice(BIO *out, USERN
2906da
         BIO_puts(out, "\n");
2906da
     }
2906da
     if (notice->exptext)
2906da
-        BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
2906da
+        BIO_printf(out, "%*sExplicit Text: %.*s\n", indent, "",
2906da
+                   notice->exptext->length,
2906da
                    notice->exptext->data);
2906da
 }
2906da
 
2906da
diff -up openssl-1.0.2k/crypto/x509v3/v3_ncons.c.read-buff openssl-1.0.2k/crypto/x509v3/v3_ncons.c
2906da
--- openssl-1.0.2k/crypto/x509v3/v3_ncons.c.read-buff	2017-01-26 14:22:04.000000000 +0100
2906da
+++ openssl-1.0.2k/crypto/x509v3/v3_ncons.c	2021-11-19 12:14:17.573630970 +0100
2906da
@@ -107,6 +107,27 @@ ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
2906da
 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
2906da
 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
2906da
 
2906da
+#define IA5_OFFSET_LEN(ia5base, offset) \
2906da
+    ((ia5base)->length - ((unsigned char *)(offset) - (ia5base)->data))
2906da
+
2906da
+/* Like memchr but for ASN1_IA5STRING. Additionally you can specify the
2906da
+ * starting point to search from
2906da
+ */
2906da
+# define ia5memchr(str, start, c) memchr(start, c, IA5_OFFSET_LEN(str, start))
2906da
+
2906da
+/* Like memrrchr but for ASN1_IA5STRING */
2906da
+static char *ia5memrchr(ASN1_IA5STRING *str, int c)
2906da
+{
2906da
+    int i;
2906da
+
2906da
+    for (i = str->length; i > 0 && str->data[i - 1] != c; i--);
2906da
+
2906da
+    if (i == 0)
2906da
+        return NULL;
2906da
+
2906da
+    return (char *)&str->data[i - 1];
2906da
+}
2906da
+
2906da
 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
2906da
                                   X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
2906da
 {
2906da
@@ -372,8 +393,12 @@ static int nc_dns(ASN1_IA5STRING *dns, A
2906da
     char *baseptr = (char *)base->data;
2906da
     char *dnsptr = (char *)dns->data;
2906da
     /* Empty matches everything */
2906da
-    if (!*baseptr)
2906da
+    if (base->length == 0)
2906da
         return X509_V_OK;
2906da
+
2906da
+    if (dns->length < base->length)
2906da
+        return X509_V_ERR_PERMITTED_VIOLATION;
2906da
+
2906da
     /*
2906da
      * Otherwise can add zero or more components on the left so compare RHS
2906da
      * and if dns is longer and expect '.' as preceding character.
2906da
@@ -384,7 +409,7 @@ static int nc_dns(ASN1_IA5STRING *dns, A
2906da
             return X509_V_ERR_PERMITTED_VIOLATION;
2906da
     }
2906da
 
2906da
-    if (strcasecmp(baseptr, dnsptr))
2906da
+    if (strncasecmp(baseptr, dnsptr, base->length))
2906da
         return X509_V_ERR_PERMITTED_VIOLATION;
2906da
 
2906da
     return X509_V_OK;
2906da
@@ -395,16 +420,17 @@ static int nc_email(ASN1_IA5STRING *eml,
2906da
 {
2906da
     const char *baseptr = (char *)base->data;
2906da
     const char *emlptr = (char *)eml->data;
2906da
+    const char *baseat = ia5memrchr(base, '@');
2906da
+    const char *emlat = ia5memrchr(eml, '@');
2906da
+    size_t basehostlen, emlhostlen;
2906da
 
2906da
-    const char *baseat = strchr(baseptr, '@');
2906da
-    const char *emlat = strchr(emlptr, '@');
2906da
     if (!emlat)
2906da
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
2906da
     /* Special case: inital '.' is RHS match */
2906da
-    if (!baseat && (*baseptr == '.')) {
2906da
+    if (!baseat && base->length > 0 && (*baseptr == '.')) {
2906da
         if (eml->length > base->length) {
2906da
             emlptr += eml->length - base->length;
2906da
-            if (!strcasecmp(baseptr, emlptr))
2906da
+            if (!strncasecmp(baseptr, emlptr, base->length))
2906da
                 return X509_V_OK;
2906da
         }
2906da
         return X509_V_ERR_PERMITTED_VIOLATION;
2906da
@@ -424,8 +450,10 @@ static int nc_email(ASN1_IA5STRING *eml,
2906da
         baseptr = baseat + 1;
2906da
     }
2906da
     emlptr = emlat + 1;
2906da
+    basehostlen = IA5_OFFSET_LEN(base, baseptr);
2906da
+    emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
2906da
     /* Just have hostname left to match: case insensitive */
2906da
-    if (strcasecmp(baseptr, emlptr))
2906da
+    if (basehostlen != emlhostlen || strncasecmp(baseptr, emlptr, emlhostlen))
2906da
         return X509_V_ERR_PERMITTED_VIOLATION;
2906da
 
2906da
     return X509_V_OK;
2906da
@@ -436,10 +464,13 @@ static int nc_uri(ASN1_IA5STRING *uri, A
2906da
 {
2906da
     const char *baseptr = (char *)base->data;
2906da
     const char *hostptr = (char *)uri->data;
2906da
-    const char *p = strchr(hostptr, ':');
2906da
+    const char *p = ia5memchr(uri, (char *)uri->data, ':');
2906da
     int hostlen;
2906da
     /* Check for foo:// and skip past it */
2906da
-    if (!p || (p[1] != '/') || (p[2] != '/'))
2906da
+    if (p == NULL
2906da
+            || IA5_OFFSET_LEN(uri, p) < 3
2906da
+            || p[1] != '/'
2906da
+            || p[2] != '/')
2906da
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
2906da
     hostptr = p + 3;
2906da
 
2906da
@@ -447,10 +478,10 @@ static int nc_uri(ASN1_IA5STRING *uri, A
2906da
 
2906da
     /* Look for a port indicator as end of hostname first */
2906da
 
2906da
-    p = strchr(hostptr, ':');
2906da
+    p = ia5memchr(uri, hostptr, ':');
2906da
     /* Otherwise look for trailing slash */
2906da
-    if (!p)
2906da
-        p = strchr(hostptr, '/');
2906da
+    if (p == NULL)
2906da
+        p = ia5memchr(uri, hostptr, '/');
2906da
 
2906da
     if (!p)
2906da
         hostlen = strlen(hostptr);
2906da
@@ -461,7 +492,7 @@ static int nc_uri(ASN1_IA5STRING *uri, A
2906da
         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
2906da
 
2906da
     /* Special case: inital '.' is RHS match */
2906da
-    if (*baseptr == '.') {
2906da
+    if (base->length > 0 && *baseptr == '.') {
2906da
         if (hostlen > base->length) {
2906da
             p = hostptr + hostlen - base->length;
2906da
             if (!strncasecmp(p, baseptr, base->length))
2906da
diff -up openssl-1.0.2k/crypto/x509v3/v3_pci.c.read-buff openssl-1.0.2k/crypto/x509v3/v3_pci.c
2906da
--- openssl-1.0.2k/crypto/x509v3/v3_pci.c.read-buff	2017-01-26 14:22:04.000000000 +0100
2906da
+++ openssl-1.0.2k/crypto/x509v3/v3_pci.c	2021-11-19 12:14:17.572630962 +0100
2906da
@@ -68,7 +68,8 @@ static int i2r_pci(X509V3_EXT_METHOD *me
2906da
     i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
2906da
     BIO_puts(out, "\n");
2906da
     if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
2906da
-        BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
2906da
+        BIO_printf(out, "%*sPolicy Text: %.*s\n", indent, "",
2906da
+                   pci->proxyPolicy->policy->length,
2906da
                    pci->proxyPolicy->policy->data);
2906da
     return 1;
2906da
 }
2906da
diff -up openssl-1.0.2k/crypto/x509v3/v3_utl.c.read-buff openssl-1.0.2k/crypto/x509v3/v3_utl.c
2906da
--- openssl-1.0.2k/crypto/x509v3/v3_utl.c.read-buff	2017-01-26 14:22:04.000000000 +0100
2906da
+++ openssl-1.0.2k/crypto/x509v3/v3_utl.c	2021-11-19 12:14:17.573630970 +0100
2906da
@@ -609,17 +609,26 @@ static int append_ia5(STACK_OF(OPENSSL_S
2906da
     /* First some sanity checks */
2906da
     if (email->type != V_ASN1_IA5STRING)
2906da
         return 1;
2906da
-    if (!email->data || !email->length)
2906da
+    if (email->data == NULL || email->length == 0)
2906da
+        return 1;
2906da
+    if (memchr(email->data, 0, email->length) != NULL)
2906da
         return 1;
2906da
     if (!*sk)
2906da
         *sk = sk_OPENSSL_STRING_new(sk_strcmp);
2906da
     if (!*sk)
2906da
         return 0;
2906da
+
2906da
+    emtmp = OPENSSL_strndup((char *)email->data, email->length);
2906da
+    if (emtmp == NULL)
2906da
+        return 0;
2906da
+
2906da
     /* Don't add duplicates */
2906da
-    if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
2906da
+    if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) {
2906da
+        OPENSSL_free(emtmp);
2906da
         return 1;
2906da
-    emtmp = BUF_strdup((char *)email->data);
2906da
-    if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
2906da
+    }
d25b66
+    if (!sk_OPENSSL_STRING_push(*sk, emtmp)) {
d25b66
+        OPENSSL_free(emtmp); /* free on push failure */
2906da
         X509_email_free(*sk);
2906da
         *sk = NULL;
2906da
         return 0;