Skip to contentSkip to conversation

tls: Only accept the Certificate Request in client mode

PATCH Applied Andrew Zaborowski 2 participants 27 messages Jan 1, 2019 – Jan 3, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/17 tls: Only accept the Certificate Request in client mode

---
 ell/tls.c | 6 ++++++
 1 file changed, 6 insertions(+)
ell/tls.c +6 -0
index eab9cc2..f4cd94c 100644
@@ -2312,6 +2312,12 @@ static void tls_handle_handshake(struct l_tls *tls, int type,
 		break;
 
 	case TLS_CERTIFICATE_REQUEST:
+		if (tls->server) {
+			TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
+					"Message invalid in server mode");
+			break;
+		}
+
 		/*
 		 * Server sends this optionally so in the WAIT_HELLO_DONE
 		 * state we accept either this or a Server Hello Done (below).
Signature · 1 line
2.19.1
#1 lore raw

PATCH 2/17 tls: Add Hello extension sending support

Declare a struct for ServerHello/ClientHello extensions with write and handle callbacks, modify our ServerHello and ClientHello message builders to append the extensions to the messages.

---
 ell/tls-private.h |  18 +++++++++
 ell/tls.c         | 101 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 112 insertions(+), 7 deletions(-)
ell/tls-private.h +18 -0
index 0908b9c..5ea8208 100644
@@ -99,6 +99,24 @@ struct tls_compression_method {
 	const char *name;
 };
 
+struct tls_hello_extension {
+	const char *name;
+	const char *short_name;
+	uint16_t id;
+	ssize_t (*client_write)(struct l_tls *tls, uint8_t *buf, size_t len);
+	/* Handle a Client Hello extension (on server), can't be NULL */
+	bool (*client_handle)(struct l_tls *tls,
+				const uint8_t *buf, size_t len);
+	/* Handle a Client Hello extension's absence (on server) */
+	bool (*client_handle_absent)(struct l_tls *tls);
+	ssize_t (*server_write)(struct l_tls *tls, uint8_t *buf, size_t len);
+	/* Handle a Server Hello extension (on client) */
+	bool (*server_handle)(struct l_tls *tls,
+				const uint8_t *buf, size_t len);
+	/* Handle a Server Hello extension's absence (on client) */
+	bool (*server_handle_absent)(struct l_tls *tls);
+};
+
 enum tls_handshake_state {
 	TLS_HANDSHAKE_WAIT_START,
 	TLS_HANDSHAKE_WAIT_HELLO,
ell/tls.c +94 -7

149 lines not on this page · raw message

Signature · 1 line
2.19.1
#4 lore raw

PATCH 3/17 tls: Parse and process received extensions

In reply to Andrew Zaborowski

On the server call the .client_handle callbacks for each supported extension in the received ClientHello and pass the list of extensions we've handled to the ServerHello builder. On the client, process the extension responses in the ServerHello by calling .server_handle callbacks.

---
 ell/tls.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 154 insertions(+), 25 deletions(-)
ell/tls.c +154 -25
index ffa09e1..f8ba3d0 100644
@@ -1577,6 +1577,135 @@ static bool tls_verify_finished(struct l_tls *tls, const uint8_t *received,
 	return true;
 }
 
+static bool tls_ptr_match(const void *a, const void *b)
+{
+	return a == b;
+}
+
+static bool tls_handle_hello_extensions(struct l_tls *tls,
+					const uint8_t *buf, size_t len,
+					struct l_queue *seen)
+{
+	unsigned int i;
+	const struct tls_hello_extension *extension;
+	bool client_hello = tls->server;
+	uint16_t extensions_size;
+
+	if (!len)
+		return true;
+
+	if (len < 2 || len > 2 + 65535)
+		goto decode_error;
+
+	extensions_size = l_get_be16(buf);
+	len -= 2;
+	buf += 2;
+
+	if (len != extensions_size)
+		goto decode_error;
+
+	while (len) {
+		uint16_t ext_id;
+		size_t ext_len;
+		bool (*handler)(struct l_tls *tls,
+				const uint8_t *buf, size_t len);
+
+		if (len < 4)
+			goto decode_error;
+
+		ext_id = l_get_be16(buf + 0);
+		ext_len = l_get_be16(buf + 2);
+		buf += 4;
+		len -= 4;
+
+		if (ext_len > len)
+			goto decode_error;
+
+		/*
+		 * RFC 5246, Section 7.4.1.4: "There MUST NOT be more than
+		 * one extension of the same type."
+		 */
+		if (l_queue_find(seen, tls_ptr_match, L_UINT_TO_PTR(ext_id))) {
+			TLS_DEBUG("Duplicate extension %u", ext_id);
+			goto decode_error;
+		}
+
+		l_queue_push_tail(seen, L_UINT_TO_PTR(ext_id));
+
+		extension = NULL;
+
+		for (i = 0; tls_extensions[i].name; i++)
+			if (tls_extensions[i].id == ext_id) {
+				extension = &tls_extensions[i];
+				break;
+			}
+
+		if (!extension)
+			goto next;
+
+		handler = client_hello ?
+			extension->client_handle : extension->server_handle;
+
+		/*
+		 * RFC 5246, Section 7.4.1.4: "If a client receives an
+		 * extension type in ServerHello that it did not request in
+		 * the associated ClientHello, it MUST abort the handshake
+		 * with an unsupported_extension fatal alert."
+		 */
+		if (!client_hello && !handler) {
+			TLS_DISCONNECT(TLS_ALERT_UNSUPPORTED_EXTENSION, 0,
+					"%s extension not expected in "
+					"a ServerHello", extension->name);
+			return false;
+		}
+
+		if (!handler(tls, buf, ext_len)) {
+			TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
+					"Hello %s extension parse error",
+					extension->name);
+			return false;
+		}
+
+next:
+		buf += ext_len;
+		len -= ext_len;
+	}
+
+	/*
+	 * Trigger any actions needed when an extension is missing and its
+	 * handler has not been called yet.
+	 */
+	for (i = 0; tls_extensions[i].name; i++) {
+		bool (*handler)(struct l_tls *tls);
+
+		extension = &tls_extensions[i];
+		handler = client_hello ?
+			extension->client_handle_absent :
+			extension->server_handle_absent;
+
+		if (!handler)
+			continue;
+
+		if (l_queue_find(seen, tls_ptr_match,
+					L_UINT_TO_PTR(extension->id)))
+			continue;
+
+		if (!handler(tls)) {
+			TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
+					"Hello %s extension missing",
+					extension->name);
+			return false;
+		}
+	}
+
+	return true;
+
+decode_error:
+	TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
+			"Hello extensions decode error");
+	return false;
+}
+
 static void tls_handle_client_hello(struct l_tls *tls,
 					const uint8_t *buf, size_t len)
 {
@@ -1585,6 +1714,7 @@ static void tls_handle_client_hello(struct l_tls *tls,
 	const uint8_t *cipher_suites;
 	const uint8_t *compression_methods;
 	int i;
+	struct l_queue *extensions_offered = NULL;
 
 	/* Do we have enough for ProtocolVersion + Random + SessionID size? */
 	if (len < 2 + 32 + 1)
@@ -1626,22 +1756,12 @@ static void tls_handle_client_hello(struct l_tls *tls,
 
 	len -= compression_methods_size;
 
-	if (len) {
-		uint16_t extensions_size;
+	extensions_offered = l_queue_new();
 
-		if (len < 2 || len > 2 + 65535)
-			goto decode_error;
-
-		extensions_size = l_get_be16(compression_methods +
-						compression_methods_size);
-		len -= 2;
-
-		if (len != extensions_size)
-			goto decode_error;
-
-		/* TODO: validate each extension in the vector, 7.4.1.4 */
-		/* TODO: check for duplicates? */
-	}
+	if (!tls_handle_hello_extensions(tls, compression_methods +
+					compression_methods_size,
+					len, extensions_offered))
+		goto cleanup;
 
 	/*
 	 * Note: if the client is supplying a SessionID we know it is false
@@ -1664,7 +1784,7 @@ static void tls_handle_client_hello(struct l_tls *tls,
 		TLS_DISCONNECT(TLS_ALERT_PROTOCOL_VERSION, 0,
 				"Client version too low: %02x",
 				tls->client_version);
-		return;
+		goto cleanup;
 	}
 
 	tls->negotiated_version = TLS_VERSION < tls->client_version ?
@@ -1697,13 +1817,13 @@ static void tls_handle_client_hello(struct l_tls *tls,
 		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
 				"No common cipher suites matching negotiated "
 				"TLS version and our certificate's key type");
-		return;
+		goto cleanup;
 	}
 
 	if (!tls_set_prf_hmac(tls)) {
 		TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
 				"Error selecting the PRF HMAC");
-		return;
+		goto cleanup;
 	}
 
 	TLS_DEBUG("Negotiated %s", tls->pending.cipher_suite->name);
@@ -1714,7 +1834,7 @@ static void tls_handle_client_hello(struct l_tls *tls,
 	if (!memchr(compression_methods, 0, compression_methods_size)) {
 		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
 				"No common compression methods");
-		return;
+		goto cleanup;
 	}
 
 	while (compression_methods_size) {
@@ -1730,8 +1850,10 @@ static void tls_handle_client_hello(struct l_tls *tls,
 
 	TLS_DEBUG("Negotiated %s", tls->pending.compression_method->name);
 
-	if (!tls_send_server_hello(tls, NULL))
-		return;
+	if (!tls_send_server_hello(tls, extensions_offered))
+		goto cleanup;
+
+	l_queue_destroy(extensions_offered, NULL);
 
 	if (tls->pending.cipher_suite->key_xchg->certificate_check && tls->cert)
 		if (!tls_send_certificate(tls))
@@ -1756,6 +1878,9 @@ static void tls_handle_client_hello(struct l_tls *tls,
 decode_error:
 	TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
 			"ClientHello decode error");
+
+cleanup:
+	l_queue_destroy(extensions_offered, NULL);
 }
 
 static void tls_handle_server_hello(struct l_tls *tls,
@@ -1764,6 +1889,8 @@ static void tls_handle_server_hello(struct l_tls *tls,
 	uint8_t session_id_size, cipher_suite_id[2], compression_method_id;
 	const char *error;
 	int i;
+	struct l_queue *extensions_seen;
+	bool result;
 
 	/* Do we have enough for ProtocolVersion + Random + SessionID len ? */
 	if (len < 2 + 32 + 1)
@@ -1782,11 +1909,13 @@ static void tls_handle_server_hello(struct l_tls *tls,
 	compression_method_id = buf[35 + session_id_size + 2];
 	len -= session_id_size + 2 + 1;
 
-	if (len != 0) { /* We know we haven't solicited any extensions */
-		TLS_DISCONNECT(TLS_ALERT_UNSUPPORTED_EXTENSION, 0,
-				"ServerHello contains extensions");
+	extensions_seen = l_queue_new();
+	result = tls_handle_hello_extensions(tls, buf + 38 + session_id_size,
+						len, extensions_seen);
+	l_queue_destroy(extensions_seen, NULL);
+
+	if (!result)
 		return;
-	}
 
 	tls->negotiated_version = l_get_be16(buf);
 
Signature · 1 line
2.19.1
#5 lore raw

PATCH 4/17 tls: Implement the Supported Elliptic Curves extension

In reply to Andrew Zaborowski

Defined in RFC 8422 5.1.1

---
 Makefile.am          |   1 +
 ell/tls-extensions.c | 108 +++++++++++++++++++++++++++++++++++++++++++
 ell/tls-private.h    |  10 ++++
 ell/tls.c            |   3 +-
 4 files changed, 120 insertions(+), 2 deletions(-)
 create mode 100644 ell/tls-extensions.c
Makefile.am +1 -0
index 8401972..297c395 100644
@@ -102,6 +102,7 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/tls-private.h \
 			ell/tls.c \
 			ell/tls-record.c \
+			ell/tls-extensions.c \
 			ell/uuid.c \
 			ell/key.c \
 			ell/pkcs5-private.h \
ell/tls-extensions.c new file +108 -0

108 lines not on this page · raw message

ell/tls-private.h +10 -0
index 5ea8208..d1cfcac 100644
@@ -117,6 +117,15 @@ struct tls_hello_extension {
 	bool (*server_handle_absent)(struct l_tls *tls);
 };
 
+extern const struct tls_hello_extension tls_extensions[];
+
+struct tls_named_curve {
+	const char *name;
+	uint16_t id;
+	unsigned int l_group;
+	size_t point_bytes;
+};
+
 enum tls_handshake_state {
 	TLS_HANDSHAKE_WAIT_START,
 	TLS_HANDSHAKE_WAIT_HELLO,
@@ -198,6 +207,7 @@ struct l_tls {
 	size_t peer_pubkey_size;
 	enum handshake_hash_type signature_hash;
 	const struct tls_hash_algorithm *prf_hmac;
+	const struct tls_named_curve *negotiated_curve;
 
 	/* SecurityParameters current and pending */
 
ell/tls.c +1 -2
index f8ba3d0..ed7f0c7 100644
@@ -191,6 +191,7 @@ static void tls_reset_handshake(struct l_tls *tls)
 	tls->peer_cert = NULL;
 	tls->peer_pubkey = NULL;
 	tls->peer_pubkey_size = 0;
+	tls->negotiated_curve = NULL;
 
 	for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
 		tls_drop_handshake_hash(tls, hash);
@@ -709,8 +710,6 @@ static const struct tls_hash_algorithm *tls_set_prf_hmac(struct l_tls *tls)
 	return NULL;
 }
 
-static const struct tls_hello_extension tls_extensions[] = {};
-
 enum tls_handshake_type {
 	TLS_HELLO_REQUEST	= 0,
 	TLS_CLIENT_HELLO	= 1,
Signature · 1 line
2.19.1
#6 lore raw

PATCH 5/17 tls: Implement the Supported Point Formats extension

In reply to Andrew Zaborowski

Defined in RFC 8422 5.1.2

---
 ell/tls-extensions.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
ell/tls-extensions.c +42 -0
index 749b8e6..3d75037 100644
@@ -96,6 +96,40 @@ static bool tls_elliptic_curves_client_absent(struct l_tls *tls)
 	return true;
 }
 
+static bool tls_ec_point_formats_client_handle(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	if (len < 2)
+		return false;
+
+	if (buf[0] != len - 1)
+		return false;
+
+	if (!memchr(buf + 1, 0, len - 1)) {
+		TLS_DEBUG("Uncompressed point format missing");
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * For compatibility with clients respond to a valid Client Hello Supported
+ * Point Formats extension with the hardcoded confirmation that we do
+ * support the single valid point format.  As a client we never send this
+ * extension so we never have to handle a server response to it either.
+ */
+static ssize_t tls_ec_point_formats_server_write(struct l_tls *tls,
+						uint8_t *buf, size_t len)
+{
+	if (len < 2)
+		return -ENOMEM;
+
+	buf[0] = 0x01;	/* ec_point_format_list length */
+	buf[1] = 0x00;	/* uncompressed */
+	return 2;
+}
+
 const struct tls_hello_extension tls_extensions[] = {
 	{
 		"Supported Elliptic Curves", "elliptic_curves", 10,
@@ -104,5 +138,13 @@ const struct tls_hello_extension tls_extensions[] = {
 		tls_elliptic_curves_client_absent,
 		NULL, NULL, NULL,
 	},
+	{
+		"Supported Point Formats", "ec_point_formats", 11,
+		NULL,
+		tls_ec_point_formats_client_handle,
+		NULL,
+		tls_ec_point_formats_server_write,
+		NULL, NULL,
+	},
 	{}
 };
Signature · 1 line
2.19.1
#7 lore raw

PATCH 6/17 tls: Allow user to set custom list of cipher suites

In reply to Andrew Zaborowski

TLS "security profiles" define which minimum cipher suites and other parameter values are allowed in a specific usage scenario for TLS, for example WPA3 in theory requires a specific security profile for its TLS-based EAP method. This will also allow the unit tests to test individual cipher suites.

---
 ell/tls-private.h |   5 +
 ell/tls.c         | 260 +++++++++++++++++++++++++++++++---------------
 2 files changed, 181 insertions(+), 84 deletions(-)
ell/tls-private.h +5 -0
index d1cfcac..938c358 100644
@@ -180,6 +180,8 @@ struct l_tls {
 	struct l_key *priv_key;
 	size_t priv_key_size;
 
+	struct tls_cipher_suite **cipher_suite_pref_list;
+
 	/* Record layer */
 
 	uint8_t *record_buf;
@@ -267,6 +269,9 @@ void tls_tx_record(struct l_tls *tls, enum tls_content_type type,
 bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
 			int len, enum tls_content_type type, uint16_t version);
 
+/* Optionally limit allowed cipher suites to a custom set */
+bool tls_set_cipher_suites(struct l_tls *tls, const char **suite_list);
+
 int tls_parse_certificate_list(const void *data, size_t len,
 				struct l_certchain **out_certchain);
 
ell/tls.c +176 -84

351 lines not on this page · raw message

Signature · 1 line
2.19.1
#9 lore raw

PATCH 7/17 tls: Move cipher suite definitions to tls-suites.c

In reply to Andrew Zaborowski

Move the cipher suite-specific code to new file ell/tls-suites.c. Unfortunately this requires a few functions and arrays to be exported from tls.c.

---
 Makefile.am       |   1 +
 ell/tls-private.h |  25 +++
 ell/tls-suites.c  | 450 ++++++++++++++++++++++++++++++++++++++++++++
 ell/tls.c         | 465 ++--------------------------------------------
 4 files changed, 490 insertions(+), 451 deletions(-)
 create mode 100644 ell/tls-suites.c
Makefile.am +1 -0
index 297c395..3eecbe0 100644
@@ -103,6 +103,7 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/tls.c \
 			ell/tls-record.c \
 			ell/tls-extensions.c \
+			ell/tls-suites.c \
 			ell/uuid.c \
 			ell/key.c \
 			ell/pkcs5-private.h \
ell/tls-private.h +25 -0
index 938c358..442dc02 100644
@@ -56,6 +56,8 @@ struct tls_hash_algorithm {
 	const char *name;
 };
 
+extern const struct tls_hash_algorithm tls_handshake_hash_data[];
+
 typedef bool (*tls_get_hash_t)(struct l_tls *tls, uint8_t tls_id,
 				uint8_t *out, size_t *len,
 				enum l_checksum_type *type);
@@ -94,6 +96,8 @@ struct tls_cipher_suite {
 	enum l_checksum_type prf_hmac;
 };
 
+extern struct tls_cipher_suite *tls_cipher_suite_pref[];
+
 struct tls_compression_method {
 	int id;
 	const char *name;
@@ -145,6 +149,19 @@ enum tls_content_type {
 	TLS_CT_APPLICATION_DATA		= 23,
 };
 
+enum tls_handshake_type {
+	TLS_HELLO_REQUEST	= 0,
+	TLS_CLIENT_HELLO	= 1,
+	TLS_SERVER_HELLO	= 2,
+	TLS_CERTIFICATE		= 11,
+	TLS_SERVER_KEY_EXCHANGE	= 12,
+	TLS_CERTIFICATE_REQUEST	= 13,
+	TLS_SERVER_HELLO_DONE	= 14,
+	TLS_CERTIFICATE_VERIFY	= 15,
+	TLS_CLIENT_KEY_EXCHANGE	= 16,
+	TLS_FINISHED		= 20,
+};
+
 /*
  * Support the minimum required set of handshake hash types for the
  * Certificate Verify digital signature and the Finished PRF seed so we
@@ -269,9 +286,17 @@ void tls_tx_record(struct l_tls *tls, enum tls_content_type type,
 bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
 			int len, enum tls_content_type type, uint16_t version);
 
+#define TLS_HANDSHAKE_HEADER_SIZE	4
+
+void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf, size_t length);
+
 /* Optionally limit allowed cipher suites to a custom set */
 bool tls_set_cipher_suites(struct l_tls *tls, const char **suite_list);
 
+void tls_generate_master_secret(struct l_tls *tls,
+				const uint8_t *pre_master_secret,
+				int pre_master_secret_len);
+
 int tls_parse_certificate_list(const void *data, size_t len,
 				struct l_certchain **out_certchain);
 
ell/tls-suites.c new file +450 -0

450 lines not on this page · raw message

ell/tls.c +14 -451

536 lines not on this page · raw message

Signature · 1 line
2.19.1
#10 lore raw

PATCH 8/17 tls: Add ServerKeyExchange callbacks to key exchange struct

In reply to Andrew Zaborowski

TLS Key Exchange algorithms using ephemeral keys (DHE_* and ECDHE_*) need to use the ServerKeyExchange message during the handshake unlike our current RSA key exchanhe, provide callbacks for sending and receiving those messages.

---
 ell/tls-private.h |  4 ++++
 ell/tls.c         | 61 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 55 insertions(+), 10 deletions(-)
ell/tls-private.h +4 -0
index 442dc02..375af2b 100644
@@ -69,6 +69,10 @@ struct tls_key_exchange_algorithm {
 
 	bool (*validate_cert_key_type)(struct l_cert *cert);
 
+	bool (*send_server_key_exchange)(struct l_tls *tls);
+	void (*handle_server_key_exchange)(struct l_tls *tls,
+						const uint8_t *buf, size_t len);
+
 	bool (*send_client_key_exchange)(struct l_tls *tls);
 	void (*handle_client_key_exchange)(struct l_tls *tls,
 						const uint8_t *buf, size_t len);
ell/tls.c +51 -10

89 lines not on this page · raw message

Signature · 1 line
2.19.1
#11 lore raw

PATCH 9/17 tls: ECHDE_RSA key exchange implementation client side

In reply to Andrew Zaborowski

Add a new key exchange definition with the callbacks needed to handle the client side of the ECDHE_RSA key exchange, defined in RFC 8422. Key Exchange method-specific state is kept in a struct allocated by the method and pointed to by a void pointer in the l_tls struct so that the internals of the method are kept in tls-suites.c

---
 ell/tls-extensions.c |  11 ++
 ell/tls-private.h    |   5 +
 ell/tls-suites.c     | 246 +++++++++++++++++++++++++++++++++++++++++++
 ell/tls.c            |   4 +
 4 files changed, 266 insertions(+)
ell/tls-extensions.c +11 -0
index 3d75037..be2fa10 100644
@@ -148,3 +148,14 @@ const struct tls_hello_extension tls_extensions[] = {
 	},
 	{}
 };
+
+const struct tls_named_curve *tls_find_curve(uint16_t id)
+{
+	unsigned int i;
+
+	for (i = 0; i < L_ARRAY_SIZE(tls_curve_pref); i++)
+		if (tls_curve_pref[i].id == id)
+			return &tls_curve_pref[i];
+
+	return NULL;
+}
ell/tls-private.h +5 -0
index 375af2b..0ed3eee 100644
@@ -81,6 +81,8 @@ struct tls_key_exchange_algorithm {
 			tls_get_hash_t get_hash);
 	bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t len,
 			tls_get_hash_t get_hash);
+
+	void (*free_params)(struct l_tls *tls);
 };
 
 struct tls_mac_algorithm {
@@ -246,6 +248,7 @@ struct l_tls {
 		 * 6.3 v1.2 + two IVs of 32 bytes.
 		 */
 		uint8_t key_block[192];
+		void *key_xchg_params;
 	} pending;
 
 	enum tls_cipher_type cipher_type[2];
@@ -301,6 +304,8 @@ void tls_generate_master_secret(struct l_tls *tls,
 				const uint8_t *pre_master_secret,
 				int pre_master_secret_len);
 
+const struct tls_named_curve *tls_find_curve(uint16_t id);
+
 int tls_parse_certificate_list(const void *data, size_t len,
 				struct l_certchain **out_certchain);
 
ell/tls-suites.c +246 -0

258 lines not on this page · raw message

ell/tls.c +4 -0
index 0aa04a1..df827f3 100644
@@ -186,6 +186,10 @@ static void tls_reset_handshake(struct l_tls *tls)
 
 	memset(tls->pending.key_block, 0, sizeof(tls->pending.key_block));
 
+	if (tls->pending.cipher_suite &&
+			tls->pending.cipher_suite->key_xchg->free_params)
+		tls->pending.cipher_suite->key_xchg->free_params(tls);
+
 	l_cert_free(tls->peer_cert);
 	l_key_free(tls->peer_pubkey);
 
Signature · 1 line
2.19.1
#12 lore raw

PATCH 10/17 tls: ECHDE_RSA key exchange implementation server side

In reply to Andrew Zaborowski

Add the handler for the ECDHE ClientKeyExchange message rx and a builder for the ServerKeyExchange message tx, both specified in RFC 8422.

---
 ell/tls-private.h |   1 +
 ell/tls-suites.c  | 122 ++++++++++++++++++++++++++++++++++++++++++++++
 ell/tls.c         |  18 +++++++
 3 files changed, 141 insertions(+)
ell/tls-private.h +1 -0
index 0ed3eee..12d350a 100644
@@ -66,6 +66,7 @@ struct tls_key_exchange_algorithm {
 	uint8_t id;
 
 	bool certificate_check;
+	bool need_ecc;
 
 	bool (*validate_cert_key_type)(struct l_cert *cert);
 
ell/tls-suites.c +122 -0

140 lines not on this page · raw message

ell/tls.c +18 -0
index df827f3..fc8ab41 100644
@@ -476,6 +476,24 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,
 		return false;
 	}
 
+	/*
+	 * On the server we know what elliptic curve we'll be using as soon
+	 * as we've processed the ClientHello so for EC-based key exchange
+	 * methods require that a curve has been selected.
+	 */
+	if (suite->key_xchg->need_ecc && tls->server &&
+			!tls->negotiated_curve) {
+		if (error) {
+			*error = error_buf;
+			snprintf(error_buf, sizeof(error_buf),
+					"No common supported elliptic curves "
+					"with the client, can't use %s",
+					suite->name);
+		}
+
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.19.1
#13 lore raw

PATCH 11/17 tls: Add RFC4492 suites using the ECDHE_RSA key exchange

In reply to Andrew Zaborowski

Add the 4 cipher suites defined in RFC 4492 that we can now support with the new ECDHE_RSA key exchange (see section 6 in RFC 4492 and 8422 for the full list of ECDH-based cipher suites.)

---
 ell/tls-suites.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
ell/tls-suites.c +32 -0
index cda192b..297b3d4 100644
@@ -802,16 +802,48 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_aes256_gcm,
 	.prf_hmac = L_CHECKSUM_SHA384,
 	.key_xchg = &tls_rsa,
+}, tls_ecdhe_rsa_with_rc4_128_sha = {
+	.id = { 0xc0, 0x11 },
+	.name = "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_rc4,
+	.mac = &tls_sha,
+	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_3des_ede_cbc_sha = {
+	.id = { 0xc0, 0x12 },
+	.name = "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_3des_ede,
+	.mac = &tls_sha,
+	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_aes_128_cbc_sha = {
+	.id = { 0xc0, 0x13 },
+	.name = "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_aes128,
+	.mac = &tls_sha,
+	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_aes_256_cbc_sha = {
+	.id = { 0xc0, 0x14 },
+	.name = "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_aes256,
+	.mac = &tls_sha,
+	.key_xchg = &tls_ecdhe_rsa,
 };
 
 struct tls_cipher_suite *tls_cipher_suite_pref[] = {
+	&tls_ecdhe_rsa_with_aes_256_cbc_sha,
+	&tls_ecdhe_rsa_with_aes_128_cbc_sha,
 	&tls_rsa_with_aes_256_cbc_sha,
 	&tls_rsa_with_aes_128_cbc_sha,
 	&tls_rsa_with_aes_256_cbc_sha256,
 	&tls_rsa_with_aes_128_cbc_sha256,
 	&tls_rsa_with_aes_256_gcm_sha384,
 	&tls_rsa_with_aes_128_gcm_sha256,
+	&tls_ecdhe_rsa_with_3des_ede_cbc_sha,
 	&tls_rsa_with_3des_ede_cbc_sha,
+	&tls_ecdhe_rsa_with_rc4_128_sha,
 	&tls_rsa_with_rc4_128_sha,
 	&tls_rsa_with_rc4_128_md5,
 	NULL,
Signature · 1 line
2.19.1
#14 lore raw

PATCH 12/17 tls: Add RFC5289 suites using the ECDHE_RSA key exchange

In reply to Andrew Zaborowski

Add the 4 cipher suites defined in RFC 5289 that we can now support with the new ECDHE_RSA key exchange, 2 of these are the same ones defined in RFC 8422.

---
 ell/tls-suites.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
ell/tls-suites.c +36 -0
index 297b3d4..af8cbcd 100644
@@ -738,6 +738,10 @@ static struct tls_mac_algorithm tls_md5 = {
 	.id = 4,
 	.hmac_type = L_CHECKSUM_SHA256,
 	.mac_length = 32,
+}, tls_sha384 = {
+	.id = 5,
+	.hmac_type = L_CHECKSUM_SHA384,
+	.mac_length = 48,
 };
 
 static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
@@ -830,6 +834,34 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_aes256,
 	.mac = &tls_sha,
 	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_aes_128_cbc_sha256 = {
+	.id = { 0xc0, 0x27 },
+	.name = "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
+	.verify_data_length = 12,
+	.encryption = &tls_aes128,
+	.mac = &tls_sha256,
+	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_aes_256_cbc_sha384 = {
+	.id = { 0xc0, 0x28 },
+	.name = "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
+	.verify_data_length = 12,
+	.encryption = &tls_aes256,
+	.mac = &tls_sha384,
+	.prf_hmac = L_CHECKSUM_SHA384,
+	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_aes_128_gcm_sha256 = {
+	.id = { 0xc0, 0x2f },
+	.name = "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
+	.verify_data_length = 12,
+	.encryption = &tls_aes128_gcm,
+	.key_xchg = &tls_ecdhe_rsa,
+}, tls_ecdhe_rsa_with_aes_256_gcm_sha384 = {
+	.id = { 0xc0, 0x30 },
+	.name = "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
+	.verify_data_length = 12,
+	.encryption = &tls_aes256_gcm,
+	.prf_hmac = L_CHECKSUM_SHA384,
+	.key_xchg = &tls_ecdhe_rsa,
 };
 
 struct tls_cipher_suite *tls_cipher_suite_pref[] = {
@@ -837,8 +869,12 @@ struct tls_cipher_suite *tls_cipher_suite_pref[] = {
 	&tls_ecdhe_rsa_with_aes_128_cbc_sha,
 	&tls_rsa_with_aes_256_cbc_sha,
 	&tls_rsa_with_aes_128_cbc_sha,
+	&tls_ecdhe_rsa_with_aes_256_cbc_sha384,
+	&tls_ecdhe_rsa_with_aes_128_cbc_sha256,
 	&tls_rsa_with_aes_256_cbc_sha256,
 	&tls_rsa_with_aes_128_cbc_sha256,
+	&tls_ecdhe_rsa_with_aes_256_gcm_sha384,
+	&tls_ecdhe_rsa_with_aes_128_gcm_sha256,
 	&tls_rsa_with_aes_256_gcm_sha384,
 	&tls_rsa_with_aes_128_gcm_sha256,
 	&tls_ecdhe_rsa_with_3des_ede_cbc_sha,
Signature · 1 line
2.19.1
#15 lore raw

PATCH 13/17 key: Add l_key_validate_dh_payload

In reply to Andrew Zaborowski

Add a function to validate local private value payloads against a the group's prime before creating kernel keys out of them, and to validate public values received from the peer.

---
 ell/ell.sym |  1 +
 ell/key.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 ell/key.h   |  3 +++
 3 files changed, 52 insertions(+)
ell/ell.sym +1 -0
index ea64ac2..c3e6156 100644
@@ -303,6 +303,7 @@ global:
 	l_key_get_info;
 	l_key_compute_dh_public;
 	l_key_compute_dh_secret;
+	l_key_validate_dh_payload;
 	l_key_encrypt;
 	l_key_decrypt;
 	l_key_sign;
ell/key.c +48 -0

54 lines not on this page · raw message

ell/key.h +3 -0
index 1529135..e8688c6 100644
@@ -80,6 +80,9 @@ bool l_key_compute_dh_secret(struct l_key *other_public, struct l_key *private,
 				struct l_key *prime,
 				void *payload, size_t *len);
 
+bool l_key_validate_dh_payload(const void *payload, size_t len,
+				const void *prime_buf, size_t prime_len);
+
 ssize_t l_key_encrypt(struct l_key *key, enum l_key_cipher_type cipher,
 			enum l_checksum_type checksum, const void *in,
 			void *out, size_t len_in, size_t len_out);
Signature · 1 line
2.19.1
#18 lore raw

PATCH 14/17 tls: DHE_RSA key exchange implementation client side

In reply to Andrew Zaborowski

Add a new key exchange definition with the callbacks needed to handle the client side of the DHE_RSA key exchange.

---
 ell/tls-suites.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 354 insertions(+)
ell/tls-suites.c +354 -0
index af8cbcd..ef1f2cc 100644
@@ -688,6 +688,360 @@ static struct tls_key_exchange_algorithm tls_ecdhe_rsa = {
 	.verify = tls_rsa_verify,
 };
 
+struct tls_dhe_params {
+	/*
+	 * We need both the raw contents here for tls_write_server_dh_params
+	 * and the l_key pointers to cache the l_keys created for the
+	 * ServerKeyExchange and not have to allocate them again during
+	 * ClientKeyExchange.  public_buf is used for both our and the
+	 * peer's DH public value while public is only used for the peer's
+	 * public value.
+	 */
+	struct l_key *prime;
+	uint8_t *prime_buf;
+	size_t prime_len;
+	struct l_key *generator;
+	uint8_t *generator_buf;
+	size_t generator_len;
+	struct l_key *private;
+	const uint8_t *private_buf;
+	size_t private_len;
+	struct l_key *public;
+	const uint8_t *public_buf;
+	size_t public_len;
+};
+
+#define TLS_DHE_MAX_SIZE 1024
+
+static void tls_free_dhe_params(struct l_tls *tls)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+	if (!params)
+		return;
+
+	tls->pending.key_xchg_params = NULL;
+
+	l_free(params->prime_buf);
+	l_free(params->generator_buf);
+	l_key_free(params->prime);
+	l_key_free(params->generator);
+	l_key_free(params->private);
+	l_key_free(params->public);
+	l_free(params);
+}
+
+static bool tls_create_dhe_keys(struct l_tls *tls)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+	if (!params)
+		return false;
+
+	if (params->prime_buf && !params->prime) {
+		params->prime = l_key_new(L_KEY_RAW, params->prime_buf,
+						params->prime_len);
+		if (!params->prime) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(prime) failed");
+			return false;
+		}
+	}
+
+	if (params->generator_buf && !params->generator) {
+		params->generator = l_key_new(L_KEY_RAW, params->generator_buf,
+						params->generator_len);
+		if (!params->generator) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(generator) failed");
+			return false;
+		}
+	}
+
+	if (params->private_buf && !params->private) {
+		params->private = l_key_new(L_KEY_RAW, params->private_buf,
+						params->private_len);
+		if (!params->private) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(private) failed");
+			return false;
+		}
+	}
+
+	if (params->public_buf && !params->public) {
+		params->public = l_key_new(L_KEY_RAW, params->public_buf,
+						params->public_len);
+		if (!params->public) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(public) failed");
+			return false;
+		}
+	}
+
+	return true;
+}
+
+static bool tls_generate_dhe_private(struct l_tls *tls, uint8_t *buf, size_t len)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+	do {
+		unsigned int written = 0;
+
+		/* Leading zeros in the private value are fine */
+
+		do {
+			size_t chunk_len =
+				written + 256 <= len ? 256 : len - written;
+
+			if (!l_getrandom(buf + written, chunk_len)) {
+				TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+						"l_getrandom(buf, %zu) failed",
+						chunk_len);
+				return false;
+			}
+
+			written += chunk_len;
+		} while (written < len);
+	} while (!l_key_validate_dh_payload(buf, len,
+						params->prime_buf,
+						params->prime_len));
+
+	return true;
+}
+
+static size_t tls_write_server_dh_params(struct l_tls *tls, uint8_t *buf)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+	uint8_t *ptr = buf;
+
+	/* RFC 5246, Section 7.4.3 */
+
+	l_put_be16(params->prime_len, ptr);
+	memcpy(ptr + 2, params->prime_buf, params->prime_len);
+	ptr += 2 + params->prime_len;
+
+	l_put_be16(params->generator_len, ptr);
+	memcpy(ptr + 2, params->generator_buf, params->generator_len);
+	ptr += 2 + params->generator_len;
+
+	l_put_be16(params->public_len, ptr);
+	memcpy(ptr + 2, params->public_buf, params->public_len);
+	ptr += 2 + params->public_len;
+
+	return ptr - buf;
+}
+
+static bool tls_get_server_dh_params_hash(struct l_tls *tls, uint8_t tls_id,
+						uint8_t *out, size_t *len,
+						enum l_checksum_type *type)
+{
+	unsigned int hash;
+	struct l_checksum *checksum;
+	uint8_t params[32 + TLS_DHE_MAX_SIZE * 3];
+	size_t params_len;
+	ssize_t hash_len, ret;
+
+	params_len = tls_write_server_dh_params(tls, params);
+
+	for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
+		if (tls_handshake_hash_data[hash].tls_id == tls_id)
+			break;
+
+	if (hash == __HANDSHAKE_HASH_COUNT)
+		return false;
+
+	hash_len = tls_handshake_hash_data[hash].length;
+
+	checksum = l_checksum_new(tls_handshake_hash_data[hash].l_id);
+	if (!checksum)
+		return false;
+
+	l_checksum_update(checksum, tls->pending.client_random, 32);
+	l_checksum_update(checksum, tls->pending.server_random, 32);
+	l_checksum_update(checksum, params, params_len);
+	ret = l_checksum_get_digest(checksum, out, hash_len);
+	l_checksum_free(checksum);
+
+	if (ret != (ssize_t) hash_len)
+		return false;
+
+	if (len)
+		*len = hash_len;
+
+	if (type)
+		*type = tls_handshake_hash_data[hash].l_id;
+
+	return true;
+}
+
+static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	struct tls_dhe_params *params;
+
+	params = l_new(struct tls_dhe_params, 1);
+	tls->pending.key_xchg_params = params;
+
+	if (len < 2)
+		goto decode_error;
+
+	params->prime_len = l_get_be16(buf);
+	if (len < 2 + params->prime_len + 2)
+		goto decode_error;
+
+	params->prime_buf = l_memdup(buf + 2, params->prime_len);
+	buf += 2 + params->prime_len;
+	len -= 2 + params->prime_len;
+
+	params->generator_len = l_get_be16(buf);
+	if (len < 2 + params->generator_len + 2)
+		goto decode_error;
+
+	params->generator_buf = l_memdup(buf + 2, params->generator_len);;
+	buf += 2 + params->generator_len;
+	len -= 2 + params->generator_len;
+
+	params->public_len = l_get_be16(buf);
+	if (len < 2 + params->public_len)
+		goto decode_error;
+
+	params->public_buf = buf + 2;
+	buf += 2 + params->public_len;
+	len -= 2 + params->public_len;
+
+	/*
+	 * Validate the values received.  Without RFC 7919 we basically have
+	 * to blindly accept the provided prime value.  We have no way to
+	 * confirm that it's actually prime or that it's a "safe prime" or
+	 * that it forms a group without small sub-groups.  There's also no
+	 * way to whitelist all valid values.  But we do a basic sanity
+	 * check and require it to be 1024-bit or longer (see weakdh.org),
+	 * might need to move to 2048 bits actually.
+	 */
+
+	if (params->prime_len > TLS_DHE_MAX_SIZE ||
+			params->generator_len > params->prime_len) {
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Server DH prime modulus or generator "
+				"too long");
+		return;
+	}
+
+	if (params->prime_len < 128 || params->prime_buf[0] == 0x00 ||
+			!(params->prime_buf[params->prime_len - 1] & 1)) {
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Server DH prime modulus invalid");
+		return;
+	}
+
+	/*
+	 * RFC 7919 Section 3.0:
+	 * "the client MUST verify that dh_Ys is in the range
+	 * 1 < dh_Ys < dh_p - 1.  If dh_Ys is not in this range, the client
+	 * MUST terminate the connection with a fatal handshake_failure(40)
+	 * alert."
+	 */
+	if (params->public_len > params->prime_len ||
+			!l_key_validate_dh_payload(params->public_buf,
+							params->public_len,
+							params->prime_buf,
+							params->prime_len)) {
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Server DH public value invalid");
+		return;
+	}
+
+	if (!tls_create_dhe_keys(tls))
+		return;
+
+	if (!tls->pending.cipher_suite->key_xchg->verify(tls, buf, len,
+						tls_get_server_dh_params_hash))
+		return;
+
+	TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
+	return;
+
+decode_error:
+	TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
+			"ServerKeyExchange decode error");
+}
+
+static bool tls_send_dhe_client_key_xchg(struct l_tls *tls)
+{
+	uint8_t buf[128 + TLS_DHE_MAX_SIZE];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+	uint8_t pre_master_secret[params->prime_len];
+	size_t pre_master_secret_len;
+	unsigned int offset = 0;
+	uint8_t private_buf[params->prime_len];
+	uint8_t public_buf[params->prime_len];
+
+	if (!tls_generate_dhe_private(tls, private_buf, params->prime_len))
+		return false;
+
+	params->private_buf = private_buf;
+	params->private_len = params->prime_len;
+
+	if (!tls_create_dhe_keys(tls))
+		return false;
+
+	memset(private_buf, 0, params->private_len);
+	params->public_buf = public_buf;
+	params->public_len = params->prime_len;
+
+	if (!l_key_compute_dh_public(params->generator, params->private,
+					params->prime, public_buf,
+					&params->public_len)) {
+		TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+				"l_key_compute_dh_public failed");
+		return false;
+	}
+
+	while (params->public_buf[0] == 0x00 && params->public_len) {
+		params->public_buf++;
+		params->public_len--;
+	}
+
+	l_put_be16(params->public_len, ptr);
+	memcpy(ptr + 2, params->public_buf, params->public_len);
+	ptr += 2 + params->public_len;
+	pre_master_secret_len = params->prime_len;
+
+	if (!l_key_compute_dh_secret(params->public, params->private,
+					params->prime, pre_master_secret,
+					&pre_master_secret_len)) {
+		TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+				"Generating DH shared-secret failed");
+		return false;
+	}
+
+	while (pre_master_secret[offset] == 0x00 &&
+			offset < pre_master_secret_len)
+		offset++;
+
+	tls_tx_handshake(tls, TLS_CLIENT_KEY_EXCHANGE, buf, ptr - buf);
+
+	tls_free_dhe_params(tls);
+	tls_generate_master_secret(tls, pre_master_secret + offset,
+					pre_master_secret_len - offset);
+	memset(pre_master_secret, 0, pre_master_secret_len);
+	return true;
+}
+
+static struct tls_key_exchange_algorithm tls_dhe_rsa = {
+	.id = 1, /* RSA_sign */
+	.certificate_check = true,
+	.need_ecc = true,
+	.validate_cert_key_type = tls_rsa_validate_cert_key,
+	.handle_server_key_exchange = tls_handle_dhe_server_key_xchg,
+	.send_client_key_exchange = tls_send_dhe_client_key_xchg,
+	.free_params = tls_free_dhe_params,
+	.sign = tls_rsa_sign,
+	.verify = tls_rsa_verify,
+};
+
 static struct tls_bulk_encryption_algorithm tls_rc4 = {
 	.cipher_type = TLS_CIPHER_STREAM,
 	.l_id = L_CIPHER_ARC4,
Signature · 1 line
2.19.1
#23 lore raw

PATCH 15/17 tls: DHE_RSA key exchange implementation server side

In reply to Andrew Zaborowski

Add the handler for the ClientKeyExchange message and a builder callback for the ServerKeyExchange message for DHE_RSA.

---
 ell/tls-suites.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 164 insertions(+)
ell/tls-suites.c +164 -0
index ef1f2cc..8e975a0 100644
@@ -688,6 +688,34 @@ static struct tls_key_exchange_algorithm tls_ecdhe_rsa = {
 	.verify = tls_rsa_verify,
 };
 
+/* RFC 3526, Section 3 */
+static const uint8_t tls_dh14_prime[] = {
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2,
+	0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1,
+	0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
+	0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e, 0x34, 0x04, 0xdd,
+	0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d,
+	0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
+	0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4, 0x4c, 0x42, 0xe9,
+	0xa6, 0x37, 0xed, 0x6b, 0x0b, 0xff, 0x5c, 0xb6, 0xf4, 0x06, 0xb7, 0xed,
+	0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5, 0xae, 0x9f, 0x24, 0x11,
+	0x7c, 0x4b, 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51, 0xec, 0xe4, 0x5b, 0x3d,
+	0xc2, 0x00, 0x7c, 0xb8, 0xa1, 0x63, 0xbf, 0x05, 0x98, 0xda, 0x48, 0x36,
+	0x1c, 0x55, 0xd3, 0x9a, 0x69, 0x16, 0x3f, 0xa8, 0xfd, 0x24, 0xcf, 0x5f,
+	0x83, 0x65, 0x5d, 0x23, 0xdc, 0xa3, 0xad, 0x96, 0x1c, 0x62, 0xf3, 0x56,
+	0x20, 0x85, 0x52, 0xbb, 0x9e, 0xd5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6d,
+	0x67, 0x0c, 0x35, 0x4e, 0x4a, 0xbc, 0x98, 0x04, 0xf1, 0x74, 0x6c, 0x08,
+	0xca, 0x18, 0x21, 0x7c, 0x32, 0x90, 0x5e, 0x46, 0x2e, 0x36, 0xce, 0x3b,
+	0xe3, 0x9e, 0x77, 0x2c, 0x18, 0x0e, 0x86, 0x03, 0x9b, 0x27, 0x83, 0xa2,
+	0xec, 0x07, 0xa2, 0x8f, 0xb5, 0xc5, 0x5d, 0xf0, 0x6f, 0x4c, 0x52, 0xc9,
+	0xde, 0x2b, 0xcb, 0xf6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7c,
+	0xea, 0x95, 0x6a, 0xe5, 0x15, 0xd2, 0x26, 0x18, 0x98, 0xfa, 0x05, 0x10,
+	0x15, 0x72, 0x8e, 0x5a, 0x8a, 0xac, 0xaa, 0x68, 0xff, 0xff, 0xff, 0xff,
+	0xff, 0xff, 0xff, 0xff,
+};
+
+static const unsigned int tls_dh14_generator = 2;
+
 struct tls_dhe_params {
 	/*
 	 * We need both the raw contents here for tls_write_server_dh_params
@@ -875,6 +903,77 @@ static bool tls_get_server_dh_params_hash(struct l_tls *tls, uint8_t tls_id,
 	return true;
 }
 
+static bool tls_send_dhe_server_key_xchg(struct l_tls *tls)
+{
+	uint8_t buf[1024 + TLS_DHE_MAX_SIZE * 3];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	struct tls_dhe_params *params;
+	uint8_t private_buf[TLS_DHE_MAX_SIZE];
+	uint8_t public_buf[TLS_DHE_MAX_SIZE];
+	uint8_t generator = tls_dh14_generator;
+	ssize_t sign_len;
+	bool result;
+
+	params = l_new(struct tls_dhe_params, 1);
+	tls->pending.key_xchg_params = params;
+
+	/*
+	 * For now hardcode the Oakley Group 14 parameters like some other
+	 * TLS implementations do without RFC 7919, which is actually a
+	 * downside because the more common the group parameters are the
+	 * less secure they are assumed to be, but also is a test that the
+	 * group is sufficiently good.
+	 *
+	 * Eventually we should make this configurable so that a unique
+	 * likely-prime number generated by either 'openssl dhparam' or
+	 * 'ssh-keygen -G' can be set, or parse /etc/ssh/moduli to select
+	 * a random pre-generated FFDH group each time.
+	 */
+	params->prime_buf = (uint8_t *) tls_dh14_prime;
+	params->prime_len = sizeof(tls_dh14_prime);
+	params->generator_buf = &generator;
+	params->generator_len = 1;
+	params->private_buf = private_buf;
+	params->private_len = params->prime_len;
+
+	result = tls_generate_dhe_private(tls, private_buf, params->prime_len) &&
+		tls_create_dhe_keys(tls);
+	params->prime_buf = NULL;
+	params->generator_buf = NULL;
+
+	if (!result)
+		return false;
+
+	memset(private_buf, 0, params->private_len);
+	params->public_buf = public_buf;
+	params->public_len = params->prime_len;
+
+	if (!l_key_compute_dh_public(params->generator, params->private,
+					params->prime, public_buf,
+					&params->public_len)) {
+		TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+				"l_key_compute_dh_public failed");
+		return false;
+	}
+
+	while (params->public_buf[0] == 0x00 && params->public_len) {
+		params->public_buf++;
+		params->public_len--;
+	}
+
+	ptr += tls_write_server_dh_params(tls, ptr);
+	sign_len = tls->pending.cipher_suite->key_xchg->sign(tls, ptr,
+					buf + sizeof(buf) - ptr,
+					tls_get_server_dh_params_hash);
+	ptr += sign_len;
+
+	if (sign_len < 0)
+		return false;
+
+	tls_tx_handshake(tls, TLS_SERVER_KEY_EXCHANGE, buf, ptr - buf);
+	return true;
+}
+
 static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
 						const uint8_t *buf, size_t len)
 {
@@ -1030,13 +1129,78 @@ static bool tls_send_dhe_client_key_xchg(struct l_tls *tls)
 	return true;
 }
 
+static void tls_handle_dhe_client_key_xchg(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+	uint8_t pre_master_secret[params->prime_len];
+	size_t pre_master_secret_len;
+	unsigned int offset = 0;
+
+	if (len < 2)
+		goto decode_error;
+
+	params->public_len = l_get_be16(buf);
+	buf += 2;
+	len -= 2;
+	params->public_buf = buf;
+
+	if (params->public_len != len)
+		goto decode_error;
+
+	/*
+	 * RFC 7919 Section 4:
+	 * "the server MUST verify that 1 < dh_Yc < dh_p - 1.  If dh_Yc is
+	 * out of range, the server MUST terminate the connection with
+	 * a fatal handshake_failure(40) alert."
+	 */
+	if (params->public_len > params->prime_len ||
+			!l_key_validate_dh_payload(params->public_buf,
+							params->public_len,
+							params->prime_buf,
+							params->prime_len)) {
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Client DH public value invalid");
+		return;
+	}
+
+	if (!tls_create_dhe_keys(tls))
+		return;
+
+	pre_master_secret_len = params->prime_len;
+
+	if (!l_key_compute_dh_secret(params->public, params->private,
+					params->prime, pre_master_secret,
+					&pre_master_secret_len)) {
+		TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+				"Generating DH shared-secret failed");
+		return;
+	}
+
+	while (pre_master_secret[offset] == 0x00 &&
+			offset < pre_master_secret_len)
+		offset++;
+
+	tls_free_dhe_params(tls);
+	tls_generate_master_secret(tls, pre_master_secret + offset,
+					pre_master_secret_len - offset);
+	memset(pre_master_secret, 0, pre_master_secret_len);
+	return;
+
+decode_error:
+	TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
+			"ClientKeyExchange decode error");
+}
+
 static struct tls_key_exchange_algorithm tls_dhe_rsa = {
 	.id = 1, /* RSA_sign */
 	.certificate_check = true,
 	.need_ecc = true,
 	.validate_cert_key_type = tls_rsa_validate_cert_key,
+	.send_server_key_exchange = tls_send_dhe_server_key_xchg,
 	.handle_server_key_exchange = tls_handle_dhe_server_key_xchg,
 	.send_client_key_exchange = tls_send_dhe_client_key_xchg,
+	.handle_client_key_exchange = tls_handle_dhe_client_key_xchg,
 	.free_params = tls_free_dhe_params,
 	.sign = tls_rsa_sign,
 	.verify = tls_rsa_verify,
Signature · 1 line
2.19.1
#24 lore raw

PATCH 16/17 tls: Add DHE_RSA-based cipher suites

In reply to Andrew Zaborowski

Add the 5 DHE_RSA suites defined in RFC 5246 and the 2 defined in RFC 5288.

---
 ell/tls-suites.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
ell/tls-suites.c +55 -0
index 8e975a0..7b2ed27 100644
@@ -1283,6 +1283,13 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_3des_ede,
 	.mac = &tls_sha,
 	.key_xchg = &tls_rsa,
+}, tls_dhe_rsa_with_3des_ede_cbc_sha = {
+	.id = { 0x00, 0x16 },
+	.name = "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_3des_ede,
+	.mac = &tls_sha,
+	.key_xchg = &tls_dhe_rsa,
 }, tls_rsa_with_aes_128_cbc_sha = {
 	.id = { 0x00, 0x2f },
 	.name = "TLS_RSA_WITH_AES_128_CBC_SHA",
@@ -1290,6 +1297,13 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_aes128,
 	.mac = &tls_sha,
 	.key_xchg = &tls_rsa,
+}, tls_dhe_rsa_with_aes_128_cbc_sha = {
+	.id = { 0x00, 0x33 },
+	.name = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_aes128,
+	.mac = &tls_sha,
+	.key_xchg = &tls_dhe_rsa,
 }, tls_rsa_with_aes_256_cbc_sha = {
 	.id = { 0x00, 0x35 },
 	.name = "TLS_RSA_WITH_AES_256_CBC_SHA",
@@ -1297,6 +1311,13 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_aes256,
 	.mac = &tls_sha,
 	.key_xchg = &tls_rsa,
+}, tls_dhe_rsa_with_aes_256_cbc_sha = {
+	.id = { 0x00, 0x39 },
+	.name = "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
+	.verify_data_length = 12,
+	.encryption = &tls_aes256,
+	.mac = &tls_sha,
+	.key_xchg = &tls_dhe_rsa,
 }, tls_rsa_with_aes_128_cbc_sha256 = {
 	.id = { 0x00, 0x3c },
 	.name = "TLS_RSA_WITH_AES_128_CBC_SHA256",
@@ -1311,6 +1332,20 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_aes256,
 	.mac = &tls_sha256,
 	.key_xchg = &tls_rsa,
+}, tls_dhe_rsa_with_aes_128_cbc_sha256 = {
+	.id = { 0x00, 0x67 },
+	.name = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",
+	.verify_data_length = 12,
+	.encryption = &tls_aes128,
+	.mac = &tls_sha256,
+	.key_xchg = &tls_dhe_rsa,
+}, tls_dhe_rsa_with_aes_256_cbc_sha256 = {
+	.id = { 0x00, 0x6b },
+	.name = "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
+	.verify_data_length = 12,
+	.encryption = &tls_aes256,
+	.mac = &tls_sha256,
+	.key_xchg = &tls_dhe_rsa,
 }, tls_rsa_with_aes_128_gcm_sha256 = {
 	.id = { 0x00, 0x9c },
 	.name = "TLS_RSA_WITH_AES_128_GCM_SHA256",
@@ -1324,6 +1359,19 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 	.encryption = &tls_aes256_gcm,
 	.prf_hmac = L_CHECKSUM_SHA384,
 	.key_xchg = &tls_rsa,
+}, tls_dhe_rsa_with_aes_128_gcm_sha256 = {
+	.id = { 0x00, 0x9e },
+	.name = "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
+	.verify_data_length = 12,
+	.encryption = &tls_aes128_gcm,
+	.key_xchg = &tls_dhe_rsa,
+}, tls_dhe_rsa_with_aes_256_gcm_sha384 = {
+	.id = { 0x00, 0x9f },
+	.name = "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
+	.verify_data_length = 12,
+	.encryption = &tls_aes256_gcm,
+	.prf_hmac = L_CHECKSUM_SHA384,
+	.key_xchg = &tls_dhe_rsa,
 }, tls_ecdhe_rsa_with_rc4_128_sha = {
 	.id = { 0xc0, 0x11 },
 	.name = "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
@@ -1385,17 +1433,24 @@ static struct tls_cipher_suite tls_rsa_with_rc4_128_md5 = {
 struct tls_cipher_suite *tls_cipher_suite_pref[] = {
 	&tls_ecdhe_rsa_with_aes_256_cbc_sha,
 	&tls_ecdhe_rsa_with_aes_128_cbc_sha,
+	&tls_dhe_rsa_with_aes_256_cbc_sha,
+	&tls_dhe_rsa_with_aes_128_cbc_sha,
 	&tls_rsa_with_aes_256_cbc_sha,
 	&tls_rsa_with_aes_128_cbc_sha,
 	&tls_ecdhe_rsa_with_aes_256_cbc_sha384,
 	&tls_ecdhe_rsa_with_aes_128_cbc_sha256,
+	&tls_dhe_rsa_with_aes_256_cbc_sha256,
+	&tls_dhe_rsa_with_aes_128_cbc_sha256,
 	&tls_rsa_with_aes_256_cbc_sha256,
 	&tls_rsa_with_aes_128_cbc_sha256,
 	&tls_ecdhe_rsa_with_aes_256_gcm_sha384,
 	&tls_ecdhe_rsa_with_aes_128_gcm_sha256,
+	&tls_dhe_rsa_with_aes_256_gcm_sha384,
+	&tls_dhe_rsa_with_aes_128_gcm_sha256,
 	&tls_rsa_with_aes_256_gcm_sha384,
 	&tls_rsa_with_aes_128_gcm_sha256,
 	&tls_ecdhe_rsa_with_3des_ede_cbc_sha,
+	&tls_dhe_rsa_with_3des_ede_cbc_sha,
 	&tls_rsa_with_3des_ede_cbc_sha,
 	&tls_ecdhe_rsa_with_rc4_128_sha,
 	&tls_rsa_with_rc4_128_sha,
Signature · 1 line
2.19.1
#25 lore raw

PATCH 17/17 tls: Switch state before handling Key Echange messages

In reply to Andrew Zaborowski

Where we handle a Client Key Exchange or a Server Key Exchange message and then switch to a new state, do the state change first so that if the tls_handle_*_key_exchange functions detect and error and move to an error state, tls->state is not overwritten afterwards. This seems simpler than adding a success/failure return value to these methods so that the callers can just stop execution on error.

---
 ell/tls.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
ell/tls.c +5 -5
index fc8ab41..7db1d9b 100644
@@ -2176,11 +2176,11 @@ static void tls_handle_handshake(struct l_tls *tls, int type,
 			break;
 		}
 
+		TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
+
 		tls->pending.cipher_suite->key_xchg->handle_server_key_exchange(
 								tls, buf, len);
 
-		TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
-
 		break;
 
 	case TLS_CERTIFICATE_REQUEST:
@@ -2247,9 +2247,6 @@ static void tls_handle_handshake(struct l_tls *tls, int type,
 			break;
 		}
 
-		tls->pending.cipher_suite->key_xchg->handle_client_key_exchange(
-								tls, buf, len);
-
 		/*
 		 * If we accepted a client Certificate message with a
 		 * certificate that has signing capability (TODO: check
@@ -2262,6 +2259,9 @@ static void tls_handle_handshake(struct l_tls *tls, int type,
 		else
 			TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC);
 
+		tls->pending.cipher_suite->key_xchg->handle_client_key_exchange(
+								tls, buf, len);
+
 		break;
 
 	case TLS_FINISHED:
Signature · 1 line
2.19.1
#16 lore raw

Re: [PATCH 13/17] key: Add l_key_validate_dh_payload

In reply to Andrew Zaborowski

Hi Andrew,

Quoted 2 lines · Andrew Zaborowski

Add a function to validate local private value payloads against a the group's prime before creating kernel keys out of them, and to

Do we have the same issue in iwd EAP-WSC?

Quoted 1 line

validate public values received from the peer.

Note that the kernel already performs almost the same check in dh_is_pubkey_valid

Might make more sense to create an API to generate a valid private key with the needed constraints. E.g. by twiddling the MSBs to 0 until the private key is less than the prime.

Quoted 6 lines
---
  ell/ell.sym |  1 +
  ell/key.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
  ell/key.h   |  3 +++
  3 files changed, 52 insertions(+)

Regards,
-Denis

#19 lore raw

Re: [PATCH 14/17] tls: DHE_RSA key exchange implementation client side

In reply to Andrew Zaborowski

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

Add a new key exchange definition with the callbacks needed to handle the client side of the DHE_RSA key exchange.

---
  ell/tls-suites.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 354 insertions(+)

<snip>

Quoted 39 lines
ell/tls-suites.c +39 -0
+static bool tls_create_dhe_keys(struct l_tls *tls)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+	if (!params)
+		return false;
+
+	if (params->prime_buf && !params->prime) {
+		params->prime = l_key_new(L_KEY_RAW, params->prime_buf,
+						params->prime_len);
+		if (!params->prime) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(prime) failed");
+			return false;
+		}
+	}
+
+	if (params->generator_buf && !params->generator) {
+		params->generator = l_key_new(L_KEY_RAW, params->generator_buf,
+						params->generator_len);
+		if (!params->generator) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(generator) failed");
+			return false;
+		}
+	}
+
+	if (params->private_buf && !params->private) {
+		params->private = l_key_new(L_KEY_RAW, params->private_buf,
+						params->private_len);
+		if (!params->private) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(private) failed");
+			return false;
+		}
+	}
+
+	if (params->public_buf && !params->public) {
+		params->public = l_key_new(L_KEY_RAW, params->public_buf,

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 782

Quoted 10 lines
ell/tls-suites.c +10 -0
+						params->public_len);
+		if (!params->public) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"l_key_new(public) failed");
+			return false;
+		}
+	}
+
+	return true;
+}

It seems like the intent of this function is to be called more than once, piecemeal creating the l_key objects. Why? That type of pattern isn't terribly readable / maintainable.

Quoted 13 lines
ell/tls-suites.c +13 -0
+
+static bool tls_generate_dhe_private(struct l_tls *tls, uint8_t *buf, size_t len)
+{
+	struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+	do {
+		unsigned int written = 0;
+
+		/* Leading zeros in the private value are fine */
+
+		do {
+			size_t chunk_len =
+				written + 256 <= len ? 256 : len - written;

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 805

Quoted 10 lines
ell/tls-suites.c +10 -0
+
+			if (!l_getrandom(buf + written, chunk_len)) {
+				TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+						"l_getrandom(buf, %zu) failed",
+						chunk_len);
+				return false;
+			}
+
+			written += chunk_len;
+		} while (written < len);

So where is the magic 256 byte thing is coming from? Is this due to the EINTR possibility? If so either use TFR() inside l_getrandom itself, or have l_getrandom perform this magic. This blocking logic doesn't belong here.

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 808

Quoted 3 lines
ell/tls-suites.c +3 -0
+	} while (!l_key_validate_dh_payload(buf, len,
+						params->prime_buf,
+						params->prime_len));

Generating a whole new random number is really a bad way of doing things. The entropy pool is a very limited resource, so don't abuse it.
A much easier way would be to simply set the most-significant-byte to
0. Or maybe even just the most-significant-bit. The prime numbers are usually always starting with 0xff, so fudging a single byte out of 192+ should be safe.

Quoted 4 lines
ell/tls-suites.c +4 -0
+
+	return true;
+}
+

