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