Skip to contentSkip to conversation

tls: Add support l_tls_set_alpn_list() and ALPN extension

PATCH In review Marcel Holtmann 2 participants 4 messages Jan 3, 2023 – Jan 9, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 tls: Add support l_tls_set_alpn_list() and ALPN extension

Add support for application layer protocol negotiation (ALPN) and provide API for setting the list of client protocols and for getting the server selected protocol.

---
 ell/ell.sym          |  2 ++
 ell/tls-extensions.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
 ell/tls-private.h    |  2 ++
 ell/tls.c            | 21 +++++++++++++
 ell/tls.h            |  3 ++
 5 files changed, 101 insertions(+)
ell/ell.sym +2 -0
index f8148f257eb1..81c0cb7f5b94 100644
@@ -515,6 +515,8 @@ global:
 	l_tls_close;
 	l_tls_reset;
 	l_tls_set_server_name;
+	l_tls_set_alpn_list;
+	l_tls_get_alpn;
 	l_tls_set_cacert;
 	l_tls_set_auth_data;
 	l_tls_set_version_range;
ell/tls-extensions.c +73 -0

85 lines not on this page · raw message

ell/tls-private.h +2 -0
index ac477885c5f7..dbc5457ef091 100644
@@ -218,6 +218,8 @@ struct l_tls {
 
 	struct tls_cipher_suite **cipher_suite_pref_list;
 	char *server_name;
+	char **alpn_list;
+	char *selected_alpn;
 
 	struct l_settings *session_settings;
 	char *session_prefix;
ell/tls.c +21 -0
index 9556efd932bc..8c1c9040ff89 100644
@@ -3421,6 +3421,8 @@ LIB_EXPORT void l_tls_free(struct l_tls *tls)
 		l_free(tls->cipher_suite_pref_list);
 
 	l_free(tls->server_name);
+	l_strv_free(tls->alpn_list);
+	l_free(tls->selected_alpn);
 	l_free(tls);
 }
 
@@ -3668,6 +3670,25 @@ LIB_EXPORT bool l_tls_set_server_name(struct l_tls *tls, const char *name)
 	return true;
 }
 