<snip>

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 841

Quoted 7 lines
ell/tls-suites.c +7 -0
+static bool tls_get_server_dh_params_hash(struct l_tls *tls, uint8_t tls_id,
+						uint8_t *out, size_t *len,
+						enum l_checksum_type *type)
+{
+	unsigned int hash;
+	struct l_checksum *checksum;
+	uint8_t params[32 + TLS_DHE_MAX_SIZE * 3];

Have you considered making this a static buf inside tls_write_server_dh_params?

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 852

Quoted 11 lines
ell/tls-suites.c +11 -0
+	size_t params_len;
+	ssize_t hash_len, ret;
+
+	params_len = tls_write_server_dh_params(tls, params);
+
+	for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
+		if (tls_handshake_hash_data[hash].tls_id == tls_id)
+			break;
+
+	if (hash == __HANDSHAKE_HASH_COUNT)
+		return false;

Why not perform the search prior to tls_write_server_dh_params?

Quoted 22 lines
ell/tls-suites.c +22 -0
+
+	hash_len = tls_handshake_hash_data[hash].length;
+
+	checksum = l_checksum_new(tls_handshake_hash_data[hash].l_id);
+	if (!checksum)
+		return false;
+
+	l_checksum_update(checksum, tls->pending.client_random, 32);
+	l_checksum_update(checksum, tls->pending.server_random, 32);
+	l_checksum_update(checksum, params, params_len);
+	ret = l_checksum_get_digest(checksum, out, hash_len);
+	l_checksum_free(checksum);
+
+	if (ret != (ssize_t) hash_len)
+		return false;
+
+	if (len)
+		*len = hash_len;
+
+	if (type)
+		*type = tls_handshake_hash_data[hash].l_id;
+

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 884

Quoted 10 lines
ell/tls-suites.c +10 -0
+	return true;
+}
+
+static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	struct tls_dhe_params *params;
+
+	params = l_new(struct tls_dhe_params, 1);
+	tls->pending.key_xchg_params = params;

