Blame SOURCES/openldap-tlso-use-openssl-api-to-verify-host.patch

ef59e1
From f2978fefa13eb92b73922e49d2f6c12b4f92ea85 Mon Sep 17 00:00:00 2001
ef59e1
From: Christian Heimes <christian@python.org>
ef59e1
Date: Fri, 10 Jan 2020 18:35:02 +0100
ef59e1
Subject: [PATCH] Use OpenSSL API to verify host
ef59e1
ef59e1
Replace custom hostname and IP address verification with OpenSSL 1.0.2
ef59e1
APIs.
ef59e1
---
ef59e1
 libraries/libldap/tls_o.c | 184 ++++++--------------------------------
ef59e1
 1 file changed, 28 insertions(+), 156 deletions(-)
ef59e1
ef59e1
diff --git a/libraries/libldap/tls_o.c b/libraries/libldap/tls_o.c
ef59e1
index e52c5507c..5adf7b74f 100644
ef59e1
--- a/libraries/libldap/tls_o.c
ef59e1
+++ b/libraries/libldap/tls_o.c
ef59e1
@@ -660,25 +660,15 @@ tlso_session_peer_dn( tls_session *sess, struct berval *der_dn )
ef59e1
 	return 0;
ef59e1
 }
ef59e1
 
ef59e1
-/* what kind of hostname were we given? */
ef59e1
-#define	IS_DNS	0
ef59e1
-#define	IS_IP4	1
ef59e1
-#define	IS_IP6	2
ef59e1
-
ef59e1
 static int
ef59e1
 tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
ef59e1
 {
ef59e1
 	tlso_session *s = (tlso_session *)sess;
ef59e1
-	int i, ret = LDAP_LOCAL_ERROR;
ef59e1
+	int ret = LDAP_LOCAL_ERROR;
ef59e1
 	X509 *x;
ef59e1
 	const char *name;
ef59e1
-	char *ptr;
ef59e1
-	int ntype = IS_DNS, nlen;
ef59e1
-#ifdef LDAP_PF_INET6
ef59e1
-	struct in6_addr addr;
ef59e1
-#else
ef59e1
-	struct in_addr addr;
ef59e1
-#endif
ef59e1
+	int flags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS;
ef59e1
+	ASN1_OCTET_STRING *ip;
ef59e1
 
ef59e1
 	if( ldap_int_hostname &&
ef59e1
 		( !name_in || !strcasecmp( name_in, "localhost" ) ) )
ef59e1
@@ -687,7 +677,6 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
ef59e1
 	} else {
ef59e1
 		name = name_in;
ef59e1
 	}
ef59e1
-	nlen = strlen(name);
ef59e1
 
ef59e1
 	x = tlso_get_cert(s);
ef59e1
 	if (!x) {
ef59e1
@@ -619,150 +619,32 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
ef59e1
 		return LDAP_SUCCESS;
ef59e1
 	}
ef59e1
 
