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

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