Why do this assignment here?

Quoted 55 lines
ell/tls-suites.c +55 -0
+
+	if (len < 2)
+		goto decode_error;
+
+	params->prime_len = l_get_be16(buf);
+	if (len < 2 + params->prime_len + 2)
+		goto decode_error;
+
+	params->prime_buf = l_memdup(buf + 2, params->prime_len);
+	buf += 2 + params->prime_len;
+	len -= 2 + params->prime_len;
+
+	params->generator_len = l_get_be16(buf);
+	if (len < 2 + params->generator_len + 2)
+		goto decode_error;
+
+	params->generator_buf = l_memdup(buf + 2, params->generator_len);;
+	buf += 2 + params->generator_len;
+	len -= 2 + params->generator_len;
+
+	params->public_len = l_get_be16(buf);
+	if (len < 2 + params->public_len)
+		goto decode_error;
+
+	params->public_buf = buf + 2;
+	buf += 2 + params->public_len;
+	len -= 2 + params->public_len;
+
+	/*
+	 * Validate the values received.  Without RFC 7919 we basically have
+	 * to blindly accept the provided prime value.  We have no way to
+	 * confirm that it's actually prime or that it's a "safe prime" or
+	 * that it forms a group without small sub-groups.  There's also no
+	 * way to whitelist all valid values.  But we do a basic sanity
+	 * check and require it to be 1024-bit or longer (see weakdh.org),
+	 * might need to move to 2048 bits actually.
+	 */
+
+	if (params->prime_len > TLS_DHE_MAX_SIZE ||
+			params->generator_len > params->prime_len) {
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Server DH prime modulus or generator "
+				"too long");
+		return;
+	}
+
+	if (params->prime_len < 128 || params->prime_buf[0] == 0x00 ||
+			!(params->prime_buf[params->prime_len - 1] & 1)) {
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Server DH prime modulus invalid");
+		return;
+	}
+
+	/*
+	 * RFC 7919 Section 3.0:

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 949

Quoted 10 lines
ell/tls-suites.c +10 -0
+	 * "the client MUST verify that dh_Ys is in the range
+	 * 1 < dh_Ys < dh_p - 1.  If dh_Ys is not in this range, the client
+	 * MUST terminate the connection with a fatal handshake_failure(40)
+	 * alert."
+	 */
+	if (params->public_len > params->prime_len ||
+			!l_key_validate_dh_payload(params->public_buf,
+							params->public_len,
+							params->prime_buf,
+							params->prime_len)) {

So can we rely on the kernel to do this part for us?

Quoted 10 lines
ell/tls-suites.c +10 -0
+		TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+				"Server DH public value invalid");
+		return;
+	}
+
+	if (!tls_create_dhe_keys(tls))
+		return;
+
+	if (!tls->pending.cipher_suite->key_xchg->verify(tls, buf, len,
+						tls_get_server_dh_params_hash))

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 969