ef59e1
-#ifdef LDAP_PF_INET6
ef59e1
-	if (inet_pton(AF_INET6, name, &addr)) {
ef59e1
-		ntype = IS_IP6;
ef59e1
-	} else 
ef59e1
-#endif
ef59e1
-	if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
ef59e1
-		if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
ef59e1
-	}
ef59e1
-	
ef59e1
-	i = X509_get_ext_by_NID(x, NID_subject_alt_name, -1);
ef59e1
-	if (i >= 0) {
ef59e1
-		X509_EXTENSION *ex;
ef59e1
-		STACK_OF(GENERAL_NAME) *alt;
ef59e1
-
ef59e1
-		ex = X509_get_ext(x, i);
ef59e1
-		alt = X509V3_EXT_d2i(ex);
ef59e1
-		if (alt) {
ef59e1
-			int n, len2 = 0;
ef59e1
-			char *domain = NULL;
ef59e1
-			GENERAL_NAME *gn;
ef59e1
-
ef59e1
-			if (ntype == IS_DNS) {
ef59e1
-				domain = strchr(name, '.');
ef59e1
-				if (domain) {
ef59e1
-					len2 = nlen - (domain-name);
ef59e1
-				}
ef59e1
-			}
ef59e1
-			n = sk_GENERAL_NAME_num(alt);
ef59e1
-			for (i=0; i
ef59e1
-				char *sn;
ef59e1
-				int sl;
ef59e1
-				gn = sk_GENERAL_NAME_value(alt, i);
ef59e1
-				if (gn->type == GEN_DNS) {
ef59e1
-					if (ntype != IS_DNS) continue;
ef59e1
-
ef59e1
-					sn = (char *) ASN1_STRING_data(gn->d.ia5);
ef59e1
-					sl = ASN1_STRING_length(gn->d.ia5);
ef59e1
-
ef59e1
-					/* ignore empty */
ef59e1
-					if (sl == 0) continue;
ef59e1
-
ef59e1
-					/* Is this an exact match? */
ef59e1
-					if ((nlen == sl) && !strncasecmp(name, sn, nlen)) {
ef59e1
-						break;
ef59e1
-					}
ef59e1
-
ef59e1
-					/* Is this a wildcard match? */
ef59e1
-					if (domain && (sn[0] == '*') && (sn[1] == '.') &&
ef59e1
-						(len2 == sl-1) && !strncasecmp(domain, &sn[1], len2))
ef59e1
-					{
ef59e1
-						break;
ef59e1
-					}
ef59e1
-
ef59e1
-				} else if (gn->type == GEN_IPADD) {
ef59e1
-					if (ntype == IS_DNS) continue;
ef59e1
-
ef59e1
-					sn = (char *) ASN1_STRING_data(gn->d.ia5);
ef59e1
-					sl = ASN1_STRING_length(gn->d.ia5);
ef59e1
-
ef59e1
-#ifdef LDAP_PF_INET6
ef59e1
-					if (ntype == IS_IP6 && sl != sizeof(struct in6_addr)) {
ef59e1
-						continue;
ef59e1
-					} else
ef59e1
-#endif
ef59e1
-					if (ntype == IS_IP4 && sl != sizeof(struct in_addr)) {
ef59e1
-						continue;
ef59e1
-					}
ef59e1
-					if (!memcmp(sn, &addr, sl)) {
ef59e1
-						break;
ef59e1
-					}
ef59e1
-				}
ef59e1
-			}
ef59e1
-
ef59e1
-			GENERAL_NAMES_free(alt);
ef59e1
-			if (i < n) {	/* Found a match */
ef59e1
-				ret = LDAP_SUCCESS;
ef59e1
-			}
ef59e1
-		}
ef59e1
-	}
ef59e1
-
ef59e1
-	if (ret != LDAP_SUCCESS) {
ef59e1
-		X509_NAME *xn;
ef59e1
-		X509_NAME_ENTRY *ne;
ef59e1
-		ASN1_OBJECT *obj;
ef59e1
-		ASN1_STRING *cn = NULL;
ef59e1
-		int navas;
ef59e1
-
ef59e1
-		/* find the last CN */
ef59e1
-		obj = OBJ_nid2obj( NID_commonName );
ef59e1
-		if ( !obj ) goto no_cn;	/* should never happen */
ef59e1
-
ef59e1
-		xn = X509_get_subject_name(x);
ef59e1
-		navas = X509_NAME_entry_count( xn );
ef59e1
-		for ( i=navas-1; i>=0; i-- ) {
ef59e1
-			ne = X509_NAME_get_entry( xn, i );
ef59e1
-			if ( !OBJ_cmp( X509_NAME_ENTRY_get_object(ne), obj )) {
ef59e1
-				cn = X509_NAME_ENTRY_get_data( ne );
ef59e1
-				break;
ef59e1
-			}
ef59e1
+	/* attempt to encode name as IP address */
ef59e1
+	ip = a2i_IPADDRESS(name);
ef59e1
+	if (ip == NULL) {
ef59e1
+		ERR_clear_error();
ef59e1
+		/* it's a hostname */
ef59e1
+		if (X509_check_host(x, name, strlen(name), flags, NULL) == 1) {
ef59e1
+			ret = LDAP_SUCCESS;
ef59e1
 		}
ef59e1
-
ef59e1
-		if( !cn )
ef59e1
-		{
ef59e1
-no_cn:
ef59e1
-			Debug( LDAP_DEBUG_ANY,
ef59e1
-				"TLS: unable to get common name from peer certificate.\n",
ef59e1
-				0, 0, 0 );
ef59e1
-			ret = LDAP_CONNECT_ERROR;
ef59e1
-			if ( ld->ld_error ) {
ef59e1
-				LDAP_FREE( ld->ld_error );
ef59e1
-			}
ef59e1
-			ld->ld_error = LDAP_STRDUP(
ef59e1
-				_("TLS: unable to get CN from peer certificate"));
ef59e1
-
ef59e1
-		} else if ( cn->length == nlen &&
ef59e1
-			strncasecmp( name, (char *) cn->data, nlen ) == 0 ) {
ef59e1
+	} else {
ef59e1
+		/* It's an IPv4 or IPv6 address */
ef59e1
+		if (X509_check_ip(x, ASN1_STRING_data(ip),
ef59e1
+		                  ASN1_STRING_length(ip), 0) == 1) {
ef59e1
 			ret = LDAP_SUCCESS;
ef59e1
-
ef59e1
-		} else if (( cn->data[0] == '*' ) && ( cn->data[1] == '.' )) {
ef59e1
-			char *domain = strchr(name, '.');
ef59e1
-			if( domain ) {
ef59e1
-				int dlen;
ef59e1
-
ef59e1
-				dlen = nlen - (domain-name);
ef59e1
-
ef59e1
-				/* Is this a wildcard match? */
ef59e1
-				if ((dlen == cn->length-1) &&
ef59e1
-					!strncasecmp(domain, (char *) &cn->data[1], dlen)) {
ef59e1
-					ret = LDAP_SUCCESS;
ef59e1
-				}
ef59e1
-			}
ef59e1
 		}
ef59e1
+		ASN1_OCTET_STRING_free(ip);
ef59e1
+	}
ef59e1
 
ef59e1
-		if( ret == LDAP_LOCAL_ERROR ) {
ef59e1
-			Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
ef59e1
-				"common name in certificate (%.*s).\n", 
ef59e1
-				name, cn->length, cn->data );
ef59e1
-			ret = LDAP_CONNECT_ERROR;
ef59e1
-			if ( ld->ld_error ) {
ef59e1
-				LDAP_FREE( ld->ld_error );
ef59e1
-			}
ef59e1
-			ld->ld_error = LDAP_STRDUP(
ef59e1
-				_("TLS: hostname does not match CN in peer certificate"));
ef59e1
+	if( ret == LDAP_LOCAL_ERROR ) {
ef59e1
+		Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
ef59e1
+			"peer certificate.\n", name, 0, 0);
ef59e1
+		ret = LDAP_CONNECT_ERROR;
ef59e1
+		if ( ld->ld_error ) {
ef59e1
+			LDAP_FREE( ld->ld_error );
ef59e1
 		}
ef59e1
+		ld->ld_error = LDAP_STRDUP(
ef59e1
+			_("TLS: hostname does not match peer certificate"));
ef59e1
 	}
ef59e1
 	X509_free(x);
ef59e1
 	return ret;