mrc0mmand / rpms / openldap

Forked from rpms/openldap 3 years ago
Clone

Blame SOURCES/openldap-openssl-ITS7595-Add-EC-support-1.patch

57672d
ITS#7595 Add Elliptic Curve support for OpenSSL
57672d
57672d
Cherry-picked upstream e631ce808ed56119e61321463d06db7999ba5a08
57672d
Author:    Howard Chu <hyc@openldap.org>
57672d
Date:      Sat Sep 7 09:47:19 2013 -0700
57672d
57672d
diff --git a/doc/man/man5/slapd-config.5 b/doc/man/man5/slapd-config.5
57672d
index 9c72e8296..2311c3096 100644
57672d
--- a/doc/man/man5/slapd-config.5
57672d
+++ b/doc/man/man5/slapd-config.5
57672d
@@ -922,6 +922,13 @@ are not used.
57672d
 When using Mozilla NSS these parameters are always generated randomly
57672d
 so this directive is ignored.
57672d
 .TP
57672d
+.B olcTLSECName: <name>
57672d
+Specify the name of a curve to use for Elliptic curve Diffie-Hellman
57672d
+ephemeral key exchange.  This is required to enable ECDHE algorithms in
57672d
+OpenSSL.  This option is not used with GnuTLS; the curves may be
57672d
+chosen in the GnuTLS ciphersuite specification. This option is also
57672d
+ignored for Mozilla NSS.
57672d
+.TP
57672d
 .B olcTLSProtocolMin: <major>[.<minor>]
57672d
 Specifies minimum SSL/TLS protocol version that will be negotiated.
57672d
 If the server doesn't support at least that version,
57672d
diff --git a/doc/man/man5/slapd.conf.5 b/doc/man/man5/slapd.conf.5
57672d
index f504adcf9..ef03e0ad8 100644
57672d
--- a/doc/man/man5/slapd.conf.5
57672d
+++ b/doc/man/man5/slapd.conf.5
57672d
@@ -1153,6 +1153,13 @@ are not used.
57672d
 When using Mozilla NSS these parameters are always generated randomly
57672d
 so this directive is ignored.
57672d
 .TP
57672d
+.B TLSECName <name>
57672d
+Specify the name of a curve to use for Elliptic curve Diffie-Hellman
57672d
+ephemeral key exchange.  This is required to enable ECDHE algorithms in
57672d
+OpenSSL.  This option is not used with GnuTLS; the curves may be
57672d
+chosen in the GnuTLS ciphersuite specification. This option is also
57672d
+ignored for Mozilla NSS.
57672d
+.TP
57672d
 .B TLSProtocolMin <major>[.<minor>]
57672d
 Specifies minimum SSL/TLS protocol version that will be negotiated.
57672d
 If the server doesn't support at least that version,
57672d
diff --git a/include/ldap.h b/include/ldap.h
57672d
index c245651c2..0964a193e 100644
57672d
--- a/include/ldap.h
57672d
+++ b/include/ldap.h
57672d
@@ -158,6 +158,7 @@ LDAP_BEGIN_DECL
57672d
 #define LDAP_OPT_X_TLS_NEWCTX		0x600f
57672d
 #define LDAP_OPT_X_TLS_CRLFILE		0x6010	/* GNUtls only */
57672d
 #define LDAP_OPT_X_TLS_PACKAGE		0x6011
57672d
+#define LDAP_OPT_X_TLS_ECNAME		0x6012
57672d
 
57672d
 #define LDAP_OPT_X_TLS_NEVER	0
57672d
 #define LDAP_OPT_X_TLS_HARD		1
57672d
diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h
57672d
index 66e04ae80..db7193f4f 100644
57672d
--- a/libraries/libldap/ldap-int.h
57672d
+++ b/libraries/libldap/ldap-int.h
57672d
@@ -165,6 +165,7 @@ struct ldaptls {
57672d
 	char		*lt_ciphersuite;
57672d
 	char		*lt_crlfile;
57672d
 	char		*lt_randfile;	/* OpenSSL only */
57672d
+	char		*lt_ecname;		/* OpenSSL only */
57672d
 	int		lt_protocol_min;
57672d
 };
57672d
 #endif
57672d
@@ -250,6 +251,7 @@ struct ldapoptions {
57672d
 #define ldo_tls_certfile	ldo_tls_info.lt_certfile
57672d
 #define ldo_tls_keyfile	ldo_tls_info.lt_keyfile
57672d
 #define ldo_tls_dhfile	ldo_tls_info.lt_dhfile
57672d
+#define ldo_tls_ecname	ldo_tls_info.lt_ecname
57672d
 #define ldo_tls_cacertfile	ldo_tls_info.lt_cacertfile
57672d
 #define ldo_tls_cacertdir	ldo_tls_info.lt_cacertdir
57672d
 #define ldo_tls_ciphersuite	ldo_tls_info.lt_ciphersuite
57672d
diff --git a/libraries/libldap/tls2.c b/libraries/libldap/tls2.c
57672d
index d25c190ea..0451b01af 100644
57672d
--- a/libraries/libldap/tls2.c
57672d
+++ b/libraries/libldap/tls2.c
57672d
@@ -118,6 +118,10 @@ ldap_int_tls_destroy( struct ldapoptions *lo )
57672d
 		LDAP_FREE( lo->ldo_tls_dhfile );
57672d
 		lo->ldo_tls_dhfile = NULL;
57672d
 	}
