Hi everyone,

I am relative new to haproxy and while setting up haproxy I stumbled upon the problem that I could not configure some ssl settings (lifetime, maxrecord) per socket.

I attached two patches which allow overwriting of these global configuration options per bind directive. To preserve backwards compatibility with already existing configurations these new options will fallback to the global configuration if their not set.

If there is anything wrong with the patch or if you need more information please let me know.

Thanks a lot and keep up the good work!
Tim
>From 363752110a4f856f9ad8da6e4d50496b8efc95ca Mon Sep 17 00:00:00 2001
From: Tim Pambor <[email protected]>
Date: Tue, 29 Jul 2014 21:03:18 +0200
Subject: [PATCH 1/2] Add bind option ssl-lifetime

Add the possibility to override the global configuration option tune.ssl.lifetime per bind option via a new option ssl-lifetime.

Signed-off-by: Tim Pambor <[email protected]>
---
 doc/configuration.txt    |  5 +++++
 include/types/listener.h |  1 +
 src/ssl_sock.c           | 28 +++++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index 90052e3..0da44ee 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -8651,6 +8651,11 @@ ssl
   appear in clear text, so that ACLs and HTTP processing will only have access
   to deciphered contents.
 
+ssl-lifetime <timeout>
+  Sets how long a cached SSL session may remain valid. This time is expressed
+  in seconds and overwrites tune.ssl.lifetime if set. For more information see
+  tune.ssl.lifetime.
+
 strict-sni
   This setting is only available when support for OpenSSL was built in. The
   SSL/TLS negotiation is allow only if the client provided an SNI which match
diff --git a/include/types/listener.h b/include/types/listener.h
index 83b63af..390331b 100644
--- a/include/types/listener.h
+++ b/include/types/listener.h
@@ -130,6 +130,7 @@ struct bind_conf {
 	char *alpn_str;            /* ALPN protocol string */
 	int alpn_len;              /* ALPN protocol string length */
 	int strict_sni;            /* refuse negotiation if sni doesn't match a certificate */
+	unsigned int ssllifetime;  /* SSL session lifetime in seconds */
 	struct eb_root sni_ctx;    /* sni_ctx tree of all known certs full-names sorted by name */
 	struct eb_root sni_w_ctx;  /* sni_ctx tree of all known certs wildcards sorted by name */
 #endif
diff --git a/src/ssl_sock.c b/src/ssl_sock.c
index cf8adc7..ff73e75 100644
--- a/src/ssl_sock.c
+++ b/src/ssl_sock.c
@@ -1565,7 +1565,9 @@ int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy
 		ERR_clear_error();
 	}
 
-	if (global.tune.ssllifetime)
+	if (bind_conf->ssllifetime)
+		SSL_CTX_set_timeout(ctx, bind_conf->ssllifetime);
+	else if (global.tune.ssllifetime)
 		SSL_CTX_set_timeout(ctx, global.tune.ssllifetime);
 
 	shared_context_set_cache(ctx);
@@ -4061,6 +4063,29 @@ static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct
 	return 0;
 }
 