Quoted 10 lines
ell/tls-suites.c +10 -0
+		return;
+
+	TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
+	return;
+
+decode_error:
+	TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
+			"ServerKeyExchange decode error");
+}
+

Regards,
-Denis

#17 lore raw

Re: [PATCH 13/17] key: Add l_key_validate_dh_payload

In reply to Denis Kenzior

Hi Denis,

Quoted 5 lines · Denis Kenzior
Quoted 2 lines · Andrew Zaborowski

Add a function to validate local private value payloads against a the group's prime before creating kernel keys out of them, and to

Do we have the same issue in iwd EAP-WSC?

I was going to send a patch for that later, we probably should validate the values but obviously the probability of the check failing in the first iteration is vert low. In TLS I guess it's higher because we may have to deal with a custom prime, but I didn't add this because of actually hitting a problem.

Quoted 5 lines
Quoted 2 lines

validate public values received from the peer.

Note that the kernel already performs almost the same check in dh_is_pubkey_valid

Oh true, this was apprently added in July. So as far as I understand that function is called in dh_compute so our compute_dh_secret should fail. But, in unit/test-key we have this "dh_degenerate" test case and it's passing, I have to look at when this check is supposed to trigger in the kernel, maybe we can still use it.

Quoted 3 lines

Might make more sense to create an API to generate a valid private key with the needed constraints.