57672d
+	if ( lo->ldo_tls_ecname ) {
57672d
+		LDAP_FREE( lo->ldo_tls_ecname );
57672d
+		lo->ldo_tls_ecname = NULL;
57672d
+	}
57672d
 	if ( lo->ldo_tls_cacertfile ) {
57672d
 		LDAP_FREE( lo->ldo_tls_cacertfile );
57672d
 		lo->ldo_tls_cacertfile = NULL;
57672d
@@ -232,6 +236,10 @@ ldap_int_tls_init_ctx( struct ldapoptions *lo, int is_server )
57672d
 		lts.lt_dhfile = LDAP_STRDUP( lts.lt_dhfile );
57672d
 		__atoe( lts.lt_dhfile );
57672d
 	}
57672d
+	if ( lts.lt_ecname ) {
57672d
+		lts.lt_ecname = LDAP_STRDUP( lts.lt_ecname );
57672d
+		__atoe( lts.lt_ecname );
57672d
+	}
57672d
 #endif
57672d
 	lo->ldo_tls_ctx = ti->ti_ctx_new( lo );
57672d
 	if ( lo->ldo_tls_ctx == NULL ) {
57672d
@@ -257,6 +265,7 @@ error_exit:
57672d
 	LDAP_FREE( lts.lt_crlfile );
57672d
 	LDAP_FREE( lts.lt_cacertdir );
57672d
 	LDAP_FREE( lts.lt_dhfile );
57672d
+	LDAP_FREE( lts.lt_ecname );
57672d
 #endif
57672d
 	return rc;
57672d
 }
57672d
@@ -646,6 +655,10 @@ ldap_pvt_tls_get_option( LDAP *ld, int option, void *arg )
57672d
 		*(char **)arg = lo->ldo_tls_dhfile ?
57672d
 			LDAP_STRDUP( lo->ldo_tls_dhfile ) : NULL;
57672d
 		break;
57672d
+	case LDAP_OPT_X_TLS_ECNAME:
57672d
+		*(char **)arg = lo->ldo_tls_ecname ?
57672d
+			LDAP_STRDUP( lo->ldo_tls_ecname ) : NULL;
57672d
+		break;
57672d
 	case LDAP_OPT_X_TLS_CRLFILE:	/* GnuTLS only */
57672d
 		*(char **)arg = lo->ldo_tls_crlfile ?
57672d
 			LDAP_STRDUP( lo->ldo_tls_crlfile ) : NULL;
57672d
@@ -765,6 +778,10 @@ ldap_pvt_tls_set_option( LDAP *ld, int option, void *arg )
57672d
 		if ( lo->ldo_tls_dhfile ) LDAP_FREE( lo->ldo_tls_dhfile );
57672d
 		lo->ldo_tls_dhfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
57672d
 		return 0;
57672d
+	case LDAP_OPT_X_TLS_ECNAME:
57672d
+		if ( lo->ldo_tls_ecname ) LDAP_FREE( lo->ldo_tls_ecname );
57672d
+		lo->ldo_tls_ecname = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
57672d
+		return 0;
57672d
 	case LDAP_OPT_X_TLS_CRLFILE:	/* GnuTLS only */
57672d
 		if ( lo->ldo_tls_crlfile ) LDAP_FREE( lo->ldo_tls_crlfile );
57672d
 		lo->ldo_tls_crlfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
57672d
diff --git a/libraries/libldap/tls_o.c b/libraries/libldap/tls_o.c
57672d
index f24060b7e..1370923af 100644
57672d
--- a/libraries/libldap/tls_o.c
57672d
+++ b/libraries/libldap/tls_o.c
57672d
@@ -373,10 +373,9 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
57672d
 		return -1;
57672d
 	}
57672d
 