+LIB_EXPORT bool l_tls_set_alpn_list(struct l_tls *tls, const char **list)
+{
+	if (!tls)
+		return false;
+
+	l_strv_free(tls->alpn_list);
+	tls->alpn_list = l_strv_copy((char **) list);
+
+	return true;
+}
+
+LIB_EXPORT const char *l_tls_get_alpn(struct l_tls *tls)
+{
+	if (!tls)
+		return NULL;
+
+	return tls->selected_alpn;
+}
+
 LIB_EXPORT bool l_tls_set_cacert(struct l_tls *tls, struct l_queue *ca_certs)
 {
 	if (tls->ca_certs) {
ell/tls.h +3 -0
index c931b5db0a54..e33f7c070008 100644
@@ -105,6 +105,9 @@ void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len);
 
 bool l_tls_set_server_name(struct l_tls *tls, const char *name);
 
+bool l_tls_set_alpn_list(struct l_tls *tls, const char **list);
+const char *l_tls_get_alpn(struct l_tls *tls);
+
 /*
  * If peer is to be authenticated, supply the CA certificates.  On success
  * the l_tls object takes ownership of the queue and the individual l_cert
Signature · 1 line
2.39.0
#1 lore raw

Hi Marcel,

On Tue, 3 Jan 2023 at 23:03, Marcel Holtmann <[email protected]> wrote:
[...]

ell/tls-extensions.c static ssize_t tls_server_name_client_write(struct l_tls *tls, · line 109

Quoted 4 lines
ell/tls-extensions.c +4 -0
+static bool tls_alpn_server_absent(struct l_tls *tls)
+{
+       if (!tls->alpn_list)
+               return false;

I believe this returning false will break most usages of l_tls.

Quoted 15 lines
ell/tls-extensions.c +9 -0
+
+       l_free(tls->selected_alpn);
+       tls->selected_alpn = NULL;
+
+       TLS_DEBUG("ALPN not supported");
+
+       return true;
+}
+
 /* Most extensions are not used when resuming a cached session */
 #define SKIP_ON_RESUMPTION()   \
        do {    \
@@ -1025,6 +1089,15 @@ const struct tls_hello_extension tls_extensions[] = {
                tls_signature_algorithms_client_absent,
                NULL, NULL, NULL,

ell/tls-extensions.c const struct tls_hello_extension tls_extensions[] = { · line 1100

Quoted 10 lines
ell/tls-extensions.c +9 -0
        },
+       {
+               "ALPN", "application_layer_protocol_negotiation", 16,
+               tls_alpn_client_write,
+               NULL,
+               NULL,
+               NULL,
+               tls_alpn_server_handle,
+               tls_alpn_server_absent,
+       },

If only the client side is implemented you might want to add a hint either in the .h comment for l_tls_set_alpn_list(), or even in its name.

Quoted 21 lines
ell/tls-extensions.c +0 -0
        {
                "Secure Renegotiation", "renegotiation_info", 0xff01,
                tls_renegotiation_info_client_write,
ell/tls-private.h +2 -0
index ac477885c5f7..dbc5457ef091 100644
@@ -218,6 +218,8 @@ struct l_tls {
 
        struct tls_cipher_suite **cipher_suite_pref_list;
        char *server_name;
+       char **alpn_list;
+       char *selected_alpn;
 
        struct l_settings *session_settings;
        char *session_prefix;
ell/tls.c +0 -0
index 9556efd932bc..8c1c9040ff89 100644

ell/tls.c LIB_EXPORT void l_tls_free(struct l_tls *tls) · line 3424

Quoted 4 lines
ell/tls.c +1 -0
@@ -3421,6 +3421,8 @@ LIB_EXPORT void l_tls_free(struct l_tls *tls)
                l_free(tls->cipher_suite_pref_list);
 
        l_free(tls->server_name);
+       l_strv_free(tls->alpn_list);

Would perhaps l_tls_set_alpn_list(tls, NULL) be more future proof?

Quoted 35 lines
ell/tls.c +20 -0
+       l_free(tls->selected_alpn);
        l_free(tls);
 }
 
@@ -3668,6 +3670,25 @@ LIB_EXPORT bool l_tls_set_server_name(struct l_tls *tls, const char *name)
        return true;
 }
 
+LIB_EXPORT bool l_tls_set_alpn_list(struct l_tls *tls, const char **list)
+{
+       if (!tls)
+               return false;
+
+       l_strv_free(tls->alpn_list);
+       tls->alpn_list = l_strv_copy((char **) list);
+
+       return true;
+}
+
+LIB_EXPORT const char *l_tls_get_alpn(struct l_tls *tls)
+{
+       if (!tls)
+               return NULL;
+
+       return tls->selected_alpn;
+}
+
 LIB_EXPORT bool l_tls_set_cacert(struct l_tls *tls, struct l_queue *ca_certs)
 {
        if (tls->ca_certs) {
ell/tls.h +0 -0
index c931b5db0a54..e33f7c070008 100644

ell/tls.h void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len); · line 109

Quoted 5 lines
ell/tls.h +2 -0
@@ -105,6 +105,9 @@ void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len);
 
 bool l_tls_set_server_name(struct l_tls *tls, const char *name);
 
+bool l_tls_set_alpn_list(struct l_tls *tls, const char **list);
+const char *l_tls_get_alpn(struct l_tls *tls);

Since these and l_tls_set_server_name are extensions, I'd move them further down in the file after the basic API, maybe aronud l_tls_set_domain_mask. Both patchsets look good otherwise.

Best regards

#2 lore raw

Hi Andrew,

Quoted 6 lines

ell/tls-extensions.c static ssize_t tls_server_name_client_write(struct l_tls *tls, · line 109

Quoted 4 lines
ell/tls-extensions.c +4 -0
+static bool tls_alpn_server_absent(struct l_tls *tls)
+{
+       if (!tls->alpn_list)
+               return false;

I believe this returning false will break most usages of l_tls.

can you elaborate why? When are you allowed to return false.

Quoted 22 lines
Quoted 22 lines
ell/tls-extensions.c +9 -0
+
+       l_free(tls->selected_alpn);
+       tls->selected_alpn = NULL;
+
+       TLS_DEBUG("ALPN not supported");
+
+       return true;
+}
+

/* Most extensions are not used when resuming a cached session */

#define SKIP_ON_RESUMPTION()   \
       do {    \
ell/tls-extensions.c +5 -0
@@ -1025,6 +1089,15 @@ const struct tls_hello_extension tls_extensions[] = {
               tls_signature_algorithms_client_absent,
               NULL, NULL, NULL,
       },
+       {
+               "ALPN", "application_layer_protocol_negotiation", 16,
+               tls_alpn_client_write,
+               NULL,
+               NULL,
Quoted 8 lines

ell/tls-extensions.c const struct tls_hello_extension tls_extensions[] = { · line 1100

Quoted 4 lines
ell/tls-extensions.c +4 -0
+               NULL,
+               tls_alpn_server_handle,
+               tls_alpn_server_absent,
+       },

If only the client side is implemented you might want to add a hint either in the .h comment for l_tls_set_alpn_list(), or even in its name.

Good point, I think that I just implement the server side since that is generally useful there as well. I was just focused on the client.

Quoted 23 lines
Quoted 23 lines
ell/tls-extensions.c +0 -0
       {
               "Secure Renegotiation", "renegotiation_info", 0xff01,
               tls_renegotiation_info_client_write,
ell/tls-private.h +2 -0
index ac477885c5f7..dbc5457ef091 100644
@@ -218,6 +218,8 @@ struct l_tls {
 
       struct tls_cipher_suite **cipher_suite_pref_list;
       char *server_name;
+       char **alpn_list;
+       char *selected_alpn;
 
       struct l_settings *session_settings;
       char *session_prefix;
ell/tls.c +0 -0
index 9556efd932bc..8c1c9040ff89 100644
@@ -3421,6 +3421,8 @@ LIB_EXPORT void l_tls_free(struct l_tls *tls)
               l_free(tls->cipher_suite_pref_list);
 
Quoted 4 lines

ell/tls.c LIB_EXPORT void l_tls_free(struct l_tls *tls) · line 3424

Quoted 2 lines
ell/tls.c +1 -0
       l_free(tls->server_name);
+       l_strv_free(tls->alpn_list);

Would perhaps l_tls_set_alpn_list(tls, NULL) be more future proof?

Let me check when I have the server side done.

Quoted 36 lines
Quoted 36 lines
ell/tls.c +20 -0
+       l_free(tls->selected_alpn);
       l_free(tls);
 }
 
@@ -3668,6 +3670,25 @@ LIB_EXPORT bool l_tls_set_server_name(struct l_tls *tls, const char *name)
       return true;
 }
 
+LIB_EXPORT bool l_tls_set_alpn_list(struct l_tls *tls, const char **list)
+{
+       if (!tls)
+               return false;
+
+       l_strv_free(tls->alpn_list);
+       tls->alpn_list = l_strv_copy((char **) list);
+
+       return true;
+}
+
+LIB_EXPORT const char *l_tls_get_alpn(struct l_tls *tls)
+{
+       if (!tls)
+               return NULL;
+
+       return tls->selected_alpn;
+}
+
 LIB_EXPORT bool l_tls_set_cacert(struct l_tls *tls, struct l_queue *ca_certs)
 {
       if (tls->ca_certs) {
ell/tls.h +0 -0
index c931b5db0a54..e33f7c070008 100644
@@ -105,6 +105,9 @@ void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len);
 
Quoted 8 lines

ell/tls.h void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len); · line 109

Quoted 4 lines
ell/tls.h +2 -0
 bool l_tls_set_server_name(struct l_tls *tls, const char *name);
 
+bool l_tls_set_alpn_list(struct l_tls *tls, const char **list);
+const char *l_tls_get_alpn(struct l_tls *tls);

Since these and l_tls_set_server_name are extensions, I'd move them further down in the file after the basic API, maybe aronud l_tls_set_domain_mask. Both patchsets look good otherwise.

Generally I would agree, but since with TLS 1.3 everything is an extension anyway, I think such argument is void these days. And logically the SNI and ALPN are pretty much core concept of TLS these days. TLS 1.3 requires SNI to be supported even while the usage is optional. There are really just only rare cases where you would not use SNI and thus making this pretty much a core feature of TLS now.

The same applies to ALPN and what I have seen from IETF, they did make sure ALPN is in their RFCs and gets used to identify the application protocol.

Regards

Marcel

#3 lore raw

Hi Marcel,

Quoted 8 lines · Marcel Holtmann
Quoted 6 lines

ell/tls-extensions.c static ssize_t tls_server_name_client_write(struct l_tls *tls, · line 109

Quoted 4 lines
ell/tls-extensions.c +4 -0
+static bool tls_alpn_server_absent(struct l_tls *tls)
+{
+       if (!tls->alpn_list)
+               return false;

I believe this returning false will break most usages of l_tls.

can you elaborate why? When are you allowed to return false.

false here means that the extension should have been present, i.e. we signal an error. If the extension code thinks we're fine to proceed it returns true.

This also makes the unit test fail.

Best regards and happy new year