Yep, may be a good idea, and this could hide the 256 byte limit for l_getrandom too (I was also thinking maybe l_getrandom itself should try to hide that detail)

Quoted 2 lines

E.g. by twiddling the MSBs to 0 until the
private key is less than the prime.

I think this is not normally done so as not to affect the probability distribution of the private values. But we probably should at least clear the top few bits of the random number that we know have to be 0 for the number to be lower than the prime. A TLS client can potentially a receive a prime from the server that has 01 in the first byte and then on average it'd need 256 iterations before it draws a value lower than the prime the naive way.

Best regards

#2 lore raw

Re: [PATCH 02/17] tls: Add Hello extension sending support

In reply to Andrew Zaborowski

Hi Andrew,

Quoted 8 lines · Andrew Zaborowski

Declare a struct for ServerHello/ClientHello extensions with write and handle callbacks, modify our ServerHello and ClientHello message builders to append the extensions to the messages.

---
  ell/tls-private.h |  18 +++++++++
  ell/tls.c         | 101 ++++++++++++++++++++++++++++++++++++++++++----
  2 files changed, 112 insertions(+), 7 deletions(-)

<snip>

ell/tls.c static void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf, · line 818

Quoted 7 lines
ell/tls.c +1 -1
@@ -812,11 +814,12 @@ static void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf,
 
  static bool tls_send_client_hello(struct l_tls *tls)
  {
-	uint8_t buf[128 + L_ARRAY_SIZE(tls_compression_pref) +
+	uint8_t buf[1024 + L_ARRAY_SIZE(tls_compression_pref) +
  			2 * L_ARRAY_SIZE(tls_cipher_suite_pref)];

So you're sort of depending on the buffer to be large enough, maybe not a great strategy long-term.

Quoted 118 lines
ell/tls.c +82 -5
  	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  	uint8_t *len_ptr;
-	int i;
+	unsigned int i;
+	uint8_t *extensions_len_ptr;
 
  	/* Fill in the Client Hello body */
 
@@ -832,7 +835,7 @@ static bool tls_send_client_hello(struct l_tls *tls)
  	len_ptr = ptr;
  	ptr += 2;
 
-	for (i = 0; i < (int) L_ARRAY_SIZE(tls_cipher_suite_pref); i++) {
+	for (i = 0; i < L_ARRAY_SIZE(tls_cipher_suite_pref); i++) {
  		if (!tls_cipher_suite_is_compatible(tls,
  						&tls_cipher_suite_pref[i],
  						NULL))
@@ -850,17 +853,58 @@ static bool tls_send_client_hello(struct l_tls *tls)
  	l_put_be16((ptr - len_ptr - 2), len_ptr);
  	*ptr++ = L_ARRAY_SIZE(tls_compression_pref);
 
-	for (i = 0; i < (int) L_ARRAY_SIZE(tls_compression_pref); i++)
+	for (i = 0; i < L_ARRAY_SIZE(tls_compression_pref); i++)
  		*ptr++ = tls_compression_pref[i].id;
 
+	extensions_len_ptr = ptr;
+	ptr += 2;
+
+	for (i = 0; tls_extensions[i].name; i++) {
+		const struct tls_hello_extension *extension =
+			&tls_extensions[i];
+		ssize_t ext_len;
+
+		/*
+		 * Note: could handle NULL client_write with non-NULL
+		 * server_handle or server_handle_absent as "server-oriented"
+		 * extension (7.4.1.4) and write empty extension_data and
+		 * simliarly require empty extension_data in
+		 * tls_handle_client_hello if client_handle NULL.
+		 */
+		if (!extension->client_write)
+			continue;
+
+		ext_len = extension->client_write(tls, ptr + 4,
+						buf + sizeof(buf) - (ptr + 4));
+		if (ext_len == -ENOMSG)
+			continue;
+
+		if (ext_len < 0) {
+			TLS_DEBUG("%s extension's client_write: %s",
+					extension->name, strerror(-ext_len));
+			return false;
+		}
+
+		l_put_be16(extension->id, ptr + 0);
+		l_put_be16(ext_len, ptr + 2);
+		ptr += 4 + ext_len;
+	}
+
+	if (ptr > extensions_len_ptr + 2)
+		l_put_be16(ptr - (extensions_len_ptr + 2), extensions_len_ptr);
+	else /* Skip the length if no extensions */
+		ptr = extensions_len_ptr;
+
  	tls_tx_handshake(tls, TLS_CLIENT_HELLO, buf, ptr - buf);
  	return true;
  }
 
-static void tls_send_server_hello(struct l_tls *tls)
+static bool tls_send_server_hello(struct l_tls *tls, struct l_queue *extensions)
  {
-	uint8_t buf[128];
+	uint8_t buf[1024];
  	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	uint8_t *extensions_len_ptr;
+	const struct l_queue_entry *entry;
 
  	/* Fill in the Server Hello body */
 
@@ -878,7 +922,49 @@ static void tls_send_server_hello(struct l_tls *tls)
 
  	*ptr++ = tls->pending.compression_method->id;
 
+	extensions_len_ptr = ptr;
+	ptr += 2;
+
+	for (entry = l_queue_get_entries(extensions); entry;
+			entry = entry->next) {
+		uint16_t ext_id = L_PTR_TO_UINT(entry->data);
+		const struct tls_hello_extension *extension = NULL;
+		unsigned int i;
+		ssize_t ext_len;
+
+		for (i = 0; tls_extensions[i].name; i++)
+			if (tls_extensions[i].id == ext_id) {
+				extension = &tls_extensions[i];
+				break;
+			}
+
+		if (!extension || !extension->server_write)
+			continue;
+
+		ext_len = extension->server_write(tls, ptr + 4,
+						buf + sizeof(buf) - (ptr + 4));
+		if (ext_len == -ENOMSG)
+			continue;
+
+		if (ext_len < 0) {
+			TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+					"%s extension's server_write: %s",
+					extension->name, strerror(-ext_len));
+			return false;
+		}
+
+		l_put_be16(ext_id, ptr + 0);
+		l_put_be16(ext_len, ptr + 2);
+		ptr += 4 + ext_len;
+	}

ell/tls.c static void tls_send_server_hello(struct l_tls *tls) · line 969

Quoted 10 lines
ell/tls.c +7 -0
+
+	if (ptr > extensions_len_ptr + 2)
+		l_put_be16(ptr - (extensions_len_ptr + 2), extensions_len_ptr);
+	else /* Skip the length if no extensions */
+		ptr = extensions_len_ptr;
+
  	tls_tx_handshake(tls, TLS_SERVER_HELLO, buf, ptr - buf);
+	return true;
  }
 

So both the server and the client versions are almost the same code. Signatures of client_write and server_write are also identical. Can these two functions be combined?

Also, don't you want the supported client extensions to also be dynamically configurable per tls object?

Regards,
-Denis

#8 lore raw

Re: [PATCH 06/17] tls: Allow user to set custom list of cipher suites

In reply to Andrew Zaborowski

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

TLS "security profiles" define which minimum cipher suites and other parameter values are allowed in a specific usage scenario for TLS, for example WPA3 in theory requires a specific security profile for its TLS-based EAP method. This will also allow the unit tests to test individual cipher suites.

---
  ell/tls-private.h |   5 +
  ell/tls.c         | 260 +++++++++++++++++++++++++++++++---------------
  2 files changed, 181 insertions(+), 84 deletions(-)

Looks good to me.

Regards,
-Denis

#20 lore raw

Re: [PATCH 14/17] tls: DHE_RSA key exchange implementation client side

In reply to Denis Kenzior

Hi Denis,

Quoted 46 lines · Denis Kenzior
Quoted 45 lines · Andrew Zaborowski
ell/tls-suites.c +45 -0
+static bool tls_create_dhe_keys(struct l_tls *tls)
+{
+     struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+     if (!params)
+             return false;
+
+     if (params->prime_buf && !params->prime) {
+             params->prime = l_key_new(L_KEY_RAW, params->prime_buf,
+                                             params->prime_len);
+             if (!params->prime) {
+                     TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+                                     "l_key_new(prime) failed");
+                     return false;
+             }
+     }
+
+     if (params->generator_buf && !params->generator) {
+             params->generator = l_key_new(L_KEY_RAW, params->generator_buf,
+                                             params->generator_len);
+             if (!params->generator) {
+                     TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+                                     "l_key_new(generator) failed");
+                     return false;
+             }
+     }
+
+     if (params->private_buf && !params->private) {
+             params->private = l_key_new(L_KEY_RAW, params->private_buf,
+                                             params->private_len);
+             if (!params->private) {
+                     TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+                                     "l_key_new(private) failed");
+                     return false;
+             }
+     }
+
+     if (params->public_buf && !params->public) {
+             params->public = l_key_new(L_KEY_RAW, params->public_buf,
+                                             params->public_len);
+             if (!params->public) {
+                     TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+                                     "l_key_new(public) failed");
+                     return false;
+             }
Quoted 8 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 782