+/* parse the "ssl-lifetime" bind keyword */
+static int bind_parse_ssl_lifetime(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
+{
+	unsigned int ssllifetime;
+	const char *res;
+
+	if (!*(args[cur_arg + 1])) {
+		if (err)
+			memprintf(err, "'%s' : expects ssl sessions <lifetime> in seconds as argument.\n", args[cur_arg]);
+		return ERR_ALERT | ERR_FATAL;
+	}
+
+	res = parse_time_err(args[cur_arg + 1], &ssllifetime, TIME_UNIT_S);
+	if (res) {
+		if (err)
+			memprintf(err, "'%s' : unexpected character '%c' in argument.\n", args[cur_arg], *res);
+		return ERR_ALERT | ERR_FATAL;
+	}
+
+	conf->ssllifetime = ssllifetime;
+	return 0;
+}
+
 /************** "server" keywords ****************/
 
 /* parse the "ca-file" server keyword */
@@ -4368,6 +4393,7 @@ static struct bind_kw_list bind_kws = { "SSL", { }, {
 	{ "no-tlsv12",             bind_parse_no_tlsv12,      0 }, /* disable TLSv12 */
 	{ "no-tls-tickets",        bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
 	{ "ssl",                   bind_parse_ssl,            0 }, /* enable SSL processing */
+	{ "ssl-lifetime",          bind_parse_ssl_lifetime,   1 }, /* set SSL session lifetime */
 	{ "strict-sni",            bind_parse_strict_sni,     0 }, /* refuse negotiation if sni doesn't match a certificate */
 	{ "verify",                bind_parse_verify,         1 }, /* set SSL verify method */
 	{ "npn",                   bind_parse_npn,            1 }, /* set NPN supported protocols */
-- 
1.9.1

>From 2dae347dffc71f3362e3fe64a07b5b9c41ab786a Mon Sep 17 00:00:00 2001
From: Tim Pambor <[email protected]>
Date: Tue, 29 Jul 2014 22:40:26 +0200
Subject: [PATCH 2/2] Add bind option ssl-maxrecord

Add the possibility to override the global configuration option tune.ssl.maxrecord per bind option via a new option ssl-maxrecord.

Signed-off-by: Tim Pambor <[email protected]>
---
 doc/configuration.txt    |  5 +++++
 include/types/listener.h |  1 +
 src/ssl_sock.c           | 27 ++++++++++++++++++++++++---
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index 0da44ee..fbe2ea3 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -8656,6 +8656,11 @@ ssl-lifetime <timeout>
   in seconds and overwrites tune.ssl.lifetime if set. For more information see
   tune.ssl.lifetime.
 
+ssl-maxrecord <number>
+  Sets the maximum amount of bytes passed to SSL_write() at a time. This option
+  overwrites tune.ssl.maxrecord if set. For detailed information see
+  tune.ssl.maxrecord.
+
 strict-sni
   This setting is only available when support for OpenSSL was built in. The
   SSL/TLS negotiation is allow only if the client provided an SNI which match
diff --git a/include/types/listener.h b/include/types/listener.h
index 390331b..1c2313d 100644
--- a/include/types/listener.h
+++ b/include/types/listener.h
@@ -131,6 +131,7 @@ struct bind_conf {
 	int alpn_len;              /* ALPN protocol string length */
 	int strict_sni;            /* refuse negotiation if sni doesn't match a certificate */
 	unsigned int ssllifetime;  /* SSL session lifetime in seconds */
+	unsigned int ssl_max_record; /* SSL max record size */
 	struct eb_root sni_ctx;    /* sni_ctx tree of all known certs full-names sorted by name */
 	struct eb_root sni_w_ctx;  /* sni_ctx tree of all known certs wildcards sorted by name */
 #endif
diff --git a/src/ssl_sock.c b/src/ssl_sock.c
index ff73e75..9dbaf9e 100644
--- a/src/ssl_sock.c
+++ b/src/ssl_sock.c
@@ -2399,7 +2399,8 @@ static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int coun
  */
 static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
 {
-	int ret, try, done;
+	int ret, try, done, max_record;
+	struct listener *listener;
 
 	done = 0;
 
@@ -2410,6 +2411,12 @@ static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int fl
 		/* a handshake was requested */
 		return 0;
 
+	listener = objt_listener(conn->target);
+	if (listener && listener->bind_conf->ssl_max_record)
+		max_record = listener->bind_conf->ssl_max_record;
+	else
+		max_record = global.tune.ssl_max_record;
+
 	/* send the largest possible block. For this we perform only one call
 	 * to send() unless the buffer wraps and we exactly fill the first hunk,
 	 * in which case we accept to do it once again.
@@ -2419,8 +2426,8 @@ static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int fl
 
 		if (!(flags & CO_SFL_STREAMER) &&
 		    !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
-		    global.tune.ssl_max_record && try > global.tune.ssl_max_record) {
-			try = global.tune.ssl_max_record;
+		    max_record && try > max_record) {
+			try = max_record;
 		}
 		else {
 			/* we need to keep the information about the fact that
@@ -4086,6 +4093,19 @@ static int bind_parse_ssl_lifetime(char **args, int cur_arg, struct proxy *px, s
 	return 0;
 }
 
+/* parse the "ssl-maxrecord" bind keyword */
+static int bind_parse_ssl_maxrecord(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
+{
+	if (!*(args[cur_arg + 1])) {
+		if (err)
+			memprintf(err, "'%s' : expects an integer argument.\n", args[cur_arg]);
+		return ERR_ALERT | ERR_FATAL;
+	}
+
+	conf->ssl_max_record = atol(args[cur_arg + 1]);
+	return 0;
+}
+
 /************** "server" keywords ****************/
 
 /* parse the "ca-file" server keyword */
@@ -4394,6 +4414,7 @@ static struct bind_kw_list bind_kws = { "SSL", { }, {
 	{ "no-tls-tickets",        bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
 	{ "ssl",                   bind_parse_ssl,            0 }, /* enable SSL processing */
 	{ "ssl-lifetime",          bind_parse_ssl_lifetime,   1 }, /* set SSL session lifetime */
+	{ "ssl-maxrecord",         bind_parse_ssl_maxrecord,  1 }, /* set SSL max record size */
 	{ "strict-sni",            bind_parse_strict_sni,     0 }, /* refuse negotiation if sni doesn't match a certificate */
 	{ "verify",                bind_parse_verify,         1 }, /* set SSL verify method */
 	{ "npn",                   bind_parse_npn,            1 }, /* set NPN supported protocols */
-- 
1.9.1

Reply via email to