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

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