Quoted 4 lines
ell/tls-suites.c +4 -0
+     }
+
+     return true;
+}

It seems like the intent of this function is to be called more than once, piecemeal creating the l_key objects. Why? That type of pattern isn't terribly readable / maintainable.

I guess the intent is to simplify error handling in the callers. I can perhaps split this in 4 separate functions, to avoid inlining all the l_key_new and TLS_DISCONNECT calls.

Quoted 18 lines
Quoted 18 lines
ell/tls-suites.c +17 -0
+
+static bool tls_generate_dhe_private(struct l_tls *tls, uint8_t *buf, size_t len)
+{
+     struct tls_dhe_params *params = tls->pending.key_xchg_params;
+
+     do {
+             unsigned int written = 0;
+
+             /* Leading zeros in the private value are fine */
+
+             do {
+                     size_t chunk_len =
+                             written + 256 <= len ? 256 : len - written;
+
+                     if (!l_getrandom(buf + written, chunk_len)) {
+                             TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+                                             "l_getrandom(buf, %zu) failed",
Quoted 8 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 805

Quoted 6 lines
ell/tls-suites.c +6 -0
+                                             chunk_len);
+                             return false;
+                     }
+
+                     written += chunk_len;
+             } while (written < len);

So where is the magic 256 byte thing is coming from?

It's to workaround this check in random.c:

62: if (len > 256)
63: return false;

I don't know exactly why it's needed but I'll play with it and see if we can remove/work around this limit.

Quoted 5 lines

Is this due to the
EINTR possibility? If so either use TFR() inside l_getrandom itself, or have l_getrandom perform this magic. This blocking logic doesn't belong here.

Quoted 8 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 808

Quoted 3 lines
ell/tls-suites.c +3 -0
+     } while (!l_key_validate_dh_payload(buf, len,
+                                             params->prime_buf,
+                                             params->prime_len));

