Blame SOURCES/openldap-cbinding-Add-channel-binding-support.patch

ef2d9b
From ca310ebff44f10739fd75aff437c7676e089b134 Mon Sep 17 00:00:00 2001
ef2d9b
From: Howard Chu <hyc@openldap.org>
ef2d9b
Date: Mon, 26 Aug 2013 23:31:48 -0700
ef2d9b
Subject: [PATCH] Add channel binding support
ef2d9b
ef2d9b
Currently only implemented for OpenSSL.
ef2d9b
Needs an option to set the criticality flag.
ef2d9b
---
ef2d9b
 include/ldap_pvt.h           |  1 +
ef2d9b
 libraries/libldap/cyrus.c    | 22 ++++++++++++++++++++++
ef2d9b
 libraries/libldap/ldap-int.h |  1 +
ef2d9b
 libraries/libldap/ldap-tls.h |  2 ++
ef2d9b
 libraries/libldap/tls2.c     |  7 +++++++
ef2d9b
 libraries/libldap/tls_g.c    |  7 +++++++
ef2d9b
 libraries/libldap/tls_m.c    |  7 +++++++
ef2d9b
 libraries/libldap/tls_o.c    | 16 ++++++++++++++++
ef2d9b
 servers/slapd/connection.c   |  8 ++++++++
ef2d9b
 servers/slapd/sasl.c         | 18 ++++++++++++++++++
ef2d9b
 servers/slapd/slap.h         |  1 +
ef2d9b
 11 files changed, 90 insertions(+)
ef2d9b
ef2d9b
diff --git a/include/ldap_pvt.h b/include/ldap_pvt.h
ef2d9b
index 871e7c180..fdc9d2de3 100644
ef2d9b
--- a/include/ldap_pvt.h
ef2d9b
+++ b/include/ldap_pvt.h
ef2d9b
@@ -430,6 +430,7 @@ LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
ef2d9b
 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
ef2d9b
 	LDAPDN_rewrite_dummy *func, unsigned flags ));
ef2d9b
 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
ef2d9b
+LDAP_F (int) ldap_pvt_tls_get_unique LDAP_P(( void *ctx, struct berval *buf, int is_server ));
ef2d9b
 
ef2d9b
 LDAP_END_DECL
ef2d9b
 
ef2d9b
diff --git a/libraries/libldap/cyrus.c b/libraries/libldap/cyrus.c
ef2d9b
index 28c241b0b..a57292800 100644
ef2d9b
--- a/libraries/libldap/cyrus.c
ef2d9b
+++ b/libraries/libldap/cyrus.c
ef2d9b
@@ -369,6 +369,10 @@ int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
ef2d9b
 		lc->lconn_sasl_sockctx = NULL;
ef2d9b
 		lc->lconn_sasl_authctx = NULL;
ef2d9b
 	}
ef2d9b
+	if( lc->lconn_sasl_cbind ) {
ef2d9b
+		ldap_memfree( lc->lconn_sasl_cbind );
ef2d9b
+		lc->lconn_sasl_cbind = NULL;
ef2d9b
+	}
ef2d9b
 
ef2d9b
 	return LDAP_SUCCESS;
ef2d9b
 }