57672d
-	if ( lo->ldo_tls_dhfile ) {
57672d
-		DH *dh = NULL;
57672d
+	if ( is_server && lo->ldo_tls_dhfile ) {
57672d
+		DH *dh;
57672d
 		BIO *bio;
57672d
-		SSL_CTX_set_options( ctx, SSL_OP_SINGLE_DH_USE );
57672d
 
57672d
 		if (( bio=BIO_new_file( lt->lt_dhfile,"r" )) == NULL ) {
57672d
 			Debug( LDAP_DEBUG_ANY,
57672d
@@ -395,7 +394,35 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
57672d
 		}
57672d
 		BIO_free( bio );
57672d
 		SSL_CTX_set_tmp_dh( ctx, dh );
57672d
+		SSL_CTX_set_options( ctx, SSL_OP_SINGLE_DH_USE );
57672d
+		DH_free( dh );
57672d
+	}
57672d
+
57672d
+#ifdef SSL_OP_SINGLE_ECDH_USE
57672d
+	if ( is_server && lo->ldo_tls_ecname ) {
57672d
+		EC_KEY *ecdh;
57672d
+
57672d
+		int nid = OBJ_sn2nid( lt->lt_ecname );
57672d
+		if ( nid == NID_undef ) {
57672d
+			Debug( LDAP_DEBUG_ANY,
57672d
+				"TLS: could not use EC name `%s'.\n",
57672d
+				lo->ldo_tls_ecname,0,0);
57672d
+			tlso_report_error();
57672d
+			return -1;
57672d
+		}
57672d
+		ecdh = EC_KEY_new_by_curve_name( nid );
57672d
+		if ( ecdh == NULL ) {
57672d
+			Debug( LDAP_DEBUG_ANY,
57672d
+				"TLS: could not generate key for EC name `%s'.\n",
57672d
+				lo->ldo_tls_ecname,0,0);
57672d
+			tlso_report_error();
57672d
+			return -1;
57672d
+		}
57672d
+		SSL_CTX_set_tmp_ecdh( ctx, ecdh );
57672d
+		SSL_CTX_set_options( ctx, SSL_OP_SINGLE_ECDH_USE );
57672d
+		EC_KEY_free( ecdh );
57672d
 	}
57672d
+#endif
57672d
 
57672d
 	if ( tlso_opt_trace ) {
57672d
 		SSL_CTX_set_info_callback( ctx, tlso_info_cb );
57672d
diff --git a/servers/slapd/bconfig.c b/servers/slapd/bconfig.c
57672d
index 250f14100..8b1e4e582 100644
57672d
--- a/servers/slapd/bconfig.c
57672d
+++ b/servers/slapd/bconfig.c
57672d
@@ -194,6 +194,7 @@ enum {
57672d
 	CFG_ACL_ADD,
57672d
 	CFG_SYNC_SUBENTRY,
57672d
 	CFG_LTHREADS,
57672d
+	CFG_TLS_ECNAME,
57672d
 
57672d
 	CFG_LAST
57672d
 };
57672d
@@ -738,6 +739,14 @@ static ConfigTable config_back_cf_table[] = {
57672d
 #endif
57672d
 		"( OLcfgGlAt:77 NAME 'olcTLSDHParamFile' "
57672d
 			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
57672d
+	{ "TLSECName", NULL, 2, 2, 0,
57672d
+#ifdef HAVE_TLS
57672d
+		CFG_TLS_ECNAME|ARG_STRING|ARG_MAGIC, &config_tls_option,
57672d
+#else
57672d
+		ARG_IGNORED, NULL,
57672d
+#endif
57672d
+		"( OLcfgGlAt:96 NAME 'olcTLSECName' "
57672d
+			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
57672d
 	{ "TLSProtocolMin",	NULL, 2, 2, 0,
57672d
 #ifdef HAVE_TLS
57672d
 		CFG_TLS_PROTOCOL_MIN|ARG_STRING|ARG_MAGIC, &config_tls_config,
57672d
@@ -819,7 +828,7 @@ static ConfigOCs cf_ocs[] = {
57672d
 		 "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
57672d
 		 "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
57672d
 		 "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
57672d
-		 "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ "
57672d
+		 "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ olcTLSECName $ "
57672d
 		 "olcTLSCRLFile $ olcTLSProtocolMin $ olcToolThreads $ olcWriteTimeout $ "
57672d
 		 "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
57672d
 		 "olcDitContentRules $ olcLdapSyntaxes ) )", Cft_Global },
57672d
@@ -3824,6 +3833,7 @@ config_tls_option(ConfigArgs *c) {
57672d
 	case CFG_TLS_CA_PATH:	flag = LDAP_OPT_X_TLS_CACERTDIR;	break;
57672d
 	case CFG_TLS_CA_FILE:	flag = LDAP_OPT_X_TLS_CACERTFILE;	break;
57672d
 	case CFG_TLS_DH_FILE:	flag = LDAP_OPT_X_TLS_DHFILE;	break;
57672d
+	case CFG_TLS_ECNAME:	flag = LDAP_OPT_X_TLS_ECNAME;	break;
57672d
 #ifdef HAVE_GNUTLS
57672d
 	case CFG_TLS_CRL_FILE:	flag = LDAP_OPT_X_TLS_CRLFILE;	break;
57672d
 #endif