Generating a whole new random number is really a bad way of doing things. The entropy pool is a very limited resource, so don't abuse it.
A much easier way would be to simply set the most-significant-byte to
0.

It's a pattern we use in the ecdh code (where the group order is only ~1/2 of the number space -- can improved by switching from the "Black box" method to the "Deterministic" method iirc) and in iwd so we'd probably need to change it everywhere. But it seems that every way to "fix" the number instead of generating a new one affects the distribution making the algorithm weaker. If that's ok then yes, we can simply force the top bits of the private value do "0" (down to the first "1" bit of the prime).

Quoted 3 lines

Or maybe even just the most-significant-bit. The prime numbers are usually always starting with 0xff, so fudging a single byte out of 192+ should be safe.

Not in TLS, most of the https servers I test against use custom generated prime numbers, I guess the openssl or gnutls package generates a random (likely-to-be) prime at install.

Quoted 8 lines
Quoted 5 lines
ell/tls-suites.c +4 -0
+
+     return true;
+}
+

<snip>

Quoted 10 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 841

Quoted 7 lines
ell/tls-suites.c +7 -0
+static bool tls_get_server_dh_params_hash(struct l_tls *tls, uint8_t tls_id,
+                                             uint8_t *out, size_t *len,
+                                             enum l_checksum_type *type)
+{
+     unsigned int hash;
+     struct l_checksum *checksum;
+     uint8_t params[32 + TLS_DHE_MAX_SIZE * 3];

Have you considered making this a static buf inside tls_write_server_dh_params?

I guess I can do that.

Quoted 14 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 852

Quoted 12 lines
ell/tls-suites.c +11 -0
+     size_t params_len;
+     ssize_t hash_len, ret;
+
+     params_len = tls_write_server_dh_params(tls, params);
+
+     for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
+             if (tls_handshake_hash_data[hash].tls_id == tls_id)
+                     break;
+
+     if (hash == __HANDSHAKE_HASH_COUNT)
+             return false;

Why not perform the search prior to tls_write_server_dh_params?

Ok.

Quoted 27 lines
Quoted 27 lines
ell/tls-suites.c +26 -0
+
+     hash_len = tls_handshake_hash_data[hash].length;
+
+     checksum = l_checksum_new(tls_handshake_hash_data[hash].l_id);
+     if (!checksum)
+             return false;
+
+     l_checksum_update(checksum, tls->pending.client_random, 32);
+     l_checksum_update(checksum, tls->pending.server_random, 32);
+     l_checksum_update(checksum, params, params_len);
+     ret = l_checksum_get_digest(checksum, out, hash_len);
+     l_checksum_free(checksum);
+
+     if (ret != (ssize_t) hash_len)
+             return false;
+
+     if (len)
+             *len = hash_len;
+
+     if (type)
+             *type = tls_handshake_hash_data[hash].l_id;
+
+     return true;
+}
+
+static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
Quoted 8 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 884