ef2d9b
@@ -482,6 +486,24 @@ ldap_int_sasl_bind(
ef2d9b
 
ef2d9b
 			(void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
ef2d9b
 			LDAP_FREE( authid.bv_val );
ef2d9b
+#ifdef SASL_CHANNEL_BINDING	/* 2.1.25+ */
ef2d9b
+			{
ef2d9b
+				char cbinding[64];
ef2d9b
+				struct berval cbv = { sizeof(cbinding), cbinding };
ef2d9b
+				if ( ldap_pvt_tls_get_unique( ssl, &cbv, 0 )) {
ef2d9b
+					sasl_channel_binding_t *cb = ldap_memalloc( sizeof(*cb) +
ef2d9b
+						cbv.bv_len);
ef2d9b
+					cb->name = "ldap";
ef2d9b
+					cb->critical = 0;
ef2d9b
+					cb->data = (char *)(cb+1);
ef2d9b
+					cb->len = cbv.bv_len;
ef2d9b
+					memcpy( cb->data, cbv.bv_val, cbv.bv_len );
ef2d9b
+					sasl_setprop( ld->ld_defconn->lconn_sasl_authctx,
ef2d9b
+						SASL_CHANNEL_BINDING, cb );
ef2d9b
+					ld->ld_defconn->lconn_sasl_cbind = cb;
ef2d9b
+				}
ef2d9b
+			}
ef2d9b
+#endif
ef2d9b
 		}
ef2d9b
 #endif
ef2d9b
 
ef2d9b
diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h
ef2d9b
index 37c342e26..1915ecab4 100644
ef2d9b
--- a/libraries/libldap/ldap-int.h
ef2d9b
+++ b/libraries/libldap/ldap-int.h
ef2d9b
@@ -305,6 +305,7 @@ typedef struct ldap_conn {
ef2d9b
 #ifdef HAVE_CYRUS_SASL
ef2d9b
 	void		*lconn_sasl_authctx;	/* context for bind */
ef2d9b
 	void		*lconn_sasl_sockctx;	/* for security layer */
ef2d9b
+	void		*lconn_sasl_cbind;		/* for channel binding */
ef2d9b
 #endif
ef2d9b
 #ifdef HAVE_GSSAPI
ef2d9b
 	void		*lconn_gss_ctx;		/* gss_ctx_id_t */
ef2d9b
diff --git a/libraries/libldap/ldap-tls.h b/libraries/libldap/ldap-tls.h
ef2d9b
index 75661c005..1eb5ae47e 100644
ef2d9b
--- a/libraries/libldap/ldap-tls.h
ef2d9b
+++ b/libraries/libldap/ldap-tls.h
ef2d9b
@@ -41,6 +41,7 @@ typedef char *(TI_session_errmsg)(tls_session *s, int rc, char *buf, size_t len
ef2d9b
 typedef int (TI_session_dn)(tls_session *sess, struct berval *dn);
ef2d9b
 typedef int (TI_session_chkhost)(LDAP *ld, tls_session *s, const char *name_in);
ef2d9b
 typedef int (TI_session_strength)(tls_session *sess);
ef2d9b
+typedef int (TI_session_unique)(tls_session *sess, struct berval *buf, int is_server);
ef2d9b
 
ef2d9b
 typedef void (TI_thr_init)(void);
ef2d9b
 
ef2d9b
@@ -64,6 +65,7 @@ typedef struct tls_impl {
ef2d9b
 	TI_session_dn *ti_session_peer_dn;
ef2d9b
 	TI_session_chkhost *ti_session_chkhost;
ef2d9b
 	TI_session_strength *ti_session_strength;
ef2d9b
+	TI_session_unique *ti_session_unique;
ef2d9b
 
ef2d9b
 	Sockbuf_IO *ti_sbio;
ef2d9b
 
ef2d9b
diff --git a/libraries/libldap/tls2.c b/libraries/libldap/tls2.c
ef2d9b
index e11d1a8a3..957e73c03 100644
ef2d9b
--- a/libraries/libldap/tls2.c
ef2d9b
+++ b/libraries/libldap/tls2.c
ef2d9b
@@ -981,6 +981,13 @@ ldap_pvt_tls_get_my_dn( void *s, struct berval *dn, LDAPDN_rewrite_dummy *func,
ef2d9b
 		rc = ldap_X509dn2bv(&der_dn, dn, (LDAPDN_rewrite_func *)func, flags );
ef2d9b
 	return rc;
ef2d9b
 }
ef2d9b
+
ef2d9b
+int
ef2d9b
+ldap_pvt_tls_get_unique( void *s, struct berval *buf, int is_server )
ef2d9b
+{
ef2d9b
+	tls_session *session = s;
ef2d9b
+	return tls_imp->ti_session_unique( session, buf, is_server );
ef2d9b
+}
ef2d9b
 #endif /* HAVE_TLS */
ef2d9b
 
ef2d9b
 int
ef2d9b
diff --git a/libraries/libldap/tls_g.c b/libraries/libldap/tls_g.c
ef2d9b
index ed1f8f1cb..dfdc35da4 100644
ef2d9b
--- a/libraries/libldap/tls_g.c
ef2d9b
+++ b/libraries/libldap/tls_g.c
ef2d9b
@@ -780,6 +780,12 @@ tlsg_session_strength( tls_session *session )
ef2d9b
 	return gnutls_cipher_get_key_size( c ) * 8;
ef2d9b
 }
ef2d9b
 
ef2d9b
+static int
ef2d9b
+tlsg_session_unique( tls_session *sess, struct berval *buf, int is_server)
ef2d9b
+{
ef2d9b
+	return 0;
ef2d9b
+}
ef2d9b
+
ef2d9b
 /* suites is a string of colon-separated cipher suite names. */
ef2d9b
 static int
ef2d9b
 tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites )
ef2d9b
@@ -1110,6 +1116,7 @@ tls_impl ldap_int_tls_impl = {
ef2d9b
 	tlsg_session_peer_dn,
ef2d9b
 	tlsg_session_chkhost,
ef2d9b
 	tlsg_session_strength,
ef2d9b
+	tlsg_session_unique,
ef2d9b
 
ef2d9b
 	&tlsg_sbio,
ef2d9b
 
ef2d9b
diff --git a/libraries/libldap/tls_m.c b/libraries/libldap/tls_m.c
ef2d9b
index 072d41d56..240bd9ff6 100644
ef2d9b
--- a/libraries/libldap/tls_m.c
ef2d9b
+++ b/libraries/libldap/tls_m.c
ef2d9b
@@ -2838,6 +2838,12 @@ tlsm_session_strength( tls_session *session )
ef2d9b
 	return rc ? 0 : keySize;
ef2d9b
 }
ef2d9b
 
ef2d9b
+static int
ef2d9b
+tlsm_session_unique( tls_session *sess, struct berval *buf, int is_server)
ef2d9b
+{
ef2d9b
+	return 0;
ef2d9b
+}
ef2d9b
+
ef2d9b
 /*
ef2d9b
  * TLS support for LBER Sockbufs
ef2d9b
  */
ef2d9b
@@ -3266,6 +3272,7 @@ tls_impl ldap_int_tls_impl = {
ef2d9b
 	tlsm_session_peer_dn,
ef2d9b
 	tlsm_session_chkhost,
ef2d9b
 	tlsm_session_strength,
ef2d9b
+	tlsm_session_unique,
ef2d9b
 
ef2d9b
 	&tlsm_sbio,
ef2d9b
 
ef2d9b
diff --git a/libraries/libldap/tls_o.c b/libraries/libldap/tls_o.c
ef2d9b
index 3c077f895..2ecee465b 100644
ef2d9b
--- a/libraries/libldap/tls_o.c
ef2d9b
+++ b/libraries/libldap/tls_o.c
ef2d9b
@@ -676,6 +676,21 @@ tlso_session_strength( tls_session *sess )
ef2d9b
 	return SSL_CIPHER_get_bits(SSL_get_current_cipher(s), NULL);
ef2d9b
 }
ef2d9b
 
ef2d9b
+static int
ef2d9b
+tlso_session_unique( tls_session *sess, struct berval *buf, int is_server)
ef2d9b
+{
ef2d9b
+	tlso_session *s = (tlso_session *)sess;
ef2d9b
+
ef2d9b
+	/* Usually the client sends the finished msg. But if the
ef2d9b
+	 * session was resumed, the server sent the msg.
ef2d9b
+	 */
ef2d9b
+	if (SSL_session_reused(s) ^ !is_server)
ef2d9b
+		buf->bv_len = SSL_get_finished(s, buf->bv_val, buf->bv_len);
ef2d9b
+	else
ef2d9b
+		buf->bv_len = SSL_get_peer_finished(s, buf->bv_val, buf->bv_len);
ef2d9b
+	return buf->bv_len;
ef2d9b
+}
ef2d9b
+
ef2d9b
 /*
ef2d9b
  * TLS support for LBER Sockbufs
ef2d9b
  */
ef2d9b
@@ -1283,6 +1298,7 @@ tls_impl ldap_int_tls_impl = {
ef2d9b
 	tlso_session_peer_dn,
ef2d9b
 	tlso_session_chkhost,
ef2d9b
 	tlso_session_strength,
ef2d9b
+	tlso_session_unique,
ef2d9b
 
ef2d9b
 	&tlso_sbio,
ef2d9b
 
ef2d9b
diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c
ef2d9b
index e34703cb3..bc2b8a4d0 100644
ef2d9b
--- a/servers/slapd/connection.c
ef2d9b
+++ b/servers/slapd/connection.c
ef2d9b
@@ -406,6 +406,7 @@ Connection * connection_init(
ef2d9b
 		c->c_sasl_sockctx = NULL;
ef2d9b
 		c->c_sasl_extra = NULL;
ef2d9b
 		c->c_sasl_bindop = NULL;
ef2d9b
+		c->c_sasl_cbind = NULL;
ef2d9b
 
ef2d9b
 		c->c_sb = ber_sockbuf_alloc( );
ef2d9b
 
ef2d9b
@@ -451,6 +452,7 @@ Connection * connection_init(
ef2d9b
 	assert( c->c_sasl_sockctx == NULL );
ef2d9b
 	assert( c->c_sasl_extra == NULL );
ef2d9b
 	assert( c->c_sasl_bindop == NULL );
ef2d9b
+	assert( c->c_sasl_cbind == NULL );
ef2d9b
 	assert( c->c_currentber == NULL );
ef2d9b
 	assert( c->c_writewaiter == 0);
ef2d9b
 	assert( c->c_writers == 0);
ef2d9b
@@ -1408,6 +1410,12 @@ connection_read( ber_socket_t s, conn_readinfo *cri )
ef2d9b
 			    c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
ef2d9b
 			slap_sasl_external( c, c->c_tls_ssf, &authid );
ef2d9b
 			if ( authid.bv_val ) free( authid.bv_val );
ef2d9b
+			{
ef2d9b
+				char cbinding[64];
ef2d9b
+				struct berval cbv = { sizeof(cbinding), cbinding };
ef2d9b
+				if ( ldap_pvt_tls_get_unique( ssl, &cbv, 1 ))
ef2d9b
+					slap_sasl_cbinding( c, &cbv );
ef2d9b
+			}
ef2d9b
 		} else if ( rc == 1 && ber_sockbuf_ctrl( c->c_sb,
ef2d9b
 			LBER_SB_OPT_NEEDS_WRITE, NULL )) {	/* need to retry */
ef2d9b
 			slapd_set_write( s, 1 );
ef2d9b
diff --git a/servers/slapd/sasl.c b/servers/slapd/sasl.c
ef2d9b
index 0bd6259be..57907d79b 100644
ef2d9b
--- a/servers/slapd/sasl.c
ef2d9b
+++ b/servers/slapd/sasl.c
ef2d9b
@@ -1503,6 +1503,21 @@ int slap_sasl_external(
ef2d9b
 	return LDAP_SUCCESS;
ef2d9b
 }
ef2d9b
 
ef2d9b
+int slap_sasl_cbinding( Connection *conn, struct berval *cbv )
ef2d9b
+{
ef2d9b
+#ifdef SASL_CHANNEL_BINDING
ef2d9b
+	sasl_channel_binding_t *cb = ch_malloc( sizeof(*cb) + cbv->bv_len );;
ef2d9b
+	cb->name = "ldap";
ef2d9b
+	cb->critical = 0;
ef2d9b
+	cb->data = (char *)(cb+1);
ef2d9b
+	cb->len = cbv->bv_len;
ef2d9b
+	memcpy( cb->data, cbv->bv_val, cbv->bv_len );
ef2d9b
+	sasl_setprop( conn->c_sasl_authctx, SASL_CHANNEL_BINDING, cb );
ef2d9b
+	conn->c_sasl_cbind = cb;
ef2d9b
+#endif
ef2d9b
+	return LDAP_SUCCESS;
ef2d9b
+}
ef2d9b
+
ef2d9b
 int slap_sasl_reset( Connection *conn )
ef2d9b
 {
ef2d9b
 	return LDAP_SUCCESS;
ef2d9b
@@ -1568,6 +1583,9 @@ int slap_sasl_close( Connection *conn )
ef2d9b
 	free( conn->c_sasl_extra );
ef2d9b
 	conn->c_sasl_extra = NULL;
ef2d9b
 
ef2d9b
+	free( conn->c_sasl_cbind );
ef2d9b
+	conn->c_sasl_cbind = NULL;
ef2d9b
+
ef2d9b
 #elif defined(SLAP_BUILTIN_SASL)
ef2d9b
 	SASL_CTX *ctx = conn->c_sasl_authctx;
ef2d9b
 	if( ctx ) {
ef2d9b
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
ef2d9b
index 09c1854f8..4b3bbd12e 100644
ef2d9b
--- a/servers/slapd/slap.h
ef2d9b
+++ b/servers/slapd/slap.h
ef2d9b
@@ -2910,6 +2910,7 @@ struct Connection {
ef2d9b
 	void	*c_sasl_authctx;	/* SASL authentication context */
ef2d9b
 	void	*c_sasl_sockctx;	/* SASL security layer context */
ef2d9b
 	void	*c_sasl_extra;		/* SASL session extra stuff */
ef2d9b
+	void	*c_sasl_cbind;		/* SASL channel binding */
ef2d9b
 	Operation	*c_sasl_bindop;	/* set to current op if it's a bind */
ef2d9b
 
ef2d9b
 #ifdef LDAP_X_TXN
ef2d9b
-- 
ef2d9b
2.26.2
ef2d9b