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

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