Quoted 6 lines
ell/tls-suites.c +6 -0
+                                             const uint8_t *buf, size_t len)
+{
+     struct tls_dhe_params *params;
+
+     params = l_new(struct tls_dhe_params, 1);
+     tls->pending.key_xchg_params = params;

Why do this assignment here?

The reason is so that in case of error tls_reset_handshake will free this and we don't have to worry about it, I can add a comment.

Quoted 60 lines
Quoted 60 lines
ell/tls-suites.c +59 -0
+
+     if (len < 2)
+             goto decode_error;
+
+     params->prime_len = l_get_be16(buf);
+     if (len < 2 + params->prime_len + 2)
+             goto decode_error;
+
+     params->prime_buf = l_memdup(buf + 2, params->prime_len);
+     buf += 2 + params->prime_len;
+     len -= 2 + params->prime_len;
+
+     params->generator_len = l_get_be16(buf);
+     if (len < 2 + params->generator_len + 2)
+             goto decode_error;
+
+     params->generator_buf = l_memdup(buf + 2, params->generator_len);;
+     buf += 2 + params->generator_len;
+     len -= 2 + params->generator_len;
+
+     params->public_len = l_get_be16(buf);
+     if (len < 2 + params->public_len)
+             goto decode_error;
+
+     params->public_buf = buf + 2;
+     buf += 2 + params->public_len;
+     len -= 2 + params->public_len;
+
+     /*
+      * Validate the values received.  Without RFC 7919 we basically have
+      * to blindly accept the provided prime value.  We have no way to
+      * confirm that it's actually prime or that it's a "safe prime" or
+      * that it forms a group without small sub-groups.  There's also no
+      * way to whitelist all valid values.  But we do a basic sanity
+      * check and require it to be 1024-bit or longer (see weakdh.org),
+      * might need to move to 2048 bits actually.
+      */
+
+     if (params->prime_len > TLS_DHE_MAX_SIZE ||
+                     params->generator_len > params->prime_len) {
+             TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+                             "Server DH prime modulus or generator "
+                             "too long");
+             return;
+     }
+
+     if (params->prime_len < 128 || params->prime_buf[0] == 0x00 ||
+                     !(params->prime_buf[params->prime_len - 1] & 1)) {
+             TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
+                             "Server DH prime modulus invalid");
+             return;
+     }
+
+     /*
+      * RFC 7919 Section 3.0:
+      * "the client MUST verify that dh_Ys is in the range
+      * 1 < dh_Ys < dh_p - 1.  If dh_Ys is not in this range, the client
+      * MUST terminate the connection with a fatal handshake_failure(40)
+      * alert."
Quoted 8 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 949

Quoted 6 lines
ell/tls-suites.c +6 -0
+      */
+     if (params->public_len > params->prime_len ||
+                     !l_key_validate_dh_payload(params->public_buf,
+                                                     params->public_len,
+                                                     params->prime_buf,
+                                                     params->prime_len)) {

So can we rely on the kernel to do this part for us?

Apparently we can't without modifying security/keys/dh.c and our keys code. The keyctl operation is too smart and assumes generate_public_key and generate_shared_secret are the same operation, so when crypto/dh.c receives the parameters it always thinks it's dealing with a generator and the private value, instead of private and public values. So it never runs the public value validation. dh_is_pubkey_valid also has one minor unimplemented feature.

Best regards

#21 lore raw

Re: [PATCH 14/17] tls: DHE_RSA key exchange implementation client side

Hi Andrew,

Quoted 8 lines
Quoted 3 lines

It seems like the intent of this function is to be called more than once, piecemeal creating the l_key objects. Why? That type of pattern isn't terribly readable / maintainable.

I guess the intent is to simplify error handling in the callers. I can perhaps split this in 4 separate functions, to avoid inlining all the l_key_new and TLS_DISCONNECT calls.

By my reading it seems like this is only needs to be called once for the client side. I can see how this is more useful for the server side.

Quoted 9 lines
Quoted 1 line

So where is the magic 256 byte thing is coming from?

It's to workaround this check in random.c:

62: if (len > 256)
63: return false;

I don't know exactly why it's needed but I'll play with it and see if we can remove/work around this limit.

So that check is there due to EINTR. getrandom guarantees to succeed for len up to 256. After that EINTR is a possibility. We never needed random buffers bigger than 256, so it was a lazy fix.

Quoted 8 lines
Quoted 4 lines

Generating a whole new random number is really a bad way of doing things. The entropy pool is a very limited resource, so don't abuse it.
A much easier way would be to simply set the most-significant-byte to
0.

It's a pattern we use in the ecdh code (where the group order is only ~1/2 of the number space -- can improved by switching from the "Black box" method to the "Deterministic" method iirc) and in iwd so we'd

Yeah we might need to fix that, but there might not be an easy way as opposed to here.

Quoted 5 lines

probably need to change it everywhere. But it seems that every way to "fix" the number instead of generating a new one affects the distribution making the algorithm weaker. If that's ok then yes, we can simply force the top bits of the private value do "0" (down to the first "1" bit of the prime).

This would be fine with all DH groups from RFC 3526. If TLS uses a different set of primes, then setting the upper MSB bits to 0 might be okay. If you're worried about this, then perhaps simply re-generating the upper N MSB bytes is another solution. Might want to check what other implementations do.

Quoted 10 lines
Quoted 9 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 884

Quoted 7 lines
ell/tls-suites.c +7 -0
+static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
+                                             const uint8_t *buf, size_t len)
+{
+     struct tls_dhe_params *params;
+
+     params = l_new(struct tls_dhe_params, 1);
+     tls->pending.key_xchg_params = params;

Why do this assignment here?

Quoted 2 lines

The reason is so that in case of error tls_reset_handshake will free this and we don't have to worry about it, I can add a comment.

Gah, that's really not obvious. A comment is needed, but it might be better to have decode_error label take care of this instead.

Quoted 15 lines
Quoted 13 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 949

Quoted 12 lines
ell/tls-suites.c +12 -0
+     /*
+      * RFC 7919 Section 3.0:
+      * "the client MUST verify that dh_Ys is in the range
+      * 1 < dh_Ys < dh_p - 1.  If dh_Ys is not in this range, the client
+      * MUST terminate the connection with a fatal handshake_failure(40)
+      * alert."
+      */
+     if (params->public_len > params->prime_len ||
+                     !l_key_validate_dh_payload(params->public_buf,
+                                                     params->public_len,
+                                                     params->prime_buf,
+                                                     params->prime_len)) {
Quoted 1 line

So can we rely on the kernel to do this part for us?

Quoted 7 lines

Apparently we can't without modifying security/keys/dh.c and our keys code. The keyctl operation is too smart and assumes generate_public_key and generate_shared_secret are the same operation, so when crypto/dh.c receives the parameters it always thinks it's dealing with a generator and the private value, instead of private and public values. So it never runs the public value validation. dh_is_pubkey_valid also has one minor unimplemented feature.

That's a bug in the kernel then. We should fix that.

Regards,
-Denis

#22 lore raw

Re: [PATCH 14/17] tls: DHE_RSA key exchange implementation client side

Quoted 18 lines · Denis Kenzior
Quoted 8 lines
Quoted 4 lines

Generating a whole new random number is really a bad way of doing things. The entropy pool is a very limited resource, so don't abuse it.
A much easier way would be to simply set the most-significant-byte to
0.

It's a pattern we use in the ecdh code (where the group order is only ~1/2 of the number space -- can improved by switching from the "Black box" method to the "Deterministic" method iirc) and in iwd so we'd

Yeah we might need to fix that, but there might not be an easy way as opposed to here.

Quoted 5 lines

probably need to change it everywhere. But it seems that every way to "fix" the number instead of generating a new one affects the distribution making the algorithm weaker. If that's ok then yes, we can simply force the top bits of the private value do "0" (down to the first "1" bit of the prime).

Quoted 3 lines

This would be fine with all DH groups from RFC 3526. If TLS uses a different set of primes, then setting the upper MSB bits to 0 might be okay.

PKCS #3 also mentions generating a priv key between 2^(n-1) and (2^n)-1, meaning forcing an MSB to 1, which accounts for both the min and max checks so I think I'm going to do that if we don't care about uniform distribution.

Quoted 2 lines

If you're worried about this, then perhaps simply re-generating the upper N MSB bytes is another solution.

Unfortunately this still would skew the distribution a little.

Quoted 42 lines

Might want to check what
other implementations do.

Quoted 10 lines
Quoted 9 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 884

Quoted 7 lines
ell/tls-suites.c +7 -0
+static void tls_handle_dhe_server_key_xchg(struct l_tls *tls,
+                                             const uint8_t *buf, size_t len)
+{
+     struct tls_dhe_params *params;
+
+     params = l_new(struct tls_dhe_params, 1);
+     tls->pending.key_xchg_params = params;

Why do this assignment here?

Quoted 2 lines

The reason is so that in case of error tls_reset_handshake will free this and we don't have to worry about it, I can add a comment.

Gah, that's really not obvious. A comment is needed, but it might be better to have decode_error label take care of this instead.

Quoted 15 lines
Quoted 13 lines

ell/tls-suites.c static struct tls_key_exchange_algorithm tls_ecdhe_rsa = { · line 949

Quoted 12 lines
ell/tls-suites.c +12 -0
+     /*
+      * RFC 7919 Section 3.0:
+      * "the client MUST verify that dh_Ys is in the range
+      * 1 < dh_Ys < dh_p - 1.  If dh_Ys is not in this range, the client
+      * MUST terminate the connection with a fatal handshake_failure(40)
+      * alert."
+      */
+     if (params->public_len > params->prime_len ||
+                     !l_key_validate_dh_payload(params->public_buf,
+                                                     params->public_len,
+                                                     params->prime_buf,
+                                                     params->prime_len)) {
Quoted 1 line

So can we rely on the kernel to do this part for us?

Quoted 7 lines

Apparently we can't without modifying security/keys/dh.c and our keys code. The keyctl operation is too smart and assumes generate_public_key and generate_shared_secret are the same operation, so when crypto/dh.c receives the parameters it always thinks it's dealing with a generator and the private value, instead of private and public values. So it never runs the public value validation. dh_is_pubkey_valid also has one minor unimplemented feature.

Quoted 1 line

That's a bug in the kernel then. We should fix that.

As far as I understand we'd also need to fix the API then because keyctl has no way to know which operation we're requesting. I.e. split KEYCTL_DH_COMPUTE into two operations or add a field to struct keyctl_dh_params. We could also force the check for both private and public values but others might argue the requirements for private values may be less strict (in RFC7919 they're the same though, iirc), or the min and max may have been checked already.

Best regards

#3 lore raw

Re: [PATCH 02/17] tls: Add Hello extension sending support

In reply to Denis Kenzior
Quoted 9 lines · Denis Kenzior

ell/tls.c static void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf, · line 818

Quoted 5 lines · Andrew Zaborowski
  static bool tls_send_client_hello(struct l_tls *tls)
  {
ell/tls.c +1 -1
-     uint8_t buf[128 + L_ARRAY_SIZE(tls_compression_pref) +
+     uint8_t buf[1024 + L_ARRAY_SIZE(tls_compression_pref) +
                      2 * L_ARRAY_SIZE(tls_cipher_suite_pref)];

So you're sort of depending on the buffer to be large enough, maybe not a great strategy long-term.

True, but there's always a fixed maximum size we need, only it's tricky to calculate that size dynamically because we'd need to complicate the API.

Quoted 125 lines
Quoted 125 lines
      uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
      uint8_t *len_ptr;
ell/tls.c +88 -5
-     int i;
+     unsigned int i;
+     uint8_t *extensions_len_ptr;
 
      /* Fill in the Client Hello body */
@@ -832,7 +835,7 @@ static bool tls_send_client_hello(struct l_tls *tls)
      len_ptr = ptr;
      ptr += 2;
 
-     for (i = 0; i < (int) L_ARRAY_SIZE(tls_cipher_suite_pref); i++) {
+     for (i = 0; i < L_ARRAY_SIZE(tls_cipher_suite_pref); i++) {
              if (!tls_cipher_suite_is_compatible(tls,
                                              &tls_cipher_suite_pref[i],
                                              NULL))
@@ -850,17 +853,58 @@ static bool tls_send_client_hello(struct l_tls *tls)
      l_put_be16((ptr - len_ptr - 2), len_ptr);
      *ptr++ = L_ARRAY_SIZE(tls_compression_pref);
 
-     for (i = 0; i < (int) L_ARRAY_SIZE(tls_compression_pref); i++)
+     for (i = 0; i < L_ARRAY_SIZE(tls_compression_pref); i++)
              *ptr++ = tls_compression_pref[i].id;
 
+     extensions_len_ptr = ptr;
+     ptr += 2;
+
+     for (i = 0; tls_extensions[i].name; i++) {
+             const struct tls_hello_extension *extension =
+                     &tls_extensions[i];
+             ssize_t ext_len;
+
+             /*
+              * Note: could handle NULL client_write with non-NULL
+              * server_handle or server_handle_absent as "server-oriented"
+              * extension (7.4.1.4) and write empty extension_data and
+              * simliarly require empty extension_data in
+              * tls_handle_client_hello if client_handle NULL.
+              */
+             if (!extension->client_write)
+                     continue;
+
+             ext_len = extension->client_write(tls, ptr + 4,
+                                             buf + sizeof(buf) - (ptr + 4));
+             if (ext_len == -ENOMSG)
+                     continue;
+
+             if (ext_len < 0) {
+                     TLS_DEBUG("%s extension's client_write: %s",
+                                     extension->name, strerror(-ext_len));
+                     return false;
+             }
+
+             l_put_be16(extension->id, ptr + 0);
+             l_put_be16(ext_len, ptr + 2);
+             ptr += 4 + ext_len;
+     }
+
+     if (ptr > extensions_len_ptr + 2)
+             l_put_be16(ptr - (extensions_len_ptr + 2), extensions_len_ptr);
+     else /* Skip the length if no extensions */
+             ptr = extensions_len_ptr;
+
      tls_tx_handshake(tls, TLS_CLIENT_HELLO, buf, ptr - buf);
      return true;
  }
 
-static void tls_send_server_hello(struct l_tls *tls)
+static bool tls_send_server_hello(struct l_tls *tls, struct l_queue *extensions)
  {
-     uint8_t buf[128];
+     uint8_t buf[1024];
      uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+     uint8_t *extensions_len_ptr;
+     const struct l_queue_entry *entry;
 
      /* Fill in the Server Hello body */
 
@@ -878,7 +922,49 @@ static void tls_send_server_hello(struct l_tls *tls)
 
      *ptr++ = tls->pending.compression_method->id;
 
+     extensions_len_ptr = ptr;
+     ptr += 2;
+
+     for (entry = l_queue_get_entries(extensions); entry;
+                     entry = entry->next) {
+             uint16_t ext_id = L_PTR_TO_UINT(entry->data);
+             const struct tls_hello_extension *extension = NULL;
+             unsigned int i;
+             ssize_t ext_len;
+
+             for (i = 0; tls_extensions[i].name; i++)
+                     if (tls_extensions[i].id == ext_id) {
+                             extension = &tls_extensions[i];
+                             break;
+                     }
+
+             if (!extension || !extension->server_write)
+                     continue;
+
+             ext_len = extension->server_write(tls, ptr + 4,
+                                             buf + sizeof(buf) - (ptr + 4));
+             if (ext_len == -ENOMSG)
+                     continue;
+
+             if (ext_len < 0) {
+                     TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
+                                     "%s extension's server_write: %s",
+                                     extension->name, strerror(-ext_len));
+                     return false;
+             }
+
+             l_put_be16(ext_id, ptr + 0);
+             l_put_be16(ext_len, ptr + 2);
+             ptr += 4 + ext_len;
+     }
+
+     if (ptr > extensions_len_ptr + 2)
+             l_put_be16(ptr - (extensions_len_ptr + 2), extensions_len_ptr);
+     else /* Skip the length if no extensions */
+             ptr = extensions_len_ptr;
+
Quoted 8 lines

ell/tls.c static void tls_send_server_hello(struct l_tls *tls) · line 969

Quoted 4 lines
ell/tls.c +1 -0
      tls_tx_handshake(tls, TLS_SERVER_HELLO, buf, ptr - buf);
+     return true;
  }
 

So both the server and the client versions are almost the same code. Signatures of client_write and server_write are also identical. Can these two functions be combined?

Ok.

Quoted 3 lines

Also, don't you want the supported client extensions to also be dynamically configurable per tls object?

We can add this at some point but I'm not sure if there's any use for disabling extensions (probably less than for disabling cipher suites which we decided to have no public api for)

Best regards